// Slide navigation
(function() {
  const slides = document.querySelectorAll('.slide');
  let current = 0;

  // Counter
  const counter = document.createElement('div');
  counter.className = 'slide-counter';
  document.body.appendChild(counter);

  // Keyboard hint
  const hint = document.createElement('div');
  hint.className = 'keyboard-hint';
  hint.textContent = 'Fleches / Espace pour naviguer | F plein ecran | L clair/sombre';
  document.body.appendChild(hint);

  // Mode indicator
  const modeInd = document.createElement('div');
  modeInd.className = 'mode-indicator';
  document.body.appendChild(modeInd);

  function showModeHint(text) {
    modeInd.textContent = text;
    modeInd.classList.add('show');
    setTimeout(() => modeInd.classList.remove('show'), 1500);
  }

  function showSlide(index) {
    if (index < 0) index = 0;
    if (index >= slides.length) index = slides.length - 1;
    slides.forEach(s => s.classList.remove('active'));
    slides[index].classList.add('active');
    current = index;
    counter.textContent = (current + 1) + ' / ' + slides.length;
    // Update URL hash
    history.replaceState(null, null, '#' + (current + 1));
  }

  function next() { showSlide(current + 1); }
  function prev() { showSlide(current - 1); }

  // Keyboard
  document.addEventListener('keydown', function(e) {
    switch(e.key) {
      case 'ArrowRight':
      case 'ArrowDown':
      case ' ':
      case 'PageDown':
        e.preventDefault();
        next();
        break;
      case 'ArrowLeft':
      case 'ArrowUp':
      case 'PageUp':
        e.preventDefault();
        prev();
        break;
      case 'Home':
        e.preventDefault();
        showSlide(0);
        break;
      case 'End':
        e.preventDefault();
        showSlide(slides.length - 1);
        break;
      case 'f':
      case 'F':
        if (!document.fullscreenElement) {
          document.documentElement.requestFullscreen();
        } else {
          document.exitFullscreen();
        }
        break;
      case 'l':
      case 'L':
        document.body.classList.toggle('light-mode');
        showModeHint(document.body.classList.contains('light-mode') ? 'Mode clair' : 'Mode sombre');
        break;
    }
  });

  // Click navigation (left half = prev, right half = next)
  document.addEventListener('click', function(e) {
    if (e.clientX < window.innerWidth / 3) {
      prev();
    } else {
      next();
    }
  });

  // Touch support
  let touchStartX = 0;
  document.addEventListener('touchstart', function(e) {
    touchStartX = e.touches[0].clientX;
  });
  document.addEventListener('touchend', function(e) {
    const diff = touchStartX - e.changedTouches[0].clientX;
    if (Math.abs(diff) > 50) {
      diff > 0 ? next() : prev();
    }
  });

  // Hash navigation
  const hash = parseInt(window.location.hash.replace('#', ''));
  if (hash && hash > 0 && hash <= slides.length) {
    showSlide(hash - 1);
  } else {
    showSlide(0);
  }
})();
