:root {
  --s0: #2b8ad8;
  --s1: #d6b70f;
  --s2: #e0383a;
  --s3: #8e9b14;
  --s4: #c8651b;
  --s5: #7a6fd0;
  --halt: #d4317b;
  --champion: #7d8a10;
  --paper: #ede6d3;
  --card: #f4efe2;
  --ink: #1d2b31;
  --slate: #5c6f7b;
  --rule: #cfc5ab;
  --zero: #ffffff;
  --one: #0f2b36;
  --one-text: #6f8792;
  --zero-text: #b4ab95;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #14191c;
    --card: #1a2125;
    --ink: #e6e0d0;
    --slate: #93a6b0;
    --rule: #2e3a40;
    --zero: #222c31;
    --one: #e6e0d0;
    --one-text: #5c6f7b;
    --zero-text: #56656c;
    --champion: #b3c22a;
    --halt: #e8558f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 18px/1.6 "Source Serif 4", Georgia, serif;
  -webkit-font-smoothing: antialiased;
}

main {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 16px 64px;
}

section, .hero, footer {
  max-width: 720px;
  margin: 0 auto;
}

section { padding-top: 56px; }

h1, h2 {
  font-weight: 400;
  color: var(--slate);
  width: fit-content;
  border-bottom: 1.5px solid var(--slate);
  padding: 0 6px 2px;
  margin: 0 0 20px;
  line-height: 1.2;
}

h1 { font-size: clamp(40px, 9vw, 64px); margin: 0 auto 12px; }
h2 { font-size: 30px; }

.hero { text-align: center; padding-top: 64px; }
.lede { font-size: 22px; color: var(--slate); margin: 0 0 20px; }

.toc {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 18px;
  font-size: 15px;
}

a { color: var(--s0); text-underline-offset: 3px; }
.toc a { color: var(--slate); }

code, .sim-text, .sequence {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.82em;
}

.halt-word {
  font: 600 12px "Source Serif 4", serif;
  background: var(--halt);
  color: white;
  border-radius: 3px;
  padding: 1px 5px;
  vertical-align: 2px;
}

.figure { margin: 28px 0; }
section > .figure { margin-left: calc(50% - min(50vw, 520px) + 16px); margin-right: calc(50% - min(50vw, 520px) + 16px); }

/* Simulator */

.sim-id {
  display: flex;
  justify-content: center;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 12px;
  color: var(--slate);
  font: 15px "JetBrains Mono", monospace;
}

.sim-text { font-size: 15px; color: var(--slate); }

input.sim-text {
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 4px 8px;
  color: var(--ink);
  width: min(100%, 30em);
  text-align: center;
}

.cycle { color: var(--s0); }
.check { color: var(--champion); }
.sim-count { display: inline-block; white-space: nowrap; font-variant-numeric: tabular-nums; }
.sim-error { color: var(--s2); text-align: center; font-size: 15px; margin: 6px 0 0; }

.sim-stage {
  display: grid;
  grid-template-columns: minmax(0, 360px) minmax(0, 1fr);
  gap: 16px;
  align-items: center;
  margin-top: 8px;
}

.sim-history { position: relative; }
.sim-hint[hidden] { display: none; }
.sim-count.halted { color: var(--champion); animation: stamp 0.5s ease-out; }
.sim-count.halted b { font-size: clamp(30px, 8vw, 44px); font-weight: 600; font-family: "Source Serif 4", Georgia, serif; vertical-align: -3px; }
@keyframes stamp { from { opacity: 0; transform: scale(1.1); } }
.sim-hint {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 0 24px;
  font-size: 15px;
  font-style: italic;
  color: var(--slate);
  border: 1px dashed var(--rule);
  border-radius: 6px;
  pointer-events: none;
}

.sim-graph { width: 100%; height: auto; max-height: 280px; }
.sim-spacetime { width: 100%; height: 320px; display: block; }

.tape {
  display: flex;
  justify-content: center;
  gap: 2px;
  margin-top: 6px;
  overflow: hidden;
  mask-image: linear-gradient(90deg, transparent, black 12%, black 88%, transparent);
}

.tape span {
  flex: 0 0 24px;
  height: 24px;
  display: grid;
  place-items: center;
  background: var(--zero);
  color: var(--zero-text);
  border: 1px solid var(--rule);
  font-size: 12px;
}

.tape span.one { background: var(--one); color: var(--one-text); border-color: var(--one); }
.tape span.head { outline: 3px solid var(--head); outline-offset: 1px; position: relative; z-index: 1; }

.sim-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px 10px;
  margin-top: 16px;
}

button {
  font: 15px "Source Serif 4", Georgia, serif;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--slate);
  border-radius: 4px;
  padding: 5px 14px;
  cursor: pointer;
}

button:hover:not(:disabled) { background: var(--card); }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
button.primary:hover { background: var(--slate); }

.sim-speed { display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--slate); }
.sim-speed input { width: 120px; accent-color: var(--s0); }
.sim-speed output { min-width: 4.5em; font: 13px "JetBrains Mono", monospace; }

.sim-table-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 20px; }
.sim-table { border-collapse: collapse; font-size: 15px; }
.sim-table th, .sim-table td { border: 1px solid var(--rule); padding: 4px 10px; text-align: center; }
.sim-table th { font-weight: 400; color: var(--slate); }
.sim-table input {
  width: 3.6em;
  font: 15px "JetBrains Mono", monospace;
  text-align: center;
  text-transform: uppercase;
  background: transparent;
  color: var(--ink);
  border: none;
  padding: 2px;
}
.sim-table input:focus { outline: 2px solid var(--s0); border-radius: 2px; }
.sim-states { display: flex; gap: 8px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }

/* State graph */

.node circle { stroke: var(--paper); stroke-width: 2; }
.node.s0 circle { fill: var(--s0); }
.node.s1 circle { fill: var(--s1); }
.node.s2 circle { fill: var(--s2); }
.node.s3 circle { fill: var(--s3); }
.node.s4 circle { fill: var(--s4); }
.node.s5 circle { fill: var(--s5); }
.node.halt rect { fill: var(--halt); stroke: var(--paper); stroke-width: 2; }
.node text { fill: white; font: 600 13px "Source Serif 4", Georgia, serif; }
.node.halt text { font-size: 10px; }
.node.active circle, .node.active rect { stroke: var(--ink); stroke-width: 4; }
.arrow { fill: none; stroke: var(--ink); stroke-width: 1.2; opacity: 0.55; }
.arrowhead { fill: var(--ink); opacity: 0.7; }
.tail.zero { fill: var(--paper); stroke: var(--ink); stroke-width: 1; }
.tail.one { fill: var(--ink); }
.move { font: 11px "Source Serif 4", Georgia, serif; fill: var(--slate); }
.edge.taken .arrow { stroke-width: 2.4; opacity: 1; }

/* Legend */

.legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 10px 28px; margin: 0 0 20px; font-size: 16px; }
.legend div { display: flex; gap: 10px; }
.legend dt { flex: 0 0 92px; color: var(--slate); display: flex; align-items: center; gap: 8px; height: 1.6em; }
.legend dd { margin: 0; }
.glyph { flex: 0 0 auto; display: inline-block; width: 14px; height: 14px; }
.node-glyph { border-radius: 50%; background: var(--s0); }
.arrow-glyph { height: 2px; background: var(--ink); opacity: 0.6; }
.tape-glyph { background: linear-gradient(90deg, var(--zero) 50%, var(--one) 50%); border: 1px solid var(--rule); }
.spacetime-glyph { background: linear-gradient(var(--s1) 33%, var(--s0) 33% 66%, var(--s1) 66%); }

/* BB(2) wall */

.wall-figure { display: flex; flex-direction: column; align-items: center; gap: 12px; }
#wall {
  width: min(100%, 432px);
  aspect-ratio: 1;
  image-rendering: pixelated;
  cursor: crosshair;
  touch-action: manipulation;
}
.wall-caption { margin: 0; text-align: center; font-size: 16px; color: var(--slate); min-height: 3.2em; max-width: 34em; }
.wall-caption b, .big { color: var(--champion); }

/* Cards */

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin: 24px calc(50% - min(50vw, 520px) + 16px);
}

.card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 12px;
  line-height: 1.35;
}
.card:hover:not(:disabled) { background: var(--card); border-color: var(--slate); }
.card-name { font-size: 20px; color: var(--slate); }
.card.wide { grid-column: 1 / -1; }
.card.wide canvas { height: 280px; }
.card code { font-size: 10px; color: var(--slate); word-break: break-all; }
.card canvas { width: 100%; height: 180px; display: block; margin: 8px 0; }
.card-result { font-size: 14px; color: var(--slate); }
.card-result .big { font-size: 26px; font-weight: 600; }
.card-note { font-size: 14px; margin-top: 4px; }

/* BB(5) */

.collatz { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.collatz-n { font-size: 22px; color: var(--slate); }
.collatz-n b { display: inline-block; min-width: 4.2em; font: 600 40px "JetBrains Mono", monospace; color: var(--ink); text-align: left; }
.collatz-rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; width: min(100%, 26em); }
.collatz-rules li { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 12px; border: 1px solid var(--rule); border-radius: 6px; opacity: 0.4; transition: opacity 0.3s, border-color 0.3s; }
.collatz-rules li.on { opacity: 1; border-color: var(--slate); background: var(--card); }
.collatz-rules li > span:first-child { color: var(--slate); font-size: 15px; }
.collatz .sequence span { transition: color 0.3s; }
.collatz .sequence span.on { color: var(--champion); font-weight: 600; }

/* BB(6) */

.tower { display: flex; flex-direction: column; gap: 18px; align-items: center; }
.tower-row { display: grid; grid-template-columns: 7.5em 6em 4.5em; align-items: center; gap: 10px; }
.twos { display: flex; align-items: flex-end; height: 3.4em; }
.twos i { font-style: normal; color: var(--s5); line-height: 1; }
.twos i:nth-child(1) { font-size: 30px; }
.twos i:nth-child(2) { font-size: 24px; transform: translateY(-14px); }
.twos i:nth-child(3) { font-size: 19px; transform: translateY(-26px); }
.twos i:nth-child(4) { font-size: 16px; transform: translateY(-36px); }
.twos i:nth-child(5) { font-size: 13px; transform: translateY(-44px); }
.twos i:nth-child(6) { font-size: 13px; transform: translateY(-50px); color: var(--slate); }
.twos i:nth-child(7) { font-size: 11px; transform: translateY(-58px); }
.tower-row:nth-child(2) .twos i { color: var(--s0); }
.tower-row.last .twos i { color: var(--s2); }
.brace { border-left: 1.5px solid var(--slate); border-radius: 6px 0 0 6px; padding-left: 8px; font-size: 15px; color: var(--slate); align-self: stretch; display: flex; align-items: center; }
.name { font-size: 20px; }
.tower-caption { margin: 0; font-size: 15px; color: var(--slate); text-align: center; }

.run-row { text-align: center; }
.sequence { color: var(--slate); line-height: 1.8; text-align: center; }

footer { margin-top: 72px; padding-top: 16px; border-top: 1px solid var(--rule); font-size: 15px; color: var(--slate); }

@media (max-width: 640px) {
  body { font-size: 17px; }
  .sim-stage { grid-template-columns: 1fr; }
  .sim-graph { max-height: 220px; }
  .sim-spacetime { height: 240px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
  .card canvas { height: 140px; }
}
