{{> header}}
<style>
  .courses-section { padding: var(--section-padding) 0; min-height: 70vh; }
  .courses-header { margin-bottom: 2.5rem; }
  .courses-header h1 { background: var(--gradient-primary); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; margin-bottom: 0.5rem; }
  .courses-header p { color: var(--color-text-muted); margin: 0; }

  .courses-filters { display: flex; gap: 1rem; margin-bottom: 2rem; flex-wrap: wrap; align-items: center; }
  .filter-btn { padding: 0.6rem 1.25rem; border-radius: 25px; border: 1px solid var(--color-border); background: var(--color-surface); color: var(--color-text); cursor: pointer; font-weight: 500; transition: all 0.2s ease; }
  .filter-btn:hover { border-color: var(--color-primary); color: var(--color-primary); }
  .filter-btn.active { background: var(--color-primary); border-color: var(--color-primary); color: white; }
  .search-box { flex: 1; min-width: 200px; max-width: 300px; }
  .search-input { width: 100%; padding: 0.6rem 1rem; padding-left: 2.5rem; border-radius: 25px; border: 1px solid var(--color-border); background: var(--color-surface); color: var(--color-text); }
  .search-input:focus { outline: none; border-color: var(--color-primary); }
  .search-wrapper { position: relative; }
  .search-icon { position: absolute; left: 1rem; top: 50%; transform: translateY(-50%); color: var(--color-text-muted); }

  .courses-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1.5rem; }
  .course-card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--border-radius); overflow: hidden; transition: all 0.3s ease; display: flex; flex-direction: column; }
  .course-card:hover { transform: translateY(-6px); box-shadow: 0 15px 40px rgba(99, 102, 241, 0.15); border-color: var(--color-primary); }
  .course-image-wrapper { position: relative; aspect-ratio: 16/9; overflow: hidden; background: var(--color-background); }
  .course-image { width: 100%; height: 100%; object-fit: cover; transition: transform 0.5s ease; }
  .course-card:hover .course-image { transform: scale(1.05); }
  .course-price-badge { position: absolute; bottom: 0.75rem; right: 0.75rem; padding: 0.4rem 0.875rem; background: rgba(0, 0, 0, 0.75); backdrop-filter: blur(10px); border-radius: 20px; font-weight: 700; font-size: 0.95rem; }
  .course-price-badge.paid { color: var(--color-primary); }
  .course-price-badge.free { background: rgba(16, 185, 129, 0.9); color: white; }
  .course-level { position: absolute; top: 0.75rem; left: 0.75rem; padding: 0.3rem 0.7rem; background: rgba(0, 0, 0, 0.75); backdrop-filter: blur(10px); border-radius: 15px; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; color: var(--color-text); }
  .course-content { padding: 1.25rem; flex: 1; display: flex; flex-direction: column; }
  .course-meta { display: flex; gap: 1rem; margin-bottom: 0.75rem; font-size: 0.8rem; color: var(--color-text-muted); }
  .course-meta span { display: flex; align-items: center; gap: 0.25rem; }
  .course-title { font-size: 1.15rem; margin-bottom: 0.75rem; line-height: 1.4; }
  .course-title a { color: var(--color-heading); transition: color 0.2s; }
  .course-title a:hover { color: var(--color-primary); }
  .course-description { font-size: 0.9rem; color: var(--color-text-muted); margin-bottom: 1rem; line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; flex: 1; }
  .course-footer { margin-top: auto; }
  .course-instructor { display: flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; color: var(--color-text-muted); }
  .instructor-avatar { width: 24px; height: 24px; border-radius: 50%; background: var(--color-primary); display: flex; align-items: center; justify-content: center; font-size: 0.7rem; color: white; font-weight: 600; }

  .enroll-btn { width: 100%; justify-content: center; margin-top: 1rem; }
  .empty-state { text-align: center; padding: 4rem 2rem; grid-column: 1 / -1; }
  .empty-state .icon { font-size: 4rem; margin-bottom: 1rem; }

  @media (max-width: 768px) {
    .courses-filters { flex-direction: column; align-items: stretch; gap: 0.75rem; }
    .search-box { max-width: none; min-width: auto; }
    .courses-grid { grid-template-columns: 1fr; gap: 1rem; }
    .courses-header h1 { font-size: 1.75rem; }
    .filter-btn { flex: 1; text-align: center; justify-content: center; min-height: 44px; }
    .courses-filters > .filter-btn { display: inline-flex; }
  }

  @media (max-width: 480px) {
    .courses-section { padding: 2rem 0; }
    .courses-header { margin-bottom: 1.5rem; }
    .courses-header h1 { font-size: 1.5rem; }
    .courses-header p { font-size: 0.9rem; }
    .filter-btn { padding: 0.5rem 1rem; font-size: 0.85rem; }
    .search-input { padding: 0.75rem 1rem 0.75rem 2.5rem; font-size: 16px; }
    .course-card { border-radius: 12px; }
    .course-content { padding: 1rem; }
    .course-title { font-size: 1rem; }
    .course-description { font-size: 0.85rem; -webkit-line-clamp: 2; }
    .course-meta { font-size: 0.75rem; gap: 0.75rem; }
    .enroll-btn { padding: 0.75rem 1rem; min-height: 44px; }
    .empty-state { padding: 3rem 1.5rem; }
    .empty-state .icon { font-size: 3rem; }
  }
</style>

<section class="courses-section">
  <div class="container">
    <div class="courses-header">
      <h1>📚 Explore Courses</h1>
      <p>Learn new skills with our expert-led courses</p>
    </div>

    <div class="courses-filters">
      <button class="filter-btn active" data-filter="all">All Courses</button>
      <button class="filter-btn" data-filter="free">Free</button>
      <button class="filter-btn" data-filter="paid">Paid</button>
      <div class="search-box">
        <div class="search-wrapper">
          <span class="search-icon">🔍</span>
          <input type="text" class="search-input" id="courseSearch" placeholder="Search courses...">
        </div>
      </div>
    </div>

    <div class="courses-grid" id="coursesGrid">
      {{#each courses}}
      <article class="course-card" data-price-type="{{priceType}}" data-title="{{title}}">
        <div class="course-image-wrapper">
          {{#if featuredImage}}
          <img src="{{featuredImage}}" alt="{{title}}" class="course-image" onerror="this.src='/api/lms/courses/placeholder-image'">
          {{else if thumbnail}}
          <img src="{{thumbnail}}" alt="{{title}}" class="course-image" onerror="this.src='/api/lms/courses/placeholder-image'">
          {{else}}
          <img src="/api/lms/courses/placeholder-image" alt="{{title}}" class="course-image">
          {{/if}}
          {{#if level}}<span class="course-level">{{level}}</span>{{/if}}
          <span class="course-price-badge {{#if (eq priceType 'FREE')}}free{{else}}paid{{/if}}">
            {{#if (eq priceType 'FREE')}}Free{{else}}${{price}}{{/if}}
          </span>
        </div>
        <div class="course-content">
          <div class="course-meta">
            <span>📖 {{_count.lessons}} lessons</span>
            {{#if duration}}<span>⏱️ {{duration}}</span>{{/if}}
          </div>
          <h3 class="course-title"><a href="/courses/{{slug}}">{{title}}</a></h3>
          <p class="course-description">{{shortDescription}}</p>
          <div class="course-footer">
            {{#if instructor}}
            <div class="course-instructor">
              <span class="instructor-avatar">{{substring instructor.name 0 1}}</span>
              <span>{{instructor.name}}</span>
            </div>
            {{/if}}
            <a href="/courses/{{slug}}" class="btn btn-primary btn-sm enroll-btn">
              {{#if (eq priceType 'FREE')}}Start Learning{{else}}Enroll Now{{/if}}
            </a>
          </div>
        </div>
      </article>
      {{else}}
      <div class="empty-state">
        <div class="icon">📚</div>
        <h2>No Courses Yet</h2>
        <p style="color: var(--color-text-muted);">Check back soon for new courses!</p>
      </div>
      {{/each}}
    </div>
  </div>
</section>

<script>
(function() {
  // Filter functionality
  document.querySelectorAll('.filter-btn').forEach(function(btn) {
    btn.addEventListener('click', function() {
      document.querySelectorAll('.filter-btn').forEach(function(b) { b.classList.remove('active'); });
      this.classList.add('active');
      var filter = this.dataset.filter;

      document.querySelectorAll('.course-card').forEach(function(card) {
        var priceType = card.dataset.priceType?.toUpperCase();
        if (filter === 'all') {
          card.style.display = '';
        } else if (filter === 'free' && priceType === 'FREE') {
          card.style.display = '';
        } else if (filter === 'paid' && priceType !== 'FREE') {
          card.style.display = '';
        } else {
          card.style.display = 'none';
        }
      });
    });
  });

  // Search functionality
  var searchInput = document.getElementById('courseSearch');
  if (searchInput) {
    searchInput.addEventListener('input', function() {
      var query = this.value.toLowerCase();
      document.querySelectorAll('.course-card').forEach(function(card) {
        var title = card.dataset.title?.toLowerCase() || '';
        card.style.display = title.includes(query) ? '' : 'none';
      });
    });
  }
})();
</script>
{{> footer}}
