@layer player {
  /* ---- top bar ------------------------------------------------------------------ */
  body { container: page / inline-size; }
  .bar {
    display: flex; align-items: center; gap: var(--gap);
    padding: 0.45rem clamp(0.75rem, 2cqi, 1.5rem);
    border-block-end: 0.07rem solid var(--rule);
    background: var(--paper);
    position: sticky; inset-block-start: 0; z-index: 3;
  }
  .brand { color: var(--ink); text-decoration: none; font-family: var(--font-title); font-weight: 900; font-size: 1.125rem; white-space: nowrap; }
  .trip { flex: 1; min-inline-size: 0; display: flex; align-items: center; gap: 0.75rem; }
  .trip-text { color: var(--ink-2); font-size: 0.875rem; white-space: nowrap; }
  .trip-short { display: none; font-family: var(--font-num); }
  .trip-road { flex: 1; min-inline-size: 3rem; position: relative; --p: 0; }
  @container page (inline-size < 44rem) {
    .brand, .trip-long { display: none; }
    .trip-short { display: inline; }
  }

  progress {
    appearance: none; border: 0 solid; overflow: clip; display: block;
    inline-size: 100%; block-size: 0.5rem; border-radius: 0.25rem;
    background: var(--asphalt);
  }
  progress::-webkit-progress-bar { background: transparent; }
  progress::-webkit-progress-value { background: var(--signal); transition: inline-size 0.3s; }
  progress::-moz-progress-bar { background: var(--signal); transition: inline-size 0.3s; }

  /* ---- the frame: chapter list, stage, story --------------------------------------- */
  .shell { display: grid; grid-template-columns: minmax(0, 1fr); min-block-size: 0; inline-size: 100%; max-inline-size: 112rem; margin-inline: auto; }
  .player {
    display: grid; gap: clamp(0.75rem, 2cqi, 1.5rem);
    padding: clamp(0.75rem, 2cqi, 1.25rem);
    grid-template-columns: minmax(0, 1fr); align-content: start; min-block-size: 0;
  }
  .stage { position: relative; }
  .story { min-inline-size: 0; }
  .chapter-line { color: var(--ink-2); font-size: 0.9375rem; }
  #step-title { font-size: clamp(1.5rem, 1.1rem + 1.6cqi, 2.2rem); margin-block: 0.25rem 1rem; }
  #step-title:focus-visible { outline-offset: 0.3rem; }

  /* One column (phones, narrow windows): the drawing stays in view while the text scrolls under it. */
  @media (max-width: 61.99rem) {
    .stage { position: sticky; inset-block-start: 6.2rem; z-index: 1; max-block-size: 52dvh; overflow-y: auto; }
    .stage .canvas { max-block-size: 26dvh; }
    .stage table.dist th, .stage table.dist td { block-size: 1.9rem; font-size: 1.0625rem; }
    .stage .board-row { font-size: 0.875rem; }
  }

  /* Wide windows: the page is a fixed frame.  Nothing scrolls except the story and the chapter list. */
  @media (min-width: 62rem) {
    body { block-size: 100dvh; overflow: hidden; }
    .shell { grid-template-rows: auto minmax(0, 1fr); }
    .player { grid-template-columns: minmax(0, 1.3fr) minmax(22rem, 1fr); align-content: stretch; }
    .stage { overflow: auto; align-content: center; align-content: safe center; justify-items: center; }   /* safe: a tall drawing starts at the top, so its top can be scrolled to */
    .stage > * { inline-size: 100%; }
    .stage > .odometer { inline-size: auto; }
    .story { overflow-y: auto; padding-inline-end: 0.5rem; max-inline-size: 38em; scrollbar-gutter: stable; }
  }
  @media (min-width: 82rem) {
    .shell { grid-template-columns: 15.5rem minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
  }

  /* ---- chapter list: a route with numbered stops -------------------------------------- */
  .rail { min-inline-size: 0; background: var(--paper); }
  .rail ol { list-style: none; margin: 0; padding: 0; }
  .stop-link { color: inherit; text-decoration: none; }
  .stop-no {
    display: inline-grid; place-items: center; inline-size: 1.9rem; block-size: 1.45rem; flex: none;
    border: 0.12rem solid var(--ink); border-radius: 0.3rem; background: var(--sheet);
    font-family: var(--font-num); font-weight: 700; font-size: 0.9375rem; line-height: 1;
  }
  .stop.is-done .stop-no { background: var(--ink); color: var(--sheet); }
  .stop.is-current .stop-no { background: var(--signal); color: var(--on-signal); }
  .stop.is-later { color: var(--ink-3); }
  .stop.is-later .stop-no { border-color: var(--ink-3); border-style: dashed; }
  .stop-steps a { color: var(--ink-2); text-decoration: none; }
  .stop-steps a[aria-current="step"] { background: var(--wash); color: var(--ink); font-weight: 700; }

  /* narrow and medium: one scrolling row of stops; only the current one shows its name */
  @media (max-width: 81.99rem) {
    .rail { position: sticky; inset-block-start: 3.1rem; z-index: 2; border-block-end: 0.07rem solid var(--rule); }
    #rail { display: flex; gap: 0.35rem; overflow-x: auto; padding: 0.4rem clamp(0.75rem, 2cqi, 1.25rem); scrollbar-width: thin; }
    .stop { flex: none; }
    .stop-link { display: flex; align-items: center; gap: 0.4rem; padding: 0.15rem 0.3rem; border-radius: 0.4rem; min-block-size: 2rem; }
    .stop:not(.is-current) .stop-title { display: none; }
    .stop-title { font-size: 0.9375rem; font-weight: 700; white-space: nowrap; }
    .rail .stop-steps, .stop-later { display: none; }
  }
  @media (min-width: 62rem) and (max-width: 81.99rem) { .rail { position: static; } }

  /* wide: a column, with the steps of the current chapter listed under it */
  @media (min-width: 82rem) {
    .rail { overflow-y: auto; border-inline-end: 0.07rem solid var(--rule); padding: 1rem 0.6rem 1rem 0.9rem; scrollbar-width: thin; }
    /* Every chapter stays on screen; a long chapter scrolls its own list of steps. */
    #rail { display: flex; flex-direction: column; max-block-size: 100%; }
    .stop { position: relative; padding-block-end: 0.55rem; flex: none; }
    .stop.is-current { flex: 0 1 auto; min-block-size: 5.5rem; display: flex; flex-direction: column; }
    /* the road that joins the stops */
    .stop::before { content: ""; position: absolute; inset-inline-start: 0.8rem; inset-block: 1.45rem 0; inline-size: 0.3rem; background: var(--asphalt); }
    .stop:last-child::before { display: none; }
    .stop-link { display: grid; grid-template-columns: 1.9rem minmax(0, 1fr); gap: 0.55rem; align-items: start; padding: 0.1rem 0.25rem 0.1rem 0; border-radius: 0.4rem; position: relative; }
    .stop-link:hover .stop-title { text-decoration: underline; text-underline-offset: 0.2em; }
    .stop-title { font-weight: 700; line-height: 1.35; padding-block-start: 0.05rem; }
    .stop-later { display: block; font-weight: 400; font-size: 0.8125rem; }
    .rail .stop-steps { display: none; margin: 0.25rem 0 0 2.45rem; }
    .rail .stop.is-current .stop-steps { display: grid; gap: 0.1rem; align-content: start; min-block-size: 0; overflow-y: auto; scrollbar-width: thin; overscroll-behavior: contain; }
    .stop-steps a { display: block; padding: 0.2rem 0.45rem; border-radius: 0.3rem; font-size: 0.875rem; line-height: 1.4; }
    .stop-steps a:hover { color: var(--ink); background: var(--sheet); }
  }

  /* ---- narration: one block per beat --------------------------------------------------- */
  .beats { display: grid; gap: 1.1rem; }
  .beat {
    padding-inline-start: 0.9rem;
    border-inline-start: 0.25rem solid var(--rule);
    display: grid; gap: 0.6rem;
    font-size: 1.0625rem;
  }
  .beat.is-now { border-inline-start-color: var(--signal); }
  .beat:not(.is-now) > p { color: var(--ink-2); }
  .beat strong { font-weight: 700; color: var(--ink); background: var(--wash); padding-inline: 0.15em; }
  .beat .calc {
    font-family: var(--font-num); font-variant-numeric: tabular-nums; font-size: 1.25em;
    color: var(--ink); letter-spacing: 0.02em;
  }
  .beat code, .more-body code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.9em; background: var(--sheet); border: 0.07rem solid var(--rule); border-radius: 0.25rem; padding: 0 0.3em; }
  .beat ol, .beat ul { margin: 0; padding-inline-start: 1.4em; display: grid; gap: 0.3rem; }
  .end-hint { margin-block: 1.25rem; color: var(--ink-3); font-size: 0.9375rem; }
  .beat table.dist { margin-inline: 0; }
  .beat table.dist th, .beat table.dist td { inline-size: auto; padding-inline: 0.9rem; white-space: nowrap; }

  /* A term that can be opened: dashed underline, like a note in the margin. */
  button.term {
    display: inline; min-block-size: 0; padding: 0; border: 0; border-radius: 0; background: none;
    color: inherit; font: inherit; line-height: inherit;
    text-decoration: underline dashed var(--signal) 0.12em; text-underline-offset: 0.25em;
    cursor: help;
  }
  button.term:hover, button.term[aria-expanded="true"] { background: var(--wash); }

  /* ---- "why", "show the sums", "say it more simply" ---------------------------- */
  details.more { border: 0.07rem solid var(--rule); border-radius: var(--radius); background: var(--sheet); }
  details.more > summary {
    cursor: pointer; list-style: none; padding: 0.45rem 0.8rem; color: var(--ink-2); font-size: 0.9375rem;
  }
  details.more > summary::-webkit-details-marker { display: none; }
  details.more > summary::before { content: "＋"; display: inline-block; inline-size: 1.3em; color: var(--ink-3); }
  details.more[open] > summary::before { content: "－"; }
  details.more > summary:hover { color: var(--ink); }
  details.more > .more-body { padding: 0 0.8rem 0.8rem 2.1rem; display: grid; gap: 0.55rem; font-size: 0.9688rem; }
  details.more.simpler { border-style: dashed; }
  details.more::details-content { interpolate-size: allow-keywords; overflow: clip; contain: layout; }
  details.more:not([open])::details-content { block-size: 0; }
  @media (prefers-reduced-motion: no-preference) {
    details.more::details-content {
      transition: block-size, content-visibility, display;
      transition-duration: 0.3s; transition-timing-function: ease-in-out;
      transition-behavior: allow-discrete;
    }
  }

  /* ---- quiz feedback ------------------------------------------------------------ */
  .answer { font-weight: 700; }
  .answer.is-right { color: var(--good); }
  .answer.is-wrong { color: var(--bad); }
  .inline-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }

  /* ---- the bubble that explains a term -------------------------------------------- */
  .bubble {
    max-inline-size: min(24rem, 92%);
    padding: 0.8rem 1rem; border: 0.1rem solid var(--ink); border-radius: var(--radius);
    background: var(--sheet); color: var(--ink);
    font-size: 0.9688rem; line-height: 1.65;
  }
  .bubble p + p { margin-block-start: 0.4rem; }
  .bubble-name { font-family: var(--font-title); font-weight: 900; font-size: 1.125rem; }
  .bubble-example { color: var(--ink-2); }
  .bubble-example:empty { display: none; }
  @supports (position-area: block-end) {
    .bubble {
      inset: auto; margin: 0.35rem 0 0;
      position-area: block-end span-inline-end;
      position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
    }
  }
  @supports not (position-area: block-end) {
    .bubble { position: fixed; inset: auto 0.75rem 4.5rem 0.75rem; inline-size: auto; max-inline-size: none; margin: 0; }
  }
  .bubble.is-inline { display: block; position: static; margin-block: 0.4rem; max-inline-size: none; }

  /* ---- bottom bar: the three controls --------------------------------------------- */
  .controls {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.5rem clamp(0.75rem, 2cqi, 1.5rem);
    border-block-start: 0.07rem solid var(--rule);
    background: var(--paper);
    position: sticky; inset-block-end: 0; z-index: 3;
  }
  .controls .keys { margin-inline-start: auto; color: var(--ink-3); font-size: 0.8125rem; }
  @media (pointer: coarse) { .controls .keys { visibility: hidden; } }
  @container page (inline-size < 34rem) { .controls .keys { display: none; } .controls .go { margin-inline-start: auto; } }

  /* ---- settings and the full table of contents ---------------------------------------- */
  dialog.menu {
    border: 0.1rem solid var(--ink); border-radius: var(--radius);
    background: var(--sheet); color: var(--ink);
    inline-size: min(32rem, 92%); max-block-size: 86dvh; padding: 1rem 1.25rem;
  }
  dialog.menu[open] { display: grid; gap: 1rem; }
  dialog.menu:modal { overscroll-behavior: contain; }
  dialog.menu::backdrop { background: rgb(0 0 0 / 0.45); overflow: hidden; overscroll-behavior: contain; }
  .menu-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
  .toc { margin: 0; padding-inline-start: 1.5em; display: grid; gap: 0.6rem; }
  .toc > li > span, .toc > li > a { font-weight: 700; }
  .toc ol { margin: 0.2rem 0 0; padding-inline-start: 1.2em; display: grid; gap: 0.1rem; font-size: 0.9375rem; }
  .toc a { color: var(--ink); text-underline-offset: 0.2em; }
  .toc a[aria-current="step"] { background: var(--wash); text-decoration: none; padding-inline: 0.25em; }
  .toc .is-later { color: var(--ink-3); }
  .setting { border: 0.07rem solid var(--rule); border-radius: var(--radius); display: flex; flex-wrap: wrap; gap: 0.25rem 1.25rem; padding: 0.5rem 0.9rem; }
  .setting legend { padding-inline: 0.4rem; color: var(--ink-2); font-size: 0.9375rem; }
  .setting label { display: inline-flex; align-items: center; gap: 0.35rem; min-block-size: 2.25rem; }

  /* ---- moving between steps ----------------------------------------------------------- */
  @keyframes leave-left { to { transform: translateX(-2.5rem); opacity: 0; } }
  @keyframes enter-right { from { transform: translateX(2.5rem); opacity: 0; } }
  @keyframes leave-right { to { transform: translateX(2.5rem); opacity: 0; } }
  @keyframes enter-left { from { transform: translateX(-2.5rem); opacity: 0; } }
  .player { view-transition-name: player; }
  ::view-transition-group(player) { animation-duration: 0.32s; animation-timing-function: ease-in-out; }
  html:active-view-transition-type(forward)::view-transition-old(player) { animation: 0.2s ease-in both leave-left; }
  html:active-view-transition-type(forward)::view-transition-new(player) { animation: 0.3s ease-out both enter-right; }
  html:active-view-transition-type(backward)::view-transition-old(player) { animation: 0.2s ease-in both leave-right; }
  html:active-view-transition-type(backward)::view-transition-new(player) { animation: 0.3s ease-out both enter-left; }
  @media (prefers-reduced-motion: reduce) {
    ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
  }
}
