/* Design tokens from the course design system. Phone is the hard case and
   the default; the laptop layout is the media query. */

:root {
  --paper: #ebe3d2;
  --paper-2: #e3dac5;
  --ink: #232922;
  --ink-2: #3a4034;
  --muted: #6b6f62;
  --accent: #d8ae34;
  --accent-ink: #8a6d1f;
  --good: #7e9269;
  --viewport: #333b32;
  --dim: #9aa08f;

  --mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: Archivo, system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  /* iOS: stop the whole page rubber-banding when a drag starts on canvas. */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

::selection { background: var(--accent); color: var(--ink); }

/* ---------------------------------------------------------------- shell */

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
}

.hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 11px 14px;
  border-bottom: 1px solid var(--ink);
  flex: 0 0 auto;
}

/* Shrinkable, so a long lesson title truncates instead of pushing the header
   link off a 390px screen. "INVERSE KINEMATICS" overflowed by 13px. */
.hdr-brand { min-width: 0; }

.hdr-mark {
  font-weight: 900;
  font-size: 15px;
  letter-spacing: -0.4px;
  flex: 0 0 auto;
}

.hdr-sub {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hdr-link {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--dim);
  padding: 4px 7px;
  white-space: nowrap;
  flex: 0 0 auto;
  /* Pushes itself and the stats readout to the right, so the two-child
     space-between above keeps working with a third child. */
  margin-left: auto;
}

.hdr-link:hover,
.hdr-link:focus-visible { color: var(--ink); border-color: var(--ink); }

.hdr-stats {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.04em;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

.main {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

/* -------------------------------------------------------------- viewport */

.stage {
  position: relative;
  flex: 0 0 38dvh;
  min-height: 180px;
  background: var(--viewport);
  overflow: hidden;
}

.stage canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.stage-tag {
  position: absolute;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  pointer-events: none;
}

.stage-tag.tl { top: 8px; left: 10px; color: var(--accent); display: flex; align-items: center; gap: 6px; }
.stage-tag.tr { top: 8px; right: 10px; color: var(--dim); }
.stage-tag.bl { bottom: 8px; left: 10px; color: var(--dim); letter-spacing: 0.02em; }
/* Input trouble, shown only when there is trouble. See Shell._frame. */
.stage-tag.br { bottom: 8px; right: 10px; color: var(--accent); }
.stage-tag[hidden] { display: none; }

/* A control frozen for the duration of a run. Dimmed rather than hidden: the
   learner needs to see the gains the trial is running with. */
.ctrl.locked { opacity: 0.45; }
.ctrl.locked input[type=range] { cursor: not-allowed; }

.btn.run { background: var(--accent); border-color: var(--ink); }
.btn.run:disabled { opacity: 0.55; cursor: default; transform: none; }

/* Episode counter, beside the run button. */
.ep-chip {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--muted);
  align-self: center;
}
.ep-chip[hidden] { display: none; }

/* Shown when the browser reclaims the GL context. Opaque, not a scrim: what
   is underneath is a blank canvas, and letting it show through would read as
   part of the failure rather than a message about it. */
.stall {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 20px;
  text-align: center;
  background: var(--viewport);
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.5;
  letter-spacing: 0.04em;
  color: var(--dim);
}

.stall[hidden] { display: none; }
.stall-text { max-width: 30em; }

.dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: blink 1.4s infinite;
}

@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0.15; } }

@media (prefers-reduced-motion: reduce) {
  .dot { animation: none; }
}

/* ------------------------------------------------------------------ tabs */

.tabs {
  display: flex;
  border-bottom: 1px solid var(--ink);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  flex: 0 0 auto;
}

.tabs button {
  flex: 1;
  padding: 11px 0;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.tabs button[aria-selected='true'] {
  background: #ede6d6;
  border-bottom-color: var(--accent);
}

/* The rail must be a bounded flex column on phone too, not only on laptop.
   Without this the panel has no definite height, so its overflow-y never
   engages, it grows to full content height, and the status bar ends up drawn
   on top of the prose. */
.rail {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px 20px;
}

.panel[hidden] { display: none; }
/* The unselected tab. Phone-only by construction: the laptop rule below
   re-shows every panel with `display: block !important`, which outranks
   this whatever the tab state says. */
.panel.tab-off { display: none; }

/* ----------------------------------------------------------------- prose */

.kicker {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 8px;
}

.headline {
  font-weight: 900;
  font-size: 22px;
  line-height: 1.18;
  margin: 0 0 12px;
}

.body p {
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink-2);
  margin: 0 0 11px;
}

.body p:last-child { margin-bottom: 0; }

/* --------------------------------------------------------------- sliders */

.ctrl { margin-bottom: 17px; }

.ctrl-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  margin-bottom: 7px;
}

.ctrl-hint {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0;
  color: var(--muted);
}

.ctrl-val { color: var(--accent-ink); font-variant-numeric: tabular-nums; }

/* Lesson 3's error trace. Overshoot and ringing are shapes, and the 3D view
   has no time axis; this is the only place the lesson spends new vertical
   pixels, and with two sliders where lesson 1 had six the budget is there. */
.ctrl.chart { margin-bottom: 14px; }

.chart-canvas {
  display: block;
  width: 100%;
  border: 1px solid rgba(35, 41, 34, 0.16);
  border-radius: 2px;
  background: var(--paper-2);
}

input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 26px;          /* generous touch target; track drawn via background */
  background: transparent;
  margin: 0;
  cursor: pointer;
}

input[type='range']::-webkit-slider-runnable-track {
  height: 2px;
  background: #4b5346;
}

input[type='range']::-moz-range-track {
  height: 2px;
  background: #4b5346;
}

input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px; height: 18px;
  margin-top: -8px;
  background: var(--accent);
  border: 1px solid var(--ink);
  border-radius: 50%;
}

input[type='range']::-moz-range-thumb {
  width: 18px; height: 18px;
  background: var(--accent);
  border: 1px solid var(--ink);
  border-radius: 50%;
}

input[type='range']:focus-visible { outline: 2px solid var(--accent-ink); outline-offset: 4px; }

/* -------------------------------------------------------------- statusbar */

.statusbar { flex: 0 0 auto; }

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  /* Home-indicator clearance on iOS; 0 everywhere else. */
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--ink);
  background: var(--paper-2);
}

.status-text {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--ink-2);
  line-height: 1.4;
  min-width: 0;
}

.status-sub {
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0;
  color: var(--muted);
  margin-top: 2px;
  /* Hard cap. The status bar sits in the same vertical budget as the lesson
     text, and a hint that wraps to four lines eats the thing it explains. */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}

.status.is-pass { background: #2a3128; border-top-color: var(--accent); }
.status.is-pass .status-text { color: #ede6d6; }
/* Pass is a terminal state and earns the extra lines: it is where the lesson
   lands its point and names what comes next. Transient hints stay at two. */
.status.is-pass .status-sub {
  color: var(--dim);
  -webkit-line-clamp: 4;
  line-clamp: 4;
}

.hold {
  height: 3px;
  background: #d5cdb6;
  margin-top: 6px;
  max-width: 260px;
  overflow: hidden;
}

.hold i {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
}

.status.is-pass .hold { background: #46503f; }
.status.is-pass .hold i { background: var(--good); }

.btn {
  background: var(--accent);
  border: 1px solid var(--ink);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  padding: 10px 16px;
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
}

.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; }
.status.is-pass .btn.ghost { color: #ede6d6; border-color: var(--dim); }

.status-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  flex: 0 0 auto;
}

.btn.next { background: var(--good); border-color: var(--ink); }

.btn.next:disabled {
  background: transparent;
  color: var(--dim);
  border-color: var(--dim);
  cursor: default;
  font-weight: 400;
}

@media (min-width: 860px) {
  .status-actions { flex-direction: row; align-items: center; }
}

/* --------------------------------------------------------------- loading */

.load {
  position: fixed;
  inset: 0;
  background: var(--paper);
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 28px;
  z-index: 20;
}

.load-bar { height: 4px; background: #d9d1bb; margin-bottom: 16px; }
.load-bar i { display: block; height: 100%; width: 0%; background: var(--accent); transition: width .2s linear; }

.load-lines {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.75;
  color: var(--ink-2);
}

.load-err { color: #8a2f1f; }

/* ------------------------------------------------------------- playground */

.code-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 6px;
  -webkit-overflow-scrolling: touch;
}

.code-chips button {
  flex: none;
  font-family: var(--mono);
  font-size: 11px;
  padding: 6px 9px;
  border: 1px solid var(--muted);
  border-radius: 3px;
  background: var(--paper-2);
  color: var(--ink);
  white-space: nowrap;
  cursor: pointer;
}

.code-chips button:active { background: var(--accent); }

.code-ta {
  width: 100%;
  box-sizing: border-box;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  tab-size: 4;
  background: var(--paper-2);
  color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 2px;
  padding: 8px;
  resize: vertical;
}

.code-ta:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.code-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  align-items: stretch;
}

.code-row select {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--mono);
  font-size: 11px;
  padding: 0 6px;
  border: 1px solid var(--ink);
  border-radius: 2px;
  background: var(--paper);
  color: var(--ink);
}

/* print() and tracebacks read like a terminal; the dark panel says so. */
.code-console {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.45;
  background: var(--viewport);
  color: var(--paper);
  margin: 6px 0 0;
  padding: 8px;
  min-height: 3em;
  max-height: 9em;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  border-radius: 2px;
}

.ctrl.code.locked { opacity: 0.45; }

/* ---------------------------------------------------------- laptop layout */

@media (min-width: 860px) {
  .main {
    flex-direction: row;
    align-items: stretch;
  }

  .stage {
    flex: 1 1 auto;
    min-height: 0;
    border-right: 1px solid var(--ink);
  }

  .tabs { display: none; }

  .rail {
    flex: 0 0 380px;
    overflow-y: auto;
  }

  /* On a laptop everything is visible at once; the tabs were only ever a
     concession to 390 px of width. `:not([hidden])` because author
     !important also beats the UA's [hidden] rule, and a phase that excludes a
     control group hides it with the hidden attribute -- without this, phase
     swapping would do nothing above 860 px. */
  .panel:not([hidden]) {
    display: block !important;
    border-bottom: 1px solid var(--ink);
    flex: 0 0 auto;
  }

  .panel:last-child { border-bottom: none; }

  .headline { font-size: 19px; }

  .stage-tag.tl, .stage-tag.tr { top: 14px; font-size: 11px; }
  .stage-tag.tl { left: 16px; }
  .stage-tag.tr { right: 16px; }
  .stage-tag.bl { bottom: 16px; left: 16px; font-size: 11px; }
}
