/* --- Global Namespace Protection & Reset --- */
#ai-chat-wrapper, 
#ai-chat-wrapper * { 
    box-sizing: border-box; 
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
#ai-chat-wrapper {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 999999;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

#ai-chat-wrapper #ai-chat-window {
    width: 420px;
    height: calc(100vh - 120px);
    background: #ffffff;
    border-radius: 24px;
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    margin-bottom: 16px;
    border: 1px solid rgba(0, 0, 0, 0.05);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (max-width: 480px) {
    #ai-chat-wrapper {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
    }
    #ai-chat-wrapper #ai-chat-window {
        width: 100%;
        max-width: 100%;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        margin-bottom: 0;
    }
    #ai-chat-wrapper .launcher-btn {
        display: none !important; 
    }
}

#ai-chat-wrapper .hidden { display: none !important; }

/* --- Header Section --- */
#ai-chat-wrapper .chat-header {
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #f3f4f6;
    background: #fff;
    height: auto;
    width: 100%;
    margin: 0;
    box-shadow: none;
}
#ai-chat-wrapper .header-info { display: flex; align-items: center; gap: 12px; }
#ai-chat-wrapper .header-logo { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; border: 1px solid #f3f4f6; margin: 0; }
#ai-chat-wrapper .header-name { font-weight: 600; font-size: 16px; color: #111; letter-spacing: -0.01em; margin: 0; }
#ai-chat-wrapper .header-action-btn { background: transparent; border: none; cursor: pointer; padding: 6px; display: flex; border-radius: 50%; transition: background 0.2s; }
#ai-chat-wrapper .header-action-btn:hover { background: #f3f4f6; }
#ai-chat-wrapper .header-actions { position: relative; display: flex; align-items: center; }

/* --- Chat Messages Area --- */
#ai-chat-wrapper #chat-messages {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    gap: 0px; 
}
#ai-chat-wrapper .msg.ai { display: flex; gap: 12px; max-width: 85%; margin-bottom: 8px; align-self: flex-start; }
#ai-chat-wrapper .msg-avatar-container { width: 32px; flex-shrink: 0; }
#ai-chat-wrapper .msg-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; }
#ai-chat-wrapper .msg-bot-name { font-size: 12px; font-weight: 600; color: #6b7280; margin-bottom: 4px; margin-left: 2px; }
#ai-chat-wrapper .bubble { padding: 12px 16px; font-size: 14.5px; line-height: 1.5; word-wrap: break-word; }
#ai-chat-wrapper .ai .bubble { background: #f3f4f6; color: #1f2937; border-radius: 4px 18px 18px 18px; }
#ai-chat-wrapper .user { align-self: flex-end; max-width: 85%; margin-bottom: 12px; margin-top: 12px; }
#ai-chat-wrapper .user .bubble { background: #000; color: #fff; border-radius: 18px 18px 4px 18px; box-shadow: 0 4px 12px rgba(0,0,0,0.08); }

/* --- Welcome Popups --- */
#ai-chat-wrapper #ai-chat-welcome-popup { margin-bottom: 16px; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; z-index: 1000; }
#ai-chat-wrapper .popup-bubbles-stack { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
#ai-chat-wrapper .popup-bubble-item { background: #ffffff; padding: 12px 20px; border-radius: 18px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1); font-size: 15px; color: #1f2937; border: 1px solid #f3f4f6; position: relative; width: auto; display: block; word-wrap: break-word; cursor: pointer; transition: transform 0.2s ease; animation: aiPopupFadeIn 0.5s ease-out forwards; }
#ai-chat-wrapper .popup-bubble-item:not(:last-child) { border-bottom-right-radius: 4px; }
#ai-chat-wrapper #close-welcome-popup { position: absolute; top: -10px; right: -10px; background: #111; color: #fff; border: none; border-radius: 50%; width: 22px; height: 22px; cursor: pointer; opacity: 0; transition: 0.2s; font-size: 14px; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,0.2); z-index: 10; }
#ai-chat-wrapper .popup-bubble-item:first-child:hover #close-welcome-popup { opacity: 1; }

/* --- Input Area --- */
#ai-chat-wrapper .chat-input-area { padding: 16px 20px 20px; border-top: 1px solid #f3f4f6; }
#ai-chat-wrapper .powered-by-brand { text-align: center; font-size: 11px; color: #9ca3af; margin-bottom: 12px; font-weight: 500; }
#ai-chat-wrapper .input-wrapper { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 16px; padding: 8px 12px; display: flex; align-items: flex-end; min-height: 44px; box-sizing: border-box; }
#ai-chat-wrapper .input-wrapper:focus-within { border-color: #000; background: #fff; }
#ai-chat-wrapper #chat-input { flex: 1; border: none !important; background: transparent !important; outline: none !important; resize: none !important; font-size: 14px; line-height: 1.5; color: #111; max-height: 150px; min-height: 24px; padding: 4px 8px; margin: 0; box-shadow: none !important; overflow-y: hidden; box-sizing: border-box; transition: none !important; }
#ai-chat-wrapper #chat-input.is-scrolling { overflow-y: auto !important; }
#ai-chat-wrapper .action-icon { background: transparent; border: none; cursor: pointer; padding: 6px; display: flex; border-radius: 8px; transition: transform 0.2s; }
#ai-chat-wrapper .action-icon:hover { transform: scale(1.1); }

/* --- Launcher Button Logic --- */
#ai-chat-wrapper .launcher-btn { width: 60px; height: 60px; border-radius: 30px; border: none; cursor: pointer; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2); display: flex; align-items: center; justify-content: center; transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); padding: 0; overflow: hidden; position: relative; margin: 0; }
#ai-chat-wrapper .launcher-btn:hover { transform: scale(1.05); }
#ai-chat-wrapper i.fa, 
#ai-chat-wrapper i.fas, 
#ai-chat-wrapper i.far {
    font-family: "FontAwesome" !important;
    font-style: normal !important;
    font-weight: normal !important;
    display: inline-block !important;
    text-decoration: inherit !important;
    width: auto !important;
    height: auto !important;
    line-height: 1 !important;
}
#ai-chat-wrapper .launcher-img-container, #ai-chat-wrapper .launcher-close-icon { position: absolute; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; transition: transform 0.3s, opacity 0.3s; }
#ai-chat-wrapper .launcher-img { width: 100%; height: 100%; object-fit: cover; margin: 0; }
#ai-chat-window.hidden ~ #ai-chat-bubble .launcher-img-container { opacity: 1 !important; transform: scale(1) rotate(0deg) !important; display: flex !important; }
#ai-chat-window.hidden ~ #ai-chat-bubble .launcher-close-icon { opacity: 0 !important; transform: scale(0) rotate(90deg) !important; }
#ai-chat-window:not(.hidden) ~ #ai-chat-bubble .launcher-img-container { opacity: 0 !important; transform: scale(0) rotate(-90deg) !important; }
#ai-chat-window:not(.hidden) ~ #ai-chat-bubble .launcher-close-icon { display: flex !important; opacity: 1 !important; transform: scale(1) rotate(0deg) !important; }

/* --- Animations & Utilities --- */
@keyframes aiPopupFadeIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
#ai-chat-wrapper .ai .bubble.typing-dots { display: flex; align-items: center; justify-content: center; gap: 5px; width: 50px; height: 32px; padding: 0; }
#ai-chat-wrapper .typing-dots span { width: 6px; height: 6px; background-color: #9ca3af; border-radius: 50%; display: inline-block; animation: ai-typing-bounce 1.4s infinite ease-in-out both; }
#ai-chat-wrapper .typing-dots span:nth-child(1) { animation-delay: -0.32s; }
#ai-chat-wrapper .typing-dots span:nth-child(2) { animation-delay: -0.16s; }
@keyframes ai-typing-bounce { 0%, 80%, 100% { transform: scale(0); opacity: 0.3; } 40% { transform: scale(1.0); opacity: 1; } }

/* --- Message Content Formatting --- */
#ai-chat-wrapper .ai .bubble ul { margin: 8px 0; padding-left: 20px; list-style-type: disc; }
#ai-chat-wrapper .ai .bubble li { margin-bottom: 4px;  color: #1f2937  !important;}
#ai-chat-wrapper .ai .bubble strong { font-weight: 600; color: inherit; }
#ai-chat-wrapper .msg-content-wrapper { display: flex; flex-direction: column; align-items: flex-start; width: 100%; }

/* --- Message Actions & Time --- */
#ai-chat-wrapper .msg-footer { display: flex; align-items: center; gap: 8px; margin-top: 4px; height: 18px; }
#ai-chat-wrapper .msg-time { font-size: 10px !important; color: #9ca3af; font-weight: 400; user-select: none; }
#ai-chat-wrapper .msg-actions { display: flex; gap: 10px; opacity: 0; transition: opacity 0.2s ease; }
#ai-chat-wrapper .msg.ai:hover .msg-actions { opacity: 1; }
#ai-chat-wrapper .ai-action-btn { background: none; border: none; padding: 0; color: #9ca3af; cursor: pointer; font-size: 12px; line-height: 1; }
#ai-chat-wrapper .ai-action-btn:hover { color: #3b82f6; }
#ai-chat-wrapper .ai-action-btn.active-rating.like { color: #10b981; }
#ai-chat-wrapper .ai-action-btn.active-rating.dislike { color: #ef4444; }
.ai-action-btn i.fa { font-family: "FontAwesome" !important; display: inline-block !important; visibility: visible !important; opacity: 1 !important; }

/* --- Options Menu & Overlays --- */
#ai-chat-wrapper #chat-options-popup { position: absolute; top: 40px; right: 0; background: #fff; width: 206px; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); border: 1px solid #f3f4f6; z-index: 1000; padding: 8px; animation: menuFadeIn 0.2s ease-out; }
#ai-chat-wrapper .menu-item { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px; background: transparent; border: none; border-radius: 8px; font-size: 14px !important; color: #374151; cursor: pointer; text-align: left; transition: background 0.2s; }
#ai-chat-wrapper .menu-item i { font-size: 16px; color: #6b7280; width: 20px; text-align: center; }
@keyframes menuFadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

#ai-chat-wrapper #chat-confirm-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255, 255, 255, 0.85); backdrop-filter: blur(4px); z-index: 2000; display: flex; align-items: center; justify-content: center; padding: 20px; border-radius: 24px; }
#ai-chat-wrapper .confirm-box { background: #fff; padding: 24px; border-radius: 16px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); text-align: center; border: 1px solid #f3f4f6; }
#ai-chat-wrapper .confirm-box h4 { margin: 0 0 10px; font-size: 16px; color: #111; }
#ai-chat-wrapper .confirm-box p { margin: 0 0 20px; font-size: 13px; color: #6b7280; line-height: 1.5; }
#ai-chat-wrapper .confirm-actions { display: flex; gap: 10px; justify-content: center; }
#ai-chat-wrapper .confirm-actions button { padding: 8px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; border: none; transition: all 0.2s; }
#ai-chat-wrapper .btn-secondary { background: #f3f4f6; color: #374151; }
#ai-chat-wrapper .btn-danger { background: #ef4444 !important; color: #fff !important; }
#ai-chat-wrapper .btn-primary { background: #000; color: #fff; }

/* --- Recent Chats View --- */
#ai-chat-wrapper #recent-chats-view { 
    position: absolute; 
    top: 60px; 
    left: 0; 
    width: 100%; 
    height: calc(100% - 140px); 
    background: #fff; 
    z-index: 500; 
    display: flex; 
    flex-direction: column; 
    border-bottom-left-radius: 16px;
    border-bottom-right-radius: 16px;
}
#ai-chat-wrapper .recent-header { padding: 10px 20px; border-bottom: 1px solid #f3f4f6; display: flex; align-items: center; gap: 15px; font-weight: 600; font-size: 14px; }
#ai-chat-wrapper #recent-chats-list { flex: 1; overflow-y: auto; padding: 10px 0; display: flex; flex-direction: column; }
#ai-chat-wrapper .recent-loading { margin: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #9ca3af; gap: 12px; }
.recent-chat-item { padding: 15px 20px; border-bottom: 1px solid #f9fafb; cursor: pointer; transition: all 0.2s ease; background: #fff; width: 100%; box-sizing: border-box; }
.recent-chat-item:hover { background: #f3f4f6; }
.recent-chat-item .chat-date { font-size: 11px; color: #9ca3af; display: block; margin-bottom: 4px; }
.recent-chat-item .chat-preview { font-size: 13px; color: #374151; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
#back-to-chat i { font-size: 16px; color: #6b7280; }
.menu-item.is-disabled { opacity: 0.7; cursor: not-allowed; pointer-events: none;filter: grayscale(1); }