/* ============================================================================
   คู่บัญชี — design tokens
   ============================================================================ */
:root {
  --bg: #F5F7F8;
  --surface: #FFFFFF;
  --surface-sunken: #ECF0F1;
  --border: #DDE3E6;
  --text: #16211F;
  --text-muted: #5B6B6B;
  --text-faint: #8B9998;

  --accent: #0F7A6B;
  --accent-strong: #0B5C51;
  --accent-soft: #E1F2EE;
  --accent-contrast: #FFFFFF;

  --good: #2FA36B;
  --good-soft: #E4F5EC;
  --warn: #C97A1E;
  --warn-soft: #FBEDDC;
  --danger: #C94A4A;
  --danger-soft: #FBE6E6;

  --radius-s: 8px;
  --radius-m: 14px;
  --radius-l: 22px;
  --shadow-card: 0 1px 2px rgba(20, 30, 28, 0.04), 0 8px 24px -12px rgba(20, 30, 28, 0.14);

  --font-display: "Bai Jamjuree", "IBM Plex Sans Thai", "Segoe UI", sans-serif;
  --font-body: "IBM Plex Sans Thai", "IBM Plex Sans", "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", "IBM Plex Sans Thai", monospace;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101614;
    --surface: #17201D;
    --surface-sunken: #1E2A26;
    --border: #2A3733;
    --text: #E9F1EE;
    --text-muted: #9DB0AB;
    --text-faint: #6E8681;

    --accent: #35C9AE;
    --accent-strong: #57DCC2;
    --accent-soft: #1B3A34;
    --accent-contrast: #06211C;

    --good: #4FCB8C;
    --good-soft: #163A2A;
    --warn: #E3A24A;
    --warn-soft: #3D2E14;
    --danger: #E37878;
    --danger-soft: #3D1E1E;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #101614;
  --surface: #17201D;
  --surface-sunken: #1E2A26;
  --border: #2A3733;
  --text: #E9F1EE;
  --text-muted: #9DB0AB;
  --text-faint: #6E8681;

  --accent: #35C9AE;
  --accent-strong: #57DCC2;
  --accent-soft: #1B3A34;
  --accent-contrast: #06211C;

  --good: #4FCB8C;
  --good-soft: #163A2A;
  --warn: #E3A24A;
  --warn-soft: #3D2E14;
  --danger: #E37878;
  --danger-soft: #3D1E1E;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
h1, h2, h3, .display { font-family: var(--font-display); text-wrap: balance; letter-spacing: 0.01em; }
.num, .amount { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }
button { font-family: inherit; }
img { max-width: 100%; }

a { color: var(--accent); }

/* ---------- layout shells ---------- */
.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
  padding-block: max(32px, env(safe-area-inset-top, 0px)) max(32px, env(safe-area-inset-bottom, 0px));
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-card);
  padding: 32px 28px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 22px;
}
.brand-mark {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-contrast);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.brand-name { font-size: 20px; font-weight: 700; }
.brand-tag { color: var(--text-muted); font-size: 13px; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 11px 13px;
  border-radius: var(--radius-s);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
  padding: 12px 16px;
  border-radius: var(--radius-s);
  border: 1px solid transparent;
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  transition: transform .05s ease, opacity .15s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-secondary { background: var(--surface-sunken); color: var(--text); border-color: var(--border); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-sm { width: auto; padding: 8px 14px; font-size: 13px; }

.switcher { display: flex; gap: 8px; margin-bottom: 18px; background: var(--surface-sunken); border-radius: var(--radius-s); padding: 4px; }
.switcher button {
  flex: 1; padding: 8px; border: none; background: transparent; border-radius: 6px;
  color: var(--text-muted); font-weight: 600; font-size: 13px; cursor: pointer;
}
.switcher button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.08); }

.error-msg {
  background: var(--danger-soft); color: var(--danger);
  padding: 10px 12px; border-radius: var(--radius-s);
  font-size: 13px; margin-bottom: 14px;
}
.hint { color: var(--text-faint); font-size: 12.5px; margin-top: 10px; text-align: center; }
.divider-row { display: flex; align-items: center; gap: 10px; margin: 18px 0; color: var(--text-faint); font-size: 12px; }
.divider-row::before, .divider-row::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.code-display {
  font-family: var(--font-mono);
  font-size: 26px;
  letter-spacing: 0.12em;
  text-align: center;
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-radius: var(--radius-s);
  padding: 14px;
  margin: 14px 0;
}

/* ---------- app shell ---------- */
.app-shell { max-width: 880px; margin: 0 auto; padding: 0 16px 96px; padding-top: max(16px, env(safe-area-inset-top, 0px)); }
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top, 0px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  margin: 0 -16px 4px;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-title { font-weight: 700; font-size: 16px; }
.topbar-sub { font-size: 12px; color: var(--text-muted); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-muted); font-size: 16px;
}

.month-nav { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 6px 0 18px; }
.month-nav button { background: var(--surface); border: 1px solid var(--border); width: 32px; height: 32px; border-radius: 8px; cursor: pointer; color: var(--text-muted); }
.month-nav .month-label { font-weight: 700; font-family: var(--font-display); font-size: 15px; min-width: 130px; text-align: center; }

.summary-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 18px; }
.stat-tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); padding: 14px; box-shadow: var(--shadow-card); }
.stat-tile .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-tile .value { font-size: 19px; font-weight: 700; }
.stat-tile .value.over { color: var(--danger); }
.stat-tile .value.ok { color: var(--good); }

.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 24px 0 10px; }
.section-head h2 { font-size: 15px; margin: 0; }
.section-head .link-btn { background: none; border: none; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; padding: 0; }
.text-link { background: none; border: none; font-size: 11.5px; font-weight: 600; cursor: pointer; padding: 0; color: var(--accent); }
.text-link.muted { color: var(--text-muted); }
.text-link.danger { color: var(--danger); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-card); padding: 16px; }

.chart-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 640px) { .chart-row { grid-template-columns: 1fr; } .summary-grid { grid-template-columns: 1fr 1fr; } }
.chart-card canvas { max-height: 220px; }
.chart-empty { color: var(--text-faint); font-size: 13px; text-align: center; padding: 40px 0; }

.budget-list { display: flex; flex-direction: column; gap: 12px; }
.budget-row .top-line { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 6px; }
.budget-row .cat-name { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.budget-bar { height: 8px; border-radius: 99px; background: var(--surface-sunken); overflow: hidden; }
.budget-bar > span { display: block; height: 100%; border-radius: 99px; background: var(--accent); transition: width .3s ease; }
.budget-bar > span.warn { background: var(--warn); }
.budget-bar > span.danger { background: var(--danger); }

.expense-list { display: flex; flex-direction: column; }
.expense-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 4px; border-bottom: 1px solid var(--border);
}
.expense-row:last-child { border-bottom: none; }
.expense-icon {
  width: 38px; height: 38px; border-radius: 11px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.expense-mid { flex: 1; min-width: 0; }
.expense-note { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense-meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.expense-amount { font-weight: 700; font-size: 14.5px; white-space: nowrap; }
.pill { font-size: 10.5px; padding: 2px 7px; border-radius: 99px; background: var(--surface-sunken); color: var(--text-muted); }
.pill.recurring { background: var(--accent-soft); color: var(--accent-strong); }
.pill-btn { font-size: 10.5px; padding: 2px 7px; border-radius: 99px; border: none; cursor: pointer; background: var(--surface-sunken); color: var(--text-muted); }
.pill-btn.spending { background: var(--warn-soft); color: var(--warn); }
.pill-btn.saving { background: var(--good-soft); color: var(--good); }
.pill-btn.earmarked { background: var(--accent-soft); color: var(--accent-strong); }
.who-avatar {
  width: 18px; height: 18px; border-radius: 99px; background: var(--accent-soft); color: var(--accent-strong);
  font-size: 9.5px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.row-delete { background: none; border: none; color: var(--text-faint); cursor: pointer; padding: 6px; font-size: 15px; }

.recurring-banner {
  background: var(--warn-soft); color: var(--warn); border-radius: var(--radius-m);
  padding: 12px 14px; display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; gap: 10px; font-size: 13px;
}
.recurring-banner button { flex-shrink: 0; }

.empty-state { text-align: center; color: var(--text-faint); padding: 36px 12px; font-size: 13.5px; }

/* ---------- FAB + modal ---------- */
.fab {
  position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: var(--accent-contrast);
  border: none; font-size: 26px; box-shadow: 0 10px 24px -8px color-mix(in srgb, var(--accent) 60%, transparent);
  cursor: pointer; z-index: 30;
  display: flex; align-items: center; justify-content: center;
}

.modal-overlay {
  position: fixed; inset: 0; background: rgba(10, 16, 14, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 40;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; } }
.modal-sheet {
  width: 100%; max-width: 440px; background: var(--surface);
  border-radius: 22px 22px 0 0; padding: 22px 20px;
  padding-bottom: max(22px, env(safe-area-inset-bottom, 0px));
  max-height: 88dvh; overflow-y: auto;
}
@media (min-width: 640px) { .modal-sheet { border-radius: var(--radius-l); } }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-head h3 { margin: 0; font-size: 17px; }
.close-x { background: none; border: none; font-size: 20px; color: var(--text-muted); cursor: pointer; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 8px 12px; border-radius: 99px; border: 1px solid var(--border);
  background: var(--surface); font-size: 13px; cursor: pointer; display: flex; gap: 6px; align-items: center;
}
.chip.selected { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); font-weight: 600; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 4px 0; }
.switch { position: relative; width: 42px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; background: var(--surface-sunken); border-radius: 99px; transition: .2s; border: 1px solid var(--border); }
.switch .track::before { content: ""; position: absolute; width: 18px; height: 18px; left: 2px; top: 2px; background: var(--surface); border-radius: 50%; transition: .2s; box-shadow: 0 1px 2px rgba(0,0,0,0.2); }
.switch input:checked + .track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .track::before { transform: translateX(18px); background: var(--accent-contrast); }

.tabbar { position: sticky; bottom: 0; display: flex; gap: 3px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); padding: 6px; margin-top: 20px; }
.tabbar button { flex: 1; min-width: 0; border: none; background: transparent; padding: 8px 2px; border-radius: 10px; font-size: 11.5px; line-height: 1.25; color: var(--text-muted); cursor: pointer; white-space: normal; word-break: keep-all; }
.tabbar button.active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 700; }
@media (max-width: 480px) { .tabbar button { font-size: 10px; padding: 7px 1px; } }

.drag-handle { display: flex; flex-direction: column; gap: 3px; justify-content: center; align-items: center; cursor: grab; touch-action: none; padding: 10px 7px; flex-shrink: 0; align-self: stretch; }
.drag-handle span { display: block; width: 15px; height: 2px; border-radius: 1px; background: var(--text-faint); }
.drag-handle:active { cursor: grabbing; }
.account-card.dragging, .recurring-item.dragging { box-shadow: 0 8px 20px rgba(0,0,0,0.2); background: var(--surface); border-radius: var(--radius-m); }

.loading-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: currentColor; animation: pulse 1s infinite ease-in-out; }
@keyframes pulse { 0%,100% { opacity: 0.3; } 50% { opacity: 1; } }

.account-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-m); box-shadow: var(--shadow-card); padding: 16px; }
.account-card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.account-card-head .bank-name { font-weight: 700; font-size: 15px; }
.account-card-head .account-label { font-size: 12.5px; color: var(--text-muted); }
.account-card-head .balance { font-size: 19px; font-weight: 700; text-align: right; }
.account-card-head .rate-pill { font-size: 10.5px; color: var(--text-muted); }
.account-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.ledger-mini { max-height: 180px; overflow-y: auto; border-top: 1px solid var(--border); margin-top: 4px; }
.ledger-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 2px; font-size: 12.5px; border-bottom: 1px solid var(--border); }
.ledger-row:last-child { border-bottom: none; }
.ledger-row .ld-desc { color: var(--text); font-weight: 500; }
.ledger-row .ld-date { color: var(--text-faint); font-size: 11px; }
.ledger-row .ld-amt.pos { color: var(--good); }
.ledger-row .ld-amt.neg { color: var(--danger); }
.ledger-empty { color: var(--text-faint); font-size: 12.5px; padding: 12px 2px; text-align: center; }

.recurring-list { display: flex; flex-direction: column; gap: 8px; }
.recurring-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 4px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.recurring-item:last-child { border-bottom: none; }
.recurring-item .rt-meta { color: var(--text-muted); font-size: 12px; }
