/* ============================================
   TOKENS
   ============================================ */
:root {
  --paper: #FAF7F0;
  --paper-shadow: #F1ECDF;
  --ink: #1C1B19;
  --ink-soft: #4A463F;
  --ink-faint: #8A8478;
  --rule: #D8D2C4;
  --rule-strong: #C7BFAC;
  --margin-red: #B5492F;
  --margin-red-soft: rgba(181, 73, 47, 0.35);
  --accent: #8B5A3C;
  --accent-soft: #E4D4C4;
  --confirm: #3C5246;
  --confirm-soft: #DCE5DE;
  --live: #C9402A;

  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Inter', -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  --rule-height: 36px;
}

* { box-sizing: border-box; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper-shadow);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
}

::selection {
  background: var(--accent-soft);
}

/* Visible keyboard focus throughout */
a:focus-visible,
button:focus-visible,
input:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--margin-red);
  outline-offset: 3px;
  border-radius: 2px;
}

/* ============================================
   APP SHELL
   ============================================ */
.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 100vh;
}

/* ============================================
   LEFT RAIL — TABLE OF CONTENTS
   ============================================ */
.toc {
  background: var(--ink);
  color: var(--paper);
  padding: 40px 28px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.toc-header {
  margin-bottom: 36px;
}

.toc-kicker {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--margin-red);
}

.toc-title {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 26px;
  line-height: 1.18;
  margin: 10px 0 0;
  color: var(--paper);
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
}

.toc-item {
  border-top: 1px solid rgba(250, 247, 240, 0.12);
}
.toc-item:last-child {
  border-bottom: 1px solid rgba(250, 247, 240, 0.12);
}

.toc-btn {
  all: unset;
  display: flex;
  align-items: baseline;
  gap: 12px;
  width: 100%;
  padding: 14px 4px;
  cursor: pointer;
  color: rgba(250, 247, 240, 0.62);
  transition: color 0.15s ease, padding-left 0.15s ease;
}

.toc-btn:hover {
  color: var(--paper);
  padding-left: 8px;
}

.toc-item.active .toc-btn {
  color: var(--paper);
  padding-left: 8px;
}

.toc-num {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-faint);
  width: 16px;
  flex-shrink: 0;
}

.toc-item.active .toc-num,
.toc-item.visited .toc-num {
  color: var(--margin-red);
}

.toc-label {
  font-family: var(--font-body);
  font-size: 14.5px;
  font-weight: 500;
  line-height: 1.3;
}

.toc-check {
  margin-left: auto;
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toc-item.visited .toc-check {
  opacity: 1;
  transform: scale(1);
}

.toc-footer {
  padding-top: 24px;
}

.progress-line {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faint);
  margin-bottom: 10px;
}

.progress-rule {
  height: 2px;
  background: rgba(250, 247, 240, 0.12);
  border-radius: 2px;
  overflow: hidden;
}

.progress-rule-fill {
  height: 100%;
  width: 0%;
  background: var(--margin-red);
  transition: width 0.4s ease;
}

/* ============================================
   RIGHT — NOTEBOOK PAGE
   ============================================ */
.page {
  position: relative;
  background:
    repeating-linear-gradient(
      to bottom,
      transparent,
      transparent calc(var(--rule-height) - 1px),
      var(--rule) calc(var(--rule-height) - 1px),
      var(--rule) var(--rule-height)
    );
  background-position: 0 168px;
  padding: 56px 72px 100px 96px;
}

.page::before {
  /* margin rule, the red vertical line */
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 64px;
  width: 1px;
  background: var(--margin-red-soft);
}

.page-eyebrow {
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 6px;
}

.page-title {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 40px;
  line-height: 1.1;
  margin: 0 0 18px;
  max-width: 720px;
}

.page-intro {
  font-size: 15.5px;
  line-height: 1.65;
  color: var(--ink-soft);
  max-width: 600px;
  margin: 0 0 36px;
}

/* Diagram + controls layout */
.workbench {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 40px;
  align-items: start;
}

.diagram-wrap {
  background: var(--paper);
  border: 1px solid var(--rule-strong);
  border-radius: 4px;
  padding: 28px;
  box-shadow: 0 1px 0 var(--rule-strong);
}

.diagram-wrap svg {
  width: 100%;
  height: auto;
  display: block;
  overflow: visible;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.control {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
}

.control-value {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--live);
  font-weight: 500;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 2px;
  background: var(--rule-strong);
  border-radius: 2px;
  outline-offset: 6px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--paper);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: grab;
  margin-top: -1px;
}
input[type="range"]:active::-webkit-slider-thumb {
  cursor: grabbing;
  background: var(--margin-red);
  box-shadow: 0 0 0 1px var(--margin-red);
}
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--paper);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: grab;
}

.control-btns {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip-btn {
  border: 1px solid var(--rule-strong);
  background: var(--paper);
  color: var(--ink-soft);
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 6px 12px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.12s ease;
}
.chip-btn:hover {
  border-color: var(--accent);
  color: var(--ink);
}
.chip-btn.active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.action-btn {
  border: 1px solid var(--accent);
  background: var(--paper);
  color: var(--accent);
  font-family: var(--font-body);
  font-weight: 500;
  font-size: 13.5px;
  padding: 10px 16px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.12s ease;
  text-align: left;
}
.action-btn:hover {
  background: var(--accent);
  color: var(--paper);
}

/* Margin notes — the "observations" column under the workbench */
.margin-notes {
  margin-top: 40px;
  max-width: 760px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.margin-note {
  position: relative;
  padding-left: 22px;
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--ink-soft);
}

.margin-note::before {
  content: "—";
  position: absolute;
  left: 0;
  color: var(--margin-red);
  font-weight: 600;
}

.margin-note strong {
  color: var(--ink);
  font-weight: 600;
}

.margin-note .live-val {
  font-family: var(--font-mono);
  color: var(--live);
  font-weight: 500;
}

/* Page nav (prev/next "turn the page") */
.page-turn {
  display: flex;
  justify-content: space-between;
  margin-top: 56px;
  padding-top: 20px;
  border-top: 1px solid var(--rule-strong);
}

.turn-btn {
  all: unset;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.04em;
  color: var(--ink-faint);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color 0.15s ease;
}
.turn-btn:hover {
  color: var(--margin-red);
}
.turn-btn.next {
  margin-left: auto;
  text-align: right;
}
.turn-btn[disabled] {
  visibility: hidden;
}

/* SVG element styling shared across diagrams */
.svg-ink { stroke: var(--ink); fill: none; stroke-width: 2; }
.svg-grid { stroke: var(--rule); stroke-width: 1; }
.svg-axis { stroke: var(--ink-faint); stroke-width: 1.25; }
.svg-accent { stroke: var(--accent); fill: none; stroke-width: 2.5; }
.svg-live { stroke: var(--live); fill: var(--live); }
.svg-confirm { stroke: var(--confirm); fill: var(--confirm); }
.svg-label { font-family: var(--font-mono); font-size: 12px; fill: var(--ink-soft); }
.svg-label-bold { font-family: var(--font-body); font-weight: 600; font-size: 13px; fill: var(--ink); }

/* Area legend (Pythagorean topic) */
.area-legend {
  display: flex;
  gap: 22px;
  justify-content: center;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink-soft);
  flex-wrap: wrap;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 7px;
}

.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  display: inline-block;
}

/* ============================================
   RESPONSIVE
   ============================================ */
@media (max-width: 980px) {
  .app {
    grid-template-columns: 1fr;
  }
  .toc {
    position: relative;
    height: auto;
    padding: 28px 24px;
  }
  .toc-list {
    overflow: visible;
  }
  .page {
    padding: 40px 24px 80px 48px;
  }
  .page::before {
    left: 16px;
  }
  .workbench {
    grid-template-columns: 1fr;
  }
  .page-title {
    font-size: 30px;
  }
}

@media (max-width: 560px) {
  .page-title { font-size: 26px; }
  .toc-title { font-size: 22px; }
}
