/* ============================================
   RSL Select Component
   Version: 2.0.0
   Description: Accessible custom select/dropdown menu with single/multi-select,
                search/filter, keyboard navigation, and extensive customization

   FilterBus Integration (v2.0.0):
   - Bidirectional: Can publish selection changes AND subscribe to filter state
   - data-filterbus-publish="key" - Publish selection to FilterBus with this key
   - data-filterbus-subscribe="key" - Subscribe to FilterBus and update selection
   - data-filter-value="value" - For visibility filtering (show/hide based on match)
   ============================================ */

/* ============================================
   CSS VARIABLES
   ============================================ */

:root {
    /* Colors */
    --select-bg: #ffffff;
    --select-border: #cbd5e0;
    --select-border-focus: #6E7BFF;
    --select-text: #2d3748;
    --select-placeholder: #a0aec0;
    --select-hover-bg: #f7fafc;
    --select-selected-bg: #edf2f7;
    --select-disabled-bg: #f7fafc;
    --select-disabled-text: #a0aec0;

    /* Option colors */
    --select-option-bg: #ffffff;
    --select-option-text: #2d3748;
    --select-option-hover-bg: #edf2f7;
    --select-option-selected-bg: #6E7BFF;
    --select-option-selected-text: #ffffff;
    --select-option-disabled-bg: #f7fafc;
    --select-option-disabled-text: #cbd5e0;

    /* Chip/tag colors */
    --select-chip-bg: #6E7BFF;
    --select-chip-text: #ffffff;
    --select-chip-hover-bg: #5a67d8;

    /* Dimensions */
    --select-height: 42px;
    --select-padding: 0.5rem 2.5rem 0.5rem 1rem;
    --select-border-radius: 6px;
    --select-max-height: 300px;
    --select-font-size: 1rem;

    /* Z-index */
    --select-dropdown-z: 1000;

    /* Transitions */
    --select-transition: all 0.2s ease;
}

/* ============================================
   SELECT CONTAINER
   ============================================ */

.rsl-select {
    position: relative;
    display: inline-block;
    width: 100%;
    max-width: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Full width variant */
.rsl-select.rsl-select-block {
    display: block;
}

/* ============================================
   SELECT TRIGGER BUTTON
   ============================================ */

.rsl-select-trigger {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    min-height: var(--select-height);
    padding: var(--select-padding);
    background: var(--select-bg);
    border: 2px solid var(--select-border);
    border-radius: var(--select-border-radius);
    font-size: var(--select-font-size);
    color: var(--select-text);
    cursor: pointer;
    transition: var(--select-transition);
    text-align: left;
    user-select: none;
}

.rsl-select-trigger:hover {
    border-color: var(--select-border-focus);
}

.rsl-select-trigger:focus {
    outline: none;
    border-color: var(--select-border-focus);
    box-shadow: 0 0 0 3px rgba(110, 123, 255, 0.1);
}

/* Open state */
.rsl-select.open .rsl-select-trigger {
    border-color: var(--select-border-focus);
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

/* Disabled state */
.rsl-select-trigger:disabled,
.rsl-select.disabled .rsl-select-trigger {
    background: var(--select-disabled-bg);
    color: var(--select-disabled-text);
    cursor: not-allowed;
    opacity: 0.6;
}

/* ============================================
   SELECT TRIGGER CONTENT
   ============================================ */

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

.rsl-select-value.placeholder {
    color: var(--select-placeholder);
}

/* Multi-select count indicator */
.rsl-select-count {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    margin-left: 0.5rem;
    background: var(--select-chip-bg);
    color: var(--select-chip-text);
    border-radius: 12px;
    font-size: 0.875rem;
    font-weight: 600;
}

/* Arrow icon */
.rsl-select-arrow {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    transition: transform 0.2s ease;
    font-size: 0.875rem;
    color: var(--select-text);
}

.rsl-select.open .rsl-select-arrow {
    transform: translateY(-50%) rotate(180deg);
}

/* Clear button */
.rsl-select-clear {
    position: absolute;
    right: 2.5rem;
    top: 50%;
    transform: translateY(-50%);
    display: none;
    padding: 0.25rem;
    background: none;
    border: none;
    color: var(--select-text);
    cursor: pointer;
    opacity: 0.5;
    transition: opacity 0.2s ease;
}

.rsl-select-clear:hover {
    opacity: 1;
}

.rsl-select.has-value .rsl-select-clear {
    display: block;
}

/* ============================================
   CHIPS/TAGS (Multi-select)
   ============================================ */

.rsl-select-chips {
    display: none;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.75rem;
    padding: 0.75rem;
    background: var(--select-hover-bg);
    border: 1px solid var(--select-border);
    border-radius: var(--select-border-radius);
}

.rsl-select-chips.visible {
    display: flex;
}

.rsl-select-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0.75rem;
    background: var(--select-chip-bg);
    color: var(--select-chip-text);
    border-radius: 16px;
    font-size: 0.875rem;
    font-weight: 500;
}

.rsl-select-chip-remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    padding: 0;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    color: var(--select-chip-text);
    cursor: pointer;
    font-size: 0.75rem;
    transition: background 0.2s ease;
}

.rsl-select-chip-remove:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Toggle chips button */
.rsl-select-chips-toggle {
    display: none;
    margin-top: 0.5rem;
    padding: 0.375rem 0.75rem;
    background: none;
    border: 1px solid var(--select-border);
    border-radius: var(--select-border-radius);
    color: var(--select-text);
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--select-transition);
}

.rsl-select.multi-select .rsl-select-chips-toggle {
    display: inline-block;
}

.rsl-select-chips-toggle:hover {
    background: var(--select-hover-bg);
    border-color: var(--select-border-focus);
}

/* ============================================
   DROPDOWN MENU
   ============================================ */

.rsl-select-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    display: none;
    background: var(--select-bg);
    border: 2px solid var(--select-border-focus);
    border-top: none;
    border-radius: 0 0 var(--select-border-radius) var(--select-border-radius);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    z-index: var(--select-dropdown-z);
    overflow: hidden;
}

.rsl-select.open .rsl-select-dropdown {
    display: block;
}

/* Dropdown above trigger (when near bottom) */
.rsl-select-dropdown.open-above {
    top: auto;
    bottom: 100%;
    border-top: 2px solid var(--select-border-focus);
    border-bottom: none;
    border-radius: var(--select-border-radius) var(--select-border-radius) 0 0;
}

/* ============================================
   SEARCH INPUT
   ============================================ */

.rsl-select-search {
    display: none;
    padding: 0.75rem;
    border-bottom: 1px solid var(--select-border);
}

.rsl-select-search.visible {
    display: block;
}

.rsl-select-search input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--select-border);
    border-radius: 4px;
    font-size: 0.875rem;
    color: var(--select-text);
    transition: border-color 0.2s ease;
}

.rsl-select-search input:focus {
    outline: none;
    border-color: var(--select-border-focus);
}

.rsl-select-search input::placeholder {
    color: var(--select-placeholder);
}

/* ============================================
   OPTIONS LIST
   ============================================ */

.rsl-select-options {
    max-height: var(--select-max-height);
    overflow-y: auto;
    overflow-x: hidden;
    margin: 0;
    padding: 0.5rem 0;
    list-style: none;
}

/* Custom scrollbar */
.rsl-select-options::-webkit-scrollbar {
    width: 8px;
}

.rsl-select-options::-webkit-scrollbar-track {
    background: var(--select-hover-bg);
}

.rsl-select-options::-webkit-scrollbar-thumb {
    background: var(--select-border);
    border-radius: 4px;
}

.rsl-select-options::-webkit-scrollbar-thumb:hover {
    background: var(--select-border-focus);
}

/* ============================================
   OPTION GROUPS
   ============================================ */

.rsl-select-optgroup {
    padding: 0.5rem 1rem 0.25rem;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--select-placeholder);
}

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

.rsl-select-option {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    color: var(--select-option-text);
    cursor: pointer;
    transition: background-color 0.15s ease;
    user-select: none;
}

.rsl-select-option:hover {
    background: var(--select-option-hover-bg);
}

.rsl-select-option:focus {
    outline: none;
    background: var(--select-option-hover-bg);
}

/* Selected option */
.rsl-select-option.selected {
    background: var(--select-option-selected-bg);
    color: var(--select-option-selected-text);
    font-weight: 500;
}

/* Focused option (keyboard navigation) */
.rsl-select-option.focused {
    background: var(--select-option-hover-bg);
    outline: 2px solid var(--select-border-focus);
    outline-offset: -2px;
}

/* Disabled option */
.rsl-select-option.disabled {
    background: var(--select-option-disabled-bg);
    color: var(--select-option-disabled-text);
    cursor: not-allowed;
    opacity: 0.5;
}

.rsl-select-option.disabled:hover {
    background: var(--select-option-disabled-bg);
}

/* ============================================
   OPTION CONTENT
   ============================================ */

/* Option icon/image */
.rsl-select-option-icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.rsl-select-option-icon img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

/* Option text */
.rsl-select-option-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Option description */
.rsl-select-option-description {
    display: block;
    font-size: 0.875rem;
    color: var(--select-placeholder);
    margin-top: 0.25rem;
}

.rsl-select-option.selected .rsl-select-option-description {
    color: rgba(255, 255, 255, 0.8);
}

/* Multi-select checkbox */
.rsl-select-checkbox {
    flex-shrink: 0;
    width: 18px;
    height: 18px;
    border: 2px solid var(--select-border);
    border-radius: 3px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.rsl-select-option.selected .rsl-select-checkbox {
    background: var(--select-option-selected-text);
    border-color: var(--select-option-selected-text);
}

.rsl-select-option.selected .rsl-select-checkbox::after {
    content: '✓';
    color: var(--select-option-selected-bg);
    font-size: 0.875rem;
    font-weight: bold;
}

/* Checkmark icon for single select */
.rsl-select-checkmark {
    flex-shrink: 0;
    width: 18px;
    height: 18px;
    opacity: 0;
    color: var(--select-option-selected-text);
    transition: opacity 0.2s ease;
}

.rsl-select-option.selected .rsl-select-checkmark {
    opacity: 1;
}

/* ============================================
   NO RESULTS MESSAGE
   ============================================ */

.rsl-select-no-results {
    padding: 1.5rem 1rem;
    text-align: center;
    color: var(--select-placeholder);
    font-size: 0.875rem;
}

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

.rsl-select.rsl-select-sm {
    --select-height: 36px;
    --select-padding: 0.375rem 2rem 0.375rem 0.75rem;
    --select-font-size: 0.875rem;
}

.rsl-select.rsl-select-lg {
    --select-height: 48px;
    --select-padding: 0.75rem 3rem 0.75rem 1.25rem;
    --select-font-size: 1.125rem;
}

/* ============================================
   DARK MODE
   ============================================ */

[data-theme="dark"] .rsl-select {
    --select-bg: #1a1a1a;
    --select-border: #4a5568;
    --select-border-focus: #7c88ff;
    --select-text: #e2e8f0;
    --select-placeholder: #718096;
    --select-hover-bg: #2d3748;
    --select-selected-bg: #2d3748;
    --select-disabled-bg: #2d3748;
    --select-disabled-text: #4a5568;

    --select-option-bg: #1a1a1a;
    --select-option-text: #e2e8f0;
    --select-option-hover-bg: #2d3748;
    --select-option-selected-bg: #6E7BFF;
    --select-option-selected-text: #ffffff;
    --select-option-disabled-bg: #2d3748;
    --select-option-disabled-text: #4a5568;

    --select-chip-bg: #6E7BFF;
    --select-chip-text: #ffffff;
    --select-chip-hover-bg: #5a67d8;
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    .rsl-select-trigger,
    .rsl-select-option,
    .rsl-select-arrow,
    .rsl-select-chip-remove {
        transition: none;
    }
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .rsl-select-trigger {
        border-width: 3px;
    }

    .rsl-select-option.focused {
        outline-width: 3px;
    }
}

/* Screen reader only text */
.rsl-select-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

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

.rsl-select.loading .rsl-select-arrow {
    display: none;
}

.rsl-select.loading .rsl-select-trigger::after {
    content: '';
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    border: 2px solid var(--select-border);
    border-top-color: var(--select-border-focus);
    border-radius: 50%;
    animation: rsl-select-spin 0.6s linear infinite;
}

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

/* ============================================
   RESPONSIVE
   ============================================ */

@media (max-width: 768px) {
    .rsl-select {
        --select-max-height: 250px;
    }

    .rsl-select-dropdown {
        position: fixed;
        top: auto !important;
        bottom: 0;
        left: 0;
        right: 0;
        max-height: 70vh;
        border-radius: var(--select-border-radius) var(--select-border-radius) 0 0;
    }

    .rsl-select-options {
        max-height: calc(70vh - 80px);
    }
}

/* ============================================
   INTEGRATION WITH RSL GRID
   ============================================ */

.slot-layout .rsl-select,
.nested-slot-layout .rsl-select {
    width: 100%;
}

/* Form integration */
.form-group .rsl-select {
    width: 100%;
}

/* ============================================
   FilterBus Integration
   Visibility and transition styles for selects
   subscribing to FilterBus state changes
   ============================================ */

/**
 * Hidden state for FilterBus-controlled selects
 * Uses display:none to collapse grid space
 */
.rsl-select.rsl-select-filterbus-hidden {
    display: none !important;
}

/**
 * Hide the parent slot-item when select is hidden
 * This collapses the grid cell so remaining items shift left
 */
.slot-item:has(.rsl-select.rsl-select-filterbus-hidden) {
    display: none !important;
}

/**
 * Visible state - default
 * Selects are visible by default before FilterBus initializes
 */
.rsl-select:not(.rsl-select-filterbus-hidden) {
    opacity: 1;
    visibility: visible;
}

/**
 * Transition for initialized selects
 * Only apply transitions after FilterBus is initialized
 * to prevent flash on page load
 */
.rsl-select.rsl-select-filterbus-initialized {
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

/**
 * Reduce motion for accessibility
 * Respects user's motion preferences
 */
@media (prefers-reduced-motion: reduce) {
    .rsl-select.rsl-select-filterbus-initialized {
        transition: none;
    }
}
