/* The lab — pages that test what a page can do with no program in it.
   Everything below is a checkbox, a radio button, or a stylesheet rule.
   Colours are borrowed from the ledger's own variables, so the lab reads the
   same under parchment and under night watch, and every moving part stops
   when the reader asks for stillness or ticks the hold-still box. */

/* ---------- The frame every demo sits in ---------- */

.demo {
  margin: 1.8rem 0 2.2rem;
  padding: 1rem;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 4px;
}

.demo > h3 { margin-top: 0; }

.demo-cap {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0.8rem 0 0;
}

.demo-code {
  font-size: 0.78rem;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 0.6rem 0 0;
  margin: 0;
}

/* The hold-still box: the same idiom as the night watch, for moving parts. */

.still {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.78rem;
  letter-spacing: 0.05em;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 0.6rem;
}

.still input {
  appearance: none;
  width: 0.7rem;
  height: 0.7rem;
  border: 1px solid var(--link);
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.still input:checked {
  background: var(--link);
  box-shadow: inset 0 0 0 2px var(--panel);
}

.still:hover { color: var(--link); }

/* ---------- The automata: two rules, forty generations ---------- */

.ca-frame {
  position: relative;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--rule);
  padding: 0.7rem;
  border-radius: 3px;
}

.ca-grid {
  /* One cell size, computed once and used for the columns and the rows, so
     the squares are square and the gaps stay even on any screen. 64 gaps of
     one pixel are subtracted before the width is divided by 65. */
  --cell: calc((min(100%, 468px) - 64px) / 65);
  display: grid;
  row-gap: 1px;
  width: min(100%, 468px);
  margin: 0 auto;
}

/* Every row is a 65-column grid, so a cell knows its own address without
   anything being told where to sit: it carries --c in the markup and the row
   puts it in that column. Unlit cells are simply absent. */

.ca-row {
  display: grid;
  grid-template-columns: repeat(65, var(--cell));
  grid-auto-rows: var(--cell);
  column-gap: 1px;
}

.ca-row i {
  grid-column: var(--c);
  background: var(--link);
  border-radius: 1px;
}

/* The readout sweep: a band of light travelling down the generations, so the
   page reads as an instrument being scanned rather than a picture. */

.ca-scan::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 18%;
  top: -20%;
  pointer-events: none;
  background: linear-gradient(to bottom,
    transparent,
    rgba(217,164,65,0.10) 35%,
    rgba(217,164,65,0.26) 50%,
    rgba(217,164,65,0.10) 65%,
    transparent);
  animation: ca-scan 6s linear infinite;
}

@keyframes ca-scan {
  from { top: -20%; }
  to   { top: 102%; }
}

/* ---------- The sort: 61 states, one playhead ---------- */

.stepper { display: block; }

.stepper input[type="radio"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

/* The playhead is invisible, so the thing being moved must show where the
   keyboard is, or the bars are the only focusable thing on the page. */

.stepper input:focus-visible ~ .stage {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}

.stage {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 4px;
  height: 170px;
  padding: 0.6rem 0.4rem 0;
  border-bottom: 1px solid var(--rule);
}

.bars { display: flex; align-items: flex-end; gap: 4px; height: 100%; }

.bars > div {
  width: clamp(10px, 4.2vw, 22px);
  height: calc(var(--h, 1) * 11px);
  background: var(--rule);
  border-radius: 2px 2px 0 0;
  transition: background 0.15s;
}

/* The bars' resting state is the finished sort, bar n holding the value n.
   Step 0, which is the state the page loads on, prints the shuffled order over
   the top of this. So if the printed states ever fail to load, the failure
   mode of the page is the right answer rather than a broken picture. */

.stage .bars > div:nth-child(1)  { --h: 1; }
.stage .bars > div:nth-child(2)  { --h: 2; }
.stage .bars > div:nth-child(3)  { --h: 3; }
.stage .bars > div:nth-child(4)  { --h: 4; }
.stage .bars > div:nth-child(5)  { --h: 5; }
.stage .bars > div:nth-child(6)  { --h: 6; }
.stage .bars > div:nth-child(7)  { --h: 7; }
.stage .bars > div:nth-child(8)  { --h: 8; }
.stage .bars > div:nth-child(9)  { --h: 9; }
.stage .bars > div:nth-child(10) { --h: 10; }
.stage .bars > div:nth-child(11) { --h: 11; }
.stage .bars > div:nth-child(12) { --h: 12; }

/* The tick strip is the whole sort at a glance: one tick per state, its
   height the number of swaps that had happened by then. */

.ticks {
  display: flex;
  align-items: flex-end;
  gap: 1px;
  height: 34px;
  margin-top: 0.5rem;
}

.ticks label {
  flex: 1 1 0;
  height: calc(var(--h, 4) * 1px);
  min-height: 2px;
  background: var(--rule);
  border: 1px solid transparent;
  border-radius: 1px 1px 0 0;
  cursor: pointer;
}

.ticks label:hover { background: var(--link); }

/* Every state ships its own previous/next pair, and only the pair for the
   ticked radio is visible. They are stacked instead of laid out in a row so
   the 122 hidden labels take up no width. */
.controls {
  position: relative;
  height: 1.45rem;
  margin-top: 0.7rem;
}

.controls .nav {
  position: absolute;
  top: 0;
  left: 0;
  visibility: hidden;
  font-size: 0.8rem;
  letter-spacing: 0.06em;
  color: var(--link);
  cursor: pointer;
  border-bottom: 1px solid var(--rule);
}

.controls .nav:nth-child(even) { left: 7rem; }

.controls .nav:hover { border-bottom-color: var(--gold); color: var(--gold); }

.readout {
  margin: 0.6rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-height: 1.4em;
}

.readout::after { content: "step 0: the starting order"; color: var(--muted); }

/* ---------- The orrery: circles in a tilted plane, and nothing else ---------- */

.orrery {
  position: relative;
  width: min(100%, 340px);
  aspect-ratio: 1;
  margin: 0 auto;
  perspective: 2400px;
}

.orrery-plane {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform: rotateX(70deg);
}

.ring {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--d);
  height: var(--d);
  margin: calc(var(--d) / -2) 0 0 calc(var(--d) / -2);
  border: 1px dashed var(--rule);
  border-radius: 50%;
  animation: ring-turn var(--p) linear infinite;
  animation-delay: var(--pd, 0s);
}

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

.planet {
  position: absolute;
  left: 50%;
  top: 0;
  width: var(--s);
  height: var(--s);
  margin-left: calc(var(--s) / -2);
  margin-top: calc(var(--s) / -2);
  border-radius: 50%;
  background: var(--pc, var(--link));
  transform: rotateX(-70deg);   /* stand the planet back up out of the tilt */
}

.planet--sun {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-radius: 50%;
  background: radial-gradient(circle, #f6d98f, var(--gold) 60%, var(--gold-dim));
  box-shadow: 0 0 22px 6px rgba(217,164,65,0.28);
}

/* The legend is flat text on purpose: the only readable thing on the page. */

.legend {
  list-style: none;
  margin: 1rem auto 0;
  padding: 0;
  max-width: 26em;
  font-size: 0.82rem;
}

.legend li {
  display: grid;
  grid-template-columns: 1.4rem 5.5rem 1fr;
  gap: 0 0.5rem;
  align-items: baseline;
  margin: 0;
  padding: 0.22rem 0;
  border-bottom: 1px solid var(--rule);
}

.legend .dot {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: var(--pc);
  display: inline-block;
}

.legend .who { color: var(--text-strong); }
.legend .num { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }

/* ---------- The clock: right once, then it drifts ---------- */

.clock {
  position: relative;
  width: min(100%, 220px);
  aspect-ratio: 1;
  margin: 0 auto;
  border: 1px solid var(--link);
  border-radius: 50%;
  background:
    repeating-conic-gradient(from 0deg, var(--rule) 0deg 0.4deg, transparent 0.4deg 6deg);
}

.clock::before {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  border: 1px solid var(--rule);
  background: var(--surface);
}

.hand {
  position: absolute;
  left: 50%;
  bottom: 50%;
  transform-origin: 50% 100%;
  background: var(--ink);
  border-radius: 2px;
}

.hand--hour { width: 4px; height: 26%; margin-left: -2px; background: var(--text-strong); }
.hand--min  { width: 3px; height: 36%; margin-left: -1.5px; }
.hand--sec  { width: 1px; height: 42%; margin-left: -0.5px; background: var(--link); }

.hand--hour { animation: ring-turn 43200s linear infinite; }
.hand--min  { animation: ring-turn 3600s linear infinite; }
.hand--sec  { animation: ring-turn 60s steps(60) infinite; }

.clock::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 7px;
  height: 7px;
  margin: -3.5px 0 0 -3.5px;
  border-radius: 50%;
  background: var(--link);
}

/* ---------- The moon: eight discs and a shadow ---------- */

.phases {
  display: flex;
  gap: 0.7rem;
  flex-wrap: wrap;
  justify-content: center;
}

.phase { text-align: center; }

.moon {
  position: relative;
  display: inline-block;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  overflow: hidden;
  background: var(--ink);
  box-shadow: inset 0 0 0 1px var(--rule);
}

.moon i,
.moon b,
.moon u {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  display: block;
}

/* The lit limb: the half of the disc facing the (imaginary) sun. */
.moon i {
  background: var(--gold);
  clip-path: inset(0 0 0 50%);
}

/* The terminator, an ellipse squashed by cos(phase). Drawn dark when it takes
   light away, gold when it adds it, so no per-phase colour is needed. */
.moon b {
  background: var(--ink);
  transform: scaleX(max(var(--c), 0));
}

.moon u {
  background: var(--gold);
  transform: scaleX(max(0 - var(--c), 0));
}

.phase .cap {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 0.3rem;
}

/* ---------- Stillness ---------- */

html:has(#lab-still:checked) .lab-anim,
html:has(#lab-still:checked) .ca-scan::after,
html:has(#lab-still:checked) .hand {
  animation-play-state: paused !important;
}

@media (prefers-reduced-motion: reduce) {
  .ca-scan::after, .ring, .hand { animation: none; }
  .hand--hour { transform: rotate(0deg); }
}

/* ---------- Narrow screens ---------- */

@media (max-width: 600px) {
  .demo { padding: 0.75rem; }
  .controls { gap: 0.9rem; }
  .legend li { grid-template-columns: 1rem 4.6rem 1fr; }
  .phases { gap: 0.5rem; }
}
/* On a narrow page the tilted plane's perspective pushes the outer rings
   past the frame; keep the whole instrument inside the column. */
@media (max-width: 620px) {
  .orrery { width: min(100%, 280px); }
}

@media (max-width: 400px) {
  .orrery { width: min(100%, 240px); }
}

@media (max-width: 340px) {
  .orrery { width: min(100%, 210px); }
}

/* On a wide page the instrument and its legend sit side by side instead of
   floating in a mostly empty box. */
@media (min-width: 760px) {
  .demo:has(.orrery) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 1rem 2.5rem;
    align-items: center;
  }
  .demo:has(.orrery) > .still { grid-column: 1 / -1; }
  .demo:has(.orrery) > .orrery { grid-column: 1; margin: 0 auto; }
  .demo:has(.orrery) > .legend { grid-column: 2; margin: 0; max-width: none; }
  .demo:has(.orrery) > .demo-cap { grid-column: 1 / -1; }
}
