/* ══════════ token ══════════ */
:root {
  color-scheme: light;
  --page:           #f9f9f7;
  --surface:        #fcfcfb;
  --surface-2:      #f2f1ed;
  --text:           #0b0b0b;
  --text-2:         #52514e;
  --muted:          #898781;
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --border:         rgba(11, 11, 11, .10);
  --border-strong:  rgba(11, 11, 11, .16);
  --track:          #eceae4;

  --series-1:       #2a78d6;
  --series-2:       #eb6834;

  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;
  --good-text:      #006300;

  --radius:   14px;
  --radius-s: 9px;
  --shadow:   0 1px 2px rgba(11,11,11,.05), 0 8px 24px -12px rgba(11,11,11,.14);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page:          #0d0d0d;
    --surface:       #1a1a19;
    --surface-2:     #232322;
    --text:          #ffffff;
    --text-2:        #c3c2b7;
    --muted:         #898781;
    --grid:          #2c2c2a;
    --axis:          #383835;
    --border:        rgba(255,255,255,.10);
    --border-strong: rgba(255,255,255,.18);
    --track:         #262625;
    --series-1:      #3987e5;
    --series-2:      #d95926;
    --good-text:     #0ca30c;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page:          #0d0d0d;
  --surface:       #1a1a19;
  --surface-2:     #232322;
  --text:          #ffffff;
  --text-2:        #c3c2b7;
  --muted:         #898781;
  --grid:          #2c2c2a;
  --axis:          #383835;
  --border:        rgba(255,255,255,.10);
  --border-strong: rgba(255,255,255,.18);
  --track:         #262625;
  --series-1:      #3987e5;
  --series-2:      #d95926;
  --good-text:     #0ca30c;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
}

* { box-sizing: border-box; }

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

h1, h2, h3 { margin: 0; font-weight: 620; letter-spacing: -.01em; }
.spacer { flex: 1; }

/* ══════════ topbar ══════════ */
.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 20px;
  padding: 10px clamp(14px, 3vw, 32px);
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark {
  width: 36px; height: 36px; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 11px; font-size: 19px; flex: none;
}
.brand h1 { font-size: 17px; line-height: 1.15; }
.brand-sub {
  margin: 1px 0 0; font-size: 11.5px; color: var(--muted);
  display: flex; align-items: center; gap: 5px; white-space: nowrap;
}
.brand-sub::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--muted); flex: none;
}
.brand-sub.is-live::before { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent); }
.brand-sub.is-local::before { background: var(--warning); }

.tabs { display: flex; gap: 2px; margin-left: auto; background: var(--surface-2); padding: 3px; border-radius: 11px; border: 1px solid var(--border); }
.tab {
  appearance: none; border: 0; background: transparent; color: var(--text-2);
  font: inherit; font-size: 13.5px; font-weight: 520; padding: 6px 14px;
  border-radius: 8px; cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.10); }
.topbar-actions { display: flex; gap: 6px; align-items: center; }

/* ══════════ bottoni ══════════ */
.btn {
  appearance: none; font: inherit; font-size: 13.5px; font-weight: 540;
  padding: 8px 14px; border-radius: var(--radius-s); cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  transition: background .12s, border-color .12s, opacity .12s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .38; cursor: not-allowed; }
.btn-primary { background: var(--series-1); border-color: transparent; color: #fff; }
.btn-primary:hover { background: color-mix(in srgb, var(--series-1) 86%, #000); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.icon-btn { padding: 7px 10px; font-size: 15px; line-height: 1; }
.btn-danger-ghost { background: transparent; border-color: transparent; color: var(--critical); }
.btn-danger-ghost:hover { background: color-mix(in srgb, var(--critical) 12%, transparent); }

/* menu ⋯ */
.menu { position: relative; }
.menu > summary { list-style: none; display: inline-flex; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50;
  min-width: 250px; padding: 5px; background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow);
}
.menu-item {
  display: block; width: 100%; text-align: left; appearance: none; border: 0;
  background: transparent; font: inherit; font-size: 13.5px; color: var(--text);
  padding: 8px 11px; border-radius: 7px; cursor: pointer;
}
.menu-item:hover { background: var(--surface-2); }
.menu-item-danger { color: var(--critical); }
.menu-panel hr { border: 0; border-top: 1px solid var(--border); margin: 5px 8px; }

/* ══════════ layout ══════════ */
main { padding: clamp(16px, 3vw, 30px) clamp(14px, 3vw, 32px) 80px; max-width: 1360px; margin: 0 auto; }
.view { display: none; }
.view.is-active { display: block; animation: fade .22s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* minmax(0,…) e non 1fr: senza il minimo esplicito la traccia si allarga fino
   al min-content dei figli e la pagina scrolla in orizzontale sul telefono. */
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }
.grid-2 > * { min-width: 0; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 20px 20px; margin-bottom: 16px;
  box-shadow: var(--shadow); min-width: 0;
}
.grid-2 .card { margin-bottom: 0; }
.card-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.card-head h2 { font-size: 15px; }
.card-hint { font-size: 12.5px; color: var(--muted); }
.card-alert { border-color: color-mix(in srgb, var(--critical) 40%, var(--border)); }
.card-alert.is-quiet { border-color: var(--border); }

/* ══════════ KPI ══════════ */
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 16px; }
@media (max-width: 1000px) { .kpi-row { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 520px)  { .kpi-row { grid-template-columns: 1fr; } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.kpi::before { content: ''; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--muted); opacity: .5; }
.kpi-good::before { background: var(--good); opacity: 1; }
.kpi-critical::before { background: var(--critical); opacity: 1; }
.kpi-warning::before { background: var(--warning); opacity: 1; }
.kpi-label { margin: 0; font-size: 12.5px; color: var(--text-2); font-weight: 520; }
.kpi-value { margin: 6px 0 2px; font-size: clamp(24px, 3vw, 30px); font-weight: 640; letter-spacing: -.02em; }
.kpi-sub { margin: 0; font-size: 12px; color: var(--muted); }

/* ══════════ righe elenco ══════════ */
.row-item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  appearance: none; border: 0; border-top: 1px solid var(--border); background: transparent;
  font: inherit; color: var(--text); padding: 11px 4px; cursor: pointer;
}
.row-item:first-child { border-top: 0; }
.row-item:hover { background: var(--surface-2); }
.badge {
  width: 22px; height: 22px; flex: none; border-radius: 7px; display: grid; place-items: center;
  font-size: 12px; font-weight: 700; color: #fff;
}
.badge-good { background: var(--good); }
.badge-critical { background: var(--critical); }
.badge-warning { background: var(--warning); color: #3a2a00; }
.badge-neutral { background: var(--muted); }
.row-main { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.row-title { font-weight: 560; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-right { text-align: right; flex: none; display: flex; flex-direction: column; }
.row-amount { font-weight: 620; font-size: 14px; }
.ago { font-size: 11.5px; color: var(--muted); }

/* ══════════ toolbar & filtri ══════════ */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.input {
  font: inherit; font-size: 14px; padding: 8px 12px; border-radius: var(--radius-s);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); width: 100%;
}
.input:focus { outline: 2px solid color-mix(in srgb, var(--series-1) 55%, transparent); outline-offset: 1px; border-color: transparent; }
.input-search { max-width: 300px; }
textarea.input { resize: vertical; line-height: 1.45; }

.seg { display: flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  appearance: none; border: 0; background: transparent; font: inherit; font-size: 13px; font-weight: 500;
  color: var(--text-2); padding: 5px 11px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.seg button:hover { color: var(--text); }
.seg button.is-active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.10); }
.toolbar .btn-primary { margin-left: auto; }

/* ══════════ tabella ══════════ */
.table-scroll {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow-x: auto; box-shadow: var(--shadow);
}
.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; min-width: 860px; }
.tbl th {
  text-align: left; font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .045em;
  color: var(--muted); padding: 11px 14px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); cursor: pointer; user-select: none; white-space: nowrap;
}
.tbl th:hover { color: var(--text-2); }
.tbl th.is-sorted { color: var(--text); }
.tbl th.is-sorted::after { content: ' ↑'; }
.tbl th.is-sorted[data-dir="desc"]::after { content: ' ↓'; }
.tbl td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tbl tbody tr { cursor: pointer; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl tbody tr:last-child td { border-bottom: 0; }
.num, .num-date { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.num-date { text-align: left; color: var(--text-2); }
.strong { font-weight: 620; }
.neg { color: var(--critical); font-weight: 620; }
.cell-name { max-width: 320px; }
.cell-title { display: block; font-weight: 560; }
.cell-sub { display: block; font-size: 11.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-action { width: 26px; color: var(--muted); text-align: right; }
.chev { font-size: 17px; }
.tag-lav {
  font-size: 9.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  border: 1px solid var(--border-strong); border-radius: 4px; padding: 0 3px; margin-left: 5px;
}
.who { font-weight: 560; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.who::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.who-valerio::before { background: var(--series-1); }
.who-anna::before { background: var(--series-2); }

.pill {
  display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 560;
  padding: 3px 9px; border-radius: 20px; white-space: nowrap;
  border: 1px solid var(--border-strong); color: var(--text-2);
}
.pill-good     { color: var(--good-text);  border-color: color-mix(in srgb, var(--good) 45%, transparent);     background: color-mix(in srgb, var(--good) 10%, transparent); }
.pill-critical { color: var(--critical);   border-color: color-mix(in srgb, var(--critical) 50%, transparent); background: color-mix(in srgb, var(--critical) 10%, transparent); }
.pill-warning  { color: var(--text);       border-color: color-mix(in srgb, var(--warning) 60%, transparent);  background: color-mix(in srgb, var(--warning) 16%, transparent); }
.pill-neutral  { color: var(--muted); }
.table-foot { font-size: 12.5px; color: var(--muted); margin: 12px 2px 0; }
.table-foot strong { color: var(--text); font-weight: 600; }

/* ══════════ promo ══════════ */
.promo-strip { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; }
.promo-chip {
  appearance: none; text-align: left; cursor: pointer; flex: none; min-width: 190px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-s);
  padding: 11px 13px; font: inherit; color: var(--text);
}
.promo-chip:hover { border-color: var(--border-strong); }
.promo-chip.is-urgent { border-color: color-mix(in srgb, var(--warning) 65%, transparent); background: color-mix(in srgb, var(--warning) 9%, var(--surface-2)); }
.promo-chip-name { display: block; font-weight: 560; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.promo-chip-meta { display: block; font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.promo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; }
.promo-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.promo-card.is-expired { opacity: .58; }
.promo-card-head { display: flex; align-items: flex-start; gap: 10px; }
.promo-card-head h3 { font-size: 14.5px; flex: 1; }
.promo-dates { margin: 6px 0 0; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.promo-cond {
  margin: 10px 0 0; font-size: 13px; color: var(--text-2); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.promo-facts { margin: 12px 0 0; display: grid; gap: 7px; }
.promo-facts div { display: grid; grid-template-columns: 46px minmax(0, 1fr); gap: 8px; }
.promo-facts dt { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding-top: 2px; }
.promo-facts dd {
  margin: 0; font-size: 12.5px; color: var(--text-2); line-height: 1.45;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.promo-card-foot { display: flex; align-items: center; gap: 7px; margin-top: auto; padding-top: 14px; flex-wrap: wrap; }
.promo-residue { font-size: 11.5px; color: var(--text-2); font-weight: 540; }
.promo-residue.is-zero { color: var(--muted); }

/* ══════════ grafici ══════════ */
.chart { display: block; overflow: visible; }
.chart .tick { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .row-label { font-size: 12px; fill: var(--text-2); }
.chart .row-label-strong { font-size: 14px; fill: var(--text); font-weight: 600; }
.chart .row-sub { font-size: 11px; fill: var(--muted); }
.chart .row-value { font-size: 12px; fill: var(--text-2); font-variant-numeric: tabular-nums; }
.chart .row-value-lg { font-size: 14px; fill: var(--text); font-weight: 620; font-variant-numeric: tabular-nums; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; font-size: 12px; color: var(--text-2); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 26px 12px; margin: 0; line-height: 1.6; }

.tooltip {
  position: fixed; z-index: 90; pointer-events: none;
  background: var(--text); color: var(--page);
  padding: 8px 11px; border-radius: 9px; font-size: 12.5px; line-height: 1.45;
  box-shadow: 0 8px 24px -8px rgba(0,0,0,.5); max-width: 260px;
}
.tooltip .tip-muted { opacity: .68; }

/* ══════════ modale ══════════ */
.modal {
  border: 1px solid var(--border-strong); border-radius: 18px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(680px, calc(100vw - 28px)); max-height: calc(100vh - 60px);
  box-shadow: 0 24px 64px -20px rgba(0,0,0,.5);
}
.modal::backdrop { background: rgba(0,0,0,.42); backdrop-filter: blur(2px); }
.modal form { display: flex; flex-direction: column; max-height: calc(100vh - 60px); }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 16px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 13px 18px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 18px 18px; }
.modal-note { margin: 14px 0 0; font-size: 12.5px; color: var(--text-2); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-s); padding: 9px 12px; }

.fld-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 13px; }
@media (max-width: 560px) { .fld-grid { grid-template-columns: minmax(0, 1fr); } }
.fld { display: flex; flex-direction: column; gap: 5px; }
.fld-wide { grid-column: 1 / -1; }
.fld-label { font-size: 12px; font-weight: 560; color: var(--text-2); }
.fld-hint { font-size: 11px; color: var(--muted); }
.fld-check {
  flex-direction: row; align-items: center; gap: 9px; font-size: 13.5px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-s); padding: 9px 12px;
  cursor: pointer; align-self: end;
}
.fld-check input { width: 16px; height: 16px; accent-color: var(--series-1); cursor: pointer; }

/* ══════════ schermata di accesso ══════════ */
.auth-gate {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: var(--page); padding: 20px;
}
.auth-gate[hidden] { display: none; }
.auth-card {
  width: min(370px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 30px 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 13px;
}
.auth-mark {
  width: 46px; height: 46px; display: grid; place-items: center; font-size: 24px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 13px;
}
.auth-card h2 { font-size: 20px; margin-top: 4px; }
.auth-sub { margin: -9px 0 6px; font-size: 13.5px; color: var(--muted); }
.auth-submit { margin-top: 5px; padding: 10px; }
.auth-error {
  margin: 0; font-size: 12.5px; color: var(--critical); font-weight: 540;
  background: color-mix(in srgb, var(--critical) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--critical) 35%, transparent);
  border-radius: var(--radius-s); padding: 8px 11px;
}
.auth-error[hidden] { display: none; }
body.is-locked { overflow: hidden; }

/* ══════════ toast ══════════ */
.toast-host { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--text); color: var(--page); padding: 10px 18px; border-radius: 22px;
  font-size: 13.5px; font-weight: 520; box-shadow: 0 8px 24px -8px rgba(0,0,0,.5);
  animation: rise .22s ease;
}
.toast-error { background: var(--critical); color: #fff; }
.toast.is-out { opacity: 0; transition: opacity .3s; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ══════════ mobile ══════════ */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .tabs { order: 3; width: 100%; margin-left: 0; overflow-x: auto; }
  .tab { flex: 1; text-align: center; }
  .topbar-actions { margin-left: auto; }
  .input-search { max-width: none; flex: 1 1 100%; }
  .toolbar .btn-primary { margin-left: 0; flex: 1 1 100%; }
  .seg { overflow-x: auto; }
}
