/* Layout: แถบหัวสีหมึกน้ำเงินเข้ม + พื้นที่ทำงานโทนเทาอมฟ้า ตารางเป็นหัวใจของหน้า */
:root {
  --ink: #14324a;          /* หัวกระดาษ / ข้อความเน้น */
  --ink-2: #1f4b6e;
  --accent: #0f7a6c;       /* ปุ่มหลัก */
  --accent-soft: #e2f2ef;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --line: #dde3e9;
  --line-strong: #c4ced8;
  --fg: #1b2530;
  --muted: #5d6b79;
  --danger: #b3372b;
  --danger-soft: #fbe9e7;
  --warn-soft: #fff4dc;
  --font: "IBM Plex Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  --font-print: "Sarabun", "TH Sarabun New", "Leelawadee UI", Tahoma, sans-serif;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 15px/1.55 var(--font); color: var(--fg); background: var(--bg); }
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.25; text-wrap: balance; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line-strong);
  border-radius: 6px; background: var(--surface);
}
textarea { min-height: 64px; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
label { display: block; font-size: 13px; font-weight: 600; color: var(--ink); margin-bottom: 4px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 6px; border: 1px solid var(--line-strong);
  background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: #e3b5ae; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { background: transparent; border-color: rgba(255,255,255,.35); color: #fff; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; margin-top: 16px; }
.btn:disabled { opacity: .55; cursor: wait; }
.icon-btn { border: 0; background: none; font-size: 24px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background: linear-gradient(160deg, var(--ink) 0 38%, var(--bg) 38%); }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border-radius: 12px;
  padding: 32px 28px; box-shadow: 0 12px 40px rgba(20,50,74,.18); }
.auth-card h1 { font-size: 22px; color: var(--ink); margin: 12px 0 8px; }
.auth-card form label { margin-top: 14px; }
.brand-mark { width: 44px; height: 44px; border-radius: 10px; background: var(--ink); color: #fff;
  display: grid; place-items: center; font-weight: 700; letter-spacing: .04em; }
#login-code { font-size: 20px; letter-spacing: .18em; text-transform: uppercase; text-align: center; font-weight: 600; }
.form-error { color: var(--danger); min-height: 1.2em; margin: 10px 0 0; font-size: 14px; }
.code-show { font-size: 28px; font-weight: 700; letter-spacing: .2em; text-align: center; padding: 14px;
  border: 2px dashed var(--accent); border-radius: 8px; background: var(--accent-soft); color: var(--ink);
  user-select: all; margin: 8px 0; font-variant-numeric: tabular-nums; }

/* ---------- topbar ---------- */
.topbar { background: var(--ink); color: #fff; position: sticky; top: 0; z-index: 20; }
.topbar-inner, .tabs, .page { max-width: 1400px; margin: 0 auto; padding-left: 20px; padding-right: 20px; }
.topbar-inner { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; padding-block: 12px 6px; }
.eyebrow { font-size: 12px; letter-spacing: .08em; opacity: .7; }
.app-title { font-size: 20px; font-weight: 700; }
.who { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 14px; }
.role-chip { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: rgba(255,255,255,.14); }
.tabs { display: flex; gap: 2px; overflow-x: auto; }
.tabs button { background: none; border: 0; color: rgba(255,255,255,.72); padding: 10px 14px; cursor: pointer;
  border-bottom: 3px solid transparent; white-space: nowrap; font-weight: 500; }
.tabs button:hover { color: #fff; }
.tabs button.active { color: #fff; border-bottom-color: #5fd0bd; }

/* ---------- page ---------- */
.page { padding-block: 24px 64px; }
.panel-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.panel-head h2 { font-size: 22px; color: var(--ink); }
.panel-head p { margin: 4px 0 0; max-width: 65ch; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.filters select, .filters input { width: auto; min-width: 180px; max-width: 100%; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 20px; font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }

.month-strip { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 4px; margin-bottom: 14px; }
.month-strip .m { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 4px 2px 6px; text-align: center; font-size: 12px; }
.month-strip .m b { display: block; font-size: 16px; color: var(--ink); font-variant-numeric: tabular-nums; }
.month-strip .m.zero b { color: var(--line-strong); }
.month-strip .bar { height: 4px; border-radius: 2px; background: var(--accent); margin: 4px auto 0; opacity: .85; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
.grid th { text-align: left; background: #eef2f5; color: var(--ink); font-weight: 600; font-size: 13px; padding: 9px 10px;
  border-bottom: 1px solid var(--line-strong); white-space: nowrap; position: sticky; top: 0; }
.grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
.grid tbody tr:hover td { background: #f8fafb; }
.grid .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.grid .nowrap { white-space: nowrap; }
.grid .topic { font-weight: 600; color: var(--ink); min-width: 220px; }
.grid .org { min-width: 150px; }
.grid .need { min-width: 190px; }
.grid .row-actions { white-space: nowrap; text-align: right; }
.grid tfoot td { font-weight: 700; background: #eef2f5; border-top: 1px solid var(--line-strong); }
.grid .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.code-cell { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 14px; font-weight: 600; letter-spacing: .08em;
  background: var(--accent-soft); color: var(--ink); padding: 2px 8px; border-radius: 4px; user-select: all; }
.bulk-wrap { max-height: 46vh; overflow: auto; }
.bulk td { padding: 5px 10px; vertical-align: middle; }
.bulk td input[type=checkbox], .bulk th input[type=checkbox] { width: auto; }
.bulk input:not([type=checkbox]) { padding: 5px 8px; }
.grid tr.grp td { background: #eef2f5; font-weight: 700; color: var(--ink); }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.pill { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px; white-space: nowrap; border: 1px solid; }
.pill.exam { color: #1f4b6e; border-color: #b9cde0; background: #edf3f9; }
.pill.workshop { color: #7a4a0c; border-color: #ecd3a6; background: var(--warn-soft); }
.pill.on { color: var(--accent); border-color: #a8d8cf; background: var(--accent-soft); }
.pill.off { color: var(--muted); border-color: var(--line); background: #f2f4f6; }
.pill.admin { color: #fff; border-color: var(--ink); background: var(--ink); }

/* ---------- org ---------- */
.inline-form { display: flex; gap: 8px; margin-bottom: 16px; max-width: 640px; }
.org-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.org-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; min-width: 0; }
.org-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.org-card-head h3 { font-size: 16px; color: var(--ink); }
.dept-list { list-style: none; padding: 0; margin: 0 0 10px; }
.dept-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 5px 0; border-bottom: 1px dashed var(--line); }
.dept-list li:last-child { border-bottom: 0; }
.dept-add { display: flex; gap: 6px; }
.dept-add input { padding: 5px 8px; }
.link-btn { background: none; border: 0; color: var(--ink-2); cursor: pointer; font-size: 13px; padding: 2px 4px; text-decoration: underline; }
.link-btn.danger { color: var(--danger); }

/* ---------- settings ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px 18px; max-width: 900px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.field.full { grid-column: 1 / -1; }

/* ---------- modal ---------- */
dialog { border: 0; border-radius: 12px; padding: 0; width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  box-shadow: 0 24px 60px rgba(0,0,0,.25); color: var(--fg); }
dialog::backdrop { background: rgba(20,50,74,.45); }
#modal-form { display: flex; flex-direction: column; max-height: calc(100vh - 24px); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.modal-head h3 { font-size: 18px; color: var(--ink); }
.modal-body { padding: 16px 20px; overflow-y: auto; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.modal-body .full { grid-column: 1 / -1; }
.modal-body fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px 14px; margin: 0; min-width: 0;
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 14px; }
.modal-body legend { font-weight: 700; color: var(--ink); padding: 0 6px; font-size: 14px; }
#modal-error { padding: 0 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); }
.radio-row { display: flex; gap: 8px; flex-wrap: wrap; }
.radio-row label, .check { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; color: var(--fg);
  border: 1px solid var(--line-strong); border-radius: 6px; padding: 7px 12px; cursor: pointer; margin: 0; }
.radio-row input, .check input { width: auto; margin: 0; }
.radio-row label:has(input:checked), .check:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-col { display: grid; gap: 6px; }
.calc { background: #f6f8fa; border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; font-variant-numeric: tabular-nums; }
.calc table { width: 100%; border-collapse: collapse; font-size: 14px; }
.calc td { padding: 3px 0; }
.calc td:last-child { text-align: right; }
.calc tr.total td { border-top: 1px solid var(--line-strong); font-weight: 700; color: var(--ink); padding-top: 6px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 3px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 8px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 100; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }
#toast.err { background: var(--danger); }

#print-area { display: none; }

@media (max-width: 640px) {
  .modal-body, .modal-body fieldset { grid-template-columns: 1fr; }
  .month-strip { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .inline-form { flex-direction: column; }
  .filters select, .filters input { min-width: 0; width: 100%; }
}
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

/* ---------- พิมพ์ ---------- */
@page { size: A4 landscape; margin: 12mm 10mm; }
@media print {
  body { background: #fff; }
  #auth-view, #app-view, dialog, #toast { display: none !important; }
  #print-area { display: block; font-family: var(--font-print); font-size: 13px; color: #000; }
  .pr-head { text-align: center; margin-bottom: 8px; }
  .pr-head .org { font-size: 15px; font-weight: 600; }
  .pr-head h1 { font-size: 19px; margin: 2px 0; }
  .pr-head .scope { font-size: 14px; }
  .pr-table { width: 100%; border-collapse: collapse; }
  .pr-table th, .pr-table td { border: 1px solid #000; padding: 3px 5px; vertical-align: top; }
  .pr-table th { background: #e9e9e9; font-weight: 700; text-align: center; vertical-align: middle;
    -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .pr-table thead { display: table-header-group; }
  .pr-table tr { page-break-inside: avoid; }
  .pr-table .c { text-align: center; }
  .pr-table .r { text-align: right; font-variant-numeric: tabular-nums; }
  .pr-table .mo { width: 26px; text-align: center; padding: 3px 0; }
  .pr-table .dot { font-size: 14px; }
  .pr-table tfoot td { font-weight: 700; background: #f3f3f3; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .pr-legend { margin-top: 6px; font-size: 12px; }
  .pr-sign { display: grid; grid-template-columns: repeat(3, 1fr); gap: 24px; margin-top: 36px; page-break-inside: avoid; text-align: center; }
  .pr-sign .line { margin-top: 34px; }
  .pr-foot { margin-top: 10px; font-size: 11px; color: #444; }
}
