/* ══════════ 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;

  /* Slot categorici per le categorie di spesa. NON sono colori di stato:
     --good vuol già dire "sotto budget", e la stessa tinta non può
     significare anche "Cibo". Bande di luminosità, croma e separazione per
     daltonismo sono quelle validate in ratbacks; le ultime due estendono la
     scala perché qui le categorie possono arrivare a otto. */
  --series-1:       #2a78d6;
  --series-2:       #eb6834;
  --series-3:       #1baf7a;
  --series-4:       #eda100;
  --series-5:       #e87ba4;
  --series-6:       #008300;
  --series-7:       #8a6ede;
  --series-8:       #00868f;

  --good:           #0ca30c;
  --warning:        #fab219;
  --serious:        #ec835a;
  --critical:       #d03b3b;
  --bad:            #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;
    --series-3:      #199e70;
    --series-4:      #c98500;
    --series-5:      #d55181;
    --series-6:      #008300;
    --series-7:      #8d74e0;
    --series-8:      #0e8f98;
    --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;
  --series-3:      #199e70;
  --series-4:      #c98500;
  --series-5:      #d55181;
  --series-6:      #008300;
  --series-7:      #8d74e0;
  --series-8:      #0e8f98;
  --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; }

/* iOS ingrandisce da sé il testo quando il telefono passa in orizzontale.
   Lo spazio della barra di scorrimento resta prenotato anche quando la pagina
   non scorre: senza, aprendo una modale tutto quello che sta dietro slittava. */
html { -webkit-text-size-adjust: 100%; scrollbar-gutter: stable; }

/* L'attributo hidden deve valere anche quando una classe dichiara un display.
   `[hidden]` è più debole di qualsiasi classe: basta un `display: flex` perché
   un elemento nascosto resti visibile. Non toglierla, e non aggiungere regole
   [hidden] per singolo elemento. */
[hidden] { display: none !important; }

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: 46px; height: 46px; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 13px; font-size: 25px; flex: none;
}
.brand h1 { font-size: 24px; line-height: 1.1; letter-spacing: -0.015em; }
.brand-sub {
  margin: 3px 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 { 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: 260px; 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) 90px; max-width: 1360px; margin: 0 auto; }
@supports (padding: max(0px)) {
  main, .topbar {
    padding-left: max(clamp(14px, 3vw, 32px), env(safe-area-inset-left));
    padding-right: max(clamp(14px, 3vw, 32px), env(safe-area-inset-right));
  }
}
.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 scorre in orizzontale sul telefono. */
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.grid-2-large { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); }
@media (max-width: 900px) { .grid-2, .grid-2-large { 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-actions { margin-left: auto; display: flex; gap: 6px; align-items: center; }

/* ══════════ KPI ══════════ */
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 16px; }
.kpi-row-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 1000px) { .kpi-row, .kpi-row-3 { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 520px)  { .kpi-row, .kpi-row-3 { 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(23px, 3vw, 29px); font-weight: 640; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-sub { margin: 0; font-size: 12px; color: var(--muted); }
.kpi-sub .su { color: var(--critical); font-weight: 560; }
.kpi-sub .giu { color: var(--good-text); font-weight: 560; }

/* ══════════ scelta del mese ══════════ */
.periodo { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.periodo-nav {
  display: flex; align-items: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-s); padding: 2px;
}
.periodo-nav button {
  appearance: none; border: 0; background: transparent; font: inherit; font-size: 15px;
  color: var(--text-2); cursor: pointer; padding: 5px 11px; border-radius: 7px; line-height: 1;
}
.periodo-nav button:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.periodo-nav button:disabled { opacity: .3; cursor: not-allowed; }
.periodo-nome {
  font-size: 17px; font-weight: 620; letter-spacing: -.01em; min-width: 168px;
  text-align: center; text-transform: capitalize;
}
.periodo .btn { margin-left: auto; }

/* ══════════ 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); }
.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; font-variant-numeric: tabular-nums; }
.punto {
  width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: var(--cat-colore, var(--muted));
}

/* ══════════ categorie con budget ══════════ */
.cat-lista { display: grid; gap: 3px; }
.cat-riga {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 3px 12px;
  align-items: baseline; padding: 9px 4px; border-top: 1px solid var(--border);
  width: 100%; text-align: left; appearance: none; background: transparent;
  border-left: 0; border-right: 0; border-bottom: 0; font: inherit; color: var(--text); cursor: pointer;
}
.cat-riga:first-child { border-top: 0; }
.cat-riga:hover { background: var(--surface-2); }
.cat-nome { display: inline-flex; align-items: center; gap: 8px; font-weight: 550; font-size: 13.5px; min-width: 0; }
.cat-nome span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-val { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 13.5px; white-space: nowrap; }
.cat-barra {
  grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--track);
  position: relative; overflow: hidden;
}
.cat-barra i { position: absolute; inset: 0 auto 0 0; border-radius: 3px; background: var(--cat-colore, var(--series-1)); }
.cat-sotto { grid-column: 1 / -1; font-size: 11.5px; color: var(--muted); }
.cat-sotto.is-sopra { color: var(--critical); font-weight: 540; }

/* ══════════ 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; }
.cerca { position: relative; display: inline-flex; max-width: 300px; flex: 1 1 auto; min-width: 0; }
.cerca .input { width: 100%; max-width: none; padding-right: 32px; }
.cerca-x {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  appearance: none; border: 0; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 12px; line-height: 1;
  width: 24px; height: 24px; border-radius: 50%;
}
.cerca-x:hover { color: var(--text); background: var(--surface-2); }
.cerca .input::-webkit-search-cancel-button { display: none; }
textarea.input { resize: vertical; line-height: 1.45; }

/* Sedici pixel non è una scelta estetica: sotto quella misura iOS ingrandisce
   la pagina appena si mette il cursore in un campo, e da lì la si può spostare
   di lato — è così che una modale finisce con le etichette fuori schermo. */
@media (pointer: coarse) {
  .input, .input-mono { font-size: 16px; }
  /* Il campo data su iOS è un controllo di sistema che si misura da sé, e con
     i 16px qui sopra sfondava il bordo destro della modale. */
  .input[type="date"] { -webkit-appearance: none; appearance: none; }
  .input[type="date"]::-webkit-date-and-time-value { text-align: left; margin: 0; }
}
.input[type="date"] { min-width: 0; max-width: 100%; }

.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;
  display: inline-flex; align-items: center; gap: 6px;
}
.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); }
.seg-menu { padding: 0; border: 0; background: transparent; }
.seg-menu .input { font-size: 13px; padding: 6px 10px; }
.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: 760px; }
.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; }
.entrata { color: var(--good-text); font-weight: 620; }
.cell-name { max-width: 340px; }
.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; }
.cat { font-weight: 540; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
/* il colore arriva da JS, dallo stesso ordine che colora i grafici: una
   categoria scritta nel CSS non saprebbe niente dell'ottava */
.cat::before {
  content: ''; width: 8px; height: 8px; border-radius: 50%; flex: none;
  background: var(--cat-colore, var(--muted));
}

.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); }
.pill-muted    { color: var(--muted); border-style: dashed; }
.riga-vuota td { text-align: center; color: var(--muted); font-size: 13px; padding: 34px 16px; }
.riga-vuota:hover td { background: transparent; }
.table-foot { font-size: 12.5px; color: var(--muted); margin: 12px 2px 0; }
.table-foot:empty { display: none; }
.table-foot strong { color: var(--text); font-weight: 600; }

/* ══════════ progetti (investimenti e viaggi) ══════════ */
.prog-lista { display: grid; gap: 8px; }
.prog {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 4px 12px; align-items: center; width: 100%; text-align: left;
  appearance: none; border: 1px solid var(--border); background: var(--surface-2);
  border-radius: var(--radius-s); padding: 11px 13px; font: inherit; color: var(--text); cursor: pointer;
}
.prog:hover { border-color: var(--border-strong); }
.prog-nome { font-weight: 560; font-size: 13.5px; overflow-wrap: anywhere; }
.prog-importo { font-variant-numeric: tabular-nums; font-weight: 620; white-space: nowrap; }
.prog.is-stima { border-style: dashed; }
.prog.is-stima .prog-importo { color: var(--text-2); }
.prog-meta { grid-column: 1 / -1; font-size: 11.5px; color: var(--muted); }

/* ══════════ abbonamenti ══════════ */
.abb {
  display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) 90px auto auto;
  gap: 8px; align-items: center; margin-bottom: 9px;
}
.abb-attivo {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px;
  color: var(--text-2); white-space: nowrap; cursor: pointer;
}
.abb-attivo input { width: 15px; height: 15px; accent-color: var(--series-1); cursor: pointer; }
@media (max-width: 620px) {
  .abb { grid-template-columns: minmax(0, 1fr) 88px auto auto; }
  .abb .a-cat { grid-column: 1 / -1; }
}

/* ══════════ grafici ══════════ */
.chart { display: block; overflow: visible; }
.chart .tick { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .tick-strong { font-size: 11px; fill: var(--text-2); font-weight: 600; font-variant-numeric: tabular-nums; }
.chart .row-label { font-size: 12px; fill: var(--text-2); }
.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 .donut-value { font-size: 21px; fill: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }
.legend { display: flex; gap: 14px; 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));
  box-shadow: 0 24px 64px -20px rgba(0,0,0,.5);
  /* L'altezza massima sta sul form, non qui: su tutti e due, il bordo di 1px
     rendeva il form più alto dello spazio interno e su Safari comparivano due
     barre di scorrimento. Niente display:flex sullo <dialog>, che su Safari fa
     collassare il form. */
  overflow: hidden;
}
.modal::backdrop { background: rgba(0,0,0,.42); backdrop-filter: blur(2px); }
/* dvh e non vh: su iOS 100vh è il viewport con la barra degli indirizzi già
   ritratta, e i pulsanti in fondo — "Salva" compreso — restavano fuori. */
.modal form { display: flex; flex-direction: column; max-height: calc(100dvh - 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; overflow-x: hidden; overscroll-behavior: contain; overflow-wrap: anywhere; }
.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; }
.modal-stretto { width: min(520px, calc(100vw - 28px)); }
.modal-largo { width: min(860px, calc(100vw - 28px)); }

.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-section {
  grid-column: 1 / -1; display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
  margin: 12px 0 -2px; padding-bottom: 7px; border-bottom: 1px solid var(--border);
}
.fld-section:first-child { margin-top: 0; }
.fld-section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-2); }
.fld-section span { font-size: 11.5px; color: var(--muted); }
.input-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.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; }

/* il conto di quanto verrà davvero registrato, sotto i campi che lo compongono:
   lordo × quantità × quota è una moltiplicazione che nessuno deve rifare a mente */
.calcolo {
  grid-column: 1 / -1; margin: 2px 0 0; padding: 10px 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-s);
  font-size: 12.5px; color: var(--text-2); display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
}
.calcolo strong { font-size: 16px; color: var(--text); font-variant-numeric: tabular-nums; }

/* campo proposto dal modello e non ancora verificato */
.fld.is-proposto > .input,
.fld.is-proposto.fld-check {
  border-color: color-mix(in srgb, var(--warning) 65%, transparent);
  background: color-mix(in srgb, var(--warning) 9%, var(--surface));
}
.fld.is-proposto > .fld-label::after {
  content: ' proposto';
  font-size: 10px; text-transform: uppercase; letter-spacing: .05em;
  color: color-mix(in srgb, var(--warning) 80%, var(--text-2));
}

/* ══════════ acquisizione: foto scontrino / estratto conto ══════════ */
.drop {
  display: flex; align-items: center; gap: 14px; margin-bottom: 16px;
  padding: 13px 15px; border-radius: var(--radius-s);
  border: 1px dashed var(--border-strong); background: var(--surface-2);
  transition: border-color .15s, background .15s;
}
.drop.is-over { border-color: var(--series-1); background: color-mix(in srgb, var(--series-1) 8%, var(--surface-2)); }
.drop.is-busy { opacity: .6; pointer-events: none; }
.drop-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.drop-main strong { font-size: 13.5px; font-weight: 580; }
.drop-main span { font-size: 11.5px; color: var(--muted); line-height: 1.4; }

.esito {
  margin: -8px 0 16px; font-size: 12.5px; line-height: 1.5;
  padding: 9px 12px; border-radius: var(--radius-s);
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2);
}
.esito.is-ok { border-color: color-mix(in srgb, var(--good) 45%, transparent); background: color-mix(in srgb, var(--good) 10%, transparent); }
.esito.is-errore { border-color: color-mix(in srgb, var(--critical) 45%, transparent); background: color-mix(in srgb, var(--critical) 10%, transparent); color: var(--critical); }
.esito.is-attesa, .esito.is-avviso {
  border-color: color-mix(in srgb, var(--warning) 50%, transparent);
  background: color-mix(in srgb, var(--warning) 12%, transparent);
}

/* barra d'attesa mentre il modello legge: sostituisce la percentuale che non
   possiamo conoscere con qualcosa che dice soltanto "sta lavorando" */
.pista {
  display: block; position: relative; height: 4px; margin-top: 9px;
  overflow: hidden; border-radius: 3px; background: var(--track);
}
.pista i {
  position: absolute; top: 0; bottom: 0; width: 34%; border-radius: 3px;
  background: var(--warning); animation: pista-va 1.15s ease-in-out infinite;
}
@keyframes pista-va { from { left: -36%; } to { left: 102%; } }
@media (prefers-reduced-motion: reduce) { .pista i { animation: none; left: 0; width: 100%; opacity: .5; } }

/* righe proposte dall'estratto conto: si spuntano una per una prima di salvare */
.proposte { display: grid; gap: 6px; max-height: 52vh; overflow-y: auto; margin-top: 12px; }
.proposta {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 4px 10px; align-items: center;
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-s);
  background: var(--surface-2);
}
.proposta.is-doppione { border-style: dashed; opacity: .72; }
.proposta input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--series-1); cursor: pointer; }
.proposta-desc { font-size: 13px; font-weight: 540; overflow-wrap: anywhere; }
.proposta-imp { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.proposta-meta { grid-column: 2 / -1; font-size: 11.5px; color: var(--muted); }
.proposta-meta select { font-size: 11.5px; padding: 2px 5px; }

/* personale / lavoro: due pulsanti e non una casella, perché la domanda ha due
   risposte pari e una casella ne farebbe una la normale e l'altra l'eccezione.
   I 16px sul carattere non sono estetica: sotto quella misura iOS ingrandisce
   la pagina al primo tocco, e da lì la modale si sposta di lato. */
.scelta { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; vertical-align: middle; }
.scelta-btn {
  font: inherit; font-size: 11.5px; line-height: 1; padding: 4px 10px;
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.scelta-btn + .scelta-btn { border-left: 1px solid var(--border); }
.scelta-btn.is-on { background: var(--series-1); color: #fff; font-weight: 600; }
@media (hover: none) { .scelta-btn { font-size: 16px; padding: 6px 14px; } }

/* una riga anticipata per lavoro: c'è, si vede, ma non fa parte del totale */
.tbl tr.is-anticipata .c-importo { color: var(--muted); font-weight: 500; }
.tag {
  display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 999px;
  font-size: 10.5px; font-weight: 600; text-transform: lowercase;
  background: var(--surface-3, var(--surface-2)); color: var(--muted);
  border: 1px solid var(--border);
}
.campo-inline { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
.tag-proposto { background: color-mix(in srgb, var(--series-1) 22%, transparent); color: var(--series-1); border-color: transparent; }

/* la card dell'auto: quattro numeri, che sul telefono diventano due colonne */
.auto-griglia { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr)); gap: 14px; }
.auto-voce { display: grid; gap: 2px; min-width: 0; }
.auto-et { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.auto-val { font-size: 20px; font-weight: 640; font-variant-numeric: tabular-nums; }
.auto-sotto { font-size: 11.5px; color: var(--muted); overflow-wrap: anywhere; }
.auto-conf { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.auto-conf input { width: 100px; }
.campo-inline > span { font-size: 12px; color: var(--muted); white-space: nowrap; }
.nota { margin: 10px 2px 0; font-size: 12.5px; color: var(--text-2); }

/* ══════════ analisi scritta ══════════ */
.analisi { font-size: 13.5px; line-height: 1.62; color: var(--text-2); }
.analisi p { margin: 0 0 10px; }
.analisi p:last-child { margin-bottom: 0; }
.analisi strong { color: var(--text); font-weight: 600; }
.analisi ul { margin: 0 0 10px; padding-left: 18px; display: grid; gap: 5px; }

/* ══════════ schermate bloccanti ══════════ */
.gate {
  position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: var(--page); padding: 20px;
}
.gate-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;
}
.gate-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;
}
.gate-card h2 { font-size: 20px; margin-top: 4px; }
.gate-sub { margin: -9px 0 6px; font-size: 13.5px; color: var(--muted); }
.gate-submit { margin-top: 5px; padding: 10px; }
.gate-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;
}
body.is-locked { overflow: hidden; }
html:has(.modal[open]) { overflow: hidden; }

/* ══════════ toast ══════════ */
.toast-host { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); 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; max-width: min(92vw, 460px); text-align: center;
}
.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; } }

/* ══════════ pulsante di inserimento sempre a portata ══════════ */
.fab {
  position: fixed; right: max(18px, env(safe-area-inset-right));
  bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 60;
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--series-1); color: #fff; font-size: 27px; line-height: 1;
  box-shadow: 0 8px 24px -6px rgba(0,0,0,.45); cursor: pointer;
}
.fab:hover { background: color-mix(in srgb, var(--series-1) 86%, #000); }
@media (min-width: 761px) { .fab { display: 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; padding: 9px 14px; }
  .topbar-actions { margin-left: auto; }
  .cerca { max-width: none; flex: 1 1 100%; }
  .toolbar .btn-primary { margin-left: 0; flex: 1 1 100%; }
  .seg { overflow-x: auto; }
  .periodo-nome { min-width: 0; flex: 1; }

  .input, input, select, textarea { font-size: 16px; }

  /* bersagli per un dito, non per un puntatore */
  .seg button { padding: 9px 13px; }
  .btn { padding: 10px 15px; }
  .btn-sm { padding: 8px 13px; }
  .icon-btn { min-width: 42px; min-height: 42px; }

  /* ─── la tabella diventa un elenco di schede ───
     Sei colonne su un telefono erano una striscia da trascinare di lato per
     leggere una riga sola. Le stesse celle, impilate: descrizione e importo in
     alto, categoria e data sotto. Il dettaglio resta a un tocco. */
  .table-scroll { background: transparent; border: 0; box-shadow: none; overflow-x: visible; }
  #tbl-mov { min-width: 0; }
  #tbl-mov thead { display: none; }
  #tbl-mov tbody tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    gap: 5px 12px; align-items: center;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 12px 14px; margin-bottom: 10px;
  }
  #tbl-mov tbody tr:hover { background: var(--surface); }
  #tbl-mov td { border: 0; padding: 0; }
  #tbl-mov .c-desc    { grid-area: 1 / 1; }
  #tbl-mov .c-importo { grid-area: 1 / 2; font-size: 15px; }
  #tbl-mov .c-cat     { grid-area: 2 / 1; }
  #tbl-mov .c-data    { grid-area: 2 / 2; text-align: right; font-size: 12px; color: var(--muted); }
  #tbl-mov .c-note,
  #tbl-mov .c-chev { display: none; }
  #tbl-mov .cell-name { max-width: none; }
  #tbl-mov tbody tr.riga-vuota { display: block; padding: 22px 14px; text-align: center; }
  .table-foot { text-align: center; }
}

@media (hover: none) {
  * { -webkit-tap-highlight-color: transparent; }
  .tbl tbody tr:active { background: var(--surface-2); }
  .prog:active, .cat-riga:active { background: var(--surface-2); }
}
