:root {
  color-scheme: light;
  --page: #f9f9f9;
  --editor: #ffffff;
  --line: #d2d2d2;
  --line-soft: #e0e0e0;
  --ink: #040403;
  --muted: #505050;
  --accent: #1f6ae2;
  --selection: #3b9ee53d;
  --element: #f0f0f0;
  --element-hover: #e0e0e0;
  --element-active: #d0d0d0;
  --error: #bf4133;
  --error-background: #fbeae5;
  --error-border: #ec9f89;

  /* Syntax colors, also used for the kinds of node. */
  --keyword: #34589d;
  --type: #b7612b;
  --function: #5847b9;
  --string: #5f803a;
  --number: #bf4133;
  --comment: #8c8982;
  --operator: #5e5c55;
  --punctuation: #716f69;

  --lam: var(--keyword);
  --app: var(--ink);
  --fan: var(--type);
  --delim: var(--string);
  --wire: var(--comment);
  --next: var(--function);

  --mono: Monaco, Menlo, monospace;
}

* {
  box-sizing: border-box;
}

/* An element that sets its own `display` would otherwise show while hidden. */
[hidden] {
  display: none !important;
}

::selection {
  background: var(--selection);
}

/* Sizes are in rem, so the page follows the browser's default font size. The
   page fills the window: the canvas takes whatever height the rest leaves. */
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 1rem/1.5 var(--mono);
}

header {
  display: flex;
  align-items: baseline;
  gap: 20px;
  flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--editor);
  border-bottom: 1px solid var(--line);
}

header h1 {
  margin: 0;
  font: 1.5rem/1 var(--mono);
  color: var(--keyword);
}

header p {
  margin: 0;
  color: var(--muted);
}

main {
  flex: 1 0 auto;
  display: grid;
  grid-template-columns: minmax(17rem, min(26rem, 30%)) minmax(0, 1fr);
  gap: 16px;
  padding: 16px 20px;
}

/* One column: the page scrolls, and the canvas takes most of a screen. */
@media (max-width: 860px) {
  main {
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
  }
}

section {
  background: var(--editor);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 16px;
}

.source {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-self: start;
}

label,
h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: normal;
  color: var(--muted);
}

select,
textarea,
button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  border-radius: 4px;
}

select {
  padding: 5px 8px;
  background: var(--editor);
}

select:focus-visible,
textarea:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 1px solid var(--accent);
  outline-offset: -1px;
}

/* The script is a transparent textarea over a copy of its text that shows
   the syntax colors. Both must lay the text out exactly alike. */
.editor {
  position: relative;
}

.editor textarea,
.editor pre {
  display: block;
  width: 100%;
  margin: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: 1rem/1.5 var(--mono);
  letter-spacing: 0;
  tab-size: 2;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  scrollbar-gutter: stable;
}

.editor textarea {
  position: relative;
  min-height: 16em;
  resize: vertical;
  overflow-y: auto;
  background: transparent;
  color: transparent;
  caret-color: var(--ink);
}

.editor pre {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-color: transparent;
  background: var(--editor);
  color: var(--ink);
  pointer-events: none;
}

.hint {
  margin: 0;
  color: var(--muted);
}

code {
  font-family: var(--mono);
  color: var(--ink);
}

button {
  padding: 5px 12px;
  background: var(--element);
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: var(--element-hover);
}

button:active:not(:disabled) {
  background: var(--element-active);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

button.primary:hover:not(:disabled),
button.primary:active:not(:disabled) {
  background: #1a5ac2;
}

input[type="range"] {
  accent-color: var(--accent);
}

.problem {
  margin: 0;
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid var(--error-border);
  background: var(--error-background);
  color: var(--error);
  white-space: pre-wrap;
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
  container-type: inline-size;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.speed {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.canvas {
  position: relative;
  flex: 1 1 0;
  min-height: max(320px, 50vh);
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  background: var(--editor);
  overflow: hidden;
}

@media (max-width: 860px) {
  .canvas {
    flex: none;
    height: 75vh;
  }
}

.canvas svg {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}

.canvas svg.dragging {
  cursor: grabbing;
}

.oversize {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.wire {
  fill: none;
  stroke: var(--wire);
  stroke-width: 1.4;
}

.wire.back {
  stroke-dasharray: 5 4;
  opacity: 0.8;
}

.wire.next {
  stroke: var(--next);
  stroke-width: 3;
}

.port {
  fill: var(--punctuation);
}

.port.next {
  fill: var(--next);
}

.node .shape {
  fill: var(--editor);
  stroke-width: 1.8;
}

.node text {
  font: 13px/1 var(--mono);
  fill: var(--ink);
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.node.lam .shape {
  stroke: var(--lam);
}

.node.lam text {
  fill: var(--lam);
}

.node.app .shape {
  stroke: var(--app);
}

.node.fan .shape {
  stroke: var(--fan);
}

.node.delim .shape {
  stroke: var(--delim);
}

.node.fan text,
.node.delim text {
  font-size: 11px;
}

.node.fan text {
  fill: var(--fan);
}

.node.delim text {
  fill: var(--delim);
}

.node.delim text.height {
  font-size: 10px;
  text-anchor: start;
}

.node.eraser .shape,
.node.free .shape {
  stroke: var(--wire);
}

.node.eraser text {
  font-size: 10px;
  fill: var(--muted);
}

.node.root .shape {
  fill: var(--ink);
  stroke: none;
}

.node.next .shape {
  stroke: var(--next);
  stroke-width: 3;
}

.readout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr) auto;
  gap: 20px;
  align-items: start;
}

/* Three lines each, scrolling beyond: if they grew with their text, the
   canvas would change size, and the net with it, from one step to the next. */
.readout pre,
.readout p {
  height: 4.5em;
  margin: 4px 0 0;
  overflow: auto;
}

.readout pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 1rem/1.5 var(--mono);
}

.next strong {
  font-weight: normal;
  color: var(--next);
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 8px 24px;
  margin: 0;
}

.stats dt {
  color: var(--muted);
}

.stats dd {
  margin: 0;
  font: 1.25rem/1.2 var(--mono);
  color: var(--number);
  font-variant-numeric: tabular-nums;
}

/* Narrower, the counts move under the text, and then the text stacks. */
@container (max-width: 60em) {
  .readout {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr);
  }

  .stats {
    grid-column: 1 / -1;
    grid-template-columns: repeat(4, auto);
    justify-content: start;
  }
}

@container (max-width: 44em) {
  .readout {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* In one column the canvas has a height of its own, so the text can grow. */
@media (max-width: 860px) {
  .readout pre,
  .readout p {
    height: auto;
  }

  .readout pre {
    max-height: 9em;
  }
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 20px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  flex: none;
  width: 1.2em;
  height: 1.2em;
  fill: var(--editor);
  stroke: var(--wire);
  stroke-width: 2;
  stroke-linejoin: round;
}

/* The stroke keeps its width in pixels however small the shape is drawn. */
.swatch * {
  vector-effect: non-scaling-stroke;
}

.swatch.lam {
  stroke: var(--lam);
}

.swatch.app {
  stroke: var(--app);
}

.swatch.fan {
  stroke: var(--fan);
}

.swatch.delim {
  stroke: var(--delim);
}

.swatch.port {
  fill: var(--punctuation);
  stroke: none;
}

/* Tokens of a script or a term, colored as the editor colors code. */
.t-keyword {
  color: var(--keyword);
}

.t-definition {
  color: var(--function);
}

.t-number {
  color: var(--number);
}

.t-comment {
  color: var(--comment);
}

.t-operator {
  color: var(--operator);
}

.t-punctuation {
  color: var(--punctuation);
}
