/* =====================================================================
   CosmicSelect — themed in-DOM dropdown + segmented control.
   Replaces the visual of native <select> so the cosmic cursor works over
   the option list (native popups are OS windows the cursor can't paint over).
   The native <select> stays in the DOM as the form source of truth.

   Drawn in the Keystone language (docs/MENU_UI_OVERHAUL_2026-10.md): night
   surfaces, cream type, rounded squares; the selected option carries a
   check. No backdrop-filter: the list opens over live WebGPU scenes.
   ===================================================================== */

/* Native source-of-truth select: removed from layout but still submitted. */
.cosmic-select__native {
    position: absolute !important;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    pointer-events: none;
}

/* ---------- Dropdown variant ---------- */
.cosmic-select {
    position: relative;
    width: 100%;
}

.cosmic-select__trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 44px;
    box-sizing: border-box;
    padding: 0 14px;
    background: rgba(var(--sb-cream-rgb), 0.04);
    border: 1px solid var(--sb-line-strong);
    border-radius: var(--sb-r-sm);
    color: var(--sb-text);
    font: 600 0.875rem / 1.2 var(--sb-font-text);
    transition:
        border-color var(--sb-dur-2) var(--sb-ease-lock),
        background var(--sb-dur-2) var(--sb-ease-lock);
    box-shadow: none;
    user-select: none;
    cursor: pointer;
}

.cosmic-select__trigger:hover {
    border-color: rgba(var(--sb-cream-rgb), 0.36);
    background: rgba(var(--sb-cream-rgb), 0.07);
}

.cosmic-select.is-open .cosmic-select__trigger {
    border-color: rgba(var(--sb-cream-rgb), 0.36);
    background: rgba(var(--sb-cream-rgb), 0.07);
    box-shadow: none;
}

.cosmic-select__trigger:focus-visible {
    outline: 2px solid rgba(var(--sb-cream-rgb), 0.82);
    outline-offset: 3px;
}

.cosmic-select__value {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cosmic-select__chevron {
    color: var(--sb-text-muted);
    font-size: 12px;
    transition: transform var(--sb-dur-2) var(--sb-ease-lock);
}

.cosmic-select.is-open .cosmic-select__chevron {
    transform: rotate(180deg);
}

.cosmic-select.is-disabled {
    opacity: 0.45;
}

.cosmic-select.is-disabled .cosmic-select__trigger {
    pointer-events: none;
}

.cosmic-select__listbox {
    position: absolute;
    z-index: 50;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    box-sizing: border-box;
    max-height: 264px;
    overflow-y: auto;
    padding: 6px;
    background: linear-gradient(180deg, rgba(30, 26, 66, 0.98), rgba(14, 12, 34, 0.98));
    border: 1px solid var(--sb-line-strong);
    border-radius: 12px;
    box-shadow: 0 18px 44px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(var(--sb-cream-rgb), 0.06);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    animation: cosmic-select-pop var(--sb-dur-2) var(--sb-ease-lock);
    /* Themed scrollbar baked into the listbox itself, so it matches whether the list
       lives inside a modal (inherits scrollbar-color) or is portaled to <body>
       (which would otherwise inherit the default grey scrollbar). */
    scrollbar-width: thin;
    scrollbar-color: rgba(var(--sb-cream-rgb), 0.24) transparent;
}

.cosmic-select__listbox::-webkit-scrollbar {
    width: 10px;
}

.cosmic-select__listbox::-webkit-scrollbar-track {
    background: transparent;
    margin: 4px;
}

.cosmic-select__listbox::-webkit-scrollbar-thumb {
    border: 3px solid transparent;
    border-radius: 6px;
    background: rgba(var(--sb-cream-rgb), 0.22) padding-box;
}

.cosmic-select__listbox::-webkit-scrollbar-thumb:hover {
    background-color: rgba(var(--sb-cream-rgb), 0.34);
}

@keyframes cosmic-select-pop {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Portal variant: rendered in <body>, position set inline from the select's rect,
   so it is never clipped by a host menu's overflow and sits above modals. */
.cosmic-select__overlay {
    position: fixed;
    top: auto;
    left: auto;
    right: auto;
    z-index: 2147483000;
}

.cosmic-select__option {
    position: relative;
    padding: 10px 34px 10px 12px;
    border-radius: 8px;
    color: var(--sb-text-muted);
    font: 600 0.875rem / 1.25 var(--sb-font-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    transition:
        background var(--sb-dur-1) linear,
        color var(--sb-dur-1) linear;
}

.cosmic-select__option.is-active {
    background: rgba(var(--sb-cream-rgb), 0.08);
    color: var(--sb-text);
}

.cosmic-select__option[aria-selected="true"] {
    color: var(--sb-text);
}

/* The chosen option carries a cream check. */
.cosmic-select__option[aria-selected="true"]::after {
    content: '';
    position: absolute;
    top: 50%;
    right: 15px;
    width: 5px;
    height: 10px;
    margin-top: -7px;
    border: solid var(--sb-text);
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}

.cosmic-select__option[aria-disabled="true"] {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ---------- Segmented variant ---------- */
.cosmic-segmented {
    display: inline-flex;
    gap: 4px;
    padding: 4px;
    background: rgba(var(--sb-night-rgb), 0.45);
    border: 1px solid var(--sb-line);
    border-radius: 13px;
    box-shadow: none;
}

.cosmic-segmented__seg {
    flex: 1 1 auto;
    min-height: 36px;
    padding: 0 14px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 9px;
    color: var(--sb-text-muted);
    font: 600 0.625rem / 1 var(--sb-font-display);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    transition:
        background var(--sb-dur-2) var(--sb-ease-lock),
        color var(--sb-dur-2) var(--sb-ease-lock),
        border-color var(--sb-dur-2) var(--sb-ease-lock);
    white-space: nowrap;
    cursor: pointer;
}

.cosmic-segmented__seg:hover:not(.is-checked) {
    background: rgba(var(--sb-cream-rgb), 0.05);
    color: var(--sb-text);
}

.cosmic-segmented__seg.is-checked {
    background: linear-gradient(180deg, rgba(var(--sb-cream-rgb), 0.12), rgba(var(--sb-cream-rgb), 0.05));
    border-color: var(--sb-line-strong);
    color: var(--sb-text);
    box-shadow: inset 0 1px 0 rgba(var(--sb-cream-rgb), 0.08);
}

.cosmic-segmented__seg:focus-visible {
    outline: 2px solid rgba(var(--sb-cream-rgb), 0.82);
    outline-offset: 2px;
    box-shadow: none;
}

.cosmic-segmented.is-disabled {
    opacity: 0.45;
    pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
    .cosmic-select__listbox { animation: none; }
    .cosmic-select__chevron,
    .cosmic-select__trigger,
    .cosmic-select__option,
    .cosmic-segmented__seg { transition: none; }
}
