:root {
    --primary: #2ecc71;
    --sidebar-bg: #ffffff;
    --chat-bg: #f0f2f5;
    --border: #e4e6eb;
    --text-main: #050505;
    --text-muted: #65676b;
    --sent-bg: #ffffff;
    --sent-text: #050505;
    --received-bg: #ffffff;
    --received-text: #050505;
    --accent-green: #00a859;
}

body { margin: 0; font-family: 'Inter', sans-serif; height: 100vh; overflow: hidden; background: var(--chat-bg); color: var(--text-main); }
.app-container { display: flex; height: 100vh; width: 100vw; }

/* Sidebar */
.sidebar { width: 400px; min-width: 220px; max-width: 700px; background: var(--sidebar-bg); border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }

.sidebar-resizer { width: 5px; cursor: col-resize; background: transparent; flex-shrink: 0; transition: background 0.15s; z-index: 10; }
.sidebar-resizer:hover, .sidebar-resizer.dragging { background: #1565c0; }
.sidebar-header { padding: 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.sidebar-header h2 { margin: 0; font-size: 1.25rem; font-weight: 600; color: #050505; }
.nav-buttons a { text-decoration: none; font-size: 0.8rem; color: var(--text-muted); margin-left: 10px; background: #f0f2f5; padding: 6px 12px; border-radius: 6px; font-weight: 500; }
.nav-buttons a:hover { color: #00a859; background: #e4e6eb; }

.contact-list { flex: 1; overflow-y: auto; }
.contact-item { display: flex; padding: 15px 20px; cursor: pointer; transition: background 0.2s; border-bottom: 1px solid #f0f2f5; align-items: flex-start; }
.contact-item:hover { background: #f5f6f7; }
.contact-item.active { background: #e7f3ff; }

.contact-avatar { width: 46px; height: 46px; border-radius: 50%; margin-right: 15px; flex-shrink: 0; }
.contact-avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; border: 1px solid #ddd; }

.contact-info { flex: 1; min-width: 0; }
.contact-name-row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 2px; }
.contact-name { font-weight: 500; font-size: 1rem; color: #050505; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact-time { font-size: 0.8rem; color: #65676b; font-weight: 500; }

.contact-last-msg-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.contact-last-msg { font-size: 0.9rem; font-weight: 400; color: #050505; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 10px; }
.contact-last-msg.has-unread { font-weight: 700; color: #1c1e21; }
.unread-badge { min-width: 20px; height: 20px; background: #e74c3c; border-radius: 10px; padding: 0 6px; display: flex; align-items: center; justify-content: center; font-size: 0.72rem; font-weight: 700; color: white; flex-shrink: 0; }

/* Bot Pill Badge */
.bot-pill { display: inline-flex; align-items: center; background: #f0f2f5; padding: 4px 10px 4px 5px; border-radius: 20px; border: 1px solid #e4e6eb; }
.bot-pill img { width: 20px; height: 20px; border-radius: 50%; margin-right: 8px; border: 1px solid #ddd; }
.bot-pill span { font-size: 0.85rem; color: #050505; font-weight: 500; }
.bot-pill.bot-pill-internal { background: #e3f2fd; border-color: #bbdefb; padding-left: 10px; }
.bot-pill.bot-pill-internal span { color: #1565c0; font-weight: 600; }
.bot-pill-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.mute-icon { font-size: 0.75rem; opacity: 0.7; }
.category-pill { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; color: #fff; }
.category-select { font-size: 0.82rem; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--border); background: #f0f2f5; color: #333; cursor: pointer; }
.sidebar-toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.sidebar-toolbar input { flex: 1; min-width: 0; border: 1px solid var(--border); border-radius: 20px; padding: 6px 12px; font-size: 0.85rem; outline: none; background: #f0f2f5; }
.sidebar-toolbar input:focus { border-color: #1877f2; background: #fff; }
.sidebar-toolbar select { flex-shrink: 0; font-size: 0.82rem; padding: 5px 8px; border-radius: 8px; border: 1px solid var(--border); background: #f0f2f5; color: #333; cursor: pointer; }

/* Chat Area */
.chat-area { flex: 1; display: flex; flex-direction: column; background: #ffffff; position: relative; }
.chat-header { padding: 15px 25px; background: white; border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.chat-header-info h3 { margin: 0; font-size: 1.05rem; color: #050505; }
.chat-header-info p { margin: 4px 0 0; font-size: 0.8rem; color: var(--text-muted); }

.messages-container { flex: 1; overflow-y: auto; padding: 20px 25px; display: flex; flex-direction: column; background: #f0f2f5; }
.message-row { display: flex; margin-bottom: 15px; max-width: 80%; align-items: center; }
.message-row.msg-highlight { animation: msg-highlight-fade 2s ease forwards; }
@keyframes msg-highlight-fade { 0%,30% { outline: 2px solid #1877f2; border-radius: 12px; } 100% { outline: 2px solid transparent; } }
.message-row.sent { align-self: flex-end; flex-direction: row-reverse; }
.message-row.received { align-self: flex-start; }

.msg-bubble { padding: 12px 16px; border-radius: 18px; font-size: 0.95rem; line-height: 1.4; position: relative; }
.sent .msg-bubble { background: #ffffff; color: #050505; border-bottom-right-radius: 4px; border: 1px solid #e4e6eb; }
.received .msg-bubble { background: #ffffff; color: #050505; border-bottom-left-radius: 4px; border: 1px solid #e4e6eb; }

.msg-time { font-size: 0.7rem; color: #8a8d91; margin-top: 6px; display: block; }
.sent .msg-time { text-align: right; color: var(--text-muted); }

.chat-input-area { padding: 12px 20px; background: white; border-top: 1px solid var(--border); display: flex; align-items: flex-end; gap: 4px; }
.chat-input { flex: 1; background: #f0f2f5; border: none; padding: 10px 16px; border-radius: 20px; outline: none; font-size: 1rem; color: black; resize: none; overflow-y: auto; max-height: 160px; line-height: 1.4; font-family: inherit; display: block; }
.send-btn { background: none; border: none; color: #1c1e21; cursor: pointer; padding: 0 15px; font-size: 1.6rem; transition: transform 0.2s; }
.send-btn:hover { transform: scale(1.1); }
.img-btn { background: none; border: none; cursor: pointer; font-size: 1.4rem; padding: 0 8px; transition: transform 0.2s; }
.img-btn:hover { transform: scale(1.1); }
.msg-image { max-width: 240px; max-height: 240px; border-radius: 10px; cursor: pointer; display: block; }
.sent .msg-bubble .msg-image { border-radius: 10px 10px 2px 10px; }
.received .msg-bubble .msg-image { border-radius: 10px 10px 10px 2px; }
.msg-image-title { font-size: 0.82rem; color: #555; margin-top: 5px; max-width: 240px; word-break: break-word; white-space: pre-wrap; }
.msg-video { max-width: 280px; max-height: 200px; border-radius: 10px; display: block; background: #000; cursor: pointer; }
.msg-sticker { width: 130px; height: 130px; object-fit: contain; display: block; }
.msg-file { display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; background: rgba(0,0,0,0.06); border-radius: 8px; color: inherit; text-decoration: none; font-size: 0.9rem; max-width: 260px; word-break: break-all; }
.msg-file:hover { background: rgba(0,0,0,0.12); }
.sent .msg-bubble .msg-video { border-radius: 10px 10px 2px 10px; }
.received .msg-bubble .msg-video { border-radius: 10px 10px 10px 2px; }

.no-chat-selected { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-muted); background: white; }
.no-chat-selected i { font-size: 4rem; margin-bottom: 15px; opacity: 0.1; }
.group-avatar { position: relative; width: 46px; height: 46px; flex-shrink: 0; }
.group-avatar img { position: absolute; width: 28px; height: 28px; border-radius: 50%; object-fit: cover; border: 2px solid white; }
.group-avatar img:nth-child(1) { top: 0; left: 0; }
.group-avatar img:nth-child(2) { bottom: 0; right: 0; }
.group-avatar img:nth-child(3) { top: 0; right: 0; width: 22px; height: 22px; }
.group-avatar-1 img { width: 40px; height: 40px; position: static; border: none; }
.group-avatar-placeholder { width: 46px; height: 46px; border-radius: 50%; background: #ccc; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; flex-shrink: 0; }
.msg-body { display: flex; flex-direction: column; }
.sender-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.sender-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.sender-avatar-placeholder { width: 32px; height: 32px; border-radius: 50%; background: #b0b3b8; display: flex; align-items: center; justify-content: center; font-size: 0.8rem; color: white; font-weight: 600; flex-shrink: 0; }
.sender-name { font-size: 0.78rem; color: #65676b; font-weight: 600; }
.internal-badge { font-size: 0.65rem; background: #e3f2fd; color: #1565c0; padding: 1px 6px; border-radius: 8px; font-weight: 600; margin-left: 5px; vertical-align: middle; }
.internal-avatar { width: 46px; height: 46px; border-radius: 50%; background: #1565c0; display: flex; align-items: center; justify-content: center; font-size: 1.3rem; flex-shrink: 0; color: white; }
.header-actions { margin-left: auto; display: flex; gap: 8px; }
.btn-sm { font-size: 0.8rem; padding: 5px 12px; border-radius: 6px; border: none; cursor: pointer; font-weight: 600; }

/* Reaction picker — hiện xuống dưới để không bị clip bởi overflow container */
.reaction-picker {
    display: none;
    position: absolute;
    top: 100%;
    padding: 8px 10px 5px;  /* padding-top lấp khoảng hở để hover liền mạch */
    background: white;
    border-radius: 24px;
    box-shadow: 0 2px 14px rgba(0,0,0,0.15);
    gap: 2px;
    z-index: 100;
    white-space: nowrap;
    border: 1px solid #e4e6eb;
    left: 0;
}
.message-row:hover .reaction-picker { display: flex; }
.message-row.sent .msg-bubble .reaction-picker { left: auto; right: 0; }
.reaction-emoji { font-size: 1.25rem; cursor: pointer; padding: 3px 5px; border-radius: 50%; transition: transform 0.15s; user-select: none; }
.reaction-emoji:hover { transform: scale(1.5) translateY(-4px); }
.picker-divider { width: 1px; background: #e4e6eb; margin: 4px 4px; align-self: stretch; }
.reply-in-picker { font-size: 1.1rem; }

.reactions-display { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 5px; }
.reaction-badge { background: white; border: 1px solid #e4e6eb; border-radius: 12px; padding: 2px 7px; font-size: 0.82rem; display: flex; align-items: center; gap: 3px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.sent .reactions-display { justify-content: flex-end; }
.sent .reaction-badge { background: rgba(255,255,255,0.2); border-color: rgba(255,255,255,0.3); color: white; }

/* Reply banner */
.reply-banner { display: none; padding: 8px 15px; background: #f0f2f5; border-top: 1px solid var(--border); align-items: center; justify-content: space-between; font-size: 0.85rem; gap: 8px; }
.reply-banner.show { display: flex; }
.reply-banner-inner { overflow: hidden; flex: 1; }
.reply-banner-name { font-weight: 600; color: #1c1e21; font-size: 0.8rem; margin-bottom: 2px; }
.reply-banner-content { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #65676b; font-size: 0.82rem; }
.reply-cancel { background: none; border: none; font-size: 1.1rem; cursor: pointer; color: #65676b; padding: 2px 6px; flex-shrink: 0; }

/* Quote inside bubble */
.msg-quote { background: rgba(0,0,0,0.06); padding: 6px 10px; border-left: 3px solid #65676b; border-radius: 4px; margin-bottom: 6px; font-size: 0.82rem; }
.sent .msg-quote { background: rgba(255,255,255,0.2); border-left-color: rgba(255,255,255,0.7); }
.msg-quote-name { font-weight: 600; margin-bottom: 2px; }
.msg-quote-content { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: 0.85; }

.btn-blue { background: #1565c0; color: white; }
.btn-blue:hover { background: #0d47a1; }
.btn-green { background: #00a859; color: white; }
.btn-green:hover { background: #007a40; }
.btn-danger { background: #e53935; color: white; }
.btn-danger:hover { background: #b71c1c; }

/* Modal */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 1000; justify-content: center; align-items: center; }
.modal-overlay.open { display: flex; }
.modal-box { background: white; border-radius: 12px; padding: 24px; width: 400px; max-height: 80vh; display: flex; flex-direction: column; box-shadow: 0 8px 32px rgba(0,0,0,0.2); }
.modal-box h3 { margin: 0 0 16px; font-size: 1.1rem; }
.modal-box input[type=text] { width: 100%; box-sizing: border-box; padding: 8px 12px; border: 1px solid #ddd; border-radius: 6px; font-size: 0.95rem; margin-bottom: 12px; }
.user-pick-list { flex: 1; overflow-y: auto; border: 1px solid #eee; border-radius: 8px; max-height: 240px; }
.user-pick-item { display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; }
.user-pick-item:hover { background: #f5f5f5; }
.user-initial { width: 36px; height: 36px; border-radius: 50%; background: #1565c0; color: white; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.95rem; flex-shrink: 0; }
.modal-actions { margin-top: 14px; display: flex; gap: 8px; justify-content: flex-end; }

/* Forward modal */
.forward-msg-preview { background: #f0f2f5; border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; border-left: 3px solid #1565c0; }
.forward-preview-from { font-size: 0.75rem; color: #888; font-weight: 600; margin-bottom: 3px; }
.forward-preview-content { font-size: 0.88rem; color: #444; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.forward-contact-list { flex: 1; overflow-y: auto; border: 1px solid #eee; border-radius: 8px; max-height: 280px; }
.forward-section-title { padding: 7px 14px 4px; font-size: 0.72rem; font-weight: 700; color: #888; text-transform: uppercase; letter-spacing: 0.5px; background: #f8f9fa; border-bottom: 1px solid #f0f0f0; }
.forward-contact-item { display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; transition: background 0.1s; }
.forward-contact-item:hover { background: #f5f5f5; }
.forward-contact-item.selected { background: #e3f2fd; }
.forward-avatar { width: 36px; height: 36px; border-radius: 50%; background: #1565c0; color: white; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.95rem; flex-shrink: 0; overflow: hidden; }
.forward-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.forward-name { flex: 1; font-size: 0.92rem; font-weight: 500; }
.forward-check { font-size: 1rem; color: #1565c0; font-weight: 700; }

/* Clickable links inside messages */
.msg-link { text-decoration: underline; word-break: break-all; }

/* Date separator */
.date-separator {
    display: flex;
    align-items: center;
    text-align: center;
    margin: 12px 0;
    gap: 10px;
}
.date-separator::before,
.date-separator::after {
    content: '';
    flex: 1;
    border-top: 1px solid #ddd;
}
.date-separator span {
    font-size: 0.75rem;
    color: var(--text-muted);
    background: #f0f2f5;
    padding: 2px 10px;
    border-radius: 10px;
    white-space: nowrap;
}

/* RAG suggestion chips */
.suggestion-chip {
    background: #f0f2f5;
    border: 1px solid #ddd;
    border-radius: 16px;
    padding: 4px 12px;
    cursor: pointer;
    font-size: 13px;
    white-space: normal;
    word-break: break-word;
}
.suggestion-chip:hover { background: #e2e8f0; }

/* Mobile back button — ẩn trên desktop */
.mobile-back-btn {
    display: none;
    background: none;
    border: none;
    font-size: 1.4rem;
    cursor: pointer;
    color: var(--text-main);
    padding: 0 10px 0 0;
    line-height: 1;
}

@media (max-width: 768px) {
    /* Ngăn overflow ngang, dùng dvh để trừ keyboard iOS */
    html, body { overflow-x: hidden; height: 100dvh; }
    .app-container { overflow: hidden; width: 100vw; height: 100dvh; }
    .chat-area { height: 100dvh; }

    /* Ghim input area ở dưới khi keyboard hiện */
    .chat-input-area { position: sticky; bottom: 0; z-index: 10; background: white; }
    .reply-banner { position: sticky; bottom: 0; z-index: 9; }
    #suggestions-bar { position: sticky; bottom: 0; z-index: 8; background: white; }

    /* Layout: sidebar full-width, chat-area ẩn mặc định */
    .sidebar { width: 100vw !important; max-width: 100vw !important; min-width: 0 !important; }
    .sidebar-resizer { display: none; }
    .chat-area { display: none !important; width: 100vw !important; max-width: 100vw !important; }

    /* Khi đang xem chat: ẩn sidebar, hiện chat-area */
    body.chat-active .sidebar { display: none !important; }
    body.chat-active .chat-area { display: flex !important; }

    /* Hiện nút back */
    .mobile-back-btn { display: block; flex-shrink: 0; }

    /* Header: 2 dòng — dòng 1: back+avatar+tên, dòng 2: actions */
    .chat-header { padding: 10px 12px; flex-wrap: wrap; gap: 4px; align-items: center; }
    .chat-header-info { flex: 1; min-width: 0; }
    .chat-header-info h3 { font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .chat-header-info p { font-size: 0.72rem; }
    .header-actions { margin-left: auto; display: flex; flex-wrap: wrap; gap: 5px; justify-content: flex-end; }
    .category-select { font-size: 0.75rem; padding: 3px 5px; max-width: 120px; }
    .btn-sm { font-size: 0.75rem; padding: 4px 10px; }

    /* Messages */
    .messages-container { padding: 10px 8px; }
    .message-row { max-width: 88%; }

    /* Input area */
    .chat-input-area { padding: 8px 8px; gap: 2px; }
    .chat-input { font-size: 0.95rem; padding: 8px 12px; }

    /* Sidebar */
    .sidebar-header { padding: 14px 16px; }
    .contact-item { padding: 12px 14px; }

    /* Modal full-width */
    .modal-box { width: 94vw !important; max-width: 94vw !important; padding: 16px; box-sizing: border-box; }

    /* Suggestion chips */
    .suggestion-chip { font-size: 12px; padding: 4px 10px; }
}

