/* ══════════════════════════════════════════════════════
   PAGE TRANSITIONS — smooth fade + slide on navigation
══════════════════════════════════════════════════════ */
(function(){
'use strict';

// Create overlay element
var overlay = document.createElement('div');
overlay.id = 'page-trans';
overlay.setAttribute('aria-hidden', 'true');
document.documentElement.appendChild(overlay);

var style = document.createElement('style');
style.textContent = [
  '#page-trans{',
    'position:fixed;inset:0;z-index:99998;',
    'background:linear-gradient(135deg,#0F1C2E 0%,#1B2D45 100%);',
    'pointer-events:none;',
    'opacity:1;',
    'transition:opacity .45s cubic-bezier(.4,0,.2,1);',
  '}',
  '#page-trans.fade-out{opacity:0;}',
].join('');
document.head.appendChild(style);

// Fade out on load
function fadeOut(){
  requestAnimationFrame(function(){
    requestAnimationFrame(function(){
      overlay.classList.add('fade-out');
    });
  });
}

if(document.readyState === 'loading'){
  document.addEventListener('DOMContentLoaded', fadeOut);
} else {
  fadeOut();
}

// Intercept nav clicks — fade in then navigate
document.addEventListener('click', function(e){
  var a = e.target.closest('a[href]');
  if(!a) return;
  var href = a.getAttribute('href');
  if(!href) return;

  // Skip non-page links
  if(a.target === '_blank') return;
  if(href.charAt(0) === '#') return;
  if(/^(tel:|mailto:|https?:\/\/wa\.me)/.test(href)) return;
  if(a.hasAttribute('download')) return;
  if(href.startsWith('http') && href.indexOf(location.hostname) === -1) return;
  if(!/\.php/.test(href) && href !== '/') return;

  e.preventDefault();

  // Fade overlay in
  overlay.style.transition = 'opacity .35s cubic-bezier(.4,0,.2,1)';
  overlay.classList.remove('fade-out');
  overlay.style.opacity = '1';

  setTimeout(function(){
    window.location.href = href;
  }, 360);
});

// Handle browser back/forward
window.addEventListener('pageshow', function(e){
  if(e.persisted){
    overlay.style.transition = 'none';
    overlay.style.opacity = '1';
    overlay.classList.remove('fade-out');
    requestAnimationFrame(function(){
      requestAnimationFrame(function(){
        overlay.style.transition = 'opacity .45s cubic-bezier(.4,0,.2,1)';
        overlay.classList.add('fade-out');
      });
    });
  }
});

})();
