/* Limit-shape lab styles; shared controls and the number pill from ../play-ui.css.
 * Wide screens: the size of the pieces at the top, the other labs and the
 * language at the top right, the pieces on the left, the loop in the middle,
 * the loupe on the right, the brand under the loop. Phones: the same order,
 * stacked. */
:root {
  --bg: #0d1117;
  --text: #d8f6ff;
  --muted: #7ea8bf;
  --line: #2d3a4e;
  --accent: #00d1ff;
  --area-accent: #2ff3ff;
  --loop: #2ff3ff;
  --circle: rgba(216, 246, 255, 0.42);
  --gold: #ffd43b;
  --focus: rgba(255, 255, 255, 0.7);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background:
    radial-gradient(900px 420px at 10% -10%, rgba(63, 184, 255, 0.14), transparent 60%),
    radial-gradient(700px 440px at 100% -8%, rgba(46, 207, 153, 0.12), transparent 64%),
    var(--bg);
  color: var(--text);
  font-family: "Manrope", "Avenir Next", "Segoe UI", sans-serif;
}
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* ---------- the grid ---------- */
.lab {
  height: 100dvh;
  display: grid;
  grid-template-columns: 252px minmax(0, 1fr) auto auto auto 56px;
  grid-template-rows: 64px minmax(0, 1fr) 56px;
  grid-template-areas:
    ". read read nav lang lang"
    "dock stage stage stage stage strip"
    "dock bar bar bar bar strip";
  gap: 8px 14px;
  padding: max(8px, env(safe-area-inset-top)) 14px max(8px, env(safe-area-inset-bottom));
}
.readout { grid-area: read; display: flex; flex: none; align-items: center; justify-content: center; gap: 10px; min-width: 0; }
.topnav { grid-area: nav; display: flex; align-items: center; gap: 8px; }
.langSel { grid-area: lang; align-self: center; justify-self: end; }
/* The chips' touch boxes reach 11 px past their row: the gap keeps them off the pieces' canvas. */
.dock { grid-area: dock; min-height: 0; display: flex; flex-direction: column; gap: 12px; }
.stage {
  grid-area: stage; position: relative; min-height: 0;
  border: 1px solid var(--line); border-radius: 18px; overflow: hidden;
  background: #0f151f;
}
.strip { grid-area: strip; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; }
.bar { grid-area: bar; display: flex; align-items: center; justify-content: center; gap: 8px; }
.side-brand { flex: 1 1 auto; min-width: 0; }

/* Between phones and wide screens the top row also takes the dock's empty corner, so that the
 * steps and the gap keep their room (app.js still centres them over the loop where it can). */
@media (min-width: 861px) and (max-width: 1100px) {
  .lab {
    grid-template-areas:
      "read read read nav lang lang"
      "dock stage stage stage stage strip"
      "dock bar bar bar bar strip";
  }
}

#loop { display: block; width: 100%; height: 100%; cursor: default; }
.stage:has(#loop:focus-visible) { box-shadow: inset 0 0 0 2px var(--focus); }
#loop:focus-visible { outline: none; }

/* ---------- language switch (the labs' own) ---------- */
.langSel {
  display: flex; border-radius: 999px;
  background: rgba(7, 6, 10, 0.82); backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.langSel button {
  cursor: pointer; border: none; color: rgba(255, 255, 255, 0.55);
  font-size: 11px; font-weight: 700; letter-spacing: 0.04em; border-radius: 999px;
}
.langSel button:hover { color: #fff; }
.langSel button[aria-pressed="true"] { color: #07060a; }
/* With a card open the switch stays above the scrim but shares the card's layer, which comes later:
 * a card tall enough to reach the top right keeps its close button in front of the switch. */
body:has(.math-pop:not([hidden])) .lab > .langSel { z-index: 10002; }

/* ---------- the size of the pieces and the gap ---------- */
.measure { cursor: default; }
.measure .area-word { font-style: italic; font-family: "Times New Roman", serif; font-size: 18px; text-transform: none !important; opacity: 0.85; }
.ctl.step .g-skip { display: none; }
.ctl.step.skip .g-step { display: none; }
.ctl.step.skip .g-skip { display: block; }
.measure.bump { animation: bump 0.45s ease; }
@keyframes bump { 40% { transform: scale(1.07); } }
.ctl[aria-disabled="true"] { opacity: 0.32; filter: grayscale(0.6); cursor: not-allowed; }
.tok {
  display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 14px;
  border-radius: 999px; border: 1.5px solid rgba(216, 246, 255, 0.28);
  background: rgba(216, 246, 255, 0.05);
  font-variant-numeric: tabular-nums; font-size: 18px; white-space: nowrap; cursor: default;
}
.tok strong { font-weight: 800; }
.gapIcon { width: 22px; height: 22px; color: var(--circle); }
.topMid { grid-area: read; display: flex; align-items: center; justify-content: center; gap: 18px; min-width: 0; }
.topMid .readout { grid-area: auto; }
.gapBox { display: flex; align-items: center; }

/* ---------- math chips: only what the state needs, beside what they
 * explain with a gap (the shared math-chip.css draws them) ---------- */
.chipHost { position: relative; display: inline-flex; align-items: center; gap: 8px; }
/* The polygon's chip sits in a corner of the stage, over the drawing. */
.chipHost.corner { position: absolute; top: 10px; right: 10px; }
/* At 500 cells, the top left of the stage names the paper's sample behind the loop. */
.tok.srcTag { position: absolute; top: 10px; left: 10px; height: 30px; padding: 0 10px; gap: 6px; font-size: 13px; color: var(--muted); }
.tok.srcTag[hidden] { display: none; }
.srcTag strong { color: var(--text); }
.srcIcon { width: 17px; height: 17px; flex: 0 0 auto; }
/* From 4 to 15 cells the same corner names the fence (optimal, then greedy); on a small stage the polygon leaves
 * the corner no room for words, and the icon stands alone (app.js, fitTag). */
.tok.srcTag.short { aspect-ratio: 1; padding: 0; justify-content: center; }
.srcTag.short strong { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* Phones: both tags a little smaller and nearer the corner, where the loop leaves more room. */
@media (max-width: 860px), (max-height: 520px) and (orientation: landscape) {
  .tok.srcTag { top: 8px; left: 8px; height: 28px; padding: 0 9px; gap: 5px; font-size: 12.5px; }
}

/* ---------- the pieces ---------- */
.pieceBox {
  position: relative; flex: 1 1 auto; min-height: 0; border-radius: 18px;
}
#pieces { display: block; width: 100%; height: 100%; cursor: pointer; }
.pieceBox:has(#pieces:focus-visible) { box-shadow: inset 0 0 0 2px var(--focus); }
#pieces:focus-visible { outline: none; }
.lab[data-n="500"] #pieces { cursor: default; }
/* A fence's arrows: a finger slides over the dots instead of moving the page (app.js). */
#pieces.scrub { touch-action: none; }
/* The caption and its chips on one line under the pieces, on every screen. */
.capRow { display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 24px; }
.chipHost.inline { display: inline-flex; gap: 16px; min-height: 24px; }
.caption {
  margin: 0; min-height: 18px; text-align: center; white-space: nowrap;
  font: 700 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.04em; color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* ---------- the loupe ---------- */
.slider {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 4px; width: 48px; border-radius: 24px;
  background: rgba(7, 6, 10, 0.55); border: 1px solid rgba(255, 255, 255, 0.12);
  color: rgba(216, 246, 255, 0.75);
  height: min(320px, 100%);
}
.slider svg { width: 20px; height: 20px; flex: 0 0 auto; }
.slider input {
  writing-mode: vertical-lr; direction: rtl;
  width: 28px; flex: 1 1 auto; min-height: 0; margin: 0;
  cursor: pointer;
}
/* The slider drawn by the lab, so that every browser shows the same track and thumb, standing or
 * lying: block-size is the track's thickness and margin-block-start centres the thumb on it, in
 * either writing mode. */
.slider input { -webkit-appearance: none; appearance: none; background: transparent; }
.slider input::-webkit-slider-runnable-track { block-size: 6px; border-radius: 3px; background: rgba(216, 246, 255, 0.18); }
.slider input::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 22px; height: 22px; margin-block-start: -8px; border-radius: 50%; border: 0; background: var(--loop); box-shadow: 0 0 10px var(--loop); }
.slider input::-moz-range-track { block-size: 6px; border-radius: 3px; background: rgba(216, 246, 255, 0.18); }
.slider input::-moz-range-progress { block-size: 6px; border-radius: 3px; background: var(--loop); }
.slider input::-moz-range-thumb { width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--loop); }
.loupeX { font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--muted); font-variant-numeric: tabular-nums; }

.f { white-space: nowrap; }

/* ---------- phones and narrow screens ---------- */
@media (max-width: 860px) {
  .lab {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 52px 48px minmax(0, 1fr) 44px minmax(88px, 26dvh) 36px;
    grid-template-areas:
      "read lang"
      "gap nav"
      "stage stage"
      "strip strip"
      "dock dock"
      "bar bar";
    gap: 6px 8px; padding: max(6px, env(safe-area-inset-top)) 8px max(6px, env(safe-area-inset-bottom));
  }
  /* The loop's own square, the rest for the pieces. */
  .stage { aspect-ratio: 1 / 1; height: 100%; width: auto; max-width: 100%; justify-self: center; }
  .topMid { display: contents; }
  .topMid .readout { grid-area: read; }
  .gapBox { grid-area: gap; }
  .readout { justify-content: flex-start; gap: 6px; transform: none !important; }
  .measure { --area-num: 28px; padding: 0 14px 0 12px; }
  .tok { height: 40px; font-size: 15px; padding: 0 12px; }
  .topnav { justify-content: flex-end; }
  .strip { flex-direction: row; }
  .slider { flex-direction: row; width: 100%; height: 44px; padding: 0 12px; }
  .slider input { writing-mode: horizontal-tb; direction: ltr; width: 100%; height: 28px; flex: 1 1 auto; }
  .side-brand { font-size: 0.66rem; padding: 0 6px; }
}
/* Phones held sideways: the loop on the left at full height, the rest on the right. */
@media (max-height: 520px) and (orientation: landscape) {
  .lab {
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: 48px 44px 44px minmax(0, 1fr) 28px;
    grid-template-areas:
      "stage read lang"
      "stage gap nav"
      "stage strip strip"
      "stage dock dock"
      "stage bar bar";
    gap: 6px 10px;
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }
  /* The loop's square, sized both ways (WebKit gives an auto column no width from the aspect ratio
   * alone), and never so wide that the controls on the right get less than 300 px. */
  .stage {
    --side: min(calc(100dvh - 12px), calc(100vw - 310px - max(10px, env(safe-area-inset-left)) - max(10px, env(safe-area-inset-right))));
    width: var(--side); height: var(--side); max-height: none; align-self: center;
  }
  .topMid { display: contents; }
  .topMid .readout { grid-area: read; }
  .gapBox { grid-area: gap; }
  .readout { justify-content: flex-start; gap: 6px; transform: none !important; }
  .measure { --area-num: 24px; height: 40px; padding: 0 12px; }
  .tok { height: 36px; font-size: 14px; }
  .topnav { justify-content: flex-end; }
  .lab-nav { gap: 6px; }
  .strip { flex-direction: row; }
  .slider { flex-direction: row; width: 100%; height: 40px; padding: 0 12px; }
  .slider input { writing-mode: horizontal-tb; direction: ltr; width: 100%; height: 28px; flex: 1 1 auto; }
  .side-brand { font-size: 0.66rem; }
}
/* Narrow phones, and phones held sideways whose right column is narrow: the steps and the language
 * switch draw closer, so that the switch never covers + (at 500 the pill is widest) and the gap's
 * chip keeps clear of the home. */
@media (max-width: 389px), (max-height: 520px) and (orientation: landscape) and (max-aspect-ratio: 41/20) {
  .langSel { gap: 0; padding: 0 2px; }
  .readout { gap: 2px; }
  .lab-nav { gap: 4px; }
}
/* Narrower still: the controls of the two top rows also get a little smaller (touch boxes 32 to 40 px). */
@media (max-width: 359px), (max-height: 520px) and (orientation: landscape) and (max-aspect-ratio: 41/20) {
  .langSel button { min-width: 32px; }
  .lab-nav { gap: 2px; }
  .ctl.nav { width: 38px; }
  .readout .ctl.step { width: 40px; }
  .tok { padding: 0 8px; gap: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  .measure.bump { animation: none; }
}
.caption { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 16ch; }
.caption .dice { width: 16px; height: 16px; flex: 0 0 auto; }

/* The two tokens of the stage open the result of the paper they show (paper-ref.js). */
.tok.srcTag[data-ref] { cursor: pointer; -webkit-tap-highlight-color: transparent; transition: border-color 0.15s ease, background 0.15s ease; }
.tok.srcTag[data-ref]:hover { border-color: rgba(255, 211, 110, 0.7); background: rgba(255, 211, 110, 0.1); }
.tok.srcTag[data-ref]:focus-visible { outline: 2px solid #ffd36e; outline-offset: 2px; }
/* A finger needs 44 px: the token's touch box reaches beyond its edge (nothing else sits there). */
.tok.srcTag[data-ref]::after { content: ""; position: absolute; inset: -8px; }
