/* ==========================================================================
   Select — every <select class="form-select"> gets a Moey dropdown instead of
   the browser's list. The <select> stays in the page (hidden), so forms,
   `required`, `value` and `change` work exactly as before.

   <select class="form-select" name="country">
     <option value="" disabled selected>Choose a country</option>      ← placeholder
     <optgroup label="Europe"><option>Portugal</option>…</optgroup>
     <option data-moey-icon="truck" data-moey-detail="2–3 days">Express</option>
     <option data-moey-color="green">Paid</option>                       ← a colored dot
   </select>

   Long lists (more than 10 options) get a search box; data-moey-search="true|false" to choose.
   `multiple` shows checkmarks and stays open. data-moey-native keeps the browser's select.
   ========================================================================== */

/* The real select: kept for forms and validation, out of sight (but where the field is,
   so the browser's "please choose" bubble points at the right place) */
.form-select-native {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

/* The field: a button that looks exactly like .form-select */
button.form-select {
  display: flex;
  align-items: center;
  gap: .5rem;
  font: inherit;
  font-size: var(--mo-control-font-size);
  text-align: start;
  cursor: pointer;
}
button.form-select:disabled { cursor: not-allowed; }
button.form-select[aria-expanded="true"] { border-color: var(--mo-theme-bg); box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring); }
.form-select-value { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.form-select-value.is-placeholder { color: var(--mo-control-placeholder); }
.form-select-value > .select-dot { display: inline-block; margin-inline-end: .5rem; vertical-align: .05em; }
.form-select-value > .mi, button.form-select > .mi { font-size: 1rem; color: var(--mo-fg-3); vertical-align: -.125em; margin-inline-end: .375rem; }
.form-select-count { flex-shrink: 0; }
/* Invalid / valid states follow the hidden select */
.form-select-native:is(.is-invalid, :user-invalid) + button.form-select {
  --mo-theme-bg: var(--mo-red-bg);
  --mo-theme-focus-ring: var(--mo-red-focus-ring);
  border-color: var(--mo-red-bg);
}
.form-select-native.is-valid + button.form-select { border-color: var(--mo-green-bg); }

/* The list ----------------------------------------------------------------------- */
.select-menu {
  --mo-menu-min-width: 0;
  --mo-menu-max-height: min(20rem, 60dvh);   /* moey.js lowers it when the field is near the edge */
}
.select-menu .menu-item { cursor: pointer; }
.select-menu .menu-item.is-active { background-color: var(--mo-menu-hover); }
.select-menu .menu-item[aria-disabled="true"] { cursor: default; opacity: .45; }
.select-menu .menu-item[aria-disabled="true"].is-active { background: none; }
.select-menu .menu-item[hidden] { display: none; }
.select-menu .menu-item-label > .mi { margin-inline-end: .5rem; font-size: 1rem; color: var(--mo-fg-3); vertical-align: -.15em; }
.select-menu .menu-item-label > .select-dot { display: inline-block; margin-inline-end: .5rem; vertical-align: .05em; }
.select-menu .menu-item-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.select-menu .menu-item[aria-selected="true"] { font-weight: var(--mo-font-weight-medium); }
/* A check on the chosen option(s) */
.select-menu .menu-item::after {
  flex-shrink: 0;
  width: 1rem;
  height: 1rem;
  margin-inline-start: auto;
  content: "";
}
.select-menu .menu-item[aria-selected="true"]::after {
  background: var(--mo-theme-fg);
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='black' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
}
.select-dot { flex-shrink: 0; width: .5rem; height: .5rem; background: var(--mo-theme-base); border-radius: 50%; }
.select-menu .menu-header[hidden] { display: none; }
.select-search { position: sticky; top: calc(-1 * var(--mo-menu-padding)); z-index: 1; padding: 0 0 .25rem; margin-top: calc(-1 * var(--mo-menu-padding)); padding-top: var(--mo-menu-padding); background: var(--mo-menu-bg); }
.select-empty { padding: .75rem; font-size: .8125rem; color: var(--mo-fg-3); text-align: center; }
.select-empty[hidden] { display: none; }
