/* ==============================================================
   APPLE DESIGN SYSTEM & FLUID WEBRTC INTERFACE
   Principii fundamentale:
   1. Răspuns instant la pointer-down (fără latență)
   2. Translucență stratificată (Frosted Glass / Vibrancy)
   3. Tipografie optică cu tracking adaptiv și tabular-nums
   4. Animații fizice bazate pe arcuri amortizate critic
   5. Accesibilitate completă (reduced-motion, reduced-transparency)
   ============================================================== */

:root {
    --bg-primary: #000000;
    --bg-surface: rgba(28, 28, 35, 0.65);
    --bg-surface-elevated: rgba(38, 38, 48, 0.75);
    --bg-tile: #121217;
    
    --border-glass: rgba(255, 255, 255, 0.12);
    --border-light-catch: rgba(255, 255, 255, 0.25);
    --border-active: #30d158;
    
    --text-primary: #ffffff;
    --text-secondary: rgba(235, 235, 245, 0.6);
    --text-tertiary: rgba(235, 235, 245, 0.38);
    
    --accent-green: #30d158;
    --accent-green-glow: rgba(48, 209, 88, 0.35);
    --accent-red: #ff453a;
    --accent-red-glow: rgba(255, 69, 58, 0.3);
    --accent-amber: #ffd60a;
    
    --card-radius: 18px;
    --tile-radius: 16px;
    --glass-blur: blur(28px) saturate(190%);
    
    /* Critically damped spring simulation (0.35s response, zero bounce) */
    --spring-quick: all 0.22s cubic-bezier(0.16, 1, 0.3, 1);
    --spring-smooth: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
}

body, html {
    width: 100%;
    height: 100%;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Helvetica Neue", sans-serif;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
}

.app-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top, 12px));
    padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
    padding-left: max(12px, env(safe-area-inset-left, 12px));
    padding-right: max(12px, env(safe-area-inset-right, 12px));
    gap: 10px;
    box-sizing: border-box;
    overflow: hidden;
}

/* ==============================================================
   1. FLOATING TOP NAVIGATION (Glass Chrome)
   ============================================================== */
.top-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 18px;
    background: var(--bg-surface);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--border-glass);
    border-top: 1px solid var(--border-light-catch);
    border-radius: var(--card-radius);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
    z-index: 10;
    flex-shrink: 0;
    width: 100%;
}

.nav-left, .nav-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 8px;
}

.brand-icon {
    font-size: 19px;
    line-height: 1;
}

.brand-title {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.015em;
    color: var(--text-primary);
    white-space: nowrap;
}

.room-pill {
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(255, 255, 255, 0.08);
    padding: 4px 12px;
    border-radius: 9999px;
    font-size: 12px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
    white-space: nowrap;
}

.room-badge {
    font-weight: 600;
    color: var(--text-primary);
}

.room-divider {
    color: var(--text-tertiary);
    font-size: 10px;
}

.room-count {
    color: var(--accent-green);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    flex-shrink: 0;
    transition: background-color 0.3s ease, box-shadow 0.3s ease;
}

.status-dot.connected {
    background-color: var(--accent-green);
    box-shadow: 0 0 10px var(--accent-green-glow);
}

.status-dot.connecting {
    background-color: var(--accent-amber);
    box-shadow: 0 0 10px rgba(255, 214, 10, 0.5);
    animation: statusPulse 1.4s infinite alternate ease-in-out;
}

.status-dot.disconnected {
    background-color: var(--accent-red);
    box-shadow: 0 0 8px var(--accent-red-glow);
}

@keyframes statusPulse {
    from { opacity: 0.5; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1.1); }
}

/* Action Buttons with Spring Feedback on pointer-down */
.action-btn {
    padding: 6px 14px;
    border-radius: 9999px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: -0.01em;
    cursor: pointer;
    border: 1px solid transparent;
    transition: var(--spring-quick);
    white-space: nowrap;
}

.action-btn:active {
    transform: scale(0.94);
}

.action-btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    cursor: pointer;
    transition: var(--spring-quick);
}

.action-btn-icon:hover {
    background: rgba(255, 255, 255, 0.15);
}

.action-btn-icon:active {
    transform: scale(0.92);
    background: rgba(255, 255, 255, 0.2);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    border-color: rgba(255, 255, 255, 0.08);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.16);
}

.btn-danger {
    background: rgba(255, 69, 58, 0.18);
    color: var(--accent-red);
    border-color: rgba(255, 69, 58, 0.35);
}

.btn-danger:hover {
    background: rgba(255, 69, 58, 0.28);
}

/* ==============================================================
   2. VIDEO STAGE & DYNAMIC GRID
   ============================================================== */
.video-stage {
    flex: 1;
    min-height: 0;
    min-width: 0;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

/* Empty room alert banner (Apple Radar style) */
.empty-room-alert {
    position: absolute;
    top: 14px;
    display: flex;
    align-items: center;
    gap: 14px;
    background: rgba(22, 22, 28, 0.88);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--border-glass);
    border-top: 1px solid var(--border-light-catch);
    padding: 10px 18px;
    border-radius: var(--tile-radius);
    max-width: 90%;
    z-index: 5;
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

.radar-beacon {
    position: relative;
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.beacon-circle {
    width: 8px;
    height: 8px;
    background-color: var(--accent-green);
    border-radius: 50%;
    z-index: 2;
    box-shadow: 0 0 8px var(--accent-green-glow);
}

.beacon-wave {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 1.5px solid var(--accent-green);
    opacity: 0;
    animation: beaconPulse 2s infinite cubic-bezier(0.2, 0.6, 0.4, 1);
}

.beacon-wave.delay {
    animation-delay: 0.9s;
}

@keyframes beaconPulse {
    0% { transform: scale(0.4); opacity: 0.9; }
    100% { transform: scale(2.2); opacity: 0; }
}

.alert-content h3 {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: -0.01em;
    margin-bottom: 2px;
}

.alert-content p {
    font-size: 11px;
    color: var(--text-secondary);
    line-height: 1.35;
}

/* Video Grid Layouts */
.video-grid {
    display: grid;
    gap: 10px;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    justify-content: center;
    align-content: center;
    transition: var(--spring-smooth);
}

.video-grid[data-tiles="1"] {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    max-width: 720px;
    max-height: 540px;
    aspect-ratio: 4 / 3;
}

.video-grid[data-tiles="2"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: 1fr;
    max-width: 1040px;
}

.video-grid[data-tiles="3"],
.video-grid[data-tiles="4"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    max-width: 980px;
}

.video-grid[data-tiles="5"],
.video-grid[data-tiles="6"] {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    max-width: 1160px;
}

.video-grid[data-tiles="7"],
.video-grid[data-tiles="8"] {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    max-width: 1280px;
}

/* Video Tile Craft */
.video-tile {
    position: relative;
    background-color: var(--bg-tile);
    border-radius: var(--tile-radius);
    overflow: hidden;
    border: 1.5px solid var(--border-glass);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    transition: border-color 0.25s ease, box-shadow 0.25s ease, transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 0;
    width: 100%;
    height: 100%;
    animation: tileEnter 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes tileEnter {
    from { opacity: 0; transform: scale(0.92); }
    to { opacity: 1; transform: scale(1); }
}

.video-tile video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
    display: block;
}

#localVideo {
    transform: scaleX(-1);
}

/* Speaking State (Glowing Emerald Ring with spring expansion) */
.video-tile.speaking {
    border-color: var(--accent-green);
    box-shadow: 0 0 24px var(--accent-green-glow), inset 0 0 12px rgba(48, 209, 88, 0.2);
}

/* Video Labels with Frosted Glass */
.video-label {
    position: absolute;
    bottom: 10px;
    left: 10px;
    display: flex;
    align-items: baseline;
    gap: 4px;
    background: rgba(10, 10, 14, 0.75);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    padding: 4px 10px;
    border-radius: 8px;
    font-size: 11px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    pointer-events: none;
    max-width: calc(100% - 20px);
}

.video-label .user-name {
    font-weight: 600;
    color: var(--text-primary);
}

.video-label .user-sub {
    font-size: 10px;
    color: var(--text-secondary);
}

/* Speaking Indicator & Live Sound Wave */
.speaking-indicator {
    display: none;
    position: absolute;
    top: 10px;
    right: 10px;
    align-items: center;
    gap: 6px;
    background: rgba(10, 10, 14, 0.75);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid var(--accent-green);
    padding: 4px 9px;
    border-radius: 8px;
    font-size: 11px;
    font-weight: 600;
    color: var(--accent-green);
    box-shadow: 0 0 12px var(--accent-green-glow);
}

.video-tile.speaking .speaking-indicator {
    display: flex;
}

.sound-wave {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    height: 12px;
}

.sound-wave span {
    display: inline-block;
    width: 2.5px;
    background-color: var(--accent-green);
    border-radius: 9999px;
    animation: waveBounce 0.65s infinite alternate ease-in-out;
}

.sound-wave span:nth-child(1) { height: 5px; animation-delay: 0.1s; }
.sound-wave span:nth-child(2) { height: 11px; animation-delay: 0.3s; }
.sound-wave span:nth-child(3) { height: 8px; animation-delay: 0.2s; }
.sound-wave span:nth-child(4) { height: 6px; animation-delay: 0.4s; }

@keyframes waveBounce {
    0% { transform: scaleY(0.35); }
    100% { transform: scaleY(1); }
}

/* ==============================================================
   3. BOTTOM TOOLBAR & PUSH-TO-TALK BUTTON
   ============================================================== */
.bottom-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px 0 2px 0;
    z-index: 10;
    flex-shrink: 0;
    width: 100%;
}

.controls-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: 100%;
    max-width: 460px;
}

.ptt-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    width: 100%;
    max-width: 380px;
    background: var(--bg-surface-elevated);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1.5px solid var(--border-glass);
    border-top: 1.5px solid var(--border-light-catch);
    padding: 11px 22px;
    border-radius: 9999px;
    color: var(--text-primary);
    cursor: pointer;
    box-shadow: 0 10px 32px rgba(0, 0, 0, 0.45);
    touch-action: none;
    transition: var(--spring-quick);
}

.ptt-button:hover {
    background: rgba(50, 50, 62, 0.9);
    border-color: rgba(255, 255, 255, 0.25);
}

.ptt-button:active,
.ptt-button.active {
    background: rgba(48, 209, 88, 0.22);
    border-color: var(--accent-green);
    box-shadow: 0 0 32px var(--accent-green-glow);
    transform: scale(0.96);
}

.ptt-icon-wrapper {
    position: relative;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.mic-svg {
    transition: transform 0.2s ease, opacity 0.2s ease;
    stroke: var(--text-primary);
}

.ptt-live-wave {
    display: none;
    position: absolute;
}

.ptt-button.active .mic-svg {
    stroke: var(--accent-green);
    transform: scale(1.1);
}

.ptt-button.active .ptt-live-wave {
    display: inline-flex;
}

.ptt-texts {
    display: flex;
    flex-direction: column;
    text-align: left;
}

.ptt-main-text {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.015em;
    color: var(--text-primary);
    white-space: nowrap;
}

.ptt-hint-text {
    font-size: 11px;
    color: var(--text-secondary);
    transition: color 0.15s ease;
    white-space: nowrap;
}

.ptt-hint-text.speaking {
    color: var(--accent-green);
    font-weight: 600;
}

/* ==============================================================
   4. MOBILE RESPONSIVE ADAPTATIONS (Screens <= 640px)
   ============================================================== */
@media (max-width: 640px) {
    .app-container {
        padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 8px)) 8px;
        gap: 6px;
    }

    .top-nav {
        padding: 7px 12px;
        border-radius: 14px;
    }

    .nav-left, .nav-right {
        gap: 8px;
    }

    .brand-title {
        display: none;
    }

    .brand-icon {
        font-size: 20px;
    }

    .room-pill {
        padding: 4px 9px;
        font-size: 11px;
        gap: 4px;
    }

    .connection-status #connectionStatusText {
        display: none;
    }

    .action-btn {
        padding: 6px 12px;
        font-size: 11px;
    }

    .action-btn-icon {
        width: 28px;
        height: 28px;
    }

    /* Video Grid vertical responsiveness */
    .video-grid {
        gap: 6px;
    }

    .video-grid[data-tiles="1"] {
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: auto;
    }

    .video-grid[data-tiles="2"] {
        grid-template-columns: 1fr;
        grid-template-rows: repeat(2, minmax(0, 1fr));
    }

    .video-grid[data-tiles="3"],
    .video-grid[data-tiles="4"] {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-rows: repeat(2, minmax(0, 1fr));
    }

    .video-grid[data-tiles="5"],
    .video-grid[data-tiles="6"] {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-rows: repeat(3, minmax(0, 1fr));
    }

    .video-grid[data-tiles="7"],
    .video-grid[data-tiles="8"] {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-rows: repeat(4, minmax(0, 1fr));
    }

    .ptt-button {
        padding: 10px 18px;
        max-width: 100%;
    }

    .ptt-main-text {
        font-size: 13px;
    }

    .ptt-hint-text {
        font-size: 10px;
    }
}

/* ==============================================================
   5. ACCESSIBILITY OVERRIDES (Apple Design Benchmark)
   ============================================================== */
@media (prefers-reduced-motion: reduce) {
    *, ::before, ::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    .video-tile {
        animation: none !important;
    }
}

@media (prefers-reduced-transparency: reduce) {
    .top-nav, .bottom-bar .ptt-button, .empty-room-alert, .video-label {
        backdrop-filter: none !important;
        -webkit-backdrop-filter: none !important;
        background: #18181f !important;
    }
}

@media (prefers-contrast: more) {
    :root {
        --border-glass: rgba(255, 255, 255, 0.4);
        --border-light-catch: #ffffff;
    }
    .top-nav, .ptt-button, .video-tile {
        border-width: 2px !important;
    }
}
