/* Grid flight: the swiss-grid set piece (santalarch's cube scene). On desktop a pinned stage (grid-flight) where a
   camera flies through a pyramid of wireframe boxes and dives into the center box's top face, which carries the
   section's image; a title hangs from the center line, then three beats (counter, heading, sentence) swap in.
   Phone-first: the default layout is the stack (phones, reduced motion, before boot): the title, a still of the grid
   seen from above with the image in its center cell, then the beats as normal blocks. The stage lives under
   [data-pinned]. Values come only from tokens.css; colors only from the section variables (principles/color.md D). */

[data-section="grid-flight"] {
  --background: var(--color-paper);
  --foreground: var(--color-ink);
  --muted: var(--color-muted);
  --line: var(--color-line);
  --accent: var(--color-accent);
  /* Client copy (Moiotso): the section's field, kept under its own name because the face redefines --background
     for its fill; the subject draws the logo's M in it. */
  --field: var(--background);
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(var(--spacing-columns), minmax(0, 1fr));
  column-gap: var(--spacing-gutter);
  row-gap: var(--spacing-group);
  padding: var(--spacing-section) var(--spacing-margin);
  overflow: hidden;
  background: var(--background);
  color: var(--foreground);

  &[data-tone="inverse"] {
    --background: var(--color-ink);
    --foreground: var(--color-paper);
    --muted: color-mix(in oklch, var(--color-paper) 70%, var(--color-ink));
    --line: color-mix(in oklch, var(--color-paper) 14%, var(--color-ink));
    --accent: oklch(from var(--color-accent) calc(l + 0.05) calc(c * 0.85) h);
  }

  .title {
    grid-column: 1 / 10;
    margin: 0;
    font: var(--type-display-weight) var(--type-display-size) / var(--type-display-leading) var(--type-display-family);
    letter-spacing: var(--type-display-tracking);
    text-wrap: balance;
  }

  /* The still: the flattened grid seen from above, one cell per box, the image in the center cell. */
  .scene {
    --cell-x: 18.75%;
    --cell-y: 33.333%;
    position: relative;
    grid-column: 1 / -1;
    aspect-ratio: 16 / 9;
  }

  .still {
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(to right, var(--line) 0 1px, transparent 1px),
      linear-gradient(to bottom, var(--line) 0 1px, transparent 1px);
    background-size: var(--cell-x) var(--cell-y);
    background-position: calc(50% - var(--cell-x) / 2) calc(50% - var(--cell-y) / 2);
  }

  .face {
    --background: color-mix(in oklch, var(--color-paper) 6%, var(--color-ink));
    position: absolute;
    top: calc(50% - var(--cell-y) / 2);
    left: calc(50% - var(--cell-x) / 2);
    width: var(--cell-x);
    height: var(--cell-y);
    outline: 1px solid var(--line);
    background: var(--background);
  }

  .face [data-subject] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
  }

  /* Client copy (Moiotso): the subject is the logo's hexagon symbol seen from above. The face's fill is the hexagon;
     the subject draws the M on it in the section's field, through a luminance mask of the kit's primary symbol (white
     M, pine hexagon at about 3% luminance, so the hexagon barely tints). The beats' text sits over it at full bleed,
     so it stays quiet: two tones of the dark field, both darker than the line role, so the crosshair (--line) reads
     across the hexagon and the M alike and the text keeps its contrast. Sized so the symbol's hexagon (91.8% of its
     square artboard) fills the face's hexagon and the M sits where the logo puts it. */
  .face .media[data-subject]:not([data-image="needed"]) {
    background: var(--field);
    mask: url("/clients/moiotso/assets/logo/svg/symbol/moiotso-symbol-primary.svg") 50% 50% / 109% 109% no-repeat luminance;
  }

  /* Image slot marker (sections/README.md): crossed and captioned until the real image replaces the slot. */
  .media[data-image="needed"] {
    --image-ratio: "1:1";
    display: grid;
    place-items: center;
    background-image:
      linear-gradient(to top right, transparent calc(50% - 1px), var(--muted) 50%, transparent calc(50% + 1px)),
      linear-gradient(to bottom right, transparent calc(50% - 1px), var(--muted) 50%, transparent calc(50% + 1px));

    &::after {
      content: var(--image-caption, "Image needed") " " var(--image-ratio);
      box-sizing: border-box;
      max-width: 100%;
      overflow: hidden;
      padding-inline: calc(var(--spacing-cell) / 2);
      white-space: nowrap;
      text-overflow: ellipsis;
      scale: calc(1 / var(--subject-scale, 1));
      background-color: inherit;
      font: var(--type-label-weight) var(--type-label-size) / var(--type-label-leading) var(--type-label-family);
      letter-spacing: var(--type-label-tracking);
      color: var(--muted);
    }

    &[data-ratio]::after {
      content: var(--image-caption, "Image needed") " " attr(data-ratio);
    }
  }

  .canvas,
  .cross {
    display: none;
  }

  .beat {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: subgrid;
    row-gap: var(--spacing-cell);
  }

  .counter {
    grid-column: 1 / -1;
    font: var(--type-label-weight) var(--type-label-size) / var(--type-label-leading) var(--type-label-family);
    letter-spacing: var(--type-label-tracking);
    font-variant-numeric: tabular-nums;
    color: var(--foreground);
  }

  .heading {
    grid-column: 1 / 7;
    margin: 0;
    font: var(--type-display-weight) var(--type-display-size) / var(--type-display-leading) var(--type-display-family);
    letter-spacing: var(--type-display-tracking);
    text-wrap: balance;
  }

  .text {
    grid-column: var(--spacing-lane-body-start) / span 4;
    margin: 0;
    font: var(--type-body-weight) var(--type-body-size) / var(--type-body-leading) var(--type-body-family);
    letter-spacing: var(--type-body-tracking);
    color: var(--foreground);
    text-wrap: pretty;
  }

  /* The stack opens with the group gap, not the section padding, so under a hero its title shows in the first desktop
     viewport (principles/layout.md rule 14, the hero teaser). */
  @media (min-width: 768px) {
    padding-block-start: var(--spacing-group);
  }

  /* Client copy (Moiotso): the review note sits at the right, as on the other sections, so it clears the title. */
  .note {
    position: absolute;
    z-index: 3;
    top: var(--spacing-cell);
    right: var(--spacing-margin);
    max-width: 24em;
    text-align: right;
  }

  /* Client copy (Moiotso): hexagon cells (data-cell="hexagon", motion/grid-flight README "Cells"). The still is the
     flattened honeycomb seen from above: an inline svg of hexagons standing on a vertex, drawn in 1px lines and
     centered on the scene, and the face is the center hexagon, the hexagon clipped from its box. --hex-w is one
     hexagon's width (vertex-to-vertex height is width / 0.866), the svg six hexagons wide. The pinned stage keeps
     the module's own face box and clip. */
  &[data-cell="hexagon"] .scene {
    --hex-w: 18%;
  }

  &[data-cell="hexagon"]:not([data-pinned]) {
    .still {
      overflow: clip;
      background-image: none;
    }

    .honeycomb {
      position: absolute;
      top: 50%;
      left: 50%;
      width: calc(var(--hex-w) * 6);
      height: auto;
      translate: -50% -50%;
      fill: none;
      stroke: var(--line);
      stroke-width: 1px;
      vector-effect: non-scaling-stroke;
    }

    .honeycomb path {
      vector-effect: non-scaling-stroke;
    }

    .face {
      top: 50%;
      left: 50%;
      width: var(--hex-w);
      height: auto;
      aspect-ratio: 0.866;
      translate: -50% -50%;
      outline: 0;
      clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    }
  }

  /* Pinned stage: 100vh; the canvas and the face fill it, the title and each beat hang from the center line (the
     heading at the margin, the sentence at the body lane), the counter at the foot of the body lane, and a crosshair
     marks the center while a beat shows. */
  &[data-pinned] {
    display: block;
    height: 100vh;
    padding: 0;

    .scene {
      position: absolute;
      inset: 0;
      aspect-ratio: auto;
    }

    .still {
      display: none;
    }

    .canvas {
      display: block;
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
    }

    /* The face's box is one 100vmax square at the stage's corner: grid-flight moves and scales it onto the
       projected top face with transforms only. */
    .face {
      top: 0;
      left: 0;
      width: 100vmax;
      height: 100vmax;
      outline: 0;
      transform-origin: 0 0;
    }

    .cross {
      display: block;
      position: absolute;
      inset: 0;
      pointer-events: none;

      &::before,
      &::after {
        content: '';
        position: absolute;
        background: var(--line);
      }

      &::before {
        inset-inline: 0;
        top: 50%;
        height: 1px;
      }

      &::after {
        inset-block: 0;
        left: 50%;
        width: 1px;
      }
    }

    .title,
    .beat {
      position: absolute;
      z-index: 2;
      inset-inline: var(--spacing-margin);
    }

    .title {
      top: 50%;
      max-width: 75%;
    }

    .beat {
      inset-block: 0;
      grid-template-columns: repeat(var(--spacing-columns), minmax(0, 1fr));
      column-gap: var(--spacing-gutter);
      grid-template-rows: 50% auto 1fr auto;
      row-gap: 0;
      padding-block-end: var(--spacing-group);
    }

    .heading {
      grid-row: 2;
      padding-block-start: var(--spacing-cell);
    }

    .text {
      grid-row: 2;
      padding-block-start: var(--spacing-cell);
    }

    .counter {
      grid-row: 4;
      grid-column: var(--spacing-lane-body-start) / -1;
    }
  }

  @media (max-width: 767px) {
    display: flex;
    flex-direction: column;
    row-gap: var(--spacing-group-mobile);
    padding: var(--spacing-section-mobile) var(--spacing-margin-mobile);

    .scene {
      --cell-x: 33.333%;
      aspect-ratio: 1;
    }

    &[data-cell="hexagon"] .scene {
      --hex-w: 33.333%;
    }

    .beat {
      display: flex;
      flex-direction: column;
      align-items: stretch;
    }

    .note {
      right: var(--spacing-margin-mobile);
    }
  }
}
