/* Khung chat: tin nhan, bieu cam, o soan tin, bang emoji. (Tach tu styles.css - nap theo DUNG thu tu trong index.html) */

/* =====================================================================
   Khung chat
   ===================================================================== */
.chat-panel {
  flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  background:
    radial-gradient(600px 400px at 100% 0%, var(--chat-glow-tr), transparent 70%),
    radial-gradient(700px 500px at 0% 100%, var(--chat-glow-bl), transparent 70%),
    var(--chat-bg);
}
.chat-empty {
  margin: auto; text-align: center; padding: 24px;
  display: flex; flex-direction: column; align-items: center;
  animation: card-rise .5s var(--ease);
}
.chat-empty-icon {
  position: relative;
  width: 96px; height: 96px; border-radius: 30px; background: var(--grad); color: #fff;
  display: flex; align-items: center; justify-content: center; margin-bottom: 22px;
  box-shadow: 0 18px 40px rgba(0, 104, 255, .3);
  animation: mark-bob 4s ease-in-out infinite;
}
.chat-empty-icon::before { /* vong hao quang mo phia sau */
  content: ""; position: absolute; inset: -18px; border-radius: 40px; z-index: -1;
  background: radial-gradient(closest-side, rgba(0, 104, 255, .18), transparent);
}
.chat-empty h2 {
  font-size: 21px; font-weight: 700; margin: 0 0 6px; letter-spacing: -.01em;
  color: var(--text-primary);
}
.chat-empty .muted { font-size: 14px; margin: 0; color: var(--text-muted); }
.chat-empty-features { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 22px; }
.chat-empty-features span {
  font-size: 12.5px; font-weight: 500; color: var(--text-secondary);
  padding: 7px 14px; border-radius: 999px;
  background: var(--chat-empty-chip-bg); border: 1px solid var(--chat-empty-chip-border);
  box-shadow: var(--shadow-sm);
}

.chat-active { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.chat-header {
  padding: 12px 20px; border-bottom: 1px solid var(--ux-border); flex-shrink: 0;
  background: var(--ux-surface); color: var(--ux-text);
  display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 68px;
  position: relative; z-index: 3;
}
.chat-header-main { min-width: 0; }
#chat-title { font-size: 16.5px; font-weight: 700; margin-right: 8px; letter-spacing: -.01em; }
#chat-account-label {
  font-size: 11.5px; color: var(--ux-on-primary); background: var(--ux-primary);
  padding: 2px 9px; border-radius: 999px; font-weight: 700; vertical-align: 2px;
}
.chat-header-actions .small-btn {
  padding: 7px 13px; font-size: 13px; border-radius: var(--ux-radius-sm); font-weight: 500;
  background: var(--ux-surface); border-color: var(--ux-border-strong); color: var(--ux-text); box-shadow: none;
}
.chat-header-actions .small-btn:hover { background: var(--ux-hover); }

.chat-messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 18px 26px 22px;
  display: flex; flex-direction: column; gap: 2px;
}

/* "Đang soạn tin nhắn..." - hien duoi ten hoi thoai trong chat-header khi co
   su kien socket "peer_typing" cho DUNG hoi thoai dang mo. */
.chat-typing {
  font-size: 12px; color: var(--primary); font-style: italic; margin-top: 2px;
  display: flex; align-items: center; gap: 5px;
}
.chat-typing::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--primary);
  animation: chat-typing-pulse 1s ease-in-out infinite;
}
@keyframes chat-typing-pulse {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 1; }
}

/* Trang thai tin nhan (Đã gửi / Đã nhận / Đã xem) - CHI hien duoi bong bong
   tin nhan OUT MOI NHAT cua hoi thoai (xem refreshLastOutStatus() trong app.js). */
.msg-status { font-size: 11px; color: var(--muted); margin: 3px 4px 0 0; align-self: flex-end; }
.msg-status.status-seen { color: var(--primary); font-weight: 600; }

/* Dai phan cach ngay ("Hôm nay" / "Hôm qua" / dd/mm/yyyy) */
.chat-date-divider { display: flex; justify-content: center; margin: 16px 0 6px; }
.chat-date-divider span {
  background: var(--chat-date-bg); color: var(--text-muted); font-size: 11.5px; font-weight: 600;
  padding: 4px 14px; border-radius: 999px; border: 1px solid var(--chat-date-border);
}

/* Moi tin nhan la 1 hang: [avatar (chi nhom, tin den)] + [ten nguoi gui + bong bong] */
.msg-row { display: flex; align-items: flex-end; gap: 8px; }
.msg-row.out { flex-direction: row-reverse; }
.msg-row.grouped { margin-top: 2px; }        /* tin ke tiep cung 1 nguoi, sat nhau hon */
.msg-row:not(.grouped) { margin-top: 10px; } /* bat dau 1 chuoi moi - cach ra chut cho de nhin */
.msg-row.msg-enter { animation: msg-in .3s var(--ease); } /* chi tin MOI den/gui (xem appendLiveMessage) */
@keyframes msg-in { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }

.msg-time { font-size: 10.5px; color: var(--muted); margin: 3px 4px 0; }
.msg-row.out .msg-time { align-self: flex-end; }
.msg-row.in .msg-time { align-self: flex-start; }

.msg-col { display: flex; flex-direction: column; max-width: min(62%, 560px); }
.msg-row.out .msg-col { align-items: flex-end; }

/* Cot avatar rong 32px du co hien anh hay khong, de bong bong luon thang hang */
.msg-avatar-slot { width: 32px; height: 32px; flex-shrink: 0; }
.msg-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; display: block; background: var(--bg-surface-muted); }
.msg-avatar-fallback {
  width: 32px; height: 32px; border-radius: 50%; color: var(--text-inverse); font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; text-transform: uppercase;
  background-image: linear-gradient(135deg, rgba(255, 255, 255, .28), rgba(0, 0, 0, .12));
}

.msg {
  max-width: 100%; padding: 9px 13px; border-radius: 18px; font-size: 14px; line-height: 1.5;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.msg-sender { font-size: 11.5px; font-weight: 600; color: var(--text-muted); margin: 0 0 3px 6px; }
.msg.in {
  background: var(--chat-bubble-in-bg); color: var(--text-primary);
  border: 1px solid var(--chat-bubble-in-border);
  box-shadow: var(--chat-bubble-in-shadow);
}
.msg-row.in:not(.grouped) .msg.in { border-top-left-radius: 6px; }
.msg.out {
  background: var(--grad); color: var(--text-inverse); border: none;
  box-shadow: var(--chat-bubble-out-shadow);
}
.msg-row.out:not(.grouped) .msg.out { border-top-right-radius: 6px; }
.msg a { color: var(--primary); }
.msg.out a { color: #fff; text-decoration: underline; }
.msg-image { max-width: 100%; max-height: 280px; border-radius: 12px; display: block; cursor: zoom-in; transition: filter .2s; }
.msg-image:hover { filter: brightness(1.05); }

/* Khung trich dan "Đang trả lời <ai đó>" BEN TRONG 1 bong bong tin nhan */
.msg-quote {
  border-left: 3px solid var(--accent);
  padding: 4px 9px; margin-bottom: 6px; border-radius: 8px;
  font-size: 12.5px; line-height: 1.35; cursor: default;
  background: var(--chat-quote-bg); overflow: hidden;
}
.msg.out .msg-quote { border-left-color: rgba(255, 255, 255, .8); background: rgba(255, 255, 255, .16); }
.msg-quote-sender { font-weight: 600; color: var(--primary); }
.msg.out .msg-quote-sender { color: #fff; }
.msg-quote-text { opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }

/* ---------- Tha bieu cam (reaction) cho tung tin nhan ---------- */
.msg-bubble-wrap { position: relative; display: inline-flex; max-width: 100%; }
.msg-row.out .msg-bubble-wrap { align-self: flex-end; }

/* Nhom nut "Trả lời" + "Thả biểu cảm" hien khi di chuot vao 1 hang tin nhan */
.msg-hover-actions {
  position: absolute; top: 50%; transform: translateY(-50%) scale(.9);
  display: flex; gap: 4px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s var(--ease);
}
.msg-row:hover .msg-hover-actions { opacity: 1; pointer-events: auto; transform: translateY(-50%) scale(1); }
.msg-row.in .msg-hover-actions { right: -70px; }
.msg-row.out .msg-hover-actions { left: -70px; flex-direction: row-reverse; }

.msg-action-btn {
  width: 29px; height: 29px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); font-size: 14px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); transition: background .2s, transform .2s var(--ease);
}
.msg-action-btn:hover { background: var(--primary-soft); transform: scale(1.12); }

/* Dai chon nhanh 6 bieu cam */
.msg-reaction-picker {
  position: absolute; bottom: calc(100% + 6px); z-index: 30;
  display: flex; gap: 2px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 7px; box-shadow: var(--shadow-lg);
  animation: pop-in .2s var(--ease);
}
@keyframes pop-in { from { opacity: 0; transform: translateY(6px) scale(.9); } to { opacity: 1; transform: none; } }
.msg-row.in .msg-reaction-picker { left: 0; }
.msg-row.out .msg-reaction-picker { right: 0; }
.msg-reaction-picker-item {
  border: none; background: transparent; font-size: 20px; padding: 3px 5px;
  border-radius: 999px; transition: transform .15s var(--ease), background .15s;
}
.msg-reaction-picker-item:hover { background: var(--surface-hover); transform: scale(1.3) translateY(-3px); }

/* Hang bieu cam da tha, sat mep duoi bong bong */
.msg-reactions { display: flex; flex-wrap: wrap; gap: 3px; margin: -8px 8px 0; position: relative; z-index: 1; }
.msg-row.out .msg-reactions { justify-content: flex-end; }
.msg-reactions.hidden { display: none; }
.msg-reaction-badge {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 1px 7px; font-size: 11.5px; cursor: pointer; box-shadow: var(--shadow);
  animation: badge-pop .3s var(--ease);
}
.msg-reaction-badge:hover { background: var(--surface-hover); }
.msg-reaction-badge.mine { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 600; }

/* Sticker DONG: sprite sheet, JS tu doi background-position - xem renderAnimatedSticker() */
.msg-sticker-anim { display: block; background-position: 0 0; image-rendering: -webkit-optimize-contrast; cursor: default; }

/* Tin nhan he thong "tong ket cuoc goi" */
.msg-call-badge { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: inherit; opacity: .95; }

/* ---------- Khu soan tin: thanh cong cu + o nhap (nen trang, noi len) ---------- */
.chat-toolbar {
  position: relative; display: flex; align-items: center; gap: 2px;
  padding: 8px 14px 0; background: var(--chat-composer-bg); flex-shrink: 0;
  border-top: 1px solid var(--border-subtle);
}
/* Nut cong cu soan tin: icon net (SVG), moi nut 1 mau rieng khi hover/dang mo */
.toolbar-btn {
  --tb: var(--primary); --tb-soft: var(--primary-soft);
  border: none; background: transparent; color: var(--chat-toolbar-icon);
  width: 38px; height: 38px; border-radius: 11px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .2s, color .2s, transform .2s var(--ease), box-shadow .2s;
}
.toolbar-btn svg { transition: transform .25s var(--ease); }
.toolbar-btn:hover { background: var(--tb-soft); color: var(--tb); transform: translateY(-2px); }
.toolbar-btn:hover svg { transform: scale(1.1); }
.toolbar-btn:active { transform: translateY(0) scale(.94); }
.tb-emoji   { --tb: #f59e0b; --tb-soft: #fff6e0; }
.tb-sticker { --tb: #ec4899; --tb-soft: #fdeaf4; }
.tb-image   { --tb: #10b981; --tb-soft: #e3f8f0; }
.tb-file    { --tb: #0068ff; --tb-soft: #e7f0ff; }
.tb-emoji:hover svg { transform: scale(1.1) rotate(-10deg); }
.tb-file:hover svg { transform: scale(1.1) rotate(12deg); }
/* Dang mo bang emoji / sticker -> giu nut o trang thai sang */
.chat-toolbar:has(#emoji-picker:not(.hidden)) .tb-emoji,
.chat-toolbar:has(#sticker-picker:not(.hidden)) .tb-sticker {
  background: var(--tb-soft); color: var(--tb); box-shadow: inset 0 0 0 1.5px var(--tb);
}
.toolbar-hint { font-size: 11.5px; color: var(--muted); margin-left: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toolbar-hint kbd {
  font-family: inherit; font-size: 10.5px; font-weight: 600; color: var(--text-2);
  background: var(--surface-2); border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 5px; padding: 0 5px;
}
@media (max-width: 900px) { .toolbar-hint { display: none; } }

/* Popover dung chung cho bang emoji va khung tim/chon sticker */
.popover {
  position: absolute; bottom: calc(100% + 10px); left: 14px; z-index: 20;
  background: var(--chat-popover-bg); border: 1px solid var(--border-default); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); padding: 10px; width: 310px;
  color: var(--text-primary);
  animation: pop-in .2s var(--ease); transform-origin: bottom left;
}
.popover.hidden { display: none; }
.popover-search { width: 100%; padding: 9px 12px; margin-bottom: 8px; font-size: 13.5px; }

/* ---------- Bang chon emoji: tieu de + tab nhom + vung cuon theo nhom ---------- */
#emoji-picker {
  display: flex; flex-direction: column; width: 352px; padding: 0; overflow: hidden;
  border-radius: 18px;
}
#emoji-picker.hidden { display: none; }
.emoji-picker-header {
  padding: 12px 16px 10px; font-size: 13.5px; font-weight: 700; color: #fff;
  background: var(--grad); letter-spacing: .01em;
}
.emoji-tabs {
  display: flex; gap: 2px; padding: 6px 8px; background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.emoji-tab {
  position: relative; flex: 1; height: 34px; border: none; background: transparent; border-radius: 9px;
  font-size: 18px; line-height: 1; filter: grayscale(1); opacity: .55;
  transition: filter .2s, opacity .2s, background .2s, transform .2s var(--ease);
}
.emoji-tab:hover { filter: none; opacity: 1; background: var(--surface); transform: translateY(-1px); }
.emoji-tab.active { filter: none; opacity: 1; background: var(--surface); box-shadow: var(--shadow-sm); }
.emoji-tab.active::after { /* gach chan gradient duoi tab dang xem */
  content: ""; position: absolute; left: 30%; right: 30%; bottom: 2px; height: 3px; border-radius: 3px; background: var(--grad);
}
.emoji-scroll { position: relative; max-height: 264px; overflow-y: auto; padding: 4px 10px 10px; scroll-behavior: smooth; }
.emoji-section-title {
  position: sticky; top: 0; z-index: 1;
  padding: 8px 4px 6px; font-size: 11px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
  color: var(--text-muted); background: var(--chat-popover-bg);
}
.emoji-grid { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 2px; }
.emoji-item {
  border: none; background: transparent; font-size: 22px; line-height: 1; aspect-ratio: 1; padding: 0; min-width: 0;
  border-radius: 10px; display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .15s var(--ease);
}
.emoji-item:hover { background: var(--primary-soft); transform: scale(1.28); }
.emoji-item:active { transform: scale(1.05); }

.sticker-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; max-height: 240px; overflow-y: auto; }
.sticker-thumb {
  width: 100%; height: 80px; object-fit: contain; cursor: pointer;
  border-radius: var(--radius-sm); background: var(--surface-2); transition: background .2s, transform .2s var(--ease);
}
.sticker-thumb:hover { background: var(--primary-soft); transform: scale(1.05); }

/* Dai goi y nhan dan theo noi dung dang go */
.sticker-suggest-row {
  display: flex; gap: 6px; padding: 8px 16px; overflow-x: auto;
  border-top: 1px solid var(--border-subtle); background: var(--chat-composer-bg); flex-shrink: 0;
}
.sticker-suggest-row.hidden { display: none; }
.sticker-suggest-thumb {
  width: 54px; height: 54px; object-fit: contain; cursor: pointer;
  border-radius: var(--radius); background: var(--surface-2); flex-shrink: 0; border: 1px solid var(--border);
  transition: border-color .2s, background .2s, transform .2s var(--ease);
}
.sticker-suggest-thumb:hover { background: var(--primary-soft); border-color: var(--primary-soft-2); transform: translateY(-2px); }

/* Thanh xem truoc "Đang trả lời <ai đó>: <nội dung>" phia tren o nhap tin */
.reply-preview-row {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  border-top: 1px solid var(--border-subtle); background: var(--accent-soft); flex-shrink: 0;
  animation: pop-in .2s var(--ease);
}
.reply-preview-row.hidden { display: none; }
.reply-preview-content { flex: 1; min-width: 0; border-left: 3px solid var(--primary); padding-left: 10px; }
.reply-preview-sender { font-size: 12px; font-weight: 600; color: var(--primary); }
.reply-preview-text { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reply-preview-row .link-btn {
  font-size: 14px; width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
  color: var(--muted); display: inline-flex; align-items: center; justify-content: center;
}
.reply-preview-row .link-btn:hover { background: var(--bg-surface); color: var(--text-primary); text-decoration: none; }

.chat-input-row {
  display: flex; padding: 8px 16px 16px; gap: 10px; background: var(--chat-composer-bg); flex-shrink: 0; align-items: center;
}
.chat-input-row input#chat-input {
  flex: 1; min-width: 0; padding: 12px 18px; border-radius: 999px;
  background: var(--chat-input-bg); border-color: var(--border-default);
}
.chat-input-row input#chat-input:focus { background: var(--bg-surface); }
.chat-input-row button {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 11px 20px; background: var(--grad); color: var(--text-inverse); border: none; border-radius: 999px;
  font-size: 14px; font-weight: 600;
  box-shadow: var(--glow);
  transition: transform .2s var(--ease), box-shadow .2s, filter .2s;
}
.chat-input-row button svg { transition: transform .25s var(--ease); }
.chat-input-row button:hover { transform: translateY(-1px); box-shadow: 0 12px 26px rgba(0, 104, 255, .4); filter: brightness(1.06); }
.chat-input-row button:hover svg { transform: translate(2px, -2px) rotate(8deg); }
.chat-input-row button:active { transform: scale(.96); }
