:root {
  --bg: #f6f7f9; --fg: #1d2430; --muted: #646c78; --card: #fff; --line: #dde1e7; --line-strong: #c4cad3;
  --accent: #1f5fbf; --input: #1747c9; --input-bg: #f3f6ff; --error: #b3261e; --negative: #b3261e;
  --series-1: #257de0; --track: #eef1f5;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --fg: #e6e9ee; --muted: #9aa3af; --card: #1c2027; --line: #2e343d; --line-strong: #444c57;
    --accent: #6ea0ff; --input: #8fb4ff; --input-bg: #1f2633; --error: #ff8a80; --negative: #ff8a80;
    --series-1: #338ced; --track: #262b33;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--fg); }
.top { display: flex; align-items: center; justify-content: space-between; padding: .7rem 1.25rem; border-bottom: 1px solid var(--line); background: var(--card); }
.brand { font-weight: 700; color: var(--fg); text-decoration: none; }
.signout { display: flex; gap: .75rem; align-items: center; margin: 0; }
.who { color: var(--muted); }
body.saving .brand::after { content: " · saving…"; font-weight: 400; color: var(--muted); }
main { max-width: 1100px; margin: 1.5rem auto 4rem; padding: 0 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1.25rem; margin-bottom: 1.25rem; }
.card.narrow { max-width: 420px; margin: 4rem auto; text-align: center; }
.hint { border-style: dashed; }
h1 { margin-top: 0; font-size: 1.5rem; }
h2 { margin: 0 0 .75rem; font-size: 1.1rem; }
.muted { color: var(--muted); }
.error { color: var(--error); }
.flash { background: color-mix(in srgb, var(--error) 12%, var(--card)); border: 1px solid var(--error); border-radius: 8px; padding: .6rem .9rem; margin: 0 0 1rem; }
a { color: var(--accent); }
button, .button { display: inline-block; font: inherit; padding: .45rem .9rem; border-radius: 6px; border: 1px solid var(--line-strong);
                  background: var(--card); color: var(--fg); cursor: pointer; text-decoration: none; }
button.small { padding: .25rem .7rem; font-size: .9rem; }
button.icon { border: none; background: none; padding: .1rem .35rem; color: var(--muted); font-size: 1rem; line-height: 1; }
button.icon:hover { color: var(--fg); }
button.icon.danger:hover { color: var(--error); }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
input, select { font: inherit; color: var(--fg); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Heading */
.sheet-head { margin-bottom: 1rem; }
.title-input { font-size: 1.6rem; font-weight: 700; border: 1px solid transparent; background: none; padding: .1rem .3rem; margin-left: -.3rem; border-radius: 6px; width: 100%; max-width: 30rem; }
.title-input:hover, .title-input:focus { border-color: var(--line-strong); background: var(--card); }
.sheet-head p { margin: .3rem 0 0; }
.input-sample { color: var(--input); font-weight: 600; }

/* KPI row */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: .9rem 1.1rem; display: flex; flex-direction: column; }
.kpi-label { color: var(--muted); font-size: .9rem; }
.kpi-value { font-size: 1.7rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.kpi-note { color: var(--muted); font-size: .85rem; }
.kpi.is-negative .kpi-value { color: var(--negative); }

/* The sheet */
.table-scroll { overflow-x: auto; }
table.sheet { width: 100%; border-collapse: collapse; min-width: 640px; }
.sheet th, .sheet td { padding: .2rem .4rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.sheet thead th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; border-bottom-color: var(--line-strong); }
.sheet .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sheet .actions { text-align: right; white-space: nowrap; width: 1%; }
.sheet tfoot th, .sheet tfoot td { font-weight: 700; border-top: 2px solid var(--line-strong); border-bottom: none; padding-top: .5rem; }
.cat-row th, .cat-row td { padding-top: .9rem; border-bottom-color: var(--line-strong); }
.cat-row .subtotal { font-weight: 600; }
.cat-input { font-weight: 650; border: 1px solid transparent; background: none; padding: .15rem .3rem; border-radius: 4px; width: 100%; max-width: 20rem; }
.cat-input:hover, .cat-input:focus { border-color: var(--line-strong); }
.cell-input { width: 100%; border: 1px solid transparent; background: none; color: var(--input); padding: .25rem .35rem; border-radius: 4px; }
.cell-input:hover { border-color: var(--line); background: var(--input-bg); }
.cell-input:focus { border-color: var(--input); background: var(--input-bg); outline: none; }
.cell-input.num { text-align: right; max-width: 9rem; }
.cell-input.narrow { max-width: 5rem; }
select.cell-input { width: auto; cursor: pointer; }
.add-row .cell-input { border-color: var(--line); border-style: dashed; color: var(--fg); }
.add-row .cell-input::placeholder { color: var(--muted); }
.add-row td { border-bottom: none; padding-bottom: .5rem; }
.add-category { display: flex; gap: .5rem; align-items: center; margin-top: 1rem; flex-wrap: wrap; }
.add-category input, .add-category select { padding: .3rem .5rem; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--card); }


/* Fixed columns so Income and Expenses line up, and debt inputs fit. */
table.sheet { table-layout: fixed; }
.sheet:not(.debts) thead th:nth-child(2) { width: 9.5rem; }
.sheet:not(.debts) thead th:nth-child(3) { width: 9.5rem; }
.sheet:not(.debts) thead th:nth-child(4) { width: 8.5rem; }
.sheet:not(.debts) thead th:nth-child(5) { width: 5.5rem; }
table.sheet.debts { min-width: 1024px; }
.debts thead th:nth-child(1) { width: 10rem; }
.debts thead th:nth-child(2) { width: 6.5rem; }
.debts thead th:nth-child(3) { width: 7rem; }
.debts thead th:nth-child(4) { width: 5rem; }
.debts thead th:nth-child(5) { width: 12rem; }
.debts thead th:nth-child(6) { width: 8rem; }
.debts thead th:nth-child(7) { width: 6rem; }
.debts thead th:nth-child(8) { width: 7rem; }
.debts thead th:nth-child(9) { width: 2.5rem; }
.sheet select.cell-input { max-width: 100%; text-overflow: ellipsis; }
.debts select.cell-input { width: 100%; }
.debts .cell-input.num { max-width: none; }

/* Months */
.month-nav { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin: .5rem 0; }
.month-input { font: inherit; padding: .4rem .5rem; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--card); }
.month-pick { margin: 0; display: flex; gap: .4rem; }
.month-banner { background: color-mix(in srgb, var(--accent) 10%, var(--card)); border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--card));
                border-radius: 8px; padding: .55rem .85rem; margin: .5rem 0 0; }
.small-print { font-size: .85rem; margin-top: .25rem; }
.change-note { display: block; font-size: .75rem; color: var(--muted); }
.history .is-viewed td { background: var(--input-bg); font-weight: 600; }
.history .negative, .negative { color: var(--negative); }
table.sheet.compact { min-width: 0; table-layout: auto; }
table.sheet.compact th, table.sheet.compact td { padding: .35rem .5rem; }

/* Spending by category: table + "Where the Money Goes" pie.
   Categorical palette, fixed order (validated reference palette). */
.spending { display: grid; grid-template-columns: minmax(0, 1fr) minmax(240px, 340px); gap: 2rem; align-items: start; }
@media (max-width: 760px) { .spending { grid-template-columns: 1fr; } }
.pie-figure { margin: 0; text-align: center; }
.pie-figure figcaption { font-weight: 650; margin-bottom: .5rem; }
.pie { width: 100%; max-width: 280px; height: auto; display: block; margin: 0 auto; }
.slice { stroke: var(--card); stroke-width: 2; stroke-linejoin: round; }
.slice:hover { opacity: .85; }
.legend { list-style: none; padding: 0; margin: .75rem 0 0; display: flex; flex-wrap: wrap; gap: .3rem .9rem; justify-content: center; font-size: .9rem; }
.swatch { display: inline-block; width: .75rem; height: .75rem; border-radius: 3px; margin-right: .4rem; vertical-align: -1px; }
.swatch.none { background: transparent; border: 1px solid var(--line-strong); }
:root { --s1: #257de0; --s2: #f47230; --s3: #00b894; --s4: #f2a600; --s5: #ee7fac; --s6: #008a17; --s7: #6a4ce2; --s8: #ec4647; }
@media (prefers-color-scheme: dark) {
  :root { --s1: #338ced; --s2: #e76624; --s3: #00a888; --s4: #d28e05; --s5: #e24d8b; --s6: #0aac23; --s7: #9887ef; --s8: #eb6263; }
}
.s1 { fill: var(--s1); background: var(--s1); } .s2 { fill: var(--s2); background: var(--s2); }
.s3 { fill: var(--s3); background: var(--s3); } .s4 { fill: var(--s4); background: var(--s4); }
.s5 { fill: var(--s5); background: var(--s5); } .s6 { fill: var(--s6); background: var(--s6); }
.s7 { fill: var(--s7); background: var(--s7); } .s8 { fill: var(--s8); background: var(--s8); }

/* Navigation */
.main-nav { display: flex; gap: 1.1rem; margin-right: auto; margin-left: 1.5rem; }
.main-nav a { color: var(--muted); text-decoration: none; padding: .2rem 0; border-bottom: 2px solid transparent; }
.main-nav a:hover { color: var(--fg); }
.main-nav a[aria-current="page"] { color: var(--fg); border-bottom-color: var(--accent); }

/* Import and transactions */
.results { list-style: none; padding: 0; margin: 0 0 1rem; }
.results li { padding: .45rem .8rem; border-radius: 8px; margin-bottom: .35rem; border: 1px solid var(--line); }
.results li.ok { background: color-mix(in srgb, var(--s6) 10%, var(--card)); border-color: color-mix(in srgb, var(--s6) 45%, var(--card)); }
.results li.failed { background: color-mix(in srgb, var(--error) 10%, var(--card)); border-color: var(--error); }
.upload { display: grid; gap: .8rem; max-width: 640px; }
.upload label { display: grid; gap: .25rem; font-weight: 600; }
.upload input[type=file], .upload select, .upload input[type=text] { font-weight: 400; padding: .4rem .5rem; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--card); }
.upload .actions-row { display: flex; gap: .75rem; align-items: center; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; margin: .75rem 0 1rem; }
.tabs { display: flex; gap: .25rem; }
.tabs a { padding: .3rem .75rem; border-radius: 999px; border: 1px solid var(--line-strong); text-decoration: none; color: var(--fg); }
.tabs a[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip { display: inline-flex; gap: .4rem; align-items: center; padding: .2rem .65rem; border-radius: 999px; background: var(--input-bg); }
table.txns { min-width: 860px; table-layout: auto; }
.txns td { padding: .35rem .45rem; }
.txns .when { white-space: nowrap; width: 6.5rem; }
.txns .acct { color: var(--muted); font-size: .88rem; width: 9rem; }
.txns .desc small { display: block; color: var(--muted); font-size: .8rem; }
.txns .amount { width: 7.5rem; }
.txns .money-in { color: var(--s6); }
.txns .sort { width: 13rem; }
.txns .always { width: 15rem; font-size: .88rem; }
.txns .always label { display: flex; gap: .35rem; align-items: center; }
.txns .always input[type=text] { width: 100%; font-size: .85rem; padding: .15rem .3rem; border: 1px solid var(--line); border-radius: 4px; background: var(--card); }
.txns tr.is-transfer td, .txns tr.is-loan td { color: var(--muted); }
.how { display: block; font-size: .72rem; color: var(--muted); }
.unsorted td { font-style: italic; }
.rule-list { columns: 2 18rem; padding: 0; list-style: none; font-size: .9rem; }
.rule-list li { break-inside: avoid; display: flex; justify-content: space-between; gap: .5rem; padding: .15rem 0; border-bottom: 1px solid var(--line); }
.inline { display: inline; margin: 0; }
.txns tr.is-done td { opacity: .45; } .txns tr.is-done .sort::after { content: "sorted"; font-size: .75rem; color: var(--muted); margin-left: .4rem; }
#spending td:first-child { white-space: nowrap; }
.txns .desc { min-width: 16rem; }
.txns .sort { width: 11rem; }
