<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Previsualización Botón WhatsApp</title>
    <style>
        /* --- ESTILOS CSS --- */
        body { font-family: sans-serif; background-color: #f0f2f5; height: 100vh; margin: 0; }

        :root {
            --wa-main-color: #fbb03b; /* Amarillo/Naranja de tu imagen */
            --wa-dark-text: #4a4a4a;
            --wa-bg-color: #ffffff;
        }

        /* Botón flotante */
        .wa-float-btn {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #25D366;
            color: white;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
            cursor: pointer;
            z-index: 1000;
            transition: transform 0.3s ease;
        }

        .wa-float-btn:hover { transform: scale(1.1); }

        /* Widget de Chat */
        .wa-widget-container {
            display: none;
            position: fixed;
            bottom: 90px;
            right: 20px;
            width: 320px;
            background-color: var(--wa-bg-color);
            border-radius: 20px;
            box-shadow: 0 8px 30px rgba(0,0,0,0.2);
            z-index: 1001;
            overflow: hidden;
            font-family: 'Segoe UI', sans-serif;
            animation: slideIn 0.3s ease-out;
            border: 1px solid #eee;
        }

        @keyframes slideIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* Cabecera */
        .wa-header {
            background-color: var(--wa-main-color);
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: white;
        }

        .wa-header-info { display: flex; align-items: center; gap: 8px; font-weight: 600; }
        .wa-status-dot {
            width: 10px; height: 10px;
            background-color: #25D366;
            border-radius: 50%; border: 2px solid white;
        }

        .wa-close {
            background: none; border: none; color: white;
            font-size: 24px; cursor: pointer; opacity: 0.8;
        }
        .wa-close:hover { opacity: 1; }

        /* Cuerpo */
        .wa-body { padding: 20px; background-color: #fafafa; }

        .wa-message-bubble {
            background-color: white;
            padding: 12px 16px;
            border-radius: 12px 12px 12px 0;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
            margin-bottom: 20px;
            color: var(--wa-dark-text);
            font-size: 14px;
            line-height: 1.5;
            position: relative;
        }
        
        /* Pequeña "colita" del globo de chat */
        .wa-message-bubble::before {
            content: ''; position: absolute; bottom: 0; left: -8px;
            width: 0; height: 0;
            border-style: solid; border-width: 0 10px 10px 0;
            border-color: transparent white transparent transparent;
        }

        .wa-input-area textarea {
            width: 100%; height: 70px;
            border: 1px solid #ddd;
            border-radius: 10px;
            padding: 10px;
            font-family: inherit; resize: none; outline: none;
            box-sizing: border-box;
            font-size: 14px;
        }
        .wa-input-area textarea:focus { border-color: var(--wa-main-color); }

        /* Pie */
        .wa-footer { padding: 15px 20px; background-color: white; border-top: 1px solid #f0f0f0; }

        .wa-launch-btn {
            background-color: var(--wa-main-color);
            color: white; border: none;
            padding: 12px; border-radius: 25px;
            font-weight: bold; cursor: pointer; width: 100%;
            display: flex; align-items: center; justify-content: center; gap: 10px;
            font-size: 15px;
            transition: background-color 0.2s;
        }
        .wa-launch-btn:hover { background-color: #e69b2b; }
    </style>
</head>
<body>

    <h2 style="text-align:center; padding-top:50px; color:#666;">Haz click en el icono verde abajo a la derecha ↘</h2>

    <div id="wa-floating-button" class="wa-float-btn">
        <svg width="32" height="32" viewBox="0 0 24 24" fill="white"><path d="M.057 24l1.687-6.163c-1.041-1.804-1.588-3.849-1.587-5.946.003-6.556 5.338-11.891 11.893-11.891 3.181.001 6.167 1.24 8.413 3.488 2.245 2.248 3.481 5.236 3.48 8.414-.003 6.557-5.338 11.892-11.893 11.892-1.99-.001-3.951-.5-5.688-1.448l-6.305 1.654zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884-.001 2.225.651 3.891 1.746 5.634l-.999 3.648 3.742-.981zm11.387-5.464c-.074-.124-.272-.198-.57-.347-.297-.149-1.758-.868-2.031-.967-.272-.099-.47-.149-.669.149-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.297-.347.446-.521.151-.172.2-.296.3-.495.099-.198.05-.372-.025-.521-.075-.148-.669-1.611-.916-2.206-.242-.579-.487-.501-.669-.51l-.57-.01c-.198 0-.52.074-.792.372s-1.04 1.016-1.04 2.479 1.065 2.876 1.213 3.074c.149.198 2.095 3.2 5.076 4.487.709.306 1.263.489 1.694.626.712.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.248-.695.248-1.29.173-1.414z"/></svg>
    </div>

    <div id="wa-chat-widget" class="wa-widget-container">
        <div class="wa-header">
            <div class="wa-header-info">
                <span class="wa-status-dot"></span>
                <span>Atención al Cliente</span>
            </div>
            <button id="wa-close-btn" class="wa-close">&times;</button>
        </div>
        <div class="wa-body">
            <div class="wa-message-bubble">
                <strong>¡Hola!</strong> 👋<br>Escribe tu consulta abajo y te responderemos por WhatsApp.
            </div>
            <div class="wa-input-area">
                <textarea id="wa-user-input" placeholder="Escribe tu mensaje..."></textarea>
            </div>
        </div>
        <div class="wa-footer">
            <button id="wa-send-btn" class="wa-launch-btn">
                Enviar a WhatsApp
                <svg width="18" height="18" viewBox="0 0 24 24" fill="white" style="transform: rotate(-45deg) translateY(-2px);"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg>
            </button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const floatBtn = document.getElementById('wa-floating-button');
            const widget = document.getElementById('wa-chat-widget');
            const closeBtn = document.getElementById('wa-close-btn');
            const sendBtn = document.getElementById('wa-send-btn');
            const userInput = document.getElementById('wa-user-input');

            // --- LÓGICA DEL WIDGET ---
            
            // 1. Abrir/Cerrar
            floatBtn.addEventListener('click', () => {
                const isHidden = widget.style.display === 'none' || widget.style.display === '';
                widget.style.display = isHidden ? 'block' : 'none';
                if(isHidden) userInput.focus();
            });

            closeBtn.addEventListener('click', () => {
                widget.style.display = 'none';
            });

            // 2. Enviar Mensaje
            sendBtn.addEventListener('click', () => {
                const mensaje = userInput.value.trim();
                
                // VALIDACIÓN: Campo vacío
                if (!mensaje) {
                    userInput.style.border = "2px solid #ff4444";
                    userInput.placeholder = "¡Necesitas escribir algo!";
                    setTimeout(() => {
                        userInput.style.border = "1px solid #ddd";
                        userInput.placeholder = "Escribe tu mensaje...";
                    }, 2000);
                    return; // Detiene la ejecución
                }

                // Si todo está bien:
                // A. Crear el enlace (pon aquí tu número real en producción)
                const numero = "51999999999"; 
                const url = `https://wa.me/${numero}?text=${encodeURIComponent(mensaje)}`;
                
                // B. Disparar Evento Personalizado
                const event = new CustomEvent('whatsappMessageSent', {
                    detail: { message: mensaje, time: new Date() }
                });
                document.dispatchEvent(event);

                // C. Abrir WhatsApp
                window.open(url, '_blank');
                
                // D. Limpiar
                widget.style.display = 'none';
                userInput.value = '';
            });

            // --- ESTO ES SOLO PARA QUE VEAS QUE EL EVENTO FUNCIONA ---
            document.addEventListener('whatsappMessageSent', (e) => {
                alert(`✅ EVENTO DETECTADO:\n\nTexto enviado: "${e.detail.message}"\n\n(Aquí se activaría tu código de seguimiento de Google/Facebook)`);
            });
        });
    </script>
</body>
</html>