/* VPN Monitor — stile dashboard interna.
   Token colore basati sulla palette di riferimento validata (stato: good/warning/critical
   sempre accompagnati da testo/pallino, mai colore da solo). */

:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);

  --good: #0ca30c;
  --good-text: #006300;
  --good-bg: #e8f5e8;
  --warn: #fab219;
  --warn-text: #7a5200;
  --warn-bg: #fdf3dc;
  --danger: #d03b3b;
  --danger-text: #8f1f1f;
  --danger-bg: #fbe9e9;
  --muted-bg: #f0efec;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: #1c5cab; text-decoration: none; }
a:hover { text-decoration: underline; }

.mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.92em; }
.muted { color: var(--muted); }

/* --- Barra superiore --- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
  position: sticky; top: 0; z-index: 10;
}
.topbar-inner {
  max-width: 1240px; margin: 0 auto; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between; height: 56px;
}
.brand {
  font-weight: 700; font-size: 17px; color: var(--ink);
  display: flex; align-items: center; gap: 8px;
}
.brand:hover { text-decoration: none; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--good); display: inline-block; }
.brand-company {
  font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--muted); border-left: 1px solid var(--hairline);
  padding-left: 10px; margin-left: 4px;
}
.nav { display: flex; gap: 4px; }
.nav a {
  color: var(--ink-2); padding: 6px 14px; border-radius: 999px; font-weight: 500;
}
.nav a:hover { background: var(--muted-bg); text-decoration: none; }
.nav a.active { background: var(--ink); color: #fff; }

/* --- Layout --- */
.container { max-width: 1240px; margin: 0 auto; padding: 28px 24px 48px; }
.footer {
  max-width: 1240px; margin: 0 auto; padding: 16px 24px 32px;
  color: var(--muted); font-size: 13px;
}

.page-head { margin-bottom: 20px; }
.page-head h1 {
  margin: 0 0 4px; font-size: 24px; font-weight: 700;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.subtitle { margin: 0; color: var(--ink-2); }
.breadcrumb { margin: 0 0 2px; font-size: 13px; color: var(--muted); }

/* --- Card riepilogative --- */
.cards {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 14px; margin-bottom: 24px;
}
.cards-4 { grid-template-columns: repeat(4, 1fr); }
.card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-left: 4px solid var(--hairline);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  min-width: 0;
}
.card-ok { border-left-color: var(--good); }
.card-warn { border-left-color: var(--warn); }
.card-danger { border-left-color: var(--danger); }
.card-muted { border-left-color: var(--muted); }

.card-label {
  font-size: 12px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--muted); margin-bottom: 6px;
}
.card-value { font-size: 28px; font-weight: 700; line-height: 1.15; }
.card-value-sm { font-size: 19px; }
.card-total { font-size: 16px; font-weight: 500; color: var(--muted); }
.card-note { font-size: 13px; color: var(--ink-2); margin-top: 4px; overflow-wrap: anywhere; }

/* --- Pannelli e tabelle --- */
.panel {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
  overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--hairline);
}
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 700; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left; font-size: 12px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--muted);
  padding: 10px 14px; border-bottom: 1px solid var(--hairline);
  white-space: nowrap;
}
td {
  padding: 10px 14px; border-bottom: 1px solid var(--hairline);
  vertical-align: top; font-variant-numeric: tabular-nums;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f5f4f1; }
.row-danger { background: #fdf4f4; }
.row-danger:hover { background: #f9e9e9; }
.badge { border: 1px solid rgba(11, 11, 11, 0.06); }
.cell-strong { font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 28px 14px; }

/* --- Badge di stato: pallino colorato + testo in inchiostro, mai colore da solo --- */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600; white-space: nowrap;
}
.badge-lg { font-size: 14px; padding: 4px 14px; }
.badge-ok { background: var(--good-bg); color: var(--good-text); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge-danger { background: var(--danger-bg); color: var(--danger-text); }
.badge-muted { background: var(--muted-bg); color: var(--ink-2); }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.dot-ok { background: var(--good); }
.dot-warn { background: var(--warn); }
.dot-danger { background: var(--danger); }
.dot-muted { background: var(--muted); }

/* --- Filtri, bottoni, paginazione --- */
.filters {
  display: flex; align-items: end; gap: 14px; flex-wrap: wrap;
  padding: 14px 18px; border-bottom: 1px solid var(--hairline);
}
.filters label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.filters select {
  font: inherit; padding: 7px 10px; min-width: 180px;
  border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--surface); color: var(--ink);
}
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  background: var(--ink); color: #fff;
  border: none; border-radius: 8px; padding: 8px 18px;
}
.btn:hover { background: #333; }
.btn-link { font-weight: 600; white-space: nowrap; }
.btn-quiet { background: var(--muted-bg); color: var(--ink-2); }
.btn-quiet:hover { background: #e6e5e1; }

/* --- Gestione client: lista di card con interruttore --- */
.client-list { display: flex; flex-direction: column; gap: 12px; }
.client-empty {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.client-card {
  background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; transition: background 0.4s ease;
}
.client-card.flash { background: var(--good-bg); }
.client-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px 18px; flex-wrap: wrap;
}
.client-id { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; }
.client-cn { font-weight: 700; font-size: 15px; overflow-wrap: anywhere; }

/* Interruttore (checkbox stilizzato) */
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 44px; height: 26px; border-radius: 999px; background: var(--muted);
  position: relative; flex: none; transition: background 0.2s ease;
}
.switch-thumb {
  position: absolute; top: 3px; left: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(11, 11, 11, 0.35);
  transition: transform 0.2s ease;
}
.switch input:checked + .switch-track { background: var(--good); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { outline: 2px solid #1c5cab; outline-offset: 2px; }
.switch input:disabled + .switch-track { opacity: 0.6; }
.switch-label { font-weight: 600; white-space: nowrap; }
.switch-label .on-label { display: none; color: var(--good-text); }
.switch-label .off-label { display: inline; color: var(--muted); }
.switch input:checked ~ .switch-label .on-label { display: inline; }
.switch input:checked ~ .switch-label .off-label { display: none; }

/* Configurazione: visibile solo quando il client è monitorato */
.config-form { display: none; }
.client-card.is-monitored .config-form {
  display: flex; align-items: end; gap: 12px 14px; flex-wrap: wrap;
  margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--hairline);
}
.config-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.config-form input {
  font: inherit; padding: 8px 10px;
  border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--surface); color: var(--ink);
}
.config-form input[name="name"] { min-width: 200px; }
.config-form input[name="alert_email"] { min-width: 220px; }
.config-form input[name="threshold_minutes"] { width: 90px; }
.save-feedback { font-weight: 600; align-self: center; }
.save-feedback.ok { color: var(--good-text); }
.save-feedback.err { color: var(--danger-text); }

/* --- Login --- */
.login-wrap { display: flex; justify-content: center; padding-top: 8vh; }
.login-panel { width: 100%; max-width: 380px; padding-bottom: 6px; }
.login-head { padding: 18px 18px 0; margin-bottom: 0; }
.login-error { padding: 12px 18px 0; margin: 0; }
.login-form {
  display: flex; flex-direction: column; gap: 14px; padding: 16px 18px;
}
.login-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.login-form input {
  font: inherit; padding: 8px 10px;
  border: 1px solid var(--hairline); border-radius: 8px;
  background: var(--surface); color: var(--ink);
}
.login-form .btn { margin-top: 4px; }

.pagination {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px;
}

/* --- Tab bar mobile (nascosta su desktop) --- */
.tabbar { display: none; }

/* --- Striscia di stato: verdetto a colpo d'occhio, solo mobile (pallino + testo) --- */
.status-strip { display: none; }
.status-strip-ok { background: var(--good-bg); color: var(--good-text); }
.status-strip-warn { background: var(--warn-bg); color: var(--warn-text); }
.status-strip-danger { background: var(--danger-bg); color: var(--danger-text); }
.status-strip-muted { background: var(--muted-bg); color: var(--ink-2); }

/* --- Banner "nuova versione disponibile" (creato da app.js) --- */
.update-banner {
  position: fixed; left: 12px; right: 12px; bottom: 12px; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--warn-bg); color: var(--warn-text);
  border: 1px solid var(--warn); border-radius: var(--radius);
  padding: 8px 8px 8px 14px; box-shadow: 0 2px 8px rgba(11, 11, 11, 0.14);
  font-weight: 600; max-width: 480px; margin: 0 auto;
}
.update-banner .btn { min-height: 44px; flex: none; }

/* --- Responsive --- */
@media (max-width: 1080px) {
  .cards, .cards-4 { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 760px) {
  .cards, .cards-4 { grid-template-columns: repeat(2, 1fr); }
  .container { padding: 20px 14px 40px; }
  .topbar-inner { padding: 0 14px; }

  /* Striscia di stato: verdetto immediato in cima */
  .status-strip {
    display: flex; align-items: center; gap: 8px;
    padding: 12px 16px; margin-bottom: 14px; min-height: 44px;
    border-radius: var(--radius); font-weight: 700;
  }
  /* "Ultimo alert" sempre a piena larghezza sotto la griglia 2×2 */
  .cards .card-alert { grid-column: 1 / -1; }
  /* Con tutto a posto la striscia basta: nascondi le 4 card numeriche */
  .cards.is-clear .card:not(.card-alert) { display: none; }

  /* Contenuto mai coperto dalla tab bar fissa */
  body { padding-bottom: calc(60px + env(safe-area-inset-bottom)); }

  /* Topbar mobile: solo brand + Esci; le pagine stanno nella tab bar */
  .nav a.nav-page { display: none; }

  /* Tab bar in basso, stile app */
  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--hairline);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    gap: 2px; padding: 8px 4px 6px; min-height: 48px;
    font-size: 11px; font-weight: 500; color: var(--ink-2);
  }
  .tabbar a.active { color: var(--ink); font-weight: 700; }
  .tabbar a:hover { text-decoration: none; }
  .update-banner { bottom: calc(64px + env(safe-area-inset-bottom)); }

  /* Gestione client: configurazione a colonna piena larghezza */
  .client-card.is-monitored .config-form { flex-direction: column; align-items: stretch; }
  .config-form label { width: 100%; }
  .config-form input,
  .config-form input[name="name"],
  .config-form input[name="alert_email"],
  .config-form input[name="threshold_minutes"] { width: 100%; min-width: 0; }

  /* Tabelle → card impilate: le etichette vengono da data-label sui td.
     tr in flex column così i campi si riordinano con `order` senza toccare
     l'ordine delle colonne desktop. */
  .table-wrap { overflow-x: visible; }
  .table-wrap thead { display: none; }
  .table-wrap table, .table-wrap tbody, .table-wrap tfoot { display: block; }
  .table-wrap tr {
    display: flex; flex-direction: column;
    padding: 12px 16px; border-bottom: 1px solid var(--hairline);
  }
  .table-wrap tbody tr:hover { background: transparent; }
  .table-wrap tbody tr.row-danger, .table-wrap tbody tr.row-danger:hover { background: #fdf4f4; }
  .table-wrap td {
    display: flex; justify-content: space-between; align-items: baseline;
    gap: 12px; padding: 3px 0; border-bottom: none;
  }
  .table-wrap td:empty { display: none; }
  .table-wrap td[data-label]::before {
    content: attr(data-label);
    font-size: 11px; font-weight: 600; letter-spacing: 0.04em;
    text-transform: uppercase; color: var(--muted); flex: none;
  }
  .table-wrap td.stack-primary { font-size: 16px; font-weight: 600; order: -3; padding-bottom: 2px; }
  .table-wrap td.stack-badge { order: -2; justify-content: flex-start; padding-bottom: 6px; }
  .table-wrap td.stack-first { order: -1; }
  .table-wrap td.stack-full { flex-direction: column; align-items: stretch; gap: 2px; }
  .table-wrap td.stack-hide { display: none; }
  .table-wrap td.stack-action { justify-content: flex-end; padding-top: 6px; }
  .table-wrap td.empty { display: block; text-align: center; }
  .table-wrap tfoot tr { border-top: 2px solid var(--hairline); border-bottom: none; }

  /* Filtri e form: colonna piena larghezza, niente zoom iOS (font 16px),
     touch target comodi */
  .filters { flex-direction: column; align-items: stretch; }
  .filters select { min-width: 0; width: 100%; }
  input, select, .btn { font-size: 16px; }
  .filters select, .filters .btn, .login-form input, .login-form .btn { min-height: 44px; }
  .btn-link { display: inline-flex; align-items: center; min-height: 44px; }
  .pagination a { padding: 12px 0; }
}
@media (max-width: 480px) {
  /* Dashboard: le 4 card numeriche restano 2×2 anche sui telefoni stretti,
     così il riassunto è più compatto e l'elenco impianti sale in cima.
     .cards-4 (dettaglio impianto) resta a 1 colonna: valori lunghi (date). */
  .cards { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .cards-4 { grid-template-columns: 1fr; gap: 10px; }
  .card-value { font-size: 24px; }
  .page-head h1 { font-size: 20px; }
}
