:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #fbfaf7;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e0dcd3;
  --accent: #b5452a;
  --accent-soft: #f6e3dc;
  --good: #2f7d4f;
  --good-soft: #e1f1e7;
  --bad: #b3261e;
  --bad-soft: #fbe3e1;
  --fig-old: #4a4a52;
  --fig-aux: #a3a3ab;
  /* colours of new objects, in the same order as the PDF */
  --c0: #b5452a;
  --c1: #1f6fb2;
  --c2: #1b8a5a;
  --c3: #8e44ad;
  --c4: #c27c0e;
  --c5: #0f8b8d;
  --c6: #c2185b;
  --c7: #5b6b7a;
  --code-keyword: #8a3b8f;
  --code-comment: #8a8a90;
  --code-string: #2f7d4f;
  --code-number: #b5452a;
  --code-macro: #2b5fa8;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141416;
    --surface: #1c1c20;
    --surface-2: #19191c;
    --ink: #e9e7e2;
    --muted: #9a9aa2;
    --line: #2f2f36;
    --accent: #e7805f;
    --accent-soft: #3a2620;
    --good: #6cc28d;
    --good-soft: #1d3326;
    --bad: #ff8a80;
    --bad-soft: #3d1f1d;
    --fig-old: #c9c7c2;
    --fig-aux: #6d6d75;
    --c0: #e7805f;
    --c1: #6aa8e8;
    --c2: #5cc495;
    --c3: #c08ae0;
    --c4: #e8b04f;
    --c5: #4fc3c5;
    --c6: #ec6fa0;
    --c7: #9fb1c2;
    --code-keyword: #d49ad8;
    --code-comment: #7c7c84;
    --code-string: #8fd1a8;
    --code-number: #f0a084;
    --code-macro: #8db4f0;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
}

.noscript {
  max-width: 40rem;
  margin: 3rem auto;
  padding: 0 1rem;
}

/* Layout ------------------------------------------------------------------ */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.logo {
  width: 24px;
  height: 24px;
  flex: none;
}

.logo-circle {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.5;
}

.logo-line {
  stroke: var(--ink);
  stroke-width: 2.5;
  stroke-linecap: round;
}

.actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 44%) minmax(0, 1fr);
  gap: 1rem;
  padding: 1rem 1rem 0.5rem;
}

@media (max-width: 900px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }
}

.pane {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  min-width: 0;
}

.pane-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.pane-head h2,
.console-pane h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 650;
}

/* Controls ---------------------------------------------------------------- */

.control {
  font: inherit;
  font-size: 0.9rem;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
}

.control:hover:not(:disabled) {
  border-color: var(--muted);
}

.control:disabled {
  opacity: 0.4;
  cursor: default;
}

.control:focus-visible,
.console-input:focus-visible,
.slider:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.control.icon {
  padding: 0.25rem 0.55rem;
}

.badge {
  font-size: 0.82rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  overflow-wrap: anywhere;
}

.badge.good {
  color: var(--good);
  background: var(--good-soft);
}

.badge.bad {
  color: var(--bad);
  background: var(--bad-soft);
}

.muted {
  color: var(--muted);
}

/* Colours of new objects: each class sets --col for its element. */
.c0 { --col: var(--c0); }
.c1 { --col: var(--c1); }
.c2 { --col: var(--c2); }
.c3 { --col: var(--c3); }
.c4 { --col: var(--c4); }
.c5 { --col: var(--c5); }
.c6 { --col: var(--c6); }
.c7 { --col: var(--c7); }

/* Editor ------------------------------------------------------------------ */

.editor {
  flex: 1;
  min-height: 22rem;
  max-height: 72vh;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}

.editor .cm-editor {
  height: 100%;
  font-size: 13.5px;
  background: var(--surface);
  color: var(--ink);
}

.editor .cm-scroller {
  font-family: var(--mono);
  line-height: 1.55;
}

.editor .cm-gutters {
  background: var(--surface-2);
  color: var(--muted);
  border-right: 1px solid var(--line);
}

.editor .cm-activeLine,
.editor .cm-activeLineGutter {
  background: transparent;
}

.editor .cm-cursor {
  border-left-color: var(--ink);
}

.editor .cm-selectionBackground,
.editor .cm-focused .cm-selectionBackground {
  background: var(--accent-soft) !important;
}

.editor .cm-current-step {
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}

.editor .cm-error-line {
  background: var(--bad-soft);
  box-shadow: inset 3px 0 0 var(--bad);
}

/* Console ----------------------------------------------------------------- */

.console-pane {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 1rem 1rem;
}

.console {
  border: 1px solid var(--muted);
  border-radius: 8px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 13px;
}

.console-log {
  height: 10rem;
  overflow-y: auto;
  padding: 0.55rem 0.8rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.console-log .in {
  color: var(--muted);
}

.console-log .err {
  color: var(--bad);
}

.console-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border-top: 1px solid var(--line);
  padding: 0.45rem 0.8rem;
  background: var(--surface-2);
  border-radius: 0 0 8px 8px;
}

.prompt {
  color: var(--accent);
  font-weight: 650;
}

.console-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  color: var(--ink);
  background: transparent;
  border: 0;
  padding: 0.25rem 0;
}

/* Figure and playback --------------------------------------------------- */

.figure {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  aspect-ratio: 1 / 1;
  width: min(100%, 62vh);
  align-self: center;
  touch-action: none;
  user-select: none;
  cursor: grab;
  overflow: hidden;
}

.figure:active {
  cursor: grabbing;
}

.figure-svg {
  display: block;
  width: 100%;
  height: 100%;
}

.fig-curve {
  fill: none;
  stroke: var(--fig-old);
  stroke-width: 1.1px;
  vector-effect: non-scaling-stroke;
}

.fig-curve.new {
  stroke: var(--col, var(--accent));
  stroke-width: 2.2px;
}

.fig-curve.aux {
  stroke: var(--fig-aux);
  stroke-dasharray: 5 4;
}

.fig-point circle {
  fill: var(--fig-old);
}

.fig-point.given circle {
  fill: var(--ink);
}

.fig-point.new circle {
  fill: var(--col, var(--accent));
}

.fig-point.aux circle {
  fill: var(--fig-aux);
}

.fig-point text,
.fig-label {
  fill: var(--muted);
  font-family: var(--sans);
}

.fig-point.new text,
.fig-label.new {
  fill: var(--col, var(--accent));
  font-weight: 650;
}

.controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.slider {
  flex: 1;
  min-width: 8rem;
  accent-color: var(--accent);
}

.step-count {
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.zoom {
  display: flex;
  gap: 0.25rem;
}

.toggle {
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.2rem;
  cursor: pointer;
}

/* universal statements: the case shown and the variables */
.params {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
}

.params-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.case-pick {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.case-now {
  color: var(--good);
  flex: 1;
  min-width: 12rem;
}

.case-now.bad {
  color: var(--bad);
}

.param {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.param-name {
  font-family: var(--mono);
  min-width: 6.5rem;
  font-variant-numeric: tabular-nums;
}

.params-hint {
  margin: 0;
  font-size: 0.8rem;
}

.hyp {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--muted);
}

.generality ul {
  margin: 0.2rem 0 0;
  padding-left: 1.2rem;
}

.step {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 0.7rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-wrap: anywhere;
}

.step-head {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}

.step-head:not(:first-child) {
  margin-top: 0.5rem;
}

.step-head code {
  font-family: var(--mono);
  font-size: 0.92rem;
}

.step-kind {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  min-width: 5.5rem;
}

.note {
  margin: 0;
  white-space: pre-wrap;
}

.obj {
  font-family: var(--mono);
  font-size: 0.85rem;
}

.obj-name {
  color: var(--col, var(--accent));
  font-weight: 650;
}

.verdict {
  font-family: var(--mono);
  font-size: 0.85rem;
}

.verdict.good {
  color: var(--good);
}

.verdict.bad {
  color: var(--bad);
}

.substeps {
  margin: 0.2rem 0 0;
  padding-left: 1.4rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.substeps code {
  font-family: var(--mono);
}

.generality {
  margin: 0.4rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

/* On wide screens the page fits the window: the editor and the step panel
   scroll inside their panes, so the console stays visible at the bottom. */
@media (min-width: 901px) {
  .app {
    height: 100vh;
    min-height: 0;
  }

  .workspace {
    flex: 1;
    min-height: 0;
  }

  .pane {
    min-height: 0;
  }

  .editor {
    min-height: 0;
    max-height: none;
  }

  .figure {
    flex: none;
    width: min(100%, 42vh);
  }

  .step {
    flex: 1 1 auto;
    min-height: 3.5rem;
    overflow-y: auto;
  }

}

@media (min-width: 901px) and (max-height: 850px) {
  .figure {
    width: min(100%, 36vh);
  }
}

/* Help -------------------------------------------------------------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 10;
}

.modal {
  background: var(--surface);
  color: var(--ink);
  border-radius: 10px;
  width: min(100%, 54rem);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1.1rem;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.modal-body {
  overflow-y: auto;
  padding: 0.5rem 1.1rem 1.1rem;
}

.help-section h3 {
  font-size: 0.95rem;
  margin: 1rem 0 0.4rem;
}

.help-section p {
  margin: 0.3rem 0;
}

.help-section pre {
  font-family: var(--mono);
  font-size: 0.85rem;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  margin: 0.3rem 0;
}

.help-row {
  display: grid;
  grid-template-columns: minmax(10rem, 17rem) 1fr;
  gap: 0.8rem;
  padding: 0.2rem 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}

/* script snippets outside the editor, coloured as the editor colours them */
.tok-keyword {
  color: var(--code-keyword);
  font-weight: 600;
}

.tok-comment {
  color: var(--code-comment);
  font-style: italic;
}

.tok-string {
  color: var(--code-string);
}

.tok-number {
  color: var(--code-number);
}

.tok-macroName {
  color: var(--code-macro);
}

.tok-operator {
  color: var(--muted);
}

.help-row code {
  font-family: var(--mono);
  font-size: 0.85rem;
}

@media (max-width: 600px) {
  .help-row {
    grid-template-columns: 1fr;
    gap: 0.1rem;
  }
}
