/* ============================================
   MedCRM Messenger Page Styles
   ============================================ */

/* Messenger Layout */
.messenger-layout {
    display: flex;
    height: calc(100vh - 65px);
    overflow: hidden;
    background: var(--bg-app);
}

/* ============ Contacts Panel (Left) ============ */
.msg-contacts-panel {
    width: 360px;
    min-width: 320px;
    max-width: 400px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.msg-contacts-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background: linear-gradient(135deg, var(--primary-light) 0%, #f8faff 100%);
}

.msg-contacts-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.msg-contacts-list {
    flex: 1;
    overflow-y: auto;
    scrollbar-width: thin;
}

.msg-contacts-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    color: var(--text-muted);
    padding: 3rem 1rem;
    font-size: 0.9rem;
}

/* Contact Card */
.msg-contact-card {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 1rem 1.25rem;
    cursor: pointer;
    border-bottom: 1px solid #f1f5f9;
    transition: background 0.15s ease;
    position: relative;
}

.msg-contact-card:hover {
    background: #f8fafc;
}

.msg-contact-card.active {
    background: var(--primary-light);
    border-left: 3px solid var(--primary-color);
}

.msg-contact-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color), #818cf8);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    font-weight: 700;
    flex-shrink: 0;
    text-transform: uppercase;
}

.msg-contact-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.msg-contact-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-main);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-contact-preview {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.msg-contact-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.35rem;
    flex-shrink: 0;
}

.msg-contact-time {
    font-size: 0.7rem;
    color: var(--text-light);
    white-space: nowrap;
}

.msg-contact-unread {
    background: var(--primary-color);
    color: white;
    font-size: 0.65rem;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
}

.msg-contact-card.has-unread .msg-contact-name {
    font-weight: 800;
}

.msg-contact-card.has-unread .msg-contact-preview {
    color: var(--text-main);
    font-weight: 500;
}

/* No contacts */
.msg-no-contacts {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
    text-align: center;
}

.msg-no-contacts i {
    font-size: 2.5rem;
    color: var(--text-light);
    margin-bottom: 1rem;
}

/* ============ Chat Panel (Right) ============ */
.msg-chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #f8fafc;
}

/* Empty State */
.msg-chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-light);
    text-align: center;
    padding: 2rem;
}

.msg-chat-empty i {
    font-size: 4rem;
    margin-bottom: 1.5rem;
    opacity: 0.4;
}

.msg-chat-empty h3 {
    font-size: 1.2rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.msg-chat-empty p {
    font-size: 0.9rem;
}

/* Messages Area */
.msg-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    scrollbar-width: thin;
}

/* Chat Bubbles */
.msg-bubble {
    max-width: 70%;
    padding: 0.75rem 1rem;
    border-radius: 18px;
    font-size: 0.9rem;
    line-height: 1.45;
    position: relative;
    animation: msgFadeIn 0.2s ease;
    word-wrap: break-word;
}

@keyframes msgFadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.msg-bubble.from-patient {
    align-self: flex-start;
    background: white;
    color: var(--text-main);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.msg-bubble.from-doctor {
    align-self: flex-end;
    background: var(--primary-color);
    color: white;
    border-bottom-right-radius: 4px;
}

.msg-bubble .msg-time {
    display: block;
    font-size: 0.65rem;
    margin-top: 0.35rem;
    opacity: 0.6;
    text-align: right;
}

.msg-bubble.from-patient .msg-time {
    color: var(--text-light);
}

/* Date separator */
.msg-date-sep {
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-light);
    padding: 0.75rem 0;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Input Area */
.msg-chat-input-area {
    display: flex;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    background: white;
    align-items: center;
}

.msg-chat-input-area input {
    flex: 1;
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 0.8rem 1.25rem;
    font-size: 0.9rem;
    font-family: 'Inter', sans-serif;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.msg-chat-input-area input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.msg-send-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--primary-color);
    color: white;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
    flex-shrink: 0;
}

.msg-send-btn:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
}

.msg-send-btn:active {
    transform: scale(0.95);
}

/* Chat Header in Topbar */
.msg-chat-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex: 1;
}

.msg-chat-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color), #818cf8);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    flex-shrink: 0;
}

.msg-chat-header-info {
    display: flex;
    flex-direction: column;
    cursor: pointer;
}

.msg-chat-header-name {
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--text-main);
}

.msg-chat-header-name:hover {
    color: var(--primary-color);
    text-decoration: underline;
}

.msg-chat-header-sub {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Back Button (mobile) */
.msg-back-btn {
    display: none;
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--primary-color);
    cursor: pointer;
    padding: 0.5rem;
    margin-right: 0.5rem;
    border-radius: 8px;
    transition: background 0.15s;
}

.msg-back-btn:hover {
    background: var(--primary-light);
}

/* ============ Patient Card Modal ============ */
.msg-patient-modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.msg-patient-modal-overlay.active {
    display: flex;
}

.msg-patient-modal {
    background: white;
    border-radius: 20px;
    width: 100%;
    max-width: 600px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
    animation: modalFadeIn 0.25s ease;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(10px);
    }

    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.msg-patient-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.msg-patient-modal-header h3 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--primary-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.msg-patient-modal-close {
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--text-muted);
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.msg-patient-modal-close:hover {
    background: #fee2e2;
    color: var(--danger-color);
}

.msg-patient-modal-body {
    padding: 1.5rem;
    overflow-y: auto;
    flex: 1;
}

/* Patient modal sections */
.msg-modal-section {
    margin-bottom: 1.25rem;
}

.msg-modal-section-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--primary-color);
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.msg-modal-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
}

.msg-modal-field {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.msg-modal-field-label {
    font-size: 0.7rem;
    color: var(--text-light);
    font-weight: 500;
}

.msg-modal-field-value {
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-main);
}

.msg-modal-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.65rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}

.msg-modal-badge.green {
    background: var(--success-bg);
    color: var(--success-color);
}

.msg-modal-badge.red {
    background: var(--danger-bg);
    color: var(--danger-color);
}

.msg-modal-badge.orange {
    background: var(--warning-bg);
    color: var(--warning-color);
}

.msg-modal-badge.indigo {
    background: var(--primary-light);
    color: var(--primary-color);
}

/* ============ Mobile Responsive ============ */
@media screen and (max-width: 768px) {
    .messenger-layout {
        position: relative;
    }

    .msg-contacts-panel {
        width: 100%;
        max-width: 100%;
        min-width: 100%;
        position: absolute;
        inset: 0;
        z-index: 2;
        transition: transform 0.3s ease;
    }

    .msg-chat-panel {
        position: absolute;
        inset: 0;
        z-index: 1;
    }

    /* When chat is open on mobile */
    .messenger-layout.chat-open .msg-contacts-panel {
        transform: translateX(-100%);
    }

    .messenger-layout.chat-open .msg-chat-panel {
        z-index: 3;
    }

    .messenger-layout.chat-open .msg-back-btn {
        display: flex !important;
    }

    .messenger-layout.chat-open #topbar-search {
        display: none !important;
    }

    .messenger-layout.chat-open #msg-chat-header {
        display: flex !important;
    }

    .msg-bubble {
        max-width: 85%;
    }

    .msg-patient-modal {
        max-width: 100%;
        border-radius: 16px;
        max-height: 90vh;
    }

    .msg-modal-grid {
        grid-template-columns: 1fr;
    }

    /* Hide search from topbar on mobile — it's in the contacts header */
    .topbar .search-container {
        display: none;
    }

    .topbar .user-info {
        display: none;
    }
}

/* Desktop: hide mobile-only elements */
@media screen and (min-width: 769px) {
    .msg-back-btn {
        display: none !important;
    }

    #msg-chat-header {
        display: flex !important;
    }
}