/* ─── Base & Reset ─── */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-tertiary: #1a1a26;
    --bg-hover: #22222e;
    --accent: #6c63ff;
    --accent-glow: rgba(108, 99, 255, 0.3);
    --accent-gradient: linear-gradient(135deg, #6c63ff, #3b82f6);
    --text-primary: #f0f0f0;
    --text-secondary: #8888a0;
    --text-muted: #55556a;
    --border: #2a2a3a;
    --success: #22c55e;
    --danger: #ef4444;
    --msg-sent: #6c63ff;
    --msg-received: #1e1e2e;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    --radius: 12px;
    --radius-lg: 16px;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* ─── Auth Pages ─── */
.auth-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg-primary);
    background-image:
        radial-gradient(ellipse at 20% 50%, rgba(108, 99, 255, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 20%, rgba(59, 130, 246, 0.06) 0%, transparent 50%);
}

.auth-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 40px;
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow);
    animation: fadeInUp 0.5s ease;
}

.auth-logo {
    text-align: center;
    margin-bottom: 32px;
}

.logo-icon {
    font-size: 48px;
    margin-bottom: 8px;
}

.auth-logo h1 {
    font-size: 28px;
    font-weight: 700;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.auth-subtitle {
    color: var(--text-secondary);
    font-size: 14px;
    margin-top: 4px;
}

.auth-form { display: none; }
.auth-form.active { display: block; }

.input-group {
    margin-bottom: 20px;
}

.input-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.input-group input {
    width: 100%;
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.2s, box-shadow 0.2s;
    outline: none;
}

.input-group input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.input-hint {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    display: block;
}

.btn-primary {
    width: 100%;
    padding: 12px;
    background: var(--accent-gradient);
    border: none;
    border-radius: var(--radius);
    color: white;
    font-size: 15px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.2s;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 20px var(--accent-glow);
}

.btn-primary:active {
    transform: translateY(0);
}

.auth-switch {
    text-align: center;
    margin-top: 20px;
    font-size: 13px;
    color: var(--text-secondary);
}

.auth-switch a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.error-msg {
    color: var(--danger);
    font-size: 13px;
    text-align: center;
    margin-top: 12px;
    min-height: 20px;
}

.success-msg {
    color: var(--success);
    font-size: 13px;
    text-align: center;
    margin-top: 12px;
    min-height: 20px;
}

/* ─── App Layout ─── */
.app-container {
    display: flex;
    height: 100vh;
    height: 100dvh;
    width: 100%;
}

/* ─── Sidebar ─── */
.sidebar {
    width: 360px;
    min-width: 360px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: relative;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 20px 12px;
}

.sidebar-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-profile-avatar {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    color: white;
}

.user-username {
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 400;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 12px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.2s, transform 0.15s;
    position: relative;
    color: var(--text-primary);
}

.icon-btn:hover {
    background: var(--bg-hover);
    transform: scale(1.05);
}

.badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--danger);
    color: white;
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: inherit;
}

.search-bar {
    padding: 0 16px 12px;
}

.search-bar input {
    width: 100%;
    padding: 10px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: 13px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

.search-bar input:focus {
    border-color: var(--accent);
}

/* ─── Chat List ─── */
.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px;
}

.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-track { background: transparent; }
.chat-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.chat-item {
    display: flex;
    align-items: center;
    padding: 12px 14px;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s;
    gap: 12px;
    margin-bottom: 2px;
}

.chat-item:hover {
    background: var(--bg-hover);
}

.chat-item.active {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
}

.chat-item.self-chat {
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
}

.chat-avatar {
    width: 46px;
    height: 46px;
    min-width: 46px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 600;
    color: white;
    position: relative;
}

.online-dot {
    width: 10px;
    height: 10px;
    background: var(--success);
    border-radius: 50%;
    border: 2px solid var(--bg-secondary);
    position: absolute;
    bottom: 0;
    right: 0;
}

.chat-info {
    flex: 1;
    min-width: 0;
}

.chat-info-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.chat-info-top h4 {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-time {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
}

.chat-preview {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-badge {
    background: var(--accent);
    color: white;
    font-size: 11px;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 8px;
}

/* ─── FAB (Floating Action Button) ─── */
.fab {
    position: absolute;
    bottom: 24px;
    right: 24px;
    width: 52px;
    height: 52px;
    background: var(--accent-gradient);
    border: none;
    border-radius: 50%;
    font-size: 28px;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 24px var(--accent-glow);
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
}

.fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 32px var(--accent-glow);
}

/* ─── Chat Area ─── */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Prevents flex flex-column from growing past 100vh */
    background: var(--bg-primary);
    background-image:
        radial-gradient(ellipse at 50% 0%, rgba(108, 99, 255, 0.03) 0%, transparent 50%);
}

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

.empty-icon {
    font-size: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.empty-state h3 {
    font-size: 20px;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.empty-state p {
    font-size: 14px;
}

/* ─── Chat Window ─── */
.chat-window {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Critical for inner scrolling */
}

.chat-header {
    display: flex;
    align-items: center;
    padding: 14px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    gap: 12px;
}

.back-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
}

.chat-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 600;
    color: white;
    background: var(--accent);
}

.chat-header-info {
    flex: 1;
}

.chat-header-info h3 {
    font-size: 15px;
    font-weight: 600;
}

.status-text {
    font-size: 12px;
    color: var(--text-muted);
}

.status-text.online {
    color: var(--success);
}

.chat-header-actions {
    display: flex;
    gap: 8px;
}

/* ─── Messages ─── */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px 28px;
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.messages-container::-webkit-scrollbar { width: 4px; }
.messages-container::-webkit-scrollbar-track { background: transparent; }
.messages-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.message {
    max-width: 65%;
    padding: 12px 18px;
    border-radius: 20px;
    font-size: 14.5px;
    line-height: 1.55;
    animation: msgIn 0.25s ease;
    word-wrap: break-word;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.message.sent {
    align-self: flex-end;
    background: var(--accent-gradient);
    color: white;
    border-bottom-right-radius: 6px;
    margin-right: 4px;
}

.message.received {
    align-self: flex-start;
    background: var(--msg-received);
    border: 1px solid var(--border);
    color: var(--text-primary);
    border-bottom-left-radius: 6px;
    margin-left: 4px;
}

.message .msg-time {
    font-size: 10px;
    color: rgba(255,255,255,0.45);
    margin-top: 6px;
    text-align: right;
}

.message.received .msg-time {
    color: var(--text-muted);
}

.msg-date-divider {
    text-align: center;
    font-size: 11px;
    color: var(--text-muted);
    padding: 12px 0;
}

/* ─── Typing Indicator ─── */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 8px 20px;
    color: var(--text-muted);
    font-size: 13px;
}

.typing-indicator .dot {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(1) { animation-delay: 0s; }
.typing-indicator .dot:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.4s; }

/* ─── Message Input ─── */
.message-input-area {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    gap: 10px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
}

.message-input-area input {
    flex: 1;
    padding: 12px 18px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 24px;
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

.message-input-area input:focus {
    border-color: var(--accent);
}

.send-btn {
    width: 44px;
    height: 44px;
    background: var(--accent-gradient);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 18px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.send-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 16px var(--accent-glow);
}

/* ─── Modals ─── */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow);
    animation: fadeInUp 0.3s ease;
    margin: 16px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px 0;
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 4px;
}

.modal-body {
    padding: 20px 24px;
}

.modal-body p {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.modal-body input {
    width: 100%;
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

.modal-body input:focus {
    border-color: var(--accent);
}

.modal-footer {
    padding: 0 24px 20px;
}

/* ─── Friend Request Items ─── */
.requests-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.request-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
}

.request-item .chat-avatar {
    width: 40px;
    height: 40px;
    min-width: 40px;
    font-size: 16px;
}

.request-info {
    flex: 1;
}

.request-info h4 {
    font-size: 14px;
    font-weight: 600;
}

.request-info span {
    font-size: 12px;
    color: var(--text-muted);
}

.request-actions {
    display: flex;
    gap: 6px;
}

.btn-accept {
    background: var(--success);
    border: none;
    color: white;
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: opacity 0.2s;
}

.btn-decline {
    background: var(--bg-hover);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: opacity 0.2s;
}

.btn-accept:hover, .btn-decline:hover { opacity: 0.8; }

.empty-requests {
    color: var(--text-muted);
    text-align: center;
    font-size: 13px;
    padding: 20px;
}

/* ─── Mobile Responsive ─── */
@media (max-width: 768px) {
    .app-container {
        position: relative;
        overflow: hidden;
    }

    .sidebar {
        width: 100%;
        min-width: 100%;
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 20;
        transition: transform 0.3s ease;
    }

    .sidebar.hidden {
        transform: translateX(-100%);
        pointer-events: none;
    }

    .back-btn {
        display: block;
    }

    .chat-area {
        width: 100%;
        min-width: 100%;
    }

    .chat-window {
        height: 100vh;
        height: 100dvh;
    }

    .message {
        max-width: 85%;
    }

    .messages-container {
        padding: 16px 12px;
    }

    .message-input-area {
        padding: 8px 10px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .message-input-area input {
        padding: 10px 14px;
        font-size: 16px; /* Prevents zoom on iOS */
    }

    .send-btn {
        width: 40px;
        height: 40px;
        font-size: 16px;
    }

    .chat-header {
        padding: 10px 12px;
    }

    .sidebar-header {
        padding: 14px 14px 8px;
    }

    .search-bar {
        padding: 0 12px 8px;
    }

    .fab {
        bottom: 20px;
        right: 20px;
        width: 48px;
        height: 48px;
        font-size: 24px;
    }

    .auth-card {
        margin: 16px;
        padding: 28px;
    }

    /* Call overlay must appear above everything */
    .call-overlay {
        z-index: 300;
    }

    .incoming-call-card {
        padding: 28px;
        min-width: 260px;
        margin: 16px;
    }
}

/* ─── Animations ─── */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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

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

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* ─── Call Overlays ─── */
.call-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    animation: fadeIn 0.3s ease;
}

.incoming-call-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 40px;
    text-align: center;
    animation: fadeInUp 0.4s ease;
    min-width: 300px;
}

.call-avatar-ring {
    width: 100px;
    height: 100px;
    margin: 0 auto 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: callPulse 2s ease-in-out infinite;
    border: 3px solid var(--accent);
}

.call-avatar {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--accent-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: 700;
    color: white;
}

.incoming-call-card h3 {
    font-size: 22px;
    margin-bottom: 6px;
}

.incoming-call-card p {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 32px;
}

.incoming-call-actions {
    display: flex;
    justify-content: center;
    gap: 32px;
}

.call-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.call-btn:hover {
    transform: scale(1.1);
}

.accept-call {
    background: var(--success);
    color: white;
    box-shadow: 0 4px 24px rgba(34, 197, 94, 0.4);
}

.decline-call {
    background: var(--danger);
    color: white;
    box-shadow: 0 4px 24px rgba(239, 68, 68, 0.4);
}

/* Active Call Screen */
.active-call-screen {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
}

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

#localVideo {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 200px;
    height: 150px;
    border-radius: 12px;
    border: 2px solid var(--border);
    object-fit: cover;
    z-index: 10;
    box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.call-info-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 20px 24px;
    background: linear-gradient(to bottom, rgba(0,0,0,0.7) 0%, transparent 100%);
    z-index: 5;
}

.call-info-bar h3 {
    font-size: 18px;
    font-weight: 600;
}

.call-info-bar span {
    font-size: 13px;
    color: var(--text-secondary);
}

.call-controls {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 20px;
    z-index: 10;
}

.call-control-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: none;
    background: rgba(255,255,255,0.15);
    backdrop-filter: blur(8px);
    font-size: 22px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.15s;
    color: white;
}

.call-control-btn:hover {
    background: rgba(255,255,255,0.25);
    transform: scale(1.05);
}

.call-control-btn.hangup-btn {
    background: var(--danger);
    box-shadow: 0 4px 16px rgba(239, 68, 68, 0.4);
}

.call-control-btn.control-off {
    background: rgba(255,255,255,0.05);
    opacity: 0.6;
}

/* Call pulse animation */
@keyframes callPulse {
    0% { box-shadow: 0 0 0 0 var(--accent-glow); }
    50% { box-shadow: 0 0 0 15px rgba(108, 99, 255, 0); }
    100% { box-shadow: 0 0 0 0 rgba(108, 99, 255, 0); }
}

/* Mobile call adjustments */
@media (max-width: 768px) {
    #localVideo {
        width: 120px;
        height: 90px;
        top: 12px;
        right: 12px;
    }
    .call-controls {
        bottom: 30px;
        gap: 16px;
    }
    .call-control-btn {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }
}

/* ─── Scrollbar (Global) ─── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
