* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: #f3f4f6;
    color: #1f2937;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Thanh Header */
header {
    background-color: #ffffff;
    border-bottom: 1px solid #e5e7eb;
    padding: 12px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
}

header h1 {
    font-size: 18px;
    font-weight: 600;
    color: #111827;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
}

.user-info .name {
    font-weight: 500;
}

.logout-btn {
    color: #ef4444;
    text-decoration: none;
    font-weight: 500;
}

.logout-btn:hover {
    text-decoration: underline;
}

/* Bố cục chính */
#main-container {
    display: flex;
    flex: 1;
    height: calc(100vh - 60px);
    overflow: hidden;
}

/* Cột trái: Danh sách cuộc chat */
#sidebar {
    width: 350px;
    background-color: #ffffff;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.sidebar-title {
    padding: 16px 20px;
    font-size: 16px;
    font-weight: 600;
    border-bottom: 1px solid #f3f4f6;
    background-color: #fafafa;
}

.chat-list {
    list-style: none;
    display: flex;
    flex-direction: column;
}

.chat-item {
    padding: 16px 20px;
    border-bottom: 1px solid #f3f4f6;
    cursor: pointer;
    transition: background-color 0.2s;
}

.chat-item:hover {
    background-color: #f9fafb;
}

.chat-item.active {
    background-color: #eff6ff;
    border-left: 4px solid #3b82f6;
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.customer-name {
    font-weight: 600;
    font-size: 14px;
    color: #00A859; /* Màu xanh thương hiệu nổi bật */
}

.chat-time {
    font-size: 11px;
    color: #9ca3af;
}

.chat-meta {
    font-size: 12px;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status-badge {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 500;
    margin-top: 6px;
}

.status-badge.status-ai {
    background-color: #e0f2fe;
    color: #0369a1;
}

.status-badge.status-staff {
    background-color: #fef3c7;
    color: #b45309;
}

/* Cột phải: Nội dung chi tiết cuộc chat */
#chat-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #f9fafb;
}

#no-chat-selected {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #9ca3af;
}

#chat-header {
    background-color: #ffffff;
    border-bottom: 1px solid #e5e7eb;
    padding: 16px 24px;
    display: none;
    justify-content: space-between;
    /* Đẩy các phần tử sang 2 bên */
    align-items: center;
    /* Căn giữa dọc */
}

#chat-header h2 {
    font-size: 16px;
    font-weight: 600;
}

#chat-header p {
    font-size: 12px;
    color: #6b7280;
    margin-top: 2px;
}

/* Vùng hiển thị tin nhắn */
#messages-container {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.msg-row {
    display: flex;
    flex-direction: column;
    max-width: 75%;
    width: fit-content;
}

.msg-row.visitor {
    align-self: flex-start;
}

.msg-row.ai {
    align-self: flex-end;
}

.msg-row.staff {
    align-self: flex-end;
}

.sender-title {
    font-size: 11px;
    color: #9ca3af;
    margin-bottom: 4px;
    font-weight: 500;
}

.msg-row.staff .sender-title,
.msg-row.ai .sender-title {
    align-self: flex-end;
}

.msg-bubble {
    padding: 10px 16px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    position: relative;
}

.visitor .msg-bubble {
    background-color: #00A859; /* Màu nền xanh lá thương hiệu chỉ định */
    color: #ffffff;            /* Chữ trắng */
    border: none;
    border-top-left-radius: 2px;
}

.ai .msg-bubble {
    background-color: #3E4095; /* Màu nền tím/indigo thương hiệu chỉ định */
    color: #ffffff;            /* Chữ trắng */
    border: none;
    border-top-right-radius: 2px;
}

.staff .msg-bubble {
    background-color: #3b82f6; /* Xanh dương đậm rõ nét cho nhân viên */
    color: #ffffff;
    border-top-right-radius: 2px;
}

/* Các nút tác vụ Sửa / Thu hồi */
.msg-actions {
    display: flex;
    gap: 8px;
    font-size: 11px;
    margin-top: 4px;
    align-self: flex-end;
}

.msg-row.visitor .msg-actions {
    align-self: flex-start;
}

.msg-row.staff .msg-actions,
.msg-row.ai .msg-actions {
    align-self: flex-end;
}

.action-link {
    color: #9ca3af;
    text-decoration: none;
    cursor: pointer;
}

.action-link:hover {
    color: #3b82f6;
    text-decoration: underline;
}

.action-link.delete:hover {
    color: #ef4444;
}

.edited-flag {
    font-size: 10px;
    color: #9ca3af;
    font-style: italic;
    margin-top: 2px;
}

/* Khu vực nhập tin nhắn */
#input-container {
    background-color: #ffffff;
    border-top: 1px solid #e5e7eb;
    padding: 16px 24px;
    display: none; /* Sẽ đổi sang flex bằng JS khi có chat */
    flex-direction: column;
    gap: 10px;
}

#input-row-1 {
    display: flex;
    gap: 12px;
    align-items: center;
}

#input-row-2 {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

#message-input {
    flex: 1;
    height: 50px;
    padding: 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    resize: none;
    font-family: inherit;
    font-size: 14px;
    outline: none;
}

#message-input:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

#send-btn {
    background-color: #3b82f6;
    color: #ffffff;
    border: none;
    padding: 0 20px;
    height: 50px;
    border-radius: 8px;
    font-weight: 500;
    font-size: 14px;
    cursor: pointer;
    transition: background-color 0.2s;
    flex-shrink: 0;
}

#send-btn:hover {
    background-color: #2563eb;
}

#file-btn {
    background-color: #e5e7eb;
    color: #4b5563;
    border: none;
    padding: 0 20px;
    height: 38px;
    border-radius: 8px;
    font-weight: 500;
    font-size: 14px;
    cursor: pointer;
    flex-shrink: 0;
}

#file-btn:hover {
    background-color: #d1d5db;
}

#reply-channel-select {
    background-color: #f3f4f6;
    color: #1f2937;
    border: 1px solid #d1d5db;
    padding: 0 12px;
    height: 38px;
    border-radius: 8px;
    font-weight: 500;
    font-size: 13px;
    cursor: pointer;
    outline: none;
    max-width: 250px;
}

/* CSS bổ sung cho thanh Tab chọn */
.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid #e5e7eb;
    background-color: #fafafa;
}

.tab-btn {
    flex: 1;
    padding: 12px;
    border: none;
    background: none;
    font-size: 14px;
    font-weight: 600;
    color: #6b7280;
    cursor: pointer;
    transition: all 0.2s;
    outline: none;
}

.tab-btn:hover {
    background-color: #f9fafb;
    color: #111827;
}

.tab-btn.active {
    color: #3b82f6;
    border-bottom: 2px solid #3b82f6;
    background-color: #ffffff;
}

/* CSS cho hàng hiển thị Nhân viên ở sidebar */
.staff-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    border-bottom: 1px solid #f3f4f6;
    transition: background-color 0.2s;
}

.staff-item:hover {
    background-color: #f9fafb;
}

.staff-name {
    font-size: 13px;
    font-weight: 500;
    color: #1f2937;
}

.staff-chat-btn {
    padding: 6px 12px;
    background-color: #10b981;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
    text-decoration: none;
}

.staff-chat-btn:hover {
    background-color: #059669;
    color: white;
    text-decoration: none;
}

/* Custom Context Menu */
#custom-context-menu {
    position: absolute;
    z-index: 10000;
    width: 180px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(12px) saturate(180%);
    -webkit-backdrop-filter: blur(12px) saturate(180%);
    border: 1px solid rgba(229, 231, 235, 0.5);
    border-radius: 10px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
    padding: 6px 0;
    display: none;
    font-family: inherit;
    font-size: 13px;
    color: #374151;
    animation: menuFadeIn 0.15s ease-out;
}

@keyframes menuFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.context-menu-item {
    padding: 8px 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background-color 0.15s, color 0.15s;
}

.context-menu-item:hover {
    background-color: #3b82f6;
    color: #ffffff;
}

.context-menu-item.danger:hover {
    background-color: #ef4444;
    color: #ffffff;
}

.context-menu-divider {
    height: 1px;
    background-color: #e5e7eb;
    margin: 4px 0;
}

/* Nút Các chat */
.other-chats-btn {
    padding: 0 10px;
    background-color: #f3f4f6;
    color: #4b5563;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    height: 34px;
    line-height: 34px;
    transition: background-color 0.2s, opacity 0.2s;
    flex-shrink: 0;
}

.other-chats-btn:hover:not(:disabled) {
    background-color: #e5e7eb;
}

.other-chats-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: #fafafa;
    color: #9ca3af;
    border-color: #e5e7eb;
}

/* Dropdown Các chat khác (Glassmorphism) */
.other-chats-dropdown {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 1000;
    width: 260px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(229, 231, 235, 0.5);
    border-radius: 10px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
    display: none;
    padding: 8px 0;
    max-height: 280px;
    overflow-y: auto;
    font-family: inherit;
    animation: menuFadeIn 0.15s ease-out;
}

.other-chats-title {
    padding: 6px 12px 10px 12px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #6b7280;
    font-weight: 600;
    border-bottom: 1px solid rgba(229, 231, 235, 0.5);
    margin-bottom: 4px;
}

.other-chat-item {
    padding: 8px 12px;
    cursor: pointer;
    transition: background-color 0.15s;
    border-bottom: 1px dotted rgba(229, 231, 235, 0.5);
    text-align: left;
}

.other-chat-item:last-child {
    border-bottom: none;
}

.other-chat-item:hover {
    background-color: rgba(59, 130, 246, 0.08);
}

/* --- MOBILE RESPONSIVE DESIGN --- */

/* Nút quay lại mặc định ẩn ở Desktop */
.mobile-back-btn {
    display: none;
    background: #f3f4f6;
    border: none;
    color: #4b5563;
    font-size: 18px;
    font-weight: bold;
    padding: 6px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-right: 12px;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

.mobile-back-btn:hover {
    background-color: #e5e7eb;
}

@media (max-width: 768px) {
    /* Điều chỉnh header chính */
    header {
        padding: 10px 16px;
    }
    header h1 {
        font-size: 15px;
    }
    .user-info {
        font-size: 12px;
        gap: 8px;
    }

    /* Bố cục chính chuyển thành 1 cột */
    #main-container {
        position: relative;
        height: calc(100vh - 60px);
    }

    #sidebar {
        width: 100%;
        display: block;
    }

    #chat-content {
        width: 100%;
        display: none; /* Mặc định ẩn khung chat ở mobile */
    }

    /* Khi có class .show-chat (đã chọn cuộc chat) */
    #main-container.show-chat #sidebar {
        display: none; /* Ẩn danh sách cuộc chat */
    }

    #main-container.show-chat #chat-content {
        display: flex; /* Hiện khung chat */
    }

    /* Nút back xuất hiện trên mobile */
    .mobile-back-btn {
        display: inline-flex;
        padding: 4px 10px;
        font-size: 16px;
        margin-right: 8px;
    }

    /* Ẩn màn hình nền "Chưa chọn cuộc chat" ở mobile */
    #no-chat-selected {
        display: none !important;
    }

    /* Tối ưu khung chat để vừa khít màn hình nhỏ */
    #chat-header {
        padding: 10px 16px;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
    }

    /* Tách block hành động phụ (SĐT & Gán nhân viên) thành các hàng riêng biệt */
    #header-actions {
        width: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        margin-top: 4px !important;
    }

    /* Hàng SĐT + Đồng bộ */
    #phone-sync-container {
        width: 100% !important;
        display: flex !important;
        gap: 6px !important;
    }

    #sync-phone-input {
        flex: 1 !important;
        width: auto !important;
        height: 36px !important;
        font-size: 12px !important;
        padding: 4px 8px !important;
    }

    #phone-sync-container button {
        height: 36px !important;
        line-height: 36px !important;
        font-size: 12px !important;
        padding: 0 12px !important;
        flex-shrink: 0 !important;
    }

    /* Hàng Gán nhân viên 1 + Gán nhân viên 2 */
    #header-actions > div:not(#phone-sync-container) {
        width: 100% !important;
        display: flex !important;
        gap: 6px !important;
    }

    #assign-staff-select-1, #assign-staff-select-2 {
        flex: 1 !important;
        max-width: none !important;
        height: 36px !important;
        font-size: 12px !important;
        padding: 4px 8px !important;
    }

    #messages-container {
        padding: 16px;
    }

    .msg-row {
        max-width: 85%;
    }

    #input-container {
        padding: 10px 16px;
        gap: 8px;
    }

    /* Hàng 1 dưới: Nút File và Chọn kênh */
    #input-row-1 {
        display: flex !important;
        gap: 8px !important;
        width: 100% !important;
        align-items: center !important;
    }

    #file-btn {
        height: 36px !important;
        padding: 0 16px !important;
        font-size: 13px !important;
        line-height: 36px !important;
    }

    #reply-channel-select {
        flex: 1 !important;
        max-width: none !important;
        height: 36px !important;
        font-size: 12px !important;
        padding: 0 10px !important;
    }

    /* Hàng 2 dưới: Nội dung chat và Nút gửi */
    #input-row-2 {
        display: flex !important;
        gap: 8px !important;
        width: 100% !important;
        align-items: flex-end !important;
    }

    #message-input {
        flex: 1 !important;
        height: 40px !important;
        font-size: 13px !important;
        padding: 8px 10px !important;
    }

    #send-btn {
        height: 40px !important;
        padding: 0 16px !important;
        font-size: 13px !important;
        line-height: 40px !important;
    }
}