/* env() cannot be read from JavaScript, so the insets are mirrored onto custom properties that
   the self-view placement can measure. Without them the thumbnail sat 8px from the edge on every
   side and ended up under the home indicator on a phone. (#484) */
:root {
    --fruuli-safe-top: env(safe-area-inset-top, 0px);
    --fruuli-safe-right: env(safe-area-inset-right, 0px);
    --fruuli-safe-bottom: env(safe-area-inset-bottom, 0px);
    --fruuli-safe-left: env(safe-area-inset-left, 0px);
}

.remote-video-container {
    display: grid;
    gap: 0.5rem;
    width: 100%;
    height: 100%;
    padding: 0.5rem;
}

.remote-video {
    position: relative;
    aspect-ratio: 8/5;
    width: 100%;
    height: 100%;
    overflow: hidden;
    border-radius: 8px;
    background: var(--mud-palette-surface);
}

/* The tile carries height: 100%, which overrides its aspect-ratio, so its shape follows the grid
   cell and not the camera. object-fit: cover then scales the stream up until the shorter side
   fills and crops the rest away - a tall single-participant cell cropped a 16:9 camera down to a
   heavily zoomed strip. Show the whole frame and letterbox it, which is what the mobile rule
   below and the screen-share view already do. (#514) */
.remote-video video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
}

.participant-initials {
    position: absolute;
    inset: 0;
    font-size: 3rem;
    font-weight: 600;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

.participant-name {
    position: absolute;
    bottom: 8px;
    left: 8px;
    padding: 4px 8px;
    background: rgba(0,0,0,0.7);
    color: white;
    border-radius: 4px;
    font-size: 0.875rem;
    z-index: 2;
}

.participant-muted {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 4px 8px;
    background: rgba(220, 38, 38, 0.9);
    color: white;
    border-radius: 4px;
    font-size: 1.25rem;
    z-index: 2;
}

/* 1 item - full screen */
.remote-video-container:has(.remote-video:nth-child(1):last-child) {
    grid-template-columns: 1fr;
}

/* 2 items - side by side on desktop, stacked on mobile */
.remote-video-container:has(.remote-video:nth-child(2):last-child) {
    grid-template-columns: 1fr;
}

@media (min-width: 768px) {
    .remote-video-container:has(.remote-video:nth-child(2):last-child) {
        grid-template-columns: 1fr 1fr;
    }
}

/* 3 items - 2 top, 1 bottom centered on desktop */
.remote-video-container:has(.remote-video:nth-child(3):last-child) {
    grid-template-columns: 1fr;
}

@media (min-width: 768px) {
    .remote-video-container:has(.remote-video:nth-child(3):last-child) {
        grid-template-columns: 1fr 1fr;
    }

        .remote-video-container:has(.remote-video:nth-child(3):last-child) .remote-video:nth-child(3) {
            grid-column: 1 / -1;
            max-width: 50%;
            margin: 0 auto;
        }
}

/* 4 items - 1 column on mobile, 2x2 grid on tablet+ */
.remote-video-container:has(.remote-video:nth-child(4):last-child) {
    grid-template-columns: 1fr;
}

@media (min-width: 768px) {
    .remote-video-container:has(.remote-video:nth-child(4):last-child) {
        grid-template-columns: 1fr 1fr;
    }
}

/* 5+ items - 3 columns on desktop, 2 on tablet, 1 on mobile */
.remote-video-container:has(.remote-video:nth-child(n+5)) {
    grid-template-columns: 1fr;
}

@media (min-width: 768px) {
    .remote-video-container:has(.remote-video:nth-child(n+5)) {
        grid-template-columns: 1fr 1fr;
    }
}

@media (min-width: 1024px) {
    .remote-video-container:has(.remote-video:nth-child(n+5)) {
        grid-template-columns: 1fr 1fr 1fr;
    }
}


/* Main layout container - switches between grid and focus mode */
.video-call-layout {
    display: flex;
    width: 100%;
    height: 100%;
    gap: 0.5rem;
    padding: 0.5rem;
}

/* Main content area - shows screen share */
.video-call-layout .main-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    overflow: hidden;
}

/* Participants sidebar - vertical filmstrip */
.video-call-layout .participants-sidebar {
    width: 220px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto;
    overflow-x: hidden;
}

/* Sidebar participant thumbnail */
.sidebar-participant {
    position: relative;
    aspect-ratio: 16/9;
    width: 100%;
    flex-shrink: 0;
    overflow: hidden;
    border-radius: 6px;
    background: var(--mud-palette-surface);
}

.sidebar-participant video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.sidebar-participant .participant-initials {
    font-size: 1.5rem;
}

.sidebar-participant .participant-name {
    font-size: 0.75rem;
    padding: 2px 6px;
    bottom: 4px;
    left: 4px;
}

.sidebar-participant .participant-muted {
    font-size: 0.875rem;
    padding: 2px 4px;
    top: 4px;
    right: 4px;
}

/* Local video in sidebar */
.sidebar-participant.local-participant {
    border: 2px solid var(--mud-palette-primary);
}

/* Screen share in main content (focus mode) */
.main-content .screen-share-video {
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: auto;
    border: none;
    border-radius: 8px;
}

.main-content .screen-share-video video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
}

/* Screen share paused overlay */
.screen-share-paused {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.7);
    z-index: 5;
}

.screen-share-paused-content {
    text-align: center;
    color: white;
}

.screen-share-paused-icon {
    font-size: 3rem;
    margin-bottom: 0.5rem;
}

.screen-share-paused-text {
    font-size: 1rem;
    opacity: 0.9;
}

/* Responsive: sidebar becomes bottom bar on mobile */
@media (max-width: 768px) {
    .video-call-layout {
        flex-direction: column;
    }

    .video-call-layout .main-content {
        flex: 1;
        min-height: 0;
    }

    .video-call-layout .participants-sidebar {
        width: 100%;
        height: 100px;
        flex-direction: row;
        overflow-x: auto;
        overflow-y: hidden;
        padding-bottom: env(safe-area-inset-bottom, 0px);
        box-sizing: content-box;
    }

    .sidebar-participant {
        width: 120px;
        height: 100%;
        aspect-ratio: auto;
        flex-shrink: 0;
    }
}

/* Screen share styles (grid mode - legacy, kept for fallback) */
.screen-share-video {
    position: relative;
    aspect-ratio: 16/9;
    width: 100%;
    height: auto;
    min-height: 200px;
    overflow: hidden;
    border-radius: 8px;
    background: var(--mud-palette-background);
    border: 2px solid var(--mud-palette-success);
    grid-column: 1 / -1;
}

.screen-share-video video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
}

.screen-share-name {
    position: absolute;
    bottom: 8px;
    left: 8px;
    padding: 4px 12px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 4px;
    font-size: 0.875rem;
    z-index: 2;
    display: flex;
    align-items: center;
}

/* Local video - floating PiP */
#local-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 240px;
    z-index: 10;
    border-radius: 8px;
    overflow: hidden;
    touch-action: none;
    will-change: transform;
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94), width 0.2s ease, opacity 0.2s ease;
    cursor: grab;
    user-select: none;
}

#local-video.pip-hidden {
    display: none;
}

#local-video.pip-dragging {
    transition: none;
    cursor: grabbing;
}

#local-video.pip-minimized {
    opacity: 0.6;
    cursor: pointer;
}

#local-video.pip-size-small {
    width: 120px;
}

#local-video.pip-size-medium {
    width: 180px;
}

#local-video.pip-size-large {
    width: 240px;
}

#local-video.pip-size-fullscreen {
    width: 100%;
    height: 100%;
    border-radius: 0;
}

#local-video-card {
    width: 100%;
    height: 100%;
}

#local-video video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 8px;
}

/* Mobile optimizations */
@media (max-width: 768px) {
    .remote-video {
        aspect-ratio: 9/16;
    }

    /* Local video PiP - mobile sizes */
    #local-video {
        aspect-ratio: 9/16;
    }

    #local-video.pip-size-small {
        width: 60px;
    }

    #local-video.pip-size-medium {
        width: 80px;
    }

    #local-video.pip-size-large {
        width: 120px;
    }

    /* Participant initials - smaller on mobile */
    .participant-initials {
        font-size: 2rem;
    }

    /* Lobby preview - keep the join actions reachable */
    #lobby-video-preview {
        max-height: 28vh;
    }

    .lobby-preview-initials {
        font-size: 2rem;
    }

    /* Participant name badge - smaller on mobile */
    .participant-name {
        font-size: 0.75rem;
        padding: 2px 6px;
        bottom: 4px;
        left: 4px;
    }

    /* Muted indicator - smaller on mobile */
    .participant-muted {
        font-size: 1rem;
        padding: 2px 4px;
        top: 4px;
        right: 4px;
    }

    /* Screen share - reduced min-height on mobile */
    .screen-share-video {
        min-height: 120px;
    }

    /* Remote video container - tighter spacing */
    .remote-video-container {
        gap: 0.25rem;
        padding: 0.25rem;
    }

    /* Remote video - smaller border radius */
    .remote-video {
        border-radius: 6px;
    }
}

/* Lobby video preview */
#lobby-video-preview {
    width: 100%;
    max-height: 32vh;
    aspect-ratio: 16 / 9;
    border-radius: 12px;
    overflow: hidden;
    background: #1a1a2e;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

#lobby-video-preview video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
}

.lobby-actions {
    position: sticky;
    bottom: 0;
    z-index: 5;
    width: 100%;
    padding: 12px 0 4px 0;
    background: var(--mud-palette-surface);
}

.lobby-preview-initials {
    font-size: 3rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.7);
    user-select: none;
}

/* Flip camera button overlay */
.flip-camera-btn {
    position: absolute;
    bottom: 8px;
    right: 8px;
    z-index: 15;
    background: rgba(0, 0, 0, 0.5) !important;
    color: white !important;
    min-width: 44px;
    min-height: 44px;
    border-radius: 50%;
    backdrop-filter: blur(4px);
}

.flip-camera-btn:hover {
    background: rgba(0, 0, 0, 0.7) !important;
}

/* Video zoom - pinch-to-zoom and pan on video elements */
.remote-video video,
#local-video video {
    transform-origin: 0 0;
}

/* Touch device specific - ensure tap targets are minimum 44x44px */
@media (hover: none) and (pointer: coarse) {
    .participant-name,
    .participant-muted {
        min-height: 24px;
        display: flex;
        align-items: center;
    }
}
