/* --- MESSAGES SPECIFIC LAYOUT --- */
.messages-main {
    flex: 1;
    display: flex;
    max-width: 985px; 
    border-right: 1px solid var(--border-line);
    /* Modern dynamic viewport height */
    height: 100dvh;
    overflow: hidden;
    position: relative;
    box-sizing: border-box;
}

/* ANTI-FLICKER CLASS FOR LOAD TIME */
.messages-main.no-transition .chat-section {
    transition: none !important;
}

/* LEFT SIDE: INBOX */
.inbox-section {
    width: 350px;
    border-right: 1px solid var(--border-line);
    display: flex;
    flex-direction: column;
    height: 100%;
    background-color: var(--bg-app);
}

.inbox-header {
    padding: 1.2rem 1.5rem;
    border-bottom: 1px solid var(--border-line);
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.inbox-header h2 { font-weight: 800; font-size: 1.5rem; letter-spacing: -0.5px; margin: 0;}

.inbox-search-container {
    display: flex; align-items: center; background-color: var(--bg-hover); 
    padding: 0.6rem 1rem; border-radius: 2rem;
}
.inbox-search-container svg { width: 16px; height: 16px; color: var(--text-muted); margin-right: 0.5rem; }
.inbox-search-container input { border: none; background: transparent; outline: none; font-size: 0.95rem; color: var(--text-primary); font-weight: 500; width: 100%; }

.inbox-list {
    flex: 1;
    overflow-y: auto;
    scrollbar-width: thin;
}

.inbox-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 1rem 1.5rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border-line);
    transition: background-color 0.2s;
}

.inbox-item:hover { background-color: var(--bg-hover); }
.inbox-item.active { background-color: var(--bg-secondary); border-right: 3px solid var(--brand-color); }

.inbox-avatar {
    width: 50px; height: 50px; border-radius: 50%; object-fit: cover;
    background-color: var(--brand-color); color: var(--bg-app);
    display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 1.2rem; flex-shrink: 0;
}

.inbox-details { flex: 1; overflow: hidden; display: flex; flex-direction: column; gap: 4px;}
.inbox-name-row { display: flex; justify-content: space-between; align-items: center; }
.inbox-name { font-weight: 800; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-primary); }
.inbox-time { font-size: 0.8rem; color: var(--text-muted); flex-shrink: 0; margin-left: 8px;}
.inbox-preview { font-size: 0.9rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500;}
.inbox-preview.unread { color: var(--text-primary); font-weight: 700; }

/* RIGHT SIDE: CHAT */
.chat-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    background-color: var(--bg-app);
    position: relative;
}

.chat-empty-state {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; padding: 2rem; color: var(--text-primary);
}
.chat-empty-state svg { width: 64px; height: 64px; margin-bottom: 1rem; color: var(--text-muted); }
.chat-empty-state h2 { font-weight: 800; font-size: 2rem; margin-bottom: 0.5rem; }
.chat-empty-state p { color: var(--text-muted); font-size: 1rem; max-width: 300px; line-height: 1.5; }

.chat-active-container { 
    display: flex; 
    flex-direction: column; 
    height: 100%; 
    width: 100%; 
    overflow: hidden; 
}

/* Chat Header */
.chat-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0.8rem 1.5rem; border-bottom: 1px solid var(--border-line);
    background-color: var(--bg-app); z-index: 10; flex-shrink: 0;
}

.back-btn { display: none; background: none; border: none; color: var(--text-primary); cursor: pointer; padding: 0.5rem; margin-right: 0.5rem; margin-left: -1rem;}
.back-btn svg { width: 24px; height: 24px; }

.chat-header-user { display: flex; align-items: center; gap: 12px; cursor: pointer; flex: 1;}
.chat-header-avatar {
    width: 44px; height: 44px; border-radius: 50%; object-fit: cover;
    background-color: var(--brand-color); color: var(--bg-app);
    display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 1.1rem;
}
.chat-header-info { display: flex; flex-direction: column; }
.chat-header-name { font-weight: 800; font-size: 1.05rem; line-height: 1.2; color: var(--text-primary);}
.chat-header-status { font-size: 0.8rem; color: var(--text-muted); font-weight: 500;}
.chat-header-status.online { color: #00ba7c; font-weight: 700;}

/* Options Dropdown */
.chat-header-actions { position: relative; }
.icon-btn { background: none; border: none; color: var(--text-primary); cursor: pointer; padding: 0.5rem; border-radius: 50%; transition: background 0.2s; }
.icon-btn:hover { background-color: var(--bg-hover); }
.icon-btn svg { width: 20px; height: 20px; }

.options-dropdown {
    position: absolute; right: 0; top: 100%; margin-top: 0.5rem;
    background-color: var(--bg-app); border: 1px solid var(--border-line);
    border-radius: 0.5rem; box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    min-width: 150px; display: none; z-index: 100; overflow: hidden;
}
.options-dropdown.active { display: block; }
.options-dropdown button {
    display: block; width: 100%; text-align: left; padding: 0.8rem 1rem;
    background: none; border: none; cursor: pointer; font-size: 0.95rem;
    font-weight: 600; color: var(--text-primary); transition: background 0.2s;
}
.options-dropdown button:hover { background-color: var(--bg-hover); }
.options-dropdown button.danger { color: var(--error); }

/* Chat Messages */
.chat-messages {
    flex: 1; padding: 1.5rem; overflow-y: auto; display: flex; flex-direction: column; gap: 1rem;
    background-color: var(--bg-app); scrollbar-width: thin;
}

.msg-wrapper { display: flex; flex-direction: column; max-width: 75%; }
.msg-wrapper.sent { align-self: flex-end; align-items: flex-end; }
.msg-wrapper.received { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
    padding: 0.75rem 1rem; border-radius: 1.2rem; font-size: 0.95rem; line-height: 1.4; font-weight: 500; word-wrap: break-word;
}
.msg-wrapper.sent .msg-bubble {
    background-color: var(--brand-color); color: var(--bg-app); border-bottom-right-radius: 4px;
}
.msg-wrapper.received .msg-bubble {
    background-color: var(--bg-hover); color: var(--text-primary); border-bottom-left-radius: 4px;
}

.msg-time { 
    display: flex; 
    align-items: center; 
    gap: 4px;
    font-size: 0.7rem; 
    color: var(--text-muted); 
    margin-top: 4px; 
    font-weight: 500;
}
.msg-tick { color: var(--text-muted); }
.msg-tick.read { color: #348dff; }

/* Chat Input */
.chat-input-area {
    flex-shrink: 0; 
    padding: 1rem 1.5rem; 
    border-top: 1px solid var(--border-line); 
    background-color: var(--bg-app);
    width: 100%;
    box-sizing: border-box;
}
#chat-input {
    flex: 1; background-color: var(--bg-hover); border: none; border-radius: 2rem;
    padding: 0.8rem 1.2rem; font-size: 0.95rem; color: var(--text-primary); outline: none; font-weight: 500;
    min-width: 0;
}
#btn-send-message {
    background-color: var(--brand-color); color: var(--bg-app); border: none;
    border-radius: 50%; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
    cursor: pointer; transition: opacity 0.2s; flex-shrink: 0;
}
#btn-send-message svg { width: 18px; height: 18px; margin-left: -2px; }
#btn-send-message:disabled { opacity: 0.5; cursor: not-allowed; }

.chat-blocked-area {
    padding: 1.5rem; border-top: 1px solid var(--border-line); background-color: var(--bg-secondary);
    text-align: center; color: var(--text-muted); font-weight: 600; display: flex; flex-direction: column; align-items: center; gap: 0.5rem;
}

.btn-outline { background-color: transparent; color: var(--text-primary); border: 1px solid var(--border-line); cursor: pointer; transition: all 0.2s; }
.btn-outline:hover { background-color: var(--bg-hover); }

/* Unread Navbar Badge */
.nav-item { position: relative; }

.nav-unread-badge {
    position: absolute;
    top: 4px;
    left: 24px;
    background-color: var(--error);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 800;
    min-width: 18px;
    height: 18px;
    border-radius: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--bg-app);
    padding: 0 4px;
    box-sizing: border-box;
}

/* Toast */
.toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(100px);
    background-color: var(--text-primary); color: var(--bg-app); padding: 0.8rem 1.5rem;
    border-radius: 2rem; font-weight: 600; font-size: 0.9rem; z-index: 3000;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1); opacity: 0;
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* =========================================================
   MOBILE NATIVE LOCK (Fixes Viewport & Keyboard Flicker)
========================================================= */
@media (max-width: 768px) {
    
    /* REMOVED html/body lock: Allows native keyboard behavior to function properly */

    .messages-main { 
        position: relative;
        height: 100dvh;
        /* Matches exactly with your bottom navigation bar */
        padding-bottom: 65px; 
    } 
    
    .inbox-section { 
        width: 100%; 
        border-right: none; 
        height: 100%; 
    }
    
    .chat-section {
        /* CRITICAL FIX: Changed from absolute to fixed. */
        /* This mathematically forces the chat window to stop above the navbar, leaving the input field exposed */
        position: fixed; 
        top: 0; 
        left: 0; 
        width: 100%; 
        bottom: 65px; /* Stop EXACTLY where the navbar starts */
        height: auto; 
        background-color: var(--bg-app); /* Ensures background is solid over inbox */
        
        transform: translateX(100%); 
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 900; 
    }
    
    .messages-main.chat-open .chat-section { transform: translateX(0); }
    .back-btn { display: block; }
    
    .nav-unread-badge { top: 2px; left: 50%; margin-left: 2px; }
    
    /* Adds safe area padding for modern iPhones with the gesture bar */
    .chat-input-area { 
        padding: 0.8rem 1rem calc(0.8rem + env(safe-area-inset-bottom, 0px)); 
    }
}

@keyframes shimmer {
    0% { background-position: -1000px 0; }
    100% { background-position: 1000px 0; }
}

.skeleton {
    background: var(--bg-hover);
    background-image: linear-gradient(90deg, var(--bg-hover) 0px, var(--border-line) 40px, var(--bg-hover) 80px);
    background-size: 1000px 100%;
    animation: shimmer 1.5s infinite linear forwards;
    border-radius: 4px;
}