/* Custom styles for Halls Land Management */

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Scroll reveal base - content is visible by default */
.scroll-reveal {
    opacity: 1;
    transform: none;
    transition: opacity 0.6s ease, transform 0.6s ease;
}

/* Only apply animation when JS is active and user prefers motion */
@media (prefers-reduced-motion: no-preference) {
    .js-enabled .scroll-reveal {
        opacity: 0;
        transform: translateY(24px);
    }

    .js-enabled .scroll-reveal.revealed {
        opacity: 1;
        transform: translateY(0);
    }

    .js-enabled .scroll-reveal.revealed:nth-child(2) { transition-delay: 0.1s; }
    .js-enabled .scroll-reveal.revealed:nth-child(3) { transition-delay: 0.2s; }
    .js-enabled .scroll-reveal.revealed:nth-child(4) { transition-delay: 0.3s; }
    .js-enabled .scroll-reveal.revealed:nth-child(5) { transition-delay: 0.4s; }
    .js-enabled .scroll-reveal.revealed:nth-child(6) { transition-delay: 0.5s; }
}

/* Header scroll state */
#site-header.scrolled {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* Mobile menu transitions */
#mobile-menu {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
}

#mobile-menu.open {
    max-height: 400px;
}

/* Mobile nav link padding */
.mobile-nav-link {
    padding: 0.5rem 0;
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #0a1628;
}

::-webkit-scrollbar-thumb {
    background: #c9a84c;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #d4b960;
}

/* Selection color */
::selection {
    background: #c9a84c;
    color: #0a1628;
}

/* Focus visible styles */
:focus-visible {
    outline: 2px solid #c9a84c;
    outline-offset: 2px;
}

/* Button ripple effect on hover */
button, a {
    -webkit-tap-highlight-color: transparent;
}
</style>
<sm-file path="script.js">
(function () {
    'use strict';

    // Mark JS as enabled for progressive enhancement
    document.documentElement.classList.add('js-enabled');

    // Mobile menu toggle
    const menuToggle = document.getElementById('menu-toggle');
    const mobileMenu = document.getElementById('mobile-menu');
    const menuIconOpen = document.getElementById('menu-icon-open');
    const menuIconClose = document.getElementById('menu-icon-close');
    const mobileNavLinks = mobileMenu.querySelectorAll('a');

    menuToggle.addEventListener('click', function () {
        const isOpen = !mobileMenu.classList.contains('hidden');
        mobileMenu.classList.toggle('hidden');
        menuIconOpen.classList.toggle('hidden');
        menuIconClose.classList.toggle('hidden');
    });

    // Close mobile menu when a link is clicked
    mobileNavLinks.forEach(function (link) {
        link.addEventListener('click', function () {
            mobileMenu.classList.add('hidden');
            menuIconOpen.classList.remove('hidden');
            menuIconClose.classList.add('hidden');
        });
    });

    // Header scroll effect
    const header = document.getElementById('site-header');
    let lastScroll = 0;

    window.addEventListener('scroll', function () {
        const currentScroll = window.pageYOffset;
        if (currentScroll > 50) {
            header.classList.add('scrolled');
        } else {
            header.classList.remove('scrolled');
        }
        lastScroll = currentScroll;
    }, { passive: true });

    // Scroll reveal using IntersectionObserver
    if (window.matchMedia('(prefers-reduced-motion: no-preference)').matches) {
        const revealElements = document.querySelectorAll('.scroll-reveal');

        if ('IntersectionObserver' in window) {
            const revealObserver = new IntersectionObserver(
                function (entries) {
                    entries.forEach(function (entry) {
                        if (entry.isIntersecting) {
                            entry.target.classList.add('revealed');
                            revealObserver.unobserve(entry.target);
                        }
                    });
                },
                {
                    threshold: 0.1,
                    rootMargin: '0px 0px -40px 0px'
                }
            );

            revealElements.forEach(function (el) {
                revealObserver.observe(el);
            });
        } else {
            // Fallback: show all elements immediately
            revealElements.forEach(function (el) {
                el.classList.add('revealed');
            });
        }
    } else {
        // User prefers reduced motion — reveal all immediately
        document.querySelectorAll('.scroll-reveal').forEach(function (el) {
            el.classList.add('revealed');
        });
    }

    // Contact form handling
    const contactForm = document.getElementById('contact-form');
    const formSuccess = document.getElementById('form-success');

    if (contactForm) {
        contactForm.addEventListener('submit', function (e) {
            e.preventDefault();

            // Simple validation check
            const requiredFields = contactForm.querySelectorAll('[required]');
            let isValid = true;

            requiredFields.forEach(function (field) {
                if (!field.value.trim()) {
                    isValid = false;
                    field.style.borderColor = '#ef4444';
                } else {
                    field.style.borderColor = '';
                }
            });

            if (isValid) {
                // Hide form, show success
                contactForm.style.display = 'none';
                formSuccess.classList.remove('hidden');
            }
        });
    }

    // Active nav link highlighting
    const sections = document.querySelectorAll('section[id]');
    const navLinks = document.querySelectorAll('nav a[href^="#"]');

    if (sections.length > 0 && navLinks.length > 0) {
        window.addEventListener('scroll', function () {
            const scrollPos = window.pageYOffset + 100;

            sections.forEach(function (section) {
                const top = section.offsetTop;
                const height = section.offsetHeight;
                const id = section.getAttribute('id');

                if (scrollPos >= top && scrollPos < top + height) {
                    navLinks.forEach(function (link) {
                        link.classList.remove('text-gold-400');
                        if (link.getAttribute('href') === '#' + id) {
                            link.classList.add('text-gold-400');
                        }
                    });
                }
            });
        }, { passive: true });
    }
})();
