// ===== SHARED JS — main.js =====

// Age gate
const GATE_KEY = 'escort_age_ok';
export function initAgeGate() {
  if (sessionStorage.getItem(GATE_KEY)) return;
  const gate = document.getElementById('age-gate');
  if (!gate) return;
  gate.style.display = 'flex';
  document.getElementById('age-yes')?.addEventListener('click', () => {
    sessionStorage.setItem(GATE_KEY, '1');
    gate.style.opacity = '0';
    gate.style.transition = 'opacity .3s';
    setTimeout(() => gate.remove(), 300);
  });
  document.getElementById('age-no')?.addEventListener('click', () => {
    window.location.href = 'https://www.google.com';
  });
}

// Toast
export function showToast(msg, type = 'success') {
  const icons = { success: '✅', error: '❌', info: 'ℹ️', warn: '⚠️' };
  const t = document.createElement('div');
  t.className = 'toast';
  t.innerHTML = `<span>${icons[type] || 'ℹ️'}</span><span>${msg}</span>`;
  document.body.appendChild(t);
  setTimeout(() => {
    t.style.opacity = '0';
    t.style.transition = 'opacity .3s';
    setTimeout(() => t.remove(), 300);
  }, 3200);
}

// Back to top button
export function initBackToTop() {
  const btn = document.querySelector('.back-to-top');
  if (!btn) return;
  window.addEventListener('scroll', () => {
    btn.classList.toggle('visible', window.scrollY > 400);
  });
  btn.addEventListener('click', e => {
    e.preventDefault();
    window.scrollTo({ top: 0, behavior: 'smooth' });
  });
}

// Mobile hamburger menu
export function initMobileMenu() {
  const hamburger = document.getElementById('hamburger');
  const mobileMenu = document.getElementById('mobile-menu');
  if (!hamburger || !mobileMenu) return;
  hamburger.addEventListener('click', () => {
    mobileMenu.classList.toggle('open');
    hamburger.textContent = mobileMenu.classList.contains('open') ? '✕' : '☰';
  });
}

// Favorite toggle on listing cards
export function initFavToggles() {
  document.querySelectorAll('.listing-act.fav').forEach(btn => {
    btn.addEventListener('click', () => {
      btn.classList.toggle('active');
      btn.textContent = btn.classList.contains('active') ? '❤️' : '🤍';
      showToast(btn.classList.contains('active') ? 'Added to favorites!' : 'Removed from favorites');
    });
  });
}

// Run all shared inits
export function initShared() {
  initAgeGate();
  initBackToTop();
  initMobileMenu();
  initFavToggles();
}
