<?php
use Dotenv\Dotenv;

require 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$school_name = $_ENV['SCHOOL_NAME'];
$logo = $_ENV['WEBSITE_LOGO'];
$currentPage = basename($_SERVER['PHP_SELF']);
?>

<aside class="main-sidebar" id="kiroSidebar">

    <!-- Brand -->
    <div class="ks-brand">
        <a href="dashboard.php" class="ks-brand-link">
            <div class="ks-logo-ring">
                <img src="<?php echo htmlentities($logo); ?>" alt="Logo" class="ks-logo">
            </div>
            <span class="ks-school-name"><?php echo htmlentities($school_name); ?></span>
            <span class="ks-portal-label">Admin Portal</span>
        </a>
    </div>

    <!-- User Panel -->
    <div class="ks-user-panel">
        <?php
        $eid = $_SESSION['sid'];
        $sql = "SELECT * FROM tblusers WHERE id=:eid LIMIT 1";
        $query = $dbh->prepare($sql);
        $query->bindParam(':eid', $eid, PDO::PARAM_STR);
        $query->execute();
        $results = $query->fetchAll(PDO::FETCH_OBJ);
        if ($query->rowCount() > 0):
            foreach ($results as $row):
                $profileImg = !empty($row->userimage) ? "staff_images/{$row->userimage}" : "assets/default-avatar.png";
                ?>
                <a href="profile.php" class="ks-user-link">
                    <div class="ks-avatar-wrap">
                        <img src="<?php echo htmlentities($profileImg); ?>" class="ks-avatar" alt="User">
                        <span class="ks-online-dot"></span>
                    </div>
                    <div class="ks-user-info">
                        <span class="ks-user-name"><?php echo htmlentities($row->name . ' ' . $row->lastname); ?></span>
                        <span class="ks-user-role">Administrator</span>
                    </div>
                    <i class="fas fa-chevron-right ks-user-arrow"></i>
                </a>
            <?php endforeach; endif; ?>
    </div>

    <!-- Search -->
    <div class="ks-search-wrap">
        <i class="fas fa-search ks-search-icon"></i>
        <input type="search" id="sidebarSearch" class="ks-search-input" placeholder="Search modules...">
        <kbd class="ks-search-kbd">⌘K</kbd>
    </div>

    <!-- Navigation -->
    <nav class="ks-nav" id="ksNav">

        <a href="dashboard.php" class="ks-nav-item <?= $currentPage === 'dashboard.php' ? 'active' : '' ?>">
            <span class="ks-nav-icon"><i class="fas fa-tachometer-alt"></i></span>
            <span class="ks-nav-label">Dashboard</span>
            <?php if ($currentPage === 'dashboard.php'): ?><span class="ks-active-bar"></span><?php endif; ?>
        </a>

        <?php
        $groups = [
            [
                'label' => 'Student Management',
                'icon' => 'fas fa-users',
                'items' => [
                    ['href' => 'add_student.php', 'icon' => 'fas fa-user-plus', 'label' => 'Add Student'],
                    ['href' => 'registration2.php', 'icon' => 'fas fa-file-alt', 'label' => 'Registration Form'],
                    ['href' => 'student_list.php', 'icon' => 'fas fa-user-graduate', 'label' => 'Manage Students'],
                    ['href' => 'promote_students.php', 'icon' => 'fas fa-medal', 'label' => 'Promote Students'],
                    ['href' => 'demote_students.php', 'icon' => 'fas fa-user-minus', 'label' => 'Demote Students'],
                    ['href' => 'registration_token.php', 'icon' => 'fas fa-id-card', 'label' => 'Registration Token'],
                ],
            ],
            [
                'label' => 'Teacher Management',
                'icon' => 'fas fa-user-tie',
                'items' => [
                    ['href' => 'add_teacher.php', 'icon' => 'fas fa-plus', 'label' => 'Add Teacher'],
                    ['href' => 'teacher_list.php', 'icon' => 'fas fa-list', 'label' => "Teacher's List"],
                    ['href' => 'assign_teacher.php', 'icon' => 'fas fa-chalkboard-teacher', 'label' => 'Assign Teacher'],
                    ['href' => 'teacher_assignments.php', 'icon' => 'fas fa-book-reader', 'label' => 'Assignments List'],
                ],
            ],
            [
                'label' => 'Result Management',
                'icon' => 'fas fa-file-contract',
                'items' => [
                    ['href' => 'approve_result.php', 'icon' => 'fas fa-check-circle', 'label' => 'Approve Result'],
                    ['href' => 'edit_result.php', 'icon' => 'fas fa-edit', 'label' => 'Edit Result'],
                    ['href' => 'publish_result.php', 'icon' => 'fas fa-calendar-check', 'label' => 'Release Date'],
                    ['href' => 'print_broadsheet_admin.php', 'icon' => 'fas fa-print', 'label' => 'Result Broadsheet'],
                    ['href' => 'previous_result1.php', 'icon' => 'fas fa-history', 'label' => 'Previous Result'],
                    ['href' => 'bulk_result.php', 'icon' => 'fas fa-layer-group', 'label' => 'Bulk (All)'],
                    ['href' => 'bulk_result_fp.php', 'icon' => 'fas fa-filter', 'label' => 'Bulk (Paid Only)'],
                    ['href' => 'reports.php', 'icon' => 'fas fa-chart-bar', 'label' => 'Result Analytics'],
                ],
            ],
            [
                'label' => 'Payment Management',
                'icon' => 'fas fa-credit-card',
                'items' => [
                    ['href' => 'approve.php', 'icon' => 'fas fa-check-double', 'label' => 'Approve Payment'],
                    ['href' => 'viewpayment.php', 'icon' => 'fas fa-eye', 'label' => 'View Payments'],
                    ['href' => 'payment_analytics.php', 'icon' => 'fas fa-chart-line', 'label' => 'Analytics'],
                    ['href' => 'view_refunds.php', 'icon' => 'fas fa-undo', 'label' => 'Refunds'],
                ],
            ],
            [
                'label' => 'Fees Management',
                'icon' => 'fas fa-receipt',
                'items' => [
                    ['href' => 'addfees.php', 'icon' => 'fas fa-plus-circle', 'label' => 'Create Fees'],
                    ['href' => 'feelist.php', 'icon' => 'fas fa-list-alt', 'label' => 'Manage Fees'],
                ],
            ],
            [
                'label' => 'Class Management',
                'icon' => 'fas fa-chalkboard',
                'items' => [
                    ['href' => 'add_class_section.php', 'icon' => 'fas fa-plus-square', 'label' => 'Add Class & Arm'],
                    ['href' => 'manage_class_section.php', 'icon' => 'fas fa-cogs', 'label' => 'Manage Class & Arm'],
                    ['href' => 'add_subject.php', 'icon' => 'fas fa-book', 'label' => 'Manage Subject'],
                ],
            ],
            [
                'label' => 'Session Management',
                'icon' => 'fas fa-calendar',
                'items' => [
                    ['href' => 'manage_session.php', 'icon' => 'fas fa-calendar-alt', 'label' => 'Session Management'],
                    ['href' => 'calendar.php', 'icon' => 'fas fa-calendar-alt', 'label' => 'Academic Calendar'],
                ],
            ],
            [
                'label' => 'Events',
                'icon' => 'fas fa-star',
                'items' => [
                    ['href' => 'announcement.php', 'icon' => 'fas fa-bullhorn', 'label' => 'Announcement'],
                    ['href' => 'add_event.php', 'icon' => 'fas fa-calendar-plus', 'label' => 'Add Event'],
                    ['href' => 'newsletterstd.php', 'icon' => 'fas fa-users', 'label' => 'Newsletter (Groups)'],
                    ['href' => 'newslettersp.php', 'icon' => 'fas fa-user-tag', 'label' => 'Newsletter (Special)'],
                    ['href' => 'email_log.php', 'icon' => 'fas fa-envelope-open-text', 'label' => 'Email Logs'],
                ],
            ],
            [
                'label' => 'Tracking',
                'icon' => 'fas fa-truck',
                'items' => [
                    ['href' => 'admin_track_submissions.php', 'icon' => 'fas fa-route', 'label' => 'Track Submissions'],
                    ['href' => 'attendance_monitor.php', 'icon' => 'fas fa-user-check', 'label' => 'Attendance'],
                    ['href' => 'rating_monitor.php', 'icon' => 'fas fa-star', 'label' => 'Ratings'],
                    ['href' => 'approval_tracking.php', 'icon' => 'fas fa-tasks', 'label' => 'Approval Tracking'],
                    ['href' => 'missed_attendance_settings.php', 'icon' => 'fas fa-calendar-times', 'label' => 'Missed Attendance Settings'],
                    ['href' => 'missed_attendance_log_view.php', 'icon' => 'fas fa-history', 'label' => 'Missed Attendance Log'],
                ],
            ],
            [
                'label' => 'Transcripts',
                'icon' => 'fas fa-file-pdf',
                'items' => [
                    ['href' => 'transcript_test.php', 'icon' => 'fas fa-file-medical-alt', 'label' => 'Mid-Term'],
                    ['href' => 'transcript.php', 'icon' => 'fas fa-file-alt', 'label' => 'Full Transcript'],
                ],
            ],
            [
                'label' => 'Documents',
                'icon' => 'fas fa-folder-open',
                'items' => [
                    ['href' => 'sendadmissionletter.php', 'icon' => 'fas fa-envelope-open', 'label' => 'Admission Letter'],
                    ['href' => 'idcard.php', 'icon' => 'fas fa-id-card', 'label' => 'Generate ID Card'],
                    ['href' => 'add_document.php', 'icon' => 'fas fa-upload', 'label' => 'Upload Documents'],
                    ['href' => 'document_list.php', 'icon' => 'fas fa-folder-tree', 'label' => 'Manage Documents'],
                ],
            ],
            [
                'label' => 'Discounts',
                'icon' => 'fas fa-tags',
                'items' => [
                    ['href' => 'manage_discount.php', 'icon' => 'fas fa-cog', 'label' => 'Manage Discount'],
                    ['href' => 'add_discount.php', 'icon' => 'fas fa-tag', 'label' => 'Add Discount'],
                ],
            ],
        ];

        foreach ($groups as $group):
            $groupPages = array_column($group['items'], 'href');
            $isOpen = in_array($currentPage, $groupPages);
            $groupId = 'ksGroup_' . preg_replace('/\W+/', '', $group['label']);
            ?>
            <div class="ks-group <?= $isOpen ? 'open' : '' ?>" id="<?= $groupId ?>">
                <button class="ks-group-toggle accNum" onclick="ksToggleGroup('<?= $groupId ?>')">
                    <span class="ks-nav-icon"><i class="<?= $group['icon'] ?>"></i></span>
                    <span class="ks-nav-label"><?= htmlentities($group['label']) ?></span>
                    <i class="fas fa-chevron-down ks-chevron"></i>
                </button>
                <div class="ks-group-body">
                    <?php foreach ($group['items'] as $item): ?>
                        <a href="<?= $item['href'] ?>"
                            class="ks-sub-item <?= $currentPage === $item['href'] ? 'active' : '' ?>">
                            <span class="ks-sub-icon"><i class="<?= $item['icon'] ?>"></i></span>
                            <span><?= htmlentities($item['label']) ?></span>
                        </a>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>

        <!-- Standalone items -->
        <div class="ks-divider"><span>Security</span></div>

        <a href="admin_emails.php" class="ks-nav-item <?= $currentPage === 'admin_emails.php' ? 'active' : '' ?>">
            <span class="ks-nav-icon"><i class="fas fa-envelope"></i></span>
            <span class="ks-nav-label">Check Email</span>
        </a>
        <a href="userregister.php" class="ks-nav-item <?= $currentPage === 'userregister.php' ? 'active' : '' ?>">
            <span class="ks-nav-icon"><i class="fas fa-user-plus"></i></span>
            <span class="ks-nav-label">Register User</span>
        </a>
        <a href="auditlog.php" class="ks-nav-item <?= $currentPage === 'auditlog.php' ? 'active' : '' ?>">
            <span class="ks-nav-icon"><i class="fas fa-file-alt"></i></span>
            <span class="ks-nav-label">Audit Logs</span>
        </a>
        <a href="changepassword.php" class="ks-nav-item <?= $currentPage === 'changepassword.php' ? 'active' : '' ?>">
            <span class="ks-nav-icon"><i class="fas fa-cog"></i></span>
            <span class="ks-nav-label">Settings</span>
        </a>

        <div class="ks-logout-wrap">
            <a href="logout.php" class="ks-logout-btn">
                <i class="fas fa-sign-out-alt"></i>
                <span>Log Out</span>
            </a>
        </div>

    </nav>
</aside>
<style>
    /* ── Kiro Sidebar 2026 ── */
    :root {
        --ks-bg: #0d1117;
        --ks-surface: rgba(255, 255, 255, 0.06);
        --ks-border: rgba(255, 255, 255, 0.12);
        --ks-accent: #93c5fd;
        --ks-accent2: #c4b5fd;
        --ks-text: #f0f6ff;
        --ks-muted: rgba(255, 255, 255, 0.45);
        --ks-hover: rgba(255, 255, 255, 0.08);
        --ks-active-bg: rgba(255, 255, 255, 0.14);
        --ks-width: 260px;
        --ks-radius: 10px;
        --ks-transition: 0.22s cubic-bezier(.4, 0, .2, 1);
    }

    .main-sidebar {
        width: var(--ks-width);
        height: 100vh;
        background: linear-gradient(180deg, var(--dark), var(--primary)) !important;
        border-right: 1px solid var(--ks-border);
        display: flex;
        flex-direction: column;
        position: fixed;
        top: 0;
        left: 0;
        z-index: 1050;
        overflow: hidden;
        transition: margin-left var(--ks-transition), width var(--ks-transition);
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
    }

    /* AdminLTE pushmenu hides sidebar by shifting margin — let it work */
    .sidebar-collapse .main-sidebar {
        margin-left: calc(-1 * var(--ks-width));
    }

    /* ── Brand ── */
    .ks-brand {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 24px 16px 18px;
        border-bottom: 1px solid var(--ks-border);
        flex-shrink: 0;
        position: relative;
    }

    .ks-brand-link {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 10px;
        text-decoration: none !important;
        width: 100%;
    }

    .ks-logo-ring {
        width: 62px;
        height: 62px;
        border-radius: 18px;
        background: rgba(255, 255, 255, 0.12);
        border: 2px solid rgba(255, 255, 255, 0.2);
        padding: 3px;
        flex-shrink: 0;
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    }

    .ks-logo {
        width: 100%;
        height: 100%;
        border-radius: 14px;
        object-fit: cover;
        display: block;
    }

    .ks-school-name {
        color: #ffffff;
        font-weight: 700;
        font-size: 0.88rem;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 200px;
        letter-spacing: 0.01em;
    }

    .ks-portal-label {
        color: rgba(255, 255, 255, 0.45);
        font-size: 0.62rem;
        letter-spacing: 1.2px;
        text-transform: uppercase;
        margin-top: -6px;
    }

    .ks-collapse-btn {
        display: none;
        /* toggle handled by header.php pushmenu */
    }

    /* ── User Panel ── */
    .ks-user-panel {
        padding: 12px 14px;
        border-bottom: 1px solid var(--ks-border);
        flex-shrink: 0;
    }

    .ks-user-link {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 8px 10px;
        border-radius: var(--ks-radius);
        text-decoration: none !important;
        transition: background var(--ks-transition);
        overflow: hidden;
    }

    .ks-user-link:hover {
        background: var(--ks-hover);
    }

    .ks-avatar-wrap {
        position: relative;
        flex-shrink: 0;
    }

    .ks-avatar {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid var(--ks-accent);
    }

    .ks-online-dot {
        position: absolute;
        bottom: 1px;
        right: 1px;
        width: 9px;
        height: 9px;
        background: #22c55e;
        border-radius: 50%;
        border: 2px solid transparent;
    }

    .ks-user-info {
        display: flex;
        flex-direction: column;
        overflow: hidden;
        flex: 1;
    }

    .ks-user-name {
        color: var(--ks-text);
        font-size: 0.82rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .ks-user-role {
        color: var(--ks-accent);
        font-size: 0.68rem;
        font-weight: 500;
        letter-spacing: 0.3px;
    }

    .ks-user-arrow {
        color: var(--ks-muted);
        font-size: 0.65rem;
        flex-shrink: 0;
    }

    /* ── Search ── */
    .ks-search-wrap {
        position: relative;
        padding: 12px 14px;
        border-bottom: 1px solid var(--ks-border);
        flex-shrink: 0;
    }

    .ks-search-icon {
        position: absolute;
        left: 26px;
        top: 50%;
        transform: translateY(-50%);
        color: var(--ks-muted);
        font-size: 0.75rem;
        pointer-events: none;
    }

    .ks-search-input {
        width: 100%;
        background: var(--ks-surface);
        border: 1px solid var(--ks-border);
        border-radius: 8px;
        color: var(--ks-text);
        font-size: 0.82rem;
        padding: 7px 36px 7px 32px;
        outline: none;
        transition: border-color var(--ks-transition);
    }

    .ks-search-input::placeholder {
        color: var(--ks-muted);
    }

    .ks-search-input:focus {
        border-color: var(--ks-accent);
    }

    .ks-search-kbd {
        position: absolute;
        right: 22px;
        top: 50%;
        transform: translateY(-50%);
        background: rgba(255, 255, 255, 0.06);
        border: 1px solid var(--ks-border);
        border-radius: 5px;
        color: var(--ks-muted);
        font-size: 0.62rem;
        padding: 1px 5px;
        font-family: monospace;
    }

    /* ── Nav ── */
    .ks-nav {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 10px 10px 20px;
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.1) transparent;
    }

    .ks-nav::-webkit-scrollbar {
        width: 4px;
    }

    .ks-nav::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.1);
        border-radius: 4px;
    }

    /* Standalone nav item */
    .ks-nav-item {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 9px 12px;
        border-radius: var(--ks-radius);
        color: var(--ks-text);
        text-decoration: none !important;
        font-size: 0.84rem;
        font-weight: 500;
        position: relative;
        transition: background var(--ks-transition), color var(--ks-transition);
        margin-bottom: 2px;
        overflow: hidden;
    }

    .ks-nav-item:hover {
        background: var(--ks-hover);
        color: #fff;
    }

    .ks-nav-item.active {
        background: var(--ks-active-bg);
        color: var(--ks-accent);
    }

    .ks-active-bar {
        position: absolute;
        right: 0;
        top: 20%;
        bottom: 20%;
        width: 3px;
        background: var(--ks-accent);
        border-radius: 3px 0 0 3px;
    }

    /* Nav icon */
    .ks-nav-icon {
        width: 28px;
        height: 28px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 7px;
        background: rgba(255, 255, 255, 0.05);
        font-size: 0.78rem;
        flex-shrink: 0;
        transition: background var(--ks-transition);
    }

    .ks-nav-item.active .ks-nav-icon,
    .ks-group-toggle:hover .ks-nav-icon {
        background: rgba(59, 130, 246, 0.2);
        color: var(--ks-accent);
    }

    /* ── Groups ── */
    .ks-group {
        margin-bottom: 2px;
    }

    .ks-group-toggle {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 9px 12px;
        border-radius: var(--ks-radius);
        background: transparent;
        border: none;
        color: var(--ks-text);
        font-size: 0.84rem;
        font-weight: 700;
        cursor: pointer;
        text-align: left;
        transition: background var(--ks-transition);
    }

    .ks-group-toggle:hover {
        background: var(--ks-hover);
    }

    .ks-group.open .ks-group-toggle {
        color: #fff;
    }

    .ks-chevron {
        margin-left: auto;
        font-size: 0.65rem;
        color: var(--ks-muted);
        transition: transform var(--ks-transition);
    }

    .ks-group.open .ks-chevron {
        transform: rotate(180deg);
    }

    .ks-group-body {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s cubic-bezier(.4, 0, .2, 1);
        padding-left: 14px;
    }

    .ks-group.open .ks-group-body {
        max-height: 600px;
    }

    .ks-sub-item {
        display: flex;
        align-items: center;
        gap: 9px;
        padding: 7px 10px;
        border-radius: 8px;
        color: #ffffff;
        text-decoration: none !important;
        font-size: 0.81rem;
        font-weight: 400;
        transition: background var(--ks-transition), color var(--ks-transition);
        margin-bottom: 1px;
        border-left: 2px solid transparent;
    }

    .ks-sub-item:hover {
        background: var(--ks-hover);
        color: #ffffff;
    }

    .ks-sub-item.active {
        color: var(--ks-accent);
        background: var(--ks-active-bg);
        border-left-color: var(--ks-accent);
        font-weight: 400;
    }

    .ks-sub-icon {
        width: 22px;
        height: 22px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.72rem;
        flex-shrink: 0;
        opacity: 0.7;
    }

    .ks-sub-item.active .ks-sub-icon {
        opacity: 1;
    }

    /* ── Divider ── */
    .ks-divider {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 14px 12px 6px;
        color: var(--ks-muted);
        font-size: 0.65rem;
        font-weight: 700;
        letter-spacing: 1.2px;
        text-transform: uppercase;
    }

    .ks-divider::after {
        content: '';
        flex: 1;
        height: 1px;
        background: var(--ks-border);
    }

    /* ── Logout ── */
    .ks-logout-wrap {
        padding: 10px 0 4px;
    }

    .ks-logout-btn {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 9px 12px;
        border-radius: var(--ks-radius);
        color: #f87171;
        text-decoration: none !important;
        font-size: 0.84rem;
        font-weight: 500;
        transition: background var(--ks-transition);
    }

    .ks-logout-btn:hover {
        background: rgba(248, 113, 113, 0.1);
        color: #fca5a5;
    }

    /* ── Search highlight ── */
    .ks-search-hidden {
        display: none !important;
    }

    /* ── AdminLTE layout integration ── */
    body:not(.sidebar-collapse) .content-wrapper,
    body:not(.sidebar-collapse) .main-header {
        margin-left: var(--ks-width) !important;
    }

    body.sidebar-collapse .content-wrapper,
    body.sidebar-collapse .main-header {
        margin-left: 0 !important;
    }
</style>

<script>
    (function () {
        // Toggle group open/close
        window.ksToggleGroup = function (id) {
            const el = document.getElementById(id);
            if (el) el.classList.toggle('open');
        };

        // Search
        const searchInput = document.getElementById('sidebarSearch');
        if (searchInput) {
            searchInput.addEventListener('input', function () {
                const q = this.value.trim().toLowerCase();
                const groups = document.querySelectorAll('.ks-group');
                const standalones = document.querySelectorAll('.ks-nav-item');

                if (!q) {
                    groups.forEach(g => g.classList.remove('ks-search-hidden'));
                    standalones.forEach(i => i.classList.remove('ks-search-hidden'));
                    return;
                }

                groups.forEach(function (group) {
                    const items = group.querySelectorAll('.ks-sub-item');
                    let anyMatch = false;
                    items.forEach(function (item) {
                        const match = item.textContent.toLowerCase().includes(q);
                        item.classList.toggle('ks-search-hidden', !match);
                        if (match) anyMatch = true;
                    });
                    const groupLabel = group.querySelector('.ks-nav-label');
                    const groupMatch = groupLabel && groupLabel.textContent.toLowerCase().includes(q);
                    if (groupMatch) {
                        items.forEach(i => i.classList.remove('ks-search-hidden'));
                        anyMatch = true;
                    }
                    group.classList.toggle('ks-search-hidden', !anyMatch);
                    if (anyMatch) group.classList.add('open');
                });

                standalones.forEach(function (item) {
                    item.classList.toggle('ks-search-hidden', !item.textContent.toLowerCase().includes(q));
                });
            });

            // ⌘K / Ctrl+K focus
            document.addEventListener('keydown', function (e) {
                if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
                    e.preventDefault();
                    searchInput.focus();
                }
            });
        }
    })();
</script>