/* =====================================================================
   OSE · Select & dropdown
   ---------------------------------------------------------------------
   (a) .select   a styled NATIVE <select>: the real control in forms.
       The wrapper draws the chevron (a mask can't sit on <select>);
       the control borrows the field box from .input (input.css).
   (b) .dropdown a custom select-only combobox (WAI-ARIA APG) that
       ENHANCES the same native select. Add data-js="dropdown" to the
       <select>; js/components/dropdown.js hides it (it stays in the form
       and in sync, so it still submits), then builds a combobox trigger
       and a listbox next to it. Without JS the native select remains.

   Markup (authored; works with or without JS)
     <div class="select">
       <select class="input select__control" id="programme"
               name="programme" data-js="dropdown" required>
         <option value="">Chọn chương trình</option>   value="" = placeholder
         <option>Tiếng Anh trẻ em</option> …
       </select>
     </div>

   Markup (built by dropdown.js; the styleguide renders it statically)
     <div class="select dropdown">
       <select class="input select__control dropdown__native" …>
       <div class="input dropdown__trigger" role="combobox" tabindex="0"
            aria-haspopup="listbox" aria-expanded="false"
            aria-controls="…-listbox" aria-labelledby="…-label">
         <span class="dropdown__value">Tiếng Anh trẻ em</span></div>
       <ul class="dropdown__listbox" id="…-listbox" role="listbox" hidden>
         <li class="dropdown__option" role="option" aria-selected="true">…</li>
       </ul>
     </div>

   States
     trigger   default · hover · focus (:focus-visible) · active ·
               disabled (:disabled / aria-disabled="true") ·
               error (aria-invalid="true") — all from .input ·
               open (aria-expanded="true": chevron flips, listbox shown)
     option    default · hover · active descendant (.dropdown__option--focus,
               set by JS from aria-activedescendant) · pressed (:active) ·
               selected (aria-selected="true", check icon) ·
               disabled (aria-disabled="true")
     placeholder  .dropdown__value[data-placeholder]
   Load after input.css (both are single classes; this file adds to .input).
   Tokens   --select-*, --dropdown-* only.
   JS       data-js="dropdown" → js/components/dropdown.js
   ===================================================================== */

@layer components {
  .select {
    position: relative;
    display: block;
    min-inline-size: 0;
  }

  /* Chevron of the native select (the dropdown trigger draws its own). */
  .select::after {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline-end: 0;
    inline-size: var(--select-padding-inline-end);
    background-color: var(--select-chevron-color);
    -webkit-mask: var(--select-chevron-icon) center / var(--select-chevron-size) no-repeat;
    mask: var(--select-chevron-icon) center / var(--select-chevron-size) no-repeat;
    pointer-events: none;
  }

  .select:has(.select__control:disabled)::after {
    background-color: var(--select-chevron-color-disabled);
  }

  .select .select__control {
    padding-inline-end: var(--select-padding-inline-end);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
  }

  .select .select__control:disabled {
    cursor: not-allowed;
  }

  /* The option list some platforms draw from page styles. */
  .select__control option {
    background-color: var(--select-option-bg);
    color: var(--select-option-text);
  }

  /* -------------------------------------------------------------------
     Dropdown (enhanced)
     ------------------------------------------------------------------- */
  .dropdown::after {
    content: none;
  }

  /* The native select stays in the form, under the trigger, invisible:
     it submits the value and anchors the browser's validation bubble. */
  .select.dropdown .dropdown__native {
    position: absolute;
    inset: 0;
    opacity: 0;
    pointer-events: none;
  }

  .select .dropdown__trigger {
    position: relative;
    display: flex;
    align-items: center;
    padding-inline-end: var(--select-padding-inline-end);
    text-align: start;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
  }

  .dropdown__trigger::after {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline-end: 0;
    inline-size: var(--select-padding-inline-end);
    background-color: var(--select-chevron-color);
    -webkit-mask: var(--select-chevron-icon) center / var(--select-chevron-size) no-repeat;
    mask: var(--select-chevron-icon) center / var(--select-chevron-size) no-repeat;
    pointer-events: none;
    transition: transform var(--select-chevron-duration) var(--select-chevron-ease);
  }

  .dropdown__trigger[aria-expanded="true"]::after {
    transform: rotate(var(--select-chevron-rotate-open));
  }

  .select .dropdown__trigger[aria-disabled="true"] {
    cursor: not-allowed;
  }

  .dropdown__trigger[aria-disabled="true"]::after {
    background-color: var(--select-chevron-color-disabled);
  }

  .dropdown__value {
    flex: 1;
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .dropdown__value[data-placeholder] {
    color: var(--dropdown-placeholder);
  }

  .dropdown__trigger[aria-disabled="true"] .dropdown__value {
    color: inherit;
  }

  /* Listbox: enter / exit motion is played by dropdown.js (transform +
     opacity, dropdown motion tokens). */
  .dropdown__listbox {
    position: absolute;
    inset-inline: 0;
    inset-block-start: calc(100% + var(--dropdown-offset));
    z-index: var(--dropdown-z);
    display: grid;
    align-content: start;
    max-block-size: var(--dropdown-max-height);
    margin: 0;
    padding: var(--dropdown-padding);
    overflow-y: auto;
    overscroll-behavior: contain;
    list-style: none;
    border: var(--dropdown-border-width) solid var(--dropdown-border);
    border-radius: var(--dropdown-radius);
    background-color: var(--dropdown-bg-base);
    background-image: linear-gradient(var(--dropdown-bg), var(--dropdown-bg));
    color: var(--dropdown-text);
    box-shadow: var(--dropdown-shadow);
  }

  .dropdown__option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--dropdown-option-gap);
    min-block-size: var(--dropdown-option-height);
    padding-block: var(--dropdown-option-padding-block);
    padding-inline: var(--dropdown-option-padding-inline);
    border-radius: var(--dropdown-option-radius);
    font-size: var(--dropdown-option-font-size);
    cursor: pointer;
    transition: background-color var(--dropdown-option-duration) var(--dropdown-option-ease);
  }

  @media (hover: hover) {
    .dropdown__option:hover {
      background-color: var(--dropdown-option-bg-hover);
    }
  }

  /* Active descendant: the option keyboard focus is on. */
  .dropdown__option--focus {
    background-color: var(--dropdown-option-bg-hover);
    box-shadow: var(--dropdown-option-focus-ring);
  }

  .dropdown__option:active {
    background-color: var(--dropdown-option-bg-active);
  }

  .dropdown__option[aria-selected="true"] {
    background-color: var(--dropdown-option-bg-selected);
    color: var(--dropdown-option-text-selected);
    font-family: var(--dropdown-option-font-family-selected);
    font-weight: var(--dropdown-option-weight-selected);
  }

  .dropdown__option[aria-selected="true"]::after {
    content: "";
    flex: none;
    inline-size: var(--dropdown-option-check-size);
    block-size: var(--dropdown-option-check-size);
    background-color: var(--dropdown-option-check-color);
    -webkit-mask: var(--dropdown-option-check-icon) center / contain no-repeat;
    mask: var(--dropdown-option-check-icon) center / contain no-repeat;
  }

  .dropdown__option[aria-disabled="true"] {
    background-color: var(--dropdown-option-bg-disabled);
    color: var(--dropdown-option-text-disabled);
    cursor: not-allowed;
  }

  /* Forced colours: masks paint with background-color (reset to Canvas
     by the UA) and box-shadow is dropped, so repaint icons and give the
     active option a real outline. */
  @media (forced-colors: active) {
    .select::after,
    .dropdown__trigger::after,
    .dropdown__option[aria-selected="true"]::after {
      forced-color-adjust: none;
      background-color: CanvasText;
    }

    .dropdown__option--focus {
      outline: var(--dropdown-option-focus-width) solid Highlight;
      outline-offset: calc(var(--dropdown-option-focus-width) * -1);
    }
  }
}
