/* ============================================================
   EDITORUM — ED SELECT MENU
   Custom select-menu component that wraps a native <select>.
   Design borrowed from Nuxt UI's SelectMenu, rebuilt in plain CSS
   using editorum design tokens (see manage-tokens.css).

   DOM layout:
     <span class="ed-select-menu" data-ed-select-menu>
       <button class="ed-select-menu__trigger" ...>
         <span class="ed-select-menu__value">...</span>
         <span class="ed-select-menu__chevron"></span>
       </button>
     </span>

   The popover is portal-rendered into document.body so it can
   escape overflow:hidden / transform / z-index stacking contexts
   (Bootstrap 3 modals, sticky toolbars, workspace panels).
   ============================================================ */

/* ---------- Wrapper + native select containment -------------
   The wrapper is the positioning root for the trigger.  The native
   <select> stays in the DOM (focusable for FormValidation, submits
   with the form, serves as the data source) but is visually hidden
   via the standard sr-only / visually-hidden pattern.  We do NOT use
   display:none because FormValidation needs to .focus() it on error.
   The wrapper + trigger are emitted in SSR (Twig form theme) so
   there is no layout shift when JS hydrates — the trigger visual
   is pixel-identical to the hydrated state. */
.ed-select-menu {
  display: block;
  min-width: 0;
  position: relative;
  width: 100%;
}

/* In flex layouts, parent containers can grow with content unless
   constrained. Ensure any col / form-group hosting the component
   respects its flex-basis and allows the trigger to ellipsize. */
[class*="col-"]:has(> .ed-select-menu),
.form-group:has(> .ed-select-menu),
[class*="col-"]:has(> .form-group > .ed-select-menu) {
  min-width: 0;
}

/* When selectpicker claims a select inside our SSR shell, hide our
   trigger — selectpicker renders its own UI.  The .bootstrap-select
   wrapper appears as a sibling of the trigger inside .ed-select-menu. */
.ed-select-menu:has(.bootstrap-select) > .ed-select-menu__trigger {
  display: none;
}
.ed-select-menu:has(.bootstrap-select) > select.ssr-native-select {
  position: static !important;
  width: auto !important;
  height: auto !important;
  padding: initial !important;
  margin: 0 !important;
  border: initial !important;
  clip: auto !important;
  clip-path: none !important;
  overflow: visible !important;
  white-space: normal !important;
  opacity: 1 !important;
  pointer-events: auto !important;
  min-height: initial !important;
  max-height: none !important;
}

/* Native <select> inside the wrapper: always hidden, regardless of
   whether JS has hydrated yet.  Uses the visually-hidden pattern so
   the element is still focusable and form-submittable.  */
.ed-select-menu > select.ssr-native-select,
.ed-select-menu > select.ssr-native-select[multiple] {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  border: 0 !important;
  clip: rect(0 0 0 0) !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  white-space: nowrap !important;
  opacity: 0 !important;
  pointer-events: none !important;
  min-height: 0 !important;
  max-height: 1px !important;
  background: transparent !important;
  box-shadow: none !important;
}

/* ---------- Trigger button ---------------------------------- */
.ed-select-menu__trigger {
  align-items: center;
  background: var(--ed-white);
  border: 1px solid var(--ed-border);
  border-radius: var(--ed-radius-md);
  color: var(--ed-text);
  cursor: pointer;
  display: flex;
  font: 400 14px/20px var(--ed-font);
  gap: var(--ed-space-2);
  height: var(--ed-control-h);
  margin: 0;
  min-height: var(--ed-control-h);
  min-width: 0;
  padding: 9px var(--ed-space-8) 9px var(--ed-space-3);
  position: relative;
  text-align: left;
  transition: border-color var(--ed-transition), box-shadow var(--ed-transition);
  width: 100%;
}

.ed-select-menu__trigger:hover:not(:disabled):not(.is-disabled) {
  border-color: var(--ed-blue);
}

.ed-select-menu__trigger:focus,
.ed-select-menu__trigger.is-open {
  border-color: var(--ed-blue);
  box-shadow: var(--ed-focus-ring);
  outline: 0;
}

.ed-select-menu__trigger.is-disabled,
.ed-select-menu__trigger:disabled {
  background: var(--ed-bg);
  color: var(--ed-muted);
  cursor: not-allowed;
  opacity: 0.7;
}

.ed-select-menu__value {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ed-select-menu__value.is-placeholder {
  color: var(--ed-placeholder);
}

.ed-select-menu__chevron {
  background: var(--ed-body);
  display: block;
  flex: 0 0 auto;
  height: var(--ed-space-4);
  pointer-events: none;
  position: absolute;
  right: var(--ed-space-2h);
  top: 50%;
  transform: translateY(-50%);
  width: var(--ed-space-4);
  -webkit-mask: var(--ed-chevron) center/contain no-repeat;
          mask: var(--ed-chevron) center/contain no-repeat;
  transition: transform var(--ed-transition);
}

.ed-select-menu__trigger.is-open .ed-select-menu__chevron {
  transform: translateY(-50%) rotate(180deg);
}

/* Has-error styling plays nice with formvalidation's parent class. */
.has-error > .ed-select-menu .ed-select-menu__trigger,
.has-error .ed-select-menu > .ed-select-menu__trigger {
  border-color: var(--ed-danger);
}
.has-error > .ed-select-menu .ed-select-menu__trigger:focus,
.has-error .ed-select-menu > .ed-select-menu__trigger:focus {
  box-shadow: var(--ed-focus-ring-danger);
}

/* ---------- Popover (portal-rendered) ----------------------- */
.ed-select-menu__popover {
  background: var(--ed-white);
  border: 1px solid var(--ed-border);
  border-radius: var(--ed-radius-md);
  box-shadow: var(--ed-shadow-lg);
  display: flex;
  flex-direction: column;
  font: 400 14px/20px var(--ed-font);
  left: 0;
  max-height: calc(100vh - 24px);
  min-width: 180px;
  opacity: 0;
  pointer-events: none;
  position: absolute;
  top: 0;
  transform: translateY(-4px);
  transition: opacity var(--ed-transition-fast), transform var(--ed-transition-fast);
  /* High enough to sit above Bootstrap 3 modals (.modal = 1050,
     .modal-backdrop = 1040) and admin sticky chrome. */
  z-index: 2000;
}

.ed-select-menu__popover.is-open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* ---------- Header (search + actions) ----------------------- */
.ed-select-menu__header {
  border-bottom: 1px solid var(--ed-border);
  display: flex;
  flex-direction: column;
  flex: 0 0 auto;
  gap: var(--ed-space-1h);
  padding: var(--ed-space-1h);
}

.ed-select-menu__search {
  background: var(--ed-white);
  border: 1px solid var(--ed-border);
  border-radius: var(--ed-radius-md);
  color: var(--ed-text);
  display: block;
  font: 400 14px/20px var(--ed-font);
  min-height: var(--ed-control-h-sm);
  padding: var(--ed-space-1h) var(--ed-space-3) var(--ed-space-1h) var(--ed-space-9);
  width: 100%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='%236b7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='7' cy='7' r='4.5'/%3E%3Cpath d='m13 13-3.1-3.1'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: var(--ed-space-3) center;
  background-size: var(--ed-space-4) var(--ed-space-4);
}

.ed-select-menu__search:focus {
  border-color: var(--ed-blue);
  box-shadow: var(--ed-focus-ring);
  outline: 0;
}

.ed-select-menu__actions {
  display: flex;
  gap: var(--ed-space-1h);
}

.ed-select-menu__action {
  background: var(--ed-bg);
  border: 1px solid var(--ed-border);
  border-radius: var(--ed-radius-sm);
  color: var(--ed-body);
  cursor: pointer;
  font: 500 12px/16px var(--ed-font);
  height: var(--ed-control-h-xs);
  padding: 0 var(--ed-space-2h);
  transition: background var(--ed-transition-fast), color var(--ed-transition-fast), border-color var(--ed-transition-fast);
}
.ed-select-menu__action:hover,
.ed-select-menu__action:focus {
  background: var(--ed-primary-bg);
  border-color: var(--ed-blue);
  color: var(--ed-blue);
  outline: 0;
}

/* ---------- Listbox + items --------------------------------- */
.ed-select-menu__listbox {
  flex: 1 1 auto;
  list-style: none;
  margin: 0;
  max-height: 320px;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--ed-space-1);
  scroll-behavior: smooth;
}

.ed-select-menu__item {
  align-items: center;
  border-radius: var(--ed-radius-sm);
  color: var(--ed-body);
  cursor: pointer;
  display: flex;
  font: 400 14px/20px var(--ed-font);
  gap: var(--ed-space-2);
  padding: var(--ed-space-2) var(--ed-space-9) var(--ed-space-2) var(--ed-space-3);
  position: relative;
  user-select: none;
}

.ed-select-menu__item-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ed-select-menu__item-check {
  background: currentColor;
  display: block;
  flex: 0 0 var(--ed-space-4);
  height: var(--ed-space-4);
  opacity: 0;
  width: var(--ed-space-4);
  --ed-sm-check: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 8.5 3 3 5.5-6'/%3E%3C/svg%3E");
  -webkit-mask: var(--ed-sm-check) center/contain no-repeat;
          mask: var(--ed-sm-check) center/contain no-repeat;
}

.ed-select-menu__item[aria-selected="true"] {
  background: var(--ed-primary-bg);
  color: var(--ed-blue);
  font-weight: 500;
}
.ed-select-menu__item[aria-selected="true"] .ed-select-menu__item-check {
  opacity: 1;
}

.ed-select-menu__item.is-highlighted {
  background: var(--ed-primary-bg);
  color: var(--ed-blue);
}

.ed-select-menu__item[aria-disabled="true"] {
  color: var(--ed-placeholder);
  cursor: not-allowed;
  opacity: 0.6;
}
.ed-select-menu__item[aria-disabled="true"]:hover {
  background: transparent;
}

/* ---------- Optgroup (label + collapsible) ------------------ */
.ed-select-menu__group {
  padding: 0;
}
.ed-select-menu__group + .ed-select-menu__group {
  border-top: 1px solid var(--ed-border);
  margin-top: var(--ed-space-1);
  padding-top: var(--ed-space-1);
}

.ed-select-menu__group-label {
  align-items: center;
  color: var(--ed-muted);
  display: flex;
  font: 600 11px/16px var(--ed-font);
  gap: var(--ed-space-1h);
  letter-spacing: 0.04em;
  padding: var(--ed-space-1h) var(--ed-space-3);
  text-transform: uppercase;
  user-select: none;
}

.ed-select-menu__group-label--collapsible {
  background: transparent;
  border: 0;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.ed-select-menu__group-label--collapsible:hover,
.ed-select-menu__group-label--collapsible:focus {
  color: var(--ed-body);
  outline: 0;
}

.ed-select-menu__group-label--collapsible::before {
  background: currentColor;
  content: "";
  display: block;
  flex: 0 0 auto;
  height: var(--ed-space-3);
  width: var(--ed-space-3);
  --ed-sm-caret: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='none' stroke='currentColor' stroke-width='2.25' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m3 4 3 3 3-3'/%3E%3C/svg%3E");
  -webkit-mask: var(--ed-sm-caret) center/contain no-repeat;
          mask: var(--ed-sm-caret) center/contain no-repeat;
  transition: transform var(--ed-transition-fast);
}
.ed-select-menu__group.is-collapsed .ed-select-menu__group-label--collapsible::before {
  transform: rotate(-90deg);
}
.ed-select-menu__group.is-collapsed .ed-select-menu__group-items {
  display: none;
}

.ed-select-menu__group-items {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* ---------- Empty state ------------------------------------- */
.ed-select-menu__empty {
  color: var(--ed-muted);
  font: 400 14px/20px var(--ed-font);
  font-style: italic;
  padding: var(--ed-space-4) var(--ed-space-3);
  text-align: center;
}

/* ---------- Item subtext (AJAX results) --------------------- */
.ed-select-menu__item-label:has(.ed-select-menu__item-subtext) {
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 18px;
}

.ed-select-menu__item-subtext {
  color: var(--ed-muted);
  font: 400 12px/16px var(--ed-font);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ed-select-menu__item[aria-selected="true"] .ed-select-menu__item-subtext {
  color: var(--ed-blue);
  opacity: 0.7;
}

/* ---------- AJAX state container ---------------------------- */
.ed-select-menu__ajax-state {
  align-items: center;
  color: var(--ed-muted);
  display: flex;
  font: 400 14px/20px var(--ed-font);
  font-style: italic;
  gap: var(--ed-space-2);
  justify-content: center;
  padding: var(--ed-space-6) var(--ed-space-3);
  text-align: center;
}

.ed-select-menu__ajax-state--error {
  color: var(--ed-danger);
}

/* ---------- Loading spinner --------------------------------- */
.ed-select-menu__spinner {
  animation: ed-sm-spin 0.6s linear infinite;
  border: 2px solid var(--ed-border);
  border-radius: 50%;
  border-top-color: var(--ed-blue);
  display: inline-block;
  flex: 0 0 auto;
  height: 16px;
  width: 16px;
}

@keyframes ed-sm-spin {
  to { transform: rotate(360deg); }
}

/* Search input loading indicator — replaces the magnifier icon
   with an animated SVG spinner while AJAX is in flight.
   Uses an SVG animation inside the background-image since <input>
   cannot have pseudo-elements. */
.ed-select-menu__search.is-loading {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Ccircle cx='8' cy='8' r='6' fill='none' stroke='%23e5e7eb' stroke-width='2'/%3E%3Cpath d='M14 8a6 6 0 0 0-6-6' fill='none' stroke='%231a56db' stroke-width='2' stroke-linecap='round'%3E%3CanimateTransform attributeName='transform' type='rotate' from='0 8 8' to='360 8 8' dur='0.6s' repeatCount='indefinite'/%3E%3C/path%3E%3C/svg%3E");
}

/* ---------- Multi-select count chip ------------------------- */
.ed-select-menu__trigger .ed-select-menu__count {
  background: var(--ed-primary-bg);
  border-radius: 999px;
  color: var(--ed-blue);
  flex: 0 0 auto;
  font: 500 12px/16px var(--ed-font);
  margin-left: var(--ed-space-1h);
  padding: 1px var(--ed-space-2);
}
