@import url("lesson-header.css?v=20260826-swipeable-nav");

/* Shared figure/image styles for lesson artwork */
.figure {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(0,0,0,.18);
  margin: 1.2rem 0 0;
}
.figure figcaption {
  color: var(--muted);
  font-size: .92rem;
  padding: .8rem 1rem;
  border-top: 1px solid var(--line);
  background: rgba(0,0,0,.12);
}

/* ==========================================================================
   endgame-lesson.css
   Shared layout + print styles for chess endgame lesson pages.
   Reused by every chapter HTML in /lessons. Fonts and piece SVGs are loaded
   from the parent /assets directory so no CDN or external dependency is used.
   ========================================================================== */

/* ---------- Fonts (mirror styles.css @font-face, relative to /lessons) ----- */
@font-face {
  font-family: "Inter";
  src: url("../assets/Inter/Inter-VariableFont_opsz,wght.ttf") format("truetype");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Inter";
  src: url("../assets/Inter/Inter-Italic-VariableFont_opsz,wght.ttf") format("truetype");
  font-weight: 100 900;
  font-style: italic;
  font-display: swap;
}
@font-face {
  font-family: "Manrope";
  src: url("../assets/Manrope/Manrope-VariableFont_wght.ttf") format("truetype");
  font-weight: 200 800;
  font-style: normal;
  font-display: swap;
}

/* ---------- Design tokens (aligned with main app palette) ---------------- */
:root {
  color-scheme: light;
  --board-light: #eee3cb;
  --board-dark: #71925a;
  --board-light-dark: #f1e3c6;
  --board-dark-dark: #6f8f72;
  --piece-shadow: rgba(7, 17, 15, 0.28);

  --ink: #172127;
  --ink-soft: #52606a;
  --ink-faint: #7b8790;
  --rule-ink: #155e67;
  --rule-bg: rgba(29, 111, 120, 0.11);
  --rule-border: rgba(190, 202, 207, 0.9);
  --recap-ink: #257a5f;
  --recap-bg: rgba(37, 122, 95, 0.1);
  --recap-border: rgba(190, 202, 207, 0.9);
  --goal-bg: rgba(255, 255, 255, 0.94);
  --goal-border: rgba(190, 202, 207, 0.88);
  --accent: #1d6f78;
  --accent-strong: #155e67;
  --accent-soft: rgba(29, 111, 120, 0.11);
  --hairline: rgba(190, 202, 207, 0.78);
  --page-bg: #f6f7f8;
  --page-bg-print: #ffffff;
  --toolbar-bg: rgba(255, 255, 255, 0.98);
  --btn-bg: rgba(255, 255, 255, 0.96);
  --btn-hover: rgba(29, 111, 120, 0.08);
  --btn-ink: #155e67;
  --check-fill: #ffffff;
  --panel-bg: rgba(255, 255, 255, 0.94);
  --panel-border: rgba(190, 202, 207, 0.88);
  --success: #257a5f;
  --danger: #c05243;

  --font-body: "Inter", "Segoe UI", system-ui, sans-serif;
  --font-display: "Manrope", "Inter", "Segoe UI", sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

  --max-width: 1280px;
  --board-max: clamp(280px, calc(100vw * 0.4 + 60px), 640px);
}

/* ---------- Dark theme (screen only; print forces light below) ---------- */
[data-theme="dark"] {
  color-scheme: dark;
  --board-light: var(--board-light-dark);
  --board-dark: var(--board-dark-dark);
  --piece-shadow: rgba(0, 0, 0, 0.4);

  --ink: #f7faf5;
  --ink-soft: #b9c6c3;
  --ink-faint: #899695;
  --rule-ink: #b0e8cf;
  --rule-bg: rgba(138, 216, 183, 0.16);
  --rule-border: rgba(52, 74, 78, 0.92);
  --recap-ink: #8ad8b7;
  --recap-bg: rgba(37, 122, 95, 0.18);
  --recap-border: rgba(52, 74, 78, 0.92);
  --goal-bg: rgba(27, 42, 46, 0.92);
  --goal-border: rgba(52, 74, 78, 0.94);
  --accent: #8ad8b7;
  --accent-strong: #b0e8cf;
  --accent-soft: rgba(138, 216, 183, 0.16);
  --hairline: rgba(52, 74, 78, 0.9);
  --page-bg: #071012;
  --toolbar-bg: rgba(28, 43, 48, 0.98);
  --btn-bg: rgba(28, 43, 48, 0.96);
  --btn-hover: rgba(138, 216, 183, 0.14);
  --btn-ink: #b0e8cf;
  --check-fill: #071012;
  --panel-bg: rgba(27, 42, 46, 0.92);
  --panel-border: rgba(52, 74, 78, 0.94);
  --success: #8ad8b7;
  --danger: #ff927d;
}

/* ---------- Base --------------------------------------------------------- */
* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(127, 175, 155, 0.14) 0%, transparent 70%),
    radial-gradient(ellipse 50% 50% at 80% 20%, rgba(201, 135, 61, 0.1) 0%, transparent 60%),
    linear-gradient(180deg, var(--page-bg) 0%, color-mix(in srgb, var(--page-bg) 96%, var(--ink-faint)) 52%, color-mix(in srgb, var(--page-bg) 92%, var(--ink-faint)) 100%);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: clamp(16px, 0.6vw + 15px, 19px);
  line-height: 1.55;
  font-feature-settings: "kern", "liga", "calt";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.lesson-page {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: clamp(24px, 2.5vw + 16px, 40px) clamp(16px, 3vw + 10px, 48px) clamp(64px, 4vw + 48px, 96px);
}

/* ---------- Top toolbar (screen only) ------------------------------------ */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 14px;
  margin: -40px -48px 24px;
  background: var(--toolbar-bg);
  backdrop-filter: saturate(140%) blur(6px);
  border-bottom: 1px solid var(--hairline);
}
.toolbar {
  flex-wrap: wrap;
  justify-content: flex-end;
}
.toolbar button {
  font-family: var(--font-body);
  font-size: clamp(13px, 0.3vw + 12px, 15px);
  font-weight: 600;
  white-space: nowrap;
  color: var(--btn-ink);
  background: var(--btn-bg);
  border: 1px solid var(--rule-border);
  border-radius: 8px;
  padding: clamp(6px, 0.3vw + 5px, 8px) clamp(12px, 0.5vw + 10px, 16px);
  cursor: pointer;
}
.toolbar button:hover { background: var(--btn-hover); }
.toolbar-link {
  font-family: var(--font-body);
  font-size: clamp(13px, 0.3vw + 12px, 15px);
  font-weight: 600;
  white-space: nowrap;
  color: var(--btn-ink);
  background: var(--btn-bg);
  border: 1px solid var(--rule-border);
  border-radius: 8px;
  padding: clamp(6px, 0.3vw + 5px, 8px) clamp(12px, 0.5vw + 10px, 16px);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.toolbar-link:hover { background: var(--btn-hover); }

/* ---------- Index page header (compact app-style topbar) ----------------- */
.index-header {
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: saturate(140%) blur(16px);
  background: color-mix(in srgb, var(--page-bg) 78%, transparent);
  border-bottom: 1px solid var(--hairline);
}
.index-header-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .65rem clamp(16px, 3vw + 10px, 48px);
}
.index-brand {
  display: flex;
  align-items: center;
  gap: .7rem;
  font-weight: 800;
  letter-spacing: .01em;
  min-width: 0;
}
.index-brand-icon {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: .8rem;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 30%, transparent));
  color: #13203a;
  box-shadow: 0 10px 30px color-mix(in srgb, var(--accent) 25%, transparent);
  font-size: 1.3rem;
}
.index-brand-label {
  font-size: .78rem;
  opacity: .6;
  letter-spacing: .06em;
  text-transform: uppercase;
  line-height: 1.3;
}
.index-brand-title {
  font-size: clamp(1rem, 1.2vw + 0.5rem, 1.25rem);
  line-height: 1.3;
  color: var(--ink);
}
.index-top-actions {
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex-shrink: 0;
}
.index-top-actions .toolbar-link {
  border-radius: 999px;
  padding: .45rem .85rem;
  font-size: clamp(12px, 0.3vw + 11px, 14px);
}
.index-top-actions .toolbar-link:hover {
  background: var(--btn-hover);
  transform: translateY(-1px);
}

/* ---------- Floating analysis popup (parent-level, draggable anywhere) --- */
.analysis-popup {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 1000;
  width: 340px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 32px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--popup-bg, #172127);
  border: 1px solid var(--popup-border, #34405c);
  border-radius: 10px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(14px);
  font-size: 13px;
  line-height: 1.5;
  color: var(--popup-ink, #e8ebf2);
}
[data-theme="light"] .analysis-popup {
  --popup-bg: #ffffff;
  --popup-border: rgba(190, 202, 207, 0.94);
  --popup-ink: #172127;
  --popup-muted: #52606a;
  --popup-accent: #1d6f78;
  --popup-card-bg: rgba(242, 246, 247, 0.92);
  --popup-card-border: rgba(190, 202, 207, 0.88);
}
[data-theme="dark"] .analysis-popup {
  --popup-bg: rgba(28, 43, 48, 0.98);
  --popup-border: rgba(52, 74, 78, 0.94);
  --popup-ink: #f7faf5;
  --popup-muted: #b9c6c3;
  --popup-accent: #8ad8b7;
  --popup-card-bg: rgba(27, 42, 46, 0.92);
  --popup-card-border: rgba(52, 74, 78, 0.94);
}
.analysis-popup[hidden] { display: none; }
.analysis-popup.is-dragging {
  box-shadow: 0 24px 50px rgba(0, 0, 0, 0.55);
  cursor: grabbing;
}
.analysis-popup-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  cursor: grab;
  border-bottom: 1px solid var(--popup-border);
  user-select: none;
}
.analysis-popup-head:active { cursor: grabbing; }
.analysis-popup-grip {
  font-size: 14px;
  color: var(--popup-muted);
  line-height: 1;
}
.analysis-popup-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--popup-accent);
}
.analysis-popup-eval {
  margin-left: auto;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 14px;
  color: var(--popup-accent);
}
.analysis-popup-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid var(--popup-border);
  border-radius: 5px;
  background: transparent;
  color: var(--popup-muted);
  cursor: pointer;
  flex-shrink: 0;
}
.analysis-popup-toggle:hover {
  background: var(--popup-card-bg);
  color: var(--popup-accent);
}
.analysis-popup-toggle svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.analysis-popup-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid var(--popup-border);
  border-radius: 5px;
  background: transparent;
  color: var(--popup-muted);
  cursor: pointer;
  flex-shrink: 0;
}
.analysis-popup-close:hover {
  background: #9a2b2b;
  border-color: #9a2b2b;
  color: #fff;
}
.analysis-popup-close svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.analysis-popup.is-minimized .analysis-popup-body { display: none; }
.analysis-popup.is-minimized .analysis-popup-head {
  border-bottom: 0;
}
.analysis-popup-body {
  padding: 8px 10px;
  overflow-y: auto;
  flex: 1;
}
.analysis-popup-summary {
  font-size: 11px;
  color: var(--popup-muted);
  margin: 0 0 8px;
}
.analysis-popup-body .pv-line-list {
  display: grid;
  gap: 6px;
}
.analysis-popup-body .pv-line {
  padding: 6px 8px;
  background: var(--popup-card-bg);
  border: 1px solid var(--popup-card-border);
  border-radius: 6px;
}
.analysis-popup-body .pv-line-head {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 11px;
  margin-bottom: 3px;
}
.analysis-popup-body .pv-line-index {
  font-weight: 700;
  color: var(--popup-muted);
}
.analysis-popup-body .pv-line-depth {
  color: var(--popup-muted);
}
.analysis-popup-body .pv-line-score {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--popup-accent);
}
.analysis-popup-body .pv-line-text {
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
  color: var(--popup-ink);
  word-spacing: 0.04em;
}
.analysis-popup-body .pv-line.is-empty .pv-line-text { opacity: 0.5; }
@media (max-width: 860px) {
  .analysis-popup {
    width: calc(100vw - 24px);
    max-height: 40vh;
    top: 12px;
    left: 12px;
  }
}

/* ---------- Table-of-contents module (shared by index pages) ------------- */
.module {
  margin-top: 36px;
}
.module-head {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--hairline);
  margin-bottom: 6px;
}
.module-name {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(20px, 1vw + 16px, 26px);
  color: var(--ink);
}
.module-topic {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(14px, 0.5vw + 12px, 16px);
  color: var(--accent);
}
.module-status {
  margin-left: auto;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  counter-reset: toc;
}
.toc-list li {
  position: relative;
  padding: 14px 0;
  border-bottom: 1px solid var(--hairline);
}
.toc-list li:last-child { border-bottom: none; }
.toc-list a.toc-link {
  text-decoration: none;
  color: inherit;
  display: block;
}
.toc-list a.toc-link:hover .toc-title { color: var(--accent); }
.toc-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
}
.toc-num {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(15px, 0.4vw + 14px, 17px);
  color: var(--accent);
  min-width: 30px;
  flex-shrink: 0;
}
.toc-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(16px, 0.6vw + 14px, 19px);
  color: var(--ink);
}
.toc-desc {
  margin: 4px 0 0 42px;
  font-size: clamp(14px, 0.4vw + 13px, 15px);
  color: var(--ink-soft);
  line-height: 1.5;
}
.module-soon {
  list-style: none;
  margin: 0;
  padding: 0;
}
.module-soon li {
  padding: 12px 0;
  border-bottom: 1px solid var(--hairline);
  color: var(--ink-soft);
  font-style: italic;
}
.module-soon li:last-child { border-bottom: none; }

@media print {
  .toc-list li, .module-soon li, .module-head { break-inside: avoid; }
}
@media (max-width: 520px) {
  .module-status { margin-left: 0; width: 100%; }
  .toc-desc { margin-left: 0; }
}

/* ---------- Hero --------------------------------------------------------- */
.hero {
  margin: 8px 0 28px;
  padding-bottom: 22px;
  border-bottom: 2px solid var(--ink);
}
.eyebrow {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: .85rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 clamp(8px, 0.5vw + 6px, 12px);
}
.chapter-title {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2.35rem, 7vw, 5.7rem);
  line-height: 1.08;
  letter-spacing: -0.055em;
  margin: 0 0 12px;
  color: var(--ink);
}
.chapter-subtitle {
  font-size: clamp(1.05rem, 1.9vw, 1.34rem);
  color: var(--ink-soft);
  margin: 0;
  max-width: 62ch;
}

/* ---------- Learning goals ----------------------------------------------- */
.goals {
  list-style: none;
  margin: 0 0 32px;
  padding: 16px 20px 16px 20px;
  background: var(--goal-bg);
  border: 1px solid var(--goal-border);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
}
.goals li {
  position: relative;
  padding-left: 24px;
  margin: 8px 0;
  color: var(--ink-soft);
  line-height: 1.6;
}
.goals li::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 10px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
}

/* ---------- Lessons ------------------------------------------------------ */
.lesson {
  margin: 0 0 36px;
  break-inside: avoid-page;
}
.lesson + .lesson { break-before: page; }

.lesson-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 0 0 6px;
}
.lesson-number {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(13px, 0.5vw + 12px, 16px);
  color: #fff;
  background: var(--accent);
  padding: 4px 11px;
  border-radius: 4px;
  letter-spacing: 0.04em;
}
.lesson-title {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.65rem, 4vw, 3.15rem);
  line-height: 1.08;
  letter-spacing: -0.035em;
  margin: 0;
  color: var(--ink);
}
.lesson-intro {
  margin: 6px 0 clamp(16px, 0.6vw + 14px, 20px);
  color: var(--ink-soft);
  font-size: clamp(1.05rem, 1.9vw, 1.34rem);
  line-height: 1.6;
  max-width: 64ch;
}

/* ---------- Position / diagram card -------------------------------------- */
.position {
  margin: 0 0 22px;
  break-inside: avoid;
  page-break-inside: avoid;
}
.position-label {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: .95rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 clamp(4px, 0.3vw + 3px, 8px);
}
.position-body {
  display: grid;
  grid-template-columns: var(--board-max) 1fr;
  gap: clamp(16px, 1.5vw + 12px, 22px);
  align-items: start;
}
.diagram {
  break-inside: avoid;
  page-break-inside: avoid;
}
.caption {
  margin: clamp(6px, 0.4vw + 5px, 10px) 0 0;
  font-size: .92rem;
  color: var(--ink-faint);
  font-style: italic;
}
.position-note {
  color: var(--ink);
  line-height: 1.65;
  max-width: 58ch;
}
.position-note p { margin: 0 0 12px; }
.position-note p:last-child { margin-bottom: 0; }

/* ---------- Chessboard (mirrors app markup: .board-grid > .board-square) - */
.board {
  position: relative;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  max-width: var(--board-max);
  border: 1px solid var(--panel-border);
  box-shadow:
    0 22px 60px rgba(0, 0, 0, 0.24),
    0 0 0 1px color-mix(in srgb, var(--accent) 14%, transparent);
  background:
    radial-gradient(circle at 20% 16%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 34%),
    linear-gradient(145deg, color-mix(in srgb, var(--panel-bg) 88%, var(--page-bg)), var(--page-bg));
  overflow: hidden;
}
/* Screen: interactive iframe (reuses the real app board). Print: static grid. */
.board-screen {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.board-iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background:
    radial-gradient(circle at 18% 14%, color-mix(in srgb, var(--accent) 14%, transparent), transparent 34%),
    linear-gradient(145deg, color-mix(in srgb, var(--panel-bg) 92%, var(--page-bg)), var(--page-bg));
}
.board-static {
  display: none;
}
.board-error {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--danger);
  font-size: 12px;
  padding: 8px;
  text-align: center;
}
.board .board-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  user-select: none;
}
.board-square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  min-width: 0;
  min-height: 0;
}
.board-square.light { background: var(--board-light); }
.board-square.dark  { background: var(--board-dark); }

/* Marked key squares (static/print board) — translucent highlight + dot */
.board-square.is-marked {
  background: var(--accent-soft);
}
.board-square.is-marked.light {
  background: color-mix(in srgb, var(--accent) 30%, transparent);
}
.board-square.is-marked.dark {
  background: color-mix(in srgb, var(--accent) 42%, transparent);
}
.board-mark {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 50%;
  width: 26%;
  height: 26%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  background: color-mix(in srgb, var(--accent) 55%, transparent);
  pointer-events: none;
}
.board-piece-shell {
  position: relative;
  z-index: 3;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.board-piece {
  width: 94%;
  height: 94%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 1px 1px var(--piece-shadow));
}
.coord-file, .coord-rank {
  position: absolute;
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
  z-index: 4;
}
.coord-file  { right: 3px; bottom: 2px; }
.coord-rank  { left: 3px;  top: 2px; }
.coord-light { color: #6f6a52; }
.coord-dark  { color: #f4eed7; }

/* ---------- Move line ---------------------------------------------------- */
.moveline {
  font-family: var(--font-mono);
  font-size: clamp(15px, 0.6vw + 14px, 19px);
  font-weight: 600;
  line-height: 1.8;
  color: var(--ink);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 14px 18px;
  margin: 0 0 20px;
  word-spacing: 0.04em;
  break-inside: avoid;
}
.moveline .move { color: var(--ink); }
.moveline .num  { color: var(--ink-faint); margin-right: 2px; }
.moveline .good { color: var(--success); font-weight: 700; }
.moveline .bad  { color: var(--danger); font-weight: 700; }
.moveline .result { color: var(--ink-faint); font-weight: 700; margin-left: 6px; }

/* ---------- Callout boxes ------------------------------------------------ */
.rule-box, .recap {
  break-inside: avoid;
  page-break-inside: avoid;
  border-radius: 6px;
  padding: 16px 20px;
  margin: 0 0 20px;
}
.rule-box {
  background: var(--rule-bg);
  border: 1px solid var(--rule-border);
  border-left: 4px solid var(--rule-ink);
}
.rule-box h3, .recap h3 {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.18rem, 2.2vw, 1.55rem);
  line-height: 1.08;
  letter-spacing: -0.015em;
  margin: 0 0 clamp(4px, 0.3vw + 3px, 8px);
}
.rule-box h3 { color: var(--rule-ink); }
.rule-box p, .recap li, .recap p {
  margin: 6px 0;
  line-height: 1.6;
}
.recap {
  background: var(--recap-bg);
  border: 1px solid var(--recap-border);
  border-left: 4px solid var(--recap-ink);
}
.recap h3 { color: var(--recap-ink); }
.recap ul { margin: 4px 0 0; padding-left: 20px; }

/* Inline accent / soft notes inside callouts and positions */
.note-accent {
  margin-top: clamp(6px, 0.4vw + 5px, 10px);
  font-size: .95rem;
  color: var(--accent);
  line-height: 1.55;
}
.note-soft {
  font-size: .95rem;
  color: var(--ink-soft);
  line-height: 1.55;
}
.position-note .note-soft { margin-top: 4px; }

/* ---------- Final summary checklist -------------------------------------- */
.checklist {
  break-inside: avoid-page;
  margin-top: 40px;
  padding-top: 24px;
  border-top: 2px solid var(--ink);
}
.checklist h2 {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(1.65rem, 4vw, 3.15rem);
  line-height: 1.08;
  letter-spacing: -0.035em;
  margin: 0 0 clamp(8px, 0.6vw + 6px, 14px);
}
.checklist ul { list-style: none; padding: 0; margin: 0; }
.checklist li {
  position: relative;
  padding: clamp(8px, 0.4vw + 6px, 10px) 8px clamp(8px, 0.4vw + 6px, 10px) 34px;
  margin: 5px 0;
  line-height: 1.55;
  border-bottom: 1px dashed var(--hairline);
  cursor: pointer;
  user-select: none;
}
.checklist li:hover {
  background: var(--panel-bg);
}
.checklist li::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 13px;
  width: 16px;
  height: 16px;
  border: 2px solid var(--accent);
  border-radius: 3px;
  background: var(--check-fill);
}
.checklist li.is-checked {
  color: var(--ink-faint);
  text-decoration: line-through;
  text-decoration-color: var(--ink-faint);
  text-decoration-thickness: 1px;
}
.checklist li.is-checked::before {
  background: var(--accent);
  border-color: var(--accent);
}
.checklist li.is-checked::after {
  content: "";
  position: absolute;
  left: 11px;
  top: 15px;
  width: 6px;
  height: 10px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
@media print {
  .checklist li { cursor: default; }
  .checklist li:hover { background: transparent; }
}

/* ---------- Test cards (Basic Test pages) ------------------------------- */
.test-section {
  margin-top: 12px;
}
.test-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(480px, 100%), 1fr));
  gap: clamp(16px, 1.5vw + 12px, 24px);
}
.test-card {
  break-inside: avoid;
  page-break-inside: avoid;
  border: 1px solid var(--hairline);
  border-radius: 8px;
  padding: 16px;
  background: var(--panel-bg);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.test-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
}
.test-number {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(13px, 0.3vw + 12px, 15px);
  color: #fff;
  background: var(--accent);
  padding: 4px 10px;
  border-radius: 4px;
  letter-spacing: 0.03em;
  flex-shrink: 0;
}
.side-to-move {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: clamp(11px, 0.2vw + 10px, 12px);
  padding: 3px 9px;
  border-radius: 4px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  flex-shrink: 0;
}
.side-to-move.white {
  background: var(--panel-bg);
  color: var(--ink);
  border: 1px solid var(--panel-border);
}
.side-to-move.black {
  background: var(--ink);
  color: var(--page-bg);
  border: 1px solid var(--ink-soft);
}
[data-theme="dark"] .side-to-move.white {
  background: var(--ink);
  color: var(--page-bg);
}
.test-question {
  font-size: .95rem;
  color: var(--ink);
  line-height: 1.5;
  padding: 10px 14px;
  background: var(--goal-bg);
  border: 1px solid var(--goal-border);
  border-left: 4px solid var(--accent);
  border-radius: 5px;
  margin: 0;
}
.test-diagram {
  align-self: center;
}
.test-diagram .board {
  width: min(480px, calc(100vw - 48px));
  height: min(480px, calc(100vw - 48px));
}
.test-fen {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  word-break: break-all;
  line-height: 1.4;
  margin: 0;
}
.fen-missing {
  display: flex;
  align-items: center;
  justify-content: center;
  width: min(280px, calc(100vw - 48px));
  height: min(280px, calc(100vw - 48px));
  border: 2px dashed var(--danger);
  border-radius: 4px;
  background: repeating-linear-gradient(
    45deg,
    transparent,
    transparent 8px,
    color-mix(in srgb, var(--danger) 8%, transparent) 8px,
    color-mix(in srgb, var(--danger) 8%, transparent) 16px
  );
  color: var(--danger);
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  padding: 20px;
  line-height: 1.5;
}

/* ---------- Lesson navigation ------------------------------------------- */
.lesson-nav-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 36px;
}
.lesson-nav-row a {
  border: 1px solid var(--hairline);
  color: var(--ink);
  background: var(--panel-bg);
  border-radius: 999px;
  padding: .72rem 1rem;
  font-size: 13px;
  font-weight: 800;
  text-decoration: none;
  box-shadow: 0 10px 26px rgba(0,0,0,.08);
}
.lesson-nav-row a:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

/* ---------- Footer ------------------------------------------------------- */
.lesson-footer {
  margin-top: 48px;
  padding-top: 16px;
  border-top: 1px solid var(--hairline);
  font-size: 13px;
  color: var(--ink-faint);
  display: flex;
  justify-content: space-between;
}
.lesson-footer .chapter-name { font-style: italic; }

/* ---------- Compact numbered endgame boards on desktop -------------------
   Keep the iframe's original layout viewport, then scale its complete visual
   output so board chrome, pieces, coordinates, trays, controls, and eval rail
   all shrink together. Tablet/mobile and print retain their existing rules. */
@media screen and (min-width: 861px) {
  html[data-lesson-series="endgame"] {
    --board-max: clamp(245px, calc(100vw * 0.35 + 52.5px), 560px);
  }

  html[data-lesson-series="endgame"] .board-iframe {
    width: 114.285714%;
    height: 114.285714%;
    transform: scale(0.875);
    transform-origin: 0 0;
  }

  html[data-lesson-series="endgame"] .test-diagram .board {
    width: min(420px, calc(100vw - 48px));
    height: min(420px, calc(100vw - 48px));
  }
}

/* ---------- Layout & spacing overrides for narrow screens ----------- */
@media (max-width: 860px) {
  .toolbar {
    margin: -24px -16px 16px;
    padding: 8px 12px;
  }

  .hero {
    margin: 4px 0 20px;
    padding-bottom: 16px;
  }

  .position-body {
    grid-template-columns: 1fr;
    gap: 16px;
    justify-items: center;
  }
  .position-note {
    max-width: none !important;
    width: 100%;
  }
  .diagram {
    width: min(100%, 760px);
    max-width: 100%;
  }
  .diagram .board {
    max-width: none;
    aspect-ratio: 1 / 1.18;
  }

  .test-grid {
    grid-template-columns: 1fr;
  }
  .test-card { padding: 14px; }
  .test-diagram .board,
  .fen-missing {
    width: min(340px, calc(100vw - 72px));
    height: min(340px, calc(100vw - 72px));
  }

  .index-header-inner {
    padding-left: 16px;
    padding-right: 16px;
  }

  .lesson-footer {
    flex-direction: column;
    gap: 4px;
  }

  .lesson-nav-row {
    flex-direction: column;
  }
  .lesson-nav-row a {
    text-align: center;
  }
}

@media (max-width: 560px) {
  .index-header-inner {
    flex-wrap: wrap;
    gap: .4rem;
    padding-top: .45rem;
    padding-bottom: .45rem;
  }
  .index-top-actions .toolbar-link {
    padding: .35rem .7rem;
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .toolbar button {
    font-size: 13px;
    padding: 5px 10px;
    width: auto;
    text-align: center;
  }
}

@media (max-width: 600px) {
  /* Keep a little side padding for text/paragraphs (readability). */
  .lesson-page {
    padding-left: 16px;
    padding-right: 16px;
  }
  .position-body {
    padding-left: 0;
    padding-right: 0;
    gap: 12px;
  }
  /* Only the chessboard breaks out to full-bleed (edge to edge). */
  .diagram {
    max-width: none;
    width: calc(100% + 32px);
    margin-left: -16px;
    margin-right: -16px;
  }
  .diagram .board {
    max-width: none;
    width: 100%;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }
  /* Re-indent the caption so it stays aligned with the body text. */
  .diagram .caption {
    padding-left: 16px;
    padding-right: 16px;
  }
}

/* ==========================================================================
   Print / PDF
   ========================================================================== */
@page {
  size: A4;
  margin: 18mm 16mm 20mm 16mm;
  @bottom-right {
    content: "Chapter 1 · p. " counter(page);
    font-family: "Inter", sans-serif;
    font-size: 9pt;
    color: #6b7280;
  }
  @bottom-left {
    content: "King + Pawn vs King: The Rule of the Square";
    font-family: "Inter", sans-serif;
    font-size: 9pt;
    color: #6b7280;
  }
}

/* ---------- Image lightbox (opt-in via .lesson-zoomable) ------------------- */
.lesson-zoomable {
  cursor: zoom-in;
  transition: transform .18s ease, box-shadow .18s ease;
}
.lesson-zoomable:hover {
  transform: scale(1.04);
}
.lesson-zoomable:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
  border-radius: 4px;
}

.lesson-lightbox {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .72);
  backdrop-filter: blur(4px);
  padding: 16px;
}
.lesson-lightbox[hidden] { display: none; }

.lesson-lightbox__image {
  max-width: 92vw;
  max-height: 88vh;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  border-radius: 12px;
  box-shadow: 0 24px 80px rgba(0,0,0,.5);
}

.lesson-lightbox__close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 999px;
  background: rgba(255,255,255,.15);
  color: #fff;
  font-size: 1.8rem;
  cursor: pointer;
  line-height: 1;
  transition: background .15s ease;
}
.lesson-lightbox__close:hover { background: rgba(255,255,255,.3); }

@media print {
  .lesson-lightbox { display: none !important; }
}

@media print {
  :root,
  [data-theme="dark"] {
    color-scheme: light;
    --page-bg: #ffffff;
    --board-max: 320px;
    --ink: #172127;
    --ink-soft: #52606a;
    --ink-faint: #7b8790;
    --rule-ink: #155e67;
    --rule-bg: #f0f7f5;
    --rule-border: #d0e2df;
    --recap-ink: #257a5f;
    --recap-bg: #f0f8f4;
    --recap-border: #c5ddd0;
    --goal-bg: #f6faf8;
    --goal-border: #d5e3df;
    --accent: #1d6f78;
    --accent-strong: #155e67;
    --hairline: #d5e0de;
    --panel-bg: #f8faf9;
    --panel-border: #d5e0de;
    --check-fill: #ffffff;
    --success: #257a5f;
    --danger: #c05243;
    --board-light: #eee3cb;
    --board-dark: #71925a;
  }

  html, body {
    background: #ffffff !important;
    color: #172127 !important;
    font-size: 12pt;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .board-square.light { background: #eee3cb !important; }
  .board-square.dark  { background: #71925a !important; }

  .lesson-page {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .toolbar, .no-print, .analysis-popup { display: none !important; }

  .hero {
    break-after: avoid-page;
    border-bottom: 2px solid #172127;
    padding-bottom: 14px;
  }
  .chapter-title { font-size: 32pt; }
  .chapter-subtitle { font-size: 12pt; }

  .goals { break-inside: avoid; background: #f6faf8 !important; border-color: #d5e3df !important; }

  .lesson { break-inside: avoid-page; }
  .lesson + .lesson { break-before: page; }

  .lesson-title { font-size: 22pt; color: #172127 !important; }
  .lesson-intro { font-size: 12pt; color: #52606a !important; }
  .lesson-number { background: #1d6f78 !important; color: #fff !important; }

  .position { break-inside: avoid; page-break-inside: avoid; }
  .position-body { grid-template-columns: var(--board-max) 1fr !important; gap: 18px; }
  .position-note { color: #172127 !important; }

  .diagram, .board { break-inside: avoid; page-break-inside: avoid; }
  .board {
    border: 1px solid #2c2c2c;
    box-shadow: none;
  }
  .board-screen, .board-iframe { display: none !important; }
  .board-static { display: block !important; }
  .board { width: var(--board-max) !important; height: var(--board-max) !important; }
  .diagram .board { width: var(--board-max) !important; height: var(--board-max) !important; aspect-ratio: auto !important; }
  .board-piece { filter: drop-shadow(0 0.4px 0.4px rgba(0,0,0,0.35)); }
  .board-square.is-marked.light { background: color-mix(in srgb, #1d6f78 28%, transparent) !important; }
  .board-square.is-marked.dark  { background: color-mix(in srgb, #1d6f78 40%, transparent) !important; }
  .board-mark {
    border-color: #fff !important;
    background: color-mix(in srgb, #1d6f78 60%, transparent) !important;
  }

  .rule-box, .recap, .moveline {
    break-inside: avoid;
    page-break-inside: avoid;
    box-shadow: none;
  }
  .rule-box {
    background: #f0f7f5 !important;
    border-color: #d0e2df !important;
    border-left-color: #155e67 !important;
  }
  .rule-box h3 { color: #155e67 !important; }
  .rule-box p { color: #172127 !important; }
  .recap {
    background: #f0f8f4 !important;
    border-color: #c5ddd0 !important;
    border-left-color: #257a5f !important;
  }
  .recap h3 { color: #257a5f !important; }
  .recap li, .recap p { color: #172127 !important; }
  .moveline { background: #f8faf9 !important; border-color: #d5e0de !important; color: #172127 !important; font-size: 11pt; }
  .moveline .num, .moveline .result { color: #7b8790 !important; }
  .moveline .good { color: #257a5f !important; }
  .moveline .bad  { color: #c05243 !important; }

  .note-accent { color: #155e67 !important; font-size: 10.5pt !important; }
  .note-soft   { color: #52606a !important; font-size: 10.5pt !important; }

  .checklist { break-inside: avoid-page; }
  .checklist h2 { color: #172127 !important; }
  .checklist li { color: #172127 !important; border-bottom-color: #d5e0de !important; text-decoration: none !important; }
  .checklist li::before { border-color: #1d6f78 !important; background: #fff !important; }
  .checklist li.is-checked { color: #172127 !important; }
  .checklist li.is-checked::before { background: #1d6f78 !important; border-color: #1d6f78 !important; }
  .checklist li.is-checked::after { border-color: #fff !important; }

  .test-card {
    break-inside: avoid;
    page-break-inside: avoid;
    background: #f8faf9 !important;
    border-color: #d5e0de !important;
  }
  .test-number { background: #1d6f78 !important; color: #fff !important; }
  .test-question {
    background: #f6faf8 !important;
    border-color: #d5e3df !important;
    border-left-color: #1d6f78 !important;
    color: #172127 !important;
  }
  .test-fen { color: #7b8790 !important; }
  .fen-missing {
    border-color: #c05243 !important;
    color: #c05243 !important;
  }
  .test-diagram .board {
    width: 280px !important;
    height: 280px !important;
  }

  .lesson-footer { display: none; }

  a { color: inherit; text-decoration: none; }

  p { orphans: 3; widows: 3; }
  h1, h2, h3 { break-after: avoid; page-break-after: avoid; }
}
