/*
 * หน้าตาของ ERP — แบบที่เจ้าของผ่านใน artifact "หน้าจอ NSE ERP" (22 พ.ย. 2026)
 * เกณฑ์อยู่ที่ docs/ui-guidelines.md · แก้หน้าจอต้องทำ artifact ให้เจ้าของตรวจก่อน push
 *
 * แถบบนบาง · เมนูซ้าย · หน้าแรกคือการ์ด module หกใบพร้อมสัญลักษณ์ (สร้างด้วย codex)
 * ตัวหนังสือ 16px · ปุ่ม/ช่องกรอก 40px · ตัวเล็กสุด 13–14px · สีหลักทองแดงเข้ม
 */
:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --raised: #eeeeeb;
  --line: rgb(28 25 23 / .12);
  --line-strong: rgb(28 25 23 / .24);
  --ink: #1c1917;
  --ink-2: #44403c;
  --ink-3: #57534e;
  --accent: #8a3d14;
  --accent-hover: #73320f;
  --accent-soft: #f6ece4;
  --on-accent: #ffffff;
  --ok: #166534; --ok-soft: #e7f3eb;
  --bad: #991b1b; --bad-soft: #fbeaea;
  --warn: #7c4a03; --warn-soft: #fbf1dc;
  --font: "IBM Plex Sans Thai", "Leelawadee UI", "Noto Sans Thai", Tahoma, sans-serif;
  --r: 8px; --r-lg: 12px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}
@media (prefers-color-scheme: dark) { :root {
  --bg: #161514; --surface: #1e1d1b; --raised: #292725; --line: rgb(240 236 230 / .12); --line-strong: rgb(240 236 230 / .24);
  --ink: #f0ece6; --ink-2: #cfc9c1; --ink-3: #aaa39a; --accent: #e0915d; --accent-hover: #eaa678; --accent-soft: #3a2619;
  --on-accent: #1c1917; --ok: #6fcf97; --ok-soft: #173323; --bad: #f08a8a; --bad-soft: #3a1c1c; --warn: #f0c070; --warn-soft: #3a2e14;
  color-scheme: dark; } }

* { box-sizing: border-box; }
/* ของที่สั่งซ่อน (hidden) ต้องหายจริง — กฎ display ของ .field ฯลฯ เคยทับจนช่องที่ควรซ่อนยังโผล่ (codex ตรวจ PR #2 รอบ 3) */
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 16px; line-height: 1.6; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { font-weight: 600; line-height: 1.3; margin: 0; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--ink-3); font-size: 14px; }
.boot { padding: 48px 16px; text-align: center; color: var(--ink-3); }

/* ── ปุ่ม / ช่องกรอก ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 0 16px;
  border-radius: var(--r); border: 1px solid var(--line-strong); background: var(--surface); cursor: pointer;
  font-weight: 500; white-space: nowrap; text-decoration: none; color: var(--ink);
  transition: background-color 150ms var(--ease), border-color 150ms var(--ease);
}
.btn:hover { background: var(--raised); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.quiet { border-color: transparent; background: none; }
.btn.quiet:hover { background: var(--raised); }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.field { display: grid; gap: 4px; margin-bottom: 16px; }
.field .label { font-weight: 500; }
.field .help { color: var(--ink-3); font-size: 14px; }
input[type=text], input[type=password], select {
  width: 100%; min-height: 40px; padding: 0 12px; border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--surface);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.pw { display: flex; gap: 8px; }
.pw input { flex: 1; min-width: 0; }
.pw .btn { flex: none; min-width: 72px; padding: 0 12px; font-weight: 400; }
.checks { display: flex; flex-wrap: wrap; gap: 8px; }
.checks label { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 12px; border: 1px solid var(--line-strong); border-radius: var(--r); cursor: pointer; }
.checks label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.err { color: var(--bad); font-weight: 500; margin: 0 0 12px; }
.err:empty { display: none; }

/* ── แถบบน ── */
.top {
  display: flex; align-items: center; gap: 12px; min-height: 56px; background: var(--surface); border-bottom: 1px solid var(--line);
  padding: 8px 16px; position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 600; color: var(--accent); margin-right: auto; white-space: nowrap; }
.entity { display: flex; align-items: center; gap: 8px; }
.entity select { width: auto; min-height: 36px; font-weight: 500; }
.who { line-height: 1.3; text-align: right; }
.who small { display: block; color: var(--ink-3); font-size: 14px; }

/* ── โครงหน้า ── */
.layout { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: calc(100vh - 56px); }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 12px 8px; }
.side .group { color: var(--ink-3); font-size: 14px; padding: 16px 12px 4px; }
.side a { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 12px; border-radius: var(--r); color: var(--ink); text-decoration: none; }
.side a:hover { background: var(--raised); }
.side a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side a img { width: 24px; height: 24px; border-radius: 6px; flex: none; }
.side a .soon { margin-left: auto; color: var(--ink-3); font-size: 12px; }
main { padding: 24px; max-width: 1200px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h2 { font-size: 22px; margin-right: auto; }
.lead { margin: -8px 0 16px; }

/* ── การ์ด module ── */
.modules { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.mod {
  display: grid; grid-template-columns: 88px minmax(0, 1fr); gap: 16px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 16px;
  color: inherit; text-decoration: none; transition: border-color 150ms var(--ease);
}
.mod:hover { border-color: var(--accent); }
.mod img { width: 88px; height: 88px; border-radius: 10px; background: var(--raised); object-fit: cover; }
.mod h3 { font-size: 18px; }
.mod h3 span { color: var(--ink-3); font-weight: 400; font-size: 14px; white-space: nowrap; }
.mod p { margin: 2px 0 8px; color: var(--ink-2); font-size: 14px; }
.chip { display: inline-block; font-size: 13px; padding: 0 8px; border-radius: 6px; border: 1px solid var(--line-strong); color: var(--ink-2); white-space: nowrap; }
.chip.ok { color: var(--ok); background: var(--ok-soft); border-color: currentColor; }
.chip.bad { color: var(--bad); background: var(--bad-soft); border-color: currentColor; }
.chip.warn { color: var(--warn); background: var(--warn-soft); border-color: currentColor; }
.notice { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: space-between;
  background: var(--warn-soft); color: var(--warn); border-radius: var(--r-lg); padding: 10px 16px; margin-bottom: 16px; }
.notice .btn { min-height: 36px; border-color: currentColor; color: inherit; background: none; }

/* ── ตาราง ── */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 14px; color: var(--ink-3); font-weight: 500; background: var(--raised); }
tr:last-child td { border-bottom: 0; }
tr.inactive td { color: var(--ink-3); }
td .sub { display: block; color: var(--ink-3); font-size: 14px; }
td.act { text-align: right; white-space: nowrap; }
table .c { text-align: center; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 20px; margin-bottom: 16px; max-width: 640px; }
.card.wide { max-width: none; }
.card h3 { font-size: 18px; margin-bottom: 8px; }
.card p { margin: 0 0 8px; }
dl.info { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; }
dl.info dt { color: var(--ink-3); }
dl.info dd { margin: 0; }

/* ── ระดับและสิทธิ์ ── */
.matrix th.c { min-width: 96px; font-size: 14px; line-height: 1.3; }
.matrix td:first-child { min-width: 240px; }
.matrix input[type=checkbox] { width: 20px; height: 20px; }
tr.grp td { background: var(--bg); font-weight: 600; padding-top: 12px; padding-bottom: 8px; }
tr.grp img { width: 24px; height: 24px; border-radius: 6px; vertical-align: -6px; margin-right: 8px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--r); overflow: hidden; }
.seg button { border: 0; background: var(--surface); min-height: 36px; padding: 0 12px; cursor: pointer; border-left: 1px solid var(--line); }
.seg button:first-child { border-left: 0; }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.seg button:disabled { color: var(--ink-3); background: var(--raised); cursor: not-allowed; text-decoration: line-through; }
.savebar { position: sticky; bottom: 0; display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-lg); padding: 12px 16px; margin-top: 16px; }
.savebar input { flex: 1; min-width: 200px; }
.savebar .err { margin: 0; width: 100%; }
.who-manages td { padding: 8px 0; vertical-align: top; border: 0; }
.who-manages td:first-child { font-weight: 600; white-space: nowrap; padding-right: 24px; }

/* ── หน้าเข้าสู่ระบบ ── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login form { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 28px; }
.login .brand { display: block; margin-bottom: 4px; }
.login h1 { font-size: 22px; margin-bottom: 20px; }
.login .btn.primary { width: 100%; }
.login .after { margin: 16px 0 0; color: var(--ink-3); font-size: 14px; }
.login .notice { display: block; }

/* ── หน้าต่าง ── */
dialog { border: 0; border-radius: var(--r-lg); padding: 0; width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
  background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog form { padding: 24px; margin: 0; }
dialog h3 { font-size: 20px; margin-bottom: 16px; }
dialog p { margin: 0 0 12px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.menu-list { display: grid; gap: 8px; margin-bottom: 16px; }
.menu-list .btn { justify-content: flex-start; width: 100%; font-weight: 400; }
.menu-list .btn.danger { margin-top: 8px; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.history { max-height: 50vh; overflow: auto; margin-bottom: 16px; }
.history > div { padding: 8px 0; border-bottom: 1px solid var(--line); }
.history > div:last-child { border-bottom: 0; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 8px);
  background: var(--ink); color: var(--bg); padding: 10px 20px; border-radius: var(--r-lg); opacity: 0; pointer-events: none;
  transition: opacity 200ms var(--ease), transform 200ms var(--ease); z-index: 30; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1000px) { .modules { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 760px) {
  .who, .entity .muted { display: none; }
  .top { flex-wrap: wrap; }
  .top .btn { padding: 0 12px; }
  /* จอแคบ: เมนูเป็นแถบแนวนอนบนสุด — ห้ามยืดสูงตาม min-height ของหน้า (เคยทำให้ปุ่มที่เลือกอยู่ยืดเป็นแท่งสูง) */
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .side { display: flex; align-items: center; gap: 4px; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 8px; }
  .side .group, .side a.mod-link { display: none; }
  .side a { white-space: nowrap; flex: none; }
  td.act { white-space: normal; }
  main { padding: 16px; }
  .modules { grid-template-columns: minmax(0, 1fr); }
  .mod { grid-template-columns: 64px minmax(0, 1fr); }
  .mod img { width: 64px; height: 64px; }
}
/* ── PO ลูกค้า (แบบที่เจ้าของผ่านใน artifact "หน้าจอ PO ลูกค้า" 22 พ.ย. 2026) ── */
.wk { display: inline-flex; align-items: center; min-height: 26px; padding: 0 8px; border-radius: 6px; font-weight: 600;
  font-variant-numeric: tabular-nums; background: var(--raised); border: 1px solid var(--line-strong); white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nw { white-space: nowrap; }
.mono { font-variant-numeric: tabular-nums; letter-spacing: .01em; }
.filters { display: flex; gap: 8px 12px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.filters .field { margin: 0; }
.filters select { width: auto; min-width: 200px; }
.filters input[type=text] { width: 220px; }
.sumline { color: var(--ink-2); margin: 0 0 12px; font-variant-numeric: tabular-nums; }
tr.flash td { background: var(--accent-soft); }
.steps { display: grid; gap: 16px; max-width: 880px; }
.steps .card { margin: 0; }
.step-no { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--raised);
  font-weight: 600; margin-right: 8px; font-size: 14px; }
.card.done .step-no { background: var(--ok-soft); color: var(--ok); }
.filebox { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.filebox .sub, .history .sub { display: block; }
.filebox .notice, .card .notice { margin: 12px 0 0; }
.wkin { display: flex; align-items: center; gap: 8px; }
.wkin span { font-weight: 600; }
.wkin input { width: 96px; font-size: 18px; font-weight: 600; text-align: center; }
.tally { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin: 8px 0 4px; }
.tally div { border: 1px solid var(--line); border-radius: var(--r); padding: 8px 12px; }
.tally b { display: block; font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.tally span { color: var(--ink-3); font-size: 14px; }
.tally .new b { color: var(--accent); }
.ent-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.ent-row:last-of-type { border-bottom: 0; }
.ent-row h4 { margin: 0; font-size: 16px; font-weight: 600; }
.skip { background: var(--raised); border-radius: var(--r); padding: 10px 14px; margin-top: 12px; color: var(--ink-2); font-size: 14px; }
.subhead { font-size: 16px; font-weight: 600; margin: 16px 0 8px; }
.more { margin-top: 8px; }
@media (max-width: 760px) {
  .tally { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters .field { flex: 1 1 140px; }
  .filters input[type=text], .filters select { width: 100%; min-width: 0; }
  .filters .field:has(.seg) { flex-basis: 100%; }
  .filters .seg { display: flex; width: 100%; }
  .filters .seg button { flex: 1; white-space: nowrap; padding: 0 8px; }
  /* ตารางบนจอแคบ: แถวละการ์ด ไม่ต้องเลื่อนซ้ายขวา */
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tr { border-bottom: 1px solid var(--line); padding: 8px 12px; }
  table.stack td { border: 0; padding: 2px 0; text-align: left; }
  table.stack td[data-l]::before { content: attr(data-l) ' '; color: var(--ink-3); font-size: 14px; }
  table.stack td.num { text-align: left; }
  table.stack td.act { padding-top: 6px; text-align: left; }
  table.stack td:not([data-l]):not(.act) { display: inline-block; width: auto; margin: 4px 8px 0 0; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ── ทะเบียนกลาง (แบบที่เจ้าของผ่านใน artifact "ทะเบียนวัตถุดิบและ BOM" 24 พ.ย. 2026) ── */
.code { font-variant-numeric: tabular-nums; letter-spacing: .01em; white-space: nowrap; }
.flag { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; padding: 0 8px; border-radius: 6px; white-space: nowrap;
  color: var(--warn); background: var(--warn-soft); border: 1px solid currentColor; }
.flag.info { color: var(--ink-2); background: var(--raised); border-color: var(--line-strong); }
.src { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.src b { font-weight: 600; color: var(--ink); }
.bom-head { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 24px; align-items: start;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 16px 20px; margin-bottom: 16px; }
.bom-head h3 { font-size: 22px; }
.bom-head .desc { color: var(--ink-2); margin: 2px 0 8px; }
.bom-head .meta { display: flex; gap: 8px 16px; flex-wrap: wrap; color: var(--ink-2); font-size: 14px; }
.bom-head .acts { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.cols { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.versions { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 8px 0; }
.versions h4 { margin: 0; padding: 8px 16px; font-size: 16px; }
.versions a { display: block; padding: 8px 16px; color: inherit; text-decoration: none; border-top: 1px solid var(--line); }
.versions a:hover { background: var(--raised); }
.versions a.on { background: var(--accent-soft); }
.versions a .sub { display: block; color: var(--ink-3); font-size: 14px; }
.diff-add { color: var(--ok); }
.diff-del { color: var(--bad); text-decoration: line-through; }
.diff-chg b { font-variant-numeric: tabular-nums; }
.kind { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 8px 0 16px; }
.kind label { display: grid; gap: 2px; padding: 12px 16px; border: 1px solid var(--line-strong); border-radius: var(--r); cursor: pointer; }
.kind label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.kind label b { display: flex; align-items: center; gap: 8px; }
.kind input { accent-color: var(--accent); width: 18px; height: 18px; margin: 0; }
.tally.five { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.edit-usage { width: 120px !important; text-align: right; }
.guess { color: var(--ink-3); font-size: 14px; }
tr.removed td { color: var(--ink-3); text-decoration: line-through; }
.pending-note { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; justify-content: space-between;
  background: var(--warn-soft); color: var(--warn); border-radius: var(--r-lg); padding: 10px 16px; margin-bottom: 16px; }
.pending-note .btn { min-height: 36px; border-color: currentColor; color: inherit; background: none; }
.side a .count { margin-left: auto; min-width: 24px; text-align: center; font-size: 13px; font-weight: 600; border-radius: 6px;
  background: var(--warn-soft); color: var(--warn); border: 1px solid currentColor; padding: 0 6px; }
tr.d-add td { background: var(--ok-soft); }
tr.d-del td { background: var(--bad-soft); }
tr.d-del td.code, tr.d-del td:nth-child(2) { text-decoration: line-through; }
.read-only-note { background: var(--raised); border-radius: var(--r); padding: 10px 14px; color: var(--ink-2); font-size: 14px; margin-bottom: 16px; }
@media (max-width: 1180px) { .cols { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 760px) {
  .bom-head { grid-template-columns: minmax(0, 1fr); }
  .bom-head .acts { justify-content: flex-start; }
  .kind { grid-template-columns: minmax(0, 1fr); }
  .tally.five { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .edit-usage { width: 100% !important; }
}
.side a .count:empty { display: none; }
.warnlist { margin: 0; padding-left: 20px; color: var(--ink-2); font-size: 14px; }
.warnlist li { margin: 2px 0; }
.diffbox { padding: 12px 16px; margin-bottom: 8px; }

/* ── ผู้ขาย + ราคา ── */
.price { font-variant-numeric: tabular-nums; font-weight: 600; }
.secret { display: flex; gap: 8px; align-items: center; background: var(--raised); color: var(--ink-2); border-radius: var(--r);
  padding: 8px 12px; font-size: 14px; margin-bottom: 12px; }
.up { color: var(--bad); } .down { color: var(--ok); }
.tally.six { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
input.short { max-width: 200px; }
@media (max-width: 760px) { .tally.six { grid-template-columns: repeat(2, minmax(0, 1fr)); } .pair { grid-template-columns: 1fr; } }
