:root {
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-invest: #1baf7a;
  --accent-renovation: #eb6834;
  --accent-tax: #4a3aa7;
  --danger: #d03b3b;
  --series-1: #2a78d6;
  --series-2: #1baf7a;
  --series-3: #eda100;
  --series-4: #008300;
  --series-5: #4a3aa7;
  --series-6: #e34948;
  --series-7: #e87ba4;
  --series-8: #eb6834;
  --other: #898781;
}
@media (prefers-color-scheme: dark) {
  :root {
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-invest: #199e70;
    --accent-renovation: #d95926;
    --accent-tax: #9085e9;
    --danger: #d03b3b;
    --series-1: #3987e5;
    --series-2: #199e70;
    --series-3: #c98500;
    --series-4: #008300;
    --series-5: #9085e9;
    --series-6: #e66767;
    --series-7: #d55181;
    --series-8: #d95926;
    --other: #898781;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

/* The browser's default link blue is close to unreadable on the dark page. */
a { color: var(--accent); }

.wrap {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

header.topbar {
  display: flex;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 20px;
  /* Without this the meta text is crushed into a ~60px column on a phone and
     wraps to seven lines while the buttons hold their width. */
  flex-wrap: wrap;
}
header.topbar h1 { font-size: 20px; font-weight: 650; margin: 0; }
header.topbar .meta { color: var(--text-muted); font-size: 13px; }
header.topbar .spacer { flex: 1; }

/* Narrow screens: title and buttons on one row, the status text on its own
   below, where it has the full width to read normally. */
@media (max-width: 700px) {
  header.topbar { gap: 8px 12px; }
  header.topbar h1 { flex: 0 0 auto; }
  header.topbar .spacer { order: 1; flex: 1; }
  header.topbar button,
  header.topbar .topbar-link { order: 2; }
  header.topbar .meta { order: 3; flex: 1 0 100%; }
}
header.topbar .topbar-link { text-decoration: none; }
header.topbar .topbar-link:hover { color: var(--text-primary); }

button, .btn {
  font: inherit;
  font-size: 13px;
  color: var(--text-secondary);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 14px;
  cursor: pointer;
}
button:hover { color: var(--text-primary); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
button:disabled { opacity: 0.5; cursor: default; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.tile .label { font-size: 13px; color: var(--text-secondary); }
.tile .value { font-size: 28px; font-weight: 650; margin-top: 2px; }
.tile .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px 14px;
  margin-bottom: 14px;
}
.card h2 { font-size: 15px; font-weight: 650; margin: 0 0 2px; }
.card .sub { font-size: 13px; color: var(--text-muted); margin: 0 0 12px; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
@media (max-width: 760px) { .grid-2 { grid-template-columns: 1fr; } }

.chart-box { position: relative; height: 300px; }
.chart-box.tall { height: 340px; }
.chart-box.donut { height: 260px; }

.loading { opacity: 0.45; transition: opacity 0.2s; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin: 10px 0 4px;
  font-size: 13px;
  color: var(--text-secondary);
}
.legend .key { display: inline-flex; align-items: center; gap: 7px; }
.legend .swatch { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

table.cats {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
  margin-top: 8px;
}
table.cats th {
  text-align: right;
  font-weight: 600;
  color: var(--text-muted);
  padding: 6px 8px;
  border-bottom: 1px solid var(--grid);
}
table.cats th:first-child { text-align: left; }
table.cats td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--grid);
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}
table.cats td:first-child { text-align: left; color: var(--text-secondary); }
table.cats tr:last-child td { border-bottom: none; }
table.cats tr.providers-total td { font-weight: 650; border-top: 1px solid var(--baseline); color: var(--text-primary); }

.err {
  color: var(--danger);
  font-size: 13.5px;
  margin: 8px 0;
  min-height: 20px;
}

/* Transaction detail page */
#detail[hidden] { display: none; }
.detail-head {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.detail-head h2 { font-size: 20px; font-weight: 650; margin: 0; }
.detail-head .meta { color: var(--text-muted); font-size: 13px; }
.detail-list-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.detail-list-head h2 { margin: 0; }
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--page);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 3px 6px 3px 12px;
  cursor: pointer;
}
.filter-chip[hidden] { display: none; }
.filter-chip .x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--border);
  font-size: 12px;
  line-height: 1;
}
.filter-chip:hover .x { background: var(--accent); color: #fff; }

.tx-search {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) repeat(3, minmax(125px, 1fr)) auto;
  gap: 10px;
  align-items: end;
  margin: 12px 0;
}
.tx-search.compact { margin: 10px 0 14px; }
.tx-search label { color: var(--text-muted); font-size: 11.5px; }
.tx-search input, .tx-search select {
  display: block;
  width: 100%;
  margin-top: 3px;
  padding: 7px 9px;
  color: var(--text-primary);
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 7px;
  font: inherit;
  font-size: 13px;
}
.tx-search-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tx-search-status { color: var(--text-muted); font-size: 12.5px; margin: 4px 0 10px; }
@media (max-width: 850px) {
  .tx-search { grid-template-columns: 1fr 1fr; }
  .tx-search label:first-child { grid-column: 1 / -1; }
}

table.txs { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.txs th {
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  padding: 8px 8px;
  border-bottom: 1px solid var(--grid);
  position: sticky;
  top: 0;
  background: var(--surface);
}
table.txs th.sortable { cursor: pointer; user-select: none; }
table.txs th.sortable:hover { color: var(--text-primary); }
table.txs td {
  padding: 8px 8px;
  border-bottom: 1px solid var(--grid);
  vertical-align: middle;
}
table.txs tr:last-child td { border-bottom: none; }
table.txs tr.tx-row { cursor: pointer; }
table.txs tr.tx-row:hover td { background: var(--page); }
table.txs .tx-caret {
  display: inline-block;
  color: var(--text-muted);
  font-size: 10px;
  margin-right: 6px;
  transition: transform 0.12s;
}
table.txs tr.tx-row.open .tx-caret { transform: rotate(90deg); }
table.txs .col-amount { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.txs .col-amount.income { color: var(--accent-invest); }
table.txs .tx-merchant { color: var(--text-primary); }
table.txs .tx-acct { color: var(--text-muted); font-size: 12px; }
table.txs .tx-return-link { color: var(--accent); }
table.txs .tx-counted { color: var(--text-muted); font-size: 11px; font-weight: 400; }
table.txs tr.tx-row.is-linked td { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
table.txs .tx-date { color: var(--text-muted); white-space: nowrap; }
table.txs tr.is-internal .tx-merchant,
table.txs tr.is-internal .col-amount { color: var(--text-muted); text-decoration: line-through; }

/* ▾ opens the manual-classification menu: an invisible native select covers
   the icon, so the browser's own dropdown renders the options. */
.classify-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
}
.classify-btn:hover { color: var(--text-primary); border-color: var(--baseline); }
.classify-btn.overridden { border-color: var(--accent); color: var(--accent); }
.classify-btn select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
.tx-empty { color: var(--text-muted); padding: 20px 8px; text-align: center; }

table.txs tr.tx-detail td { padding: 0 8px 10px; background: var(--page); }
dl.raw {
  display: grid;
  grid-template-columns: minmax(120px, max-content) 1fr;
  gap: 2px 14px;
  margin: 4px 0 6px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--grid);
  border-radius: 8px;
  font-size: 12.5px;
}
dl.raw dt { color: var(--text-muted); font-variant-numeric: tabular-nums; }
dl.raw dd {
  margin: 0;
  color: var(--text-primary);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  word-break: break-word;
}

/* AI review controls */
.tx-ai { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; align-items: center; max-width: 240px; }
.tx-ai:empty { display: none; }
.tx-manual { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; align-items: center; max-width: 240px; }
.tx-manual:empty { display: none; }
.ai-btn {
  font-size: 11.5px;
  padding: 2px 9px;
  border-radius: 999px;
}
.ai-btn.suggest { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.ai-btn.suggest:hover { background: var(--accent); color: #fff; }
.ai-btn.ghost { border-color: transparent; background: none; color: var(--text-muted); padding: 2px 5px; }
.ai-badge {
  font-size: 11px;
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}
.ai-badge.ok { color: var(--accent-invest); border-color: color-mix(in srgb, var(--accent-invest) 45%, transparent); }
.ai-badge.warn { color: var(--accent-renovation); border-color: color-mix(in srgb, var(--accent-renovation) 45%, transparent); font-weight: 600; }
.ai-badge.err { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.ai-note { flex-basis: 100%; font-size: 11px; color: var(--accent-renovation); }
.classify-btn optgroup { font-style: normal; color: var(--text-muted); }
.classify-btn option { color: var(--text-primary); background: var(--surface); }
table.txs tr.is-outlier td { background: color-mix(in srgb, var(--accent-renovation) 8%, transparent); }
table.txs td.outlier-reason { color: var(--text-secondary); font-size: 12.5px; max-width: 340px; }
.outliers-h { font-size: 13px; font-weight: 600; color: var(--text-secondary); margin: 14px 0 4px; }
.outliers-h:first-child { margin-top: 4px; }

/* Add-rule modal */
dialog#rule-modal {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text-primary);
  padding: 22px 24px;
  width: min(620px, 94vw);
}
dialog#rule-modal::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog#rule-modal h2 { font-size: 16px; margin: 0 0 4px; }
dialog#rule-modal .sub { font-size: 12.5px; color: var(--text-muted); margin: 0 0 10px; }
dialog#rule-modal label { display: block; font-size: 13px; color: var(--text-secondary); margin: 10px 0 4px; }
dialog#rule-modal input, dialog#rule-modal select {
  width: 100%;
  font: inherit;
  font-size: 13.5px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-primary);
}
dialog#rule-modal input:focus, dialog#rule-modal select:focus { outline: 2px solid var(--accent); border-color: transparent; }
.modal-actions { display: flex; gap: 8px; margin-top: 14px; }

/* Rule match previews (modal + settings) */
.rule-preview { max-height: 260px; overflow: auto; }
.rule-preview table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.rule-preview td { padding: 4px 8px; border-bottom: 1px solid var(--grid); color: var(--text-secondary); }
.rule-preview td:first-child { white-space: nowrap; color: var(--text-muted); }
.rule-preview td:last-child { white-space: nowrap; }
.preview-summary { font-size: 12.5px; color: var(--text-secondary); margin: 8px 0 6px; }
.tx-similar { margin-bottom: 10px; }
.tx-similar .preview-summary:first-child { font-weight: 600; color: var(--text); }

/* Settings page */
#settings[hidden] { display: none; }
.settings-actions { display: flex; gap: 8px; align-items: center; margin: 10px 0 4px; flex-wrap: wrap; }
.settings-currency { font-size: 13px; color: var(--text-secondary); display: block; margin-bottom: 4px; }
.settings-currency input {
  width: 56px;
  font: inherit;
  padding: 5px 8px;
  margin-left: 6px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-primary);
}
.rule-entry {
  border: 1px solid var(--grid);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 10px;
}
.rule-entry-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.rule-entry-head .rule-name {
  flex: 1;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 6px 9px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text-primary);
}
textarea.rule-patterns {
  display: block;
  width: 100%;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.6;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
  color: var(--text-primary);
  resize: vertical;
}
textarea.rule-patterns:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* Login */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
  width: min(360px, 92vw);
}
.login-card h1 { font-size: 19px; margin: 0 0 18px; }
.login-card label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin: 12px 0 4px;
}
.login-card input {
  width: 100%;
  font: inherit;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--baseline);
  background: var(--page);
  color: var(--text-primary);
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login-card button { width: 100%; margin-top: 18px; padding: 10px; font-size: 14px; }
.login-card .google-signin {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  margin-top: 8px;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  color: var(--text-primary);
}
.login-card .google-signin .g {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  color: #4285f4;
  font-weight: 700;
  border: 1px solid var(--border);
}

/* Onboarding reuses the login card, a touch wider for the longer form. */
.onboarding-card { width: min(440px, 92vw); }
.onboarding-intro { font-size: 13px; color: var(--text-muted); margin: 0 0 8px; line-height: 1.5; }
.login-card .field-hint { font-size: 12px; color: var(--text-muted); margin: 4px 0 0; line-height: 1.4; }
.login-card .field-hint code { font-size: 11.5px; }
.login-card .optional { color: var(--text-muted); font-weight: 400; }

/* A field label with its "?" help toggle sitting next to it. */
.login-card .label-row {
  display: flex;
  align-items: baseline;
  gap: 7px;
  margin: 12px 0 4px;
}
.login-card .label-row label { margin: 0; }

/* The "?" itself: a circle small enough to read as an aside, not a control the
   form needs. .help-icon-inline is the same circle rendered as static text, so
   the intro can point at it. The selector has to out-specify
   `.login-card button`, which stretches form buttons to the full width. */
.login-card .help-icon,
.help-icon-inline {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 17px;
  height: 17px;
  margin: 0;
  padding: 0;
  border: 1px solid var(--baseline);
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1;
}
.login-card .help-icon { cursor: pointer; }
.login-card .help-icon:hover { color: var(--text-primary); border-color: var(--text-muted); }
.login-card .help-icon:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.login-card .help-icon[aria-expanded="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.help-panel {
  margin: 8px 0 2px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--page);
  font-size: 12.5px;
  color: var(--text-secondary);
  line-height: 1.5;
}
.help-panel[hidden] { display: none; }
.help-panel p { margin: 0 0 8px; }
.help-panel p:last-child { margin-bottom: 0; }
.help-panel strong { color: var(--text-primary); font-weight: 600; }
.help-steps-title { color: var(--text-primary); font-weight: 600; }
.help-steps {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

/* About */
.about-card { width: min(480px, 92vw); }
.about-card p { font-size: 13.5px; color: var(--text-secondary); margin: 0 0 12px; line-height: 1.55; }
.about-card .field-hint { margin: 0 0 4px; }
.about-card .about-cta {
  display: block;
  margin-top: 18px;
  padding: 10px;
  text-align: center;
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.card-footer {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

/* Onboarding step two: the proposed category list. */
.suggest-title { font-size: 15px; margin: 0 0 4px; }
.suggest-list { margin: 10px 0; display: flex; flex-direction: column; gap: 2px; }
.suggest-row {
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: baseline;
  gap: 8px;
  padding: 5px 0;
  font-size: 13px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.suggest-row:last-child { border-bottom: none; }
.suggest-name { font-weight: 550; }
.suggest-meta { color: var(--text-muted); font-size: 11.5px; }
#suggest-skip { width: 100%; margin-top: 8px; }
.login-card input[type="file"] { padding: 8px; font-size: 13px; }
.rule-flags { display: flex; gap: 14px; flex-wrap: wrap; margin: 6px 0 8px; font-size: 12.5px; color: var(--text-secondary); }
.rule-flags label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.rule-flags .flag-warning:not(:empty) { color: var(--warning, #b45309); }
