/* ------------------------------------------------------------------ tokens */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-raised: #ffffff;
  --surface-sunken: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6e69;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --series-1: #2a78d6;
  --series-1-wash: rgba(42, 120, 214, 0.10);
  --accent: #256abf;
  --accent-ink: #ffffff;
  --accent-soft: #e6effb;
  --pos: #006300;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn-soft: #fff6dc;
  --warn-border: #f0d48a;
  --info-soft: #eaf2fc;
  --flash: #fff1bf;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.08);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-raised: #232322;
    --surface-sunken: #141413;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #9a9890;
    --grid: #2c2c2a;
    --axis: #44443f;
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.18);
    --series-1: #3987e5;
    --series-1-wash: rgba(57, 135, 229, 0.14);
    --accent: #3987e5;
    --accent-ink: #0b0b0b;
    --accent-soft: #1b2a3d;
    --pos: #2fbf4a;
    --danger: #f07a72;
    --danger-soft: #3a1d1b;
    --warn-soft: #33290f;
    --warn-border: #6b5518;
    --info-soft: #172433;
    --flash: #4a3f12;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-raised: #232322;
  --surface-sunken: #141413;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #9a9890;
  --grid: #2c2c2a;
  --axis: #44443f;
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.18);
  --series-1: #3987e5;
  --series-1-wash: rgba(57, 135, 229, 0.14);
  --accent: #3987e5;
  --accent-ink: #0b0b0b;
  --accent-soft: #1b2a3d;
  --pos: #2fbf4a;
  --danger: #f07a72;
  --danger-soft: #3a1d1b;
  --warn-soft: #33290f;
  --warn-border: #6b5518;
  --info-soft: #172433;
  --flash: #4a3f12;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.5);
}

/* ------------------------------------------------------------------ base */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.45 var(--font);
}

h1, h2, h3, p { margin: 0; }

button, input, select { font: inherit; color: inherit; }

code {
  font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-sunken);
  padding: 1px 5px;
  border-radius: 4px;
}

[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------------ buttons & fields */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-raised);
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-sunken); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-sunken); color: var(--danger); }

.field { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field input, .field select, .inline-form input, .list-tools input[type="search"] {
  width: 100%;
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-raised);
  font-size: 15px;
  font-weight: 400;
  color: var(--ink);
}
.optional { font-weight: 400; color: var(--muted); }

.stack { display: grid; gap: 14px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.inline { display: inline; margin: 0; }

.inline-form { display: flex; gap: 8px; }

.segmented {
  display: flex;
  gap: 0;
  margin: 0;
  padding: 0;
  border: 0;
}
.segmented legend { font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 6px; padding: 0; }
.segmented label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  border: 1px solid var(--border-strong);
  background: var(--surface-raised);
  cursor: pointer;
  font-size: 14px;
}
.segmented label:first-of-type { border-radius: 8px 0 0 8px; }
.segmented label:last-of-type { border-radius: 0 8px 8px 0; border-left: 0; }
.segmented label:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); font-weight: 600; }
.segmented input { accent-color: var(--accent); }

.form-error { color: var(--danger); font-size: 14px; }

/* ------------------------------------------------------------------ layout */

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
}
.brand { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.topbar-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.sync-status { font-size: 13px; color: var(--muted); margin-right: 4px; }

.page {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px 16px 64px;
  display: grid;
  gap: 20px;
}

/* ------------------------------------------------------------------ notices & toasts */

.notice {
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 14px;
}
.notice ul { margin: 6px 0 0; padding-left: 20px; }
.notice-info { background: var(--info-soft); }
.notice-warn { background: var(--warn-soft); border-color: var(--warn-border); }
.notice-error { background: var(--danger-soft); border-color: var(--danger); }

.toast-region {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 50;
  display: grid;
  gap: 8px;
  width: min(480px, calc(100vw - 32px));
}
.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--ink);
  color: var(--page);
  box-shadow: var(--shadow);
  font-size: 14px;
}
.toast-error { background: var(--danger); color: #fff; }

.empty-state {
  padding: 40px 20px;
  text-align: center;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  color: var(--ink-2);
}
.empty-state h2 { color: var(--ink); font-size: 18px; margin-bottom: 6px; }

/* ------------------------------------------------------------------ bank groups */

.banks { display: grid; gap: 12px; min-width: 0; }
.bank { min-width: 0; }

.bank-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.bank-head::-webkit-details-marker { display: none; }
.bank-head:hover { background: var(--surface-sunken); }

.bank-chevron {
  flex: none;
  width: 9px;
  height: 9px;
  margin: 0 3px 0 2px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
}
.bank[open] .bank-chevron { transform: rotate(45deg) translate(-2px, -2px); }

.bank-title { flex: 1; display: grid; min-width: 0; }
.bank-name { font-size: 17px; font-weight: 650; }
.bank-sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.bank-body { display: grid; gap: 12px; padding-top: 12px; }

@media (prefers-reduced-motion: reduce) { .bank-chevron { transition: none; } }

/* ------------------------------------------------------------------ account card */

.account {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 18px 6px;
  min-width: 0;
}

.account-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
  gap: 8px 16px;
  margin-bottom: 12px;
}
.account-title h3 { margin: 0; font-size: 18px; font-weight: 600; }
.account-balance { display: grid; justify-items: end; text-align: right; }
.balance-label { font-size: 12px; color: var(--muted); }
.balance-value { font-size: 24px; font-weight: 650; letter-spacing: -0.01em; }
.balance-meta { font-size: 12px; color: var(--muted); }

.chart-box { position: relative; height: 280px; margin: 0 -4px 8px; }
.chart-empty { padding: 24px 0; color: var(--muted); font-size: 14px; }

.list-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}
.list-tools input[type="search"] { flex: 1 1 220px; width: auto; min-height: 34px; font-size: 14px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.check input { accent-color: var(--accent); }
.list-count { font-size: 13px; color: var(--muted); margin-left: auto; }

/* ------------------------------------------------------------------ transaction table */

.table-scroll {
  max-height: 460px;
  overflow: auto;
  margin: 0 -18px;
  border-top: 1px solid var(--border);
  overscroll-behavior: contain;
}

.txns { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 14px; }
.txns thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  box-shadow: inset 0 -1px 0 var(--border);
  padding: 8px 10px;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.txns td { padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.txns tr:last-child td { border-bottom: 0; }
.txns tbody tr:hover td { background: var(--surface-sunken); }
.txns th:first-child, .txns td:first-child { padding-left: 18px; }
.txns th:last-child, .txns td:last-child { padding-right: 18px; }

.col-date { width: 118px; color: var(--ink-2); white-space: nowrap; }
.col-amount { width: 128px; }
.col-cat { width: 180px; text-align: right; }
th.col-cat { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.num { font-weight: 500; }
td.pos { color: var(--pos); }

.vendor-line { display: flex; align-items: center; gap: 6px; min-width: 0; }
.vendor-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sub { display: block; font-size: 12px; color: var(--muted); }
.mobile-only { display: none; }

.tag {
  flex: none;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--ink-2);
  font-size: 11px;
  font-weight: 600;
}
.del-txn, .hide-txn { width: 22px; height: 22px; font-size: 16px; }
.tag-warn { background: var(--warn-soft); color: var(--ink); border: 1px solid var(--warn-border); }

/* The hide button appears on hover/focus, and always on flagged duplicates and touch screens. */
.hide-txn { opacity: 0; }
.txns tr:hover .hide-txn, .hide-txn:focus-visible, .txns tr.is-dup .hide-txn { opacity: 1; }
@media (hover: none) { .hide-txn { opacity: 1; } }

.txns tr.is-hidden td { color: var(--muted); }
.txns tr.is-hidden .vendor-name, .txns tr.is-hidden .col-amount { text-decoration: line-through; }

.link-btn {
  flex: none;
  padding: 0 2px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }

.cat-btn {
  max-width: 100%;
  min-height: 28px;
  padding: 2px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-raised);
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.cat-btn:hover, .cat-btn[aria-expanded="true"] { border-color: var(--accent); }
.cat-btn.is-empty { border-style: dashed; background: transparent; color: var(--muted); font-weight: 400; }

.no-match td { padding: 20px 18px; color: var(--muted); text-align: center; }

@keyframes row-flash { from { background: var(--flash); } to { background: transparent; } }
.txns tr.flash td { animation: row-flash 2.2s ease-out; }

/* ------------------------------------------------------------------ category menu */

.menu {
  position: absolute;
  z-index: 40;
  width: 230px;
  padding: 6px;
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu-list { max-height: 260px; overflow-y: auto; display: grid; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 32px;
  padding: 4px 10px 4px 28px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
  font-size: 14px;
  cursor: pointer;
  position: relative;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-sunken); outline: none; }
.menu-item[aria-checked="true"] { font-weight: 600; }
.menu-item[aria-checked="true"]::before { content: "✓"; position: absolute; left: 10px; color: var(--accent); }
.menu-item-muted { color: var(--muted); border-top: 1px solid var(--border); border-radius: 0 0 6px 6px; margin-top: 4px; }
.menu-empty { padding: 8px 10px; font-size: 13px; color: var(--muted); }
.menu-new { padding: 6px 4px 2px; border-top: 1px solid var(--border); margin-top: 4px; }
.menu-new input {
  width: 100%;
  min-height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
  font-size: 14px;
}

/* ------------------------------------------------------------------ dialogs */

dialog {
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-raised);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.stack > .dialog-head { margin-bottom: 0; }
.dialog-head h2 { font-size: 17px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.category-list { list-style: none; margin: 14px 0 0; padding: 0; max-height: 50vh; overflow-y: auto; }
.category-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 2px;
  border-top: 1px solid var(--border);
}
.category-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.category-count { font-size: 13px; color: var(--muted); white-space: nowrap; }
.category-empty { color: var(--muted); font-size: 14px; justify-content: center; }

/* ------------------------------------------------------------------ login */

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  padding: 28px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: grid;
  gap: 18px;
}

/* ------------------------------------------------------------------ small screens */

@media (max-width: 640px) {
  .topbar-actions { width: 100%; }
  .topbar-actions #sync-btn { flex: 1 1 100%; }
  .sync-status { width: 100%; }
  .account { padding: 14px 14px 4px; border-radius: 12px; }
  .bank-head { padding: 12px 14px; gap: 10px; }
  .table-scroll { margin: 0 -14px; }
  .chart-box { height: 220px; }
  .balance-value { font-size: 20px; }
  .list-count { margin-left: 0; }
  .add-txn-btn { width: 100%; }
  .col-date { display: none; }
  .mobile-only { display: block; }
  .col-amount { width: 96px; }
  .col-cat { width: 118px; }
  .txns th:first-child, .txns td:first-child { padding-left: 14px; }
  .txns th:last-child, .txns td:last-child { padding-right: 14px; }
  .txns td, .txns th { padding-left: 6px; padding-right: 6px; }
  .txns .col-vendor { padding-left: 14px; }
}
