/* Interactive Chess Book — warm "old book" palette */
:root {
  --paper: #f5efe3;
  --paper-2: #ede5d4;
  --surface: #fbf8f1;
  --ink: #2a2420;
  --ink-2: #5d534a;
  --ink-3: #8f8477;
  --line: #d9cfbd;
  --accent: #2f5d50;
  --accent-ink: #f5efe3;
  --accent-soft: #dce7e1;

  --sq-light: #eadbc0;
  --sq-dark: #b08a62;
  --sq-hl: rgba(236, 196, 64, 0.55);
  --sq-last: rgba(47, 93, 80, 0.28);
  --sq-select: rgba(47, 93, 80, 0.45);

  --arrow-primary: #2f5d50;
  --arrow-danger: #b23a2e;
  --arrow-warning: #c78a12;
  --arrow-muted: #6d6358;
  --zone-danger: rgba(178, 58, 46, 0.30);
  --zone-warning: rgba(212, 150, 20, 0.34);
  --zone-primary: rgba(47, 93, 80, 0.30);

  --ok: #2f6b3a;
  --bad: #a13a2c;

  --font-text: 'Zodiak', 'Iowan Old Style', 'Palatino Linotype', serif;
  --font-ui: 'Satoshi', 'Segoe UI', system-ui, sans-serif;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(42, 36, 32, .06), 0 8px 24px rgba(42, 36, 32, .08);
  --move-ms: 420ms;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1c1916;
    --paper-2: #24201c;
    --surface: #26221e;
    --ink: #efe7da;
    --ink-2: #c3b8a8;
    --ink-3: #8e8476;
    --line: #3a342d;
    --accent: #7fb8a4;
    --accent-ink: #13201b;
    --accent-soft: #24352f;
    --ok: #8fcf98;
    --bad: #f0907f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
    --paper: #1c1916;
    --paper-2: #24201c;
    --surface: #26221e;
    --ink: #efe7da;
    --ink-2: #c3b8a8;
    --ink-3: #8e8476;
    --line: #3a342d;
    --accent: #7fb8a4;
    --accent-ink: #13201b;
    --accent-soft: #24352f;
    --ok: #8fcf98;
    --bad: #f0907f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  padding: 0.9rem clamp(1rem, 3vw, 2.5rem);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: .75rem; color: var(--ink); text-decoration: none; }
.logo { width: 34px; height: 34px; color: var(--accent); flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-name { font-weight: 700; letter-spacing: -0.01em; }
.brand-sub { font-size: .85rem; color: var(--ink-2); }

.progress { display: flex; gap: 6px; align-items: center; }
.progress button {
  width: 30px; height: 8px; padding: 0; border: 0; border-radius: 4px;
  background: var(--line); cursor: pointer; transition: background .2s, transform .2s;
}
.progress button:hover { transform: scaleY(1.4); }
.progress button.done { background: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.progress button.current { background: var(--accent); }
.progress button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  width: 100%;
  max-width: 1240px;
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2.5rem);
  display: grid;
  grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
  gap: clamp(1.5rem, 4vw, 3.5rem);
  align-items: start;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
}

/* ---------- Board ---------- */
.board-col { position: sticky; top: 1rem; }
@media (max-width: 860px) { .board-col { position: static; } }
.board-wrap {
  padding: 10px;
  background: #5b4230;
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-width: min(600px, 100%, 72dvh);
  margin: 0 auto;
}
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  user-select: none;
  touch-action: none;
  border-radius: 4px;
  overflow: hidden;
}
.squares {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
}
.sq { position: relative; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq::after { content: ""; position: absolute; inset: 0; pointer-events: none; transition: background .25s; }
.sq.last::after { background: var(--sq-last); }
.sq.hl::after { background: var(--sq-hl); }
.sq.selected::after { background: var(--sq-select); }
.sq.hint::after { background: transparent; box-shadow: inset 0 0 0 4px var(--arrow-warning); animation: pulse 1.2s ease-in-out infinite; }
.sq.target::before {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 28%; height: 28%; transform: translate(-50%, -50%);
  border-radius: 50%; background: rgba(42, 36, 32, .28); z-index: 1; pointer-events: none;
}
.sq.target.capture::before { width: 86%; height: 86%; background: transparent; box-shadow: inset 0 0 0 5px rgba(42,36,32,.28); }
.sq.wrong::after { background: rgba(178, 58, 46, .45); }
@keyframes pulse { 50% { box-shadow: inset 0 0 0 7px var(--arrow-warning); } }

.coord {
  position: absolute; font-size: clamp(9px, 1.5vw, 12px); font-weight: 700;
  pointer-events: none; line-height: 1;
}
.coord.rank { top: 4px; left: 4px; }
.coord.file { bottom: 3px; right: 4px; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }
.overlay.zones-layer { z-index: 1; }
.overlay .zone { transition: opacity .3s; }
.overlay .arrow { opacity: .85; }

.pieces { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.piece {
  position: absolute; left: 0; top: 0;
  width: 12.5%; height: 12.5%;
  transition: transform var(--move-ms) cubic-bezier(.3,.7,.2,1), opacity .25s;
  will-change: transform;
}
.piece img { width: 100%; height: 100%; display: block; pointer-events: none; }
.piece.dragging { transition: none; z-index: 10; filter: drop-shadow(0 6px 8px rgba(0,0,0,.35)); }
.piece.fading { opacity: 0; }
.board.interactive { cursor: pointer; }
.board.interactive .pieces { pointer-events: none; }

/* ---------- Scoresheet ---------- */
.scoresheet {
  max-width: min(600px, 100%, 72dvh);
  margin: 1rem auto 0;
  display: flex; gap: .75rem; align-items: baseline;
  font-size: .92rem; color: var(--ink-2);
}
.scoresheet .label { font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .08em; font-size: .72rem; }
.moves { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .15rem .7rem; font-variant-numeric: tabular-nums; }
.moves li { white-space: nowrap; }
.moves .num { color: var(--ink-3); margin-right: .2rem; }
.moves .san.latest { color: var(--ink); font-weight: 700; background: var(--accent-soft); border-radius: 4px; padding: 0 .25rem; }
.moves:empty::before { content: "—"; }

/* ---------- Text column ---------- */
.text-col { max-width: 560px; }
.step-count { margin: 0 0 .35rem; font-size: .8rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.step-title {
  font-family: var(--font-text); font-weight: 500;
  font-size: clamp(1.7rem, 1.2rem + 1.6vw, 2.4rem);
  line-height: 1.1; letter-spacing: -0.015em; margin: 0 0 1.25rem;
  text-wrap: balance;
}
.narration { font-family: var(--font-text); font-size: 1.2rem; line-height: 1.6; }
.narration .seg {
  display: inline; color: var(--ink-3);
  transition: color .3s, background-color .3s;
  border-radius: 3px; padding: 0 .08em;
  box-decoration-break: clone; -webkit-box-decoration-break: clone;
}
.narration .seg.spoken { color: var(--ink); }
.narration .seg.active { color: var(--ink); background: var(--accent-soft); }
.narration.static .seg { color: var(--ink); }

.exercise {
  margin-top: 1.5rem; padding: 1.1rem 1.2rem;
  background: var(--surface); border: 1px solid var(--line);
  border-left: 4px solid var(--arrow-warning); border-radius: var(--radius);
}
.exercise.solved { border-left-color: var(--ok); }
.exercise-prompt { margin: 0; font-weight: 700; }
.exercise-feedback { margin: .5rem 0 0; min-height: 1.5em; color: var(--ink-2); }
.exercise-feedback.ok { color: var(--ok); font-weight: 500; }
.exercise-feedback.bad { color: var(--bad); }
.exercise-actions { display: flex; gap: .5rem; margin-top: .75rem; }

.source { margin-top: 1.5rem; border-top: 1px solid var(--line); padding-top: 1rem; }
.source summary { cursor: pointer; font-weight: 500; color: var(--ink-2); font-size: .92rem; }
.source summary:hover { color: var(--ink); }
.source blockquote {
  margin: .85rem 0 .5rem; padding-left: 1rem; border-left: 2px solid var(--line);
  font-family: var(--font-text); font-style: italic; color: var(--ink-2); line-height: 1.55;
}
.source-ref { margin: 0; font-size: .82rem; color: var(--ink-3); }

/* ---------- Controls ---------- */
.controls { display: flex; gap: .6rem; margin-top: 1.75rem; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: 0 1rem;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--ink);
  font-weight: 500; cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { border-color: var(--ink-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: default; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); min-width: 120px; }
.btn.primary svg { fill: currentColor; stroke: none; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { min-height: 36px; font-size: .9rem; background: transparent; }
.btn .icon-stop { display: none; }
.btn.playing .icon-play { display: none; }
.btn.playing .icon-stop { display: block; }
.btn.pulse { animation: btnpulse 1.4s ease-in-out infinite; }
@keyframes btnpulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 22%, transparent); } }

.settings {
  display: flex; flex-wrap: wrap; gap: .75rem 1.25rem; align-items: center;
  margin-top: 1.25rem; font-size: .88rem; color: var(--ink-2);
}
.toggle { display: inline-flex; gap: .4rem; align-items: center; cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }
.field { display: inline-flex; gap: .5rem; align-items: center; }
.field select {
  max-width: 220px; padding: .3rem .5rem; border: 1px solid var(--line);
  border-radius: 6px; background: var(--surface);
}
.field input[type=range] { accent-color: var(--accent); width: 110px; }
.kbd-hint { margin-top: 1rem; font-size: .8rem; color: var(--ink-3); }
kbd {
  font-family: var(--font-ui); font-size: .75rem; padding: 0 .35rem;
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; background: var(--surface);
}
@media (hover: none) { .kbd-hint { display: none; } }

.footer {
  border-top: 1px solid var(--line);
  padding: 1rem clamp(1rem, 3vw, 2.5rem);
  font-size: .8rem; color: var(--ink-3);
}
.footer p { margin: 0; max-width: 1240px; margin: 0 auto; }
.footer a { color: var(--ink-2); }

@media (prefers-reduced-motion: reduce) {
  :root { --move-ms: 0ms; }
  .sq.hint::after, .btn.pulse { animation: none; }
}

/* ---------- Top bar extras ---------- */
.topbar-right { display: flex; align-items: center; gap: 1rem; }
.topbar-right .btn.ghost { min-height: 34px; text-decoration: none; }
body.on-toc .progress { display: none; }

/* ---------- Contents ---------- */
.toc { flex: 1; width: 100%; max-width: 980px; margin: 0 auto; padding: clamp(1.5rem, 5vw, 4rem) clamp(1rem, 3vw, 2.5rem); }
.toc-head { max-width: 640px; margin-bottom: 3rem; }
.toc-title { font-family: var(--font-text); font-weight: 500; font-size: clamp(2.4rem, 1.6rem + 3vw, 3.6rem); line-height: 1.05; letter-spacing: -0.02em; margin: 0 0 1rem; }
.toc-lede { font-family: var(--font-text); font-size: 1.2rem; color: var(--ink-2); margin: 0 0 1.5rem; }
.toc-head .btn.primary { text-decoration: none; }
.chapter { border-top: 1px solid var(--line); padding: 1.5rem 0; display: grid; grid-template-columns: 200px 1fr; gap: 1.5rem; }
@media (max-width: 700px) { .chapter { grid-template-columns: 1fr; gap: .5rem; } }
.chapter-label { font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); margin: 0; }
.chapter-title { font-family: var(--font-text); font-weight: 500; font-size: 1.2rem; margin: .25rem 0 0; line-height: 1.25; }
.sections { list-style: none; margin: 0; padding: 0; display: grid; gap: .9rem; }
.section-row { display: grid; grid-template-columns: 2.2rem 1fr; gap: .25rem .5rem; }
.section-num { color: var(--ink-3); font-variant-numeric: tabular-nums; }
.section-title { font-weight: 500; }
.section-row.soon .section-title { color: var(--ink-3); font-weight: 400; }
.section-row.soon .section-title::after { content: "coming soon"; margin-left: .6rem; font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); border: 1px solid var(--line); border-radius: 999px; padding: .05rem .5rem; }
.lesson-links { grid-column: 2; display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .35rem; }
.lesson-link { display: inline-flex; gap: .5rem; align-items: baseline; padding: .45rem .8rem; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); text-decoration: none; font-size: .92rem; transition: border-color .15s, transform .15s; }
.lesson-link:hover { border-color: var(--accent); transform: translateY(-1px); }
.lesson-link .ex { color: var(--accent); font-weight: 700; font-size: .78rem; }

/* ---------- Board themes ---------- */
:root, :root[data-board="walnut"] { --sq-light: #eadbc0; --sq-dark: #b08a62; --frame: #5b4230; }
:root[data-board="blue"]   { --sq-light: #dee3e6; --sq-dark: #8ca2ad; --frame: #4a5d68; }
:root[data-board="green"]  { --sq-light: #ffffdd; --sq-dark: #86a666; --frame: #4d6437; }
:root[data-board="slate"]  { --sq-light: #d9d9d9; --sq-dark: #9a9a9a; --frame: #505050; }
:root[data-board="coral"]  { --sq-light: #f3e2d8; --sq-dark: #d08b73; --frame: #7a4636; }
:root[data-board="ice"]    { --sq-light: #e8f1f7; --sq-dark: #9fbbd1; --frame: #52708a; }
.board-wrap { background: var(--frame); }
.board.no-coords .coord { display: none; }

.settings-panel { margin-top: 1.25rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.settings-panel summary { display: flex; align-items: center; gap: .5rem; padding: .7rem 1rem; cursor: pointer; font-weight: 500; font-size: .92rem; color: var(--ink-2); list-style: none; }
.settings-panel summary::-webkit-details-marker { display: none; }
.settings-panel summary svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.settings-panel[open] summary { border-bottom: 1px solid var(--line); }
.settings-panel .settings { margin: 0; padding: 1rem; display: grid; grid-template-columns: 1fr; gap: .85rem; }
.settings-panel .field { display: grid; grid-template-columns: 7rem 1fr; align-items: center; }
.settings-panel .field select { max-width: 100%; width: 100%; }
.swatches { display: flex; flex-wrap: wrap; gap: .5rem; }
.swatch { width: 34px; height: 34px; padding: 0; border-radius: 6px; border: 2px solid transparent; cursor: pointer; overflow: hidden; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.swatch span { display: block; }
.swatch[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--surface) inset; }
.swatch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.waiting { animation: btnpulse 1.4s ease-in-out infinite; }

@media (max-width: 860px) {
  .controls { position: sticky; bottom: 0; z-index: 20; background: var(--paper); margin: 1.25rem -1rem 0; padding: .6rem 1rem calc(.6rem + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
  .controls .btn.primary { flex: 1; }
  .narration { font-size: 1.1rem; }
}

/* silent modes: quicker moves */
:root[data-pace="fast"] { --move-ms: 240ms; }
@media (prefers-reduced-motion: reduce) { :root[data-pace="fast"] { --move-ms: 0ms; } }
