/* Base & Reset */
*, *::before, *::after {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    color: #0A1F3F;
    overflow-x: hidden;
}

.font-serif {
    font-family: 'Playfair Display', Georgia, serif;
}

.font-sans {
    font-family: 'Inter', system-ui, sans-serif;
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
}
::-webkit-scrollbar-track {
    background: #F8FAFB;
}
::-webkit-scrollbar-thumb {
    background: #7ED0C4;
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: #5BBFB2;
}

/* Nav color states */
.nav-color {
    color: #0A1F3F;
    transition: color 0.4s ease;
}
.nav-subcolor {
    color: #163A6B;
    transition: color 0.4s ease;
}

body.nav-scrolled .nav-color {
    color: #0A1F3F;
}
body.nav-scrolled .nav-subcolor {
    color: #163A6B;
}

/* Hero nav state */
body.hero-active .nav-color {
    color: #FFFFFF;
}
body.hero-active .nav-subcolor {
    color: rgba(255, 255, 255, 0.6);
}

/* Mobile menu link */
.mobile-menu-link {
    font-family: 'Playfair Display', Georgia, serif;
}

/* Reveal animations — progressive enhancement via JS */
.reveal {
    opacity: 1;
    transform: none;
    transition: opacity 0.8s ease, transform 0.8s ease;
}

@media (prefers-reduced-motion: no-preference) {
    .reveal {
        opacity: 0;
        transform: translateY(30px);
    }
    .reveal.revealed {
        opacity: 1;
        transform: translateY(0);
    }
    .reveal.delay-1 { transition-delay: 0.1s; }
    .reveal.delay-2 { transition-delay: 0.2s; }
    .reveal.delay-3 { transition-delay: 0.3s; }
}

/* Navbar scroll state */
body.nav-scrolled #navbar {
    background: rgba(255, 255, 255, 0.97);
    backdrop-filter: blur(12px);
    box-shadow: 0 1px 0 rgba(10, 31, 63, 0.08);
}

/* Mobile menu open */
body.menu-open {
    overflow: hidden;
}

#mobile-menu {
    transform: translateX(100%);
}
body.menu-open #mobile-menu {
    transform: translateX(0);
}

/* Hamburger animation */
body.menu-open #menu-line-1 {
    transform: rotate(45deg) translate(5px, 5px);
}
body.menu-open #menu-line-2 {
    opacity: 0;
}
body.menu-open #menu-line-3 {
    transform: rotate(-45deg) translate(5px, -5px);
    width: 1.5rem;
}

/* Back to top button */
#back-to-top.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

/* Focus styles */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid #7ED0C4;
    outline-offset: 2px;
    border-radius: 4px;
}

/* Selection color */
::selection {
    background: #7ED0C4;
    color: #0A1F3F;
}
</style>

<sm-file path="script.js">
(function () {
    'use strict';

    // ─── Navbar scroll behavior ───────────────────────────────────────────────
    const navbar = document.getElementById('navbar');
    const heroSection = document.getElementById('hero');
    const body = document.body;

    function updateNav() {
        const heroBottom = heroSection.getBoundingClientRect().bottom;
        const scrolled = window.scrollY > 80;

        if (scrolled) {
            body.classList.add('nav-scrolled');
            body.classList.remove('hero-active');
        } else if (heroBottom <= 0) {
            body.classList.add('nav-scrolled');
            body.classList.remove('hero-active');
        } else {
            body.classList.remove('nav-scrolled');
            body.classList.add('hero-active');
        }
    }

    window.addEventListener('scroll', updateNav, { passive: true });
    updateNav();

    // ─── Mobile menu ──────────────────────────────────────────────────────────
    const menuBtn = document.getElementById('mobile-menu-btn');
    const mobileMenu = document.getElementById('mobile-menu');
    const menuLinks = document.querySelectorAll('.mobile-menu-link');
    let menuOpen = false;

    function toggleMenu() {
        menuOpen = !menuOpen;
        body.classList.toggle('menu-open', menuOpen);
        menuBtn.setAttribute('aria-expanded', menuOpen);
    }

    menuBtn.addEventListener('click', toggleMenu);

    menuLinks.forEach(function (link) {
        link.addEventListener('click', function () {
            if (menuOpen) toggleMenu();
        });
    });

    // Close on Escape
    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape' && menuOpen) toggleMenu();
    });

    // ─── Scroll reveal (progressive enhancement) ──────────────────────────────
    const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    if (!prefersReducedMotion && 'IntersectionObserver' in window) {
        const revealElements = document.querySelectorAll('.reveal');

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

        revealElements.forEach(function (el) {
            revealObserver.observe(el);
        });
    } else {
        // If JS is disabled or reduced motion is preferred, ensure all elements are visible
        document.querySelectorAll('.reveal').forEach(function (el) {
            el.classList.add('revealed');
        });
    }

    // ─── Back to top button ───────────────────────────────────────────────────
    const backToTop = document.getElementById('back-to-top');

    function toggleBackToTop() {
        if (window.scrollY > 600) {
            backToTop.classList.add('visible');
        } else {
            backToTop.classList.remove('visible');
        }
    }

    window.addEventListener('scroll', toggleBackToTop, { passive: true });

    backToTop.addEventListener('click', function () {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });

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

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

            // Basic validation
            const inputs = contactForm.querySelectorAll('input[required], textarea[required]');
            let valid = true;

            inputs.forEach(function (input) {
                if (!input.value.trim()) {
                    input.classList.add('border-red-400');
                    valid = false;
                } else {
                    input.classList.remove('border-red-400');
                }
            });

            if (valid) {
                contactForm.style.display = 'none';
                formSuccess.classList.remove('hidden');
            }
        });
    }

    // ─── Smooth scroll for anchor links ───────────────────────────────────────
    document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
        anchor.addEventListener('click', function (e) {
            const targetId = this.getAttribute('href');
            if (targetId === '#') return;

            const target = document.querySelector(targetId);
            if (target) {
                e.preventDefault();
                const navHeight = navbar.offsetHeight;
                const targetPosition = target.getBoundingClientRect().top + window.scrollY - navHeight - 20;

                window.scrollTo({
                    top: targetPosition,
                    behavior: 'smooth'
                });
            }
        });
    });
})();
