:root {
  --bg: #f3f5f7;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #64707d;
  --line: #e2e6ea;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e6f4f2;
  --ok: #15803d;
  --ok-soft: #e8f6ed;
  --warn: #b45309;
  --warn-soft: #fdf3e3;
  --err: #b91c1c;
  --err-soft: #fdecec;
  --radius: 12px;
  --tabs-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --card: #1a2129;
    --text: #e6eaee;
    --muted: #93a0ad;
    --line: #2b3540;
    --primary: #14b8a6;
    --primary-ink: #04201d;
    --primary-soft: #12302d;
    --ok: #4ade80;
    --ok-soft: #12291b;
    --warn: #fbbf24;
    --warn-soft: #2e2410;
    --err: #f87171;
    --err-soft: #2f1616;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
}
h3 { margin: 0 0 10px; font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--primary); color: var(--primary-ink);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand svg { width: 22px; height: 22px; fill: currentColor; }
.topstats { font-size: 13px; opacity: .9; white-space: nowrap; }

/* ---------- Tabs: bottom on phone, top on desktop ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs button {
  border: 0; background: none; color: var(--muted); font: inherit; font-size: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; cursor: pointer;
}
.tabs button svg { width: 24px; height: 24px; fill: currentColor; }
.tabs button.active { color: var(--primary); font-weight: 600; }

main { max-width: 1100px; margin: 0 auto; padding: 12px 16px; }
.tab { display: none; }
.tab.active { display: block; }

@media (min-width: 900px) {
  body { padding-bottom: 24px; }
  .tabs {
    position: sticky; top: 52px; bottom: auto; height: auto; padding: 0 16px;
    display: flex; justify-content: center; gap: 4px; border-top: 0; border-bottom: 1px solid var(--line);
  }
  .tabs button { flex-direction: row; gap: 8px; font-size: 15px; padding: 12px 20px; border-bottom: 3px solid transparent; }
  .tabs button svg { width: 20px; height: 20px; }
  .tabs button.active { border-bottom-color: var(--primary); }
  main { padding: 20px 24px; }
}

/* ---------- Cards & controls ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.card-head h3 { margin: 0; }

input[type=text], input[type=search], input[type=number], select, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; min-width: 0;
}
textarea { resize: vertical; min-height: 44px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.btn {
  font: inherit; font-weight: 600; cursor: pointer; user-select: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--primary); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.danger { color: var(--err); border-color: var(--err); }
.btn.sm { padding: 6px 10px; font-size: 13px; }
.btn svg { width: 20px; height: 20px; fill: currentColor; }

.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }

.banner { border-radius: 10px; padding: 10px 12px; margin: 10px 16px 0; font-size: 14px; }
.card .banner { margin: 10px 0 0; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.err { background: var(--err-soft); color: var(--err); }
.banner a { color: inherit; font-weight: 700; }

/* ---------- Scan tab ---------- */
.session-strip { font-size: 13px; color: var(--muted); margin-bottom: 10px; display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; }
.session-strip b { color: var(--text); }
.session-strip a { color: var(--primary); font-weight: 600; cursor: pointer; }

.scan-box form { position: relative; }
.scan-row { display: flex; gap: 8px; }
.scan-row input { font-size: 17px; padding: 12px 14px; }
.icon-btn span { display: none; }
@media (min-width: 480px) { .icon-btn span { display: inline; } }

.suggest {
  list-style: none; margin: 6px 0 0; padding: 0; position: absolute; left: 0; right: 0; z-index: 10;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; max-height: 320px; overflow: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,.15);
}
.suggest li { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.suggest li:last-child { border-bottom: 0; }
.suggest li:hover, .suggest li.sel { background: var(--primary-soft); }
.suggest .s-name { font-weight: 600; }
.suggest .s-sub { font-size: 12px; color: var(--muted); }

.cam-wrap { position: relative; margin-top: 12px; border-radius: 10px; overflow: hidden; background: #000; }
#reader { width: 100%; }
#reader video { width: 100% !important; display: block; }
.cam-tools { display: flex; gap: 8px; padding: 8px; background: rgba(0,0,0,.6); }
.cam-tools select { background: #222; color: #fff; border-color: #444; }
.cam-tools .btn { margin-left: auto; }
.cam-flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .25s; }
.cam-flash.ok { background: rgba(21,128,61,.45); opacity: 1; transition: none; }
.cam-flash.err { background: rgba(185,28,28,.45); opacity: 1; transition: none; }

.last { border-width: 2px; transition: background .3s, border-color .3s; }
.last.ok { border-color: var(--ok); background: var(--ok-soft); }
.last.unknown { border-color: var(--warn); background: var(--warn-soft); }
.last-empty { color: var(--muted); text-align: center; padding: 18px 0; }
.last-top { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.last-name { font-size: 19px; font-weight: 700; line-height: 1.25; }
.last-sub { font-size: 13px; color: var(--muted); margin-top: 4px; word-break: break-all; }
.last-tag { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--card); white-space: nowrap; }
.last.ok .last-tag { color: var(--ok); }
.last.unknown .last-tag { color: var(--warn); }
.last-qty { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 12px; }
.last-qty .btn { width: 52px; height: 52px; font-size: 24px; padding: 0; border-radius: 50%; }
.last-qty input { width: 110px; text-align: center; font-size: 28px; font-weight: 800; padding: 6px; }
.last-unit { text-align: center; font-size: 13px; color: var(--muted); margin-top: 4px; }
.last-actions { display: flex; justify-content: center; margin-top: 10px; }

.recent { list-style: none; margin: 0; padding: 0; }
.recent li { display: flex; gap: 10px; align-items: baseline; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.recent li:last-child { border-bottom: 0; }
.recent .r-time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.recent .r-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent .r-delta { font-weight: 700; color: var(--ok); }
.recent .r-delta.neg { color: var(--err); }
.recent .unk { color: var(--warn); }
.recent-empty { color: var(--muted); font-size: 13px; }

/* ---------- Results ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; text-align: center; }
.stat-n { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-l { font-size: 12px; color: var(--muted); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; }
.toolbar input { flex: 1 1 220px; }
.toolbar-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar-btns .btn { flex: 1; }

.table-wrap { overflow-x: auto; margin-top: 8px; }
.res-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.res-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.res-table td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.res-table tr.unk td { background: var(--warn-soft); }
.res-table .c-stt { width: 36px; color: var(--muted); }
.res-table .c-qty { width: 130px; text-align: center; }
.res-table .c-del { width: 36px; }
.res-table .mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.res-table .sub { font-size: 12px; color: var(--muted); margin-top: 2px; word-break: break-all; }
.qty-ed { display: flex; align-items: center; gap: 4px; justify-content: center; }
.qty-ed button { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--text); font-size: 18px; cursor: pointer; padding: 0; }
.qty-ed input { width: 58px; text-align: center; padding: 6px 4px; font-weight: 700; }
.del-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 18px; padding: 4px; }
.del-btn:hover { color: var(--err); }
.empty-note { color: var(--muted); text-align: center; padding: 20px 0 8px; font-size: 14px; }
.m-only { display: block; }
.d-only { display: none; }
@media (max-width: 759px) {
  .res-table .c-stt { width: 22px; padding-left: 0; padding-right: 2px; }
  .res-table .c-qty { width: 112px; }
  .res-table .c-del { width: 26px; padding: 0; }
  .qty-ed { gap: 2px; }
  .qty-ed button { width: 28px; height: 30px; }
  .qty-ed input { width: 46px; }
}
@media (min-width: 760px) {
  .m-only { display: none; }
  .d-only { display: table-cell; }
}
.prod-table td:nth-child(2), .prod-table td:nth-child(1) { white-space: nowrap; }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (min-width: 760px) { .btn-grid { grid-template-columns: repeat(4, 1fr); } }
.btn-grid .btn { white-space: normal; text-align: center; }

.form { display: grid; gap: 12px; }
.form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
@media (min-width: 760px) { #sessionForm { grid-template-columns: 1fr 1fr; } #sessionForm label:nth-child(3), #sessionForm .muted, #sessionForm .btn { grid-column: 1 / -1; } #sessionForm .btn { justify-self: start; } }

/* ---------- Modal & toast ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-box { background: var(--card); border-radius: 14px; width: 100%; max-width: 440px; max-height: 90vh; overflow: auto; padding: 18px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-box h3 { font-size: 18px; }
.modal-box p { margin: 6px 0; }
.modal-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; justify-content: flex-end; }
.modal-btns .btn { flex: 1 1 auto; }
.code-pill { display: inline-block; font-family: ui-monospace, Consolas, monospace; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; word-break: break-all; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 60;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 12px);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 10px;
  background: #1f2937; color: #fff; font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast.ok { background: var(--ok); color: #fff; }
.toast.err { background: #b91c1c; color: #fff; }
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* ---------- Print (A4) ---------- */
#printArea { display: none; }
@page { size: A4 portrait; margin: 12mm 10mm; }
@media print {
  body { background: #fff; color: #000; padding: 0; font-size: 11pt; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; }
  .p-title { text-align: center; font-size: 16pt; font-weight: 700; margin: 0; }
  .p-sub { text-align: center; font-style: italic; font-size: 9pt; margin: 2px 0 10px; }
  .p-info { width: 100%; font-size: 10.5pt; margin-bottom: 8px; border-collapse: collapse; }
  .p-info td { padding: 1px 4px; }
  .p-table { width: 100%; border-collapse: collapse; font-size: 10pt; }
  .p-table th, .p-table td { border: 1px solid #000; padding: 3px 5px; }
  .p-table th { background: #e5e7eb; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .p-table thead { display: table-header-group; }
  .p-table tr { page-break-inside: avoid; }
  .p-table .r { text-align: right; } .p-table .c { text-align: center; }
  .p-sign { display: flex; justify-content: space-around; margin-top: 18px; text-align: center; page-break-inside: avoid; }
  .p-sign div { width: 40%; } .p-sign i { font-size: 9pt; }
  .p-warn { text-align: center; font-weight: 700; font-size: 10pt; border: 1px dashed #000; padding: 2px; margin: 0 0 8px; }
  .p-note { border: 1px solid #000; padding: 4px 6px; margin: 0 0 8px; font-size: 10.5pt; page-break-inside: avoid; }
  .p-note-text { white-space: pre-wrap; word-break: break-word; }
  .p-note-line { border-bottom: 1px dotted #000; height: 16px; }
  .p-stamp { position: fixed; top: 40%; left: 0; right: 0; text-align: center; font-size: 80pt; font-weight: 800; color: rgba(0,0,0,.08);
             transform: rotate(-30deg); pointer-events: none; z-index: 0; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ===================== v2: đăng nhập, phiên, quản trị ===================== */
.boot { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.auth { position: fixed; inset: 0; z-index: 40; overflow: auto; background: var(--bg); display: flex; align-items: flex-start; justify-content: center; padding: 8vh 16px 24px; }
.auth-card { width: 100%; max-width: 400px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 22px; box-shadow: 0 12px 40px rgba(0,0,0,.08); }
.auth-brand { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; }
.auth-brand img { border-radius: 10px; }
.auth-brand b { display: block; font-size: 19px; }
.auth-brand span { color: var(--muted); font-size: 13px; }
.auth .banner { margin: 10px 0 0; }
.auth .form .btn { width: 100%; }
code { font-family: ui-monospace, Consolas, monospace; font-size: .92em; background: var(--bg); padding: 1px 5px; border-radius: 4px; }

.form select, .filters select { appearance: auto; }
input[type=date], input[type=password] { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; min-width: 0; }

.chip { border: 0; border-radius: 99px; padding: 5px 11px; font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer; white-space: nowrap; background: rgba(255,255,255,.18); color: var(--primary-ink); }
.chip.warn { background: #fbbf24; color: #3b2a00; }
.chip.err { background: #fecaca; color: #7f1d1d; }

.badge { display: inline-block; font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 99px; white-space: nowrap; vertical-align: middle; }
.badge.open { background: var(--ok-soft); color: var(--ok); }
.badge.closed { background: var(--primary-soft); color: var(--primary); }
.badge.cancelled { background: var(--err-soft); color: var(--err); }
.badge.role { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.sess-card { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; padding: 12px 14px; }
.sess-card .sc-main { min-width: 0; flex: 1 1 200px; }
.sess-card .sc-code { font-weight: 800; font-size: 16px; }
.sess-card .sc-sub { color: var(--muted); font-size: 13px; }
.sess-card .sc-btns { display: flex; gap: 6px; }

.list-card { display: block; width: 100%; text-align: left; font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 8px; cursor: pointer; }
.list-card:hover { border-color: var(--primary); }
.list-card .lc-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.list-card .lc-code { font-weight: 800; }
.list-card .lc-sub { color: var(--muted); font-size: 13px; margin-top: 3px; }
.list-card .lc-stats { display: flex; gap: 14px; margin-top: 6px; font-size: 13px; }
.list-card .lc-stats b { font-variant-numeric: tabular-nums; }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; }
.filters .date { display: grid; gap: 2px; font-size: 12px; color: var(--muted); font-weight: 600; }
.filters input[type=search], .filters .check, .filters .btn { grid-column: 1 / -1; }
@media (min-width: 900px) {
  .filters { grid-template-columns: 1.2fr 1.2fr 1fr 1fr 1.5fr auto; }
  .filters input[type=search], .filters .btn { grid-column: auto; }
  .filters .check { grid-column: 1 / -1; margin: 0; }
}

.detail-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.detail-head h2 { margin: 0; font-size: 20px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 3px 12px; font-size: 14px; margin-top: 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.actions .btn { flex: 1 1 auto; }
.stats.four { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 640px) { .stats.four { grid-template-columns: repeat(4, 1fr); } }
.people { width: 100%; border-collapse: collapse; font-size: 14px; }
.people td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.people td.n { text-align: right; font-variant-numeric: tabular-nums; }
.pend-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--warn); margin-left: 4px; vertical-align: middle; }
.diff-pos { color: var(--ok); font-weight: 700; }
.diff-neg { color: var(--err); font-weight: 700; }
.res-table td.n, .res-table th.n { text-align: right; font-variant-numeric: tabular-nums; }

.seg { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; margin-bottom: 12px; overflow-x: auto; }
.seg button { flex: 1; border: 0; background: none; font: inherit; font-weight: 600; color: var(--muted); padding: 9px 10px; border-radius: 9px; cursor: pointer; white-space: nowrap; }
.seg button.active { background: var(--primary); color: var(--primary-ink); }
.row-btns { display: flex; gap: 4px; justify-content: flex-end; }
.icon-sm { border: 1px solid var(--line); background: var(--card); color: var(--text); border-radius: 8px; padding: 4px 8px; cursor: pointer; font: inherit; font-size: 13px; }
.icon-sm:hover { border-color: var(--primary); }
.inactive td { opacity: .5; }
.audit-detail { font-size: 12px; color: var(--muted); word-break: break-word; max-width: 360px; }
body:not(.is-admin) .admin-only { display: none !important; }
.choose-list { margin-top: 8px; }
.modal-box .form { margin-top: 8px; }
.modal-box select { width: 100%; }
.log-table td { font-size: 13px; }

/* v2.1: kho được gán, ghi chú nhiều dòng, lịch sử chốt */
.pre { white-space: pre-wrap; word-break: break-word; }
.err-text { color: var(--err); }
.wh-pick { border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; margin: 0; max-height: 220px; overflow: auto; }
.wh-pick legend { font-size: 13px; color: var(--muted); font-weight: 600; padding: 0 4px; }
.wh-pick .check { margin: 6px 0; color: var(--text); font-weight: 400; display: flex; }
.hist { margin-top: 10px; font-size: 13px; }
.hist summary { cursor: pointer; color: var(--muted); font-weight: 600; }
.hist ol { margin: 6px 0 0; padding-left: 20px; }
.modal-box .banner { margin: 8px 0; }
