:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --raised: #ffffff;
  --ink: #0b0b0b;
  --ink2: #52514e;
  --muted: #7a7873;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, .10);
  --wash: rgba(11, 11, 11, .05);
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e3f2ef;
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s1-track: #dbe9fb;
  --good: #0ca30c;
  --good-text: #006300;
  --warn: #fab219;
  --warn-text: #8a5a00;
  --warn-track: #fdefcb;
  --crit: #d03b3b;
  --crit-text: #b42318;
  --crit-track: #f7dada;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 10px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --raised: #222220;
    --ink: #f0efec;
    --ink2: #c3c2b7;
    --muted: #9a988f;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, .10);
    --wash: rgba(255, 255, 255, .06);
    --accent: #2bb3a3;
    --accent-ink: #0b0b0b;
    --accent-soft: #12302c;
    --s1: #3987e5;
    --s2: #d95926;
    --s3: #199e70;
    --s1-track: #1b2f4a;
    --good-text: #3fc23f;
    --warn-text: #fab219;
    --warn-track: #3a2f12;
    --crit-text: #f07b7b;
    --crit-track: #3d1d1d;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --raised: #222220; --ink: #f0efec; --ink2: #c3c2b7; --muted: #9a988f;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, .10); --wash: rgba(255, 255, 255, .06);
  --accent: #2bb3a3; --accent-ink: #0b0b0b; --accent-soft: #12302c;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s1-track: #1b2f4a;
  --good-text: #3fc23f; --warn-text: #fab219; --warn-track: #3a2f12; --crit-text: #f07b7b; --crit-track: #3d1d1d; --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--page); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", "Leelawadee UI", "Noto Sans Thai", "Sarabun", sans-serif;
  font-size: 15px; line-height: 1.5;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px 22px; text-align: center; box-shadow: var(--shadow);
}
.login-icon { border-radius: 14px; }
.login-card h1 { margin: 10px 0 2px; font-size: 22px; }
.login-card p { margin: 4px 0; }
#pin {
  width: 100%; margin: 16px 0 10px; padding: 12px; font-size: 26px; letter-spacing: 10px; text-align: center;
  border: 1px solid var(--axis); border-radius: 10px; background: var(--raised);
}
#pin:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn-primary {
  width: 100%; padding: 11px; border: 0; border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  font-weight: 600; font-size: 16px;
}
.btn-primary:disabled { opacity: .6; cursor: default; }
.login-msg { min-height: 1.5em; color: var(--crit-text); font-size: 14px; }

/* ---------- top bar ---------- */
.top {
  position: sticky; top: 0; z-index: 20; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { grid-column: 1; display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; white-space: nowrap; min-width: 0; }
.brand img { border-radius: 7px; }
.month-nav { grid-column: 2; display: flex; align-items: center; gap: 2px; }
#mLabel { min-width: 118px; text-align: center; font-weight: 600; white-space: nowrap; }
.top-actions { grid-column: 3; justify-self: end; display: flex; align-items: center; gap: 2px; position: relative; }
@media (max-width: 480px) { .brand span { display: none; } #mLabel { min-width: 104px; } }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border: 0; border-radius: 9px;
  background: transparent; font-size: 22px; line-height: 1; color: var(--ink2);
}
.icon-btn:hover { background: var(--wash); }
.icon-btn:disabled { opacity: .3; cursor: default; background: transparent; }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
.menu {
  position: absolute; right: 0; top: 42px; min-width: 210px; background: var(--raised); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 8px 30px rgba(0, 0, 0, .15); padding: 6px; display: grid;
}
.menu a, .menu button {
  display: block; text-align: left; padding: 9px 12px; border: 0; background: none; border-radius: 8px;
  text-decoration: none; color: var(--ink); font-size: 14.5px;
}
.menu a:hover, .menu button:hover { background: var(--wash); }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  display: grid; justify-items: center; gap: 1px; padding: 7px 0 6px; border: 0; background: none; color: var(--muted);
  font-size: 11.5px;
}
.tabs svg { width: 22px; height: 22px; }
.tabs button[aria-selected="true"] { color: var(--accent); font-weight: 600; }
main { max-width: 980px; margin: 0 auto; padding: 14px 14px 96px; outline: none; }
.foot { max-width: 980px; margin: -80px auto 0; padding: 0 14px 92px; color: var(--muted); font-size: 12px; text-align: center; }
@media (min-width: 760px) {
  .tabs {
    position: sticky; top: 53px; bottom: auto; display: flex; justify-content: center; gap: 6px;
    border-top: 0; border-bottom: 1px solid var(--border); padding: 6px 0;
  }
  .tabs button { display: flex; align-items: center; gap: 6px; padding: 6px 14px; border-radius: 999px; font-size: 14px; }
  .tabs svg { width: 18px; height: 18px; }
  .tabs button[aria-selected="true"] { background: var(--accent-soft); }
  main { padding: 20px 20px 40px; }
  .foot { margin-top: -20px; padding-bottom: 24px; }
}

/* ---------- cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px;
  margin: 0 0 14px; box-shadow: var(--shadow);
}
.card > h2 { margin: 0 0 10px; font-size: 15.5px; display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.card > h2 .muted { font-weight: 400; font-size: 13px; }
.grid2 { display: grid; gap: 14px; }
@media (min-width: 760px) { .grid2 { grid-template-columns: 1fr 1fr; align-items: start; } }
.empty { color: var(--muted); padding: 6px 0; margin: 0; }

/* hero + tiles */
.hero .label { color: var(--ink2); font-size: 14px; }
.hero .value { font-size: 44px; font-weight: 700; line-height: 1.15; letter-spacing: -.5px; margin: 2px 0 4px; }
@media (min-width: 760px) { .hero .value { font-size: 52px; } }
.delta { font-size: 13.5px; color: var(--ink2); display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.delta .up { color: var(--crit-text); font-weight: 600; }
.delta .down { color: var(--good-text); font-weight: 600; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
@media (min-width: 760px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile { background: var(--raised); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.tile .t-label { font-size: 12.5px; color: var(--ink2); }
.tile .t-value { font-size: 19px; font-weight: 650; overflow-wrap: anywhere; }
.tile .t-sub { font-size: 12px; color: var(--muted); }

/* status chip */
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 600; padding: 1px 8px 1px 6px;
  border-radius: 999px; white-space: nowrap; background: var(--wash); color: var(--ink2); }
.chip svg { width: 13px; height: 13px; flex: none; }
.chip.crit { background: var(--crit-track); color: var(--crit-text); }
.chip.warn { background: var(--warn-track); color: var(--warn-text); }
.chip.good { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good-text); }
.chip.info { background: var(--s1-track); color: var(--ink); }

/* upcoming list */
.due-list { list-style: none; margin: 0; padding: 0; }
.due-list li { display: grid; grid-template-columns: 52px 1fr auto; gap: 10px; align-items: center; padding: 8px 0;
  border-top: 1px solid var(--border); }
.due-list li:first-child { border-top: 0; }
.due-date { text-align: center; line-height: 1.15; }
.due-date b { display: block; font-size: 19px; }
.due-date span { font-size: 11.5px; color: var(--muted); }
.due-name { min-width: 0; }
.due-name .n { font-weight: 600; overflow-wrap: anywhere; }
.due-amt { text-align: right; font-weight: 600; white-space: nowrap; }

/* horizontal bars (หมวด) */
.hbars { display: grid; gap: 2px; }
.hbar { display: grid; grid-template-columns: 104px 1fr 40px; align-items: center; gap: 8px; padding: 5px 4px;
  border: 0; background: none; text-align: left; border-radius: 8px; width: 100%; }
.hbar:hover, .hbar:focus-visible { background: var(--wash); }
.hb-label { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hb-track { display: flex; align-items: center; gap: 6px; min-width: 0; }
.hb-fill { flex: 0 0 calc((100% - 80px) * var(--f)); min-width: 3px; height: 14px; background: var(--s1); border-radius: 0 4px 4px 0; }
.hb-val { font-size: 13.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.hb-share { font-size: 12.5px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 420px) { .hbar { grid-template-columns: 84px 1fr 34px; } }

/* meter */
.meter-row { padding: 7px 0; border-top: 1px solid var(--border); }
.meter-row:first-child { border-top: 0; }
.meter-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; flex-wrap: wrap; margin-bottom: 5px; }
.meter-head .n { font-weight: 600; }
.meter-head .v { font-size: 13.5px; color: var(--ink2); }
.meter { position: relative; height: 8px; border-radius: 4px; background: var(--s1-track); }
.meter .m-fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--s1); }
.meter.warn { background: var(--warn-track); } .meter.warn .m-fill { background: var(--warn); }
.meter.crit { background: var(--crit-track); } .meter.crit .m-fill { background: var(--crit); }
.meter .m-tick { position: absolute; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; background: var(--ink2); border-radius: 1px; }
.meter-foot { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }

/* segmented bar (ใครจ่าย) */
.seg { display: flex; gap: 2px; height: 8px; border-radius: 4px; overflow: hidden; margin: 4px 0 10px; }
.seg span { min-width: 2px; }
.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.legend li { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.legend .amt { margin-left: auto; font-weight: 600; font-variant-numeric: tabular-nums; }
.legend .pct { color: var(--muted); font-size: 12.5px; width: 40px; text-align: right; font-variant-numeric: tabular-nums; }
.sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend-row { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13.5px; color: var(--ink2); margin-bottom: 6px; }
.legend-row span { display: inline-flex; align-items: center; gap: 6px; }
.bg1 { background: var(--s1); } .bg2 { background: var(--s2); } .bg3 { background: var(--s3); } .bg0 { background: var(--axis); }

/* svg chart */
.chart { width: 100%; overflow: hidden; }
.chart svg { display: block; touch-action: pan-y; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .f1 { fill: var(--s1); } .chart .f2 { fill: var(--s2); } .chart .f3 { fill: var(--s3); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover, .chart .hit.on { fill: var(--wash); }
.chart .cur { fill: var(--ink); font-weight: 700; }
details.table-toggle { margin-top: 8px; }
details.table-toggle summary { cursor: pointer; color: var(--accent); font-size: 13.5px; }

/* table */
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; margin-top: 6px; }
table.data th, table.data td { padding: 6px 6px; border-bottom: 1px solid var(--border); text-align: right; font-variant-numeric: tabular-nums; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 500; font-size: 12.5px; }

/* debt cards */
.debts { display: grid; gap: 12px; }
@media (min-width: 760px) { .debts { grid-template-columns: 1fr 1fr; } }
.debt { background: var(--raised); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.debt-top { display: flex; gap: 8px; align-items: flex-start; justify-content: space-between; }
.debt-name { font-weight: 700; font-size: 16px; overflow-wrap: anywhere; }
.debt-meta { font-size: 12.5px; color: var(--muted); }
.debt-left { font-size: 24px; font-weight: 700; margin: 6px 0 2px; }
.debt-left small { font-size: 13px; font-weight: 400; color: var(--muted); margin-left: 4px; }
.debt-facts { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; margin-top: 10px; font-size: 13px; }
.debt-facts div { display: flex; justify-content: space-between; gap: 6px; border-bottom: 1px dashed var(--border); padding-bottom: 3px; }
.debt-facts span { color: var(--muted); }
details.closed summary { cursor: pointer; color: var(--ink2); }

/* transactions */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.filters select, .filters input {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 9px; background: var(--raised); min-width: 0;
}
.filters input { flex: 1 1 160px; }
.seg-ctl { display: inline-flex; background: var(--wash); border-radius: 10px; padding: 2px; }
.seg-ctl button { border: 0; background: none; padding: 5px 12px; border-radius: 8px; font-size: 13.5px; color: var(--ink2); }
.seg-ctl button[aria-pressed="true"] { background: var(--raised); color: var(--ink); font-weight: 600; box-shadow: var(--shadow); }
.tx-sum { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13.5px; color: var(--ink2); margin: 2px 0 8px; }
.tx-sum b { color: var(--ink); }
.tx-day { margin-top: 10px; }
.tx-day h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 0 2px; display: flex; justify-content: space-between; }
.tx { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 8px 0; border-top: 1px solid var(--border); }
.tx:first-of-type { border-top: 0; }
.tx .d { font-weight: 550; overflow-wrap: anywhere; }
.tx .m { font-size: 12.5px; color: var(--muted); grid-column: 1; }
.tx .a { grid-row: 1 / span 2; grid-column: 2; align-self: center; text-align: right; font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.tx .a.inc { color: var(--good-text); }
.tx .a.self { color: var(--muted); font-weight: 500; }
.more { display: block; margin: 12px auto 0; padding: 8px 18px; border-radius: 999px; border: 1px solid var(--border); background: var(--raised); }

/* tooltip + loading */
.tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--raised); color: var(--ink); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 10px; font-size: 13px; box-shadow: 0 6px 24px rgba(0, 0, 0, .18); max-width: 260px;
}
.tip b { font-weight: 650; }
.tip .row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tip .row span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--ink2); }
.loading { position: fixed; inset: 0; z-index: 40; display: grid; place-content: center; justify-items: center; gap: 10px;
  background: color-mix(in srgb, var(--page) 80%, transparent); color: var(--ink2); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--grid); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { text-align: center; padding: 40px 10px; }
.error-box button { margin-top: 10px; padding: 8px 18px; border-radius: 999px; border: 1px solid var(--border); background: var(--raised); }
@media (prefers-reduced-motion: reduce) { .spinner, .icon-btn.spin svg { animation-duration: 3s; } }
