/**
 * Language Selector Styles
 * Supports both LTR and RTL layouts
 */

/* Language Selector Container */
.language-selector {
    position: relative;
    display: inline-block;
    font-family: inherit;
    z-index: 100; /* Establish stacking context above sibling header elements so dropdown is not obscured by search bar or auth buttons */
}

/* Main Button */
.language-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    color: inherit;
    font-size: 14px;
    cursor: pointer;
    transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
    min-height: 44px; /* iOS HIG touch target */
}

.language-btn:hover {
    background: rgba(0, 207, 255, 0.1);
    border-color: transparent;
}

.language-btn:focus {
    outline: none;
    box-shadow: 0 0 0 2px var(--accent-cyan, #00cfff);
}

.lang-flag {
    font-size: 18px;
    line-height: 1;
}

.lang-code {
    font-weight: 500;
    letter-spacing: 0.5px;
}

.lang-arrow {
    transition: transform 0.2s ease;
    opacity: 0.7;
}

.language-btn[aria-expanded="true"] .lang-arrow {
    transform: rotate(180deg);
}

/* Desktop: hide mobile-only elements */
.lang-backdrop { display: none; }
.lang-sheet-handle { display: none; }

/* Dropdown Menu */
.language-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    min-width: 180px;
    max-height: 320px;
    overflow-y: auto;
    background: var(--bg-secondary, #1a1f2e);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--panel-radius);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
    z-index: 99999; /* Must be above all other elements including modals */
}

.language-dropdown.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

/* RTL Support - dropdown opens to the left */
[dir="rtl"] .language-dropdown {
    right: auto;
    left: 0;
}

/* Language Option */
.language-option {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 12px 16px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    border: none;
    border-radius: 0;
    color: inherit;
    font-size: 14px;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s ease;
}

[dir="rtl"] .language-option {
    text-align: right;
}

.language-option:hover {
    background: rgba(255, 255, 255, 0.05);
}

.language-option:focus {
    outline: none;
    background: rgba(255, 255, 255, 0.08);
}

.language-option.active {
    background: rgba(0, 212, 255, 0.1);
    color: var(--accent-cyan, #00cfff);
}

.language-option:first-of-type {
    border-radius: 11px 11px 0 0;
}

.language-option:last-of-type {
    border-radius: 0 0 11px 11px;
}

.lang-name {
    flex: 1;
}

.lang-check {
    color: var(--accent-cyan, #00cfff);
    font-weight: bold;
}

/* Mobile Styles */
@media (max-width: 768px) {
    /* Backdrop for mobile bottom sheet — real DOM element for reliable touch */
    .lang-backdrop {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.2s ease, visibility 0.2s ease;
        z-index: 99998;
        pointer-events: none;
    }

    .language-selector.dropdown-open .lang-backdrop {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    /* Lock body scroll when bottom sheet is open */
    body.lang-sheet-open {
        overflow: hidden !important;
        touch-action: none;
    }

    .language-dropdown {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        max-height: 50vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        /* Override body.lang-sheet-open `touch-action: none` so the bottom
           sheet itself can still be scrolled vertically by touch — required
           to reach English (top of list) from far down the list. Reported
           by @joebottrader on mobile Chrome (#4941, #4944). */
        touch-action: pan-y;
        background: var(--bg-secondary, #1a1f2e) !important;
        border-radius: 16px 16px 0 0;
        transform: translateY(100%);
        z-index: 99999;
        padding-top: 20px;
    }

    .language-dropdown.open {
        transform: translateY(0);
    }

    .lang-sheet-handle {
        display: block;
        width: 36px;
        height: 4px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 2px;
        margin: 0 auto 8px;
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
    }

    .language-option {
        padding: 16px 20px;
        font-size: 16px;
        -webkit-appearance: none;
        appearance: none;
        background: transparent !important;
        color: #e2e8f0;
        -webkit-tap-highlight-color: rgba(0, 212, 255, 0.2);
    }

    .language-option:hover,
    .language-option:active {
        background: rgba(255, 255, 255, 0.05) !important;
    }

    .language-option.active,
    .language-option.active:hover {
        background: rgba(0, 212, 255, 0.1) !important;
        color: var(--accent-cyan, #00cfff);
    }

    .language-btn {
        -webkit-tap-highlight-color: rgba(0, 212, 255, 0.2);
    }

    /* Add safe area padding for iOS */
    .language-dropdown::after {
        content: '';
        display: block;
        height: env(safe-area-inset-bottom, 0);
    }
}

/* Compact variant for headers */
.language-selector.compact .language-btn {
    padding: 6px 10px;
    min-height: 44px;
}

.language-selector.compact .lang-code {
    display: none;
}

/* Dark theme adjustments */
.language-selector.dark .language-btn {
    background: transparent;
    border-color: transparent;
}

.language-selector.dark .language-dropdown {
    background: #0d1117;
}

/* Light theme */
.language-selector.light .language-btn {
    background: rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.1);
    color: #1a1f2e;
}

.language-selector.light .language-dropdown {
    background: #ffffff;
    border-color: rgba(0, 0, 0, 0.1);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.language-selector.light .language-option:hover {
    background: rgba(0, 0, 0, 0.05);
}

.language-selector.light .language-option.active {
    background: rgba(0, 150, 200, 0.1);
    color: #0096c8;
}

/* Animation for language change */
@keyframes languageChanged {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.language-btn.changed {
    animation: languageChanged 0.3s ease;
}
