:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --good: #006300;
  --bad: #b42323;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4;
  --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --prev: #c3c2b7;
  --warn-bg: #fff4d6; --warn-ink: #6b4b00;
  --err-bg: #fde8e8; --err-ink: #8a1f1f;
  --ok-bg: #e5f4e5; --ok-ink: #0b4f0b;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --accent-ink: #86b6ef; --good: #0ca30c; --bad: #e66767;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
    --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --prev: #4a4a46;
    --warn-bg: #3a2f10; --warn-ink: #f5d48a; --err-bg: #3a1717; --err-ink: #f3b0b0; --ok-bg: #12301a; --ok-ink: #9ee09e;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-ink: #86b6ef; --good: #0ca30c; --bad: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
  --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --prev: #4a4a46;
  --warn-bg: #3a2f10; --warn-ink: #f5d48a; --err-bg: #3a1717; --err-ink: #f3b0b0; --ok-bg: #12301a; --ok-ink: #9ee09e;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font: 14px/1.5 var(--font); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
button, select, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 600; }
.company-pick { min-width: 0; max-width: 100%; }
.company-pick select { font-weight: 600; padding: 6px 10px; max-width: 100%; text-overflow: ellipsis; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; }
.tabs a {
  padding: 6px 12px; border-radius: 8px; color: var(--ink-2); text-decoration: none;
}
.tabs a:hover { background: var(--surface-2); }
.tabs a[aria-current="page"] { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.who { margin-left: auto; color: var(--muted); font-size: 12px; text-align: right; }

select, input[type="text"], input[type="number"], input[type="email"], textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
}
input[type="number"] { text-align: right; }

/* ---------- filters ---------- */
.filters {
  display: flex; gap: 16px; align-items: center; flex-wrap: wrap;
  padding: 12px 24px; color: var(--ink-2);
}
.filters label { display: inline-flex; gap: 6px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 12px; cursor: pointer; color: var(--ink-2); }
.seg button[aria-checked="true"] { background: var(--accent); color: #fff; }
.check { cursor: pointer; }
[hidden] { display: none !important; }

.view { padding: 0 24px 48px; }
.banner { margin: 0 24px 12px; padding: 10px 14px; border-radius: var(--radius); background: var(--warn-bg); color: var(--warn-ink); }

/* ---------- cards & layout ---------- */
.grid { display: grid; gap: 16px; }
.grid.tiles { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px;
  min-width: 0;
}
.card h2 { font-size: 15px; margin: 0 0 2px; font-weight: 600; }
.card .sub { color: var(--muted); font-size: 12px; margin: 0 0 12px; }
.section-title { font-size: 13px; color: var(--muted); font-weight: 600; margin: 24px 0 8px; text-transform: none; }
.chart-box { position: relative; height: 280px; }

.tile .label { color: var(--ink-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 600; margin: 2px 0 4px; }
.tile .meta { display: flex; flex-direction: column; gap: 2px; font-size: 12px; color: var(--muted); }
.delta { font-variant-numeric: tabular-nums; font-weight: 600; }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }
.delta.flat { color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.fin { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
table.fin th, table.fin td { padding: 6px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
table.fin thead th {
  position: sticky; top: 0; z-index: 2; background: var(--surface-2); color: var(--ink-2);
  font-weight: 600; font-size: 12px; text-align: right;
}
table.fin thead th.corner { text-align: left; }
table.fin td.num { text-align: right; }
table.fin td.name { min-width: 260px; }
table.fin thead tr.group th { text-align: center; border-bottom: 1px solid var(--grid); }
table.fin thead tr.group th.corner { vertical-align: bottom; }
table.fin thead tr.group + tr th { top: 33px; }
/* Wide tables (units × months): keep the account column visible while scrolling sideways. */
.table-wrap.wide { max-height: 78vh; }
.table-wrap.wide table.fin td.name { position: sticky; left: 0; z-index: 1; background: var(--surface); }
.table-wrap.wide table.fin thead th.corner { position: sticky; left: 0; z-index: 3; background: var(--surface-2); text-align: left; }
.table-wrap.wide table.fin tr.total td.name { background: var(--surface-2); }
.table-wrap.wide table.fin td.name { min-width: 240px; max-width: 320px; white-space: normal; box-shadow: 1px 0 0 var(--grid); }
table.fin th.unit-total, table.fin td.unit-total { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
table.fin tr.section td { font-weight: 600; cursor: pointer; }
table.fin tr.section td.name::before { content: "▸"; display: inline-block; width: 16px; color: var(--muted); }
table.fin tr.section.open td.name::before { content: "▾"; }
table.fin tr.sub td.name { padding-left: 34px; color: var(--ink-2); cursor: pointer; }
table.fin tr.sub td.name::before { content: "▸"; display: inline-block; width: 14px; color: var(--muted); }
table.fin tr.sub.open td.name::before { content: "▾"; }
table.fin tr.acct td { color: var(--ink-2); font-size: 13px; }
table.fin tr.acct td.name { padding-left: 34px; }
table.fin tr.acct.deep td.name { padding-left: 52px; }
table.fin tr.acct .code { color: var(--muted); margin-right: 6px; font-size: 12px; }
table.fin tr.total td { font-weight: 700; background: var(--surface-2); }
table.fin tr.metric td { color: var(--ink-2); font-style: normal; font-size: 13px; }
table.fin tr.metric td.name { padding-left: 26px; }
table.fin td.neg { color: var(--bad); }
table.fin td.dim, table.fin th.dim { color: var(--muted); }
table.fin th.sep, table.fin td.sep { border-left: 1px solid var(--grid); }
table.plain { border-collapse: collapse; width: 100%; }
table.plain th, table.plain td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.plain th { color: var(--ink-2); font-size: 12px; font-weight: 600; }
table.plain td.num, table.plain th.num { text-align: right; font-variant-numeric: tabular-nums; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 0 0 12px; }
.toolbar .spacer { flex: 1; }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 2px 8px; font-size: 12px; }

.msg { padding: 10px 14px; border-radius: var(--radius); margin: 0 0 12px; }
.msg.err { background: var(--err-bg); color: var(--err-ink); }
.msg.ok { background: var(--ok-bg); color: var(--ok-ink); }
.msg.warn { background: var(--warn-bg); color: var(--warn-ink); }

.drop {
  border: 2px dashed var(--axis); border-radius: var(--radius); padding: 32px; text-align: center; color: var(--ink-2);
  background: var(--surface); cursor: pointer;
}
.drop.over { border-color: var(--accent); background: var(--surface-2); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.months { display: grid; grid-template-columns: repeat(12, minmax(48px, 1fr)); gap: 6px; }
.months .m { border: 1px solid var(--border); border-radius: 8px; padding: 6px; text-align: center; font-size: 12px; background: var(--surface); }
.months .m.done { background: var(--ok-bg); color: var(--ok-ink); }
.months .m.legacy { background: var(--surface-2); color: var(--ink-2); }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin: 0 0 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
.legend i.line { height: 2px; border-radius: 1px; width: 14px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 10px; z-index: 20;
}
.empty { color: var(--muted); padding: 32px; text-align: center; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); font-size: 12px; }
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); }

@media (max-width: 720px) {
  .topbar, .filters, .view { padding-left: 16px; padding-right: 16px; }
  .banner { margin-left: 16px; margin-right: 16px; }
  .grid.two { grid-template-columns: 1fr; }
  .who { width: 100%; text-align: left; margin-left: 0; }
  .months { grid-template-columns: repeat(6, 1fr); }
}
@media print {
  .topbar nav, .filters, .toolbar { display: none; }
}
