<script src="https://unpkg.com/lucide@latest"></script>

<?php
$tours = $tours ?? [];
// Check for success reference from your BookingController
$successRef = service('request')->getGet('ref');
?>

<?php if ($successRef): ?>
<div id="successOverlay" class="fixed inset-0 z-[200] flex items-center justify-center bg-navy/60 backdrop-blur-md animate-[fadeIn_0.5s_ease-out]">
    <div class="bg-surface p-12 rounded-[3rem] text-center shadow-2xl max-w-sm mx-4 transform animate-[scaleIn_0.4s_ease-out]">
        <div class="w-24 h-24 bg-secondary/20 text-secondary rounded-full flex items-center justify-center mx-auto mb-6">
            <i data-lucide="check-circle-2" class="w-12 h-12"></i>
        </div>
        <h3 class="text-2xl font-black text-navy mb-2">Expedition Secured!</h3>
        <p class="text-muted text-sm mb-6">Ref: <span class="font-bold text-navy"><?= esc($successRef) ?></span><br>Check your email for the trek invoice.</p>
        <button onclick="document.getElementById('successOverlay').remove()" class="w-full py-4 bg-navy text-white rounded-2xl font-black uppercase tracking-widest text-xs">Return to Trails</button>
    </div>
</div>
<?php endif; ?>

<section id="featured-packages" class="py-24 bg-surface font-sans overflow-hidden">
  <div class="container mx-auto px-6">
    
    <div class="flex flex-col md:flex-row md:items-end justify-between mb-20 gap-8">
      <div class="max-w-2xl">
        <div class="inline-flex items-center gap-3 px-4 py-2 bg-primary/10 rounded-full mb-6">
          <span class="flex h-2 w-2 rounded-full bg-secondary animate-pulse"></span>
          <span class="text-secondary font-bold uppercase tracking-widest text-[10px]">2025/2026 Season Open</span>
        </div>
        <h2 class="text-4xl md:text-6xl font-black text-navy tracking-tighter leading-[0.9]">
          Curated <span class="text-secondary">Mount Kenya</span> Expeditions
        </h2>
        <p class="mt-6 text-lg text-muted leading-relaxed">
          Professional guiding, eco-conscious trekking, and life-changing summits. Choose a package tailored to your experience level.
        </p>
      </div>
      <div class="hidden md:block">
         <a href="<?= base_url('tours') ?>" class="text-muted hover:text-secondary font-bold transition-colors uppercase tracking-widest text-xs">View All Routes &rarr;</a>
      </div>
    </div>

    <?php if (empty($tours)): ?>
      <div class="py-20 text-center border-2 border-dashed border-border rounded-[3rem]">
        <i data-lucide="mountain-snow" class="w-12 h-12 text-border mx-auto mb-4"></i>
        <p class="text-muted italic font-medium">Exploring new trails. Packages arriving soon.</p>
      </div>
    <?php else: ?>
      <div class="grid gap-8 md:grid-cols-2 lg:grid-cols-3">
        <?php foreach ($tours as $tour): ?>
          <?php
            $images = !empty($tour['images']) ? json_decode($tour['images'], true) : [];
            $firstImage = !empty($images) ? $images[0] : null;
            
            $diff = match(strtolower($tour['difficulty'] ?? 'intermediate')) {
              'beginner' => ['bg' => 'bg-secondary/20', 'text' => 'text-secondary', 'label' => 'Standard Trek'],
              'intermediate' => ['bg' => 'bg-accent/20', 'text' => 'text-accent', 'label' => 'Challenging'],
              'advanced' => ['bg' => 'bg-warning/20', 'text' => 'text-warning', 'label' => 'Elite Ascent'],
              default => ['bg' => 'bg-border/30', 'text' => 'text-muted', 'label' => 'Standard']
            };
          ?>
          
          <article class="group relative bg-surface rounded-[2.5rem] p-4 transition-all duration-500 hover:bg-white hover:shadow-2xl flex flex-col h-full border border-transparent hover:border-border">
            
            <div class="relative h-80 w-full rounded-[2rem] overflow-hidden mb-6">
              <?php if ($firstImage): ?>
                <img src="<?= base_url('assets/uploads/tours/' . $firstImage) ?>" alt="<?= esc($tour['title']) ?> - Mount Kenya" class="w-full h-full object-cover transition-transform duration-1000 group-hover:scale-110">
              <?php else: ?>
                <div class="w-full h-full bg-border/30 flex items-center justify-center"><i data-lucide="image" class="text-muted"></i></div>
              <?php endif; ?>

              <div class="absolute top-4 left-4">
                <span class="<?= $diff['bg'] ?> <?= $diff['text'] ?> text-[10px] font-black uppercase tracking-widest px-4 py-2 rounded-xl backdrop-blur-md bg-opacity-90">
                  <?= $diff['label'] ?>
                </span>
              </div>
            </div>

            <div class="px-4 pb-4 flex flex-col flex-grow">
              <div class="mb-4">
                <span class="text-secondary font-bold text-[10px] uppercase tracking-widest block mb-1">
                  <?= esc($tour['route'] ?? 'Mt. Kenya Trail') ?>
                </span>
                <h3 class="text-2xl font-black text-navy tracking-tight leading-7">
                  <?= esc($tour['title']) ?>
                </h3>
              </div>

              <div class="space-y-3 mb-8">
                <div class="flex items-center gap-3 text-muted">
                  <div class="w-8 h-8 rounded-lg bg-surface flex items-center justify-center shadow-sm">
                    <i data-lucide="clock" class="w-4 h-4 text-secondary"></i>
                  </div>
                  <span class="text-sm font-bold text-navy"><?= esc($tour['duration_days']) ?> Days Expedition</span>
                </div>
                <div class="flex items-center gap-3 text-muted">
                  <div class="w-8 h-8 rounded-lg bg-surface flex items-center justify-center shadow-sm">
                    <i data-lucide="shield-check" class="w-4 h-4 text-secondary"></i>
                  </div>
                  <span class="text-sm font-bold text-navy">Certified Professional Guides</span>
                </div>
              </div>

              <div class="mt-auto grid grid-cols-2 gap-3">
                <a href="<?= base_url('tours/' . $tour['slug']) ?>" class="flex items-center justify-center py-4 rounded-2xl border-2 bg-accent border-accent text-white text-[10px] font-black uppercase tracking-widest hover:bg-surface hover:text-accent transition-all">
                  Full Details
                </a>
                <a href="<?= base_url('booking?tour=' . $tour['slug']) ?>" class="flex items-center justify-center py-4 rounded-2xl bg-secondary text-white text-[10px] font-black uppercase tracking-widest hover:bg-surface hover:text-secondary border border-secondary shadow-lg shadow-secondary/30 transition-all">
                  Book Now
                </a>
              </div>
            </div>
          </article>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>

<style>
  @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
  @keyframes scaleIn { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
</style>

<script>
  lucide.createIcons();
</script>