:root {
    --bg-color: #0f172a;
    --chat-bg: #1e293b;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --sent-bg: linear-gradient(135deg, #3b82f6, #8b5cf6);
    --received-bg: #334155;
    --input-bg: #0f172a;
    --border-color: rgba(255, 255, 255, 0.1);
    --online-color: #10b981;
}

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

body {
    font-family: 'Cairo', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.chat-container {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    background-color: var(--chat-bg);
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

@media (min-width: 768px) {
    .chat-container {
        width: 90%;
        max-width: 1000px;
        height: 90vh;
        border-radius: 24px;
        border: 1px solid var(--border-color);
        overflow: hidden;
    }
}

/* Header */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 15px;
    background-color: rgba(30, 41, 59, 0.95);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border-color);
    z-index: 10;
    max-height: 60px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 15px;
}

.avatar {
    position: relative;
    width: 38px;
    height: 38px;
}

.avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--border-color);
}

.status-dot {
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid var(--chat-bg);
    background-color: #64748b;
    transition: background-color 0.3s ease;
}

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

.details {
    min-width: 0;
}

.details h2 {
    font-size: 16px;
    font-weight: 700;
    margin: 0 0 4px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

.status-text {
    font-size: 12px;
    color: var(--text-secondary);
    transition: color 0.3s ease;
}

.status-text.typing {
    color: #3b82f6;
    font-weight: 600;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: color 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.icon-btn.active svg {
    fill: currentColor;
}

/* --- Search UI --- */
.search-input {
    width: 0;
    opacity: 0;
    border: none;
    background: var(--input-bg);
    color: var(--text-primary);
    border-radius: 20px;
    padding: 0;
    transition: all 0.3s ease;
    font-size: 13px;
    outline: none;
}
.search-input:not(.hidden) {
    width: 120px;
    opacity: 1;
    padding: 5px 10px;
    margin-right: 5px;
}
.highlight {
    background-color: rgba(255, 235, 59, 0.4);
    border-radius: 3px;
    padding: 0 2px;
}
/* ----------------- */

/* --- Link Previews --- */
.link-preview-container {
    margin-top: 8px;
    width: 100%;
}
.lp-card {
    display: flex;
    flex-direction: column;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 12px;
    overflow: hidden;
    text-decoration: none !important;
    color: inherit;
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: background 0.2s;
}
.lp-card:hover {
    background: rgba(0, 0, 0, 0.15);
}
.message.received .lp-card {
    background: rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.1);
}
.lp-img img {
    width: 100%;
    height: 120px;
    object-fit: cover;
    display: block;
}
.lp-content {
    padding: 8px 10px;
}
.lp-title {
    font-weight: bold;
    font-size: 13px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.3;
}
.lp-desc {
    font-size: 11px;
    opacity: 0.8;
    margin-top: 4px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 1.3;
}
/* --------------------- */

/* --- Call UI --- */
.call-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 23, 42, 0.95);
    -webkit-backdrop-filter: blur(15px);
    backdrop-filter: blur(15px);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
}
.call-overlay.hidden { display: none !important; }

.call-box {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    animation: fadeIn 0.3s;
}
.call-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    overflow: hidden;
    border: 3px solid rgba(255,255,255,0.2);
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}
.call-avatar img { width: 100%; height: 100%; object-fit: cover; }
.call-box h3 { font-size: 24px; margin: 10px 0 5px; }
.call-box p { font-size: 16px; opacity: 0.7; }
.call-timer { font-size: 20px; font-weight: bold; margin-top: 10px; font-variant-numeric: tabular-nums; }

.call-actions {
    display: flex;
    gap: 30px;
    margin-top: 30px;
}
.call-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    color: white;
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
    transition: transform 0.2s, background 0.2s;
}
.call-btn:active { transform: scale(0.9); }
.call-btn.accept { background: #22c55e; animation: pulseCall 1.5s infinite; }
.call-btn.reject { background: #ef4444; }
.call-btn.secondary { background: rgba(255,255,255,0.2); }
.call-btn.secondary.muted { background: #f59e0b; color: white; }

@keyframes pulseCall {
    0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); }
    70% { box-shadow: 0 0 0 20px rgba(34, 197, 94, 0); }
    100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}
/* ---------------- */

/* Chat Body */
.chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    scroll-behavior: smooth;
}

.chat-body::-webkit-scrollbar {
    width: 6px;
}

.chat-body::-webkit-scrollbar-thumb {
    background-color: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
}

/* ========================================= */
/* MESSAGES - WHATSAPP / TELEGRAM STYLE      */
/* ========================================= */

.message {
    display: flex;
    align-items: flex-end;
    /* Avatar aligned to bottom */
    position: relative;
    max-width: 85%;
    animation: fadeIn 0.3s ease forwards;
    gap: 5px;
    /* Space between bubble and avatar */
}

@media (min-width: 768px) {
    .message {
        max-width: 60%;
    }
}

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

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

/* My messages (Sent) - Align to the Right edge */
.message.sent {
    align-self: flex-start;
    /* In RTL, flex-start is the physical RIGHT */
    flex-direction: row-reverse;
    /* In RTL: Avatar on Right, Bubble on Left */
}

/* Other messages (Received) - Align to the Left edge */
.message.received {
    align-self: flex-end;
    /* In RTL, flex-end is the physical LEFT */
    flex-direction: row;
    /* In RTL: Avatar on Left, Bubble on Right */
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    border: 1px solid var(--border-color);
    flex-shrink: 0;
}

.message-content {
    padding: 6px 10px;
    border-radius: 18px;
    position: relative;
    font-size: 14px;
    line-height: 1.5;
    z-index: 2;
    display: flex;
    flex-direction: column;
}

/* Style for Sent Bubble */
.message.sent .message-content {
    background: var(--sent-bg);
    color: #fff;
    border-bottom-right-radius: 4px;
    /* WhatsApp style tail */
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.2);
}

/* Style for Received Bubble */
.message.received .message-content {
    background: var(--received-bg);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
    /* WhatsApp style tail */
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

/* Time text inside the bubble */
.time {
    display: block;
    font-size: 10px;
    margin-top: 2px;
    opacity: 0.7;
    text-align: left;
}

.message.sent .time {
    text-align: right;
}

/* Message Options Menu (Reply / Delete) */
.message-options {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: 5px;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    z-index: 10;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.message:hover .message-options {
    opacity: 1;
    visibility: visible;
}

/* Sent messages: Options menu should appear on the LEFT of the bubble */
.message.sent .message-options {
    right: 100%;
    /* Push to physical LEFT in RTL */
    margin-right: 10px;
}

/* Received messages: Options menu should appear on the RIGHT of the bubble */
.message.received .message-options {
    left: 100%;
    /* Push to physical RIGHT in RTL */
    margin-left: 10px;
}

.option-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 16px;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.option-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

.option-btn.delete:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* ========================================= */
/* TYPING & INPUT                            */
/* ========================================= */

.typing-indicator {
    align-self: flex-end;
    /* Left in RTL */
    background: var(--received-bg);
    padding: 12px 16px;
    border-radius: 20px;
    border-top-left-radius: 4px;
    display: flex;
    gap: 4px;
    align-items: center;
    width: fit-content;
    margin-bottom: 10px;
}

.typing-indicator.hidden {
    display: none;
}

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

.typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

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

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Footer */
.chat-footer {
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 5px));
    background-color: rgba(30, 41, 59, 0.95);
    border-top: 1px solid var(--border-color);
}

.input-wrapper {
    display: flex;
    align-items: center;
    background-color: var(--input-bg);
    border-radius: 30px;
    padding: 6px 12px;
    gap: 12px;
    border: 1px solid var(--border-color);
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.input-wrapper:focus-within {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

#messageInput {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 15px;
    padding: 10px 0;
    outline: none;
    resize: none;
    max-height: 120px;
    overflow-y: auto;
    line-height: 1.4;
    box-sizing: border-box;
}

#messageInput::placeholder {
    color: var(--text-secondary);
}

.send-btn {
    background: var(--sent-bg);
    color: #fff;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    flex-shrink: 0;
}

.send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
}

.send-icon {
    /* Flip paper plane horizontally for RTL */
    transform: scaleX(-1);
    margin-right: 2px;
}

.mic-btn {
    background: transparent;
    color: var(--text-secondary);
    transition: all 0.3s ease;
    width: 42px;
    height: 42px;
}

.mic-btn:hover {
    color: var(--sent-bg);
}

.mic-btn.recording {
    background-color: #ef4444;
    color: white;
    border-radius: 50%;
    animation: pulseMicTelegram 1.5s infinite;
    z-index: 10;
}

@keyframes pulseMicTelegram {
    0% { transform: scale(1.2); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
    50% { transform: scale(1.35); box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
    100% { transform: scale(1.2); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

@keyframes pulseMic {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.1); opacity: 0.7; }
    100% { transform: scale(1); opacity: 1; }
}

.recording-timer {
    display: none;
    align-items: center;
    gap: 10px;
    color: #ef4444;
    font-weight: 700;
    font-size: 15px;
    margin-right: 10px;
    width: 100%;
}

.recording-timer.active {
    display: flex;
    animation: scaleIn 0.3s ease forwards;
}

.recording-timer::after {
    content: "« نقرة للإنهاء";
    color: var(--text-secondary);
    font-weight: normal;
    font-size: 13px;
    margin-right: auto;
    animation: slideHint 2s infinite ease-in-out;
}

@keyframes slideHint {
    0% { transform: translateX(0); opacity: 0.4; }
    50% { transform: translateX(-5px); opacity: 0.8; }
    100% { transform: translateX(0); opacity: 0.4; }
}

.recording-dot {
    width: 10px;
    height: 10px;
    background-color: #ef4444;
    border-radius: 50%;
    animation: pulseMic 1.5s infinite;
}


/* ========================================= */
/* LOGIN OVERLAY                             */
/* ========================================= */

.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.login-card {
    background-color: rgba(30, 41, 59, 0.7);
    -webkit-backdrop-filter: blur(15px);
    backdrop-filter: blur(15px);
    border: 1px solid var(--border-color);
    padding: 40px;
    border-radius: 24px;
    text-align: center;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    max-width: 90%;
    width: 400px;
    animation: fadeIn 0.5s ease forwards;
}

.login-card h2 {
    font-size: 24px;
    margin-bottom: 10px;
    color: var(--text-primary);
}

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

.login-card .input-group {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.login-card input {
    background-color: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 15px 20px;
    border-radius: 12px;
    font-family: inherit;
    font-size: 16px;
    outline: none;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.login-card input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.login-card button {
    background: var(--sent-bg);
    color: #fff;
    border: none;
    padding: 15px 20px;
    border-radius: 12px;
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.login-card button:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(59, 130, 246, 0.4);
}

.login-section {
    margin: 15px 0;
    text-align: right;
}

.login-section p {
    font-size: 14px;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.login-divider {
    text-align: center;
    position: relative;
    margin: 20px 0;
}

.login-divider::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--border-color);
    z-index: 1;
}

.login-divider span {
    background: var(--surface-light);
    padding: 0 10px;
    position: relative;
    z-index: 2;
    color: var(--text-secondary);
    font-size: 14px;
}

.restore-btn {
    background: var(--bg-color);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.restore-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* ========================================= */
/* MISC FEATURES                             */
/* ========================================= */

.reply-preview {
    background-color: var(--chat-bg);
    padding: 10px 15px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 12px 12px 0 0;
    margin-bottom: -10px;
    position: relative;
    z-index: 1;
}

.reply-preview.hidden {
    display: none;
}

.reply-content {
    display: flex;
    flex-direction: column;
    border-right: 4px solid #3b82f6;
    /* RTL right border */
    padding-right: 10px;
    flex: 1;
}

.reply-sender {
    font-size: 13px;
    font-weight: 700;
    color: #3b82f6;
}

.reply-text {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90%;
}

.close-reply {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    width: 24px;
    height: 24px;
    padding: 3px;
}

.quote-box {
    background-color: rgba(0, 0, 0, 0.15);
    border-right: 3px solid rgba(255, 255, 255, 0.5);
    padding: 6px 10px;
    border-radius: 6px;
    margin-bottom: 8px;
    font-size: 13px;
    cursor: pointer;
}

.message.received .quote-box {
    background-color: rgba(255, 255, 255, 0.05);
    border-right-color: #3b82f6;
}

.quote-sender {
    font-weight: bold;
    display: block;
    margin-bottom: 2px;
    opacity: 0.9;
}

.quote-text {
    opacity: 0.8;
}

.reaction-popup {
    position: fixed;
    background: rgba(30, 41, 59, 0.95);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    padding: 5px 10px;
    display: flex;
    gap: 5px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    z-index: 9000;
    animation: scaleIn 0.2s ease forwards;
}

.reaction-popup.hidden {
    display: none;
}

@keyframes scaleIn {
    from {
        transform: scale(0.8);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.reaction-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    transition: transform 0.2s;
    padding: 5px;
}

.reaction-btn:hover {
    transform: scale(1.3);
}

.custom-reaction-btn {
    background: #f1f5f9;
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    margin: 0 5px;
}

.custom-reaction-btn:hover {
    background: #e2e8f0;
    color: var(--text-primary);
    transform: scale(1.1);
}

.reaction-bubble {
    position: absolute;
    bottom: -10px;
    right: 15px;
    background: var(--chat-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 2px 6px;
    font-size: 12px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}

.time-receipt-wrapper {
    display: flex;
    align-items: center;
    gap: 5px;
    justify-content: flex-end;
    margin-top: 5px;
}

.message.received .time-receipt-wrapper {
    justify-content: flex-start;
}

.read-receipt {
    display: inline-flex;
    color: rgba(255, 255, 255, 0.65);
    /* Translucent white for sent/delivered */
}

.read-receipt.read {
    color: #34B7F1;
    /* Blue for read */
}

.read-receipt svg {
    width: 16px;
    height: 16px;
    margin-right: 4px;
    /* Space between time and checkmark (RTL means right is towards time) */
}

.secret-badge {
    background-color: rgba(255, 255, 255, 0.1);
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 12px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    transition: background-color 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100px;
}

@media (max-width: 500px) {
    .secret-badge {
        display: none;
    }
}

.logout-btn:hover {
    color: #ef4444;
    background: rgba(239, 68, 68, 0.1);
}

/* --- Sidebar & 1-on-1 Layout --- */
.app-container {
    display: flex;
    width: 100%;
    height: 100vh;
    background: #0f172a;
    overflow: hidden;
}

.sidebar {
    width: 300px;
    background: #1e293b;
    border-left: 1px solid #334155;
    display: flex;
    flex-direction: column;
    transition: all 0.3s ease;
}

.sidebar-header {
    padding: 20px;
    background: #0f172a;
    border-bottom: 1px solid #334155;
}

.sidebar-header h3 {
    margin: 0;
    color: #f8fafc;
    font-size: 1.2rem;
}

.users-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.user-item {
    display: flex;
    align-items: center;
    padding: 12px;
    border-radius: 12px;
    cursor: pointer;
    transition: background 0.2s;
    margin-bottom: 5px;
}

.user-item:hover {
    background: #334155;
}

.user-item.active {
    background: #3b82f6;
}

.user-item .avatar {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    margin-left: 15px;
    background: #475569;
}

.user-item .details h4 {
    margin: 0;
    color: #f8fafc;
    font-size: 1rem;
}

.user-item .details p {
    margin: 3px 0 0;
    color: #cbd5e1;
    font-size: 0.8rem;
}

.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background: #0f172a;
}

.no-chat-selected {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #64748b;
}

.no-chat-selected h2 {
    margin-top: 20px;
    font-size: 1.5rem;
}

.chat-container {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-width: 100%;
    border-radius: 0;
    box-shadow: none;
}

@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        position: absolute;
        z-index: 10;
        height: 100%;
    }

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

    .chat-area {
        width: 100%;
    }

    /* Add a back button for mobile */
    .mobile-back-btn {
        display: block;
        margin-left: 10px;
        background: none;
        border: none;
        color: white;
        cursor: pointer;
    }
}

/* --- Telegram-style Context Menu --- */
.message-context-menu {
    position: fixed;
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    z-index: 9000;
    min-width: 150px;
    overflow: hidden;
    transform: scale(0.95);
    opacity: 0;
    pointer-events: none;
    transition: all 0.15s ease-out;
}

.message-context-menu.show {
    transform: scale(1);
    opacity: 1;
    pointer-events: auto;
}

.menu-item {
    padding: 12px 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: none;
    border: none;
    color: #f8fafc;
    cursor: pointer;
    font-size: 0.95rem;
    font-family: 'Cairo', sans-serif;
    text-align: right;
    transition: background 0.2s;
    width: 100%;
}

.menu-item:hover {
    background: #334155;
}

.menu-item.delete {
    color: #ef4444;
}

.menu-item.delete:hover {
    background: rgba(239, 68, 68, 0.1);
}

.menu-item svg {
    width: 18px;
    height: 18px;
}

/* Dots Button on Message */
.msg-dots-btn {
    background: none;
    border: none;
    color: #94a3b8;
    cursor: pointer;
    padding: 2px;
    border-radius: 50%;
    margin-right: 5px;
    opacity: 0;
    transition: opacity 0.2s;
}

.message:hover .msg-dots-btn {
    opacity: 1;
}

@media (max-width: 768px) {
    .msg-dots-btn {
        opacity: 1;
        /* Always show on mobile */
    }
}

.edited-tag {
    font-size: 0.7rem;
    color: #94a3b8;
    margin-right: 5px;
    font-style: italic;
}

/* Date Separator */
.date-separator {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 14px 0;
    gap: 8px;
}

.date-separator::before,
.date-separator::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
    opacity: 0.5;
}

.date-separator span {
    font-size: 11px;
    color: var(--text-secondary);
    background: var(--chat-bg);
    padding: 3px 10px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    white-space: nowrap;
}

/* Pinned Message Banner */
.pinned-banner {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: rgba(59, 130, 246, 0.12);
    border-bottom: 1px solid rgba(59, 130, 246, 0.3);
    font-size: 13px;
    color: var(--text-secondary);
    cursor: pointer;
}

.pinned-banner span {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-primary);
}

.pinned-banner.hidden {
    display: none;
}

.unpin-btn {
    font-size: 12px;
    color: var(--text-secondary);
    padding: 2px 5px;
}

.unpin-btn:hover {
    color: #ef4444;
}


/* Pin menu item */
.menu-item.pin {
    color: #f59e0b;
}

.menu-item.pin:hover {
    background: rgba(245, 158, 11, 0.1);
}

/* Media Messages */
.msg-image {
    max-width: 100%;
    max-height: 280px;
    border-radius: 12px;
    display: block;
    cursor: pointer;
    margin-bottom: 4px;
    object-fit: cover;
}

.msg-video {
    max-width: 100%;
    max-height: 280px;
    border-radius: 12px;
    display: block;
    margin-bottom: 4px;
}

.msg-audio {
    width: 100%;
    min-width: 220px;
    margin-bottom: 4px;
    border-radius: 8px;
}

.msg-file {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    color: inherit;
    text-decoration: none;
    font-size: 14px;
    margin-bottom: 4px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    transition: background 0.2s;
}

.msg-file:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* ========================================= */
/* IMAGE PREVIEW MODAL                       */
/* ========================================= */

.img-preview-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.img-preview-modal.hidden {
    display: none;
}

/* Simple Preview Overlay */
.simple-preview-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.92);
    backdrop-filter: blur(8px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}
.simple-preview-overlay.hidden { display: none !important; }
.simple-preview-content {
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 16px;
    padding: 20px;
    max-width: min(90vw, 480px);
    width: 100%;
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.simple-preview-content img {
    max-width: 100%;
    max-height: 55vh;
    border-radius: 12px;
    object-fit: contain;
}
.simple-preview-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
}
.btn-preview-cancel, .btn-preview-send {
    padding: 10px 24px;
    border: none;
    border-radius: 20px;
    font-weight: 700;
    cursor: pointer;
    font-family: 'Cairo', sans-serif;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}
.btn-preview-cancel { background: rgba(255,255,255,0.1); color: white; }
.btn-preview-cancel:hover { background: rgba(255,255,255,0.2); }
.btn-preview-send { background: linear-gradient(135deg, #3b82f6, #6366f1); color: white; }
.btn-preview-send:hover { opacity: 0.9; }

.ipm-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.9);
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    z-index: 1;
}

.ipm-container {
    position: relative;
    z-index: 2;
    background: var(--chat-bg);
    width: 90%;
    max-width: 800px;
    height: 90vh;
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border-color);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

.ipm-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    background: rgba(30, 41, 59, 0.8);
}

.ipm-close {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.ipm-close:hover {
    background: #ef4444;
}

.ipm-main {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #000;
    overflow: hidden;
}

.ipm-canvas-wrap {
    position: relative;
    max-width: 100%;
    max-height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ipm-preview-img {
    max-width: 100%;
    max-height: calc(90vh - 180px);
    object-fit: contain;
}

#ipmCanvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
}

.ipm-draw-toolbar {
    position: absolute;
    bottom: 20px;
    background: rgba(30, 41, 59, 0.9);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    padding: 10px 15px;
    border-radius: 30px;
    display: flex;
    gap: 15px;
    align-items: center;
    border: 1px solid var(--border-color);
    z-index: 20;
}

.ipm-tool {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    opacity: 0.7;
    transition: all 0.2s;
    padding: 5px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ipm-tool:hover,
.ipm-tool.active {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
    transform: scale(1.1);
}

#ipmColor {
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    padding: 0;
    overflow: hidden;
}

#ipmColor::-webkit-color-swatch-wrapper {
    padding: 0;
}

#ipmColor::-webkit-color-swatch {
    border: none;
    border-radius: 50%;
}

#ipmSize {
    width: 80px;
    cursor: pointer;
}

.ipm-emoji-picker {
    position: absolute;
    bottom: 80px;
    background: rgba(30, 41, 59, 0.95);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    padding: 15px;
    border-radius: 15px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 10px;
    font-size: 24px;
    border: 1px solid var(--border-color);
    z-index: 20;
    cursor: pointer;
}

.ipm-emoji-picker.hidden {
    display: none;
}

.ipm-thumbs {
    display: flex;
    gap: 10px;
    padding: 15px;
    overflow-x: auto;
    background: var(--chat-bg);
    border-bottom: 1px solid var(--border-color);
}

.ipm-thumbs::-webkit-scrollbar {
    height: 6px;
}

.ipm-thumbs::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 10px;
}

.ipm-thumb {
    position: relative;
    width: 60px;
    height: 60px;
    flex-shrink: 0;
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s;
    opacity: 0.6;
}

.ipm-thumb.active {
    border-color: #3b82f6;
    opacity: 1;
}

.ipm-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.ipm-thumb-del {
    position: absolute;
    top: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    border: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ipm-thumb-del:hover {
    background: #ef4444;
}

.ipm-footer {
    display: flex;
    padding: 15px;
    gap: 15px;
    background: var(--chat-bg);
    align-items: center;
}

.ipm-caption {
    flex: 1;
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 12px 15px;
    border-radius: 20px;
    font-family: inherit;
    font-size: 15px;
    outline: none;
}

.ipm-caption:focus {
    border-color: #3b82f6;
}

.ipm-send {
    background: var(--sent-bg);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 20px;
    font-family: inherit;
    font-size: 15px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.ipm-send:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
}

.ipm-send:disabled {
    opacity: 0.7;
    cursor: not-allowed;
    transform: none;
}

.enc-loading {
    padding: 20px;
    min-height: 100px;
    border: 1px dashed var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 14px;
    color: var(--text-secondary);
}

/* ========================================= */
/* SCROLL TO BOTTOM & UNREAD BADGE           */
/* ========================================= */
.scroll-bottom-btn {
    position: absolute;
    bottom: 80px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    z-index: 100;
    color: var(--text-primary);
    transition: all 0.2s ease;
}

.scroll-bottom-btn:hover {
    background-color: var(--hover-bg);
    transform: translateY(-2px);
}

.scroll-bottom-btn.hidden {
    display: none;
}

.unread-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background-color: #34B7F1;
    color: white;
    font-size: 11px;
    font-weight: bold;
    border-radius: 50%;
    min-width: 20px;
    height: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 5px;
}

.unread-badge.hidden {
    display: none;
}

/* Header Dropdown */
.header-dropdown {
    transition: all 0.3s ease;
    opacity: 1;
    transform: translateY(0);
}
.header-dropdown.hidden {
    display: none !important;
    opacity: 0;
    transform: translateY(-10px);
}
.dropdown-item:hover {
    background: rgba(255, 255, 255, 0.1) !important;
}


/* Chats List Screen */
.chats-list-screen { width: 100vw; height: 100vh; display: flex; flex-direction: column; background: var(--bg-main); }
.chats-list-header { background: var(--bg-secondary); padding: 15px 20px 0; border-bottom: 1px solid var(--border-color); }
.chats-list-header .header-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }
.chats-list-header h2 { color: var(--text-primary); font-size: 24px; }
.chats-tabs { display: flex; gap: 20px; }
.tab-btn { background: none; border: none; color: var(--text-secondary); font-size: 16px; font-family: inherit; padding-bottom: 10px; cursor: pointer; border-bottom: 2px solid transparent; transition: 0.3s; }
.tab-btn.active { color: var(--primary-color); border-bottom-color: var(--primary-color); }
.chats-list-body { flex: 1; overflow-y: auto; padding: 10px; }
.chat-item { display: flex; align-items: center; padding: 12px; border-radius: 12px; cursor: pointer; transition: 0.2s; margin-bottom: 5px; background: rgba(255, 255, 255, 0.02); }
.chat-item:hover { background: rgba(255, 255, 255, 0.05); }
.chat-item .avatar { width: 50px; height: 50px; border-radius: 50%; margin-left: 15px; background: #334155; object-fit: cover; }
.chat-item .details { flex: 1; }
.chat-item h3 { color: var(--text-primary); font-size: 16px; margin-bottom: 3px; display: flex; align-items: center; gap: 5px; }
.chat-item p { color: var(--text-secondary); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .actions { display: flex; align-items: center; gap: 10px; }
.pin-btn { background: none; border: none; color: var(--text-secondary); cursor: pointer; opacity: 0.5; transition: 0.2s; font-size:18px; }
.pin-btn.pinned { opacity: 1; filter: drop-shadow(0 0 5px rgba(255, 255, 255, 0.5)); }
.loading-chats { text-align: center; color: var(--text-secondary); margin-top: 20px; }
