:root {
  color-scheme: light dark;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, .12);
  --income: #2a78d6;
  --expense: #eb6834;
  --other: #898781;
  --control: #898781;
  --accent: #256abf;
  --accent-ink: #fff;
  --link: #256abf;
  --good: #006300;
  --bad: #d03b3b;
  --focus: #2a78d6;
}
@media (prefers-color-scheme: dark) {
  :root {
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, .12);
    --income: #3987e5;
    --expense: #d95926;
    --accent: #3987e5;
    --accent-ink: #0b0b0b;
    --link: #6da7ec;
    --good: #0ca30c;
    --bad: #e66767;
    --focus: #6da7ec;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--link); }
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: clamp(1.4rem, 1.1rem + 1.2vw, 1.9rem); font-weight: 650; }
h2 { font-size: 1rem; font-weight: 650; }
p { margin: 0; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* minmax(0, 1fr): without it a wide table sets the column's minimum width and the page overflows on phones */
.wrap { max-width: 1100px; margin: 0 auto; padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.sub, .hint, .foot { color: var(--ink-2); font-size: .9rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.card > h2 + * { margin-top: 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-head + * { margin-top: 12px; }

/* controls */
.btn, select, input[type="search"], textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--control); border-radius: 8px;
}
::placeholder { color: var(--ink-2); opacity: .85; }
.btn { min-height: 40px; padding: 6px 14px; cursor: pointer; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { min-height: 32px; padding: 2px 10px; font-size: .875rem; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn[aria-pressed="true"] { background: var(--surface-2); border-color: var(--ink-2); }
select, input[type="search"] { min-height: 40px; padding: 6px 10px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* input card */
.input { display: grid; gap: 12px; }
.input > * { margin: 0; }
.input.dragging { outline: 3px solid var(--focus); outline-offset: 2px; }
textarea { width: 100%; min-height: 8.5rem; padding: 10px 12px; resize: vertical; font: .875rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.cur { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: .9rem; }
#status { min-height: 1.5em; font-size: .9rem; }
#status.err { color: var(--bad); }
details { font-size: .875rem; color: var(--ink-2); }
summary { cursor: pointer; }
details ul { margin: 8px 0 0; padding-left: 1.2rem; }

/* summary cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); gap: 12px; }
.stat { padding: 14px 16px; }
.stat h2 { color: var(--ink-2); font-size: .875rem; font-weight: 500; }
.stat .v { margin-top: 4px; font-size: clamp(1.35rem, 1rem + 1.4vw, 1.9rem); font-weight: 650; overflow-wrap: anywhere; }
.v.pos { color: var(--good); }
.v.neg { color: var(--bad); }

/* charts */
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 16px; }
.legend { display: flex; gap: 16px; margin: 0 0 8px; padding: 0; list-style: none; color: var(--ink-2); font-size: .875rem; }
.legend i, .key { display: inline-block; border-radius: 2px; vertical-align: middle; }
.legend i { width: 10px; height: 10px; margin-right: 6px; }
.chart-body { position: relative; min-width: 0; }
.income { background: var(--income); }
.expense { background: var(--expense); }

.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.bars li { display: grid; grid-template-columns: minmax(70px, 32%) 1fr; gap: 12px; align-items: center; }
.bar-name { color: var(--ink-2); font-size: .9rem; overflow-wrap: anywhere; }
.bar-track { display: flex; align-items: center; gap: 8px; min-width: 0; }
.bar-fill { flex: 0 0 calc((100% - 7.5rem) * var(--f, 0)); min-width: 2px; height: 16px; background: var(--expense); border-radius: 0 4px 4px 0; }
.bars .other .bar-fill { background: var(--other); }
.bars li:hover .bar-fill { filter: brightness(1.1); }
.bar-value { font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { color: var(--ink-2); font-size: .9rem; }

svg text { font: 11px system-ui, -apple-system, "Segoe UI", sans-serif; fill: var(--ink-2); }
svg .grid { stroke: var(--grid); stroke-width: 1; }
svg .axis { stroke: var(--axis); stroke-width: 1; }
svg .band { fill: var(--surface-2); opacity: 0; }
svg .band.on { opacity: 1; }
svg .b-inc { fill: var(--income); }
svg .b-exp { fill: var(--expense); }
svg text.top { fill: var(--ink); font-weight: 600; }
svg { display: block; max-width: 100%; touch-action: pan-y; }
svg:focus-visible { outline-offset: 4px; border-radius: 4px; }

.tip { position: absolute; top: 0; z-index: 2; pointer-events: none; min-width: 150px; padding: 8px 10px; font-size: .8125rem; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 4px 16px rgba(0, 0, 0, .14); }
.tip h3 { margin: 0 0 4px; font-size: .8125rem; font-weight: 600; color: var(--ink-2); }
.tip div { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.tip strong { font-variant-numeric: tabular-nums; }
.key { width: 10px; height: 3px; margin-right: 6px; }

/* table */
.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filters input[type="search"] { flex: 1 1 200px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; }
td:nth-child(2) { white-space: normal; overflow-wrap: anywhere; min-width: 10rem; }
tbody tr:last-child td { border-bottom: 0; }
th { background: var(--surface-2); font-weight: 600; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
th button { font: inherit; font-weight: inherit; color: inherit; background: none; border: 0; padding: 4px 0; cursor: pointer; }
th[aria-sort="ascending"] button::after { content: " \25B2"; font-size: .7em; }
th[aria-sort="descending"] button::after { content: " \25BC"; font-size: .7em; }
td.pos { color: var(--good); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); font-size: .8125rem; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-top: 12px; color: var(--ink-2); font-size: .9rem; }
.pager .row { gap: 6px; }

/* help dialog */
dialog { width: min(640px, calc(100% - 32px)); max-height: calc(100% - 32px); padding: 0; color: var(--ink); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dialog-body { padding: 20px; display: grid; gap: 12px; }
.dialog-body h3 { margin: 8px 0 0; font-size: .95rem; }
.dialog-body ul { margin: 0; padding-left: 1.2rem; }
.dialog-body pre { margin: 0; padding: 10px 12px; overflow-x: auto; background: var(--surface-2); border-radius: 8px; font-size: .8125rem; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

@media (max-width: 560px) {
  .wrap { padding: 12px; }
  /* tables scroll sideways inside their card; keep the last (amount) column pinned in view */
  th, td { padding: 8px; }
  td:nth-child(2) { min-width: 6.5rem; }
  th:last-child, td:last-child { position: sticky; right: 0; background: var(--surface); border-left: 1px solid var(--grid); }
  th:last-child { background: var(--surface-2); }
  .cur { margin-left: 0; width: 100%; }
  .bars li { grid-template-columns: 1fr; gap: 2px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
