:root {
  --bg: #f6f7fb; --surface: #ffffff; --surface-2: #f0f2f8; --text: #172033; --muted: #66708a;
  --border: #e3e7f1; --primary: #1b57b8; --primary-ink: #ffffff; --primary-soft: #e6eefb;
  --ok: #16a34a; --ok-soft: #e7f7ee; --warn: #d97706; --warn-soft: #fff4e0; --bad: #dc2626; --bad-soft: #fdeaea;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 6px 20px rgba(20, 30, 60, .06);
  --radius: 16px; --nav-h: 4rem; --side-w: 14.5rem;
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; font-size: 100%; }
/* La dimensione dei caratteri scelta dall'utente vale SOLO su schermi larghi (versione web): sul telefono il layout resta quello standard. */
@media (min-width: 821px) {
  html[data-font="large"] { font-size: 118%; }
  html[data-font="xlarge"] { font-size: 138%; }
}
@media (max-width: 820px) { .fontrow { display: none; } }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
a { color: var(--primary); }
.muted { color: var(--muted); } .small { font-size: .85rem; } .right { text-align: right; }
.boot { padding: 2rem; text-align: center; color: var(--muted); }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { min-height: 100dvh; display: grid; grid-template-columns: var(--side-w) 1fr; }
.side {
  position: sticky; top: 0; height: 100dvh; padding: 1.25rem 1rem; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: .25rem;
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.1rem; margin: 0 .5rem 1rem; }
.brand img { width: 32px; height: 32px; border-radius: 9px; }
.nav-item {
  display: flex; align-items: center; gap: .7rem; padding: .65rem .8rem; border-radius: 12px; color: var(--muted);
  text-decoration: none; font-weight: 600; position: relative;
}
.nav-item .ico { font-size: 1.15rem; width: 1.5rem; text-align: center; }
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); }
.badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: .7rem; padding: 0 .4rem; min-width: 1.2rem; text-align: center; margin-left: auto; }
.side-foot { margin-top: auto; display: grid; gap: .5rem; }
.main { min-width: 0; padding: 1.5rem clamp(1rem, 3vw, 2.5rem) 3rem; max-width: 1100px; width: 100%; margin: 0 auto; }
.topbar { display: flex; align-items: center; gap: .75rem; margin-bottom: 1.25rem; }
.topbar h1 { margin: 0; flex: 1; }
.savestate { font-size: .8rem; color: var(--muted); }
.savestate.error, .savestate.conflict { color: var(--bad); font-weight: 600; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side {
    position: fixed; z-index: 20; inset: auto 0 0 0; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    flex-direction: row; padding: 0 .25rem env(safe-area-inset-bottom); border-right: 0; border-top: 1px solid var(--border);
    justify-content: space-around; gap: 0;
  }
  .brand, .side-foot { display: none; }
  .nav-item { flex-direction: column; gap: .1rem; padding: .45rem .3rem; font-size: .7rem; flex: 1; justify-content: center; border-radius: 10px; }
  .nav-item .badge { position: absolute; top: 2px; right: 22%; margin: 0; }
  .main { padding: 1rem 1rem calc(var(--nav-h) + 5rem + env(safe-area-inset-bottom)); }
}

/* ---------- componenti ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); }
.card + .card, .stack > * + * { margin-top: 1rem; }
.grid { display: grid; gap: 1rem; }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.kpi .label { color: var(--muted); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 1.6rem; font-weight: 750; margin-top: .2rem; }
.kpi.bad .value { color: var(--bad); } .kpi.ok .value { color: var(--ok); } .kpi.warn .value { color: var(--warn); }
.row { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.row.between { justify-content: space-between; } .grow { flex: 1; min-width: 0; }

button, .btn {
  font: inherit; font-weight: 650; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 12px; padding: .6rem 1rem; cursor: pointer; min-height: 44px; display: inline-flex; align-items: center; gap: .4rem;
  text-decoration: none;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--bad); border-color: var(--bad-soft); background: var(--bad-soft); }
button.ghost { background: transparent; border-color: transparent; }
button.small { min-height: 34px; padding: .3rem .7rem; font-size: .85rem; }
button:disabled { opacity: .55; cursor: not-allowed; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

label { display: grid; gap: .3rem; font-size: .85rem; font-weight: 650; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: .6rem .75rem; min-height: 44px; width: 100%;
}
input[type="checkbox"] { width: 1.2rem; height: 1.2rem; min-height: 0; accent-color: var(--primary); }
label.check { display: flex; align-items: center; gap: .6rem; color: var(--text); font-weight: 500; font-size: .95rem; }
.form { display: grid; gap: .85rem; }
.form .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .85rem; }

.chip { display: inline-block; padding: .1rem .6rem; border-radius: 999px; font-size: .75rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.chip.paid, .chip.great, .chip.ok-l { background: var(--ok-soft); color: var(--ok); }
.chip.overdue, .chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.today, .chip.soon, .chip.watch { background: var(--warn-soft); color: var(--warn); }

.list { display: grid; gap: .6rem; }
.item {
  display: flex; align-items: center; gap: .8rem; padding: .8rem; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); cursor: pointer; text-align: left; width: 100%; min-height: 56px;
}
.item:hover { background: var(--surface-2); }
.item .ico { width: 2.4rem; height: 2.4rem; border-radius: 12px; display: grid; place-items: center; font-size: 1.2rem; flex: none; }
.item .t { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amount { font-weight: 750; white-space: nowrap; }
.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }
.empty .big { font-size: 2.2rem; }

.notice { padding: .8rem 1rem; border-radius: 14px; background: var(--primary-soft); border: 1px solid var(--border); }
.notice.warn { background: var(--warn-soft); } .notice.bad { background: var(--bad-soft); } .notice.ok { background: var(--ok-soft); }

.fab {
  position: fixed; right: 1.25rem; bottom: 1.5rem; z-index: 15; width: 60px; height: 60px; border-radius: 50%; padding: 0;
  justify-content: center; font-size: 1.8rem; box-shadow: 0 8px 24px rgba(27, 87, 184, .45);
}
@media (max-width: 820px) { .fab { bottom: calc(var(--nav-h) + 1rem + env(safe-area-inset-bottom)); } }

/* ---------- modale ---------- */
dialog { border: 0; border-radius: 20px; padding: 0; width: min(640px, calc(100vw - 1.5rem)); max-height: 92dvh; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog::backdrop { background: rgba(10, 14, 30, .55); backdrop-filter: blur(2px); }
.dlg { padding: 1.25rem; display: grid; gap: 1rem; max-height: 92dvh; overflow: auto; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.dlg-foot { display: flex; gap: .6rem; justify-content: flex-end; flex-wrap: wrap; position: sticky; bottom: -1.25rem; background: var(--surface); padding: .75rem 0 0; }

/* ---------- calendario ---------- */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .75rem; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal .dow { text-align: center; font-size: .75rem; font-weight: 700; color: var(--muted); padding: .25rem 0; }
.cal button.day {
  min-height: 58px; padding: .3rem; border-radius: 12px; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 3px;
  background: var(--surface); font-weight: 600;
}
.cal button.day.out { opacity: .35; }
.cal button.day.today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cal button.day.sel { background: var(--primary-soft); }
.dots { display: flex; gap: 3px; flex-wrap: wrap; justify-content: center; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.overdue { background: var(--bad); } .dot.paid { background: var(--ok); } .dot.soon, .dot.today { background: var(--warn); } .dot.ok { background: var(--primary); }

/* ---------- grafico ---------- */
.chart { width: 100%; height: 190px; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .bar { fill: var(--primary); }
.chart .bar:hover { opacity: .8; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }

/* ---------- auth ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
.auth .card { width: min(440px, 100%); }
.auth .brand { justify-content: center; margin: 0 0 1rem; font-size: 1.3rem; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: .25rem; background: var(--surface-2); padding: .25rem; border-radius: 14px; margin-bottom: 1rem; }
.tabs button { border: 0; background: transparent; }
.tabs button.on { background: var(--surface); box-shadow: var(--shadow); }
.code-box { font: 700 1.15rem/1.6 ui-monospace, Consolas, monospace; letter-spacing: .08em; text-align: center; padding: 1rem; background: var(--surface-2); border-radius: 14px; word-break: break-all; user-select: all; }

#toasts { position: fixed; z-index: 100; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 1rem); display: grid; gap: .5rem; width: min(440px, calc(100vw - 2rem)); pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: .75rem 1rem; border-radius: 14px; box-shadow: var(--shadow); pointer-events: auto; animation: pop .2s ease-out; }
.toast.error { background: var(--bad); color: #fff; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); transition: width .2s; }
.spinner { width: 18px; height: 18px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

.grid > *, .stack > *, .list > * { min-width: 0; }
.row select, .row input:not([type=checkbox]) { width: auto; max-width: 100%; }
.row input.grow { width: 100%; }
.item .grow { overflow: hidden; }
.grid > .card, .grid > * { margin-top: 0 !important; }
.dot { display: inline-block; }

.marks { display: flex; flex-wrap: wrap; gap: 2px; justify-content: center; align-items: center; line-height: 1; }
.mk { font-size: .85rem; padding-bottom: 2px; border-bottom: 3px solid var(--muted); border-radius: 2px; }
.mk.overdue { border-color: var(--bad); } .mk.soon, .mk.today { border-color: var(--warn); }
.mk.ok { border-color: var(--primary); } .mk.paid { border-color: var(--ok); opacity: .75; }
.more { font-size: .65rem; color: var(--muted); font-weight: 700; }
.cal button.day { min-height: 64px; }
.fab { width: auto; height: 56px; border-radius: 28px; padding: 0 1.25rem; font-size: 1rem; gap: .5rem; }
.fab .plus { font-size: 1.6rem; line-height: 1; }
.choice { display: grid; gap: .75rem; }
.choice button { justify-content: flex-start; padding: 1rem; min-height: 64px; width: 100%; text-align: left; gap: .9rem; }
.choice button .ico { font-size: 1.6rem; }
.choice button small { display: block; color: var(--muted); font-weight: 500; }
.choice button.primary small { color: inherit; opacity: .85; }
.addbar { display: flex; gap: .6rem; flex-wrap: wrap; margin-bottom: 1rem; }
.addbar button { flex: 1 1 160px; justify-content: center; }

.mk.exp { opacity: .4; border-bottom: 2px dashed var(--muted); }
.mk.miss { border-bottom: 2px dashed var(--bad); opacity: .9; }
@media (max-width: 820px) { .nav-item.desk-only { display: none; } }

.auth-logo { text-align: center; margin: -.25rem 0 .5rem; }
.auth-logo img { width: min(220px, 62%); height: auto; }
.brand img { border-radius: 10px; }
.auth-logo img { background: #fbfaf6; border-radius: 18px; padding: .25rem; }

.fontsize { display: inline-flex; gap: .4rem; flex-wrap: wrap; }
.fontsize button { min-width: 3.4rem; justify-content: center; padding: .4rem .8rem; font-weight: 700; }
.fontsize button[aria-checked="true"] { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.fontsize .a1 { font-size: .9rem; } .fontsize .a2 { font-size: 1.1rem; } .fontsize .a3 { font-size: 1.35rem; }

/* ---------- popup in sovraimpressione ---------- */
.tip { position: fixed; z-index: 300; max-width: min(21rem, calc(100vw - 1rem)); padding: .7rem .85rem; border-radius: 14px; background: var(--text); color: var(--bg);
  box-shadow: 0 10px 30px rgba(0,0,0,.3); font-size: .85rem; line-height: 1.35; pointer-events: none; opacity: 0; transform: translateY(4px); transition: opacity .12s, transform .12s; }
.tip.on { opacity: 1; transform: none; }
.tip-title { font-weight: 750; margin-bottom: .2rem; }
.tip-note { opacity: .8; font-size: .8rem; margin: .1rem 0 .35rem; }
.tip-line { display: flex; justify-content: space-between; gap: 1rem; padding: .08rem 0; }
.kpi { cursor: help; transition: transform .15s, box-shadow .15s; }
.kpi:hover, .kpi:focus-visible { transform: translateY(-2px); }
.chart .hit { fill: transparent; cursor: help; }
.chart .hit:hover, .chart .hit:focus-visible { fill: var(--primary); fill-opacity: .08; outline: none; }
.chart .seg { transition: opacity .15s; }
.legend { display: flex; flex-wrap: wrap; gap: .4rem .9rem; font-size: .85rem; }
.legend .lg { display: inline-flex; align-items: center; gap: .35rem; }
.legend i { width: .7rem; height: .7rem; border-radius: 3px; display: inline-block; }

/* ---------- piccole animazioni (rispettano "riduci animazioni") ---------- */
@keyframes bob { 0%,100% { transform: translateY(0); } 40% { transform: translateY(-4px) rotate(-6deg); } 70% { transform: translateY(0) rotate(4deg); } }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes ring { 0%,100% { transform: rotate(0); } 10%,30%,50% { transform: rotate(-14deg); } 20%,40% { transform: rotate(14deg); } 60% { transform: rotate(0); } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.18); } }
@keyframes spin-pop { to { transform: rotate(180deg) scale(1.15); } }
.kico { display: inline-block; font-size: 1.05rem; transform-origin: 50% 20%; }
.kico.ring { animation: ring 2.4s ease-in-out infinite; }
.kico.pulse { animation: pulse 2.2s ease-in-out infinite; }
.nav-item:hover .ico, .nav-item.active .ico { animation: bob .7s ease-out 1; }
.item:hover .ico { animation: bob .6s ease-out 1; }
.fab:hover .plus { animation: spin-pop .25s ease-out forwards; }
.empty .big { display: inline-block; animation: float 3.2s ease-in-out infinite; }
.mascot { display: block; margin: 0 auto .5rem; width: min(220px, 70%); height: auto; animation: float 3.6s ease-in-out infinite; }
.brand img { transition: transform .3s; } .brand:hover img { transform: rotate(-8deg) scale(1.08); }

/* ---------- guida ---------- */
.guide { display: grid; gap: .75rem; }
.guide-sec { padding: 0; }
.guide-sec > summary { cursor: pointer; padding: 1rem 1.1rem; font-weight: 700; font-size: 1.05rem; list-style: none; display: flex; align-items: center; gap: .5rem; }
.guide-sec > summary::-webkit-details-marker { display: none; }
.guide-sec > summary::after { content: '▾'; margin-left: auto; color: var(--muted); transition: transform .2s; }
.guide-sec[open] > summary::after { transform: rotate(180deg); }
.guide-sec .gi { font-size: 1.3rem; }
.guide-body { padding: 0 1.1rem 1.1rem; }
.guide-body ul, .guide-body ol { margin: 0; padding-left: 1.3rem; display: grid; gap: .35rem; }
.guide-body p { margin: 0; }
.checklist ul { list-style: none; padding: 0; margin: .6rem 0 0; display: grid; gap: .25rem; }
.checklist li.done { color: var(--muted); text-decoration: line-through; }
details.faq { border-top: 1px solid var(--border); padding: .6rem 0; }
details.faq summary { cursor: pointer; font-weight: 650; }
details.faq p { margin: .4rem 0 0; color: var(--muted); }
.addbar button { flex: 1 1 10rem; min-height: 48px; justify-content: center; }

.checks { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.checks li { padding: .6rem .8rem; border-radius: 12px; background: var(--surface-2); }
.checks li.ko { background: var(--bad-soft); }

/* mascotte */
.mascot-img { display: block; height: auto; max-width: 100%; user-select: none; }
.guide-hero { display: flex; align-items: center; gap: 1rem; }
.guide-hero .hero-img { width: clamp(84px, 22%, 150px); flex: none; }
.guide-mascot { display: flex; align-items: center; gap: .75rem; padding: .5rem .75rem; border-radius: 14px; background: var(--surface-2); }
.guide-mascot .mascot-img { width: 72px; flex: none; }
.bubble { position: relative; font-weight: 600; font-size: .95rem; }
.title-img { width: 40px; }
@media (max-width: 480px) { .guide-hero { flex-direction: column; text-align: center; } .guide-hero .hero-img { width: 110px; } }
@media (prefers-reduced-motion: reduce) { .mascot { animation: none; } }

.avg-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); gap: .5rem; }
.avg-tile { padding: .45rem .65rem; border-radius: 10px; background: var(--surface-2); border-top: 3px solid var(--border); min-width: 0; }
.avg-label { font-size: .72rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avg-val { font-size: 1.05rem; font-weight: 750; line-height: 1.3; }
.avg-sub { font-size: .7rem; color: var(--muted); }

.sender-group { display: grid; gap: .5rem; border: 1px solid var(--border); border-radius: 12px; padding: .15rem .75rem .6rem; }
.sender-group > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: .5rem; padding: .5rem 0; font-weight: 700; }
.sender-group > summary::-webkit-details-marker { display: none; }
.sender-group > summary::after { content: '▾'; margin-left: auto; color: var(--muted); transition: transform .2s; }
.sender-group[open] > summary::after { transform: rotate(180deg); }
.sender-group p { margin: 0; }
.sender-row { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem .75rem; padding: .6rem .8rem; border-radius: 12px; background: var(--surface-2); border-left: 4px solid var(--border); }
.sender-group.validated .sender-row { border-left-color: var(--ok); }
.sender-group.orphaned .sender-row { border-left-color: var(--warn); }
.sender-group.ignored .sender-row { opacity: .9; }
.sender-addr { font-weight: 650; overflow-wrap: anywhere; }
/* L'indirizzo ha sempre una larghezza minima: i comandi (menu, pulsanti) vanno a capo sotto invece di schiacciarlo lettera per lettera. */
.sender-row > .grow { flex: 1 1 14rem; min-width: min(14rem, 100%); }
.sender-row > .row { flex: 1 1 auto; }
.sender-row select { max-width: 100%; min-width: 0; flex: 1 1 11rem; }

.admin-tabs { grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr)); }
.item .row { flex-wrap: wrap; }

/* Pagina Sostieni: classifica e costi */
.sub-tabs { grid-template-columns: repeat(3, 1fr); }
.rank-list { display: grid; gap: .5rem; }
.rank-row { display: flex; align-items: center; gap: .75rem; padding: .7rem .9rem; border-radius: 12px; background: var(--surface-2); }
.rank-row .pos { width: 2rem; text-align: center; font-weight: 800; font-size: 1.1rem; }
.rank-row .who { flex: 1; font-weight: 650; letter-spacing: .02em; }
.rank-row .amt { font-weight: 750; white-space: nowrap; }
.cost-table { width: 100%; border-collapse: collapse; }
.cost-table th, .cost-table td { text-align: left; padding: .55rem .4rem; border-bottom: 1px solid var(--border); }
.cost-table td.num, .cost-table th.num { text-align: right; white-space: nowrap; }
.cost-table tfoot td { font-weight: 750; border-bottom: 0; }
.author-card { display: grid; gap: .6rem; }
