/*
  Paper: the page's own layout, on design/tokens.css and design/components.css and nothing else.
  Components (the bar, buttons, the switch, the field, the rail, sheets, the open sheet, the fall's
  HUD, the archive) live in components.css; this file places them, and holds what only the site
  needs: the wall's grid, the fall's canvases, the peel's flap, the story under a sheet, the paper's
  age. Lanes are placed by the script from LANES in design/tokens.js, so no lane is typed here.
*/
@layer tokens, base, components, app;

@layer app {
  [hidden] {
    display: none !important;
  }

  html,
  body {
    block-size: 100%;
  }

  body {
    overflow: hidden;
  }

  .icon-set {
    position: absolute;
    inline-size: 0;
    block-size: 0;
    overflow: hidden;
  }

  /* "Skip to the wall": the first stop for the keyboard on a wall, out of sight until it has focus */
  .skip-link {
    position: fixed;
    inset: var(--space-2) auto auto var(--margin);
    z-index: var(--z-bubble);
    translate: 0 calc(-100% - var(--space-4));
  }

  .skip-link:focus-visible {
    translate: 0 0;
  }

  body:not([data-phase="wall"]) .skip-link {
    display: none;
  }

  /* ── The bar: fixed at the top; what it holds follows the phase ───────── */
  .bar {
    position: fixed;
    inset: 0 0 auto;
    padding-inline: max(var(--margin), env(safe-area-inset-left)) max(var(--margin), env(safe-area-inset-right));
  }

  .bar .bar-title {
    margin: 0;
  }

  body[data-phase="ask"] .bar > :not(.bar-name, .switch),
  body:not([data-phase="ask"]) .bar-name,
  body[data-phase="archive"] .bar > :is(nav, [data-all], [data-poster], .copy) {
    display: none;
  }

  /* during the fall the bar gives way to the fall's own controls */
  body[data-phase="fall"] .bar {
    visibility: hidden;
  }

  /* ── The fall, under everything: the wormhole (WebGL), or the flat fall for reduced motion ── */
  .fall,
  .worm,
  .tunnel {
    position: fixed;
    inset: 0;
    z-index: var(--z-wall);
    display: block;
    inline-size: 100%;
    block-size: 100%;
  }

  .hud {
    inset-block-start: max(var(--margin), env(safe-area-inset-top));
  }

  [data-boost] .controls .hint {
    color: var(--now-ink);
    background: var(--now-ground);
  }

  /* ── The present: the field, and the decades under it; one left edge with the bar ───────── */
  .present,
  .wall,
  .archive {
    position: fixed;
    inset: var(--bar) 0 0;
    z-index: var(--z-wall);
  }

  .present {
    display: grid;
    grid-template-rows: 1fr auto;
    gap: var(--space-7);
    padding: var(--space-7) var(--margin) calc(var(--space-9) + var(--space-5));
    overflow-y: auto;
    background: var(--now-ground);
  }

  /* the cover lies under everything on the present, over its stock */
  .present > .cover {
    position: fixed;
    inset: var(--bar) 0 0;
    z-index: var(--z-cover);
  }

  .ask {
    align-self: center;
  }

  /* ── Round 8 flags (prototypes/paper/flags.js): what Karan says yes or no to on the preview ─────────────── */
  /* the present's corner and the drifting cover photos go (noPeek, noCover) */
  :root[data-r8~="noPeek"] .peek,
  :root[data-r8~="noCover"] .present > .cover {
    display: none;
  }

  /* the room the corner needed at the foot of the start screen goes back to the decades (--space-6 under the
     credits, so the start screen still fits 1440 x 900 when a decade's moments run to two lines) */
  :root[data-r8~="noPeek"] .present {
    padding-block-end: var(--space-6);
  }

  /* pointing at a decade sets the field's word in that decade's face (eraWord); the box keeps its size */
  .field-stack[data-era] .field-input {
    font-family: var(--era-face);
    font-weight: var(--era-weight);
  }

  /* the tooth on the Now surfaces too, at half the sheets' strength (toothNow) */
  :root[data-r8~="toothNow"] :is(.present, .bar, .wall-end) {
    background-image: var(--tooth-now);
    background-size: var(--tooth-size);
  }

  .field-stack {
    position: relative;
    display: grid;
  }

  .field-stack > * {
    grid-area: 1 / 1;
  }

  /* the input shows what is typed and scrolls if it is long; the word lies under it, unseen, until it leaves and travels */
  .word.in-field {
    visibility: hidden;
    justify-self: start;
    max-inline-size: 100%;
    padding: 0 0 var(--space-2);
    overflow: hidden;
    font: var(--font-field);
    letter-spacing: var(--track-field);
    white-space: pre;
    pointer-events: none;
  }

  /* the field's examples (examples.js): over the empty field, in each one's decade face, word by word */
  .field-example {
    display: grid;
    align-self: start;
    min-inline-size: 0;
    overflow: hidden;
    padding: 0 0 var(--space-2);
    font: var(--font-field);
    letter-spacing: var(--track-field);
    white-space: nowrap;
    color: var(--now-ink-soft);
    pointer-events: none;
  }

  .field-example > .ex-line {
    grid-area: 1 / 1;
    align-self: end;
    font-family: var(--ex-face);
    font-weight: var(--ex-weight);
    font-size: calc(1em * var(--fit, 1));
  }

  .ex-word {
    display: inline-block;
  }

  .field-stack:has(.field-input:not(:placeholder-shown)) .field-example {
    visibility: hidden;
  }

  @media (prefers-reduced-motion: no-preference) {
    .ex-word {
      animation: ex-in 520ms var(--ease-arrive) both;
      animation-delay: calc(var(--i) * 80ms);
    }

    .ex-line[data-out] .ex-word {
      animation: ex-out 260ms var(--ease-leave) both;
      animation-delay: calc(var(--i) * 30ms);
    }
  }

  @keyframes ex-in {
    from {
      opacity: 0;
      translate: 0 0.35em;
      filter: blur(0.12em);
    }
  }

  @keyframes ex-out {
    to {
      opacity: 0;
      translate: 0 -0.3em;
    }
  }

  /* the word on its way: a slip of the present's paper, black on stock */
  .word.travelling {
    position: fixed;
    z-index: var(--z-hud);
    display: grid;
    place-items: center;
    margin: 0;
    padding: var(--space-1) var(--space-3);
    font: var(--font-field);
    letter-spacing: var(--track-field);
    white-space: nowrap;
    color: var(--now-ink);
    background: var(--now-ground);
    pointer-events: none;
  }

  .word.landing {
    margin: 0;
    font-family: var(--then-face);
    font-weight: var(--then-weight);
    line-height: var(--then-leading);
    letter-spacing: var(--then-tracking);
    pointer-events: none;
  }

  .era-name {
    text-box: trim-both cap alphabetic;
  }

  /* ── The present's corner, curled: lift it to see the layers of the past underneath ─────── */
  .peek {
    --peek: var(--space-6);
    position: fixed;
    inset: auto 0 0 auto;
    z-index: var(--z-wall);
    inline-size: calc(var(--peek) * 2 + var(--space-7));
    block-size: calc(var(--peek) * 2 + var(--space-7));
    cursor: grab;
    touch-action: none;
  }

  .peek-layers {
    position: absolute;
    inset: auto 0 0 auto;
    inline-size: min(calc(var(--space-9) * 4.5), 100vw);
    block-size: min(calc(var(--space-9) * 4.5), 100vw);
    clip-path: polygon(100% 100%, 100% calc(100% - var(--peek) * 2), calc(100% - var(--peek) * 2) 100%);
    pointer-events: none;
  }

  /* the fold: the back of the present's paper, a shade darker towards the crease, outlined in ink */
  .peek-flap {
    position: absolute;
    inset: auto 0 0 auto;
    inline-size: calc(var(--peek) * 2);
    block-size: calc(var(--peek) * 2);
    background: linear-gradient(135deg, var(--stock), color-mix(in srgb, var(--stock), var(--black) var(--shade)) 50%);
    clip-path: polygon(0 100%, 100% 0, 0 0);
    filter: drop-shadow(var(--shadow-flap));
    pointer-events: none;
  }

  .peek:focus-visible {
    outline: var(--line-strong) solid var(--now-ink);
    outline-offset: var(--focus-inset);
  }

  /* ── A wall: the night's sheets on its decade's paper ─────────────────────────────────── */
  .wall {
    overflow-y: auto;
    overscroll-behavior: contain;
    /* the era's paper with its tooth (Round 8): the landing never shows a flat, bare ground */
    background: var(--tooth) 0 0 / var(--tooth-size) repeat, var(--paper);
  }

  .wall:focus {
    outline: none;
  }

  /* laid out while you fall, so its answers can be fitted, but not seen until you land */
  .wall.preparing {
    visibility: hidden;
  }

  .sheets {
    display: grid;
    grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
    grid-auto-rows: var(--row);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* desktop: exactly one screen, eight rows */
  @media (min-width: 1024px) {
    .sheets {
      grid-template-rows: repeat(var(--rows-per-screen), minmax(0, 1fr));
      block-size: 100%;
    }
  }

  .cell {
    display: grid;
    min-inline-size: 0;
    min-block-size: 0;
  }

  .wall .sheet {
    block-size: 100%;
    cursor: pointer;
  }

  /* not pasted yet: rolled up at its top edge, out of sight */
  .sheet[data-unpasted] {
    clip-path: inset(0 0 100% 0);
  }

  /* drawn for a poster: flat, no curls */
  .sheets[data-flat] .sheet {
    --c: 0;
  }

  .sheet-label,
  .sheet-answer,
  .sheet-source {
    position: relative;
    pointer-events: none;
  }

  /* the whole sheet is the button that opens it; its own controls sit above it */
  .opener {
    position: absolute;
    inset: 0;
    padding: 0;
    background: none;
    border: 0;
    border-radius: var(--radius);
    cursor: pointer;
  }

  .opener:focus-visible {
    outline: none;
  }

  .follow,
  .curl,
  .sheet-roll {
    z-index: var(--z-sheet-lifted);
  }

  /* the answer fills what the label and the source leave; the script fits its size */
  .sheet-answer {
    max-inline-size: 100%;
  }

  /* a photo, printed in the sheet's ink as a coarse halftone, above the answer */
  .halftone {
    position: absolute;
    inset: 0;
    inline-size: 100%;
    block-size: 100%;
    pointer-events: none;
  }

  /* over the photo, the label keeps a patch of its own ground, so the dots never swallow it; the patch is an
     outline, so the letters stay exactly where check 4 measures them */
  [data-lane="face"] .sheet-label {
    align-self: flex-start;
    background: var(--ground);
    outline: var(--space-2) solid var(--ground);
  }

  [data-lane="face"] .sheet-answer {
    position: relative;
    align-self: start;
    padding-block-start: var(--space-1);
    background: var(--ground);
  }

  /* Round 8: a face's photo keeps the top 55% of its sheet, so it is never a thin band of eyes over a big name */
  [data-lane="face"] .sheet-front {
    container-type: size;
  }

  [data-lane="face"] .sheet-label {
    margin-block-end: max(0px, calc(55cqb - 1lh));
  }

  /* Round 8: what someone said hangs its opening quote in the margin, so the words line up */
  [data-lane="people"] .sheet-answer {
    text-indent: -0.45em;
  }

  /* the fold while a sheet is dragged: the back of its paper, the print showing through mirrored,
     darker at the fold; always inside its own sheet */
  .sheet-flap {
    position: absolute;
    inset: 0;
    display: none;
    overflow: hidden;
    /* the ink outline: the paper inside is cut a --line in from every edge */
    background: var(--ink);
    pointer-events: none;
  }

  .flap-paper {
    position: absolute;
    inset: 0;
    overflow: hidden;
    background: var(--flap-ground);
  }

  .sheet[data-peeling] .sheet-flap {
    display: block;
  }

  .sheet[data-peeling] .curl::before,
  .sheet[data-peeling] .curl::after {
    display: none;
  }

  .flap-print {
    position: absolute;
    transform-origin: 0 0;
    opacity: 0.14;
  }

  .flap-print .sheet-front {
    clip-path: none;
  }

  .flap-curl {
    position: absolute;
    block-size: calc(var(--space-9) * 40); /* longer than any sheet's diagonal */
    transform-origin: 0 50%;
    background: linear-gradient(to right, color-mix(in srgb, var(--black) calc(var(--shade) * 1.5), transparent), transparent);
  }

  .sheet[data-peeling] {
    z-index: var(--z-sheet-lifted);
  }

  /* ── The story under a sheet, and behind it when it is open ──────────────────────────── */
  .sheet-under {
    justify-content: flex-start;
  }

  .under-label {
    font: var(--font-label);
    text-box: trim-both cap alphabetic;
  }

  .sheet-under .under-source {
    margin-block-start: auto;
    font: var(--font-source);
    letter-spacing: var(--track-small);
  }

  .sheet-under img {
    display: block;
    flex: 1;
    inline-size: 100%;
    min-block-size: 0;
    object-fit: cover;
    object-position: 50% 25%;
  }

  .sheet-under ol,
  .page-story ol {
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .sheet-under li,
  .page-story ol li {
    display: grid;
    grid-template-columns: minmax(var(--space-9), auto) 1fr;
    gap: var(--space-3);
    align-items: baseline;
    font-family: var(--then-face);
    font-weight: var(--then-weight);
    font-size: var(--text-5);
    line-height: var(--then-leading);
  }

  .sheet-under li span:first-child,
  .page-story ol li span:first-child {
    font: var(--font-source);
    font-variant-numeric: tabular-nums;
  }

  /* ── An open sheet ────────────────────────────────────────────────────────────────── */
  .open-sheet {
    overflow-y: auto;
    overscroll-behavior: contain;
    --then-tracking: 0;
  }

  .open-sheet:modal {
    max-block-size: none;
    max-inline-size: none;
  }

  .page-story img {
    display: block;
    max-inline-size: 100%;
    block-size: auto;
  }

  .page-story .capture {
    position: relative;
    inline-size: min(100%, calc(var(--space-9) * 10));
    aspect-ratio: 4 / 3;
    overflow: hidden;
    background: var(--stock);
  }

  .page-story .capture iframe {
    position: absolute;
    inset: 0;
    inline-size: 200%;
    block-size: 200%;
    border: 0;
    scale: 0.5;
    transform-origin: 0 0;
  }

  /* ── All moments ──────────────────────────────────────────────────────────────────── */
  .archive {
    overflow-y: auto;
    background: var(--now-ground);
  }

  /* the wall moving into or out of its tile sits over all moments */
  .wall[data-leaving] {
    z-index: var(--z-sheet-lifted);
  }

  .archive-title:focus {
    outline: none;
  }

  .tile-link {
    cursor: pointer;
  }

  /* ── The Capsule theme's fall (/capsule/): the HUD and the controls as glass on the splat wormhole, lit by the
     decade being passed (--decade-glow, set by paper.js). The glass is dark enough that stock type keeps 4.5:1 over
     the brightest splats; Paper's fall is untouched. ── */
  :root[data-home="capsule"] {
    --glass-ground: color-mix(in oklab, var(--black) 66%, transparent);
    --glass-hover: color-mix(in oklab, var(--stock) 14%, var(--glass-ground));
    --glass-press: color-mix(in oklab, var(--stock) 24%, var(--glass-ground));
    --glass-edge: color-mix(in oklab, var(--stock) 24%, transparent);
    --glass-blur: blur(18px) saturate(150%);
    --glass-radius: 14px;
    --decade-glow: var(--stock);
    --glow: color-mix(in oklab, var(--decade-glow) 75%, transparent);
  }

  :root[data-home="capsule"] :is(.hud-status, .hud-narration, .hud-year, .hud-passed, .controls) {
    color: var(--stock);
    background: var(--glass-ground);
    border: var(--line) solid var(--glass-edge);
    border-radius: var(--glass-radius);
    -webkit-backdrop-filter: var(--glass-blur);
    backdrop-filter: var(--glass-blur);
  }

  :root[data-home="capsule"] .hud-status {
    border-radius: 999px;
  }

  /* the decade's name and the year glow in the decade's colour; the line under the name keeps plain stock */
  :root[data-home="capsule"] .hud-narration {
    /* no outer glow: the card's reveal is a clip-path, which would square it off */
    border-color: color-mix(in oklab, var(--decade-glow) 45%, var(--glass-edge));
  }

  :root[data-home="capsule"] .hud-decade {
    text-shadow: 0 0 18px var(--glow), 0 0 4px var(--glow);
  }

  :root[data-home="capsule"] .hud-year {
    padding-block: var(--space-1);
    text-shadow: 0 0 14px var(--glow);
  }

  :root[data-home="capsule"] .hud-passed {
    padding: var(--space-1) var(--space-3);
    border-radius: 999px;
  }

  /* the controls: one floating glass bar; the buttons are clear pills in it, lit under the pointer */
  :root[data-home="capsule"] .controls {
    padding: var(--space-1);
    border-radius: calc(var(--control) / 2 + var(--space-1));
  }

  :root[data-home="capsule"] .controls :is(.button, .switch) {
    color: var(--stock);
    background: transparent;
    border-color: transparent;
    border-radius: 999px;
  }

  :root[data-home="capsule"] .controls :is(.button, .switch):is(:hover, [data-state~="hover"]) {
    background: var(--glass-hover);
    border-color: var(--glass-edge);
  }

  :root[data-home="capsule"] .controls :is(.button, .switch):is(:active, [data-state~="pressed"]),
  :root[data-home="capsule"] .controls .switch:is(:has(input:checked), [data-state~="on"]) {
    color: var(--stock);
    background: var(--glass-press);
    border-color: var(--glass-edge);
  }

  :root[data-home="capsule"] .controls .hint {
    color: var(--stock);
    background: transparent;
    border-radius: 999px;
  }

  /* falling faster: the hint lights up in the decade's colour */
  :root[data-home="capsule"] [data-boost] .controls .hint {
    color: var(--stock);
    background: var(--glass-press);
    box-shadow: 0 0 0 var(--line) var(--decade-glow), 0 0 18px var(--glow);
  }

  /* the fall's speed dial: only laid out here (in a row, no fieldset frame); its look is the theme's */
  :root[data-home="capsule"] .speed-dial {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0;
    padding: 0 var(--space-2);
    border: 0;
    min-inline-size: 0;
  }

  :root[data-home="capsule"] .speed-step {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    font: var(--font-control);
    color: var(--stock);
    cursor: pointer;
  }

  /* no see-through glass for those who asked for less: the same panels, solid */
  @media (prefers-reduced-transparency: reduce) {
    :root[data-home="capsule"] {
      --glass-ground: color-mix(in oklab, var(--black) 94%, var(--stock));
      --glass-blur: none;
    }
  }

  /* Round 8: figures in the Now type are tabular, so dates and counts line up and never change width. Last in
     the layer, because the `font` shorthand above resets them */
  :is(.sheet-label, .sheet-source, .under-label, .hud, .hud *, .credits, .years, .choices, .field-note, .bubble, .spread-credit) {
    font-variant-numeric: tabular-nums;
  }
}
