:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #17181b;
  --muted: #6b7079;
  --border: #e2e4e8;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --expense: #d93d42;
  --expense-soft: #fde8e8;
  --income: #1f9d55;
  --income-soft: #e3f6eb;
  --bubble-user: #2f6fed;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --radius: 16px;
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --surface: #1b1d21;
    --surface-2: #26292e;
    --text: #eceef1;
    --muted: #9aa0aa;
    --border: #2d3036;
    --accent: #5b8cff;
    --expense: #ff6b6f;
    --expense-soft: #3a1d1f;
    --income: #3fcf7f;
    --income-soft: #173126;
    --bubble-user: #3b6fe0;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
[hidden] { display: none !important; }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
}

/* ---------- top bar ---------- */
.topbar {
  padding-top: var(--safe-t);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-inner {
  min-height: 52px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; flex: 1; }
.period-nav { display: flex; align-items: center; justify-content: space-between; width: 100%; }
.period-nav .label { text-align: center; }
.period-nav .label b { display: block; font-size: 16px; }
.period-nav .label small { color: var(--muted); font-size: 12px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn:disabled { opacity: .3; }
.mini-summary { font-size: 12px; color: var(--muted); text-align: right; line-height: 1.3; }
.mini-summary b { font-size: 14px; color: var(--expense); font-variant-numeric: tabular-nums; }

/* ---------- views ---------- */
.view {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* ---------- chat ---------- */
.chat-list { padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; }
.msg.user {
  align-self: flex-end;
  background: var(--bubble-user);
  color: #fff;
  padding: 9px 13px;
  border-radius: 18px 18px 4px 18px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.bot { align-self: flex-start; width: 88%; }
.bot-text {
  background: var(--surface);
  padding: 9px 13px;
  border-radius: 18px 18px 18px 4px;
  box-shadow: var(--shadow);
  white-space: pre-wrap;
}
.bot-text.error { color: var(--expense); }
.typing { display: inline-flex; gap: 4px; padding: 14px 16px; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow); }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.draft {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.draft.saved { opacity: .75; }
.draft.discarded { opacity: .45; }
.draft-actions { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.draft-status { padding: 10px 14px; font-size: 14px; color: var(--muted); border-top: 1px solid var(--border); }
.draft-status.ok { color: var(--income); }

.btn {
  flex: 1;
  min-height: 46px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 16px;
  background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--expense); background: var(--expense-soft); }
.btn:active { filter: brightness(.94); }
.btn:disabled { opacity: .5; }

/* transaction row (shared by draft cards + month list) */
.tx {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  width: 100%;
  text-align: left;
  min-height: 60px;
}
.tx + .tx { border-top: 1px solid var(--border); }
button.tx:active { background: var(--surface-2); }
.tx-icon {
  width: 38px; height: 38px; border-radius: 12px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 18px;
}
.tx-icon.expense { background: var(--expense-soft); }
.tx-icon.income { background: var(--income-soft); }
.tx-main { flex: 1; min-width: 0; }
.tx-note { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta { font-size: 13px; color: var(--muted); }
.tx-amt { font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tx-amt.expense { color: var(--expense); }
.tx-amt.income { color: var(--income); }
.tx-edit-hint { font-size: 12px; color: var(--muted); padding: 0 14px 8px; }

.welcome { text-align: center; color: var(--muted); padding: 18px 8px 6px; font-size: 14px; }
.welcome h2 { color: var(--text); font-size: 18px; margin: 4px 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; }
.chip {
  padding: 8px 12px; border-radius: 999px; background: var(--surface);
  box-shadow: var(--shadow); font-size: 14px; color: var(--text);
}
.chip:active { background: var(--surface-2); }

/* ---------- composer ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 22px;
  padding: 10px 16px;
  font-size: 16px; /* >=16px prevents iOS zoom on focus */
  max-height: 120px;
  line-height: 1.35;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; flex-shrink: 0;
}
.send:disabled { opacity: .45; }

/* ---------- tab bar ---------- */
.tabbar {
  display: flex;
  flex-shrink: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.tab {
  flex: 1;
  height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; color: var(--muted);
}
.tab.active { color: var(--accent); font-weight: 600; }
body.keyboard-open .tabbar { display: none; }

/* ---------- month ---------- */
#month { padding: 12px 12px 24px; display: flex; flex-direction: column; gap: 12px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat {
  background: var(--surface); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow);
}
.stat.wide { grid-column: 1 / -1; }
.stat small { display: block; color: var(--muted); font-size: 13px; }
.stat b { font-size: 20px; font-variant-numeric: tabular-nums; }
.stat.wide b { font-size: 26px; }
.stat .expense { color: var(--expense); }
.stat .income { color: var(--income); }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-title { display: flex; justify-content: space-between; align-items: baseline; padding: 12px 14px 4px; font-weight: 650; }
.card-title small { color: var(--muted); font-weight: 400; font-size: 13px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin: 6px 14px 4px; }
.seg button { flex: 1; padding: 7px 0; border-radius: 8px; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.cat-row { padding: 9px 14px; }
.cat-row .top { display: flex; justify-content: space-between; gap: 8px; font-size: 15px; }
.cat-row .top span:last-child { font-variant-numeric: tabular-nums; font-weight: 600; }
.cat-row .bar { height: 6px; border-radius: 3px; background: var(--surface-2); margin-top: 6px; overflow: hidden; }
.cat-row .bar i { display: block; height: 100%; border-radius: 3px; }
.cat-row .pct { font-size: 12px; color: var(--muted); margin-top: 3px; }

.day-head {
  display: flex; justify-content: space-between;
  padding: 10px 14px 6px; font-size: 13px; color: var(--muted);
  background: var(--surface-2);
}
.day-head span:last-child { font-variant-numeric: tabular-nums; }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; font-size: 15px; }
.add-manual { width: 100%; padding: 14px; color: var(--accent); font-weight: 600; border-top: 1px solid var(--border); }

/* ---------- bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20;
  animation: fade .15s ease-out;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-width: 560px; margin: 0 auto;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(16px + var(--safe-b));
  max-height: 92dvh; overflow-y: auto;
  animation: up .2s ease-out;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 10px; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input[type="text"], .field input[type="date"], .field input[type="password"], .field input[inputmode] {
  width: 100%; min-height: 48px; padding: 10px 14px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--bg);
  font-size: 16px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.field .amount-input { font-size: 24px !important; font-weight: 650; font-variant-numeric: tabular-nums; }
.type-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.type-toggle button { min-height: 44px; border-radius: 12px; background: var(--surface-2); font-weight: 600; color: var(--muted); }
.type-toggle button.on.expense { background: var(--expense-soft); color: var(--expense); }
.type-toggle button.on.income { background: var(--income-soft); color: var(--income); }
.cat-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.cat-grid button {
  padding: 8px 12px; border-radius: 999px; background: var(--surface-2); font-size: 14px;
  border: 1.5px solid transparent;
}
.cat-grid button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.sheet-actions { display: flex; gap: 8px; margin-top: 6px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  top: calc(var(--safe-t) + 64px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 30;
  max-width: calc(100% - 32px); text-align: center;
  animation: fade .15s ease-out;
}

@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
