/* =========================================
   1. GLOBAL RESET & BASE STYLES
   ========================================= */
* { box-sizing: border-box; outline: none; -webkit-tap-highlight-color: transparent; }

body {
    margin: 0; padding: 0;
    background: #09090b; /* Deep Black */
    font-family: 'Inter', sans-serif;
    height: 100vh; 
    height: 100dvh; 
    width: 100vw;
    display: flex;
    flex-direction: column;
    color: white;
    overflow: hidden; 
}

/* =========================================
   2. HEADER (TOP BAR)
   ========================================= */
.top-bar {
    height: 60px;
    background: #18181b;
    border-bottom: 1px solid #27272a;
    display: flex; align-items: center;
    padding: 0 20px;
    flex-shrink: 0; 
    z-index: 50;
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
}

.brand { 
    display: flex; align-items: center; gap: 10px; 
    font-weight: 700; font-size: 1.1rem; 
}
.brand img { width: 24px; height: 24px; }

.chat-toggle-btn {
    background: transparent;
    border: 1px solid #3f3f46;
    color: #a1a1aa;
    padding: 6px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.85rem;
    display: flex; align-items: center; gap: 6px;
    margin-left: auto; 
    margin-right: 15px;
    transition: all 0.2s;
}
.chat-toggle-btn:hover { background: #27272a; color: white; border-color: #52525b; }

.status-pill {
    background: #27272a; padding: 6px 12px; border-radius: 20px;
    font-size: 0.8rem; display: flex; align-items: center; gap: 8px; border: 1px solid #3f3f46;
}
#status-dot { width: 8px; height: 8px; background: #999; border-radius: 50%; }


/* =========================================
   3. MAIN LAYOUT CONTAINER
   ========================================= */
.app-container {
    display: flex;
    flex: 1; 
    position: relative;
    width: 100vw;
    max-width: 100vw; 
    overflow: hidden; 
}


/* =========================================
   4. VIDEO PANE (LEFT SIDE)
   ========================================= */
.video-pane {
    flex: 1; 
    background: #000;
    position: relative;
    display: flex; justify-content: center; align-items: center;
    padding: 20px;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.video-wrapper {
    width: 100%;
    height: 100%;
    max-height: 90vh; 
    position: relative;
    border-radius: 16px;
    overflow: hidden;
    background: #111;
    box-shadow: 0 0 30px rgba(0,0,0,0.6);
}

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

/* Local Video (Picture-in-Picture) */
.local-video-container {
    position: absolute;
    bottom: 20px; right: 20px;
    width: 150px; aspect-ratio: 9/16; 
    background: #000;
    border-radius: 12px;
    overflow: hidden;
    border: 2px solid rgba(255,255,255,0.2);
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    z-index: 50; /* Above regular content but below toast */
    transition: all 0.3s;
}
#localVideo { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }


/* =========================================
   5. VIDEO CONTROLS (OVERLAY) - FIXED
   ========================================= */
.video-controls {
    position: absolute;
    bottom: max(30px, calc(10px + env(safe-area-inset-bottom))); 
    left: 50%;
    transform: translateX(-50%);
    /* High Z-Index to ensure it sits on top of everything */
    z-index: 100; 
    display: flex; align-items: center; gap: 15px;
    width: max-content;
    
    /* Ensure clicks pass through the container but catch on buttons */
    pointer-events: none; 
}

/* Buttons inside controls need to catch clicks */
.video-controls > button {
    pointer-events: auto;
}

.btn {
    padding: 14px 32px;
    border-radius: 30px; border: none; font-weight: 700; font-size: 1rem;
    cursor: pointer; transition: transform 0.1s, background 0.2s;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    white-space: nowrap;
}
.btn:active { transform: scale(0.95); }

.btn-primary { background: white; color: black; }
.btn-primary:hover { background: #f4f4f5; }
.btn-danger { background: #ef4444; color: white; }
.btn-danger:hover { background: #dc2626; }

/* Circular Action Buttons (Mute & Swap) */
.btn-icon {
    width: 50px; height: 50px;
    border-radius: 50%;
    border: none;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    background: rgba(255,255,255,0.2);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: all 0.2s ease;
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
    color: white;
}
.btn-icon:hover { background: rgba(255,255,255,0.35); transform: scale(1.05); }
.btn-icon:active { transform: scale(0.95); }
.btn-icon .material-symbols-rounded { font-size: 24px; }
.btn-icon.muted { background: #ef4444; box-shadow: 0 0 15px rgba(239, 68, 68, 0.6); }


/* =========================================
   6. CHAT PANE (RIGHT SIDE)
   ========================================= */
.chat-pane {
    width: 360px; 
    flex: none; 
    background: #18181b;
    border-left: 1px solid #27272a;
    display: flex; flex-direction: column;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
    overflow: hidden;
    white-space: nowrap; 
    opacity: 1;
}

/* COLLAPSED STATE (Desktop) */
body.chat-hidden .chat-pane {
    width: 0px;
    opacity: 0;
    border-left: 0;
}

.chat-header {
    padding: 15px;
    background: #27272a;
    font-weight: 600; text-align: center; font-size: 0.9rem; letter-spacing: 0.5px;
    border-bottom: 1px solid #3f3f46;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    display: flex; flex-direction: column; gap: 10px;
}

.msg {
    padding: 10px 14px; border-radius: 12px; font-size: 0.9rem;
    max-width: 85%; line-height: 1.4; word-wrap: break-word; white-space: normal;
}
.msg.me { align-self: flex-end; background: #3b82f6; color: white; border-bottom-right-radius: 2px; }
.msg.peer { align-self: flex-start; background: #3f3f46; color: white; border-bottom-left-radius: 2px; }
.system-msg { align-self: center; font-size: 0.8rem; color: #a1a1aa; background: #27272a; padding: 4px 10px; border-radius: 8px; margin: 10px 0; }

.chat-input-area {
    padding: 15px;
    padding-bottom: max(15px, env(safe-area-inset-bottom));
    background: #18181b;
    border-top: 1px solid #27272a;
    display: flex; gap: 10px;
}
#msgInput {
    flex: 1; background: #27272a; border: 1px solid #3f3f46; color: white;
    padding: 12px; border-radius: 25px; font-size: 0.95rem;
    transition: border 0.2s;
}
#msgInput:focus { border-color: #3b82f6; }

#sendBtn {
    width: 45px; height: 45px; border-radius: 50%; border: none;
    background: #3b82f6; display: flex; align-items: center; justify-content: center;
    cursor: pointer; transition: background 0.2s; color: white;
}
#sendBtn:hover { background: #2563eb; }


/* =========================================
   7. MOBILE RESPONSIVE LAYOUT
   ========================================= */
@media (max-width: 768px) {
    .app-container { flex-direction: column; }
    
    .video-pane {
        flex: none; 
        height: 60%; 
        padding: 0;
        transition: height 0.4s ease; 
    }
    .video-wrapper {
        border-radius: 0; 
        max-height: none;
        box-shadow: none;
    }
    
    .video-controls { 
        width: 100%; 
        justify-content: center;
        bottom: 20px; 
    }
    .btn { padding: 12px 24px; font-size: 0.9rem; }
    
    .local-video-container { 
        width: 100px; 
        bottom: 90px; 
        right: 15px; 
    }

    .chat-pane {
        width: 100%;
        height: 40%; 
        border-left: none; 
        border-top: 1px solid #27272a;
        transition: height 0.4s ease, opacity 0.2s;
    }
    .chat-header { display: none; } 

    body.chat-hidden .chat-pane {
        width: 100%;
        height: 0; 
        opacity: 0;
        border-top: 0;
    }
    
    body.chat-hidden .video-pane {
        height: 100%;
    }
    
    body.chat-hidden .video-controls {
        bottom: max(30px, calc(15px + env(safe-area-inset-bottom)));
    }
    
    body.chat-hidden .local-video-container {
        bottom: max(100px, calc(85px + env(safe-area-inset-bottom)));
    }
}

/* =========================================
   8. TOAST NOTIFICATIONS
   ========================================= */
#toast-container {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000; 
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 90%;
    max-width: 400px;
    pointer-events: none; 
}

.toast {
    background: #27272a; 
    color: white;
    padding: 12px 20px;
    border-radius: 50px; 
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
    font-size: 0.9rem;
    font-weight: 500;
    display: flex; align-items: center; gap: 10px;
    border: 1px solid #3f3f46;
    animation: slideDownFade 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards;
    opacity: 0;
    transform: translateY(-20px);
    pointer-events: auto; 
}

.toast.error { border-color: #ef4444; color: #fca5a5; }
.toast.success { border-color: #2ecc71; color: #86efac; }
.toast.info { border-color: #3b82f6; color: #93c5fd; }
.toast .material-symbols-rounded { font-size: 20px; }

@keyframes slideDownFade {
    to { opacity: 1; transform: translateY(0); }
}
.toast.hide { animation: fadeOutUp 0.3s forwards; }
@keyframes fadeOutUp { to { opacity: 0; transform: translateY(-20px); } }