/* ============================================
   ChatApp – WhatsApp-inspired + Dark Mode
   ============================================ */

:root, [data-theme="light"] {
    --primary: #0d6efd;
    --primary-dark: #0b5ed7;
    --sidebar-bg: #f0f2f5;
    --chat-bg: #e5ddd5;
    --header-bg: #f0f2f5;
    --msg-out: #d9fdd3;
    --msg-in: #ffffff;
    --border: #e9ecef;
    --text: #111b21;
    --text-muted: #667781;
    --body-bg: #d1d7db;
    --card-bg: #ffffff;
    --input-bg: #ffffff;
    --hover-bg: #e9edef;
    --badge: #25d366;
}

[data-theme="dark"] {
    --primary: #4dabf7;
    --primary-dark: #339af0;
    --sidebar-bg: #111b21;
    --chat-bg: #0b141a;
    --header-bg: #202c33;
    --msg-out: #005c4b;
    --msg-in: #202c33;
    --border: #2a3942;
    --text: #e9edef;
    --text-muted: #8696a0;
    --body-bg: #0b141a;
    --card-bg: #111b21;
    --input-bg: #2a3942;
    --hover-bg: #2a3942;
    --badge: #25d366;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--body-bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

/* ---------- Auth pages ---------- */
.auth-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.auth-wrapper { width: 100%; max-width: 420px; padding: 1rem; }

.auth-card {
    background: #fff;
    border-radius: 16px;
    padding: 2rem;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    color: #111;
}

.auth-logo {
    width: 64px; height: 64px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
}

/* ---------- Main Layout ---------- */
.chat-app {
    display: flex;
    height: 100vh;
    max-width: 1400px;
    margin: 0 auto;
    background: var(--card-bg);
    box-shadow: 0 0 20px rgba(0,0,0,0.15);
}

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

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
    gap: 8px;
}

.avatar {
    width: 40px; height: 40px;
    border-radius: 50%;
    object-fit: cover;
    background: #adb5bd;
    flex-shrink: 0;
}

.avatar-upload-wrap {
    position: relative;
    cursor: pointer;
}

.avatar-edit-badge {
    position: absolute;
    bottom: -2px; right: -2px;
    width: 18px; height: 18px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    font-size: 0.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--header-bg);
}

.btn-icon {
    width: 36px; height: 36px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: var(--text-muted);
    background: transparent;
    border: none;
}

.btn-icon:hover {
    background: var(--hover-bg);
    color: var(--text);
}

.search-box {
    position: relative;
    padding: 8px 12px;
    background: var(--header-bg);
}

.search-box i {
    position: absolute;
    left: 24px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
}

.search-box input {
    width: 100%;
    padding: 8px 12px 8px 36px;
    border: none;
    border-radius: 8px;
    background: var(--input-bg);
    color: var(--text);
    outline: none;
    font-size: 0.95rem;
}

.sidebar-tabs {
    border-bottom: 1px solid var(--border);
    background: var(--header-bg);
}

.sidebar-tabs .nav-link {
    border: none;
    border-radius: 0;
    color: var(--text-muted);
    font-weight: 500;
    padding: 10px;
}

.sidebar-tabs .nav-link.active {
    color: var(--primary);
    border-bottom: 2px solid var(--primary);
    background: transparent;
}

.user-list {
    flex: 1;
    overflow-y: auto;
}

.user-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}

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

.status-dot {
    position: absolute;
    bottom: 0; right: 0;
    width: 12px; height: 12px;
    border-radius: 50%;
    border: 2px solid var(--sidebar-bg);
    background: #adb5bd;
}

.status-dot.online { background: #25d366; }

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

.user-info .name {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text);
}

.user-info .preview {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-meta { text-align: right; flex-shrink: 0; }

.user-meta .time {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.badge-unread {
    background: var(--badge);
    color: #fff;
    font-size: 0.7rem;
    padding: 2px 6px;
    border-radius: 10px;
    margin-top: 4px;
    display: inline-block;
}

.group-badge {
    font-size: 0.65rem;
    background: var(--primary);
    color: #fff;
    padding: 1px 5px;
    border-radius: 4px;
    margin-left: 4px;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    position: relative;
}

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

.empty-icon { font-size: 4rem; opacity: 0.4; margin-bottom: 1rem; }

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

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

.back-btn { font-size: 1.3rem; color: var(--text-muted); padding: 0; }

.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.message {
    max-width: 65%;
    padding: 8px 12px;
    border-radius: 8px;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 1px 1px rgba(0,0,0,0.08);
}

.message.mine {
    align-self: flex-end;
    background: var(--msg-out);
    border-top-right-radius: 0;
    color: var(--text);
}

.message.theirs {
    align-self: flex-start;
    background: var(--msg-in);
    border-top-left-radius: 0;
    color: var(--text);
}

.message .sender-name {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 2px;
}

.message .text { font-size: 0.95rem; line-height: 1.4; }

.message .meta {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-align: right;
    margin-top: 2px;
}

.typing-indicator {
    padding: 4px 20px;
    display: flex;
    align-items: center;
    gap: 4px;
    color: var(--text-muted);
}

.typing-indicator span {
    width: 8px; height: 8px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.2s infinite;
}

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

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

.message-form {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--header-bg);
    border-top: 1px solid var(--border);
}

.message-form input {
    flex: 1;
    border: none;
    border-radius: 24px;
    padding: 10px 18px;
    background: var(--input-bg);
    color: var(--text);
    outline: none;
    font-size: 0.95rem;
}

.send-btn {
    width: 44px; height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s;
}

.send-btn:hover { background: var(--primary-dark); }

/* Modal */
.modal-content {
    background: var(--card-bg);
    color: var(--text);
    border-color: var(--border);
}

.modal-header, .modal-footer {
    border-color: var(--border);
}

.member-list {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px;
}

.member-list .form-check {
    padding: 6px 8px 6px 28px;
}

/* Responsive */
@media (max-width: 767.98px) {
    .sidebar { width: 100%; min-width: 100%; }
    .chat-area {
        display: none;
        position: absolute;
        inset: 0;
        z-index: 10;
    }
    .chat-app.show-chat .sidebar { display: none; }
    .chat-app.show-chat .chat-area { display: flex; }
    .message { max-width: 85%; }
}

/* Message timestamp on hover */
.message .meta {
    opacity: 0.7;
    transition: opacity 0.15s;
}
.message:hover .meta {
    opacity: 1;
}
.message .meta .full-time {
    display: none;
}
.message:hover .meta .short-time {
    display: none;
}
.message:hover .meta .full-time {
    display: inline;
}

/* Group avatar badge in chat header */
#chatAvatarContainer .avatar-edit-badge {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 18px;
    height: 18px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    font-size: 0.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--header-bg);
    margin: 0;
    padding: 0;
}

#leaveGroupBtn {
    white-space: nowrap;
    font-size: 0.8rem;
}

.safety-number {
    background: var(--input-bg);
    border: 1px solid var(--border);
    font-size: 0.95rem;
    line-height: 1.8;
    letter-spacing: 0.05em;
    word-break: break-all;
    user-select: all;
}

/* Reply bar */
.reply-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--header-bg);
    border-top: 1px solid var(--border);
}
.reply-bar-content { flex: 1; min-width: 0; border-left: 3px solid var(--primary); padding-left: 8px; }
.reply-bar-text { font-size: 0.85rem; color: var(--text-muted); }

.attach-btn {
    width: 40px; height: 40px;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-muted); cursor: pointer; border-radius: 50%;
}
.attach-btn:hover { background: var(--hover-bg); color: var(--text); }

/* Message extras */
.message .reply-quote {
    border-left: 3px solid var(--primary);
    padding: 4px 8px;
    margin-bottom: 4px;
    background: rgba(0,0,0,0.05);
    border-radius: 4px;
    font-size: 0.8rem;
    color: var(--text-muted);
}
[data-theme="dark"] .message .reply-quote { background: rgba(255,255,255,0.05); }

.message .media-img {
    max-width: 100%;
    max-height: 240px;
    border-radius: 8px;
    margin-bottom: 4px;
    cursor: pointer;
}
.message .media-file {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: rgba(0,0,0,0.05);
    border-radius: 8px;
    font-size: 0.85rem;
    text-decoration: none;
    color: var(--text);
}
.message .ticks { font-size: 0.7rem; margin-left: 4px; opacity: 0.8; }
.message .ticks.read { color: #53bdeb; }

.message-actions {
    position: absolute;
    top: -8px;
    right: 4px;
    display: none;
    gap: 2px;
}
.message:hover .message-actions { display: flex; }
.message-actions button {
    border: none;
    background: var(--header-bg);
    border-radius: 50%;
    width: 28px; height: 28px;
    font-size: 0.75rem;
    color: var(--text-muted);
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.message.deleted .text { font-style: italic; opacity: 0.6; }

.reactions-box {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}
.reaction-chip {
    border: 1px solid var(--border);
    background: var(--input-bg);
    border-radius: 12px;
    padding: 1px 6px;
    font-size: 0.75rem;
    cursor: pointer;
    color: var(--text);
}
.reaction-chip.mine {
    border-color: var(--primary);
    background: rgba(13, 110, 253, 0.12);
}
.react-picker, .emoji-picker {
    position: absolute;
    z-index: 1000;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 6px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    max-width: 220px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}
.emoji-picker {
    position: absolute;
    bottom: 56px;
    left: 48px;
}
.react-picker button, .emoji-picker button {
    border: none;
    background: transparent;
    font-size: 1.25rem;
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
}
.react-picker button:hover, .emoji-picker button:hover {
    background: var(--hover-bg);
}
.edited-label {
    font-size: 0.65rem;
    font-style: italic;
    color: var(--text-muted);
    margin-right: 4px;
}
.message-form { position: relative; }

.chat-search-bar {
    padding: 8px 12px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
    position: relative;
}
.chat-search-results {
    max-height: 200px;
    overflow-y: auto;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-top: 4px;
}
.chat-search-results .item {
    padding: 8px 10px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    font-size: 0.85rem;
}
.chat-search-results .item:hover { background: var(--hover-bg); }

.profile-panel {
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 300px;
    max-width: 100%;
    background: var(--sidebar-bg);
    border-left: 1px solid var(--border);
    z-index: 20;
    display: flex;
    flex-direction: column;
}
.profile-panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
}
.profile-panel-body { padding: 16px; }
.avatar-lg { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; }

.status-item {
    display: flex;
    gap: 10px;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
}
.status-item img { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; }
.status-ring { box-shadow: 0 0 0 2px var(--primary); border-radius: 50%; padding: 2px; }
