/* ===================== KHUNG CHAT (.ch-) ===================== */
.ch { display: flex; flex-direction: column; height: 100%; min-height: 0; background: var(--bg); position: relative; }
.ch__body { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }
.ch__composer[hidden], .ch__selbar[hidden] { display: none; }
.ch__selbar { display: flex; align-items: center; gap: 6px; padding: 10px 14px; border-top: 1px solid var(--border); background: var(--bg-elev); }
.ch__selcount { font-weight: 700; }
.ch__selspacer { flex: 1; }
.ch__selclose { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: var(--text-2); }
.ch__selclose:hover { background: var(--hover); }
.ch__composer-fail { padding: 14px; text-align: center; color: var(--danger); }

/* ---------- Đầu ---------- */
.ch-header { display: flex; align-items: center; gap: 6px; height: 64px; padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--bg-elev); flex: none; }
.ch-header__back { display: none; }
.ch-header__who { display: flex; align-items: center; gap: 12px; min-width: 0; padding: 4px 8px 4px 4px; border-radius: var(--r-md); text-align: left; }
.ch-header__who:hover { background: var(--hover); }
.ch-header__txt { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.ch-header__txt b { font-size: var(--fs-md); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-header__txt small { color: var(--text-3); font-size: var(--fs-xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-header__txt small.is-typing { color: var(--accent); }
.ch-header__spacer { flex: 1; }
.ch-header .is-on { color: var(--accent); background: var(--accent-soft); }
@media (max-width: 760px) { .ch-header__back { display: inline-flex; } }

/* ---------- Tin ghim ---------- */
.ch-pinbar { display: flex; align-items: center; gap: 4px; padding: 6px 10px; background: var(--bg-elev); border-bottom: 1px solid var(--border); }
.ch-pinbar__body { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-align: left; padding: 4px 6px; border-radius: var(--r-sm); }
.ch-pinbar__body:hover { background: var(--hover); }
.ch-pinbar__ic { color: var(--accent); display: inline-flex; }
.ch-pinbar__txt { display: flex; flex-direction: column; min-width: 0; font-size: var(--fs-sm); line-height: 1.3; }
.ch-pinbar__txt b { color: var(--accent); font-size: var(--fs-xs); }
.ch-pinbar__txt span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.ch-pinbar__x { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; color: var(--text-3); }
.ch-pinbar__x:hover { background: var(--hover); color: var(--text); }

/* ---------- Tìm trong chat ---------- */
.ch-search { background: var(--bg-elev); border-bottom: 1px solid var(--border); }
.ch-search__bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; color: var(--text-3); }
.ch-search__input { flex: 1; min-width: 0; height: 36px; border: 0; outline: 0; background: transparent; color: var(--text); }
.ch-search__info { font-size: var(--fs-xs); color: var(--text-3); white-space: nowrap; }
.ch-search__btn { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 50%; color: var(--text-2); }
.ch-search__btn:hover { background: var(--hover); }
.ch-search__list { max-height: 220px; overflow-y: auto; border-top: 1px solid var(--border); }
.ch-search__item { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; width: 100%; padding: 8px 14px; text-align: left; }
.ch-search__item:hover, .ch-search__item.is-active { background: var(--accent-soft); }
.ch-search__item span { grid-column: 1 / -1; color: var(--text-2); font-size: var(--fs-sm); }
.ch-search__item small { color: var(--text-3); }

/* ---------- Vùng cuộn ---------- */
.ch-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 12px 0 6px; scroll-behavior: auto; }
.ch-list { display: flex; flex-direction: column; padding: 0 clamp(10px, 3vw, 28px); min-height: 100%; justify-content: flex-end; }
.ch-day { display: flex; justify-content: center; margin: 14px 0 8px; position: sticky; top: 4px; z-index: 2; }
.ch-day span { padding: 3px 12px; border-radius: var(--r-pill); font-size: var(--fs-xs); font-weight: 600; color: var(--text-2); background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-1); }
.ch-older { display: flex; justify-content: center; padding: 8px; }
.ch-older__btn { color: var(--accent); font-weight: 600; font-size: var(--fs-sm); padding: 6px 14px; border-radius: var(--r-pill); }
.ch-older__btn:hover { background: var(--accent-soft); }

.ch-jump { position: absolute; right: 18px; bottom: 14px; width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-2); z-index: 5; color: var(--text-2); }
.ch-jump:hover { color: var(--accent); }
.ch-jump__n { position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; font-size: 11px; font-weight: 700; display: grid; place-items: center; color: var(--accent-text); background: var(--accent); }
.ch-jump__n[hidden] { display: none; }

.ch-typing { display: flex; align-items: center; gap: 8px; padding: 2px 22px 6px; font-size: var(--fs-xs); color: var(--text-3); }
.ch-typing[hidden] { display: none; }
.ch-typing__dots { display: inline-flex; gap: 3px; }
.ch-typing__dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--text-3); animation: ch-dot 1s infinite; }
.ch-typing__dots i:nth-child(2) { animation-delay: .15s; } .ch-typing__dots i:nth-child(3) { animation-delay: .3s; }
@keyframes ch-dot { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-3px); opacity: 1; } }

/* ---------- Tin nhắn ---------- */
.ch-msg { display: flex; align-items: flex-end; gap: 8px; margin-top: 10px; position: relative; max-width: 100%; border-radius: var(--r-md); }
.ch-msg.is-cont { margin-top: 2px; }
.ch-msg.is-me { justify-content: flex-end; }
.ch-msg--system { justify-content: center; margin: 12px 0; }
.ch-system { padding: 4px 12px; border-radius: var(--r-pill); font-size: var(--fs-xs); color: var(--text-2); background: var(--surface-2); text-align: center; }
.ch-msg__main { display: flex; flex-direction: column; min-width: 0; max-width: min(72%, 560px); align-items: flex-start; }
.ch-msg.is-me .ch-msg__main { align-items: flex-end; }
.ch-msg__av, .ch-msg__gap { width: 32px; flex: none; cursor: pointer; }
.ch-msg.is-flash { animation: ch-flash 2s var(--ease); }
@keyframes ch-flash { 0%, 60% { background: var(--accent-soft); box-shadow: 0 0 0 6px var(--accent-soft); } 100% { background: transparent; box-shadow: 0 0 0 6px transparent; } }
.ch-msg.is-selected { background: var(--accent-soft); box-shadow: 0 0 0 4px var(--accent-soft); }
.ch-select { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border-strong); display: grid; place-items: center; color: var(--accent-text); flex: none; align-self: center; }
.ch-select.is-on { background: var(--accent); border-color: var(--accent); }

.ch-bubble { position: relative; padding: 8px 12px 6px; border-radius: 18px; background: var(--bubble-other); color: var(--bubble-other-text); box-shadow: var(--shadow-1); overflow-wrap: anywhere; word-break: break-word; max-width: 100%; }
.ch-msg.is-me .ch-bubble { background: var(--bubble-me); color: var(--bubble-me-text); }
.ch-msg.is-other:not(.is-cont) .ch-bubble { border-top-left-radius: 6px; }
.ch-msg.is-me:not(.is-cont) .ch-bubble { border-top-right-radius: 6px; }
.ch-msg.is-other.is-cont .ch-bubble { border-top-left-radius: 6px; }
.ch-msg.is-me.is-cont .ch-bubble { border-top-right-radius: 6px; }
.ch-msg.is-other:not(.is-last) .ch-bubble { border-bottom-left-radius: 6px; }
.ch-msg.is-me:not(.is-last) .ch-bubble { border-bottom-right-radius: 6px; }
.ch-bubble--sticker, .ch-bubble--image { background: transparent !important; box-shadow: none; padding: 2px; }
.ch-bubble--sticker .ch-meta, .ch-bubble--image .ch-meta { color: var(--text-3); }
.ch-bubble--image:has(.ch-text) { background: var(--bubble-other) !important; padding: 4px 4px 6px; box-shadow: var(--shadow-1); }
.ch-msg.is-me .ch-bubble--image:has(.ch-text) { background: var(--bubble-me) !important; }
.ch-bubble--image .ch-text { padding: 6px 8px 0; }
.ch-bubble--recalled { background: transparent !important; border: 1px dashed var(--border-strong); box-shadow: none; color: var(--text-3) !important; }
.ch-recalled { display: inline-flex; align-items: center; gap: 6px; font-style: italic; font-size: var(--fs-sm); }
.ch-text { white-space: pre-wrap; line-height: 1.45; }
.ch-text a, .ch-linkcard { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.ch-mention { font-weight: 700; color: var(--accent); }
.ch-msg.is-me .ch-mention { color: inherit; text-decoration: underline; }
.ch-sender { font-size: var(--fs-xs); font-weight: 700; color: var(--accent); margin-bottom: 2px; cursor: pointer; }
.ch-forwarded { display: flex; align-items: center; gap: 4px; font-size: var(--fs-xs); opacity: .75; margin-bottom: 3px; }
.ch-sticker { font-size: 76px; line-height: 1.1; padding: 4px 8px; }
.ch-meta { display: flex; align-items: center; justify-content: flex-end; gap: 5px; margin-top: 2px; font-size: 11px; opacity: .72; line-height: 1; }
.ch-meta__state.is-read { color: #4fc3f7; opacity: 1; }
.ch-msg.is-me .ch-meta__state.is-read { color: #9fe6ff; }
.ch-meta__star { color: #f5b301; display: inline-flex; }
.ch-msg.is-timed .ch-bubble { box-shadow: 0 0 0 1.5px var(--warning) inset; }

.ch-quote { display: flex; gap: 8px; width: 100%; text-align: left; margin-bottom: 6px; padding: 5px 8px; border-radius: 10px; background: rgba(127, 127, 127, .14); }
.ch-quote__bar { width: 3px; border-radius: 2px; background: var(--accent); flex: none; }
.ch-msg.is-me .ch-quote__bar { background: currentColor; opacity: .7; }
.ch-quote__body { display: flex; flex-direction: column; min-width: 0; font-size: var(--fs-sm); line-height: 1.3; }
.ch-quote__body b { font-size: var(--fs-xs); }
.ch-quote__body span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .85; }

/* Ảnh */
.ch-images { display: grid; gap: 3px; border-radius: 14px; overflow: hidden; max-width: 340px; }
.ch-images.n2 { grid-template-columns: 1fr 1fr; } .ch-images.n3 { grid-template-columns: 1fr 1fr; } .ch-images.n3 .ch-images__cell:first-child { grid-column: 1 / -1; }
.ch-images.n4 { grid-template-columns: 1fr 1fr; }
.ch-images__cell { position: relative; display: block; padding: 0; background: var(--surface-2); overflow: hidden; aspect-ratio: 1; }
.ch-images.is-single .ch-images__cell { aspect-ratio: auto; }
.ch-images img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform var(--dur); }
.ch-images.is-single img { max-height: 380px; object-fit: contain; }
.ch-images__cell:hover img { transform: scale(1.03); }
.ch-images__more { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0, 0, 0, .5); color: #fff; font-size: 26px; font-weight: 700; }

/* Tệp / thoại / liên hệ / vị trí / liên kết */
.ch-file, .ch-contact, .ch-location, .ch-linkcard { display: flex; align-items: center; gap: 12px; min-width: 220px; text-decoration: none; text-align: left; color: inherit; }
.ch-file__ic { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; font-size: 11px; font-weight: 800; background: rgba(127, 127, 127, .2); flex: none; }
.ch-file__meta, .ch-contact__meta, .ch-location__meta, .ch-linkcard__txt { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.3; }
.ch-file__meta b, .ch-linkcard__txt b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-file__meta small, .ch-contact__meta small, .ch-location__meta small, .ch-linkcard__txt small { opacity: .7; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-location__pin { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; background: var(--danger-soft); color: var(--danger); }
.ch-linkcard { margin-top: 6px; padding: 8px 10px; border-radius: 10px; background: rgba(127, 127, 127, .14); text-decoration: none; min-width: 0; }
.ch-linkcard__ic { opacity: .8; }

.ch-voice { display: flex; align-items: center; gap: 10px; min-width: 220px; }
.ch-voice__btn { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-text); flex: none; }
.ch-msg.is-me .ch-voice__btn { background: rgba(255, 255, 255, .92); color: var(--accent); }
.ch-voice__wave { flex: 1; height: 30px; display: flex; align-items: center; gap: 2px; cursor: pointer; }
.ch-voice__wave i { flex: 1; min-width: 2px; border-radius: 2px; background: currentColor; opacity: .35; }
.ch-voice__wave i.is-done { opacity: 1; }
.ch-voice__time { font-size: var(--fs-xs); font-variant-numeric: tabular-nums; opacity: .8; }

/* Bình chọn */
.ch-poll { min-width: 250px; display: flex; flex-direction: column; gap: 6px; }
.ch-poll__head { display: flex; gap: 8px; align-items: flex-start; }
.ch-poll__meta, .ch-poll__foot { opacity: .7; font-size: var(--fs-xs); }
.ch-poll__opt { position: relative; display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 10px; overflow: hidden; text-align: left; background: rgba(127, 127, 127, .14); }
.ch-poll__opt:disabled { cursor: default; }
.ch-poll__fill { position: absolute; inset: 0 auto 0 0; background: var(--accent); opacity: .22; transition: width var(--dur) var(--ease); }
.ch-poll__opt.is-mine { outline: 1.5px solid var(--accent); }
.ch-poll__check, .ch-poll__text, .ch-poll__count { position: relative; }
.ch-poll__text { flex: 1; }
.ch-poll__count { font-weight: 700; font-size: var(--fs-sm); }

/* Cảm xúc */
.ch-reacts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: -6px; padding: 0 6px; position: relative; z-index: 1; }
.ch-react { display: inline-flex; align-items: center; gap: 3px; height: 24px; padding: 0 7px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-1); font-size: 13px; }
.ch-react small { font-size: 11px; font-weight: 700; color: var(--text-2); }
.ch-react.is-mine { background: var(--accent-soft); border-color: var(--accent); }
.ch-quickreact { display: flex; gap: 2px; padding: 4px; }
.ch-quickreact__btn { width: 38px; height: 38px; border-radius: 50%; font-size: 22px; display: grid; place-items: center; transition: transform var(--dur-fast); }
.ch-quickreact__btn:hover { transform: scale(1.25); background: var(--hover); }
.ch-quickreact__btn.is-mine { background: var(--accent-soft); }

/* Thanh công cụ rê chuột */
.ch-hover { position: absolute; top: -14px; display: none; gap: 2px; padding: 2px; border-radius: var(--r-pill); background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-2); z-index: 3; }
.ch-msg.is-other .ch-hover { left: 56px; } .ch-msg.is-me .ch-hover { right: 6px; }
.ch-msg:hover .ch-hover, .ch-msg:focus-within .ch-hover { display: inline-flex; }
.ch-hover__btn { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; color: var(--text-2); }
.ch-hover__btn:hover { background: var(--hover); color: var(--accent); }
@media (hover: none) { .ch-hover { display: none !important; } }

/* Chuyển tiếp */
.ch-fwd { display: flex; flex-direction: column; gap: 10px; }
.ch-fwd__list { max-height: 300px; overflow-y: auto; }
.ch-fwd__row { display: flex; align-items: center; gap: 10px; }
.ch-fwd__preview { display: flex; flex-direction: column; padding: 8px 10px; border-radius: var(--r-md); background: var(--surface-2); font-size: var(--fs-sm); }
.ch-fwd__preview span { color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-fwd__count { margin-right: auto; color: var(--text-2); font-size: var(--fs-sm); align-self: center; }

/* Trình xem ảnh */
.ch-viewer { position: fixed; inset: 0; z-index: 1000; background: rgba(8, 10, 20, .92); display: flex; flex-direction: column; animation: ch-fade .16s var(--ease); }
.ch-viewer.is-leaving { opacity: 0; transition: opacity .16s; }
@keyframes ch-fade { from { opacity: 0; } }
.ch-viewer__bar { display: flex; align-items: center; gap: 4px; padding: 10px 14px; color: #fff; }
.ch-viewer__spacer { flex: 1; }
.ch-viewer__title, .ch-viewer__count { font-size: var(--fs-sm); opacity: .85; }
.ch-viewer__count { margin-right: 8px; }
.ch-viewer__btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; }
.ch-viewer__btn:hover { background: rgba(255, 255, 255, .16); }
.ch-viewer__stage { flex: 1; min-height: 0; display: grid; place-items: center; overflow: hidden; touch-action: none; }
.ch-viewer__img { max-width: 94vw; max-height: 82vh; object-fit: contain; user-select: none; transition: transform .08s linear; border-radius: 6px; }
.ch-viewer .is-prev, .ch-viewer .is-next { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(255, 255, 255, .12); width: 48px; height: 48px; }
.ch-viewer .is-prev { left: 14px; } .ch-viewer .is-next { right: 14px; }
.ch-viewer .ch-viewer__btn[hidden] { display: none; }

@media (max-width: 760px) {
  .ch-msg__main { max-width: 84%; }
  .ch-sticker { font-size: 64px; }
  .ch-list { padding: 0 10px; }
}
