:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --good: #006300;
  --warn: #9a6200;
  --bad: #c0392b;
  --card-outline: rgba(255, 255, 255, 0.9);
  --card-outline-halo: rgba(0, 0, 0, 0.55);
  /* set colors: validated categorical palette, fixed order */
  --set-1: #2a78d6; --set-2: #eb6834; --set-3: #1baf7a; --set-4: #eda100;
  --set-5: #e87ba4; --set-6: #008300; --set-7: #4a3aa7; --set-8: #e34948;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --text: #ffffff; --text-2: #c3c2b7; --muted: #898781;
    --line: #2c2c2a; --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --accent-ink: #ffffff;
    --good: #0ca30c; --warn: #fab219; --bad: #e66767;
    --set-1: #3987e5; --set-2: #d95926; --set-3: #199e70; --set-4: #c98500;
    --set-5: #d55181; --set-6: #008300; --set-7: #9085e9; --set-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --text: #ffffff; --text-2: #c3c2b7; --muted: #898781;
  --line: #2c2c2a; --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --accent-ink: #ffffff;
  --good: #0ca30c; --warn: #fab219; --bad: #e66767;
  --set-1: #3987e5; --set-2: #d95926; --set-3: #199e70; --set-4: #c98500;
  --set-5: #d55181; --set-6: #008300; --set-7: #9085e9; --set-8: #e66767;
}

* { box-sizing: border-box; }
/* author `display` rules (e.g. .busy's flex) would otherwise override the hidden attribute */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
}
.top {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 900px; margin: 0 auto; padding: 14px 16px 6px;
}
h1 { font-size: 20px; margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 18px 0 4px; }
main { max-width: 900px; margin: 0 auto; padding: 0 16px 48px; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; margin-top: 12px;
}
.lead { margin: 0 0 14px; }
.hint, .note { color: var(--text-2); font-size: 14px; margin: 10px 0 0; }
.note { margin: 0 0 8px; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; }
.actions.end { justify-content: flex-end; margin-top: 14px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 600; font-size: 15px;
  padding: 11px 16px; min-height: 44px; border-radius: 10px; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible, .switch input:focus-visible + .track, .card-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.big { font-size: 17px; padding: 14px 22px; width: 100%; }
.btn[aria-pressed="true"] { background: var(--text); color: var(--surface); border-color: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.capture .actions .btn { flex: 1 1 160px; }

.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text-2); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track { width: 36px; height: 22px; border-radius: 11px; background: var(--line); position: relative; transition: background .15s; }
.switch .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--surface); box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s; }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(14px); }

.loading { margin-top: 8px; font-size: 13px; color: var(--text-2); }
.loading .bar { height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.loading .bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.loading.done { display: none; }

.stage { position: relative; margin: 12px 0 0; border-radius: 14px; overflow: hidden; background: #000; }
.stage-inner { position: relative; }
.stage canvas { display: block; width: 100%; height: auto; }
.stage svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.busy {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px;
  background: rgba(0, 0, 0, .45); color: #fff; font-weight: 600;
}
.spinner { width: 20px; height: 20px; border: 3px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.summary { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.count { font-weight: 700; font-size: 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 13px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--text-2); }
.chip.warn { color: var(--warn); border-color: currentColor; }

.mode-toggle { display: flex; gap: 8px; margin-top: 12px; }
.mode-toggle .btn { flex: 1; padding: 8px 12px; min-height: 40px; font-size: 14px; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 14px; color: var(--text-2); }
.check input { width: 18px; height: 18px; }

.answer { margin-top: 12px; }
.headline { font-size: 22px; font-weight: 700; margin: 0; }
.subline { color: var(--text-2); margin: 2px 0 12px; }
/* many sets scroll inside the box, not the page; the padding keeps focus rings unclipped */
.set-controls { display: flex; flex-wrap: wrap; align-content: flex-start; gap: 8px; max-height: 45vh; overflow-y: auto; padding: 3px; margin: -3px; }
.set-controls .btn { padding: 8px 12px; min-height: 40px; font-size: 14px; }
/* fade the edge where more buttons are scrolled out of view */
.set-controls.fade-bottom { -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent); mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent); }
.set-controls.fade-top { -webkit-mask-image: linear-gradient(to top, #000 calc(100% - 40px), transparent); mask-image: linear-gradient(to top, #000 calc(100% - 40px), transparent); }
.set-controls.fade-top.fade-bottom { -webkit-mask-image: linear-gradient(transparent, #000 40px, #000 calc(100% - 40px), transparent); mask-image: linear-gradient(transparent, #000 40px, #000 calc(100% - 40px), transparent); }
.more-below { align-self: center; margin: 6px auto 0; display: block; border: 0; background: none; font: inherit; font-size: 13px; font-weight: 600; color: var(--accent); cursor: pointer; padding: 2px 8px; }
.more-below[hidden] { display: none; }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.hint-text { color: var(--text-2); font-size: 14px; margin: 10px 0 0; min-height: 1em; }

.error { color: var(--bad); margin-top: 12px; }

/* overlay */
.ov-card { fill: none; stroke: var(--card-outline); stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.ov-halo { fill: none; stroke: var(--card-outline-halo); stroke-width: 5; vector-effect: non-scaling-stroke; }
.ov-cut { stroke: #fab219; stroke-dasharray: 8 6; }
.ov-set { fill: none; stroke-width: 6; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.ov-hint { fill: rgba(250, 178, 25, .18); stroke: #fab219; stroke-width: 5; vector-effect: non-scaling-stroke; animation: pulse 1.2s ease-in-out infinite; }
.ov-focus { fill: rgba(42, 120, 214, .22); stroke: #2a78d6; stroke-width: 6; vector-effect: non-scaling-stroke; }
@keyframes pulse { 50% { stroke-opacity: .35; fill-opacity: .05; } }
.ov-label { font: 700 13px system-ui, sans-serif; fill: #fff; paint-order: stroke; stroke: rgba(0,0,0,.75); stroke-width: 3px; }

/* debug */
.debug h2 { margin-bottom: 6px; }
.debug-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 8px; }
.stat { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.stat .k { font-size: 12px; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-weight: 700; font-size: 17px; }
.stat .d { font-size: 12.5px; color: var(--text-2); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.card-tile { border: 1px solid var(--border); border-radius: 10px; padding: 8px; cursor: pointer; background: var(--page); text-align: left; font: inherit; color: inherit; }
.card-tile.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card-tile { display: grid; grid-template-columns: 80px 1fr; column-gap: 10px; align-content: start; }
.card-tile canvas { display: block; width: 80px; height: 128px; border-radius: 4px; grid-row: 1 / span 3; }
.card-tile .probs { grid-column: 1 / -1; }
.card-tile .ct-head { font-weight: 700; font-size: 13px; }
.card-tile .ct-pred { font-size: 13px; margin: 2px 0 6px; }
.card-tile .flag { font-size: 12px; font-weight: 600; }
.card-tile .flag.warn { color: var(--warn); }
.prob { display: grid; grid-template-columns: 58px 1fr 38px; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); margin-top: 3px; }
.prob:first-of-type { margin-top: 8px; }
.prob .pb { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.prob .pb > div { height: 100%; background: var(--accent); border-radius: 3px; }
.prob .pb.low > div { background: var(--warn); }
.prob b { color: var(--text); font-weight: 600; text-align: right; }

/* wide screens: photo on the left, results on the right; phones keep the single column */
@media (min-width: 960px) {
  .top, main { max-width: 1320px; }
  .loading, .capture { max-width: 900px; margin-left: auto; margin-right: auto; }
  .results-main {
    display: grid; grid-template-columns: minmax(0, max-content) minmax(340px, 420px);
    justify-content: center; column-gap: 20px; align-items: start;
  }
  /* the whole photo fits on screen; the stage shrinks to it so the overlay stays aligned */
  .stage { width: fit-content; max-width: 100%; }
  .stage canvas { width: auto; max-width: 100%; max-height: calc(100vh - 90px); }
  /* the panel is never taller than the photo can be; only the set buttons scroll */
  .result { position: sticky; top: 12px; display: flex; flex-direction: column; max-height: calc(100vh - 90px); }
  .result .answer:not([hidden]) { display: flex; flex-direction: column; min-height: 0; flex: 0 1 auto; }
  .result .set-controls { max-height: none; min-height: 50px; flex: 0 1 auto; }
}
