@layer tokens {
  :root {
    /* Room */
    --room-floor: oklch(24% 0.018 55);
    --room-wall: oklch(38% 0.025 55);
    --room-fixture: oklch(31% 0.02 55);
    /* Complementary to the warm floor, so the tops separate hardest. Also the
       colour the arm notch is painted in, which keeps the gap between a guest's
       arms flush with the table they are sitting at. */
    --top-surface: oklch(74% 0.075 190);
    --top-edge: oklch(8% 0.008 60);
    --top-shadow: oklch(0% 0 0 / 38%);

    /* Guests. Seat index picks one, so a full six-top has no repeats. */
    --shirt-0: oklch(68.5% 0.135 22.9);
    --shirt-1: oklch(68.5% 0.115 257.9);
    --shirt-2: oklch(89.1% 0.159 155.9);
    --shirt-3: oklch(78.1% 0.148 312.6);
    --shirt-4: oklch(84% 0.145 92);
    /* Rose, not teal: hue 195 now belongs to the table top. */
    --shirt-5: oklch(74% 0.12 350);

    --skin-light: oklch(87.6% 0.069 77.4);
    --skin-tan: oklch(75.2% 0.113 77.6);
    --skin-deep: oklch(61.9% 0.117 65.8);

    --hair-black: oklch(23.9% 0 90);
    --hair-brown: oklch(32.6% 0.055 53.3);
    --hair-ginger: oklch(55.9% 0.108 59.1);

    /*
     * Pure ratios only. Anything derived from --top-size has to be declared on
     * .seat-table instead: a custom property substitutes its var()s at
     * computed-value time on the element that declares it, so a calc() written
     * here against a property that only exists further down resolves to
     * guaranteed-invalid at :root and inherits down broken.
     */
    --diner-hand-x: 25%;
    --diner-hand-y: 88%;
    --diner-hair-x: 38%;
    --diner-hair-y: 24%;
    --diner-arm-center: 92%;

    --chip-size: 1.375rem;
    --chip-bar: 0.625rem;
    --chip-bar-weight: 0.0875rem;

    --ease-settle: cubic-bezier(0.34, 1.1, 0.4, 1);
    --duration-seat: 480ms;
    --duration-pop: 180ms;
  }
}

@layer layout {
  .demo-stage {
    inline-size: min(92vw, var(--measure-l));
    margin-block: var(--space-4xl);
    margin-inline: auto;
    padding-inline: var(--space-l);
    /* A floor plan is a fixed-proportion diagram. Below the room's minimum it
       pans rather than shrinking the tables past legibility. */
    overflow-x: auto;
    overscroll-behavior-x: contain;
  }

  .room {
    /*
     * A grid, not absolute placement. An absolutely positioned table would be
     * the containing block for its own popover, and position-try would then
     * test overflow against a 120px box instead of the room, flipping every
     * popover regardless of where its table sits.
     */
    position: relative;
    display: grid;
    grid-template: repeat(9, minmax(0, 1fr)) / repeat(12, minmax(0, 1fr));
    aspect-ratio: 3 / 2;
    min-inline-size: 34rem;
    container-type: inline-size;
    border: 2px solid var(--room-wall);
    border-radius: var(--radius-m);
    background-color: var(--room-floor);
    background-image:
      linear-gradient(90deg, oklch(100% 0 0 / 3%) 1px, transparent 1px),
      linear-gradient(oklch(100% 0 0 / 3%) 1px, transparent 1px);
    background-size: 2.5rem 2.5rem;
  }
}

@layer components {
  .fixture {
    position: absolute;
    background-color: var(--room-fixture);
    border-radius: var(--radius-s);

    &[data-kind="pass"] {
      inset-block-end: 6%;
      inset-inline-end: 6%;
      inline-size: 22%;
      block-size: 7%;
    }

    /* The doorway is a gap in the wall, so it reads as an opening, not a block. */
    &[data-kind="door"] {
      inset-block-end: -2px;
      inset-inline-start: 6%;
      inline-size: 14%;
      block-size: 4px;
      background-color: var(--color-accent);
      border-radius: 0;
    }
  }

  .seat-table {
    /*
     * Relative only so the unenhanced popover has something to sit above.
     * The @supports block below makes it static again, which anchoring needs.
     */
    position: relative;
    place-self: center;
    /*
     * The box is the whole seating arrangement, not just the top. Guests orbit
     * to --diner-ring plus half a torso, so the anchor box has to reach past
     * them or position-area parks the popover on top of the far guest.
     */
    inline-size: calc(var(--top-size) * 1.55);
    aspect-ratio: 1;
    counter-reset: party var(--party);
    anchor-name: var(--anchor-name);

    /* Seat geometry, all derived from this table's own top. */
    --diner-ring: calc(var(--top-size) * 0.52);
    --diner-head: calc(var(--diner-torso) * 0.46);
    --diner-hand: calc(var(--diner-torso) * 0.085);
    --diner-hair-radius: calc(var(--diner-head) * 0.30);
    --diner-arm-inset-x: calc(var(--diner-torso) * 0.28);
    --diner-arm-inset-y: calc(var(--diner-torso) * 0.36);

    /*
     * The checked radio is the only state. One :has() per count turns it into a
     * number, and every part below reads that number instead of repeating the
     * chain. :where() keeps the whole selector at (0,1,0).
     */
    &:has(:where(.count-state[data-count="1"]:checked)) {
      --party: 1;
    }

    &:has(:where(.count-state[data-count="2"]:checked)) {
      --party: 2;
    }

    &:has(:where(.count-state[data-count="3"]:checked)) {
      --party: 3;
    }

    &:has(:where(.count-state[data-count="4"]:checked)) {
      --party: 4;
    }

    &:has(:where(.count-state[data-count="5"]:checked)) {
      --party: 5;
    }

    &:has(:where(.count-state[data-count="6"]:checked)) {
      --party: 6;
    }

    /* Bigger tops seat more, so the guests shrink to keep gaps between them. */
    &[data-seats="2"] {
      --seats: 2;
      --top-size: clamp(2.25rem, 7.4cqi, 4rem);
      --diner-torso: calc(var(--top-size) * 0.52);
    }

    &[data-seats="4"] {
      --seats: 4;
      --top-size: clamp(3rem, 9.7cqi, 5.25rem);
      --diner-torso: calc(var(--top-size) * 0.48);
    }

    &[data-seats="6"] {
      --seats: 6;
      --top-size: clamp(3.75rem, 12.4cqi, 6.75rem);
      --diner-torso: calc(var(--top-size) * 0.40);
    }

    &[data-id="1"] {
      grid-area: 2 / 2;
      --anchor-name: --table-1;
    }

    &[data-id="2"] {
      grid-area: 2 / 5;
      --anchor-name: --table-2;
    }

    &[data-id="3"] {
      grid-area: 2 / 8;
      --anchor-name: --table-3;
    }

    &[data-id="4"] {
      grid-area: 4 / 11;
      --anchor-name: --table-4;
    }

    &[data-id="5"] {
      grid-area: 5 / 2;
      --anchor-name: --table-5;
    }

    &[data-id="6"] {
      grid-area: 5 / 6;
      --anchor-name: --table-6;
    }

    &[data-id="7"] {
      grid-area: 6 / 10;
      --anchor-name: --table-7;
    }

    &[data-id="8"] {
      grid-area: 8 / 4;
      --anchor-name: --table-8;
    }
  }

  /* Off-screen but focusable, so each table is an arrow-key operable group. */
  .count-state {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    clip-path: inset(50%);
    appearance: none;
  }

  .top {
    display: grid;
    place-items: center;
    inline-size: 100%;
    block-size: 100%;
    forced-color-adjust: none;

    > * {
      grid-area: 1 / 1;
    }
  }

  .surface {
    inline-size: var(--top-size);
    block-size: var(--top-size);
    background-color: var(--top-surface);
    border-radius: 50%;
    box-shadow:
      0 0 0 1px var(--top-edge),
      0 0.25rem 0.75rem var(--top-shadow);
  }

  /*
   * One <i> per seat, seen from above.
   *   element  = the seat, orbited into place around the top
   *   ::before = torso disc, with the arm notch and both hands as background layers
   *   ::after  = head, hair over skin
   * Head points outward and arms reach in, so no counter-rotation is needed.
   */
  .diner {
    position: relative;
    inline-size: var(--diner-torso);
    block-size: var(--diner-torso);
    transform: rotate(calc(180deg + var(--seat) * 360deg / var(--party))) translateY(calc(var(--diner-ring) * -1));
    /*
     * Seats past the party size fade out rather than being selected away.
     * clamp() reads the same --party every other part does, which replaces one
     * rule per (capacity, count) pair with a single declaration.
     */
    opacity: clamp(0, calc(var(--party) - var(--seat)), 1);
    scale: clamp(0.3, calc(var(--party) - var(--seat)), 1);
    transition:
      transform var(--duration-seat) var(--ease-settle),
      opacity var(--duration-seat) ease,
      scale var(--duration-seat) var(--ease-settle);

    &::before,
    &::after {
      content: "";
      position: absolute;
      border-radius: 50%;
    }

    &::before {
      inset: 0;
      background-color: var(--shirt);
      background-image:
        radial-gradient(circle var(--diner-hand) at var(--diner-hand-x) var(--diner-hand-y), var(--skin) 99%, transparent 100%),
        radial-gradient(circle var(--diner-hand) at calc(100% - var(--diner-hand-x)) var(--diner-hand-y), var(--skin) 99%, transparent 100%),
        radial-gradient(
          ellipse var(--diner-arm-inset-x) var(--diner-arm-inset-y) at 50% var(--diner-arm-center),
          var(--top-surface) 99%,
          transparent 100%
        );
    }

    &::after {
      inset-block-start: 0;
      inset-inline-start: 50%;
      inline-size: var(--diner-head);
      block-size: var(--diner-head);
      translate: -50% 0;
      background-color: var(--skin);
      /* Hair as an offset disc, so the hairline curves and sits off-centre. */
      background-image: radial-gradient(
        circle var(--diner-hair-radius) at var(--diner-hair-x) var(--diner-hair-y),
        var(--hair) 99%,
        transparent 100%
      );
    }

    &[data-seat="0"] {
      --seat: 0;
      --shirt: var(--shirt-0);
      --skin: var(--skin-light);
      --hair: var(--hair-brown);
    }

    &[data-seat="1"] {
      --seat: 1;
      --shirt: var(--shirt-1);
      --skin: var(--skin-tan);
      --hair: var(--hair-black);
    }

    &[data-seat="2"] {
      --seat: 2;
      --shirt: var(--shirt-2);
      --skin: var(--skin-deep);
      --hair: var(--hair-brown);
    }

    &[data-seat="3"] {
      --seat: 3;
      --shirt: var(--shirt-3);
      --skin: var(--skin-light);
      --hair: var(--hair-ginger);
    }

    &[data-seat="4"] {
      --seat: 4;
      --shirt: var(--shirt-4);
      --skin: var(--skin-tan);
      --hair: var(--hair-black);
    }

    &[data-seat="5"] {
      --seat: 5;
      --shirt: var(--shirt-5);
      --skin: var(--skin-deep);
      --hair: var(--hair-ginger);
    }
  }

  /*
   * The popover is a child of its table on purpose: hovering it keeps :hover on
   * the table (hover walks the DOM, not the visual box), so the pointer can
   * travel into the stepper without it closing. The block-end padding is the
   * bridge across the offset.
   */
  .party {
    position: absolute;
    z-index: 2;
    inset-block-end: 100%;
    inset-inline-start: 50%;
    translate: -50% 0;
    display: flex;
    align-items: center;
    gap: var(--space-2xs);
    padding: var(--space-2xs) var(--space-s);
    margin-block: var(--space-2xs);
    white-space: nowrap;
    background-color: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    opacity: 0;
    visibility: hidden;
    transition:
      opacity var(--duration-pop) ease,
      visibility var(--duration-pop) ease;

    /*
     * Transparent bridge on both block sides, so the pointer can cross the gap
     * to the pill without the popover closing. Symmetric on purpose: flip-block
     * may put the pill either above or below, and this covers both.
     */
    &::after {
      content: "";
      position: absolute;
      inset-inline: 0;
      inset-block: calc(var(--space-m) * -1);
      z-index: -1;
    }
  }

  .party-name {
    font-size: 0.8125rem;
    color: var(--color-muted);
  }

  .count {
    min-inline-size: 1.25ch;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-align: center;

    &::after {
      content: counter(party);
    }
  }

  .control {
    position: relative;
    inline-size: var(--chip-size);
    block-size: var(--chip-size);
    background-color: var(--color-bg);
    border-radius: 50%;
    /*
     * Only a revealed label takes the pointer. A chip at its bound has none, so
     * it cannot be hovered or pressed and needs no separate disabled state.
     */
    pointer-events: none;
    transition:
      background-color var(--duration-pop) ease,
      scale var(--duration-pop) ease;

    /* Horizontal bar on both chips; the vertical bar makes it a plus. */
    &::before,
    &::after {
      content: "";
      position: absolute;
      inset-block-start: 50%;
      inset-inline-start: 50%;
      inline-size: var(--chip-bar);
      block-size: var(--chip-bar-weight);
      background-color: var(--color-text);
      translate: -50% -50%;
    }

    &::after {
      rotate: 90deg;
    }

    /* Same clamp as the seats: dim at 1 for minus, dim at capacity for plus. */
    &[data-dir="down"] {
      opacity: clamp(0.3, calc(var(--party) - 1), 1);

      &::after {
        content: none;
      }
    }

    &[data-dir="up"] {
      opacity: clamp(0.3, calc(var(--seats) - var(--party)), 1);
    }

    &:has(:where(.step:hover)) {
      background-color: var(--color-surface-2, var(--room-fixture));
    }

    &:has(:where(.step:active)) {
      scale: 0.88;
    }
  }

  /*
   * Two labels per chip, one per reachable count. A style query reveals only the
   * pair valid right now, which is what lets a fixed minus and plus retarget a
   * different radio in every state.
   */
  .step {
    display: none;
    position: absolute;
    inset: 0;
    pointer-events: auto;
    cursor: pointer;

    @container style(--party: 1) {
      &[data-from="1"] {
        display: block;
      }
    }

    @container style(--party: 2) {
      &[data-from="2"] {
        display: block;
      }
    }

    @container style(--party: 3) {
      &[data-from="3"] {
        display: block;
      }
    }

    @container style(--party: 4) {
      &[data-from="4"] {
        display: block;
      }
    }

    @container style(--party: 5) {
      &[data-from="5"] {
        display: block;
      }
    }

    @container style(--party: 6) {
      &[data-from="6"] {
        display: block;
      }
    }
  }

  .seat-table:is(:hover, :focus-within) {
    & :where(.party) {
      opacity: 1;
      visibility: visible;
    }

    & :where(.surface) {
      outline: 2px solid var(--color-accent);
      outline-offset: 3px;
    }
  }

  /*
   * Anchor positioning is Emerging: anchor-name and position-area are Baseline
   * 2026, position-anchor is not, so the guard keys on the part that has not
   * shipped. Everything above already places the popover off its own table, so
   * an engine without it loses only the wall-aware flipping.
   */
  @supports (position-anchor: --a) {
    /* An anchor-name element that is also positioned does not resolve as an
       anchor, and static hands the popover's containing block to .room, which
       is what makes position-try test overflow against the room's walls. */
    .seat-table {
      position: static;
    }

    .party {
      position-anchor: var(--anchor-name);
      position-area: block-start;
      position-try-fallbacks: flip-block;
      inset: auto;
      translate: none;
      justify-self: anchor-center;
    }
  }
}

@layer overrides {
  @media (prefers-contrast: more) {
    .party {
      border-color: var(--color-text);
    }

    .party-name {
      color: var(--color-text);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .diner,
    .party,
    .control {
      transition-duration: 1ms;
    }
  }
}
