<?php
require_once __DIR__ . '/includes/functions.php';
$settings   = getAllSettings();
$sec        = getAllSectionSettings('universities');
$meta_title = 'Courses & Programs | ' . ($settings['site_name'] ?? 'Global College');

$catFilter  = $_GET['cat'] ?? '';
$uniFilter  = (int)($_GET['uni'] ?? 0);
$search     = trim($_GET['q'] ?? '');
$viewId     = (int)($_GET['id'] ?? 0);

$allUnis    = getUniversities();
$custom     = getCustomSections('universities');

// Single university view
$uniDetail  = null;
$uniCourses = [];
$uniFaculties = [];
if ($viewId) {
    foreach ($allUnis as $u) { if ($u['id'] == $viewId) { $uniDetail = $u; break; } }
    if ($uniDetail) {
        $uniCourses = getUniversityCourses($viewId);
        // Fetch faculties if function exists
        if (function_exists('getUniversityFaculties')) {
            $uniFaculties = getUniversityFaculties($viewId);
        }
    }
}

include __DIR__ . '/includes/header.php';
?>

<!-- HERO -->
<section style="background:var(--grad-dark);padding:120px 0 60px;">
  <div class="container" style="text-align:center;">
    <span class="badge" style="background:rgba(27,63,160,.2);border-color:rgba(27,63,160,.4);color:#fca5a5;"><i class="fas fa-graduation-cap"></i> Courses & Programs</span>
    <h1 style="color:#fff;margin:16px 0 12px;font-size:clamp(1.8rem,4vw,2.8rem);">Find Your Perfect Program</h1>
    <p style="color:#94a3b8;max-width:560px;margin:0 auto;">Explore UGC-recognised degrees from India's top universities. UG, PG, Diploma, Certificate and PhD programs available.</p>

    <!-- Search Bar -->
    <form method="GET" style="display:flex;gap:0;max-width:500px;margin:28px auto 0;border-radius:50px;overflow:hidden;box-shadow:0 8px 30px rgba(0,0,0,.3);">
      <input type="text" name="q" value="<?= htmlspecialchars($search) ?>" placeholder="Search programs, universities..." style="flex:1;padding:14px 22px;border:none;font-size:.9rem;outline:none;font-family:inherit;">
      <?php if ($catFilter): ?><input type="hidden" name="cat" value="<?= htmlspecialchars($catFilter) ?>"><?php endif; ?>
      <button type="submit" style="padding:14px 26px;background:var(--primary);border:none;color:#fff;font-size:.9rem;cursor:pointer;font-weight:700;"><i class="fas fa-search"></i></button>
    </form>
  </div>
</section>

<?php if ($uniDetail): ?>
<!-- ================================================================
     SINGLE UNIVERSITY DETAIL VIEW
     ================================================================ -->
<section class="section" style="background:#f5f6fa;padding-top:36px;">
  <div class="container">
    <div style="margin-bottom:20px;">
      <a href="/universities.php" style="color:var(--primary);font-size:.88rem;font-weight:700;"><i class="fas fa-arrow-left"></i> Back to All Programs</a>
    </div>

    <div style="display:grid;grid-template-columns:300px 1fr;gap:28px;align-items:start;" class="uni-detail-grid">

      <!-- ── LEFT SIDEBAR ── -->
      <div style="position:sticky;top:90px;">

        <!-- University Card -->
        <div style="background:#fff;border:1.5px solid var(--border);border-radius:var(--radius-lg);padding:24px;text-align:center;margin-bottom:16px;box-shadow:0 2px 12px rgba(0,0,0,.05);">
          <?php if (!empty($uniDetail['logo'])): ?>
            <div style="width:100%;height:140px;overflow:hidden;border-radius:10px;margin:0 auto 14px;background:#f8fafc;display:flex;align-items:center;justify-content:center;padding:12px;box-sizing:border-box;border:1.5px solid var(--border);">
              <img src="/assets/images/uploads/universities/<?= htmlspecialchars(basename($uniDetail['logo'])) ?>" alt="<?= htmlspecialchars($uniDetail['name']) ?>" style="max-width:100%;max-height:116px;width:auto;height:auto;object-fit:contain;display:block;">
            </div>
          <?php else: ?>
            <div style="width:64px;height:64px;background:rgba(27,63,160,.1);border-radius:16px;display:flex;align-items:center;justify-content:center;margin:0 auto 14px;"><i class="fas fa-university" style="font-size:1.6rem;color:var(--primary);"></i></div>
          <?php endif; ?>
          <h2 style="font-size:1rem;margin-bottom:8px;line-height:1.4;"><?= htmlspecialchars($uniDetail['name']) ?></h2>
          <?php if (!empty($uniDetail['accreditation'])): ?>
          <span style="background:rgba(27,63,160,.1);color:var(--primary);padding:4px 12px;border-radius:20px;font-size:.7rem;font-weight:700;display:inline-block;margin-bottom:8px;"><?= htmlspecialchars($uniDetail['accreditation']) ?></span>
          <?php endif; ?>
          <?php if (!empty($uniDetail['location'])): ?>
          <div style="font-size:.8rem;color:var(--text-mid);"><i class="fas fa-map-marker-alt" style="color:var(--primary);"></i> <?= htmlspecialchars($uniDetail['location']) ?></div>
          <?php endif; ?>
          <?php if (!empty($uniDetail['established'])): ?>
          <div style="font-size:.78rem;color:var(--text-light);margin-top:4px;"><i class="fas fa-calendar" style="color:var(--primary);"></i> Est. <?= $uniDetail['established'] ?></div>
          <?php endif; ?>
        </div>

        <!-- Stats Row -->
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:16px;">
          <div style="background:#fff;border:1.5px solid var(--border);border-radius:12px;padding:14px;text-align:center;">
            <div style="font-size:1.4rem;font-weight:800;color:var(--primary);"><?= count($uniCourses) ?></div>
            <div style="font-size:.68rem;color:var(--text-light);margin-top:2px;">Programs</div>
          </div>
          <?php
            $catCount = count(array_unique(array_column($uniCourses, 'category')));
          ?>
          <div style="background:#fff;border:1.5px solid var(--border);border-radius:12px;padding:14px;text-align:center;">
            <div style="font-size:1.4rem;font-weight:800;color:var(--primary);"><?= $catCount ?></div>
            <div style="font-size:.68rem;color:var(--text-light);margin-top:2px;">Categories</div>
          </div>
        </div>

        <!-- Highlights -->
        <?php
        $highlights = array_filter([
          $uniDetail['highlight1'] ?? '',
          $uniDetail['highlight2'] ?? '',
          $uniDetail['highlight3'] ?? '',
        ]);
        if (!empty($highlights)):
        ?>
        <div style="background:#fff;border:1.5px solid var(--border);border-radius:var(--radius-lg);padding:18px;margin-bottom:16px;">
          <div style="font-size:.75rem;font-weight:700;color:var(--text-mid);text-transform:uppercase;letter-spacing:.5px;margin-bottom:10px;">Key Highlights</div>
          <?php foreach ($highlights as $hl): ?>
          <div style="display:flex;align-items:center;gap:8px;margin-bottom:7px;font-size:.8rem;color:var(--black);">
            <i class="fas fa-check-circle" style="color:#22c55e;flex-shrink:0;"></i> <?= htmlspecialchars($hl) ?>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <!-- About -->
        <?php if (!empty($uniDetail['description'])): ?>
        <div style="background:#fff;border:1.5px solid var(--border);border-radius:var(--radius-lg);padding:18px;margin-bottom:16px;">
          <div style="font-size:.75rem;font-weight:700;color:var(--text-mid);text-transform:uppercase;letter-spacing:.5px;margin-bottom:8px;">About University</div>
          <p style="color:var(--text-mid);font-size:.8rem;line-height:1.75;margin:0;"><?= htmlspecialchars($uniDetail['description']) ?></p>
        </div>
        <?php endif; ?>

        <!-- Faculties -->
        <?php if (!empty($uniFaculties)): ?>
        <div style="background:#fff;border:1.5px solid var(--border);border-radius:var(--radius-lg);padding:18px;margin-bottom:16px;">
          <div style="font-size:.75rem;font-weight:700;color:var(--text-mid);text-transform:uppercase;letter-spacing:.5px;margin-bottom:10px;">Faculties Offered</div>
          <?php foreach ($uniFaculties as $fac): ?>
          <div style="display:flex;align-items:center;gap:8px;margin-bottom:6px;font-size:.8rem;color:var(--black);">
            <i class="fas fa-angle-right" style="color:var(--primary);flex-shrink:0;"></i> <?= htmlspecialchars($fac['name']) ?>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <a href="/college-finder.php" class="btn btn-primary" style="width:100%;justify-content:center;display:flex;"><i class="fas fa-search"></i> Get Free Counseling</a>
      </div>

      <!-- ── RIGHT: COURSES ACCORDION ── -->
      <div>
        <?php
        $catGroups = [];
        foreach ($uniCourses as $c) { $catGroups[$c['category']][] = $c; }
        $isBOSSEDetail = ($uniDetail['id'] == 1);
        $catIcons = ['UG'=>'fas fa-graduation-cap','PG'=>'fas fa-user-graduate','PhD'=>'fas fa-flask','Diploma'=>'fas fa-certificate','Certificate'=>'fas fa-award'];
        $catColors = ['UG'=>'#1B3FA0','PG'=>'#7c3aed','PhD'=>'#dc2626','Diploma'=>'#d97706','Certificate'=>'#059669'];
        ?>

        <!-- Category Quick-Jump Tabs -->
        <?php if (count($catGroups) > 1): ?>
        <div style="display:flex;gap:6px;flex-wrap:wrap;margin-bottom:20px;">
          <?php foreach (array_keys($catGroups) as $cat): ?>
          <button onclick="jumpToSection('cat-<?= $uniDetail['id'].'-'.preg_replace('/\W/','',strtolower($cat)) ?>')"
            style="padding:6px 16px;border-radius:50px;border:1.5px solid <?= $catColors[$cat] ?? 'var(--primary)' ?>;background:<?= $catColors[$cat] ?? 'var(--primary)' ?>18;color:<?= $catColors[$cat] ?? 'var(--primary)' ?>;font-size:.75rem;font-weight:700;cursor:pointer;">
            <i class="<?= $catIcons[$cat] ?? 'fas fa-book' ?>"></i> <?= $cat ?> (<?= count($catGroups[$cat]) ?>)
          </button>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>

        <?php foreach ($catGroups as $cat => $list):
          $sectionId = 'cat-'.$uniDetail['id'].'-'.preg_replace('/\W/','',strtolower($cat));
          $accent = $catColors[$cat] ?? '#1B3FA0';
          $icon   = $catIcons[$cat] ?? 'fas fa-book';
        ?>
        <div id="<?= $sectionId ?>" style="margin-bottom:20px;background:#fff;border:1.5px solid var(--border);border-radius:var(--radius-lg);overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,.04);">

          <!-- Category Header (click to collapse) -->
          <div class="acc-header" onclick="toggleAccordion(this)"
            style="display:flex;align-items:center;justify-content:space-between;padding:16px 20px;cursor:pointer;background:<?= $accent ?>0d;border-bottom:1.5px solid <?= $accent ?>22;">
            <div style="display:flex;align-items:center;gap:10px;">
              <span style="width:34px;height:34px;background:<?= $accent ?>;border-radius:8px;display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                <i class="<?= $icon ?>" style="color:#fff;font-size:.85rem;"></i>
              </span>
              <div>
                <div style="font-weight:700;font-size:.95rem;color:var(--black);"><?= $cat ?> Programs</div>
                <div style="font-size:.72rem;color:var(--text-light);"><?= count($list) ?> program<?= count($list)>1?'s':'' ?> available</div>
              </div>
            </div>
            <i class="fas fa-chevron-down acc-icon" style="color:<?= $accent ?>;transition:transform .3s;"></i>
          </div>

          <!-- Accordion Body -->
          <div class="acc-body" style="display:block;">
            <?php if ($isBOSSEDetail && $cat === 'Certificate'): ?>
            <!-- BOSSE Subject Tables -->
            <div style="padding:16px;display:flex;flex-direction:column;gap:14px;">
              <?php foreach ($list as $c):
                $specs = !empty($c['specializations']) ? json_decode($c['specializations'], true) : [];
              ?>
              <div style="background:#f8fafc;border:1.5px solid var(--border);border-radius:var(--radius);overflow:hidden;">
                <div style="background:#071535;padding:10px 16px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:8px;">
                  <span style="color:#fff;font-weight:700;font-size:.88rem;"><?= htmlspecialchars($c['name']) ?></span>
                  <?php if (!empty($c['short_name'])): ?><span style="color:#93c5fd;font-size:.72rem;">(<?= htmlspecialchars($c['short_name']) ?>)</span><?php endif; ?>
                </div>
                <?php if (!empty($specs)): ?>
                <table style="width:100%;border-collapse:collapse;">
                  <thead>
                    <tr style="background:#bfdbfe;">
                      <th style="padding:7px 12px;font-size:.7rem;font-weight:700;color:#1e3a8a;text-align:center;width:40px;border:1px solid #93c5fd;">No</th>
                      <th style="padding:7px 12px;font-size:.7rem;font-weight:700;color:#1e3a8a;text-align:center;border:1px solid #93c5fd;">Examination</th>
                      <th style="padding:7px 12px;font-size:.7rem;font-weight:700;color:#1e3a8a;text-align:center;border:1px solid #93c5fd;">Subject Details</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php foreach ($specs as $i => $subject): ?>
                    <tr style="background:#e0f2fe;">
                      <td style="padding:7px 12px;font-size:.78rem;text-align:center;border:1px solid #bae6fd;color:#1e40af;"><?= $i+1 ?></td>
                      <?php if ($i === 0): ?>
                      <td rowspan="<?= count($specs) ?>" style="padding:8px 12px;font-size:.78rem;text-align:center;border:1px solid #bae6fd;background:#dbeafe;color:#1e40af;font-weight:600;vertical-align:middle;"><?= htmlspecialchars($c['name']) ?></td>
                      <?php endif; ?>
                      <td style="padding:7px 12px;font-size:.78rem;text-align:center;border:1px solid #bae6fd;color:#1e3a8a;font-weight:500;"><?= htmlspecialchars($subject) ?></td>
                    </tr>
                    <?php endforeach; ?>
                  </tbody>
                </table>
                <?php endif; ?>
                <div style="padding:10px 14px;display:flex;gap:14px;flex-wrap:wrap;align-items:center;background:#fff;border-top:1px solid var(--border);">
                  <?php if (!empty($c['eligibility'])): ?><span style="font-size:.72rem;color:var(--text-mid);"><i class="fas fa-check-circle" style="color:green;"></i> <?= htmlspecialchars($c['eligibility']) ?></span><?php endif; ?>
                  <?php if (!empty($c['duration'])): ?><span style="font-size:.72rem;color:var(--text-mid);"><i class="fas fa-clock" style="color:var(--primary);"></i> <?= htmlspecialchars($c['duration']) ?></span><?php endif; ?>
                  <a href="/college-finder.php?course=<?= urlencode($c['name']) ?>" class="btn btn-primary btn-sm" style="margin-left:auto;"><i class="fas fa-paper-plane"></i> Enquire</a>
                </div>
              </div>
              <?php endforeach; ?>
            </div>

            <?php else: ?>
            <!-- Regular Course Cards — searchable grid inside accordion -->
            <div style="padding:14px 16px;">
              <!-- Mini search inside category -->
              <?php if (count($list) > 6): ?>
              <div style="margin-bottom:12px;">
                <input type="text" placeholder="Search in <?= $cat ?> programs..." onkeyup="filterCourses(this,'<?= $sectionId ?>')"
                  style="width:100%;padding:8px 14px;border:1.5px solid var(--border);border-radius:50px;font-size:.8rem;outline:none;font-family:inherit;box-sizing:border-box;">
              </div>
              <?php endif; ?>

              <div class="courses-grid" style="display:flex;flex-direction:column;gap:8px;">
                <?php foreach ($list as $idx => $c):
                  $specs = !empty($c['specializations']) ? json_decode($c['specializations'], true) : [];
                ?>
                <div class="course-row" style="background:#f8fafc;border:1.5px solid var(--border);border-radius:10px;padding:14px 16px;transition:border-color .2s,box-shadow .2s;" onmouseover="this.style.borderColor='<?= $accent ?>';this.style.boxShadow='0 2px 12px <?= $accent ?>22'" onmouseout="this.style.borderColor='var(--border)';this.style.boxShadow='none'">
                  <div style="display:flex;justify-content:space-between;align-items:flex-start;gap:10px;flex-wrap:wrap;">
                    <div style="flex:1;min-width:0;">
                      <div style="font-weight:700;font-size:.88rem;color:var(--black);">
                        <?= htmlspecialchars($c['name']) ?>
                        <?= !empty($c['short_name']) ? '<span style="color:'.$accent.';font-size:.75rem;margin-left:4px;">('.$c['short_name'].')</span>' : '' ?>
                      </div>
                      <div style="display:flex;gap:14px;margin-top:5px;flex-wrap:wrap;align-items:center;">
                        <?php if (!empty($c['duration'])): ?>
                        <span style="font-size:.72rem;color:var(--text-light);"><i class="fas fa-clock" style="color:<?= $accent ?>;"></i> <?= htmlspecialchars($c['duration']) ?></span>
                        <?php endif; ?>
                        <?php if (!empty($c['eligibility'])): ?>
                        <span style="font-size:.72rem;color:var(--text-light);"><i class="fas fa-check" style="color:green;"></i> <?= htmlspecialchars($c['eligibility']) ?></span>
                        <?php endif; ?>
                        <?php if (!empty($c['fees_per_year'])): ?>
                        <span style="font-size:.72rem;color:var(--text-light);"><i class="fas fa-rupee-sign" style="color:<?= $accent ?>;"></i> <?= formatFee($c['fees_per_year']) ?>/yr</span>
                        <?php endif; ?>
                      </div>
                      <?php if (!empty($specs)): ?>
                      <div style="margin-top:8px;display:flex;flex-wrap:wrap;gap:4px;">
                        <?php foreach (array_slice($specs,0,4) as $sp): ?>
                        <span style="background:#fff;border:1px solid var(--border);padding:2px 8px;border-radius:10px;font-size:.65rem;color:var(--text-mid);"><?= htmlspecialchars($sp) ?></span>
                        <?php endforeach; ?>
                        <?php if (count($specs) > 4): ?>
                        <span style="background:#fff;border:1px solid var(--border);padding:2px 8px;border-radius:10px;font-size:.65rem;color:var(--primary);">+<?= count($specs)-4 ?> more</span>
                        <?php endif; ?>
                      </div>
                      <?php endif; ?>
                    </div>
                    <a href="/college-finder.php?course=<?= urlencode($c['name']) ?>" class="btn btn-sm" style="background:<?= $accent ?>18;border:1.5px solid <?= $accent ?>44;color:<?= $accent ?>;white-space:nowrap;flex-shrink:0;"><i class="fas fa-paper-plane"></i> Apply</a>
                  </div>
                </div>
                <?php endforeach; ?>
              </div>
            </div>
            <?php endif; ?>
          </div>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
  </div>
</section>

<?php else: ?>
<!-- ================================================================
     ALL UNIVERSITIES LISTING
     ================================================================ -->
<section class="section" style="background:#f5f6fa;">
  <div class="container">

    <!-- Category Filter -->
    <div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:28px;" class="reveal">
      <a href="/universities.php<?= $search?'?q='.urlencode($search):'' ?>" class="filter-btn <?= !$catFilter?'active':'' ?>">All Programs</a>
      <?php foreach (['UG','PG','Diploma','Certificate','PhD'] as $cat): ?>
      <a href="/universities.php?cat=<?= $cat ?><?= $search?'&q='.urlencode($search):'' ?>" class="filter-btn <?= $catFilter===$cat?'active':'' ?>"><?= $cat ?></a>
      <?php endforeach; ?>
    </div>

    <?php
    $displayUnis = [];
    foreach ($allUnis as $u) {
        $courses = getUniversityCourses($u['id']);
        $filtered = [];
        foreach ($courses as $c) {
            if ($catFilter && $c['category'] !== $catFilter) continue;
            if ($search && stripos($c['name'],$search)===false && stripos($u['name'],$search)===false) continue;
            $filtered[] = $c;
        }
        if ($search || $catFilter) {
            if (!empty($filtered)) { $u['filtered_courses'] = $filtered; $displayUnis[] = $u; }
        } else {
            $u['filtered_courses'] = $courses;
            $displayUnis[] = $u;
        }
    }
    ?>

    <?php if (empty($displayUnis)): ?>
    <div style="text-align:center;padding:80px 20px;background:#fff;border-radius:var(--radius-lg);border:1.5px solid var(--border);">
      <i class="fas fa-search" style="font-size:3rem;color:var(--border);margin-bottom:16px;display:block;"></i>
      <h3 style="color:var(--text-mid);margin-bottom:8px;">No programs found</h3>
      <p style="color:var(--text-light);font-size:.88rem;">Try a different search or <a href="/universities.php" style="color:var(--primary);">view all programs</a>.</p>
    </div>
    <?php else: ?>
    <p style="color:var(--text-mid);font-size:.84rem;margin-bottom:20px;" class="reveal">
      Showing <?= count($displayUnis) ?> universit<?= count($displayUnis)===1?'y':'ies' ?><?= $catFilter?' for '.$catFilter:'' ?><?= $search?' matching "'.htmlspecialchars($search).'"':'' ?>
    </p>

    <?php
    $catColors = ['UG'=>'#1B3FA0','PG'=>'#7c3aed','PhD'=>'#dc2626','Diploma'=>'#d97706','Certificate'=>'#059669'];
    $catIcons  = ['UG'=>'fas fa-graduation-cap','PG'=>'fas fa-user-graduate','PhD'=>'fas fa-flask','Diploma'=>'fas fa-certificate','Certificate'=>'fas fa-award'];
    foreach ($displayUnis as $u):
        $courses   = $u['filtered_courses'];
        $catGroups = [];
        foreach ($courses as $c) { $catGroups[$c['category']][] = $c; }
        $isBOSSE = ($u['id'] == 1);
        $uniCardId = 'uni-'.$u['id'];
    ?>
    <div class="uni-listing-card reveal" style="background:#fff;border:1.5px solid <?= $isBOSSE ? '#bfdbfe' : 'var(--border)' ?>;border-radius:var(--radius-lg);overflow:hidden;margin-bottom:20px;">

      <?php if (!empty($u['logo'])): ?>
      <div style="width:100%;height:160px;overflow:hidden;position:relative;display:flex;align-items:center;justify-content:center;padding:16px;box-sizing:border-box;<?= $isBOSSE ? 'background:linear-gradient(135deg,#071535,#1B3FA0);border-bottom:2px solid #f5a623;' : 'background:#f8fafc;border-bottom:1px solid var(--border);' ?>">
        <img src="/assets/images/uploads/universities/<?= htmlspecialchars(basename($u['logo'])) ?>" alt="<?= htmlspecialchars($u['name']) ?>" style="max-width:100%;max-height:128px;width:auto;height:auto;object-fit:contain;display:block;" loading="lazy">
      </div>
      <?php endif; ?>

      <!-- University Header -->
      <div class="uni-listing-head" style="padding:18px 20px;border-bottom:1px solid var(--border);<?= $isBOSSE ? 'background:linear-gradient(135deg,#071535 0%,#1B3FA0 100%);' : '' ?>">
        <div style="display:flex;align-items:center;gap:14px;flex-wrap:wrap;">
          <?php if (empty($u['logo'])): ?>
          <div style="width:52px;height:52px;background:<?= $isBOSSE ? 'rgba(255,255,255,.1)' : '#f8fafc' ?>;border-radius:10px;display:flex;align-items:center;justify-content:center;border:1px solid <?= $isBOSSE ? 'rgba(255,255,255,.2)' : 'var(--border)' ?>;flex-shrink:0;">
            <i class="fas fa-university" style="color:<?= $isBOSSE ? '#fff' : 'var(--primary)' ?>;font-size:1.2rem;"></i>
          </div>
          <?php endif; ?>
          <div style="flex:1;min-width:0;">
            <h3 style="font-size:.98rem;color:<?= $isBOSSE ? '#fff' : 'var(--black)' ?>;margin-bottom:6px;"><?= htmlspecialchars($u['name']) ?></h3>
            <div style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">
              <?php if (!empty($u['accreditation'])): ?>
              <span style="background:<?= $isBOSSE?'rgba(245,166,35,.2)':'rgba(27,63,160,.1)' ?>;color:<?= $isBOSSE?'#f5d87a':'var(--primary)' ?>;padding:2px 10px;border-radius:20px;font-size:.67rem;font-weight:700;border:1px solid <?= $isBOSSE?'rgba(245,166,35,.4)':'rgba(27,63,160,.2)' ?>;"><?= htmlspecialchars($u['accreditation']) ?></span>
              <?php endif; ?>
              <?php foreach (array_keys($catGroups) as $cat): ?>
              <span style="background:<?= $catColors[$cat]??'#1B3FA0' ?>18;color:<?= $catColors[$cat]??'#1B3FA0' ?>;padding:2px 9px;border-radius:12px;font-size:.67rem;font-weight:700;border:1px solid <?= $catColors[$cat]??'#1B3FA0' ?>33;">
                <i class="<?= $catIcons[$cat]??'fas fa-book' ?>"></i> <?= $cat ?> (<?= count($catGroups[$cat]) ?>)
              </span>
              <?php endforeach; ?>
            </div>
          </div>
          <div style="display:flex;gap:8px;flex-shrink:0;flex-wrap:wrap;">
            <a href="/universities.php?id=<?= $u['id'] ?>" class="btn btn-sm" style="border-radius:50px;<?= $isBOSSE?'background:rgba(255,255,255,.15);border:1px solid rgba(255,255,255,.3);color:#fff;':'' ?>"><i class="fas fa-eye"></i> View All</a>
            <a href="/college-finder.php" class="btn btn-primary btn-sm" style="border-radius:50px;<?= $isBOSSE?'background:#f5a623;border-color:#f5a623;color:#071535;':'' ?>"><i class="fas fa-paper-plane"></i> Enquire</a>
          </div>
        </div>
      </div>

      <!-- Course Preview — Accordion per category -->
      <div style="padding:0 20px 16px;">
        <?php if ($isBOSSE): ?>
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:8px;margin-top:14px;">
          <?php
          $bosseStreams = [
            ['icon'=>'fas fa-school','label'=>'SSLC (Class 10)','subjects'=>'Kannada · English · Maths · Science · Social Science','elig'=>'Up to Class 9'],
            ['icon'=>'fas fa-flask','label'=>'2nd PUC — Science','subjects'=>'Kannada · English · Maths · Physics · Chemistry · Bio/CS','elig'=>'10th Pass'],
            ['icon'=>'fas fa-briefcase','label'=>'2nd PUC — Commerce','subjects'=>'Kannada · English · Business · Accounts · CS','elig'=>'10th Pass'],
            ['icon'=>'fas fa-palette','label'=>'2nd PUC — Arts','subjects'=>'Kannada · English · History · Economics · Political Sci','elig'=>'10th Pass'],
          ];
          foreach ($bosseStreams as $bs): ?>
          <div style="background:#f0f4ff;border:1px solid #bfdbfe;border-radius:10px;padding:12px 14px;">
            <div style="font-weight:700;font-size:.8rem;color:#1e3a8a;margin-bottom:4px;"><i class="<?= $bs['icon'] ?>" style="color:#1B3FA0;margin-right:4px;"></i><?= $bs['label'] ?></div>
            <div style="font-size:.7rem;color:#475569;line-height:1.6;"><?= $bs['subjects'] ?></div>
            <div style="font-size:.67rem;color:#64748b;margin-top:4px;"><i class="fas fa-check-circle" style="color:green;"></i> <?= $bs['elig'] ?></div>
          </div>
          <?php endforeach; ?>
        </div>

        <?php else: ?>
        <!-- Accordion for each category in listing view -->
        <?php foreach ($catGroups as $cat => $list):
          $accId    = 'acc-'.$u['id'].'-'.preg_replace('/\W/','',strtolower($cat));
          $accent   = $catColors[$cat] ?? '#1B3FA0';
          $icon     = $catIcons[$cat]  ?? 'fas fa-book';
          $isFirst  = $cat === array_key_first($catGroups);
        ?>
        <div style="margin-top:10px;border:1.5px solid <?= $accent ?>22;border-radius:10px;overflow:hidden;">
          <div class="acc-header" onclick="toggleAccordion(this)"
            style="display:flex;align-items:center;justify-content:space-between;padding:10px 14px;cursor:pointer;background:<?= $accent ?>08;">
            <div style="display:flex;align-items:center;gap:8px;">
              <span style="width:26px;height:26px;background:<?= $accent ?>;border-radius:6px;display:flex;align-items:center;justify-content:center;flex-shrink:0;">
                <i class="<?= $icon ?>" style="color:#fff;font-size:.7rem;"></i>
              </span>
              <span style="font-weight:700;font-size:.82rem;color:var(--black);"><?= $cat ?> Programs</span>
              <span style="font-size:.7rem;color:var(--text-light);">(<?= count($list) ?>)</span>
            </div>
            <i class="fas fa-chevron-down acc-icon" style="color:<?= $accent ?>;font-size:.75rem;transition:transform .3s;<?= $isFirst?'':'transform:rotate(-90deg)' ?>"></i>
          </div>
          <div class="acc-body" style="<?= $isFirst?'display:block':'display:none' ?>">
            <div style="padding:10px 12px;display:grid;grid-template-columns:repeat(auto-fill,minmax(240px,1fr));gap:6px;">
              <?php foreach (array_slice($list,0,12) as $c): ?>
              <div style="background:#f8fafc;border:1px solid var(--border);border-radius:8px;padding:10px 12px;">
                <div style="font-weight:700;font-size:.82rem;color:var(--black);"><?= htmlspecialchars($c['name']) ?><?= !empty($c['short_name'])?' <span style="color:'.$accent.';font-size:.7rem;">('.$c['short_name'].')</span>':'' ?></div>
                <div style="display:flex;gap:10px;margin-top:4px;flex-wrap:wrap;">
                  <?php if (!empty($c['duration'])): ?><span style="font-size:.68rem;color:var(--text-light);"><i class="fas fa-clock" style="color:<?= $accent ?>;"></i> <?= htmlspecialchars($c['duration']) ?></span><?php endif; ?>
                  <?php if (!empty($c['fees_per_year'])): ?><span style="font-size:.68rem;color:var(--text-light);"><i class="fas fa-rupee-sign" style="color:<?= $accent ?>;"></i> <?= formatFee($c['fees_per_year']) ?>/yr</span><?php endif; ?>
                </div>
              </div>
              <?php endforeach; ?>
              <?php if (count($list) > 12): ?>
              <div style="grid-column:1/-1;text-align:center;padding:8px;">
                <a href="/universities.php?id=<?= $u['id'] ?>" style="font-size:.78rem;color:<?= $accent ?>;font-weight:700;">+ <?= count($list)-12 ?> more programs — View All <i class="fas fa-arrow-right"></i></a>
              </div>
              <?php endif; ?>
            </div>
          </div>
        </div>
        <?php endforeach; ?>
        <?php endif; ?>
      </div>
    </div>
    <?php endforeach; ?>
    <?php endif; ?>
  </div>
</section>

<?php foreach ($custom as $cs): renderCustomSection($cs); endforeach; ?>
<?php endif; ?>

<!-- CTA -->
<section style="background:var(--grad);padding:70px 0;">
  <div class="container" style="text-align:center;">
    <h2 style="color:#fff;margin-bottom:12px;font-size:clamp(1.5rem,3vw,2rem);">Need Help Choosing the Right Program?</h2>
    <p style="color:rgba(255,255,255,.8);margin-bottom:28px;">Our counselors will help you find the best-fit university and course — for free.</p>
    <a href="tel:9741128999" class="btn btn-white btn-lg"><i class="fas fa-phone"></i> Call Now for Free Counseling</a>
  </div>
</section>

<style>
/* Filter Buttons */
.filter-btn { display:inline-flex;align-items:center;padding:7px 20px;border-radius:50px;border:1.5px solid var(--border);background:#fff;font-size:.8rem;font-weight:700;cursor:pointer;transition:all .2s;color:var(--text-mid);text-decoration:none; }
.filter-btn.active,.filter-btn:hover { background:var(--primary);border-color:var(--primary);color:#fff; }

/* Accordion */
.acc-header:hover { filter:brightness(.97); }
.acc-body { overflow:hidden;transition:all .3s ease; }
.acc-icon.open { transform:rotate(180deg)!important; }

/* Listing head */
.uni-listing-head { border-bottom:1px solid var(--border); }

/* Responsive */
@media(max-width:900px){
  .uni-detail-grid { grid-template-columns:1fr!important; }
  .uni-detail-grid > div:first-child { position:static!important; }
}
@media(max-width:600px){
  .uni-listing-head > div { flex-direction:column;align-items:flex-start!important; }
  .uni-listing-head > div > div:last-child { margin-left:0!important; }
}
</style>

<script>
// Accordion toggle
function toggleAccordion(header) {
  const body = header.nextElementSibling;
  const icon = header.querySelector('.acc-icon');
  if (body.style.display === 'none') {
    body.style.display = 'block';
    icon.style.transform = 'rotate(180deg)';
  } else {
    body.style.display = 'none';
    icon.style.transform = 'rotate(-90deg)';
  }
}

// Jump to section (detail page tabs)
function jumpToSection(id) {
  const el = document.getElementById(id);
  if (el) {
    el.scrollIntoView({ behavior: 'smooth', block: 'start' });
    // Open accordion if closed
    const body = el.querySelector('.acc-body');
    const icon = el.querySelector('.acc-icon');
    if (body && body.style.display === 'none') {
      body.style.display = 'block';
      if (icon) icon.style.transform = 'rotate(180deg)';
    }
  }
}

// Filter courses inside accordion
function filterCourses(input, sectionId) {
  const val = input.value.toLowerCase();
  const section = document.getElementById(sectionId);
  if (!section) return;
  section.querySelectorAll('.course-row').forEach(row => {
    row.style.display = row.textContent.toLowerCase().includes(val) ? '' : 'none';
  });
}
</script>

<?php include __DIR__ . '/includes/footer.php'; ?>