:root {
    /* INTEGRATION: Use Theme Variables with fallbacks */
    --primary: var(--primary, #007bff); /* Zvine OS Blue */
    --primary-hover: #0056b3;
    --bg-light: #F3F4F6;
    --bg-white: #FFFFFF;
    --text-dark: var(--text-main, #343a40);
    --text-gray: #6B7280;
    --border: #E5E7EB;
    --green: #10B981;
    --red: #EF4444;
}

/* --- MAIN LAYOUT --- */
#zvine-app-container {
    display: flex;
    width: 100%;
    /* RESPONSIVE HEIGHT: Fill screen minus header/padding offset */
    height: 750px; 
    max-height: calc(100vh - 120px); 
    margin: 0 auto;
    background: var(--bg-white);
    border-radius: 12px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.05);
    overflow: hidden;
    font-family: 'Inter', sans-serif;
    border: 1px solid var(--border);
}

/* --- SIDEBAR --- */
.zvine-sidebar {
    width: 320px;
    background: #fff;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.zvine-sidebar-header {
    padding: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #FAFAFA;
}

/* User & Search */
.user-profile-trigger { 
    position: relative; 
    cursor: pointer; 
    flex-shrink: 0; /* Prevent squashing */
}

/* FIX: Force dimensions to prevent theme overrides */
.user-avatar-sm { 
    width: 40px !important; 
    height: 40px !important; 
    border-radius: 50%; 
    object-fit: cover; 
    border: 2px solid #fff; 
    box-shadow: 0 2px 4px rgba(0,0,0,0.1); 
    display: block;
}

.status-dot { 
    width: 10px; height: 10px; 
    background: var(--text-gray); 
    border: 2px solid #fff; border-radius: 50%; 
    position: absolute; bottom: 0; right: 0; 
}
.status-dot.online { background: var(--green); }

.icon-btn { 
    background: transparent; border: none; cursor: pointer; 
    color: var(--text-gray); padding: 8px; border-radius: 8px; 
    transition: all 0.2s; display: flex; align-items: center; justify-content: center; 
}
.icon-btn:hover { background: var(--bg-light); color: var(--primary); }

.zvine-search-wrapper { padding: 10px 16px; border-bottom: 1px solid var(--border); }
#user-search { 
    width: 100%; padding: 10px 14px; border-radius: 8px; 
    border: 1px solid var(--border); background: var(--bg-light); outline: none; 
}

/* Tabs */
.zvine-nav-tabs { display: flex; padding: 10px 16px; gap: 10px; }
.nav-tab { 
    flex: 1; padding: 8px; border: none; background: transparent; 
    font-weight: 600; color: var(--text-gray); cursor: pointer; border-radius: 6px; 
}
.nav-tab.active { background: var(--bg-light); color: var(--text-dark); }

/* Contacts List */
.zvine-contacts-scroll { flex: 1; overflow-y: auto; }
.contact-item { 
    display: flex; align-items: center; padding: 12px 16px; 
    cursor: pointer; transition: background 0.1s; 
}
.contact-item:hover { background: var(--bg-light); }
.contact-item.active { background: #EEF2FF; border-left: 3px solid var(--primary); }

.avatar-box { 
    position: relative; 
    margin-right: 12px; 
    flex-shrink: 0; /* KEY FIX: Prevents avatar from turning into an oval */
}

/* FIX: Force dimensions to prevent theme overrides */
.contact-avatar { 
    width: 48px !important; 
    height: 48px !important; 
    border-radius: 50%; 
    object-fit: cover; 
    background: #eee; 
    display: block;
}

.status-indicator { 
    width: 12px; height: 12px; 
    border-radius: 50%; border: 2px solid #fff; 
    position: absolute; bottom: 0; right: 0; background: #ccc; 
}
.status-indicator.online { background: var(--green); }

.contact-info { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.contact-name { 
    font-weight: 600; color: var(--text-dark); font-size: 15px; 
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 
}
.contact-snippet { font-size: 12px; color: var(--text-gray); margin-top: 2px; }
.unread-badge { 
    background: var(--primary); color: #fff; font-size: 11px; 
    padding: 2px 8px; border-radius: 10px; font-weight: bold; 
    flex-shrink: 0; 
}

/* --- CHAT AREA --- */
.zvine-chat-area { flex: 1; display: flex; flex-direction: column; background: #fff; position: relative; }

.zvine-chat-header { 
    height: 65px; padding: 0 15px; 
    display: flex; align-items: center; justify-content: space-between; 
    border-bottom: 1px solid var(--border); 
}
.chat-meta { flex: 1; margin-left: 10px; overflow: hidden; }
.chat-meta h3 { 
    margin: 0; font-size: 16px; font-weight: 700; color: var(--text-dark); 
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 
}
#chat-subtitle { font-size: 12px; color: var(--green); }

.zvine-messages-wrapper { 
    flex: 1; padding: 20px; overflow-y: auto; background: #F9FAFB; 
    display: flex; flex-direction: column; gap: 8px; 
}
.empty-state { margin: auto; text-align: center; color: var(--text-gray); }
.empty-icon { font-size: 40px; margin-bottom: 10px; }

/* Chat Bubbles */
.msg-bubble { 
    max-width: 70%; padding: 10px 14px; border-radius: 12px; 
    font-size: 14px; line-height: 1.5; position: relative; 
    box-shadow: 0 1px 2px rgba(0,0,0,0.05); 
}
.msg-sent { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 2px; }
.msg-received { align-self: flex-start; background: #fff; color: var(--text-dark); border: 1px solid var(--border); border-bottom-left-radius: 2px; }
.msg-meta { 
    font-size: 10px; display: flex; justify-content: flex-end; 
    align-items: center; gap: 4px; margin-top: 4px; opacity: 0.8; 
}

/* Composer */
.zvine-composer { 
    padding: 12px; background: #fff; border-top: 1px solid var(--border); 
    display: flex; gap: 8px; align-items: center; 
}
#msg-input { 
    flex: 1; padding: 10px 15px; border-radius: 20px; 
    border: 1px solid var(--border); background: var(--bg-light); outline: none; 
}
#msg-input:focus { border-color: var(--primary); background: #fff; }
.send-btn { color: var(--primary); }
.voice-btn { color: var(--red); }

/* Voice UI */
#voice-recorder-ui { 
    padding: 15px; background: #FEF2F2; border-top: 1px solid #FCA5A5; 
    display: flex; justify-content: space-between; align-items: center; 
}
.recording-status { display: flex; align-items: center; gap: 10px; color: var(--red); font-weight: bold; }
.pulse-dot { width: 10px; height: 10px; background: var(--red); border-radius: 50%; animation: pulse 1s infinite; }
@keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.5; } 100% { opacity: 1; } }

/* --- MODALS (PREMIUM & RESPONSIVE) --- */
.zvine-modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(2px);
    z-index: 9999; display: flex; align-items: center; justify-content: center;
    opacity: 1; transition: opacity 0.2s;
}
.zvine-modal-overlay.hidden { display: none; opacity: 0; pointer-events: none; }

.zvine-modal-box {
    background: #fff; width: 400px; max-width: 90%; max-height: 90vh;
    overflow-y: auto; border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    transform: scale(1); transition: transform 0.2s; position: relative;
    display: flex; flex-direction: column;
}
.zvine-modal-box.wide { width: 500px; }

.modal-header {
    padding: 16px 20px; border-bottom: 1px solid var(--border);
    display: flex; justify-content: space-between; align-items: center; background: #fff;
    position: sticky; top: 0; z-index: 10;
}
.modal-header h3 { margin: 0; font-size: 18px; color: var(--text-dark); }
.close-modal-btn { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--text-gray); line-height: 1; }
.close-modal-btn:hover { color: var(--red); }

.modal-body { padding: 20px; }

.modal-footer {
    padding: 16px 20px; background: var(--bg-light); border-top: 1px solid var(--border);
    display: flex; justify-content: flex-end; gap: 10px;
}

/* Modal Form Elements */
.text-center { text-align: center; }
.zvine-form-control { width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 6px; box-sizing: border-box; }
.zvine-form-select { width: 60%; padding: 8px; border: 1px solid var(--border); border-radius: 6px; }
.btn-primary { background: var(--primary); color: #fff; border: none; padding: 8px 16px; border-radius: 6px; cursor: pointer; font-weight: 500; }
.btn-secondary { background: var(--bg-light); border: 1px solid var(--border); padding: 8px 16px; border-radius: 6px; cursor: pointer; }
.btn-ghost { background: transparent; border: none; color: var(--text-gray); cursor: pointer; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

/* Profile Avatar Upload Styling */
.avatar-upload-container { 
    position: relative; display: inline-block; margin-bottom: 15px; 
    flex-shrink: 0; 
}
.avatar-lg { 
    width: 100px !important; height: 100px !important; 
    border-radius: 50%; object-fit: cover; 
    border: 3px solid var(--bg-light); display: block; 
}
.avatar-overlay {
    position: absolute; bottom: 0; right: 0;
    background: var(--primary); color: #fff;
    width: 32px; height: 32px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; border: 2px solid #fff; font-size: 14px;
}

/* Group Edit Specifics */
.group-info-edit { display: flex; align-items: center; gap: 15px; margin-bottom: 20px; }
.group-avatar-wrapper { position: relative; flex-shrink: 0; }
.avatar-md { 
    width: 60px !important; height: 60px !important; 
    border-radius: 50%; object-fit: cover; background: #eee; 
}
.avatar-overlay-sm {
    position: absolute; bottom: -5px; right: -5px;
    background: #fff; border: 1px solid var(--border);
    width: 24px; height: 24px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 12px; cursor: pointer; box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.group-name-wrapper { flex: 1; display: flex; gap: 8px; }

.divider { height: 1px; background: var(--border); margin: 20px 0; }
.members-section h4 { margin: 0 0 10px 0; font-size: 14px; color: var(--text-gray); text-transform: uppercase; letter-spacing: 0.5px; }
.add-member-row { display: flex; gap: 10px; margin-bottom: 15px; }
.add-member-row select { flex: 1; }

.members-list-scroll { max-height: 150px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; padding: 5px; }
.member-row { display: flex; justify-content: space-between; padding: 8px; border-bottom: 1px solid var(--border); font-size: 14px; }
.member-row:last-child { border-bottom: none; }
.btn-remove-member { color: var(--red); cursor: pointer; background: none; border: none; font-size: 18px; line-height: 1; }

/* Utility */
.hidden { display: none !important; }

/* --- MOBILE OPTIMIZATION (THEME & MODAL FIXES) --- */
@media (max-width: 768px) {
    /* 1. Main App Container: Full Screen Mode */
    #zvine-app-container {
        /* Expand width to counteract Theme padding */
        width: calc(100% + 30px); 
        margin: -20px -15px 0 -15px;
        /* Fill Viewport Height */
        height: calc(100vh - 110px); 
        max-height: none;
        border: none;
        border-radius: 0;
        display: flex;
        flex-direction: column;
    }

    /* 2. Toggle View Logic (List vs Chat) */
    .zvine-sidebar { width: 100%; height: 100%; display: flex; border-right: none; }
    .zvine-chat-area { width: 100%; height: 100%; display: none; }
    
    /* When Chat is Active */
    #zvine-app-container.mobile-view-active .zvine-sidebar { display: none; }
    #zvine-app-container.mobile-view-active .zvine-chat-area { display: flex; }

    /* 3. Mobile Back Button */
    #btn-back-to-contacts { 
        display: flex !important; margin-right: 5px; 
        background: #f0f0f0; border-radius: 50%; 
        width: 32px; height: 32px; 
        justify-content: center; align-items: center; 
    }
    
    /* 4. Chat Adjustments */
    .msg-bubble { max-width: 85%; }

    /* 5. Responsive Modals */
    .zvine-modal-box, .zvine-modal-box.wide {
        width: 90% !important; 
        max-width: 350px; 
        margin: auto;
    }
    
    /* Group Edit Stacking */
    .group-info-edit { flex-direction: column; text-align: center; }
    .group-name-wrapper { width: 100%; flex-direction: column; }
    .group-name-wrapper button { width: 100%; }
    
    /* Member Add Stacking */
    .add-member-row { flex-direction: column; }
    .zvine-form-select { width: 100%; }
    #btn-add-member { width: 100%; }
}

/* Hide Back button on desktop */
#btn-back-to-contacts { display: none; }