/* Palette follows the validated reference data-viz palette (light + dark). Shared by every page. */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --series-1: #2a78d6;   /* blue: the main series, bars */
  --series-2: #eb6834;   /* orange: what is selected */
  --series-3: #1baf7a;
  --warn: #fab219;
  --good: #006300;
  --bad: #d03b3b;        /* errors, Delete */
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --good: #0ca30c;
    --bad: #ef6b6b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }
/* The browser's own [hidden] rule is weaker than any id or class selector that
   sets display, so elements toggled with .hidden = true would stay on screen.
   This makes the attribute mean what it says. */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--page);
  padding: 0 20px 32px;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
p { margin: 0; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
a { color: var(--series-1); }
/* The page stops at 1800px and sits in the middle, so an ultrawide doesn't
   spread a table across 3,000px; the side padding stays on body. */
.topbar, .filters, .tabs, main { max-width: 1800px; margin-left: auto; margin-right: auto; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 16px 0 12px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 26px; line-height: 1; color: var(--series-1); }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
/* Who is signed in, with Sign out beside the name. */
.account { display: inline-flex; gap: 6px; align-items: center; padding-left: 8px; border-left: 1px solid var(--border); font-size: 13px; }

.btn {
  font: inherit; font-weight: 550; cursor: pointer; text-decoration: none;
  padding: 7px 12px; border-radius: 8px; border: 1px solid transparent;
  background: var(--series-1); color: #fff;
}
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--border); }
.btn[disabled] { opacity: 0.6; cursor: progress; }
.btn.small { padding: 4px 9px; font-size: 12px; }

.kpis {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;
  margin-bottom: 14px;
}
@media (max-width: 760px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}
.kpi .label { font-size: 12px; color: var(--ink-2); }
.kpi .value { font-size: 26px; font-weight: 650; letter-spacing: -0.02em; margin-top: 2px; }
.kpi .value small { font-size: 14px; font-weight: 500; color: var(--ink-2); margin-left: 2px; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.card {
  margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px 8px; box-shadow: var(--shadow);
}
.card figcaption { font-weight: 600; font-size: 13px; color: var(--ink-2); margin-bottom: 6px; }
/* A caption with controls on its right (the Overview chart's Day | Week | Month); they wrap under it when narrow. */
.card-head { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; justify-content: space-between; }
.card-head .seg button { font-size: 12px; padding: 2px 10px; }
.chart-box { position: relative; height: 240px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--ink);
  padding: 20px 22px; width: min(720px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog input:not([type="checkbox"]):not([type="radio"]):not([type="file"]), dialog select, dialog textarea {
  font: inherit; color: var(--ink); background: var(--page);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px;
}
dialog textarea { resize: vertical; }
.form-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
.form-msg { font-size: 13px; margin: 8px 0; color: var(--ink-2); }
.form-msg.error { color: var(--bad); }

/* One message at a time along the bottom; inverted colours so it reads in either theme. */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 2000;
  display: flex; align-items: center; gap: 12px; max-width: calc(100vw - 32px);
  padding: 10px 10px 10px 16px; border-radius: 10px; font-size: 13px;
  background: var(--ink); color: var(--page); box-shadow: var(--shadow);
}
.toast.error { border-left: 5px solid var(--bad); }
.toast-action {
  font: inherit; font-weight: 650; cursor: pointer; color: inherit; background: none;
  border: 1px solid color-mix(in srgb, var(--page) 45%, transparent); border-radius: 6px; padding: 3px 10px;
}
.toast-close { font: inherit; cursor: pointer; color: inherit; background: none; border: none; opacity: 0.7; padding: 2px 6px; }
.toast-close:hover, .toast-action:hover { opacity: 1; filter: brightness(1.2); }

/* The message every tab but Import shows while there are no transactions yet. */
.empty { margin: 48px auto; max-width: 520px; text-align: center; color: var(--ink-2); font-size: 15px; }
.empty .btn { margin-top: 14px; }

/* ---- the dashboards ---- */
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.card + .card, .kpis + .card, .toolbar + .card, .import-grid + .card { margin-top: 14px; }
.chart-box.tall { height: 320px; }

/* The account menu: a <details> that drops down under the name. */
.menu { position: relative; }
.menu > summary { list-style: none; display: inline-block; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 1000; min-width: 190px;
  display: flex; flex-direction: column; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
}
.menu-list button {
  font: inherit; text-align: left; color: var(--ink); background: none; border: 0; border-radius: 6px;
  padding: 7px 10px; cursor: pointer;
}
.menu-list button:hover { background: var(--page); }

/* One row of filters above everything they scope: the range, then the person. */
.filters { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; margin-bottom: 10px; }
.range { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.seg, .pills { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.seg button, .pills button {
  font: inherit; font-size: 13px; cursor: pointer; color: var(--ink-2);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 4px 11px;
}
.seg button:hover, .pills button:hover { color: var(--ink); }
.seg button[aria-pressed="true"], .pills button[aria-pressed="true"] {
  color: var(--ink); font-weight: 600; border-color: var(--series-1);
  background: color-mix(in srgb, var(--series-1) 10%, var(--surface));
}
.custom { display: inline-flex; gap: 6px; align-items: center; }
input[type="date"], input[type="search"], input[type="text"], select {
  font: inherit; font-size: 13px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; max-width: 100%;
}

/* Tabs: buttons that show one section at a time. */
.tabs {
  display: flex; gap: 2px; overflow-x: auto; margin-bottom: 14px;
  border-bottom: 1px solid var(--border); scrollbar-width: none;
}
.tabs button {
  font: inherit; font-weight: 550; cursor: pointer; color: var(--ink-2); background: none; white-space: nowrap;
  border: 0; border-bottom: 2px solid transparent; padding: 8px 12px; margin-bottom: -1px;
}
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--series-1); }

/* While a tab reloads, its old numbers stay put, faded: no flash, no jump. */
.tab.loading { opacity: 0.55; transition: opacity 0.15s 0.1s; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar label { display: inline-flex; gap: 6px; align-items: center; color: var(--ink-2); font-size: 13px; }
#tx-q { flex: 1 1 240px; min-width: 0; }

/* Stat tiles: a change reads up (spending more: red) or down (less: green), with an arrow, never colour alone. */
.kpi .value.up, td.up { color: var(--bad); }
.kpi .value.down, td.down { color: var(--good); }

/* Tables scroll sideways inside their card on a narrow screen; the header row stays put. */
.table-wrap { overflow: auto; max-height: 70vh; margin: 0 -14px; padding: 0 14px; }
table.data { width: 100%; table-layout: auto; border-collapse: collapse; font-size: 13px; }
table.data th {
  position: sticky; top: 0; z-index: 1; background: var(--surface);
  text-align: left; font-weight: 600; font-size: 12px; color: var(--ink-2);
  padding: 7px 10px; border-bottom: 1px solid var(--axis); white-space: nowrap;
}
table.data th.num { text-align: right; }
table.data td { padding: 4px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
/* The text column (.grow) takes the slack; every other column is as wide as
   its content, so the numbers sit next to each other instead of spreading out. */
table.data .grow { width: 100%; }
table.data:has(.grow) td:not(.grow) { white-space: nowrap; }
table.data tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--page) 60%, var(--surface)); }
table.data tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td, tr.clickable:focus-visible td { background: color-mix(in srgb, var(--series-1) 7%, var(--surface)); }
tr.clickable:focus-visible { outline: 2px solid var(--series-1); outline-offset: -2px; }
td.amount { white-space: nowrap; }
tr.kind-refund td.amount { color: var(--good); }
tr.kind-payment td { color: var(--muted); }
tr.stopped td { color: var(--muted); }

button.link {
  font: inherit; color: var(--ink); background: none; border: 0; padding: 0; cursor: pointer; text-align: left;
  text-decoration: underline; text-decoration-color: var(--axis); text-underline-offset: 3px;
}
button.link:hover { text-decoration-color: var(--series-1); }

/* A category's colour as a small square beside its name: the colour marks, the text names. */
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; vertical-align: -1px; }
.cat-cell { display: inline-block; vertical-align: middle; white-space: nowrap; }
.cat-cell select { max-width: 190px; padding: 2px 6px; font-size: 12px; }
.source { display: block; font-size: 11px; color: var(--muted); margin-left: 18px; }
td > .source { margin-left: 0; }
.cat-cell .source { white-space: normal; max-width: 220px; }

/* Share of the range's spending: a thin bar on a hairline track, with the percent beside it. */
.share-col { white-space: nowrap; }
.share { display: inline-block; width: 200px; height: 6px; border-radius: 3px; background: var(--grid); vertical-align: middle; overflow: hidden; }
.share-bar { display: block; height: 100%; border-radius: 3px; }
.share-pct { display: inline-block; width: 40px; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 12px; }

.badge, .pill, .chip {
  display: inline-block; font-size: 11px; font-weight: 600; line-height: 1.5; white-space: nowrap;
  padding: 0 7px; border-radius: 999px; border: 1px solid var(--border); color: var(--ink-2); background: var(--page);
}
.badge.refund { color: var(--good); }
.pill.good { color: var(--good); border-color: color-mix(in srgb, var(--good) 35%, transparent); }
.chip { font-size: 12px; padding: 3px 4px 3px 10px; }
.chip button { font: inherit; cursor: pointer; background: none; border: 0; color: var(--ink-2); padding: 0 4px; }

.pager { display: flex; gap: 12px; align-items: center; justify-content: flex-end; padding: 10px 0 4px; font-size: 13px; }

/* Import: the drop zone beside the how-to. */
.import-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; }
@media (max-width: 760px) { .import-grid { grid-template-columns: minmax(0, 1fr); } }
.drop {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; min-height: 160px;
  border: 2px dashed var(--axis); border-radius: var(--radius); background: var(--surface); cursor: pointer; text-align: center; padding: 20px;
}
.drop:hover, .drop.over { border-color: var(--series-1); background: color-mix(in srgb, var(--series-1) 6%, var(--surface)); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop:focus-within { outline: 2px solid var(--series-1); outline-offset: 2px; }
.result { margin-top: 14px; display: grid; gap: 6px; }
.result.error { color: var(--bad); }
.help h2 { margin-bottom: 8px; }
.help ol { margin: 0; padding: 0 0 8px 20px; display: grid; gap: 6px; color: var(--ink-2); }

/* Dialogs */
dialog h2 { margin-bottom: 8px; }
.icon-btn { font: inherit; font-size: 15px; cursor: pointer; color: var(--ink-2); background: none; border: 0; border-radius: 6px; padding: 2px 8px; }
.icon-btn:hover { color: var(--ink); background: var(--page); }

/* The detail dialog: a merchant, or a category in a month, looked at closely. */
dialog.detail { width: min(960px, calc(100vw - 32px)); }
.detail.loading > :not(.detail-head) { opacity: 0.55; transition: opacity 0.15s 0.1s; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.detail-head h2 { font-size: 18px; margin-bottom: 4px; }
.detail-sub { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; font-size: 13px; }
.detail-kpis { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.detail-kpis .kpi .value { font-size: 18px; }
.detail .table-wrap { max-height: 50vh; }
.detail .card + .card, .detail .kpis + .card { margin-top: 12px; }
.detail .pager { justify-content: space-between; }
/* Wide screens: the detail docks on the right, full height, scrolling inside,
   with no backdrop, and the page makes room for it so nothing hides under it. */
dialog.detail.docked {
  position: fixed; inset: 0 0 0 auto; z-index: 1500; margin: 0;
  width: 560px; max-width: 560px; height: 100vh; max-height: 100vh; overflow: auto;
  border-radius: 0; border-width: 0 0 0 1px; box-shadow: -12px 0 32px rgba(0, 0, 0, 0.18);
}
body.detail-docked { padding-right: calc(560px + 20px); }
dialog h3 { font-size: 13px; margin: 16px 0 2px; }
.field { display: grid; gap: 4px; margin: 10px 0; font-size: 13px; color: var(--ink-2); }
.invite-link { display: flex; gap: 6px; margin: 10px 0; }
.invite-link input { flex: 1; min-width: 0; }

/* Wide screens: the chart beside the table on Overview and Categories (both as
   tall as the row), the monthly cost beside the table on Subscriptions. */
@media (min-width: 1500px) {
  #tab-overview, #tab-categories { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 14px; }
  #tab-overview > .kpis, #tab-categories > .toolbar { grid-column: 1 / -1; margin-bottom: 0; }
  #tab-overview > .card, #tab-categories > .card { margin-top: 0; display: flex; flex-direction: column; }
  /* The chart fills its card; contain: size keeps the canvas from growing the row it sizes to. */
  #tab-overview .chart-box, #tab-categories .chart-box { flex: 1 1 auto; min-height: 320px; contain: size; }
  #tab-subscriptions { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(0, 4fr); gap: 14px; align-items: start; }
  #tab-subscriptions > .kpis { grid-template-columns: minmax(0, 1fr); margin-bottom: 0; }
  #tab-subscriptions > .card { margin-top: 0; }
}

@media (max-width: 520px) {
  body { padding: 0 12px 32px; }
  .share { width: 70px; }
  .kpi .value { font-size: 22px; }
  .chart-box.tall { height: 260px; }
}

/* Rules: the order categories are decided in, the Add row, the built-in rules grouped by category. */
.precedence { margin: 0 0 12px; color: var(--ink-2); }
.add-rule { margin-bottom: 10px; }
.add-rule .toolbar { margin-bottom: 4px; }
.add-rule input[type="text"] { width: 220px; }
#rule-preview { min-height: 1.45em; overflow-wrap: anywhere; }
.btn.danger { background: var(--bad); }
.rule-remove { text-align: right; }
.rule-groups { columns: 3 280px; column-gap: 24px; padding-bottom: 6px; }
.rule-group { break-inside: avoid; margin-bottom: 14px; }
.rule-group h3 { font-size: 13px; margin-bottom: 4px; }
.rule-group ul { list-style: none; margin: 0; padding: 0 0 0 18px; display: grid; gap: 3px; font-size: 13px; }
.rule-group li { overflow-wrap: anywhere; }
.rule-group li.dim { opacity: 0.55; }
#rule-filter { width: 260px; }
@media (max-width: 520px) {
  .add-rule .toolbar label { flex: 1 1 100%; flex-wrap: wrap; }
  .add-rule input[type="text"], #rule-filter { width: 100%; }
}

/* Import: the staged file waiting for the Import button. */
.ready { margin-top: 14px; }
.ready p { margin: 0 0 8px; }
