/* พิมพ์ฉลากบาร์โค้ด (Labels.razor, LabelCard.razor)
   - lbl-*  : หน้าจอ (รายการสินค้า ตั้งค่า ตัวอย่าง)
   - lblp-* : ตัวฉลาก ใช้ทั้งตัวอย่างบนจอและตอนพิมพ์ (ขนาดเป็น mm, ตัวอักษรเป็น pt)
   - พิมพ์: html[data-print="labels"] ซ่อนทุกอย่างยกเว้น .lblp-area
     ขนาดหน้ากระดาษ (@page) มาจาก <style> ในหน้า Labels (เปลี่ยนตามรุ่นฉลาก) */

/* ---------- โครงหน้า (รอบ 27: หน้าเดียวจบ) ----------
   จอคอม: หัว + เครื่องมือ / ซ้าย รายการ (เลื่อนในกรอบ) / ขวา ตัวอย่าง (เลื่อนในกรอบ)
   มือถือ: แท็บ รายการ | ตัวอย่าง + ปุ่มพิมพ์ติดล่าง */
.lbl-app { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.lbl-app > * { min-width: 0; }
.lbl-app .alert { margin: 0; }

.lbl-top { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap; }
.lbl-title { min-width: 0; }
.lbl-title .context { margin: 0 0 2px; }
.lbl-title h1 { margin: 0; }
.lbl-tools { display: flex; align-items: center; gap: 10px; }

.lbl-size-btn {
  display: inline-flex; align-items: center; gap: 10px; min-height: 48px; padding: 6px 12px 6px 8px;
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--ink);
  font: inherit; text-align: left; cursor: pointer; box-shadow: var(--shadow-sm, none);
}
.lbl-size-btn:hover { border-color: var(--brand); }
.lbl-size-ico { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; background: var(--brand-soft); color: var(--brand); flex: none; }
.lbl-size-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.lbl-size-text small { font-size: .72rem; color: var(--muted); }
.lbl-size-text strong { font-size: .95rem; font-weight: 700; white-space: nowrap; }
.lbl-size-caret { color: var(--muted); flex: none; }
.lbl-print-btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 0 22px; font-weight: 700; white-space: nowrap; }

.lbl-tabs { display: none; }
.lbl-mbar { display: none; }

.lbl-body { display: grid; grid-template-columns: minmax(360px, 440px) minmax(0, 1fr); gap: 16px; align-items: start; }
.lbl-body > .panel { margin: 0; min-width: 0; display: flex; flex-direction: column; padding: 16px; }
.lbl-panel h2, .lbl-preview-head h2 { font-size: 1.02rem; margin: 0; }

/* ---------- ค้นหา/เพิ่มสินค้า ---------- */
.lbl-search { position: relative; flex: none; margin-bottom: 10px; }
.lbl-search-box {
  display: flex; align-items: center; gap: 8px; padding: 0 12px; min-height: 46px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--muted);
}
.lbl-search-box:focus-within { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.lbl-search-box input { flex: 1; min-width: 0; border: 0; outline: 0; background: none; font: inherit; font-size: 16px; color: var(--ink); padding: 10px 0; }
.lbl-results {
  position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 4px);
  max-height: min(360px, 60vh); overflow-y: auto; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow-lg);
}
.lbl-result {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; min-height: 48px;
  border: 0; border-radius: 10px; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.lbl-result:hover, .lbl-result:focus-visible { background: var(--brand-tint); }
.lbl-result-text { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.3; }
.lbl-result-text strong { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lbl-result-text small { font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lbl-result-add {
  flex: none; display: grid; place-items: center; width: 30px; height: 30px; border-radius: 999px;
  background: var(--brand-soft); color: var(--brand); font-weight: 700; font-size: 1.1rem;
}
.lbl-results-empty, .lbl-results-more { margin: 6px 10px; }
.lbl-flash { margin: 6px 2px 0; font-size: .85rem; color: var(--ok); }
.lbl-flash.bad { color: var(--danger); }

/* ---------- รายการที่จะพิมพ์ ---------- */
.lbl-list-head { flex: none; display: flex; align-items: center; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.lbl-list-head .lbl-clear { margin-left: auto; }
.lbl-bulk-n { display: inline-flex; align-items: center; gap: 6px; font-size: .88rem; color: var(--muted); white-space: nowrap; }
.lbl-bulk-n input {
  width: 64px; font: inherit; font-size: 16px; text-align: right; padding: 5px 8px;
  border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); color: var(--ink);
}
.lbl-missing {
  flex: none; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px;
  margin-bottom: 8px; padding: 10px 12px; border-radius: 12px; background: var(--warn-soft); color: #6B4500; font-size: .88rem;
}
.lbl-missing small { opacity: .85; }
.lbl-scroll { min-height: 0; }
.lbl-empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  padding: 36px 16px; text-align: center; color: var(--muted); border: 1.5px dashed var(--line); border-radius: 14px;
}
.lbl-empty p { margin: 0; max-width: 260px; font-size: .92rem; }
.lbl-empty svg { opacity: .55; }

.lbl-rows { display: flex; flex-direction: column; gap: 8px; }
.lbl-row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 6px; align-items: start;
  padding: 10px 10px 10px 12px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface);
}
.lbl-row.is-skip { opacity: .7; }
.lbl-prod { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.lbl-prod strong { font-weight: 600; overflow-wrap: anywhere; line-height: 1.3; }
.lbl-code-text { font-size: .8rem; color: var(--muted); letter-spacing: .03em; overflow-wrap: anywhere; }
.lbl-code-miss { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 6px; }
.lbl-code-miss .tag { margin: 0; font-size: .72rem; }
.lbl-code-miss small { font-size: .78rem; }
.lbl-remove { width: 34px; height: 34px; margin: -4px -4px 0 0; color: var(--muted); border: 0; background: none; }
.lbl-remove:hover { color: var(--danger); }
.lbl-row-ctl { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.lbl-unit { flex: 0 1 140px; min-width: 0; }
.lbl-unit select {
  width: 100%; font: inherit; font-size: 15px; padding: 6px 8px; min-height: 38px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink);
}
.lbl-unit-one { display: inline-block; padding: 6px 10px; border-radius: 10px; background: var(--chip); font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.lbl-price { flex: 1 1 auto; font-size: .88rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lbl-qty { flex: none; display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; background: var(--surface); }
.lbl-qty input {
  width: 52px; border: 0; outline: 0; background: none; text-align: center; font: inherit; font-size: 16px; font-weight: 700; color: var(--ink); padding: 6px 0;
}
.lbl-step {
  width: 36px; height: 38px; border: 0; background: var(--chip); color: var(--ink); font: inherit; font-size: 1.15rem; font-weight: 700; cursor: pointer;
}
.lbl-step:hover { background: var(--brand-soft); color: var(--brand); }
/* ป้ายกำกับช่อง: ให้โปรแกรมอ่านหน้าจออ่านได้ ไม่แสดงบนจอ */
.lbl-lbl { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- ตั้งค่า (ป๊อปอัป) ---------- */
.lbl-settings { display: flex; flex-direction: column; gap: 10px; }
.lbl-set-cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.lbl-group-title { margin: 4px 0 6px; font-size: .82rem; font-weight: 700; color: var(--muted); }
.lbl-presets { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.lbl-preset {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; min-height: 66px; padding: 8px 6px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); font: inherit; cursor: pointer; line-height: 1.25;
}
.lbl-preset strong { font-size: .92rem; font-weight: 700; }
.lbl-preset small { font-size: .75rem; }
.lbl-preset:hover { border-color: var(--muted); }
.lbl-preset.on { border-color: var(--brand); background: var(--brand-tint); box-shadow: inset 0 0 0 1px var(--brand); }
.lbl-preset-shape { display: block; border: 1.5px solid currentColor; border-radius: 3px; opacity: .45; }
.lbl-preset.on .lbl-preset-shape { color: var(--brand); opacity: 1; }
.lbl-start { margin: 0; }
.lbl-start input { max-width: 120px; }
.lbl-start small { font-size: .78rem; }
.lbl-opts { display: flex; flex-wrap: wrap; gap: 8px; }
.lbl-opt {
  display: inline-flex; align-items: center; gap: 6px; min-height: 38px; padding: 0 14px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-size: .9rem; user-select: none;
}
.lbl-opt input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.lbl-opt::before { content: ""; width: 14px; height: 14px; border-radius: 4px; border: 1.5px solid var(--muted); flex: none; }
.lbl-opt.on { border-color: var(--brand); background: var(--brand-tint); color: var(--brand-strong, var(--brand)); font-weight: 600; }
.lbl-opt.on::before { border-color: var(--brand); background: var(--brand) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 12 5 5 9-10'/%3E%3C/svg%3E") center / 11px no-repeat; }
.lbl-opt:focus-within { box-shadow: 0 0 0 3px var(--brand-soft); }
.lbl-print-hint { margin: 2px 0 0; line-height: 1.45; }

/* ---------- ตัวอย่าง ---------- */
.lbl-preview-head { flex: none; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin-bottom: 10px; }
.lbl-opts-link { margin-left: auto; }
.lbl-preview-box {
  overflow: auto; padding: 16px; border-radius: 12px; min-height: 240px;
  background: var(--chip); border: 1px solid var(--line);
}
.lbl-preview-empty { margin: 8px 0; text-align: center; }
.lblp-area.is-roll { display: flex; flex-wrap: wrap; gap: 12px; align-content: flex-start; }
.lblp-area.is-sheet { display: flex; flex-wrap: wrap; gap: 16px; justify-content: center; }

/* จอคอม: ทั้งหน้าพอดีจอ รายการกับตัวอย่างเลื่อนในกรอบของตัวเอง */
@media screen and (min-width: 1025px) {
  .main:has(.content > .lbl-app) { overflow: hidden; display: flex; flex-direction: column; }
  .content:has(> .lbl-app) { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; padding: 20px 24px; max-width: none; }
  .lbl-app { flex: 1 1 auto; min-height: 0; }
  .lbl-body { flex: 1 1 auto; min-height: 0; align-items: stretch; }
  .lbl-body > .panel { min-height: 0; overflow: hidden; }
  .lbl-scroll { flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; margin: 0 -8px; padding: 0 8px 4px; }
  .lbl-preview-box { flex: 1 1 auto; min-height: 0; }
}

/* ---------- ตัวฉลาก (จอ + พิมพ์) ---------- */
.lblp-label {
  display: flex; flex-direction: column; align-items: stretch; overflow: hidden; box-sizing: border-box;
  background: #fff; color: #000; text-align: center; line-height: 1.15; font-family: var(--font);
  padding: 1.2mm 1.8mm;
}
.lblp-shop { flex: none; font-size: 6pt; font-weight: 600; letter-spacing: .02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lblp-name {
  flex: none; font-size: 7.5pt; font-weight: 600; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden;
}
.lblp-meta { flex: none; display: flex; justify-content: center; align-items: baseline; gap: 1.5mm; white-space: nowrap; overflow: hidden; }
.lblp-unit { font-size: 6.5pt; overflow: hidden; text-overflow: ellipsis; }
.lblp-unit:empty { display: none; }
.lblp-price { font-size: 9.5pt; font-weight: 700; }
.lblp-price:empty { display: none; }
.lblp-bars { flex: 1 1 0; min-height: 4mm; max-height: 22mm; margin-top: .5mm; }
.lblp-bars svg { display: block; width: 100%; height: 100%; }
.lblp-digits { flex: none; font-size: 7pt; letter-spacing: .06em; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }

/* ขนาดตัวอักษรตามความสูงฉลาก */
.sz-s .lblp-label { padding: .8mm 1.2mm; }
.sz-s .lblp-shop { font-size: 5pt; }
.sz-s .lblp-name { font-size: 6pt; }
.sz-s .lblp-unit { font-size: 5.5pt; }
.sz-s .lblp-price { font-size: 7.5pt; }
.sz-s .lblp-digits { font-size: 6pt; }
.sz-s .lblp-bars { min-height: 3.5mm; margin-top: .3mm; }
.sz-l .lblp-label { padding: 1.6mm 2.2mm; }
.sz-l .lblp-shop { font-size: 7pt; }
.sz-l .lblp-name { font-size: 9pt; }
.sz-l .lblp-unit { font-size: 7.5pt; }
.sz-l .lblp-price { font-size: 12pt; }
.sz-l .lblp-digits { font-size: 8pt; }

/* แผ่น A4: ตารางขนาด mm (คอลัมน์/แถว/ช่องไฟมาจาก style ในหน้า) */
.lblp-sheet { display: grid; box-sizing: content-box; align-content: start; }
.lblp-sheet .lblp-label { width: 100%; height: 100%; }
.lblp-blank { width: 100%; height: 100%; }

@media screen {
  .lblp-area.is-roll .lblp-label { box-shadow: 0 1px 3px rgba(0, 0, 0, .18); border-radius: 1.5mm; }
  /* แสดงเป็นกระดาษ A4 ย่อส่วน (padding = ขอบกระดาษจริง) */
  .lblp-sheet { padding: var(--my, 0) var(--mx, 0); background: #fff; box-shadow: 0 2px 8px rgba(0, 0, 0, .18); zoom: .55; }
  .lblp-sheet .lblp-label { outline: 1px dashed #c9c0b8; outline-offset: -1px; }
  .lblp-blank {
    outline: 1px dashed #c9c0b8; outline-offset: -1px;
    background: repeating-linear-gradient(45deg, #f1ebe5 0 6px, #fff 6px 12px);
  }
}

/* ปุ่มพิมพ์ฉลากในรายละเอียดเอกสารสต็อก (Inventory.razor) */
.lbl-doc-actions { margin: 8px 0 6px; }

/* ---------- จอเล็ก ---------- */
@media (max-width: 1024px) {
  .lbl-body { grid-template-columns: minmax(0, 1fr); }
  .lbl-preview-box { max-height: 70vh; }
}
@media (max-width: 760px) {
  .lbl-app { gap: 10px; }
  .lbl-top { align-items: center; flex-wrap: nowrap; }
  .lbl-title .context { display: none; }
  .lbl-title h1 { font-size: 1.3rem; }
  .lbl-tools .lbl-print-btn { display: none; }
  .lbl-size-btn { min-height: 44px; padding: 4px 10px 4px 6px; gap: 8px; }
  .lbl-size-ico { width: 32px; height: 32px; }
  .lbl-size-text strong { font-size: .88rem; }

  .lbl-tabs {
    display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 14px; background: var(--chip);
    position: sticky; top: calc(52px + env(safe-area-inset-top)); z-index: 9;
  }
  .lbl-tabs button {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 0 8px;
    border: 0; border-radius: 10px; background: none; color: var(--muted); font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .lbl-tabs button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
  .lbl-tab-n { display: inline-grid; place-items: center; min-width: 22px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); font-size: .75rem; }

  .lbl-body.tab-list .lbl-preview-panel, .lbl-body.tab-preview .lbl-list { display: none; }
  .lbl-body > .panel { padding: 12px; }
  .lbl-preview-box { max-height: none; padding: 10px; min-height: 200px; }
  .lblp-area.is-roll { gap: 8px; justify-content: center; }
  .lblp-area.is-roll .lblp-label { zoom: .74; }
  .lblp-sheet { zoom: .38; }

  .lbl-row-ctl { gap: 8px; }
  .lbl-unit { flex-basis: 104px; }
  .lbl-step { width: 32px; }
  .lbl-qty input { width: 44px; }
  .lbl-list-head { flex-wrap: nowrap; }
  .lbl-bulk-n span { display: none; }
  .lbl-list-head .btn { white-space: nowrap; padding-left: 10px; padding-right: 10px; }
  .lbl-size-text small { display: none; }
  .lbl-missing { padding: 8px 10px; }

  .lbl-set-cols { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .lbl-presets { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .lbl-preset { min-height: 58px; padding: 6px 2px; }
  .lbl-preset strong { font-size: .8rem; }
  .lbl-preset small { font-size: .68rem; }

  .lbl-mbar {
    display: block; position: sticky; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 9;
    padding: 8px; border-radius: 18px; background: var(--surface); box-shadow: var(--shadow-lg, 0 6px 24px rgba(0,0,0,.15));
  }
  .lbl-mbar .lbl-print-btn { width: 100%; }
}

/* =====================================================================
   พิมพ์ (html[data-print="labels"])
   ใช้ :has() ผูกกับหน้าที่มี .lblp-area เท่านั้น: ถ้าออกจากหน้านี้แล้ว data-print ยังค้าง
   การพิมพ์ใบเสร็จหน้าอื่นจะไม่โดนกฎนี้ (ใบเสร็จใช้ค่า slip/a4/held ของตัวเอง)
   ===================================================================== */
@media print {
  html[data-print="labels"]:has(.lblp-area),
  html[data-print="labels"]:has(.lblp-area) body {
    margin: 0 !important; padding: 0 !important; height: auto !important; overflow: visible !important;
    background: #fff !important; color: #000 !important;
  }
  /* ทุกอย่างที่ไม่ใช่ฉลากและไม่ได้ห่อฉลากอยู่: ไม่แสดง (ไม่กินที่ ไม่เกิดหน้าว่าง) */
  html[data-print="labels"]:has(.lblp-area) body *:not(:has(.lblp-area)):not(.lblp-area):not(.lblp-area *) { display: none !important; }
  /* กล่องที่ห่อฉลาก (shell, main, panel, กล่องตัวอย่าง): ล้างระยะ/กรอบ/การเลื่อน */
  html[data-print="labels"]:has(.lblp-area) body *:has(.lblp-area) {
    display: block !important; position: static !important; inset: auto !important;
    margin: 0 !important; padding: 0 !important; border: 0 !important; border-radius: 0 !important;
    width: auto !important; max-width: none !important; min-width: 0 !important;
    height: auto !important; min-height: 0 !important; max-height: none !important;
    overflow: visible !important; background: none !important; box-shadow: none !important;
    transform: none !important; filter: none !important; zoom: 1 !important;
  }
  html[data-print="labels"] .lblp-area,
  html[data-print="labels"] .lblp-area * { visibility: visible !important; }
  html[data-print="labels"] .lblp-area { display: block !important; margin: 0 !important; padding: 0 !important; }

  /* ม้วน: 1 ดวง = 1 หน้า (หน้ากระดาษเท่าขนาดดวง ขอบ 0) */
  html[data-print="labels"] .lblp-area.is-roll .lblp-label {
    zoom: 1 !important; box-shadow: none !important; border-radius: 0 !important; margin: 0 !important;
    break-after: page; page-break-after: always; break-inside: avoid;
  }
  html[data-print="labels"] .lblp-area.is-roll .lblp-label:last-child { break-after: auto; page-break-after: auto; }

  /* A4: 1 ตาราง = 1 แผ่น (ขอบกระดาษมาจาก @page ในหน้า) */
  html[data-print="labels"] .lblp-sheet {
    zoom: 1 !important; padding: 0 !important; margin: 0 !important; box-shadow: none !important;
    break-after: page; page-break-after: always; break-inside: avoid; overflow: hidden;
  }
  html[data-print="labels"] .lblp-sheet:last-child { break-after: auto; page-break-after: auto; }
  html[data-print="labels"] .lblp-sheet .lblp-label,
  html[data-print="labels"] .lblp-blank { outline: none !important; background: #fff !important; }
}
