:root {
  --bg: #ffffff;
  --bg-sidebar: #f6f5f3;
  --text: #1c1b1a;
  --text-muted: #5f5b56;
  --accent: #8a3b2c;
  --accent-soft: #f0e2dc;
  --border: #e4e0da;
  --code-bg: #f2efe9;
  --link: #8a3b2c;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  --sidebar-width: 280px;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  /* :not([data-theme="light"]) lets the manual toggle force light mode
     even when the OS is in dark mode. */
  :root:not([data-theme="light"]) {
    --bg: #17151a;
    --bg-sidebar: #1c1a20;
    --text: #e8e4de;
    --text-muted: #a39d95;
    --accent: #e0a58c;
    --accent-soft: #3a2a24;
    --border: #322e33;
    --code-bg: #211f25;
    --link: #e0a58c;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }
}

/* Manual override: forces dark mode even when the OS is in light mode. */
:root[data-theme="dark"] {
  --bg: #17151a;
  --bg-sidebar: #1c1a20;
  --text: #e8e4de;
  --text-muted: #a39d95;
  --accent: #e0a58c;
  --accent-soft: #3a2a24;
  --border: #322e33;
  --code-bg: #211f25;
  --link: #e0a58c;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.55;
}

.layout {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1.25rem 3rem;
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar .brand {
  display: block;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
  margin-bottom: 0.15rem;
}

.sidebar .brand-sub {
  display: block;
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.lang-switch {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 1.5rem;
}

.lang-switch-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-right: 0.1rem;
}

.lang-switch a {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.15rem 0.5rem;
}

.lang-switch a:hover {
  border-color: var(--accent);
  color: var(--text);
}

.lang-switch a.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}

.sidebar .part-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 1.4rem 0 0.4rem;
  font-weight: 600;
}

.sidebar .part-label:first-of-type { margin-top: 0; }

.sidebar ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.sidebar li a {
  display: block;
  padding: 0.32rem 0.5rem;
  margin: 0.05rem 0;
  border-radius: 6px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.88rem;
  line-height: 1.35;
}

.sidebar li a:hover {
  background: var(--accent-soft);
  color: var(--text);
}

.sidebar li a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-toggle {
  display: none;
}

.mobile-bar {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sidebar);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
}

.theme-toggle:hover { border-color: var(--accent); }

.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: inline; }

:root[data-theme="dark"] .theme-toggle .icon-sun { display: inline; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun { display: inline; }
  :root:not([data-theme="light"]) .theme-toggle .icon-moon { display: none; }
}

/* ---------- Sticky section header ---------- */

.sticky-header {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  z-index: 30;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 0.7rem 2rem;
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--text-muted);
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  white-space: nowrap;
  overflow: hidden;
}

.sticky-header.visible {
  opacity: 1;
  transform: translateY(0);
}

.sticky-header .chapter {
  color: var(--text);
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sticky-header .sep { opacity: 0.5; }

.sticky-header .section {
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 860px) {
  .sticky-header { left: 0; padding: 0.6rem 1.25rem; }
}

/* ---------- Main content ---------- */

.content-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
  padding: 3rem 2rem 5rem;
}

main {
  max-width: 720px;
  width: 100%;
}

main.home {
  max-width: 780px;
}

article h1 {
  font-family: var(--serif);
  font-size: 2rem;
  line-height: 1.25;
  margin: 0 0 1.6rem;
  color: var(--text);
}

article h2 {
  font-family: var(--serif);
  font-size: 1.4rem;
  margin: 2.4rem 0 1rem;
  padding-top: 0.4rem;
  border-top: 1px solid var(--border);
  color: var(--text);
}

article > h2:first-of-type {
  border-top: none;
  padding-top: 0;
}

article p, article li {
  font-family: var(--serif);
  font-size: 1.05rem;
  color: var(--text);
}

article p { margin: 1.1rem 0; }

article ul, article ol {
  padding-left: 1.4rem;
}

article li { margin: 0.4rem 0; }

article a { color: var(--link); }

article hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

article strong { color: var(--text); }

article code {
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.9em;
}

.viz-embed {
  margin: 1.6rem 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-sidebar);
}

.viz-embed video {
  display: block;
  width: 100%;
  height: auto;
  background: #000;
}

/* Contents block: first ordered list right after the "Contents" heading.
   Matched by class, not id — the heading's id is a markdown-generated
   slug that varies by language (only ever "contents" in English). */
article ol.contents-list {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1rem 1rem 2.2rem;
}

article ol.contents-list a {
  text-decoration: none;
  color: var(--text);
}

article ol.contents-list a:hover {
  color: var(--accent);
  text-decoration: underline;
}

/* ---------- Chapter nav (prev/next) ---------- */

.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--sans);
}

.chapter-nav a {
  flex: 1;
  text-decoration: none;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem 1rem;
  font-size: 0.85rem;
}

.chapter-nav a:hover { background: var(--accent-soft); }

.chapter-nav .dir {
  display: block;
  color: var(--text-muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.chapter-nav .next { text-align: right; }
.chapter-nav .spacer { visibility: hidden; }

/* ---------- Home page ---------- */

.hero {
  font-family: var(--serif);
}

.hero h1 {
  font-size: 2.3rem;
  margin: 0 0 1rem;
}

.hero p {
  font-size: 1.1rem;
  color: var(--text-muted);
  max-width: 62ch;
}

.toc-part {
  margin-top: 2.5rem;
}

.toc-part h2 {
  font-family: var(--sans);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  border: none;
  margin: 0 0 0.9rem;
  padding: 0;
}

.toc-grid {
  display: grid;
  gap: 0.6rem;
}

.toc-item {
  display: flex;
  gap: 0.9rem;
  align-items: baseline;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  text-decoration: none;
  color: var(--text);
  background: var(--bg-sidebar);
}

.toc-item:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.toc-item .num {
  font-family: var(--sans);
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 1.6rem;
}

.toc-item .title {
  font-family: var(--serif);
  font-size: 1.02rem;
}

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    z-index: 20;
    left: 0;
    top: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open { transform: translateX(0); }

  .content-wrap { padding: 1.25rem 1.25rem 4rem; }

  .mobile-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: sticky;
    top: 1rem;
    z-index: 10;
    margin-bottom: 1rem;
  }

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.5rem 0.8rem;
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
  }

  .scrim {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 15;
  }

  .scrim.open { display: block; }
}

@media (min-width: 861px) {
  .scrim { display: none; }
}

/* ---------- Context menu (report an inaccuracy) ---------- */

.ctx-menu {
  position: fixed;
  z-index: 100;
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  padding: 0.3rem;
  min-width: 200px;
}

.ctx-menu.open { display: block; }

.ctx-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  font-family: var(--sans);
  font-size: 0.85rem;
  color: var(--text);
  cursor: pointer;
}

.ctx-menu-item:hover { background: var(--accent-soft); }

/* ---------- Report modal ---------- */

.report-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  padding: 1rem;
}

.report-overlay.open { display: flex; }

.report-modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1.5rem;
  max-width: 480px;
  width: 100%;
  font-family: var(--sans);
}

.report-title {
  font-family: var(--serif);
  font-size: 1.25rem;
  margin: 0 0 1rem;
  color: var(--text);
}

.report-selection-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.report-selection {
  margin: 0 0 1rem;
  padding: 0.6rem 0.8rem;
  background: var(--code-bg);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  font-family: var(--serif);
  font-size: 0.92rem;
  color: var(--text);
  max-height: 8rem;
  overflow-y: auto;
}

.report-textarea {
  width: 100%;
  resize: vertical;
  min-height: 5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  font-family: var(--sans);
  font-size: 0.9rem;
  background: var(--bg);
  color: var(--text);
}

.report-textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.report-status {
  min-height: 1.2rem;
  font-size: 0.82rem;
  margin-top: 0.5rem;
  color: var(--text-muted);
}

.report-privacy-note {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 0.6rem;
  line-height: 1.4;
}

.report-status.error { color: #c0392b; }
.report-status.success { color: #2e7d32; }

.report-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1rem;
}

.report-actions button {
  font-family: var(--sans);
  font-size: 0.85rem;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  cursor: pointer;
}

.btn-secondary {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-secondary:hover { background: var(--accent-soft); }

.btn-primary {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: var(--bg);
}

.btn-primary:hover { opacity: 0.9; }

.btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ---------- Self-check quiz ---------- */

.quiz-section {
  margin-top: 3rem;
  padding: 1.75rem 2rem 2rem;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-sidebar);
  box-shadow: var(--shadow);
  font-family: var(--sans);
}

.quiz-section h2 {
  font-family: var(--serif);
  font-size: 1.4rem;
  margin: 0 0 0.6rem;
  padding-top: 0;
  border-top: none;
  color: var(--text);
}

.quiz-intro {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin: 0 0 1.5rem;
}

/* ---- Progress bar ---- */

.quiz-progress {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 1.5rem;
}

.quiz-progress-track {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.quiz-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.quiz-progress-label {
  flex-shrink: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ---- Questions (one shown at a time) ---- */

.quiz-q {
  display: none;
}

.quiz-q.quiz-q-active {
  display: block;
  animation: quiz-fade-in 0.2s ease;
}

@keyframes quiz-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.quiz-prompt {
  font-family: var(--serif);
  font-size: 1.1rem;
  line-height: 1.5;
  color: var(--text);
  margin: 0 0 1.1rem;
}

.quiz-options {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.quiz-option {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.85rem;
  background: var(--bg);
  cursor: pointer;
  font-size: 0.94rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.quiz-option:hover { border-color: var(--accent); }

.quiz-option:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.quiz-option input { flex-shrink: 0; margin-top: 0.2rem; accent-color: var(--accent); }

.quiz-option.quiz-correct-option {
  border-color: #2e7d32;
  background: rgba(46, 125, 50, 0.12);
}

.quiz-option.quiz-wrong-option {
  border-color: #c0392b;
  background: rgba(192, 57, 43, 0.12);
}

.quiz-numeric-input {
  width: 100%;
  max-width: 220px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  font-family: var(--sans);
  font-size: 0.95rem;
  background: var(--bg);
  color: var(--text);
}

.quiz-numeric-input:focus { outline: none; border-color: var(--accent); }

.quiz-q.quiz-correct .quiz-numeric-input { border-color: #2e7d32; }
.quiz-q.quiz-incorrect .quiz-numeric-input { border-color: #c0392b; }

.quiz-feedback {
  font-size: 0.85rem;
  font-weight: 600;
  margin-top: 0.7rem;
  min-height: 1.1rem;
}

.quiz-q.quiz-correct .quiz-feedback { color: #2e7d32; }
.quiz-q.quiz-incorrect .quiz-feedback { color: #c0392b; }

.quiz-explanation {
  display: none;
  margin-top: 0.7rem;
  padding: 0.7rem 0.9rem;
  border-radius: 10px;
  background: var(--code-bg);
  font-size: 0.88rem;
  color: var(--text-muted);
}

.quiz-q.quiz-correct .quiz-explanation,
.quiz-q.quiz-incorrect .quiz-explanation {
  display: block;
  animation: quiz-fade-in 0.2s ease;
}

.quiz-warning {
  color: #c0392b;
  font-size: 0.85rem;
  min-height: 1.1rem;
  margin: 0.8rem 0 0;
}

.quiz-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1.4rem;
}

.quiz-actions button {
  font-family: var(--sans);
  font-size: 0.85rem;
  border-radius: 8px;
  padding: 0.65rem 1.3rem;
  cursor: pointer;
}

.quiz-progress[hidden],
.quiz-actions[hidden],
.quiz-form[hidden] {
  display: none;
}

/* ---- Results screen ---- */

.quiz-result {
  text-align: center;
  padding: 1rem 0 0.5rem;
}

.quiz-result-score {
  font-family: var(--serif);
  font-size: 2.6rem;
  font-weight: 700;
  color: var(--accent);
}

.quiz-result-message {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin: 0.6rem 0 1.4rem;
}

.quiz-result .quiz-retry-btn {
  font-family: var(--sans);
  font-size: 0.85rem;
  border-radius: 8px;
  padding: 0.65rem 1.3rem;
  cursor: pointer;
}

@media (max-width: 600px) {
  .quiz-section { padding: 1.4rem 1.25rem 1.6rem; }
}
