:root {
    --bg-primary: #0f0f0f;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #252525;
    --text-primary: #ffffff;
    --text-secondary: #888888;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --success: #22c55e;
    --danger: #ef4444;
    --border: #333333;
    --radius: 12px;
    --radius-sm: 8px;
}

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

html {
    height: 100%;
    height: -webkit-fill-available;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100%;
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow: hidden;
    overflow-x: hidden;
}

.screen {
    display: none;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.screen.active {
    display: flex;
}

.app {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

#chat-screen.video-view #chat-area {
    display: none;
}

#chat-screen.video-view .media-section {
    flex: 1;
    min-height: 0;
    display: flex;
    overflow: hidden;
    align-items: center;
    justify-content: center;
}

#chat-screen.video-view .video-wrapper {
    max-height: calc(100% - 1rem);
    aspect-ratio: 4/3;
}

#chat-screen.video-view .video-wrapper video {
    object-fit: contain;
}

#chat-screen.video-view .video-grid {
    grid-template-columns: repeat(auto-fit, minmax(min(25vw, 300px), 1fr));
    padding: 1rem;
    width: 100%;
    height: 100%;
}

#chat-screen.video-view .video-wrapper {
    max-width: none;
    width: 100%;
    height: 100%;
}

#chat-screen.chat-view #chat-area {
    flex: 1;
    min-height: 0;
}

#chat-screen.chat-view .media-section {
    display: none;
}

.chat-view {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

#connect-screen {
    justify-content: center;
    align-items: center;
    background: var(--bg-primary);
    padding: 1rem;
}

.connect-container {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2rem 1.5rem;
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.logo {
    display: flex;
    justify-content: center;
    margin-bottom: 1.5rem;
    color: var(--accent);
}

.logo svg {
    width: 56px;
    height: 56px;
}

.connect-container h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    background: linear-gradient(135deg, #fff 0%, var(--accent) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.tagline {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 2rem;
}

.input-group {
    display: flex;
    gap: 0.5rem;
}

input[type="text"], #message-input, textarea#message-input {
    flex: 1;
    padding: 0.5rem 1rem;
    background: var(--bg-tertiary);
    border: 2px solid transparent;
    color: var(--text-primary);
    font-size: 1rem;
    outline: none;
    transition: all 0.2s;
    box-sizing: border-box;
    font-family: inherit;
    border-radius: var(--radius-sm);
}

textarea#message-input {
    height: 32px !important;
    min-height: 32px !important;
    max-height: 32px !important;
    line-height: 32px;
    padding: 0 12px;
    overflow: hidden !important;
}

input[type="text"]:focus, #message-input:focus {
    border-color: var(--accent);
}

input[type="text"]::placeholder {
    color: var(--text-secondary);
}

.divider {
    display: flex;
    align-items: center;
    color: var(--text-secondary);
    margin: 1.5rem 0;
    font-size: 0.8rem;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

.divider span {
    padding: 0 1rem;
}

.btn {
    padding: 0.875rem 1.5rem;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn.primary {
    background: var(--accent);
    color: white;
}

.btn.primary:hover {
    background: #4f8ff7;
    box-shadow: 0 0 20px rgba(59, 130, 246, 0.6);
}

.btn.primary:active {
    transform: scale(0.96);
}

.btn.secondary {
    background: transparent;
    color: var(--text-primary);
    border: 2px solid var(--border);
    width: 100%;
    transition: all 0.2s;
}

.btn.secondary:hover {
    background: var(--bg-tertiary);
    border-color: var(--accent);
    box-shadow: 0 0 15px rgba(59, 130, 246, 0.3);
}

.btn.secondary:active {
    transform: scale(0.98);
}

.btn.small {
    padding: 4px 1rem;
    height: auto;
    border-radius: var(--radius-sm);
    box-sizing: border-box;
}

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

.room-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
}

.status-dot.connected {
    background: var(--success);
}

.view-toggle {
    display: flex;
    background: var(--bg-tertiary);
    border-radius: 10px;
    padding: 3px;
    gap: 2px;
}

.toggle-btn {
    padding: 0.4rem 1rem;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn:hover {
    color: var(--text-primary);
}

.toggle-btn.active {
    background: var(--accent);
    color: white;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

.icon-btn {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.5rem;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 30px;
    box-sizing: border-box;
}

.icon-btn:hover {
    background: var(--bg-primary);
    border-color: var(--accent);
}

.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.messages {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin-top: auto;
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
    font-size: 1rem;
}

.message {
    max-width: 100%;
    padding: 0.2rem 0;
    word-wrap: break-word;
    line-height: 1.4;
    overflow: hidden;
    font-size: 1rem;
}

.message img.image-preview {
    max-width: 120px;
    max-height: 90px;
    width: 120px;
    height: 90px;
    object-fit: cover;
}

.message .msg-name {
    font-weight: 700;
    color: var(--accent);
    margin-right: 0.5rem;
}

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

.message .msg-text .mention {
    color: var(--accent);
    font-weight: bold;
    background: rgba(99, 102, 241, 0.15);
    padding: 0 0.25rem;
    border-radius: 3px;
}

.message .msg-text pre {
    background: #2d2d2d;
    border-radius: 6px;
    padding: 0.75rem;
    margin: 0.5rem 0;
    overflow-x: auto;
    white-space: pre;
}

.message .msg-text code {
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
    font-size: 0.9em;
}

.message .meta {
    font-size: 0.8rem;
    opacity: 0.5;
    margin-left: 0.5rem;
}

.message.system {
    align-self: center;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-style: italic;
}

/* Local notices such as /help are multi-line and pre-aligned. */
.message.system .msg-text {
    white-space: pre-wrap;
    font-style: normal;
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
}

.message.received {
    align-self: flex-start;
}

.message.sent {
    align-self: flex-start;
}

.message.file {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    flex-wrap: wrap;
}

.message .file-check {
    color: #4caf50;
    font-weight: bold;
    margin-left: 0.5rem;
    font-size: 1rem;
}

.message.file:hover {
    opacity: 0.9;
}

.media-section {
    padding: 1rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: relative;
    flex: 1;
    min-height: 0;
}

.video-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(20vw, 250px), 1fr));
    gap: 0.75rem;
    padding: 0.5rem;
    flex: 1;
    align-items: center;
    justify-items: center;
    width: 100%;
    height: 100%;
}

.video-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 4/3;
    border-radius: 12px;
    overflow: hidden;
    background: var(--bg-primary);
    max-width: 500px;
}

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

.video-wrapper.local video {
    transform: scaleX(-1);
}

.video-label {
    position: absolute;
    bottom: 4px;
    left: 4px;
    font-size: 0.85rem;
    font-weight: 600;
    background: rgba(0,0,0,0.7);
    padding: 4px 8px;
    border-radius: 4px;
}

.call-controls {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 10;
}

.call-btn {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.call-btn:hover {
    background: var(--border);
    transform: scale(1.05);
}

.call-extras {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    opacity: 0;
    transition: opacity 0.2s;
}

.call-controls:hover .call-extras,
.call-extras:hover {
    opacity: 1;
}

.call-extras.hidden {
    display: none;
    height: 0;
}

.call-btn.active {
    background: var(--accent);
    color: white;
}

.call-btn.call-start {
    background: var(--success);
}

.call-btn.call-start:hover {
    background: #16a34a;
}

.call-btn.call-end {
    background: var(--danger);
}

.call-btn.call-end:hover {
    background: #dc2626;
}

.call-btn.hidden {
    display: none;
}

.chat-call-btn {
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    padding: 0 0.5rem;
    height: 30px;
}

.chat-call-btn:hover {
    border-color: var(--accent);
    background: var(--border);
}

.chat-call-btn.in-call {
    background: var(--danger);
    border-color: var(--danger);
    color: white;
}

.input-area {
    display: flex;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    align-items: center;
}

.mention-dropdown {
    position: absolute;
    bottom: 100%;
    left: 0;
    right: 0;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 8px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 100;
}

.mention-item {
    padding: 0.5rem 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.mention-item:hover,
.mention-item.selected {
    background: var(--accent);
    color: white;
}

.file-btn {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0 0.5rem;
    cursor: pointer;
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    transition: all 0.2s;
    height: 30px;
}

.file-btn:hover {
    border-color: var(--accent);
}

#send-btn {
    flex-shrink: 0;
}

.code-btn {
    height: 30px;
    padding: 0 0.5rem;
}

.hidden {
    display: none !important;
}

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

.message {
    animation: fadeIn 0.2s ease-out;
}

@media (max-width: 600px) and (max-height: 900px) and (pointer: coarse) {
    .connect-container {
        padding: 1.5rem 1rem;
        border-radius: 12px;
        max-width: calc(100% - 2rem);
        overflow: hidden;
    }
    
    .logo svg {
        width: 44px;
        height: 44px;
    }
    
    .connect-container h1 {
        font-size: 1.4rem;
    }
    
    .tagline {
        font-size: 0.8rem;
        margin-bottom: 1.5rem;
    }
    
    .input-group {
        flex-direction: column;
        gap: 0.75rem;
    }
    
    .btn {
        width: 100%;
        min-width: 0;
        padding: 0.9rem;
        white-space: normal;
        word-wrap: break-word;
    }
    
    #room-id-input, #create-id-input {
        width: 100%;
        font-size: 16px;
        min-width: 0;
    }
    
    .message {
        max-width: 95%;
        font-size: 0.85rem;
    }
    
    .chat-header {
        padding: 0.5rem 0.75rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    #chat-screen.video-view .chat-header {
        display: none;
    }
    
    #chat-screen.video-view .media-section {
        height: 100dvh;
        padding: 0;
        border: none;
    }
    
    .room-info {
        font-size: 0.85rem;
    }
    
    .view-toggle {
        display: none;
    }
    
    .view-toggle .toggle-btn {
        flex: 1;
        padding: 0.4rem;
        font-size: 0.8rem;
    }
    
    .input-area {
        padding: 0.5rem;
        gap: 0.4rem;
        flex-wrap: wrap;
    }
    
    #message-input {
        flex: 1;
        min-width: 100px;
        font-size: 16px;
        height: 32px !important;
        line-height: 32px;
        padding: 0 12px;
    }
    
    .file-btn {
        padding: 0.5rem 0.6rem;
        font-size: 1.1rem;
    }
    
    #send-btn {
        display: none;
    }
    
    .chat-header .icon-btn {
        padding: 0.4rem;
    }
    
    #video-grid,
    .video-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 2px;
    }
    
    .video-wrapper {
        flex: 1;
        width: 100%;
        min-height: 0;
        aspect-ratio: unset;
        max-width: unset;
        border-radius: 0;
    }
    
    .media-section {
        padding: 0;
        min-height: unset;
        flex: 1;
        min-height: 0;
        border-top: none;
    }
    
    .video-container {
        height: 100%;
        min-height: unset;
        border-radius: 0;
        display: flex;
        align-items: stretch;
        justify-content: stretch;
    }
    
    .video-container video {
        object-fit: cover;
        width: 100%;
        height: 100%;
    }
    
    .chat-header {
        padding: 0.5rem;
    }
    
    #chat-screen {
        height: 100dvh;
        padding: 0;
        margin: 0;
    }
    
    #chat-screen.video-view {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    .app {
        height: 100dvh;
        height: -webkit-fill-available;
    }
    
    .screen {
        height: 100%;
        min-height: 100dvh;
    }
    
    #connect-screen {
        height: 100dvh;
        height: -webkit-fill-available;
    }
}

.view-toggle-mobile {
    display: none;
    position: fixed;
    bottom: 60px;
    right: 15px;
    z-index: 1000;
    background: var(--accent);
    border: none;
    border-radius: 50%;
    width: 56px;
    height: 56px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    align-items: center;
    justify-content: center;
    color: white;
}

.lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 10000;
    justify-content: center;
    align-items: center;
    cursor: pointer;
}

.lightbox.active {
    display: flex;
}

.lightbox-img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
    border-radius: 8px;
}

.lightbox-close {
    position: absolute;
    top: 20px;
    right: 30px;
    font-size: 40px;
    color: #fff;
    cursor: pointer;
    line-height: 1;
}

.message.received.file img.image-preview,
.message.sent.file img.image-preview {
    border-radius: 4px;
    margin: 4px 0;
    cursor: pointer;
}

.message-input-wrapper {
    position: relative;
    flex: 1;
    display: flex;
}

.message-input-wrapper textarea {
    flex: 1;
    resize: none;
    height: 32px;
    min-height: 32px;
    box-sizing: border-box;
    line-height: 1.2;
    padding: 6px 12px;
    overflow: hidden;
}

#lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 10000;
    justify-content: center;
    align-items: center;
    cursor: pointer;
}

.qr-modal,
#code-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 10001;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.modal-close {
    position: absolute;
    top: 20px;
    right: 30px;
    font-size: 40px;
    color: #fff;
    cursor: pointer;
    line-height: 1;
}

.modal-content {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: 12px;
    text-align: center;
}

.code-modal-content {
    background: var(--bg-secondary);
    padding: 20px;
    border-radius: 12px;
    width: 90%;
    max-width: 600px;
}

.code-modal-content label {
    display: block;
    margin-bottom: 10px;
    color: var(--text-primary);
}

#code-input {
    width: 100%;
    height: 200px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    font-family: monospace;
    resize: vertical;
}

#send-code-btn {
    margin-top: 10px;
    padding: 10px 20px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
}

.qr-modal .modal-content {
    background: #fff;
    color: #333;
}

#qr-label {
    color: #333;
    font-weight: bold;
    word-break: break-all;
    padding: 0 10px;
}

#qrcode {
    margin-bottom: 15px;
}

.lightbox-img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
    border-radius: 8px;
}
