/* Gravity dropdowns: one look for every <select> on every surface (issue #149).
 *
 * The contract: THIS sheet owns how a dropdown looks (border, corner, chevron,
 * colors, hover, focus, disabled, and the open list where the browser allows
 * it). Screen stylesheets own only size and placement: width, height,
 * min-height, font-size, margins and the left padding. A screen that needs a
 * different look adds a token override (--gd-select-*), not another rule.
 *
 * Why !important: about fifteen screen stylesheets each restyle dropdowns,
 * several with id-level selectors or !important of their own, and several use
 * the `background` shorthand, which would wipe the chevron. Pinning the few
 * identity properties here is what makes this sheet the single source; size
 * properties are deliberately left un-pinned.
 *
 * Colors come from whichever token set the surface defines: the Desk's
 * --sheet/--rule/--ink/--sign, the Help Center and Support pages'
 * --surface/--line/--ink|--text/--accent, then system colors. They resolve on
 * the element, so per-workspace accents and Help Center themes carry through,
 * and dark mode follows each surface's own switch.
 *
 * The open list: Chrome and Edge (135+) support `appearance: base-select`,
 * which lets us draw the list as a Gravity panel. It is enabled only for a
 * fine pointer, so phones and tablets keep their own picker sheet. Every other
 * browser shows its system list; the closed control still looks the same.
 */

:is(select:not([multiple]):not([size]):not(.picker-native), select[multiple], select[size]){
  --_bg:var(--gd-select-bg, var(--sheet, var(--surface, Field)));
  --_ink:var(--gd-select-ink, var(--ink, var(--text, FieldText)));
  --_line:var(--gd-select-line, var(--rule, var(--line, color-mix(in srgb, currentColor 28%, transparent))));
  --_accent:var(--gd-select-accent, var(--sign, var(--accent, Highlight)));
  --_danger:var(--gd-select-danger, var(--danger, #C4352F));
  --_radius:var(--gd-select-radius, var(--control-radius, 9px));
  --_hover:color-mix(in srgb, var(--_ink) 7%, transparent);
  --_ease:var(--gd-ease, cubic-bezier(.2,.8,.2,1));
}

/* ---------- The closed control ---------- */
select:not([multiple]):not([size]):not(.picker-native){
  appearance:none!important;-webkit-appearance:none!important;
  box-sizing:border-box;
  padding-inline-end:32px!important;
  border:1px solid var(--_line)!important;
  border-radius:var(--_radius)!important;
  background-color:var(--_bg)!important;
  /* Lucide chevron-down in a mid-grey that reads on light and dark grounds.
     A same-origin file, not a data: URI: the public Help Center and Support
     pages send `img-src 'self'`, which blocks data: images. */
  background-image:url("select-chevron.svg")!important;
  background-repeat:no-repeat!important;
  background-position:right 10px center!important;
  background-size:14px 14px!important;
  color:var(--_ink)!important;
  box-shadow:0 1px 1px color-mix(in srgb, var(--_ink) 5%, transparent)!important;
  cursor:pointer;
  text-overflow:ellipsis;
  transition:border-color 120ms var(--_ease), background-color 120ms var(--_ease), box-shadow 120ms var(--_ease);
}
select:not([multiple]):not([size]):not(.picker-native):hover:not(:disabled){
  border-color:color-mix(in srgb, var(--_line), var(--_ink) 32%)!important;
  background-color:color-mix(in srgb, var(--_bg), var(--_ink) 3%)!important;
}
select:not([multiple]):not([size]):not(.picker-native):active:not(:disabled){
  background-color:color-mix(in srgb, var(--_bg), var(--_ink) 6%)!important;
}

/* ---------- Every dropdown and list box: focus, state, disabled ---------- */
:is(select:not([multiple]):not([size]):not(.picker-native), select[multiple], select[size]):focus-visible{
  outline:2px solid var(--_accent)!important;
  outline-offset:2px!important;
  border-color:var(--_accent)!important;
  box-shadow:none!important;
}
/* "Changed, not saved yet" marker used by Settings, Dashboards and Knowledge. */
:is(select:not([multiple]):not([size]):not(.picker-native), select[multiple], select[size]).chg{
  border-color:var(--_accent)!important;
  box-shadow:0 0 0 1px var(--_accent)!important;
}
:is(select:not([multiple]):not([size]):not(.picker-native), select[multiple], select[size])[aria-invalid=true]{
  border-color:var(--_danger)!important;
  box-shadow:0 0 0 1px var(--_danger)!important;
}
:is(select:not([multiple]):not([size]):not(.picker-native), select[multiple], select[size]):disabled{
  opacity:.55!important;
  cursor:not-allowed!important;
  box-shadow:none!important;
}

/* ---------- List boxes (multiple / size): same frame, no chevron ---------- */
:is(select[multiple], select[size]){
  box-sizing:border-box;
  padding:4px!important;
  border:1px solid var(--_line)!important;
  border-radius:var(--_radius)!important;
  background-color:var(--_bg)!important;
  background-image:none!important;
  color:var(--_ink)!important;
}
:is(select[multiple], select[size]) option{padding:6px 8px;border-radius:6px}
:is(select[multiple], select[size]) option:checked{
  background:color-mix(in srgb, var(--_accent) 16%, var(--_bg));
  color:var(--_ink);
}

/* Native lists that inherit nothing (Firefox, Windows) still get the theme. */
select option, select optgroup{background-color:var(--_bg);color:var(--_ink)}

/* ---------- The open list, where the browser lets us draw it ---------- */
@media (hover:hover) and (pointer:fine){
  @supports (appearance:base-select){
    select:not([multiple]):not([size]):not(.picker-native),
    select:not([multiple]):not([size]):not(.picker-native)::picker(select){
      appearance:base-select!important;
    }
    /* base-select lays the control out as a flex box that, unlike the system
       control, does not centre its text: fixed-height screens (Dashboards'
       30px filters) would show it riding the top edge. */
    select:not([multiple]):not([size]):not(.picker-native){align-items:center!important}
    /* The chevron is the background image above; drop the built-in one. */
    select:not([multiple]):not([size]):not(.picker-native)::picker-icon{display:none}

    select:not([multiple]):not([size]):not(.picker-native)::picker(select){
      box-sizing:border-box;
      min-inline-size:anchor-size(inline);
      max-block-size:min(360px, 60dvh);
      margin-block:4px;
      padding:4px;
      border:1px solid var(--_line);
      border-radius:calc(var(--_radius) + 3px);
      background:var(--_bg);
      color:var(--_ink);
      box-shadow:var(--gd-shadow-2, 0 6px 18px rgba(27,26,23,.12), 0 1px 3px rgba(27,26,23,.08));
      overflow:auto;
      opacity:1;
      translate:0 0;
      transition:opacity 120ms var(--_ease), translate 120ms var(--_ease), overlay 120ms allow-discrete, display 120ms allow-discrete;
    }
    @starting-style{
      select:not([multiple]):not([size]):not(.picker-native)::picker(select):popover-open{opacity:0;translate:0 -4px}
    }

    select:not([multiple]):not([size]):not(.picker-native) option{
      display:flex;align-items:center;gap:8px;
      min-block-size:32px;
      padding:6px 10px 6px 8px;
      border-radius:calc(var(--_radius) - 2px);
      background:transparent;
      color:var(--_ink);
      font:inherit;
      cursor:pointer;
    }
    select:not([multiple]):not([size]):not(.picker-native) option:is(:hover, :focus-visible){
      background:var(--_hover);
      outline:none;
    }
    select:not([multiple]):not([size]):not(.picker-native) option:checked{font-weight:600}
    select:not([multiple]):not([size]):not(.picker-native) option::checkmark{
      color:var(--_accent);
      font-weight:700;
    }
    select:not([multiple]):not([size]):not(.picker-native) option:disabled{opacity:.45;cursor:not-allowed}
    select:not([multiple]):not([size]):not(.picker-native) optgroup{
      background:transparent;
      padding-block-start:4px;
    }
    select:not([multiple]):not([size]):not(.picker-native) optgroup + optgroup{
      margin-block-start:4px;
      border-block-start:1px solid var(--_line);
    }
    select:not([multiple]):not([size]):not(.picker-native) optgroup legend{
      padding:6px 10px 4px;
      color:color-mix(in srgb, var(--_ink) 62%, transparent);
      font-size:11px;font-weight:600;letter-spacing:.04em;text-transform:uppercase;
    }
  }
}

@media (prefers-reduced-motion:reduce){
  select, select::picker(select){transition:none!important}
}
