@layer components {
  .popup {
    --panel-border-radius: 0.5em;
    --panel-padding: var(--block-space);
    --panel-size: auto;

    inset: 0 auto auto 50%;
    max-block-size: 80vh;
    min-inline-size: min(25ch, calc(100dvw - var(--block-space-double)));
    max-inline-size: min(55ch, calc(100dvw - var(--block-space-double)));
    overflow: auto;
    position: absolute;
    transform: translateX(-50%);
    z-index: var(--z-popup);

    &[open] {
      display: flex;
    }

    &:has(.popup__footer) {
      --panel-padding: var(--block-space) var(--block-space) 0 var(--block-space)
    }

    #header:has(&) {
      max-inline-size: 100vw;
      position: relative;
      z-index: 5;
    }

    .card & {
      min-inline-size: 25ch;
      max-inline-size: 25ch;
    }
  }

  .popup__footer {
    background-color: var(--color-canvas);
    border-block-start: 1px solid var(--color-ink-lighter);
    font-size: var(--text-small);
    inset: auto 0 0;
    margin-inline: calc(var(--block-space) * -1);
    padding: var(--block-space-half) var(--inline-space) var(--block-space);
    position: sticky;
    text-align: center;
    z-index: 1;
  }

  .popup__group {
    background: transparent;
    border-radius: 0.3em;
    max-inline-size: 100%;
    padding-inline: var(--inline-space);

    .btn {
      --btn-background: transparent;
    }

    @media (any-hover: hover) {
      &:hover {
        --hover-size: 0;

        background: var(--color-selected);
      }
    }

    &[aria-selected] {
      box-shadow: 0 0 0 var(--hover-size) var(--color-selected-dark) inset;
    }
  }

  .popup__item {
    --btn-background: transparent;
    --btn-border-radius: 0.3em;
    --btn-border-size: 0;
    --btn-font-weight: 500;
    --btn-padding: var(--inline-space-half) 0 var(--inline-space-half) var(--inline-space);
    --hover-size: 0;
    --outline-color: transparent;

    flex: 1 1 auto;
    justify-content: start;
    max-inline-size: 100%;
    text-align: start;

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        --btn-background: var(--color-selected);
      }
    }

    &[aria-selected] {
      border-radius: 0.3em;
      box-shadow: 0 0 0 var(--hover-size) var(--color-selected-dark) inset;
    }

    &:has(input:checked)  {
      --btn-background: transparent;
      --btn-color: var(--color-ink);

      .icon {
        &.checked {
          block-size: 1em;
          display: block;
          inline-size: 1em;
          margin-inline-end: var(--inline-space);
        }
      }

      @media (any-hover: hover) {
        &:where(:not(:active):hover) {
          --hover-size: 0;
          --btn-background: var(--color-selected);
        }
      }
    }
  }

  .popup__list {
    display: flex;
    flex-direction: column;
    inline-size: 100%;
    margin: 0;
    max-inline-size: 100%;
    padding: 0;
    row-gap: 2px;
  }

  .popup__new {
    --btn-icon-size: 1em;

    padding-inline-start: var(--inline-space-half);

    .icon {
      margin-inline-end: 0.5em;
    }
  }

  .popup__title {
    font-weight: 800;
  }

  .popup__group-title {
    font-size: var(--text-small);
    margin-block: var(--block-space-half);
    padding-inline: var(--inline-space);
    text-transform: uppercase;
  }
}

.popup--animated {
  --backdrop-speed: 150ms;
  --speed: 150ms;

  opacity: 0;
  transform: scale(0.2) translateX(-50%);
  transform-origin: top left;
  transition:
      display var(--speed) allow-discrete,
      opacity var(--speed),
      overlay var(--speed) allow-discrete,
      transform var(--speed);

  &::backdrop {
    background-color: var(--color-always-black);
    opacity: 0;
    transform: scale(1);
    transition:
      display var(--backdrop-speed) allow-discrete,
      opacity var(--backdrop-speed),
      overlay var(--backdrop-speed) allow-discrete;
  }

  &[open] {
    opacity: 1;
    transform: scale(1) translateX(-50%);

    &::backdrop {
      opacity: 0.5;
    }
  }

  @starting-style {
    &[open] {
      opacity: 0;
      transform: scale(0.2) translateX(-50%);
    }

    &[open]::backdrop {
      opacity: 0;
    }
  }
}
