.chat-popup {
    position: fixed;
    bottom: 80px;
    right: 20px;
    width: 300px;
    max-height: 450px;
    border: 1px solid #ddd;
    border-radius: 10px;
    display: none;
    flex-direction: column;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    z-index: 1050;
}


#chat_name {
    color: white;
}

.chat-header {
    background-color: #0d6efd;
    color: white;
    padding: 10px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    cursor: pointer;
}

.chat-body {
    padding: 10px;
    overflow-y: auto;
    height: 250px;
    background-color: #f8f9fa;
}

.chat-footer {
    padding: 10px;
    border-top: 1px solid #ddd;
}

.chat-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background-color: #0d6efd;
    color: white;
    border: none;
    border-radius: 50%;
    width: 55px;
    height: 55px;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    z-index: 1050;
}

.chat-message {
    align-items: flex-end;
}

.message-avatar img {
    width: 35px;
    height: 35px;
    object-fit: cover;
}


/* Layout improvements for a clean, responsive chat (mobile-first) */
.chat-shell {
    display: flex;
    flex-direction: column; /* mobile: stacked */
    height: auto; /* let content define height on mobile */
    min-height: 100%;
}

/* Enhance for tablets and up */
@media (min-width: 768px) {
    .chat-shell {
        flex-direction: row; /* side by side */
        height: 70vh;
        min-height: 60vh;
    }

    .chat-contacts, .chat-panel {
        width: 100%;
    }

    /* lift small-screen caps */
    .chat-contacts .chat-contacts-list {
        max-height: none;
    }

    .messages-scroll {
        min-height: 0;
    }
}

/* Enhance further for desktops */
@media (min-width: 992px) {
    .chat-shell {
        height: 100%;
    }
}

/* Mobile-specific constraints */
.chat-contacts .chat-contacts-list {
    max-height: 100%;
}

.messages-scroll {
    min-height: 40vh;
}

.chat-contacts, .chat-panel {
    display: flex;
    flex-direction: column;
}

.chat-contacts {
    width: 100%;
}

.chat-contacts .card-body {
    padding: 0;
}

.chat-message.sender {
    flex-direction: row-reverse;
}

.chat-contacts-list {
    overflow-y: auto;
}

.chat-contacts .list-group-item {
    border: 0;
    border-bottom: 1px solid #f1f1f1;
}

.chat-contacts .list-group-item.active, .chat-contacts .list-group-item:hover {
    background: #f6f8ff;
}

.chat-panel {
    width: 100%;
}

/* Ensure the chat card uses flex so the middle area can scroll */
.chat-panel .card {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-panel .card-header {
    flex: 0 0 auto;
}

.composer {
    flex: 0 0 auto;
}

.messages-scroll {
    flex: 1 1 auto;
    min-height: 0;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.chat-header img {
    width: 40px;
    height: 40px;
    object-fit: cover;
}

.chat-header h6 {
    font-size: 1rem;
    line-height: 1.25rem;
}

.messages-scroll {
    /* wrapper provides padding and height via flex; inner container scrolls */
    overflow: hidden;
    padding: 1rem;
    background: #fafbfc;
    flex: 1 1 auto;
    min-height: 0; /* allow flex child to shrink and provide height */
    position: relative; /* anchor loader overlay */
}

/* Full overlay loader for conversation fetch */
.messages-loader {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.8);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
}

.composer {
    border-top: 1px solid #eef0f3;
    padding: 0.75rem;
}

/* Modern bubbles */
.bubble {
    max-width: min(78%, 560px);
    padding: .6rem .8rem;
    border-radius: 16px;
    position: relative;
}

.bubble .meta {
    font-size: .75rem;
    color: #6b7280;
    margin-top: .25rem;
}

.msg {
    display: flex;
    gap: .5rem;
    margin-bottom: .75rem;
}

.msg.me {
    justify-content: flex-start;
    flex-direction: row-reverse;
}

.msg.them {
    justify-content: flex-start;
}

.msg .avatar-32 {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    margin-top: .25rem;
}

.msg.me .bubble {
    background: #d1e7dd;
    color: #0b3d2c;
    border-top-right-radius: 4px;
    font-size: 14px;
}

.msg.them .bubble {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-top-left-radius: 4px;
    font-size: 14px;
}

/* Make each chat container the scrollable area */
.chat-message-container {
    height: 100%;
    overflow-y: auto;
}

/* Empty state */
.empty-state {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: #6b7280;
    padding: 2rem;
}

.message-username.sender {
    text-align: right;
}

.message-username.receiver {
    text-align: left;
}
/* Compact & Accessible Chat Overrides (appended) */
/* Make popup more compact */
.chat-popup {
    width: 280px;
    max-height: 420px;
}

.chat-header {
    padding: 8px 10px;
}

.chat-body {
    padding: 8px;
    height: 220px;
}

.chat-footer {
    padding: 6px 8px;
}

/* Smaller avatars for compact layout */
.message-avatar img {
    width: 28px;
    height: 28px;
}

/* Compact message bubbles regardless of Bootstrap utility classes applied by JS */
.message-content {
    font-size: 13px;
    line-height: 1.35;
    padding: .45rem .6rem !important;
    border-radius: 12px !important;
    border: 1px solid #e5e7eb;
    word-wrap: break-word;
    word-break: break-word;
}

/* Sender (often given bg-primary text-white in JS) -> soften background, dark text for contrast */
.message-content.bg-primary {
    background-color: #e6f0ff !important; /* soft blue */
    color: #0a2a66 !important;            /* deep slate/blue text */
    border-color: #c7d2fe !important;
}

/* Receiver (often bg-success) -> soft green with dark teal text for readability */
.message-content.bg-success {
    background-color: #ecfdf5 !important; /* mint/soft green */
    color: #065f46 !important;            /* dark teal text */
    border-color: #a7f3d0 !important;
}

/* Some streams use bg-light text-primary */
.message-content.bg-light {
    background-color: #f8fafc !important; /* near-white */
    color: #111827 !important;            /* near-black */
    border-color: #e5e7eb !important;
}

/* Normalize paragraph inside bubbles (JS wraps in <p>) */
.message-content p {
    margin-bottom: 0.2rem;
}

/* Smaller, subtle timestamp */
.message-content .timestamp,
.timestamp.small,
.small.timestamp {
    font-size: 11px;
    color: #6b7280 !important;
}

/* Tighten message row spacing */
.chat-message {
    margin-bottom: 6px;
}

/* Make inputs compact */
.chat-footer .form-control {
    font-size: 0.875rem;
    padding: .3rem .5rem;
    height: 34px;
}

.chat-footer .btn {
    --bs-btn-padding-y: .28rem;
    --bs-btn-padding-x: .6rem;
    --bs-btn-font-size: .875rem;
    padding: .28rem .6rem; /* for non-BS var aware buttons */
}

/* Ensure the header close button remains visible and non-aggressive */
.chat-header .btn.btn-light {
    color: #111827;
    border: 1px solid #e5e7eb;
}

/* Namespaced hardening to avoid external CSS conflicts */
.x-chat {
    font-family: inherit;
    color: #111827;
}

/* Contain all subparts under x-chat to raise specificity */
.x-chat .chat-header {
    background-color: #0d6efd !important;
    color: #fff !important;
}

.x-chat .chat-body {
    background-color: #f8f9fa !important;
}

/* Inputs inside chat: ensure readability and compactness */
.x-chat .chat-footer .form-control {
    background-color: #ffffff !important;
    color: #111827 !important;
    border-color: #ced4da !important;
}
.x-chat .chat-footer .form-control::placeholder {
    color: #6b7280 !important;
}

/* Primary button inside chat: enforce consistent colors independent of page theme */
.x-chat .chat-footer .btn-primary,
.x-chat .btn-primary {
    background-color: #0d6efd !important;
    border-color: #0d6efd !important;
    color: #ffffff !important;
}
.x-chat .btn-primary:hover,
.x-chat .btn-primary:focus {
    background-color: #0b5ed7 !important;
    border-color: #0a58ca !important;
    color: #ffffff !important;
}
.x-chat .btn-primary:active,
.x-chat .btn-primary.active {
    background-color: #0a58ca !important;
    border-color: #0a58ca !important;
}

/* Neutralize Bootstrap text utility classes inside message bubbles to avoid white-on-light contrast issues */
.x-chat .message-content,
.x-chat .message-content p,
.x-chat .message-content .text-white,
.x-chat .message-content .text-light,
.x-chat .message-content .text-body,
.x-chat .message-content .text-dark {
    color: inherit !important;
}

/* Ensure bubble color overrides always win, even if JS injects text-white */
.x-chat .message-content.bg-primary {
    background-color: #e6f0ff !important;
    color: #0a2a66 !important;
    border-color: #c7d2fe !important;
}
.x-chat .message-content.bg-success {
    background-color: #ecfdf5 !important;
    color: #065f46 !important;
    border-color: #a7f3d0 !important;
}
.x-chat .message-content.bg-light {
    background-color: #f8fafc !important;
    color: #111827 !important;
    border-color: #e5e7eb !important;
}

/* Tighten timestamp color within namespace */
.x-chat .message-content .timestamp,
.x-chat .timestamp.small,
.x-chat .small.timestamp {
    color: #6b7280 !important;
}

/* Slightly lift z-index to sit above most elements but below modals */
.x-chat.chat-popup {
    z-index: 1060;
}
