:root {
  --green: #1f7a3a; --green-d: #145a29; --green-l: #e7f5ec;
  --red: #c62828; --red-l: #fdecea; --amber: #b26a00; --amber-l: #fff4df;
  --ink: #1b1b1b; --muted: #666; --line: #e3e5e3; --bg: #f4f5f4; --card: #fff;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 16px/1.45 system-ui, "Noto Sans Thai", "Sarabun", sans-serif; color: var(--ink); background: var(--bg); -webkit-tap-highlight-color: transparent; }
button, input { font: inherit; }
.hidden, [hidden] { display: none !important; }

/* Top bar */
.top { position: sticky; top: 0; z-index: 5; background: var(--green); color: #fff; display: flex; align-items: center; gap: 8px; padding: 10px 12px; min-height: 56px; }
.top h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .sub { font-size: 13px; opacity: .85; font-weight: 400; }
.top button { background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.5); border-radius: 10px; padding: 8px 12px; min-height: 40px; }
.top .back { border: 0; font-size: 24px; padding: 4px 10px; }
.top .count { background: rgba(255,255,255,.18); border-radius: 999px; padding: 4px 12px; font-weight: 700; white-space: nowrap; }

main { max-width: 680px; margin: 0 auto; padding: 12px 16px 24px; }

/* Forms */
.field { display: block; margin: 12px 0; }
.field span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=password], input[type=search] { width: 100%; padding: 12px 14px; border: 1px solid #c9ccc9; border-radius: 12px; min-height: 50px; background: #fff; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 12px 18px; border-radius: 12px; border: 1px solid #c9ccc9; background: #fff; min-height: 50px; cursor: pointer; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; font-weight: 700; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; font-weight: 700; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: default; }
.error { background: var(--red-l); color: var(--red); border-radius: 12px; padding: 12px; margin: 12px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Home */
.home-grid { display: grid; gap: 14px; margin-top: 8px; }
.home-tile { display: flex; align-items: center; gap: 16px; padding: 22px 18px; border-radius: 16px; border: 0; color: #fff; font-size: 22px; font-weight: 700; text-align: left; min-height: 104px; }
.home-tile small { display: block; font-size: 14px; font-weight: 400; opacity: .9; }
.home-tile .icon { font-size: 38px; }
.tile-out { background: #b3261e; }
.tile-in { background: var(--green); }

/* Order list */
.search { display: flex; gap: 8px; margin-bottom: 12px; }
.order-card { display: block; width: 100%; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 10px; color: inherit; }
.order-card .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.order-card .oid { font-weight: 700; font-size: 18px; }
.order-card .who { margin-top: 2px; }
.badge { display: inline-block; font-size: 13px; border-radius: 999px; padding: 2px 10px; background: var(--amber-l); color: var(--amber); white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* Scan screen */
body.scanning-screen { padding-bottom: 150px; }
.cam { position: relative; background: #000; border-radius: 14px; overflow: hidden; aspect-ratio: 16/9; }
.cam video, .cam canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.cam video { object-fit: cover; }
.cam canvas { pointer-events: none; }
.guide { position: absolute; left: 5%; right: 5%; top: 26%; bottom: 26%; border: 3px solid rgba(255,255,255,.85); border-radius: 8px; box-shadow: 0 0 0 2000px rgba(0,0,0,.35); pointer-events: none; transition: border-color .12s; }
.guide::after { content: ""; position: absolute; left: 3%; right: 3%; top: 50%; height: 2px; background: #ff3b30; }
.guide.armed { border-color: #ffd60a; }
.cam .flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .25s; }
.cam .flash.ok { background: rgba(46,160,67,.45); opacity: 1; transition: none; }
.cam .flash.bad { background: rgba(198,40,40,.5); opacity: 1; transition: none; }
.cam .msg { position: absolute; left: 0; right: 0; bottom: 6px; text-align: center; color: #fff; font-size: 13px; text-shadow: 0 1px 2px #000; padding: 0 8px; }
.cam-tools { display: flex; gap: 8px; margin: 8px 0 0; }
.cam-tools .btn { min-height: 40px; padding: 6px 12px; font-size: 14px; }

.result { border-radius: 14px; padding: 12px 14px; margin: 12px 0; border-left: 8px solid var(--line); background: #fff; }
.result .big { font-size: 20px; font-weight: 700; }
.result .code { font: 600 15px ui-monospace, monospace; word-break: break-all; }
.result.ok { border-color: var(--green); background: var(--green-l); }
.result.warn { border-color: var(--amber); background: var(--amber-l); }
.result.bad { border-color: var(--red); background: var(--red-l); }

.progress { height: 10px; background: #dfe3df; border-radius: 999px; overflow: hidden; margin: 6px 0 2px; }
.progress > div { height: 100%; background: var(--green); transition: width .2s; }

.head-info { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin: 12px 0; font-size: 14px; }
.head-info b { font-size: 16px; }

.line { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 10px; display: grid; grid-template-columns: 64px 1fr; gap: 12px; }
.line.done { border-color: #9fd3ae; background: #f6fbf7; }
.line.last-hit { box-shadow: 0 0 0 3px #34c759; }
.line img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; background: #eee; }
.line .ph { width: 64px; height: 64px; border-radius: 10px; background: #eceeec; display: flex; align-items: center; justify-content: center; color: #aaa; font-size: 26px; }
.line .name { font-weight: 600; }
.line .meta { font-size: 13px; color: var(--muted); }
.line .qty { font-size: 18px; font-weight: 700; }
.line .qty.done { color: var(--green); }
.scans { list-style: none; padding: 0; margin: 6px 0 0; grid-column: 1 / -1; }
.scans li { display: flex; align-items: center; justify-content: space-between; gap: 8px; font: 13px ui-monospace, monospace; padding: 4px 0; border-top: 1px dashed var(--line); word-break: break-all; }
.scans li.bad { color: var(--red); }
.scans button { font-family: system-ui; border: 1px solid #d0d3d0; background: #fff; border-radius: 8px; padding: 4px 10px; min-height: 34px; color: var(--red); }

.problems { background: var(--amber-l); color: #6b4300; border-radius: 12px; padding: 10px 14px; margin: 12px 0; font-size: 14px; }
.problems ul { margin: 4px 0 0; padding-left: 18px; }
.extra { border-color: #f1b0ad; background: #fff7f6; }

.manual { display: flex; gap: 8px; margin: 12px 0; }

/* Bottom action bar with the hold-to-scan trigger */
.actionbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; background: rgba(244,245,244,.97); box-shadow: 0 -2px 10px rgba(0,0,0,.08); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.actionbar .inner { max-width: 680px; margin: 0 auto; display: grid; gap: 8px; }
.actionbar .row { display: flex; gap: 8px; }
.actionbar .row .btn { flex: 1; min-height: 44px; font-size: 15px; }
.trigger { width: 100%; min-height: 76px; border: 0; border-radius: 18px; background: var(--green); color: #fff; font-size: 22px; font-weight: 800; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.trigger.active { background: var(--green-d); transform: scale(.985); }
.trigger:disabled { background: #9bb8a4; }
.pending { text-align: center; font-size: 13px; color: var(--amber); }

#toast { position: fixed; left: 50%; top: 70px; transform: translateX(-50%); background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; max-width: 90vw; text-align: center; }
#toast.show { opacity: .95; }

/* Confirm dialog */
dialog { border: 0; border-radius: 16px; padding: 20px; width: min(420px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 8px; }
dialog .actions { display: flex; gap: 8px; margin-top: 16px; }
dialog .actions .btn { flex: 1; }

.done-screen { text-align: center; padding: 40px 10px; }
.done-screen .ico { font-size: 64px; }
