:root {
  --ink: #1e2638;
  --muted: #5d6679;
  --paper: #f7f3ea;
  --card: #fffdf8;
  --line: #e3dccb;
  --blue: #1f5fd6;
  --red: #d0342c;
  --gold: #c88a14;
  --sally-a: #b3262b;
  --sally-b: #f4efe4;
  --sally-c: #1f4fa6;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
  -webkit-tap-highlight-color: transparent;
}
main { max-width: 860px; margin: 0 auto; padding: 0 14px 24px; }
.masthead { max-width: 860px; margin: 0 auto; padding: 22px 14px 8px; }
.masthead h1 { font: 700 2rem/1.1 Georgia, "Times New Roman", serif; margin: 0 0 6px; letter-spacing: 0.01em; }
.tag { margin: 0; color: var(--muted); max-width: 46em; }

.panel { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px 20px; margin-top: 12px; }
.step { font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 14px 0 8px; }
.step:first-child { margin-top: 0; }

.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 8px; }
.mode { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: pointer; background: #fff; }
.mode:has(input:checked) { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(31,95,214,0.15); }
.mode input { margin-top: 4px; accent-color: var(--blue); }
.mode small { display: block; color: var(--muted); font-size: 0.85rem; line-height: 1.35; }

.bells { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; max-width: 460px; }
.bell { border: 1px solid var(--line); background: #fff; border-radius: 50% 50% 12px 12px / 60% 60% 12px 12px; padding: 8px 0 6px; cursor: pointer; color: var(--ink); font: inherit; display: flex; flex-direction: column; align-items: center; }
.bell b { font: 700 1.3rem/1 ui-monospace, Menlo, monospace; }
.bell small { font-size: 0.68rem; color: var(--muted); }
.bell.on { background: var(--blue); border-color: var(--blue); color: #fff; }
.bell.on small { color: #dbe6ff; }
.hint { color: var(--muted); font-size: 0.92rem; margin: 8px 0 0; min-height: 1.5em; }

.row-controls { display: flex; flex-wrap: wrap; gap: 10px 22px; align-items: center; }
select { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: #fff; margin-left: 6px; max-width: 100%; }
.check { display: flex; gap: 6px; align-items: center; }
.latency { margin-top: 10px; font-size: 0.92rem; color: var(--muted); }
.latency summary { cursor: pointer; }
.latency input { width: 100%; max-width: 420px; accent-color: var(--blue); }

button { font: inherit; cursor: pointer; }
.primary { background: var(--ink); color: #fff; border: 0; border-radius: 999px; padding: 12px 26px; font-weight: 650; font-size: 1.05rem; }
.primary:hover { background: #2f3b57; }
#start { margin-top: 18px; min-width: 180px; }
.secondary { background: #fff; color: var(--ink); border: 1px solid var(--ink); border-radius: 999px; padding: 11px 20px; font-weight: 600; }
.ghost { background: transparent; border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; color: var(--muted); }
.keys { color: var(--muted); font-size: 0.88rem; margin: 10px 0 0; }
kbd { font: 600 0.8rem ui-monospace, Menlo, monospace; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 0 5px; background: #fff; }

.learn { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 12px; }
.learn summary { cursor: pointer; font-weight: 650; }
.learn-body { padding-top: 6px; }
#studyCanvas, #resultCanvas { width: 100%; display: block; }
.works { columns: 2; font-size: 0.9rem; color: var(--muted); padding-left: 1.2em; }
.notation h3 { font-size: 1rem; margin: 14px 0 4px; }
.notation p { font-size: 0.93rem; margin: 6px 0; }
code { font: 0.9em ui-monospace, Menlo, monospace; background: #f0ebde; padding: 1px 5px; border-radius: 4px; }

/* ---------- ringing stage ---------- */
.stage { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; height: calc(100svh - 120px); min-height: 520px; }
.stagebar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.call { font: 700 1.35rem/1.2 Georgia, serif; min-height: 1.6em; }
.meters { display: flex; gap: 12px; align-items: center; font-size: 0.85rem; color: var(--muted); }
.meter b { color: var(--ink); font-variant-numeric: tabular-nums; }
.ladder-wrap { flex: 1; min-height: 240px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
#ladder { width: 100%; height: 100%; display: block; touch-action: manipulation; }
.clock { display: flex; justify-content: center; gap: 6px; }
.clock .cell { width: 38px; height: 30px; display: grid; place-items: center; border-radius: 8px; background: #fff; border: 1px solid var(--line); font: 700 0.95rem ui-monospace, Menlo, monospace; color: var(--muted); transition: background 60ms; }
.clock .cell.gap { width: 26px; color: #b9b1a0; }
.clock .cell.on { background: var(--gold); color: #fff; border-color: var(--gold); }
.clock .cell.mine { box-shadow: inset 0 0 0 2px var(--blue); color: var(--blue); }
.clock .cell.on.mine { background: var(--blue); color: #fff; }
.feedback { text-align: center; min-height: 1.6em; font-weight: 600; font-variant-numeric: tabular-nums; }
.feedback.good { color: var(--blue); }
.feedback.fair { color: #1a8576; }
.feedback.loose { color: #a86f00; }
.feedback.wrong { color: var(--red); }

.pull { display: flex; align-items: center; gap: 16px; justify-content: center; width: 100%; min-height: 96px; border: 0; border-radius: 18px; background: var(--ink); color: #fff; user-select: none; -webkit-user-select: none; touch-action: manipulation; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.pull:active, .pull.hit { animation: pullhit 160ms ease-out; }
@keyframes pullhit { 0% { transform: translateY(5px); background: #33405e; } 100% { transform: none; } }
.pull.demo { opacity: 0.45; }
.pull-label { display: flex; flex-direction: column; align-items: flex-start; }
.pull-label b { font-size: 1.35rem; }
.pull-label small { color: #c2c8d6; }
.sally { width: 22px; height: 70px; border-radius: 11px; background: repeating-linear-gradient(160deg, var(--sally-a) 0 7px, var(--sally-b) 7px 14px, var(--sally-c) 14px 21px); transition: all 180ms; }
.sally.back { width: 6px; background: #d9c9a3; border-radius: 3px; }

/* ---------- results ---------- */
.verdict { font-size: 1.08rem; margin: 4px 0 12px; }
.res-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 18px; align-items: start; }
.stats { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 0.93rem; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.structure h3 { font-size: 1rem; margin: 14px 0 4px; }
.structure p { font-size: 0.92rem; color: var(--muted); }
.leads { padding-left: 0; list-style: none; margin: 4px 0; font-size: 0.93rem; }
.leads li { padding: 2px 0; }
.leads li span { color: var(--muted); font-variant-numeric: tabular-nums; margin-left: 4px; }
.leads li.ok::before { content: "✓ "; color: var(--blue); font-weight: 700; }
.leads li.slip::before { content: "✗ "; color: var(--red); font-weight: 700; }
.legend { font-size: 0.82rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; }
.sw { display: inline-block; width: 18px; height: 4px; border-radius: 2px; vertical-align: middle; margin-right: -4px; }
.sw.blue { background: var(--blue); }
.sw.ghostline { background: rgba(30,40,60,0.25); }
.sw.red { background: rgba(208,52,44,0.55); height: 2px; }
.sw.miss { background: var(--red); width: 8px; height: 8px; border-radius: 50%; }
.res-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }

.foot { max-width: 860px; margin: 0 auto; padding: 8px 14px 28px; color: var(--muted); font-size: 0.85rem; }
.foot a { color: var(--muted); }
dialog { max-width: 560px; border: 1px solid var(--line); border-radius: 14px; padding: 18px 22px; color: var(--ink); background: var(--card); }
dialog::backdrop { background: rgba(20,24,36,0.4); }
dialog p { font-size: 0.93rem; }

@media (max-width: 640px) {
  .masthead h1 { font-size: 1.6rem; }
  .masthead { padding-top: 14px; }
  .res-grid { grid-template-columns: 1fr; }
  .works { columns: 1; }
  .stage { height: calc(100svh - 24px); min-height: 480px; }
  body.ringing .masthead, body.ringing .foot { display: none; }
  .clock .cell { width: 34px; }
  .call { font-size: 1.1rem; }
  .meters { font-size: 0.78rem; gap: 8px; }
  .pull { min-height: 110px; }
}
