/* ==========================================================================
   MYVIBE CHATBOX MODULE - REALTIME SYNC (MULTI-THREAD)
   ========================================================================== */

#mv-chat-widget { position: fixed; bottom: 30px; right: 30px; z-index: 10001; font-family: 'Inter', sans-serif; }
#mv-chat-widget svg { pointer-events: none; } /* Fix lỗi extension indexOf */

/* Nút Toggle Floating */
#mv-chat-toggle { width: 60px; height: 60px; border-radius: 50%; background: var(--vibe-gradient); color: white; border: none; box-shadow: 0 4px 20px rgba(108, 46, 231, 0.4); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); position: relative; }
#mv-chat-toggle:hover { transform: translateY(-5px) scale(1.05); box-shadow: 0 8px 25px rgba(108, 46, 231, 0.6); }
#mv-chat-toggle svg { width: 28px; height: 28px; transition: 0.3s; position: absolute; max-width: 100%; }
#mv-chat-toggle .icon-close { opacity: 0; transform: scale(0) rotate(-90deg); }
#mv-chat-widget.active #mv-chat-toggle .icon-chat { opacity: 0; transform: scale(0) rotate(90deg); }
#mv-chat-widget.active #mv-chat-toggle .icon-close { opacity: 1; transform: scale(1) rotate(0); }

/* Hộp Chat (Chat Box Container) */
#mv-chat-box { position: absolute; bottom: 80px; right: 0; width: 360px; height: 550px; max-height: 75vh; background: #ffffff; border-radius: 20px; box-shadow: 0 10px 40px -10px rgba(0,0,0,0.2); display: flex; flex-direction: column; overflow: hidden; opacity: 0; visibility: hidden; transform: translateY(20px) scale(0.95); transform-origin: bottom right; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); border: none !important; }
#mv-chat-widget.active #mv-chat-box { opacity: 1; visibility: visible; transform: translateY(0) scale(1); }

/* Chat Header */
.mv-chat-header { background: var(--vibe-gradient); color: white; padding: 16px 20px; display: flex; align-items: center; gap: 12px; }
#mv-chat-back-btn { transition: transform 0.2s ease, opacity 0.2s ease; }
#mv-chat-back-btn:hover { transform: translateX(-3px); opacity: 0.8; }
.mv-chat-header-avatar { width: 40px; height: 40px; background: rgba(255,255,255,0.2); border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.mv-chat-header-info { min-width: 0; flex: 1; }
.mv-chat-header-info h4 { margin: 0 0 2px 0; font-size: 15px; font-weight: 700; color: white; }
.mv-chat-status-dot { width: 8px; height: 8px; background: #4ade80; border-radius: 50%; display: inline-block; box-shadow: 0 0 8px #4ade80; }
#chat-header-topic { white-space: normal !important; max-width: none !important; overflow: visible !important; }

/* Chat Tools Buttons */
.mv-chat-tool-btn { background: rgba(255, 255, 255, 0.15); border: none; color: white; cursor: pointer; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1); padding: 0; flex-shrink: 0; }
.mv-chat-tool-btn:hover { background: rgba(255, 255, 255, 0.35); transform: translateY(-2px); box-shadow: 0 4px 10px rgba(0,0,0,0.15); }
.mv-chat-tool-btn.btn-danger:hover { background: #ef4444; box-shadow: 0 4px 10px rgba(239, 68, 68, 0.3); }
.mv-chat-tool-btn svg { width: 16px; height: 16px; }

/* Màn hình Nhập Thông Tin (Cho Khách mới) */
#mv-chat-auth { padding: 30px 24px; text-align: center; flex: 1; background: #f8fafc; }
#mv-chat-auth h3 { font-size: 1.1rem; color: var(--text-main); margin-bottom: 10px; }
#mv-chat-auth p { font-size: 0.9rem; color: var(--text-light); margin-bottom: 24px; }
.mv-auth-input { width: 100%; padding: 12px 16px; border-radius: 12px; border: 1px solid #e2e8f0; margin-bottom: 12px; font-size: 0.95rem; font-family: 'Inter', sans-serif; }
.mv-auth-input:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px rgba(13, 62, 154, 0.1); }

/* =================================================
   KHÓA CHẶT LAYOUT TRÁNH VỠ
   ================================================= */
#mv-chat-widget #mv-chat-core { flex: 1 1 0% !important; flex-direction: column !important; overflow: hidden !important; height: 100% !important; width: 100% !important; }
#mv-chat-widget.active #mv-chat-core[style*="display: flex"], #mv-chat-widget.active #mv-chat-core[style*="display: block"] { display: flex !important; }
#mv-chat-widget #mv-chat-messages { flex: 1 1 0% !important; overflow-y: auto !important; display: flex !important; flex-direction: column !important; padding: 20px !important; background: #f8fafc !important; gap: 12px !important; height: 100% !important; }
#mv-chat-widget #mv-chat-messages::-webkit-scrollbar { width: 6px; }
#mv-chat-widget #mv-chat-messages::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 10px; }
#mv-chat-widget #mv-chat-messages::before { content: ""; margin-top: auto !important; }
#mv-chat-widget #mv-chat-footer { flex-shrink: 0 !important; border-top: 1px solid var(--border) !important; background: #fff !important; z-index: 10 !important; margin-top: 0 !important; padding: 12px 16px !important; display: flex !important; align-items: center !important; gap: 8px !important; }

/* Bong bóng chat */
.mv-msg { max-width: 85%; padding: 10px 14px; border-radius: 16px; font-size: 0.95rem; line-height: 1.5; animation: chatFadeIn 0.3s ease; word-wrap: break-word; flex-shrink: 0; overflow: visible !important; position: relative; transition: margin-bottom 0.2s ease; }
@keyframes chatFadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.mv-msg.has-reaction { margin-bottom: 18px !important; }
.mv-msg:not(.has-reaction) { margin-bottom: 4px; }
.mv-msg-system { background: #ffffff; color: var(--text-body); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; box-shadow: 0 2px 5px rgba(0,0,0,0.02); }
.mv-msg-user { background: var(--primary); color: white; align-self: flex-end; border-bottom-right-radius: 4px; box-shadow: 0 2px 5px rgba(13, 62, 154, 0.2); }
.mv-chat-time { font-size: 10px; opacity: 0.7; margin-top: 6px; text-align: right; }
.mv-msg-system .mv-chat-time { text-align: left; }
.mv-msg img { max-width: 100%; height: auto; border-radius: 8px; margin-top: 5px; }

/* Form nhập nội dung Footer */
.mv-chat-input-wrapper { flex: 1; background: #f1f5f9; border-radius: 20px; padding: 8px 16px; display: flex; align-items: center; }
.mv-chat-input-wrapper input { width: 100%; border: none; background: transparent; outline: none; font-size: 0.95rem; color: var(--text-main); font-family: 'Inter', sans-serif; }
.mv-chat-send-btn { background: var(--primary); color: white; border: none; width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: 0.2s; flex-shrink: 0; }
.mv-chat-send-btn:hover { background: var(--primary-dark); transform: scale(1.05); }
.mv-chat-send-btn:disabled { background: #cbd5e1; cursor: not-allowed; transform: none; }

/* Typing Indicator */
.mv-typing-indicator { display: none; align-items: center; gap: 4px; padding: 12px 16px; background: #ffffff; border: 1px solid var(--border); border-radius: 16px; border-bottom-left-radius: 4px; align-self: flex-start; width: fit-content; margin-bottom: 10px; flex-shrink: 0; }
.mv-typing-indicator span { width: 6px; height: 6px; background: #94a3b8; border-radius: 50%; animation: typingBounce 1.4s infinite ease-in-out both; }
.mv-typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.mv-typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
@keyframes typingBounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } }

/* =================================================
   TIN NHẮN MẪU (CANNED RESPONSES)
   ================================================= */
.mv-canned-item { background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 16px; padding: 16px; position: relative; display: flex; flex-direction: column; gap: 12px; transition: all 0.3s ease; }
.mv-canned-item:focus-within { border-color: var(--primary); background: #fff; box-shadow: 0 4px 20px rgba(13, 62, 154, 0.08); }
.mv-canned-input-row { display: flex; align-items: center; width: 100%; gap: 12px; }
.mv-canned-input-group { display: flex; align-items: center; flex: 1; background: #fff; border: 1px solid #e2e8f0; border-radius: 10px; overflow: hidden; transition: 0.2s; height: 42px; }
.mv-canned-input-group:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(13, 62, 154, 0.1); }
.mv-canned-prefix { flex: 0 0 44px !important; width: 44px !important; max-width: 44px !important; height: 100%; display: flex; align-items: center; justify-content: center; background: #f1f5f9; color: #94a3b8; font-weight: 800; font-family: monospace; border-right: 1px solid #e2e8f0; font-size: 1.05rem; user-select: none; box-sizing: border-box; padding: 0; margin: 0; }
.mv-canned-input-short { flex: 1; min-width: 0; border: none; padding: 0 12px; outline: none; font-family: monospace; font-size: 0.95rem; font-weight: 600; color: var(--text-main); background: transparent; height: 100%; box-sizing: border-box; }
.mv-canned-input-short::placeholder { color: #cbd5e1; font-weight: 400; font-family: 'Inter', sans-serif; }
.mv-canned-input-text { width: 100%; padding: 12px 16px; border: 1px solid #e2e8f0; border-radius: 10px; font-size: 0.95rem; resize: vertical; min-height: 80px; font-family: 'Inter', sans-serif; transition: 0.2s; outline: none; background: #fff; color: var(--text-body); box-sizing: border-box; }
.mv-canned-input-text:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(13, 62, 154, 0.1); }
.mv-canned-input-text::placeholder { color: #94a3b8; }
.del-canned-btn { color: #ef4444; background: #fee2e2; border: none; width: 42px; height: 42px; border-radius: 50px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; flex-shrink: 0; }
.del-canned-btn:hover { background: #ef4444; color: #fff; transform: scale(1.05); box-shadow: 0 4px 10px rgba(239, 68, 68, 0.2); }
.mv-suggestion-item { padding: 10px 16px; border-bottom: 1px solid #f1f5f9; cursor: pointer; transition: 0.2s; display: flex; flex-direction: column; gap: 4px; }
.mv-suggestion-item:last-child { border-bottom: none; }
.mv-suggestion-item:hover, .mv-suggestion-item.active { background: #eff6ff; }
.mv-suggestion-short { font-size: 0.8rem; font-weight: 700; color: var(--primary); font-family: monospace; }
.mv-suggestion-text { font-size: 0.85rem; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* =================================================
   DANH SÁCH CHAT
   ================================================= */
.mv-chat-list-del-btn { width: 32px; height: 32px; border-radius: 50%; background: #fee2e2; color: #ef4444; border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; opacity: 0; transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1); flex-shrink: 0; }
.mv-chat-list-del-btn:hover { background: #ef4444; color: white; transform: scale(1.1); box-shadow: 0 4px 10px rgba(239, 68, 68, 0.3); }
@media (hover: none) { .mv-chat-list-del-btn { opacity: 1; background: transparent; } }

/* Admin List */
#mv-admin-chat-list { flex: 1; overflow-y: auto; background: #fff; flex-direction: column; }
.mv-admin-chat-item { padding: 15px 20px; border-bottom: 1px solid #f1f5f9; cursor: pointer; display: flex; align-items: center; gap: 12px; transition: 0.2s; }
.mv-admin-chat-item:hover { background: #f8fafc; }
.mv-admin-chat-item.unread { background: #eff6ff; }
.mv-admin-chat-item:hover .mv-chat-list-del-btn { opacity: 1; }
.mv-admin-avatar { width: 40px; height: 40px; background: #e0e7ff; color: var(--primary); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; flex-shrink: 0; }
.mv-admin-info { flex: 1; min-width: 0; }
.mv-admin-name { font-size: 0.95rem; font-weight: 700; color: var(--text-main); margin-bottom: 2px; display: flex; justify-content: space-between; }
.mv-admin-time { font-size: 0.75rem; color: #94a3b8; font-weight: 400; }
.mv-admin-msg { font-size: 0.85rem; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 4px; }
.mv-admin-chat-item.unread .mv-admin-name { color: var(--primary); }
.mv-admin-chat-item.unread .mv-admin-msg { font-weight: 600; color: var(--text-body); }

/* User List */
.mv-user-chat-item { padding: 15px 20px; border-bottom: 1px solid #f1f5f9; cursor: pointer; display: flex; align-items: center; gap: 12px; transition: 0.2s; background: #fff; }
.mv-user-chat-item:hover { background: #eff6ff; }
.mv-user-chat-item.unread { background: #e0e7ff; }
.mv-user-chat-item.unread .mv-user-name { color: var(--primary); }
.mv-user-chat-item.unread .mv-user-msg { font-weight: 600; color: var(--text-body); }
.mv-user-chat-item:hover .mv-chat-list-del-btn { opacity: 1; }
.mv-user-avatar { width: 40px; height: 40px; background: var(--vibe-gradient); color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; flex-shrink: 0; }
.mv-user-info { flex: 1; min-width: 0; }
.mv-user-name { font-size: 0.95rem; font-weight: 700; color: var(--text-main); margin-bottom: 2px; display: flex; justify-content: space-between; }
.mv-user-time { font-size: 0.75rem; color: #94a3b8; font-weight: 400; }
.mv-user-msg { font-size: 0.85rem; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; justify-content: space-between; align-items: center; margin-top: 4px;}

@media (max-width: 768px) { #mv-chat-widget { bottom: calc(env(safe-area-inset-bottom) + 80px); right: 16px; } #mv-chat-box { width: calc(100vw - 32px); height: 480px; right: 0; } }

/* =================================================
   DYNAMIC CSS EXTRACTED FROM JS
   ================================================= */
.mv-chat-unread-badge { position: absolute; top: -4px; right: -4px; background: #ef4444; color: white; font-size: 12px; font-weight: 800; min-width: 22px; height: 22px; padding: 0 5px; border-radius: 12px; display: flex; align-items: center; justify-content: center; border: 2px solid white; box-shadow: 0 2px 5px rgba(0,0,0,0.2); z-index: 10; pointer-events: none; }
.mv-chat-status-badge { padding: 2px 6px; border-radius: 4px; font-size: 10px; margin-left: 6px; font-weight: 600; }
.mv-chat-status-badge.closed { background: #f1f5f9; color: #94a3b8; }
.mv-chat-status-badge.deleted { background: #fee2e2; color: #ef4444; }
.mv-chat-status-badge.open { background: #dcfce7; color: #16a34a; }
.mv-chat-name-wrap { display: flex; align-items: center; gap: 6px; }
.mv-chat-snippet-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.mv-chat-topic-tag { color: var(--primary); font-size: 0.8rem; font-weight: 700; margin-right: 4px;}
.unread-dot { width: 8px; height: 8px; background-color: var(--primary); border-radius: 50%; flex-shrink: 0; box-shadow: 0 0 6px rgba(13, 62, 154, 0.4); }
.mv-empty-state { text-align: center; padding: 40px 20px; color: #94a3b8; font-size: 0.95rem; }
.mv-canned-empty { background: #fff; border-radius: 16px; border: 1px dashed #cbd5e1; }
.mv-canned-empty svg { margin-bottom: 12px; }

/* Tin nhắn mở rộng (Reply, Reaction, Actions) */
.mv-msg-reply { font-size: 0.8rem; background: rgba(0,0,0,0.05); padding: 6px 10px; border-radius: 8px; margin-bottom: 6px; opacity: 0.8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mv-msg-user .mv-msg-reply { border-left: 3px solid rgba(255,255,255,0.5); }
.mv-msg-system .mv-msg-reply { border-left: 3px solid var(--primary); }
.mv-msg-reaction { position: absolute; bottom: -12px; background: white; border-radius: 50%; padding: 3px; box-shadow: 0 2px 6px rgba(0,0,0,0.15); display: flex; align-items: center; justify-content: center; z-index: 5; border: 1px solid #f1f5f9; }
.mv-msg-user .mv-msg-reaction { right: 16px; }
.mv-msg-system .mv-msg-reaction { left: 16px; }
.mv-msg-actions { position: absolute; top: 50%; transform: translateY(-50%); display: flex; gap: 4px; opacity: 0; transition: 0.2s; pointer-events: none; z-index: 10; }
.mv-msg-user .mv-msg-actions { right: 100%; padding-right: 8px; }
.mv-msg-system .mv-msg-actions { left: 100%; padding-left: 8px; }
.mv-msg:hover .mv-msg-actions { opacity: 1 !important; pointer-events: auto !important; }
.mv-msg-action-btn { background: white; border: 1px solid #e2e8f0; border-radius: 50%; width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; cursor: pointer; color: #64748b; transition: 0.2s; }
.mv-msg-action-btn:hover { background: #f8fafc !important; color: var(--primary) !important; border-color: var(--primary) !important; transform: scale(1.05); }

/* [NEW] Style cho tính năng Thu Hồi Tin Nhắn */
.mv-msg-action-btn.btn-recall { color: #ef4444; }
.mv-msg-action-btn.btn-recall:hover { background: #fef2f2 !important; color: #dc2626 !important; border-color: #fca5a5 !important; }
.msg-is-recalled { background: transparent !important; border: 1px solid #e2e8f0 !important; box-shadow: none !important; }
.msg-is-recalled.mv-msg-user { border-color: rgba(13, 62, 154, 0.2) !important; color: var(--text-body); }
.msg-is-recalled.mv-msg-system { border-color: #e2e8f0 !important; }
.msg-is-recalled .mv-chat-time { color: #94a3b8; }

/* Khối xem trước Reply */
.mv-chat-reply-preview { background: #f8fafc; border-top: 1px solid #e2e8f0; padding: 10px 16px; display: flex; justify-content: space-between; align-items: center; font-size: 0.85rem; color: #64748b; }
.mv-chat-reply-preview strong { color: var(--text-main); margin-left: 4px; }
.mv-chat-reply-preview .close-reply { cursor: pointer; color: #ef4444; font-weight: bold; padding: 4px; font-size: 1rem; line-height: 1; border-radius: 4px; transition: 0.2s; }
.mv-chat-reply-preview .close-reply:hover { background: #fee2e2; }

/* Các tin nhắn đặc biệt */
.mv-chat-spinner-msg { background: transparent !important; border: none !important; text-align: center; box-shadow: none !important; align-self: center; margin-top: 20px; }
.mv-chat-seen-status { background: transparent !important; border: none !important; box-shadow: none !important; align-self: flex-end; padding: 0 14px 0 0 !important; margin-top: -8px; margin-bottom: 12px !important; font-size: 0.75rem !important; color: #94a3b8 !important; max-width: 100%; display: flex; align-items: center; gap: 2px; }
.mv-msg-error { background: #fee2e2 !important; color: #ef4444 !important; border-color: #fca5a5 !important; }

/* Notice đóng chat */
.mv-chat-closed-text { color: #ef4444; font-size: 0.9rem; margin-bottom: 0; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 6px; }

/* Menu chuột phải (Context Menu) */
#mv-chat-context-menu { position: fixed; z-index: 999999; background: #fff; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.15); border: 1px solid #e2e8f0; padding: 8px; min-width: 230px; opacity: 0; visibility: hidden; transform: scale(0.95); transform-origin: top left; transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s; }
.mv-ctx-item { padding: 10px 16px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 12px; font-size: 0.95rem; font-family: 'Inter', sans-serif; transition: 0.2s; white-space: nowrap; color: var(--text-body); user-select: none; }
.mv-ctx-item:hover { background: #f8fafc; color: var(--primary); }
.mv-ctx-item svg { width: 16px; height: 16px; transition: 0.2s; }
.mv-ctx-divider { height: 1px; background: #f1f5f9; margin: 6px 0; }
.mv-chat-ctx-delete { color: #ef4444; }
.mv-chat-ctx-delete:hover { background: #fef2f2; color: #dc2626; }
.mv-chat-ctx-delete svg { stroke: #ef4444; }

/* Ô check xóa vĩnh viễn */
.mv-chat-delete-everyone-wrap { margin-top: 15px; text-align: left; background: #fef2f2; padding: 12px; border-radius: 12px; border: 1px solid #fee2e2; }
.mv-chat-delete-everyone-label { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; color: #ef4444; font-weight: 600; margin: 0; }
.mv-chat-delete-everyone-checkbox { width: 18px; height: 18px; cursor: pointer; accent-color: #ef4444; }