/* ========================================
   CSS Custom Properties
   ======================================== */
:root {
    --color-bg: #0f0f1a;
    --color-surface: #1a1a2e;
    --color-surface-alt: #16213e;
    --color-border: #2a2a4a;
    --color-text: #e0e0e0;
    --color-text-muted: #8888aa;
    --color-primary: #6c63ff;
    --color-primary-hover: #5a52e0;
    --color-success: #2ecc71;
    --color-danger: #e74c3c;
    --color-accent: #00d2ff;
    --color-chat-bg: #12121f;
    --color-message-bg: #1e1e35;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 40px rgba(0, 0, 0, 0.4);
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    --header-height: 52px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

/* ========================================
   Reset & Base
   ======================================== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    background-color: var(--color-bg);
    color: var(--color-text);
    height: 100dvh;
    height: 100vh; /* fallback */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.hidden {
    display: none !important;
}

/* ========================================
   Login Screen
   ======================================== */
#login-screen {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    padding: 20px;
    background: linear-gradient(135deg, var(--color-bg) 0%, var(--color-surface-alt) 100%);
}

#login-form {
    background: var(--color-surface);
    padding: 40px 32px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    text-align: center;
    width: 100%;
    max-width: 380px;
    border: 1px solid var(--color-border);
}

.login-logo {
    color: var(--color-primary);
    margin-bottom: 16px;
}

#login-form h2 {
    color: var(--color-text);
    margin-bottom: 4px;
    font-size: 1.5rem;
    font-weight: 600;
}

.login-subtitle {
    color: var(--color-text-muted);
    font-size: 0.875rem;
    margin-bottom: 24px;
}

#login-form input {
    padding: 12px 16px;
    width: 100%;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: 1rem;
    background: var(--color-bg);
    color: var(--color-text);
    outline: none;
    transition: border-color 0.2s;
}

#login-form input:focus {
    border-color: var(--color-primary);
}

#login-form input::placeholder {
    color: var(--color-text-muted);
}

#login-form button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px 24px;
    margin-top: 16px;
    border: none;
    border-radius: var(--radius-md);
    background: var(--color-primary);
    color: white;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    min-height: 48px;
    -webkit-tap-highlight-color: transparent;
}

#login-form button:hover {
    background: var(--color-primary-hover);
}

#login-form button:active {
    transform: scale(0.98);
}

/* ========================================
   Header
   ======================================== */
#app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 16px;
    padding-top: var(--safe-top);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--color-primary);
}

.header-title {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--color-text);
}

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

#user-display {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    background: var(--color-bg);
    padding: 4px 12px;
    border-radius: var(--radius-xl);
    border: 1px solid var(--color-border);
}

/* ========================================
   Chat Screen Layout
   ======================================== */
#chat-screen {
    display: flex;
    flex-direction: column;
    height: 100%;
}

#main-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

/* ========================================
   Video Grid
   ======================================== */
#video-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 8px;
    padding: 8px;
    background: var(--color-bg);
    overflow-y: auto;
    flex: 1;
    min-height: 0;
    align-content: start;
}

.video-container {
    position: relative;
    aspect-ratio: 4 / 3;
    background: var(--color-surface);
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid var(--color-border);
}

video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.video-label {
    position: absolute;
    bottom: 8px;
    left: 8px;
    color: white;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    padding: 4px 10px;
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: var(--radius-sm);
}

/* ========================================
   Chat Panel
   ======================================== */
#chat-panel {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}

#chat-toggle {
    display: none;
}

#chat-container {
    display: flex;
    flex-direction: column;
    height: 220px;
    padding: 0;
    background: var(--color-surface);
}

/* Messages */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 10px 12px;
    scroll-behavior: smooth;
    background: var(--color-chat-bg);
}

.message {
    padding: 6px 10px;
    margin-bottom: 4px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    line-height: 1.4;
    animation: fadeIn 0.2s ease;
}

.message strong {
    color: var(--color-accent);
    font-weight: 600;
}

.message.system-message {
    color: var(--color-text-muted);
    font-style: italic;
    font-size: 0.8rem;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Chat Form */
#chat-form {
    display: flex;
    gap: 8px;
    padding: 8px 12px;
    border-top: 1px solid var(--color-border);
}

#message-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    background: var(--color-bg);
    color: var(--color-text);
    outline: none;
    transition: border-color 0.2s;
    min-height: 44px;
}

#message-input:focus {
    border-color: var(--color-primary);
}

#message-input::placeholder {
    color: var(--color-text-muted);
}

#chat-form button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: var(--radius-md);
    background: var(--color-primary);
    color: white;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

#chat-form button:hover {
    background: var(--color-primary-hover);
}

#chat-form button:active {
    transform: scale(0.93);
}

/* Controls */
#controls {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 8px 12px;
    padding-bottom: calc(8px + var(--safe-bottom));
    border-top: 1px solid var(--color-border);
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
}

.control-group label {
    display: flex;
    align-items: center;
    color: var(--color-text-muted);
    flex-shrink: 0;
}

.control-group select {
    flex: 1;
    min-width: 0;
    padding: 8px 10px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
    font-size: 0.8rem;
    outline: none;
    min-height: 40px;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

.control-group select:focus {
    border-color: var(--color-primary);
}

#toggle-webcam {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    border: none;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    min-height: 40px;
    white-space: nowrap;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

#toggle-webcam {
    background-color: var(--color-success);
}

#toggle-webcam.active {
    background-color: var(--color-danger);
}

#toggle-webcam:active {
    transform: scale(0.95);
}

.webcam-icon-off {
    display: none;
}

#toggle-webcam.active .webcam-icon-on {
    display: none;
}

#toggle-webcam.active .webcam-icon-off {
    display: inline;
}

/* ========================================
   Scrollbar Styling
   ======================================== */
#messages::-webkit-scrollbar,
#video-grid::-webkit-scrollbar {
    width: 6px;
}

#messages::-webkit-scrollbar-track,
#video-grid::-webkit-scrollbar-track {
    background: transparent;
}

#messages::-webkit-scrollbar-thumb,
#video-grid::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}

/* ========================================
   Mobile Responsive (< 768px)
   ======================================== */
@media (max-width: 768px) {
    #video-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 6px;
        padding: 6px;
    }

    #chat-toggle {
        display: flex;
        align-items: center;
        gap: 8px;
        width: 100%;
        padding: 10px 14px;
        border: none;
        background: var(--color-surface-alt);
        color: var(--color-text);
        font-size: 0.9rem;
        font-weight: 500;
        cursor: pointer;
        border-top: 1px solid var(--color-border);
        -webkit-tap-highlight-color: transparent;
    }

    .chat-toggle-arrow {
        margin-left: auto;
        transition: transform 0.3s ease;
    }

    #chat-panel.collapsed .chat-toggle-arrow {
        transform: rotate(180deg);
    }

    #chat-panel.collapsed #chat-container {
        display: none;
    }

    #chat-container {
        height: 240px;
    }

    #controls {
        flex-direction: column;
        align-items: stretch;
    }

    .control-group {
        flex: none;
    }

    #toggle-webcam {
        width: 100%;
        justify-content: center;
    }

    #webcam-label {
        display: inline;
    }
}

/* ========================================
   Small Mobile (< 480px)
   ======================================== */
@media (max-width: 480px) {
    #video-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 4px;
        padding: 4px;
    }

    .video-label {
        font-size: 0.65rem;
        padding: 2px 6px;
        bottom: 4px;
        left: 4px;
    }

    #login-form {
        padding: 32px 24px;
    }

    #login-form h2 {
        font-size: 1.3rem;
    }
}

/* ========================================
   Landscape mobile
   ======================================== */
@media (max-height: 500px) and (orientation: landscape) {
    #main-content {
        flex-direction: row;
    }

    #video-grid {
        flex: 1;
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    }

    #chat-panel {
        width: 280px;
        border-top: none;
        border-left: 1px solid var(--color-border);
    }

    #chat-toggle {
        display: none;
    }

    #chat-container {
        height: 100%;
        flex: 1;
    }

    #chat-panel.collapsed #chat-container {
        display: flex;
    }

    #controls {
        flex-direction: column;
        align-items: stretch;
    }
}

/* ========================================
   Desktop (> 1024px) - side-by-side layout
   ======================================== */
@media (min-width: 1024px) {
    #main-content {
        flex-direction: row;
    }

    #video-grid {
        flex: 1;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    }

    #chat-panel {
        width: 340px;
        border-top: none;
        border-left: 1px solid var(--color-border);
    }

    #chat-container {
        height: 100%;
        flex: 1;
    }
}
