:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface-1: #ffffff;
  --surface-2: #efefef;
  --border: #e4e4e2;
  --text-primary: #191919;
  --text-secondary: #666666;
  --series-1: #257de0;
  --series-1-soft: rgba(37, 125, 224, 0.12);
  --prev: #9c9c9c;
  --good: #006c10;
  --bad: #c42b2c;
  --warning-bg: #fff4da;
  --critical-bg: #fde8e8;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121212;
    --surface-1: #191919;
    --surface-2: #262626;
    --border: #2c2c2c;
    --text-primary: #efefef;
    --text-secondary: #9c9c9c;
    --series-1: #338ced;
    --series-1-soft: rgba(51, 140, 237, 0.16);
    --prev: #6b6b6b;
    --good: #3cc153;
    --bad: #eb6263;
    --warning-bg: #3a2f12;
    --critical-bg: #3d1a1a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text-primary); }
body { min-height: 100dvh; padding-top: var(--safe-t); }
[hidden] { display: none !important; }
h1 { font-size: 28px; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 15px; margin: 0 0 10px; font-weight: 600; }
p { margin: 0; }
.muted { color: var(--text-secondary); }
.small { font-size: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
code { font-size: 13px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.num, .hero-value, .amount, .bar-val, .stat b { font-variant-numeric: tabular-nums; }

/* ---------- forms ---------- */
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-secondary); margin-bottom: 12px; }
input, select, button { font: inherit; color: inherit; }
input, select {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-1); font-size: 16px; color: var(--text-primary);
}
textarea {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border); resize: vertical;
  background: var(--surface-1); font: inherit; font-size: 16px; color: var(--text-primary);
}
textarea:focus, input:focus, select:focus { outline: 2px solid var(--series-1); outline-offset: 0; }
button { cursor: pointer; border: 0; background: var(--surface-2); padding: 11px 16px; border-radius: 10px; }
button.primary { background: var(--series-1); color: #fff; font-weight: 600; }
button.primary:disabled { opacity: 0.6; }
button.link { background: none; color: var(--series-1); padding: 6px; }
.icon-btn { padding: 6px 12px; font-size: 14px; }
.row { display: flex; gap: 10px; align-items: flex-end; }
.grow { flex: 1; }
.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text-primary); font-size: 15px; }
.check input { width: 20px; height: 20px; margin: 0; }
.hint { font-size: 12px; color: var(--text-secondary); margin: -6px 0 12px; }
.error { color: var(--bad); font-size: 14px; min-height: 1em; margin-bottom: 8px; }

/* ---------- gates ---------- */
.gate { max-width: 420px; margin: 0 auto; padding: 32px 20px; }
.gate h1 { margin-bottom: 8px; }
.gate > p { margin-bottom: 20px; }
#unlock { text-align: center; padding-top: 18vh; }
#unlock form { margin: 24px 0 12px; }
#unlock input { text-align: center; margin-bottom: 8px; }
#unlock button.primary { width: 100%; }
#setup button.primary { width: 100%; }
.lock-icon { width: 56px; height: 56px; margin: 0 auto 16px; border-radius: 16px; background: var(--series-1);
  mask: url("icons/lock.svg") center / 28px no-repeat; -webkit-mask: url("icons/lock.svg") center / 28px no-repeat; }

/* ---------- app shell ---------- */
#app { max-width: 560px; margin: 0 auto; padding: 8px 16px calc(84px + var(--safe-b)); }
.topbar { display: flex; justify-content: space-between; align-items: flex-start; padding: 8px 0 12px; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  background: color-mix(in srgb, var(--surface-1) 88%, transparent); backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px); border-top: 1px solid var(--border); padding: 6px 4px calc(6px + var(--safe-b));
}
.tabbar button { flex: 1; max-width: 112px; background: none; font-size: 12px; padding: 10px 2px; color: var(--text-secondary); }
.tabbar button.on { color: var(--series-1); font-weight: 600; }
.panel { background: var(--surface-1); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; border: 1px solid var(--border); }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; }

.banner { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 10px; margin-bottom: 8px; font-size: 14px; }
.banner.warning { background: var(--warning-bg); }
.banner.error { background: var(--critical-bg); }
.banner b { white-space: nowrap; }

/* ---------- home ---------- */
.hero { padding: 8px 0 20px; }
.hero-value { font-size: 40px; font-weight: 700; letter-spacing: -0.03em; line-height: 1.1; margin: 2px 0 4px; }
.delta { font-size: 15px; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.card { min-width: 0; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.card-main { display: flex; align-items: center; gap: 12px; }
.card-text { flex: 1; min-width: 0; }
.card-label { font-weight: 600; font-size: 15px; }
.card-sub { font-size: 12px; color: var(--text-secondary); }
.card .amount { font-weight: 600; text-align: right; white-space: nowrap; font-size: 15px; }
.card .spark-slot { flex: none; display: flex; }
.card details summary { display: block; list-style: none; cursor: pointer; }
.card details summary::-webkit-details-marker { display: none; }
.card .accounts { margin: 10px 0 0; padding: 10px 0 0; border-top: 1px solid var(--border); list-style: none; font-size: 14px; }
.card .accounts li { display: flex; justify-content: space-between; padding: 3px 0; color: var(--text-secondary); }
.spark { flex: none; }
.spark-line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.spark-dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--text-secondary); margin-left: 6px; font-weight: 500; }

/* ---------- spending ---------- */
.month-picker { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.month-picker strong { font-size: 17px; }
.stat-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat span { display: block; font-size: 12px; color: var(--text-secondary); }
.stat b { display: block; font-size: 20px; margin-top: 2px; }
.stat small { font-size: 12px; color: var(--text-secondary); }
.legend { font-size: 12px; color: var(--text-secondary); display: flex; gap: 6px; align-items: center; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.lg-now { background: var(--series-1); }
.lg-prev { width: 3px !important; background: var(--prev); margin-left: 8px; }
.bars { list-style: none; margin: 0; padding: 0; }
.bars li { padding: 7px 0; cursor: pointer; }
.bar-top { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 5px; gap: 8px; }
.bar-val { white-space: nowrap; }
.bar-val small { color: var(--text-secondary); margin-left: 6px; }
.bar-track { position: relative; height: 10px; }
.bar-fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.bar-prev { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--prev); border-radius: 1px; }

/* ---------- activity ---------- */
#tx-search { margin-bottom: 10px; }
.chips { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; }
.chips button { padding: 7px 14px; border-radius: 999px; font-size: 14px; background: var(--surface-1); border: 1px solid var(--border); white-space: nowrap; }
.chips button.on { background: var(--series-1); color: #fff; border-color: var(--series-1); }
.day { font-size: 12px; color: var(--text-secondary); margin: 14px 4px 6px; text-transform: uppercase; letter-spacing: 0.04em; }
.tx { display: flex; align-items: center; gap: 10px; background: var(--surface-1); padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.tx-group { border-radius: var(--radius); overflow: hidden; border: 1px solid var(--border); }
.tx-group .tx:last-child { border-bottom: 0; }
.tx-main { flex: 1; min-width: 0; }
.tx-name { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-sub { font-size: 12px; color: var(--text-secondary); display: flex; gap: 6px; align-items: center; margin-top: 2px; flex-wrap: wrap; }
.chip { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-primary); }
.chip.move { background: var(--series-1-soft); color: var(--series-1); }
.tx .amount { font-size: 15px; white-space: nowrap; }
.tx .amount.in { color: var(--good); }
.tx .amount.neutral { color: var(--text-secondary); }
.more { width: 100%; margin-top: 12px; }
.empty { text-align: center; color: var(--text-secondary); padding: 32px 0; }

/* ---------- update ---------- */
.entries { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.entries li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); align-items: center; }
.entries li:last-child { border-bottom: 0; }
.entries button { padding: 4px 10px; font-size: 13px; }

/* ---------- history ---------- */
.chart { position: relative; min-height: 220px; touch-action: pan-y; }
.line-chart { width: 100%; height: 220px; display: block; }
.line-chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; }
.line-chart .area { fill: var(--series-1-soft); }
.line-chart .grid { stroke: var(--border); stroke-width: 1; }
.line-chart .axis { fill: var(--text-secondary); font-size: 11px; }
.line-chart .crosshair { stroke: var(--text-secondary); stroke-width: 1; stroke-dasharray: 3 3; }
.line-chart .line-dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.tooltip { position: absolute; top: 0; pointer-events: none; background: var(--text-primary); color: var(--surface-1);
  padding: 6px 9px; border-radius: 8px; font-size: 13px; display: flex; flex-direction: column; white-space: nowrap; }
.tooltip span { opacity: 0.75; font-size: 11px; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 8px; }
.table th, .table td { text-align: left; padding: 6px 0; border-bottom: 1px solid var(--border); }
.table td:last-child, .table th:last-child { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- sheet ---------- */
dialog { border: 0; border-radius: 18px 18px 0 0; padding: 20px 16px calc(16px + var(--safe-b)); width: 100%; max-width: 560px;
  margin: auto auto 0; background: var(--surface-1); color: var(--text-primary); }
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.tx-amount { font-size: 28px; font-weight: 700; margin: 8px 0 16px; }

/* ---------- settings ---------- */
.health { list-style: none; margin: 0; padding: 0; }
.health li { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.health li:last-child { border-bottom: 0; }
.h-icon { font-size: 16px; line-height: 22px; }
.h-text { flex: 1; min-width: 0; }
.h-top { display: flex; justify-content: space-between; gap: 8px; font-size: 15px; }
.h-state { font-size: 13px; font-weight: 600; white-space: nowrap; }
.h-failed .h-state { color: var(--bad); }
.h-ok .h-state { color: var(--good); }
.h-not_set_up .h-state, .h-not_set_up b { color: var(--text-secondary); }
.h-sub { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.legend-hint { margin: 10px 0 0; }
.cat-hint { margin: 4px 0 8px; }
.cats { list-style: none; margin: 0; padding: 0; }
.cats li { border-bottom: 1px solid var(--border); }
.cats li:last-child { border-bottom: 0; }
.cats button { width: 100%; display: flex; justify-content: space-between; gap: 10px; text-align: left;
  background: none; padding: 11px 0; border-radius: 0; }
.cats button > span:first-child { flex: none; white-space: nowrap; }
.cats .kw { font-size: 13px; color: var(--text-secondary); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-delete { border-top: 1px solid var(--border); padding-top: 12px; margin: 4px 0 12px; }
.device-actions { margin-top: 12px; }
button.danger { color: var(--bad); }
.tabbar button { font-size: 11px; }
