/* Palette dati validata con lo script six-checks (light e dark, tutti PASS; in chiaro
   aqua, giallo e magenta sono sotto 3:1 → ogni grafico ha legenda e tabella). */
:root {
  color-scheme: light;
  --page:            #f9f9f7;
  --surface:         #fcfcfb;
  --surface-sunken:  #f2f1ec;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --muted:           #898781;
  --grid:            #e1e0d9;
  --baseline:        #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);

  /* un colore per ETF, in ordine fisso: il colore segue lo strumento, mai il rango */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;

  --accent:          #2a78d6;
  --accent-track:    #cde2fb;
  --under:           #2a78d6;   /* sottopeso (polo freddo) */
  --over:            #e34948;   /* sovrappeso (polo caldo) */
  --good:            #0ca30c;
  --warning:         #fab219;
  --critical:        #d03b3b;
  --success-text:    #006300;

  --radius:   10px;
  --radius-s: 6px;
  --shadow:   0 1px 2px rgba(11, 11, 11, 0.04), 0 1px 8px rgba(11, 11, 11, 0.04);
  --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-sunken: #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --muted:          #898781;
    --grid:           #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --accent:         #3987e5;
    --accent-track:   #184f95;
    --under:          #3987e5;
    --over:           #e66767;
    --success-text:   #0ca30c;
    --shadow:         0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
/* le regole display dei componenti non devono scavalcare l'attributo hidden */
[hidden] { display: none !important; }

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

/* ------------------------------------------------------------------ layout */
.wrap { width: 100%; max-width: 1480px; margin-inline: auto; padding-inline: 28px; }

.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 24px; padding-block: 14px 12px; }
.brand { font-size: 15px; font-weight: 650; letter-spacing: -0.01em; }
.brand span { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
.stato-quotazioni { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 12.5px; color: var(--text-secondary); }
.stato-quotazioni .punto { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.stato-quotazioni .punto.ok { background: var(--good); }
.stato-quotazioni .punto.parziale { background: var(--warning); }
.stato-quotazioni .punto.errore { background: var(--critical); }
.stato-quotazioni .punto.carica { background: var(--accent); animation: pulsa 1s ease-in-out infinite; }
@keyframes pulsa { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .stato-quotazioni .punto.carica { animation: none; } }

.tabs { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.tabs-inner { display: flex; gap: 2px; overflow-x: auto; }
.tab {
  appearance: none; border: 0; background: none; cursor: pointer;
  font: inherit; font-weight: 500; color: var(--text-secondary);
  padding: 11px 14px; border-bottom: 2px solid transparent; white-space: nowrap; border-radius: 0;
}
.tab:hover { color: var(--text-primary); filter: none; }
.tab[aria-selected="true"] { color: var(--text-primary); border-bottom-color: var(--accent); font-weight: 600; }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: var(--radius-s); }

main { padding-block: 18px 64px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; margin-bottom: 18px; box-shadow: var(--shadow); min-width: 0;
}
.card > h2, .card-head h2 { margin: 0 0 4px; font-size: 14px; font-weight: 650; letter-spacing: -0.01em; }
.card > .sub, .card-head .sub { margin: 0 0 14px; font-size: 12.5px; color: var(--muted); }
.card-head { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: flex-start; justify-content: space-between; }
.card-head > div:first-child { min-width: 0; flex: 1 1 260px; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr)); gap: 18px; }
.cols > .card { margin-bottom: 0; }
.cols + .card, .cols + .cols { margin-top: 18px; }

/* ------------------------------------------------------------------ filtri */
.filtri {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px;
  padding: 12px 0 14px; margin-bottom: 4px;
}
.gruppo-filtro { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.gruppo-filtro > .etichetta { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.segmenti { display: inline-flex; background: var(--surface-sunken); border: 1px solid var(--border); border-radius: var(--radius-s); padding: 2px; }
.segmenti button {
  background: none; border: 0; color: var(--text-secondary); font-weight: 550; font-size: 12.5px;
  padding: 5px 10px; border-radius: 4px;
}
.segmenti button:hover { color: var(--text-primary); background: var(--surface); filter: none; }
.segmenti button[aria-pressed="true"] { background: var(--surface); color: var(--text-primary); font-weight: 650; box-shadow: var(--shadow); }
.intervallo { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.intervallo input { width: 138px; padding: 5px 8px; font-size: 12.5px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-etf {
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
  font-size: 12.5px; font-weight: 550; padding: 4px 10px 4px 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-secondary);
}
.chip-etf:hover { filter: none; border-color: var(--baseline); color: var(--text-primary); }
.chip-etf .key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.chip-etf[aria-pressed="false"] { background: transparent; color: var(--muted); border-style: dashed; }
.chip-etf[aria-pressed="false"] .key { background: transparent !important; box-shadow: inset 0 0 0 1.5px var(--baseline); }
.link-btn { background: none; border: 0; padding: 2px 4px; color: var(--accent); font-size: 12.5px; font-weight: 550; }
.link-btn:hover { text-decoration: underline; filter: none; }

/* --------------------------------------------------------------- hero e tile */
.hero { display: grid; grid-template-columns: minmax(260px, 1.2fr) repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 18px; }
.hero > * { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.hero dt { margin: 0; font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.hero dd { margin: 6px 0 0; font-size: 24px; font-weight: 640; letter-spacing: -0.025em; }
.hero .principale dd { font-size: 40px; line-height: 1.1; }
.hero .foot { margin-top: 4px; font-size: 12.5px; color: var(--text-secondary); }
.hero .foot strong { font-weight: 650; }

/* verde/rosso semantico: solo dove il verso significa davvero bene/male (P/L) */
.pos { color: var(--success-text); }
.neg { color: var(--critical); }
/* coppia divergente: direzione rispetto al target, senza giudizio di valore */
.under { color: var(--under); }
.over  { color: var(--over); }

/* ------------------------------------------------------------------ grafici */
.viz { position: relative; width: 100%; outline: none; transition: opacity 0.15s; }
.viz:focus-visible { box-shadow: 0 0 0 2px var(--accent); border-radius: 4px; }
.viz.attesa { opacity: 0.45; }
.viz svg { display: block; overflow: visible; }
.viz text { font-family: var(--font); font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.viz .griglia line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.viz .asse { stroke: var(--baseline); stroke-width: 1; shape-rendering: crispEdges; }
.viz .zero { stroke: var(--baseline); stroke-width: 1; shape-rendering: crispEdges; }
.viz .linea { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.viz .area { stroke: none; fill-opacity: 0.1; }
.viz .croce { stroke: var(--text-secondary); stroke-width: 1; opacity: 0.6; shape-rendering: crispEdges; }
.viz .etichetta-finale { fill: var(--text-secondary); font-weight: 600; font-size: 11.5px; }
.viz .punto-hover { stroke: var(--surface); stroke-width: 2; }
.viz .vuoto { fill: var(--muted); font-size: 12.5px; }

.legenda { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12.5px; color: var(--text-secondary); margin: 0 0 10px; }
.legenda span { display: inline-flex; align-items: center; gap: 7px; }
.legenda .k-linea { width: 14px; height: 2px; border-radius: 2px; flex: none; }
.legenda .k-box { width: 10px; height: 10px; border-radius: 2px; flex: none; }
.legenda .k-area { width: 14px; height: 10px; border-radius: 2px; flex: none; opacity: 0.18; }

.viz-tip {
  position: fixed; z-index: 60; pointer-events: none; min-width: 150px; max-width: 320px;
  background: var(--surface); color: var(--text-primary); border: 1px solid var(--border);
  border-radius: var(--radius-s); box-shadow: 0 6px 24px rgba(0, 0, 0, 0.16);
  padding: 8px 10px; font-size: 12px; opacity: 0; transition: opacity 0.1s;
}
.viz-tip.on { opacity: 1; }
.viz-tip .t { color: var(--muted); font-size: 11.5px; margin-bottom: 4px; }
.viz-tip .r { display: grid; grid-template-columns: 14px auto 1fr; align-items: center; gap: 7px; padding: 1px 0; }
.viz-tip .r .k { width: 12px; height: 2px; border-radius: 2px; }
.viz-tip .r strong { font-variant-numeric: tabular-nums; font-weight: 650; }
.viz-tip .r span:last-child { color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* barre orizzontali (allocazione, rate, ribilanciamento) */
.barre { display: grid; grid-template-columns: minmax(120px, 190px) 1fr; gap: 9px 14px; align-items: center; }
.barre .nome { font-size: 12.5px; color: var(--text-secondary); line-height: 1.25; display: flex; align-items: center; gap: 8px; min-width: 0; }
.barre .nome .key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.barre .nome span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.traccia { position: relative; height: 22px; }
.traccia .fill { position: absolute; left: 0; top: 4px; height: 14px; border-radius: 0 4px 4px 0; min-width: 2px; }
.traccia .target { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--text-primary); opacity: 0.75; border-radius: 1px; }
.traccia .valore { position: absolute; top: 50%; transform: translateY(-50%); font-size: 12px; font-weight: 600; color: var(--text-secondary); white-space: nowrap; font-variant-numeric: tabular-nums; padding-left: 6px; }
.traccia .asse0 { position: absolute; left: 0; top: 0; bottom: 0; width: 1px; background: var(--baseline); }
.traccia:hover .fill { filter: brightness(1.12); }

/* manubrio del ribilanciamento: oggi (anello) → dopo (pieno), segno = target */
.manubrio { position: relative; height: 22px; }
.manubrio .linea-base { position: absolute; left: 0; right: 0; top: 50%; height: 1px; background: var(--grid); }
.manubrio .tratto { position: absolute; top: 50%; height: 2px; margin-top: -1px; background: var(--baseline); }
.manubrio .dot { position: absolute; top: 50%; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%; box-shadow: 0 0 0 2px var(--surface); }
.manubrio .dot.oggi { background: var(--surface) !important; border: 2px solid; }
.manubrio .target { position: absolute; top: 1px; bottom: 1px; width: 2px; margin-left: -1px; background: var(--text-primary); opacity: 0.75; }

.meter { position: relative; height: 16px; border-radius: 4px; background: var(--accent-track); overflow: visible; margin: 22px 0 8px; }
.meter .fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); border-radius: 4px 0 0 4px; }
.meter .target { position: absolute; top: -5px; bottom: -5px; width: 2px; margin-left: -1px; background: var(--text-primary); }
.meter .target::after { content: attr(data-label); position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); font-size: 11px; color: var(--text-secondary); white-space: nowrap; padding-bottom: 2px; }
.meter-legenda { display: flex; justify-content: space-between; gap: 12px; font-size: 12.5px; color: var(--text-secondary); }
.meter-legenda strong { color: var(--text-primary); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ tabelle */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em; padding: 0 10px 8px;
  border-bottom: 1px solid var(--grid); white-space: nowrap; vertical-align: bottom;
}
td { padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-sunken); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.etf { font-weight: 550; line-height: 1.3; display: flex; align-items: center; gap: 9px; min-width: 200px; }
.etf .key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.etf small { display: block; font-weight: 400; color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
tfoot td { padding: 10px; border-top: 2px solid var(--baseline); font-weight: 650; }
.dimmed { color: var(--muted); }

/* ------------------------------------------------------------------- chip */
.chip {
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-sunken);
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.chip.in_linea .dot { background: var(--good); }
.chip.sottopeso .dot { background: var(--under); }
.chip.sovrappeso .dot { background: var(--over); }
.chip.vuoto .dot { background: var(--muted); }
.chip.in_linea { color: var(--success-text); }
.chip.sottopeso { color: var(--under); }
.chip.sovrappeso { color: var(--over); }
.chip.vuoto { color: var(--muted); }

/* ------------------------------------------------------------------- form */
form.grid, .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; align-items: end; }
label { display: block; font-size: 12px; font-weight: 550; color: var(--text-secondary); margin-bottom: 5px; }
label .hint { display: block; font-weight: 400; color: var(--muted); font-size: 11.5px; margin-top: 2px; }
input, select, textarea {
  width: 100%; font: inherit; font-size: 13.5px; padding: 8px 10px;
  border: 1px solid var(--baseline); border-radius: var(--radius-s);
  background: var(--surface); color: var(--text-primary);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
input[type="number"] { font-variant-numeric: tabular-nums; }
input.cell { padding: 5px 8px; font-size: 13px; width: 110px; text-align: right; }
input.cell.largo { width: 130px; }
input.cell.stretto { width: 84px; }
input.cell:invalid, input.errore { border-color: var(--critical); outline-color: var(--critical); }
input[type="range"] { padding: 0; accent-color: var(--accent); min-width: 110px; }
.campo-peso { display: flex; align-items: center; gap: 10px; justify-content: flex-end; }
.campo-peso input[type="range"] { width: 140px; }

button {
  font: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer;
  padding: 9px 16px; border-radius: var(--radius-s);
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
}
button:hover { filter: brightness(1.07); }
button:focus-visible { outline: 2px solid var(--text-primary); outline-offset: 2px; }
button.ghost { background: var(--surface); color: var(--text-primary); border-color: var(--baseline); }
button.mini { padding: 5px 11px; font-size: 12.5px; }
button.danger { background: none; border-color: transparent; color: var(--muted); padding: 4px 8px; font-size: 16px; line-height: 1; }
button.danger:hover { color: var(--critical); background: var(--surface-sunken); filter: none; }
button:disabled { opacity: 0.5; cursor: not-allowed; filter: none; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 16px; }
.actions .spinta { margin-left: auto; }

/* ------------------------------------------------------------------ varie */
.empty { padding: 34px 10px; text-align: center; color: var(--muted); }
.note {
  font-size: 12.5px; color: var(--text-secondary); background: var(--surface-sunken);
  border-left: 3px solid var(--baseline); border-radius: 0 var(--radius-s) var(--radius-s) 0;
  padding: 11px 14px; margin: 16px 0 0;
}
.note strong { color: var(--text-primary); }
.note.avviso { border-left-color: var(--warning); }
a { color: var(--accent); }
.saved { font-size: 12.5px; color: var(--success-text); font-weight: 550; }
.errore-testo { font-size: 12.5px; color: var(--critical); font-weight: 550; }
.modifiche { font-size: 12.5px; color: var(--text-secondary); font-weight: 550; }
details.dati { margin-top: 12px; }
details.dati summary { cursor: pointer; font-size: 12.5px; color: var(--text-secondary); font-weight: 550; }
details.dati[open] summary { margin-bottom: 10px; }

.gruppo-acquisti { border: 1px solid var(--border); border-radius: var(--radius-s); margin-bottom: 10px; }
.gruppo-acquisti > summary {
  cursor: pointer; list-style: none; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 18px;
  padding: 10px 14px; font-size: 13px;
}
.gruppo-acquisti > summary::-webkit-details-marker { display: none; }
.gruppo-acquisti > summary::before { content: "▸"; color: var(--muted); font-size: 11px; transition: transform 0.15s; }
.gruppo-acquisti[open] > summary::before { transform: rotate(90deg); }
.gruppo-acquisti > summary .data { font-weight: 650; min-width: 92px; }
.gruppo-acquisti > summary .meta { color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.gruppo-acquisti > summary .spinta { margin-left: auto; }
.gruppo-acquisti .table-wrap { padding: 0 8px 8px; }

#toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(160%);
  background: var(--text-primary); color: var(--page); padding: 11px 20px;
  border-radius: var(--radius-s); font-size: 13.5px; font-weight: 550;
  transition: transform 0.22s ease, visibility 0.22s; z-index: 70; max-width: 90vw; box-shadow: var(--shadow);
  visibility: hidden;
}
#toast.show { transform: translateX(-50%) translateY(0); visibility: visible; }
#toast.error { background: var(--critical); color: #fff; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  color: var(--text-primary); padding: 22px 24px; max-width: 560px; width: calc(100vw - 32px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.28);
}
dialog::backdrop { background: rgba(11, 11, 11, 0.55); }
dialog h2 { margin: 0 0 4px; font-size: 15px; font-weight: 650; }
dialog .sub { margin: 0 0 18px; font-size: 12.5px; color: var(--muted); }

/* ------------------------------------------------------------------ accesso */
.accesso { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.accesso-card { width: 100%; max-width: 400px; padding: 26px 26px 20px; }
.accesso-card .brand { margin-bottom: 22px; }
.accesso-card form, .accesso-card #passo-telegram { display: grid; gap: 14px; }
.accesso-card h2 { margin: 0; font-size: 15px; font-weight: 650; }
.accesso-card .sub { margin: -8px 0 0; font-size: 12.5px; color: var(--muted); }
.accesso-card button[type="submit"], #tg-verifica { width: 100%; }
.accesso-card .actions button { flex: 1 1 auto; }
.accesso-card #messaggio { margin: 14px 0 0; min-height: 1.2em; }
.passi { margin: 0; padding-left: 20px; font-size: 13px; color: var(--text-secondary); display: grid; gap: 6px; }
.passi a { font-weight: 650; }
input.pin { font-size: 24px; letter-spacing: 0.35em; text-align: center; font-variant-numeric: tabular-nums; padding: 10px; }
code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 0.95em; background: var(--surface-sunken); padding: 1px 5px; border-radius: 4px; }

/* utente e banner */
.utente { display: flex; align-items: center; gap: 10px; font-size: 12.5px; color: var(--text-secondary); padding-left: 14px; border-left: 1px solid var(--border); }
.utente strong { color: var(--text-primary); font-weight: 600; }
.banner { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--warning); border-radius: var(--radius-s); padding: 11px 14px; margin: 14px 0 0; font-size: 13px; color: var(--text-secondary); display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.banner strong { color: var(--text-primary); }
.fasi { font-size: 12.5px; color: var(--text-secondary); margin: 12px 0 0; }
.fasi strong { color: var(--text-primary); }

@media (max-width: 720px) {
  .utente { border-left: 0; padding-left: 0; }
  .wrap { padding-inline: 16px; }
  .stato-quotazioni { margin-left: 0; width: 100%; }
  .hero { grid-template-columns: 1fr 1fr; }
  .hero .principale { grid-column: 1 / -1; }
  .hero .principale dd { font-size: 32px; }
  .barre { grid-template-columns: minmax(90px, 120px) 1fr; }
  .intervallo input { width: 128px; }
}
