document.addEventListener('DOMContentLoaded', () => {
    // Initialize Lucide Icons
    lucide.createIcons();

    // Initialize AOS
    AOS.init({
        duration: 800,
        once: true,
        offset: 100
    });

    // Countdown Timer
    const countdownDate = new Date('June 15, 2026 23:59:59').getTime();

    const updateCountdown = () => {
        const now = new Date().getTime();
        const distance = countdownDate - now;

        if (distance < 0) {
            document.getElementById('countdown').innerHTML = "OFERTA FINALIZADA";
            return;
        }

        const days = Math.floor(distance / (1000 * 60 * 60 * 24));
        const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((distance % (1000 * 60)) / 1000);

        document.getElementById('days').innerText = days.toString().padStart(2, '0');
        document.getElementById('hours').innerText = hours.toString().padStart(2, '0');
        document.getElementById('minutes').innerText = minutes.toString().padStart(2, '0');
        document.getElementById('seconds').innerText = seconds.toString().padStart(2, '0');
    };

    setInterval(updateCountdown, 1000);
    updateCountdown();

    // FAQ Accordion
    const faqItems = document.querySelectorAll('.faq-item');
    faqItems.forEach(item => {
        const question = item.querySelector('.faq-question');
        question.addEventListener('click', () => {
            faqItems.forEach(i => {
                if (i !== item) i.classList.remove('active');
            });
            item.classList.toggle('active');
        });
    });

    // Form Submission (Simulation)
    const contactForm = document.getElementById('contact-form');
    if (contactForm) {
        contactForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const button = contactForm.querySelector('button');
            const originalText = button.innerText;
            
            button.innerText = 'Enviando...';
            button.disabled = true;

            // Simulate API call
            setTimeout(() => {
                alert('¡Gracias por tu interés! Un asesor se contactará contigo en menos de 5 minutos para enviarte el Manual del Inversionista.');
                button.innerText = originalText;
                button.disabled = false;
                contactForm.reset();
            }, 1500);
        });
    }

    // Dynamic Inventory Counter (Visual effect)
    const unitsElement = document.getElementById('units-count');
    if (unitsElement) {
        setTimeout(() => {
            unitsElement.style.color = '#e74c3c';
            setTimeout(() => unitsElement.style.color = '', 1000);
        }, 3000);
    }

    // Sticky Header Effect
    const header = document.getElementById('main-header');
    if (header) {
        window.addEventListener('scroll', () => {
            if (window.scrollY > 50) {
                header.style.padding = '10px 0';
            } else {
                header.style.padding = '20px 0';
            }
        });
    }
});
