:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #14171c;
  --muted: #5d6570;
  --line: #dadee4;
  --accent: #0a7a52;
  --accent-text: #ffffff;
  --accent-soft: #e2f3ec;
  --warn-bg: #fff3d1;
  --warn-text: #5c4100;
  --danger: #b42318;
  --bar: #14171c;
  --bar-text: #ffffff;
  --key: #eef0f3;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #1a1f25;
    --text: #eef1f4;
    --muted: #9aa3ad;
    --line: #2e353d;
    --accent: #2fb380;
    --accent-text: #06231a;
    --accent-soft: #143528;
    --warn-bg: #3a2f10;
    --warn-text: #ffe2a0;
    --danger: #ff7a6e;
    --bar: #0b0d10;
    --key: #252b33;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

h1 { font-size: 17px; margin: 0; }
h2 { font-size: 15px; margin: 22px 0 8px; }
h3 { font-size: 14px; margin: 18px 0 6px; }
a { color: var(--accent); }
.muted { color: var(--muted); font-size: 14px; margin: 6px 0; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bar); color: var(--bar-text);
}
.pill { font-size: 13px; padding: 3px 10px; border-radius: 99px; background: #2b3138; color: #d6dbe0; }
.pill.ok { background: #0f5c3f; color: #d8f5e8; }
.pill.wait { background: #8a5a00; color: #fff3d6; }

main { max-width: 640px; margin: 0 auto; }
.view { display: none; padding: 0 16px 16px; }
.view.active { display: block; }
#view-scan { padding: 0 0 16px; }
#view-scan > :not(.camera) { margin-left: 16px; margin-right: 16px; }

button, .btn { touch-action: manipulation; }
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 0 18px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 38px; padding: 0 14px; font-size: 14px; }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin: 10px 0; }

input:not([type=checkbox]), select {
  width: 100%; min-height: 48px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: inherit;
}
.check { display: flex; align-items: center; gap: 10px; margin: 14px 0; }
.check input { width: 22px; height: 22px; }

/* Camera */
.camera {
  position: relative; overflow: hidden; background: #000;
  height: min(44vh, 380px);
}
.camera video { width: 100%; height: 100%; object-fit: cover; display: block; }
.guide { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.guide-box {
  /* Nearly square, so upright barcodes fit as well as lying ones. */
  width: 80%; height: 76%;
  border: 3px solid rgba(255, 255, 255, .85); border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
  transition: border-color .15s;
}
.camera.hit .guide-box { border-color: #3ee08f; }
.camera.skip .guide-box { border-color: #ffb020; }
.cam-msg {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center; text-align: center; padding: 20px;
  background: #0d1013; color: #e8ebee;
}
.cam-msg p { margin: 0; max-width: 34ch; }
.torch {
  position: absolute; right: 12px; bottom: 12px;
  min-height: 40px; padding: 0 14px; border-radius: 99px; border: 0;
  background: rgba(0, 0, 0, .6); color: #fff; font: inherit; font-size: 14px;
}
.torch.on { background: #ffd34d; color: #000; }
.teach {
  position: absolute; left: 10px; right: 10px; top: 10px;
  display: flex; flex-direction: column; gap: 6px;
  padding: 12px; border-radius: var(--radius);
  background: rgba(255, 243, 209, .96); color: #4a3500; font-size: 14px;
}
.teach-actions { display: flex; gap: 8px; justify-content: flex-end; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips span { font: 13px ui-monospace, Menlo, Consolas, monospace; background: #fff; padding: 2px 8px; border-radius: 6px; }

.scan-status { margin: 12px 0 2px; font-weight: 600; min-height: 1.4em; }
.reader-info { margin: 0 0 12px; font-size: 12px; color: var(--muted); min-height: 1.4em; }
.reader-info.bad { color: var(--danger); }
.manual { display: flex; gap: 8px; }
.manual input { flex: 1; font-family: ui-monospace, Menlo, Consolas, monospace; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.list li.tap { cursor: pointer; }
.list li > div:first-child { min-width: 0; }
.list .code { font: 600 15px ui-monospace, Menlo, Consolas, monospace; word-break: break-all; }
.list .sub { color: var(--muted); font-size: 13px; }
.list .counts { font-size: 14px; margin-top: 2px; }
.list .counts b { font-weight: 700; }
.list .tot { flex-shrink: 0; text-align: right; font-size: 13px; color: var(--muted); }
.list .tot b { display: block; font-size: 24px; line-height: 1.1; color: var(--accent); }
.list .pending { color: #8a5a00; font-size: 12px; }

.summary { display: flex; gap: 10px; margin: 16px 0 10px; }
.summary div { flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.summary strong { display: block; font-size: 26px; }
.summary span { color: var(--muted); font-size: 14px; }
.export { display: flex; gap: 10px; margin-bottom: 12px; }
.export .primary { flex: 1; }
#search { margin-bottom: 12px; }

.rules { list-style: none; margin: 0; padding: 0; }
.rules li { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 8px; }
.rules code { font-size: 13px; color: var(--muted); word-break: break-all; }
.rules .row { margin: 8px 0 0; }
.rule-form { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.rule-form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }

/* Bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; min-height: 60px; border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 600; font-size: 15px;
}
.tabs button.active { color: var(--accent); box-shadow: inset 0 3px 0 var(--accent); }

/* Count sheet */
.sheet {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, .45);
}
.sheet-panel {
  width: 100%; max-width: 520px; max-height: 100%; overflow-y: auto;
  background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
}
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.sheet-head .code { font: 700 20px ui-monospace, Menlo, Consolas, monospace; word-break: break-all; }
.sheet-head .muted { margin: 2px 0 0; }
.x { border: 0; background: none; color: var(--muted); font-size: 22px; min-width: 44px; min-height: 44px; }
.warn { background: var(--warn-bg); color: var(--warn-text); border-radius: var(--radius); padding: 10px 12px; font-size: 14px; }
.sheet-title { flex: 1; min-width: 0; }
.sheet-total { display: flex; flex-direction: column; align-items: flex-end; flex-shrink: 0; }
.sheet-total .label { font-size: 13px; color: var(--muted); }
.sheet-total #s-total { font-size: 28px; font-weight: 800; line-height: 1.1; color: var(--accent); }
.fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.field {
  display: flex; flex-direction: column; align-items: flex-start; min-width: 0;
  padding: 8px 10px; border: 2px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--text); font: inherit; text-align: left;
}
.field.active { border-color: var(--accent); background: var(--accent-soft); }
.field .label { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.field .value { font-size: 30px; font-weight: 700; min-height: 1.2em; line-height: 1.2; }
.field .value.empty { color: var(--muted); font-weight: 400; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.keypad button {
  min-height: 54px; border: 0; border-radius: var(--radius);
  background: var(--key); color: var(--text); font: inherit; font-size: 24px; font-weight: 600;
}
.keypad button:active { filter: brightness(.9); }
.keypad .soft { font-size: 17px; color: var(--muted); }
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; }
.sheet-actions .primary { flex: 2; }
.history { font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 8px; }
.history div { padding: 2px 0; }

/* Toasts */
.toasts {
  position: fixed; left: 12px; right: 12px; bottom: calc(72px + env(safe-area-inset-bottom)); z-index: 30;
  display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none;
}
.toast {
  pointer-events: auto; width: 100%; max-width: 520px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 10px 10px 14px; border-radius: var(--radius);
  background: #23282f; color: #fff; font-size: 14px; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}
.toast.warn { background: #6b4a00; }
.toast.error { background: #8f1d15; }
.toast button { flex-shrink: 0; min-height: 38px; padding: 0 12px; border: 0; border-radius: 8px; background: #fff; color: #000; font: inherit; font-weight: 700; font-size: 14px; }
/* While the count sheet is open its buttons sit where toasts go: show them at the top. */
.sheet:not([hidden]) ~ .toasts { top: calc(10px + env(safe-area-inset-top)); bottom: auto; }

@media (max-height: 720px) {
  .keypad button { min-height: 44px; font-size: 21px; }
  .field .value { font-size: 24px; }
  .sheet-panel { gap: 8px; padding-top: 10px; }
  .camera { height: 38vh; }
}
