/* The lab, second run: four pages that push the no-program rule harder.
   Same palette as the ledger, same promise: a stylesheet can say what a state
   is, never what happens next. Everything here is a transform, a gradient, a
   path string, or a playhead. Colours come from the ledger's variables, so all
   of it reads the same under parchment and under night watch. */

/* ---------- Curves that draw themselves ---------- */

/* The figure is a fixed 200 x 200 box on purpose: the path data is in the same
   units as the box, which is the only reason an HTML dot can ride the same
   curve the SVG stroke is drawing. Scaling the box would break the alignment. */

.curves {
  display: grid;
  grid-template-columns: repeat(auto-fit, 200px);
  gap: 1.4rem 1rem;
  justify-content: center;
}

.curve {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 0;
}

.curve svg {
  display: block;
  width: 200px;
  height: 200px;
}

.curve path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

/* The ghost is the curve that has already been drawn and stays put; the ink is
   the bright line going round it right now. Two paths, one shape. */

.curve .ghost {
  stroke: var(--rule);
  stroke-width: 0.7;
}

.curve .ink {
  stroke: var(--link);
  stroke-width: 1.4;
  filter: drop-shadow(0 0 3px rgba(217,164,65,0.45));
}

.rider {
  position: absolute;
  left: 0;
  top: 0;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 9px 3px rgba(217,164,65,0.45);
}

.curve figcaption {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 0.5rem;
  font-size: 0.74rem;
  color: var(--muted);
  line-height: 1.5;
}

.curve figcaption strong { color: var(--text-strong); display: block; }
.curve figcaption code { font-size: 0.7rem; background: none; padding: 0; }

/* ---------- The tunnel: perspective on a pile of squares ---------- */

.stage-3d {
  position: relative;
  height: 300px;
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 50%, var(--panel) 0%, var(--surface) 72%);
  border: 1px solid var(--rule);
  border-radius: 3px;
  perspective: 380px;
  perspective-origin: 50% 46%;
}

.corridor {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

/* Each hoop is a square cross-section of the corridor, sitting --z pixels
   further down the tunnel and turned a few degrees so the walls do not line
   up. Twelve of them, each a fixed slice of the same 6 second flight. */

.hoop {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 260px;
  height: 260px;
  margin: -130px 0 0 -130px;
  border: 1px solid var(--link);
  box-shadow: inset 0 0 0 1px var(--rule), 0 0 12px rgba(217,164,65,0.10);
  animation: hoop-fly 6s linear infinite;
  animation-delay: calc(var(--k) * -0.5s);
}

.hoop::before,
.hoop::after {
  content: "";
  position: absolute;
  inset: 18%;
  border: 1px dashed var(--rule);
}

.hoop::after { inset: 38%; border-style: solid; border-color: var(--gold-line); }

/* Every hoop carries its own --k, so the keyframes can turn each one by a
   different angle on its way past: the corridor twists as you fall into it. */

@keyframes hoop-fly {
  from { transform: translate3d(0, 0, 120px) rotate(calc(var(--k) * 6deg)); opacity: 0; }
  12%  { opacity: 1; }
  78%  { opacity: 1; }
  to   { transform: translate3d(0, 0, -900px) rotate(calc(var(--k) * 6deg + 50deg)); opacity: 0; }
}

/* The floor: a lattice in perspective, pulled towards the viewer forever. */

.floor {
  position: absolute;
  left: -60%;
  right: -60%;
  bottom: -40%;
  height: 120%;
  background-image:
    repeating-linear-gradient(to right, var(--rule) 0 1px, transparent 1px 44px),
    repeating-linear-gradient(to bottom, var(--rule) 0 1px, transparent 1px 44px);
  transform: rotateX(74deg);
  transform-origin: 50% 0%;
  animation: floor-run 2.4s linear infinite;
  opacity: 0.75;
}

@keyframes floor-run {
  from { background-position: 0 0, 0 0; }
  to   { background-position: 0 0, 0 44px; }
}

/* The iris: a conic fan behind a round window, turning at a rate that has
   nothing to do with how long a day is. */

.iris {
  position: absolute;
  inset: 12%;
  border-radius: 50%;
  background:
    repeating-conic-gradient(from 0deg,
      var(--link) 0deg 3deg, transparent 3deg 15deg);
  -webkit-mask-image: radial-gradient(circle, #000 0 44%, rgba(0,0,0,0.25) 62%, transparent 78%);
  mask-image: radial-gradient(circle, #000 0 44%, rgba(0,0,0,0.25) 62%, transparent 78%);
  animation: iris-turn 26s linear infinite;
}

.iris::after {
  content: "";
  position: absolute;
  inset: 26%;
  border-radius: 50%;
  border: 1px solid var(--gold-line);
  background: radial-gradient(circle, rgba(217,164,65,0.16), transparent 70%);
}

@keyframes iris-turn {
  to { transform: rotate(360deg); }
}

/* ---------- The fault: three copies of a thing, badly aligned ---------- */

.fault-stage {
  position: relative;
  height: 220px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 3px;
}

/* Chromatic split. Three copies of one heading, screened together, each one
   torn sideways on a different schedule. The tearing is the whole trick: no
   colour is out of place, only out of time. */

.split {
  position: relative;
  height: 100%;
  display: grid;
  place-items: center;
  font-size: clamp(1.6rem, 7vw, 2.6rem);
  letter-spacing: 0.06em;
  color: transparent;
  isolation: isolate;
}

.split span {
  grid-area: 1 / 1;
  mix-blend-mode: screen;
  white-space: nowrap;
}

.split .r { color: #e0564f; animation: tear 4.2s steps(1, end) infinite; }
.split .g { color: #5fc98a; animation: tear 3.7s steps(1, end) infinite reverse; }
.split .b { color: #5a8fe0; animation: tear 5.1s steps(1, end) infinite; }
.split .w { color: var(--cream); }

@keyframes tear {
  0%, 88%  { transform: translate(0, 0); clip-path: inset(0 0 0 0); }
  89%      { transform: translate(-7px, 1px); clip-path: inset(12% 0 63% 0); }
  90%      { transform: translate(6px, -2px); clip-path: inset(58% 0 22% 0); }
  91%      { transform: translate(-3px, 0); clip-path: inset(31% 0 41% 0); }
  92%      { transform: translate(0, 0); clip-path: inset(0 0 0 0); }
}

/* Scanlines, a sweep bar, and the faint mains hum of a screen that is on. */

.scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(to bottom, rgba(0,0,0,0) 0 2px, rgba(0,0,0,0.16) 2px 3px),
    linear-gradient(to bottom, transparent 0 44%, rgba(217,164,65,0.10) 48%, transparent 52% 100%);
  background-size: 100% 3px, 100% 100%;
  animation: roll 7s linear infinite;
}

@keyframes roll {
  from { background-position: 0 0, 0 -100%; }
  to   { background-position: 0 3px, 0 100%; }
}

/* Data mosh: 48 blocks, each with its own delay, each jittering sideways. */

.mosh {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-auto-rows: 14px;
  gap: 2px;
  padding: 0.8rem;
}

.mosh i {
  background: var(--rule);
  border-radius: 1px;
  animation: mosh 2.6s steps(1, end) infinite;
  animation-delay: calc(var(--j) * -0.11s);
}

.mosh i:nth-child(3n) { background: var(--link); opacity: 0.55; }
.mosh i:nth-child(7n) { background: var(--gold); opacity: 0.35; }

@keyframes mosh {
  0%, 74%  { transform: translateX(0); }
  76%      { transform: translateX(-7px); }
  78%      { transform: translateX(5px); }
  80%      { transform: translateX(-3px); }
  82%      { transform: translateX(0); }
}

/* ---------- Interference: two patterns arguing ---------- */

.moire {
  position: relative;
  height: 260px;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 3px;
}

.lattice {
  position: absolute;
  inset: -20%;
  background-image:
    repeating-linear-gradient(0deg, var(--rule) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(90deg, var(--rule) 0 1px, transparent 1px 7px);
  animation: lattice-drift 24s linear infinite;
}

.lattice--b {
  background-image:
    repeating-linear-gradient(0deg, var(--link) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(90deg, var(--link) 0 1px, transparent 1px 7px);
  animation-direction: reverse;
  animation-duration: 31s;
  mix-blend-mode: difference;
  opacity: 0.85;
}

@keyframes lattice-drift {
  from { transform: rotate(0deg) scale(1); }
  to   { transform: rotate(3deg) scale(1.06); }
}

/* Plasma: four radial gradients, screened together, each turning at its own
   rate. The result looks computed. It is four divs and a blend mode. */

.plasma {
  position: relative;
  height: 260px;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: var(--surface);
  isolation: isolate;
}

.plasma i {
  position: absolute;
  inset: -30%;
  mix-blend-mode: screen;
  opacity: 0.75;
}

.plasma i:nth-child(1) {
  background: radial-gradient(circle at 30% 30%, #d9a441 0%, transparent 42%);
  animation: plasma-turn 17s linear infinite;
}

.plasma i:nth-child(2) {
  background: radial-gradient(circle at 70% 60%, #4b6fa8 0%, transparent 45%);
  animation: plasma-turn 23s linear infinite reverse;
}

.plasma i:nth-child(3) {
  background: radial-gradient(circle at 55% 25%, #a8443c 0%, transparent 38%);
  animation: plasma-turn 31s linear infinite;
}

.plasma i:nth-child(4) {
  background: conic-gradient(from 0deg, #2e8b6a, #d9a441, #4b6fa8, #2e8b6a);
  -webkit-mask-image: radial-gradient(circle at 50% 50%, #000 0 30%, transparent 72%);
  mask-image: radial-gradient(circle at 50% 50%, #000 0 30%, transparent 72%);
  animation: plasma-turn 44s linear infinite reverse;
  opacity: 0.5;
}

@keyframes plasma-turn {
  to { transform: rotate(360deg); }
}

/* Kaleidoscope: one fan of wedges, folded against itself with a difference
   blend. Twelve wedges, one conic gradient, and an illusion. */

.kaleido {
  position: relative;
  height: 260px;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 3px;
  display: grid;
  place-items: center;
}

.kaleido i {
  position: absolute;
  width: 240px;
  height: 240px;
  border-radius: 50%;
  background: repeating-conic-gradient(from 0deg,
    var(--link) 0deg 8deg, transparent 8deg 30deg,
    var(--gold) 30deg 36deg, transparent 36deg 60deg);
  -webkit-mask-image: radial-gradient(circle, #000 0 46%, transparent 74%);
  mask-image: radial-gradient(circle, #000 0 46%, transparent 74%);
  animation: iris-turn 34s linear infinite;
}

.kaleido i:nth-child(2) {
  animation-direction: reverse;
  animation-duration: 19s;
  mix-blend-mode: difference;
  opacity: 0.8;
  background: repeating-conic-gradient(from 4deg,
    var(--gold) 0deg 5deg, transparent 5deg 27deg);
}

.kaleido i:nth-child(3) {
  width: 90px;
  height: 90px;
  border: 1px dashed var(--rule);
  background: none;
  animation-duration: 11s;
}

/* ---------- Stillness and narrow screens ---------- */

html:has(#lab-still:checked) .lab-anim,
html:has(#lab-still:checked) .rider,
html:has(#lab-still:checked) .hoop,
html:has(#lab-still:checked) .floor,
html:has(#lab-still:checked) .iris,
html:has(#lab-still:checked) .split span,
html:has(#lab-still:checked) .scanlines,
html:has(#lab-still:checked) .mosh i,
html:has(#lab-still:checked) .lattice,
html:has(#lab-still:checked) .plasma i,
html:has(#lab-still:checked) .kaleido i {
  animation-play-state: paused !important;
}

@media (prefers-reduced-motion: reduce) {
  .hoop, .floor, .iris, .split span, .scanlines, .mosh i,
  .lattice, .plasma i, .kaleido i, .rider { animation: none; }
  .hoop { opacity: 0.5; }
}

@media (max-width: 600px) {
  .stage-3d { height: 240px; }
  .moire, .plasma, .kaleido, .fault-stage { height: 220px; }
  .curves { grid-template-columns: 200px; }
}