/**
 * 企业CMS - 前端交互脚本 v2
 * UI Designer 设计方案
 */

document.addEventListener('DOMContentLoaded', function() {

    // ===== Header 滚动效果 =====
    const header = document.querySelector('.header');
    let lastScroll = 0;

    window.addEventListener('scroll', function() {
        const currentScroll = window.pageYOffset;

        // Add shadow on scroll
        if (currentScroll > 10) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }

        lastScroll = currentScroll;
    }, { passive: true });

    // ===== Back to Top Button =====
    const backToTop = document.querySelector('.back-to-top');

    if (backToTop) {
        window.addEventListener('scroll', function() {
            if (window.pageYOffset > 400) {
                backToTop.classList.add('visible');
            } else {
                backToTop.classList.remove('visible');
            }
        }, { passive: true });
    }

    // ===== Scroll Reveal Animation =====
    const observerOptions = {
        root: null,
        rootMargin: '0px 0px -60px 0px',
        threshold: 0.1
    };

    const observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                entry.target.classList.add('visible');
                observer.unobserve(entry.target);
            }
        });
    }, observerOptions);

    // Observe elements with fade-up class
    document.querySelectorAll('.fade-up').forEach(function(el) {
        observer.observe(el);
    });

    // Auto-add fade-up to sections
    document.querySelectorAll('.features, .products, .news, .contact-brief').forEach(function(section) {
        section.classList.add('fade-up');
        observer.observe(section);
    });

    // Also observe cards
    document.querySelectorAll('.card').forEach(function(card, index) {
        card.style.transitionDelay = (index % 3) * 80 + 'ms';
        card.classList.add('fade-up');
        observer.observe(card);
    });

    // ===== Smooth scroll for anchor links =====
    document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            var target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({ behavior: 'smooth', block: 'start' });
            }
        });
    });

    // ===== Nav active state update =====
    // Highlight current page link based on URL

});
