/* ============ Tokens ============ */
:root {
  --bg: #f8f5f0;
  --bg-card: #ffffff;
  --bg-soft: #f0ece5;
  --bg-sunken: #ebe6dd;
  --border: #e5dfd3;
  --border-strong: #d6cebd;
  --text: #1f1c17;
  --text-2: #5a544a;
  --text-3: #8c8578;
  --accent: oklch(0.62 0.13 42);
  --accent-soft: oklch(0.94 0.04 42);
  --accent-ink: oklch(0.32 0.08 42);
  --good: oklch(0.6 0.12 150);
  --good-soft: oklch(0.94 0.04 150);
  --warn: oklch(0.68 0.14 75);
  --warn-soft: oklch(0.95 0.05 75);
  --danger: oklch(0.58 0.16 25);
  --danger-soft: oklch(0.94 0.04 25);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-lg: 22px;
  --shadow-sm: 0 1px 2px rgba(40, 30, 20, 0.04), 0 1px 1px rgba(40, 30, 20, 0.03);
  --shadow-md: 0 4px 16px rgba(40, 30, 20, 0.06), 0 1px 2px rgba(40, 30, 20, 0.04);
  --shadow-lg: 0 24px 60px rgba(40, 30, 20, 0.18), 0 4px 12px rgba(40, 30, 20, 0.08);
  --font-sans: "Helvetica Neue", Helvetica, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-num: "SF Mono", "JetBrains Mono", "Menlo", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ============ Layout shell ============ */
.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: 96px;
  position: relative;
}
@media (min-width: 720px) {
  .app { max-width: 560px; box-shadow: 0 0 0 1px var(--border); background: var(--bg); }
}

.app-header {
  padding: 22px 20px 14px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.app-title {
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-3);
  margin: 0;
}
.app-month {
  font-size: 13px;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
}

/* ============ Bottom tab bar ============ */
.tabbar {
  position: fixed;
  bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 480px;
  background: rgba(248, 245, 240, 0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  z-index: 30;
}
@media (min-width: 720px) {
  .tabbar { max-width: 560px; }
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  border-radius: 12px;
  font-size: 11px;
  color: var(--text-3);
  font-weight: 500;
}
.tab.active { color: var(--accent-ink); background: var(--accent-soft); }
.tab-glyph {
  font-size: 18px;
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
}

/* ============ Cards ============ */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 16px;
}
.card-flat {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 16px;
}

.section {
  padding: 0 16px;
  margin-bottom: 18px;
}
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 4px 8px;
}
.section-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-2);
  margin: 0;
}
.section-action {
  font-size: 12px;
  color: var(--accent-ink);
  font-weight: 500;
}

/* ============ Amount display ============ */
.amount-display {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 22px 24px 18px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
.amount-label {
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 8px;
}
.amount-value {
  font-family: var(--font-num);
  font-size: 48px;
  font-weight: 300;
  letter-spacing: -0.02em;
  color: var(--text);
  line-height: 1;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.amount-value .currency {
  font-size: 18px;
  color: var(--text-3);
  font-weight: 400;
}
.amount-value.zero { color: var(--text-3); }

/* ============ Segmented (paid by, etc) ============ */
.segmented {
  display: flex;
  background: var(--bg-sunken);
  border-radius: 12px;
  padding: 3px;
  gap: 3px;
}
.segmented button {
  flex: 1;
  padding: 8px 10px;
  border-radius: 9px;
  font-size: 13px;
  color: var(--text-2);
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
.segmented button.on {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* ============ Category grid ============ */
.cat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.cat-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-2);
  min-height: 64px;
  transition: all 0.15s;
}
.cat-btn .glyph {
  font-size: 20px;
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
}
.cat-btn.on {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* ============ Number pad ============ */
.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.numpad button {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 0;
  font-size: 22px;
  font-weight: 400;
  font-family: var(--font-num);
  color: var(--text);
  transition: background 0.1s;
}
.numpad button:active { background: var(--bg-soft); }
.numpad button.del { color: var(--text-2); }
.numpad button.submit {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-family: var(--font-sans);
  font-size: 16px;
  font-weight: 600;
  grid-column: span 1;
}
.numpad button.submit:disabled {
  background: var(--bg-sunken);
  color: var(--text-3);
  border-color: var(--border);
}

/* ============ Text inputs ============ */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field-label {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-3);
  font-weight: 600;
}
.input, .select {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 15px;
  color: var(--text);
  width: 100%;
}
.input::placeholder { color: var(--text-3); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

/* ============ Expense list rows ============ */
.tx-list { display: flex; flex-direction: column; }
.tx-day-group { margin-bottom: 6px; }
.tx-day-head {
  padding: 10px 4px 6px;
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 12px;
  color: var(--text-3);
  font-variant-numeric: tabular-nums;
}
.tx-day-head .total { font-family: var(--font-num); color: var(--text-2); }
.tx-row {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom: none;
  font-size: 14px;
}
.tx-row:first-child { border-top-left-radius: var(--radius); border-top-right-radius: var(--radius); }
.tx-row:last-child { border-bottom-left-radius: var(--radius); border-top-right-radius: var(--radius); border-bottom-right-radius: var(--radius); border-bottom: 1px solid var(--border); }
.tx-row:only-child { border-radius: var(--radius); border-bottom: 1px solid var(--border); }
.tx-row + .tx-row { border-top: 1px solid var(--border); }
.tx-glyph {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--bg-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
}
.tx-main {
  display: flex; flex-direction: column; gap: 2px;
  min-width: 0;
}
.tx-cat { font-weight: 500; }
.tx-note {
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tx-amt {
  font-family: var(--font-num);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
.tx-paid {
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.tx-paid.h { background: oklch(0.62 0.13 250); }
.tx-paid.w { background: oklch(0.62 0.13 340); }

/* ============ Stat cards (insights) ============ */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
}
.stat-label {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-3);
  font-weight: 600;
  margin-bottom: 6px;
}
.stat-value {
  font-family: var(--font-num);
  font-size: 24px;
  font-weight: 400;
  color: var(--text);
}
.stat-sub {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 2px;
}
.stat-sub.good { color: oklch(0.5 0.12 150); }
.stat-sub.bad { color: var(--danger); }

/* ============ Category breakdown bars ============ */
.breakdown-row {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
}
.breakdown-row + .breakdown-row { border-top: 1px solid var(--border); }
.breakdown-glyph {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--bg-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
}
.breakdown-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.breakdown-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; }
.breakdown-pct { color: var(--text-3); font-variant-numeric: tabular-nums; font-size: 11px; font-family: var(--font-num); }
.breakdown-bar { height: 4px; background: var(--bg-sunken); border-radius: 99px; overflow: hidden; }
.breakdown-bar > div { height: 100%; background: var(--accent); border-radius: 99px; }
.breakdown-amt {
  font-family: var(--font-num);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* ============ Credit cards ============ */
.cc-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 14px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.cc-row.paid { background: var(--bg-soft); }
.cc-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.cc-name { font-weight: 500; font-size: 14px; }
.cc-meta { font-size: 12px; color: var(--text-3); display: flex; gap: 8px; align-items: center; }
.cc-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.cc-amt { font-family: var(--font-num); font-size: 16px; font-variant-numeric: tabular-nums; }
.cc-pill {
  font-size: 10px;
  letter-spacing: 0.06em;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 99px;
  background: var(--warn-soft);
  color: oklch(0.45 0.12 75);
}
.cc-pill.paid { background: var(--good-soft); color: oklch(0.4 0.1 150); }

/* ============ Modal / sheet ============ */
.scrim {
  position: fixed; inset: 0;
  background: rgba(20, 16, 12, 0.45);
  z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade 0.18s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: 100%;
  max-width: 480px;
  background: var(--bg);
  border-top-left-radius: 22px;
  border-top-right-radius: 22px;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  max-height: 90vh;
  overflow: auto;
  animation: rise 0.22s cubic-bezier(.2,.8,.2,1);
}
@keyframes rise { from { transform: translateY(40px); } to { transform: translateY(0); } }
.sheet-handle {
  width: 36px; height: 4px;
  border-radius: 99px;
  background: var(--border-strong);
  margin: -4px auto 14px;
}
.sheet h3 { margin: 0 0 12px; font-size: 17px; font-weight: 600; }

/* ============ Misc ============ */
.empty {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-3);
  font-size: 14px;
}
.muted { color: var(--text-3); }
.mono { font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.txt-good { color: oklch(0.5 0.12 150); }
.txt-bad { color: var(--danger); }
.fab {
  position: fixed;
  right: 20px;
  bottom: 84px;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  box-shadow: var(--shadow-md);
  font-size: 28px;
  display: flex; align-items: center; justify-content: center;
  z-index: 25;
}
.month-switch {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  padding: 12px 16px 8px;
}
.month-switch button {
  padding: 6px 10px;
  border-radius: 99px;
  color: var(--text-3);
  font-size: 18px;
}
.month-switch .label {
  font-size: 15px;
  font-weight: 600;
  min-width: 120px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.kv-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.kv-row + .kv-row { margin-top: 8px; }
.kv-label { font-size: 14px; color: var(--text-2); }
.kv-value { font-family: var(--font-num); font-variant-numeric: tabular-nums; font-weight: 500; }

.toolbtn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--text-2);
  font-weight: 500;
}
.toolbtn.primary { background: var(--accent); color: white; border-color: var(--accent); }

.toast {
  position: fixed;
  bottom: 110px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 10px 18px;
  border-radius: 99px;
  font-size: 13px;
  z-index: 60;
  animation: toast 1.6s ease forwards;
}
@keyframes toast {
  0% { opacity: 0; transform: translateX(-50%) translateY(8px); }
  10%, 80% { opacity: 1; transform: translateX(-50%) translateY(0); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-8px); }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}
