/* ============= CSS Variables ============= */
:root {
    --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    --accent-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
    --bg-dark: #0f0f1e;
    --bg-medium: #1a1a2e;
    --bg-light: #252542;
    --bg-chat: #0d0d1a;
    --text-primary: #ffffff;
    --text-secondary: #b4b4c7;
    --text-muted: #6b6b80;
    --border-color: rgba(255, 255, 255, 0.08);
    --accent-primary: #667eea;
    --accent-secondary: #764ba2;
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);
}

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
}

a {
    color: var(--accent-primary);
    text-decoration: none;
}

/* ============= Auth Screen ============= */
#auth-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-dark);
    background-image: radial-gradient(ellipse at top, rgba(102, 126, 234, 0.15) 0%, transparent 50%),
        radial-gradient(ellipse at bottom, rgba(118, 75, 162, 0.1) 0%, transparent 50%);
}

.auth-container {
    width: 100%;
    max-width: 420px;
    padding: 24px;
}

.auth-header {
    text-align: center;
    margin-bottom: 40px;
}

.auth-header h1 {
    font-size: 42px;
    font-weight: 700;
    background: var(--primary-gradient);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 12px;
}

.auth-header p {
    color: var(--text-secondary);
    font-size: 16px;
}

.auth-card {
    background: rgba(26, 26, 46, 0.8);
    backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-xl);
    padding: 40px;
    box-shadow: var(--shadow-lg);
}

.form-tabs {
    display: flex;
    gap: 12px;
    margin-bottom: 30px;
}

.tab-btn {
    flex: 1;
    padding: 12px;
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
}

.tab-btn.active {
    background: var(--primary-gradient);
    color: white;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.auth-form {
    display: none;
}

.auth-form.active {
    display: block;
    animation: fadeIn 0.3s;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
}

.form-group input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-light);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 15px;
    transition: all 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.btn-primary {
    width: 100%;
    padding: 14px;
    background: var(--primary-gradient);
    border: none;
    border-radius: var(--radius-md);
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

/* ============= Chat Screen ============= */
#chat-screen {
    display: none;
    height: 100vh;
    grid-template-columns: 380px 1fr;
    background: var(--bg-dark);
}

/* ============= Sidebar ============= */
.sidebar {
    background: var(--bg-medium);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.sidebar-header {
    padding: 20px 24px;
    background: var(--bg-light);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.current-user {
    display: flex;
    align-items: center;
    gap: 16px;
    flex: 1;
}

.current-user .avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 600;
    box-shadow: var(--shadow-sm);
    flex-shrink: 0;
}

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

.user-info h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.user-info p {
    font-size: 13px;
    color: var(--text-secondary);
}

.btn-logout {
    background: rgba(239, 68, 68, 0.1);
    border: none;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--error);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.btn-logout:hover {
    background: rgba(239, 68, 68, 0.2);
}

.btn-logout svg {
    width: 20px;
    height: 20px;
}

/* Mobile close button */
.mobile-close-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 8px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
    margin-right: 12px;
}

.mobile-close-btn:hover {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error);
}

/* Sidebar footer for mobile logout */
.sidebar-footer {
    padding: 16px 24px;
    background: var(--bg-light);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.btn-logout-mobile {
    width: 100%;
    padding: 14px 20px;
    background: rgba(239, 68, 68, 0.1);
    border: none;
    border-radius: var(--radius-md);
    color: var(--error);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.btn-logout-mobile:hover {
    background: rgba(239, 68, 68, 0.2);
}

/* Responsive visibility helpers */
.desktop-only {
    display: flex;
}

.mobile-only {
    display: none;
}

/* Scrollable sidebar content */
.sidebar-content {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-section {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-section h4 {
    padding: 16px 24px 12px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 0;
    flex-shrink: 0;
}

.users-list,
.conversations-list {
    display: flex;
    flex-direction: column;
}

.conversation-item,
.user-item {
    padding: 16px 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    transition: all 0.2s;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.conversation-item:hover,
.user-item:hover {
    background: var(--bg-light);
}

.conversation-item.active,
.user-item.active {
    background: var(--bg-light);
    border-left: 3px solid var(--accent-primary);
}

.avatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--primary-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 600;
    flex-shrink: 0;
}

.user-details {
    flex: 1;
    min-width: 0;
}

.user-details h5 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.user-details p {
    font-size: 13px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status {
    font-size: 12px;
    font-weight: 500;
}

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

.status.offline {
    color: var(--text-muted);
}

.empty-state {
    padding: 60px 24px;
    text-align: center;
    color: var(--text-muted);
}

.empty-state p {
    font-size: 15px;
    margin-bottom: 8px;
}

.empty-state small {
    font-size: 13px;
}

/* ============= Chat Area ============= */
.chat-area {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-chat);
    overflow: hidden;
}

#empty-chat {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    flex-direction: column;
    gap: 16px;
}

#empty-chat h2 {
    font-size: 28px;
    background: var(--primary-gradient);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

#empty-chat p {
    color: var(--text-secondary);
    font-size: 15px;
}

#active-chat {
    display: none;
    flex-direction: column;
    height: 100%;
}

.chat-header {
    padding: 20px 24px;
    background: var(--bg-medium);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 16px;
}

.mobile-menu-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 8px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.mobile-menu-btn:hover {
    background: var(--bg-light);
}

.mobile-menu-empty {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
    background: var(--bg-medium);
    box-shadow: var(--shadow-md);
}

.mobile-menu-empty:hover {
    background: var(--bg-light);
}

.chat-user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
}

.chat-user-info .avatar {
    width: 44px;
    height: 44px;
    font-size: 16px;
}

.chat-user-info h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 2px;
}

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

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

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.message {
    display: flex;
    gap: 12px;
    max-width: 70%;
    animation: messageSlide 0.3s ease;
}

@keyframes messageSlide {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

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

.message .avatar {
    width: 36px;
    height: 36px;
    font-size: 14px;
}

.message-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message.sent .message-content {
    align-items: flex-end;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.5;
    word-wrap: break-word;
    max-width: 100%;
}

.message.received .message-bubble {
    background: var(--bg-medium);
    border-bottom-left-radius: 4px;
}

.message.sent .message-bubble {
    background: var(--primary-gradient);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.message .message-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.6);
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.message.received .message-time {
    color: rgba(255, 255, 255, 0.5);
}

/* WhatsApp-style read receipts */
.receipt-icon {
    font-size: 14px;
    font-weight: bold;
}

.receipt-icon.sent {
    color: rgba(255, 255, 255, 0.5);
}

.receipt-icon.delivered {
    color: rgba(255, 255, 255, 0.6);
}

.receipt-icon.read {
    color: #4facfe;
}

.message-media {
    max-width: 100%;
    border-radius: var(--radius-md);
    overflow: hidden;
}

.message-media img,
.message-media video {
    max-width: 100%;
    display: block;
    border-radius: var(--radius-md);
}

/* Message Input */
.message-input-container {
    padding: 20px 24px;
    background: var(--bg-medium);
    border-top: 1px solid var(--border-color);
}

.file-preview {
    background: var(--bg-light);
    padding: 12px 16px;
    border-radius: var(--radius-md);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.file-preview button {
    background: none;
    border: none;
    color: var(--error);
    cursor: pointer;
    font-size: 20px;
    padding: 4px 8px;
}

.message-form {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-light);
    padding: 12px 16px;
    border-radius: var(--radius-xl);
}

.attachment-btn,
.send-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.attachment-btn:hover,
.send-btn:hover {
    background: rgba(102, 126, 234, 0.1);
    color: var(--accent-primary);
}

.message-form input[type="text"] {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 15px;
    padding: 8px;
}

.message-form input[type="text"]:focus {
    outline: none;
}

.message-form input[type="text"]::placeholder {
    color: var(--text-muted);
}

/* ============= Toast Notifications ============= */
.toast-container {
    position: fixed;
    top: 24px;
    right: 24px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.toast {
    background: var(--bg-medium);
    backdrop-filter: blur(20px);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    min-width: 320px;
    animation: toastSlide 0.3s ease;
}

.toast.success {
    border-left: 4px solid var(--success);
}

.toast.error {
    border-left: 4px solid var(--error);
}

.toast.info {
    border-left: 4px solid var(--accent-primary);
}

@keyframes toastSlide {
    from {
        opacity: 0;
        transform: translateX(100px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* ============= Scrollbar ============= */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* ============= Mobile Overlay ============= */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 99;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active {
    display: block;
    opacity: 1;
}

/* ============= Responsive Design ============= */
@media (max-width: 768px) {
    #chat-screen {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        width: 85%;
        max-width: 340px;
        height: 100vh;
        z-index: 100;
        transform: translateX(0);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.5);
    }

    .sidebar.mobile-hide {
        transform: translateX(-100%);
    }

    .sidebar.mobile-show {
        transform: translateX(0);
    }

    /* Show mobile elements, hide desktop */
    .mobile-close-btn {
        display: flex;
    }

    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: block;
    }

    .mobile-menu-btn {
        display: flex;
    }

    .chat-header {
        padding: 16px;
    }

    .messages-container {
        padding: 16px;
        padding-bottom: 100px;
    }

    .message {
        max-width: 85%;
    }

    .message-input-container {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 16px;
        background: var(--bg-medium);
        border-top: 1px solid var(--border-color);
    }

    .auth-container {
        padding: 16px;
    }

    .auth-card {
        padding: 32px 24px;
    }

    #empty-chat {
        position: relative;
    }
}

@media (max-width: 480px) {
    .sidebar {
        width: 90%;
    }

    .auth-header h1 {
        font-size: 32px;
    }

    .message {
        max-width: 90%;
    }

    .message-bubble {
        font-size: 14px;
        padding: 10px 14px;
    }
}

/* Touch-friendly */
@media (hover: none) and (pointer: coarse) {

    .user-item,
    .conversation-item,
    .btn-logout,
    .attachment-btn,
    .send-btn,
    .mobile-menu-btn {
        min-height: 44px;
        min-width: 44px;
    }

    .message-form input[type="text"] {
        font-size: 16px;
    }
}

/* iOS fixes */
@supports (-webkit-touch-callout: none) {

    #chat-screen,
    .sidebar {
        height: 100vh;
        height: -webkit-fill-available;
    }

    .messages-container {
        -webkit-overflow-scrolling: touch;
    }

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