/* Reset & Base */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; width: 100%; overflow: hidden; font-size: 16px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f5f5f7;
    color: #1d1d1f;
}

.hidden { display: none !important; }

/* Screens */
.screen { display: none; width: 100%; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

/* Typography */
h1 { font-size: 28px; font-weight: 700; }
h2 { font-size: 22px; font-weight: 600; }
h3 { font-size: 16px; font-weight: 600; color: #86868b; margin-bottom: 8px; }
.subtitle { color: #86868b; font-size: 14px; margin-top: 4px; }

/* Header */
.header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 16px;
    background: #ffffff;
    border-bottom: 1px solid #e5e5ea;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
#room-screen .header {
    background: rgba(20, 20, 22, 0.95);
    color: #e5e5ea;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    padding: 8px 16px;
    font-size: 14px;
}
#room-screen .header .room-info { display: flex; align-items: center; gap: 12px; }
#room-screen .header span { color: #e5e5ea; }
.screen-header {
    display: flex; align-items: center; gap: 16px;
    padding: 12px 16px; background: #ffffff; border-bottom: 1px solid #e5e5ea;
}
.user-info { display: flex; align-items: center; gap: 10px; }
.header-actions { display: flex; gap: 8px; align-items: center; }

/* Forms */
.form {
    width: 100%; max-width: 360px;
    display: flex; flex-direction: column; gap: 12px;
    padding: 20px;
}
.form.scrollable { overflow-y: auto; flex: 1; }

input, textarea {
    padding: 12px 14px;
    border-radius: 10px;
    border: 1px solid #d1d1d6;
    background: #ffffff;
    color: #1d1d1f;
    font-size: 15px;
    outline: none;
    width: 100%;
    transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus { border-color: #007aff; box-shadow: 0 0 0 3px rgba(0,122,255,0.15); }
input:disabled { opacity: 0.5; cursor: not-allowed; background: #f9f9fb; }
label { font-size: 13px; color: #86868b; margin-bottom: -8px; margin-top: 4px; }

/* Buttons */
button { cursor: pointer; border: none; font-weight: 600; transition: opacity 0.15s, background 0.15s; }
button:active { opacity: 0.7; }
.btn-primary {
    padding: 13px; border-radius: 10px; background: #007aff; color: #fff; font-size: 16px;
}
.btn-primary:hover { background: #0066d6; }
.btn-primary-sm {
    padding: 8px 16px; border-radius: 8px; background: #007aff; color: #fff; font-size: 14px;
}
.btn-primary-sm:hover { background: #0066d6; }
.btn-secondary {
    padding: 13px; border-radius: 10px; background: #e5e5ea; color: #1d1d1f; font-size: 16px;
}
.btn-secondary:hover { background: #d1d1d6; }
.btn-secondary-sm {
    padding: 8px 16px; border-radius: 8px; background: #e5e5ea; color: #1d1d1f; font-size: 14px;
}
.btn-secondary-sm:hover { background: #d1d1d6; }
.btn-danger {
    padding: 13px; border-radius: 10px; background: #ff3b30; color: #fff; font-size: 16px;
}
.btn-danger-sm {
    padding: 6px 14px; border-radius: 8px; background: #ff3b30; color: #fff; font-size: 13px;
}
.btn-link {
    background: none; color: #007aff; font-size: 14px; text-decoration: underline; padding: 4px;
}
.btn-link-sm {
    background: none; color: #007aff; font-size: 13px; padding: 4px 8px;
}
.btn-icon {
    background: none; color: #1d1d1f; font-size: 20px; padding: 4px 8px; border-radius: 8px;
}
.btn-icon:hover { background: #e5e5ea; }
.btn-back {
    background: none; color: #007aff; font-size: 15px; padding: 4px 8px;
}
.btn-close { background: none; color: #8e8e93; font-size: 18px; padding: 4px 8px; }
.btn-close:hover { color: #1d1d1f; }

/* Login Screen */
#login-screen, #register-screen {
    justify-content: center; align-items: center;
    padding: 20px;
}
#register-screen { justify-content: flex-start; }
.logo { text-align: center; margin-bottom: 30px; }
.logo-img {
    max-width: 220px;
    max-height: 80px;
    object-fit: contain;
    display: block;
    margin: 0 auto;
}
.logo-icon { font-size: 56px; margin-bottom: 8px; }

.error-msg { color: #ff3b30; font-size: 13px; text-align: center; }
.success-msg { color: #34c759; font-size: 14px; text-align: center; padding: 16px; }
.status-link { color: #007aff; font-size: 13px; cursor: pointer; text-align: center; margin-top: 12px; }

/* Dashboard */
#dashboard-screen { overflow: hidden; }

.tabs {
    display: flex; gap: 0;
    background: #ffffff; border-bottom: 1px solid #e5e5ea;
    overflow-x: auto;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.tab-btn {
    flex: 1; min-width: 70px;
    padding: 12px 8px; font-size: 13px;
    background: none; color: #86868b;
    border-bottom: 2px solid transparent;
    white-space: nowrap;
}
.tab-btn.active { color: #007aff; border-bottom-color: #007aff; font-weight: 600; }
.badge {
    background: #ff3b30; color: #fff; font-size: 11px; padding: 2px 6px;
    border-radius: 10px; margin-left: 4px;
}

.tab-content { flex: 1; overflow: hidden; }
.tab-panel { display: none; flex-direction: column; height: 100%; overflow-y: auto; padding: 12px; }
.tab-panel.active { display: flex; }
.tab-panel.scrollable { overflow-y: auto; }

.tab-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.tab-actions input { flex: 1; }
.tab-actions input { padding: 10px 12px; font-size: 14px; }

.section { margin-bottom: 16px; }
.list { display: flex; flex-direction: column; gap: 8px; }

/* Room Cards */
.room-card {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px; border-radius: 12px; background: #ffffff;
    border: 1px solid #e5e5ea;
    box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.room-card-info h4 { font-size: 15px; }
.room-card-info span { font-size: 12px; color: #86868b; }
.room-card-actions { display: flex; gap: 6px; }
.room-card .role-badge {
    font-size: 11px; padding: 3px 8px; border-radius: 6px;
}
.role-badge.owner { background: rgba(0,122,255,0.12); color: #007aff; }
.role-badge.member { background: rgba(52,199,89,0.12); color: #34c759; }

/* Notifications */
.notif-item {
    display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
    padding: 12px; border-radius: 10px; background: #ffffff;
    border: 1px solid #e5e5ea;
}
.notif-item.unread { background: #f0f5ff; border-left: 3px solid #007aff; }
.notif-content h4 { font-size: 14px; font-weight: 600; }
.notif-content p { font-size: 13px; color: #86868b; margin-top: 2px; }
.notif-time { font-size: 11px; color: #aeaeb2; white-space: nowrap; }
.notif-actions { display: flex; gap: 4px; margin-top: 8px; }
.btn-accept { padding: 5px 12px; border-radius: 6px; background: #34c759; color: #fff; font-size: 12px; }
.btn-decline { padding: 5px 12px; border-radius: 6px; background: #ff3b30; color: #fff; font-size: 12px; }

/* Profile */
.profile-photo-section {
    display: flex; flex-direction: column; align-items: center; gap: 10px;
    margin-bottom: 20px; padding-top: 10px;
}
.avatar-large {
    width: 100px; height: 100px; border-radius: 50%; object-fit: cover;
    background: #e5e5ea; border: 3px solid #f5f5f7;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.avatar-small { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: #e5e5ea; }

/* Search results */
.user-search-item {
    display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 10px;
    background: #ffffff; border: 1px solid #e5e5ea;
}
.user-search-item img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; }
.user-search-info { flex: 1; }
.user-search-info strong { font-size: 14px; }
.user-search-info span { font-size: 12px; color: #86868b; display: block; }

/* Room Screen (Video) */
.room-main {
    display: flex; flex: 1; overflow: hidden;
    min-height: 0; /* critical: allow flex child to shrink below content size */
}
#videos-container {
    flex: 1; display: flex; flex-wrap: wrap; gap: 4px;
    overflow: hidden; padding: 4px;
    position: relative;
    background: #0a0a0a;
    min-height: 0;
}
.video-wrapper {
    position: relative; background: #1a1a1a; border-radius: 12px; overflow: hidden;
    flex: 1 1 33%; min-width: 0; min-height: 0;
    border: 2px solid #2d2d2d;
}
.video-wrapper.remote {
    border: 2px solid #3a3a3a;
    box-shadow: 0 0 0 1px rgba(255,255,255,0.08);
}

/* PiP — local stream floats bottom-right when not in grid */
.video-wrapper.local.pip {
    position: absolute !important;
    bottom: 80px; right: 16px;
    width: var(--pip-width, 200px);
    height: var(--pip-height, 150px);
    min-width: var(--pip-width, 200px);
    min-height: var(--pip-height, 150px);
    flex: none;
    z-index: 40;
    border: 2px solid #007aff;
    border-radius: 14px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.6);
    cursor: grab;
}
.video-wrapper.local.pip:active { cursor: grabbing; }
.video-wrapper.local.pip .video-label { font-size: 10px; padding: 2px 8px; }

video { width: 100%; height: 100%; object-fit: contain; display: block; background: #0a0a0a; }
.video-label {
    position: absolute; bottom: 6px; left: 6px;
    background: rgba(0,0,0,0.65); color: #fff;
    padding: 3px 8px; border-radius: 6px; font-size: 11px;
    pointer-events: none;
}

/* Layout states */
#videos-container.pip-mode .video-wrapper.remote { flex: 1 1 100%; height: 100%; }
#videos-container.pip-2 .video-wrapper.remote { flex: 1 1 calc(50% - 2px); height: 100%; }

/* Grid: 2×2 (4-up) */
#videos-container.grid-4 .video-wrapper { flex: 1 1 calc(50% - 2px); max-height: calc(50% - 2px); }
#videos-container.grid-4 .local:not(.pip) { position: relative !important; }

/* Grid: 5+ users */
#videos-container.grid-many .video-wrapper { flex: 1 1 calc(33.33% - 4px); max-height: calc(33.33% - 4px); }

/* Chat Panel — right side overlay */
.chat-panel {
    position: absolute; top: 0; right: 0; bottom: 0;
    width: 340px; max-width: 90vw;
    border-left: 1px solid rgba(255,255,255,0.08);
    display: flex; flex-direction: column;
    background: rgba(24, 24, 26, 0.97);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 60;
    color: #e5e5ea;
    transition: transform 0.25s ease;
    transform: translateX(0);
}
.chat-panel.hidden { transform: translateX(100%); }
.chat-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 14px; border-bottom: 1px solid rgba(255,255,255,0.08);
    font-weight: 600; color: #e5e5ea;
}
.chat-messages {
    flex: 1; overflow-y: auto; padding: 10px;
    display: flex; flex-direction: column; gap: 8px;
}
.chat-bubble {
    max-width: 80%; padding: 8px 12px; border-radius: 12px; font-size: 13px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.chat-bubble.me { align-self: flex-end; background: #007aff; color: #fff; border-bottom-right-radius: 4px; }
.chat-bubble.me .msg-user { color: rgba(255,255,255,0.8); }
.chat-bubble.me .msg-time { color: rgba(255,255,255,0.7); }
.chat-bubble.other { align-self: flex-start; background: rgba(255,255,255,0.1); color: #e5e5ea; border-bottom-left-radius: 4px; }
.chat-bubble.system { align-self: center; background: none; color: #8e8e93; font-size: 12px; font-style: italic; box-shadow: none; }
.chat-bubble .msg-user { font-size: 11px; margin-bottom: 2px; }
.chat-bubble:not(.me) .msg-user { color: #007aff; }
.chat-bubble .msg-time { font-size: 10px; margin-top: 4px; text-align: right; color: #8e8e93; }
.chat-image { max-width: 150px; max-height: 120px; border-radius: 8px; object-fit: contain; }

.typing-indicator { padding: 4px 14px; font-size: 12px; color: #8e8e93; font-style: italic; }
.chat-input-area { display: flex; gap: 6px; padding: 10px; border-top: 1px solid rgba(255,255,255,0.08); }
.chat-input-area input {
    flex: 1; padding: 10px 12px;
    background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.12);
    color: #e5e5ea;
}
.chat-input-area input::placeholder { color: #8e8e93; }

.ribbon-btn.danger { color: #ff3b30; border-color: rgba(255,59,48,0.2); }
.ribbon-btn.danger:hover { background: rgba(255,59,48,0.15); }

/* Call modal buttons */
.call-btn-accept {
    width: 56px; height: 56px; border-radius: 50%;
    background: #34c759; color: #fff; font-size: 24px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; border: none;
}
.call-btn-decline {
    width: 56px; height: 56px; border-radius: 50%;
    background: #ff3b30; color: #fff; font-size: 24px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; border: none;
}

/* Ribbon Controls Bar — bottom of screen */
.controls-ribbon {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    padding: 10px 16px;
    background: rgba(20, 20, 22, 0.95);
    border-top: 1px solid rgba(255,255,255,0.08);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    flex-shrink: 0;
    position: relative;
    z-index: 50;
}
.ribbon-btn {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 8px 16px;
    color: #ccc;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    min-width: 64px;
}
.ribbon-btn:hover { background: rgba(255,255,255,0.12); color: #fff; }
.ribbon-btn .ribbon-icon { font-size: 20px; }
.ribbon-btn.off { color: #ff3b30; border-color: rgba(255,59,48,0.3); }
.ribbon-btn.off:hover { background: rgba(255,59,48,0.15); }
.ribbon-btn.active { color: #007aff; border-color: rgba(0,122,255,0.3); }
.ribbon-sep {
    width: 1px; height: 32px; background: rgba(255,255,255,0.1);
    margin: 0 4px;
}

/* Chat Panel — right side overlay */
.chat-panel {
    position: absolute; top: 0; right: 0; bottom: 0;
    width: 340px; max-width: 90vw;
    border-left: 1px solid rgba(255,255,255,0.08);
    display: flex; flex-direction: column;
    background: rgba(24, 24, 26, 0.97);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 60;
    color: #e5e5ea;
    transition: transform 0.25s ease;
    transform: translateX(0);
}
.chat-panel.hidden { transform: translateX(100%); }

/* Modals */
.modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.3); backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
}
.modal-content {
    background: #ffffff; border-radius: 16px; padding: 24px;
    max-width: 360px; width: 90%;
    display: flex; flex-direction: column; gap: 14px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
}
.call-modal { text-align: center; }
.call-icon { font-size: 64px; }
.call-actions { display: flex; gap: 20px; justify-content: center; margin-top: 8px; }
.modal-actions { display: flex; gap: 8px; }

/* Toast */
#toast-container {
    position: fixed; top: 16px; left: 50%; transform: translateX(-50%);
    z-index: 200; display: flex; flex-direction: column; gap: 8px;
    pointer-events: none;
}
.toast {
    background: #1d1d1f; color: #fff;
    padding: 12px 20px; border-radius: 10px;
    font-size: 14px; animation: toastIn 0.3s ease;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
.toast.error { background: #ff3b30; }
.toast.success { background: #34c759; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* Empty state */
.empty { text-align: center; color: #aeaeb2; padding: 30px 20px; font-size: 14px; }

/* Large screens */
@media (min-width: 1024px) {
    h1 { font-size: 34px; }
    .controls-ribbon { padding: 12px 24px; gap: 12px; }
    .ribbon-btn { padding: 10px 20px; font-size: 12px; }
    .ribbon-btn .ribbon-icon { font-size: 22px; }
}

/* Pending invites section */
.pending-invite-card {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px; border-radius: 10px;
    background: #f0f5ff; border: 1px solid rgba(0,122,255,0.2);
    margin-bottom: 6px;
}
.pending-invite-card .invite-info { flex: 1; }
.pending-invite-card h4 { font-size: 14px; }
.pending-invite-card span { font-size: 12px; color: #86868b; }
