/* Stile della UI. Dark di default: chi la usa ci passa la giornata, e un tema
   chiaro sparato in faccia alle 7 del mattino e' il motivo per cui poi la gente
   torna a guardare il foglio Google.

   Tutto passa da variabili CSS: i colori di stato (giallo/rosso/grigio) sono le
   uniche cose che la UI dice davvero, e devono voler dire la stessa cosa in
   ogni vista. Definirli due volte vuol dire vederli divergere.

   Niente @import, niente font remoti: la VPS puo' non avere uscita libera e un
   font che non arriva e' una pagina che salta per mezzo secondo — un CDN giu' e'
   un'interfaccia morta. Si usa lo stack di sistema. */

:root {
  --bg: #0e1116;
  --bg-elev: #151a21;
  --panel: #171d26;
  --panel-2: #1c2430;
  --line: #27303d;
  --line-soft: #202834;

  --txt: #e6edf3;
  --txt-dim: #9aa7b8;
  --txt-faint: #6b7888;

  --accent: #4c8dff;
  --accent-dim: #2a4d8f;

  /* I tre stati che la tabella libri deve gridare. Il nome dice il significato,
     non il colore: se un giorno il giallo diventa arancione, il senso resta. */
  --st-low: #f0b429;        /* spesa fra 0.01 e 2.00 */
  --st-low-bg: rgba(240, 180, 41, 0.14);
  --st-broken: #ff5f56;     /* verde ma spesa zero */
  --st-broken-bg: rgba(255, 95, 86, 0.16);
  --st-unknown: #6b7888;    /* mai letto: assenza di dato, non zero */
  --st-unknown-bg: rgba(107, 120, 136, 0.10);
  --st-ok: #35c46a;
  --st-ok-bg: rgba(53, 196, 106, 0.12);
  --st-info: #4c8dff;
  --st-info-bg: rgba(76, 141, 255, 0.12);

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);

  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4f6f9;
  --bg-elev: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f3f7;
  --line: #d6dde6;
  --line-soft: #e4e9ef;

  --txt: #16202b;
  --txt-dim: #5a6878;
  --txt-faint: #8b98a8;

  --accent: #1d6ae5;
  --accent-dim: #a9c6f7;

  /* Su fondo chiaro gli stessi colori del tema scuro sbiadiscono: il giallo a
     #f0b429 su bianco sta sotto il rapporto 4.5:1 e il grigio del "mai letto"
     arriva a 2.7, cioe' un dato importante che si legge peggio della sua etichetta.
     Qui sono scuriti fino a passare AA — il colore *significa* qualcosa, e un
     significato che si vede solo con la luce giusta non e' un significato. */
  --st-low: #7a5400;
  --st-low-bg: rgba(240, 180, 41, 0.22);
  --st-broken: #c02b22;
  --st-broken-bg: rgba(255, 95, 86, 0.16);
  --st-unknown: #566371;
  --st-unknown-bg: rgba(107, 120, 136, 0.10);
  --st-ok: #14803c;
  --st-ok-bg: rgba(53, 196, 106, 0.16);
  --st-info: #1d6ae5;
  --st-info-bg: rgba(76, 141, 255, 0.12);

  --shadow: 0 6px 20px rgba(20, 30, 45, 0.12);

  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--txt);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* ---------------------------------------------------------------- header */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  flex-wrap: wrap;
}

.brand {
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
}

.spacer { flex: 1 1 auto; }

/* Lo stato della connessione non e' decorazione: e' la differenza fra "sto
   guardando i numeri di adesso" e "sto guardando quelli di stanotte". */
.conn {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12px;
  color: var(--txt-dim);
  white-space: nowrap;
  background: var(--panel);
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--st-ok);
  flex: none;
}

.conn[data-state="loading"] .conn-dot { background: var(--accent); animation: pulse 1s infinite; }
.conn[data-state="stale"] { border-color: var(--st-low); color: var(--st-low); }
.conn[data-state="stale"] .conn-dot { background: var(--st-low); }
.conn[data-state="lost"] { border-color: var(--st-broken); color: var(--st-broken); background: var(--st-broken-bg); }
.conn[data-state="lost"] .conn-dot { background: var(--st-broken); animation: pulse 1s infinite; }

@keyframes pulse { 50% { opacity: 0.25; } }

.icon-btn {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 12px;
  color: var(--txt-dim);
}
.icon-btn:hover { border-color: var(--accent); color: var(--txt); }

/* Banner di connessione persa. Sta sotto la barra, occupa spazio, non si chiude:
   se sparisce da solo l'utente torna a fidarsi di numeri fermi. */
.offline-banner {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  background: var(--st-broken-bg);
  border-bottom: 1px solid var(--st-broken);
  color: var(--st-broken);
  font-size: 13px;
  font-weight: 500;
}
.offline-banner.on { display: flex; }
.offline-banner .spacer { flex: 1; }
.offline-banner button {
  background: transparent;
  border: 1px solid currentColor;
  border-radius: var(--radius-sm);
  padding: 3px 10px;
  font-size: 12px;
}

/* ------------------------------------------------------------------- nav */

.tabs {
  display: flex;
  gap: 2px;
  padding: 0 10px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 9px 13px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--txt-dim);
  font-size: 13.5px;
  white-space: nowrap;
}
.tab:hover { color: var(--txt); }
.tab[aria-current="page"] { color: var(--txt); border-bottom-color: var(--accent); font-weight: 550; }

.tab-badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--txt-dim);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  text-align: center;
}
.tab-badge[data-sev="error"] { background: var(--st-broken); color: #fff; }
.tab-badge[data-sev="warning"] { background: var(--st-low); color: #1a1400; }

/* ------------------------------------------------------------------ main */

main {
  padding: 16px;
  max-width: 1800px;
  margin: 0 auto;
}

h1 { font-size: 19px; margin: 0 0 2px; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 10px; letter-spacing: -0.01em; }
h3 { font-size: 13px; margin: 0 0 6px; color: var(--txt-dim); font-weight: 600; }

.view-head { margin-bottom: 14px; }
.view-head p { margin: 0; color: var(--txt-dim); font-size: 13px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}
.panel > h2 { display: flex; align-items: center; gap: 8px; }
.panel-sub { color: var(--txt-dim); font-size: 12.5px; margin: -6px 0 10px; }

.grid { display: grid; gap: 14px; }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(270px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }

/* --------------------------------------------------------------- numeri */

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.stat-label { font-size: 12px; color: var(--txt-dim); margin-bottom: 4px; }
.stat-value {
  font-size: 22px;
  font-weight: 620;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat-value.na { color: var(--txt-faint); font-weight: 400; }
.stat-note { font-size: 11.5px; color: var(--txt-faint); margin-top: 4px; line-height: 1.35; }

.num { font-variant-numeric: tabular-nums; }
.na { color: var(--txt-faint); }
.mono { font-family: var(--mono); font-size: 12px; }

/* --------------------------------------------------------------- badges */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 550;
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge[data-sev="error"] { background: var(--st-broken-bg); color: var(--st-broken); border-color: var(--st-broken); }
.badge[data-sev="warning"] { background: var(--st-low-bg); color: var(--st-low); border-color: var(--st-low); }
.badge[data-sev="info"] { background: var(--st-info-bg); color: var(--st-info); border-color: var(--st-info); }
.badge[data-sev="success"] { background: var(--st-ok-bg); color: var(--st-ok); border-color: var(--st-ok); }
.badge[data-sev="muted"] { background: var(--st-unknown-bg); color: var(--txt-dim); border-color: var(--line); }

.pill {
  display: inline-block;
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border: 1px solid var(--line-soft);
  color: var(--txt-dim);
  font-size: 11px;
  white-space: nowrap;
}

/* --------------------------------------------------------------- inputs */

input[type="text"], input[type="search"], input[type="password"], select {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--txt);
  padding: 6px 9px;
  font: inherit;
  font-size: 13px;
  min-width: 0;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

label.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--txt-dim);
  white-space: nowrap;
  cursor: pointer;
}

.btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 13px;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 550; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.1); }
.btn-sm { padding: 3px 8px; font-size: 12px; }

.toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.toolbar .grow { flex: 1 1 220px; }

/* -------------------------------------------------------------- tabelle */

/* Il contenitore scrolla, non la pagina: 15 colonne su un telefono devono poter
   scorrere di lato senza portarsi dietro l'intestazione della finestra. */
.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  max-height: calc(100vh - 230px);
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  font-size: 13px;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  padding: 7px 9px;
  text-align: left;
  font-weight: 600;
  font-size: 11.5px;
  color: var(--txt-dim);
  white-space: nowrap;
}

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--txt); }
th .sort-arrow { color: var(--accent); }

tbody td {
  padding: 6px 9px;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: top;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--panel-2); }

/* Prima colonna ancorata: senza il titolo, una riga a 12 mercati e' 36 numeri
   senza soggetto. */
.sticky-col {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
thead .sticky-col { z-index: 3; background: var(--panel-2); }
tbody tr:hover .sticky-col { background: var(--panel-2); }

.cell-title { font-weight: 550; max-width: 320px; }
.cell-title a { color: var(--txt); }
.cell-title a:hover { color: var(--accent); }
.cell-sub { font-size: 11px; color: var(--txt-faint); font-family: var(--mono); margin-top: 1px; }

/* Cella di mercato: tre numeri impilati (spesa, royalty, CTR) e un colore che
   dice cosa c'e' che non va. Il colore e' il messaggio, il resto e' la prova. */
.mkt {
  min-width: 78px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  border-left: 1px solid var(--line-soft);
}
.mkt-spend { font-weight: 600; }
.mkt-sub { font-size: 10.5px; color: var(--txt-faint); line-height: 1.3; }

.mkt[data-state="low"] { background: var(--st-low-bg); }
.mkt[data-state="low"] .mkt-spend { color: var(--st-low); }
.mkt[data-state="broken"] { background: var(--st-broken-bg); }
.mkt[data-state="broken"] .mkt-spend { color: var(--st-broken); }
.mkt[data-state="unknown"] { background: var(--st-unknown-bg); }
.mkt[data-state="unknown"] .mkt-spend { color: var(--st-unknown); font-weight: 400; }

.empty {
  padding: 28px 14px;
  text-align: center;
  color: var(--txt-dim);
  font-size: 13px;
}
.empty strong { display: block; color: var(--txt); margin-bottom: 4px; font-size: 14px; }

.pager {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
  padding: 10px 2px 0;
  font-size: 12.5px;
  color: var(--txt-dim);
}

/* ------------------------------------------------------ matrice copertura */

.matrix td { padding: 0; border-left: 1px solid var(--line-soft); }

.cov-cell {
  display: block;
  width: 100%;
  min-width: 62px;
  padding: 5px 6px;
  border: 0;
  background: transparent;
  text-align: center;
  font-size: 11px;
  line-height: 1.3;
  color: var(--txt-dim);
}
.cov-cell:hover { outline: 1px solid var(--accent); outline-offset: -1px; }
.cov-cell .cov-mark { font-weight: 650; font-size: 12px; display: block; }
.cov-cell .cov-age { font-size: 10px; color: var(--txt-faint); }

.cov-cell[data-kind="ok"] { background: var(--st-ok-bg); color: var(--st-ok); }
.cov-cell[data-kind="missing_account"] { background: var(--st-broken-bg); color: var(--st-broken); }
.cov-cell[data-kind="never_scraped"] { background: var(--st-broken-bg); color: var(--st-broken); }
.cov-cell[data-kind="inactive_account"] { background: var(--st-low-bg); color: var(--st-low); }
.cov-cell[data-kind="stale_account"] { background: var(--st-low-bg); color: var(--st-low); }
.cov-cell[data-kind="no_policy"] { background: var(--st-info-bg); color: var(--st-info); }
.cov-cell[data-kind="none"] { color: var(--txt-faint); }

/* Intestazione di colonna col conteggio: e' qui che "AU 0/53" salta all'occhio
   senza doverlo cercare in 71 righe. */
.mkt-head { text-align: center; min-width: 62px; }
.mkt-head .mkt-code { font-size: 12.5px; color: var(--txt); font-weight: 650; }
.mkt-head .mkt-cov { font-size: 10px; font-weight: 500; font-variant-numeric: tabular-nums; }
.mkt-head .mkt-cov[data-bad="1"] { color: var(--st-broken); }
.mkt-head .mkt-cov[data-bad="0"] { color: var(--txt-faint); }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 12px; }
.legend-item { display: flex; align-items: flex-start; gap: 7px; max-width: 340px; }
.legend-swatch {
  width: 13px; height: 13px; border-radius: 3px; flex: none; margin-top: 2px;
  border: 1px solid var(--line);
}
.legend-item span { color: var(--txt-dim); }
.legend-item b { color: var(--txt); font-weight: 600; }

/* ------------------------------------------------------------------ alert */

.alert-card {
  display: flex;
  gap: 11px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: var(--radius-sm);
  background: var(--panel);
  margin-bottom: 8px;
}
.alert-card[data-sev="error"] { border-left-color: var(--st-broken); }
.alert-card[data-sev="warning"] { border-left-color: var(--st-low); }
.alert-card[data-sev="info"] { border-left-color: var(--st-info); }
.alert-card[data-sev="success"] { border-left-color: var(--st-ok); }
.alert-card.resolved { opacity: 0.6; }
.alert-card.snoozed { opacity: 0.7; border-style: dashed; }

.alert-main { flex: 1; min-width: 0; }
.alert-title { font-weight: 600; margin-bottom: 3px; }
.alert-body { font-size: 12.5px; color: var(--txt-dim); white-space: pre-wrap; word-break: break-word; }
.alert-body code {
  font-family: var(--mono);
  font-size: 11px;
  background: var(--panel-2);
  padding: 1px 4px;
  border-radius: 3px;
}
.alert-meta { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 7px; font-size: 11px; color: var(--txt-faint); }
.alert-todo {
  margin-top: 7px;
  padding: 6px 9px;
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  border-left: 2px solid var(--accent-dim);
  font-size: 12px;
  color: var(--txt-dim);
}
.alert-todo b { color: var(--txt); }
.alert-actions { display: flex; flex-direction: column; gap: 5px; flex: none; align-items: stretch; }

.group-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 16px 0 8px;
}
.group-head h3 { margin: 0; color: var(--txt); font-size: 14px; }
.group-head .count { color: var(--txt-faint); font-size: 12px; }

/* ---------------------------------------------------------- dettaglio libro */

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--txt-dim); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }

.spark { display: block; width: 100%; height: 34px; }
.spark path { fill: none; stroke: var(--accent); stroke-width: 1.5; }
.spark .area { fill: var(--accent); opacity: 0.12; stroke: none; }

.notes {
  margin: 10px 0 0;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--st-low-bg);
  border-left: 2px solid var(--st-low);
  font-size: 12px;
  color: var(--txt-dim);
}
.notes ul { margin: 0; padding-left: 16px; }

.back-link { font-size: 12.5px; color: var(--txt-dim); display: inline-block; margin-bottom: 8px; }

/* ----------------------------------------------------------------- toast */

.toasts {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(420px, calc(100vw - 28px));
}
.toast {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  box-shadow: var(--shadow);
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}
.toast[data-sev="error"] { border-left-color: var(--st-broken); }
.toast[data-sev="success"] { border-left-color: var(--st-ok); }

.skeleton {
  height: 60px;
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--panel) 25%, var(--panel-2) 50%, var(--panel) 75%);
  background-size: 300% 100%;
  animation: shimmer 1.2s infinite linear;
}
@keyframes shimmer { to { background-position: -150% 0; } }

/* ----------------------------------------------------------------- login */

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.login-box {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
}
.login-box h1 { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.login-box p { color: var(--txt-dim); font-size: 12.5px; margin: 0 0 16px; }
.login-box input { width: 100%; margin-bottom: 10px; padding: 8px 10px; }
.login-box .btn { width: 100%; justify-content: center; padding: 8px; }
.login-error {
  display: none;
  margin-top: 10px;
  padding: 7px 9px;
  border-radius: var(--radius-sm);
  background: var(--st-broken-bg);
  color: var(--st-broken);
  font-size: 12.5px;
}
.login-error.on { display: block; }

/* ------------------------------------------------------------ responsive */

@media (max-width: 760px) {
  main { padding: 10px; }
  .topbar-row { padding: 8px 10px; gap: 8px; }
  .brand { font-size: 13px; }
  .conn { font-size: 11px; padding: 3px 8px; }
  .table-wrap { max-height: none; }
  .cell-title { max-width: 180px; }
  .alert-card { flex-direction: column; }
  .alert-actions { flex-direction: row; flex-wrap: wrap; }
  .grid-4 { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .stat-value { font-size: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
