// ══════════════════════════════════════════════════════════════
// SHARED TRANSLATIONS (common keys across pages)
// ══════════════════════════════════════════════════════════════
const T_SHARED = {
  uk: {
    'nav-home':'Головна','nav-gameplay':'Геймплей','nav-download':'Завантажити','nav-docs':'Документація',
    'footer-tag':'Зроблено з ❤️ на C++20 + Vulkan. Відкрий свій світ.',
  },
  en: {
    'nav-home':'Home','nav-gameplay':'Gameplay','nav-download':'Download','nav-docs':'Docs',
    'footer-tag':'Made with ❤️ on C++20 + Vulkan. Open your world.',
  },
  fr: {
    'nav-home':'Accueil','nav-gameplay':'Gameplay','nav-download':'Télécharger','nav-docs':'Docs',
    'footer-tag':'Fait avec ❤️ en C++20 + Vulkan. Ouvre ton monde.',
  }
};

// Language switcher (reads from window.T which each page defines + T_SHARED)
let currentLang = localStorage.getItem('vr-lang') || 'uk';

function setLang(lang) {
  currentLang = lang;
  localStorage.setItem('vr-lang', lang);
  document.documentElement.lang = lang === 'uk' ? 'uk' : lang === 'fr' ? 'fr' : 'en';
  document.querySelectorAll('.lang-btn').forEach(b => {
    b.classList.toggle('active', b.textContent.toLowerCase() === lang);
  });
  applyTranslations();
}

function applyTranslations() {
  const pageT = (window.T && window.T[currentLang]) || {};
  const L = Object.assign({}, T_SHARED[currentLang] || {}, pageT);
  document.querySelectorAll('[data-i18n]').forEach(el => {
    const key = el.dataset.i18n;
    if (L[key] !== undefined) el.innerHTML = L[key];
  });
  document.querySelectorAll('[data-i18n-placeholder]').forEach(el => {
    const key = el.dataset.i18nPlaceholder;
    if (L[key] !== undefined) el.placeholder = L[key];
  });
  if (typeof onTranslationsApplied === 'function') onTranslationsApplied(L);
}

// Cursor glow
const cursorEl = document.getElementById('cursor-glow');
if (cursorEl) {
  document.addEventListener('mousemove', e => {
    cursorEl.style.left = e.clientX + 'px';
    cursorEl.style.top  = e.clientY + 'px';
  });
}

// Scroll progress
const progressEl = document.getElementById('progress');
if (progressEl) {
  window.addEventListener('scroll', () => {
    const pct = window.scrollY / Math.max(1, document.body.scrollHeight - window.innerHeight) * 100;
    progressEl.style.width = pct + '%';
  });
}

// Particle background
(function initParticleBg() {
  const bgC = document.getElementById('bg-canvas');
  if (!bgC) return;
  const bgX = bgC.getContext('2d');
  let W, H, particles = [];
  function resizeBg() { W = bgC.width = window.innerWidth; H = bgC.height = window.innerHeight; }
  resizeBg(); window.addEventListener('resize', resizeBg);
  for (let i = 0; i < 120; i++) particles.push({
    x: Math.random()*1000, y: Math.random()*1000,
    vx: (Math.random()-.5)*.3, vy: (Math.random()-.5)*.3,
    r: Math.random()*1.6+.3, a: Math.random()*.5+.1, hue: 30+Math.random()*25
  });
  function animBg() {
    bgX.clearRect(0,0,W,H);
    particles.forEach(p => {
      p.x = (p.x + p.vx + W) % W; p.y = (p.y + p.vy + H) % H;
      bgX.beginPath(); bgX.arc(p.x,p.y,p.r,0,Math.PI*2);
      bgX.fillStyle=`hsla(${p.hue},80%,65%,${p.a})`; bgX.fill();
    });
    for (let i=0;i<particles.length;i++) {
      for (let j=i+1;j<particles.length;j++) {
        const dx=particles[i].x-particles[j].x, dy=particles[i].y-particles[j].y;
        const d=Math.sqrt(dx*dx+dy*dy);
        if (d<110) {
          bgX.beginPath(); bgX.moveTo(particles[i].x,particles[i].y); bgX.lineTo(particles[j].x,particles[j].y);
          bgX.strokeStyle=`rgba(244,185,66,${.07*(1-d/110)})`; bgX.lineWidth=.5; bgX.stroke();
        }
      }
    }
    requestAnimationFrame(animBg);
  }
  animBg();
})();

// Scroll reveal
const revealObs = new IntersectionObserver(entries => {
  entries.forEach(en => {
    if (en.isIntersecting) en.target.classList.add('in');
  });
}, {threshold: 0.12});
document.querySelectorAll('.reveal').forEach(el => revealObs.observe(el));

// Init
document.addEventListener('DOMContentLoaded', () => setLang(currentLang));
