/* ---------- Design tokens ---------- */
:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11,11,11,0.10);
  --border-strong: rgba(11,11,11,0.16);

  --series-blue: #2a78d6;
  --series-blue-dim: #cde2fb;
  --series-blue-deep: #163f73;
  --series-orange: #eb6834;
  --series-orange-deep: #9c3c14;
  --series-aqua: #1baf7a;
  --series-yellow: #eda100;
  --series-magenta: #e87ba4;
  --series-green: #008300;
  --series-violet: #4a3aa7;
  --series-red: #e34948;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --success-text: #006300;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(11,11,11,0.06), 0 4px 16px rgba(11,11,11,0.05);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255,255,255,0.10);
    --border-strong: rgba(255,255,255,0.18);

    --series-blue: #3987e5;
    --series-blue-dim: #184f95;
    --series-blue-deep: #1f5fa8;
    --series-orange: #d95926;
    --series-orange-deep: #9c3f16;
    --series-aqua: #199e70;
    --series-yellow: #c98500;
    --series-magenta: #d55181;
    --series-green: #008300;
    --series-violet: #9085e9;
    --series-red: #e66767;
    --status-good: #0ca30c;
    --status-warning: #fab219;
    --status-serious: #ec835a;
    --status-critical: #e66767;
    --success-text: #0ca30c;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255,255,255,0.10);
  --border-strong: rgba(255,255,255,0.18);

  --series-blue: #3987e5;
  --series-blue-dim: #184f95;
  --series-blue-deep: #1f5fa8;
  --series-orange: #d95926;
  --series-orange-deep: #9c3f16;
  --series-aqua: #199e70;
  --series-yellow: #c98500;
  --series-magenta: #d55181;
  --series-green: #008300;
  --series-violet: #9085e9;
  --series-red: #e66767;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #e66767;
  --success-text: #0ca30c;
}

* { box-sizing: border-box; min-width: 0; }
html, body { margin: 0; padding: 0; overflow-x: hidden; touch-action: manipulation; }
body {
  font-family: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.topbar {
  padding: calc(16px + env(safe-area-inset-top)) 16px 8px;
}
.brand {
  font-size: 20px;
  font-weight: 700;
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding: 4px 12px 12px;
  position: sticky;
  top: 0;
  background: var(--page);
  z-index: 5;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.tab.active {
  background: var(--series-blue);
  color: #fff;
}

main { padding: 0 12px; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------- Panels ---------- */
.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: 8px;
  margin-bottom: 12px;
}
.panel-header h2 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
  color: var(--text-primary);
}

/* ---------- Segmented toggle (panel-header grouping switch) ---------- */
.seg-toggle {
  display: inline-flex;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
  flex: 0 0 auto;
}
.seg-toggle-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.seg-toggle-btn.on {
  background: var(--series-blue);
  color: #fff;
}

/* ---------- Month/Year nav ---------- */
.month-nav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding: 12px 4px;
}
.month-label {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 17px;
  font-weight: 700;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  font-size: 18px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--gridline); }
.export-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  box-shadow: var(--shadow);
}
.export-btn span { font-size: 14px; line-height: 1; }
.export-btn:hover { background: var(--gridline); }

.btn-ghost {
  border: none;
  background: transparent;
  color: var(--series-blue);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 8px;
  flex-basis: 100%;
  text-align: center;
  order: 4;
}

/* ---------- Stat tiles ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.stat-grid-3 { grid-template-columns: 1fr; }
@media (min-width: 480px) {
  .stat-grid-3 { grid-template-columns: repeat(3, 1fr); }
}
.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.stat-tile.highlight {
  border-color: var(--series-blue);
  box-shadow: 0 0 0 1px var(--series-blue) inset, var(--shadow);
}
.stat-tile-wide {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  text-align: center;
}
.stat-tile-wide.highlight {
  border-color: var(--series-blue);
  box-shadow: 0 0 0 1px var(--series-blue) inset, var(--shadow);
}
.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.stat-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stat-value.negative { color: var(--status-critical); }
.stat-value.positive { color: var(--success-text); }

/* ---------- Card breakdown ---------- */
.card-breakdown { display: flex; flex-direction: column; gap: 10px; }
.card-row {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.card-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.card-row-title { display: flex; flex-direction: column; }
.card-row-name { font-weight: 600; font-size: 14px; }
.card-row-bank { font-size: 12px; color: var(--text-muted); }
.card-row-amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.card-row-amount.positive { color: var(--success-text); }
.card-row-amount.negative { color: var(--status-critical); }
.card-row.assist-rank-top { border-color: var(--series-blue); box-shadow: 0 0 0 1px var(--series-blue) inset; }
.assist-rank-row .card-row-split { margin-top: 8px; }
.card-row-compact { padding: 8px 12px; }
.card-row-compact .card-row-bar-track { margin-bottom: 0; }
.card-row-amt-col { display: flex; flex-direction: column; align-items: flex-end; flex-shrink: 0; }
.card-row-count { font-size: 11px; color: var(--text-muted); }
.card-row-bar-track {
  height: 6px;
  border-radius: 999px;
  background: var(--gridline);
  overflow: hidden;
  margin-bottom: 8px;
}
.card-row-bar-fill-wrap {
  display: flex;
  height: 100%;
}
.card-row-bar-seg { height: 100%; }
.card-row-bar-seg.seg-our { background: var(--series-blue); }
.card-row-bar-seg.seg-friend { background: var(--status-good); }
.card-row-bar-seg.seg-subsidy { background: var(--status-warning); }
.card-row-bar-fill {
  height: 100%;
  background: var(--series-blue);
  border-radius: 999px;
}
.card-row-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
}
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  cursor: pointer;
  border: 1px solid transparent;
  background: none;
}
.badge-paid { color: var(--status-good); background: color-mix(in srgb, var(--status-good) 14%, transparent); }
.badge-unpaid { color: var(--status-warning); background: color-mix(in srgb, var(--status-warning) 20%, transparent); }
.badge-cashback { color: var(--series-aqua); background: color-mix(in srgb, var(--series-aqua) 16%, transparent); }

/* ---------- Expense list ---------- */
.expense-list { display: flex; flex-direction: column; gap: 8px; }
.expense-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.expense-item:hover { background: var(--gridline); }
.expense-item-main { display: flex; flex-direction: column; flex: 1 1 auto; min-width: 0; }
.expense-item-desc { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense-item-meta { font-size: 12px; color: var(--text-muted); }
.expense-item-amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; flex-shrink: 0; }
.expense-item-amount small { display: block; font-weight: 500; font-size: 11px; color: var(--text-muted); }
.empty-state { text-align: center; color: var(--text-muted); font-size: 13px; padding: 24px 8px; }

/* ---------- Forms ---------- */
.form-grid, form { display: flex; flex-direction: column; gap: 12px; }
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}
.checkbox-label { flex-direction: row; align-items: center; gap: 8px; }
input[type="text"], input[type="number"], input[type="date"], select {
  font-size: 15px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  font-family: inherit;
}
input:focus, select:focus { outline: 2px solid var(--series-blue); outline-offset: 1px; }
details {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
details summary { cursor: pointer; font-size: 13px; font-weight: 600; color: var(--text-secondary); }
details form, details label { margin-top: 10px; }
.form-actions { display: flex; justify-content: flex-end; }

button { font-family: inherit; }
.btn-primary, .btn-secondary, .btn-danger {
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary { background: var(--series-blue); color: #fff; }
.btn-secondary { background: var(--gridline); color: var(--text-primary); }
.btn-danger { background: none; color: var(--status-critical); margin-right: auto; }
.btn-sm { padding: 6px 12px; font-size: 12px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 50;
}
@media (min-width: 560px) {
  .modal-overlay { align-items: center; }
}
.modal {
  background: var(--surface-1);
  color: var(--text-primary);
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  max-height: 92dvh;
  overflow: hidden;
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  position: relative;
  display: flex;
  flex-direction: column;
}
@media (min-width: 560px) {
  .modal { border-radius: var(--radius); }
}
.modal-scrollbody {
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  padding: 44px calc(20px + env(safe-area-inset-right)) calc(92px + env(safe-area-inset-bottom)) calc(20px + env(safe-area-inset-left));
}
.modal h2 { margin: 0 0 14px; font-size: 17px; }
.modal-header-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.modal-header-row h2 { margin: 0; }
.modal-close-btn {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-close-btn:hover { background: var(--gridline); }
.modal-close-floating {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 6;
}
#btnCloseExpenseModal { display: none; }
.modal-scroll-nav {
  position: absolute;
  right: 12px;
  bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 6;
}
.modal-nav-btn {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: 15px;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-nav-btn:hover { background: var(--gridline); }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 4px; }

/* ---------- Settings list ---------- */
.settings-list { display: flex; flex-direction: column; gap: 8px; }
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer;
}
.settings-row:hover { background: var(--gridline); }
.settings-row-name { font-weight: 600; font-size: 14px; }
.settings-row-meta { font-size: 12px; color: var(--text-muted); }
/* Rows with a long description on the left and an amount on the right (subsidy popup). */
.settings-row.money-row { gap: 12px; }
.money-row-main { flex: 1 1 auto; min-width: 0; }
.money-row-amount { flex-shrink: 0; white-space: nowrap; font-variant-numeric: tabular-nums; font-weight: 600; }
.money-row-items {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.settings-row.tax-calc-row { flex-direction: column; align-items: stretch; gap: 6px; cursor: default; }
.tax-calc-header { display: flex; align-items: center; justify-content: space-between; }
.tax-bracket-list { display: flex; flex-direction: column; gap: 4px; padding-top: 6px; border-top: 1px dashed var(--border); }
.tax-bracket-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 11px; color: var(--text-muted); }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.hint { font-size: 12px; color: var(--text-muted); margin: 0; }
.hint.positive { color: var(--success-text); }
.hint.negative { color: var(--status-critical); }

/* ---------- History month groups ---------- */
.history-month-panel { padding: 0; overflow: hidden; }
.history-month-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  color: var(--text-primary);
  text-align: left;
}
.history-month-header:hover { background: var(--gridline); }
.history-month-label { font-size: 14px; font-weight: 700; }
.history-month-count { font-size: 12px; font-weight: 500; color: var(--text-muted); }
.history-month-total {
  font-size: 14px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.history-month-chevron { font-size: 12px; color: var(--text-muted); }
.history-month-items { padding: 0 16px 16px; }

/* ---------- Filter row (history) ---------- */
.filter-row { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.filter-row:last-of-type { margin-bottom: 14px; }
@media (min-width: 480px) {
  .filter-row { flex-direction: row; }
}
.filter-row input, .filter-row select { flex: 1; }
.filter-row-wrap { flex-wrap: wrap; }
.filter-row-wrap select { flex: 1 1 45%; min-width: 120px; }

/* ---------- Summary rows (income) ---------- */
.summary-rows { display: flex; flex-direction: column; gap: 8px; }
.summary-row {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 14px; padding: 8px 2px; border-bottom: 1px solid var(--gridline);
}
.summary-row:last-child { border-bottom: none; font-weight: 700; font-size: 15px; padding-top: 12px; }
.summary-row-label { color: var(--text-secondary); }
.summary-row-value { font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- Income form: subsections & computed rows ---------- */
.subsection {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.subsection-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font-family: inherit;
  color: inherit;
  cursor: pointer;
  text-align: left;
}
.subsection-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
}
.subsection-header-right { display: flex; align-items: center; gap: 8px; }
.subsection-chevron { font-size: 11px; color: var(--text-muted); transition: transform .15s; }
.subsection.collapsed .subsection-chevron { transform: rotate(-90deg); }
.subsection-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border-strong);
}
.subsection.collapsed .subsection-body { display: none; }
.rate-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}
.rate-label { color: var(--text-secondary); }
.rate-value { font-weight: 600; font-variant-numeric: tabular-nums; }
.rate-row.total-row { border-top: 1px solid var(--gridline); padding-top: 8px; }
.rate-row.total-row .rate-value { color: var(--series-blue); font-weight: 700; }
.subsection-header-right .rate-value { color: var(--series-blue); font-weight: 700; }

.summary-row.total-row .summary-row-value { color: var(--series-blue); }
.summary-row.net-total-row {
  font-weight: 700;
  font-size: 18px;
  padding: 4px 2px;
}
.summary-row.net-total-row .summary-row-value { color: var(--success-text); }

.btn-block { width: 100%; }

/* ---------- Chips (quick-entry selectors) ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0; }
.chip {
  border: 1.5px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.chip.on { background: var(--series-blue); border-color: var(--series-blue); color: #fff; }
.chip.amber.on { background: var(--status-warning); border-color: var(--status-warning); color: #3a2a05; }
.chip .x { margin-left: 6px; opacity: .7; }
.seg-title {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 700;
  margin: 16px 0 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.seg-title .mng {
  font-size: 12px;
  color: var(--series-blue);
  background: none;
  border: none;
  font-family: inherit;
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}
.row { display: flex; gap: 8px; align-items: center; }

/* ---------- Amount display + numpad ---------- */
.amt-display {
  background: var(--series-blue);
  border-radius: 16px;
  padding: 16px;
  color: #fff;
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 4px 0 12px;
}
.amt-display .cur {
  font-size: 13px;
  background: rgba(255,255,255,0.18);
  padding: 4px 9px;
  border-radius: 8px;
  font-weight: 700;
}
.amt-display .num {
  font-size: 32px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: .01em;
  word-break: break-all;
}
.numpad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.numpad button {
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: 12px;
  padding: 14px 0;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.numpad button:active { background: var(--gridline); }
.numpad .op { background: color-mix(in srgb, var(--status-warning) 18%, transparent); color: var(--text-primary); font-size: 20px; }
.numpad .eq { background: var(--series-blue); color: #fff; }
.numpad .fn { background: var(--gridline); font-size: 16px; }

#expOtherLabel { margin-top: 2px; }

/* ---------- Card budget status (numpad form + card breakdown) ---------- */
.budget-status {
  margin: 8px 0 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.budget-status-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  margin-bottom: 6px;
}
.budget-status-label { color: var(--text-secondary); font-weight: 600; }
.budget-status-pct { font-weight: 700; font-variant-numeric: tabular-nums; }
.budget-bar-track {
  height: 7px;
  border-radius: 999px;
  background: var(--gridline);
  overflow: hidden;
}
.budget-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width .15s ease, background-color .15s ease;
}
.budget-bar-fill.green { background: var(--status-good); }
.budget-bar-fill.yellow { background: var(--status-warning); }
.budget-bar-fill.red { background: var(--status-critical); }
.budget-status-hint { margin-top: 6px; font-size: 12px; color: var(--text-secondary); }
.budget-status-hint.negative { color: var(--status-critical); font-weight: 700; }
#dateChips { align-items: center; }
#dateChips input[type="date"] { flex: 1 1 150px; }
#splitPeopleList, #fundEntriesListWrap { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.split-person-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.split-person-row .split-person-name { flex: 0 0 88px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split-person-row input[type="number"] { flex: 1; padding: 8px 10px; }
.split-person-row .checkbox-label { flex: 0 0 auto; font-size: 12px; font-weight: 500; }
.split-person-row .split-full-btn { flex: 0 0 auto; padding: 6px 10px; font-size: 12px; }
.diff-bad { color: var(--status-critical); font-weight: 700; }

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  right: calc(20px + env(safe-area-inset-right));
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: 56px;
  height: 56px;
  border-radius: 999px;
  background: var(--series-blue);
  color: #fff;
  border: none;
  font-size: 28px;
  line-height: 1;
  box-shadow: 0 6px 20px rgba(42,120,214,0.4);
  cursor: pointer;
  z-index: 40;
}
.fab:hover { filter: brightness(1.08); }

/* ---------- Chart ---------- */
.chart-wrap { width: 100%; }
.chart-legend-row { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; margin-top: 8px; }
.chart-legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
.chart-legend-item i { display: inline-block; width: 14px; height: 2px; border-radius: 1px; }
.chart-legend-item-series i { width: 10px; height: 10px; border-radius: 3px; }
.chart-tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--text-primary);
  color: var(--surface-1);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  z-index: 100;
  transform: translate(-50%, -110%);
  white-space: nowrap;
}

@media (max-width: 380px) {
  .stat-value { font-size: 17px; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 12px 22px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  box-shadow: var(--shadow);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Remaining-balance panel ---------- */
.remaining-panel { text-align: center; }
.stat-clickable { cursor: pointer; }
.stat-clickable:hover { background: var(--gridline); }
.remaining-panel .stat-label { margin-bottom: 4px; }
.stat-value-lg { font-size: 30px; }
.stat-value-lg.positive, .stat-value.positive { color: var(--success-text); }
.stat-value-lg.negative, .stat-value.negative { color: var(--status-critical); }

/* ---------- Donut chart ---------- */
.donut-wrap { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.donut-legend { width: 100%; display: flex; flex-direction: column; gap: 8px; }
.donut-legend-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.donut-swatch { width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; }
.donut-legend-name { flex: 1; color: var(--text-primary); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.donut-legend-value { color: var(--text-secondary); font-variant-numeric: tabular-nums; white-space: nowrap; }
.donut-center-label { font-size: 13px; fill: var(--text-secondary); }
.donut-center-value { font-size: 20px; font-weight: 700; fill: var(--text-primary); }

/* ---------- Card breakdown (expandable, urgency) ---------- */
.card-row { cursor: pointer; transition: border-color .15s; }
.card-row.urgency-green { border-color: color-mix(in srgb, var(--status-good) 55%, var(--border-strong)); }
.card-row.urgency-yellow { border-color: var(--status-warning); box-shadow: 0 0 0 1px var(--status-warning) inset; }
.card-row.urgency-red { border-color: var(--status-critical); box-shadow: 0 0 0 1px var(--status-critical) inset; }
.card-row-days { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; }
.card-row-days.urgency-green { color: var(--status-good); background: color-mix(in srgb, var(--status-good) 14%, transparent); }
/* Solid yellow + near-black text: a tinted yellow washes out on the dark theme's card. */
.card-row-days.urgency-yellow { color: #2b1d00; background: var(--status-warning); }
.card-row-days.urgency-red { color: var(--status-critical); background: color-mix(in srgb, var(--status-critical) 16%, transparent); }
.card-row-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.card-row-items { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border-strong); display: flex; flex-direction: column; }
.card-row-subfund { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }
.card-row-split { display: flex; flex-direction: column; gap: 4px; }
.card-row-split + .card-row-items-list { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border-strong); }
.card-row-items-list { display: flex; flex-direction: column; gap: 6px; }
.card-row-hint { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }
.card-row-split-row { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.card-row-split-item + .card-row-split-item { margin-top: 2px; }
.card-row-split-subnote { font-size: 11px; color: var(--text-muted); text-align: right; margin-top: 1px; }
.card-row-split-subnote.is-settled { color: var(--success-text); }
.card-row-split-row .amt-settled { color: var(--status-good); font-weight: 700; }
.card-row-split-row .amt-unsettled { color: var(--status-critical); font-weight: 700; }
.card-row-split-friendtotal { font-weight: 600; color: var(--text-primary); padding-top: 4px; border-top: 1px dotted var(--border-strong); }
.card-row-payable { font-size: 13px; font-weight: 700; color: var(--text-primary); }
.card-row-budget { margin-top: 8px; }
.card-row-budget-row { display: flex; align-items: center; justify-content: space-between; font-size: 11px; color: var(--text-secondary); margin-bottom: 4px; font-variant-numeric: tabular-nums; }

/* ---------- Friend settlement receipt ---------- */
.receipt-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin-bottom: 12px;
}
.receipt-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 10px; }
.receipt-header-title { display: flex; flex-direction: column; gap: 2px; }
.receipt-name { font-size: 16px; font-weight: 700; }
.receipt-month { font-size: 12px; color: var(--text-muted); }
.receipt-total { font-size: 15px; font-weight: 700; }
.receipt-items { display: flex; flex-direction: column; gap: 6px; border-top: 1px dashed var(--border-strong); border-bottom: 1px dashed var(--border-strong); padding: 10px 0; margin-bottom: 10px; }
.receipt-item-row { display: flex; align-items: center; justify-content: space-between; font-size: 13px; gap: 8px; }
.receipt-item-desc { color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.receipt-item-amt { font-variant-numeric: tabular-nums; color: var(--text-secondary); white-space: nowrap; }
.receipt-footer { display: flex; align-items: center; justify-content: space-between; }
.receipt-outstanding { font-weight: 700; }
.receipt-outstanding.settled { color: var(--success-text); }
.receipt-outstanding.pending { color: var(--status-critical); }
.receipt-open-btn { width: 100%; text-align: left; background: none; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; cursor: pointer; color: inherit; font-family: inherit; margin-bottom: 8px; }
.receipt-open-btn:hover { background: var(--gridline); }
.receipt-preview-wrap { display: flex; justify-content: center; min-width: 0; background: var(--page); border-radius: var(--radius-sm); padding: 12px; overflow: auto; max-height: 60vh; }
.receipt-preview-wrap img { display: block; max-width: 100%; min-width: 0; height: auto; border-radius: 10px; box-shadow: var(--shadow); }

/* ---------- Spending plans ---------- */
.plan-card { margin-bottom: 10px; }
.plan-item { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border-strong); }
.plan-item .budget-bar-track { margin: 2px 0; }
.plan-item .negative { color: var(--status-critical); font-weight: 600; }
.plan-item .positive { color: var(--status-good); font-weight: 600; }
.plan-entry-chips { margin-top: 2px; }
.plan-entry-chips .chip { font-size: 11px; padding: 4px 10px; }
#tab-plan .stat-tile-wide + .stat-tile-wide { margin-top: 8px; }

/* ---------- Assistant: bill-paid action + tile details ---------- */
.assist-bill-actions { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 8px; }
.assist-view-link { font-size: 12px; font-weight: 600; color: var(--series-blue); }
.assist-paid-row { opacity: 0.85; }
.assist-detail-block { margin-bottom: 10px; }
.assist-detail-block .card-row-split { margin-top: 8px; }
.assist-detail-total { margin-top: 6px; padding-top: 10px; border-top: 1px solid var(--border-strong); }
.assist-detail-total .card-row-split-row { font-size: 14px; color: var(--text-primary); }

/* ---------- Tab icons: each one does its own little move when its tab is picked ---------- */
.tab { display: inline-flex; align-items: center; gap: 6px; transition: transform 0.15s ease; }
.tab:active { transform: scale(0.94); }
.tab-icon { display: inline-block; font-size: 15px; line-height: 1; }
.tab:hover .tab-icon { transform: translateY(-1px); }
.tab.active[data-tab="dashboard"] .tab-icon { animation: tab-bounce 0.6s ease; }
.tab.active[data-tab="income"] .tab-icon { animation: tab-coin 0.7s ease; }
.tab.active[data-tab="yearly"] .tab-icon { animation: tab-grow 0.6s ease; transform-origin: bottom; }
.tab.active[data-tab="history"] .tab-icon { animation: tab-clock 0.8s ease; }
.tab.active[data-tab="plan"] .tab-icon { animation: tab-wiggle 0.6s ease; }
.tab.active[data-tab="assist"] .tab-icon { animation: tab-compass 0.9s ease; }
.tab.active[data-tab="settings"] .tab-icon { animation: tab-gear 0.8s ease; }
@keyframes tab-bounce { 0%, 100% { transform: translateY(0); } 30% { transform: translateY(-6px); } 55% { transform: translateY(0); } 75% { transform: translateY(-2px); } }
@keyframes tab-coin { from { transform: rotateY(0); } to { transform: rotateY(720deg); } }
@keyframes tab-grow { 0% { transform: scaleY(0.3); } 60% { transform: scaleY(1.2); } 100% { transform: scaleY(1); } }
@keyframes tab-clock { from { transform: rotate(-360deg); } to { transform: rotate(0); } }
@keyframes tab-wiggle { 0%, 100% { transform: rotate(0); } 20% { transform: rotate(-14deg); } 40% { transform: rotate(12deg); } 60% { transform: rotate(-8deg); } 80% { transform: rotate(4deg); } }
@keyframes tab-compass { 0% { transform: rotate(0); } 30% { transform: rotate(160deg); } 55% { transform: rotate(-40deg); } 75% { transform: rotate(20deg); } 100% { transform: rotate(0); } }
@keyframes tab-gear { from { transform: rotate(0); } to { transform: rotate(180deg); } }
@media (prefers-reduced-motion: reduce) {
  .tab, .tab-icon { transition: none; }
  .tab.active .tab-icon { animation: none !important; }
}

/* ---------- Assistant rows on narrow screens: label above a never-wrapping amount ---------- */
.assist-rank-row .card-row-top { align-items: flex-start; }
.assist-rank-row .card-row-title { flex: 1 1 auto; min-width: 0; }
.assist-amount { display: flex; flex-direction: column; align-items: flex-end; flex: 0 0 auto; white-space: nowrap; font-size: 16px; line-height: 1.25; }
.assist-amount small { font-size: 11px; font-weight: 600; opacity: 0.85; }
