<!-- Blog Section -->
<section id="blog" class="py-24 bg-light font-sans" aria-labelledby="blog-heading">
  <div class="container mx-auto px-6">
    <!-- Header -->
    <div class="flex flex-col md:flex-row md:items-end justify-between mb-16 gap-6">
      <div class="max-w-xl">
        <div class="flex items-center gap-2 mb-4">
          <span class="w-8 h-1 bg-secondary rounded-full" aria-hidden="true"></span>
          <span class="text-secondary font-black uppercase tracking-[0.3em] text-[10px]">The Explorer's Journal</span>
        </div>
        <h2 id="blog-heading" class="text-4xl md:text-5xl font-black text-navy tracking-tighter leading-none">
          Insights from the <span class="text-secondary italic">High Peaks</span>
        </h2>
      </div>
      <a href="<?= base_url('blog') ?>" 
         class="group inline-flex items-center gap-2 text-navy font-black uppercase tracking-widest text-xs border-b-2 border-secondary pb-1 hover:text-secondary transition-colors"
         aria-label="Explore all blog stories">
        Explore All Stories
        <i data-lucide="arrow-up-right" class="w-4 h-4 group-hover:translate-x-1 group-hover:-translate-y-1 transition-transform"></i>
      </a>
    </div>

    <?php if (empty($blogPosts)): ?>
      <!-- Empty State -->
      <div class="bg-surface rounded-3xl p-16 text-center border border-border shadow-sm">
        <div class="w-16 h-16 bg-light rounded-full flex items-center justify-center mx-auto mb-6">
          <i data-lucide="pen-tool" class="w-7 h-7 text-muted"></i>
        </div>
        <p class="text-muted text-lg font-medium italic">New summit stories are currently being written...</p>
      </div>

    <?php else: ?>
      <!-- Blog Grid -->
      <div class="grid gap-10 md:grid-cols-2 lg:grid-cols-3">
        <?php foreach ($blogPosts as $post): ?>
          <?php
            $hasImage = !empty($post['image']);
            $imageUrl = $hasImage ? base_url('assets/uploads/blog/' . $post['image']) : '';
            $title = esc($post['title']);
            $excerpt = esc(substr(strip_tags($post['excerpt'] ?? ''), 0, 160));
            $slug = $post['slug'] ?? '#';
            $date = date('M d, Y', strtotime($post['created_at'] ?? 'now'));
          ?>

          <article class="group flex flex-col bg-surface rounded-3xl overflow-hidden border border-border transition-all duration-300 hover:shadow-xl hover:-translate-y-1 h-full">
            <!-- Image -->
            <div class="relative h-64 overflow-hidden">
              <?php if ($hasImage): ?>
                <img 
                  src="<?= $imageUrl ?>" 
                  alt="<?= $title ?> — Mount Kenya Treks Blog"
                  loading="lazy"
                  class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105"
                >
              <?php else: ?>
                <div class="w-full h-full bg-light flex items-center justify-center">
                  <i data-lucide="mountain" class="w-12 h-12 text-muted"></i>
                </div>
              <?php endif; ?>

              <div class="absolute top-5 left-5">
                <span class="bg-white/80 backdrop-blur-sm px-3 py-1.5 rounded-lg text-[10px] font-black uppercase tracking-widest text-navy">
                  <?= $date ?>
                </span>
              </div>
            </div>

            <!-- Content -->
            <div class="p-6 flex flex-col flex-grow">
              <h3 class="text-xl font-black text-navy tracking-tight leading-tight mb-3 group-hover:text-secondary transition-colors">
                <?= $title ?>
              </h3>
              
              <p class="text-muted text-sm leading-relaxed mb-6 flex-grow">
                <?= $excerpt ?>...
              </p>

              <a href="<?= base_url('blog/' . $slug) ?>" 
                 class="inline-flex items-center gap-2 text-secondary font-black uppercase tracking-widest text-[10px] hover:gap-3 transition-all"
                 aria-label="Read full article: <?= $title ?>">
                Read Full Article
                <i data-lucide="chevron-right" class="w-3.5 h-3.5"></i>
              </a>
            </div>
          </article>
        <?php endforeach; ?>
      </div>

      <!-- ✅ Schema.org for SEO -->
      <script type="application/ld+json">
      {
        "@context": "https://schema.org",
        "@type": "Blog",
        "name": "Mount Kenya Treks Blog",
        "description": "Insights on Mount Kenya expeditions, trekking tips, gear advice, and summit stories.",
        "url": "<?= base_url('blog') ?>",
        "blogPost": [
          <?php foreach (array_slice($blogPosts, 0, 3) as $i => $post): ?>
          {
            "@type": "BlogPosting",
            "headline": "<?= esc($post['title']) ?>",
            "description": "<?= esc(strip_tags($post['excerpt'] ?? '')) ?>",
            "url": "<?= base_url('blog/' . ($post['slug'] ?? '')) ?>",
            "datePublished": "<?= date('c', strtotime($post['created_at'] ?? 'now')) ?>",
            "image": "<?= !empty($post['image']) ? base_url('assets/uploads/blog/' . $post['image']) : base_url('assets/images/logo.svg') ?>",
            "author": {
              "@type": "Organization",
              "name": "Mount Kenya Treks & Safaris"
            }
          }<?= $i < 2 ? ',' : '' ?>
          <?php endforeach; ?>
        ]
      }
      </script>
    <?php endif; ?>
  </div>
</section>

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