/*
 * OptiFlow v3 - shared design system.
 *
 * Every page under templates/ is a standalone HTML file with no
 * {% extends %} - this file is what actually unifies them, linked from
 * each page's <head> instead of each page reinventing its own copy of
 * the same tokens/buttons/modals/tables. Page-specific layout (the
 * unique panels/grids each page needs) stays in that page's own
 * <style> block; only things meant to look/behave identically
 * everywhere belong here.
 *
 * Renaming or removing a class here means finding every template that
 * uses it - there's no compiler to catch a stale reference.
 */

/*
 * IBM Plex, served from this machine.
 *
 * These used to come from fonts.googleapis.com, linked in the <head> of
 * 31 templates ABOVE this file. That is a render-blocking request to the
 * internet in an app that otherwise never leaves the LAN, and the lab's
 * Wi-Fi has no reliable internet: iPad Safari would block first paint
 * until the request timed out, every cold page load. It never showed on
 * the server PC, which had it cached.
 *
 * It bit the iPad harder than a Windows client for a second reason - the
 * fallback stack below is 'Segoe UI', which does not exist on iPadOS, so
 * the iPad had more reason to sit and wait for the webfont.
 *
 * latin subset only: its range is U+0000-00FF, which covers the N-tilde
 * in Filipino patient and clinic names. latin-ext would only add Eastern
 * European characters this lab has no use for.
 *
 * IBM Plex is SIL Open Font License 1.1, so redistributing it inside the
 * .exe is fine. Sans is a variable font - all four weights are the same
 * file, declared separately exactly as Google's own stylesheet did.
 */
@font-face { font-family: 'IBM Plex Sans'; font-style: normal; font-weight: 400;
  font-display: swap; src: url('../fonts/ibm-plex-sans-var-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Sans'; font-style: normal; font-weight: 500;
  font-display: swap; src: url('../fonts/ibm-plex-sans-var-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Sans'; font-style: normal; font-weight: 600;
  font-display: swap; src: url('../fonts/ibm-plex-sans-var-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Sans'; font-style: normal; font-weight: 700;
  font-display: swap; src: url('../fonts/ibm-plex-sans-var-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Mono'; font-style: normal; font-weight: 400;
  font-display: swap; src: url('../fonts/ibm-plex-mono-400-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Mono'; font-style: normal; font-weight: 500;
  font-display: swap; src: url('../fonts/ibm-plex-mono-500-latin.woff2') format('woff2'); }
@font-face { font-family: 'IBM Plex Mono'; font-style: normal; font-weight: 600;
  font-display: swap; src: url('../fonts/ibm-plex-mono-600-latin.woff2') format('woff2'); }

:root {
    /* Sidebar */
    --sidebar: #0F172A; --sidebar-ink: #AEB9CC; --sidebar-active-bg: #1B2740; --sidebar-line: #22304A;

    /* Surfaces & text */
    --canvas: #F5F6F8; --card: #FFFFFF; --ink: #101826; --muted: #5E6B7D; --line: #E1E5EA;

    /* Semantic colors (each with a soft background for pills/badges) */
    --accent: #2563EB; --accent-soft: #EAF0FE; --accent-dark: #1D4FD1;
    --good: #16A34A; --good-soft: #E9F8EE; --good-ink: #166A3B;
    --warn: #D97706; --warn-soft: #FEF3E2; --warn-ink: #8A5A0A;
    --bad: #DC2626; --bad-soft: #FDEBEA; --bad-ink: #A5241C;
    /* *-ink: the readable text colour on the matching *-soft background. */
    --placeholder: #A6AFBB; --highlight: #FAFBFF;

    /* Fixed rider colors - was hardcoded hex duplicated per-page; now one
       source, and any page assigns them by CSS class name (see badges below)
       rather than inline style. */
    --rider-1: #8E44AD; --rider-2: #E67E22; --rider-3: #3498DB;

    /* Spacing scale - use these for new/updated rules instead of an
       arbitrary px value. */
    --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
    --space-5: 24px; --space-6: 32px;

    /* Radius scale */
    --radius-sm: 6px; --radius: 8px; --radius-lg: 12px; --radius-pill: 999px;

    --shadow: 0 1px 2px rgba(16,22,28,.04), 0 6px 18px -10px rgba(16,22,28,.14);
    --shadow-lg: 0 20px 50px rgba(16,22,28,.35);
}

*, *::before, *::after { box-sizing: border-box; }

body {
    font-family: 'IBM Plex Sans', 'Segoe UI', sans-serif;
    background: var(--canvas);
    margin: 0;
    color: var(--ink);
    font-size: 14px;
}

.mono { font-family: 'IBM Plex Mono', ui-monospace, monospace; }
.tnum { font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- */
/* App shell: sidebar + main. A page opts in with .app-shell on the  */
/* outermost wrapper (or applies .sidebar/.main directly, both work  */
/* the same way flex children of a row do).                         */
/* ---------------------------------------------------------------- */

.app-shell, body.app-shell { display: flex; height: 100vh; overflow: hidden; }

.sidebar { width: 216px; background: var(--sidebar); color: var(--sidebar-ink); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar .brand { padding: 18px 18px 16px; border-bottom: 1px solid var(--sidebar-line); }
.sidebar .brand .word { font-family: 'IBM Plex Mono', monospace; font-size: 14px; font-weight: 600; color: #fff; letter-spacing: .02em; }
.sidebar .brand .ver { font-size: 10.5px; color: #5E6B85; margin-top: 2px; }
.sidebar nav { padding: 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav-link { padding: 10px 12px; color: var(--sidebar-ink); text-decoration: none; display: flex; align-items: center; gap: 10px; border-radius: var(--radius-sm); font-size: 13.5px; }
.nav-link:hover { background: var(--sidebar-active-bg); color: #fff; }
.nav-link.active { background: var(--sidebar-active-bg); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.nav-link .ic { font-size: 14px; width: 18px; text-align: center; flex: none; opacity: .9; }
/* Collapsible nav groups (Intake / Production / Billing / Business).
   A group whose items are all hidden for this role isn't rendered at all,
   and only the group holding the current page starts open - see
   services/auth.py's nav_sections(). */
.sidebar nav .nav-group { display: flex; flex-direction: column; }
.sidebar nav .nav-section { font-family: 'IBM Plex Mono', monospace; font-size: 9.5px; letter-spacing: .1em;
    text-transform: uppercase; color: #5A6B85; padding: 11px 12px 5px; user-select: none;
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    background: none; border: none; width: 100%; text-align: left; cursor: pointer; }
.sidebar nav .nav-section:hover { color: #8FA0BD; }
.sidebar nav .nav-section .chev { font-size: 13px; line-height: 1; transition: transform .15s ease;
    opacity: .65; }
.sidebar nav .nav-group.open .nav-section .chev { transform: rotate(90deg); }
.sidebar nav .nav-group-items { display: none; flex-direction: column; gap: 2px; }
.sidebar nav .nav-group.open .nav-group-items { display: flex; }

.sidebar .sidebar-foot { padding: 12px 18px 16px; border-top: 1px solid var(--sidebar-line); font-family: 'IBM Plex Mono', monospace; font-size: 10.5px; color: #6C7A93; }
/* Signed-in user + one-tap switch, for the shared front-desk machine. */
.sidebar .sidebar-foot .who-form { margin: 0 0 4px; }
.sidebar .sidebar-foot .who { width: 100%; text-align: left; background: none; border: none; cursor: pointer;
    padding: 4px 0; font-family: inherit; color: inherit; display: block; }
.sidebar .sidebar-foot .who:hover .nm { color: var(--accent); }
.sidebar .sidebar-foot .who .nm { display: block; font-family: 'IBM Plex Sans', sans-serif; font-size: 12.5px;
    font-weight: 600; color: #E8EDF5; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar .sidebar-foot .who .rl { display: block; font-size: 9.5px; letter-spacing: .06em;
    text-transform: uppercase; color: #6C7A93; margin-top: 1px; }
.sidebar .sidebar-foot .setup-nag { display: block; font-family: 'IBM Plex Sans', sans-serif; font-size: 11px;
    line-height: 1.4; color: #F5B971; text-decoration: none; background: rgba(217,119,6,.14);
    border-radius: var(--radius-sm); padding: 8px 9px; margin-bottom: 8px; }
.sidebar .sidebar-foot .setup-nag:hover { background: rgba(217,119,6,.24); color: #FFCF94; }
.sidebar .sidebar-foot .stop-server { margin-left: auto; background: none; border: none; cursor: pointer;
    font-family: inherit; font-size: 10px; color: #6C7A93; padding: 2px 5px; border-radius: 4px; }
.sidebar .sidebar-foot .stop-server:hover { background: rgba(200,60,50,.18); color: #F08A80; }
.sidebar .sidebar-foot .row { display: flex; align-items: center; gap: 7px; margin-top: 6px; }
.sidebar .sidebar-foot .led { width: 6px; height: 6px; border-radius: 50%; background: #3A4A66; flex: none; }
.sidebar .sidebar-foot .led.on { background: var(--good); }

.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

.topbar { display: flex; align-items: center; gap: 16px; padding: 12px 24px; background: var(--card); border-bottom: 1px solid var(--line); flex: none; }
.topbar .crumb { font-family: 'IBM Plex Mono', monospace; font-size: 12px; color: var(--muted); white-space: nowrap; }
.topbar .crumb b { color: var(--ink); font-weight: 600; }
.topbar .crumb a { color: inherit; text-decoration: none; }
.topbar .crumb a:hover { color: var(--accent); text-decoration: underline; }
.topbar .spacer { flex: 1; }
.topbar .net-pill { font-family: 'IBM Plex Mono', monospace; font-size: 10.5px; padding: 5px 10px; border-radius: var(--radius-pill); display: flex; align-items: center; gap: 7px; background: var(--canvas); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.topbar .net-pill a { text-decoration: none; }
.topbar .net-pill a:hover { text-decoration: underline; }

.content-area { flex: 1; display: flex; overflow: hidden; }
/* A page's column: centred at the width the page sets (forms narrower,
   lists wider). Pages that use the full width simply don't have one. */
.content-inner { margin-left: auto; margin-right: auto; }

/* ---------------------------------------------------------------- */
/* Buttons - one system: .btn + a color modifier + optional          */
/* size/width modifiers. Replaces the ~10 page-specific button       */
/* classes that used to each define their own padding/radius/color.  */
/* ---------------------------------------------------------------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 18px; border: none; border-radius: var(--radius-sm);
    font-family: inherit; font-size: 13px; font-weight: 600;
    cursor: pointer; text-decoration: none; white-space: nowrap;
    transition: background-color .12s ease, border-color .12s ease;
}
.btn:disabled { cursor: not-allowed; opacity: .55; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }

.btn-dark { background: var(--ink); color: #fff; }
.btn-dark:hover:not(:disabled) { background: #1B2740; }

.btn-good { background: var(--good); color: #fff; }
.btn-good:hover:not(:disabled) { background: #128A3E; }

.btn-warn { background: var(--warn); color: #fff; }
.btn-warn:hover:not(:disabled) { background: #B45F04; }

.btn-secondary { background: var(--card); color: var(--muted); border: 1px solid var(--line); }
.btn-secondary:hover:not(:disabled) { background: var(--canvas); color: var(--ink); }

.btn-danger { background: var(--bad-soft); color: var(--bad); border: 1px solid transparent; }
.btn-danger:hover:not(:disabled) { background: var(--bad); color: #fff; }

.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--canvas); }

.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-lg { padding: 14px 22px; font-size: 15px; }
.btn-block { width: 100%; }
/* A square icon button (x, arrows) - same height as .btn-sm. */
.btn-icon { padding: 0; width: 30px; height: 30px; justify-content: center; flex: none; }
/* Looks like text, behaves like a button: Clear, Remove, "Someone else". */
.btn-link { border: none; background: none; color: var(--muted); font-size: 12px; font-weight: 600; cursor: pointer;
    padding: 4px 6px; border-radius: 4px; font-family: inherit; }
.btn-link:hover { color: var(--bad); background: var(--bad-soft); }

/* A pill-style toggle (view switcher, date-range chips) - active state
   shares the same "filled dark" look everywhere it appears. */
.btn-toggle { background: var(--card); border: 1px solid var(--line); color: var(--muted); padding: 8px 12px; cursor: pointer; border-radius: var(--radius-sm); font-weight: 600; font-size: 12.5px; display: inline-flex; align-items: center; gap: 5px; }
.btn-toggle.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------------------------------------------------------------- */
/* Modal - one pattern everywhere: hidden via display:none, shown by  */
/* setting style.display='flex' (see app.js's openModal/closeModal). */
/* ---------------------------------------------------------------- */

.modal { display: none; position: fixed; inset: 0; background: rgba(16,22,28,.55); justify-content: center; align-items: center; z-index: 9999; padding: 20px; }
.modal.active { display: flex; }
.modal-content { background: #fff; border-radius: var(--radius-lg); width: 600px; max-width: 100%; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-lg); }
.modal-content.modal-sm { width: 380px; }
.modal-content.modal-lg { width: 820px; }
.modal-header { background: var(--ink); color: #fff; padding: 15px 20px; display: flex; justify-content: space-between; align-items: center; border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
.modal-header h2, .modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--line); }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 11px; font-weight: 600; color: var(--muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .04em; }
.form-group input, .form-group select, .form-group textarea {
    width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: var(--radius-sm);
    font-family: inherit; font-size: 13px; background: #fff; color: var(--ink);
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
    outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---------------------------------------------------------------- */
/* Tables - one class for every data grid in the app.                */
/* ---------------------------------------------------------------- */

.data-table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); border: 1px solid var(--line); }
.data-table th { background: var(--canvas); text-align: left; padding: 11px 12px; font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; border-bottom: 1px solid var(--line); position: sticky; top: 0; }
.data-table td { padding: 11px 12px; border-bottom: 1px solid var(--line); font-size: 12.5px; vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--canvas); }
.data-table input, .data-table select { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: var(--radius-sm); font-size: 12.5px; font-family: inherit; }
.data-table tr.hidden { display: none; }

/* Generic "filtered out" / JS-toggled visibility helper, used well
   beyond tables (rows, cards, grid items hidden by a client-side filter). */
.hidden { display: none !important; }

/* Compact variant for a table that's mostly editable inline inputs
   (e.g. a line-items editor) - tighter cell padding. */
.data-table.compact td, .data-table.compact th { padding: 8px; }

/* ---------------------------------------------------------------- */
/* Badges / pills                                                    */
/* ---------------------------------------------------------------- */

.badge { display: inline-block; padding: 3px 9px; border-radius: var(--radius-pill); font-size: 10.5px; font-weight: 700; }
.badge-neutral { background: var(--canvas); color: var(--muted); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn-ink); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }

/* Rider badges - always solid + white text (was inline hex per page). */
.badge-rider { color: #fff; }
.badge-rider-1 { background: var(--rider-1); }
.badge-rider-2 { background: var(--rider-2); }
.badge-rider-3 { background: var(--rider-3); }

/* Plain colored-text status (no pill background) - for dense tables
   where a full badge per row feels heavy. */
.status-text { font-weight: 600; }
.status-text.is-accent { color: var(--accent); }
.status-text.is-good { color: var(--good); }
.status-text.is-neutral { color: var(--ink); }
.status-text.is-bad { color: var(--bad); text-decoration: line-through; }
.status-text.is-muted { color: var(--muted); font-style: italic; }

/* ---------------------------------------------------------------- */
/* Cards / misc                                                      */
/* ---------------------------------------------------------------- */

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; margin-bottom: 18px; }
.msg-box { padding: 10px 14px; border-radius: var(--radius-sm); font-size: 13px; margin-bottom: 8px; }
.msg-error { background: var(--bad-soft); color: var(--bad-ink); border-left: 3px solid var(--bad); }
.msg-warn { background: var(--warn-soft); color: var(--warn-ink); border-left: 3px solid var(--warn); }
.msg-good { background: var(--good-soft); color: var(--good-ink); border-left: 3px solid var(--good); }
.msg-info { background: var(--accent-soft); color: var(--accent-dark); border-left: 3px solid var(--accent); }

/* Toasts - see OptiToast in app.js. Bottom-right, fades out, tap to close. */
.toast { position: fixed; right: 24px; bottom: 24px; background: var(--ink); color: #fff; padding: 11px 18px;
    border-radius: var(--radius-lg); font-size: 13px; font-weight: 600; z-index: 10000; opacity: 0;
    transition: opacity .2s; pointer-events: none; max-width: min(440px, 90vw); white-space: pre-line;
    box-shadow: var(--shadow-lg); cursor: pointer; }
.toast.show { opacity: 1; pointer-events: auto; }
.toast.good { background: var(--good); }
.toast.bad { background: var(--bad); }

/* Job card - one job as it appears on an invoice (label lines, Rx, price).
   Shared by the invoice preview/detail modal and the Recent Intakes list. */
.inv-job-card { border:1px solid var(--line); border-radius:var(--radius-sm); overflow:hidden; }
.inv-job-head { display:flex; justify-content:space-between; align-items:center; background:var(--canvas); padding:7px 10px; font-weight:700; font-size:12.5px; gap:8px; }
.inv-job-head .inv-job-total { white-space:nowrap; font-family:'IBM Plex Mono',monospace; }
.inv-job-rx { font-family:'IBM Plex Mono',monospace; color:var(--muted); font-size:10.5px; padding:6px 10px 0; }
.inv-job-lines { padding:2px 10px 8px; }
.inv-job-line { display:flex; justify-content:space-between; gap:10px; font-size:12px; color:#444; padding:3px 0; border-bottom:1px dotted var(--line); }
.inv-job-line:last-child { border-bottom:none; }
.inv-job-card.clickable { cursor:pointer; transition:border-color .12s, box-shadow .12s; }
.inv-job-card.clickable:hover { border-color:var(--accent); box-shadow:0 0 0 3px var(--accent-soft); }

/* ---------------------------------------------------------------- */
/* Tablets and the hideable sidebar                                  */
/* ---------------------------------------------------------------- */
/* The menu button (added to the top bar by fragments/sidebar.html).
   On a tablet - 1180px wide or less, i.e. anything smaller than an
   11-inch iPad - the sidebar starts hidden and slides in over the page,
   so forms get the whole screen. On a desktop the button folds it away. */
.sidebar-toggle { flex: none; width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center;
    border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: var(--radius);
    font-size: 18px; line-height: 1; cursor: pointer; padding: 0; }
.sidebar-toggle:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.sidebar-toggle.floating { position: fixed; top: 10px; left: 10px; z-index: 600; box-shadow: var(--shadow); }
.sidebar-backdrop { display: none; }

@media (min-width: 1181px) {
    body.sidebar-collapsed .sidebar { display: none; }
}

@media (max-width: 1180px) {
    .sidebar { position: fixed; top: 0; left: 0; bottom: 0; z-index: 4000; width: 248px;
        transform: translateX(-100%); transition: transform .2s ease; }
    body.sidebar-open .sidebar { transform: none; box-shadow: 8px 0 28px rgba(16,22,28,.35); }
    .sidebar-backdrop { display: block; position: fixed; inset: 0; z-index: 3999; background: rgba(16,22,28,.45);
        opacity: 0; pointer-events: none; transition: opacity .2s ease; }
    body.sidebar-open .sidebar-backdrop { opacity: 1; pointer-events: auto; }
    .sidebar .nav-link { padding: 12px 12px; }            /* finger-sized */

    .topbar { padding: 10px 14px; gap: 10px; flex-wrap: wrap; }
}

@media (max-width: 900px) {
    /* The LAN address is for setting up the host PC; on a tablet
       it only crowds out the page's own controls. */
    .topbar #netPill { display: none; }
}

/* Checkboxes and radio buttons keep their own size. Pages style every
   <input> as a full-width padded box; Chrome ignores that on a checkbox,
   but iPad Safari applies it, and inside a small label like "Polish" the
   box was squeezed until it could not be seen. */
input[type="checkbox"], input[type="radio"] { width: auto; padding: 0; flex: none; }

/* Touch screens: iPad Safari zooms the whole page into any field whose
   text is under 16px - every tap into an Rx box would jump the layout. */
@media (pointer: coarse) {
    input, select, textarea, .ts-control, .ts-control input { font-size: 16px !important; }
    input[type="checkbox"], input[type="radio"] { width: 20px !important; height: 20px; margin: 0; }
}

/* Print at lab, beside a PDF link (static/js/lab_print.js) */
.lab-print-btn { margin-left: 6px; gap: 5px; }
.lab-print-btn svg { flex: none; }

/* ---------------------------------------------------------------------------
   Phones. Same markup and same theme as the desktop; only how it behaves changes.
   Page <style> blocks load after this file, so these rules lead with "body"
   to outrank them. Tables get their labels from static/js/mobile_tables.js.
   --------------------------------------------------------------------------- */
.grid-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 700px) {
    /* 100vh is taller than the visible screen once a phone's address bar shows. */
    body.app-shell, .app-shell { height: 100dvh; }
    body .main, body .content-area, body .content-inner { min-width: 0; max-width: 100%; }
    body .content-area { padding: 12px; }
    body .card { padding: 14px; }
    body h1, body h2, body h3, body p, body td, body li, body label, body .crumb { overflow-wrap: anywhere; }

    /* Top bar: the breadcrumb wraps, and a page's actions sit on their own row. */
    body .topbar { padding: 8px 12px; gap: 8px; }
    body .topbar .crumb { white-space: normal; min-width: 0; flex: 1 1 60%; }
    body .topbar .spacer { display: none; }
    body .topbar input, body .topbar .search-wrap, body .topbar form { min-width: 0; max-width: 100%; flex: 1 1 100%; width: 100%; }

    /* Forms: fields never push past the screen. */
    body input:not([type=checkbox]):not([type=radio]), body select, body textarea { min-width: 0; max-width: 100%; }
    input[type=date] { width: 100%; }
    body .filter-bar { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; padding: 12px; }
    body .filter-group { min-width: 0; margin: 0 !important; align-items: stretch !important; }
    body .filter-input { min-width: 0; width: 100%; box-sizing: border-box; }

    /* Tap targets. */
    body .btn { min-height: 42px; }
    body .btn-sm { min-height: 40px; padding: 6px 12px; }
    body .btn-link { min-height: 36px; padding: 6px 8px; display: inline-flex; align-items: center; }
    body .btn-toggle { min-height: 40px; justify-content: center; }
    body .lab-print-btn { margin-left: 0; }

    /* Rows of page tabs scroll inside themselves instead of squashing. */
    body .tabs { display: flex; flex-wrap: nowrap; overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
    body .tabs .tab { flex: none; white-space: nowrap; }

    /* Pop-ups fill the phone, from the bottom, and their buttons stay in reach. */
    body .modal { padding: 0; align-items: flex-end; }
    body .modal-content, body .modal-content.modal-lg, body .modal-content.modal-sm { width: 100%; max-width: 100% !important; max-height: 94dvh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
    body .modal-body { padding: 14px; }
    body .modal-actions, body .modal-footer { position: sticky; bottom: 0; background: var(--card); flex-wrap: wrap; padding: 12px 14px calc(12px + env(safe-area-inset-bottom)) !important; }
    body .modal-actions .btn { flex: 1 1 40%; }

    /* Tables become stacked rows: a block per row, with "label  value" per cell.
       Block cells with a floated label, not flex: a cell often holds text and
       tags together, and flex would spread those apart. */
    body table.stack-table, body table.stack-table tbody { display: block; width: 100%; min-width: 0; border: none; box-shadow: none; background: none; }
    body table.stack-table thead, body table.stack-table colgroup { display: none; }
    body table.stack-table tr { display: block; padding: 10px 12px; background: var(--card); border: 1px solid var(--line);
        border-radius: var(--radius-sm); margin-bottom: 8px; }
    body table.stack-table tr:hover td { background: none; }
    body table.stack-table td { display: block; width: auto; max-width: 100%; padding: 3px 0; border: none; text-align: right;
        min-width: 0; overflow-wrap: anywhere; white-space: normal; background: none; }
    body table.stack-table td::after { content: ""; display: block; clear: both; }
    body table.stack-table td:empty { display: none; }
    body table.stack-table td:first-child { font-weight: 600; }
    body table.stack-table td[data-label]:not([data-label=""])::before { content: attr(data-label); float: left; margin-right: 12px; max-width: 45%; text-align: left;
        font-size: 10.5px; font-weight: 600; line-height: 1.7; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; white-space: normal; }
    body table.stack-table td[colspan] { text-align: left; }
    /* A cell holding a field: label above, full-width field. */
    body table.stack-table td:has(> input:not([type=checkbox]):not([type=hidden]), > select, > textarea, > .ts-wrapper) { text-align: left; }
    body table.stack-table td[data-label]:has(> input:not([type=checkbox]):not([type=hidden]), > select, > textarea, > .ts-wrapper)::before { float: none; display: block; max-width: 100%; margin: 0 0 3px; }
    body table.stack-table td > input:not([type=checkbox]), body table.stack-table td > select, body table.stack-table td > textarea { width: 100% !important; box-sizing: border-box; }

    /* Wide tables (six columns or more): two columns of label-over-value,
       so a Tracker row isn't a screenful. Actions take the full width. */
    body table.stack-two tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 14px; }
    body table.stack-two td { text-align: left; padding: 0; }
    body table.stack-two td[data-label]:not([data-label=""])::before { float: none; display: block; max-width: 100%; margin: 0; }
    body table.stack-two td[data-label=""], body table.stack-two td[colspan],
    body table.stack-two td:has(button, form, a.btn) { grid-column: 1 / -1; }
    body table.stack-two td[data-label=""]:has(button, a.btn) { text-align: right; }
    body table.stack-two tr[class*="hidden"] { display: none; }
}

/* Long forms: the main button stays on screen, without covering the last field. */
@media (max-width: 700px) {
    body .row-foot, body .summary-foot, body .total-block, body .sticky-actions { position: sticky; bottom: 0; z-index: 30;
        padding: 10px 0 calc(10px + env(safe-area-inset-bottom)); background: var(--card); border-top: 1px solid var(--line); }

    /* Label + field rows and pickers wrap instead of running off the edge. */
    body .field-row { flex-wrap: wrap; }
    body .field-row > .field { min-width: 0; flex: 1 1 140px !important; max-width: 100%; }
    body .field-row > .field.narrow { flex: 1 1 90px !important; }
    body .mode-switch { flex-wrap: wrap; max-width: 100%; }
    body .mode-switch .mode-option { flex: 1 1 auto; min-width: 0; white-space: normal; }
    body [id*="ItemPicker"], body [id*="Picker"] { min-width: 0; max-width: 100%; }
    body [id*="Picker"] [style*="flex"], body [id*="Picker"] > div { flex-wrap: wrap !important; min-width: 0; max-width: 100%; }
    body [id*="Picker"] input, body [id*="Picker"] select { flex: 1 1 70px !important; min-width: 64px !important; }
}

@media (max-width: 700px) {
    /* Date ranges and quick-filter buttons get a whole row of the filter grid. */
    body .filter-group:has(input[type=date]), body .filter-group:has(.btn-toggle), body .filter-group:has(.btn) { grid-column: 1 / -1; }
    body .filter-group:has(input[type=date]) > div { display: flex; align-items: center; gap: 6px; width: 100%; }
    body .filter-group:has(input[type=date]) input[type=date] { flex: 1 1 0; width: auto; }
    body .filter-group .btn-toggle { flex: 1 1 0; }
    body .filter-group > div:has(> .btn-toggle) { display: flex; gap: 4px; width: 100%; }
    body .filter-group > div:has(> .btn) { width: 100%; }
    body .filter-group > div > .btn { flex: 1 1 100%; width: 100%; }

    /* Grids a page set inline, like Analytics' three tables side by side. */
    body [style*="grid-template-columns:repeat(3"], body [style*="grid-template-columns: repeat(3"],
    body [style*="grid-template-columns:repeat(2"], body [style*="grid-template-columns: repeat(2"] { grid-template-columns: minmax(0, 1fr) !important; }

    body .field-row > .field[style*="flex:3"] { flex: 1 1 100% !important; }
    /* A section's toolbar: extra buttons above, the total and Save bar below. */
    body .order-toolbar { display: flex; flex-wrap: wrap; gap: 10px; }
    body .order-toolbar .left-actions { display: flex; flex-wrap: wrap; gap: 8px; flex: 1 1 100%; }
    body .order-toolbar .left-actions .btn { flex: 1 1 45%; }
    body .order-toolbar .total-block { flex: 1 1 100%; align-items: center; justify-content: flex-end; }
}

/* The foot-of-page strip when the lab PC and the online version are connected. */
.link-banner { position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 900; display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px;
    width: max-content; max-width: min(92vw, 720px); padding: 8px 14px; font-size: 13px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 2px 10px rgba(0, 0, 0, .15); }
.link-banner.warn { border-color: var(--warn); }

/* The wheel picker for SPH, CYL and ADD on touch screens (static/js/app.js). */
.wheel-sheet { position: fixed; inset: 0; z-index: 1000; background: rgba(16, 22, 28, .35); display: flex; align-items: flex-end; justify-content: center; }
.wheel-sheet[hidden] { display: none; }
.wheel-panel { width: 100%; max-width: 480px; background: var(--card); border-radius: var(--radius-lg) var(--radius-lg) 0 0; box-shadow: var(--shadow-lg);
    padding: 12px 14px calc(14px + env(safe-area-inset-bottom)); }
.wheel-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.wheel-title { flex: 1; font-weight: 600; color: var(--ink); }
.wheel-box { position: relative; height: 220px; }
.wheel-band { position: absolute; left: 0; right: 0; top: 88px; height: 44px; border-radius: var(--radius); background: var(--accent-soft); pointer-events: none; }
.wheel { position: relative; height: 100%; overflow-y: auto; scroll-snap-type: y mandatory; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
.wheel::-webkit-scrollbar { display: none; }
.wheel-pad { height: 88px; }
.wheel-row { height: 44px; line-height: 44px; text-align: center; scroll-snap-align: center; font: 600 20px/44px 'IBM Plex Mono', monospace; color: var(--ink); cursor: pointer; }
body.wheel-open { overflow: hidden; }
