/* ==========================================================================
   Dropdown menu — a trigger and a .menu, wrapped in .dropdown. moey.js does the rest.

   <div class="dropdown">
     <button class="btn dropdown-toggle">Options</button>
     <div class="menu">
       <button class="menu-item"><i class="mi mi-pencil"></i>Edit<span class="menu-shortcut">⌘E</span></button>
       <hr class="menu-divider">
       <button class="menu-item theme-red"><i class="mi mi-trash-2"></i>Delete</button>
     </div>
   </div>

   Opens on click (the browser's Popover API: top layer, click outside / Esc closes),
   positions itself below the trigger and flips when there's no room.
   Keyboard: ↑ ↓ Home End, type a letter to jump, Enter to choose, Esc to close.
   Placement: data-moey-placement="bottom-start|bottom-end|bottom-center|top-start|top-end|top-center|start|end|left|right|auto"
   on the .menu (default bottom-start; always flips to fit) · .menu-match: at least as wide as the button
   A .menu outside a .dropdown is a plain, static menu panel.
   ========================================================================== */

.menu {
  --mo-menu-bg: var(--mo-bg-raised);
  --mo-menu-padding: .3125rem;
  --mo-menu-radius: var(--mo-radius-overlay);
  --mo-menu-item-radius: max(var(--mo-menu-radius) - var(--mo-menu-padding), 2px);
  --mo-menu-item-height: 2rem;
  --mo-menu-min-width: 12rem;
  --mo-menu-hover: color-mix(in oklab, var(--mo-fg-body) 6%, transparent);
  display: flex;
  flex-direction: column;
  min-width: var(--mo-menu-min-width);
  max-width: min(22rem, 100vw - 1rem);
  padding: var(--mo-menu-padding);
  margin: 0;
  font-size: .875rem;
  color: var(--mo-fg-body);
  list-style: none;
  background: var(--mo-menu-bg);
  border: 0;
  border-radius: var(--mo-menu-radius);
  box-shadow: var(--mo-shadow-overlay);
}

/* As a popover: the browser hides it until open; moey.js sets top/left */
.menu[popover] {
  position: fixed;
  inset: auto;
  max-height: var(--mo-menu-max-height, calc(100dvh - 1rem));
  overflow-y: auto;
  overscroll-behavior: contain;
}
.dropdown { position: relative; display: inline-flex; vertical-align: middle; }
/* Closed = gone. (.menu sets display: flex, which would otherwise override the browser's own
   hiding of closed popovers and leave an invisible menu catching the mouse.) The close
   animation still plays: display is transitioned with allow-discrete. */
.menu[popover]:not(:popover-open) { display: none; }
.dropdown > .menu:not([popover]) { display: none; }   /* until moey.js makes it a popover */
.menu:focus-visible { outline: 0; }
/* Split buttons: <div class="btn-group dropdown">…<button class="btn dropdown-toggle">…<div class="menu"> */
.btn-group > .menu { margin: 0; border-radius: var(--mo-menu-radius); }

/* Items ------------------------------------------------------------------------ */
.menu-item {
  display: flex;
  flex-shrink: 0;
  gap: .625rem;
  align-items: center;
  width: 100%;
  min-height: var(--mo-menu-item-height);
  padding: .25rem .625rem;
  font: inherit;
  color: inherit;
  text-align: start;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  background: transparent;
  border: 0;
  border-radius: var(--mo-menu-item-radius);
  outline: 0;
}
.menu-item:is(:hover, :focus-visible, :focus) { color: var(--mo-menu-hover-color, inherit); background-color: var(--mo-menu-hover); }
.menu-item > .mi { flex-shrink: 0; font-size: 1rem; color: var(--mo-fg-3); }
.menu-item:is(.disabled, :disabled, [aria-disabled="true"]) { pointer-events: none; opacity: .45; }

/* Current item (e.g. the page you're on) */
.menu-item:is(.active, [aria-current]:not([aria-current="false"])) {
  font-weight: var(--mo-font-weight-medium);
  color: var(--mo-theme-fg-emphasis);
  background-color: var(--mo-theme-bg-subtle);
}
.menu-item:is(.active, [aria-current]:not([aria-current="false"])) > .mi { color: inherit; }

/* Destructive / colored items: <button class="menu-item theme-red"> */
.menu-item[class*="theme-"] { color: var(--mo-theme-fg); }
.menu-item[class*="theme-"] > .mi { color: inherit; }
.menu-item[class*="theme-"]:is(:hover, :focus) { background-color: var(--mo-theme-subtle); }

/* Right-side extras */
.menu-shortcut,
.menu-item > .badge,
.menu-item > .menu-meta { margin-inline-start: auto; }
.menu-shortcut,
.menu-meta { padding-inline-start: 1.5rem; font-size: .75rem; color: var(--mo-fg-3); letter-spacing: .02em; }
.menu-item > .menu-shortcut ~ .badge { margin-inline-start: 0; }

/* Two-line items */
.menu-item-content { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; white-space: normal; }
.menu-item-detail { font-size: .75rem; color: var(--mo-fg-3); }
.menu-item:has(.menu-item-detail) { padding-block: .4375rem; }

/* Check & radio items: role="menuitemcheckbox" / "menuitemradio" + aria-checked.
   moey.js toggles them and keeps the menu open. */
.menu-item:is([role="menuitemcheckbox"], [role="menuitemradio"])::before {
  flex-shrink: 0;
  width: 1rem;
  height: 1rem;
  content: "";
  background-color: currentcolor;
  mask: var(--mo-icon-check) center / contain no-repeat;
  opacity: 0;
}
.menu-item[role="menuitemradio"]::before {
  mask: radial-gradient(circle, #000 0 3px, transparent 3.5px) center / 1rem 1rem no-repeat;
}
.menu-item[aria-checked="true"]::before { opacity: 1; }

/* Submenus: a .dropdown inside a menu — its .menu-item gets a › and opens the inner .menu beside it
   <div class="dropdown"><button class="menu-item">Share</button><div class="menu">…</div></div> */
.menu .dropdown { position: static; display: flex; flex-direction: column; }
.menu .dropdown > .menu-item::after {
  flex-shrink: 0;
  width: .875rem;
  height: .875rem;
  margin-inline-start: auto;
  content: "";
  background-color: currentcolor;
  mask: var(--mo-icon-chevron) center / contain no-repeat;
  opacity: .55;
  rotate: -90deg;
}
.menu .dropdown > .menu-item:dir(rtl)::after { rotate: 90deg; }
.menu .dropdown > .menu-item > .menu-shortcut { margin-inline-end: .5rem; }
.menu .dropdown > .menu-item[aria-expanded="true"] { background-color: var(--mo-menu-hover); }

/* Structure -------------------------------------------------------------------- */
.menu-header {
  padding: .5rem .625rem .25rem;
  font-size: .6875rem;
  font-weight: var(--mo-font-weight-semibold);
  color: var(--mo-fg-3);
  text-transform: uppercase;
  letter-spacing: .05em;
}
.menu-divider {
  flex-shrink: 0;
  height: 0;
  margin: var(--mo-menu-padding) calc(var(--mo-menu-padding) * -1);
  border: 0;
  border-top: var(--mo-border-width) solid var(--mo-border-color);
  opacity: 1;
}
/* Free content: a profile summary, a small form, help text */
.menu-text { padding: .375rem .625rem; color: var(--mo-fg-3); white-space: normal; }
.menu-body { padding: .5rem .625rem; }
.menu > li { display: contents; }               /* <ul class="menu"><li>…</li></ul> works too */

/* Colors ------------------------------------------------------------------------
   .menu-<color> (menu-green …) or theme-*   neutral menu; the highlight, current item and checks take the color
   .menu-subtle   a light tint of the color (*-100, *-900 in dark mode)
   .menu-vivid    a full-color menu (*-600)        .menu-deep   a dark one (*-800)
   (menu-dark + menu-vivid = an inverse menu that stays dark in light mode)
   All built on the theme tokens, so light and dark mode just work. */
.menu:is([class*="theme-"], /*@colors menu*/):not(.menu-subtle, .menu-vivid, .menu-deep) {
  --mo-menu-hover: var(--mo-theme-subtle);
  --mo-menu-hover-color: var(--mo-theme-subtle-text);
}
.menu:is([class*="theme-"], /*@colors menu*/):not(.menu-subtle, .menu-vivid, .menu-deep) .menu-item:is(:hover, :focus) > .mi { color: var(--mo-theme-fg); }
.menu:is([class*="theme-"], /*@colors menu*/):not(.menu-subtle, .menu-vivid, .menu-deep) .menu-item[aria-checked="true"]::before { color: var(--mo-theme-fg); }

.menu-subtle {                                   /* the *-100 tint (a deep *-900 tint in dark mode) */
  --mo-menu-bg: var(--mo-theme-tint);
  --mo-menu-hover: var(--mo-theme-tint-hover);
  --mo-menu-hover-color: var(--mo-theme-fg-emphasis);
  --mo-menu-muted: var(--mo-theme-fg);           /* resolved here, so colored items inside keep the menu's color */
  color: var(--mo-theme-fg-emphasis);
}
.menu-subtle :is(.menu-item > .mi, .menu-header, .menu-shortcut, .menu-meta, .menu-text, .menu-item-detail) { color: var(--mo-menu-muted); }
.menu-subtle .menu-item:is(.active, [aria-current]:not([aria-current="false"])) { background-color: var(--mo-theme-tint-hover); }

/* Full-color menus: the fill and the text on it */
.menu-vivid { --_menu-fill: var(--mo-theme-vivid); --_menu-on: var(--mo-theme-vivid-text); }
.menu-deep  { --_menu-fill: var(--mo-theme-deep);  --_menu-on: var(--mo-theme-deep-text); }
:is(.menu-vivid, .menu-deep) {
  --mo-menu-bg: var(--_menu-fill);
  --mo-menu-hover: color-mix(in oklab, var(--_menu-on) 14%, transparent);
  --mo-menu-hover-color: var(--_menu-on);
  color: var(--_menu-on);
}
:is(.menu-vivid, .menu-deep) :is(.menu-item > .mi, .menu-header, .menu-shortcut, .menu-meta, .menu-text, .menu-item-detail) { color: inherit; opacity: .75; }
:is(.menu-vivid, .menu-deep) .menu-item:is(:hover, :focus) > .mi { opacity: 1; }
:is(.menu-vivid, .menu-deep) .menu-item:is(.active, [aria-current]:not([aria-current="false"])) {
  color: inherit;
  background-color: color-mix(in oklab, var(--_menu-on) 20%, transparent);
}
/* Colored items (e.g. theme-red) in tinted and full-color menus follow the menu — no red on a tinted or filled menu */
:is(.menu-subtle, .menu-vivid, .menu-deep) .menu-item[class*="theme-"] { color: inherit; }
:is(.menu-subtle, .menu-vivid, .menu-deep) .menu-item[class*="theme-"]:is(:hover, :focus) { color: var(--mo-menu-hover-color); background-color: var(--mo-menu-hover); }
.menu-subtle .menu-item[class*="theme-"] > .mi { color: var(--mo-menu-muted); }
:is(.menu-subtle, .menu-vivid, .menu-deep) .menu-divider { border-top-color: color-mix(in oklab, currentcolor 16%, transparent); }

.menu-sm { --mo-menu-item-height: 1.75rem; --mo-menu-min-width: 10rem; font-size: .8125rem; }
.menu-lg { --mo-menu-item-height: 2.375rem; --mo-menu-padding: .375rem; font-size: .9375rem; }

/* Trigger caret ------------------------------------------------------------------ */
.dropdown-toggle::after {
  flex-shrink: 0;
  width: .875rem;
  height: .875rem;
  margin-inline-end: -.125rem;
  content: "";
  background-color: currentcolor;
  mask: var(--mo-icon-chevron) center / contain no-repeat;
  opacity: .6;
  transition: rotate var(--mo-duration) var(--mo-ease);
}
.dropdown-toggle[aria-expanded="true"]::after { rotate: 180deg; }

/* Animation -------------------------------------------------------------------- */
@media (prefers-reduced-motion: no-preference) {
  .menu[popover] {
    transform-origin: var(--mo-menu-origin, top left);
    transition:
      opacity .12s var(--mo-ease),
      scale .12s var(--mo-ease),
      overlay .12s allow-discrete,
      display .12s allow-discrete;
  }
  .menu[popover]:not(:popover-open) { opacity: 0; scale: .96; }
  @starting-style {
    .menu[popover]:popover-open { opacity: 0; scale: .96; }
  }
}
