﻿/* --- VARIABLES & THEME --- */
:root {
    /* Brand Colors */
    --cw-primary: #2563eb;
    --cw-primary-hover: #1d4ed8;
    --cw-bg-dark: #0f172a;
    --cw-bg-glass: rgba(15, 23, 42, 0.98);
    --cw-surface: rgba(255, 255, 255, 0.05);
    --cw-border: rgba(255, 255, 255, 0.1);
    
    /* Functional */
    --cw-text-main: #f1f5f9;
    --cw-text-muted: #94a3b8;
    --cw-green: #10b981;
    --cw-red: #ef4444;
    --cw-rec-wave: rgba(241, 245, 249, 0.95);
    
    /* Bubbles */
    --cw-msg-user-bg: #2563eb;
    --cw-msg-user-text: #ffffff;
    --cw-msg-agent-bg: #4b5563;
    --cw-msg-agent-text: #e2e8f0;

    /* Dimensions & Physics */
    --cw-width: 380px;
    --cw-height: 700px;
    --cw-radius: 20px;
    --cw-font: 'Outfit', sans-serif;
    --cw-ease: cubic-bezier(0.25, 0.8, 0.25, 1);
}

/* --- RESET --- */
.ef-chat-widget * { box-sizing: border-box; margin: 0; padding: 0; font-family: var(--cw-font); -webkit-tap-highlight-color: transparent; }
.ef-chat-widget button { cursor: pointer; border: none; background: none; display: flex; align-items: center; justify-content: center; transition: 0.2s; color: inherit; pointer-events: auto; }
.ef-chat-widget input, .ef-chat-widget textarea { outline: none; background: transparent; border: none; color: inherit; font-family: inherit; }
.hidden { display: none !important; }

/* --- LAUNCHER (FIXED CENTER) --- */
#ef-chat-launcher {
    position: fixed; bottom: 24px; right: 24px;
    width: 64px; height: 64px;
    background: var(--cw-primary);
    border-radius: 50%;
    box-shadow: 0 8px 30px rgba(37, 99, 235, 0.5);
    cursor: pointer; z-index: 2147483647;
    display: flex; align-items: center; justify-content: center;
    transition: transform 0.3s var(--cw-ease), background 0.3s, opacity 0.3s, visibility 0.3s;
    border: 1px solid rgba(255,255,255,0.2);
}
#ef-chat-launcher:hover { transform: scale(1.05) translateY(-2px); background: var(--cw-primary-hover); }
#ef-chat-launcher svg { width: 30px; height: 30px; fill: white; transition: all 0.3s var(--cw-ease); position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

/* Icons State */
#ef-chat-launcher .icon-chat { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(0); }
#ef-chat-launcher .icon-close { opacity: 0; transform: translate(-50%, -50%) scale(0.5) rotate(-90deg); }
#ef-chat-launcher.active .icon-chat { opacity: 0; transform: translate(-50%, -50%) scale(0.5) rotate(90deg); }
#ef-chat-launcher.active .icon-close { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(0); }

/* Online Badge */
.ef-launcher-badge {
    position: absolute; top: -4px; right: -4px;
    min-width: 20px; height: 20px; padding: 0 5px;
    background: #ef4444; color: #ffffff;
    border: 2px solid var(--cw-bg-dark);
    border-radius: 999px; transition: transform 0.2s, opacity 0.2s;
    z-index: 2; display: none;
    align-items: center; justify-content: center;
    font-size: 0.7rem; font-weight: 700; line-height: 1;
}
.ef-launcher-badge.show { display: flex; }

/* --- MAIN CONTAINER --- */
#ef-chat-container {
    position: fixed; bottom: 100px; right: 24px;
    width: var(--cw-width); height: var(--cw-height);
    max-height: calc(100vh - 120px);
    background: var(--cw-bg-glass);
    backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
    border: 1px solid var(--cw-border);
    border-radius: var(--cw-radius);
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
    z-index: 2147483646;
    display: flex; flex-direction: column;
    transform: translateY(20px) scale(0.95); opacity: 0; visibility: hidden;
    transition: all 0.3s var(--cw-ease); overflow: hidden;
}
#ef-chat-container.open { transform: translateY(0) scale(1); opacity: 1; visibility: visible; }

/* --- HEADER --- */
.ef-chat-header {
    padding: 16px 20px;
    background: rgba(255,255,255,0.03);
    border-bottom: 1px solid var(--cw-border);
    display: flex; justify-content: space-between; align-items: center;
    flex-shrink: 0; z-index: 10;
}
.ef-header-info h3 { font-size: 1rem; font-weight: 700; color: var(--cw-text-main); margin-bottom: 2px; }
.ef-status-wrapper { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--cw-text-muted); font-weight: 500; }
.ef-status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--cw-text-muted); }
.ef-status-dot.online { background: var(--cw-green); box-shadow: 0 0 8px rgba(16,185,129,0.4); }

.ef-header-actions { display: flex; gap: 8px; align-items: center; }
.ef-header-actions button { width: 32px; height: 32px; border-radius: 8px; color: var(--cw-text-muted); }
.ef-header-actions button:hover { background: var(--cw-surface); color: var(--cw-text-main); }
.ef-header-actions svg { width: 18px; height: 18px; fill: currentColor; }

/* Mobile Close Button (Hidden on Desktop) */
#ef-btn-close-mobile { display: none; }

/* --- CHAT BODY --- */
.ef-chat-body {
    flex: 1; padding: 20px;
    overflow-y: auto; overflow-x: hidden;
    display: flex; flex-direction: column; gap: 14px;
    position: relative; scroll-behavior: auto;
    z-index: 5;
    overscroll-behavior-y: contain;
}
.ef-chat-body::-webkit-scrollbar { width: 5px; }
.ef-chat-body::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 10px; }

/* Messages */
.ef-msg-row { display: flex; width: 100%; }
.ef-msg-row.user { justify-content: flex-end; }
.ef-msg-row.agent { justify-content: flex-start; }

.ef-msg-bubble {
    max-width: 85%; padding: 10px 14px; border-radius: 16px;
    font-size: 0.95rem; line-height: 1.5; position: relative;
    word-wrap: break-word; color: var(--cw-text-main);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    animation: msgPop 0.3s var(--cw-ease);
}
@keyframes msgPop { from { opacity: 0; transform: translateY(10px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }

.ef-msg-row.agent .ef-msg-bubble { background: var(--cw-msg-agent-bg); border-bottom-left-radius: 4px; }
.ef-msg-row.user .ef-msg-bubble { background: var(--cw-msg-user-bg); color: var(--cw-msg-user-text); border-bottom-right-radius: 4px; }
.ef-chat-widget.guest-mode .ef-msg-row.user .ef-msg-bubble { background: #6b7280; color: #ffffff; border-bottom-right-radius: 4px; }
.ef-msg-bubble a { color: #7dd3fc; text-decoration: underline; word-break: break-all; }
.ef-msg-row.user .ef-msg-bubble a { color: #dbeafe; }
.ef-msg-link-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-top: 6px;
    padding: 6px 12px;
    border-radius: 10px;
    background: #2563eb;
    color: #ffffff !important;
    border: 1px solid rgba(255, 255, 255, 0.24);
    text-decoration: none !important;
    font-size: 0.78rem;
    font-weight: 700;
    line-height: 1.15;
    letter-spacing: 0.01em;
    max-width: 100%;
    word-break: break-word;
}
.ef-msg-link-btn:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
}

.ef-msg-img { max-width: 100%; border-radius: 12px; cursor: pointer; margin-bottom: 4px; display: block; border: 1px solid rgba(255,255,255,0.1); }
.ef-msg-time { font-size: 0.65rem; opacity: 0.7; text-align: right; display: block; margin-top: 2px; font-weight: 500; }
.ef-msg-date-sep {
    align-self: center;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    color: rgba(255,255,255,0.78);
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 999px;
    padding: 4px 10px;
    margin: 4px 0 2px;
}

/* CUSTOM AUDIO PLAYER */
.ef-audio-msg {
    display: flex; align-items: center; gap: 12px;
    width: 100%;
    max-width: 240px;
    min-width: 220px;
    padding: 4px 0;
}
.ef-audio-control-btn {
    width: 32px; height: 32px; border-radius: 50%;
    background: rgba(0,0,0,0.15); flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    color: inherit; cursor: pointer; transition: 0.2s;
    border: 1px solid rgba(255,255,255,0.1);
}
.ef-audio-control-btn:hover { background: rgba(0,0,0,0.25); }
.ef-audio-control-btn svg { width: 12px; height: 12px; fill: currentColor; }

/* Waveform Visualizer in Bubble */
.ef-audio-wave-static {
    flex: 1; height: 24px; 
    display: flex; align-items: center; gap: 3px;
    overflow: hidden;
}
.ef-wave-bar-static {
    width: 3px; background: currentColor; 
    opacity: 0.35; border-radius: 2px;
    transition: opacity 0.1s; min-height: 4px; 
}
.ef-wave-bar-static.played { opacity: 1; } 

.ef-audio-meta {
    font-size: 0.7rem; min-width: 30px; text-align: right; 
    font-variant-numeric: tabular-nums; opacity: 0.9; font-weight: 500;
}
.ef-msg-bubble.ef-msg-bubble-audio {
    padding: 8px 12px;
    min-width: 248px;
}
.ef-msg-row.user .ef-msg-bubble.ef-msg-bubble-audio {
    background: var(--cw-msg-user-bg);
    border-bottom-right-radius: 4px;
}
.ef-msg-row.agent .ef-msg-bubble.ef-msg-bubble-audio {
    background: var(--cw-msg-agent-bg);
    border-bottom-left-radius: 4px;
}

/* Scroll Badge */
.ef-scroll-badge {
    position: absolute; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px);
    background: var(--cw-primary); color: white;
    padding: 8px 16px; border-radius: 30px;
    font-size: 0.8rem; font-weight: 600;
    cursor: pointer; box-shadow: 0 8px 20px rgba(0,0,0,0.4);
    z-index: 50; opacity: 0; visibility: hidden; transition: all 0.3s var(--cw-ease);
    display: flex; gap: 8px; align-items: center; border: 1px solid rgba(255,255,255,0.2);
    pointer-events: auto;
}
.ef-scroll-badge.visible { opacity: 1; visibility: visible; transform: translateX(-50%) translateY(0); }
.ef-scroll-badge svg { width: 12px; height: 12px; fill: white; animation: bounce 2s infinite; }
@keyframes bounce { 0%, 20%, 50%, 80%, 100% {transform: translateY(0);} 40% {transform: translateY(-3px);} 60% {transform: translateY(-2px);} }

/* --- FOOTER & INPUT --- */
.ef-chat-footer {
    padding: 12px 16px 20px 16px;
    border-top: 1px solid var(--cw-border);
    background: rgba(10, 15, 30, 0.98); 
    flex-shrink: 0; position: relative;
    display: flex; flex-direction: column; gap: 10px;
    z-index: 2147483650;
}

/* Media Preview */
.ef-media-preview {
    display: flex; align-items: center; justify-content: space-between;
    background: var(--cw-surface); padding: 10px; border-radius: 12px;
    margin-bottom: 8px; animation: fadeIn 0.2s; border: 1px solid var(--cw-border);
}
.ef-media-preview.hidden { display: none; }
.ef-media-preview img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; }
.ef-media-info { flex: 1; margin-left: 12px; font-size: 0.9rem; color: var(--cw-text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ef-btn-remove-media { color: var(--cw-red); padding: 6px; border-radius: 6px; }
.ef-btn-remove-media:hover { background: rgba(239,68,68,0.1); }
.ef-btn-remove-media svg { width: 18px; height: 18px; fill: currentColor; }

/* Input Bar Container */
.ef-input-container {
    display: flex; align-items: flex-end; gap: 8px;
    position: relative; min-height: 48px;
}

/* Textarea Wrapper */
.ef-textarea-wrapper {
    flex: 1; display: flex; align-items: center;
    background: rgba(255,255,255,0.05);
    border: 1px solid var(--cw-border);
    border-radius: 24px; padding: 2px 12px;
    transition: all 0.2s;
    min-height: 44px;
}
.ef-textarea-wrapper:focus-within { border-color: var(--cw-primary); background: rgba(255,255,255,0.08); }

#ef-chat-input {
    flex: 1; width: 100%;
    color: var(--cw-text-main); font-size: 16px; /* No iOS zoom */
    resize: none; max-height: 120px;
    padding: 10px 0; line-height: 1.5;
    overflow-y: hidden;
}

/* Action Buttons */
.ef-action-btn {
    width: 40px; height: 40px; border-radius: 50%;
    color: var(--cw-text-muted); flex-shrink: 0; transition: 0.2s; margin-bottom: 2px;
    pointer-events: auto;
}
.ef-action-btn:hover { color: var(--cw-text-main); background: var(--cw-surface); }
.ef-action-btn svg { width: 22px; height: 22px; fill: currentColor; }

/* Send Button */
#ef-btn-send {
    background: var(--cw-primary); color: white; display: none;
    width: 44px; height: 44px; margin-left: 4px; margin-bottom: 2px;
}
#ef-btn-send:hover { background: var(--cw-primary-hover); transform: scale(1.05); }
#ef-btn-send svg { transform: translateX(2px); width: 20px; height: 20px; }

/* Mic Button */
#ef-btn-mic { 
    width: 44px; height: 44px; margin-left: 4px; margin-bottom: 2px;
    background: rgba(255,255,255,0.05); color: var(--cw-text-main);
}
#ef-btn-mic:hover { background: rgba(255,255,255,0.1); }

/* --- RECORDING OVERLAY --- */
.ef-recording-overlay {
    position: absolute; bottom: 0; left: 0; right: 0; top: 0;
    background: var(--cw-bg-dark); 
    display: none; align-items: center; justify-content: space-between;
    padding: 0 16px; z-index: 30; border-radius: 0 0 16px 16px;
    border-top: 1px solid var(--cw-border);
}
.ef-rec-left { display: flex; align-items: center; gap: 12px; flex: 1; }
.ef-rec-dot { width: 10px; height: 10px; background: var(--cw-red); border-radius: 50%; animation: pulse 1s infinite; flex-shrink: 0; }
.ef-rec-timer { font-variant-numeric: tabular-nums; color: var(--cw-text-main); font-weight: 700; font-size: 1rem; min-width: 56px; letter-spacing: 0.3px; }

/* Canvas for Real-time Waveform */
#ef-rec-canvas {
    height: 30px; flex: 1; margin-left: 10px;
    opacity: 0.9;
}

.ef-rec-actions { display: flex; gap: 8px; align-items: center; }
.ef-btn-cancel-rec { color: var(--cw-text-muted); width: 36px; height: 36px; border-radius: 50%; }
.ef-btn-cancel-rec:hover { color: var(--cw-red); background: rgba(239,68,68,0.1); }
.ef-btn-cancel-rec svg { width: 20px; height: 20px; fill: currentColor; }

/* Pause/Resume Button in Rec */
.ef-btn-pause-rec { color: var(--cw-text-main); width: 36px; height: 36px; border-radius: 50%; margin-right: 4px; }
.ef-btn-pause-rec:hover { background: rgba(255,255,255,0.1); }
.ef-btn-pause-rec svg { width: 18px; height: 18px; fill: currentColor; }
.ef-btn-pause-rec.paused { color: var(--cw-red); animation: pulse 2s infinite; }

.ef-btn-confirm-rec { background: var(--cw-green); color: white; width: 40px; height: 40px; border-radius: 50%; box-shadow: 0 4px 10px rgba(16,185,129,0.3); }
.ef-btn-confirm-rec:hover { transform: scale(1.1); }
.ef-btn-confirm-rec svg { width: 20px; height: 20px; fill: currentColor; }

/* --- EMOJI PICKER (OPTIMIZED FOR MOBILE) --- */
.ef-emoji-panel {
    position: absolute; bottom: 85px; left: 20px; right: 20px;
    height: 350px; background: #1e293b; border: 1px solid var(--cw-border);
    border-radius: 16px; box-shadow: var(--cw-shadow);
    display: none; flex-direction: column; overflow: hidden;
    z-index: 100; animation: fadeIn 0.2s;
}
.ef-emoji-panel.visible { display: flex; }

.ef-emoji-header { padding: 12px; border-bottom: 1px solid var(--cw-border); background: rgba(0,0,0,0.2); }
.ef-emoji-search {
    width: 100%; background: rgba(255,255,255,0.05); border: 1px solid var(--cw-border);
    border-radius: 8px; padding: 8px 12px; color: white; font-size: 0.9rem;
}
.ef-emoji-search:focus { border-color: var(--cw-primary); }

.ef-emoji-tabs { display: flex; justify-content: space-evenly; background: rgba(0,0,0,0.1); padding: 8px; border-bottom: 1px solid var(--cw-border); }
.ef-emoji-tab { padding: 6px; border-radius: 6px; cursor: pointer; font-size: 1.2rem; opacity: 0.5; transition: 0.2s; filter: grayscale(1); }
.ef-emoji-tab:hover { background: rgba(255,255,255,0.05); opacity: 0.8; }
.ef-emoji-tab.active { opacity: 1; background: rgba(255,255,255,0.1); filter: grayscale(0); transform: scale(1.1); }

.ef-emoji-list { flex: 1; overflow-y: auto; padding: 12px; display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; align-content: start; }
.ef-emoji-item { font-size: 1.4rem; cursor: pointer; text-align: center; border-radius: 6px; transition: 0.1s; padding: 4px 0; }
.ef-emoji-item:hover { background: rgba(255,255,255,0.1); transform: scale(1.2); }

/* --- RESPONSIVE MOBILE --- */
@media (max-width: 1024px) {
    #ef-btn-emoji { display: none !important; }
    #ef-emoji-panel { display: none !important; }
}

@media (max-width: 480px) {
    #ef-chat-container {
        width: 100%; height: 100dvh; max-height: 100dvh;
        bottom: 0; right: 0; border-radius: 0; border: none;
    }
    .ef-chat-header { padding-top: max(16px, env(safe-area-inset-top)); }
    .ef-chat-footer { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
    #ef-chat-launcher { bottom: 20px; right: 20px; }
    
    /* MOBILE FEATURE: Hide launcher when chat is open */
    #ef-chat-launcher.ef-hidden-mobile {
        opacity: 0; pointer-events: none; transform: scale(0);
        visibility: hidden;
    }

    /* MOBILE FEATURE: Show X close button */
    #ef-btn-close-mobile { display: flex; }

    /* MOBILE FEATURE: Taller Emoji Panel & Visibility */
    .ef-emoji-panel {
        left: 0; right: 0; bottom: 0; 
        border-radius: 20px 20px 0 0;
        position: fixed; 
        height: 55vh; /* Increased height to show 4+ rows */
        transform: translateY(100%); 
        transition: transform 0.3s;
        z-index: 2147483649; 
        padding-bottom: 90px; /* Safety padding */
    }
    .ef-emoji-panel.visible { display: flex; transform: translateY(0); }
    
    /* Ensure grid is dense enough for 4 rows */
    .ef-emoji-list {
        grid-template-columns: repeat(7, 1fr); /* Slightly larger icons on mobile */
    }
    .ef-emoji-item { font-size: 1.6rem; padding: 6px 0; }
}

@keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.4; } 100% { opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
