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

:root {
    --bg:          #101116;
    --bg-raised:   #16171e;
    --bg-sidebar:  #0c0d11;
    --surface:     #1d1e26;
    --surface-2:   #262835;
    --border:      #2a2c38;
    --text:        #e7e8ee;
    --text-muted:  #8a8c9c;
    --accent:      #5b8cff;
    --accent-dim:  #3d5bb3;
    --live:        #34d399;
    --danger:      #f0546b;
    --radius:      8px;
    --sidebar-w:   232px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    height: 100dvh; /* iOS Safari's address bar resizes the viewport; 100vh alone clips content under it */
    overflow: hidden;
    font-size: 14px;
    -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

/* ── LOADING SCREEN ──────────────────────────────────────────── */
#loading-screen {
    position: fixed; inset: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 20px;
    background: var(--bg);
}
.loading-mark {
    width: 20px; height: 20px; border-radius: 50%;
    background: var(--live);
    box-shadow: 0 0 20px 4px rgba(52,211,153,0.35);
}
.spinner {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    animation: spin 0.8s linear infinite;
}
.spinner-lg { width: 34px; height: 34px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 13px; color: var(--text-muted); }

/* ── AUTH SCREEN ─────────────────────────────────────────────── */
#auth-screen {
    position: fixed; inset: 0;
    display: flex; align-items: center; justify-content: center;
    background:
        radial-gradient(circle at 20% 20%, rgba(91,140,255,0.08), transparent 45%),
        var(--bg);
}

.auth-box {
    width: min(360px, calc(100vw - 32px));
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 28px;
}

.brand {
    display: flex; align-items: center; gap: 8px;
    margin-bottom: 22px;
}
.brand-dot {
    width: 9px; height: 9px; border-radius: 50%;
    background: var(--live);
    box-shadow: 0 0 8px var(--live);
}
.brand-name { font-weight: 600; font-size: 15px; letter-spacing: .01em; }

.field { margin-bottom: 14px; }
.field label {
    display: block; font-size: 11px; font-weight: 600; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: .06em; margin-bottom: 6px;
}
.field input {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    padding: 9px 10px;
    font-size: 16px; /* 16px is the iOS Safari threshold below which focusing an input auto-zooms the page */
    outline: none;
}
.field input:focus { border-color: var(--accent); }

.auth-tabs {
    display: flex; gap: 4px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 3px;
    margin-bottom: 16px;
}
.auth-tab {
    flex: 1;
    padding: 7px;
    border: none;
    background: transparent;
    border-radius: 6px;
    color: var(--text-muted);
    font-size: 13px; font-weight: 500;
    cursor: pointer;
}
.auth-tab.active { background: var(--surface-2); color: var(--text); }

.remember-row {
    display: flex; align-items: center; gap: 8px;
    color: var(--text-muted); font-size: 13px;
    margin-bottom: 16px; cursor: pointer;
}
.remember-row input { accent-color: var(--accent); width: 14px; height: 14px; }

.btn {
    width: 100%; padding: 10px; border: none; border-radius: var(--radius);
    font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dim); }
.btn-primary:disabled { opacity: .5; cursor: default; }

.auth-error {
    font-size: 13px; color: var(--danger); margin-top: 10px; min-height: 16px;
}

/* ── APP LAYOUT ──────────────────────────────────────────────── */
#app { display: flex; width: 100%; height: 100vh; height: 100dvh; }

#sidebar {
    width: var(--sidebar-w); min-width: var(--sidebar-w);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
}

.sidebar-heading {
    font-size: 11px; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: .06em;
    padding: 16px 14px 8px;
}

#room-list { overflow-y: auto; flex: 1; padding: 0 6px; }

.room-entry {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 8px; margin: 2px 0;
    border-radius: var(--radius);
    cursor: pointer; color: var(--text-muted);
}
.room-entry:hover { background: var(--surface); color: var(--text); }
.room-entry.joined { background: var(--surface-2); color: var(--text); }
.room-entry .room-live-dot {
    width: 6px; height: 6px; border-radius: 50%; background: var(--live);
    flex-shrink: 0; opacity: 0;
}
.room-entry.has-live-members .room-live-dot { opacity: 1; }
.room-entry .room-name { font-size: 13.5px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-entry .room-count { font-size: 11px; color: var(--text-muted); }

.sidebar-spacer { flex-shrink: 0; height: 8px; }

.log-toggle-button {
    margin: 0 12px 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 12px; font-weight: 500;
    border-radius: var(--radius);
    padding: 7px 0;
    cursor: pointer;
}
.log-toggle-button:hover { color: var(--text); }

.self-panel {
    border-top: 1px solid var(--border);
    padding: 10px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    display: flex; align-items: center; gap: 8px;
}
.avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: var(--accent-dim);
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; font-weight: 700; color: #fff;
    position: relative; flex-shrink: 0;
}
.avatar .status-dot {
    position: absolute; bottom: -1px; right: -1px;
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--live);
    border: 2px solid var(--bg-sidebar);
}
.self-name { font-size: 13px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.self-controls { display: flex; gap: 2px; }

.icon-button {
    width: 28px; height: 28px; border: none; border-radius: 6px;
    background: transparent; color: var(--text-muted); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.icon-button svg { width: 15px; height: 15px; pointer-events: none; }
.icon-button:hover { background: var(--surface-2); color: var(--text); }
.icon-button.active { color: var(--danger); }

/* ── MAIN PANEL ──────────────────────────────────────────────── */
#main-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.empty-state {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 10px; color: var(--text-muted);
}
.empty-state svg { opacity: .3; }

#room-live-view { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.room-header {
    padding: 14px 20px; border-bottom: 1px solid var(--border);
    display: flex; align-items: baseline; gap: 10px; flex-shrink: 0;
}
.room-header h1 { font-size: 16px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.room-header-hint { font-size: 12px; color: var(--text-muted); flex-shrink: 0; }

#video-grid {
    flex: 1; overflow-y: auto; padding: 16px;
    display: grid; gap: 10px;
    grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr));
    align-content: start;
    position: relative;
}
#video-stage, #video-filmstrip, #stage-controls { display: none; }

/* Focused-tile mode: one tile fills the stage, everything else filmstrips along the bottom. */
#video-grid.focused-mode {
    display: flex; flex-direction: column;
    overflow: hidden;
}
#video-grid.focused-mode #video-stage {
    display: flex;
    flex: 1; min-height: 0;
}
#video-grid.focused-mode #video-stage .tile { width: 100%; aspect-ratio: unset; }
#video-grid.focused-mode #video-filmstrip {
    display: flex; gap: 10px;
    padding-top: 10px; flex-shrink: 0;
    overflow-x: auto;
}
#video-grid.focused-mode #video-filmstrip .tile {
    width: 180px; min-width: 180px; height: 110px;
    flex-shrink: 0; aspect-ratio: unset;
}
/* Collapsing the filmstrip just hides it — the stage (flex: 1 above) fills the space it leaves. */
#video-grid.focused-mode.filmstrip-collapsed #video-filmstrip { display: none; }

#video-grid.focused-mode #stage-controls {
    display: flex; gap: 6px;
    position: absolute; top: 24px; right: 24px; z-index: 10;
}
#stage-controls .icon-button {
    width: 34px; height: 34px;
    background: rgba(0,0,0,.5); color: #fff;
}
#stage-controls .icon-button:hover { background: rgba(0,0,0,.7); }
#stage-controls .icon-button svg { width: 17px; height: 17px; }
#stage-controls .icon-button.active { color: var(--accent); }
#collapse-filmstrip-button svg { transition: transform .15s ease; }
#collapse-filmstrip-button.active svg { transform: rotate(180deg); }

.tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    aspect-ratio: 16 / 10;
    position: relative; overflow: hidden;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.tile.speaking { box-shadow: 0 0 0 2px var(--live); }
.tile.tile-screen { border-color: var(--accent); }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }

.tile-placeholder { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.tile-placeholder .tile-avatar {
    width: 56px; height: 56px; border-radius: 50%;
    background: var(--accent-dim);
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; font-weight: 700; color: #fff;
}
.tile-placeholder .tile-name { font-size: 12.5px; color: var(--text-muted); }

.tile-label {
    position: absolute; left: 8px; bottom: 8px;
    background: rgba(0,0,0,.6);
    color: #fff; font-size: 12px; font-weight: 500;
    padding: 3px 8px; border-radius: 5px;
    display: flex; align-items: center; gap: 5px;
}
.tile-badge {
    position: absolute; top: 8px; width: 22px; height: 22px; border-radius: 50%;
    background: rgba(0,0,0,.6);
    display: flex; align-items: center; justify-content: center; color: #fff;
}
.tile-badge svg { width: 12px; height: 12px; }
.tile-badge.mic-off { right: 8px; }
.tile-badge.deafened { right: 34px; }

.watch-screen-button {
    position: absolute; top: 8px; left: 8px;
    background: rgba(0,0,0,.6); color: #fff; border: none;
    font-size: 11px; font-weight: 600; border-radius: 5px;
    padding: 4px 8px; cursor: pointer;
}
.watch-screen-button:hover { background: rgba(0,0,0,.8); }

#controls-bar {
    flex-shrink: 0; padding: 14px; border-top: 1px solid var(--border);
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    background: var(--bg-raised);
    display: flex; align-items: center; justify-content: center; gap: 10px;
}
.ctrl-button {
    width: 44px; height: 44px; border-radius: 50%; border: none;
    background: var(--surface-2); color: var(--text); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.ctrl-button svg { width: 18px; height: 18px; }
.ctrl-button:hover { background: var(--border); }
.ctrl-button.off { background: var(--danger); color: #fff; }
.ctrl-button.on-air { background: var(--live); color: #08130e; }
.leave-button { width: auto; border-radius: 22px; padding: 0 20px; background: var(--danger); color: #fff; font-weight: 600; font-size: 13px; }
.leave-button:hover { background: #c93e52; }

/* ── PROTOCOL LOG ────────────────────────────────────────────── */
#log-panel {
    position: fixed; right: 12px; bottom: 48px; width: 380px; height: 320px;
    background: var(--bg-raised); border: 1px solid var(--border); border-radius: 10px;
    display: flex; flex-direction: column; overflow: hidden; z-index: 60;
}
.log-panel-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px 10px; border-bottom: 1px solid var(--border);
    font-size: 12px; font-weight: 600; color: var(--text-muted);
}
.log-panel-header button {
    background: none; border: none; color: var(--text-muted); font-size: 11px; cursor: pointer;
}
#log-panel-body {
    flex: 1; overflow-y: auto; padding: 6px 8px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 11.5px; line-height: 1.6;
}
.log-line { white-space: pre-wrap; word-break: break-all; }
.log-line.out { color: var(--accent); }
.log-line.in  { color: var(--live); }

/* ── CONTEXT MENU ────────────────────────────────────────────── */
.context-menu {
    position: fixed;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px;
    min-width: 160px;
    z-index: 100;
    box-shadow: 0 8px 24px rgba(0,0,0,.35);
}
.context-menu button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 7px 10px;
    border: none;
    background: none;
    color: var(--text);
    font-size: 13px;
    border-radius: 5px;
    cursor: pointer;
}
.context-menu button:hover { background: var(--surface-2); }
.context-menu button.danger { color: var(--danger); }
.context-menu-input {
    display: block;
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 5px;
    color: var(--text);
    font-size: 16px; /* see .field input above — avoids iOS Safari's auto-zoom on focus */
    padding: 7px 8px;
    margin-bottom: 4px;
    outline: none;
}
.context-menu-input:focus { border-color: var(--accent); }

/* ── STATUS BAR ──────────────────────────────────────────────── */
#status-bar {
    position: fixed; right: 12px; bottom: 12px;
    background: var(--bg-raised); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 6px 12px; font-size: 12px; color: var(--text-muted);
    display: flex; align-items: center; gap: 7px; z-index: 60;
}
.connection-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--danger); }
.connection-dot.connected { background: var(--live); }

/* ── MOBILE LAYOUT ───────────────────────────────────────────── */
@media (max-width: 780px) {
    /* Sidebar and main panel take turns filling the screen instead of sitting side by side.
       Driven entirely by #room-live-view's existing .hidden toggle (see client.js's joinRoom/
       leaveRoom) — no separate mobile state needed. Not in a room -> sidebar (room list) fills
       the screen. In a room -> main panel (video grid + controls) fills the screen; the way back
       to the room list is the existing Leave button. */
    #sidebar { width: 100%; min-width: 0; }
    #app:has(#room-live-view:not(.hidden)) #sidebar { display: none; }
    #app:has(#room-live-view.hidden) #main-panel { display: none; }

    /* Bigger touch targets throughout — the desktop sizes are mouse-cursor sizes. */
    .room-entry { padding: 12px 10px; }
    .auth-tab { padding: 11px; }
    .self-controls .icon-button { width: 38px; height: 38px; }
    .self-controls .icon-button svg { width: 17px; height: 17px; }
    #stage-controls .icon-button { width: 40px; height: 40px; }
    #stage-controls { top: 12px; right: 12px; gap: 6px; }
    .ctrl-button { width: 40px; height: 40px; }
    #controls-bar { gap: 8px; padding: 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
    .leave-button { padding: 0 16px; }
    .context-menu button { padding: 10px 12px; }

    /* Fixed 380px panel would overflow a phone screen — make it a bottom sheet instead. */
    #log-panel {
        left: 12px; right: 12px; width: auto;
        bottom: 12px; height: min(320px, 60vh);
    }

    /* Was colliding with the centered controls-bar buttons on narrow screens — move it out of the way. */
    #status-bar { top: 12px; bottom: auto; right: 12px; }
}
