/* ---------- tokens ---------- */
:root {
  --bg: #f4f5f4;
  --card: #ffffff;
  --card-2: #f0f2f1;
  --text: #121816;
  --muted: #66716e;
  --line: #e2e6e4;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d6efeb;
  --pos: #006300;          /* income / good delta text */
  --neg: #b42318;
  /* chart roles (reference palette, light) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --status-good: #0ca30c;
  --status-warn: #fab219;
  --status-crit: #d03b3b;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1211;
    --card: #171e1c;
    --card-2: #1f2826;
    --text: #e8eeec;
    --muted: #94a19d;
    --line: #26302d;
    --accent: #2dd4bf;
    --accent-ink: #04201c;
    --accent-soft: #123a35;
    --pos: #0ca30c;
    --neg: #f97066;
    --series-1: #3987e5;
    --series-2: #d95926;
    --grid: #2c2c2a;
    --axis: #383835;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] { color-scheme: dark; }

/* ---------- base ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1 { font-size: 26px; margin: 8px 0 4px; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.strong { font-weight: 650; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.pad { padding: 0 16px; }
.error { color: var(--neg); margin: 0; font-size: 14px; }

input, select, button, textarea { font: inherit; color: inherit; }
/* 16px minimum keeps iOS Safari from zooming into fields. */
input, select { font-size: 16px; }
input:not([type=checkbox]), select {
  width: 100%;
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card-2);
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button { cursor: pointer; border: 0; background: none; padding: 0; }

.card {
  background: var(--card);
  border-radius: 20px;
  padding: 18px;
  margin: 0 0 12px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; }
.card-head h2 { margin-bottom: 8px; }
.btn { border-radius: 14px; padding: 14px 20px; font-weight: 650; width: 100%; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.secondary { background: var(--card-2); }
.btn.danger { background: transparent; color: var(--neg); border: 1px solid var(--line); }
.link { color: var(--accent); padding: 8px 4px; }
.icon-btn { width: 44px; height: 44px; font-size: 28px; color: var(--accent); border-radius: 50%; }

/* ---------- auth ---------- */
.auth { max-width: 420px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 48px) 16px 32px; }
.auth-brand { text-align: center; margin-bottom: 20px; }
.auth-brand img { border-radius: 14px; }
.form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field label { font-size: 14px; font-weight: 600; }

/* ---------- app shell ---------- */
#app { max-width: 560px; margin: 0 auto; padding: env(safe-area-inset-top) 16px calc(env(safe-area-inset-bottom) + 96px); }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 4px) 0 8px;
  margin-top: calc(-1 * env(safe-area-inset-top));
  background: var(--bg);
}
.month-label { font-size: 18px; font-weight: 700; padding: 8px 12px; border-radius: 10px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: end;
  max-width: 560px; margin: 0 auto;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(16px);
  -webkit-backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar button { display: grid; justify-items: center; gap: 2px; font-size: 11px; color: var(--muted); padding: 4px 0; }
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 650; }
.tabbar .ti { font-size: 20px; line-height: 1; filter: grayscale(1); opacity: .7; }
.tabbar button[aria-current="page"] .ti { filter: none; opacity: 1; }
.fab {
  justify-self: center; align-self: center;
  width: 56px !important; height: 56px; margin-top: -22px;
  border-radius: 50%; background: var(--accent) !important; color: var(--accent-ink) !important;
  font-size: 32px !important; line-height: 1; font-weight: 400 !important;
  box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 40%, transparent);
  display: grid !important; place-items: center;
}

/* ---------- hero ---------- */
.hero-label { font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.hero-value { font-size: 40px; font-weight: 750; letter-spacing: -.02em; margin: 2px 0 10px; }
.hero-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; font-weight: 650; }
.hero-budget { margin-top: 14px; display: grid; gap: 6px; }
.progress { height: 8px; border-radius: 4px; background: var(--card-2); overflow: hidden; }
.progress > span { display: block; height: 100%; border-radius: 4px; background: var(--accent); width: 0; transition: width .4s ease; }
.progress.warn > span { background: var(--status-warn); }
.progress.over > span { background: var(--status-crit); }

.insights { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px 12px; padding: 0 16px; scrollbar-width: none; }
.insights::-webkit-scrollbar { display: none; }
.chip-insight {
  flex: 0 0 auto; background: var(--card); border-radius: 14px; padding: 10px 12px;
  font-size: 13px; box-shadow: var(--shadow); max-width: 240px;
}
.chip-insight b { display: block; font-size: 15px; }

.search { margin-bottom: 8px; }
.search input { background: var(--card); border-color: transparent; }

/* ---------- transactions ---------- */
.day-head { display: flex; justify-content: space-between; margin: 18px 4px 6px; font-size: 13px; font-weight: 650; color: var(--muted); }
.tx {
  display: grid; grid-template-columns: 42px 1fr auto; align-items: center; gap: 12px;
  width: 100%; text-align: left; background: var(--card); padding: 10px 14px 10px 10px;
  border-radius: 16px; margin-bottom: 6px; box-shadow: var(--shadow);
}
.tx-ico { width: 42px; height: 42px; border-radius: 12px; background: var(--card-2); display: grid; place-items: center; font-size: 22px; }
.tx-main { min-width: 0; }
.tx-cat { font-weight: 600; }
.tx-note { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amt { font-weight: 650; white-space: nowrap; }
.tx-amt.income { color: var(--pos); }
.tx-sub { font-size: 12px; color: var(--muted); text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.empty .big { font-size: 44px; display: block; margin-bottom: 8px; }

/* ---------- segmented ---------- */
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--card-2); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.segmented button { padding: 8px; border-radius: 10px; font-weight: 600; font-size: 14px; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ---------- stats ---------- */
.cat-bars { display: grid; gap: 12px; }
.cat-bar { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 14px; }
.cat-bar .track { grid-column: 1 / -1; height: 10px; }
.cat-bar .track span { display: block; height: 100%; border-radius: 0 4px 4px 0; background: var(--series-1); min-width: 2px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .avgline { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .bar { fill: var(--series-1); }
.chart .bar.s2 { fill: var(--series-2); }
.chart .hit { fill: transparent; }
.chart .hit:hover + .bar, .chart .bar.active { opacity: .75; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin: -4px 0 8px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.s1 { background: var(--series-1); }
.sw.s2 { background: var(--series-2); }
.table-view { margin-top: 10px; font-size: 13px; }
.table-view summary { color: var(--accent); cursor: pointer; }
.table-view table { width: 100%; border-collapse: collapse; margin-top: 8px; font-variant-numeric: tabular-nums; }
.table-view th, .table-view td { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: right; }
.table-view th:first-child, .table-view td:first-child { text-align: left; }
.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--text); color: var(--bg); font-size: 13px; padding: 6px 10px; border-radius: 8px;
  transform: translate(-50%, calc(-100% - 10px)); white-space: nowrap;
}

/* ---------- budgets ---------- */
.budget-list { display: grid; gap: 8px; }
.budget {
  display: grid; grid-template-columns: 42px 1fr; gap: 6px 12px; align-items: center;
  width: 100%; text-align: left; background: var(--card); border-radius: 16px; padding: 12px 14px 12px 10px; box-shadow: var(--shadow);
}
.budget .tx-ico { grid-row: span 2; }
.budget-top { display: flex; justify-content: space-between; gap: 8px; font-size: 15px; }
.budget-status { font-size: 12px; font-weight: 650; }
.budget-status.over { color: var(--status-crit); }
.budget-status.warn { color: color-mix(in srgb, var(--status-warn) 70%, var(--text)); }
.budget-none { color: var(--muted); font-size: 14px; }

/* ---------- settings ---------- */
.profile { display: flex; gap: 14px; align-items: center; }
.avatar { width: 52px; height: 52px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 750; font-size: 20px; }
.group { background: var(--card); border-radius: 16px; overflow: hidden; margin-bottom: 8px; box-shadow: var(--shadow); }
.group-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 16px 8px; font-weight: 600; }
.row-btn {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; padding: 14px 16px; text-align: left; min-height: 52px;
}
.row-btn + .row-btn { border-top: 1px solid var(--line); }
.row-btn select { width: auto; padding: 6px 8px; background: var(--card-2); }
.chev { color: var(--muted); font-size: 20px; }
.danger-text { color: var(--neg); }

/* ---------- sheets ---------- */
dialog.sheet {
  border: 0; padding: 0 16px calc(env(safe-area-inset-bottom) + 16px);
  width: 100%; max-width: 560px; max-height: 92vh;
  margin: auto auto 0; border-radius: 22px 22px 0 0;
  background: var(--bg); color: var(--text);
  overflow-y: auto; overscroll-behavior: contain;
}
dialog.sheet[open] { animation: up .22s ease-out; }
dialog.sheet::backdrop { background: rgba(0,0,0,.35); }
@keyframes up { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { dialog.sheet[open] { animation: none; } .progress > span { transition: none; } }
.sheet form, #panel-body { display: grid; gap: 12px; padding-bottom: 8px; }
.sheet-grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 4px; }
.sheet-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; position: sticky; top: 0; background: var(--bg); padding: 4px 0 8px; z-index: 1; }
.sheet-head h2 { margin: 0; font-size: 17px; }
.sheet-head > :last-child { justify-self: end; }
.sheet-head > :first-child { justify-self: start; }

.amount-row { display: grid; grid-template-columns: 1fr 100px; gap: 8px; }
#e-amount { font-size: 34px; font-weight: 700; padding: 10px 14px; background: var(--card); border-color: transparent; }
#e-currency { background: var(--card); border-color: transparent; }
.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.cat-opt {
  display: grid; justify-items: center; gap: 2px; padding: 10px 2px 8px; border-radius: 14px;
  background: var(--card); font-size: 11px; border: 2px solid transparent; line-height: 1.2;
}
.cat-opt .e { font-size: 24px; }
.cat-opt[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); }
.row2 { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
#e-note, #e-date { background: var(--card); border-color: transparent; }
.switch { display: flex; align-items: center; gap: 8px; font-size: 14px; white-space: nowrap; padding: 0 4px; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }

.list { background: var(--card); border-radius: 16px; overflow: hidden; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; min-height: 52px; width: 100%; text-align: left; }
.list-item + .list-item { border-top: 1px solid var(--line); }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .e { font-size: 22px; width: 28px; text-align: center; }
.link-box { word-break: break-all; background: var(--card); border-radius: 12px; padding: 12px; font-size: 14px; user-select: all; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.emoji-input { width: 64px !important; text-align: center; font-size: 22px !important; }
.inline { display: flex; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 90px); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 60;
  max-width: calc(100% - 32px);
}
