:root {
  --bg: #121410;
  --ink: #e8e4da;
  --muted: #a39e92;
  --panel: #1b1e1a;
  --line: rgba(232, 228, 218, 0.12);
  --orange: #dd7e49;
  --blue: #56a8d6;
  --program: #9a1c1c;
  --f-shadow: #8c4c22;
  --g-shadow: #1b4e78;
  --lcd: #747f6e;
  --led-ink: #ff382e;
  --led-glow: rgb(255 56 46 / 48%);
  --face: #1e211c;
  --gutter: 52px;
  --bezel: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  min-height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(1200px 500px at 20% -10%, rgba(221, 126, 73, 0.08), transparent 50%),
    var(--bg);
  color: var(--ink);
  font-family: "Avenir Next", "Segoe UI", sans-serif;
}

a { color: var(--orange); }

footer p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.store img {
  display: block;
  height: 40px;
  width: auto;
}

.github {
  display: flex;
  color: var(--muted);
}

.github:hover { color: var(--ink); }

.github svg {
  display: block;
  width: 22px;
  height: 22px;
}

.stage {
  flex: 1;
  min-height: 0;
  width: 100%;
  padding: 32px var(--gutter) 40px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: var(--gutter);
  align-items: stretch;
  container-type: size;
  container-name: stage;
  /* Two cases stacked, plus the gap between them. The screen sits inside the bezel. */
  --face: min(calc((100cqh - 12px) / 2 - 2 * var(--bezel)), calc(100cqw - 460px));
}

.watch-column {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  min-width: 0;
  height: 100%;
  width: max-content;
  max-width: 100%;
  padding-left: 16px;
  box-sizing: border-box;
}

.watch-slot {
  min-height: 0;
  height: auto;
  padding: 0;
  display: flex;
  align-items: flex-start;
  box-sizing: border-box;
}

.watch-wrap {
  position: relative;
  width: calc(var(--face) + 2 * var(--bezel));
  height: auto;
  aspect-ratio: 1;
  box-sizing: border-box;
  padding: var(--bezel);
}

.watch {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--face);
  border-radius: 9%;
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.04),
    0 0 0 11px #2c2e2a,
    0 0 0 12px #151614,
    0 24px 50px rgba(0, 0, 0, 0.45);
  overflow: hidden;
  touch-action: none;
}

.crown {
  position: absolute;
  right: -22px;
  top: 27%;
  width: 16px;
  height: 24%;
  border-radius: 5px;
  cursor: ns-resize;
  touch-action: none;
  background:
    repeating-linear-gradient(
      to bottom,
      #8d9088 0 2px,
      #5c5f59 2px 5px,
      #3a3d38 5px 7px
    );
  box-shadow: 2px 0 0 #1a1b19, -1px 0 0 rgba(255, 255, 255, 0.25);
}

.face {
  position: absolute;
  display: flex;
  flex-direction: column;
}

.lcd {
  background: var(--lcd);
  border-radius: 4px;
  padding: 2px 3px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.lcd[data-view="x"] {
  padding-block: 3px;
  box-shadow: inset 0 0 0 0.75px #6c7667;
}

.lcd.led-display[data-view="x"] {
  box-shadow: inset 0 0 0 0.75px #4a191d;
}

.lcd-rows {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 1px;
  min-height: 0;
}

.lcd-row { display: flex; gap: 2px; height: var(--digit-h); flex: none; }

.digits { display: flex; flex: none; }

.cell {
  position: relative;
  width: var(--cell-w);
  height: var(--digit-h);
  flex: none;
}

.cell img.glyph {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% * 35 / 43);
  height: 100%;
}

.cell img.dot {
  position: absolute;
  width: calc(100% * 10 / 43);
  height: calc(100% * 8 / 53);
  left: calc(100% * 36 / 43 - 50% * 10 / 43);
  top: calc(100% * 49 / 53 - 50% * 8 / 53);
}

.annunciators {
  width: 13px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex: none;
}

.annunciators img {
  width: 12px;
  height: 25%;
  object-fit: contain;
}

.lcd-label {
  width: var(--label-w);
  flex: none;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  color: #282828;
  font-family: "ui-rounded", "Avenir Next", "Segoe UI", sans-serif;
  font-weight: 650;
  font-size: var(--label-size);
  line-height: 1;
}

.credit {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 4px;
  color: #282828;
  font-size: var(--credit-size);
  line-height: 1;
  height: var(--credit-h);
  padding: 0 5px;
  font-weight: 600;
  flex: none;
}

.credit-name {
  align-self: stretch;
  display: flex;
  align-items: center;
  padding-left: 10px;
  cursor: pointer;
}

.portrait {
  image-rendering: pixelated;
  flex: 1;
  width: 100%;
  min-height: 0;
  display: block;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(4, 1fr);
  min-height: 0;
  flex: none;
}

.key {
  position: relative;
  border: 0;
  padding: 0;
  color: #f5f4f1;
  background: linear-gradient(#4d4d4d, #292929 7%, #292929 64%, #1a1a1a 80%, #1a1a1a);
  border-radius: 18%;
  box-shadow: inset 0 0 0 0.5px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  font-family: "ui-rounded", "Avenir Next", "Segoe UI", sans-serif;
  overflow: hidden;
  container-type: size;
}

.key.f {
  color: #0d0d0d;
  background: linear-gradient(#eea277, #e17e49 8%, #e17e49 64%, #964e24 80%, #964e24);
}

.key.g {
  color: #0d0d0d;
  background: linear-gradient(#8cc4e6, #4592d0 8%, #4592d0 64%, #1c4e7a 80%, #1c4e7a);
}

.key.down { transform: translateY(1.5px) scale(0.95); }

.key.down::before,
.key.down::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
}

.key.down::before {
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.16) 48%, transparent 72%);
  mix-blend-mode: multiply;
}

.key.down::after {
  background: radial-gradient(circle at 50% 46%, rgba(255, 237, 199, 0.5), rgba(255, 219, 158, 0.14) 42%, transparent 75%);
  mix-blend-mode: plus-lighter;
}

.key-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 4% 6% 5%;
}

.key .title {
  margin: auto;
  font-size: 52cqh;
  font-weight: 500;
  line-height: 1;
}

.key.shift .title { font-size: 58cqh; font-weight: 650; }
.key.g .title { transform: translateY(-0.25em); }
.key.sign .title { font-size: 42cqh; }

.enter-word {
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-weight: 650;
  font-size: 28cqw;
  letter-spacing: 0.04em;
  line-height: 0.92;
}

.enter-exponent {
  position: absolute;
  left: 23%;
  top: 46%;
  transform: translate(-50%, -50%) rotate(-90deg);
  color: #858585;
  font-size: 16cqw;
  font-weight: 600;
  letter-spacing: 0.06em;
  line-height: 1;
}

.legend {
  display: flex;
  justify-content: center;
  width: 100%;
  font-weight: 700;
  font-size: 24cqw;
  line-height: 1;
  gap: 0.15em;
  white-space: nowrap;
  padding-bottom: 4cqh;
}

.legend.leading, .legend.trailing { font-size: 18cqw; }
.legend.leading { justify-content: flex-start; padding-left: 8cqw; }
.legend.trailing { justify-content: flex-end; padding-right: 8cqw; }
.legend.tight { gap: 0; font-size: 22cqw; }
/* x↔y and over run past the key. A slight sideways squeeze keeps the height. */
.legend.swap { transform: scaleX(0.86); }
.legend .g-lab { color: var(--blue); }
.legend .f-lab { color: var(--orange); }
.legend .f-lab.program { color: var(--program); }
.cue {
  display: inline-flex;
  align-items: flex-end;
  align-self: flex-end;
  justify-content: center;
  gap: 0.18em;
  height: 0.78em;
  transform: translateY(-0.015em);
}
.cue svg { height: 100%; width: auto; display: block; }

.hint { margin-top: 16px; font-size: 0.92rem; }
kbd {
  font-family: inherit;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3em;
  color: var(--ink);
}

.panel, .problem, .group {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
}

#state {
  display: flex;
  flex-direction: column;
  align-self: start;
  width: calc(var(--face) + 2 * var(--bezel));
  aspect-ratio: 1;
  height: auto;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  overflow-anchor: none;
}

.panel {
  width: 100%;
  box-sizing: border-box;
}

.state-card {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}
.state-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
.state-tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
}
.state-tab {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--muted);
  font: 650 0.62rem/1 "Avenir Next", "Segoe UI", sans-serif;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 7px 0 6px;
  cursor: pointer;
}
.state-tab[aria-selected="true"] {
  color: var(--ink);
  box-shadow: inset 0 -1.5px 0 var(--ink);
}
.state-tab-live {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--program);
}
.state-card .stack-readout {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  width: auto;
  max-width: none;
  margin: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.state-card .macros { padding: 10px 14px 2px; }

.stack-readout {
  background: #10120f;
  border-radius: 10px;
  padding: 8px 10px;
  width: 100%;
  box-sizing: border-box;
}

.stack-readout .lcd-rows {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
}
.stack-readout .lcd-label { color: #8d9486; }

.panel h2, .group h2 {
  margin: 0 0 10px;
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 650;
}

.macros {
  display: grid;
  grid-template-columns: 1.8rem max-content minmax(0, 1fr);
  column-gap: 10px;
  row-gap: 8px;
  align-items: center;
}
.macro {
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
  align-items: center;
}
.macro-value {
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink);
  text-align: center;
  white-space: nowrap;
}
.macro-head {
  color: var(--muted);
  font: 650 0.62rem/1 "Avenir Next", "Segoe UI", sans-serif;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.macro-head-value { text-align: center; }
.macro-head-program,
.keys { justify-self: start; text-align: left; }
.macro-name {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--program);
  width: 1.55rem;
  height: 1.35rem;
  display: grid;
  place-items: center;
  border-radius: 999px;
  justify-self: center;
}
.macro-name.live {
  background: var(--program);
  color: #0d0d0d;
}
.keys { display: flex; flex-wrap: wrap; gap: 4px; min-height: 1.6rem; }
.empty { color: var(--muted); font-size: 0.82rem; }

.chip, .script button.chip {
  border: 0;
  border-radius: 6px;
  background: #2a2a2a;
  color: #f3f1ea;
  font: 600 0.75rem/1 "ui-rounded", "Avenir Next", sans-serif;
  padding: 4px 6px;
}
.chip.fn { display: inline-flex; align-items: baseline; gap: 0.2em; background: #242424; }
.chip.fn.tight { gap: 0; }
.chip .part.f { color: var(--orange); }
.chip .part.g { color: var(--blue); }
.chip .part.program { color: #e24b4b; }
.chip.mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #242424;
}
.chip.mark .iterate {
  height: 0.9em;
  width: auto;
  display: block;
}
.chip.mark .iterate path { fill: var(--blue); }
.chip.tight .part + .part { margin-left: 0; }
.chip.now { outline: 2px solid #fff; outline-offset: 1px; }

.group { padding: 16px; }
.group + .group { margin-top: 0; }
.problems {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  padding-bottom: 8px;
}
.group, .problem { min-width: 0; max-width: 100%; }
.problem {
  padding: 12px;
  margin-top: 10px;
  background: #141613;
  opacity: 1;
  transition: opacity 120ms ease;
}
@media (hover: hover) and (pointer: fine) {
  .problem { opacity: 0.34; }
  .problem:hover,
  .problem:focus-within,
  .problem.selected {
    opacity: 1;
  }
}
.problem h3 { margin: 0 0 6px; font-size: 1rem; font-weight: 650; }
.expr { font-size: 1.15rem; line-height: 1.35; overflow-x: auto; }
.expr .katex { color: var(--ink); }
.expr .katex .done { color: #e24b4b; }
.expr .katex .done .frac-line,
.expr .katex .done .overline-line { border-color: #e24b4b; }
.expr .katex-display { margin: 0.3em 0; text-align: left; }
.expr .katex-display > .katex { text-align: left; }
.note { margin: 0 0 8px; color: var(--muted); font-size: 0.92rem; }
.answer { margin: 8px 0; font-variant-numeric: tabular-nums; }
.answer strong { font-family: "Iowan Old Style", Palatino, Georgia, serif; font-size: 1.25rem; font-weight: 500; }
.script { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
.actions { display: flex; gap: 8px; }
.actions button {
  border: 1px solid var(--line);
  background: #262822;
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 12px;
  font: 600 0.85rem/1 "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}
.actions button.primary { background: var(--orange); color: #1a120c; border-color: transparent; }
.problem.selected { border-color: rgba(221, 126, 73, 0.7); }

footer {
  flex: none;
  width: 100%;
  padding: 8px var(--gutter) 14px;
  display: flex;
  align-items: center;
  gap: 16px;
}

@media (max-width: 820px) {
  body {
    height: auto;
    min-height: 100dvh;
    overflow-x: clip;
    overflow-y: auto;
  }
  .stage {
    container-type: normal;
    flex: none;
    display: flex;
    flex-direction: column;
    gap: 20px;
    overflow: visible;
    padding: 20px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  }
  .watch-column {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    max-width: 460px;
    height: auto;
    margin-inline: auto;
    min-width: 0;
    padding-left: 0;
  }
  .watch-slot {
    height: auto;
    width: 100%;
    display: flex;
    justify-content: flex-start;
    padding: 0 22px 0 0;
  }
  .watch-wrap {
    width: 100%;
    height: auto;
    aspect-ratio: 1;
  }
  #state {
    width: calc(100% - 22px);
    aspect-ratio: 1;
    height: auto;
    max-height: none;
    overflow: hidden;
  }
  .problems { overflow: visible; }
  .problem,
  .problem:hover,
  .problem:focus-within,
  .problem.selected { opacity: 1; }
  footer {
    flex-wrap: wrap;
    row-gap: 10px;
    padding: 12px max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  }
}

/* Watch over the stack until two squares no longer fit. A shorter window,
   including a phone turned sideways, puts them side by side. */
@media (max-height: 640px) and (min-width: 560px) {
  body {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
  }
  .stage {
    container-type: size;
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    gap: 20px 28px;
    overflow: hidden;
    align-items: stretch;
    padding: 16px max(16px, env(safe-area-inset-right)) 20px max(16px, env(safe-area-inset-left));
    /* One case each, as tall as the row can be while problems keep a strip. */
    --face: min(calc(100cqh - 160px - 2 * var(--bezel)), calc((100cqw - 120px) / 2 - 2 * var(--bezel)));
  }
  .watch-column {
    display: contents;
  }
  .watch-slot {
    grid-column: 1;
    grid-row: 1;
    height: auto;
    width: auto;
    max-width: none;
    margin: 0 0 0 16px;
    min-height: 0;
    align-self: start;
    justify-content: flex-start;
    padding: 0;
  }
  .watch-wrap,
  #state {
    width: calc(var(--face) + 2 * var(--bezel));
    height: auto;
    aspect-ratio: 1;
    max-width: none;
    max-height: none;
  }
  #state {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    margin: 0;
    overflow: hidden;
  }
  .problems {
    grid-column: 1 / -1;
    grid-row: 2;
    align-self: stretch;
    height: auto;
    min-height: 0;
    overflow: auto;
  }
  .problem,
  .problem:hover,
  .problem:focus-within,
  .problem.selected { opacity: 1; }
  footer {
    flex-wrap: wrap;
    row-gap: 8px;
    padding: 6px max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
}

.lcd.led-display {
  background: #301013;
}

.led-display .lcd-label,
.led-display .credit {
  color: var(--led-ink);
  text-shadow: 0 0 0.21em var(--led-glow);
}

.credit-title {
  align-self: stretch;
  display: flex;
  flex: 1;
  align-items: center;
  gap: 0.5em;
  padding: 0 6px 0 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-shadow: inherit;
  text-align: left;
  cursor: pointer;
}

.keypad .key { grid-column: var(--right-column); }
.keypad.left-handed .key { grid-column: var(--left-column); }
.keypad.left-handed .legend.leading { justify-content: flex-end; padding: 0 8cqw 4cqh 0; }
.keypad.left-handed .legend.trailing { justify-content: flex-start; padding: 0 0 4cqh 8cqw; }

.settings-gear {
  font-size: 1.65em;
  line-height: 1;
}

.display-settings {
  display: grid;
  grid-template-rows: auto repeat(3, minmax(0, 1fr));
  gap: 5%;
  height: 100%;
  padding: 8%;
  color: #282828;
  font: 650 clamp(10px, calc(var(--cell-w) * 0.46), 21px) system-ui, sans-serif;
}

.led-display .display-settings {
  color: var(--led-ink);
  text-shadow: 0 0 0.21em var(--led-glow);
}

.led-display .display-settings button {
  text-shadow: inherit;
  box-shadow: 0 0 0.21em var(--led-glow);
}
.settings-heading { display: flex; justify-content: space-between; align-items: center; gap: 5px; }
.settings-heading h2 { font: inherit; font-size: 1.25em; margin: 0; }
.display-settings button {
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 4px;
  cursor: pointer;
}
.settings-back { padding: 0.3em 0.6em; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 4px; padding: 0 0.5em; white-space: nowrap; }
.setting-value { font-weight: 800; text-align: right; }
