/* ==========================================================================
   1. KONFIGURASI & VARIABEL UTAMA
   ========================================================================== */
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

:root {
    /* --- Warna Brand (Gradient Ungu-Biru) --- */
    --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --primary-solid: #764ba2;
    
    /* --- Warna Status --- */
    --success: #4CAF50;  
    --warning: #fb8c00;  
    --danger: #F44336;   
    --info: #03A9F4;     
    --read-check: #00e676; 

    /* --- Warna Netral --- */
    --bg-app: #ffffff;
    --bg-chat: #f0f2f5;
    --border: #e0e0e0;
    
    /* --- Teks --- */
    --text-dark: #1e293b;
    --text-body: #475569;
    --text-light: #94a3b8;
    --text-inverse: #ffffff;

    /* --- Dimensi --- */
    --widget-w: 380px;
    --widget-h: 600px;
    --radius: 16px;
    --z-index: 2147483647; /* Max Z-Index */
}

/* --- WRAPPER UTAMA & RESET --- */
#pmb-chatbot-widget {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    line-height: 1.5;
    color: var(--text-body);
    text-align: left;
    
    /* Posisi Fixed di Pojok Kanan Bawah */
    position: fixed;
    bottom: 0;
    right: 0;
    z-index: var(--z-index);
    
    /* Pointer Events None agar wrapper tidak menghalangi klik website di area kosong */
    pointer-events: none; 
    width: auto;
    height: auto;
}

/* Reset Box Sizing saja (JANGAN RESET PADDING/MARGIN DI SINI) */
#pmb-chatbot-widget * {
    box-sizing: border-box !important;
    outline: none !important;
}

/* Pastikan input dan button mewarisi font */
#pmb-chatbot-widget input,
#pmb-chatbot-widget button,
#pmb-chatbot-widget textarea,
#pmb-chatbot-widget select {
    font-family: 'Inter', sans-serif !important;
    font-size: 14px;
}

/* Lindungi Font Awesome */
#pmb-chatbot-widget .fas,
#pmb-chatbot-widget .far,
#pmb-chatbot-widget .fab,
#pmb-chatbot-widget .fa {
    font-family: "Font Awesome 6 Free", "Font Awesome 5 Free", "FontAwesome", sans-serif !important;
    font-weight: 900;
    font-style: normal;
    line-height: 1;
}

/* ==========================================================================
   2. TOMBOL PELUNCUR (FLOATING BUTTON)
   ========================================================================== */
#pmb-chatbot-button {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 60px;
    height: 60px;
    background: var(--primary-gradient);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(118, 75, 162, 0.25), 0 8px 16px rgba(118, 75, 162, 0.2);
    z-index: var(--z-index);
    transition: transform 0.3s ease;
    border: 2px solid rgba(255, 255, 255, 0.2);
    pointer-events: auto; /* Aktifkan klik kembali */
}

#pmb-chatbot-button:hover {
    transform: scale(1.1) translateY(-2px);
}

#pmb-chatbot-button svg {
    width: 28px;
    height: 28px;
    fill: var(--text-inverse);
}

.notification-dot {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 20px;
    height: 20px;
    background-color: var(--danger);
    color: white;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
    animation: pulse-red 2s infinite;
}

@keyframes pulse-red { 
    0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.4); } 
    70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(244, 67, 54, 0); } 
    100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(244, 67, 54, 0); } 
}

/* ==========================================================================
   3. KONTAINER UTAMA (WINDOW CHAT)
   ========================================================================== */
#pmb-chatbot-container {
    position: fixed;
    bottom: 100px;
    right: 30px;
    width: var(--widget-w);
    height: var(--widget-h);
    max-height: 80vh;
    background: var(--bg-app);
    border-radius: var(--radius);
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none; 
    z-index: var(--z-index);
    border: 1px solid rgba(0,0,0,0.05);
}

#pmb-chatbot-container.show {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all; /* Aktifkan interaksi */
    visibility: visible;
}

#pmb-chatbot-container.hidden {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    visibility: hidden;
}

/* ==========================================================================
   4. HEADER
   ========================================================================== */
.chatbot-header {
    background: var(--primary-gradient);
    padding: 15px 20px !important; /* Paksa padding */
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-inverse);
    flex-shrink: 0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.chatbot-header-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chatbot-avatar {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: white;
    border: 1px solid rgba(255,255,255,0.3);
}

.chatbot-info h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.2;
    color: #fff;
}

.chatbot-status {
    font-size: 12px;
    opacity: 0.9;
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 2px;
    color: rgba(255,255,255,0.9);
}

.chatbot-status::before {
    content: '';
    display: block;
    width: 7px;
    height: 7px;
    background-color: #00e676;
    border-radius: 50%;
    box-shadow: 0 0 5px rgba(105, 240, 174, 0.5);
}

#chatbot-close {
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.8);
    font-size: 20px;
    cursor: pointer;
    transition: 0.2s;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

#chatbot-close:hover {
    color: white;
    background: rgba(255,255,255,0.2);
    border-radius: 50%;
}

/* ==========================================================================
   5. AREA PESAN
   ========================================================================== */
#chatbot-messages {
    flex: 1;
    padding: 20px !important; /* Paksa padding */
    overflow-y: auto;
    background-color: var(--bg-chat);
    background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
    background-size: 24px 24px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

#chatbot-messages::-webkit-scrollbar { width: 5px; }
#chatbot-messages::-webkit-scrollbar-track { background: transparent; }
#chatbot-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }

/* Wrapper Pesan */
.message-wrapper {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* --- POSISI --- */
.message-wrapper.user {
    align-self: flex-end;
    align-items: flex-end;
}

.message-wrapper.ai, 
.message-wrapper.agent {
    align-self: flex-start;
    align-items: flex-start;
}

/* Style Bubble */
.message-bubble {
    padding: 12px 16px !important; /* Paksa padding */
    border-radius: 18px;
    font-size: 13.5px;
    line-height: 1.5;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

/* Bubble User */
.message-wrapper.user .message-bubble {
    background: var(--primary-gradient);
    color: white;
    border-bottom-right-radius: 4px;
}
.message-wrapper.user .message-bubble a { color: #e0e7ff; text-decoration: underline; }

/* Bubble Bot/Agent */
.message-wrapper.ai .message-bubble,
.message-wrapper.agent .message-bubble {
    background: white;
    color: var(--text-dark);
    border-bottom-left-radius: 4px;
    border: 1px solid #e2e8f0;
}
.message-wrapper.ai .message-bubble a { color: var(--primary-solid); font-weight: 600; }

/* Info Pesan */
.message-info {
    font-size: 10px;
    color: var(--text-light);
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 2px;
}

/* Centang Read */
.msg-status { font-size: 10px; margin-left: 4px; color: #cbd5e1; }
.msg-status.read { color: var(--read-check); }

/* Typing Indicator */
.typing-wrapper { align-self: flex-start; margin-bottom: 10px; }
.typing-bubble {
    background: white;
    border: 1px solid #e2e8f0;
    padding: 12px 16px !important;
    border-radius: 18px;
    border-bottom-left-radius: 4px;
    display: inline-flex; align-items: center; gap: 4px;
    width: fit-content;
}
.typing-dot { width: 6px; height: 6px; background: #94a3b8; border-radius: 50%; animation: typing 1.4s infinite ease-in-out; }
.typing-dot:nth-child(1) { animation-delay: -0.32s; }
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes typing { 0%, 80%, 100% { transform: scale(0); opacity: 0.5; } 40% { transform: scale(1.1); opacity: 1; } }
.typing-text { font-size: 10px; color: var(--text-light); margin-top: 4px; margin-left: 4px; }

/* ==========================================================================
   6. INPUT AREA
   ========================================================================== */
#chatbot-input-area {
    padding: 12px !important;
    background-color: white;
    border-top: 1px solid #f1f5f9;
    flex-shrink: 0;
    width: 100%;
}

.input-main-area {
    display: flex;
    align-items: center;
    gap: 8px;
    background-color: #fff;
    padding: 0;
    width: 100%;
}

#chatbot-input {
    flex: 1;
    border: 1px solid #e2e8f0;
    border-radius: 20px;
    padding: 10px 15px !important;
    font-size: 14px;
    resize: none;
    max-height: 100px;
    min-height: 40px;
    background: #f9fafb;
    color: #333;
    width: 100%;
}
#chatbot-input:focus { border-color: var(--primary-solid); background: #fff; }

#chatbot-send {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--primary-gradient); color: white;
    border: none; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: transform 0.2s;
    flex-shrink: 0;
}
#chatbot-send:hover { transform: scale(1.1); }
#chatbot-send:disabled { background: #cbd5e1; cursor: default; transform: none; }

#chatbot-attach {
    width: 36px; height: 36px;
    display: flex; align-items: center; justify-content: center;
    color: #64748b; cursor: pointer;
    border-radius: 50%; border: none; background: none;
    transition: background 0.2s; flex-shrink: 0;
}
#chatbot-attach:hover { background-color: #f1f5f9; color: var(--primary-solid); }

/* ==========================================================================
   7. QUICK ACTIONS (CHIPS)
   ========================================================================== */
.quick-actions-mini {
    display: flex; gap: 8px; overflow-x: auto; padding: 8px 0 0 0;
    scrollbar-width: none; width: 100%;
}
.quick-actions-mini::-webkit-scrollbar { display: none; }

.quick-action-mini-btn {
    background: white;
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-main);
    white-space: nowrap;
    cursor: pointer;
    display: flex; align-items: center; gap: 6px;
    transition: all 0.2s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}
.quick-action-mini-btn:hover {
    background: var(--bg-chat);
    border-color: var(--primary-solid);
    color: var(--primary-solid);
    transform: translateY(-2px);
}

/* ==========================================================================
   8. SYSTEM & QUEUE & PREVIEW
   ========================================================================== */
.system-message { text-align: center; margin: 12px auto; width: 100%; display: flex; justify-content: center; }
.system-message-content {
    display: inline-flex; align-items: center; gap: 8px; justify-content: center;
    background: rgba(226, 232, 240, 0.7);
    padding: 6px 16px !important; border-radius: 12px;
    font-size: 11px; color: var(--text-medium); font-weight: 600;
    border: 1px solid var(--border);
}

.system-actions { display: flex; justify-content: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; width: 100%; }
.system-action-btn {
    font-size: 12px; font-weight: 600; padding: 8px 16px; border-radius: 20px;
    border: 1px solid var(--border); background: white; color: var(--text-body);
    cursor: pointer; transition: all 0.2s ease; display: flex; align-items: center; gap: 6px;
}
.system-action-btn:hover { background: #f8fafc; transform: translateY(-2px); }
.system-action-btn.primary { background: var(--primary-gradient); color: white; border: none; box-shadow: 0 4px 10px rgba(118, 75, 162, 0.3); }

.queue-info {
    background: #fffbeb; border: 1px solid #fcd34d; border-radius: 16px;
    padding: 16px; margin: 12px 0; text-align: center; width: 100%;
    box-shadow: 0 4px 12px rgba(251, 191, 36, 0.1);
}
.queue-header { display: flex; align-items: center; justify-content: center; gap: 8px; margin-bottom: 10px; color: #b45309; font-weight: 700; font-size: 14px; }
.queue-details { display: flex; flex-direction: column; gap: 8px; }
.queue-detail { display: flex; justify-content: space-between; padding-bottom: 6px; border-bottom: 1px dashed rgba(180, 83, 9, 0.2); font-size: 13px; }
.detail-label { color: #b45309; }
.detail-value { font-weight: 700; color: #92400e; }

.live-chat-status {
    padding: 8px 16px; background: rgba(255,255,255,0.98);
    border-bottom: 1px solid var(--border); font-size: 11px;
    display: flex; justify-content: space-between; align-items: center; z-index: 10; width: 100%;
}
.live-chat-status.connecting { background: #fff7ed; color: #c2410c; }
.live-chat-status.connected { background: #f0fdf4; color: #15803d; }
.status-indicator { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; margin-right: 6px; animation: pulse 2s infinite; }
.live-chat-status.connected .status-indicator { animation: none; }
.status-action-btn { background: white; border: 1px solid currentColor; border-radius: 12px; padding: 2px 8px; font-size: 10px; cursor: pointer; color: inherit; }

#file-preview-container {
    margin-bottom: 8px; border-radius: 8px; border: 1px solid #e2e8f0;
    width: 100%; display: flex; justify-content: space-between; align-items: center;
    padding: 8px; background: #f8f9fa;
}

/* ==========================================================================
   9. PRE-CHAT FORM
   ========================================================================== */
.pre-chat-form {
    background: white; padding: 20px; border-radius: 16px;
    border: 1px solid var(--border); margin: 10px auto; width: 95%;
    box-shadow: 0 4px 15px rgba(0,0,0,0.05);
}
.pre-chat-form h4 { font-size: 14px; text-align: center; margin-bottom: 15px; font-weight: 700; margin-top: 0; }
.form-group { margin-bottom: 12px; width: 100%; }
.form-label { display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px; color: var(--text-body); }
.pre-chat-form input, .pre-chat-form textarea {
    width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 8px;
    font-size: 13px; background: #f8fafc;
    font-family: inherit;
}
.pre-chat-form input:focus { outline: none; border-color: var(--primary-solid); background: white; }
.form-actions { display: flex; gap: 10px; margin-top: 15px; }
.form-actions button { flex: 1; padding: 10px; border-radius: 8px; border: none; font-size: 12px; font-weight: 600; cursor: pointer; }
.form-actions button.primary { background: var(--primary-gradient); color: white; }
.form-actions button.secondary { background: #f1f5f9; color: var(--text-body); }

/* ==========================================================================
   MOBILE RESPONSIVE (FULL SCREEN MODE)
   ========================================================================== */

@media (max-width: 480px) {

    /* 1. Container menjadi Full Screen & Fixed */
    #pmb-chatbot-container {
        width: 100vw !important;
        height: 100dvh !important; /* Gunakan dynamic viewport height */
        max-height: none !important;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 0 !important;
        margin: 0;
        display: flex;
        flex-direction: column;
        position: fixed;
        z-index: 2147483647; /* Z-index Maksimal agar menutupi navbar web utama */
        background-color: #fff; /* Wajib ada background */
    }

    /* 2. Logika Tampil/Sembunyi */
    #pmb-chatbot-container.hidden {
        transform: translateY(100%);
        opacity: 1; 
        visibility: visible; /* Tetap visible agar transisi jalan */
        pointer-events: none; /* Agar klik tembus ke web saat tutup */
    }
    
    #pmb-chatbot-container.show {
        transform: translateY(0);
        opacity: 1;
        pointer-events: auto;
    }

    /* 3. Header: Sesuaikan dengan Poni HP */
    .chatbot-header {
        padding-top: max(15px, env(safe-area-inset-top)) !important;
        padding-bottom: 15px !important;
        height: auto;
        flex-shrink: 0; /* Jangan gepeng */
    }

    /* 4. Area Pesan: Scrollable tapi Terisolasi */
    #chatbot-messages {
        flex: 1;
        height: auto;
        overflow-y: auto;
        /* FIX SCROLL BOCOR: Mencegah scroll tembus ke body */
        overscroll-behavior: contain; 
        -webkit-overflow-scrolling: touch;
    }

    /* 5. Input Area: Aman dari Gesture Bar */
    #chatbot-input-area {
        padding-bottom: max(15px, env(safe-area-inset-bottom)) !important;
        flex-shrink: 0;
    }

    /* 6. TOMBOL FLOATING (MODIFIKASI PENTING) */
    #pmb-chatbot-button {
        bottom: 20px;
        right: 20px;
        width: 56px;
        height: 56px;
        z-index: 2147483646; /* Satu level di bawah container */
    }

    /* --- LOGIKA PENGUNCIAN (SOLUSI MASALAH ANDA) --- */

    /* A. Sembunyikan Tombol saat Container punya class .show */
    /* Menggunakan CSS modern :has() agar tidak perlu ubah JS */
    body:has(#pmb-chatbot-container.show) #pmb-chatbot-button {
        display: none !important;
    }

    /* Fallback untuk browser lama: Jika body punya class .chat-open */
    body.chat-open #pmb-chatbot-button {
        display: none !important;
    }

    /* B. Kunci Scroll Web Utama saat Chat Terbuka */
    /* Ini akan membuat web belakang diam total */
    body:has(#pmb-chatbot-container.show),
    body.chat-open {
        overflow: hidden !important;
        position: fixed !important; /* Kunci mati posisi body */
        width: 100% !important;
        height: 100% !important;
    }
}