// Initialize Lucide Icons
lucide.createIcons();

// Mobile Menu Logic
const mobileToggle = document.getElementById('mobile-toggle');
const navLinks = document.getElementById('nav-links');

if (mobileToggle) {
    mobileToggle.addEventListener('click', () => {
        navLinks.classList.toggle('active');
        
        // Block body scroll
        if (navLinks.classList.contains('active')) {
            document.body.style.overflow = 'hidden';
            if(document.documentElement.classList.contains('snap-scroll-enabled')){
                document.documentElement.style.overflowY = 'hidden';
            }
        } else {
            document.body.style.overflow = '';
            if(document.documentElement.classList.contains('snap-scroll-enabled')){
                document.documentElement.style.overflowY = 'scroll';
            }
        }
    });
}

// Close mobile menu on link click
const mobileNavLinks = document.querySelectorAll('.nav-link-main, .muted-link, .mega-utilities a');
mobileNavLinks.forEach(link => {
    link.addEventListener('click', () => {
        if(navLinks && navLinks.classList.contains('active')) {
            navLinks.classList.remove('active');
            document.body.style.overflow = '';
            if(document.documentElement.classList.contains('snap-scroll-enabled')){
                document.documentElement.style.overflowY = 'scroll';
            }
        }
    });
});

// Toggle Mega Menu on Mobile (Accordion like)
const megaTriggers = document.querySelectorAll('.mega-trigger');
megaTriggers.forEach(trigger => {
    trigger.addEventListener('click', (e) => {
        if (window.innerWidth <= 768) {
            e.preventDefault(); 
            trigger.parentElement.classList.toggle('expanded');
        }
    });
});

// Register ScrollTrigger
gsap.registerPlugin(ScrollTrigger);

// Navbar Scroll Effect
const navbar = document.getElementById('navbar');
window.addEventListener('scroll', () => {
    if (window.scrollY > 50) {
        navbar.classList.add('scrolled');
    } else {
        navbar.classList.remove('scrolled');
    }
});

// Hero Animation (initial load)
gsap.from(".hero-title", {
    duration: 1.2,
    y: 50,
    opacity: 0,
    ease: "power4.out",
    delay: 0.2
});

gsap.from(".hero-subtitle", {
    duration: 1.2,
    y: 30,
    opacity: 0,
    ease: "power4.out",
    delay: 0.4
});

gsap.from(".hero .btn", {
    duration: 1,
    y: 20,
    opacity: 0,
    ease: "power4.out",
    delay: 0.6,
    stagger: 0.1
});

// Apple/Tesla like fade-up animations on scroll (Naprawione na rzecz Scroll-Snappingu)
// Używamy całych sekcji jako wyzwalaczy (triggers) z racji, że szybkie snapy psuły kalkulacje małych elementów.
const sectionsForReveal = document.querySelectorAll('section, footer');

sectionsForReveal.forEach((sec) => {
    const reveals = sec.querySelectorAll('.reveal');
    if (reveals.length > 0) {
        gsap.to(reveals, {
            scrollTrigger: {
                trigger: sec,
                start: "top 75%", // Odpali jak tylko 25% sekcji wejdzie na pole widzenia
                toggleActions: "play none none reverse" 
            },
            y: 0,
            opacity: 1,
            duration: 1.2,
            stagger: 0.15,
            ease: "power3.out"
        });
    }
});

// Zabezpieczenie po załadowaniu elementów i upewnieniu się, że Snapping ma poprawne gabaryty:
window.addEventListener('load', () => {
    ScrollTrigger.refresh();
});

// Parallax slight effect for images
const imgWrappers = document.querySelectorAll('.img-wrapper img');
imgWrappers.forEach((img) => {
    gsap.to(img, {
        scrollTrigger: {
            trigger: img.parentElement,
            start: "top bottom",
            end: "bottom top",
            scrub: true
        },
        y: 30, 
        scale: 1.05,
        ease: "none"
    });
});
