/* Hey AGGIE — ฝั่งโรงงาน (ใช้สีและชิ้นส่วนจาก styles.css)
   ธีมเดียวกับฝั่งตัวแทน (เก็ทสั่ง 24 ต.ค.): หน้านี้ใช้ body.agent-app + .shell.agent ด้วย
   คอมไม่มีแถบบนดำ (แถบซ้ายกระจกลอยมีโลโก้) มือถือแถบล่างเป็นแคปซูลกระจกลอย */

/* คอม: คำว่า "โรงงาน" ที่เคยอยู่บนแถบบน ย้ายมาใต้โลโก้ในแถบซ้าย (แถบบนซ่อนบนคอม) */
@media (min-width: 768px) {
  .shell.agent .rail::before { order: -2; }
  .shell.agent .rail::after { content: 'โรงงาน'; order: -1; margin: -6px 0 8px; font-size: 12px; font-weight: 600; color: var(--brand-ink); letter-spacing: .02em; }
}
/* มือถือ: แถบล่างลอยทับเนื้อหา จึงดันแถบปริ้น/แถบลองสแกนขึ้นไปอยู่เหนือแถบล่าง */
@media (max-width: 767px) {
  .shell.agent :is(.col-fac, .col-pack) { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
  .shell.agent .print-bar { border-radius: 18px 18px 0 0; }
}

.topbar { display: flex; align-items: center; gap: 14px; }
.topbar-role { flex: none; color: var(--rail-text); font-size: 15px; font-weight: 600; }
.toast { bottom: 90px; } /* อยู่เหนือแถบปริ้น/แถบลองสแกน */

/* ---------- ออเดอร์ (ออฟฟิศ) ---------- */
.col-fac { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); container: fac / inline-size; }
.fac-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.fac { max-width: 1280px; margin: 0 auto; padding: 18px 24px 24px; display: flex; flex-direction: column; gap: 12px; }
.fac-head { display: flex; flex-direction: column; }
.fac-main { min-width: 0; display: flex; flex-direction: column; gap: 12px; }

.agents { display: flex; gap: 8px; margin: 0 -24px; padding: 2px 24px; overflow-x: auto; scrollbar-width: none; }
.agents::-webkit-scrollbar { display: none; }
.agents-title { display: none; margin: 0 0 4px; font-size: 13px; color: var(--text-2); }
.agent-chip {
  flex: none; display: inline-flex; align-items: center; gap: 8px;
  height: 36px; padding: 0 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--text-2); font-size: 14px; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.agent-chip em { font-size: 12.5px; font-style: normal; color: var(--muted); }
.agent-chip.on { background: var(--rail); border-color: var(--rail); color: #fff; }
.agent-chip.on em { color: var(--rail-text); }

.seg { display: flex; gap: 4px; padding: 4px; border-radius: var(--r); background: #ebe8e1; }
.seg button {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 40px; border: 0; border-radius: 9px; background: transparent; color: var(--text-2); font-weight: 600; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(16, 24, 20, .1); }
.seg em { min-width: 22px; padding: 0 7px; border-radius: 999px; background: rgba(0, 0, 0, .06); font-size: 12.5px; font-style: normal; line-height: 20px; }
.seg button.on em { background: var(--brand-soft); color: var(--brand-ink); }

.day-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.day-bar[hidden] { display: none; }
.day-chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--text-2); font-size: 14px; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.day-chip .ic { width: 16px; height: 16px; }
.day-chip.on { background: var(--rail); border-color: var(--rail); color: #fff; }
.day-pick { position: relative; display: inline-flex; }
.day-input { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }

.toolbar { display: flex; align-items: center; gap: 12px; }
.toolbar .search { flex: 1; max-width: 380px; margin: 0 0 0 auto; background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }
.check { display: inline-flex; align-items: center; gap: 10px; padding-left: 16px; font-size: 14px; font-weight: 600; color: var(--text-2); white-space: nowrap; cursor: pointer; }
.check input, .o-row > input { width: 20px; height: 20px; margin: 0; accent-color: var(--brand); cursor: pointer; }

.o-list { display: flex; flex-direction: column; gap: 10px; }
.o-headrow { display: none; }
.o-row {
  display: grid; gap: 4px 12px; align-items: start;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  grid-template-areas: "cb main pcs" "cb agent agent" "cb items items" "cb track track";
  padding: 14px 16px; border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow); cursor: pointer;
}
.o-row > input { grid-area: cb; margin-top: 3px; }
.o-row.sel { background: #fbf6ea; box-shadow: 0 0 0 2px var(--brand), var(--shadow); }
.o-row.fresh { animation: fresh 2.5s ease-out; }
@keyframes fresh { 0%, 50% { background: var(--amber-soft); } }
.o-main { grid-area: main; min-width: 0; }
.o-code { display: flex; align-items: center; flex-wrap: wrap; gap: 2px 10px; }
.o-code time { font-size: 13px; color: var(--muted); }
.o-code time.old { color: var(--amber); font-weight: 600; }
.o-code .tag { margin: 0; }
.o-cust { font-weight: 600; }
.o-agent { grid-area: agent; font-size: 14px; color: var(--text-2); }
.o-label { margin-right: 6px; font-size: 12.5px; color: var(--muted); }
.o-items { grid-area: items; font-size: 14.5px; }
.o-items small { color: var(--text-2); }
.o-pcs { grid-area: pcs; text-align: right; white-space: nowrap; font-size: 14px; color: var(--text-2); }
.o-pcs b { font-size: 18px; color: var(--text); }
.o-track { grid-area: track; display: flex; align-items: baseline; gap: 6px; margin-top: 4px; padding-top: 8px; border-top: 1px dashed var(--line); }
.o-track b { font-size: 15px; }
.empty-note .btn { margin: 10px 4px 0; }

.print-bar {
  flex: none; display: flex; align-items: center; justify-content: flex-end; gap: 14px;
  padding: 12px 24px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--surface);
}
.pb-text { font-size: 14.5px; color: var(--text-2); }
.print-bar .btn { height: 46px; padding: 0 22px; }

/* จอคอม: ตัวแทนอยู่ซ้าย ตารางออเดอร์อยู่ขวา */
@container fac (min-width: 900px) {
  .fac { display: grid; grid-template-columns: 230px minmax(0, 1fr); grid-template-areas: "agents head" "agents main"; gap: 14px 24px; align-items: start; }
  .fac-head { grid-area: head; }
  .fac-main { grid-area: main; }
  .agents {
    grid-area: agents; position: sticky; top: 18px;
    flex-direction: column; gap: 2px; margin: 0; padding: 14px 10px; overflow: visible;
    border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow);
  }
  .agents-title { display: block; padding: 0 6px; }
  .agent-chip { justify-content: space-between; width: 100%; height: 40px; border: 0; border-radius: var(--r-sm); background: transparent; }
  .agent-chip:hover { background: var(--surface-2); }
  .agent-chip.on { background: var(--brand-soft); color: var(--brand-ink); }
  .agent-chip.on em { color: var(--brand-ink); }
  .o-list { gap: 0; overflow: hidden; border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow); }
  .o-headrow, .o-row {
    grid-template-columns: 20px minmax(0, 1.25fr) minmax(0, .85fr) minmax(0, 1.5fr) 56px 150px;
    grid-template-areas: "cb main agent items pcs track"; gap: 16px;
  }
  .o-headrow { display: grid; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface-2); font-size: 13px; font-weight: 600; color: var(--text-2); }
  .o-row { padding: 14px 16px; border-radius: 0; box-shadow: none; }
  .o-row + .o-row { border-top: 1px solid var(--line); }
  .o-row.sel { background: #fbf6ea; box-shadow: inset 3px 0 0 var(--brand); }
  .o-label { display: none; }
  .o-agent { padding-top: 2px; }
  .o-pcs { text-align: left; }
  .o-track { flex-direction: column; margin: 0; padding: 0; border: 0; }
  .print-bar { padding-right: max(24px, calc((100% - 1280px) / 2 + 24px)); }
}

/* ---------- แพ็ค (ใช้เครื่องสแกนอย่างเดียว ตัวใหญ่ อ่านจากระยะยืนได้) ---------- */
.col-pack { position: relative; flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); container: pack / inline-size; }
.pack-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.pack { max-width: 1280px; min-height: 100%; margin: 0 auto; padding: 18px 24px 24px; display: flex; flex-direction: column; gap: 14px; }

.scanbar { display: flex; align-items: center; gap: 14px; min-height: 58px; padding: 8px 18px; border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow); }
.scan-label { flex: none; display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text-2); }
.scan-code { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; font-size: 22px; font-weight: 700; }
.caret { display: inline-block; width: 2px; height: 24px; margin-left: 2px; vertical-align: -4px; background: var(--brand); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
/* รหัสยาว (เช่นลิงก์ในคิวอาร์) ตัดเป็น … ไม่ดันแถบเกินจอ */
.scan-last { flex: 0 1 auto; min-width: 0; max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; color: var(--muted); }
.scan-last b { color: var(--text-2); }

.pack-msg { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-radius: var(--r-lg); font-size: 22px; font-weight: 700; line-height: 1.35; }
.pack-msg .ic { width: 28px; height: 28px; }
.pack-msg span { min-width: 0; overflow-wrap: anywhere; }
.pack-msg.info { background: var(--blue-soft); color: var(--blue); }
.pack-msg.ok { background: var(--green-soft); color: var(--green); animation: pop .25s ease-out; }
.pack-msg.warn { background: var(--amber-soft); color: var(--amber); }
.pack-msg.bad { background: var(--red); color: #fff; animation: shake .4s; }
.pack-msg.done { padding: 20px 22px; background: var(--green); color: #fff; font-size: 28px; animation: pop .3s ease-out; }
.pack-msg.done .ic { width: 36px; height: 36px; }
@keyframes pop { from { transform: scale(.97); } }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }

.pack-idle { flex: 1; display: grid; place-content: center; justify-items: center; gap: 8px; padding: 48px 0; text-align: center; color: var(--text-2); }
.pack-idle .ic { width: 96px; height: 96px; color: var(--brand); stroke-width: 1.5; }
.pack-idle h2 { margin: 8px 0 0; font-size: 32px; color: var(--text); }
.pack-idle p { margin: 0; font-size: 18px; }

.pack-grid { display: flex; flex-direction: column; gap: 14px; }
.box-info { gap: 14px; }
.box-info h3, .box-items h3 { font-size: 16px; color: var(--text-2); }
.big-track { font-size: 32px; font-weight: 700; line-height: 1.2; }
.cust-name { font-size: 28px; font-weight: 700; line-height: 1.3; }
.cust-addr { margin: 4px 0 0; font-size: 20px; line-height: 1.55; }
.box-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 12px; border-top: 1px dashed var(--line); }
.items-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.items-head b { font-size: 18px; }
.progress { height: 10px; border-radius: 5px; background: var(--line); overflow: hidden; }
.progress i { display: block; height: 100%; border-radius: inherit; background: var(--brand); transition: width .2s; }
.pick-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.pick-list li { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 12px 16px; border: 2px solid transparent; border-radius: var(--r); background: var(--surface-2); }
.pick-list li.full { background: var(--green-soft); }
.pick-list li.hit { border-color: var(--green); }
.pick-name { min-width: 0; display: flex; flex-direction: column; }
.pick-name b { font-size: 22px; line-height: 1.3; }
.pick-name small { font-size: 16px; color: var(--text-2); }
.cnt { flex: none; font-size: 22px; font-weight: 600; color: var(--muted); }
.cnt b { font-size: 38px; color: var(--text); }
.pick-list li.full .cnt b { color: var(--green); }

.demo-scan {
  flex: none; display: flex; align-items: center; gap: 8px; overflow-x: auto;
  padding: 10px 24px; border-top: 1px dashed #d6d0c4; background: var(--surface-2); font-size: 13px; color: var(--text-2);
}
.demo-label { flex: none; font-weight: 600; }
.demo-scan button { flex: none; height: 30px; padding: 0 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-size: 13px; white-space: nowrap; cursor: pointer; }
.demo-scan button:hover { border-color: var(--brand); color: var(--brand-ink); }
.demo-scan .demo-cam { display: inline-flex; align-items: center; gap: 6px; border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.demo-scan .demo-cam .ic { width: 16px; height: 16px; }
.demo-scan .demo-cam.on { background: var(--brand-soft); }

/* กล้อง (มีเฉพาะหน้าทดลอง): ลอยมุมขวาล่าง เหนือแถบลองสแกน */
.cam { position: absolute; right: 24px; bottom: 64px; z-index: 5; width: 320px; padding: 8px; border-radius: 14px; background: var(--rail); box-shadow: 0 12px 32px rgba(0, 0, 0, .28); }
.cam[hidden] { display: none; }
.cam-view { aspect-ratio: 4 / 3; overflow: hidden; border-radius: 8px; background: #000; outline: 3px solid transparent; transition: outline-color .15s; }
.cam.hit .cam-view { outline-color: var(--green); }
.cam-view video { display: block; width: 100%; height: 100%; object-fit: cover; }
.cam-view.mirror video { transform: scaleX(-1); }
.cam-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 4px 0; font-size: 13px; color: var(--rail-text); }
.cam-close { display: grid; place-items: center; width: 30px; height: 30px; border: 0; border-radius: 8px; background: rgba(255, 255, 255, .08); color: #fff; cursor: pointer; }
.cam-close .ic { width: 18px; height: 18px; }

/* ---------- ตามแบบร่างพี่เบ็น (24 ต.ค.): ยกเลิก / โหลดซ้ำ / รับตีกลับ / สต็อก — หน้าตาให้แชทออกแบบปรับต่อ ---------- */
.seg em.alert, .seg button.on em.alert { background: var(--red); color: #fff; }
.o-row.locked { cursor: default; }
.o-row.locked > input { opacity: .3; cursor: default; }
.pb-actions { display: flex; gap: 8px; }

.ret-idle { flex: none; padding: 28px 0 12px; }
.ret-idle .ic { width: 72px; height: 72px; }
.ret-list { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; }
.ret-list li { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 12px 16px; border: 2px solid transparent; border-radius: var(--r); background: var(--surface-2); }
.ret-list li.hit { border-color: var(--green); background: var(--green-soft); }
.ret-main { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.ret-main b { font-size: 18px; }
.ret-main small { color: var(--text-2); }
.ret-list time { flex: none; color: var(--muted); }

.stock-page { max-width: 1280px; margin: 0 auto; padding: 18px 24px 24px; display: flex; flex-direction: column; gap: 12px; }
.stock-list { gap: 0; padding-top: 4px; padding-bottom: 4px; }
.stock-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; }
.stock-row + .stock-row { border-top: 1px solid var(--line); }
.stock-qty { flex: none; color: var(--text-2); }
.stock-qty b { font-size: 24px; color: var(--text); }
.stock-qty.neg b { color: var(--red); }

/* จอคอม: ใบปะหน้าอยู่ซ้าย สินค้าที่ต้องใส่อยู่ขวา */
@container pack (min-width: 820px) {
  .pack-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); align-items: start; gap: 16px; }
}

/* ---------- มือถือ ---------- */
@media (max-width: 767px) {
  .brand-box { flex: 1; width: auto; min-width: 0; }
  .toast { bottom: 140px; }
  .topbar-role { font-size: 14px; }
  .fac { padding: 14px 12px 20px; }
  .agents { margin: 0 -12px; padding: 2px 12px; }
  .toolbar { flex-wrap: wrap; }
  .toolbar .search { order: -1; flex-basis: 100%; max-width: none; margin: 0; }
  .check { padding-left: 4px; }
  .print-bar { justify-content: space-between; padding: 10px 12px; flex-wrap: wrap; gap: 8px; }
  .stock-page { padding: 14px 12px 20px; }
  /* แท็บ 4 อัน (เพิ่มแท็บยกเลิก) ให้อยู่บรรทัดเดียว */
  .seg button { gap: 4px; padding: 0 4px; white-space: nowrap; font-size: 13.5px; }
  .seg em { min-width: 18px; padding: 0 5px; }
  .print-bar .btn { padding: 0 16px; }
  .pack { padding: 12px 12px 20px; }
  .scanbar { gap: 10px; padding: 8px 14px; }
  .scan-code { font-size: 18px; }
  .pack-msg { font-size: 18px; padding: 12px 14px; }
  .pack-msg.done { font-size: 22px; padding: 16px; }
  .pack-idle { padding: 32px 0; }
  .pack-idle h2 { font-size: 26px; }
  .big-track { font-size: 24px; }
  .cust-name { font-size: 22px; }
  .cust-addr { font-size: 17px; }
  .pick-name b { font-size: 18px; }
  .pick-name small { font-size: 14px; }
  .cnt { font-size: 18px; }
  .cnt b { font-size: 30px; }
  .demo-scan { padding: 8px 12px; }
  .cam { right: 12px; bottom: 56px; width: 200px; }
  /* แถบลองสแกนถูกดันขึ้นเหนือแถบล่างลอย กล้องต้องขึ้นตามไปด้วย */
  .shell.agent .cam { bottom: calc(84px + 56px + env(safe-area-inset-bottom)); }
}
