document.addEventListener('DOMContentLoaded', function() {
    const languageToggle = document.getElementById('language-toggle');
    const langLabel = document.getElementById('lang-label');
    const portugueseContent = document.getElementById('portuguese-content');
    const englishContent = document.getElementById('english-content');
    const heroHeading = document.getElementById('hero-heading');
    const navCommitment = document.getElementById('nav-commitment');
    const navDocuments = document.getElementById('nav-documents');
    const htmlElement = document.documentElement;

    // Get saved language preference or default to Portuguese
    const savedLanguage = localStorage.getItem('preferred-language') || 'pt-PT';
    setLanguage(savedLanguage);

    languageToggle.addEventListener('click', function() {
        const currentLang = htmlElement.lang;
        const newLang = currentLang === 'pt-PT' ? 'en' : 'pt-PT';
        setLanguage(newLang);
    });

    function setLanguage(lang) {
        if (lang === 'en') {
            htmlElement.lang = 'en';
            portugueseContent.classList.add('hidden');
            englishContent.classList.remove('hidden');
            heroHeading.lang = 'en';
            heroHeading.textContent = 'The internet should be accessible to everyone. This is our commitment.';
            langLabel.textContent = 'Português';
            if (navCommitment) navCommitment.textContent = 'Commitment';
            if (navDocuments) navDocuments.textContent = 'Documents';
        } else {
            htmlElement.lang = 'pt-PT';
            englishContent.classList.add('hidden');
            portugueseContent.classList.remove('hidden');
            heroHeading.lang = 'pt-PT';
            heroHeading.textContent = 'A internet deve ser acessível para todos. Este é o nosso compromisso.';
            langLabel.textContent = 'English';
            if (navCommitment) navCommitment.textContent = 'Compromisso';
            if (navDocuments) navDocuments.textContent = 'Documentos';
        }
        localStorage.setItem('preferred-language', lang);
    }
});
