:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --brand: #1f4e79;
  --brand-ink: #ffffff;
  --brand-soft: #e6eef7;
  --ok: #15803d;   --ok-soft: #dcfce7;
  --warn: #b45309; --warn-soft: #fef3c7;
  --bad: #b91c1c;  --bad-soft: #fee2e2;
  --info: #1d4ed8; --info-soft: #dbeafe;
  --in: #0f766e;   --out: #9a3412;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 1px 3px rgba(15,23,42,.05);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220; --surface: #111a2b; --surface-2: #0f1726; --line: #1f2a3d; --line-strong: #2c3a52;
    --text: #e5e9f0; --muted: #94a3b8; --brand: #5b9bd5; --brand-ink: #0b1220; --brand-soft: #16263d;
    --ok: #4ade80; --ok-soft: #12301f; --warn: #fbbf24; --warn-soft: #36290c;
    --bad: #f87171; --bad-soft: #3a1515; --info: #93c5fd; --info-soft: #172a4a;
    --in: #5eead4; --out: #fdba74; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220; --surface: #111a2b; --surface-2: #0f1726; --line: #1f2a3d; --line-strong: #2c3a52;
  --text: #e5e9f0; --muted: #94a3b8; --brand: #5b9bd5; --brand-ink: #0b1220; --brand-soft: #16263d;
  --ok: #4ade80; --ok-soft: #12301f; --warn: #fbbf24; --warn-soft: #36290c;
  --bad: #f87171; --bad-soft: #3a1515; --info: #93c5fd; --info-soft: #172a4a;
  --in: #5eead4; --out: #fdba74; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, system-ui, "IBM Plex Sans Thai", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
.r { text-align: right; }
.grow { flex: 1; }
.num { font-variant-numeric: tabular-nums; }

/* header */
.top { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 12px; padding: 14px 24px 6px; max-width: 1320px; margin: 0 auto; }
.logo { width: 36px; height: 36px; border-radius: 9px; background: var(--brand); display: grid; place-items: center; flex: none; }
.logo svg { width: 22px; height: 22px; fill: none; stroke: var(--brand-ink); stroke-width: 2.2; stroke-linejoin: round; }
h1 { font-size: 18px; margin: 0; line-height: 1.2; }
.sub { margin: 0; color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 2px; padding: 0 16px; max-width: 1320px; margin: 0 auto; overflow-x: auto; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; padding: 10px 14px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 500;
}
.tabs button:hover { color: var(--text); }
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); }

main { max-width: 1320px; margin: 0 auto; padding: 20px 24px 64px; }
.tab { display: none; }
.tab.on { display: block; }
h2 { font-size: 17px; margin: 0 0 14px; }
.help { color: var(--muted); font-size: 13px; margin: 4px 0 12px; }
.banner { background: var(--bad-soft); color: var(--bad); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 16px; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px;
  box-shadow: var(--shadow); text-align: left; display: flex; flex-direction: column; gap: 2px;
}
button.kpi { cursor: pointer; }
button.kpi:hover { border-color: var(--line-strong); }
button.kpi.active { outline: 2px solid var(--brand); outline-offset: -1px; }
.kpi span { color: var(--muted); font-size: 13px; }
.kpi b { font-size: 22px; font-variant-numeric: tabular-nums; }
.kpi.warn b { color: var(--warn); } .kpi.bad b { color: var(--bad); } .kpi.info b { color: var(--info); }

/* toolbar & inputs */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
input, select {
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px; padding: 7px 10px; min-height: 38px;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
input[type="search"] { min-width: 240px; }
.inl { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.chk input { min-height: auto; }
.btn {
  border: 1px solid transparent; border-radius: 8px; padding: 8px 14px; min-height: 38px; cursor: pointer; font-weight: 500;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.primary:disabled { opacity: .55; cursor: progress; }
.btn.ghost { background: var(--surface); border-color: var(--line-strong); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--surface); border-color: var(--bad); color: var(--bad); }
.btn.sm { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 18px; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--bad-soft); color: var(--bad); }

/* tables */
.tablewrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
.grid th, .grid td { padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.grid th { background: var(--surface-2); color: var(--muted); font-weight: 500; font-size: 13px; text-align: left; position: sticky; top: 0; }
.grid th.r { text-align: right; }
.grid td.r { font-variant-numeric: tabular-nums; }
.grid tbody tr:last-child td { border-bottom: 0; }
.grid tbody tr.click { cursor: pointer; }
.grid tbody tr.click:hover td { background: var(--surface-2); }
.grid td.name { white-space: normal; min-width: 180px; }
.grid code, .code { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 13px; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.pill { display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 500; }
.pill.OK { background: var(--ok-soft); color: var(--ok); }
.pill.LOW { background: var(--warn-soft); color: var(--warn); }
.pill.OUT { background: var(--bad-soft); color: var(--bad); }
.pill.OVER { background: var(--info-soft); color: var(--info); }
.pill.off { background: var(--surface-2); color: var(--muted); }

/* level bar */
.lvl { min-width: 170px; }
.bar { position: relative; height: 8px; border-radius: 4px; background: var(--surface-2); border: 1px solid var(--line); }
.bar .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 3px; background: var(--ok); }
.bar .fill.LOW { background: var(--warn); } .bar .fill.OUT { background: var(--bad); } .bar .fill.OVER { background: var(--info); }
.bar .mk { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--muted); }
.lvl-txt { font-size: 12px; color: var(--muted); margin-top: 3px; font-variant-numeric: tabular-nums; }

/* doc forms */
.doc { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.fields { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 8px; }
.fields label, .dlg label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.fields label input, .dlg label input, .dlg label select { color: var(--text); font-size: 15px; }
.fields .wide { grid-column: 1 / -1; }
.lines { margin-top: 12px; }
.lines th { text-align: left; font-weight: 500; font-size: 13px; color: var(--muted); padding: 6px 6px; border-bottom: 1px solid var(--line); }
.lines th.r { text-align: right; }
.lines td { padding: 6px; vertical-align: top; border-bottom: 1px solid var(--line); }
.lines td input { width: 100%; }
.lines td.item { min-width: 280px; width: 40%; }
.lines td.n input { text-align: right; font-variant-numeric: tabular-nums; min-width: 100px; }
.lines td.ro { text-align: right; padding-top: 14px; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.lines .meta { font-size: 12px; color: var(--muted); margin-top: 3px; }
.lines .meta.err { color: var(--bad); }
.lines input.err { border-color: var(--bad); }
.docfoot { display: flex; gap: 12px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.total { color: var(--muted); }
.total b { color: var(--text); font-size: 18px; font-variant-numeric: tabular-nums; }

/* doc list */
.doclist { display: flex; flex-direction: column; gap: 10px; }
.dcard { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.dcard.voided { opacity: .6; }
.dhead { display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer; flex-wrap: wrap; }
.dhead .t { font-weight: 600; font-size: 12px; padding: 2px 8px; border-radius: 6px; }
.t.IN { background: var(--ok-soft); color: var(--in); }
.t.OUT { background: var(--warn-soft); color: var(--out); }
.t.ADJ { background: var(--info-soft); color: var(--info); }
.t.VOID { background: var(--surface-2); color: var(--muted); }
.dhead .ref { color: var(--muted); font-size: 14px; flex: 1; min-width: 200px; }
.dhead .ref b { color: var(--text); font-weight: 500; }
.dhead .amt { font-variant-numeric: tabular-nums; font-weight: 600; }
.dbody { border-top: 1px solid var(--line); padding: 4px 14px 12px; display: none; }
.dcard.open .dbody { display: block; }
.dbody table td, .dbody table th { padding: 6px 8px; font-size: 14px; border-bottom: 1px solid var(--line); }
.dbody table th { color: var(--muted); font-weight: 500; text-align: left; font-size: 12.5px; }
.dbody .acts { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.strike { text-decoration: line-through; }

/* dialog */
.dlg { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.dlg::backdrop { background: rgba(15,23,42,.45); }
.dlg form { padding: 20px; }
.dlg h3 { margin: 0 0 14px; font-size: 17px; }
#dlg-body { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
#dlg-body .wide { grid-column: 1 / -1; }
#dlg-body .note { grid-column: 1 / -1; color: var(--muted); font-size: 13px; margin: 0; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.dlg.wide-dlg { width: min(920px, calc(100vw - 32px)); }
.dlg .scroll { grid-column: 1 / -1; max-height: 50vh; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.dlg .stats { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.dlg .stats div { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; color: var(--muted); }
.dlg .stats b { display: block; color: var(--text); font-size: 16px; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; transition: .2s; max-width: calc(100vw - 32px); z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--bad); color: #fff; }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpis .kpi:first-child { grid-column: 1 / -1; }
  .fields { grid-template-columns: 1fr; }
  .lines thead { display: none; }
  .lines tr { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
  .lines td { border: 0; padding: 0; }
  .lines td.item { grid-column: 1 / -1; width: auto; min-width: 0; }
  .lines td.ro { padding-top: 8px; text-align: left; }
  .lines td.ro::before { content: attr(data-label) " "; }
  .lines td.n input { min-width: 0; }
  .lines td.x { grid-column: 2; justify-self: end; }
}
@media (max-width: 600px) {
  .brand { padding: 12px 16px 4px; }
  .tabs { padding: 0 8px; }
  main { padding: 16px 16px 64px; }
  input[type="search"] { min-width: 0; width: 100%; }
  .toolbar > * { flex: 1 1 auto; }
  .toolbar .grow { display: none; }
  #dlg-body { grid-template-columns: 1fr; }
  .dlg .stats { grid-template-columns: 1fr 1fr; }
}
