/* Tokens: clar per defecte; fosc pel sistema o pel selector de tema (data-theme guanya en tots dos sentits). */
:root {
  color-scheme: light;
  --bg: #f9f9f7; --surface: #fcfcfb; --ink: #0b0b0b; --ink2: #52514e; --muted: #898781;
  --grid: #e1e0d9; --axis: #c3c2b7; --border: rgba(11, 11, 11, 0.10); --wash: rgba(11, 11, 11, 0.05);
  --on-ink: #ffffff; --pos-text: #006300; --neg-text: #b02a2a;
  --s-inc: #1baf7a; --s-fix: #2a78d6; --s-extra: #eb6834; --s-pos: #2a78d6; --s-neg: #e34948; --s-line: #2a78d6; --s-big: #4a3aa7;
  --ok: #2a78d6; --ok-track: #cde2fb; --warn: #fab219; --warn-track: #fdecc4; --over: #d03b3b; --over-track: #f6d5d5;
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --wash: rgba(255, 255, 255, 0.07);
    --on-ink: #0b0b0b; --pos-text: #0ca30c; --neg-text: #e66767;
    --s-inc: #199e70; --s-fix: #3987e5; --s-extra: #d95926; --s-pos: #3987e5; --s-neg: #e66767; --s-line: #3987e5; --s-big: #9085e9;
    --ok: #3987e5; --ok-track: #0d366b; --warn-track: #4d3a0c; --over-track: #4a1b1b;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --wash: rgba(255, 255, 255, 0.07);
  --on-ink: #0b0b0b; --pos-text: #0ca30c; --neg-text: #e66767;
  --s-inc: #199e70; --s-fix: #3987e5; --s-extra: #d95926; --s-pos: #3987e5; --s-neg: #e66767; --s-line: #3987e5; --s-big: #9085e9;
  --ok: #3987e5; --ok-track: #0d366b; --warn-track: #4d3a0c; --over-track: #4a1b1b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html { background: var(--bg); }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; text-align: inherit; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 15px; font-weight: 650; }
h3 { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin: 14px 0 2px; }
em { font-style: normal; color: var(--muted); }

#app { max-width: 480px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(92px + env(safe-area-inset-bottom, 0px)); }
main { padding: 4px 16px 0; display: flex; flex-direction: column; gap: 12px; }
.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: calc(14px + env(safe-area-inset-top, 0px)) 16px 10px; min-height: 60px; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 20px; color: var(--ink2); }
.icon-btn:hover { background: var(--wash); }

.mnav { display: flex; align-items: center; gap: 4px; }
.mnav span { font-size: 20px; font-weight: 650; letter-spacing: -0.01em; min-width: 9ch; text-align: center; }
.mnav button { width: 36px; height: 36px; border-radius: 50%; font-size: 22px; line-height: 1; display: grid; place-items: center; color: var(--ink2); }
.mnav button:hover { background: var(--wash); }
.mnav button:disabled { opacity: 0.25; cursor: default; background: none; }
.card .mnav { justify-content: center; margin: 6px 0; }
.card .mnav span { font-size: 15px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.card.flush { padding: 4px 0; gap: 0; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.caption, .hero-sub { font-size: 13px; color: var(--ink2); }
.hero-sub.strong { color: var(--ink); font-weight: 600; }
.empty { color: var(--muted); font-size: 14px; padding: 8px 16px; }
.card:not(.flush) .empty { padding: 4px 0; }
.link { color: var(--ink2); font-size: 13px; font-weight: 600; text-decoration: underline; text-underline-offset: 3px; padding: 6px 0; }

.hero { gap: 8px; }
.hero-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.hero-head small { font-size: 13px; color: var(--ink2); }
.hero-num { font-size: 48px; font-weight: 650; letter-spacing: -0.03em; line-height: 1.1; }

/* Estat: sempre icona + text, mai només color. */
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--wash); color: var(--ink); white-space: nowrap; }
.chip.st-ok { box-shadow: inset 0 0 0 1.5px var(--ok); }
.chip.st-warn { box-shadow: inset 0 0 0 1.5px var(--warn); }
.chip.st-over { box-shadow: inset 0 0 0 1.5px var(--over); }
.meter { height: 10px; border-radius: 999px; background: var(--ok-track); overflow: hidden; }
.meter span { display: block; height: 100%; border-radius: 999px; background: var(--ok); transition: width 0.3s; }
.meter.st-warn { background: var(--warn-track); } .meter.st-warn span { background: var(--warn); }
.meter.st-over { background: var(--over-track); } .meter.st-over span { background: var(--over); }
.st-ok { --st: var(--ok); --st-track: var(--ok-track); } .st-warn { --st: var(--warn); --st-track: var(--warn-track); } .st-over { --st: var(--over); --st-track: var(--over-track); }
/* Barra del pla: fixos en gris (no es toquen) + extra gastat en el color d'estat; la resta és el disponible. */
.meter2 { display: flex; gap: 2px; height: 14px; border-radius: 999px; overflow: hidden; background: var(--st-track); }
.meter2 span { height: 100%; transition: width 0.3s; }
.m-fix { --c: var(--muted); background: var(--muted); } .m-extra { --c: var(--st); background: var(--st); border-radius: 0 999px 999px 0; }
.m-big { --c: var(--s-big); background: var(--s-big); } .m-res { background: var(--s-big); opacity: 0.35; }
.meter2.split { background: var(--wash); }
.meter2 .seg { background: var(--c); }
.m-inv { --c: var(--s-big); background: var(--s-big); } .m-liq { --c: var(--s-fix); background: var(--s-fix); }
.s-invest { --c: var(--s-big); } .s-liquid { --c: var(--s-fix); } .s-save { --c: var(--s-inc); }
.worth-num { font-size: 30px; font-weight: 650; letter-spacing: -0.02em; line-height: 1.15; }
.li.total { cursor: default; font-weight: 650; border-top: 1px solid var(--grid); border-radius: 0; margin-top: 4px; }
.li.total:hover { background: none; } .li.total > span:last-child { color: var(--ink); }
.sw.m-free { --c: var(--st-track); box-shadow: inset 0 0 0 1px var(--border); }
.eqs { display: flex; flex-direction: column; }
.eq { display: grid; grid-template-columns: 16px 1fr auto; gap: 6px; padding: 7px 0; font-size: 14px; color: var(--ink2); }
.eq i { font-style: normal; color: var(--muted); } .eq b { font-weight: 600; font-variant-numeric: tabular-nums; color: var(--ink); }
.eq.sum { border-top: 1px solid var(--grid); color: var(--ink); } .eq.strong { font-weight: 650; } .eq.strong b { font-size: 17px; font-weight: 650; }
.bline { display: flex; flex-direction: column; gap: 6px; padding: 6px 0; width: 100%; }
.bline.plain span { font-size: 13px; color: var(--ink2); font-variant-numeric: tabular-nums; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tiles.two { grid-template-columns: repeat(2, 1fr); }
.tiles.four { grid-template-columns: repeat(2, 1fr); }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 10px 12px; min-width: 0; }
.card .tile { background: var(--wash); border: 0; }
.tile small { display: block; font-size: 12px; color: var(--ink2); }
.tile b { font-size: 18px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; }
.pos { color: var(--pos-text); } .neg { color: var(--neg-text); }

.seg { display: inline-flex; background: var(--wash); border-radius: 12px; padding: 3px; gap: 2px; max-width: 100%; }
.seg button { padding: 7px 12px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--ink2); white-space: nowrap; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(0, 0, 0, 0.08); }
.search, .field input, .field select { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); min-width: 0; }
.search:focus, .field input:focus, .field select:focus, .amount input:focus { outline: 2px solid var(--ok); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 13px; color: var(--ink2); }
.field.check { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.field.check input { flex: none; }
.field.check input { width: 22px; height: 22px; accent-color: var(--ink); }

/* Moviments i llistes */
.tx { display: flex; align-items: center; gap: 12px; width: 100%; padding: 9px 16px; }
.card:not(.flush) .tx { padding-left: 0; padding-right: 0; }
.tx:hover, .li:hover { background: var(--wash); }
.tx-ic { width: 38px; height: 38px; border-radius: 12px; background: var(--wash); display: grid; place-items: center; font-size: 18px; flex: none; }
.tx-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.tx-main b { font-weight: 600; font-size: 14px; }
.tx-main small { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.day-head { display: flex; justify-content: space-between; padding: 12px 16px 4px; font-size: 12px; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.li { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: calc(100% + 16px); margin: 0 -8px; padding: 11px 8px; border-radius: 10px; font-size: 14px; cursor: pointer; }
.li > span:last-child { color: var(--ink2); font-variant-numeric: tabular-nums; white-space: nowrap; }

.btn { background: var(--ink); color: var(--on-ink); border-radius: 12px; padding: 12px 18px; font-weight: 600; text-align: center; }
.btn.small { padding: 7px 12px; font-size: 13px; border-radius: 10px; }
.period { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-top: -6px; }
.btn.ghost { background: var(--wash); color: var(--ink); }
.btn.danger { background: none; color: var(--neg-text); box-shadow: inset 0 0 0 1.5px var(--over); margin-right: auto; }

/* Navegació inferior */
.tabs { position: fixed; left: 50%; bottom: 0; transform: translateX(-50%); width: 100%; max-width: 480px; display: grid; grid-template-columns: 1fr 1fr 72px 1fr 1fr; align-items: end;
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom, 0px)); background: var(--surface); border-top: 1px solid var(--border); z-index: 5; }
.tabs button { display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: 11px; font-weight: 600; color: var(--muted); padding: 6px 0; }
.tabs button i { font-style: normal; font-size: 18px; line-height: 1.2; }
.tabs button.on { color: var(--ink); }
.tabs .fab { width: 56px; height: 56px; margin: -22px auto 4px; border-radius: 20px; background: var(--ink); color: var(--on-ink); font-size: 30px; font-weight: 400; display: grid; place-items: center; padding: 0 0 3px; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.22); }

/* Fulls modals */
#sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-bg { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.sheet-panel { position: relative; width: 100%; max-width: 480px; max-height: 92dvh; overflow-y: auto; background: var(--surface); border-radius: 24px 24px 0 0; padding: 20px 16px calc(16px + env(safe-area-inset-bottom, 0px)); }
.sheet-panel form { display: flex; flex-direction: column; gap: 12px; }
.sheet-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.err { color: var(--neg-text); font-size: 13px; font-weight: 600; }
.amount { display: flex; align-items: baseline; justify-content: center; gap: 6px; padding: 6px 0; }
.amount input { width: 6.2ch; min-width: 0; flex: 0 1 auto; border: 0; background: none; font-size: 48px; font-weight: 650; letter-spacing: -0.03em; text-align: right; border-radius: 8px; }
.amount span { font-size: 28px; font-weight: 600; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.catchip { display: inline-flex; align-items: center; gap: 6px; padding: 7px 11px; border-radius: 999px; background: var(--wash); font-size: 13px; font-weight: 600; }
.catchip.on { background: var(--ink); color: var(--on-ink); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

#toast { position: fixed; left: 50%; bottom: calc(104px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--on-ink); padding: 9px 16px; border-radius: 999px; font-size: 13px; font-weight: 600; z-index: 110; max-width: calc(100% - 32px); }
#tip { position: fixed; z-index: 40; pointer-events: none; background: var(--ink); color: var(--on-ink); padding: 7px 10px; border-radius: 10px; font-size: 12px; line-height: 1.45; white-space: pre-line; font-variant-numeric: tabular-nums; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25); }

/* Gràfics: el color de cada sèrie entra per --c; el text sempre va amb tinta, mai amb el color de la sèrie. */
.s-inc { --c: var(--s-inc); } .s-fix { --c: var(--s-fix); } .s-extra { --c: var(--s-extra); }
.s-exp { --c: var(--s-fix); } .s-big { --c: var(--s-big); } .s-ideal { --c: var(--muted); }
.s-pos { --c: var(--s-pos); } .s-neg { --c: var(--s-neg); } .s-line { --c: var(--s-line); }
.chart { display: block; width: 100%; height: auto; overflow: visible; }
.chart path, .chart circle { fill: var(--c); }
.chart .c-line { fill: none; stroke: var(--c); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .c-area { opacity: 0.1; }
.chart .c-dot { stroke: var(--surface); stroke-width: 2; }
.chart .c-grid { stroke: var(--grid); stroke-width: 1; }
.chart .c-base { stroke: var(--axis); stroke-width: 1; }
.chart .c-ideal { stroke: var(--muted); stroke-width: 1.5; }
.chart .c-ref { stroke: var(--ink); stroke-width: 1.5; }
.chart text { font-size: 10px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .c-reflabel, .chart .c-endlabel { fill: var(--ink); font-weight: 600; font-size: 11px; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; }
.chart .c-hit { fill: transparent; cursor: default; }
.chart .c-hit:hover { fill: var(--wash); }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--ink2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 3px; background: var(--c); }
.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 13px; }
.hbar-val { font-variant-numeric: tabular-nums; color: var(--ink2); }
.hbar-track { grid-column: 1 / -1; height: 8px; }
.hbar-fill { display: block; height: 100%; min-width: 3px; background: var(--s-fix); border-radius: 0 4px 4px 0; }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--grid); }
th { font-size: 12px; font-weight: 600; color: var(--ink2); }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
tr:last-child td { border-bottom: 0; }

@media (min-width: 520px) {
  #app { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
  .tabs { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
  .sheet-panel { border-radius: 24px; margin-bottom: 24px; }
}
@media print { .tabs, .top .icon-btn { display: none; } }

/* Bloqueig amb PIN */
#lock { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: grid; place-items: center; padding: 24px 16px; overflow-y: auto; }
.lock-box { width: 100%; max-width: 320px; display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; }
.lock-logo { width: 64px; height: 64px; border-radius: 18px; }
.pin-dots { display: flex; gap: 14px; margin: 14px 0 18px; }
.pin-dots i { width: 14px; height: 14px; border-radius: 50%; box-shadow: inset 0 0 0 1.5px var(--muted); }
.pin-dots i.on { background: var(--ink); box-shadow: none; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; width: 100%; }
.keypad button { height: 64px; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); font-size: 24px; font-weight: 600; text-align: center; }
.keypad button:active { background: var(--wash); }
.btn.wide { width: 100%; display: block; }
label.btn, label.li { cursor: pointer; }
.create-form { width: 100%; display: flex; flex-direction: column; gap: 12px; text-align: left; }
.lock-box > .btn, .lock-box > .link { margin-top: 4px; }
.banner { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; padding: 10px 12px; border-radius: 14px; background: var(--warn-track); font-size: 13px; font-weight: 600; }
.who { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; font-size: 14px; color: var(--ink2); }
.who b { color: var(--ink); }
