/* ============================================================
   messenger.css — tmMessenger layout
   Targets the markup emitted by tmMessenger (gen_navbar_section,
   gen_search_section, gen_messages_section, gen_control_panel)
   plus index.html's sidebar and modals.

   Assumes Bootstrap 5 is already loaded. Nothing here restates
   Bootstrap utilities except where a util needs a companion rule
   (e.g. .chatroom.bg-primary text colour).
   ============================================================ */

:root {
    /* Height reserved for the site header + any page chrome above
       the messenger. Tune this one value to fit your layout. */
    --msgr-chrome-h:      160px;
    --msgr-h:             calc(100vh - var(--msgr-chrome-h));
    --msgr-h-min:         480px;

    --msgr-sidebar-w:     320px;
    --msgr-avatar:        40px;
    --msgr-avatar-sm:     28px;

    --msgr-border:        #dee2e6;
    --msgr-surface:       #ffffff;
    --msgr-surface-alt:   #f6f7f9;
    --msgr-hover:         #eef1f5;
    --msgr-active:        #0d6efd;
    --msgr-muted:         #6c757d;
    --msgr-bubble:        #f1f3f5;
    --msgr-bubble-own:    #d9e7ff;

    --msgr-radius:        .5rem;
    --msgr-radius-lg:     1rem;
    --msgr-gap:           .75rem;
}

/* Shared utility the plugin toggles constantly. If your global
   stylesheet already defines .hide, delete this block. */
.hide { display: none !important; }

.body-no-scroll { overflow: hidden; }


/* ============================================================
   1. Shell
   ============================================================ */

.messager-container {
    display: flex;
    align-items: stretch;
    height: var(--msgr-h);
    min-height: var(--msgr-h-min);
    background: var(--msgr-surface);
    border: 1px solid var(--msgr-border);
    border-radius: var(--msgr-radius);
    overflow: hidden;
}


/* ============================================================
   2. Sidebar — chatroom list
   ============================================================ */

#messages-sidebar-container {
    display: flex;
    flex-direction: column;
    flex: 0 0 var(--msgr-sidebar-w);
    width: var(--msgr-sidebar-w);
    min-width: 0;
    border-right: 1px solid var(--msgr-border);
    background: var(--msgr-surface-alt);
    padding-top: .75rem;
}

#messages-sidebar-container > hr {
    margin: .5rem 0;
    opacity: .35;
}

#messages-sidebar-container .search-chatroom {
    border-radius: 2rem;
}

/* The scroll region. flex:1 + min-height:0 is what lets the list
   scroll instead of pushing the sidebar past its container. */
#messenger-sidebar-container {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 .25rem;
}

.chatrooms-container {
    display: block;
}

/* --- one chatroom row --- */

.chatroom {
    display: block;
    position: relative;
    padding: .625rem .75rem;
    border-radius: var(--msgr-radius);
    cursor: pointer;
    transition: background-color .12s ease;
}

.chatroom:hover { background-color: var(--msgr-hover); }

/* The plugin marks the active room with Bootstrap's .bg-primary,
   which sets a background but not a foreground. */
.chatroom.bg-primary,
.chatroom.bg-primary .group-name,
.chatroom.bg-primary .desc { color: #fff; }

.chatroom.bg-primary .desc { opacity: .85; }

.chatroom .group-name {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-bottom: .25rem;
}

/* Stacked avatars — .photo is used by gen_chatrooms(),
   .participants-container by gen_chatroom(). Same treatment. */
.chatroom .photo,
.chatroom .participants-container {
    display: flex;
    align-items: center;
    margin-bottom: .25rem;
}

.chatroom .photo img,
.chatroom .participant img {
    width: var(--msgr-avatar-sm);
    height: var(--msgr-avatar-sm);
    border-radius: 50%;
    object-fit: cover;
    background: var(--msgr-hover);
    border: 2px solid var(--msgr-surface-alt);
}

.chatroom .photo img + img,
.chatroom .participant + .participant { margin-left: -.5rem; }

.chatroom .desc {
    display: block;
    font-size: .8125rem;
    line-height: 1.3;
    color: var(--msgr-muted);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.chatroom .btn-unread:empty { display: none; }

.chatroom .btn-unread {
    min-width: 1.25rem;
    font-size: .6875rem;
    line-height: 1.25rem;
    text-align: center;
}

.btn-load-more-chatrooms { font-size: .875rem; }


/* ============================================================
   3. Main pane
   ============================================================ */

.messages-container {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    padding: 0 .75rem .75rem;
}

/* --- navbar row (gen_navbar_section) --- */

#navbar {
    flex: 0 0 auto;
    list-style: none;
    margin: 0;
    padding: .5rem 0;
    border-bottom: 1px solid var(--msgr-border);
    align-items: center;
}

#channel-name {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

#members-block .profile-container {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: .25rem;
    list-style: none;
    margin: 0;
    padding: 0;
}

#members-block .profile-container > li { display: flex; align-items: center; }

#members-block .profile img {
    width: var(--msgr-avatar-sm);
    height: var(--msgr-avatar-sm);
    border-radius: 50%;
    object-fit: cover;
    cursor: pointer;
    border: 2px solid var(--msgr-surface);
}

#members-block .profile + .profile { margin-left: -.5rem; }

#num-members {
    font-size: .8125rem;
    color: var(--msgr-muted);
}

/* --- search bar --- */

#search-messages-container { flex: 0 0 auto; }

/* --- message viewport --- */

.message-content {
    position: relative;
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    border: 1px solid var(--msgr-border);
    border-radius: var(--msgr-radius);
    margin-top: .5rem;
    overflow: hidden;
}

.btn-load-messages {
    flex: 0 0 auto;
    text-align: center;
    padding: .5rem 0;
}

.btn-load-messages img.loading { width: 24px; height: 24px; }

/* #messages and #searched-messages. Only one is visible at a
   time — the plugin toggles .hide between them — so both claim
   the full remaining height. */
.message-content .inner-content {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    list-style: none;
    margin: 0;
    padding: .75rem;
    scroll-behavior: smooth;
}

/* --- a single message --- */

li.message {
    display: flex;
    align-items: flex-start;
    gap: var(--msgr-gap);
    padding: .375rem 0;
}

li.message .profile {
    flex: 0 0 var(--msgr-avatar);
    cursor: pointer;
}

li.message .profile img {
    width: var(--msgr-avatar);
    height: var(--msgr-avatar);
    border-radius: 50%;
    object-fit: cover;
    background: var(--msgr-hover);
}

li.message .content {
    flex: 1 1 auto;
    min-width: 0;
}

li.message .time {
    color: var(--msgr-muted);
    font-size: .75rem;
}

li.message .comment {
    display: inline-block;
    max-width: 100%;
    margin-top: .125rem;
    padding: .5rem .75rem;
    background: var(--msgr-bubble);
    border-radius: var(--msgr-radius-lg);
    border-top-left-radius: .25rem;
}

li.message .message-text {
    overflow-wrap: anywhere;
    word-break: break-word;
}

li.message .message-text img {
    max-width: 100%;
    height: auto;
    border-radius: var(--msgr-radius);
}

li.message .message-text p:last-child { margin-bottom: 0; }

/* Optional: own-message alignment. The plugin doesn't currently
   tag these — add class="own" to the <li> in displayMessage()
   when data.uid == aidInput and this activates. */
li.message.own { flex-direction: row-reverse; }
li.message.own .content { text-align: right; }
li.message.own .comment {
    background: var(--msgr-bubble-own);
    border-top-left-radius: var(--msgr-radius-lg);
    border-top-right-radius: .25rem;
    text-align: left;
}

/* Speech-bubble classes from the disabled branch of
   displayMessage(), kept so re-enabling it doesn't look broken. */
.speech-bubble { padding: .5rem .75rem; border-radius: var(--msgr-radius-lg); }
.sender-speech-bubble    { background: var(--msgr-bubble-own); }
.recipient-speech-bubble { background: var(--msgr-bubble); }

/* --- search highlight --- */

.highlight {
    background: #fff3cd;
    border-radius: .125rem;
    padding: 0 .0625rem;
}


/* ============================================================
   4. Composer
   ============================================================ */

.control-panel {
    flex: 0 0 auto;
    border-radius: var(--msgr-radius);
}

.control-panel #editor {
    min-height: 72px;
    max-height: 220px;
    overflow-y: auto;
}

/* Placeholder for the contenteditable when tmEditor is inactive */
.control-panel #editor:empty:before {
    content: attr(placeholder);
    color: var(--msgr-muted);
    pointer-events: none;
}

/* Simple-keyboard variant */
.control-panel .chat-control {
    display: flex;
    align-items: flex-end;
    gap: .5rem;
}

.control-panel .chat-control .left  { flex: 1 1 auto; min-width: 0; }
.control-panel .chat-control .right { flex: 0 0 auto; }

.control-panel #messageInput {
    resize: none;
    height: 64px;
}


/* ============================================================
   5. Profile slide-out panel
   ============================================================ */

#profilePanel {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 1045;
    width: 340px;
    max-width: 90vw;
    height: 100vh;
    background: var(--msgr-surface);
    border-left: 1px solid var(--msgr-border);
    box-shadow: -8px 0 24px rgba(0, 0, 0, .08);
    padding: .75rem;
    overflow-y: auto;
    transition: transform .22s ease, visibility .22s;
}

.profile-panel-hidden {
    transform: translateX(100%);
    visibility: hidden;
}

.profile-panel-visible {
    transform: translateX(0);
    visibility: visible;
}


/* ============================================================
   6. Member rows (modals + people search)
   ============================================================ */

.members-list,
.searched-members-container,
#messages-people-container,
#searched-people-container {
    list-style: none;
    margin: 0;
    padding: 0;
}

.member {
    padding: .5rem;
    border-bottom: 1px solid var(--msgr-border);
}

.member:last-child { border-bottom: 0; }

.member .profile {
    display: flex;
    align-items: center;
    gap: .75rem;
}

.member .photo { flex: 0 0 var(--msgr-avatar); }

.member .photo img {
    width: var(--msgr-avatar);
    height: var(--msgr-avatar);
    border-radius: 50%;
    object-fit: cover;
    background: var(--msgr-hover);
}

.member .name {
    flex: 1 1 auto;
    min-width: 0;
}

.member .name > div {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.member .status { color: var(--msgr-muted); }

.member .control {
    flex: 0 0 auto;
    position: relative;
}


/* ============================================================
   7. Floating chat window (btn-chat-max / minChat / midChat)
   ============================================================ */

.chat-window {
    position: fixed;
    right: 1rem;
    bottom: 0;
    z-index: 1040;
    display: none;
    background: var(--msgr-surface);
    border: 1px solid var(--msgr-border);
    border-bottom: 0;
    border-radius: var(--msgr-radius) var(--msgr-radius) 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, .12);
    overflow: hidden;
    transition: width .18s ease, height .18s ease;
}

#modalOverlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1035;
    background: rgba(0, 0, 0, .4);
}


/* ============================================================
   8. Scrollbars
   ============================================================ */

.div-scrollbar,
.inner-content,
#profilePanel { scrollbar-width: thin; scrollbar-color: #c4c9d0 transparent; }

.div-scrollbar::-webkit-scrollbar,
.inner-content::-webkit-scrollbar,
#profilePanel::-webkit-scrollbar { width: 8px; }

.div-scrollbar::-webkit-scrollbar-thumb,
.inner-content::-webkit-scrollbar-thumb,
#profilePanel::-webkit-scrollbar-thumb {
    background: #c4c9d0;
    border-radius: 4px;
}

.div-scrollbar::-webkit-scrollbar-track,
.inner-content::-webkit-scrollbar-track,
#profilePanel::-webkit-scrollbar-track { background: transparent; }


/* ============================================================
   9. Responsive
   ============================================================ */

@media (max-width: 991.98px) {
    :root { --msgr-sidebar-w: 260px; }
}

@media (max-width: 767.98px) {
    :root { --msgr-chrome-h: 110px; }

    .messager-container {
        flex-direction: column;
        height: auto;
        min-height: 0;
        border-radius: 0;
    }

    #messages-sidebar-container {
        flex: 0 0 auto;
        width: 100%;
        max-height: 40vh;
        border-right: 0;
        border-bottom: 1px solid var(--msgr-border);
    }

    .messages-container {
        height: var(--msgr-h);
        min-height: 360px;
    }

    #profilePanel { width: 100%; max-width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    .chatroom,
    #profilePanel,
    .chat-window { transition: none; }

    .message-content .inner-content { scroll-behavior: auto; }
}
