:root {
  --bg: #f5f6f8; --panel: #fff; --text: #1f2933; --muted: #6b7785; --line: #e3e7ec;
  --accent: #0f766e; --accent-soft: #e6f4f2; --danger: #b42318; --warn: #b54708; --ok: #067647;
  --side: #10302d; --side-text: #cfe3df;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: "Sarabun", "Leelawadee UI", "Tahoma", sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
#app { display: flex; min-height: 100vh; }
#sidebar { width: 230px; background: var(--side); color: var(--side-text); padding: 16px 0; flex-shrink: 0; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { font-size: 18px; padding: 0 18px 14px; color: #fff; }
.company-switch { padding: 0 18px 14px; border-bottom: 1px solid rgba(255,255,255,.1); margin-bottom: 8px; }
.company-switch label { font-size: 12px; opacity: .7; display: block; margin-bottom: 4px; }
.company-switch select { width: 100%; padding: 6px; border-radius: 6px; border: 0; font: inherit; }
.company-switch a { color: var(--side-text); opacity: .8; display: inline-block; margin-top: 6px; }
#nav a { display: block; padding: 8px 18px; color: var(--side-text); text-decoration: none; }
#nav a:hover { background: rgba(255,255,255,.06); }
#nav a.active { background: rgba(255,255,255,.12); color: #fff; border-left: 3px solid #5eead4; padding-left: 15px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; opacity: .55; padding: 14px 18px 4px; }
main { flex: 1; padding: 24px 28px 60px; min-width: 0; }
h1 { font-size: 22px; margin: 0 0 16px; font-weight: 600; }
h2 { font-size: 16px; margin: 20px 0 10px; font-weight: 600; }
.small { font-size: 12px; }
.muted { color: var(--muted); }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; margin-bottom: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 22px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
button, .btn { font: inherit; padding: 7px 14px; border-radius: 7px; border: 1px solid var(--line); background: #fff; color: var(--text); cursor: pointer; text-decoration: none; display: inline-block; }
button:hover, .btn:hover { border-color: #c5ccd4; }
.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.primary:hover { background: #0d635c; }
.danger { color: var(--danger); border-color: #f1c4bf; }
.link { border: 0; background: none; color: var(--accent); padding: 0; }
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid #cfd6de; border-radius: 6px; background: #fff; color: var(--text); width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #99e0d6; border-color: var(--accent); }
input[type=checkbox] { width: auto; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px 16px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.check { display: flex; gap: 6px; align-items: center; }
table { width: 100%; border-collapse: collapse; background: var(--panel); }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f7faf9; }
tfoot td { font-weight: 600; background: #fafbfc; }
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow-x: auto; }
table.lines { min-width: 860px; }
.lines td { padding: 4px 6px; }
.lines input, .lines select { padding: 5px 7px; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px; background: #eef1f4; color: var(--muted); white-space: nowrap; }
.badge.posted { background: var(--accent-soft); color: var(--accent); }
.badge.draft { background: #fef3e2; color: var(--warn); }
.badge.void { background: #fde8e6; color: var(--danger); text-decoration: line-through; }
.badge.paid { background: #e3f6ea; color: var(--ok); }
.badge.partial { background: #fef3e2; color: var(--warn); }
.badge.unpaid { background: #eef1f4; color: var(--muted); }
.badge.ai { background: #ede9fe; color: #6d28d9; }
.totals { margin-left: auto; width: 320px; max-width: 100%; }
.totals div { display: flex; justify-content: space-between; padding: 4px 0; font-variant-numeric: tabular-nums; }
.totals .grand { font-weight: 700; font-size: 16px; border-top: 1px solid var(--line); padding-top: 8px; }
dialog { border: 0; border-radius: 12px; padding: 0; width: 640px; max-width: calc(100vw - 32px); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(16, 24, 32, .45); }
#modalForm { padding: 20px; }
#modalForm .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#toast { position: fixed; bottom: 20px; right: 20px; background: #1f2933; color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 420px; }
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 4px 12px; }
.kv .k { color: var(--muted); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

@media (max-width: 800px) {
  #app { flex-direction: column; }
  #sidebar { width: 100%; height: auto; position: static; }
  main { padding: 16px; }
  .two { grid-template-columns: 1fr; }
}

/* ---------- เอกสารพิมพ์ ---------- */
.print-page { background: #fff; width: 210mm; min-height: 297mm; margin: 0 auto; padding: 14mm 14mm; color: #000; font-size: 13px; }
.print-page .ph { display: flex; justify-content: space-between; gap: 20px; border-bottom: 2px solid #000; padding-bottom: 10px; }
.print-page .ph .co { font-size: 16px; font-weight: 700; }
.print-page .title { text-align: right; }
.print-page .title .t { font-size: 20px; font-weight: 700; }
.print-page .meta { display: grid; grid-template-columns: 1fr 260px; gap: 16px; margin: 12px 0; }
.print-page .box { border: 1px solid #999; border-radius: 6px; padding: 8px 10px; }
.print-page table th { background: #f0f0f0; color: #000; border: 1px solid #999; }
.print-page table td { border-left: 1px solid #999; border-right: 1px solid #999; border-bottom: 0; }
.print-page table tbody tr:last-child td { border-bottom: 1px solid #999; }
.print-page .sum { display: flex; justify-content: space-between; gap: 16px; margin-top: 10px; }
.print-page .words { background: #f0f0f0; padding: 6px 10px; border-radius: 4px; align-self: flex-start; }
.print-page .sign { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-top: 50px; text-align: center; }
.print-page .sign div { border-top: 1px dotted #000; padding-top: 6px; }
.print-bar { text-align: center; padding: 12px; }
@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff; }
  .no-print, .print-bar { display: none !important; }
  main { padding: 0; }
  .print-page { margin: 0; width: auto; min-height: auto; }
}

/* ---------- เบิกค่าใช้จ่าย ---------- */
.co-name { padding: 0 18px 12px; font-size: 13px; opacity: .8; border-bottom: 1px solid rgba(255,255,255,.1); margin-bottom: 8px; }
.badge.cl-draft { background: #fef3e2; color: var(--warn); }
.badge.cl-submitted { background: #fff4d6; color: #8a5a00; }
.badge.cl-approved { background: var(--accent-soft); color: var(--accent); }
.badge.cl-rejected { background: #fde8e6; color: var(--danger); }
.badge.cl-paid { background: #e3f6ea; color: var(--ok); }
.badge.cl-void { background: #fde8e6; color: var(--danger); text-decoration: line-through; }
.claim-item { padding: 12px 16px; }
.claim-item.has-error { border-color: #f1c4bf; }
.ci-head { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.ci-head .spacer { flex: 1; }
.ci-grid { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.claim-item > .field { margin-top: 10px; }
ul.chk { margin: 10px 0 0; padding-left: 18px; font-size: 13px; }
ul.chk li { margin: 2px 0; }
ul.chk li.error { color: var(--danger); }
ul.chk li.warn { color: var(--warn); }
ul.chk li.info { color: var(--muted); }
.att { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; }
.att-chip { background: #f1f4f6; border-radius: 6px; padding: 2px 8px; font-size: 12px; }
.att-chip a { color: var(--text); }
details.hint summary { cursor: pointer; }
ul.checklist { list-style: none; padding: 0; margin: 0 0 8px; }
ul.checklist li { padding: 4px 0 4px 24px; position: relative; }
ul.checklist li::before { content: '○'; position: absolute; left: 4px; color: var(--muted); }
ul.checklist li.ok::before { content: '✓'; color: var(--ok); font-weight: 700; }
ul.checklist li.todo::before { content: '!'; color: var(--warn); font-weight: 700; left: 8px; }
.print-page { position: relative; }
.print-page .sign.sign3 { grid-template-columns: 1fr 1fr 1fr; gap: 20px; }
.print-page .sign.cl > div { font-size: 12px; line-height: 1.8; border-top: 0; }
.draft-mark { position: absolute; top: 40%; left: 0; right: 0; text-align: center; font-size: 64px; color: rgba(180, 35, 24, .12); transform: rotate(-20deg); pointer-events: none; font-weight: 700; }.me-box { padding: 14px 18px; margin-top: 12px; border-top: 1px solid rgba(255,255,255,.1); font-size: 13px; }
.me-box a { color: var(--side-text); opacity: .85; }
.auth { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.auth .card { width: 380px; max-width: 100%; padding: 24px; }
.brand-lg { font-size: 22px; margin-bottom: 14px; color: var(--accent); }

/* ---------- ไฟล์แนบ / ถ่ายรูป ---------- */
.att-zone { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.att-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.att-btn { padding: 8px 12px; background: var(--accent-soft); border-color: #b9e3dc; color: var(--accent); }
.att-chip { display: inline-flex; align-items: center; gap: 6px; }
.att-chip.pending { background: #fff4d6; }
.att-thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 4px; vertical-align: middle; background: #fff; }
.att-icon { font-size: 22px; }
.claim-cards { display: none; }

/* ---------- มือถือ ---------- */
#topbar { display: none; }
@media (max-width: 800px) {
  body:not(.bare) #topbar { display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 30; background: var(--side); color: #fff; padding: 8px 12px; }
  #topbar #menuBtn { background: transparent; border: 0; color: #fff; font-size: 22px; padding: 2px 8px; }
  #topbar .tb-title { color: #fff; text-decoration: none; font-size: 17px; flex: 1; }
  #topbar .tb-new { padding: 6px 12px; }
  #sidebar { display: none; }
  body.menu-open #sidebar { display: block; position: fixed; top: 48px; left: 0; bottom: 0; width: 260px; height: auto; z-index: 40; box-shadow: 4px 0 20px rgba(0,0,0,.3); }
  body.bare #sidebar { display: none !important; }
  main { padding: 14px 12px 90px; }
  h1 { font-size: 19px; }
  input, select, textarea { font-size: 16px; } /* iOS ไม่ซูมหน้าจอตอนแตะช่องกรอก */
  .toolbar input { width: 100% !important; }
  .cards { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .value { font-size: 18px; }
  .hide-mobile { display: none; }
  .claim-cards { display: block; }
  .claim-card { display: block; color: inherit; text-decoration: none; padding: 12px 14px; margin-bottom: 10px; }
  .cc-top, .cc-mid { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
  .cc-mid { margin: 6px 0 4px; }
  .totals { width: 100%; }
  .kv { grid-template-columns: 110px 1fr; }
  .att-btn { flex: 1 1 140px; padding: 12px; }
  .att-thumb { width: 56px; height: 56px; }
  /* ปุ่มบันทึก/ส่งอนุมัติ ติดด้านล่างจอ */
  main > .toolbar:last-of-type:has(#submit) { position: fixed; left: 0; right: 0; bottom: 0; margin: 0; padding: 10px 12px; background: #fff; border-top: 1px solid var(--line); z-index: 20; }
  main > .toolbar:last-of-type:has(#submit) .spacer { display: none; }
  main > .toolbar:last-of-type:has(#submit) > * { flex: 1; text-align: center; }
  dialog { width: 100%; max-width: calc(100vw - 16px); }
  #modalForm .grid { grid-template-columns: 1fr; }
}

/* ---------- แก้จากผลทดสอบ ---------- */
body.bare #sidebar, body.bare #topbar { display: none !important; } /* หน้า login / พิมพ์ ไม่มีเมนูทุกขนาดจอ */
#toast { z-index: 100; white-space: pre-line; }
.totals div { gap: 12px; }
.totals-card { width: auto; }
.att-chip .link.danger { padding: 6px 10px; min-width: 36px; min-height: 36px; font-size: 16px; }
@media (max-width: 800px) {
  #toast { left: 12px; right: 12px; bottom: 84px; max-width: none; } /* อยู่เหนือแถบปุ่มบันทึกด้านล่าง */
  .totals-card { width: 100%; }
}

/* ---------- Microsoft 365 ---------- */
.ms-btn { display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%; padding: 11px 14px; font-size: 15px; }
.ms-logo { display: grid; grid-template-columns: 9px 9px; gap: 2px; }
.ms-logo i { width: 9px; height: 9px; display: block; }
.ms-logo i:nth-child(1) { background: #f25022; } .ms-logo i:nth-child(2) { background: #7fba00; }
.ms-logo i:nth-child(3) { background: #00a4ef; } .ms-logo i:nth-child(4) { background: #ffb900; }
.pw-alt { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.pw-alt summary { cursor: pointer; }
.auth-error { background: #fde8e6; color: var(--danger); padding: 8px 10px; border-radius: 6px; margin-bottom: 12px; font-size: 13px; }

.badge.co-badge { background: #e8eefc; color: #2b4ea2; font-weight: 600; }

/* ---------- เอกสารทั้งหมดในไฟล์เดียว ---------- */
.all-bar { position: sticky; top: 0; z-index: 10; background: #fff; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; text-align: left; }
.all-toc { display: flex; flex-wrap: wrap; gap: 6px; }
.all-toc a { font-size: 12px; background: var(--accent-soft); color: var(--accent); padding: 3px 8px; border-radius: 99px; text-decoration: none; }
section + section, section + .print-page { margin-top: 16px; }
.att-page .att-head { font-size: 12px; color: #444; border-bottom: 1px solid #ccc; padding-bottom: 6px; margin-bottom: 10px; }
.att-page img, .att-pdfpage img { display: block; max-width: 100%; max-height: 255mm; margin: 0 auto; object-fit: contain; }
@media print {
  section { break-before: page; }
  section:first-of-type { break-before: auto; }
  .att-pdfpage + .att-pdfpage { break-before: page; }
}
@media (max-width: 800px) {
  .print-page { width: 100%; min-height: auto; padding: 14px; font-size: 12px; }
}

.review-hint { background: #fff8e6; border-color: #f5d08a; padding: 10px 14px; }
main section { scroll-margin-top: 120px; }
.welfare-box { background: #eef9f6; border-color: #99e0d6; padding: 10px 14px; }
.welfare-box.over { background: #fdf0ee; border-color: #f1c4bf; }

.nav-count:not(:empty) { display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 6px; border-radius: 10px; background: var(--danger); color: #fff; font-size: 12px; line-height: 20px; text-align: center; }
.payouts { display: grid; gap: 12px; }
.payout .po-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.payout .po-name { font-size: 18px; font-weight: 700; }
.payout .po-bank { margin-top: 4px; }
.payout .mono { font-family: ui-monospace, Consolas, monospace; font-size: 16px; letter-spacing: .5px; }
.payout .po-amt { text-align: right; }
.payout .po-total { font-size: 24px; font-weight: 700; }
.payout.no-bank { border-color: #f1c4bf; }
.po-claims { width: 100%; margin-top: 10px; border-top: 1px solid var(--line, #e5e7eb); }
.po-claims td { padding: 6px 4px; vertical-align: top; }
.mybank { padding: 10px 14px; }
.mybank.missing { background: #fff8e6; border-color: #f5d08a; }
.peakb .po-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.peakb .po-name { font-size: 18px; font-weight: 700; }
.peakb .po-amt { text-align: right; }
.peakb .po-total { font-size: 24px; font-weight: 700; }
