/* Two-tone system: cream (light) + gray (dark). Font: Bai Jamjuree everywhere. */
:root {
  --cream-50: #fffdf6;
  --cream-100: #fbf6e6;
  --cream-200: #f4ecd4;
  --cream-300: #e8dcb8;
  --cream-400: #d3c391;
  --gray-900: #232326;
  --gray-800: #2c2c30;
  --gray-700: #3a3a3f;
  --gray-600: #4d4d53;
  --gray-500: #6b6b72;
  --gray-400: #8d8d94;
  --gray-300: #b4b2ab;

  --bg: var(--cream-100);
  --surface: var(--cream-50);
  --surface-2: var(--cream-200);
  --ink: var(--gray-800);
  --muted: var(--gray-500);
  --line: var(--cream-300);
  --accent: var(--gray-700);
  --accent-ink: var(--cream-100);
  --side-bg: var(--gray-800);
  --side-ink: var(--cream-200);
  --side-hover: rgba(244, 236, 212, .10);
  --th-bg: var(--gray-700);
  --th-ink: var(--cream-100);
  --total-bg: var(--cream-200);
  --shadow: 0 1px 2px rgba(44, 44, 48, .06), 0 8px 24px rgba(44, 44, 48, .06);
  --radius: 14px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: var(--gray-900);
  --surface: var(--gray-800);
  --surface-2: var(--gray-700);
  --ink: var(--cream-200);
  --muted: var(--gray-300);
  --line: #46464c;
  --accent: var(--cream-300);
  --accent-ink: var(--gray-900);
  --side-bg: #1b1b1e;
  --side-ink: var(--cream-200);
  --side-hover: rgba(244, 236, 212, .08);
  --th-bg: #4a4a50;
  --th-ink: var(--cream-100);
  --total-bg: #3a3a3f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--gray-900);
    --surface: var(--gray-800);
    --surface-2: var(--gray-700);
    --ink: var(--cream-200);
    --muted: var(--gray-300);
    --line: #46464c;
    --accent: var(--cream-300);
    --accent-ink: var(--gray-900);
    --side-bg: #1b1b1e;
    --side-ink: var(--cream-200);
    --side-hover: rgba(244, 236, 212, .08);
    --th-bg: #4a4a50;
    --th-ink: var(--cream-100);
    --total-bg: #3a3a3f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body, button, input, select, textarea, table { font-family: 'Bai Jamjuree', system-ui, sans-serif; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-size: 15px; line-height: 1.55; font-weight: 400;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1rem; }
a { color: inherit; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.ok { color: var(--ink); font-weight: 600; }
.err { background: var(--surface-2); border-left: 4px solid var(--accent); padding: .6rem .9rem; border-radius: 8px; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; gap: .45rem; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: .5rem 1rem; border-radius: 10px; font-size: .93rem; font-weight: 500;
  transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.small { padding: .25rem .65rem; font-size: .85rem; border-radius: 8px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  width: 100%; padding: .55rem .75rem; border-radius: 10px; font-size: .95rem;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 110px; resize: vertical; }
label { display: grid; gap: .3rem; font-size: .88rem; color: var(--muted); font-weight: 500; }
label.inline { display: inline-flex; align-items: center; gap: .5rem; }
label.inline input { width: auto; }
.form-grid { display: grid; gap: .9rem; }
.form-grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
hr { border: 0; border-top: 1px solid var(--line); width: 100%; margin: .3rem 0; }

/* ---------- auth ---------- */
.auth-body { display: grid; place-items: center; min-height: 100vh; padding: 1rem;
  background: radial-gradient(1200px 600px at 10% -10%, var(--cream-200), transparent 60%), var(--bg); }
:root[data-theme="dark"] .auth-body { background: radial-gradient(1200px 600px at 10% -10%, #3a3a3f, transparent 60%), var(--bg); }
.auth-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 2rem; box-shadow: var(--shadow); display: grid; gap: 1.1rem; }
.brand-mark { width: 44px; height: 44px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 700; font-size: 1.2rem; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.side { background: var(--side-bg); color: var(--side-ink); padding: 1.2rem .9rem; display: flex; flex-direction: column; gap: 1.2rem; position: sticky; top: 0; height: 100vh; }
.side .brand { display: flex; gap: .7rem; align-items: center; padding: .2rem .5rem; }
.brand-logo { width: 44px; height: 44px; object-fit: contain; border-radius: 6px; flex: none; }
.auth-card > .brand-logo { width: 84px; height: 84px; }
.side .brand .brand-logo { background: var(--cream-100); padding: 2px; }
.side .brand small { display: block; opacity: .65; font-size: .75rem; }
.nav { display: grid; gap: .2rem; }
.nav a { text-decoration: none; padding: .6rem .8rem; border-radius: 10px; display: flex; gap: .65rem; align-items: center; opacity: .85; font-weight: 500; }
.nav a:hover { background: var(--side-hover); opacity: 1; }
.nav a.active { background: var(--cream-200); color: var(--gray-800); opacity: 1; }
.nav .grp { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; opacity: .5; padding: .8rem .8rem .2rem; }
.side .foot { margin-top: auto; display: grid; gap: .5rem; font-size: .85rem; }
.side .foot .btn { background: transparent; color: var(--side-ink); border-color: rgba(244, 236, 212, .25); justify-content: center; }
.main { padding: 1.6rem clamp(1rem, 3vw, 2.4rem) 3rem; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.3rem; }
.menu-btn { display: none; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; box-shadow: var(--shadow); }
.card + .card { margin-top: 1rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: .7rem; align-items: end; margin-bottom: 1rem; }
.toolbar > label { min-width: 150px; }
.toolbar .spacer { flex: 1; }
.tabs { display: flex; gap: .3rem; flex-wrap: wrap; background: var(--surface-2); padding: .3rem; border-radius: 12px; width: fit-content; max-width: 100%; margin-bottom: 1rem; }
.tabs button { border: 0; background: transparent; color: var(--ink); padding: .45rem .9rem; border-radius: 9px; cursor: pointer; font-weight: 500; font-size: .92rem; }
.tabs button.on { background: var(--accent); color: var(--accent-ink); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .8rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1.1rem; }
.stat .k { font-size: .8rem; color: var(--muted); }
.stat .v { font-size: 1.55rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .s { font-size: .8rem; color: var(--muted); }

/* ---------- tables ---------- */
.tbl-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 12px; max-height: 72vh; }
table.rep { border-collapse: separate; border-spacing: 0; width: 100%; font-size: .88rem; }
table.rep th, table.rep td { padding: .42rem .65rem; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); }
table.rep th:last-child, table.rep td:last-child { border-right: 0; }
table.rep thead th { background: var(--th-bg); color: var(--th-ink); font-weight: 600; text-align: center; position: sticky; top: 0; z-index: 2; border-color: rgba(244, 236, 212, .18); }
table.rep thead th { min-width: 58px; }
table.rep thead tr.r2 th { top: 2.1rem; }

table.rep tr.total td, table.rep tr.sum td { font-weight: 700; }
table.rep td.lbl { white-space: nowrap; font-weight: 500; }

table.rep tfoot td { background: var(--accent); color: var(--accent-ink); font-weight: 700; position: sticky; bottom: 0; }
table.plain { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.plain th { text-align: left; color: var(--muted); font-weight: 500; font-size: .8rem; padding: .5rem; border-bottom: 1px solid var(--line); }
table.plain td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
.badge { display: inline-block; padding: .1rem .55rem; border-radius: 99px; background: var(--surface-2); font-size: .78rem; border: 1px solid var(--line); }
.badge.dark { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.note { font-size: .82rem; color: var(--muted); margin-top: .6rem; }

/* upload */
.drop { border: 2px dashed var(--cream-400); border-radius: 16px; padding: 2rem; text-align: center; background: var(--surface); cursor: pointer; transition: .15s; }
:root[data-theme="dark"] .drop { border-color: var(--gray-500); }
.drop.over, .drop:hover { border-color: var(--accent); background: var(--surface-2); }
.drop strong { display: block; font-size: 1.05rem; }
.file-row { display: grid; grid-template-columns: 1fr 260px auto; gap: .7rem; align-items: center; padding: .7rem 0; border-bottom: 1px solid var(--line); }
.file-row .st { grid-column: 1 / -1; font-size: .85rem; color: var(--muted); }
@media (max-width: 760px) { .file-row { grid-template-columns: 1fr; } }

/* ai */
.answer { white-space: pre-wrap; background: var(--surface-2); border-radius: 12px; padding: 1rem 1.1rem; line-height: 1.7; }
.row2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }

/* modal & toast */
.modal-back { position: fixed; inset: 0; background: rgba(20, 20, 22, .55); display: grid; place-items: center; padding: 1rem; z-index: 50; }
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; width: min(920px, 100%); max-height: 90vh; overflow: auto; padding: 1.4rem; box-shadow: var(--shadow); display: grid; gap: 1rem; }
.modal.narrow { width: min(480px, 100%); }
.modal .actions { display: flex; justify-content: flex-end; gap: .6rem; }
.toast { position: fixed; bottom: 1.2rem; right: 1.2rem; background: var(--accent); color: var(--accent-ink); padding: .7rem 1.1rem; border-radius: 12px; box-shadow: var(--shadow); z-index: 60; max-width: 80vw; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: sp .7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes sp { to { transform: rotate(360deg); } }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: 260px; height: 100vh; transform: translateX(-100%); transition: transform .2s; z-index: 40; }
  .side.open { transform: none; }
  .menu-btn { display: inline-flex; }
}
@media print {
  .side, .toolbar, .tabs, .menu-btn, .no-print { display: none !important; }
  .shell { display: block; } body { background: #fff; color: #000; }
  .card { box-shadow: none; border: 0; padding: 0; }
  .tbl-wrap { max-height: none; overflow: visible; border: 0; }
  table.rep thead th { position: static; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  table.rep { font-size: 9pt; }
}

/* ---------- formal segmented switches (theme / language) ---------- */
.ctrl-row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.seg { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 2px; background: var(--surface-2); gap: 2px; }
.seg button { display: inline-flex; align-items: center; gap: .35rem; border: 0; background: transparent; color: var(--ink); padding: .3rem .65rem; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: .8rem; letter-spacing: .02em; }
.seg button:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.seg-ic { display: inline-grid; place-items: center; padding: 0 .25rem 0 .4rem; color: var(--muted); }
.side .seg { background: transparent; border-color: rgba(244, 236, 212, .28); }
.side .seg button { color: var(--side-ink); }
.side .seg button[aria-pressed="true"] { background: var(--cream-200); color: var(--gray-800); }
.side .seg-ic { color: var(--side-ink); opacity: .7; }
.side .ctrl-row { padding: 0 .3rem; }
label.wide { min-width: 280px; flex: 1 1 280px; }

/* ---------- report tables: paper look, colours come from the example workbooks (see reports.js) ---------- */
.tbl-wrap { background: #fff; }
table.rep { background: #fff; color: #000; font-size: .9rem; }
table.rep th, table.rep td { border: 1px solid #000; padding: .3rem .5rem; }
table.rep thead th { background: #fff; color: #000; font-weight: 700; text-align: center; vertical-align: middle; }
table.rep tfoot td { position: static; background: #fff; color: #000; font-weight: 700; }
table.rep td.zero { color: #555; }
table.rep td.ctr { text-align: center; }
table.rep td.al-l { text-align: left; white-space: nowrap; }
table.rep td.b, table.rep td.b.num { font-weight: 700; }
table.rep td.num { text-align: right; }
table.rep.t-orange td.num, table.rep.t-blue td.num, table.rep.t-gold td.num, table.rep.t-plain td.num { text-align: center; }
table.rep.t-small { font-size: .78rem; } table.rep.t-mid { font-size: .86rem; }
table.rep.t-plain thead th.big { font-size: 1.05rem; padding: .5rem .8rem; }
table.rep.t-plain td { font-size: .95rem; }
.doc-title { text-align: center; font-weight: 700; line-height: 1.5; margin: 0 0 .7rem; padding: .35rem .5rem; border-radius: 4px; }
.doc-title[class*="f-"] { }
.doc-foot { margin-top: .7rem; font-size: .85rem; font-weight: 600; line-height: 1.6; }
@media print { .tbl-wrap { border: 0; } table.rep th, table.rep td { -webkit-print-color-adjust: exact; print-color-adjust: exact; } .doc-title { -webkit-print-color-adjust: exact; print-color-adjust: exact; } }
