<?php
/**
 * Dashboard Tim Monev — SMART-KKN UMUS 2026
 */
require_once __DIR__ . '/../kkn-system/includes/config.php';
require_once __DIR__ . '/../kkn-system/includes/db.php';
require_once __DIR__ . '/../kkn-system/includes/auth.php';

if (session_status() === PHP_SESSION_NONE) session_start();

// Handle logout
if (isset($_GET['logout'])) {
    session_destroy();
    header('Location: ' . BASE_URL . '/monev-dashboard/');
    exit;
}

$page = $_GET['page'] ?? 'dashboard';

// ── Handle Login ──────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login_monev'])) {
    $nidn = trim($_POST['nidn'] ?? '');
    $pass = $_POST['password'] ?? '';
    $row = fetch(
        "SELECT u.*, mo.id as monev_id, mo.nama as monev_nama, mo.nidn
         FROM kkn_users u
         JOIN kkn_monev mo ON mo.user_id = u.id
         WHERE (mo.nidn = ? OR u.username = ?) AND u.status = 'active' AND u.role = 'monev'",
        [$nidn, $nidn]
    );
    if ($row && password_verify($pass, $row['password'])) {
        $_SESSION['kkn_user_id'] = $row['id'];
        $_SESSION['kkn_role']    = 'monev';
        $_SESSION['kkn_user']    = $row;
        header('Location: ' . BASE_URL . '/monev-dashboard/'); exit;
    }
    $login_error = 'NIDN atau password salah.';
}

// ── Require Login ─────────────────────────────────────────────
if (empty($_SESSION['kkn_role']) || $_SESSION['kkn_role'] !== 'monev') {
    $err = $login_error ?? '';
    ?><!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Login Tim Monev — SMART-KKN UMUS 2026</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<style>
*{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'Inter',sans-serif;min-height:100vh;background:linear-gradient(135deg,#0D2E5C 0%,#1A56A4 100%);display:flex;align-items:center;justify-content:center;padding:1rem;}
.card{background:#fff;border-radius:16px;padding:2.5rem;width:100%;max-width:420px;box-shadow:0 20px 60px rgba(0,0,0,.25);}
.logo{text-align:center;margin-bottom:1.75rem;}
.logo img{width:120px;height:120px;object-fit:contain;border-radius:12px;}
.logo h1{font-family:'Playfair Display',serif;font-size:.9rem;color:#0D2E5C;margin-top:.5rem;}
.logo p{font-size:.75rem;color:#8A96A8;margin-top:2px;}
.badge-monev{display:inline-block;background:#E8F0FB;color:#0D2E5C;padding:3px 12px;border-radius:20px;font-size:.72rem;font-weight:700;margin-top:.4rem;}
.fg{margin-bottom:1.1rem;}
.fg label{display:block;font-size:.8rem;font-weight:600;color:#2C3545;margin-bottom:.4rem;}
.fg input{width:100%;padding:11px 14px;border:1.5px solid #E2E8F0;border-radius:8px;font-size:.8rem;font-family:'Inter',sans-serif;outline:none;transition:.2s;}
.fg input:focus{border-color:#1A56A4;}
.btn{width:100%;padding:13px;background:#0D2E5C;color:#fff;border:none;border-radius:8px;font-size:.8rem;font-weight:700;cursor:pointer;font-family:'Inter',sans-serif;transition:.2s;}
.btn:hover{background:#1A56A4;}
.error{background:#FFF5F5;border:1px solid #FED7D7;color:#C53030;padding:10px 14px;border-radius:8px;font-size:.75rem;margin-bottom:1rem;}
.divider{border:none;border-top:1px solid #F0F4F8;margin:1.25rem 0;}
.back{text-align:center;font-size:.75rem;color:#8A96A8;}
.back a{color:#1A56A4;font-weight:600;text-decoration:none;}
</style>
</head>
<body>
<div class="card">
  <div class="logo">
    <img src="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png" alt="SMART-KKN" style="background:#fff;border-radius:8px;padding:2px;">
    <h1>SMART-KKN</h1>
    <p>Sistem Manajemen KKN UMUS 2026</p>
    <span class="badge-monev">Login Tim Monev</span>
  </div>
  <?php if($err): ?><div class="error">⚠ <?= htmlspecialchars($err) ?></div><?php endif; ?>
  <form method="POST">
    <div class="fg"><label>NIDN</label><input type="text" name="nidn" placeholder="Masukkan NIDN" required autofocus></div>
    <div class="fg"><label>Password</label><input type="password" name="password" placeholder="Masukkan password" required></div>
    <button type="submit" name="login_monev" class="btn">Masuk</button>
  </form>
  <hr class="divider">
  <div class="back"><a href="<?= BASE_URL ?>"><i>←</i> Kembali ke Website LPPM</a></div>
</div>
</body>
</html>
    <?php exit;
}

$monev    = $_SESSION['kkn_user'];
$monev_id = $monev['monev_id'];

// ── Handle Save Evaluasi Logbook ──────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_eval'])) {
    $logbook_id = (int)$_POST['logbook_id'];
    $status     = in_array($_POST['status_eval'],['valid','tidak_valid']) ? $_POST['status_eval'] : 'valid';
    $catatan    = strip_tags(trim($_POST['catatan_eval'] ?? ''));
    query("INSERT INTO kkn_monev_eval (monev_id, logbook_id, status_eval, catatan)
           VALUES (?,?,?,?)
           ON DUPLICATE KEY UPDATE status_eval=?, catatan=?, updated_at=NOW()",
        [$monev_id, $logbook_id, $status, $catatan, $status, $catatan]);
    header('Location: ' . BASE_URL . '/monev-dashboard/?page=logbook&mhs_id=' . (int)$_POST['mhs_id'] . '&saved=1');
    exit;
}

// ── Handle Save Evaluasi Luaran ───────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_luaran_eval'])) {
    $mhs_id   = (int)$_POST['mhs_id'];
    $jenis    = in_array($_POST['jenis_luaran'],['jurnal','media','laporan_kelompok']) ? $_POST['jenis_luaran'] : 'jurnal';
    $status   = in_array($_POST['status_luaran'],['valid','tidak_valid']) ? $_POST['status_luaran'] : 'valid';
    $catatan  = strip_tags(trim($_POST['catatan_luaran'] ?? ''));
    query("INSERT INTO kkn_monev_luaran (monev_id, mhs_id, jenis_luaran, status_eval, catatan)
           VALUES (?,?,?,?,?)
           ON DUPLICATE KEY UPDATE status_eval=?, catatan=?, updated_at=NOW()",
        [$monev_id, $mhs_id, $jenis, $status, $catatan, $status, $catatan]);
    header('Location: ' . BASE_URL . '/monev-dashboard/?page=logbook&mhs_id=' . $mhs_id . '&saved=1');
    exit;
}

// ── Load Data ─────────────────────────────────────────────────
$mhs_list = fetchAll(
    "SELECT m.*, u.status as akun_status,
            (SELECT COUNT(*) FROM kkn_logbook l WHERE l.mahasiswa_id = m.id AND l.bukti_link != '') as total_bukti,
            (SELECT COUNT(*) FROM kkn_monev_eval me
             JOIN kkn_logbook lb ON lb.id = me.logbook_id
             WHERE lb.mahasiswa_id = m.id AND me.monev_id = ? AND me.status_eval = 'tidak_valid') as total_tidak_valid
     FROM kkn_monev_mhs mm
     JOIN kkn_mahasiswa m ON m.nim = mm.nim
     JOIN kkn_users u ON u.id = m.user_id
     WHERE mm.monev_id = ?
     ORDER BY m.kecamatan, m.nama",
    [$monev_id, $monev_id]
);

$total      = count($mhs_list);
$by_kec     = [];
foreach ($mhs_list as $m) $by_kec[$m['kecamatan']][] = $m;

function av($n){$w=explode(' ',$n);$i=strtoupper(substr($w[0],0,1));if(isset($w[1]))$i.=strtoupper(substr($w[1],0,1));return $i;}
$colors=['#0D2E5C','#276749','#1A56A4','#7C3AED','#C8922A','#831843','#0f766e','#b45309','#0369a1'];

// ── Load data laporan kelompok ───────────────────────────────
$lapkel_list = [];
if ($page === 'lapkel') {
    $lapkel_list = fetchAll(
        "SELECT d.id as dpl_id, d.nama as dpl_nama, d.kecamatan, d.kabupaten,
                lk.judul as laporan_judul, lk.link_url as laporan_link, lk.uploaded_at as lk_updated,
                ev.nilai_lapkel, ev.catatan as eval_catatan
         FROM kkn_monev_lapkel ml
         JOIN kkn_dpl d ON d.kecamatan = ml.kecamatan
         LEFT JOIN kkn_luaran_kelompok lk ON lk.dpl_id = d.id AND lk.jenis = 'laporan'
         LEFT JOIN kkn_monev_eval_lapkel ev ON ev.dpl_id = d.id AND ev.monev_id = ?
         WHERE ml.monev_id = ?
         ORDER BY d.kecamatan",
        [$monev_id, $monev_id]
    );
}

// ── Handle Save Evaluasi Laporan Kelompok ─────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_lapkel'])) {
    $dpl_id  = (int)$_POST['dpl_id'];
    $nilai   = floatval($_POST['nilai_lapkel'] ?? 0);
    $nilai   = max(0, min(100, $nilai));
    $catatan = strip_tags(trim($_POST['catatan_lapkel'] ?? ''));
    query("INSERT INTO kkn_monev_eval_lapkel (monev_id, dpl_id, nilai_lapkel, catatan)
           VALUES (?,?,?,?)
           ON DUPLICATE KEY UPDATE nilai_lapkel=?, catatan=?, updated_at=NOW()",
        [$monev_id, $dpl_id, $nilai, $catatan, $nilai, $catatan]);
    header('Location: ' . BASE_URL . '/monev-dashboard/?page=lapkel&saved=1'); exit;
}

// ── Load detail mahasiswa (untuk halaman logbook) ─────────────
$sel_mhs = null;
$logbooks = [];
$luaran_ind = [];
$luaran_kel = null;
if ($page === 'logbook' && isset($_GET['mhs_id'])) {
    $mid = (int)$_GET['mhs_id'];
    // Pastikan mahasiswa ini milik monev ini
    $sel_mhs = fetch(
        "SELECT m.* FROM kkn_mahasiswa m
         JOIN kkn_monev_mhs mm ON mm.nim = m.nim
         WHERE m.id = ? AND mm.monev_id = ?", [$mid, $monev_id]
    );
    if ($sel_mhs) {
        $logbooks = fetchAll(
            "SELECT lb.*, me.status_eval, me.catatan as eval_catatan
             FROM kkn_logbook lb
             LEFT JOIN kkn_monev_eval me ON me.logbook_id = lb.id AND me.monev_id = ?
             WHERE lb.mahasiswa_id = ?
             ORDER BY lb.minggu, lb.kegiatan_no",
            [$monev_id, $mid]
        );
        $luaran_ind = fetchAll(
            "SELECT lu.*, me.status_eval, me.catatan as eval_catatan
             FROM kkn_luaran lu
             LEFT JOIN kkn_monev_luaran me ON me.mhs_id = lu.mahasiswa_id AND me.monev_id = ? AND me.jenis_luaran = lu.jenis
             WHERE lu.mahasiswa_id = ? AND lu.jenis IN ('jurnal','media')",
            [$monev_id, $mid]
        );
        // Luaran kelompok (laporan)
        $luaran_kel = fetch(
            "SELECT lk.*, me.status_eval, me.catatan as eval_catatan
             FROM kkn_luaran_kelompok lk
             LEFT JOIN kkn_monev_luaran me ON me.mhs_id = ? AND me.monev_id = ? AND me.jenis_luaran = 'laporan_kelompok'
             WHERE lk.dpl_id = (SELECT dpl_id FROM kkn_mahasiswa WHERE id = ?)",
            [$mid, $monev_id, $mid]
        );
        // Proker individu & kelompok
        $proker_ind = fetch("SELECT * FROM kkn_proker_individu WHERE mahasiswa_id=?", [$mid]);
        $proker_kel = fetch("SELECT * FROM kkn_proker_kelompok WHERE dpl_id=(SELECT dpl_id FROM kkn_mahasiswa WHERE id=?) LIMIT 1", [$mid]);
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Dashboard Tim Monev — SMART-KKN UMUS 2026</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<link rel="icon" type="image/png" href="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tabler/icons-webfont@2.47.0/tabler-icons.min.css">
<style>
*{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'Inter',sans-serif;background:#F5F7FA;min-height:100vh;color:#1C2A3A;}
.sidebar{position:fixed;left:0;top:0;bottom:0;width:230px;background:linear-gradient(180deg,#0A2247,#0D2E5C);color:#fff;display:flex;flex-direction:column;z-index:100;}
.sb-brand{padding:14px 14px 10px;border-bottom:1px solid rgba(255,255,255,.08);display:flex;align-items:center;gap:10px;}
.sb-brand img{width:42px;height:42px;border-radius:10px;object-fit:contain;}
.sb-brand-text .name{font-size:.78rem;font-weight:700;color:#fff;}
.sb-brand-text .sub{font-size:.62rem;color:rgba(255,255,255,.4);}
.sb-user{padding:12px 14px;border-bottom:1px solid rgba(255,255,255,.08);display:flex;align-items:center;gap:9px;}
.sb-av{width:36px;height:36px;border-radius:50%;background:#C8922A;display:flex;align-items:center;justify-content:center;font-size:.78rem;font-weight:700;color:#fff;flex-shrink:0;}
.sb-uname{font-size:.8rem;font-weight:600;color:#fff;line-height:1.3;}
.sb-urole{font-size:.65rem;color:rgba(255,255,255,.45);}
.sb-nav{flex:1;padding:8px;}
.nav-sec{font-size:.6rem;font-weight:700;letter-spacing:.12em;text-transform:uppercase;color:rgba(255,255,255,.25);padding:10px 8px 4px;}
.nav-a{display:flex;align-items:center;gap:8px;padding:8px 10px;color:rgba(255,255,255,.6);text-decoration:none;border-radius:8px;font-size:.75rem;font-weight:500;margin-bottom:1px;border-left:3px solid transparent;transition:.15s;}
.nav-a:hover{background:rgba(255,255,255,.07);color:rgba(255,255,255,.9);}
.nav-a.active{background:rgba(200,146,42,.15);color:#FFE566;border-left-color:#C8922A;}
.nav-a i{font-size:.82rem;width:16px;text-align:center;}
.sb-foot{padding:8px;border-top:1px solid rgba(255,255,255,.07);}
.sb-foot a{display:flex;align-items:center;gap:7px;padding:7px 10px;color:rgba(255,255,255,.4);text-decoration:none;border-radius:7px;font-size:.78rem;transition:.15s;}
.sb-foot a:hover{background:rgba(255,255,255,.07);color:rgba(255,255,255,.8);}
.main{margin-left:234px;min-height:100vh;}
.topbar{background:#fff;border-bottom:1px solid #E2E8F0;padding:.875rem 1.75rem;display:flex;justify-content:space-between;align-items:center;position:sticky;top:0;z-index:50;}
.topbar-title{font-family:'Playfair Display',serif;font-size:.9rem;color:#0D2E5C;}
.topbar-right{font-size:.8rem;color:#8A96A8;}
.content{padding:1.75rem;}
.stat-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:1rem;margin-bottom:1.75rem;}
.stat-card{background:#fff;border-radius:12px;padding:1.25rem 1.5rem;border:1px solid #E2E8F0;}
.stat-n{font-family:'Playfair Display',serif;font-size:1.6rem;font-weight:700;color:#0D2E5C;line-height:1;}
.stat-l{font-size:.75rem;color:#8A96A8;margin-top:.25rem;}
.stat-ic{font-size:1.1rem;margin-bottom:.5rem;color:#C8922A;}
.card{background:#fff;border-radius:12px;border:1px solid #E2E8F0;overflow:hidden;margin-bottom:1.25rem;}
.card-hd{padding:.875rem 1.25rem;border-bottom:1px solid #F0F4F8;display:flex;justify-content:space-between;align-items:center;}
.card-hd h3{font-size:.8rem;font-weight:700;color:#0D2E5C;display:flex;align-items:center;gap:6px;}
.kec-hd{padding:.375rem 1rem;background:#F8FAFF;border-bottom:1px solid #F0F4F8;font-size:.72rem;font-weight:700;color:#0D2E5C;letter-spacing:.06em;text-transform:uppercase;display:flex;align-items:center;gap:6px;}
table{width:100%;border-collapse:collapse;font-size:.78rem;}
table th{background:#F8FAFF;padding:7px 12px;text-align:left;font-size:.7rem;font-weight:700;color:#8A96A8;letter-spacing:.05em;text-transform:uppercase;}
table td{padding:7px 12px;border-top:1px solid #F5F7FA;vertical-align:middle;}
table tr:hover td{background:#FAFBFF;}
.av{width:32px;height:32px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:.72rem;font-weight:700;color:#fff;flex-shrink:0;}
.badge{display:inline-block;padding:2px 9px;border-radius:20px;font-size:.7rem;font-weight:600;}
.badge-valid{background:#C6F6D5;color:#276749;}
.badge-tidak{background:#FEE2E2;color:#991B1B;}
.badge-belum{background:#FEF9C3;color:#713F12;}
.btn{display:inline-flex;align-items:center;gap:.3rem;padding:5px 11px;border:none;border-radius:7px;font-size:.72rem;font-weight:600;cursor:pointer;font-family:'Inter',sans-serif;text-decoration:none;transition:.2s;}
.btn-navy{background:#0D2E5C;color:#fff;}.btn-navy:hover{background:#1A56A4;color:#fff;}
.btn-sm{padding:5px 10px;font-size:.72rem;}
.btn-ghost{background:transparent;color:#1A56A4;border:1px solid #dbeafe;}
.alert-ok{background:#F0FFF4;border:1px solid #C6F6D5;color:#276749;padding:10px 14px;border-radius:8px;font-size:.83rem;margin-bottom:1rem;display:flex;align-items:center;gap:6px;}

/* Logbook view */
.minggu-tab{display:flex;gap:6px;flex-wrap:wrap;margin-bottom:1.25rem;}
.m-tab{padding:5px 14px;border-radius:20px;border:1px solid #E2E8F0;font-size:.78rem;font-weight:600;cursor:pointer;background:#fff;color:#64748b;transition:.15s;}
.m-tab.active,.m-tab:hover{background:#0D2E5C;color:#fff;border-color:#0D2E5C;}
.kegiatan-card{background:#fff;border:1px solid #E2E8F0;border-radius:10px;padding:1.25rem;margin-bottom:.875rem;}
.kegiatan-hd{display:flex;justify-content:space-between;align-items:flex-start;gap:1rem;margin-bottom:.875rem;}
.kegiatan-title{font-weight:600;color:#0D2E5C;font-size:.8rem;}
.kegiatan-meta{font-size:.75rem;color:#8A96A8;margin-top:3px;}
.bukti-link{display:inline-flex;align-items:center;gap:5px;color:#1A56A4;font-size:.75rem;text-decoration:none;padding:4px 10px;background:#EEF4FF;border-radius:6px;}
.bukti-link:hover{background:#dbeafe;}
.eval-form{display:flex;align-items:center;gap:8px;flex-wrap:wrap;}
.eval-select{padding:5px 10px;border:1px solid #E2E8F0;border-radius:6px;font-size:.78rem;font-family:'Inter',sans-serif;outline:none;cursor:pointer;}
.eval-input{padding:5px 10px;border:1px solid #E2E8F0;border-radius:6px;font-size:.78rem;font-family:'Inter',sans-serif;outline:none;flex:1;min-width:150px;}
.luaran-card{background:#fff;border:1px solid #E2E8F0;border-radius:10px;padding:1.25rem;margin-bottom:.875rem;}
.luaran-title{font-weight:600;color:#0D2E5C;font-size:.78rem;margin-bottom:.5rem;}
.luaran-meta{font-size:.78rem;color:#64748b;}
</style>
</head>
<body>
<div class="sidebar">
  <div class="sb-brand">
    <img src="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png" alt="SMART-KKN" style="background:#fff;border-radius:8px;padding:2px;">
    <div class="sb-brand-text">
      <div class="name">SMART-KKN</div>
      <div class="sub">UMUS 2026</div>
    </div>
  </div>
  <div class="sb-user">
    <div>
      <div class="sb-uname"><?= htmlspecialchars($monev['monev_nama']) ?></div>
      <div class="sb-urole">Tim Monev</div>
    </div>
  </div>
  <nav class="sb-nav">
    <a class="nav-a <?= $page==='dashboard'?'active':'' ?>" href="?page=dashboard"><i class="ti ti-home"></i> Dashboard</a>
    <div class="nav-sec">Mahasiswa</div>
    <a class="nav-a <?= in_array($page,['daftar','logbook'])?'active':'' ?>" href="?page=daftar"><i class="ti ti-users"></i> Daftar Mahasiswa</a>
    <div class="nav-sec">Program Kerja</div>
    <a class="nav-a <?= $page==='proker_ind'?'active':'' ?>" href="?page=proker_ind"><i class="ti ti-user"></i> Individu</a>
    <a class="nav-a <?= $page==='proker_kel'?'active':'' ?>" href="?page=proker_kel"><i class="ti ti-users"></i> Kelompok</a>
    <div class="nav-sec">Laporan</div>
    <a class="nav-a <?= $page==='lapkel'?'active':'' ?>" href="?page=lapkel"><i class="ti ti-files"></i> Laporan Kelompok</a>
    <a class="nav-a <?= $page==='rekap'?'active':'' ?>" href="?page=rekap"><i class="ti ti-table"></i> Rekap Evaluasi</a>
  </nav>
  <div class="sb-foot">
    <a href="<?= BASE_URL ?>"><i class="ti ti-home"></i> Kembali ke Website</a>
    <a href="?logout=1"><i class="ti ti-logout"></i> Logout</a>
  </div>
</div>

<div class="main">
  <div class="topbar">
    <div class="topbar-title">
      <?php
      if ($page==='dashboard')      echo 'Dashboard';
      elseif ($page==='daftar')     echo 'Daftar Mahasiswa';
      elseif ($page==='logbook')    echo 'Evaluasi Bukti Kegiatan';
      elseif ($page==='proker_ind') echo 'Program Kerja Individu';
      elseif ($page==='proker_kel') echo 'Program Kerja Kelompok';
      elseif ($page==='lapkel')     echo 'Evaluasi Laporan Kelompok';
      elseif ($page==='rekap')      echo 'Rekap Evaluasi';
      ?>
    </div>
    <div class="topbar-right"><i class="ti ti-calendar" style="margin-right:4px;"></i><?= date('d F Y') ?> &nbsp;·&nbsp; KKN UMUS 2026</div>
  </div>

  <div class="content">
    <?php if(isset($_GET['saved'])): ?>
    <div class="alert-ok"><i class="ti ti-check"></i> Evaluasi berhasil disimpan.</div>
    <?php endif; ?>

    <?php if ($page === 'dashboard'): ?>
    <!-- ─── DASHBOARD ─────────────────────────────────────── -->
    <?php
    $total_bukti   = array_sum(array_column($mhs_list,'total_bukti'));
    $total_tidak   = array_sum(array_column($mhs_list,'total_tidak_valid'));
    ?>
    <div class="stat-grid">
      <div class="stat-card"><div class="stat-ic"><i class="ti ti-users"></i></div><div class="stat-n"><?= $total ?></div><div class="stat-l">Mahasiswa Dimonev</div></div>
      <div class="stat-card"><div class="stat-ic"><i class="ti ti-link"></i></div><div class="stat-n"><?= $total_bukti ?></div><div class="stat-l">Total Bukti Terkumpul</div></div>
      <div class="stat-card"><div class="stat-ic" style="color:#C53030;"><i class="ti ti-x"></i></div><div class="stat-n" style="color:#C53030;"><?= $total_tidak ?></div><div class="stat-l">Bukti Tidak Valid</div></div>
    </div>

    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-map-pin" style="color:#C8922A;"></i> Sebaran Mahasiswa per Kecamatan</h3></div>
      <div style="padding:1rem 1.25rem;display:grid;grid-template-columns:repeat(3,1fr);gap:.75rem;">
        <?php foreach($by_kec as $kec=>$list): ?>
        <a href="?page=daftar#kec-<?= urlencode($kec) ?>" style="text-decoration:none;">
          <div style="background:#F8FAFF;border-radius:8px;padding:.875rem;border:1px solid #E2E8F0;transition:.15s;" onmouseover="this.style.borderColor='#1A56A4'" onmouseout="this.style.borderColor='#E2E8F0'">
            <div style="font-size:.8rem;font-weight:700;color:#0D2E5C;"><?= htmlspecialchars($kec) ?></div>
            <div style="font-size:.72rem;color:#8A96A8;margin-top:2px;"><?= count($list) ?> mahasiswa</div>
          </div>
        </a>
        <?php endforeach; ?>
      </div>
    </div>

    <?php elseif ($page === 'daftar'): ?>
    <!-- ─── DAFTAR MAHASISWA ──────────────────────────────── -->
    <div class="card">
      <div class="card-hd">
        <h3><i class="ti ti-users" style="color:#C8922A;"></i> Mahasiswa Bimbingan Monev</h3>
        <span style="font-size:.8rem;color:#8A96A8;"><?= $total ?> mahasiswa · <?= count($by_kec) ?> kecamatan</span>
      </div>
      <?php $ci=0; foreach($by_kec as $kec=>$list): ?>
      <div class="kec-hd" id="kec-<?= urlencode($kec) ?>">
        <i class="ti ti-map-pin"></i><?= htmlspecialchars($kec) ?>
        <span style="font-weight:400;color:#8A96A8;">(<?= count($list) ?> mhs)</span>
      </div>
      <table>
        <tr><th>Mahasiswa</th><th>Prodi</th><th>Bukti Terkumpul</th><th>Tidak Valid</th><th>Aksi</th></tr>
        <?php foreach($list as $m):
          $col = $colors[$ci % count($colors)];
        ?>
        <tr>
          <td>
            <div style="display:flex;align-items:center;gap:10px;">
              <div class="av" style="background:<?= $col ?>;"><?= av($m['nama']) ?></div>
              <div>
                <div style="font-weight:600;color:#0D2E5C;"><?= htmlspecialchars($m['nama']) ?></div>
                <div style="font-size:.73rem;color:#8A96A8;"><?= $m['nim'] ?></div>
              </div>
            </div>
          </td>
          <td style="font-size:.75rem;"><?= htmlspecialchars($m['prodi']) ?></td>
          <td style="font-size:.75rem;"><?= $m['total_bukti'] ?> kegiatan</td>
          <td>
            <?php if($m['total_tidak_valid'] > 0): ?>
            <span class="badge badge-tidak"><?= $m['total_tidak_valid'] ?> tidak valid</span>
            <?php else: ?>
            <span style="font-size:.78rem;color:#8A96A8;">—</span>
            <?php endif; ?>
          </td>
          <td>
            <a href="?page=logbook&mhs_id=<?= $m['id'] ?>" class="btn btn-navy btn-sm">
              <i class="ti ti-clipboard-check"></i> Evaluasi
            </a>
          </td>
        </tr>
        <?php endforeach; $ci++; ?>
      </table>
      <?php endforeach; ?>
    </div>

    <?php elseif ($page === 'logbook' && $sel_mhs): ?>
    <!-- ─── EVALUASI LOGBOOK ──────────────────────────────── -->
    <div style="margin-bottom:1rem;display:flex;align-items:center;gap:10px;flex-wrap:wrap;">
      <a href="?page=daftar" style="font-size:.75rem;color:#1A56A4;text-decoration:none;display:flex;align-items:center;gap:4px;"><i class="ti ti-arrow-left"></i> Kembali</a>
      <span style="color:#D1D5DB;">|</span>
      <div style="display:flex;align-items:center;gap:8px;">
        <div class="av" style="background:#0D2E5C;width:28px;height:28px;font-size:.68rem;"><?= av($sel_mhs['nama']) ?></div>
        <div>
          <span style="font-weight:700;color:#0D2E5C;font-size:.8rem;"><?= htmlspecialchars($sel_mhs['nama']) ?></span>
          <span style="font-size:.78rem;color:#8A96A8;margin-left:6px;"><?= $sel_mhs['nim'] ?> · <?= htmlspecialchars($sel_mhs['prodi']) ?></span>
        </div>
      </div>
    </div>

    <!-- Program Kerja -->
    <?php if($proker_ind || $proker_kel): ?>
    <div class="card" style="margin-bottom:1rem;">
      <div class="card-hd"><h3><i class="ti ti-clipboard-list" style="color:#C8922A;"></i> Program Kerja</h3></div>
      <div style="padding:1rem 1.25rem;display:grid;grid-template-columns:1fr 1fr;gap:1rem;">
        <?php if($proker_ind): ?>
        <div>
          <div style="font-size:.7rem;font-weight:700;color:#8A96A8;text-transform:uppercase;letter-spacing:.05em;margin-bottom:.5rem;">Individu</div>
          <table style="width:100%;border-collapse:collapse;">
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Judul</td><td style="font-size:.73rem;color:#1A202C;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_ind['judul']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Deskripsi</td><td style="font-size:.73rem;color:#64748b;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_ind['deskripsi']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Sasaran</td><td style="font-size:.73rem;color:#64748b;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_ind['target']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Target Luaran</td><td style="font-size:.73rem;color:#1A56A4;font-weight:500;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_ind['target_luaran']??'—') ?></td></tr>
          </table>
        </div>
        <?php endif; ?>
        <?php if($proker_kel): ?>
        <div>
          <div style="font-size:.7rem;font-weight:700;color:#8A96A8;text-transform:uppercase;letter-spacing:.05em;margin-bottom:.5rem;">Kelompok</div>
          <table style="width:100%;border-collapse:collapse;">
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Judul</td><td style="font-size:.73rem;color:#1A202C;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_kel['judul']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Deskripsi</td><td style="font-size:.73rem;color:#64748b;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_kel['deskripsi']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Sasaran</td><td style="font-size:.73rem;color:#64748b;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_kel['target']??'—') ?></td></tr>
            <tr><td style="font-size:.73rem;color:#64748b;font-weight:600;width:100px;padding:2px 0;vertical-align:top;">Target Luaran</td><td style="font-size:.73rem;color:#1A56A4;font-weight:500;padding:2px 0 2px 4px;vertical-align:top;"><?= htmlspecialchars($proker_kel['target_luaran']??'—') ?></td></tr>
          </table>
        </div>
        <?php endif; ?>
      </div>
    </div>
    <?php endif; ?>

    <!-- Logbook per Minggu -->
    <?php
    $by_minggu = [];
    foreach ($logbooks as $lb) $by_minggu[$lb['minggu']][] = $lb;
    ?>
    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-calendar" style="color:#C8922A;"></i> Evaluasi Bukti Logbook</h3></div>
      <?php if(empty($logbooks)): ?>
      <div style="padding:2rem;text-align:center;color:#8A96A8;font-size:.78rem;">Mahasiswa belum mengisi logbook.</div>
      <?php else: ?>
      <?php for($mg=1;$mg<=6;$mg++): ?>
      <?php if(empty($by_minggu[$mg])) continue; ?>
      <div style="padding:.625rem 1.25rem;background:#F8FAFF;border-bottom:1px solid #F0F4F8;font-size:.75rem;font-weight:700;color:#0D2E5C;">
        Minggu <?= $mg ?> <span style="font-weight:400;color:#8A96A8;">(<?= count($by_minggu[$mg]) ?> kegiatan)</span>
      </div>
      <div style="padding:.875rem 1.25rem;">
        <?php foreach($by_minggu[$mg] as $lb): ?>
        <div class="kegiatan-card">
          <div class="kegiatan-hd">
            <div>
              <div class="kegiatan-title"><?= htmlspecialchars($lb['nama_kegiatan']) ?></div>
              <div class="kegiatan-meta">
                <?php if($lb['waktu']): ?><i class="ti ti-calendar" style="font-size:.75rem;"></i> <?= htmlspecialchars($lb['waktu']) ?> &nbsp;<?php endif; ?>
                <?php if($lb['deskripsi']): ?><span style="color:#94a3b8;"><?= htmlspecialchars(mb_strimwidth($lb['deskripsi'],0,80,'...')) ?></span><?php endif; ?>
              </div>
            </div>
            <?php if($lb['status_eval']): ?>
            <span class="badge <?= $lb['status_eval']==='valid'?'badge-valid':'badge-tidak' ?>">
              <?= $lb['status_eval']==='valid'?'Valid':'Tidak Valid' ?>
            </span>
            <?php endif; ?>
          </div>
          <?php if($lb['bukti_link']): ?>
          <div style="margin-bottom:.875rem;">
            <a href="<?= htmlspecialchars($lb['bukti_link']) ?>" target="_blank" class="bukti-link">
              <i class="ti ti-brand-google-drive" style="color:#1A56A4;"></i> Lihat Bukti Google Drive
            </a>
          </div>
          <form method="POST" style="margin:0;">
            <input type="hidden" name="save_eval" value="1">
            <input type="hidden" name="logbook_id" value="<?= $lb['id'] ?>">
            <input type="hidden" name="mhs_id" value="<?= $sel_mhs['id'] ?>">
            <div class="eval-form">
              <select name="status_eval" class="eval-select" required>
                <option value="">-- Pilih Status --</option>
                <option value="valid" <?= $lb['status_eval']==='valid'?'selected':'' ?>>Valid</option>
                <option value="tidak_valid" <?= $lb['status_eval']==='tidak_valid'?'selected':'' ?>>Tidak Valid</option>
              </select>
              <input type="text" name="catatan_eval" class="eval-input" placeholder="Catatan (opsional)" value="<?= htmlspecialchars($lb['eval_catatan']??'') ?>">
              <button type="submit" class="btn btn-navy btn-sm"><i class="ti ti-device-floppy"></i> Simpan</button>
            </div>
          </form>
          <?php else: ?>
          <div style="font-size:.78rem;color:#F59E0B;display:flex;align-items:center;gap:5px;">
            <i class="ti ti-alert-triangle"></i> Belum ada bukti Google Drive
          </div>
          <?php endif; ?>
        </div>
        <?php endforeach; ?>
      </div>
      <?php endfor; ?>
      <?php endif; ?>
    </div>

    <!-- Luaran Individu -->
    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-file-text" style="color:#C8922A;"></i> Evaluasi Luaran Individu</h3></div>
      <div style="padding:1rem 1.25rem;">
        <?php
        $jenis_luaran = ['jurnal'=>'Artikel Jurnal Pengabdian','media'=>'Artikel Media Massa'];
        foreach($jenis_luaran as $jenis=>$label):
          $lu = null;
          foreach($luaran_ind as $l) { if($l['jenis']===$jenis){ $lu=$l; break; } }
        ?>
        <div class="luaran-card">
          <div class="luaran-title"><?= $label ?></div>
          <?php if($lu && $lu['link_url']): ?>
          <div style="margin-bottom:.875rem;">
            <div class="luaran-meta" style="margin-bottom:.5rem;"><?= htmlspecialchars($lu['judul']??'') ?></div>
            <a href="<?= htmlspecialchars($lu['link_url']) ?>" target="_blank" class="bukti-link">
              <i class="ti ti-brand-google-drive" style="color:#1A56A4;"></i> Lihat Dokumen
            </a>
          </div>
          <form method="POST" style="margin:0;">
            <input type="hidden" name="save_luaran_eval" value="1">
            <input type="hidden" name="mhs_id" value="<?= $sel_mhs['id'] ?>">
            <input type="hidden" name="jenis_luaran" value="<?= $jenis ?>">
            <div class="eval-form">
              <select name="status_luaran" class="eval-select">
                <option value="">-- Status --</option>
                <option value="valid" <?= ($lu['status_eval']??'')==='valid'?'selected':'' ?>>Valid</option>
                <option value="tidak_valid" <?= ($lu['status_eval']??'')==='tidak_valid'?'selected':'' ?>>Tidak Valid</option>
              </select>
              <input type="text" name="catatan_luaran" class="eval-input" placeholder="Catatan" value="<?= htmlspecialchars($lu['eval_catatan']??'') ?>">
              <button type="submit" class="btn btn-navy btn-sm"><i class="ti ti-device-floppy"></i> Simpan</button>
            </div>
          </form>
          <?php else: ?>
          <div style="font-size:.78rem;color:#F59E0B;display:flex;align-items:center;gap:5px;">
            <i class="ti ti-alert-triangle"></i> Belum dikumpulkan
          </div>
          <?php endif; ?>
        </div>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- Luaran Kelompok -->
    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-users" style="color:#C8922A;"></i> Evaluasi Luaran Kelompok</h3></div>
      <div style="padding:1rem 1.25rem;">
        <div class="luaran-card">
          <div class="luaran-title">Laporan Kelompok</div>
          <?php if($luaran_kel && $luaran_kel['laporan_link']): ?>
          <div style="margin-bottom:.875rem;">
            <div class="luaran-meta" style="margin-bottom:.5rem;"><?= htmlspecialchars($luaran_kel['laporan_judul']??'Laporan Kelompok') ?></div>
            <a href="<?= htmlspecialchars($luaran_kel['laporan_link']) ?>" target="_blank" class="bukti-link">
              <i class="ti ti-brand-google-drive" style="color:#1A56A4;"></i> Lihat Laporan
            </a>
          </div>
          <form method="POST">
            <input type="hidden" name="save_luaran_eval" value="1">
            <input type="hidden" name="mhs_id" value="<?= $sel_mhs['id'] ?>">
            <input type="hidden" name="jenis_luaran" value="laporan_kelompok">
            <div class="eval-form">
              <select name="status_luaran" class="eval-select">
                <option value="">-- Status --</option>
                <option value="valid" <?= ($luaran_kel['status_eval']??'')==='valid'?'selected':'' ?>>Valid</option>
                <option value="tidak_valid" <?= ($luaran_kel['status_eval']??'')==='tidak_valid'?'selected':'' ?>>Tidak Valid</option>
              </select>
              <input type="text" name="catatan_luaran" class="eval-input" placeholder="Catatan" value="<?= htmlspecialchars($luaran_kel['eval_catatan']??'') ?>">
              <button type="submit" class="btn btn-navy btn-sm"><i class="ti ti-device-floppy"></i> Simpan</button>
            </div>
          </form>
          <?php else: ?>
          <div style="font-size:.78rem;color:#F59E0B;display:flex;align-items:center;gap:5px;">
            <i class="ti ti-alert-triangle"></i> Belum dikumpulkan
          </div>
          <?php endif; ?>
        </div>
      </div>
    </div>

    <?php elseif ($page === 'lapkel'): ?>
    <!-- ─── LAPORAN KELOMPOK ────────────────────────────────── -->
    <div class="card">
      <div class="card-hd">
        <h3><i class="ti ti-files" style="color:#C8922A;"></i> Evaluasi Laporan Kelompok</h3>
        <span style="font-size:.8rem;color:#8A96A8;"><?= count($lapkel_list) ?> kelompok</span>
      </div>
      <?php if(empty($lapkel_list)): ?>
      <div style="padding:2rem;text-align:center;color:#8A96A8;">Tidak ada data kelompok.</div>
      <?php else: ?>
      <?php foreach($lapkel_list as $lk): ?>
      <div style="padding:1.25rem;border-bottom:1px solid #F0F4F8;">
        <!-- Header kelompok -->
        <div style="display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:.875rem;flex-wrap:wrap;gap:.5rem;">
          <div>
            <div style="font-weight:700;color:#0D2E5C;font-size:.82rem;">
              <i class="ti ti-map-pin" style="color:#C8922A;font-size:.78rem;"></i>
              <?= htmlspecialchars($lk['kecamatan']) ?>, <?= htmlspecialchars($lk['kabupaten']) ?>
            </div>
            <div style="font-size:.78rem;color:#8A96A8;margin-top:2px;">
              DPL: <?= htmlspecialchars($lk['dpl_nama']) ?>
            </div>
          </div>
          <?php if($lk['nilai_lapkel'] !== null && $lk['nilai_lapkel'] !== ''): ?>
          <span style="font-family:'Playfair Display',serif;font-size:1rem;font-weight:700;color:#C8922A;">
            <?= $lk['nilai_lapkel'] ?>
          </span>
          <?php endif; ?>
        </div>

        <!-- Laporan link -->
        <?php if($lk['laporan_link']): ?>
        <div style="margin-bottom:.875rem;">
          <div style="font-size:.78rem;color:#64748b;margin-bottom:.4rem;">
            <?= htmlspecialchars($lk['laporan_judul'] ?? 'Laporan Kelompok') ?>
          </div>
          <a href="<?= htmlspecialchars($lk['laporan_link']) ?>" target="_blank" class="bukti-link">
            <i class="ti ti-brand-google-drive" style="color:#1A56A4;"></i> Lihat Laporan Kelompok
          </a>
        </div>
        <form method="POST">
          <input type="hidden" name="save_lapkel" value="1">
          <input type="hidden" name="dpl_id" value="<?= $lk['dpl_id'] ?>">
          <div class="eval-form">
            <div style="display:flex;align-items:center;gap:6px;">
              <label style="font-size:.78rem;color:#64748b;white-space:nowrap;">Nilai (0-100)</label>
              <input type="number" name="nilai_lapkel" min="0" max="100" step="0.01"
                class="eval-select" style="width:90px;"
                value="<?= htmlspecialchars($lk['nilai_lapkel']??'') ?>"
                placeholder="0-100" required>
            </div>
            <input type="text" name="catatan_lapkel" class="eval-input" placeholder="Catatan (opsional)" value="<?= htmlspecialchars($lk['eval_catatan']??'') ?>">
            <button type="submit" class="btn btn-navy btn-sm"><i class="ti ti-device-floppy"></i> Simpan</button>
          </div>
        </form>
        <?php else: ?>
        <div style="font-size:.78rem;color:#F59E0B;display:flex;align-items:center;gap:5px;">
          <i class="ti ti-alert-triangle"></i> Laporan kelompok belum dikumpulkan
        </div>
        <?php endif; ?>
      </div>
      <?php endforeach; ?>
      <?php endif; ?>
    </div>

    <?php elseif ($page === 'proker_ind'): ?>
    <!-- ─── PROGRAM KERJA INDIVIDU ──────────────────────────── -->
    <div class="card">
      <div class="card-hd">
        <h3><i class="ti ti-user" style="color:#C8922A;"></i> Program Kerja Individu
          <span style="background:#0D2E5C;color:#fff;padding:1px 9px;border-radius:20px;font-size:.68rem;margin-left:6px;"><?= count($mhs_list) ?> mhs</span>
        </h3>
      </div>
      <table>
        <style>.proker-table td{vertical-align:top!important;padding-top:7px;}</style>
        <colgroup>
          <col style="width:16%">
          <col style="width:10%">
          <col style="width:18%">
          <col style="width:30%">
          <col style="width:14%">
          <col style="width:12%">
        </colgroup>
        <tr><th>Mahasiswa</th><th>Kecamatan</th><th>Judul Program Kerja</th><th>Deskripsi</th><th>Sasaran Kegiatan</th><th>Target Luaran</th></tr>
        <?php foreach($mhs_list as $m):
          $pi_list = fetchAll("SELECT * FROM kkn_proker_individu WHERE mahasiswa_id=? ORDER BY created_at", [$m['id']]);
        ?>
        <?php if(empty($pi_list)): ?>
        <tr>
          <td style="vertical-align:top;">
            <div style="font-weight:600;color:#0D2E5C;font-size:.78rem;"><?= htmlspecialchars($m['nama']) ?></div>
            <div style="font-size:.68rem;color:#8A96A8;"><?= $m['nim'] ?> · <?= htmlspecialchars($m['prodi']) ?></div>
          </td>
          <td style="vertical-align:top;font-size:.75rem;"><?= htmlspecialchars($m['kecamatan']) ?></td>
          <td colspan="4"><span style="color:#F59E0B;font-size:.75rem;"><i class="ti ti-alert-triangle"></i> Belum diisi</span></td>
        </tr>
        <?php else: ?>
        <?php foreach($pi_list as $idx=>$pi): ?>
        <tr>
          <?php if($idx===0): ?>
          <td rowspan="<?= count($pi_list) ?>" style="vertical-align:top;">
            <div style="font-weight:600;color:#0D2E5C;font-size:.78rem;"><?= htmlspecialchars($m['nama']) ?></div>
            <div style="font-size:.68rem;color:#8A96A8;"><?= $m['nim'] ?> · <?= htmlspecialchars($m['prodi']) ?></div>
          </td>
          <td rowspan="<?= count($pi_list) ?>" style="vertical-align:top;font-size:.75rem;"><?= htmlspecialchars($m['kecamatan']) ?></td>
          <?php endif; ?>
          <td style="vertical-align:top;font-size:.78rem;font-weight:600;"><?= htmlspecialchars($pi['judul']) ?></td>
          <td style="vertical-align:top;font-size:.72rem;color:#64748b;"><?= htmlspecialchars($pi['deskripsi']??'—') ?></td>
          <td style="vertical-align:top;font-size:.75rem;"><?= htmlspecialchars($pi['target']??'—') ?></td>
          <td style="vertical-align:top;font-size:.75rem;color:#1A56A4;font-weight:500;"><?= htmlspecialchars($pi['target_luaran']??'—') ?></td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
        <?php endforeach; ?>
      </table>
    </div>

    <?php elseif ($page === 'proker_kel'): ?>
    <!-- ─── PROGRAM KERJA KELOMPOK ──────────────────────────── -->
    <?php
    // Filter kecamatan berdasarkan jatah tim monev (kkn_monev_lapkel)
    $kec_list = fetchAll("SELECT ml.kecamatan, d.id as dpl_id FROM kkn_monev_lapkel ml JOIN kkn_dpl d ON d.kecamatan = ml.kecamatan WHERE ml.monev_id=? ORDER BY ml.kecamatan", [$monev_id]);
    ?>
    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-users" style="color:#C8922A;"></i> Program Kerja Kelompok</h3></div>
      <?php foreach($kec_list as $kec):
        $pk_list = fetchAll("SELECT pk.*, m.nama as input_nama FROM kkn_proker_kelompok pk JOIN kkn_mahasiswa m ON m.id=pk.input_by WHERE pk.dpl_id=? ORDER BY pk.created_at", [$kec['dpl_id']]);
      ?>
      <div class="kec-hd"><i class="ti ti-map-pin"></i> <?= htmlspecialchars($kec['kecamatan']) ?></div>
      <?php if($pk_list): ?>
      <table>
        <style>.proker-kel-table td{vertical-align:top!important;padding-top:7px;}</style>
        <colgroup>
          <col style="width:4%">
          <col style="width:16%">
          <col style="width:30%">
          <col style="width:22%">
          <col style="width:14%">
          <col style="width:14%">
        </colgroup>
        <tr><th>#</th><th>Judul Program Kerja Kelompok</th><th>Deskripsi</th><th>Sasaran Kegiatan</th><th>Target Luaran</th><th>Diinput oleh</th></tr>
        <?php foreach($pk_list as $i=>$pk): ?>
        <tr>
          <td style="color:#8A96A8;font-size:.75rem;"><?= $i+1 ?></td>
          <td style="vertical-align:top;font-size:.78rem;font-weight:600;"><?= htmlspecialchars($pk['judul']) ?></td>
          <td style="vertical-align:top;font-size:.72rem;color:#64748b;"><?= htmlspecialchars($pk['deskripsi']??'—') ?></td>
          <td style="vertical-align:top;font-size:.75rem;"><?= htmlspecialchars($pk['target']??'—') ?></td>
          <td style="vertical-align:top;font-size:.75rem;color:#1A56A4;font-weight:500;"><?= htmlspecialchars($pk['target_luaran']??'—') ?></td>
          <td style="vertical-align:top;font-size:.72rem;color:#8A96A8;"><?= htmlspecialchars($pk['input_nama']) ?></td>
        </tr>
        <?php endforeach; ?>
      </table>
      <?php else: ?>
      <div style="padding:1rem 1.25rem;font-size:.78rem;color:#8A96A8;">Belum ada proker kelompok.</div>
      <?php endif; ?>
      <?php endforeach; ?>
    </div>

    <?php elseif ($page === 'rekap'): ?>
    <!-- ─── REKAP ──────────────────────────────────────────── -->
    <div class="card">
      <div class="card-hd"><h3><i class="ti ti-table" style="color:#C8922A;"></i> Rekap Evaluasi</h3></div>
      <table>
        <tr><th>#</th><th>Mahasiswa</th><th>Prodi</th><th>Kecamatan</th><th style="text-align:center;">Bukti</th><th style="text-align:center;">Tidak Valid</th><th>Aksi</th></tr>
        <?php foreach($mhs_list as $i=>$m): ?>
        <tr style="<?= $i%2?'background:#F8FAFF;':'' ?>">
          <td style="color:#8A96A8;font-size:.78rem;"><?= $i+1 ?></td>
          <td>
            <div style="font-weight:600;color:#0D2E5C;font-size:.78rem;"><?= htmlspecialchars($m['nama']) ?></div>
            <div style="font-size:.72rem;color:#8A96A8;"><?= $m['nim'] ?></div>
          </td>
          <td style="font-size:.75rem;"><?= htmlspecialchars($m['prodi']) ?></td>
          <td style="font-size:.75rem;"><?= htmlspecialchars($m['kecamatan']) ?></td>
          <td style="text-align:center;font-size:.78rem;"><?= $m['total_bukti'] ?></td>
          <td style="text-align:center;">
            <?php if($m['total_tidak_valid']>0): ?>
            <span class="badge badge-tidak"><?= $m['total_tidak_valid'] ?></span>
            <?php else: ?>
            <span style="font-size:.78rem;color:#8A96A8;">—</span>
            <?php endif; ?>
          </td>
          <td><a href="?page=logbook&mhs_id=<?= $m['id'] ?>" class="btn btn-ghost btn-sm"><i class="ti ti-eye"></i> Detail</a></td>
        </tr>
        <?php endforeach; ?>
      </table>
    </div>

    <?php endif; ?>
  </div>
</div>


</body>
</html>
