/* ==========================================================================
   9x.cl — nine machines for looking at numbers
   One stylesheet, no imports, no webfonts. Light theme is warm printed paper
   (after Oliver Byrne's 1847 Euclid); dark theme is the same plate in ink.
   ========================================================================== */

/* --- 1. Tokens ----------------------------------------------------------- */

:root {
  color-scheme: light;

  --paper:      #f6f1e6;
  --paper-2:    #efe8d9;
  --paper-3:    #e6ddc9;
  --ink:        #14161a;
  --ink-2:      #57534b;
  --ink-3:      #8a8377;
  --line:       #dbd2be;
  --line-2:     #c2b79e;

  --red:        #c1391c;
  --blue:       #1f428f;
  --gold:       #a8760a;

  --fig-bg:     #fbf8f0;
  --fig-fg:     #14161a;
  --fig-grid:   #ded3bc;
  --fig-faint:  #eae0cc;

  --c1: #c1391c;  --c2: #1f428f;  --c3: #a8760a;
  --c4: #2f7d5f;  --c5: #7e3792;  --c6: #b5561f;
  --c7: #146b80;  --c8: #93314e;  --c9: #4f5a26;

  --shadow: 0 1px 0 rgba(20, 22, 26, .04), 0 6px 24px -14px rgba(20, 22, 26, .35);
  /* A faint laid-paper grain, drawn with a gradient so it costs no bytes. */
  --texture: repeating-linear-gradient(90deg, rgba(120, 100, 60, .022) 0 1px, transparent 1px 3px);

  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --wrap: 76rem;
  --measure: 38rem;
  --rhythm: 1.7;
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --paper:      #0e1013;
  --paper-2:    #14171c;
  --paper-3:    #1b1f26;
  --ink:        #ece6da;
  --ink-2:      #9c968a;
  --ink-3:      #6e6b64;
  --line:       #272c34;
  --line-2:     #3a414d;

  --red:        #ff6d4d;
  --blue:       #82a8ff;
  --gold:       #efc054;

  --fig-bg:     #0a0c0f;
  --fig-fg:     #ece6da;
  --fig-grid:   #232830;
  --fig-faint:  #181c22;

  --c1: #ff6f52;  --c2: #82a8ff;  --c3: #f2c24e;
  --c4: #57c98e;  --c5: #cf87e0;  --c6: #ff9a5c;
  --c7: #55c9d6;  --c8: #ff7fa5;  --c9: #b8cc61;

  --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 8px 28px -16px rgba(0, 0, 0, .9);
  --texture: none;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --paper:      #0e1013;
    --paper-2:    #14171c;
    --paper-3:    #1b1f26;
    --ink:        #ece6da;
    --ink-2:      #9c968a;
    --ink-3:      #6e6b64;
    --line:       #272c34;
    --line-2:     #3a414d;

    --red:        #ff6d4d;
    --blue:       #82a8ff;
    --gold:       #efc054;

    --fig-bg:     #0a0c0f;
    --fig-fg:     #ece6da;
    --fig-grid:   #232830;
    --fig-faint:  #181c22;

    --c1: #ff6f52;  --c2: #82a8ff;  --c3: #f2c24e;
    --c4: #57c98e;  --c5: #cf87e0;  --c6: #ff9a5c;
    --c7: #55c9d6;  --c8: #ff7fa5;  --c9: #b8cc61;

    --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 8px 28px -16px rgba(0, 0, 0, .9);
    --texture: none;
  }
}

/* --- 2. Base ------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: clamp(1rem, .96rem + .2vw, 1.075rem);
  line-height: var(--rhythm);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  background-image: var(--texture);
}

img, svg, canvas { max-width: 100%; display: block; }

[hidden] { display: none !important; }

a {
  color: inherit;
  text-decoration-color: var(--line-2);
  text-underline-offset: .18em;
  text-decoration-thickness: 1px;
}
a:hover { text-decoration-color: var(--red); }

:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  border-radius: 2px;
}

::selection { background: color-mix(in srgb, var(--gold) 35%, transparent); }

.wrap {
  width: 100%;
  max-width: var(--wrap);
  margin-inline: auto;
  padding-inline: clamp(1.15rem, 4vw, 2.75rem);
}

.skip {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--ink);
  color: var(--paper);
  padding: .6rem 1rem;
  z-index: 50;
  font-family: var(--mono);
  font-size: .8rem;
}
.skip:focus { left: .5rem; top: .5rem; }

.vh {
  position: absolute; width: 1px; height: 1px;
  margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* --- 3. Typography ------------------------------------------------------- */

h1, h2, h3 {
  font-weight: 600;
  line-height: 1.18;
  letter-spacing: -.012em;
  margin: 0;
  text-wrap: balance;
}

.eyebrow {
  font-family: var(--mono);
  font-size: .7rem;
  letter-spacing: .19em;
  text-transform: uppercase;
  color: var(--red);
  margin: 0 0 .9rem;
}

.prose { max-width: var(--measure); }
.prose p { margin: 0 0 1.15em; }
.prose h2 {
  font-size: 1.02rem;
  font-family: var(--mono);
  font-weight: 500;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-2);
  margin: 2.8em 0 1em;
  padding-bottom: .55em;
  border-bottom: 1px solid var(--line);
}
.prose h2:first-child { margin-top: 0; }
.prose ul, .prose ol { margin: 0 0 1.15em; padding-left: 1.3em; }
.prose li { margin-bottom: .55em; }
.prose li::marker { color: var(--ink-3); }
.prose strong { font-weight: 650; }
.prose em { font-style: italic; }
.prose cite { font-style: italic; }
.prose code {
  font-family: var(--mono);
  font-size: .86em;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: .08em .34em;
}
.prose sup { font-size: .72em; line-height: 0; }

.prose .rules {
  list-style: none;
  padding: 0;
  margin: 0 0 1.4em;
  border-left: 2px solid var(--red);
}
.prose .rules li {
  padding: .1em 0 .1em 1.1rem;
  margin-bottom: .35em;
}
.prose ol.rules { counter-reset: r; }
.prose ol.rules li { counter-increment: r; }
.prose ol.rules li::before {
  content: counter(r) ". ";
  font-family: var(--mono);
  font-size: .82em;
  color: var(--red);
}

.formula {
  font-family: var(--mono);
  font-size: .94rem;
  letter-spacing: .01em;
  color: var(--ink);
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-left: 2px solid var(--blue);
  padding: .85rem 1rem;
  margin: 0 0 1.15em !important;
  overflow-x: auto;
}

/* --- 4. Header / footer -------------------------------------------------- */

.site-head {
  border-bottom: 1px solid var(--line);
  background: var(--paper); /* fallback where color-mix is unsupported */
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.site-head .wrap {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  min-height: 3.75rem;
}

.brand {
  font-family: var(--mono);
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -.03em;
  text-decoration: none;
  display: inline-flex;
  align-items: baseline;
  gap: .12em;
}
.brand .x { color: var(--red); }
.brand .tld {
  font-size: .62em;
  color: var(--ink-3);
  font-weight: 400;
  letter-spacing: 0;
}

.nav {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: clamp(.75rem, 2.4vw, 1.5rem);
  font-family: var(--mono);
  font-size: .78rem;
  letter-spacing: .06em;
}
.nav a { text-decoration: none; color: var(--ink-2); white-space: nowrap; }
.nav a:hover, .nav a[aria-current="page"] { color: var(--ink); }
.nav a[aria-current="page"] { box-shadow: inset 0 -1px 0 var(--red); }

.theme-toggle {
  font: inherit;
  font-family: var(--mono);
  font-size: .78rem;
  color: var(--ink-2);
  background: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: .3rem .7rem;
  cursor: pointer;
  line-height: 1.4;
}
.theme-toggle:hover { color: var(--ink); border-color: var(--line-2); }

.site-foot {
  border-top: 1px solid var(--line);
  margin-top: 6rem;
  padding: 2.5rem 0 3.5rem;
  font-family: var(--mono);
  font-size: .76rem;
  line-height: 1.85;
  color: var(--ink-3);
}
.site-foot .wrap {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem 2rem;
  justify-content: space-between;
}
.site-foot a { color: var(--ink-2); text-decoration: none; }
.site-foot a:hover { color: var(--ink); text-decoration: underline; }
.site-foot p { margin: 0; }

main { display: block; }

/* --- 5. Home ------------------------------------------------------------- */

.hero { padding: clamp(3rem, 9vw, 6.5rem) 0 clamp(2.25rem, 5vw, 3.5rem); }
.hero h1 {
  font-size: clamp(2.9rem, 1.4rem + 6.2vw, 5.5rem);
  letter-spacing: -.045em;
  font-family: var(--mono);
  font-weight: 700;
  margin-bottom: .35em;
}
.hero h1 .x { color: var(--red); }
.hero .lede {
  font-size: clamp(1.2rem, 1.02rem + .7vw, 1.6rem);
  line-height: 1.44;
  max-width: 30ch;
  margin: 0 0 1.6rem;
  text-wrap: balance;
}
.hero .sub { max-width: var(--measure); color: var(--ink-2); margin: 0; }
.hero .sub + .sub { margin-top: 1em; }

.grid-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 1px solid var(--line-2);
  padding-bottom: .7rem;
  margin-bottom: 1.6rem;
  font-family: var(--mono);
  font-size: .72rem;
  letter-spacing: .19em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(1rem, 2.2vw, 1.6rem);
  list-style: none;
  padding: 0;
  margin: 0;
}
@media (max-width: 62rem) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 34rem) { .grid { grid-template-columns: 1fr; } }

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  background: var(--paper-2);
  transition: border-color .18s ease, transform .18s ease, box-shadow .18s ease;
}
.card:hover, .card:focus-within {
  border-color: var(--line-2);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}
.card a::after {
  content: "";
  position: absolute;
  inset: 0;
}
.card a { text-decoration: none; }
.card:hover .card-name { color: var(--red); }

.card-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  background: var(--fig-bg);
  border-bottom: 1px solid var(--line);
  overflow: hidden;
}
.card-thumb canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
/* Sits after the canvas in the markup so it paints on top without a z-index,
   which would otherwise lift it above the card's full-area click target. */
.card-plate {
  position: absolute;
  top: .55rem;
  left: .65rem;
  font-family: var(--mono);
  font-size: .66rem;
  letter-spacing: .16em;
  color: var(--ink-3);
  pointer-events: none;
}

.card-body { padding: .95rem 1.05rem 1.15rem; }
.card-name {
  font-family: var(--mono);
  font-size: 1.02rem;
  font-weight: 600;
  letter-spacing: -.01em;
  margin: 0 0 .3rem;
  transition: color .18s ease;
}
.card-note {
  margin: 0;
  font-size: .875rem;
  line-height: 1.5;
  color: var(--ink-2);
}

/* --- 6. Machine page ----------------------------------------------------- */

.plate-head { padding: clamp(2.25rem, 6vw, 4rem) 0 1.8rem; }
.plate-head h1 {
  font-family: var(--mono);
  font-size: clamp(2.1rem, 1.4rem + 3vw, 3.4rem);
  letter-spacing: -.038em;
  font-weight: 700;
  margin-bottom: .3em;
}
.plate-head .subtitle {
  font-size: clamp(1.08rem, 1rem + .45vw, 1.35rem);
  color: var(--ink-2);
  max-width: 34ch;
  margin: 0;
  text-wrap: balance;
}

.figure {
  margin: 0 0 clamp(2.5rem, 6vw, 4rem);
  border: 1px solid var(--line-2);
  background: var(--paper-2);
  box-shadow: var(--shadow);
}
.figure-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 17.5rem;
}
@media (max-width: 56rem) {
  .figure-body { grid-template-columns: minmax(0, 1fr); }
}

/* The stage is a grid so the inner frame always stretches to the row height,
   whichever of the figure and the control panel turns out to be taller. The
   canvas is positioned out of flow so its own size can never feed back into
   the box the ResizeObserver is watching. */
.stage {
  display: grid;
  position: relative;
  background: var(--fig-bg);
  min-width: 0;
}
.stage-inner {
  position: relative;
  min-width: 0;
  min-height: clamp(19rem, 44vw, 33rem);
}
.stage-inner canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: pan-y;
}
@media (max-width: 56rem) { .stage-inner { min-height: min(88vw, 30rem); } }

/* Top-left, so it never collides with the rule strip Plate IV draws along the
   bottom of its frame. */
.stage-badge {
  position: absolute;
  left: .7rem;
  top: .7rem;
  font-family: var(--mono);
  font-size: .68rem;
  letter-spacing: .04em;
  color: var(--ink-3);
  background: var(--fig-bg);
  background: color-mix(in srgb, var(--fig-bg) 78%, transparent);
  padding: .15rem .4rem;
  pointer-events: none;
  white-space: pre;
  max-width: calc(100% - 1.4rem);
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel {
  border-left: 1px solid var(--line);
  background: var(--paper-2);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
@media (max-width: 56rem) {
  .panel { border-left: 0; border-top: 1px solid var(--line); }
}

.panel-scroll {
  padding: 1rem 1.15rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: .9rem;
}

/* On a wide screen the panel is capped so a machine with many controls does
   not stretch the figure to an absurd height; the controls scroll instead. */
@media (min-width: 56.0625rem) {
  .panel { max-height: 34rem; }
  .panel-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

.ctl { display: flex; flex-direction: column; gap: .3rem; }
.ctl-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: .6rem;
  font-family: var(--mono);
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-2);
}
.ctl-value {
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  letter-spacing: 0;
  text-transform: none;
  font-size: .78rem;
}

.ctl-row { display: flex; align-items: center; gap: .4rem; }
.ctl-row input[type="range"] { flex: 1 1 auto; min-width: 0; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 1.35rem;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 2px;
  background: var(--line-2);
}
input[type="range"]::-moz-range-track {
  height: 2px;
  background: var(--line-2);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 13px; height: 13px;
  margin-top: -5.5px;
  border-radius: 50%;
  background: var(--red);
  border: 0;
}
input[type="range"]::-moz-range-thumb {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--red);
  border: 0;
}
input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px color-mix(in srgb, var(--blue) 45%, transparent); }
input[type="range"]:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 3px color-mix(in srgb, var(--blue) 45%, transparent); }

select, input[type="number"], input[type="text"] {
  font-family: var(--mono);
  font-size: .82rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-2);
  border-radius: 3px;
  padding: .35rem .45rem;
  width: 100%;
  min-width: 0;
}
input[type="number"] { font-variant-numeric: tabular-nums; }

.ctl-check {
  flex-direction: row;
  align-items: center;
  gap: .55rem;
  font-family: var(--mono);
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-2);
  cursor: pointer;
}
.ctl-check input { accent-color: var(--red); width: 1rem; height: 1rem; margin: 0; cursor: pointer; }

.btn {
  font-family: var(--mono);
  font-size: .74rem;
  letter-spacing: .07em;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line-2);
  border-radius: 3px;
  padding: .42rem .7rem;
  cursor: pointer;
  white-space: nowrap;
  line-height: 1.4;
}
.btn:hover { border-color: var(--red); color: var(--red); }
.btn:active { transform: translateY(1px); }
.btn[data-primary] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn[data-primary]:hover { background: var(--red); border-color: var(--red); color: #fff; }
:root[data-theme="dark"] .btn[data-primary]:hover { color: #14161a; }

.btn-row { display: flex; flex-wrap: wrap; gap: .4rem; }
.btn-row .btn { flex: 1 1 auto; }

.panel-foot {
  margin-top: auto;
  border-top: 1px solid var(--line);
  padding: .8rem 1.15rem;
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  align-items: center;
}
.panel-foot .btn { flex: 1 1 auto; }

.readouts {
  border-top: 1px solid var(--line);
  padding: .8rem 1.15rem;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: .55rem .9rem;
  font-family: var(--mono);
  font-size: .72rem;
}
.readout { min-width: 0; }
.readout dt { color: var(--ink-3); letter-spacing: .08em; text-transform: uppercase; font-size: .64rem; margin: 0 0 .12rem; }
.readout dd { margin: 0; font-variant-numeric: tabular-nums; color: var(--ink); overflow: hidden; text-overflow: ellipsis; }
.readout dd.hot { color: var(--red); }

.panel > .legend { padding: 0 1.15rem 1rem; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: .25rem .8rem;
  font-family: var(--mono);
  font-size: .68rem;
  color: var(--ink-2);
}
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.legend i {
  width: .62rem; height: .62rem; border-radius: 2px;
  display: inline-block; flex: none;
}

.figure-caption {
  border-top: 1px solid var(--line);
  padding: .7rem 1.15rem;
  font-family: var(--mono);
  font-size: .7rem;
  line-height: 1.6;
  color: var(--ink-3);
}

.nojs {
  padding: 1.15rem;
  font-family: var(--mono);
  font-size: .78rem;
  color: var(--ink-2);
  background: var(--paper-3);
  border-bottom: 1px solid var(--line);
}

/* --- 7. Pager ------------------------------------------------------------ */

.pager {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
  margin-top: clamp(3rem, 7vw, 5rem);
  border-top: 1px solid var(--line-2);
  padding-top: 1.4rem;
}
@media (max-width: 34rem) { .pager { grid-template-columns: 1fr; } }
.pager a {
  text-decoration: none;
  display: block;
  padding: .75rem 0;
}
.pager .dir {
  font-family: var(--mono);
  font-size: .68rem;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--ink-3);
  display: block;
  margin-bottom: .25rem;
}
.pager .to { font-family: var(--mono); font-size: 1.05rem; font-weight: 600; }
.pager a:hover .to { color: var(--red); }
.pager .next { text-align: right; }
@media (max-width: 34rem) { .pager .next { text-align: left; } }

/* --- 8. About / plain pages ---------------------------------------------- */

.page-head { padding: clamp(2.5rem, 7vw, 4.5rem) 0 1.5rem; }
.page-head h1 {
  font-family: var(--mono);
  font-size: clamp(2rem, 1.4rem + 2.6vw, 3rem);
  letter-spacing: -.035em;
  font-weight: 700;
}

.spec {
  border-top: 1px solid var(--line);
  margin: 0;
  max-width: var(--measure);
  font-size: .93rem;
}
.spec > div {
  display: grid;
  grid-template-columns: 9.5rem minmax(0, 1fr);
  gap: 1rem;
  padding: .7rem 0;
  border-bottom: 1px solid var(--line);
}
@media (max-width: 34rem) { .spec > div { grid-template-columns: 1fr; gap: .1rem; } }
.spec dt {
  font-family: var(--mono);
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-3);
  padding-top: .28em;
}
.spec dd { margin: 0; }

.plate-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-width: var(--measure);
  border-top: 1px solid var(--line);
}
.plate-list li { border-bottom: 1px solid var(--line); }
.plate-list a {
  display: grid;
  grid-template-columns: 3rem minmax(0, 1fr);
  gap: 1rem;
  padding: .8rem 0;
  text-decoration: none;
  align-items: baseline;
}
.plate-list .n { font-family: var(--mono); font-size: .72rem; color: var(--ink-3); letter-spacing: .12em; }
.plate-list .t { font-family: var(--mono); font-weight: 600; }
.plate-list .d { color: var(--ink-2); font-size: .9rem; display: block; margin-top: .1rem; }
.plate-list a:hover .t { color: var(--red); }

.error-code {
  font-family: var(--mono);
  font-size: clamp(5rem, 18vw, 11rem);
  font-weight: 700;
  letter-spacing: -.06em;
  line-height: .9;
  color: var(--red);
  margin: 0 0 1rem;
}
