:root {
  --bg: #f2f5f8;
  --card: #ffffff;
  --text: #1b2733;
  --muted: #6a7785;
  --line: #d9e0e7;
  --brand: #1f5f8b;
  --brand-2: #e3eff7;
  --ok: #1e8e3e;
  --ok-bg: #e6f4ea;
  --ng: #d93025;
  --ng-bg: #fce8e6;
  --warn: #b06000;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Noto Sans Thai", "Sarabun", Tahoma, system-ui, sans-serif;
  font-size: 16px;
}
button { font: inherit; }
input, select, textarea { font: inherit; color: inherit; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--brand); color: #fff;
}
.topbar h1 { font-size: 17px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .icon-btn { background: rgba(255,255,255,.15); color: #fff; border: 0; border-radius: 8px; padding: 6px 10px; }
.topbar .saved { font-size: 12px; opacity: .85; }
.syncbar { background: var(--brand-2); color: var(--brand); font-size: 12px; text-align: center; padding: 4px 8px; }
[data-sync-status][data-state="error"], [data-sync-status][data-state="badcode"], [data-sync-status][data-state="notconfigured"] { color: var(--ng); }
[data-sync-status][data-state="ok"] { color: var(--ok); }
.content { padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); max-width: 760px; margin: 0 auto; }
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav button {
  flex: 1; border: 0; background: none; padding: 8px 2px 9px; color: var(--muted);
  font-size: 12px; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.bottomnav button .ic { font-size: 18px; line-height: 1; }
.bottomnav button.active { color: var(--brand); font-weight: 600; }

.card { background: var(--card); border-radius: 12px; padding: 14px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.card h2 { font-size: 16px; margin: 0 0 10px; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; }
.grow { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }

.btn { border: 0; border-radius: 10px; padding: 11px 16px; font-weight: 600; cursor: pointer; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { background: var(--brand-2); color: var(--brand); }
.btn.danger { background: var(--ng-bg); color: var(--ng); }
.btn.ok { background: var(--ok); color: #fff; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }

/* ---------- forms ---------- */
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-size: 16px;
}
.field textarea { min-height: 70px; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

/* ---------- home list ---------- */
.report { display: flex; gap: 10px; align-items: center; }
.report .info { flex: 1; min-width: 0; }
.report .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.report .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.badge { display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: #eef1f4; color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.ng { background: var(--ng-bg); color: var(--ng); }

/* ---------- item chips ---------- */
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; margin-bottom: 4px; scrollbar-width: thin; }
.chip {
  flex: 0 0 auto; border: 1px solid var(--line); background: #fff; border-radius: 10px;
  padding: 6px 10px; font-size: 13px; text-align: center; min-width: 56px; line-height: 1.2;
}
.chip b { display: block; font-size: 14px; }
.chip.sel { border-color: var(--brand); background: var(--brand); color: #fff; }
.chip.ok:not(.sel) { border-color: var(--ok); background: var(--ok-bg); }
.chip.ng:not(.sel) { border-color: var(--ng); background: var(--ng-bg); }

.spec { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 6px 0 12px; }
.spec div { background: var(--bg); border-radius: 8px; padding: 6px 8px; }
.spec small { display: block; font-size: 11px; color: var(--muted); }
.spec span { font-weight: 700; }

.samples { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; }
.sample { display: flex; align-items: center; gap: 6px; }
.sample .n { width: 26px; text-align: right; color: var(--muted); font-size: 13px; flex: 0 0 auto; }
.sample input {
  flex: 1; min-width: 0; padding: 9px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 16px; text-align: center; background: #fff;
}
.sample input.ok { border-color: var(--ok); background: var(--ok-bg); }
.sample input.ng { border-color: var(--ng); background: var(--ng-bg); color: var(--ng); font-weight: 700; }
.seg { flex: 1; display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: #fff; padding: 9px 0; font-weight: 600; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on.okb { background: var(--ok); color: #fff; }
.seg button.on.ngb { background: var(--ng); color: #fff; }

.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 12px; text-align: center; }
.stats div { background: var(--bg); border-radius: 8px; padding: 6px 2px; }
.stats small { display: block; font-size: 11px; color: var(--muted); }
.stats span { font-weight: 700; font-size: 14px; }
.stats .ok { color: var(--ok); } .stats .ng { color: var(--ng); }

.thumb { width: 100%; border-radius: 8px; border: 1px solid var(--line); display: block; }

/* ---------- summary ---------- */
table.sum { width: 100%; border-collapse: collapse; font-size: 13px; }
table.sum th, table.sum td { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: center; }
table.sum td.l, table.sum th.l { text-align: left; }
.t-ok { color: var(--ok); font-weight: 700; } .t-ng { color: var(--ng); font-weight: 700; }
.verdict { font-size: 22px; font-weight: 800; text-align: center; padding: 12px; border-radius: 10px; }
.verdict.ok { background: var(--ok-bg); color: var(--ok); }
.verdict.ng { background: var(--ng-bg); color: var(--ng); }
.verdict.na { background: #eef1f4; color: var(--muted); }
.sigbox { border: 1px dashed var(--line); border-radius: 10px; height: 70px; display: flex; align-items: center; justify-content: center; background: #fafbfc; cursor: pointer; color: var(--muted); font-size: 13px; }
.sigbox img { max-height: 64px; max-width: 100%; }

/* ---------- preview ---------- */
.preview { overflow: hidden; }
.preview .scaler { transform-origin: 0 0; }
.preview .sheet { box-shadow: 0 1px 6px rgba(0,0,0,.2); margin-bottom: 16px; }
.pdf-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pdf-actions .full { grid-column: 1 / -1; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal.hidden { display: none; }
.modal-box { background: #fff; border-radius: 14px; padding: 14px; width: 100%; max-width: 520px; }
.modal-title { font-weight: 700; margin-bottom: 8px; }
#sigCanvas { width: 100%; height: 200px; border: 1px solid var(--line); border-radius: 10px; touch-action: none; background: #fff; margin-bottom: 10px; display: block; }
.img-wrap { width: 100%; height: 100%; overflow: auto; display: flex; align-items: center; }
.img-wrap img { width: 200%; max-width: none; background: #fff; }
.close-img { position: fixed; top: calc(12px + env(safe-area-inset-top)); right: 12px; }

.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: 14px; max-width: 90vw; text-align: center; }
.busy { position: fixed; inset: 0; z-index: 70; background: rgba(255,255,255,.75); display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--brand); }

/* =========================================================
   A4 inspection sheet (replicates the Excel master)
   794 x 1123 px  = A4 @ 96 dpi
   ========================================================= */
#printRoot { position: absolute; left: -10000px; top: 0; }
.sheet {
  width: 794px; height: 1123px; background: #fff; color: #000;
  padding: 28px 26px; overflow: hidden;
  font-family: Arial, "Helvetica Neue", Tahoma, sans-serif; font-size: 7.4px; line-height: 1.05;
}
.sheet table { border-collapse: collapse; width: 100%; table-layout: fixed; }
.sheet td { border: 0.6px solid #000; padding: 0 1px; text-align: center; vertical-align: middle; overflow: hidden; white-space: nowrap; }
.sheet td.wrap { white-space: pre-line; }
.sheet .frame { border: 1.6px solid #000; }
.sheet .hd td { border: 0; background: #daeef3; }
.sheet .hd .title { font-family: "Times New Roman", serif; font-size: 15px; font-weight: 700; }
.sheet .hd .co { font-family: "Times New Roman", serif; font-size: 11.5px; font-weight: 700; text-align: left; padding-left: 8px; }
.sheet .hd .addr { font-family: "Times New Roman", serif; font-size: 9px; font-weight: 700; text-align: left; padding-left: 8px; }
.sheet .hd .pg { font-size: 8.5px; font-weight: 700; }
.sheet .hd .logo img { height: 60px; display: block; margin: 0 auto; }
.sheet td.l { text-align: left; padding-left: 2px; }
.sheet td.b { font-weight: 700; }
.sheet td.blue { background: #daeef3; }
.sheet td.ng { color: #d00000; font-weight: 700; }
.sheet td.okj { font-weight: 700; }
.sheet td.val { font-weight: 700; }
.sheet td.nb { border: 0; }
.sheet .small { font-size: 6.6px; }
.sheet .pic-panel { padding: 0; border: 0.6px solid #000; vertical-align: top; }
.sheet .pic-left { position: relative; height: 100%; }
.sheet .pic-label { position: absolute; left: 2px; top: 2px; font-weight: 700; }
.sheet .pic-img { position: absolute; left: 40px; top: 12px; height: 205px; }
.sheet .rev { position: absolute; left: 0; right: 0; bottom: 0; }
.sheet .rev td { height: 14px; font-size: 6.6px; font-weight: 700; }
.sheet .jt td { font-size: 7.4px; }
.sheet .jt .big { font-size: 15px; font-weight: 700; }
.sheet .jt .res { font-size: 14px; font-weight: 800; }
.sheet .jt .res.ok { color: #0a7a2f; }
.sheet .jt .res.ng { color: #d00000; }
.sheet .jt .sig img { max-height: 32px; max-width: 95%; }
.sheet .note { text-align: left; padding-left: 2px; font-family: "Times New Roman", serif; font-size: 6.8px; white-space: pre-line; }

@media print {
  @page { size: A4 portrait; margin: 0; }
  body { background: #fff; }
  #app, .modal, .toast, .busy { display: none !important; }
  #printRoot { position: static; }
  #printRoot .sheet { page-break-after: always; break-after: page; }
  #printRoot .sheet:last-child { page-break-after: auto; break-after: auto; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
