/* ==========================================
   1. VỊ TRÍ NỔI & NÚT BẬT/TẮT (FLOATING WIDGET)
   ========================================== */
#ai-chat-widget {
    display: none; /* Ẩn hoàn toàn tới khi JS khôi phục xong trạng thái (chống giật khi F5) */
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 99999;
    font-family: sans-serif;
}

#ai-chat-widget.is-ready {
    display: block;
}

#ai-chat-toggle-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 12px 20px;
    border: none;
    border-radius: 30px;
    background: #0084ff;
    color: #fff;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 132, 255, 0.35);
    transition: transform 0.2s ease, background 0.2s ease;
}

#ai-chat-toggle-btn:hover {
    background: #0073e6;
    transform: translateY(-2px);
}

#ai-chat-toggle-btn .ai-icon-close {
    display: none;
}

/* Đổi chữ trên nút khi khung chat đang mở */
#ai-chat-widget.is-open #ai-chat-toggle-btn .ai-icon-open {
    display: none;
}

#ai-chat-widget.is-open #ai-chat-toggle-btn .ai-icon-close {
    display: inline;
}

/* ==========================================
   2. KHUNG CHAT CHÍNH
   ========================================== */
#ai-chat-container {
    position: absolute;
    bottom: 60px;
    right: 0;
    width: 420px;
    max-width: calc(100vw - 40px);
    height: 550px;
    max-height: calc(100vh - 100px);
    display: flex;
    flex-direction: column;
    border: 1px solid #ccc;
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
    box-sizing: border-box;

    /* Hiệu ứng đóng/mở mượt */
    opacity: 0;
    visibility: hidden;
    transform: translateY(15px) scale(0.98);
    transform-origin: bottom right;
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}

#ai-chat-widget.is-open #ai-chat-container {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* Thanh tiêu đề trên cùng */
#ai-chat-container .ai-chat-titlebar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: #0084ff;
    color: #fff;
    font-size: 14px;
}

#ai-chat-container #ai-chat-close-btn {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    padding: 0 4px;
}

/* Thông tin khách: xếp dọc (Tên / SĐT) */
#ai-chat-container .ai-chat-header {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 10px 14px;
    background: #f1f1f1;
    border-bottom: 1px solid #ccc;
}

#ai-chat-container .ai-chat-header input {
    width: 100%;
    padding: 7px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
    background: #fff;
    box-sizing: border-box;
}

/* Khung tin nhắn */
#ai-chat-messages {
    transition: opacity 0.15s ease;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 15px;
    overflow-y: auto;
    background: #f9f9f9;
}

/* Ẩn tới khi lịch sử tải xong để không bị nháy lời chào -> lịch sử */
#ai-chat-messages.is-loading {
    opacity: 0;
}

#ai-chat-messages .ai-msg {
    max-width: 80%;
    padding: 8px 12px;
    border-radius: 12px;
    line-height: 1.5;
    font-size: 14px;
    word-wrap: break-word;
}

#ai-chat-messages .ai-msg-bot {
    align-self: flex-start;
    background: #e4e6eb;
    color: #050505;
}

#ai-chat-messages .ai-msg-user {
    align-self: flex-end;
    background: #0084ff;
    color: #fff;
}

#ai-chat-messages .ai-msg-meta {
    margin-top: 4px;
    font-size: 11px;
    color: #777;
}

#ai-chat-messages .ai-msg-error {
    color: #d63638;
    font-weight: bold;
}

/* Thanh nhập tin nhắn */
#ai-chat-container .ai-chat-form {
    display: flex;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid #ccc;
    background: #fff;
    margin: 0;
}

#ai-chat-container .ai-chat-form input {
    flex: 1;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
}

#ai-chat-container .ai-chat-form button {
    padding: 8px 16px;
    border: none;
    border-radius: 4px;
    background: #0084ff;
    color: #fff;
    font-weight: bold;
    cursor: pointer;
}

#ai-chat-container .ai-chat-form button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}