/**
 * ============================================================================
 * GLASS DROPDOWN SYSTEM - Unified Styles
 * ============================================================================
 *
 * LLM INSTRUCTIONS FOR FUTURE DEVELOPMENT:
 * ----------------------------------------
 * This is the SINGLE SOURCE OF TRUTH for dropdown styling across MindStage.
 *
 * HOW TO USE:
 * 1. Add class="glass-dropdown" to any <select> element
 * 2. Include {% glass_dropdown_assets %} in the template (usually in extra_css block)
 * 3. The JavaScript will automatically enhance the select into a styled dropdown
 *
 * EXAMPLE:
 *   <select class="filter-select glass-dropdown" id="sortSelect">
 *       <option value="recent">Most Recent</option>
 *       <option value="popular">Most Popular</option>
 *   </select>
 *
 * DO NOT:
 * - Create page-specific dropdown CSS (it belongs here)
 * - Use native <select> styling with appearance:none (use glass-dropdown instead)
 * - Add mobile dropdown CSS to individual templates
 * - Create separate "mobile-category-select" elements (use glass-dropdown)
 *
 * MOBILE SUPPORT:
 * Mobile styles are included in this file. They automatically apply at 768px breakpoint.
 * The dropdown uses larger touch targets (44px min) and optimized spacing for mobile.
 *
 * CONTAINER FIXES:
 * If dropdowns are clipped by parent containers, add the parent class to the
 * "Container overflow fixes" section below. Do NOT add inline overflow:visible.
 *
 * VARIANTS:
 * - .select-sm: Smaller padding/font for compact UIs
 * - .select-lg: Larger padding/font for prominent dropdowns
 * - .has-error: Red border for validation errors
 * - .has-success: Green border for valid state
 * - .disabled: Grayed out, non-interactive
 *
 * ============================================================================
 */

/* ==========================================================================
   Fallback styling to prevent flash of native selects before JS enhancement
   ========================================================================== */
select.glass-dropdown,
select[data-glass-dropdown="true"] {
    appearance: none;
    -webkit-appearance: none;
    width: 100%;
    padding: 0.75rem 1rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    color: var(--text, #e7e7ee);
    font-size: 0.95rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s ease;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(255,255,255,0.6)' stroke-width='2'%3e%3cpath d='M6 9l6 6 6-6'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.85rem center;
    background-size: 1rem;
}

select.glass-dropdown:focus,
select[data-glass-dropdown="true"]:focus {
    border-color: rgba(124, 156, 255, 0.6);
    box-shadow: 0 0 0 3px rgba(124, 156, 255, 0.2);
    outline: none;
}

select.glass-dropdown option,
select[data-glass-dropdown="true"] option,
select.glass-dropdown optgroup,
select[data-glass-dropdown="true"] optgroup {
    background: #0f111a;
    color: var(--text, #e7e7ee);
}

/* ==========================================================================
   BASE WRAPPER STYLES
   ========================================================================== */

.glass-select-wrapper {
    position: relative;
    width: 100%;
    /* Ensure dropdowns aren't clipped */
    overflow: visible !important;
    /* Ensure wrapper has proper stacking context */
    z-index: auto;
}

/* When dropdown is active, boost the wrapper's z-index */
.glass-select-wrapper:has(.glass-select-dropdown.active) {
    z-index: 999 !important;
}

/* Hide the native select - it's still there for form submission */
.glass-select-wrapper select {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   DISPLAY ELEMENT (the clickable trigger)
   ========================================================================== */

.glass-select-display {
    width: 100%;
    padding: 0.75rem 1rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    color: var(--text, #e7e7ee);
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    justify-content: space-between;
    align-items: center;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    /* Ensure minimum touch target for accessibility */
    min-height: 44px;
}

.glass-select-display:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.12);
}

.glass-select-display.active {
    border-color: var(--accent, #7c9cff);
    background: rgba(255, 255, 255, 0.08);
    box-shadow: 0 0 0 4px rgba(124, 156, 255, 0.1);
}

.glass-select-display .select-value {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.glass-select-display .select-arrow {
    transition: transform 0.3s ease;
    color: var(--muted, #8a8c9e);
    flex-shrink: 0;
    margin-left: 0.5rem;
}

.glass-select-display.active .select-arrow {
    transform: rotate(180deg);
    color: var(--accent, #7c9cff);
}

/* Flip arrow back when dropdown opens upward */
.glass-select-wrapper:has(.glass-select-dropdown.open-upward) .glass-select-display.active .select-arrow {
    transform: rotate(0deg);
}

/* ==========================================================================
   DROPDOWN PANEL (the options list)
   ========================================================================== */

.glass-select-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: linear-gradient(135deg, rgb(20, 23, 35), rgb(15, 17, 25));
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    max-height: 300px;
    overflow-y: auto;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.3s ease;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.glass-select-dropdown.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

/* Open upward when not enough space below */
.glass-select-dropdown.open-upward {
    top: auto;
    bottom: calc(100% + 4px);
    transform: translateY(10px);
}

.glass-select-dropdown.open-upward.active {
    transform: translateY(0);
}

/* ==========================================================================
   DROPDOWN OPTIONS
   ========================================================================== */

.glass-select-option {
    padding: 0.75rem 1rem;
    color: var(--text, #e7e7ee);
    cursor: pointer;
    transition: all 0.2s ease;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    /* Minimum touch target */
    min-height: 44px;
    display: flex;
    align-items: center;
}

.glass-select-option:last-child {
    border-bottom: none;
}

/* Selected before hover so :hover wins when both apply (MQA-58). The previous
   design used accent colors for the text itself (pale mint #9bffdc on hover,
   pale blue #7c9cff when selected), which were unreadable against the tinted
   glass backgrounds. Text now stays high-contrast white on both states and
   the accent is expressed as a left border + background tint instead. */
.glass-select-option.selected {
    background: rgba(124, 156, 255, 0.14);
    color: #ffffff;
    font-weight: 600;
    box-shadow: inset 3px 0 0 var(--accent, #7c9cff);
}

.glass-select-option:hover {
    background: rgba(124, 156, 255, 0.24);
    color: #ffffff;
    padding-left: 1.25rem;
}

.glass-select-option.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* ==========================================================================
   OPTGROUP STYLING
   ========================================================================== */

.glass-select-optgroup {
    padding: 0.5rem 1rem;
    color: var(--text-secondary, #8a8c9e);
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    background: rgba(255, 255, 255, 0.02);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: default;
    pointer-events: none;
}

.glass-select-optgroup + .glass-select-option {
    padding-left: 1.5rem;
}

/* ==========================================================================
   SCROLLBAR STYLING
   ========================================================================== */

.glass-select-dropdown::-webkit-scrollbar {
    width: 8px;
}

.glass-select-dropdown::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.02);
    border-radius: 4px;
}

.glass-select-dropdown::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

.glass-select-dropdown::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ==========================================================================
   SIZE VARIANTS
   ========================================================================== */

/* Small variant - for compact filter bars */
.glass-select-wrapper.select-sm .glass-select-display {
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
    min-height: 36px;
}

.glass-select-wrapper.select-sm .glass-select-option {
    padding: 0.5rem 0.75rem;
    min-height: 36px;
}

/* Large variant - for prominent form fields */
.glass-select-wrapper.select-lg .glass-select-display {
    padding: 1rem 1.25rem;
    font-size: 1.05rem;
    min-height: 52px;
}

/* ==========================================================================
   INLINE FORM SUPPORT
   ========================================================================== */

.form-inline .glass-select-wrapper {
    display: inline-block;
    width: auto;
    min-width: 150px;
}

/* ==========================================================================
   VALIDATION STATES
   ========================================================================== */

/* Error state */
.glass-select-wrapper.has-error .glass-select-display {
    border-color: rgba(231, 76, 60, 0.5);
}

.glass-select-wrapper.has-error .glass-select-display:focus {
    box-shadow: 0 0 0 4px rgba(231, 76, 60, 0.1);
}

/* Success state */
.glass-select-wrapper.has-success .glass-select-display {
    border-color: rgba(46, 204, 113, 0.5);
}

/* Disabled state */
.glass-select-wrapper.disabled .glass-select-display {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* ==========================================================================
   MULTIPLE SELECT SUPPORT
   ========================================================================== */

.glass-select-wrapper.multiple .glass-select-display .select-value {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
}

.glass-select-wrapper.multiple .select-tag {
    background: rgba(124, 156, 255, 0.2);
    color: var(--accent, #7c9cff);
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    font-size: 0.85rem;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.glass-select-wrapper.multiple .select-tag .remove {
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.glass-select-wrapper.multiple .select-tag .remove:hover {
    opacity: 1;
}

/* ==========================================================================
   SEARCH INPUT (for searchable dropdowns)
   ========================================================================== */

.glass-select-search {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    position: sticky;
    top: 0;
    background: inherit;
    backdrop-filter: inherit;
    -webkit-backdrop-filter: inherit;
}

.glass-select-search input {
    width: 100%;
    padding: 0.5rem;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    color: var(--text, #e7e7ee);
    font-size: 0.9rem;
}

.glass-select-search input:focus {
    outline: none;
    border-color: var(--accent, #7c9cff);
    background: rgba(255, 255, 255, 0.08);
}

/* ==========================================================================
   LOADING STATE
   ========================================================================== */

.glass-select-wrapper.loading .glass-select-display::after {
    content: '';
    position: absolute;
    right: 3rem;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    border: 2px solid rgba(124, 156, 255, 0.3);
    border-top-color: var(--accent, #7c9cff);
    border-radius: 50%;
    animation: glass-select-spin 0.8s linear infinite;
}

@keyframes glass-select-spin {
    to { transform: translateY(-50%) rotate(360deg); }
}

/* ==========================================================================
   LIGHT MODE SUPPORT
   ========================================================================== */

/* No prefers-color-scheme: light block. MindStage doesn't have a light
   theme — pages are hard-coded dark. A previous light-mode override here
   was the *only* prefers-color-scheme rule in the codebase, and it switched
   the dropdown panel to near-white while the host page stayed dark, leaving
   light-mode users with a pale dropdown floating on a dark page (MQA-62). */

/* ==========================================================================
   CONTAINER OVERFLOW FIXES

   LLM NOTE: If you find dropdowns being clipped by a parent container,
   add the container class here instead of adding inline styles to templates.
   This keeps all dropdown fixes in one place.
   ========================================================================== */

/* Form containers */
.form-group:has(.glass-select-wrapper),
.form-control-wrapper:has(.glass-select-wrapper),
.input-group:has(.glass-select-wrapper),
.form-section:has(.glass-select-wrapper) {
    overflow: visible !important;
    position: relative;
    z-index: auto;
}

.form-section {
    overflow: visible !important;
    position: relative;
    z-index: 1;
}

.form-section:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Control panels (used in library pages) */
.control-panel,
.control-panel-simple,
.control-row {
    overflow: visible !important;
    position: relative;
    z-index: 1;
}

.control-panel:has(.glass-select-dropdown.active),
.control-panel-simple:has(.glass-select-dropdown.active),
.control-row:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Category dropdown wrappers */
.category-dropdown-wrapper,
.mobile-category-group {
    overflow: visible !important;
    position: relative;
    z-index: 1;
}

.category-dropdown-wrapper:has(.glass-select-dropdown.active),
.mobile-category-group:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Search boxes */
.search-box {
    overflow: visible !important;
    position: relative;
    z-index: 1;
}

.search-box:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Filter groups */
.filter-group {
    overflow: visible !important;
    position: relative;
    z-index: 1;
}

.filter-group:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Card containers */
.card,
.panel,
.stat-card,
.summary-card,
.generator-card {
    overflow: visible !important;
    position: relative;
}

.card:has(.glass-select-dropdown.active),
.panel:has(.glass-select-dropdown.active),
.stat-card:has(.glass-select-dropdown.active),
.summary-card:has(.glass-select-dropdown.active),
.generator-card:has(.glass-select-dropdown.active) {
    z-index: 100 !important;
}

/* Modal containers */
.modal-content:has(.glass-select-wrapper) {
    overflow: visible !important;
}

/* ==========================================================================
   MOBILE STYLES (768px and below)

   LLM NOTE: All mobile dropdown styling should be here, NOT in individual
   templates. This ensures consistent mobile experience across the app.
   ========================================================================== */

@media (max-width: 768px) {
    /* Limit dropdown height on mobile */
    .glass-select-dropdown {
        max-height: 50vh;
        border-radius: 10px;
    }

    /* Compact display for mobile */
    .glass-select-display {
        padding: 0.5rem 0.75rem;
        padding-right: 2rem; /* Space for arrow */
        font-size: 0.85rem;
        border-radius: 10px;
        min-height: 40px;
    }

    /* Smaller arrow on mobile */
    .glass-select-display .select-arrow {
        width: 16px;
        height: 16px;
        position: absolute;
        right: 0.5rem;
    }

    /* Touch-friendly options */
    .glass-select-option {
        padding: 0.6rem 0.85rem;
        font-size: 0.9rem;
        min-height: 44px; /* Apple's recommended touch target */
    }

    /* Compact search on mobile */
    .glass-select-search {
        padding: 0.5rem 0.75rem;
    }

    .glass-select-search input {
        padding: 0.4rem 0.6rem;
        font-size: 0.85rem;
    }

    /* Filter group specific mobile styles */
    .filter-group .glass-select-wrapper {
        min-width: 0 !important;
        flex: 1;
    }

    .filter-group .glass-select-display {
        padding: 0.45rem 2rem 0.45rem 0.6rem !important;
        font-size: 0.8rem !important;
        border-radius: 8px !important;
        background: rgba(255, 255, 255, 0.03) !important;
        border: 1px solid rgba(255, 255, 255, 0.08) !important;
    }

    .filter-group .glass-select-display .select-arrow {
        width: 16px !important;
        height: 16px !important;
    }

    .filter-group .glass-select-dropdown {
        border-radius: 8px !important;
    }

    .filter-group .glass-select-option {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.85rem !important;
    }

    /* Control panel adjustments */
    .control-panel .glass-select-wrapper,
    .control-row .glass-select-wrapper {
        flex: 1;
        min-width: 0;
    }
}

/* ==========================================================================
   SMALL MOBILE (480px and below)
   ========================================================================== */

@media (max-width: 480px) {
    .glass-select-display {
        padding: 0.4rem 0.6rem;
        padding-right: 1.8rem;
        font-size: 0.8rem;
    }

    .glass-select-option {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }

    /* Full width dropdowns on very small screens */
    .glass-select-dropdown {
        left: -0.5rem;
        right: -0.5rem;
        border-radius: 8px;
    }
}
