:root {
  --bg: #efe4cc;
  --surface: #f9f3e4;
  --viewer: #e2d3b1;
  --ink: #2b2219;
  --muted: #6f5f4b;
  --line: #cdbb98;
  --accent: #b23f2b;
  --accent-ink: #fffaf0;
  --sea: #2f5d6b;
  --high: #4c7a35;
  --mid: #b98216;
  --low: #b23f2b;
  --zero: #7c7166;
  --redact: #1c1815;
  --shadow: 0 10px 30px rgba(43, 34, 25, 0.18);
  --display: "IM Fell English SC", "Iowan Old Style", Georgia, serif;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17140f;
    --surface: #221d16;
    --viewer: #0f0d0a;
    --ink: #efe4cc;
    --muted: #a8977c;
    --line: #3d3428;
    --accent: #e06a50;
    --accent-ink: #1a120c;
    --sea: #7fb2bf;
    --high: #8dbb6c;
    --mid: #e0b04a;
    --low: #e06a50;
    --zero: #9a8f84;
    --redact: #000;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17140f;
  --surface: #221d16;
  --viewer: #0f0d0a;
  --ink: #efe4cc;
  --muted: #a8977c;
  --line: #3d3428;
  --accent: #e06a50;
  --accent-ink: #1a120c;
  --sea: #7fb2bf;
  --high: #8dbb6c;
  --mid: #e0b04a;
  --low: #e06a50;
  --zero: #9a8f84;
  --redact: #000;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "stage" "panel";
  height: 100dvh;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--sea); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
.muted { color: var(--muted); font-size: 13px; }

/* ------------------------------------------------------------ header */
.topbar {
  grid-area: top;
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand {
  display: flex; align-items: center; gap: 10px; margin: 0;
  font: 400 28px/1 var(--display); letter-spacing: 0.02em;
}
.brand-mark { width: 28px; height: 28px; }
.brand-num { font: 600 13px/1 var(--sans); color: var(--muted); letter-spacing: 0; transform: translateY(2px); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: 1.5px solid var(--line); background: transparent;
  display: grid; place-items: center; font-weight: 700; font-size: 17px;
}
.icon-btn:hover { border-color: var(--ink); }
.icon-btn svg { width: 18px; height: 18px; }

/* ------------------------------------------------------------ viewer */
.stage { grid-area: stage; position: relative; min-height: 0; background: var(--viewer); }
.viewer { position: absolute; inset: 0; }
.viewer-error { padding: 24px; text-align: center; color: var(--muted); }
.viewer-tools {
  position: absolute; top: 12px; right: 12px; z-index: 5;
  display: flex; flex-direction: column; gap: 6px;
}
.tool {
  width: 40px; height: 40px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface);
  box-shadow: var(--shadow); font-size: 22px; line-height: 1;
  display: grid; place-items: center;
}
.tool svg { width: 20px; height: 20px; }
.tool:hover { border-color: var(--ink); }
.viewer-hint {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  background: rgba(28, 24, 21, 0.78); color: #f4ead3; padding: 6px 14px; border-radius: 99px;
  font-size: 13px; pointer-events: none; transition: opacity 0.6s; white-space: nowrap; z-index: 4;
}
.viewer-hint.gone { opacity: 0; }

/* ------------------------------------------------------------ panel */
.panel {
  grid-area: panel;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 14px 16px;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  overflow-y: auto;
  max-height: 55dvh;
}
.guess { display: grid; gap: 10px; max-width: 560px; margin: 0 auto; }
.guess-row { display: flex; gap: 6px; align-items: center; justify-content: center; }
.nudge {
  min-width: 46px; height: 44px; padding: 0 8px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); font-weight: 600; font-variant-numeric: tabular-nums;
}
.nudge:hover { border-color: var(--ink); }
.year-field input {
  width: 6ch; height: 52px; text-align: center;
  font: 400 40px/1 var(--display); color: var(--ink);
  background: transparent; border: none; border-bottom: 2px solid var(--ink);
  -moz-appearance: textfield; appearance: textfield;
}
.year-field input::-webkit-outer-spin-button,
.year-field input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.year-field input:focus { outline: none; border-bottom-color: var(--accent); }

.slider {
  --pct: 50%;
  -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; margin: 0;
}
.slider::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--accent) var(--pct), var(--line) var(--pct));
}
.slider::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
.slider::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; width: 26px; height: 26px; margin-top: -10px; border-radius: 50%;
  background: var(--surface); border: 3px solid var(--accent); box-shadow: var(--shadow);
}
.slider::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface); border: 3px solid var(--accent);
}
.slider-scale { position: relative; height: 16px; margin: -6px 13px 0; font-size: 11px; color: var(--muted); }
.slider-scale span { position: absolute; transform: translateX(-50%); font-variant-numeric: tabular-nums; }

.primary {
  height: 48px; border-radius: 12px; border: none;
  background: var(--accent); color: var(--accent-ink);
  font-weight: 700; font-size: 16px; letter-spacing: 0.01em;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}
.primary:hover { filter: brightness(1.06); }
.primary:active { transform: translateY(1px); }

/* ------------------------------------------------------------ result */
.result { max-width: 560px; margin: 0 auto; display: grid; gap: 14px; }
.result-head { display: flex; gap: 16px; align-items: center; }
.score-big { font: 400 56px/1 var(--display); color: var(--high); font-variant-numeric: tabular-nums; }
.score-big[data-tier="mid"] { color: var(--mid); }
.score-big[data-tier="low"] { color: var(--low); }
.score-big[data-tier="zero"] { color: var(--zero); }
.score-big small { font: 600 16px var(--sans); color: var(--muted); }
.r-verdict { margin: 0; font-weight: 700; font-size: 18px; }
.r-diff { margin: 2px 0 0; color: var(--muted); }

.timeline {
  position: relative; height: 46px; margin: 4px 12px 0;
  border-bottom: 2px solid var(--line);
}
.tl-window {
  position: absolute; bottom: -2px; height: 10px; border-radius: 4px 4px 0 0;
  background: color-mix(in srgb, var(--high) 35%, transparent);
}
.tl-pin { position: absolute; bottom: -2px; width: 2px; height: 26px; transform: translateX(-1px); }
.tl-pin span {
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  font: 600 12px/1 var(--sans); padding: 3px 6px; border-radius: 6px; white-space: nowrap;
}
.tl-answer { background: var(--high); }
.tl-answer span { background: var(--high); color: var(--surface); }
.tl-guess { background: var(--accent); height: 12px; }
.tl-guess span { background: var(--accent); color: var(--accent-ink); }
.timeline.close .tl-guess span { bottom: auto; top: 18px; }
.timeline.close { margin-bottom: 18px; }

.reveal h2 { margin: 0 0 2px; font: 400 22px/1.2 var(--display); }
.r-meta { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.r-blurb { margin: 0 0 6px; }
.r-credit { margin: 0; font-size: 13px; color: var(--muted); }

.share-row { display: grid; gap: 8px; }
.share-preview {
  margin: 0; padding: 10px 12px; border-radius: 10px; border: 1px dashed var(--line);
  background: var(--bg); font: 14px/1.45 var(--sans); white-space: pre-wrap; word-break: break-word;
}
.next { margin: 0; text-align: center; color: var(--muted); }
.next strong { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ modals */
.modal {
  width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 48px);
  border: 1px solid var(--line); border-radius: 16px; padding: 20px 22px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(20, 16, 12, 0.55); }
.modal h2 { margin: 0 0 10px; font: 400 26px/1.1 var(--display); }
.modal h3 { margin: 18px 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.modal ol { padding-left: 20px; }
.modal li { margin-bottom: 6px; }
.close {
  float: right; border: none; background: transparent; font-size: 26px; line-height: 1;
  margin: -6px -8px 0 0; color: var(--muted);
}
.redacted-sample { background: var(--redact); color: transparent; border-radius: 2px; padding: 0 2px; }
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.stat strong { display: block; font: 400 32px/1.1 var(--display); }
.stat span { font-size: 12px; color: var(--muted); }
.dist { display: grid; gap: 5px; }
.dist-row { display: grid; grid-template-columns: 58px 1fr; gap: 8px; align-items: center; font-size: 13px; }
.dist-label { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.dist-bar {
  --w: 0%;
  display: block; width: max(var(--w), 26px); background: var(--line); border-radius: 4px;
  text-align: right; padding: 1px 7px;
}
.dist-row.mine .dist-bar { background: var(--accent); color: var(--accent-ink); }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); font-weight: 600; max-width: calc(100vw - 32px);
}

/* OpenSeadragon navigator */
.navigator { border: 2px solid var(--surface) !important; box-shadow: var(--shadow); background: var(--viewer) !important; }
.displayregion { border-color: var(--accent) !important; }

/* ------------------------------------------------------------ wide screens: sidebar */
@media (min-width: 900px) {
  body {
    grid-template-columns: minmax(0, 1fr) 400px;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "top top" "stage panel";
  }
  .panel {
    max-height: none; border-top: none; border-left: 1px solid var(--line);
    padding: 24px; display: flex; flex-direction: column; justify-content: center;
  }
  .guess, .result { width: 100%; }
  body.solved .panel { justify-content: flex-start; }
}
@media (max-width: 420px) {
  .nudge { min-width: 40px; padding: 0 4px; }
  .year-field input { font-size: 34px; }
  .brand { font-size: 24px; }
  .result-head { gap: 12px; }
  .score-big { font-size: 46px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ------------------------------------------------------------ archive */
.topbar-right { display: flex; gap: 8px; }
.icon-btn { position: relative; }
.badge {
  position: absolute; top: -4px; right: -6px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  font: 700 11px/18px var(--sans); text-align: center;
}
.archive-bar {
  grid-area: notice;
  display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; justify-content: center;
  padding: 7px 16px; font-size: 13px; text-align: center;
  background: color-mix(in srgb, var(--mid) 22%, var(--surface));
  border-bottom: 1px solid var(--line);
}
.archive-bar a { font-weight: 600; white-space: nowrap; }
body:has(.archive-bar) {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "notice" "stage" "panel";
}
.secondary {
  height: 44px; border-radius: 12px; border: 1.5px solid var(--accent);
  background: transparent; color: var(--accent); font-weight: 700;
}
.secondary:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.archive-list {
  list-style: none; margin: 8px 0 0; padding: 0;
  max-height: min(60dvh, 480px); overflow-y: auto; display: grid; gap: 4px;
}
.archive-list a {
  display: grid; grid-template-columns: 52px 1fr auto; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  color: inherit; text-decoration: none;
}
.archive-list a:hover { border-color: var(--ink); }
.archive-list .current a { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.a-num { font-weight: 700; font-variant-numeric: tabular-nums; }
.a-date { color: var(--muted); }
.a-play { font-weight: 600; color: var(--accent); }
.a-score {
  min-width: 38px; text-align: center; padding: 2px 8px; border-radius: 99px;
  font-weight: 700; font-variant-numeric: tabular-nums; color: var(--surface); background: var(--high);
}
.a-score[data-tier="mid"] { background: var(--mid); }
.a-score[data-tier="low"] { background: var(--low); }
.a-score[data-tier="zero"] { background: var(--zero); }
@media (min-width: 900px) {
  body:has(.archive-bar) {
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "top top" "notice notice" "stage panel";
  }
}

/* ------------------------------------------------------------ guesses & hints */
.hints { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.hint {
  display: flex; gap: 10px; align-items: baseline; padding: 7px 12px; border-radius: 10px;
  border: 1px solid var(--line); border-left: 4px solid var(--far); background: var(--bg); font-size: 14px;
}
.hint[data-near="true"] { border-left-color: var(--near); }
.hint b { font: 400 20px/1 var(--display); font-variant-numeric: tabular-nums; min-width: 3.2ch; }
.hint span { color: var(--muted); }
.hint.fresh { animation: hint-in 0.45s ease-out; }
@keyframes hint-in { from { transform: translateY(-6px); opacity: 0; } to { transform: none; opacity: 1; } }
.slider-wrap { position: relative; }
.slider-marks { position: absolute; inset: 0 13px; pointer-events: none; }
.mark {
  position: absolute; top: 4px; width: 4px; height: 20px; margin-left: -2px; border-radius: 2px;
  background: var(--far); box-shadow: 0 0 0 1.5px var(--surface);
}
.mark[data-near="true"] { background: var(--near); }
.guess-count { display: block; font-size: 12px; font-weight: 600; opacity: 0.8; letter-spacing: 0.02em; }
.primary:has(.guess-count:not(:empty)) { height: 54px; }
.r-trail { margin: -4px 0 0; font-size: 13px; color: var(--muted); }
.r-trail:empty { display: none; }
.tl-tick {
  position: absolute; bottom: -2px; width: 2px; height: 8px; transform: translateX(-1px);
  background: var(--muted); opacity: 0.7;
}
:root { --near: #d9822b; --far: #3f74a8; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --near: #e9a04f; --far: #7aa9d6; } }
:root[data-theme="dark"] { --near: #e9a04f; --far: #7aa9d6; }
