<!-- Newsletter Section -->
<section id="newsletter" class="py-16 bg-surface" aria-label="Subscribe to our expedition updates">
  <div class="container mx-auto px-6">
    <div class="relative bg-accent/10 rounded-3xl overflow-hidden shadow-xl border border-accent/20">
      
      <!-- Subtle background pattern -->
      <div class="absolute inset-0 opacity-[0.02] pointer-events-none uppercase text-[70px] font-black leading-none text-navy select-none">
        Expedition Intelligence
      </div>

      <div class="relative z-10 p-6 sm:p-8 md:p-10 lg:p-12">
        <div class="flex flex-col lg:flex-row items-center justify-between gap-8">
          
          <!-- Content Column -->
          <div class="flex flex-col gap-4 max-w-lg">
            <div class="flex items-center gap-2">
              <span class="w-2 h-2 rounded-full bg-secondary animate-pulse" aria-hidden="true"></span>
              <span class="text-secondary font-mono text-[10px] font-black uppercase tracking-[0.3em]">Live Dispatch</span>
            </div>
            <h2 class="text-2xl md:text-3xl font-black text-navy tracking-tighter uppercase leading-none">
              Peak <span class="italic text-secondary font-light">Intelligence</span>
            </h2>
            <p class="text-muted text-sm font-medium uppercase tracking-wide">
              Gear science and summit conditions. Delivered weekly.
            </p>

            <!-- Hidden schema for SEO -->
            <div hidden>
              <div itemscope itemtype="https://schema.org/NewsArticle">
                <meta itemprop="headline" content="Weekly Mount Kenya Expedition Updates">
                <meta itemprop="description" content="Professional insights on routes, weather, gear, and summit conditions for Mount Kenya climbers.">
              </div>
            </div>
          </div>

          <!-- Form Column -->
          <div class="w-full lg:max-w-md">
            <form id="newsletterForm" method="post" action="<?= base_url('lead/newsletter') ?>" class="flex flex-col gap-3">
              <label for="newsletterEmail" class="sr-only">Enter your email to subscribe to Mount Kenya Treks updates</label>
              <div class="flex flex-col sm:flex-row gap-3">
                <input 
                  type="email" 
                  id="newsletterEmail"
                  name="email" 
                  placeholder="YOUR@EMAIL.COM" 
                  required
                  aria-label="Email address for newsletter subscription"
                  class="flex-1 bg-white/80 backdrop-blur-sm py-4 px-5 text-navy placeholder:text-muted/70 rounded-2xl outline-none text-[11px] font-mono tracking-[0.2em] uppercase border border-border focus:border-secondary focus:ring-2 focus:ring-secondary/20 transition-colors"
                >
                <button 
                  type="submit"
                  class="w-2/5 sm:w-auto py-4 px-6 bg-secondary text-white rounded-2xl font-black uppercase tracking-[0.2em] text-[10px] shadow-lg hover:bg-white hover:text-secondary border border-secondary transition-all active:scale-[0.98]"
                >
                  Join
                </button>
              </div>
              <div 
                id="newsletterMessage" 
                class="min-h-[16px] text-[9px] font-black uppercase tracking-[0.2em] text-muted"
                aria-live="polite"
                role="status"
              ></div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

<script>
  // Initialize Lucide icons if available
  if (typeof lucide !== 'undefined') {
    lucide.createIcons();
  }

  // Handle newsletter form submission
  document.getElementById('newsletterForm')?.addEventListener('submit', async (e) => {
    e.preventDefault();
    const form = e.target;
    const emailInput = form.querySelector('input[name="email"]');
    const btn = form.querySelector('button[type="submit"]');
    const messageEl = document.getElementById('newsletterMessage');

    btn.disabled = true;
    btn.style.opacity = '0.6';
    messageEl.textContent = 'SYNCING...';
    messageEl.className = 'min-h-[16px] text-[9px] font-black uppercase tracking-[0.2em] text-muted animate-pulse';

    try {
      const res = await fetch(form.action, {
        method: 'POST',
        headers: { 
          'Content-Type': 'application/json',
          'X-CSRF-TOKEN': '<?= csrf_hash() ?>' 
        },
        body: JSON.stringify({ email: emailInput.value })
      });

      const data = await res.json();
      
      if (data.success) {
        messageEl.className = 'min-h-[16px] text-[9px] font-black uppercase tracking-[0.2em] text-secondary';
        messageEl.textContent = 'VERIFIED. WELCOME.';
        form.reset();
      } else {
        messageEl.className = 'min-h-[16px] text-[9px] font-black uppercase tracking-[0.2em] text-warning';
        messageEl.textContent = 'INVALID INPUT.';
      }
    } catch (err) {
      messageEl.className = 'min-h-[16px] text-[9px] font-black uppercase tracking-[0.2em] text-warning';
      messageEl.textContent = 'NETWORK ERROR.';
    } finally {
      btn.disabled = false;
      btn.style.opacity = '1';
      setTimeout(() => { messageEl.style.opacity = '0'; }, 5000);
    }
  });
</script>