@layer layout {
  .stage {
    --stage-max: 850px;
    --stage-min: 260px;
  }
}

@layer components {
  .nav {
    display: flex;
    align-items: center;
    gap: var(--space-10);
    min-inline-size: max-content;
    padding: var(--space-10);
    background: var(--color-surface);
    color: var(--color-text);
    border: 1px solid var(--color-border);

    b {
      margin-inline-end: auto;
    }

    button {
      border: 0;
      padding-block: var(--space-5);
      padding-inline: var(--space-7);
    }
  }

  /* collapses out of the box tree so .nav's own gap applies uniformly across every item
     and the hamburger, with no double-gap from wrapping the countable items in their own
     flex container; :nth-last-child() below still counts its real DOM children, since
     display:contents only changes rendering, not DOM structure */
  .nav-items {
    display: contents;
  }

  :is(.nav a, .nav button, .nav-menu a, .nav-menu button) {
    white-space: nowrap;
  }

  .more {
    /* the popover opens into the top layer, which position:absolute can't reach back from
       to find .nav as its containing block — anchor-name + anchor() bridges that gap */
    anchor-name: --nav-trigger;
    display: none;
    font-size: 1.1rem;
    line-height: 1;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--color-text);
    cursor: pointer;

    &:hover {
      background: var(--color-surface-2);
    }
  }

  /* --- item collapse: position decides visibility, not a per-item tier class --- */

  @container (width < 650px) {
    .nav-items > :nth-last-child(-n + 2) {
      display: none;
    }
    .more {
      display: inline-flex;
    }
  }

  @container (width < 450px) {
    .nav-items > :nth-last-child(-n + 5) {
      display: none;
    }
  }

  /* --- overflow menu: a native popover, so open/close/outside-click/Escape are all free --- */

  .nav-menu {
    position: fixed;
    /* named explicitly here (anchor(--nav-trigger side)) rather than via the position-anchor
       property, since position-anchor itself has meaningfully weaker support than the core
       anchor-name/anchor() mechanism this depends on */
    top: anchor(--nav-trigger bottom);
    right: anchor(--nav-trigger right);
    /* the UA stylesheet sets all four [popover] insets to 0 by default; left/bottom have to
       be reset to auto or this box stays over-constrained and the anchor() values above lose */
    bottom: auto;
    left: auto;
    margin-block-start: var(--space-5);
    padding: var(--space-4);
    display: grid;
    gap: var(--space-1);
    list-style: none;
    min-inline-size: 10rem;
    background: var(--color-surface);
    color: var(--color-text);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: 0 var(--space-10) var(--space-13) var(--shadow-color);
    opacity: 0;
    translate: 0 calc(var(--space-2) * -1);

    @media (prefers-reduced-motion: no-preference) {
      transition: opacity .15s ease, translate .15s ease, display .15s allow-discrete;
    }

    &:popover-open {
      opacity: 1;
      translate: 0 0;

      @starting-style {
        opacity: 0;
        translate: 0 calc(var(--space-2) * -1);
      }
    }

    li {
      display: none;
      margin: 0;
    }

    /* mirrors the same position-based logic as the inline row above: a menu item only
       appears once its inline twin has actually been hidden by the row's own breakpoints */
    @container (width < 650px) {
      > :nth-last-child(-n + 2) {
        display: block;
      }
    }

    @container (width < 450px) {
      > :nth-last-child(-n + 5) {
        display: block;
      }
    }

    a,
    button {
      display: block;
      inline-size: 100%;
      padding-block: var(--space-5);
      padding-inline: var(--space-6);
      border-radius: var(--space-4);
      text-align: start;
      text-decoration: none;
      color: inherit;
      background: transparent;
      border: 0;
      cursor: pointer;
    }

    a:hover,
    button:hover {
      background: var(--color-surface-2);
    }
  }
}
