/* A menu that opens under its button, positioned by CSS alone */

@layer components {
  .popup-anchor {
    position: relative;
  }

  .popup {
    position: absolute;
    inset: calc(100% + 6px) auto auto 0;
    z-index: 10;
    min-width: 210px;
    margin: 0;
    padding: 5px;
    border: 1px solid var(--color-line);
    border-radius: 16px;
    background: var(--color-surface);
    color: var(--color-ink);
    box-shadow: var(--shadow-popup);
    transform-origin: top left;
    opacity: 0;
    scale: 0.96;
    transition: opacity 120ms var(--ease-out-expo), scale 120ms var(--ease-out-expo), display 120ms allow-discrete;

    /* Grows out of its button, and leaves faster than it came */
    &[open] {
      opacity: 1;
      scale: 1;
      transition-duration: 180ms;
    }

    @starting-style {
      &[open] {
        opacity: 0;
        scale: 0.96;
      }
    }

    /* Opened with a tap or click, the menu takes focus itself, which needs no ring */
    &:focus {
      outline: none;
    }

    @media (prefers-reduced-motion: reduce) {
      scale: none;
    }

    form {
      display: contents;
    }
  }

  /* Without room beside its button, the menu lines up with the start of the button's row, which always
     fits, and grows from its right, nearest the button */
  .popup-anchor:has(> .popup--row) {
    position: static;
  }

  .popup--row {
    transform-origin: top right;
  }

  /* Under a button at the end of its row, like a keep's More: lined up with the button's end, growing from it */
  .popup--end {
    inset: calc(100% + 6px) 0 auto auto;
    transform-origin: top right;
  }

  .popup__item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 40px;
    padding: 0 10px;
    border-radius: 9px;
    font-size: var(--text-control);
    font-weight: 500;
    text-align: left;

    &:active, &:focus-visible {
      background: var(--color-well);
      outline: none;
    }

    @media (hover: hover) and (pointer: fine) {
      &:hover {
        background: var(--color-well);
      }
    }

    .icon {
      width: 17px;
      height: 17px;
      color: var(--color-ink-secondary);
    }
  }

  .popup__separator {
    height: 1px;
    margin: 5px 10px;
    background: var(--color-line);
  }

  .popup__item--negative {
    color: var(--color-coral-text);

    .icon {
      color: currentColor;
    }
  }
}
