:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --surface: #ffffff;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6b6a66;
  --line: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #1c5cab;
  --on-accent: #ffffff;
  /* Entrate verdi, uscite rosse: riempimenti (barre, pulsanti) e testo hanno toni separati per il contrasto. */
  --inc: #008300;
  --inc-text: #006300;
  --inc-tint: #e4f3e4;
  --exp: #e34948;
  --exp-strong: #d03b3b;
  --exp-text: #c22f2f;
  --exp-tint: #fde8e8;
  --warn: #fab219;
  --shadow: 0 1px 2px rgba(11, 11, 11, .06), 0 4px 16px rgba(11, 11, 11, .04);
  /* Serie per categoria (tavolozza categorica validata per daltonismo, ordine fisso) + grigio per «Altre». */
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s4: #eda100;
  --s5: #e87ba4;
  --s-other: #a9a8a0;
  --tab-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
/* Tema scuro: data-theme è impostato da index.html e js/theme.js (Automatico segue il dispositivo). */
:root[data-theme=dark] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #a09f98;
  --line: #2c2c2a;
  --axis: #383835;
  --accent: #6da7ec;
  --on-accent: #0b0b0b;
  --inc: #0ca30c;
  --inc-text: #3ecf5a;
  --inc-tint: #12301a;
  --exp: #e66767;
  --exp-strong: #d03b3b;
  --exp-text: #f08080;
  --exp-tint: #3a1b1b;
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --s4: #c98500;
  --s5: #d55181;
  --s-other: #5d5c57;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
h1, h2, h3, p, ul { margin: 0; padding: 0; }
ul { list-style: none; }
a { color: var(--accent); text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#app { max-width: 560px; margin: 0 auto; min-height: 100dvh; }

#topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
#topbar h1 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
#topbar .icon-btn { margin-left: -10px; }

#main { padding: 4px 16px calc(var(--tab-h) + var(--safe-b) + 24px); }
.view { display: grid; gap: 16px; }
.view > section { display: grid; gap: 8px; min-width: 0; }

#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: center;
  padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
}
#tabbar a {
  flex: 1; max-width: 112px; height: var(--tab-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11px; font-weight: 500; min-width: 0;
}
#tabbar a span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
#tabbar a[aria-current] { color: var(--accent); font-weight: 700; }

/* ---------- Elementi comuni ---------- */

.card { background: var(--surface); border-radius: 16px; box-shadow: var(--shadow); min-width: 0; }
.card.pad, .card.empty, section.card { padding: 16px; }
.card.empty { display: grid; gap: 12px; justify-items: center; text-align: center; padding: 28px 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.num, .amt { font-variant-numeric: tabular-nums; white-space: nowrap; }
.amt { font-weight: 600; }
.amt.income { color: var(--inc-text); }
.amt.expense { color: var(--exp-text); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.dot.income { background: var(--inc); }
.dot.expense { background: var(--exp); }

.sec-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 0 4px; }
.sec-head h2, h2.group { font-size: 15px; font-weight: 700; }
.sec-head a { font-size: 14px; font-weight: 600; }
h2.group { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; padding: 4px 4px 0; margin-bottom: -8px; }

.icon-btn {
  width: 44px; height: 44px; display: inline-grid; place-items: center; flex: none;
  background: none; border: 0; border-radius: 50%; font-size: 26px; line-height: 1; color: var(--text);
}
.icon-btn:active { background: var(--line); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); font-weight: 600;
}
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--on-accent); }
.btn.danger { background: var(--exp-strong); border-color: transparent; color: #fff; }
.btn.danger.ghost { background: none; color: var(--exp-text); border-color: var(--line); }
.btn:active { opacity: .8; }

.list > * + * { border-top: 1px solid var(--line); }
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; min-height: 54px; padding: 8px 16px;
  background: none; border: 0; text-align: left; color: var(--text);
}
.row small { display: block; color: var(--muted); font-size: 13px; font-weight: 400; margin-top: 2px; }
.row select { border: 0; background: none; color: var(--accent); font-weight: 600; text-align: right; max-width: 55%; padding: 8px 0; }
.chev { color: var(--muted); font-size: 22px; }
input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }

.meter { display: block; height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; }
.meter.thin { height: 6px; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.meter > .warn { background: var(--warn); }
.meter > .over, .meter > .expense { background: var(--exp); }
.meter > .income { background: var(--inc); }

.seg { display: flex; padding: 3px; gap: 3px; border-radius: 12px; background: var(--line); }
.seg button { flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: none; font-weight: 600; color: var(--text-2); }
.seg button[aria-pressed=true] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg button.income[aria-pressed=true] { color: var(--inc-text); }
.seg button.expense[aria-pressed=true] { color: var(--exp-text); }

.pnav { display: flex; align-items: center; justify-content: space-between; }
.pnav-label { font-weight: 700; font-size: 17px; text-align: center; }

/* ---------- Home ---------- */

.summary { padding: 18px 16px 16px; display: grid; gap: 4px; }
.hero { font-size: 38px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.hero.neg { color: var(--exp-text); }
.totals { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 10px; }
.totals > div, .stats > div { display: grid; gap: 2px; min-width: 0; }
.lbl { color: var(--muted); font-size: 13px; }
.totals b { font-size: 17px; }

.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.big {
  display: grid; justify-items: center; gap: 2px; min-height: 112px; padding: 14px 8px;
  border: 0; border-radius: 20px; color: #fff; font-size: 18px; font-weight: 700;
  box-shadow: var(--shadow);
}
.big .sign { font-size: 44px; line-height: 1; font-weight: 400; }
.big.income { background: #008300; }
.big.expense { background: #d03b3b; }
.big:active { transform: scale(.97); }

.budget { display: grid; gap: 6px; width: 100%; padding: 12px 16px; background: none; border: 0; text-align: left; }
.budget-head { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-weight: 600; }
.budget-head .num { font-weight: 400; color: var(--text-2); font-size: 14px; }
.budget-foot { font-size: 13px; color: var(--muted); }
.budget-foot.over { color: var(--exp-text); font-weight: 600; }

/* ---------- Movimenti ---------- */

.ci {
  width: 40px; height: 40px; flex: none; display: inline-grid; place-items: center;
  border-radius: 12px; font-size: 20px; line-height: 1;
}
.ci.income { background: var(--inc-tint); }
.ci.expense { background: var(--exp-tint); }

.tx-row { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 60px; padding: 10px 16px; background: none; border: 0; text-align: left; }
.tx-row:active, .row:active, .budget:active { background: color-mix(in srgb, var(--line) 50%, transparent); }
.tx-main { flex: 1; min-width: 0; display: grid; }
.tx-cat { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-note { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.month { display: grid; gap: 8px; }
.month + .month { margin-top: 12px; }
.month-head {
  display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 2px 12px;
  padding: 14px 16px; border-radius: 16px; background: var(--surface); box-shadow: var(--shadow);
}
.month-head h2 { font-size: 19px; }
.month-bal { font-weight: 700; font-size: 17px; }
.month-head .totals { grid-column: 1 / -1; margin-top: 6px; }
.month-head .totals b { font-size: 15px; }
.day-head { display: flex; justify-content: space-between; gap: 12px; padding: 6px 4px 0; font-size: 13px; font-weight: 600; color: var(--muted); }

/* ---------- Categorie ---------- */

.cat-row { display: flex; align-items: center; gap: 12px; padding: 10px 16px; }
.cat-body { flex: 1; min-width: 0; display: grid; gap: 6px; }
.cat-line { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
.cat-line > :first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-row.empty .cat-line { font-weight: 400; color: var(--text-2); }
.pct { width: 38px; text-align: right; color: var(--muted); font-size: 13px; }
section.card > .list { margin: 0 -16px; }

.sortable li { display: flex; align-items: center; background: var(--surface); }
.sortable li:first-child { border-radius: 16px 16px 0 0; }
.sortable li:last-child { border-radius: 0 0 16px 16px; }
.sortable .row { justify-content: flex-start; flex: 1; min-width: 0; font-weight: 600; }
.drag {
  width: 56px; align-self: stretch; display: grid; place-items: center; flex: none;
  font-size: 24px; color: var(--muted); cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none;
}
.sortable li.dragging { position: relative; z-index: 2; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); border-radius: 12px; transform: scale(1.02); }
.rule { justify-content: flex-start; }
.rule .tx-cat, .rule .tx-note { white-space: normal; }

/* ---------- Grafici ---------- */

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stats b { font-size: 15px; }
.chart-title { font-size: 15px; font-weight: 700; }
.legend { display: flex; gap: 16px; margin: 6px 0 4px; font-size: 13px; color: var(--text-2); }
.chart { display: block; width: 100%; height: auto; margin-top: 4px; touch-action: manipulation; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.chart .tick.on { fill: var(--text); font-weight: 700; }
.chart .bar.income { fill: var(--inc); }
.chart .bar.expense { fill: var(--exp); }
.chart .sel { fill: var(--line); opacity: .7; }
.chart .stk.income { fill: var(--inc); }
.chart .stk.expense { fill: var(--exp); }
.s1 { --c: var(--s1); } .s2 { --c: var(--s2); } .s3 { --c: var(--s3); } .s4 { --c: var(--s4); } .s5 { --c: var(--s5); } .s-other { --c: var(--s-other); }
.chart .stk[class*=" s"] { fill: var(--c); }
.dot[class*=" s"] { background: var(--c); }
.legend.wrap { flex-wrap: wrap; gap: 6px 14px; }
.detail-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 14px; }
.detail-list li { display: flex; justify-content: space-between; gap: 12px; }
.table-scroll { overflow-x: auto; }
.table-scroll table { min-width: max-content; }
.rank-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.rank { list-style: none; margin: 0 -8px; padding: 0; }
.rank button { display: grid; gap: 6px; width: 100%; padding: 8px; border: 0; border-radius: 10px; background: none; text-align: left; color: var(--text); }
.rank button:active { background: var(--bg); }
.rank-line { display: flex; justify-content: space-between; gap: 12px; font-size: 15px; }
.rank-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.rank-line small { color: var(--muted); font-size: 12px; margin-left: 4px; }
.rank-track { display: block; height: 10px; }
.rank-bar { display: block; height: 100%; min-width: 2px; border-radius: 0 4px 4px 0; }
.rank-bar.expense { background: var(--exp); }
.rank-bar.income { background: var(--inc); }
.rank.has-sel button[aria-pressed=false] .rank-bar { background: var(--s-other); }
.rank.has-sel button[aria-pressed=false] { color: var(--text-2); }
.rank button[aria-pressed=true] { background: var(--bg); }
.chart .hit { fill: transparent; cursor: pointer; }
.detail { display: grid; gap: 10px; margin-top: 8px; padding: 12px; border-radius: 12px; background: var(--bg); }
.detail-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
h3.sub { font-size: 13px; color: var(--text-2); margin: 12px 0 0; }
.table-view { margin-top: 12px; font-size: 14px; }
.table-view summary { color: var(--accent); font-weight: 600; padding: 8px 0; cursor: pointer; }
.table-view table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table-view th, .table-view td { padding: 6px 4px; border-top: 1px solid var(--line); text-align: right; font-weight: 400; }
.table-view th:first-child { text-align: left; }
.table-view thead th { color: var(--muted); font-size: 12px; border-top: 0; }

/* ---------- Pannelli e moduli ---------- */

.sheet-wrap { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); animation: fade .15s; }
.sheet {
  position: relative; width: 100%; max-width: 560px; max-height: 94dvh; overflow-y: auto; overscroll-behavior: contain;
  padding: 8px 16px calc(16px + var(--safe-b));
  background: var(--surface); border-radius: 20px 20px 0 0; animation: rise .2s ease-out;
}
.sheet.confirm { padding-top: 24px; display: grid; gap: 12px; }
.confirm-msg { font-size: 17px; font-weight: 600; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet, .sheet-backdrop { animation: none; } }

.sheet form { display: grid; gap: 14px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: -8px; z-index: 1; background: var(--surface); margin: 0 -4px; padding: 4px; }
.sheet-head h2 { font-size: 19px; }
form[data-type=income] { --type: var(--inc-text); --type-fill: #008300; }
form[data-type=expense] { --type: var(--exp-text); --type-fill: #d03b3b; }
form[data-type] .sheet-head h2 { color: var(--type); }
.btn.typed { background: var(--type-fill, var(--accent)); color: #fff; }

.field { display: grid; gap: 6px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 600; color: var(--text-2); }
.field small { font-weight: 400; color: var(--muted); }
.field input, .field select {
  width: 100%; min-height: 48px; padding: 0 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); font-size: 16px;
  -webkit-appearance: none; appearance: none;
}
.field.amount input { min-height: 64px; font-size: 32px; font-weight: 700; text-align: right; color: var(--type, var(--text)); font-variant-numeric: tabular-nums; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; font-weight: 600; }
.recurring-opts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.cat-pick {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--text); text-align: left;
}
.cat-pick-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-pick-change { color: var(--accent); font-size: 14px; font-weight: 600; flex: none; }
.picker[data-type=income] { --type: var(--inc-text); }
.picker[data-type=expense] { --type: var(--exp-text); }
.picker .chips { margin-top: 8px; }
.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.chip {
  display: grid; justify-items: center; gap: 4px; padding: 8px 4px; min-width: 0;
  border: 2px solid transparent; border-radius: 14px; background: var(--bg); font-size: 12px; font-weight: 500;
}
.chip span:last-child { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip[aria-pressed=true] { border-color: var(--type, var(--accent)); background: var(--surface); font-weight: 700; }

.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; max-height: 196px; overflow-y: auto; padding: 2px; }
.icon-grid button { height: 44px; border: 2px solid transparent; border-radius: 12px; background: var(--bg); font-size: 22px; padding: 0; }
.icon-grid button[aria-pressed=true] { border-color: var(--type, var(--accent)); }

.loc { display: grid; gap: 8px; padding: 12px; border-radius: 12px; background: var(--bg); font-size: 14px; }
.loc-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.error { color: var(--exp-text); font-weight: 600; font-size: 14px; }
.actions { display: flex; gap: 10px; }
.actions .btn { flex: 1; }
.actions .btn.ghost { flex: 0 1 auto; }

.install-app { display: flex; align-items: center; gap: 14px; margin: 4px 0 16px; }
.install-app img { flex: none; border-radius: 13px; box-shadow: var(--shadow); }
.install-steps { display: grid; gap: 12px; margin: 0 0 16px; padding: 0; list-style: none; counter-reset: step; }
.install-steps li { display: flex; align-items: center; gap: 12px; counter-increment: step; }
.install-steps li::before {
  content: counter(step); flex: none; display: grid; place-items: center; width: 28px; height: 28px;
  border-radius: 50%; background: var(--accent); color: var(--on-accent); font-size: 14px; font-weight: 700;
}
.sheet.install > .muted { margin: -4px 0 16px; }
.inline-ico { vertical-align: -3px; color: var(--accent); }
.install-card { display: grid; gap: 12px; justify-items: start; }

.cloud-body, .sheet.cloud form { display: grid; gap: 14px; }
.link-btn { justify-self: start; padding: 4px 0; border: 0; background: none; color: var(--accent); font-weight: 600; }
.recovery-code { padding: 16px; border-radius: 12px; background: var(--bg); border: 1px dashed var(--line); text-align: center; font: 700 22px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .06em; user-select: all; }
.year-sum { display: grid; gap: 4px; }
.year-sum .month-bal { font-size: 26px; }
.hide-empty { padding: 0 4px; font-weight: 500; color: var(--text-2); }
.note { padding: 0 4px; margin-top: -6px; }
.cloud-row > span:first-child { font-weight: 600; }
.cloud-last { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.btn:disabled { opacity: .5; }
.btn.ghost { background: none; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-b) + 16px); z-index: 30;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none; transition: .2s;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 999px;
  background: var(--text); color: var(--bg); font-size: 14px; font-weight: 600;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
