<?php if (empty($materi)): ?>
    <div class="col-12 text-center text-muted py-5">
        <i class="fa fa-folder-open fa-2x mb-2"></i>
        <div>Tidak ada materi ditemukan</div>
    </div>
<?php endif; ?>

<?php foreach ($materi as $m): ?>
    <div class="col-md-4 mb-4">
        <div class="card card-module h-100 position-relative shadow-sm border-0">

            <!-- Badge tipe -->
            <span class="badge badge-type 
            <?= $m['tipe'] === 'PDF' ? 'bg-danger' : 'bg-success' ?>">
                <?= esc($m['tipe']) ?>
            </span>

            <div class="card-body d-flex flex-column">

                <div class="module-title mb-1">
                    <?= esc($m['judul']) ?>
                </div>

                <div class="module-meta text-muted mb-2">
                    <?= esc($m['nama_mapel']) ?> · Kelas <?= esc($m['kelas']) ?>
                </div>

                <div class="module-desc mb-3">
                    <?= esc(word_limiter($m['deskripsi'], 18)) ?>
                </div>

                <div class="mt-auto d-flex justify-content-between align-items-center">
                    <small class="text-muted">
                        <i class="far fa-eye"></i> <?= (int) $m['view_count'] ?>
                    </small>

                    <?php if ($m['tipe'] === 'VIDEO'): ?>
                        <a href="<?= site_url('elearning/materi/view/' . $m['id']) ?>"
                            class="btn btn-sm btn-outline-primary btn-action">
                            <i class="fas fa-play me-1"></i> Putar
                        </a>
                    <?php else: ?>
                        <a href="<?= base_url($m['file_path']) ?>" class="btn btn-sm btn-outline-secondary btn-action">
                            <i class="fas fa-download me-1"></i> Unduh
                        </a>
                    <?php endif ?>
                </div>

            </div>
        </div>
    </div>
<?php endforeach ?>