/* Packman — ระบบใบเบิก
   ออกแบบสำหรับใช้งานในโกดัง: ปุ่มใหญ่แตะง่าย ตัวหนังสืออ่านชัด ใช้ได้ทั้งมือถือและคอม */

:root {
  /* สีหลัก — เขียวอมน้ำเงิน อ่านง่ายทั้งบนจอสว่างและในโกดังแสงน้อย */
  --brand: #0d9488;
  --brand-dark: #0f766e;
  --brand-soft: #ccfbf1;
  --brand-ring: rgba(13, 148, 136, .28);

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --line: #e3e7ec;
  --line-strong: #cbd2da;

  --text: #131a22;
  --muted: #67727f;
  --faint: #98a2b0;

  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #be123c;
  --danger-bg: #ffe4e6;
  --ok: #15803d;
  --ok-bg: #dcfce7;

  --r: 14px;
  --r-sm: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -12px rgba(16, 24, 40, .18);
  --shadow-lg: 0 2px 4px rgba(16, 24, 40, .06), 0 20px 40px -16px rgba(16, 24, 40, .28);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161b22;
    --surface-2: #1d232c;
    --line: #262d38;
    --line-strong: #333c49;
    --text: #e8edf3;
    --muted: #97a3b2;
    --faint: #6b7889;
    --brand-soft: #062f2b;
    --warn-bg: #3a2a09;
    --danger-bg: #3d0f1c;
    --ok-bg: #0c2f1a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .7);
    --shadow-lg: 0 2px 4px rgba(0, 0, 0, .5), 0 20px 40px -16px rgba(0, 0, 0, .8);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* ปุ่มตั้ง display:inline-flex ไว้ ซึ่งทับค่าเริ่มต้นของ [hidden] ทำให้ซ่อนไม่ลง
   ต้องประกาศทับอีกชั้น ไม่งั้นปุ่ม "กลับ" จะโผล่บนหน้าแรกทั้งที่สั่งซ่อนแล้ว */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", -apple-system, "Segoe UI", system-ui, sans-serif;
  font-size: 15.5px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
p { margin: 0; }

/* ---------- โครงหน้า ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 17px; }
.topbar .sub { color: var(--muted); font-size: 12.5px; font-weight: 400; }
.spacer { margin-left: auto; }

.wrap { max-width: 900px; margin: 0 auto; padding: 18px 16px 132px; }

/* ---------- การ์ด ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 12px; }
.card-pad { padding: 16px; }

/* ---------- ปุ่ม ---------- */
button, .btn {
  font: inherit; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform .06s ease, background .15s ease, border-color .15s ease;
  text-decoration: none;
}
button:active, .btn:active { transform: translateY(1px); }
button:focus-visible, .btn:focus-visible, input:focus-visible {
  outline: none; box-shadow: 0 0 0 3px var(--brand-ring); border-color: var(--brand);
}
.btn-primary {
  background: var(--brand); border-color: var(--brand); color: #fff;
  box-shadow: 0 1px 2px rgba(16,24,40,.08), 0 6px 16px -8px var(--brand);
}
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-lg { min-height: 56px; font-size: 16.5px; padding: 0 24px; }
.btn-block { width: 100%; }
button[disabled] { opacity: .45; pointer-events: none; }

/* ---------- ป้ายสถานะ ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600; line-height: 1.7;
  background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.chip.draft   { background: var(--surface-2); color: var(--muted); }
.chip.printed { background: var(--warn-bg); color: var(--warn); }
.chip.queued,
.chip.sending { background: var(--brand-soft); color: var(--brand-dark); }
.chip.sent    { background: var(--ok-bg); color: var(--ok); }
.chip.error   { background: var(--danger-bg); color: var(--danger); }
.chip.cancelled { background: var(--surface-2); color: var(--faint); text-decoration: line-through; }

/* ---------- รายการใบเบิกหน้าแรก ---------- */
.doc-row {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.doc-row:last-child { border-bottom: 0; }
.doc-main { min-width: 0; flex: 1; }
.doc-no { font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: .01em; }
.doc-meta { color: var(--muted); font-size: 13px; margin-top: 1px; }
.doc-actions { display: flex; gap: 8px; flex-shrink: 0; }
.doc-actions button { min-height: 38px; padding: 0 12px; font-size: 14px; }

/* ---------- แท็บหมวด ---------- */
.tabs {
  display: flex; gap: 6px; padding: 5px;
  background: var(--surface-2); border-radius: 12px;
  margin-bottom: 14px;
}
.tabs button {
  flex: 1; min-height: 40px; padding: 0 10px;
  border: 0; background: transparent; color: var(--muted);
  font-size: 14.5px; border-radius: 8px;
}
.tabs button.on {
  background: var(--surface); color: var(--text);
  box-shadow: 0 1px 3px rgba(16,24,40,.12);
}
.tabs .n { font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }
.tabs button.on .n { color: var(--brand); }

/* ---------- ช่องค้นหา ---------- */
.search-row { display: flex; gap: 8px; margin-bottom: 12px; }
.search-row .search { flex: 1; margin-bottom: 0; }
.search-row button { flex-shrink: 0; font-size: 14px; padding: 0 14px; }
.search {
  position: relative; margin-bottom: 12px;
}
.search input {
  width: 100%; min-height: 46px; padding: 0 14px 0 40px;
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-sm);
}
.search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--faint); }

/* ---------- แถวสินค้า ---------- */
.item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.item:last-child { border-bottom: 0; }
.item.picked { background: color-mix(in srgb, var(--brand-soft) 45%, transparent); }
.thumb {
  width: 46px; height: 46px; flex-shrink: 0;
  border-radius: 9px; object-fit: cover;
  background: var(--surface-2); border: 1px solid var(--line);
}
.thumb-fallback { display: grid; place-items: center; font-size: 18px; }
.item-main { flex: 1; min-width: 0; }
.item-name {
  font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-sub {
  color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- ตัวเพิ่มลดจำนวน ---------- */
.stepper { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.stepper button {
  width: 38px; min-height: 38px; padding: 0;
  border-radius: 9px; font-size: 19px; font-weight: 500;
  color: var(--muted);
}
/* ตัดปุ่ม -/+ ออกตามที่ Mai ขอ (คนแพคพิมพ์ตัวเลขเร็วกว่ากดทีละครั้งอยู่แล้ว)
   ช่องกรอกจึงกว้างขึ้นได้ กดง่ายขึ้นด้วย */
.stepper input {
  width: 110px; min-height: 46px; text-align: center; font-size: 17px;
  font: inherit; font-weight: 650; font-variant-numeric: tabular-nums;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 9px;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input.has { border-color: var(--brand); color: var(--brand-dark); }

/* ---------- แถบล่างติดจอ ---------- */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  border-top: 1px solid var(--line);
}
.dock-in {
  max-width: 900px; margin: 0 auto;
  display: flex; align-items: center; gap: 14px;
}
.dock .count { flex: 1; min-width: 0; }
.dock .count b { font-variant-numeric: tabular-nums; }
.dock .count small { display: block; color: var(--muted); font-size: 12.5px; }

/* ---------- กล่องข้อความ ---------- */
.empty { text-align: center; padding: 44px 20px; color: var(--muted); }
.empty .big { font-size: 40px; line-height: 1; margin-bottom: 10px; opacity: .5; }

.note {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 14px; border-radius: var(--r-sm);
  font-size: 13.5px; line-height: 1.5;
}
.note.warn { background: var(--warn-bg); color: var(--warn); }
.note.err  { background: var(--danger-bg); color: var(--danger); }
.note.ok   { background: var(--ok-bg); color: var(--ok); }

/* ---------- ป๊อปอัพ ---------- */
.modal-bg {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(9, 12, 17, .5); backdrop-filter: blur(3px);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 16px;
}
@media (min-width: 620px) { .modal-bg { align-items: center; } }
.modal {
  width: 100%; max-width: 440px;
  background: var(--surface); border-radius: 18px;
  box-shadow: var(--shadow-lg);
  padding: 22px;
  animation: pop .18s ease-out;
}
@keyframes pop { from { transform: translateY(12px); opacity: 0 } to { transform: none; opacity: 1 } }
.modal h3 { font-size: 17px; margin-bottom: 4px; }
.modal p { color: var(--muted); font-size: 13.5px; margin-bottom: 16px; }
.modal input[type=text] {
  width: 100%; min-height: 50px; padding: 0 14px; margin-bottom: 14px;
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-sm);
}
.modal .row { display: flex; gap: 10px; }
.modal .row button { flex: 1; }

/* ---------- แถบแจ้งเตือนลอย ---------- */
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%);
  z-index: 70; max-width: calc(100% - 32px);
  padding: 12px 18px; border-radius: 999px;
  background: #101820; color: #fff; font-size: 14px; font-weight: 550;
  box-shadow: var(--shadow-lg);
  animation: pop .18s ease-out;
}
.toast.err { background: var(--danger); }
.toast.ok  { background: var(--ok); }

/* ---------- เช็คลิสต์ตอนรับของ ---------- */
.check {
  width: 30px; height: 30px; flex-shrink: 0;
  border-radius: 8px; border: 2px solid var(--line-strong);
  background: var(--surface); cursor: pointer;
  display: grid; place-items: center; color: transparent;
}
.check.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.diff { font-size: 12.5px; font-weight: 650; font-variant-numeric: tabular-nums; }
.diff.short { color: var(--danger); }
.diff.over  { color: var(--warn); }
.diff.same  { color: var(--ok); }

.skeleton {
  height: 66px; border-radius: var(--r-sm);
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--line) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: sk 1.3s ease infinite;
  margin-bottom: 8px;
}
@keyframes sk { from { background-position: 100% 0 } to { background-position: 0 0 } }

/* ================= หน้าพิมพ์ ใบเบิก A4 แนวนอน ครึ่งขวา ================= */
.sheet { display: none; }

@media print {
  @page { size: A4 landscape; margin: 8mm; }
  body { background: #fff; color: #000; }
  .topbar, .dock, .no-print, .toast { display: none !important; }
  .wrap { padding: 0; max-width: none; }
  .sheet { display: block; }
  .sheet + .sheet { page-break-before: always; }

  /* โหมด "ดู" บนจอกางเนื้อหาเต็มความกว้างเพื่ออ่านง่าย
     แต่ตอนพิมพ์ต้องกลับมาอยู่ครึ่งขวาเสมอ ไม่งั้นสั่งพิมพ์จากหน้าดูแล้วได้ผังผิด
     (เจอจริง 2026-09-03: พิมพ์จาก ?preview แล้วเนื้อหากางเต็มแผ่น) */
  .preview .sheet {
    padding: 0 !important; margin: 0 !important;
    background: #fff !important; box-shadow: none !important; border-radius: 0 !important;
  }
  .preview .sheet-inner { width: 50% !important; margin-left: 50% !important; }
}

.sheet-inner {
  /* งานอยู่ครึ่งขวาของแผ่น ตามที่ Mai ใช้อยู่เดิม */
  width: 50%; margin-left: 50%;
  font-size: 13pt; color: #000;
}
.sheet-head { display: flex; align-items: flex-start; gap: 10px; border-bottom: 2px solid #000; padding-bottom: 6px; }
.sheet-head .t { font-size: 19pt; font-weight: 700; }
.sheet-head .r { margin-left: auto; text-align: right; font-size: 12pt; }
.sheet table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.sheet th, .sheet td { border: 1px solid #000; padding: 5px 7px; font-size: 13pt; }
.sheet th { background: #eee; font-weight: 700; }
.sheet td.num { text-align: right; font-variant-numeric: tabular-nums; }
/* ช่องจำนวนคือตัวเลขที่คลังต้องอ่านจากระยะไกลตอนหยิบของ ทำให้ใหญ่กว่าคอลัมน์อื่น */
.sheet td.qty { font-size: 17pt; font-weight: 700; text-align: center; }
.sheet td.pic { padding: 2px; text-align: center; }
.sheet td.pic img {
  width: 16mm; height: 16mm; object-fit: contain; display: block; margin: 0 auto;
}
/* เบราว์เซอร์ตัดรูป/สีพื้นออกตอนพิมพ์เป็นค่าเริ่มต้น ต้องสั่งให้พิมพ์ตามที่เห็น */
.sheet, .sheet * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.sheet .sign { margin-top: 26px; display: flex; gap: 26px; font-size: 12pt; }
.sheet .sign div { flex: 1; border-top: 1px dotted #000; padding-top: 4px; text-align: center; }

/* ดูตัวอย่างใบเบิกบนจอ */
.preview .sheet { display: block; background: #fff; color: #000;
  padding: 14mm; border-radius: 8px; box-shadow: var(--shadow); margin-bottom: 14px; }
.preview .sheet-inner { width: 100%; margin-left: 0; }


/* ---------- จอแคบ: บีบตัวเพิ่มลดให้เหลือที่ให้ชื่อสินค้ามากขึ้น ----------
   วัดบนจอ 375px แล้วพบว่าตัวเพิ่มลดกินไป ~146px จนชื่อโดนตัดเกือบทุกแถว */
@media (max-width: 430px) {
  .item { gap: 9px; padding: 9px 12px; }
  .thumb { width: 38px; height: 38px; border-radius: 8px; }
  .stepper { gap: 3px; }
  .stepper input { width: 86px; min-height: 44px; font-size: 16px; }
  .item-name { font-size: 14.5px; }
  .item-sub { font-size: 11.5px; }
  .wrap { padding-left: 12px; padding-right: 12px; }
  .doc-actions button { padding: 0 10px; font-size: 13.5px; }
}

/* จอเล็กมาก (iPhone SE) บีบเพิ่มอีกนิด */
@media (max-width: 360px) {
  .stepper input { width: 74px; }
}

/* ================= หน้ายืนยันยอด ================= */

/* แถบ "ตรวจแล้ว x/y" ตรึงใต้หัวจอ ตามระบบเดิม
   ต้องอยู่นอก .card ไม่งั้น sticky ตายเมื่อการ์ดเลื่อนพ้นจอ (บทเรียนจากโปรเจกต์เบิกกล่อง) */
.checkbar {
  position: sticky; top: 62px; z-index: 20;
  margin: -4px 0 12px; padding: 12px 16px;
  border-radius: var(--r-sm);
  background: var(--warn-bg); color: var(--warn);
  font-size: 18px; font-weight: 700; text-align: center;
  box-shadow: var(--shadow);
  transition: background .2s ease, color .2s ease;
}
.checkbar.ok { background: var(--ok-bg); color: var(--ok); }

h2.sec {
  font-size: 14px; color: var(--muted); font-weight: 600;
  margin: 22px 0 8px 2px;
}

/* แถวที่ตรวจแล้ว: พื้นเทา ชื่อขีดฆ่า รูปเป็นขาวดำ ช่องกรอกจาง
   ให้กวาดตาแล้วรู้ทันทีว่าเหลืออะไรยังไม่ตรวจ */
.item.conf.done { background: var(--surface-2); }
.item.conf.done .item-name { text-decoration: line-through; color: var(--muted); }
.item.conf.done .thumb { filter: grayscale(1); opacity: .55; }
.item.conf.done .stepper input { opacity: .5; }

/* ปุ่มติ๊กอยู่ขวาสุด หลังช่อง "ได้จริง" ตามผังเดิม */
.item.conf .check { margin-left: 2px; }

/* ---------- กล่องเลือกของนอกใบเบิก ---------- */
.ex-list {
  max-height: 46vh; overflow-y: auto;
  margin: 4px 0 14px; border: 1px solid var(--line); border-radius: var(--r-sm);
}
.ex-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; min-height: 52px;
  border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
  background: transparent; text-align: left; font-weight: 500;
}
.ex-row:last-child { border-bottom: 0; }
.ex-row:hover { background: var(--surface-2); }
.ex-row img { width: 34px; height: 34px; object-fit: cover; border-radius: 7px; flex-shrink: 0; }
.ex-row .ex-ic { width: 34px; text-align: center; font-size: 18px; flex-shrink: 0; }
.ex-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ex-sku { color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; flex-shrink: 0; }

.toast.warn { background: var(--warn); color: #fff; }
