/* =====================================================================
   CHAT: trò chuyện trực tiếp (templates/_chat.html, static/js/app-chat.js).
   File riêng để gỡ dễ; chỉ được nạp khi CHAT_ENABLED=1. Mọi class có tiền tố chat-, chỉ dùng biến màu ở đầu style.css.
   Từ 1280px (khớp DESKTOP_QUERY trong app-chat.js): cột phải dính khi cuộn. Nhỏ hơn: nút nổi mở ngăn kéo từ dưới lên.
   ===================================================================== */

/* ---------- bố cục trang có cột chat ---------- */
@media (min-width: 1280px) {
    .container.chat-page-container {
        max-width: 1600px;
    }

    .chat-page {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 360px;
        gap: 1.5rem;
        align-items: start;
    }

    /* cao hơn thì khi trang đang ở đầu (cột nằm dưới thanh menu) ô soạn tin bị khuất dưới mép màn hình */
    .chat-aside {
        position: sticky;
        top: .75rem;
        height: calc(100vh - 7.5rem);
        min-height: 26rem;
        margin-top: 1rem;
    }

    .chat-fab,
    .chat-close {
        display: none;
    }
}

/* ---------- khối chat ---------- */
.chat-box {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--c-surface);
    border: 1px solid var(--c-line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    color: var(--c-ink);
}

/* tiêu đề luôn một dòng; trạng thái dài (vd "Đang mở ở tab khác · Dùng ở tab này") tự xuống dòng dưới tiêu đề */
.chat-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .25rem .5rem;
    padding: .75rem 1rem .6rem;
    border-bottom: 1px solid var(--c-line);
}

.chat-title {
    display: flex;
    align-items: center;
    gap: .5rem;
    flex: 0 0 auto;
    margin: 0;
    white-space: nowrap;
    font-size: 1.02rem;
    font-weight: 700;
    color: var(--c-heading-accent);
}

.chat-title .fa-comments {
    color: var(--c-accent);
}

.chat-status {
    flex: 0 0 auto;
    margin-left: auto;
    color: var(--c-muted);
    font-size: .78rem;
    white-space: nowrap;
}

.chat-status-live::before {
    content: "";
    display: inline-block;
    width: .5rem;
    height: .5rem;
    margin-right: .35rem;
    border-radius: 50%;
    background: var(--c-accent);
    vertical-align: middle;
}

.chat-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: .25rem .75rem .5rem;
}

.chat-older-wrap {
    text-align: center;
}

.chat-older {
    font-size: .8rem;
}

.chat-info {
    margin: .75rem .25rem;
    color: var(--c-muted);
    font-size: .85rem;
    text-align: center;
}

.chat-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

/* ---------- một tin nhắn ---------- */
.chat-msg {
    display: grid;
    grid-template-columns: 1.9rem minmax(0, 1fr);
    column-gap: .55rem;
    padding: .4rem .25rem;
    border-radius: 8px;
}

.chat-msg:hover {
    background: var(--c-hover);
}

.chat-msg .auth-avatar {
    width: 1.9rem;
    height: 1.9rem;
    font-size: .8rem;
}

/* tin liên tiếp của cùng một người (app-chat.js applyGroup): bỏ ảnh và tên cho gọn */
.chat-msg-cont {
    padding-top: 0;
}

.chat-msg-cont .auth-avatar {
    visibility: hidden;
    height: 0;
}

.chat-msg-cont .chat-msg-name {
    display: none;
}

.chat-msg-head {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: .15rem .45rem;
    line-height: 1.3;
}

.chat-msg-name {
    font-weight: 600;
    font-size: .86rem;
    overflow-wrap: anywhere;
}

.chat-msg-time {
    color: var(--c-muted);
    font-size: .74rem;
}

.chat-msg-text {
    margin: .1rem 0 0;
    font-size: .9rem;
    line-height: 1.45;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.chat-msg-mine .chat-msg-name {
    color: var(--c-accent-ink);
}

.chat-msg-del {
    margin-left: auto;
    padding: 0 .25rem;
    border: 0;
    background: none;
    color: var(--c-muted);
    font-size: .78rem;
    opacity: 0;
}

.chat-msg:hover .chat-msg-del,
.chat-msg-del:focus-visible {
    opacity: 1;
}

@media (hover: none) {
    .chat-msg-del {
        opacity: 1;
    }
}

.chat-msg-del:hover {
    color: var(--c-down);
}

.chat-msg-flash {
    animation: chat-flash 1.6s ease-out;
}

@keyframes chat-flash {
    from {
        background: var(--c-accent-soft);
    }
}

/* ---------- nút "Có tin mới" ---------- */
.chat-new-pill {
    position: absolute;
    left: 50%;
    bottom: 7.5rem;
    transform: translateX(-50%);
    border-radius: 999px;
    box-shadow: var(--shadow);
    z-index: 2;
}

/* ---------- ô soạn tin / lời mời đăng nhập ---------- */
.chat-footer {
    padding: .6rem .75rem .25rem;
    border-top: 1px solid var(--c-line);
}

.chat-form {
    display: flex;
    gap: .5rem;
    align-items: flex-end;
}

.chat-input {
    flex: 1 1 auto;
    min-height: 2.4rem;
    max-height: 7.5rem;
    resize: none;
    font-size: .9rem;
}

.chat-send {
    flex: 0 0 auto;
}

.chat-form-meta {
    display: flex;
    justify-content: space-between;
    gap: .5rem;
    margin-top: .3rem;
    min-height: 1.1rem;
    font-size: .76rem;
    color: var(--c-muted);
}

.chat-error {
    color: var(--c-down);
}

.chat-count-over {
    color: var(--c-down);
    font-weight: 600;
}

.chat-banner {
    margin: 0;
    padding: .55rem .7rem;
    border-radius: 8px;
    background: var(--c-warn-bg);
    color: var(--c-ink);
    font-size: .84rem;
}

.chat-guest {
    text-align: center;
}

.chat-guest p {
    margin: 0 0 .5rem;
    font-size: .86rem;
    color: var(--c-ink-2);
}

/* khách xem bản lưu: mờ dần phần cuối danh sách để gợi ý đăng nhập xem tiếp */
.chat-box-guest .chat-scroll {
    -webkit-mask-image: linear-gradient(to bottom, #000 70%, transparent);
    mask-image: linear-gradient(to bottom, #000 70%, transparent);
}

.chat-note {
    margin: 0;
    padding: .3rem .9rem .6rem;
    color: var(--c-muted);
    font-size: .72rem;
    line-height: 1.35;
}

/* ---------- màn hình nhỏ: nút nổi + ngăn kéo ---------- */
@media (max-width: 1279.98px) {
    .chat-fab {
        position: fixed;
        right: 1rem;
        bottom: 1rem;
        z-index: 1035;
        display: inline-flex;
        align-items: center;
        gap: .45rem;
        padding: .6rem 1rem;
        border-radius: 999px;
        box-shadow: var(--shadow);
        font-weight: 600;
    }

    .chat-box {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1045;
        height: min(82vh, 40rem);
        height: min(82dvh, 40rem);
        border-radius: var(--radius) var(--radius) 0 0;
        transform: translateY(105%);
        visibility: hidden;
        transition: transform .22s ease, visibility 0s linear .22s;
    }

    .chat-box.chat-open {
        transform: none;
        visibility: visible;
        transition: transform .22s ease;
    }

    .chat-close {
        margin-left: .25rem;
    }
}

@media (min-width: 576px) and (max-width: 1279.98px) {
    .chat-box {
        left: auto;
        right: 1rem;
        bottom: 1rem;
        width: 380px;
        border-radius: var(--radius);
    }
}

@media (prefers-reduced-motion: reduce) {
    .chat-box,
    .chat-box.chat-open {
        transition: none;
    }

    .chat-msg-flash {
        animation: none;
    }
}
