/* -- Dropdown component ---------------------------------------- */

@layer components {
  .dropdown-content {
    background-color: var(--popover);
    color: var(--popover-foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 0.25rem;
    min-width: 8rem;
    box-shadow: 0 4px 16px oklch(0 0 0 / 0.12), 0 0 0 1px var(--border);

    /* -- Anchor positioning ------------------------------------ */
    position: fixed;
    inset: auto;
    top: anchor(bottom);
    left: anchor(left);
    margin: 0;
    margin-top: 4px;
    /* the preferred edge first; when the menu would leave the viewport it
       flips to the trigger's OTHER edge (flip-inline swaps left: anchor(left)
       <-> right: anchor(right)), above the trigger (flip-block), or both */
    position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;

    /* data-align="end": the menu's end edge lines up with the trigger's end
       (a split button's chevron, an icon trigger at a row's end) instead of
       hanging out past it - and falls back to start-aligned under the
       trigger when there is no room for that (the fallbacks above) */
    &[data-align="end"] {
      left: auto;
      right: anchor(right);
    }

    /* -- Animation --------------------------------------------- */
    opacity: 0;
    transform: scale(0.96) translateY(-0.25rem);
    /* overlay keeps a closing menu in the top layer until its exit
       animation ends - otherwise a closing SUBMENU drops back into its
       parent menu for 150ms, where the parent's transform makes it the
       containing block: the parent grew scrollbars and resized */
    transition: opacity 150ms ease, transform 150ms ease,
                overlay 150ms allow-discrete,
                display 150ms allow-discrete;
    /* a menu never scrolls sideways; submenus live in the top layer */
    overflow-x: hidden;

    &:popover-open {
      opacity: 1;
      transform: scale(1) translateY(0);
    }
  }

  @starting-style {
    .dropdown-content:popover-open {
      opacity: 0;
      transform: scale(0.96) translateY(-0.25rem);
    }
  }

  .dropdown-item {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.375rem 0.5rem;
    border-radius: calc(var(--radius) * 0.6);
    font-size: 0.875rem;
    border: none;
    background: transparent;
    color: var(--foreground);
    cursor: pointer;
    outline: none;
    text-align: start;
    text-decoration: none;
    font: inherit;
    font-size: 0.875rem;

    &:hover, &[data-highlighted] {
      background-color: var(--accent);
      color: var(--accent-foreground);
    }

    &[data-variant="destructive"] {
      &:hover, &[data-highlighted] {
        background-color: var(--destructive);
        color: var(--destructive-foreground);
      }
    }

    &:disabled, &[aria-disabled="true"] {
      pointer-events: none;
      opacity: 0.5;
    }

    /* data-inset: lines a plain item (or a label) up with the text of
       checkbox / radio items */
    &[data-inset] { padding-inline-start: 1.5rem; }
    & > svg { width: 1rem; height: 1rem; flex-shrink: 0; color: var(--muted-foreground); }
    &[data-variant="destructive"] > svg { color: currentColor; }
  }
  .dropdown-label[data-inset] { padding-inline-start: 1.5rem; }

  /* -- Submenus ----------------------------------------------------
     <div class="dropdown-sub">
       <button class="dropdown-item dropdown-sub-trigger" role="menuitem">…</button>
       <div class="dropdown-content dropdown-sub-content" role="menu" popover>…</div>
     </div>
     The submenu is a DOM descendant of its menu, so the Popover API keeps
     the parent open while it shows, closes open siblings and light-dismisses
     the whole tree - any depth. It opens beside its trigger (flipping to
     the other side, or up, when there is no room); dropdown.js anchors it. */
  .dropdown-sub { display: contents; }
  .dropdown-sub-trigger {
    &::after {
      content: "";
      width: 1rem;
      height: 1rem;
      margin-inline-start: auto;
      flex-shrink: 0;
      background-color: currentColor;
      opacity: 0.6;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 18 6-6-6-6'/%3E%3C/svg%3E") center / contain no-repeat;
    }
    &:dir(rtl)::after { scale: -1 1; }
    /* the shortcut sits before the chevron */
    & .dropdown-shortcut { margin-inline-start: auto; }
    &:has(.dropdown-shortcut)::after { margin-inline-start: 0.5rem; }
    &[aria-expanded="true"] {
      background-color: var(--accent);
      color: var(--accent-foreground);
    }
  }
  .dropdown-content.dropdown-sub-content {
    top: anchor(top);
    left: anchor(right);
    margin: 0;
    margin-block-start: calc(-0.25rem - 1px);
    margin-inline-start: 0.125rem;
    position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
    transform: scale(0.96) translateX(-0.25rem);

    &:popover-open { transform: none; }
    &:dir(rtl) { left: auto; right: anchor(left); }
  }
  @starting-style {
    .dropdown-content.dropdown-sub-content:popover-open {
      opacity: 0;
      transform: scale(0.96) translateX(-0.25rem);
    }
  }

  /* -- Checkbox / Radio indicators ----------------------------- */
  .dropdown-check,
  .dropdown-radio {
    padding-inline-start: 1.5rem;
    position: relative;

    &::before {
      content: '';
      position: absolute;
      inset-inline-start: 0.375rem;
      top: 50%;
      transform: translateY(-50%);
      width: 0.875rem;
      height: 0.875rem;
    }
  }

  /* Mask (not background-image): a data: URI SVG resolves currentColor to
     BLACK when used as an image, making the checks invisible in dark mode.
     Masking paints them with background-color: currentColor instead. */
  .dropdown-check[aria-checked="true"]::before,
  .dropdown-radio[aria-checked="true"]::before {
    background-color: currentColor;
  }

  .dropdown-check[aria-checked="true"]::before {
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
  }

  .dropdown-radio[aria-checked="true"]::before {
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='black'%3E%3Ccircle cx='12' cy='12' r='4.5'/%3E%3C/svg%3E");
  }

  .dropdown-check[aria-checked="true"]::before,
  .dropdown-radio[aria-checked="true"]::before {
    mask-size: contain;
    mask-repeat: no-repeat;
  }

  .dropdown-separator {
    height: 1px;
    background: var(--border);
    margin: 0.25rem -0.25rem;
  }

  .dropdown-label {
    padding: 0.375rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted-foreground);
  }

  .dropdown-shortcut {
    margin-inline-start: auto;
    font-size: 0.75rem;
    color: var(--muted-foreground);
    letter-spacing: 0.05em;
  }
  /* on a destructive highlight the shortcut follows the item's foreground
     (muted grey on red was unreadable) */
  .dropdown-item[data-variant="destructive"]:is(:hover, [data-highlighted]) .dropdown-shortcut { color: inherit; opacity: 0.8; }

  /* -- Sizes ----------------------------------------------------
     data-size goes on the .dropdown-content popover; items, labels,
     shortcuts and the check indicators scale together. md == the unsized
     default. The [data-size] compound outranks the single-class item
     rules; it only touches block padding/font so the check/radio inline
     padding (padding-inline-start) keeps working. */
  .dropdown-content[data-size="xs"] {
    & .dropdown-item { padding-block: 0.1875rem; font-size: 0.75rem; }
    & .dropdown-label { padding-block: 0.1875rem; font-size: 0.6875rem; }
    & .dropdown-shortcut { font-size: 0.6875rem; }
    & :is(.dropdown-check, .dropdown-radio)::before { width: 0.75rem; height: 0.75rem; }
  }
  .dropdown-content[data-size="sm"] {
    & .dropdown-item { padding-block: 0.25rem; font-size: 0.8125rem; }
    & .dropdown-shortcut { font-size: 0.75rem; }
  }
  .dropdown-content[data-size="md"] {
    & .dropdown-item { padding-block: 0.375rem; font-size: 0.875rem; }
  }
  .dropdown-content[data-size="lg"] {
    & .dropdown-item { padding-block: 0.5rem; font-size: 1rem; }
    & .dropdown-label { font-size: 0.8125rem; }
    & .dropdown-shortcut { font-size: 0.875rem; }
  }
  .dropdown-content[data-size="xl"] {
    & .dropdown-item { padding-block: 0.625rem; font-size: 1.125rem; }
    & .dropdown-label { font-size: 0.875rem; }
    & .dropdown-shortcut { font-size: 1rem; }
  }

  /* -- Accessibility ------------------------------------------ */

  @media (prefers-reduced-motion: reduce) {
    .dropdown-content {
      transition: none;
    }
    .dropdown-item {
      transition: none;
    }
  }

  @media (forced-colors: active) {
    .dropdown-content {
      border-color: ButtonText;
    }
    .dropdown-item {
      &:hover, &[data-highlighted] {
        forced-color-adjust: none;
        background: Highlight;
        color: HighlightText;
      }
    }
  }
}
