/* สมุดภาษี — layout: sidebar ledger + work surface; print = black on white A4 */
:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --sunken: #f5f7f9;
  --ink: #18212c;
  --muted: #5a6776;
  --line: #d6dde5;
  --accent: #1f4e86;
  --accent-ink: #ffffff;
  --accent-soft: #e3ecf7;
  --ok: #1d7a4a;
  --warn: #9a6200;
  --void: #b3261e;
  --void-soft: #fbe9e7;
  --side: #17283d;
  --side-ink: #c9d6e6;
  --side-active: #ffffff;
  --f-ui: "IBM Plex Sans Thai", "Noto Sans Thai", Tahoma, sans-serif;
  --f-doc: "Sarabun", "TH Sarabun New", Tahoma, sans-serif;
  --f-num: "IBM Plex Mono", ui-monospace, Consolas, monospace;
  --r: 6px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f151c; --surface: #17202a; --sunken: #121a22; --ink: #e4eaf1; --muted: #93a1b2; --line: #2a3644;
    --accent: #7fb0eb; --accent-ink: #0d1722; --accent-soft: #1c2f46; --ok: #5cc48b; --warn: #e0a84a; --void: #ff8a80; --void-soft: #3a1f1e;
    --side: #0b1118; --side-ink: #9fb0c4; --side-active: #ffffff; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f151c; --surface: #17202a; --sunken: #121a22; --ink: #e4eaf1; --muted: #93a1b2; --line: #2a3644;
  --accent: #7fb0eb; --accent-ink: #0d1722; --accent-soft: #1c2f46; --ok: #5cc48b; --warn: #e0a84a; --void: #ff8a80; --void-soft: #3a1f1e;
  --side: #0b1118; --side-ink: #9fb0c4; --side-active: #ffffff; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.55 var(--f-ui); }
h1, h2, h3 { text-wrap: balance; margin: 0; line-height: 1.3; }
h1 { font-size: 1.45rem; font-weight: 600; }
h2 { font-size: 1.1rem; font-weight: 600; }
a { color: var(--accent); }
.num, td.n, th.n { font-family: var(--f-num); font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.boot { padding: 40px; color: var(--muted); }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 228px 1fr; min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); padding: 18px 12px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; overflow: auto; }
.brand { display: flex; align-items: baseline; gap: 8px; padding: 0 8px; color: var(--side-active); }
.brand b { font-size: 1.2rem; letter-spacing: .02em; }
.brand span { font-size: .72rem; letter-spacing: .12em; text-transform: uppercase; color: var(--side-ink); }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav .grp { font-size: .7rem; letter-spacing: .12em; text-transform: uppercase; opacity: .65; padding: 12px 10px 4px; }
.nav a { color: var(--side-ink); text-decoration: none; padding: 7px 10px; border-radius: var(--r); display: block; }
.nav a:hover { background: rgba(255,255,255,.06); color: var(--side-active); }
.nav a.on { background: rgba(255,255,255,.12); color: var(--side-active); font-weight: 600; }
.side .who { margin-top: auto; font-size: .82rem; padding: 0 8px; display: flex; flex-direction: column; gap: 6px; }
.side .who button { align-self: flex-start; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; padding: 12px max(16px, env(safe-area-inset-right, 0px)) 12px max(24px, env(safe-area-inset-left, 0px)); background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar label { display: flex; align-items: center; gap: 8px; font-size: .85rem; color: var(--muted); }
.topbar select, .topbar input { min-width: 0; }
.topbar .co { flex: 1 1 260px; }
.topbar .co select { flex: 1; max-width: 420px; font-weight: 600; color: var(--ink); }
.view { padding: 24px max(16px, env(safe-area-inset-right, 0px)) 48px max(24px, env(safe-area-inset-left, 0px)); display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.head { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; justify-content: space-between; }
.head .sub { color: var(--muted); font-size: .9rem; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* ---------- controls ---------- */
input, select, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 7px 10px; min-width: 0; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button, .btn { font: inherit; font-size: .92rem; cursor: pointer; border-radius: var(--r); padding: 7px 14px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); text-decoration: none; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
button:hover, .btn:hover { border-color: var(--accent); }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.danger { color: var(--void); }
.ghost { background: transparent; border-color: transparent; color: inherit; }
.side .ghost { color: var(--side-ink); border: 1px solid rgba(255,255,255,.18); padding: 4px 10px; font-size: .8rem; }
button:disabled { opacity: .5; cursor: default; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 18px; min-width: 0; }
.grid { display: grid; gap: 12px 16px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: .8rem; color: var(--muted); }
.field.wide { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .76rem; font-weight: 600; color: var(--muted); letter-spacing: .03em; background: var(--sunken); white-space: nowrap; }
tbody tr:hover { background: var(--sunken); }
tbody tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 600; border-top: 2px solid var(--line); border-bottom: 0; }
td.act { white-space: nowrap; text-align: right; }
td.act button { padding: 3px 9px; font-size: .82rem; }
tr.void td { color: var(--muted); text-decoration: line-through; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

.pill { display: inline-block; font-size: .74rem; padding: 1px 8px; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap; }
.pill.void { color: var(--void); background: var(--void-soft); border-color: transparent; text-decoration: none; }
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }

/* ---------- dashboard ---------- */
.ledger { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); overflow: hidden; }
.ledger > div { padding: 16px 18px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 2px; }
.ledger .k { font-size: .8rem; color: var(--muted); }
.ledger .v { font: 600 1.35rem/1.3 var(--f-num); font-variant-numeric: tabular-nums; }
.ledger .s { font-size: .8rem; color: var(--muted); }
.ledger .v.pos { color: var(--void); }
.ledger .v.neg { color: var(--ok); }
.cols { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 20px; }
.deadlines { display: flex; flex-direction: column; gap: 10px; }
.deadlines div { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px dashed var(--line); padding-bottom: 8px; }

/* ---------- doc form ---------- */
.lines td { padding: 6px; }
.lines input { width: 100%; }
.lines .c-qty { width: 90px; } .lines .c-unit { width: 90px; } .lines .c-price { width: 130px; } .lines .c-amt { width: 130px; }
.lines .c-prod { width: 230px; }
.lines td.amt { font-family: var(--f-num); text-align: right; padding-top: 13px; }
.totals { display: grid; grid-template-columns: 1fr auto; gap: 6px 24px; max-width: 420px; margin-left: auto; }
.totals .num { font-size: 1rem; }
.totals .grand { font-weight: 700; font-size: 1.1rem; border-top: 1px solid var(--line); padding-top: 6px; }
.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; }
.form-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end; position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; border-top: 1px solid var(--line); }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding-block: 24px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
.auth .panel { width: 100%; max-width: 480px; display: flex; flex-direction: column; gap: 14px; padding: 28px; }
.auth h1 { font-size: 1.6rem; }
.err { color: var(--void); font-size: .9rem; min-height: 1.2em; }

/* ---------- toast / dialog ---------- */
#toast { position: fixed; right: max(16px, env(safe-area-inset-right, 0px)); bottom: calc(16px + env(safe-area-inset-bottom, 0px)); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 50; max-width: calc(100vw - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.18); }
#toast.bad { background: var(--void); color: #fff; }
dialog { border: 1px solid var(--line); border-radius: 10px; padding: 0; background: var(--surface); color: var(--ink); width: min(620px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10,18,28,.45); }
#dlg-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }

/* ---------- report screen ---------- */
.paper { background: #fff; color: #111; border: 1px solid var(--line); border-radius: 4px; padding: 28px; overflow-x: auto; font-family: var(--f-doc); }
.paper table th { background: #f2f2f2; color: #111; }
.paper td, .paper th { border: 1px solid #999; color: #111; }
.paper tbody tr:hover { background: transparent; }

/* ---------- print overlay ---------- */
#print { position: fixed; inset: 0; z-index: 40; background: #8a939d; overflow: auto; padding-block: 0 40px; }
#print .bar { position: sticky; top: 0; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; padding: 12px max(16px, env(safe-area-inset-right, 0px)) 12px max(16px, env(safe-area-inset-left, 0px)); background: #2b3542; }
#print .bar button { background: #fff; color: #111; }
#print .bar .primary { background: #1f4e86; color: #fff; }
.sheet { background: #fff; color: #111; width: 210mm; max-width: calc(100% - 32px); min-height: 297mm; margin: 20px auto; padding: 14mm 14mm; font: 14px/1.45 var(--f-doc); box-shadow: 0 4px 20px rgba(0,0,0,.25); }
.sheet.land { width: 297mm; min-height: 210mm; }
.sheet table { width: 100%; border-collapse: collapse; }
.sheet th, .sheet td { border: 1px solid #555; padding: 4px 6px; color: #111; background: transparent; font-size: 13px; letter-spacing: 0; }
.sheet th { background: #eee; text-align: center; font-weight: 600; white-space: normal; }
.sheet .noborder td { border: 0; padding: 1px 0; }
.sheet .n { font-family: var(--f-doc); }
.sheet h1 { font-size: 20px; text-align: center; font-weight: 700; }
.sheet h2 { font-size: 16px; }
.doc-top { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; }
.doc-co b { font-size: 17px; }
.doc-title { text-align: right; }
.doc-title .t { font-size: 20px; font-weight: 700; }
.doc-title .copy { display: inline-block; border: 1.5px solid #111; padding: 0 10px; font-weight: 600; margin-top: 4px; }
.doc-meta { display: grid; grid-template-columns: 1fr 230px; gap: 0; border: 1px solid #555; margin: 12px 0; }
.doc-meta > div { padding: 8px 10px; }
.doc-meta > div + div { border-left: 1px solid #555; }
.doc-sum { display: grid; grid-template-columns: 1fr 260px; margin-top: -1px; }
.doc-sum .words { border: 1px solid #555; border-right: 0; padding: 8px 10px; display: flex; align-items: center; font-weight: 600; }
.doc-sum table td { font-size: 13px; }
.signs { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-top: 48px; text-align: center; }
.signs div { border-top: 1px dotted #111; padding-top: 6px; }
.void-stamp { color: #b3261e; border: 3px solid #b3261e; font-size: 28px; font-weight: 700; padding: 2px 20px; display: inline-block; transform: rotate(-8deg); }
.box { display: inline-block; width: 13px; height: 13px; border: 1px solid #111; vertical-align: -2px; margin-right: 4px; text-align: center; line-height: 11px; font-size: 12px; }
.w50 td { vertical-align: middle; }
.rpt-head { text-align: center; margin-bottom: 10px; }
.rpt-head div { font-size: 14px; }
.rpt-meta { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px 16px; margin: 8px 0; font-size: 13px; }
.page-break { break-after: page; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; padding: 12px max(16px, env(safe-area-inset-right, 0px)) 12px max(16px, env(safe-area-inset-left, 0px)); gap: 10px; }
  .nav { flex-direction: row; overflow-x: auto; gap: 4px; }
  .nav .grp { display: none; }
  .nav a { white-space: nowrap; }
  .side .who { flex-direction: row; align-items: center; justify-content: space-between; margin-top: 0; }
  .cols, .split { grid-template-columns: 1fr; }
  .topbar { padding-left: max(16px, env(safe-area-inset-left, 0px)); }
  .view { padding-left: max(16px, env(safe-area-inset-left, 0px)); }
  .doc-meta, .doc-sum { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

@page { size: A4; margin: 10mm; }
@page land { size: A4 landscape; margin: 8mm; }
@media print {
  body { background: #fff; }
  #app, #toast, #print .bar { display: none !important; }
  #print { position: static; background: #fff; padding: 0; overflow: visible; }
  .sheet { box-shadow: none; margin: 0; width: auto; max-width: none; min-height: 0; padding: 0; }
  .sheet.land { page: land; }
  .page-break { break-after: page; }
}
