/* Barcode Scanner - shared stylesheet.
 *
 * Served to BOTH the scanner page (/) and the full-history page (/history) so
 * the two cannot drift apart: the history rows, the quantity badge, the editor
 * stepper and the brand colours are defined once, here. The camera-specific
 * rules further down are simply unused on the history page.
 *
 * Extracted from an inline <style> block, which is why the comments read as
 * they do.
 */
  :root {
    --brand: #0c768a;
    --brand-dark: #095e6e;
    --brand-tint: #e6f2f4;
    --ink: #14262b;
    --muted: #6b8087;
    --line: #dde6e8;
    --bg: #f5f8f9;
    --card: #ffffff;
    --ok: #1f9d63;
    --warn: #b8860b;
    --err: #c0392b;
    --r: 12px;
    --shadow: 0 1px 2px rgba(16,42,48,.06), 0 6px 18px rgba(16,42,48,.06);
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; }
  body {
    background: var(--bg);
    color: var(--ink);
    font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-text-size-adjust: 100%;
    padding: 0 0 env(safe-area-inset-bottom);
  }
  .wrap { max-width: 560px; margin: 0 auto; padding: 0 14px 40px; }
  /* Keep the last card clear of the floating scan button. */
  body.hasfab .wrap { padding-bottom: 132px; }

  header {
    position: sticky; top: 0; z-index: 20;
    background: rgba(245,248,249,.92);
    backdrop-filter: saturate(160%) blur(8px);
    border-bottom: 1px solid var(--line);
    padding: 11px 0 10px;
    margin-bottom: 14px;
  }
  .headrow { display: flex; align-items: center; gap: 10px; }
  h1 { font-size: 17px; font-weight: 650; margin: 0; letter-spacing: -.01em; }
  .headrow .spacer { flex: 1; }
  .chip {
    display: inline-flex; align-items: center; gap: 6px;
    font-size: 11.5px; font-weight: 600; color: var(--brand);
    background: var(--brand-tint); border: 1px solid #cfe4e8;
    padding: 4px 9px; border-radius: 999px; white-space: nowrap;
  }
  .chip .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
  .chip.warn { color: #8a6400; background: #fdf5e3; border-color: #f0e0b6; }
  .chip.err  { color: var(--err); background: #fdeceb; border-color: #f6d4d0; }

  /* ── camera stage ───────────────────────────────────────── */
  /* Square on a phone. A 3:4 panel ate most of the screen before the operator
     reached any control, and since the feed is landscape the extra height was
     cropping the sides anyway - so square shows MORE width, not less, while
     taking roughly a quarter less screen height. Decoding is unaffected either
     way: frames are read from the full video element, never from this box. */
  .stage {
    position: relative; width: 100%; aspect-ratio: 1 / 1;
    background: #0f1c20; border-radius: var(--r); overflow: hidden;
    box-shadow: var(--shadow);
  }
  /* Wide screens (desktop, landscape phone) get the camera's own shape. */
  @media (min-aspect-ratio: 1/1) { .stage { aspect-ratio: 4 / 3; } }
  .stage video { width: 100%; height: 100%; object-fit: cover; display: block; }
  .stage.off video { visibility: hidden; }

  .stagemsg {
    position: absolute; inset: 0; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 10px;
    color: #cfe0e3; text-align: center; padding: 24px;
  }
  .stagemsg svg { opacity: .55; }
  .stagemsg p { margin: 0; font-size: 13.5px; max-width: 30ch; line-height: 1.45; }
  .stage.off .stagemsg { display: flex; }
  .stage:not(.off) .stagemsg { display: none; }

  /* Reticle: a square target with corner brackets. One square frames both
     kinds of code the operator meets - a 1D barcode lays across it, a QR sits
     inside it - and it now matches the stage, which is already square on a
     phone. It is a guide only: frames are decoded from the full video element,
     never from this box, so a barcode wider than the square still reads. */
  .reticle { position: absolute; inset: 0; pointer-events: none; display: none; }
  .stage:not(.off) .reticle { display: block; }
  /* Sized off the stage HEIGHT and squared up with aspect-ratio, so it always
     fits. The stage is never taller than it is wide (1/1 on a phone, 4/3 on a
     wide screen), which is what makes height the safe dimension to drive from:
     a square 68% as tall as the box can never overflow the sides either. */
  .reticle .band {
    position: absolute; top: 50%; left: 50%; height: 68%;
    aspect-ratio: 1 / 1; transform: translate(-50%, -50%);
    border-radius: 6px;
    box-shadow: 0 0 0 1px rgba(255,255,255,.35), 0 0 0 9999px rgba(6,16,20,.30);
  }
  /* The sweep lives INSIDE the square and is placed in percentages, so it
     travels the square's full height whatever size the stage is. A fixed
     pixel travel, as before, would shrink to a wobble in the middle. */
  .reticle .band .line {
    position: absolute; left: 0; right: 0; top: 50%; height: 2px;
    transform: translateY(-1px);
    background: linear-gradient(90deg, transparent, #57e0c8 15%, #57e0c8 85%, transparent);
    box-shadow: 0 0 10px rgba(87,224,200,.85);
    opacity: .28;
  }
  /* The sweep runs ONLY during a scan. A permanently sweeping line would tell
     the operator the app is searching when it is doing nothing at all. */
  .stage.scanning .reticle .band .line { animation: sweep 1.2s ease-in-out infinite; opacity: 1; }
  .stage.scanning .reticle .band {
    box-shadow: 0 0 0 1px rgba(255,255,255,.65), 0 0 0 9999px rgba(6,16,20,.20);
  }
  @keyframes sweep {
    0%, 100% { top: 8%;  opacity: .35; }
    50%      { top: 92%; opacity: 1; }
  }
  .reticle .corner {
    position: absolute; width: 26px; height: 26px;
    border: 2.5px solid rgba(255,255,255,.85); border-radius: 3px;
  }
  .reticle .c1 { top: 12px; left: 12px;  border-right: 0; border-bottom: 0; }
  .reticle .c2 { top: 12px; right: 12px; border-left: 0;  border-bottom: 0; }
  .reticle .c3 { bottom: 12px; left: 12px;  border-right: 0; border-top: 0; }
  .reticle .c4 { bottom: 12px; right: 12px; border-left: 0;  border-top: 0; }
  @media (prefers-reduced-motion: reduce) { .reticle .band .line { animation: none; } }

  /* Green flash on a successful read, so a noisy shop floor gets visual
     confirmation without needing the beep turned up. */
  .flash {
    position: absolute; inset: 0; pointer-events: none;
    background: #29e08a; opacity: 0; transition: opacity .12s ease-out;
  }
  .flash.on { opacity: .38; }
  /* Amber for a repeat: green says "new item on the list", which is the wrong
     message when the same item was counted again. */
  .flash.warn { background: #ffb020; }

  .badge-live {
    position: absolute; top: 12px; left: 12px;
    display: none; align-items: center; gap: 6px;
    background: rgba(6,16,20,.62); color: #fff;
    font-size: 11px; font-weight: 600; letter-spacing: .02em;
    padding: 4px 9px; border-radius: 999px;
  }
  .badge-live .dot { width: 6px; height: 6px; border-radius: 50%; background: #ff5a5a; }
  .stage.scanning .badge-live .dot { animation: pulse .7s ease-in-out infinite; }
  @keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }
  .stage:not(.off) .badge-live { display: inline-flex; }
  @media (prefers-reduced-motion: reduce) { .badge-live .dot { animation: none; } }

  /* ── buttons ────────────────────────────────────────────── */
  button { font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
  .btn {
    border: 1px solid var(--line); background: var(--card); color: var(--ink);
    border-radius: 10px; padding: 11px 14px; font-weight: 600; font-size: 14px;
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    transition: background .14s, border-color .14s, transform .05s;
    min-height: 44px; /* comfortable tap target */
  }
  .btn:active { transform: translateY(1px); }
  .btn:disabled { opacity: .45; cursor: not-allowed; }
  .btn.primary {
    background: var(--brand); border-color: var(--brand); color: #fff; width: 100%;
    font-size: 15px; padding: 14px;
  }
  .btn.primary:hover:not(:disabled) { background: var(--brand-dark); border-color: var(--brand-dark); }
  .btn.primary.stop { background: #b8382b; border-color: #b8382b; }
  .btn.primary.stop:hover { background: #9d2f24; border-color: #9d2f24; }
  .btn.ghost { background: transparent; }
  .btn.ghost:hover:not(:disabled) { background: var(--brand-tint); border-color: #cfe4e8; }

  /* "Show all" - the way off the scanner page and onto the full list. A link
     rather than a button, because it navigates: a real <a> keeps the browser's
     own back button, middle-click and "open in new tab" all working.
     Same-origin normal navigation also means the scanner page is re-created on
     return, so it re-reads storage instead of trusting a stale copy in memory. */
  .showall {
    display: flex; width: 100%; margin-top: 10px; box-sizing: border-box;
    text-decoration: none; color: var(--brand-dark); font-weight: 600;
  }
  .showall[hidden] { display: none; }

  .controls { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
  .controls .btn.primary { flex: 1 1 100%; }

  /* Beep volume. A segmented control rather than a toggle, because how loud a
     phone can beep depends on the phone and on the noise around it - the one
     setting that genuinely has to be adjustable per shop. */
  .volrow { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
  .rowlbl {
    font-size: 11px; font-weight: 700; letter-spacing: .07em;
    text-transform: uppercase; color: var(--muted); white-space: nowrap;
  }
  .seg {
    display: flex; flex: 1; min-width: 0;
    border: 1px solid var(--line); border-radius: 10px;
    overflow: hidden; background: var(--card);
  }
  .segbtn {
    flex: 1 1 0; min-width: 0; border: 0; border-left: 1px solid var(--line);
    background: transparent; color: var(--ink);
    padding: 10px 4px; font-size: 13px; font-weight: 600; min-height: 44px;
    transition: background .14s, color .14s;
  }
  .segbtn:first-child { border-left: 0; }
  .segbtn.on { background: var(--brand); color: #fff; }
  .segbtn:hover:not(.on) { background: var(--brand-tint); }

  /* ── generic card ───────────────────────────────────────── */
  .card {
    background: var(--card); border: 1px solid var(--line);
    border-radius: var(--r); box-shadow: var(--shadow);
    padding: 14px; margin-top: 14px;
    /* scroll-margin reserves the sticky header above and the floating scan
       button below, so a card scrolled into view is never covered by either. */
    scroll-margin-top: 76px; scroll-margin-bottom: 128px;
  }
  .card h2 {
    font-size: 12px; text-transform: uppercase; letter-spacing: .07em;
    color: var(--muted); margin: 0 0 10px; font-weight: 700;
  }

  /* ── last scan ──────────────────────────────────────────── */
  #lastCard { text-align: center; }
  #lastCard .code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 26px; font-weight: 700; letter-spacing: .01em;
    word-break: break-all; line-height: 1.25; margin: 2px 0 8px;
  }
  #lastCard .meta { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
  .pill {
    font-size: 11.5px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
    background: var(--brand-tint); color: var(--brand-dark); border: 1px solid #cfe4e8;
  }
  .pill.plain { background: #f1f5f6; color: var(--muted); border-color: var(--line); }

  /* ── undoing a misread ──────────────────────────────────── */
  /* A misread barcode is indistinguishable from a correct one, on screen and in
     the CSV, so the only useful remedy is an immediate reversal. The offer is
     shown only while there is something to reverse, and the change is confirmed
     first: taking a unit back off a line that was counted correctly is the more
     expensive of the two mistakes, and the button sits right where the thumb
     already is. */
  .undobar { margin-top: 12px; }
  .undobar[hidden] { display: none; }
  .undobar .btn { width: 100%; }
  .undoconfirm {
    margin-top: 12px; padding: 11px 10px; border-radius: 10px;
    background: #f6fafb; border: 1px solid #e3edef; text-align: center;
  }
  .undoconfirm[hidden] { display: none; }
  .undotext { font-size: 12.5px; line-height: 1.45; color: var(--ink); margin: 0 0 10px; }
  .undoacts { display: flex; gap: 8px; }
  .undoacts .btn { flex: 1 1 0; min-width: 0; font-size: 13.5px; }
  .btn.danger {
    background: var(--err); border-color: var(--err); color: #fff; font-weight: 700;
  }
  .btn.danger:hover:not(:disabled) { background: #a93026; border-color: #a93026; }
  /* ── manual entry ───────────────────────────────────────── */
  .field { display: flex; gap: 8px; }
  .field input {
    flex: 1; min-width: 0; font: inherit;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
    background: #fbfdfd; color: var(--ink); min-height: 44px;
  }
  .field input:focus { outline: 2px solid var(--brand-tint); border-color: var(--brand); }
  .hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

  /* ── history ────────────────────────────────────────────── */
  .hrow {
    display: flex; align-items: center; gap: 10px;
    padding: 9px 0; border-top: 1px solid #eef3f4;
  }
  .hrow:first-of-type { border-top: 0; }
  .hrow .body { flex: 1; min-width: 0; }
  .hrow .code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 14.5px; font-weight: 600; word-break: break-all; line-height: 1.3;
  }
  .hrow .sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
  .hrow .mini {
    border: 1px solid var(--line); background: #fff; color: var(--muted);
    border-radius: 8px; width: 34px; height: 34px; padding: 0;
    display: inline-flex; align-items: center; justify-content: center; flex: none;
  }
  .hrow .mini:hover { background: var(--brand-tint); color: var(--brand-dark); border-color: #cfe4e8; }
  /* Quantity badge. A repeated barcode is ONE line with a count, not a new line
     per scan, so this badge is the entire repeat indicator - it replaces the old
     "repeat" tag that appeared on every duplicate line.
     It is now shown on EVERY line, not just repeats: a stock count should state
     its quantity explicitly, and a permanent badge gives the row an obvious tap
     target for correcting a miscount. Amber while the count is above 1 keeps the
     repeat signal loud; a plain grey x1 does not shout. */
  .qtybadge {
    display: inline-block; margin-left: 7px; vertical-align: 2px;
    /* .code is monospace; the badge is a label, so reset to the UI font. */
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 11.5px; font-weight: 800; letter-spacing: .01em;
    color: var(--muted); background: #f4f8f9; border: 1px solid var(--line);
    border-radius: 999px; padding: 1px 8px; white-space: nowrap;
    cursor: pointer;
  }
  .qtybadge.on { color: #7a4a05; background: #fdf1d8; border-color: #efd9a4; }

  /* ── editing a line's quantity ──────────────────────────── */
  /* The row's body is the tap target. It is a control now, so it must not
     behave like selectable text. */
  .hrow .body { cursor: pointer; -webkit-tap-highlight-color: transparent; }
  .hrow .body:focus-visible { outline: 2px solid var(--brand-tint); border-radius: 6px; }
  .hrow.editing { flex-wrap: wrap; }
  .hrow.editing .body { cursor: default; }
  /* Two lines, because one does not fit on a phone. As a single row this was
     461px wide inside a 390px screen, which pushed "Remove line" off the edge
     where it could not be reached. Line 1 is the stepper (the thing you came
     for), line 2 is the two actions.
     min-width:0 is essential: a flex item defaults to min-width:auto, so it
     refuses to shrink below its content and silently overflows the card. */
  .editrow {
    flex-basis: 100%; min-width: 0; display: flex; flex-wrap: wrap;
    align-items: center; gap: 8px;
    margin: 10px 0 2px; padding: 10px; border-radius: 10px;
    background: #f6fafb; border: 1px solid #e3edef;
  }
  .editrow .step {
    border: 1px solid var(--line); background: #fff; color: var(--ink);
    border-radius: 9px; width: 46px; height: 46px; padding: 0;
    font-size: 22px; font-weight: 700; line-height: 1;
    display: inline-flex; align-items: center; justify-content: center;
    flex: none; font-family: inherit; cursor: pointer;
  }
  .editrow .step:hover { background: var(--brand-tint); border-color: #cfe4e8; }
  .editrow .step:disabled { opacity: .4; }
  /* Big enough to hit with a thumb, and monospace so the digits do not jitter
     sideways as the number changes. flex-basis 0 + min-width 0 lets it shrink
     with the card instead of forcing it wider. */
  .editrow .qtyin {
    flex: 1 1 0; min-width: 0; height: 46px; text-align: center;
    font: inherit; font-weight: 800; font-size: 17px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    border: 1px solid var(--line); border-radius: 9px;
    background: #fff; color: var(--ink);
  }
  .editrow .qtyin:focus { outline: 2px solid var(--brand-tint); border-color: var(--brand); }
  .editrow .acts {
    flex-basis: 100%; display: flex; align-items: center; gap: 8px;
    justify-content: flex-end;
  }
  .editrow .done {
    border: 0; background: var(--brand); color: #fff; font-weight: 700;
    font-size: 13.5px; border-radius: 9px; height: 42px; padding: 0 18px;
    flex: none; font-family: inherit; cursor: pointer;
  }
  .editrow .rm {
    border: 1px solid transparent; background: transparent; color: var(--err);
    font-size: 12.5px; font-weight: 700; padding: 0 10px; height: 42px;
    flex: none; font-family: inherit; cursor: pointer;
    margin-right: auto;
  }
  .editrow .rm:hover { border-color: #f2c9c9; background: #fdf4f4; border-radius: 9px; }
  /* An edited count is not the same as a scanned one - say so, so a
     hand-corrected number can never be mistaken for what the scanner read. */
  .editedmark { color: var(--warn); font-weight: 700; }
  .empty-note { color: var(--muted); font-size: 13px; text-align: center; padding: 10px 0; }
  .toolbar { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
  .toolbar .btn { flex: 1 1 0; min-width: 120px; font-size: 13px; padding: 9px 10px; }

  /* ── toast ──────────────────────────────────────────────── */
  /* ── floating scan button ───────────────────────────────── */
  /* Fixed to the bottom centre of the screen so the thumb lands on it without
     a reach, and it stays put instead of scrolling away. */
  .scanbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 55;
    display: none; justify-content: center;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    pointer-events: none;
  }
  .scanbar.show { display: flex; }

  .scanfab {
    position: relative;
    width: 92px; height: 92px; border-radius: 50%; border: 0;
    background: radial-gradient(circle at 50% 30%, #f0564a, #cf1f19 72%);
    color: #fff; pointer-events: auto; padding: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    box-shadow:
      0 12px 28px rgba(176,26,20,.40),
      0 3px 8px rgba(0,0,0,.22),
      inset 0 -3px 8px rgba(0,0,0,.16),
      inset 0 2px 3px rgba(255,255,255,.28);
    transition: transform .12s ease, filter .12s ease;
    -webkit-user-select: none; user-select: none;
  }
  .scanfab .txt { font-size: 10.5px; font-weight: 800; letter-spacing: .11em; line-height: 1; }
  .scanfab:active:not(:disabled) { transform: scale(.94); }
  .scanfab:disabled { cursor: default; filter: saturate(.75) brightness(.94); }
  .scanfab .ring {
    position: absolute; inset: -8px; border-radius: 50%;
    border: 3px solid rgba(207,31,25,.55); opacity: 0;
  }
  /* An expanding ring while hunting tells the operator to keep the phone
     steady, without them having to read anything mid-scan. */
  .scanfab.busy .ring { animation: fabpulse 1.1s ease-out infinite; }
  @keyframes fabpulse {
    0%   { opacity: .9; transform: scale(.90); }
    100% { opacity: 0;  transform: scale(1.30); }
  }
  @media (prefers-reduced-motion: reduce) {
    .scanfab.busy .ring { animation: none; opacity: .65; }
  }

  #toast {
    position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%) translateY(16px);
    background: #14262b; color: #fff; padding: 10px 16px; border-radius: 999px;
    font-size: 13.5px; font-weight: 600; box-shadow: 0 8px 24px rgba(0,0,0,.25);
    opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s;
    z-index: 60; max-width: 88vw; text-align: center;
  }
  #toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
  /* Otherwise the message sits underneath the button that triggered it. */
  body.hasfab #toast { bottom: 130px; }
  /* A repeat is worth noticing, so it is amber and stays up longer. */
  #toast.warn { background: #8a4b06; }

  /* The result card turns amber on a repeat, so a miscount is visible at a
     glance instead of hiding as an extra line further down the list.
     var(--shadow) is kept so the card lifts off the page exactly as before. */
  #lastCard.repeat {
    background: #fffbf2; border-color: #efce8c;
    box-shadow: inset 4px 0 0 var(--warn), var(--shadow);
  }
  #lastCard.repeat h2 { color: var(--warn); }

  .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  details summary { cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; }
  details[open] summary { margin-bottom: 10px; }
  .sample-row { display: flex; gap: 8px; flex-wrap: wrap; }
  .sample-row .btn { flex: 1 1 44%; font-size: 13px; padding: 9px 10px; }
