/* ============================================================================
 * DESIGN SYSTEM — Portale di monitoraggio ambientale
 * Unico foglio di stile per tutte le pagine.
 * ==========================================================================*/

/* ---------------------------------------------------------------- 1. TOKEN */
:root {
  /* Marchio */
  --brand:        #0d9488;
  --brand-2:      #2dd4bf;
  --brand-3:      #ccfbf1;
  --brand-ink:    #04211e;

  /* Stato */
  --ok:           #10b981;
  --warn:         #f59e0b;
  --danger:       #ef4444;
  --info:         #38bdf8;

  /* Forma */
  --r-xs: 6px; --r-sm: 9px; --r: 13px; --r-lg: 18px; --r-xl: 24px; --r-full: 999px;

  /* Ritmo */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 22px; --s6: 30px; --s7: 42px; --s8: 60px;

  /* Struttura */
  --rail: 76px;
  --topbar: 62px;
  --maxw: 1560px;

  --ease: cubic-bezier(.22,.61,.36,1);
  --font: 'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, monospace;
}

:root[data-theme="dark"] {
  --bg:           #080b12;
  --bg-2:         #0c111b;
  --surface:      #111827;
  --surface-2:    #161f31;
  --surface-3:    #1d2841;
  --panel:        #111827;
  --border:       #1f2a3d;
  --border-2:     #2b3a52;
  --text:         #eef2f8;
  --text-2:       #9aa8bd;
  --text-3:       #64748b;
  --muted:        #9aa8bd;
  --chip:         #172033;
  --shadow-1: 0 1px 2px rgba(0,0,0,.4);
  --shadow-2: 0 6px 22px rgba(0,0,0,.42);
  --shadow-3: 0 20px 56px rgba(0,0,0,.55);
  --glow:     0 0 0 1px rgba(255,255,255,.04) inset;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg:           #f5f7fa;
  --bg-2:         #eef2f7;
  --surface:      #ffffff;
  --surface-2:    #f7f9fc;
  --surface-3:    #eef2f7;
  --panel:        #ffffff;
  --border:       #e3e8f0;
  --border-2:     #cfd8e5;
  --text:         #0d1526;
  --text-2:       #55647c;
  --text-3:       #8a99ae;
  --muted:        #55647c;
  --chip:         #eff3f9;
  --shadow-1: 0 1px 2px rgba(16,24,40,.06);
  --shadow-2: 0 6px 20px rgba(16,24,40,.09);
  --shadow-3: 0 20px 50px rgba(16,24,40,.14);
  --glow:     none;
  color-scheme: light;
}

/* ----------------------------------------------------------------- 2. BASE */
*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; text-decoration: none; }
h1,h2,h3,h4 { margin: 0; font-weight: 700; letter-spacing: -.015em; line-height: 1.25; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p  { margin: 0; }
img { max-width: 100%; display: block; }

/* numeri sempre allineati in colonna */
.num, .stat-v, .metric-v, .kpi-v { font-variant-numeric: tabular-nums; font-feature-settings: 'tnum' 1; }

::selection { background: color-mix(in oklab, var(--brand) 38%, transparent); }

:where(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}

/* scrollbar discreta */
* { scrollbar-width: thin; scrollbar-color: var(--border-2) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 99px; border: 3px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-track { background: transparent; }

/* --------------------------------------------------------------- 3. SHELL */
.app {
  display: grid;
  grid-template-columns: var(--rail) 1fr;
  min-height: 100vh;
  min-height: 100dvh;
}

/* --- barra laterale a icone --- */
.rail {
  grid-column: 1;
  position: sticky; top: 0;
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; align-items: center;
  gap: var(--s2);
  padding: var(--s4) var(--s2);
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  z-index: 30;
}
.rail-logo {
  width: 40px; height: 40px; border-radius: var(--r);
  display: grid; place-items: center;
  margin-bottom: var(--s3);
  box-shadow: var(--shadow-1);
  flex: 0 0 auto;
}
.rail-logo img { width: 40px; height: 40px; border-radius: var(--r); }
.rail-sep { width: 26px; height: 1px; background: var(--border); margin: var(--s2) 0; flex: 0 0 auto; }
.rail-spacer { flex: 1 1 auto; }

.rail-btn {
  all: unset;
  position: relative;
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: var(--r);
  color: var(--text-3);
  cursor: pointer;
  transition: background .16s var(--ease), color .16s var(--ease), transform .16s var(--ease);
  flex: 0 0 auto;
}
.rail-btn svg { width: 21px; height: 21px; stroke-width: 1.8; }
.rail-btn:hover { background: var(--surface-2); color: var(--text); }
.rail-btn:active { transform: scale(.94); }
.rail-btn.is-active { background: color-mix(in oklab, var(--brand) 16%, transparent); color: var(--brand-2); }
:root[data-theme="light"] .rail-btn.is-active { color: var(--brand); }
.rail-btn.is-active::before {
  content: ''; position: absolute; left: -10px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 20px; border-radius: 0 3px 3px 0; background: var(--brand);
}
/* etichetta al passaggio del mouse */
.rail-btn::after {
  content: attr(data-label);
  position: absolute; left: calc(100% + 12px); top: 50%; transform: translateY(-50%) translateX(-6px);
  padding: 6px 10px; border-radius: var(--r-sm);
  background: var(--surface-3); color: var(--text);
  font-size: 12px; font-weight: 600; white-space: nowrap;
  box-shadow: var(--shadow-2); border: 1px solid var(--border);
  opacity: 0; pointer-events: none;
  transition: opacity .16s var(--ease), transform .16s var(--ease);
  z-index: 40;
}
.rail-btn:hover::after { opacity: 1; transform: translateY(-50%) translateX(0); }

/* --- barra superiore --- */
.main { grid-column: 2; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar);
  display: flex; align-items: center; gap: var(--s3);
  padding: 0 var(--s5);
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--border);
}
.crumbs { display: flex; align-items: center; gap: var(--s2); min-width: 0; font-size: 13px; color: var(--text-3); }
.crumbs a:hover { color: var(--text); }
.crumbs .sep { opacity: .45; }
.crumbs .here { color: var(--text); font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .grow { flex: 1 1 auto; }

.page { padding: var(--s6) var(--s5) var(--s8); width: 100%; max-width: var(--maxw); margin: 0 auto; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); flex-wrap: wrap; margin-bottom: var(--s5); }
.page-head .sub { color: var(--text-2); font-size: 13.5px; margin-top: 3px; }

/* --------------------------------------------------------- 4. FORME BASE */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), var(--glow);
}
.card-pad { padding: var(--s5); }
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s3);
  padding: var(--s4) var(--s5);
  border-bottom: 1px solid var(--border);
}
.card-title { font-size: 14px; font-weight: 700; letter-spacing: -.01em; }
.card-sub { font-size: 12.5px; color: var(--text-3); margin-top: 2px; }

.grid { display: grid; gap: var(--s4); }
.g-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.g-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.g-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.g-auto { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.g-auto-sm { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.g-metrics { grid-template-columns: repeat(auto-fill, minmax(232px, 1fr)); }
.row { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; }
.stack { display: grid; gap: var(--s4); }
.sect { margin-top: var(--s6); }
.sect-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s3); margin-bottom: var(--s4); }
.sect-head h2 { font-size: 16px; }
.sect-head .hint { font-size: 12.5px; color: var(--text-3); }

/* ------------------------------------------------------------ 5. CONTROLLI */
.btn {
  all: unset;
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  height: 38px; padding: 0 14px;
  border-radius: var(--r-sm);
  font-size: 13.5px; font-weight: 600;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border);
  cursor: pointer; white-space: nowrap;
  transition: background .15s var(--ease), border-color .15s var(--ease), transform .12s var(--ease);
}
.btn:hover { background: var(--surface-3); border-color: var(--border-2); }
.btn:active { transform: translateY(1px); }
.btn svg { width: 16px; height: 16px; stroke-width: 1.9; flex: 0 0 auto; }

.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: color-mix(in oklab, var(--brand) 86%, #fff); border-color: transparent; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-icon { width: 38px; padding: 0; }
.btn-sm { height: 32px; padding: 0 10px; font-size: 12.5px; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .45; pointer-events: none; }

/* gruppo a segmenti (periodi, viste) */
.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm);
}
.seg button {
  all: unset; cursor: pointer;
  padding: 6px 13px; border-radius: var(--r-xs);
  font-size: 13px; font-weight: 600; color: var(--text-2);
  transition: background .15s var(--ease), color .15s var(--ease);
}
.seg button:hover { color: var(--text); }
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }

/* campo di ricerca */
.search {
  display: flex; align-items: center; gap: var(--s2);
  height: 38px; padding: 0 12px; min-width: 0;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm);
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.search:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 18%, transparent); }
.search svg { width: 16px; height: 16px; color: var(--text-3); flex: 0 0 auto; }
.search input { all: unset; flex: 1 1 auto; min-width: 0; font-size: 13.5px; color: var(--text); }
.search input::placeholder { color: var(--text-3); }

.field { display: grid; gap: 6px; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.field input, .field select {
  width: 100%; height: 44px; padding: 0 13px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  font: inherit; font-size: 14px;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.field input:focus, .field select:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 18%, transparent);
}
select.input {
  height: 38px; padding: 0 30px 0 12px;
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 9px center / 15px;
  color: var(--text); border: 1px solid var(--border); border-radius: var(--r-sm);
  font: inherit; font-size: 13.5px; font-weight: 600;
  appearance: none; cursor: pointer; max-width: 100%;
}
select.input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 18%, transparent); }

/* --------------------------------------------------------------- 6. STATO */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  height: 24px; padding: 0 9px;
  border-radius: var(--r-full);
  font-size: 12px; font-weight: 650; letter-spacing: -.005em;
  background: var(--chip); color: var(--text-2);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: 0 0 auto; }

.badge-ok      { color: var(--ok);     background: color-mix(in oklab, var(--ok) 13%, transparent);     border-color: color-mix(in oklab, var(--ok) 30%, transparent); }
.badge-warn    { color: var(--warn);   background: color-mix(in oklab, var(--warn) 14%, transparent);   border-color: color-mix(in oklab, var(--warn) 32%, transparent); }
.badge-danger  { color: var(--danger); background: color-mix(in oklab, var(--danger) 14%, transparent); border-color: color-mix(in oklab, var(--danger) 32%, transparent); }
.badge-unknown { color: var(--text-3); background: var(--chip); border-color: var(--border); }

.tone-ok      { color: var(--ok); }
.tone-warn    { color: var(--warn); }
.tone-danger  { color: var(--danger); }
.tone-unknown { color: var(--text-3); }

/* ------------------------------------------------- 7. TESSERA STATISTICA */
.stat {
  display: grid; gap: 3px;
  padding: var(--s4) var(--s5);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), var(--glow);
}
.stat-k { font-size: 11.5px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--text-3); }
.stat-v { font-size: 28px; font-weight: 750; letter-spacing: -.03em; line-height: 1.1; }
.stat-n { font-size: 12.5px; color: var(--text-3); }

/* ---------------------------------------- 8. CARD METRICA (cuore della UI) */
/* Principio: la card sta ferma e neutra. Un solo elemento è colorato — il
   riempimento — e il colore dice lo stato. Niente fondali rossi dietro un
   valore che va benissimo. */
.metric {
  position: relative;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: var(--s3);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), var(--glow);
  transition: border-color .18s var(--ease), box-shadow .18s var(--ease);
}
.metric:hover { border-color: var(--border-2); box-shadow: var(--shadow-2), var(--glow); }
.metric.is-warn   { border-color: color-mix(in oklab, var(--warn) 34%, var(--border)); }
.metric.is-danger { border-color: color-mix(in oklab, var(--danger) 42%, var(--border)); }
.metric.is-unknown .metric-v { color: var(--text-3); }

.metric-top { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); }
.metric-sym { font-size: 13.5px; font-weight: 750; letter-spacing: -.01em; }
.metric-name {
  font-size: 11.5px; color: var(--text-3); line-height: 15px; min-height: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.metric-val { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s2); flex-wrap: wrap; }
.metric-num { display: flex; align-items: baseline; gap: 5px; min-width: 0; }
.metric-v { font-size: 27px; font-weight: 750; letter-spacing: -.035em; line-height: 1.05; }
.metric-u { font-size: 12px; color: var(--text-3); font-weight: 600; }
.metric-judge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px; font-weight: 650; white-space: nowrap;
}
.metric-judge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: 0 0 auto; }

/* --- indicatore: fondo neutro, riempimento colorato, tacca sulla soglia --- */
.meter { position: relative; height: 6px; border-radius: var(--r-full); background: var(--surface-3); }
.meter-fill {
  position: absolute; inset: 0 auto 0 0; border-radius: var(--r-full);
  transition: width .45s var(--ease), background .3s var(--ease);
}
.meter-win {                      /* fascia consigliata, per i parametri a intervallo */
  position: absolute; top: 0; bottom: 0; border-radius: var(--r-full);
  background: color-mix(in oklab, var(--ok) 60%, transparent);
}
.meter-tick {                     /* dove inizia l'attenzione */
  position: absolute; top: -3px; width: 2px; height: 12px; border-radius: 1px;
  background: var(--text-3); opacity: .6; transform: translateX(-1px);
}
.meter-mark {                     /* posizione del valore, per i parametri a intervallo */
  position: absolute; top: -4px; width: 3px; height: 14px; border-radius: 2px;
  background: var(--text); transform: translateX(-50%);
  box-shadow: 0 0 0 2px var(--surface);
  transition: left .45s var(--ease);
}
.metric-cap { font-size: 11px; color: var(--text-3); line-height: 1.4; }

/* pulsante informativo: presente ma non invadente */
.metric .mi-btn {
  width: 22px; height: 22px; flex: 0 0 22px;
  border-color: transparent; background: transparent; color: var(--text-3);
  font-size: 13px;
}
.metric .mi-btn:hover { background: var(--surface-3); color: var(--brand-2); border-color: transparent; transform: none; }
:root[data-theme="light"] .metric .mi-btn:hover { color: var(--brand); }

/* --------------------------------------------------- 9. CARD DISPOSITIVO */
.device {
  display: grid; gap: var(--s3);
  padding: var(--s4);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), var(--glow);
  cursor: pointer;
  transition: border-color .18s var(--ease), transform .18s var(--ease), box-shadow .18s var(--ease);
}
.device:hover { transform: translateY(-2px); border-color: var(--border-2); box-shadow: var(--shadow-2), var(--glow); }
.device.is-active { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand), var(--shadow-2); }
.device-top { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s2); }
.device-name { font-size: 14.5px; font-weight: 700; }
.device-meta { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.device-vals { display: flex; gap: var(--s4); flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); }
.device-vals b { color: var(--text); font-weight: 650; }

/* --------------------------------------------------------- 10. CARD SEDE */
.site {
  position: relative; display: grid; gap: var(--s4);
  padding: var(--s5);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-1), var(--glow);
  cursor: pointer; overflow: hidden;
  transition: transform .2s var(--ease), border-color .2s var(--ease), box-shadow .2s var(--ease);
}
.site::before {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 3px;
  background: var(--border-2); transition: background .2s var(--ease);
}
.site.is-ok::before     { background: linear-gradient(90deg, var(--ok), color-mix(in oklab, var(--ok) 55%, var(--brand-2))); }
.site.is-warn::before   { background: linear-gradient(90deg, var(--warn), #fbbf24); }
.site.is-danger::before { background: linear-gradient(90deg, var(--danger), #fb7185); }
.site:hover { transform: translateY(-3px); border-color: var(--border-2); box-shadow: var(--shadow-3), var(--glow); }
.site-title { font-size: 17px; font-weight: 750; letter-spacing: -.02em; }
.site-addr { font-size: 12.5px; color: var(--text-3); margin-top: 2px; }
.site-kpis { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: var(--s3); }
.site-kpis > div { min-width: 0; }
.site-kpis .kpi-v { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site .badge { flex: 0 0 auto; max-width: 50%; overflow: hidden; }
.kpi-k { font-size: 11px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--text-3); }
.kpi-v { font-size: 19px; font-weight: 750; letter-spacing: -.025em; margin-top: 1px; }

/* barra di ripartizione degli stati */
.split { display: flex; height: 6px; border-radius: var(--r-full); overflow: hidden; gap: 2px; background: var(--chip); }
.split span { display: block; height: 100%; }

/* ------------------------------------------------- 11. LISTA "ATTENZIONE" */
.alert-row {
  display: flex; align-items: center; gap: var(--s4);
  padding: var(--s3) var(--s4);
  border-radius: var(--r);
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
  transition: border-color .15s var(--ease), background .15s var(--ease);
}
.alert-row:hover { border-color: var(--border-2); background: var(--surface-3); }
.alert-ico { width: 34px; height: 34px; border-radius: var(--r-sm); display: grid; place-items: center; flex: 0 0 auto; font-size: 15px; }
.alert-ico.is-warn   { background: color-mix(in oklab, var(--warn) 16%, transparent);   color: var(--warn); }
.alert-ico.is-danger { background: color-mix(in oklab, var(--danger) 16%, transparent); color: var(--danger); }
.alert-body { flex: 1 1 auto; min-width: 0; }
.alert-t { font-size: 13.5px; font-weight: 650; }
.alert-d { font-size: 12.5px; color: var(--text-2); margin-top: 1px; }

/* ------------------------------------------------- 12. VUOTO / CARICAMENTO */
.empty {
  display: grid; place-items: center; gap: var(--s3);
  padding: var(--s8) var(--s5); text-align: center;
  border: 1px dashed var(--border-2); border-radius: var(--r-lg);
  color: var(--text-3);
}
.empty-ico { width: 46px; height: 46px; border-radius: var(--r); display: grid; place-items: center; background: var(--surface-2); color: var(--text-3); }
.empty h3 { font-size: 15px; color: var(--text); }
.empty p { font-size: 13px; max-width: 46ch; }

.skel {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
  border-radius: var(--r-sm);
}
@keyframes shimmer { 0% { background-position: 100% 0 } 100% { background-position: 0 0 } }

/* ------------------------------------------------------------ 13. AVVISI */
.toasts { position: fixed; right: var(--s5); bottom: var(--s5); z-index: 9990; display: grid; gap: var(--s2); }
.toast {
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s4);
  background: var(--surface-3); border: 1px solid var(--border-2); border-radius: var(--r);
  box-shadow: var(--shadow-3); font-size: 13px; max-width: 380px;
  animation: toastIn .28s var(--ease);
}
.toast.is-err { border-color: color-mix(in oklab, var(--danger) 45%, var(--border)); }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px) } }

/* -------------------------------------------------------- 14. RESPONSIVE */
@media (max-width: 1100px) {
  .g-4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .g-3 { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 860px) {
  :root { --topbar: 56px; }

  /* la barra laterale diventa barra di navigazione in basso */
  .app { grid-template-columns: 1fr; }
  .main { grid-column: 1; padding-bottom: 66px; }
  .rail {
    position: fixed; inset: auto 0 0 0; top: auto;
    height: 62px; width: 100%;
    flex-direction: row; justify-content: space-around; align-items: center;
    gap: 0; padding: 0 var(--s3);
    border-right: none; border-top: 1px solid var(--border);
    background: color-mix(in oklab, var(--bg-2) 94%, transparent);
    backdrop-filter: blur(14px);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .rail-logo, .rail-sep, .rail-spacer { display: none; }
  .rail-btn { width: 52px; height: 44px; }
  .rail-btn.is-active::before { left: 50%; top: auto; bottom: -1px; transform: translateX(-50%); width: 22px; height: 3px; border-radius: 3px 3px 0 0; }
  .rail-btn::after { display: none; }

  .page { padding: var(--s5) var(--s4) var(--s7); }
  .topbar { padding: 0 var(--s4); }
  .sect-head .hint { display: none; }   /* su schermo stretto il titolo viene prima */
  .g-2, .g-3, .g-4 { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .g-auto { grid-template-columns: repeat(auto-fill, minmax(215px, 1fr)); }
  .g-metrics { grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); }
  h1 { font-size: 20px; }
  .toasts { right: var(--s3); left: var(--s3); bottom: 74px; }
  .toast { max-width: none; }
}

@media (max-width: 520px) {
  .g-2, .g-3, .g-4, .g-auto, .g-auto-sm, .g-metrics { grid-template-columns: 1fr; }
  .stat-v { font-size: 24px; }
  .metric-v { font-size: 26px; }
  .page { padding: var(--s4) var(--s3) var(--s7); }
  .card-pad { padding: var(--s4); }
  .site { padding: var(--s4); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ------------------------------------------------------------- 15. UTILI */
.u-muted { color: var(--text-2); }
.u-dim   { color: var(--text-3); }
.u-sm    { font-size: 12.5px; }
.u-xs    { font-size: 11.5px; }
.u-b     { font-weight: 650; }
.u-right { margin-left: auto; }
.u-hide  { display: none !important; }
.u-trunc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-mono  { font-family: var(--mono); font-size: 12px; }

/* ========================================== 16. STRISCIA DEI DISPOSITIVI */
.devstrip {
  display: flex; gap: var(--s2);
  overflow-x: auto; padding: 3px 3px var(--s2);
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
}
.devstrip::-webkit-scrollbar { display: none; }
.devpill {
  all: unset; cursor: pointer; scroll-snap-align: start;
  display: flex; align-items: center; gap: var(--s2);
  padding: 9px 14px; flex: 0 0 auto;
  border-radius: var(--r-full);
  background: var(--surface); border: 1px solid var(--border);
  font-size: 13.5px; font-weight: 600; color: var(--text-2);
  transition: border-color .16s var(--ease), color .16s var(--ease), background .16s var(--ease);
}
.devpill:hover { color: var(--text); border-color: var(--border-2); }
.devpill .dot { width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto; }
.devpill[aria-selected="true"] {
  background: var(--surface-3); color: var(--text);
  border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand);
}

/* ============================================= 17. RIQUADRO DI SINTESI */
.hero {
  position: relative; overflow: hidden;
  display: grid; gap: var(--s3);
  padding: var(--s5) var(--s6);
  border-radius: var(--r-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow-1), var(--glow);
}
.hero::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(660px 200px at 0% 0%, color-mix(in oklab, var(--tone,var(--brand)) 13%, transparent), transparent 70%);
}
.hero::before {
  content: ''; position: absolute; inset: 0 auto 0 0; width: 4px;
  background: var(--tone, var(--brand));
}
.hero.is-ok      { --tone: var(--ok); }
.hero.is-warn    { --tone: var(--warn); }
.hero.is-danger  { --tone: var(--danger); }
.hero.is-unknown { --tone: var(--text-3); }
.hero-top { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); flex-wrap: wrap; position: relative; }
.hero-h { font-size: 22px; font-weight: 750; letter-spacing: -.025em; position: relative; }
.hero-p { font-size: 14px; color: var(--text-2); max-width: 78ch; position: relative; line-height: 1.6; }
.hero-act { position: relative; display: flex; gap: var(--s2); flex-wrap: wrap; margin-top: var(--s1); }
.hero-note { display: block; margin-top: var(--s2); font-size: 13px; font-weight: 600; }

/* ==================================================== 18. MINI MAPPA */
.mapbox { height: 300px; border-radius: 0 0 var(--r-lg) var(--r-lg); overflow: hidden; }
.mapbox.is-flat { border-radius: var(--r-lg); }
.leaflet-container { background: var(--surface-2); font-family: var(--font); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--surface-3); color: var(--text);
  border: 1px solid var(--border-2); box-shadow: var(--shadow-3);
  border-radius: var(--r);
}
.leaflet-popup-content { margin: 11px 14px; font-size: 13px; line-height: 1.55; }
.leaflet-container a.leaflet-popup-close-button { color: var(--text-3); }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--border); }
.leaflet-bar a:hover { background: var(--surface-2); }
.leaflet-control-attribution { background: color-mix(in oklab, var(--surface) 85%, transparent) !important; color: var(--text-3) !important; }
.leaflet-control-attribution a { color: var(--text-2) !important; }
.mk {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2.5px solid var(--surface);
  box-shadow: 0 0 0 1.5px color-mix(in oklab, var(--text) 25%, transparent), 0 2px 6px rgba(0,0,0,.4);
}

/* ================================================== 19. AREA GRAFICO */
.chartwrap { position: relative; height: clamp(320px, 52vh, 560px); padding: var(--s4) var(--s5) var(--s5); }
.chartwrap canvas { position: absolute; inset: var(--s4) var(--s5) var(--s5); }
.chart-load {
  position: absolute; inset: 0; display: none; place-items: center; z-index: 3;
  background: color-mix(in oklab, var(--surface) 72%, transparent); backdrop-filter: blur(2px);
  border-radius: var(--r-lg); font-size: 13px; color: var(--text-2);
}
.chart-load.is-on { display: grid; }
.spinner {
  width: 22px; height: 22px; border-radius: 50%; margin: 0 auto var(--s2);
  border: 2.5px solid var(--border-2); border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg) } }

/* chip per la scelta della metrica nel grafico */
.mchips { display: flex; gap: var(--s2); flex-wrap: wrap; }
.mchip {
  all: unset; cursor: pointer;
  padding: 7px 13px; border-radius: var(--r-full);
  background: var(--surface-2); border: 1px solid var(--border);
  font-size: 13px; font-weight: 600; color: var(--text-2);
  transition: all .15s var(--ease);
}
.mchip:hover { color: var(--text); border-color: var(--border-2); }
.mchip[aria-selected="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }

@media (max-width: 860px) {
  .hero { padding: var(--s4) var(--s5); }
  .hero-h { font-size: 19px; }
  .mapbox { height: 240px; }
  .chartwrap { height: 340px; padding: var(--s3) var(--s4) var(--s4); }
  .chartwrap canvas { inset: var(--s3) var(--s4) var(--s4); }
}

/* ================================================== 20. PAGINA DI ACCESSO */
.auth { display: grid; grid-template-columns: 1.05fr .95fr; min-height: 100vh; min-height: 100dvh; }

.auth-side {
  position: relative; overflow: hidden;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: var(--s7) var(--s7) var(--s6);
  background: linear-gradient(155deg, #0b2f2b 0%, #071a2b 48%, #05101c 100%);
  color: #e9fbf7;
}
.auth-side::before {
  content: ''; position: absolute; inset: -30% -20% auto -30%; height: 90%;
  background: radial-gradient(closest-side, rgba(45,212,191,.30), transparent 72%);
  filter: blur(8px);
}
.auth-side::after {
  content: ''; position: absolute; inset: auto -25% -35% 20%; height: 80%;
  background: radial-gradient(closest-side, rgba(13,148,136,.26), transparent 72%);
}
.auth-brand { position: relative; display: flex; align-items: center; gap: var(--s3); }
.auth-bottom { position: relative; display: grid; gap: var(--s6); }
.auth-brand img { width: 40px; height: 40px; border-radius: var(--r); }
.auth-brand span { font-size: 14.5px; font-weight: 700; letter-spacing: -.01em; }
.auth-claim { position: relative; max-width: 30ch; }
.auth-claim h2 { font-size: clamp(27px, 3.1vw, 40px); font-weight: 800; letter-spacing: -.035em; line-height: 1.12; }
.auth-claim p { margin-top: var(--s4); font-size: 15px; line-height: 1.65; color: rgba(233,251,247,.72); max-width: 42ch; }
.auth-list { position: relative; display: grid; gap: var(--s3); margin: 0; padding: 0; list-style: none; }
.auth-list li { display: flex; align-items: flex-start; gap: var(--s3); font-size: 13.5px; color: rgba(233,251,247,.85); }
.auth-list .tick {
  width: 21px; height: 21px; border-radius: 50%; flex: 0 0 auto; margin-top: 1px;
  display: grid; place-items: center;
  background: rgba(45,212,191,.18); color: #5eead4;
}
.auth-list .tick svg { width: 12px; height: 12px; stroke-width: 3; }

.auth-main { display: grid; place-items: center; padding: var(--s6) var(--s5); background: var(--bg); }
.auth-box { width: min(400px, 100%); display: grid; gap: var(--s5); }
.auth-box h1 { font-size: 26px; letter-spacing: -.03em; }
.auth-box .lead { color: var(--text-2); font-size: 14px; margin-top: 5px; }
.auth-form { display: grid; gap: var(--s4); }
.auth-pass { position: relative; }
.auth-pass input { padding-right: 46px; }
.auth-eye {
  all: unset; cursor: pointer; position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 34px; height: 34px; border-radius: var(--r-xs);
  display: grid; place-items: center; color: var(--text-3);
}
.auth-eye:hover { color: var(--text); background: var(--surface-3); }
.auth-eye svg { width: 17px; height: 17px; stroke-width: 1.9; }
.auth-msg {
  display: none; align-items: flex-start; gap: var(--s2);
  padding: var(--s3) var(--s4); border-radius: var(--r-sm); font-size: 13px; line-height: 1.5;
}
.auth-msg.is-on { display: flex; }
.auth-msg.is-err { background: color-mix(in oklab, var(--danger) 13%, transparent); color: var(--danger);
  border: 1px solid color-mix(in oklab, var(--danger) 34%, transparent); }
.auth-msg.is-ok { background: color-mix(in oklab, var(--ok) 13%, transparent); color: var(--ok);
  border: 1px solid color-mix(in oklab, var(--ok) 34%, transparent); }
.auth-msg svg { width: 16px; height: 16px; flex: 0 0 auto; margin-top: 1px; stroke-width: 2; }
.auth-foot { font-size: 12px; color: var(--text-3); text-align: center; }
.auth-btn-big { height: 46px; font-size: 14.5px; width: 100%; }

@media (max-width: 900px) {
  .auth { grid-template-columns: 1fr; }
  .auth-side { padding: var(--s5) var(--s4); gap: var(--s5); }
  .auth-claim h2 { font-size: 24px; }
  .auth-claim p { font-size: 14px; margin-top: var(--s3); }
  .auth-list { display: none; }
  .auth-main { padding: var(--s6) var(--s4) var(--s7); }
}

/* ===================================================== 21. PAGINA MAPPA */
.maplayout {
  display: grid; grid-template-columns: 350px 1fr;
  height: calc(100vh - var(--topbar)); height: calc(100dvh - var(--topbar));
  min-height: 0;
}
.mapside {
  display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--border); background: var(--bg-2);
}
.mapside-head { padding: var(--s4); border-bottom: 1px solid var(--border); display: grid; gap: var(--s3); }
.mapside-list { overflow: auto; padding: var(--s3); display: grid; gap: var(--s2); align-content: start; }
.maprow {
  display: grid; gap: 7px; padding: var(--s3) var(--s4);
  border-radius: var(--r); border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; transition: border-color .15s var(--ease), transform .15s var(--ease);
}
.maprow:hover { border-color: var(--border-2); transform: translateX(2px); }
.maprow.is-active { border-color: var(--brand); box-shadow: 0 0 0 1px var(--brand); }
.maprow-t { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); }
.maprow-n { font-size: 13.5px; font-weight: 650; }
.maprow-a { font-size: 12px; color: var(--text-3); }
.mapcanvas { min-width: 0; min-height: 0; }
.mapcanvas #gmap { width: 100%; height: 100%; }

@media (max-width: 860px) {
  .maplayout { grid-template-columns: 1fr; grid-template-rows: 46vh 1fr; height: auto; }
  .mapside { border-right: none; border-bottom: 1px solid var(--border); order: 2; }
  .mapcanvas { order: 1; height: 46vh; }
  .maplayout { grid-template-rows: auto auto; }
}

/* I tile OpenStreetMap sono chiari: in tema scuro li invertiamo,
   così la mappa non abbaglia e resta coerente col resto. */
:root[data-theme="dark"] .leaflet-tile {
  filter: grayscale(1) invert(1) brightness(.88) contrast(.82);
}
:root[data-theme="dark"] .leaflet-control-zoom a { color: var(--text); }
