:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #d03b3b;
  --good-text: #006300;
  --bad-text: #b02a2a;
  /* series: รายรับ = slot 1 (blue), รายจ่าย = slot 2 (orange) */
  --series-income: #2a78d6;
  --series-expense: #eb6834;
  --grid: #e8e7e3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #9a9990;
    --accent: #3987e5;
    --danger: #e66767;
    --good-text: #5fd35f;
    --bad-text: #ff8a8a;
    --series-income: #3987e5;
    --series-expense: #d95926;
    --grid: #2c2c29;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #33332f;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #9a9990;
  --accent: #3987e5;
  --danger: #e66767;
  --good-text: #5fd35f;
  --bad-text: #ff8a8a;
  --series-income: #3987e5;
  --series-expense: #d95926;
  --grid: #2c2c29;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "IBM Plex Sans Thai", system-ui, sans-serif;
}
[hidden] { display: none !important; }
h1 { font-size: 22px; margin: 0 0 8px; }
h2 { font-size: 15px; font-weight: 600; margin: 0 0 12px; color: var(--text-2); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.spacer { flex: 1; }

input, select, button { font: inherit; color: inherit; }
input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-height: 38px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }

.btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  min-height: 38px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { min-height: 30px; padding: 3px 10px; font-size: 13px; }

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

/* ---------- login ---------- */
.center-view { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(420px, 100%); display: flex; flex-direction: column; gap: 14px; }
.login-card p { margin: 0; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; }
.tabs { display: flex; gap: 4px; overflow-x: auto; }
.tabs button {
  border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--text-2);
}
.tabs button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.user { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 14px; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); }

main { padding: 20px 24px 48px; max-width: 1280px; margin: 0 auto; }
.tab { display: flex; flex-direction: column; gap: 16px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.toolbar .grow { flex: 1; min-width: 200px; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi-label { font-size: 13px; color: var(--text-2); }
.kpi-value { font-size: clamp(18px, 2.2vw, 24px); font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap; }
.kpi-value.pos { color: var(--good-text); }
.kpi-value.neg { color: var(--bad-text); }

.chart-wrap { position: relative; height: 280px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* horizontal bars (HTML) */
.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.hbar-name { color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { height: 10px; }
.hbar-fill { height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.hbar-value { font-variant-numeric: tabular-nums; color: var(--text); }
.empty { color: var(--muted); font-size: 13px; padding: 12px 0; }

/* ---------- wallets ---------- */
.wallet-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.wallet-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.wallet-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-weight: 500; }
.wallet-balance { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; margin: 6px 0 4px; }
.wallet-balance.neg { color: var(--bad-text); }
.wallet-meta { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.table-title { padding: 14px 16px 0; margin: 0 0 10px; }

/* ---------- budgets ---------- */
.budget-list { display: flex; flex-direction: column; gap: 12px; }
.budget-row { display: grid; grid-template-columns: minmax(110px, 30%) 1fr auto; gap: 12px; align-items: center; font-size: 13px; }
.budget-track { height: 10px; background: var(--surface-2); border-radius: 5px; position: relative; overflow: hidden; }
.budget-fill { height: 100%; border-radius: 5px; background: var(--series-expense); }
.budget-mark { position: absolute; top: 0; bottom: 0; left: 80%; width: 2px; background: var(--surface); }
.budget-value { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--text-2); }
.budget-status { font-weight: 600; color: var(--bad-text); }

/* ---------- tables ---------- */
.table-card { padding: 0; overflow: hidden; }
.table-card > p { padding: 0 16px 12px; margin: 0; }
.table-scroll { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { font-weight: 500; color: var(--text-2); background: var(--surface-2); white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tfoot td { font-weight: 600; background: var(--surface-2); }
.table input, .table select { min-height: 32px; padding: 4px 8px; width: 100%; min-width: 90px; }
.amount-income { color: var(--good-text); }
.amount-expense { color: var(--bad-text); }
.type-pill { display: inline-flex; align-items: center; gap: 6px; }
.type-pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--series-expense); }
.type-pill.income::before { background: var(--series-income); }
.avatar { width: 28px; height: 28px; border-radius: 50%; vertical-align: middle; margin-right: 8px; object-fit: cover; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0;
  background: var(--surface); color: var(--text);
  width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.dialog-form { display: flex; flex-direction: column; gap: 12px; padding: 20px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 4px; }
.seg { display: flex; gap: 8px; }
.seg label { flex-direction: row; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 14px; color: var(--text); }
.seg label:has(input:checked) { border-color: var(--accent); background: var(--surface-2); }

.history { border-top: 1px solid var(--border); padding-top: 10px; font-size: 13px; display: flex; flex-direction: column; gap: 6px; }
.history-title { font-weight: 600; color: var(--text-2); }
.history-item { color: var(--text-2); }
.history-item .muted { font-size: 12px; }
label.check { flex-direction: row; align-items: center; gap: 8px; min-height: 38px; color: var(--text); font-size: 14px; }
label.check input { min-height: auto; width: 18px; height: 18px; }
.join-code { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 17px; font-weight: 600; letter-spacing: 2px; }
.edited-mark { font-size: 12px; color: var(--muted); cursor: help; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s;
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .topbar { padding: 10px 16px; gap: 8px; }
  .user { margin-left: 0; width: 100%; justify-content: flex-end; order: -1; }
  .brand { display: none; }
  main { padding: 16px 16px 40px; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 19px; }
  .grid-2 { grid-template-columns: 1fr; }
  .toolbar > label, .toolbar > .btn { flex: 1 1 140px; }
}
