:root {
  /* Dark unless the visitor's system asks for light, or the lights button has been
     pressed: it sets data-theme, and that settles the scheme below. */
  color-scheme: dark light;

  /* Each color as light-dark(in the light, in the dark). --accent is for the links,
     where it has to be readable as text. Shoes and socks come in three colors, which
     only have to be seen. None of them is red, or anywhere near it: under letters
     this size, red reads as the name bleeding. The first is yellow in the dark and a
     yellow-green in the light, where yellow goes muddy and, striped with the dark
     legs, looks like a bee. The dark values are repeated in the @supports block below,
     and --paper in both schemes as theme-color in index.html: change all three. */
  --paper: light-dark(#e9e8e6, #0e0e0f);
  --ink: light-dark(#141415, #f1ede4);
  --edge: light-dark(#5d5b57, #8b8882);
  --accent: light-dark(#1b5fd6, #4d9bff);
  --shoe-1: light-dark(#659a00, #ffd60a);
  --shoe-2: light-dark(#009474, #2fe0b5);
  --shoe-3: light-dark(#2f7bf0, #4d9bff);

  --pad: clamp(1rem, 4.5vw, 7rem);
  --text-size: clamp(1.0625rem, 0.9rem + 0.45vw, 1.5rem);
  /* The name stands one word to a row, at the smallest of three sizes. The width: the
     longer word has to fit across, and how many ems that is depends on the font, so the
     script measures it and says so in --name-ems (until it has, or if it never runs, 7.2
     is room enough in the widest system fonts). The height, 20svh: the two rows on their
     legs need about two and a half ems. And max(11rem, 9vw), so that on a big screen it
     stops growing. Where the width is what limits it, the name is as wide as the viewport
     less the padding on both sides. (Where scrollbars take room of their own, the right
     padding gives it up.) */
  --name-size: min((100vw - 2 * var(--pad)) / var(--name-ems, 7.2), 20svh, max(11rem, 9vw));
  --sans: system-ui, "Helvetica Neue", Arial, sans-serif;
}

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

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

/* Browsers from before light-dark() (it reached the last of the three engines in
   mid-2024) get the dark side only, and no button for a switch that would do nothing. */
@supports not (color: light-dark(#000, #fff)) {
  :root {
    color-scheme: dark;
    --paper: #0e0e0f;
    --ink: #f1ede4;
    --edge: #8b8882;
    --accent: #4d9bff;
    --shoe-1: #ffd60a;
    --shoe-2: #2fe0b5;
    --shoe-3: #4d9bff;
  }

  #lights {
    display: none;
  }
}

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

html {
  /* The room for a scrollbar is kept whether or not the page scrolls, so that a window that
     gets short enough to scroll does not narrow the stage, and shift the name sideways. */
  scrollbar-gutter: stable;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* A finger on the stage is for the letters: dragged, it marches them about, so it must
   not drag the page about as well, or stretch it, or pull it down to reload. Two
   fingers still zoom. The script only says the page is anchored while all of it is on
   screen at its natural size: zoomed in, or with somewhere to scroll to, a finger
   moves the page as it would any other. */
:root[data-anchored] {
  overscroll-behavior: none;
  touch-action: pinch-zoom;
}

/* Repeated on the stage as a precaution: Safari on a phone does not always take the
   root's touch-action at its word. */
:root[data-anchored] main {
  touch-action: pinch-zoom;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: var(--text-size) / 1.5 var(--sans);
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* The stage. Letter homes are measured from its top left. Its content is one centered
   column exactly as wide as the name. The first row is only space: room for the top
   row of letters to stand up and hop in, and it gives way first on a short screen. */
main {
  position: relative;
  overflow: clip; /* a letter may be shooed to the edge without the page growing scrollbars */
  display: grid;
  grid-template-columns: minmax(var(--pad), 1fr) auto minmax(var(--pad), 1fr);
  /* Head room, the name, a gap, the links, the buttons: with whatever height is left
     over shared out equally above and below the links, so that the page fills the
     screen from top to bottom instead of huddling at the top of it. Nothing above the
     name changes height, so nothing the script does can move its homes. */
  grid-template-rows:
    calc(var(--name-size) * 0.6 + clamp(0rem, 10svh - 3rem, 5rem))
    [name] auto
    calc(var(--name-size) * 0.3)
    1fr
    [links] auto
    1fr
    [controls] auto;
  min-height: 100svh;
  padding-block: clamp(0.75rem, 4svh, var(--pad));
}

/* While a letter is being carried. */
main[data-carrying] {
  cursor: grabbing;
  -webkit-user-select: none;
  user-select: none;
}

h1,
nav,
#controls {
  grid-column: 2;
}

h1 {
  grid-row: name;
}

nav {
  grid-row: links;
}

#controls {
  grid-row: controls;
}

/* Everything under the name takes the name's width rather than adding to it, and sits
   in front of the letters: they walk behind the links and the buttons, never over them.
   The exceptions are a letter in hand, which is drawn over everything until it is let go,
   and one up on the lights button, which would otherwise stand behind whatever is above
   it. Letters never catch the pointer, so even then the links and buttons can be pressed.
   A width of 0 with a min-width of 100% adds nothing to the auto column's size and then
   fills it, where width: 100% would let a long link widen the name's column. */
nav,
#controls {
  position: relative;
  z-index: 100000; /* above every letter's (how far down the stage it stands), below one in hand's or on a switch's (draw.js) */
  width: 0;
  min-width: 100%;
}

/* The line height is 1.2 so that the lower row can stand up under the upper row's feet. */
h1 {
  width: min-content; /* one word to a row */
  margin: 0;
  font: 800 var(--name-size) / 1.2 var(--sans);
  font-kerning: none; /* so nothing shifts when each letter becomes its own box */
  letter-spacing: 0.02em;
  text-transform: uppercase;
  text-box: trim-both cap alphabetic;
}

/* The name is selected and copied from its hidden copy, not from the loose letters. */
h1 > [aria-hidden] {
  -webkit-user-select: none;
  user-select: none;
}

.word {
  display: inline-block;
  white-space: nowrap;
}

/* Each letter is its own layer: the script stacks them so whoever stands lower on the
   page is in front, and a thin outline in the page's color keeps one legible against
   another. Any of them may be moved at any moment, so the layers are kept. The script
   picks the letter under a pointer by where it is, so the boxes themselves must not
   catch it. */
.letter {
  position: relative;
  display: inline-block;
  will-change: transform;
  pointer-events: none;
  -webkit-text-stroke: 0.045em var(--paper);
  paint-order: stroke fill;
}

/* The glyph is drawn from data-glyph and not held as text, so that the name is in the
   document once, in the hidden copy that assistive tech and find-in-page read. It takes
   the heading's case, spacing and stroke as the text would. */
.letter::before {
  content: attr(data-glyph);
}

/* An empty box at the end of each letter. With nothing in its flow it sits exactly on
   the text's baseline, which tells the script where that is, and the letter's legs and
   eyes hang off it. The whole letter shares a layer so that its face and feet move
   together when the layer is composited. */
.rig {
  position: relative;
  display: inline-block;
  width: 0;
  height: 0;
}

/* Give the legs a viewport that contains their strokes. The top clips at the
   baseline so the leg outline can't bite into the glyph. Allow for full stretch
   on either side and for the inverse scale that keeps legs long under a squash. */
.legs {
  --leg-inset: calc(var(--letter-width) + 1.25em);
  position: absolute;
  inset: 0 auto auto calc(-1 * var(--leg-inset));
  width: calc(var(--leg-inset) + 1.25em);
  height: 6em;
  overflow: hidden;
  fill: none;
  stroke-linejoin: round;
}

.leg-paths {
  transform: translateX(var(--leg-inset));
}

/* A bare line of the letter's own color ending in a blob reads from across a room as
   the name bleeding. So the legs are dressed: a striped sock, and a shoe with a flat
   sole and a toe. Legs and shoes get an outline like the letters', so that one crossing
   another letter does not melt into it. A leg's is a fatter copy of it drawn underneath,
   round at the ends; the leg and sock above it are cut square at the foot, because a
   rounded end would poke out under the sole. */
.casing {
  stroke: var(--paper);
  stroke-width: 0.11em;
  stroke-linecap: round;
}

.leg,
.sock {
  stroke-width: 0.05em;
}

.leg {
  stroke: var(--ink);
}

/* The sock is bands of the shoe's color over the leg, the first a little way down.
   On small type the bands stop shrinking, so they stay stripes and do not turn to dots. */
.sock {
  --band: max(0.042em, 3px);
  stroke: var(--shoe);
  stroke-dasharray: var(--band);
  stroke-dashoffset: var(--band);
}

/* The three colors are dealt out along each word, a matching pair to a letter. */
.letter:nth-child(3n + 1) {
  --shoe: var(--shoe-1);
}

.letter:nth-child(3n + 2) {
  --shoe: var(--shoe-2);
}

.letter:nth-child(3n) {
  --shoe: var(--shoe-3);
}

.shoe {
  fill: var(--shoe);
  stroke: var(--paper);
  stroke-width: 0.04em;
  paint-order: stroke;
}

.sole {
  stroke: var(--ink);
  stroke-width: 0.022em;
}

/* A dot of the page's color on the letter's ink. --x is ems from the rig, --y ems
   above the baseline, --size its width in ems: smaller on a letter with little ink to put it on. */
.eye {
  position: absolute;
  inset: auto auto calc(var(--y) * 1em) calc(var(--x) * 1em);
  width: calc(var(--size) * 1em);
  height: calc(var(--size) * 1em);
  margin: 0 0 calc(var(--size) * -0.5em) calc(var(--size) * -0.5em);
  border-radius: 50%;
  background: var(--paper);
}

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Each line is a plate of the page's color, so it stays readable with a letter behind it. */
li {
  width: fit-content;
  background: var(--paper);
  overflow-wrap: anywhere;
}

a {
  display: inline-block;
  padding-block: 0.25rem;
  color: var(--ink);
  text-decoration-line: underline;
  text-decoration-color: var(--accent);
  text-decoration-thickness: max(1px, 0.07em);
  text-underline-offset: 0.25em;
}

@media (hover: hover) {
  a:hover {
    color: var(--accent);
  }
}

#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding-block-start: clamp(0.75rem, 8svh - 1.5rem, 3rem);
  font-size: 1rem;
}

/* The display above would otherwise show the buttons in spite of their hidden attribute.
   Without scripting they stay hidden, as nothing would answer them. With it, they keep
   their place until the script reveals them, so nothing jumps when they appear. */
#controls[hidden] {
  display: none;
}

@media (scripting: enabled) {
  #controls[hidden] {
    display: flex;
    visibility: hidden;
  }
}

button {
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--edge);
  background: var(--paper);
  color: inherit;
  font: inherit;
  cursor: pointer;
}

@media (hover: hover) {
  button:hover {
    border-color: var(--ink);
  }
}

/* "calm" is a switch, and shows as pressed in for as long as it is on. */
button[aria-pressed="true"] {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--paper);
}

@media (pointer: coarse) {
  /* A comfortable target for a finger, however narrow the emoji on the lights button. */
  button {
    min-width: 2.75rem;
    min-height: 2.75rem;
  }

  a {
    padding-block: 0.6rem;
  }
}

/* On a screen with no height to spare (a phone on its side) the links stand side by side. */
@media (max-height: 28rem) {
  ul {
    display: flex;
    flex-wrap: wrap;
    column-gap: 1.5rem;
  }
}

/* The decisions panel (why.ts) and the key guide (guide.ts): drawn over the page, outside the
   stage, so they move nothing, and dragged about by their title bars (float.ts). */
#why,
#guide {
  position: fixed;
  top: 0;
  left: 0;
  /* Above a letter in hand (UP_FRONT, 200000, in draw.ts) and the links and buttons (100000): nothing covers them.
     Whichever of the two was last opened, pressed or given the focus is a little higher still (float.ts). */
  --panel-z: 300000;
  z-index: var(--panel-z);
  display: flex;
  flex-direction: column;
  border: 1px solid var(--edge);
  background: var(--paper);
  color: var(--ink);
}

#why {
  /* A fixed size: it is dragged, and a box that changes size under the pointer loses it. */
  width: min(24rem, calc(100vw - 1rem));
  height: min(30rem, 55svh);
  font: 0.75rem / 1.4 var(--sans);
  font-variant-numeric: tabular-nums;
}

/* The display above would otherwise show them in spite of their hidden attribute. */
#why[hidden],
#guide[hidden] {
  display: none;
}

#why header,
#guide header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem;
  border-bottom: 1px solid var(--edge);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#why h2,
#guide h2 {
  margin: 0 auto 0 0;
  font-size: inherit;
}

#why button,
#guide button {
  padding: 0.15rem 0.5rem;
  font-size: inherit;
}

#why .body,
#why .log {
  margin: 0;
  padding: 0.25rem 0.5rem;
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

#why .body {
  flex: 1 1 auto;
  min-height: 0;
}

#why .log {
  flex: 0 0 8rem;
  border-top: 1px solid var(--edge);
}

/* With a screen's width to stand beside the name in, it is tall enough to show all fourteen
   letters without scrolling, and its log is shorter for it. A phone keeps the name in view. */
@media (min-width: 48rem) and (min-height: 30rem) {
  #why {
    height: min(40rem, calc(100svh - 1rem));
  }

  #why .log {
    flex-basis: 5rem;
  }
}

/* A finger needs more to hit than the panel's buttons are. */
@media (pointer: coarse) {
  #why button,
  #guide header button,
  #guide .keys button {
    min-width: 2.5rem;
    min-height: 2.5rem;
  }
}

/* On a screen with no height to spare (a phone on its side) the panel is taller for it, and its log shorter. */
@media (max-height: 30rem) {
  #why {
    height: 80svh;
  }

  #why .log {
    flex-basis: 4rem;
  }
}

/* The scrollers are in the box, and an outline outside it would be cut off. */
#why :is(.body, .log):focus-visible {
  outline-offset: -2px;
}

#why ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* The page's plates are for text over letters: the panel is not over any. */
#why li {
  width: auto;
  background: none;
  overflow-wrap: anywhere;
}

#why .pairs {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  gap: 0 0.5rem;
  margin: 0 0 0.25rem;
}

#why .pairs.wide {
  grid-template-columns: auto 1fr;
  margin: 0.25rem 0 0;
}

#why dd {
  margin: 0;
}

#why dt,
#why .at,
#why [data-idle] {
  color: var(--edge);
}

#why .letters li {
  display: flex;
  gap: 0.5rem;
}

/* Wide enough for a glyph and two digits. */
#why .letters b {
  flex: none;
  width: 3.5ch;
  white-space: nowrap;
}

#why .log .at {
  margin-inline-end: 0.5rem;
}

/* The key guide (guide.ts). Its size is its contents' (they never change while it is dragged),
   unless it has been made smaller to keep clear of the name, and then its rows scroll. */
#guide {
  width: min(46rem, calc(100vw - 1rem));
  max-height: calc(100svh - 1rem);
  font: 0.875rem / 1.35 var(--sans);
}

#guide .body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0.5rem;
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

/* Made smaller, the guide fades out at its foot until it is scrolled to the end, so that a
   row cut through there reads as more to scroll to and not as something broken. It takes
   no room, and where the rows do not scroll (or the browser cannot tell how far they are
   scrolled) there is no fade. */
#guide .body::after {
  content: '';
  position: sticky;
  bottom: -0.5rem;
  display: block;
  height: 1.25rem;
  margin-top: -1.25rem;
  background: linear-gradient(transparent, var(--paper));
  opacity: 0;
  pointer-events: none;
}

@supports (animation-timeline: scroll()) {
  #guide .body::after {
    animation: more-below linear both;
    animation-timeline: scroll(nearest block);
  }
}

@keyframes more-below {
  0%,
  90% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

#guide ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr));
  gap: 0.15rem 0.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* The page's plates are for text over letters: the guide is not over any. */
#guide li {
  width: auto;
  background: none;
}

/* A row is a button, plain until it is pointed at or has the focus. */
#guide li > button {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  width: 100%;
  padding: 0.2rem 0.35rem;
  border-color: transparent;
  background: none;
  text-align: start;
}

#guide kbd {
  flex: none;
  min-width: 1.75em;
  padding: 0 0.3em;
  border: 1px solid var(--edge);
  font: inherit;
  font-weight: 600;
  text-align: center;
}

#guide .march {
  display: flex;
  grid-column: 1 / -1;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding-inline: 0.35rem;
}

#guide .keys {
  display: flex;
  gap: 0.15rem;
}

#guide .keys button {
  padding: 0;
  border-color: transparent;
  background: none;
}

#guide p {
  margin: 0.5rem 0.35rem 0;
  color: var(--edge);
}

@media (hover: hover) {
  #guide :is(li > button, .keys button):hover {
    border-color: var(--edge);
  }
}

@media (forced-colors: active) {
  #why dt,
  #why .at,
  #why [data-idle] {
    color: GrayText;
  }
}

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

/* Forced colors put an opaque plate behind every run of text, and a letter's plate
   would blot out whoever it walks in front of. So the name opts out and takes the
   system's colors by hand. */
@media (forced-colors: active) {
  h1 {
    forced-color-adjust: none;
    color: CanvasText;
  }

  /* The system's colors are not ours to switch. */
  #lights {
    display: none;
  }

  /* Pressed in: the system's selection colors, taken by hand so that the label gets
     no plate of its own, inside a border that shows whatever those colors are. */
  button[aria-pressed="true"] {
    forced-color-adjust: none;
    border-color: CanvasText;
    background: SelectedItem;
    color: SelectedItemText;
  }

  .letter {
    -webkit-text-stroke-color: Canvas;
  }

  .eye {
    background: Canvas;
  }

  .leg {
    stroke: CanvasText;
  }

  .shoe {
    fill: CanvasText;
  }

  .casing,
  .sock,
  .shoe,
  .sole {
    stroke: Canvas;
  }
}

@media print {
  /* Paper is light, whatever the lights button was last told. */
  :root,
  :root[data-theme] {
    color-scheme: light;
  }

  .rig,
  #controls,
  #why,
  #guide {
    display: none;
  }

  /* The outline only tells overlapping letters apart on screen, and paper takes no
     backgrounds, so it would print as a gray rim. The transform is the one the script
     writes inline, hence !important. */
  .letter {
    -webkit-text-stroke: 0;
    transform: none !important;
  }
}
