:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #1c5cab;
  --accent-ink: #ffffff;
  --series-income: #2a78d6;
  --series-expense: #eb6834;
  --bar: #2a78d6;
  --good: #006300;
  --bad: #d03b3b;
  --warn-bg: #fff4dc;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --series-income: #3987e5;
    --series-expense: #d95926;
    --bar: #3987e5;
    --good: #0ca30c;
    --bad: #e66767;
    --warn-bg: #3a2e12;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --series-income: #3987e5;
  --series-expense: #d95926;
  --bar: #3987e5;
  --good: #0ca30c;
  --bad: #e66767;
  --warn-bg: #3a2e12;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px));
}
h1 { font-size: 1.5rem; margin: 0.25rem 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 0.75rem; }
a { color: var(--accent); }
.muted { color: var(--ink-2); }
.small { font-size: 0.85rem; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: center;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--accent); color: var(--accent-ink);
}
.brand { font-weight: 700; font-size: 1.05rem; }
.top-total { font-weight: 600; font-variant-numeric: tabular-nums; }

main { max-width: 860px; margin: 0 auto; padding: 16px; }

.row { display: flex; align-items: center; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.gap { gap: 10px; }
.wrap { flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 639px) { .tiles > .tile:last-child:nth-child(odd) { grid-column: 1 / -1; } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
a.tile-link { color: inherit; text-decoration: none; }
.deposit-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0 12px; margin-top: 12px; }
.deposit-form .row { grid-column: 1 / -1; }
.suggest { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.tile-label { color: var(--ink-2); font-size: 0.82rem; }
.tile-value { font-size: 1.3rem; font-weight: 650; margin-top: 2px; overflow-wrap: anywhere; }
.tile-sub { color: var(--muted); font-size: 0.78rem; margin-top: 2px; }
.up-bad { color: var(--bad); }
.down-good { color: var(--good); }

input, select, button { font: inherit; color: inherit; }
input[type="text"], input[type="number"], input[type="date"], input[type="search"], select {
  background: var(--surface); border: 1px solid var(--axis); border-radius: 10px;
  padding: 9px 11px; min-height: 42px; max-width: 100%;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
  padding: 10px 16px; min-height: 42px; font-weight: 600; cursor: pointer;
}
.btn.secondary { background: var(--surface-2); color: var(--ink); }
.btn.danger { background: var(--bad); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; color: var(--ink-2); font-size: 0.9rem; }
.field > input, .field > select { color: var(--ink); font-size: 1rem; }
.check { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; }

.file-drop {
  display: flex; align-items: center; justify-content: center; text-align: center;
  border: 2px dashed var(--axis); border-radius: var(--radius); padding: 28px 16px;
  cursor: pointer; color: var(--accent); font-weight: 600; margin: 8px 0;
}
.file-drop input { display: none; }

.mapping { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; margin-bottom: 12px; }
.table-wrap { overflow-x: auto; margin-bottom: 12px; border: 1px solid var(--border); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--grid); text-align: left; white-space: nowrap; }
th { background: var(--surface-2); }

.filters { display: grid; grid-template-columns: 1fr; gap: 8px; margin-bottom: 10px; }
@media (min-width: 640px) { .filters { grid-template-columns: 2fr 1fr 1fr; } }
#addTxBtn { margin-bottom: 10px; }

.tx-list { list-style: none; padding: 0; margin: 8px 0 0; }
.tx-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 4px; border-bottom: 1px solid var(--grid); cursor: pointer;
}
.tx-list li:hover { background: var(--surface-2); }
.tx-main { min-width: 0; }
.tx-desc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta { color: var(--muted); font-size: 0.8rem; }
.tx-amt { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tx-amt.pos { color: var(--good); }
.tx-day { background: none !important; cursor: default !important; color: var(--ink-2); font-size: 0.8rem; font-weight: 600; padding-top: 16px !important; }

.chart { width: 100%; position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .empty { color: var(--muted); padding: 8px 0; }
.legend { display: flex; gap: 16px; color: var(--ink-2); font-size: 0.85rem; margin-bottom: 8px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.s-income { background: var(--series-income); }
.s-expense { background: var(--series-expense); }
.chart-tip {
  position: absolute; pointer-events: none; background: var(--ink); color: var(--page);
  padding: 6px 9px; border-radius: 8px; font-size: 0.8rem; white-space: nowrap; z-index: 5;
  transform: translate(-50%, calc(-100% - 8px));
}

.cat-row { display: grid; grid-template-columns: minmax(90px, 34%) 1fr 92px; gap: 10px; align-items: center; padding: 5px 0; }
.cat-name { font-size: 0.88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-track { height: 10px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.cat-fill { height: 100%; background: var(--bar); border-radius: 4px; }
.cat-val { text-align: right; font-size: 0.85rem; font-variant-numeric: tabular-nums; color: var(--ink-2); white-space: nowrap; }

.insights { list-style: none; padding: 0; margin: 0; }
.insights li { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); margin-bottom: 8px; }
.insights li.warn { background: var(--warn-bg); }
.insights li b { font-weight: 650; }

.top-list { margin: 0; padding-left: 20px; }
.top-list li { padding: 4px 0; }
.top-list .v { float: right; font-variant-numeric: tabular-nums; color: var(--ink-2); }

.rules { list-style: none; padding: 0; margin: 0; }
.rules li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--grid); }
.rules button { background: none; border: 0; color: var(--bad); cursor: pointer; font-size: 1.1rem; }

.danger-zone { border-color: color-mix(in srgb, var(--bad) 40%, transparent); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 0.72rem; padding: 4px 0;
}
.tabbar a .ic { font-size: 1.2rem; line-height: 1; }
.tabbar a.active { color: var(--accent); font-weight: 600; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 20px;
  background: var(--surface); color: var(--ink); width: min(420px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog input[type="number"] { width: 100%; margin: 8px 0 14px; font-size: 1.2rem; }
dialog .field > input, dialog .field > select { width: 100%; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: var(--ink); color: var(--page); padding: 10px 16px; border-radius: 10px; z-index: 20;
  max-width: calc(100vw - 32px); font-size: 0.9rem;
}
