/* ============================================
   RSL Video Player Component
   Version: 1.0.0
   Description: Accessible video player with support for
   YouTube, Vimeo, and self-hosted videos. Features custom
   controls, lazy loading, captions, and full keyboard navigation.

   WCAG 2.1 AA Compliant:
   - Full keyboard navigation (Space, arrows, M, F, C)
   - Screen reader announcements via aria-live
   - Visible focus indicators
   - Respects prefers-reduced-motion
   ============================================ */

/* ===========================================
   CSS Custom Properties
   =========================================== */
:root {
    /* Colors */
    --rsl-video-bg: #000000;
    --rsl-video-controls-bg: linear-gradient(0deg, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.4) 50%, transparent 100%);
    --rsl-video-controls-bg-top: linear-gradient(180deg, rgba(0,0,0,0.6) 0%, transparent 100%);
    --rsl-video-progress-bg: rgba(255, 255, 255, 0.3);
    --rsl-video-progress-buffered: rgba(255, 255, 255, 0.5);
    --rsl-video-progress-fill: #6E7BFF;
    --rsl-video-text: #ffffff;
    --rsl-video-text-secondary: rgba(255, 255, 255, 0.8);
    --rsl-video-icon: #ffffff;
    --rsl-video-hover: #6E7BFF;
    --rsl-video-focus: #6E7BFF;
    --rsl-video-overlay-bg: rgba(0, 0, 0, 0.5);
    --rsl-video-play-btn-bg: rgba(110, 123, 255, 0.9);
    --rsl-video-play-btn-hover: rgba(110, 123, 255, 1);
    --rsl-video-error-bg: rgba(220, 53, 69, 0.9);

    /* Sizing */
    --rsl-video-controls-height: 48px;
    --rsl-video-btn-size: 40px;
    --rsl-video-btn-size-mobile: 48px;
    --rsl-video-progress-height: 4px;
    --rsl-video-progress-height-hover: 8px;
    --rsl-video-volume-width: 80px;
    --rsl-video-play-overlay-size: 80px;
    --rsl-video-border-radius: 8px;

    /* Timing */
    --rsl-video-transition: 0.2s ease;
    --rsl-video-controls-timeout: 3s;
}

/* ===========================================
   Base Container
   =========================================== */
.rsl-video-player {
    position: relative;
    width: 100%;
    background: var(--rsl-video-bg);
    border-radius: var(--rsl-video-border-radius);
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    line-height: 1;
    isolation: isolate;
}

.rsl-video-player:focus {
    outline: none;
}

.rsl-video-player:focus-visible {
    outline: 3px solid var(--rsl-video-focus);
    outline-offset: 2px;
}

/* ===========================================
   Aspect Ratio Containers
   =========================================== */
.rsl-video-aspect {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* Default 16:9 */
}

.rsl-video-player[data-video-aspect="16:9"] .rsl-video-aspect {
    padding-bottom: 56.25%;
}

.rsl-video-player[data-video-aspect="4:3"] .rsl-video-aspect {
    padding-bottom: 75%;
}

.rsl-video-player[data-video-aspect="1:1"] .rsl-video-aspect {
    padding-bottom: 100%;
}

.rsl-video-player[data-video-aspect="9:16"] .rsl-video-aspect {
    padding-bottom: 177.78%;
}

.rsl-video-player[data-video-aspect="21:9"] .rsl-video-aspect {
    padding-bottom: 42.86%;
}

/* ===========================================
   Size Variants
   =========================================== */
.rsl-video-player[data-video-size="sm"] {
    max-width: 400px;
}

.rsl-video-player[data-video-size="md"] {
    max-width: 640px;
}

.rsl-video-player[data-video-size="lg"] {
    max-width: 960px;
}

.rsl-video-player[data-video-size="full"] {
    max-width: 100%;
}

/* ===========================================
   Video Container
   =========================================== */
.rsl-video-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.rsl-video-container video,
.rsl-video-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: none;
}

/* ===========================================
   Poster/Thumbnail Overlay
   =========================================== */
.rsl-video-poster {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 30;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--rsl-video-bg);
}

.rsl-video-poster img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
}

.rsl-video-poster::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--rsl-video-overlay-bg);
    transition: background var(--rsl-video-transition);
    pointer-events: none;
}

.rsl-video-poster:hover::after {
    background: rgba(0, 0, 0, 0.3);
}

/* Hidden when video is playing */
.rsl-video-player[data-state="playing"] .rsl-video-poster,
.rsl-video-player[data-state="paused"] .rsl-video-poster,
.rsl-video-player[data-state="ended"] .rsl-video-poster.hidden {
    display: none;
}

/* ===========================================
   Play Overlay Button
   =========================================== */
.rsl-video-play-overlay {
    position: relative;
    z-index: 11;
    width: var(--rsl-video-play-overlay-size);
    height: var(--rsl-video-play-overlay-size);
    border-radius: 50%;
    background: var(--rsl-video-play-btn-bg);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--rsl-video-transition);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.rsl-video-play-overlay:hover {
    background: var(--rsl-video-play-btn-hover);
    transform: scale(1.1);
    box-shadow: 0 6px 30px rgba(0, 0, 0, 0.4);
}

.rsl-video-play-overlay:focus {
    outline: none;
}

.rsl-video-play-overlay:focus-visible {
    outline: 3px solid #ffffff;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(110, 123, 255, 0.5);
}

.rsl-video-play-overlay svg {
    width: 32px;
    height: 32px;
    fill: var(--rsl-video-icon);
    margin-left: 4px; /* Visual centering for play icon */
}

/* ===========================================
   Controls Container
   =========================================== */
.rsl-video-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--rsl-video-controls-bg);
    padding: 1rem;
    padding-top: 3rem;
    z-index: 20;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--rsl-video-transition), visibility var(--rsl-video-transition);
}

/* Show controls on hover, focus, or when paused */
.rsl-video-player:hover .rsl-video-controls,
.rsl-video-player:focus-within .rsl-video-controls,
.rsl-video-player[data-state="paused"] .rsl-video-controls,
.rsl-video-player[data-state="ended"] .rsl-video-controls,
.rsl-video-player[data-controls-visible="true"] .rsl-video-controls {
    opacity: 1;
    visibility: visible;
}

/* Always show on mobile */
@media (hover: none) {
    .rsl-video-player[data-state="playing"] .rsl-video-controls {
        opacity: 1;
        visibility: visible;
    }
}

/* ===========================================
   Progress Bar
   =========================================== */
.rsl-video-progress-container {
    width: 100%;
    margin-bottom: 0.75rem;
}

.rsl-video-progress {
    position: relative;
    width: 100%;
    height: var(--rsl-video-progress-height);
    background: var(--rsl-video-progress-bg);
    border-radius: 4px;
    cursor: pointer;
    transition: height var(--rsl-video-transition);
}

.rsl-video-progress:hover,
.rsl-video-progress:focus {
    height: var(--rsl-video-progress-height-hover);
}

.rsl-video-progress:focus {
    outline: none;
}

.rsl-video-progress:focus-visible {
    outline: 2px solid var(--rsl-video-focus);
    outline-offset: 2px;
}

.rsl-video-progress-buffered {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: var(--rsl-video-progress-buffered);
    border-radius: 4px;
    pointer-events: none;
}

.rsl-video-progress-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: var(--rsl-video-progress-fill);
    border-radius: 4px;
    pointer-events: none;
    transition: width 0.1s linear;
}

.rsl-video-progress-thumb {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%) scale(0);
    width: 14px;
    height: 14px;
    background: var(--rsl-video-progress-fill);
    border-radius: 50%;
    pointer-events: none;
    transition: transform var(--rsl-video-transition);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.rsl-video-progress:hover .rsl-video-progress-thumb,
.rsl-video-progress:focus .rsl-video-progress-thumb {
    transform: translate(-50%, -50%) scale(1);
}

/* ===========================================
   Control Buttons Row
   =========================================== */
.rsl-video-controls-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.rsl-video-controls-left {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.rsl-video-controls-center {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.rsl-video-controls-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ===========================================
   Control Buttons
   =========================================== */
.rsl-video-btn {
    width: var(--rsl-video-btn-size);
    height: var(--rsl-video-btn-size);
    border: none;
    background: transparent;
    color: var(--rsl-video-icon);
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--rsl-video-transition);
    padding: 0;
}

.rsl-video-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--rsl-video-hover);
}

.rsl-video-btn:focus {
    outline: none;
}

.rsl-video-btn:focus-visible {
    outline: 2px solid var(--rsl-video-focus);
    outline-offset: 2px;
    background: rgba(255, 255, 255, 0.1);
}

.rsl-video-btn svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

.rsl-video-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Mobile larger touch targets */
@media (max-width: 480px) {
    .rsl-video-btn {
        width: var(--rsl-video-btn-size-mobile);
        height: var(--rsl-video-btn-size-mobile);
    }
}

/* ===========================================
   Volume Controls
   =========================================== */
.rsl-video-volume-container {
    display: flex;
    align-items: center;
    position: relative;
}

.rsl-video-volume-slider-container {
    width: 0;
    overflow: hidden;
    transition: width var(--rsl-video-transition);
}

.rsl-video-volume-container:hover .rsl-video-volume-slider-container,
.rsl-video-volume-container:focus-within .rsl-video-volume-slider-container {
    width: var(--rsl-video-volume-width);
    margin-left: 0.5rem;
}

.rsl-video-volume-slider {
    width: var(--rsl-video-volume-width);
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--rsl-video-progress-bg);
    border-radius: 4px;
    cursor: pointer;
}

.rsl-video-volume-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 12px;
    height: 12px;
    background: var(--rsl-video-progress-fill);
    border-radius: 50%;
    cursor: pointer;
}

.rsl-video-volume-slider::-moz-range-thumb {
    width: 12px;
    height: 12px;
    background: var(--rsl-video-progress-fill);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.rsl-video-volume-slider:focus {
    outline: none;
}

.rsl-video-volume-slider:focus-visible {
    outline: 2px solid var(--rsl-video-focus);
    outline-offset: 2px;
}

/* Always show volume on mobile */
@media (max-width: 480px) {
    .rsl-video-volume-slider-container {
        width: 60px;
        margin-left: 0.5rem;
    }
}

/* ===========================================
   Time Display
   =========================================== */
.rsl-video-time {
    color: var(--rsl-video-text);
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    min-width: 100px;
    text-align: center;
}

.rsl-video-time-current {
    color: var(--rsl-video-text);
}

.rsl-video-time-separator {
    color: var(--rsl-video-text-secondary);
    margin: 0 0.25rem;
}

.rsl-video-time-duration {
    color: var(--rsl-video-text-secondary);
}

/* ===========================================
   Caption Button Active State
   =========================================== */
.rsl-video-btn-captions[aria-pressed="true"] {
    color: var(--rsl-video-progress-fill);
}

.rsl-video-btn-captions[aria-pressed="true"]::after {
    content: '';
    position: absolute;
    bottom: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 4px;
    height: 4px;
    background: var(--rsl-video-progress-fill);
    border-radius: 50%;
}

/* ===========================================
   Loading State
   =========================================== */
.rsl-video-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 15;
    display: none;
}

.rsl-video-player[data-state="loading"] .rsl-video-loading {
    display: block;
}

.rsl-video-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: var(--rsl-video-progress-fill);
    border-radius: 50%;
    animation: rsl-video-spin 0.8s linear infinite;
}

@keyframes rsl-video-spin {
    to {
        transform: rotate(360deg);
    }
}

/* ===========================================
   Error State
   =========================================== */
.rsl-video-error {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 25;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.9);
    color: var(--rsl-video-text);
    text-align: center;
    padding: 2rem;
}

.rsl-video-player[data-state="error"] .rsl-video-error {
    display: flex;
}

.rsl-video-error-icon {
    width: 48px;
    height: 48px;
    fill: #dc3545;
    margin-bottom: 1rem;
}

.rsl-video-error-message {
    font-size: 1rem;
    margin-bottom: 1rem;
    max-width: 300px;
}

.rsl-video-error-retry {
    padding: 0.75rem 1.5rem;
    background: var(--rsl-video-play-btn-bg);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all var(--rsl-video-transition);
}

.rsl-video-error-retry:hover {
    background: var(--rsl-video-play-btn-hover);
}

.rsl-video-error-retry:focus-visible {
    outline: 2px solid white;
    outline-offset: 2px;
}

/* ===========================================
   Title Bar (Top)
   =========================================== */
.rsl-video-title-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    background: var(--rsl-video-controls-bg-top);
    padding: 1rem;
    padding-bottom: 2rem;
    z-index: 20;
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--rsl-video-transition), visibility var(--rsl-video-transition);
    pointer-events: none;
}

.rsl-video-player:hover .rsl-video-title-bar,
.rsl-video-player:focus-within .rsl-video-title-bar,
.rsl-video-player[data-state="paused"] .rsl-video-title-bar {
    opacity: 1;
    visibility: visible;
}

.rsl-video-title {
    color: var(--rsl-video-text);
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ===========================================
   Fullscreen Styles
   =========================================== */
.rsl-video-player:fullscreen,
.rsl-video-player:-webkit-full-screen {
    border-radius: 0;
}

.rsl-video-player:fullscreen .rsl-video-aspect,
.rsl-video-player:-webkit-full-screen .rsl-video-aspect {
    padding-bottom: 0 !important;
    height: 100vh;
}

/* ===========================================
   Screen Reader Only Content
   =========================================== */
.rsl-video-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;
}

/* ===========================================
   Caption Display
   =========================================== */
.rsl-video-captions {
    position: absolute;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    font-size: 1rem;
    max-width: 80%;
    text-align: center;
    z-index: 19;
    display: none;
}

.rsl-video-captions.visible {
    display: block;
}

/* ===========================================
   Responsive Adjustments
   =========================================== */
@media (max-width: 480px) {
    .rsl-video-controls {
        padding: 0.75rem;
        padding-top: 2rem;
    }

    .rsl-video-time {
        font-size: 0.75rem;
        min-width: 80px;
    }

    .rsl-video-title {
        font-size: 0.875rem;
    }

    .rsl-video-play-overlay {
        width: 64px;
        height: 64px;
    }

    .rsl-video-play-overlay svg {
        width: 24px;
        height: 24px;
    }
}

/* ===========================================
   Dark Mode Adjustments
   =========================================== */
[data-theme="dark"] .rsl-video-player {
    --rsl-video-progress-fill: #818cf8;
    --rsl-video-play-btn-bg: rgba(129, 140, 248, 0.9);
    --rsl-video-play-btn-hover: rgba(129, 140, 248, 1);
    --rsl-video-focus: #818cf8;
    --rsl-video-hover: #818cf8;
}

/* ===========================================
   Reduced Motion
   =========================================== */
@media (prefers-reduced-motion: reduce) {
    .rsl-video-player,
    .rsl-video-player * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .rsl-video-spinner {
        animation: none;
        border-top-color: var(--rsl-video-progress-fill);
        border-right-color: var(--rsl-video-progress-fill);
    }
}

/* ===========================================
   High Contrast Mode
   =========================================== */
@media (prefers-contrast: high) {
    .rsl-video-player {
        --rsl-video-progress-bg: #666666;
        --rsl-video-progress-fill: #ffffff;
    }

    .rsl-video-btn:focus-visible,
    .rsl-video-progress:focus-visible,
    .rsl-video-play-overlay:focus-visible {
        outline: 3px solid #ffffff;
        outline-offset: 2px;
    }
}

/* ===========================================
   Print Styles
   =========================================== */
@media print {
    .rsl-video-player {
        display: none;
    }
}
