/* Spec Match theme ("Clinical Clean") on top of Pico CSS.
   Light only: colours are tokens below; Pico is re-themed through its own variables. */
:root {
  --sm-bg: #f6f8fa;
  --sm-card: #ffffff;
  --sm-fg: #1c2733;
  --sm-muted: #66737f;
  --sm-line: #e1e6eb;
  --sm-line-strong: #cfd6dd;
  --sm-accent: #0f6e7a;
  --sm-accent-hover: #0b5a64;
  --sm-accent-soft: #e3f2f3;
  --sm-ok: #1e7a46;
  --sm-ok-bg: #e2f4e9;
  --sm-bad: #b4232c;
  --sm-bad-bg: #fbe5e6;
  --sm-bad-row: #fdf3f3;
  --sm-chk: #8a5a00;
  --sm-chk-bg: #fdf1d6;
  --sm-chk-row: #fff8e8;
  --sm-info: #1d4f91;
  --sm-info-bg: #e3ecfa;
  --sm-neutral: #44525e;
  --sm-neutral-bg: #e8ecf0;
  --sm-shadow: 0 1px 2px rgba(20, 40, 60, 0.05), 0 1px 1px rgba(20, 40, 60, 0.03);
  --sm-radius: 10px;
  --sm-sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --sm-mono: "IBM Plex Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
  --sm-topbar-h: 56px;
  --sm-tabbar-h: 62px;
}

:root:not([data-theme="dark"]), [data-theme="light"] {
  --pico-font-family: var(--sm-sans);
  --pico-font-size: 93.75%;
  --pico-line-height: 1.5;
  --pico-border-radius: 8px;
  --pico-background-color: var(--sm-bg);
  --pico-color: var(--sm-fg);
  --pico-h1-color: var(--sm-fg);
  --pico-h2-color: var(--sm-fg);
  --pico-h3-color: var(--sm-fg);
  --pico-h4-color: var(--sm-fg);
  --pico-muted-color: var(--sm-muted);
  --pico-muted-border-color: var(--sm-line);
  --pico-primary: var(--sm-accent);
  --pico-primary-background: var(--sm-accent);
  --pico-primary-border: var(--sm-accent);
  --pico-primary-underline: rgba(15, 110, 122, 0.4);
  --pico-primary-hover: var(--sm-accent-hover);
  --pico-primary-hover-background: var(--sm-accent-hover);
  --pico-primary-hover-border: var(--sm-accent-hover);
  --pico-primary-hover-underline: var(--sm-accent-hover);
  --pico-primary-focus: rgba(15, 110, 122, 0.25);
  --pico-primary-inverse: #fff;
  --pico-secondary: var(--sm-neutral);
  --pico-secondary-background: var(--sm-neutral);
  --pico-secondary-border: var(--sm-line-strong);
  --pico-secondary-hover: var(--sm-fg);
  --pico-secondary-hover-background: var(--sm-fg);
  --pico-secondary-hover-border: var(--sm-muted);
  --pico-secondary-focus: rgba(68, 82, 94, 0.2);
  --pico-card-background-color: var(--sm-card);
  --pico-card-sectioning-background-color: var(--sm-card);
  --pico-card-box-shadow: var(--sm-shadow);
  --pico-table-border-color: var(--sm-line);
  --pico-table-row-stripped-background-color: #f9fbfc;
  --pico-form-element-background-color: #fff;
  --pico-form-element-border-color: var(--sm-line-strong);
  --pico-form-element-focus-color: var(--sm-accent);
  --pico-form-element-spacing-vertical: 0.6rem;
  --pico-form-element-spacing-horizontal: 0.8rem;
  --pico-typography-spacing-vertical: 1rem;
  --pico-spacing: 1rem;
  --pico-block-spacing-vertical: 1.1rem;
  --pico-block-spacing-horizontal: 1.25rem;
  --pico-code-background-color: var(--sm-neutral-bg);
  --pico-code-color: var(--sm-fg);
}

body { background: var(--sm-bg); min-height: 100vh; }
h1, h2, h3, h4 { font-weight: 600; letter-spacing: -0.01em; text-wrap: balance; }
h2 { font-size: 1.5rem; margin-bottom: 0.75rem; }
h3 { font-size: 1.15rem; margin-bottom: 0.6rem; }
h2 small, h3 small { font-weight: 400; font-size: 0.8em; }
section { margin-bottom: 2rem; }
code, kbd { font-family: var(--sm-mono); }
table { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--sm-accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 20; background: var(--sm-card); border-bottom: 1px solid var(--sm-line); }
.topbar-inner { max-width: 1280px; margin: 0 auto; height: var(--sm-topbar-h); padding: 0 1.25rem; display: flex; align-items: center; gap: 1.5rem; }
.brand { display: inline-flex; align-items: center; gap: 0.55rem; font-weight: 700; font-size: 1.05rem; color: var(--sm-fg); text-decoration: none; white-space: nowrap; }
.brand:hover { color: var(--sm-fg); }
.brand-mark { width: 24px; height: 24px; border-radius: 7px; background: var(--sm-accent); position: relative; flex: none; }
.brand-mark::before, .brand-mark::after { content: ""; position: absolute; top: 8px; width: 8px; height: 8px; border: 2px solid #fff; border-radius: 50%; }
.brand-mark::before { left: 3px; } .brand-mark::after { right: 3px; }

.mainnav { display: flex; align-items: center; gap: 0.15rem; flex: 1; min-width: 0; }
.mainnav > a, .menu > summary { padding: 0.4rem 0.7rem; border-radius: 7px; color: var(--sm-muted); text-decoration: none; font-size: 0.93rem; white-space: nowrap; }
.mainnav > a:hover, .menu > summary:hover { background: var(--sm-bg); color: var(--sm-fg); }
.mainnav > a.on { background: var(--sm-accent-soft); color: var(--sm-accent); font-weight: 600; }

/* Dropdowns built on <details> (works without JS; base.html closes them on outside click). */
.menu { position: relative; margin: 0; }
.menu > summary { list-style: none; cursor: pointer; display: inline-flex; align-items: center; gap: 0.35rem; }
.menu > summary::-webkit-details-marker { display: none; }
.menu > summary::after { content: ""; width: 0.45rem; height: 0.45rem; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: translateY(-2px) rotate(45deg); float: none; margin: 0; background: none; }
.menu[open] > summary { background: var(--sm-bg); color: var(--sm-fg); }
.menu:has(.on) > summary { color: var(--sm-accent); font-weight: 600; }
.menu-panel { position: absolute; top: calc(100% + 6px); left: 0; min-width: 200px; background: var(--sm-card); border: 1px solid var(--sm-line); border-radius: var(--sm-radius); box-shadow: 0 8px 24px rgba(20, 40, 60, 0.12); padding: 0.35rem; display: grid; gap: 1px; z-index: 30; }
.menu-panel a, .menu-panel .linkish { display: block; padding: 0.5rem 0.75rem; border-radius: 6px; color: var(--sm-fg); text-decoration: none; font-size: 0.92rem; }
.menu-panel a:hover, .menu-panel .linkish:hover { background: var(--sm-bg); }
.menu-panel a.on { color: var(--sm-accent); font-weight: 600; }
.menu-panel form { margin: 0; }
.linkish { width: 100%; text-align: left; background: none; border: 0; margin: 0; font: inherit; cursor: pointer; }
.usermenu { margin-left: auto; }
.usermenu .menu-panel { left: auto; right: 0; }
.usermenu > summary { color: var(--sm-fg); }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--sm-accent-soft); color: var(--sm-accent); font-weight: 700; font-size: 0.85rem; display: inline-grid; place-items: center; }
.who { font-size: 0.85rem; color: var(--sm-muted); }

/* ---------- Bottom tab bar (phones / small tablets) ---------- */
.tabbar { display: none; }
@media (max-width: 991px) {
  .mainnav { display: none; }
  .who { display: none; }
  .topbar-inner { padding: 0 1rem; height: 52px; }
  body.signed-in main { padding-bottom: calc(var(--sm-tabbar-h) + 1.5rem + env(safe-area-inset-bottom, 0px)); }
  .tabbar {
    display: grid; grid-template-columns: repeat(5, 1fr);
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    background: var(--sm-card); border-top: 1px solid var(--sm-line);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .tabbar > a, .tabmore > summary {
    height: var(--sm-tabbar-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
    font-size: 0.72rem; color: var(--sm-muted); text-decoration: none; list-style: none; cursor: pointer;
  }
  .tabmore > summary::-webkit-details-marker { display: none; }
  .tabbar > a::before, .tabmore > summary::before { content: ""; width: 20px; height: 20px; background: currentColor; -webkit-mask: var(--icon) center / contain no-repeat; mask: var(--icon) center / contain no-repeat; }
  .tabbar > a.on, .tabmore:has(.on) > summary, .tabmore[open] > summary { color: var(--sm-accent); font-weight: 600; }
  .tabbar > a[href="/"] { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Crect x='4' y='4' width='16' height='16' rx='2'/%3E%3Cpath d='M8 9h8M8 13h8M8 17h5'/%3E%3C/svg%3E"); }
  .tabbar > a[href="/inbox"] { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M3 13l3-8h12l3 8v6H3z'/%3E%3Cpath d='M3 13h5l1 3h6l1-3h5'/%3E%3C/svg%3E"); }
  .tabbar > a[href="/queue"] { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 3l10 18H2z'/%3E%3Cpath d='M12 10v5M12 18v.5'/%3E%3C/svg%3E"); }
  .tabbar > a[href="/upload"] { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 8h3l2-3h6l2 3h3v12H4z'/%3E%3Ccircle cx='12' cy='14' r='3.5'/%3E%3C/svg%3E"); }
  .tabmore > summary { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='black'%3E%3Ccircle cx='5' cy='12' r='2'/%3E%3Ccircle cx='12' cy='12' r='2'/%3E%3Ccircle cx='19' cy='12' r='2'/%3E%3C/svg%3E"); }
  .tabmore { margin: 0; }
  .tabmore .sheet {
    position: fixed; left: 0.75rem; right: 0.75rem; bottom: calc(var(--sm-tabbar-h) + 0.5rem + env(safe-area-inset-bottom, 0px));
    max-height: 70vh; overflow-y: auto; background: var(--sm-card); border: 1px solid var(--sm-line); border-radius: 14px;
    box-shadow: 0 -8px 30px rgba(20, 40, 60, 0.18); padding: 0.5rem; display: grid; gap: 1px;
  }
  .sheet a { padding: 0.8rem 0.9rem; border-radius: 8px; color: var(--sm-fg); text-decoration: none; }
  .sheet a:hover { background: var(--sm-bg); }
  .sheet a.on { color: var(--sm-accent); font-weight: 600; background: var(--sm-accent-soft); }
  .sheet-group { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--sm-muted); padding: 0.8rem 0.9rem 0.3rem; }
  /* 16px inputs stop iOS zooming in on focus. */
  input, select, textarea { font-size: 16px !important; }
}

/* ---------- Page layout ---------- */
main.container { max-width: 1280px; padding-top: 1.75rem; padding-bottom: 3rem; }
@media (max-width: 575px) { main.container { padding-top: 1.1rem; } h2 { font-size: 1.3rem; } }

article { border: 1px solid var(--sm-line); border-radius: var(--sm-radius); box-shadow: var(--sm-shadow); }
article > header, article > footer { background: transparent; }
article > header { border-bottom: 1px solid var(--sm-line); }
article > footer { border-top: 1px solid var(--sm-line); }

.brand { font-weight: 700; }
form.inline { display: inline; margin: 0; }
button, [role="button"] { font-weight: 600; }
button.small, a[role="button"].small { padding: 0.3rem 0.75rem; font-size: 0.85rem; margin: 0; width: auto; }
.narrow { max-width: 420px; margin: 3rem auto; }
.muted { color: var(--sm-muted); }
.error { color: var(--sm-bad); }
.danger-zone { border: 1px solid #f0b8bb; }
.danger-zone p { margin: 0.25rem 0 0; }
button.danger { color: var(--sm-bad); border-color: var(--sm-bad); }
.ok { color: var(--sm-ok); }
.warn { color: var(--sm-chk); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 0.75rem 1rem; flex-wrap: wrap; }
.row > h2, .row > h3 { margin-bottom: 0; }
.notice { padding: 0.75rem 1rem; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.75rem; margin-bottom: 2rem; }
.stat { background: var(--sm-card); border: 1px solid var(--sm-line); border-radius: var(--sm-radius); box-shadow: var(--sm-shadow); padding: 0.85rem 1rem; font-size: 0.85rem; color: var(--sm-muted); }
.stat span { display: block; font-size: 1.75rem; font-weight: 700; color: var(--sm-fg); line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-link { text-decoration: none; transition: border-color 0.15s; }
.stat-link:hover { border-color: var(--sm-accent); color: var(--sm-muted); }
.stat.status-correct span { color: var(--sm-ok); }
.stat.status-incorrect span { color: var(--sm-bad); }
.stat.status-needs_review span { color: var(--sm-chk); }
.status-text-correct { color: var(--sm-ok); }
.status-text-incorrect { color: var(--sm-bad); }
.status-text-needs_review { color: var(--sm-chk); }
.stat.attention { border-color: #e9c46a; background: #fffcf3; }
.stat.attention span { color: var(--sm-chk); }
@media (max-width: 575px) { .stats { grid-template-columns: repeat(2, 1fr); } .stat span { font-size: 1.45rem; } }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 0.15rem 0.6rem; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; line-height: 1.5; vertical-align: middle; }
.badge.status-pending, .badge.stage-waiting_photos { background: var(--sm-neutral-bg); color: var(--sm-neutral); }
.badge.status-correct, .badge.stage-ready, .badge.run-ok { background: var(--sm-ok-bg); color: var(--sm-ok); }
.badge.status-incorrect, .badge.stage-awaiting_remake, .badge.run-failed { background: var(--sm-bad-bg); color: var(--sm-bad); }
.badge.status-needs_review, .badge.stage-needs_attention, .badge.run-partial, .badge.run-running { background: var(--sm-chk-bg); color: var(--sm-chk); }
.badge.stage-dispatched { background: var(--sm-info-bg); color: var(--sm-info); }

/* ---------- Flash messages ---------- */
.flash { padding: 0.7rem 1rem; border-radius: 8px; border: 1px solid var(--sm-line); background: var(--sm-card); }
.flash-success { background: var(--sm-ok-bg); border-color: #b6dfc4; color: var(--sm-ok); }
.flash-error { background: var(--sm-bad-bg); border-color: #f0b8bb; color: var(--sm-bad); }

/* ---------- Tables ---------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; background: var(--sm-card); border: 1px solid var(--sm-line); border-radius: var(--sm-radius); box-shadow: var(--sm-shadow); margin-bottom: var(--pico-spacing); }
.table-scroll > table { margin: 0; }
article .table-scroll, .photo-card .table-scroll, .photo-panel .table-scroll { box-shadow: none; }
table th { font-weight: 600; }
thead th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--sm-muted); background: #fafbfc; }
th, td { padding: 0.6rem 0.8rem; }
tbody tr:last-child > * { border-bottom: 0; }
table.striped tbody tr:hover > * { background: #f3f7f9; }

table.readings, table.findings, table.history { font-size: 0.85rem; margin: 0; }
table.readings th, table.readings td { padding: 0.35rem 0.5rem; text-align: center; }
table.readings td { font-family: var(--sm-mono); font-size: 0.82rem; }
.part-title { font-size: 0.85rem; margin: 0.75rem 0 0.35rem; color: var(--sm-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.low-conf { background: var(--sm-chk-bg); color: var(--sm-chk); font-weight: 600; }
table.findings td { vertical-align: top; }
tr.finding-incorrect td { background: var(--sm-bad-row); }
tr.finding-needs_review td { background: var(--sm-chk-row); }
td.flag-incorrect { outline: 2px solid var(--sm-bad); outline-offset: -2px; color: var(--sm-bad); font-weight: 700; }
td.flag-needs_review { outline: 2px dashed #e0a100; outline-offset: -2px; }
td .num, td.num { font-family: var(--sm-mono); }
td.actions { white-space: nowrap; }
td.actions form { margin-right: 0.25rem; }
table.runs details { margin: 0; }
table.correct-form input { width: 5.5rem; padding: 0.25rem 0.4rem; margin: 0; height: auto; font-size: 0.9rem; font-family: var(--sm-mono); }

/* On phones, tables marked .stack turn into one card per row, labelled from data-label. */
@media (max-width: 640px) {
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tr { padding: 0.6rem 0.9rem; border-bottom: 1px solid var(--sm-line); }
  table.stack tbody tr:last-child { border-bottom: 0; }
  table.stack td { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; padding: 0.2rem 0; border: 0; text-align: right; background: transparent !important; }
  table.stack td[data-label]::before { content: attr(data-label); color: var(--sm-muted); font-size: 0.8rem; text-align: left; flex: none; }
  table.stack td.stack-title { font-size: 1rem; text-align: left; }
  table.stack tr.finding-incorrect { background: var(--sm-bad-row); box-shadow: inset 4px 0 0 var(--sm-bad); }
  table.stack tr.finding-needs_review { background: var(--sm-chk-row); box-shadow: inset 4px 0 0 #e0a100; }
}

/* ---------- Photos ---------- */
.group { padding: 1rem 1.1rem; }
.group > header { margin: -1rem -1.1rem 1rem; padding: 0.8rem 1.1rem; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 1rem; }
.photo-card { background: var(--sm-card); border: 1px solid var(--sm-line); border-radius: var(--sm-radius); padding: 0.6rem; font-size: 0.85rem; min-width: 0; display: grid; gap: 0.35rem; align-content: start; }
.photo-card strong { display: block; overflow-wrap: anywhere; }
.photo-card .thumb img, .photo-panel .thumb img { display: block; width: 100%; border-radius: 6px; background: var(--sm-neutral-bg); }
.photo-card .thumb img { height: 170px; object-fit: cover; }
.photo-card details { margin: 0.25rem 0 0; }
.select-photo { display: flex; gap: 0.4rem; align-items: center; margin-bottom: 0.25rem; }
.role-form { margin: 0.25rem 0; }
.role-form select { padding: 0.3rem 2rem 0.3rem 0.6rem; font-size: 0.85rem; margin: 0; height: auto; }
.assign-bar { display: flex; gap: 0.5rem; align-items: center; max-width: 520px; margin-bottom: 1rem; }
.assign-bar input, .assign-bar button { margin: 0; }
.assign-bar button { width: auto; white-space: nowrap; }

.compare { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 1.25rem; }
.compare > section { min-width: 0; margin-bottom: 0; }
.photo-panel { padding: 0.85rem; display: grid; gap: 0.4rem; min-width: 0; }
.photo-panel .thumb img { max-height: 380px; object-fit: contain; }
.photo-panel .row strong { overflow-wrap: anywhere; min-width: 0; }
.photo-panel .row:last-child { gap: 0.5rem; justify-content: flex-start; padding-top: 0.4rem; border-top: 1px solid var(--sm-line); }
.photo-panel .row:last-child small { flex: 1 1 100%; }
.warnings li { margin-bottom: 0.4rem; }
.warnings .warn-job_number_mismatch { color: var(--sm-bad); font-weight: 600; }
.confirm footer { margin-top: 1rem; }
ul.issues { margin: 0.25rem 0; padding-left: 1.2rem; font-size: 0.85rem; color: var(--sm-chk); }

/* ---------- Job result & decision ---------- */
.job-head { margin-bottom: 0.5rem; }
.job-head .badges { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.result, .decision { padding: 0; overflow: hidden; }
.result > header, .decision > header { margin: 0; padding: 0.85rem 1.15rem; display: flex; justify-content: space-between; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.result > p, .result > footer, .decision > p, .decision-actions { margin-left: 1.15rem; margin-right: 1.15rem; }
.result > p:first-of-type, .decision > p:first-of-type { margin-top: 1rem; }
.result > .table-scroll { margin: 0.25rem 0 0; border-radius: 0; border-left: 0; border-right: 0; box-shadow: none; }
.result > small { display: block; padding: 0.6rem 1.15rem 0; }
.result > footer { margin: 0.75rem 0 0; padding: 0.7rem 1.15rem; }
.result > header { border-left: 5px solid #90a4ae; }
.result-correct > header { border-left-color: var(--sm-ok); background: var(--sm-ok-bg); }
.result-incorrect > header { border-left-color: var(--sm-bad); background: var(--sm-bad-bg); }
.result-needs_review > header { border-left-color: #e0a100; background: var(--sm-chk-bg); }
.result .note { font-weight: 600; color: var(--sm-chk); }
.decision { padding-bottom: 1rem; }
.decision-actions form { margin: 1rem 0 0; }
.decision-actions label { font-size: 0.9rem; }
.decision-actions button { width: auto; }

/* ---------- Photo collection health ---------- */
.health { padding: 0.8rem 1.1rem; border-left: 5px solid #90a4ae; }
.health-ok { border-left-color: var(--sm-ok); }
.health-warning, .health-never { border-left-color: #e0a100; }
.health-stale, .health-failing { border-left-color: var(--sm-bad); }

form.filters { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
form.filters > * { width: auto; margin: 0; }

/* ---------- Login ---------- */
.login-card { padding: 2rem 1.75rem; }
.login-card h2 { margin-bottom: 0.25rem; }
.login-card button { width: 100%; margin-top: 0.5rem; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
