* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    --bg-tertiary: #e9ecef;
    --text-primary: #212529;
    --text-secondary: #6c757d;
    --text-muted: #adb5bd;
    --border-color: #dee2e6;
    --accent-color: #0d6efd;
    --success-color: #198754;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
    --shadow-lg: 0 1rem 3rem rgba(0, 0, 0, 0.175);
}

[data-theme="dark"] {
    --bg-primary: #212529;
    --bg-secondary: #2d3338;
    --bg-tertiary: #343a40;
    --text-primary: #f8f9fa;
    --text-secondary: #adb5bd;
    --text-muted: #6c757d;
    --border-color: #495057;
    --accent-color: #0d6efd;
    --success-color: #198754;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 1rem 3rem rgba(0, 0, 0, 0.5);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Utility Classes */
.hidden {
    display: none !important;
}

/* Username Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2rem;
    text-align: center;
    max-width: 420px;
    width: 90%;
    box-shadow: var(--shadow-lg);
    animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
    from {
        transform: translateY(-20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header h2 {
    color: var(--text-primary);
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.modal-header p {
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
    font-size: 0.95rem;
}

.input-group {
    position: relative;
    margin-bottom: 1.5rem;
}

.input-group input {
    width: 100%;
    padding: 0.875rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-secondary);
    font-size: 1rem;
    color: var(--text-primary);
    transition: all 0.2s ease;
    outline: none;
}

.input-group input:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.1);
    background: var(--bg-primary);
}

.input-group input::placeholder {
    color: var(--text-muted);
}

#joinButton {
    width: 100%;
    padding: 0.875rem 1rem;
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

#joinButton:hover {
    background: #0b5ed7;
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

/* Chat Interface */
#chatInterface {
    height: 100vh;
    display: flex;
    flex-direction: column;
    margin: 10px;
    height: calc(100vh - 20px);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    overflow: hidden;
    background: var(--bg-primary);
}

/* Header */
.chat-header {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.header-left h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0;
}

#participantCount {
    color: var(--text-secondary);
    font-size: 0.9rem;
    background: var(--bg-primary);
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    border: 1px solid var(--border-color);
}

.header-btn {
    padding: 0.5rem 1rem;
    background: var(--accent-color);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    color: white;
    font-size: 0.875rem;
    font-weight: 500;
    transition: all 0.2s ease;
}

.header-btn:hover {
    background: #0b5ed7;
    transform: translateY(-1px);
}

/* Main Chat Area */
.chat-main {
    flex: 1;
    position: relative;
    padding: 1rem;
    overflow: hidden;
    background: var(--bg-primary);
}

/* Video Grid */
.video-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    height: 100%;
    justify-content: flex-start;
    align-items: flex-start;
    padding: 1rem;
}

.remoteVideoContainer {
    position: relative;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
    min-width: 200px;
    aspect-ratio: 16 / 9;
    box-shadow: var(--shadow);
    transition: all 0.2s ease;
}

.remoteVideoContainer:hover {
    box-shadow: var(--shadow-lg);
    transform: translateY(-2px);
}

.remoteVideo {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.remoteUsername {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    backdrop-filter: blur(10px);
}

/* Responsive grid sizing */
.remoteVideoContainer:only-child {
    width: min(70vw, 800px);
}

.video-grid:has(.remoteVideoContainer:nth-child(2)) .remoteVideoContainer {
    width: min(45vw, 500px);
}

.video-grid:has(.remoteVideoContainer:nth-child(3)) .remoteVideoContainer {
    width: min(30vw, 350px);
}

.video-grid:has(.remoteVideoContainer:nth-child(4)) .remoteVideoContainer {
    width: min(22vw, 280px);
}

/* Self View */
.self-view {
    position: fixed;
    bottom: 4rem;
    right: 1rem;
    width: 200px;
    aspect-ratio: 16 / 9;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    z-index: 200;
}

.self-view:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

#selfVideo {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1); /* Mirror effect */
}

.self-view-label {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.25rem;
    font-size: 0.75rem;
    font-weight: 500;
    text-align: center;
    backdrop-filter: blur(10px);
}

/* Chat Controls */
.chat-controls {
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    padding: 1rem;
    display: flex;
    justify-content: center;
    gap: 1rem;
}

.control-btn {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    border: 1px solid var(--border-color);
    background: var(--bg-primary);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--text-primary);
    font-size: 0.875rem;
    font-weight: 500;
}

.control-btn:hover {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
    transform: translateY(-1px);
    box-shadow: var(--shadow);
}

.control-btn span {
    font-size: 0.875rem;
}

.mute-btn.muted {
    background: #dc3545;
    color: white;
    border-color: #dc3545;
}

.mute-btn.muted:hover {
    background: #c82333;
    transform: translateY(-1px);
}

.video-btn.disabled {
    background: #dc3545;
    color: white;
    border-color: #dc3545;
}

.video-btn.disabled:hover {
    background: #c82333;
    transform: translateY(-1px);
}

.hangup-btn {
    background: #dc3545;
    color: white;
    border-color: #dc3545;
}

.hangup-btn:hover {
    background: #c82333;
    transform: translateY(-1px);
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .chat-header {
        padding: 1rem;
    }
    
    .header-left h1 {
        font-size: 1.2rem;
    }
    
    .chat-main {
        padding: 0.5rem;
    }
    
    .remoteVideoContainer:only-child {
        width: 90vw;
    }
    
    .video-grid:has(.remoteVideoContainer:nth-child(2)) .remoteVideoContainer {
        width: 45vw;
    }
    
    .video-grid:has(.remoteVideoContainer:nth-child(3)) .remoteVideoContainer {
        width: 45vw;
    }
    
    .video-grid:has(.remoteVideoContainer:nth-child(4)) .remoteVideoContainer {
        width: 45vw;
    }
    
    .self-view {
        width: 120px;
        bottom: 6rem;
    }
    
    .chat-controls {
        gap: 0.5rem;
        padding: 0.75rem;
    }
    
    .control-btn {
        padding: 0.75rem;
        min-width: 60px;
    }
    
    .control-btn span {
        font-size: 0.8rem;
    }
}

/* Connection Quality Indicator */
.connection-quality {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.8rem;
    margin-left: 1rem;
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
}

.quality-indicator {
    font-size: 0.7rem;
    animation: pulse 2s infinite;
}

.quality-indicator.good {
    color: var(--success-color);
}

.quality-indicator.fair {
    color: var(--warning-color);
}

.quality-indicator.poor {
    color: var(--danger-color);
    animation: pulse 1s infinite;
}

.quality-text {
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* IRC-style Chat Panel */
.main-content {
    display: flex;
    height: 100%;
    gap: 0.5rem;
}

.video-grid {
    flex: 1;
    min-width: 0; /* Allow flexbox to shrink */
}

.chat-panel {
    width: 320px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    display: flex;
    flex-direction: column;
    font-family: 'Courier New', 'Monaco', monospace;
    font-size: 0.85rem;
    transition: width 0.3s ease;
}

.chat-panel.collapsed {
    width: 40px;
    overflow: hidden;
}

.chat-header-bar {
    background: var(--bg-tertiary);
    padding: 0.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 0.5rem 0.5rem 0 0;
}

.chat-title {
    font-weight: bold;
    color: var(--accent-color);
    white-space: nowrap;
}

.chat-toggle-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 0.25rem;
    font-size: 1rem;
    transition: background-color 0.2s ease;
}

.chat-toggle-btn:hover {
    background: var(--bg-primary);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    background: var(--bg-primary);
    line-height: 1.4;
    max-height: 300px;
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.message {
    margin-bottom: 0.25rem;
    word-wrap: break-word;
}

.message-timestamp {
    color: var(--text-muted);
    font-size: 0.75rem;
    margin-right: 0.5rem;
}

.message-username {
    color: var(--accent-color);
    font-weight: bold;
    margin-right: 0.5rem;
}

.message-text {
    color: var(--text-primary);
}

.system-message {
    color: var(--text-muted);
    font-style: italic;
    margin-bottom: 0.25rem;
}

.join-message {
    color: var(--success-color);
}

.leave-message {
    color: var(--danger-color);
}

.own-message .message-username {
    color: var(--success-color);
}

.action-message {
    font-style: italic;
}

.action-message .message-text {
    color: var(--text-secondary);
}

.chat-input-container {
    padding: 0.5rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-tertiary);
    border-radius: 0 0 0.5rem 0.5rem;
}

.chat-prompt {
    color: var(--accent-color);
    font-weight: bold;
    font-size: 1rem;
}

.chat-input {
    flex: 1;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 0.25rem;
    padding: 0.375rem 0.5rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.85rem;
}

.chat-input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(13, 110, 253, 0.25);
}

.send-button {
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 0.25rem;
    padding: 0.375rem 0.75rem;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    transition: background-color 0.2s ease;
}

.send-button:hover {
    background: #0056b3;
}

.send-button:disabled {
    background: var(--text-muted);
    cursor: not-allowed;
}

/* Mobile-specific styles */
.mobile-only {
    display: none;
}

.mobile-chat-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
}

.mobile-chat-overlay.hidden {
    transform: translateY(100%);
}

.mobile-chat-header {
    background: var(--bg-tertiary);
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 10;
}

.mobile-chat-header h3 {
    color: var(--accent-color);
    font-family: 'Courier New', 'Monaco', monospace;
    font-size: 1.2rem;
    margin: 0;
}

.close-chat-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.25rem;
    transition: background-color 0.2s ease;
}

.close-chat-btn:hover {
    background: var(--bg-secondary);
}

.mobile-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    font-family: 'Courier New', 'Monaco', monospace;
    font-size: 0.9rem;
    line-height: 1.4;
    -webkit-overflow-scrolling: touch;
}

.mobile-chat-input-container {
    padding: 1rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 0.5rem;
    background: var(--bg-secondary);
    position: sticky;
    bottom: 0;
}

.mobile-chat-input {
    flex: 1;
    background: var(--bg-primary);
    border: 2px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.75rem;
    color: var(--text-primary);
    font-family: 'Courier New', 'Monaco', monospace;
    font-size: 1rem;
    min-height: 44px; /* Touch-friendly size */
}

.mobile-chat-input:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.25);
}

.mobile-send-button {
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: 0.5rem;
    padding: 0.75rem 1.5rem;
    cursor: pointer;
    font-weight: 600;
    min-width: 80px;
    min-height: 44px; /* Touch-friendly size */
    transition: background-color 0.2s ease;
}

.mobile-send-button:hover {
    background: #0056b3;
}

.mobile-send-button:active {
    transform: scale(0.98);
}

/* Enhanced mobile responsive design */
@media (max-width: 768px) {
    /* Hide desktop chat panel on mobile */
    .chat-panel {
        display: none;
    }
    
    /* Show mobile-only elements */
    .mobile-only {
        display: flex;
    }
    
    /* Adjust main content for full-screen video */
    .main-content {
        flex-direction: column;
        height: 100%;
    }
    
    .video-grid {
        flex: 1;
        width: 100%;
        padding: 0.5rem;
    }
    
    /* Mobile-optimized video containers */
    .remoteVideoContainer {
        border-radius: 0.75rem;
        margin-bottom: 0.5rem;
    }
    
    .remoteVideo {
        border-radius: 0.75rem;
    }
    
    /* Larger touch targets for controls */
    .control-btn {
        min-width: 60px;
        min-height: 60px;
        padding: 0.75rem;
        border-radius: 50%;
        flex-direction: column;
        gap: 0.25rem;
        font-size: 0.75rem;
    }
    
    .btn-icon {
        font-size: 1.5rem;
        line-height: 1;
    }
    
    .btn-text {
        font-size: 0.7rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }
    
    /* Mobile chat controls styling */
    .chat-controls {
        padding: 1rem;
        gap: 1rem;
        background: var(--bg-secondary);
        border-top: 1px solid var(--border-color);
    }
    
    /* Optimize header for mobile */
    .chat-header {
        padding: 0.75rem 1rem;
        flex-wrap: wrap;
    }
    
    .header-left {
        flex: 1;
        min-width: 0;
    }
    
    .header-left h1 {
        font-size: 1.2rem;
    }
    
    .connection-quality {
        margin-left: 0.5rem;
        font-size: 0.7rem;
        padding: 0.2rem 0.4rem;
    }
    
    /* Mobile self-view positioning */
    .self-view {
        width: 100px;
        height: 75px;
        bottom: 7rem;
        right: 1rem;
    }
    
    /* Mobile username modal */
    .modal-content {
        margin: 2rem 1rem;
        padding: 2rem 1.5rem;
        border-radius: 1rem;
    }
    
    .input-group input {
        padding: 0.75rem;
        font-size: 1rem;
        border-radius: 0.5rem;
    }
    
    #joinButton {
        padding: 0.75rem 2rem;
        font-size: 1rem;
        border-radius: 0.5rem;
    }
    
    /* Improve mobile message display */
    .message {
        padding: 0.25rem 0;
        line-height: 1.5;
    }
    
    .message-timestamp {
        font-size: 0.7rem;
    }
    
    .message-username {
        font-size: 0.85rem;
    }
    
    .message-text {
        font-size: 0.9rem;
        word-break: break-word;
    }
}

/* Extra small devices (phones in portrait) */
@media (max-width: 480px) {
    .chat-controls {
        gap: 0.75rem;
        padding: 0.75rem;
    }
    
    .control-btn {
        min-width: 50px;
        min-height: 50px;
        padding: 0.5rem;
    }
    
    .btn-icon {
        font-size: 1.2rem;
    }
    
    .btn-text {
        font-size: 0.6rem;
    }
    
    .modal-content {
        margin: 1rem 0.5rem;
        padding: 1.5rem 1rem;
    }
}

/* Landscape orientation optimizations */
@media (max-width: 896px) and (orientation: landscape) {
    .chat-header {
        padding: 0.5rem 1rem;
    }
    
    .chat-controls {
        padding: 0.75rem 1rem;
    }
    
    .control-btn {
        min-height: 50px;
    }
    
    .self-view {
        bottom: 5.5rem;
        width: 80px;
        height: 60px;
    }
}

/* Touch-friendly interactions */
@media (hover: none) and (pointer: coarse) {
    .control-btn:active {
        transform: scale(0.95);
        background: var(--bg-tertiary);
    }
    
    .header-btn:active {
        transform: scale(0.95);
    }
    
    .chat-toggle-btn:active {
        transform: scale(0.95);
    }
}