/* Palette: validated 8-slot categorical set (light / dark steps) from the data-viz reference palette. */
:root {
  color-scheme: light;
  --bg: #fcfcfb; --card: #ffffff; --fg: #0b0b0b; --muted: #52514e; --line: #e4e3df;
  --accent: #2a78d6; --neg: #b3261e; --pos: #1a7f37; --warn: #8a5a00;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #1a1a19; --card: #222221; --fg: #ffffff; --muted: #c3c2b7; --line: #34342f;
    --accent: #3987e5; --neg: #f2918c; --pos: #6fcf8a; --warn: #e0b04a;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, -apple-system, sans-serif; }
h1 { font-size: 1.05rem; margin: 0; }
h2 { font-size: 1rem; margin: 0 0 .6rem; }
.muted { color: var(--muted); } .neg { color: var(--neg); } .pos { color: var(--pos); } .note { font-size: .85rem; margin: .25rem 0 0; }
.fatal { padding: 1rem; color: var(--neg); }

header { display: flex; gap: 1rem; align-items: center; padding: .7rem 1rem; border-bottom: 1px solid var(--line); flex-wrap: wrap; position: sticky; top: 0; background: var(--bg); z-index: 5; }
header nav { display: flex; gap: .25rem; flex: 1; flex-wrap: wrap; }
header a { color: var(--accent); font-size: .9rem; }
#who { color: var(--muted); font-size: .85rem; }
nav button { background: none; border: 0; padding: .4rem .75rem; border-radius: 6px; color: var(--fg); cursor: pointer; font: inherit; }
nav button:hover { background: var(--line); }
nav button.active { background: var(--line); font-weight: 600; }
main { padding: 1rem; max-width: 74rem; margin: 0 auto; }

button, input, select, textarea { font: inherit; color: var(--fg); }
input, select, textarea { background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: .4rem .55rem; }
button { background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: .4rem .8rem; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: .85rem; }
button.link.danger { color: var(--neg); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.bar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.bar p { margin: 0; } .bar label { display: flex; gap: .5rem; align-items: center; color: var(--muted); font-size: .9rem; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); gap: .75rem; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: .8rem 1rem; }
.tile-label { color: var(--muted); font-size: .8rem; }
.tile-value { font-size: 1.6rem; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.2; }
.tile-sub { font-size: .78rem; }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr)); gap: .75rem; margin-top: .75rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; margin-top: .75rem; }
.grid2 .card { margin-top: 0; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .45rem .5rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: .8rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.pending td { color: var(--muted); } tr.disabled td { opacity: .5; }
table.compact td { padding: .2rem .5rem; font-size: .85rem; border: 0; }
.badge { margin-left: .4rem; font-size: .7rem; border: 1px solid var(--line); border-radius: 999px; padding: 0 .4rem; color: var(--warn); }
td select { max-width: 11rem; } td.saving select { opacity: .5; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .75rem; }
.filters input[type=search] { flex: 1; min-width: 12rem; }
.pager { display: flex; gap: 1rem; align-items: center; justify-content: center; padding: 1rem 0; }

.rule-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.rule-form input[type=text] { flex: 1; min-width: 10rem; } .rule-form input[type=number] { width: 5.5rem; }
#r-result, #apply-result { margin-top: .6rem; } .apply { margin-top: 1rem; display: grid; gap: .5rem; justify-items: start; }

.chart { margin: 0; } .md .chart, .ask-body .chart { margin: .75rem 0; } .chart figcaption { font-weight: 600; margin-bottom: .4rem; }
.chart-box { position: relative; width: 100%; } .chart .link { margin-top: .35rem; }
.chart-table { margin-top: .5rem; font-size: .85rem; }

.ask-head { display: flex; justify-content: space-between; align-items: center; }
.transcript { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 1rem; min-height: 22rem; max-height: 62vh; overflow-y: auto; display: flex; flex-direction: column; gap: .75rem; }
.ask-empty { margin: auto; text-align: center; max-width: 40rem; }
.chips { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; margin-top: .75rem; }
.chip { border-radius: 999px; font-size: .85rem; }
.msg { max-width: 92%; padding: .6rem .85rem; border-radius: 12px; border: 1px solid var(--line); }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-color: var(--accent); } .msg.user p { margin: 0; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--bg); width: min(92%, 52rem); }
.msg.error { border-color: var(--neg); }
.queries { font-size: .8rem; color: var(--muted); margin-bottom: .4rem; } .queries pre { margin: .3rem 0; white-space: pre-wrap; }
.ask-form { display: flex; gap: .5rem; align-items: flex-start; margin-top: .75rem; }
.ask-form textarea { flex: 1; resize: vertical; }

.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: .5rem 0; } .md h3, .md h4, .md h5, .md h6 { margin: .8rem 0 .3rem; font-size: 1rem; }
.md ul, .md ol { margin: .5rem 0; padding-left: 1.3rem; }
.md pre { background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: .5rem .7rem; overflow-x: auto; }
.md code { font: .85em ui-monospace, Menlo, monospace; }
.md-table { overflow-x: auto; margin: .5rem 0; } .md-table table { font-size: .9rem; }

.toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: .5rem 1rem; border-radius: 8px; z-index: 20; max-width: 90vw; }
.toast.error { background: var(--neg); color: #fff; }

@media (max-width: 640px) { .col-account { display: none; } .grid2 { grid-template-columns: 1fr; } main { padding: .75rem; } }

.org-select { font-weight: 600; }
.badge.book { color: var(--accent); }
.notice { border-color: var(--accent); }
.key-status { margin: .25rem 0 .75rem; }
#key-input { flex: 1; min-width: 14rem; font-family: ui-monospace, Menlo, monospace; }
#key-msg { margin: .6rem 0 0; min-height: 1.4em; }
.steps { padding-left: 1.2rem; display: grid; gap: .5rem; }
.steps a, .card a { color: var(--accent); }

[hidden] { display: none !important; }

.ask-input-wrap { flex: 1; position: relative; }
.ask-input-wrap textarea { width: 100%; resize: vertical; }
.palette { position: absolute; bottom: 100%; left: 0; right: 0; background: var(--card); border: 1px solid var(--line); border-radius: 8px; margin-bottom: .35rem; max-height: 16rem; overflow-y: auto; z-index: 6; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
.palette-item { display: flex; gap: .75rem; justify-content: space-between; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: none; padding: .5rem .75rem; }
.palette-item:last-child { border-bottom: 0; }
.palette-item:hover { background: var(--line); }
.badge.via { margin: .5rem 0 0; display: inline-block; color: var(--muted); }
.takeaway { margin: .6rem 0 0; padding: .5rem .75rem; border-left: 3px solid var(--accent); background: var(--bg); }

/* ---- Transactions (phase 1) ---- */
.banner { padding: .6rem .9rem; border-radius: 8px; border: 1px solid var(--line); margin-bottom: .75rem; }
.banner.warn { border-color: var(--warn); color: var(--warn); }
.tx-toolbar { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .5rem; }
.tx-toolbar input[type=search] { flex: 1; min-width: 12rem; }
label.inline { display: inline-flex; gap: .35rem; align-items: center; color: var(--muted); font-size: .85rem; }
label.inline select, label.inline input { color: var(--fg); }
.filter-panel { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; padding: .75rem; border: 1px solid var(--line); border-radius: 10px; background: var(--card); margin-bottom: .5rem; }
.filter-panel input[type=number] { width: 6.5rem; }
.presets { display: inline-flex; gap: .6rem; }
.tx-summary { margin: .25rem 0 .5rem; font-size: .85rem; }
.bulkbar { position: sticky; top: 3.4rem; z-index: 4; display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; padding: .6rem .75rem; margin-bottom: .5rem; background: var(--card); border: 1px solid var(--accent); border-radius: 10px; }
.sorthead { background: none; border: 0; padding: 0; color: inherit; font: inherit; cursor: pointer; text-align: inherit; }
.sorthead:hover { color: var(--accent); }
th.num .sorthead { width: 100%; text-align: right; }
.col-select { width: 2rem; }
tr.selected td { background: color-mix(in srgb, var(--accent) 12%, transparent); }
tr.focused td { outline: 1px solid var(--accent); }
tr.ignored td { opacity: .55; text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--muted) 60%, transparent); }
tr[data-id] { cursor: pointer; }
.badge { margin-left: .35rem; }
.badge.warn { color: var(--warn); }
.empty { padding: 1.5rem; text-align: center; }
.tx-groups details.group { border: 1px solid var(--line); border-radius: 10px; margin-bottom: .5rem; background: var(--card); }
.tx-groups summary { display: grid; grid-template-columns: 1fr auto 8rem 8rem; gap: .75rem; padding: .6rem .85rem; cursor: pointer; align-items: center; }
.group-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-body { padding: 0 .5rem .5rem; overflow-x: auto; }
.group-table td { font-size: .9rem; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(26rem, 100vw); background: var(--card); border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(0,0,0,.25); padding: 1rem 1.1rem 2rem; overflow-y: auto; z-index: 10; }
.drawer h2 { font-size: 1.05rem; margin: 0; } .drawer h3 { font-size: .85rem; color: var(--muted); margin: 1.1rem 0 .35rem; text-transform: uppercase; letter-spacing: .04em; }
.drawer-head { display: flex; justify-content: space-between; gap: 1rem; align-items: baseline; }
.drawer-amount { font-size: 1.6rem; font-weight: 650; margin: .25rem 0; font-variant-numeric: tabular-nums; }
.drawer input[type=text], .drawer select, .drawer textarea { width: 100%; margin-bottom: .4rem; }
.drawer .radio { display: flex; gap: .4rem; align-items: flex-start; font-size: .9rem; margin: .25rem 0; }
.drawer button.primary { margin-top: .35rem; }
.small { font-size: .8rem; word-break: break-word; }
.history { padding-left: 1rem; font-size: .8rem; }
.bank { margin-top: .75rem; }

/* ---- mobile: cards instead of a table, bottom navigation, bottom-sheet drawer ---- */
@media (max-width: 700px) {
  body { padding-bottom: 3.6rem; }
  header { position: static; }
  header nav { position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; background: var(--card); border-top: 1px solid var(--line); justify-content: space-around; flex-wrap: nowrap; padding: .3rem .25rem calc(.3rem + env(safe-area-inset-bottom)); }
  header nav button { flex: 1; padding: .55rem .1rem; font-size: .8rem; }
  #txns thead { display: none; }
  #txns, #txns tbody { display: block; }
  #txns tr[data-id] { display: grid; grid-template-columns: 1.6rem minmax(0, 1fr) auto; gap: .15rem .6rem; padding: .65rem .5rem; border-bottom: 1px solid var(--line); }
  #txns td { display: block; border: 0; padding: 0; min-width: 0; }
  #txns select { min-width: 0; }
  html, body { overflow-x: hidden; }
  main, .tx-toolbar, .filter-panel, .bulkbar { max-width: 100vw; min-width: 0; }
  .tx-toolbar input[type=search] { flex: 1 1 100%; min-width: 0; }
  .tx-toolbar .inline { min-width: 0; }
  header { gap: .5rem; } #who { max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #txns .col-select { grid-row: 1 / span 3; align-self: center; }
  #txns .col-merchant { grid-column: 2; font-weight: 600; }
  #txns .col-amount { grid-column: 3; grid-row: 1; text-align: right; }
  #txns .col-date { grid-column: 2; grid-row: 2; color: var(--muted); font-size: .8rem; }
  #txns .col-category { grid-column: 2 / span 2; grid-row: 3; }
  #txns .col-category select { max-width: 100%; width: 100%; }
  #txns .col-account, #txns .col-actions { display: none; }
  .tx-toolbar label.inline { flex: 1 1 40%; } .tx-toolbar label.inline select { flex: 1; min-width: 0; }
  .bulkbar { top: 0; }
  .drawer { top: auto; bottom: 0; left: 0; right: 0; width: 100%; max-height: 85vh; border-left: 0; border-top: 1px solid var(--line); border-radius: 14px 14px 0 0; padding-bottom: 5rem; }
  .tx-groups summary { grid-template-columns: 1fr auto; } .tx-groups summary .num { grid-column: 2; }
  .filter-panel select, .filter-panel input { flex: 1 1 45%; min-width: 0; }
  main { padding-bottom: 1rem; }
}

/* ---- brand ---- */
h1.brand { display: flex; align-items: center; line-height: 0; }
h1.brand img { width: 40px; height: 40px; border-radius: 9px; display: block; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
