/* ==========================================================================
   BlinkDuel Arena - Persistent Real-Time Duel Room Styles
   ========================================================================== */

:root {
    --bd-arena-max-width: 1480px;
    --bd-bg-dark: #0c0e12;
    --bd-surface: #12151b;
    --bd-surface-glass: rgba(18, 21, 27, 0.75);
    --bd-border: rgba(255, 255, 255, 0.09);
    --bd-border-focus: #38bdf8;
    --bd-cyan: #38bdf8;
    --bd-cyan-glow: rgba(56, 189, 248, 0.35);
    --bd-green: #22c55e;
    --bd-green-glow: rgba(34, 197, 94, 0.35);
    --bd-red: #ff796a;
    --bd-text: #f8fafc;
    --bd-text-muted: #94a3b8;
    --bd-radius: 20px;
}

/* Room Lobby Container */
#room-lobby[data-entry="false"] {
    max-width: var(--bd-arena-max-width);
    margin: 0 auto;
    padding: 8px 0 32px;
    width: 100%;
}

/* Arena Header */
#room-lobby[data-entry="false"] .duel-room-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--bd-surface-glass);
    backdrop-filter: blur(12px);
    border: 1px solid var(--bd-border);
    border-radius: 14px;
    margin-bottom: 12px;
    position: sticky;
    top: 8px;
    z-index: 25;
}

#room-lobby[data-entry="false"] .duel-room-header .eyebrow {
    color: var(--bd-cyan);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin: 0 0 2px 0;
}

#room-lobby[data-entry="false"] .duel-room-header h1 {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 0;
    color: var(--bd-text);
}

#room-lobby #leave-room {
    min-height: 38px;
    padding: 6px 14px;
    font-size: 0.85rem;
    font-weight: 600;
    border-radius: 8px;
    border: 1px solid rgba(239, 68, 68, 0.35);
    color: #fca5a5;
    background: rgba(239, 68, 68, 0.08);
    transition: all 0.15s ease;
}

#room-lobby #leave-room:hover {
    background: rgba(239, 68, 68, 0.2);
    border-color: var(--bd-red);
    color: #fff;
}

/* ==========================================================================
   Desktop Arena Layout (YOU | VS | OPPONENT)
   ========================================================================== */

#duel-media {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
}

#duel-media .duel-videos {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 90px minmax(0, 1fr);
    gap: 16px;
    align-items: center;
    position: relative;
    width: 100%;
}

/* Player Panels */
#duel-media .local-video,
#duel-media .remote-video {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    width: 100%;
    position: relative;
}

/* Player Header Bar */
#duel-media .arena-player {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    background: var(--bd-surface);
    border: 1px solid var(--bd-border);
    border-radius: 12px;
    min-height: 48px;
}

#duel-media .player-identity {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

#duel-media .player-tag {
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.06em;
    padding: 2px 8px;
    border-radius: 6px;
    text-transform: uppercase;
}

#duel-media .self-tag {
    background: rgba(56, 189, 248, 0.15);
    color: var(--bd-cyan);
    border: 1px solid rgba(56, 189, 248, 0.3);
}

#duel-media .opponent-tag {
    background: rgba(244, 63, 94, 0.15);
    color: #fb7185;
    border: 1px solid rgba(244, 63, 94, 0.3);
}

#duel-media .arena-player h3 {
    margin: 0;
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--bd-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#duel-media .player-badge {
    font-size: 0.8rem;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--bd-text-muted);
    border: 1px solid var(--bd-border);
    flex-shrink: 0;
}

#duel-media .arena-player:has([id$="-status"]:is(:contains("Ready"), [data-ready="true"])) .player-badge {
    background: rgba(34, 197, 94, 0.15);
    color: var(--bd-green);
    border-color: rgba(34, 197, 94, 0.3);
}

/* Camera Stages */
#duel-media .camera-stage {
    width: 100%;
    max-width: none;
    aspect-ratio: 4 / 3;
    min-height: 380px;
    max-height: 560px;
    background: #07090c;
    border: 1px solid var(--bd-border);
    border-radius: var(--bd-radius);
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.6), 0 12px 32px rgba(0, 0, 0, 0.4);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#duel-media .camera-stage.duel-frame {
    border: 6px solid transparent;
    background: linear-gradient(#07090c, #07090c) padding-box, var(--rim) border-box;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.6), inset 0 0 30px rgba(0, 0, 0, 0.7);
}

#duel-media .local-video .camera-stage:not(.duel-frame) {
    border-color: rgba(56, 189, 248, 0.2);
}

#duel-media .remote-video .camera-stage:not(.duel-frame) {
    border-color: rgba(244, 63, 94, 0.2);
}

#duel-media .camera-stage video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center center;
    border-radius: inherit;
}

#duel-media #camera-video {
    transform: scaleX(-1);
}

#duel-media #opponent-video {
    transform: none;
}

#duel-media .camera-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: #64748b;
    font-size: 0.95rem;
    font-weight: 500;
    text-align: center;
    padding: 24px;
}

#duel-media .placeholder-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    color: #475569;
}

/* Simulated Opponent Avatar */
#duel-media .simulated-opponent {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 24px;
    text-align: center;
}

#duel-media .simulated-opponent img {
    width: 96px;
    height: auto;
    filter: drop-shadow(0 4px 12px rgba(0,0,0,0.5));
}

#duel-media .simulated-opponent strong {
    font-size: 1rem;
    color: var(--bd-text);
}

#duel-media #simulated-opponent-state {
    font-size: 0.85rem;
    color: var(--bd-green);
    background: rgba(34, 197, 94, 0.12);
    padding: 3px 10px;
    border-radius: 12px;
}

/* ==========================================================================
   Center VS Column & Game State
   ========================================================================= */

#duel-media .duel-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    width: 100%;
    position: relative;
    z-index: 10;
}

#duel-media .versus-label {
    font-size: 1.85rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    color: var(--bd-cyan);
    text-shadow: 0 0 24px var(--bd-cyan-glow);
    line-height: 1;
    margin-bottom: 6px;
}

#duel-media #room-state {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--bd-text-muted);
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--bd-border);
    border-radius: 14px;
    padding: 4px 8px;
    margin: 0;
    max-width: 90px;
    overflow-wrap: break-word;
    text-align: center;
    line-height: 1.25;
}

/* ==========================================================================
   Match Stage & Countdown / Result Overlays
   ========================================================================== */

#duel-media .match-stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    width: 100%;
}

/* Countdown Overlay: Dramatic, immersive, scale animation */
#room-lobby[data-arena="countdown"] #match-stage {
    position: absolute;
    inset: 0;
    z-index: 30;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(10, 12, 16, 0.5);
    backdrop-filter: blur(4px);
    border-radius: var(--bd-radius);
    pointer-events: none;
    text-align: center;
}

#room-lobby[data-arena="countdown"] #match-label {
    font-size: clamp(4.5rem, 12vw, 8.5rem);
    font-weight: 900;
    color: var(--bd-green);
    text-shadow: 0 0 50px var(--bd-green-glow), 0 0 100px rgba(34, 197, 94, 0.3);
    line-height: 1;
    animation: countdownScale 0.9s cubic-bezier(0.16, 1, 0.3, 1) infinite;
    letter-spacing: 0.02em;
}

@keyframes countdownScale {
    0% { transform: scale(1.18); opacity: 0.8; }
    50% { transform: scale(1); opacity: 1; }
    100% { transform: scale(0.96); opacity: 0.9; }
}

#room-lobby:is([data-arena="countdown"], [data-arena="playing"]) .versus-label,
#room-lobby:is([data-arena="countdown"], [data-arena="playing"]) #room-state {
    display: none;
}

/* During Duel (Playing Mode) */
#room-lobby[data-arena="playing"] #match-stage {
    display: flex;
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 25;
    pointer-events: none;
    background: rgba(10, 12, 16, 0.75);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 20px;
    padding: 6px 16px;
}

#room-lobby[data-arena="playing"] #match-label {
    font-size: 0.95rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    color: var(--bd-green);
    text-transform: uppercase;
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
}

#room-lobby[data-arena="playing"] #match-label::before {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--bd-green);
    box-shadow: 0 0 10px var(--bd-green);
    animation: livePulse 1.2s ease-in-out infinite;
}

@keyframes livePulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.85); }
}

/* Face loss warning banner */
#duel-media #face-loss-warning {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 28;
    background: rgba(239, 68, 68, 0.92);
    color: #fff !important;
    padding: 10px 20px;
    border-radius: 12px;
    font-weight: 700;
    font-size: 0.95rem;
    box-shadow: 0 8px 24px rgba(239, 68, 68, 0.4);
    pointer-events: none;
    white-space: nowrap;
}

/* Conceal setup and lobby controls during duel */
#room-lobby:is([data-arena="countdown"], [data-arena="playing"]) .room-overview,
#room-lobby:is([data-arena="countdown"], [data-arena="playing"]) #clock-status,
#room-lobby:is([data-arena="countdown"], [data-arena="playing"]) .room-actions {
    display: none !important;
}

#room-lobby[data-arena="playing"] #get-ready-drawer,
#room-lobby[data-arena="playing"] #camera-consent-details,
#room-lobby[data-arena="playing"] #readiness-panel,
#room-lobby[data-arena="playing"] #preparation-calibration,
#room-lobby[data-arena="playing"] #duel-diagnostics,
#room-lobby[data-arena="playing"] .ready-controls {
    display: none !important;
}

/* ==========================================================================
   Result Overlay (Over Arena, Cameras Visible Behind)
   ========================================================================== */

#room-lobby[data-arena="result"] .duel-videos {
    position: relative;
}

#room-lobby[data-arena="result"] .versus-label,
#room-lobby[data-arena="result"] #room-state {
    display: none;
}

#room-lobby[data-arena="result"] #match-stage {
    position: absolute;
    inset: 0;
    z-index: 35;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(10, 13, 18, 0.82);
    backdrop-filter: blur(14px);
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: var(--bd-radius);
    padding: 32px 24px;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.7);
    animation: resultFadeIn 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes resultFadeIn {
    from { opacity: 0; transform: scale(0.96); }
    to { opacity: 1; transform: scale(1); }
}

#duel-media #result-artwork {
    width: 96px;
    height: 96px;
    object-fit: contain;
    filter: drop-shadow(0 8px 24px rgba(0, 0, 0, 0.6));
    margin-bottom: 12px;
}

#duel-media #match-label {
    font-size: clamp(2.2rem, 5vw, 3.8rem);
    font-weight: 900;
    letter-spacing: 0.04em;
    line-height: 1.1;
    margin: 0 0 8px 0;
    text-transform: uppercase;
}

#duel-media #match-stage[data-outcome="win"] #match-label {
    color: var(--bd-green);
    text-shadow: 0 0 36px var(--bd-green-glow);
}

#duel-media #match-stage[data-outcome="loss"] #match-label {
    color: #ff796a;
    background: #101114;
    text-shadow: 0 0 36px rgba(239, 68, 68, 0.4);
}

#duel-media #match-stage[data-outcome="draw"] #match-label {
    color: var(--bd-cyan);
    text-shadow: 0 0 36px var(--bd-cyan-glow);
}

#duel-media #match-stage[data-outcome="pending"] #match-label {
    color: var(--bd-text);
}

#duel-media #match-detail {
    font-size: 1.1rem;
    color: #cbd5e1;
    margin: 0 0 8px 0;
    max-width: 500px;
}

#duel-media #match-duration {
    font-size: 1rem;
    font-weight: 600;
    color: var(--bd-text-muted);
    font-variant-numeric: tabular-nums;
    margin: 0 0 16px 0;
    background: rgba(255, 255, 255, 0.06);
    padding: 4px 12px;
    border-radius: 12px;
}

#duel-media .result-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 14px;
    margin-top: 12px;
}

#duel-media .result-actions .btn {
    min-height: 48px;
    padding: 10px 24px;
    font-size: 1rem;
    font-weight: 700;
    border-radius: 12px;
    transition: all 0.15s ease;
}

#duel-media #draw-rematch {
    background: var(--bd-green);
    color: #0c0e12;
    border: 0;
    box-shadow: 0 4px 16px var(--bd-green-glow);
}

#duel-media #draw-rematch:hover:not(:disabled) {
    background: #16a34a;
    transform: translateY(-1px);
}

#duel-media #new-duel {
    background: transparent;
    color: var(--bd-text);
    border: 1px solid rgba(255, 255, 255, 0.25);
}

#duel-media #new-duel:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: #fff;
}

#duel-media #rematch-status {
    font-size: 0.875rem;
    color: var(--bd-text-muted);
    margin-top: 14px;
    max-width: 480px;
}

/* ==========================================================================
   Mobile Arena Layout (FaceTime / PIP Mode <= 768px)
   ========================================================================== */

@media (max-width: 768px) {
    #duel-media .duel-videos {
        position: relative;
        width: 100%;
        height: 66vh;
        min-height: 460px;
        max-height: 680px;
        border-radius: var(--bd-radius);
        overflow: hidden;
        background: #07090c;
        border: 1px solid var(--bd-border);
        box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6);
        display: block;
        margin: 6px 0 16px;
    }

    /* Opponent video is full background */
    #duel-media .remote-video {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
    }

    #duel-media .remote-video .camera-stage {
        width: 100%;
        height: 100%;
        min-height: 0;
        max-height: none;
        aspect-ratio: auto;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    #duel-media .remote-video .arena-player {
        position: absolute;
        top: 12px;
        left: 12px;
        z-index: 6;
        background: rgba(12, 15, 20, 0.78);
        backdrop-filter: blur(10px);
        padding: 6px 12px;
        border-radius: 20px;
        border: 1px solid rgba(255, 255, 255, 0.12);
        min-height: auto;
        gap: 8px;
    }

    #duel-media .remote-video .arena-player h3 {
        font-size: 0.85rem;
    }

    /* Local "You" video is floating PIP in upper-right */
    #duel-media .local-video {
        position: absolute;
        top: 12px;
        right: 12px;
        width: 124px;
        height: 168px;
        z-index: 15;
        border-radius: 16px;
        overflow: hidden;
        box-shadow: 0 10px 28px rgba(0, 0, 0, 0.75);
        border: 2px solid rgba(255, 255, 255, 0.28);
        background: #12151b;
        transition: transform 0.2s ease;
    }

    #duel-media .local-video .camera-stage {
        width: 100%;
        height: 100%;
        min-height: 0;
        max-height: none;
        aspect-ratio: auto;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    #duel-media .local-video .camera-stage.duel-frame {
        border: 3px solid transparent;
        background: linear-gradient(#07090c, #07090c) padding-box, var(--rim) border-box;
    }

    #duel-media .local-video video {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: center center;
    }

    #duel-media .local-video .camera-placeholder {
        padding: 8px;
        gap: 4px;
        font-size: 0.72rem;
    }

    #duel-media .local-video .placeholder-icon {
        width: 40px;
        height: 40px;
    }

    #duel-media .local-video .placeholder-icon svg {
        width: 20px;
        height: 20px;
    }

    #duel-media .local-video .arena-player {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 4;
        background: linear-gradient(transparent, rgba(10, 12, 16, 0.9));
        padding: 4px 6px;
        border-radius: 0;
        border: 0;
        min-height: auto;
    }

    #duel-media .local-video .player-tag {
        font-size: 0.65rem;
        padding: 1px 4px;
    }

    #duel-media .local-video .arena-player h3 {
        font-size: 0.75rem;
    }

    #duel-media .local-video .player-badge {
        display: none;
    }

    /* Center overlays */
    #duel-media .duel-center {
        position: absolute;
        inset: 0;
        z-index: 8;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        pointer-events: none;
    }

    /* Hide center VS badge and state when the invitation card is active on mobile */
    #duel-media.is-inviting .duel-center .versus-label,
    #duel-media.is-inviting .duel-center #room-state,
    #duel-media:has(#room-invitation:not([hidden])) .duel-center .versus-label,
    #duel-media:has(#room-invitation:not([hidden])) .duel-center #room-state {
        display: none;
    }

    #duel-media .duel-center .versus-label {
        font-size: 2.2rem;
        background: rgba(12, 15, 20, 0.65);
        backdrop-filter: blur(8px);
        padding: 4px 18px;
        border-radius: 24px;
        border: 1px solid rgba(255, 255, 255, 0.12);
    }

    #duel-media .duel-center #room-state {
        background: rgba(12, 15, 20, 0.75);
        backdrop-filter: blur(8px);
        margin-top: 8px;
        max-width: 180px;
    }

    #duel-media #match-stage {
        pointer-events: auto;
    }
}

/* Reduced Motion & Forced Colors */
@media (prefers-reduced-motion: reduce) {
    #duel-media * {
        animation: none !important;
        transition: none !important;
    }
}

@media (forced-colors: active) {
    #duel-media .arena-player,
    #duel-media .camera-stage {
        border-color: CanvasText;
    }
}

@media (max-height: 500px) { #room-lobby[data-entry="false"] .duel-room-header { position: static; } }
