@layer components {
  /* Base: a non-modal <dialog> anchored below its relatively-positioned parent
  /* -------------------------------------------------------------------------- */

  .popup {
    --popup-gap: 4px;
    --popup-max-block-size: 70dvh;
    --popup-min-inline-size: 20ch;
    --popup-padding: 4px;

    /* The spec's menu: neutral fill, 10px radius, the deep menu shadow (its
       first layer is the hairline edge, so no border) */
    background-color: var(--color-menu);
    border: 0;
    border-radius: var(--border-radius-large);
    box-shadow: var(--shadow-menu);
    color: var(--color-ink);
    inset: calc(100% + var(--popup-gap)) auto auto 50%;
    margin: 0;
    max-block-size: var(--popup-max-block-size);
    max-inline-size: min(40ch, calc(100vw - var(--main-padding) * 2));
    min-inline-size: min(var(--popup-min-inline-size), calc(100vw - var(--main-padding) * 2));
    opacity: 0;
    overflow: auto;
    padding: var(--popup-padding);
    position: absolute;
    transform: scale(0.95);
    transform-origin: top center;
    transition: var(--duration-fast) allow-discrete;
    transition-property: display, opacity, overlay, transform;
    translate: -50%;
    z-index: var(--z-popup);

    /* transform: none (not scale(1)) so no compositing layer lingers and the
       border paints on the same pixel boundary as the untransformed trigger */
    &[open] {
      opacity: 1;
      transform: none;
    }

    @starting-style {
      &[open] {
        opacity: 0;
        transform: scale(0.95);
      }
    }
  }

  /* Variants
  /* -------------------------------------------------------------------------- */

  /* Flush with the anchor's inline-end edge, for right-aligned triggers */
  .popup--align-end {
    inset-inline: auto 0;
    transform-origin: top right;
    translate: none;
  }

  /* Elements
  /* -------------------------------------------------------------------------- */

  /* A hairline inset 8px from each side, 4px above and below */
  .popup__section + .popup__section {
    border-block-start: var(--border);
    margin-block-start: 4px;
    margin-inline: 8px;
    padding-block-start: 4px;
  }

  .popup__summary {
    align-items: center;
    border-radius: var(--border-radius);
    color: var(--color-ink-muted);
    cursor: pointer;
    display: flex;
    font-size: var(--text-small);
    font-weight: 500;
    gap: 0.5em;
    list-style: none;
    padding: 0.4em;
    user-select: none;

    &::-webkit-details-marker {
      display: none;
    }

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--color-surface-subtle);
        color: var(--color-ink);
      }
    }
  }

  .popup__summary-caret {
    --icon-size: 0.9em;

    transition: rotate var(--duration-fast) ease-in-out;

    details[open] > .popup__summary & {
      rotate: 90deg;
    }
  }

  .popup__list {
    display: flex;
    flex-direction: column;
    gap: 0;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  /* 24px rows: 13px text on a 16px line, 4px above and below */
  .popup__btn {
    --btn-block-size: 1.5rem;
    --btn-border-size: 0;
    --btn-color: var(--color-ink);
    --btn-font-weight: 400;
    --btn-padding: 0 8px;

    border-radius: var(--border-radius);
    gap: 8px;
    inline-size: 100%;
    justify-content: flex-start;
    text-align: start;
    text-decoration: none;

    /* Hover and keyboard highlight: the highlight pair, its ring drawn inside
       so the row keeps its size */
    &:focus-visible {
      outline: 0;
    }

    &:focus-visible,
    &:hover {
      --btn-background: var(--color-accent-fill);
      --btn-color: var(--color-ink);

      box-shadow: inset 0 0 0 1px var(--color-accent);
    }
  }

  .popup__btn-label {
    /* line-height above 1 so overflow: hidden doesn't clip descenders
       against .btn's line-height: 1 */
    line-height: 16px;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .popup__icon {
    --icon-size: 16px;
  }

  /* The check is only revealed on the current item */
  .popup__check {
    display: none;
    margin-inline-start: auto;

    .popup__btn[aria-current] & {
      display: inline-block;
    }
  }

  .popup__empty {
    color: var(--color-ink-muted);
    font-size: var(--text-small);
    margin: 0;
    padding: 4px 8px;
  }

  @media print {
    .popup { display: none; }
  }
}
