/* ============================================
   CUSTOM PLAYER CONTROLS (cp-*) + SETTINGS POPOVER
   Fully custom controls bar replacing native <video controls>
   ============================================ */

/* ── Hide ALL native video controls completely ── */
video.player-video::-webkit-media-controls { display: none !important; opacity: 0 !important; }
video.player-video::-webkit-media-controls-enclosure { display: none !important; }
video.player-video::-webkit-media-controls-panel { display: none !important; }
video.player-video::-webkit-media-controls-start-playback-button { display: none !important; }
video.player-video::-webkit-media-controls-play-button { display: none !important; }
video.player-video::-webkit-media-controls-volume-slider { display: none !important; }
video.player-video::-webkit-media-controls-mute-button { display: none !important; }
video.player-video::-webkit-media-controls-timeline { display: none !important; }
video.player-video::-webkit-media-controls-current-time-display { display: none !important; }
video.player-video::-webkit-media-controls-time-remaining-display { display: none !important; }
video.player-video::-webkit-media-controls-fullscreen-button { display: none !important; }
video.player-video::-webkit-media-controls-overflow-button { display: none !important; }
video.player-video::-webkit-media-controls-toggle-closed-captions-button { display: none !important; }
/* Firefox */
video.player-video::-moz-range-track { display: none !important; }

/* ── Center play/pause overlay ── */
.cp-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    cursor: pointer;
    /* GPU compositing + stacking context WITHOUT creating containing block for fixed children */
    will-change: opacity;
    isolation: isolate;
}

.cp-play-center {
    width: 68px;
    height: 68px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    transition: transform 0.2s ease, opacity 0.3s ease, background 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
}

.cp-play-center i {
    margin-left: 3px; /* optical center for play icon */
}

.cp-overlay.paused .cp-play-center {
    opacity: 1;
    pointer-events: auto;
}

.cp-play-center:hover {
    background: rgba(0, 0, 0, 0.7);
    transform: scale(1.08);
}

.cp-play-center:active {
    transform: scale(0.95);
}

/* ── Bottom controls bar ── */
.cp-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 110;
    padding: 0 12px 10px;
    background: linear-gradient(0deg, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.4) 60%, transparent 100%);
    opacity: 1;
    transition: opacity 0.35s ease;
    display: flex;
    flex-direction: column;
    gap: 2px;
    /* GPU compositing + stacking context WITHOUT creating containing block for fixed children */
    will-change: opacity;
    isolation: isolate;
}

.player-video-container.cp-idle .cp-controls {
    opacity: 0;
    pointer-events: none;
}

.player-video-container.cp-idle .cp-overlay .cp-play-center {
    opacity: 0 !important;
    pointer-events: none !important;
}

.player-video-container.cp-idle {
    cursor: none;
}

/* ── Progress bar ── */
.cp-progress-wrap {
    position: relative;
    height: 22px;
    display: flex;
    align-items: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.cp-progress-bar {
    position: absolute;
    left: 0;
    right: 0;
    height: 4px;
    background: rgba(255,255,255,0.2);
    border-radius: 2px;
    overflow: hidden;
    pointer-events: none;
    transition: height 0.15s ease;
}

.cp-progress-wrap:hover .cp-progress-bar,
.cp-progress-wrap:active .cp-progress-bar {
    height: 6px;
}

.cp-progress-buffered {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: rgba(255,255,255,0.25);
    border-radius: 2px;
    width: 0%;
}

.cp-progress-filled {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: var(--color-primary, #ff6b35);
    border-radius: 2px;
    width: 0%;
}

.cp-progress-seek {
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    -webkit-tap-highlight-color: transparent;
}

/* ── Buttons row ── */
.cp-buttons-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2px;
    height: 36px;
}

.cp-buttons-left,
.cp-buttons-right {
    display: flex;
    align-items: center;
    gap: 2px;
}

/* ── Common button style ── */
.cp-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: rgba(255,255,255,0.9);
    font-size: 15px;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.cp-btn:hover {
    background: rgba(255,255,255,0.12);
    color: #fff;
}

.cp-btn:active {
    transform: scale(0.9);
}

/* ── Volume group (button + slider isolated from timecodes) ── */
.cp-volume-group {
    display: inline-flex;
    align-items: center;
    gap: 0;
}

.cp-volume-slider-wrap {
    width: 0;
    overflow: hidden;
    transition: width 0.25s ease;
    display: flex;
    align-items: center;
}

/* Desktop: open on hover over the volume GROUP only (not entire left bar) */
.cp-volume-group:hover .cp-volume-slider-wrap,
.cp-volume-slider-wrap.open {
    width: 80px;
}

.cp-volume-slider {
    width: 68px;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: linear-gradient(to right, var(--color-primary, #ff6b35) var(--cp-vol-pct, 100%), rgba(255,255,255,0.15) var(--cp-vol-pct, 100%));
    border-radius: 20px;
    outline: none;
    cursor: pointer;
    margin: 0 6px;
    -webkit-tap-highlight-color: transparent;
}

.cp-volume-slider::-webkit-slider-runnable-track {
    height: 6px;
    border-radius: 20px;
}

.cp-volume-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    cursor: pointer;
    border: none;
    margin-top: -4px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

.cp-volume-slider::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    cursor: pointer;
    border: none;
    box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

.cp-volume-slider::-moz-range-track {
    height: 6px;
    background: rgba(255,255,255,0.15);
    border-radius: 20px;
}

/* ── Time display ── */
.cp-time {
    font-size: 13px;
    font-weight: 500;
    color: rgba(255,255,255,0.8);
    white-space: nowrap;
    user-select: none;
    margin-left: 8px;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.02em;
}

/* ── Timecodes inline (inside controls bar after time) ── */
.cp-timecodes {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-left: 8px;
}

.cp-timecodes.d-none { display: none !important; }

.cp-tc-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: rgba(255,255,255,0.7);
    font-size: 11px;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.cp-tc-btn:hover {
    background: rgba(255,255,255,0.12);
    color: #fff;
}

.cp-tc-btn:disabled {
    opacity: 0.3;
    cursor: default;
}

.cp-tc-current {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 4px;
    color: var(--color-primary, #ff6b35);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease;
    white-space: nowrap;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-tap-highlight-color: transparent;
}

.cp-tc-current:hover {
    background: rgba(255,255,255,0.15);
}

.cp-tc-current i {
    font-size: 9px;
    color: rgba(255,255,255,0.5);
}

/* ── Cast to TV button ── */
.cp-btn--cast-active {
    color: var(--color-primary, #ff6b35) !important;
}
.cp-btn--cast-active i {
    animation: cp-cast-pulse 1.5s ease-in-out infinite;
}
@keyframes cp-cast-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ── Settings anchor (gear + dropdown) ── */
.cp-settings-anchor {
    position: relative;
    display: inline-flex;
}

/* ══ Mobile responsive ══ */
@media (max-width: 768px) {
    /* ── Кнопки масштабируются по ширине экрана (clamp: min 30, ideal 9vw, max 40) ── */
    .cp-btn {
        width: clamp(30px, 9vw, 40px);
        height: clamp(30px, 9vw, 40px);
        font-size: clamp(13px, 3.8vw, 16px);
    }
    .cp-buttons-row {
        height: clamp(30px, 9vw, 40px);
        gap: 0;
    }
    .cp-buttons-left,
    .cp-buttons-right {
        gap: 0;
    }
    .cp-play-center {
        width: clamp(44px, 14vw, 56px);
        height: clamp(44px, 14vw, 56px);
        font-size: clamp(16px, 5vw, 22px);
    }
    .cp-time {
        font-size: clamp(10px, 2.8vw, 12px);
        margin-left: 3px;
    }
    .cp-controls {
        padding: 0 6px 4px;
        gap: 1px;
    }
    .cp-progress-wrap {
        height: 20px;
    }

    /* ── Volume на мобиле: только по тапу ── */
    .cp-volume-slider-wrap {
        width: 0;
    }
    .cp-volume-slider-wrap.open {
        width: 70px;
    }
    .cp-volume-group:hover .cp-volume-slider-wrap {
        width: 0;
    }
    .cp-volume-group:hover .cp-volume-slider-wrap.open {
        width: 70px;
    }
    .cp-volume-slider {
        width: 58px;
        margin: 0 4px;
    }

    /* ── Таймкоды: компактные ── */
    .cp-timecodes {
        margin-left: 2px;
        gap: 1px;
    }
    .cp-tc-btn {
        width: 24px;
        height: 24px;
        font-size: 10px;
    }
    .cp-tc-current {
        max-width: 72px;
        font-size: 10px;
        padding: 1px 5px;
        gap: 2px;
    }
    .cp-tc-current i {
        font-size: 8px;
    }

    /* ── Меню качества: компактные пункты, прокрутка работает ── */
    .quality-option {
        min-height: 36px;
        padding: 8px 12px;
        font-size: 13px;
    }
    .player-settings-btn {
        min-height: 36px;
        padding: 8px 12px;
    }
    .player-settings-btn-title {
        font-size: 12px;
    }
    .quality-dropdown-header {
        padding: 7px 12px;
        font-size: 10px;
    }
    .quality-dropdown {
        border-radius: 10px;
    }
    .quality-options,
    .player-settings-options {
        max-height: min(240px, calc(100vh - 120px));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        touch-action: pan-y;
    }
}

/* ══ Very small phones (≤420px) — clamp уже справится, но тут тонкие настройки ══ */
@media (max-width: 420px) {
    .cp-tc-current {
        max-width: 56px;
        font-size: 9px;
    }
    .cp-tc-btn {
        width: clamp(22px, 6vw, 26px);
        height: clamp(22px, 6vw, 26px);
        font-size: 10px;
    }
    .cp-controls {
        padding: 0 4px 3px;
    }
}

/* Popover (home: Quality/Audio) */
.quality-dropdown {
    position: absolute;
    bottom: calc(100% + 8px);
    right: 0;
    min-width: 190px;
    max-width: 220px;
    background: rgba(0, 0, 0, 0.92);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 12px;
    /* Важно: flyout-панель (.player-settings-panel) позиционируется ВНЕ поповера (слева),
       поэтому overflow не должен обрезать её. */
    overflow: visible;
    z-index: 140;
    opacity: 0;
    pointer-events: none;
    transform: translateY(8px);
    transition: opacity 0.18s ease, transform 0.18s ease;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}

.quality-dropdown.active {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

@media (max-width: 520px) {
    /* На мобиле: dropdown = полноэкранный overlay (как YouTube) */
    .quality-dropdown {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: none !important;
        max-height: none !important;
        min-width: 0 !important;
        border-radius: 0 !important;
        border: none !important;
        background: rgba(0, 0, 0, 0.95) !important;
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        display: flex !important;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        z-index: 9999 !important;
        /* Убираем transform чтобы не мерцало */
        transform: none !important;
        transition: opacity 0.15s ease !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    .quality-dropdown .player-settings-home {
        width: 100%;
        max-width: 260px;
        padding: 16px;
    }
    .quality-dropdown .quality-dropdown-header {
        text-align: center;
        padding: 12px 16px;
        font-size: 12px;
    }
    .quality-dropdown .player-settings-btn {
        min-height: 48px;
        padding: 14px 16px;
        font-size: 15px;
        border-radius: 8px;
        margin-bottom: 4px;
    }
    .quality-dropdown .player-settings-btn:hover,
    .quality-dropdown .player-settings-btn:active {
        background: rgba(255, 255, 255, 0.12);
    }
}

.quality-dropdown-header {
    padding: 10px 14px;
    font-size: 11px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: rgba(255, 255, 255, 0.70);
    border-bottom: 1px solid rgba(255, 255, 255, 0.10);
}

.player-settings-home {
    display: flex;
    flex-direction: column;
}

.player-settings-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 14px;
    background: transparent;
    border: 0;
    color: rgba(255, 255, 255, 0.95);
    cursor: pointer;
    text-align: left;
    transition: background 0.18s ease;
}

.player-settings-btn:hover {
    background: rgba(255, 255, 255, 0.10);
}

.player-settings-btn-title {
    font-size: 13px;
    font-weight: 750;
}

.player-settings-btn-right {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.player-settings-current {
    font-size: 12px;
    font-weight: 900;
    color: rgba(255, 255, 255, 0.80);
    max-width: 115px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.player-settings-btn-right i {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.55);
}

/* Flyout panel: opens to the LEFT of the popover */
.player-settings-panel {
    position: absolute;
    top: 0;
    right: calc(100% + 8px);
    width: 220px;
    max-width: min(240px, calc(100vw - 24px));
    background: rgba(0, 0, 0, 0.92);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 12px;
    overflow: hidden;
    z-index: 141;
    transform: translateX(10px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s ease, transform 0.18s ease;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
    display: flex;
    flex-direction: column;
}

.player-settings-panel.active {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
}

/* Panel header controls (kept for mobile overlay; hidden on desktop) */
.player-settings-back,
.player-settings-panel-title {
    display: none;
}

@media (max-width: 520px) {
    /* На мобиле: settings panel в потоке внутри dropdown (full-screen overlay) */
    .player-settings-panel {
        position: static !important;
        width: 100%;
        max-width: 260px;
        max-height: none;
        border-radius: 0;
        border: none;
        background: transparent;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        overflow: visible;
        transform: none !important;
        opacity: 1 !important;
        pointer-events: auto !important;
        box-shadow: none;
        display: none;
        z-index: auto;
        padding: 16px 0;
    }

    .player-settings-panel.active {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .player-settings-back {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        width: 100%;
        padding: 12px 16px;
        background: transparent;
        border: 0;
        border-bottom: 1px solid rgba(255, 255, 255, 0.10);
        color: rgba(255, 255, 255, 0.92);
        cursor: pointer;
        text-align: left;
        font-size: 14px;
    }

    .player-settings-back i {
        color: rgba(255, 255, 255, 0.70);
    }

    .player-settings-panel-title {
        display: block;
        width: 100%;
        padding: 12px 16px 6px;
        font-size: 11px;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        color: rgba(255, 255, 255, 0.70);
    }

    .player-settings-section {
        width: 100%;
    }

    .quality-options,
    .player-settings-options {
        max-height: 60vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        touch-action: pan-y;
    }

    .quality-option {
        min-height: 44px;
        padding: 12px 16px;
        font-size: 15px;
    }
}

.player-settings-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.quality-options,
.player-settings-options {
    max-height: min(320px, calc(100vh - 170px));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    /* Mobile: prevent scroll chaining to page */
    overscroll-behavior: contain;
    touch-action: pan-y;
}

.quality-option {
    padding: 10px 14px;
    font-size: 13px;
    font-weight: 650;
    color: rgba(255, 255, 255, 0.95);
    cursor: pointer;
    transition: background 0.18s ease;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.quality-option:hover {
    background: rgba(255, 255, 255, 0.10);
}

.quality-option.active,
.quality-option.selected {
    background: var(--color-primary, #22c55e);
    color: #ffffff;
}

.quality-option.active::after,
.quality-option.selected::after {
    content: '✓';
    margin-left: 8px;
    font-weight: 900;
}

/* Neon theme highlight */
html[data-theme="neon"] .quality-option.active,
html[data-theme="neon"] .quality-option.selected {
    background: linear-gradient(135deg, #00f3ff, #a855f7);
}

/* Scrollbar */
.quality-options::-webkit-scrollbar,
.player-settings-options::-webkit-scrollbar {
    width: 6px;
}
.quality-options::-webkit-scrollbar-track,
.player-settings-options::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
}
.quality-options::-webkit-scrollbar-thumb,
.player-settings-options::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.25);
    border-radius: 3px;
}
.quality-options::-webkit-scrollbar-thumb:hover,
.player-settings-options::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.40);
}

/* Light theme: keep menu readable INSIDE video container (always dark) */
html[data-theme="light"] .player-video-container #playerQualityButton .quality-btn,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .quality-btn,
html[data-theme="light"] .player-video-container #playerQualityButton .quality-dropdown,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .quality-dropdown,
html[data-theme="light"] .player-video-container #playerQualityButton .player-settings-panel,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .player-settings-panel {
    background: rgba(0, 0, 0, 0.92) !important;
    color: #ffffff !important;
    border-color: rgba(255, 255, 255, 0.15) !important;
}

html[data-theme="light"] .player-video-container #playerQualityButton .player-settings-btn,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .player-settings-btn,
html[data-theme="light"] .player-video-container #playerQualityButton .player-settings-current,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .player-settings-current,
html[data-theme="light"] .player-video-container #playerQualityButton .player-settings-btn-right i,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .player-settings-btn-right i,
html[data-theme="light"] .player-video-container #playerQualityButton .quality-option,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .quality-option,
html[data-theme="light"] .player-video-container #playerQualityButton .quality-dropdown-header,
html[data-theme="light_japan"] .player-video-container #playerQualityButton .quality-dropdown-header {
    color: rgba(255, 255, 255, 0.92) !important;
}


