/* ==========================================================================
   Change Lives — CLSelect: accessible dropdown/combobox (public/ui/select.css)
   Progressive enhancement skin over native <select>: the native control stays
   in the DOM (form submit + no-JS fallback); this layer styles the custom
   trigger and the portalled listbox rendered by public/ui/select.js.

   The component consumes existing page tokens only. The landing page uses the
   shared site.css tokens; the creators page maps its glass tokens onto the
   same component aliases in its local scope. The JS copies resolved aliases
   onto the body portal so nested theme values survive overflow-safe rendering.
   ========================================================================== */

/* Component aliases for the shared site tokens. The two pages with selects
   provide these tokens in their own scope; no component hex colors are needed. */
.clsel,
.clsel-pop {
  --clsel-surface: var(--bg-raised);
  --clsel-ink: var(--ink);
  --clsel-ink-soft: var(--ink-soft);
  --clsel-ink-mute: var(--ink-mute);
  --clsel-line: var(--line);
  --clsel-line-strong: var(--line-gold, var(--glass-border));
  --clsel-field: color-mix(in srgb, var(--clsel-ink) 4%, transparent);
  --clsel-hover: color-mix(in srgb, var(--clsel-ink) 7%, transparent);
  --clsel-accent: var(--gold-bright, var(--gold-lt));
  --clsel-accent-2: var(--gold);
  --clsel-danger: var(--vermilion-bright, var(--red-lt));
  --clsel-ease: var(--ease-out, cubic-bezier(0.23, 1, 0.32, 1));
  --clsel-shadow: var(--shadow-lift, var(--glass-shadow));
}

.clu-creators .clsel {
  --clsel-surface: var(--bg);
  --clsel-ink: var(--body);
  --clsel-ink-soft: var(--head);
  --clsel-ink-mute: var(--muted);
  --clsel-line: var(--glass-border);
  --clsel-line-strong: var(--gold);
  --clsel-field: var(--glass-fill);
  --clsel-hover: var(--glass-fill-gold);
  --clsel-accent: var(--gold-lt);
  --clsel-accent-2: var(--gold);
  --clsel-danger: var(--red-lt);
  --clsel-shadow: var(--glass-shadow);
}

/* Native progressive-enhancement fallback: keep the browser's option popup
   on the same dark scheme and use the owning page's tokens. */
select { color-scheme: dark; }
select option { background: var(--bg-raised); color: var(--ink); }
.clu-creators select option { background: var(--bg); color: var(--body); }
.field select:focus-visible { outline-offset: 2px; }

/* --------------------------------------------------------------------------
   Wrapper + visually hidden native select (still the form-submit source;
   focus is forwarded to the trigger by select.js)
   -------------------------------------------------------------------------- */
.clsel { position: relative; display: block; }
.clsel--compact { display: inline-block; vertical-align: middle; }

.clsel .clsel-native {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0 0 0 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
  appearance: none !important;
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   Trigger (role="combobox")
   -------------------------------------------------------------------------- */
.clsel-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 12px 44px 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--clsel-line);
  background: var(--clsel-field);
  color: var(--clsel-ink);
  font: 400 15px/1.4 "Instrument Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  text-align: left;
  cursor: pointer;
  transition: border-color 200ms ease;
}
.clsel-trigger:hover { border-color: var(--clsel-line-strong); }
.clsel-trigger:focus-visible {
  outline: 2px solid var(--clsel-accent);
  outline-offset: 2px;
  border-color: var(--clsel-accent);
}
.clsel-trigger[aria-expanded="true"] { border-color: var(--clsel-line-strong); }
.clsel.is-invalid .clsel-trigger,
.clsel-trigger[aria-invalid="true"] { border-color: var(--clsel-danger); }
.clsel-trigger:disabled { opacity: 0.55; cursor: not-allowed; }

.clsel-value {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.clsel-value.clsel-placeholder { color: var(--clsel-ink-mute); }

.clsel-chevron {
  position: absolute;
  right: 16px;
  top: 50%;
  width: 16px;
  height: 16px;
  margin-top: -8px;
  color: var(--clsel-ink-mute);
  pointer-events: none;
  transition: transform 150ms var(--clsel-ease);
}
.clsel-trigger[aria-expanded="true"] .clsel-chevron { transform: rotate(180deg); }
.clsel-chevron svg { display: block; width: 100%; height: 100%; }

/* compact variant: the creators toolbar sort control */
.clsel--compact .clsel-trigger {
  width: auto;
  min-width: 150px;
  min-height: 44px;
  padding: 9px 38px 9px 14px;
  font-size: 13.5px;
  border-radius: 12px;
}
.clsel--compact .clsel-chevron { right: 14px; }

/* --------------------------------------------------------------------------
   Portalled listbox (appended to document.body, position: fixed)
   -------------------------------------------------------------------------- */
.clsel-pop {
  position: fixed;
  z-index: 1000;
  min-width: 220px;
  max-height: 320px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 6px;
  border-radius: 14px;
  border: 1px solid var(--clsel-line);
  background: var(--clsel-surface);
  color: var(--clsel-ink);
  box-shadow: var(--clsel-shadow);
  transform-origin: top center;
}
.clsel-pop[hidden] { display: none; }
.clsel-pop.is-up { transform-origin: bottom center; }

.clsel-pop.is-open { animation: clsel-pop-in 150ms var(--clsel-ease); }
.clsel-pop.is-closing {
  animation: clsel-pop-out 120ms ease forwards;
  pointer-events: none;
}
@keyframes clsel-pop-in {
  from { opacity: 0; transform: translateY(-4px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
@keyframes clsel-pop-out {
  from { opacity: 1; transform: none; }
  to { opacity: 0; transform: translateY(-4px) scale(0.98); }
}

.clsel-pop::-webkit-scrollbar { width: 10px; }
.clsel-pop::-webkit-scrollbar-thumb {
  background: var(--clsel-hover);
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: content-box;
}
.clsel-pop::-webkit-scrollbar-track { background: transparent; }

/* --------------------------------------------------------------------------
   Options (role="option")
   -------------------------------------------------------------------------- */
.clsel-option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 9px;
  color: var(--clsel-ink);
  font-size: 15px;
  line-height: 1.35;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.clsel-option + .clsel-option { margin-top: 2px; }
.clsel-option-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.clsel-option.is-active {
  background: var(--clsel-hover);
  outline: 2px solid var(--clsel-accent);
  outline-offset: -2px;
}
.clsel-option[aria-selected="true"] {
  color: var(--clsel-accent);
  font-weight: 600;
}
.clsel-check {
  width: 16px;
  height: 16px;
  flex: none;
  color: var(--clsel-accent-2);
  opacity: 0;
}
.clsel-option[aria-selected="true"] .clsel-check { opacity: 1; }
.clsel-option.is-disabled {
  color: var(--clsel-ink-mute);
  cursor: not-allowed;
}

/* --------------------------------------------------------------------------
   Motion gate + small screens
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .clsel-pop.is-open,
  .clsel-pop.is-closing { animation: none; }
  .clsel-chevron,
  .clsel-trigger { transition: none; }
}

@media (max-width: 640px) {
  /* 16px keeps iOS Safari from zoom-jumping when the control is focused */
  .clsel-trigger { font-size: 16px; }
  .clsel-pop { border-radius: 16px; }
}
