@php
    $isFrontRoute = fn (...$patterns) => request()->routeIs(...$patterns);
    $isRouteGroupActive = fn (array $patterns) => $isFrontRoute(...$patterns);
    $currentPageSlug = (int) request()->route('slug', 0);
    $examLinks = [
        [
            'route' => 'competitionlist',
            'label' => 'Home',
            'icon' => 'fa-home',
            'helper' => 'Return to dashboard or contest flow',
            'active' => $isRouteGroupActive(['competitionlist', 'contestdetails', 'competitionjoin', 'examstart', 'examslive']),
        ],
        [
            'route' => 'gamehistory',
            'label' => 'Exam History',
            'icon' => 'fa-list',
            'helper' => 'Check previous joined exams',
            'active' => $isRouteGroupActive(['gamehistory']),
        ],
        [
            'route' => 'scoringresult',
            'label' => 'Scoring Result',
            'icon' => 'fa-trophy',
            'helper' => 'See score and progress details',
            'active' => $isRouteGroupActive(['scoringresult']),
        ],
        [
            'route' => 'scholershipresult',
            'label' => 'Scholarship Results',
            'icon' => 'fa-graduation-cap',
            'helper' => 'Published ranking and final result',
            'active' => $isRouteGroupActive(['scholershipresult', 'myexamresultdetails', 'examsresultt', 'examsanswersheet']),
        ],
    ];
    $accountLinks = [
        [
            'route' => 'walletrecharge',
            'label' => 'Add Money',
            'icon' => 'fa-credit-card',
            'helper' => 'Recharge before the next exam',
            'active' => $isRouteGroupActive(['walletrecharge', 'walletamount']),
        ],
        [
            'route' => 'wallethistory',
            'label' => 'Wallet History',
            'icon' => 'fa-history',
            'helper' => 'See recharge and wallet movement',
            'active' => $isRouteGroupActive(['wallethistory']),
        ],
        [
            'route' => 'userwithdraw',
            'label' => 'Withdraw',
            'icon' => 'fa-money',
            'helper' => 'Request winning reward payout',
            'active' => $isRouteGroupActive(['userwithdraw', 'withdrawhistory']),
        ],
        [
            'route' => 'editprofile',
            'label' => 'Edit Profile',
            'icon' => 'fa-user',
            'helper' => 'Update student profile details',
            'active' => $isRouteGroupActive(['editprofile', 'profileimg', 'mobileotp', 'emailvarification']),
        ],
        [
            'route' => 'userpasswordchange',
            'label' => 'Change Password',
            'icon' => 'fa-lock',
            'helper' => 'Secure your account access',
            'active' => $isRouteGroupActive(['userpasswordchange']),
        ],
    ];
    $policyLinks = [
        ['slug' => 8, 'label' => 'Terms & Conditions', 'icon' => 'fa-file-text-o'],
        ['slug' => 9, 'label' => 'Privacy Policy', 'icon' => 'fa-shield'],
        ['slug' => 5, 'label' => 'Legality Policy', 'icon' => 'fa-balance-scale'],
        ['slug' => 10, 'label' => 'Refund Policy', 'icon' => 'fa-refresh'],
        ['slug' => 11, 'label' => 'Cancellation Policy', 'icon' => 'fa-ban'],
        ['slug' => 6, 'label' => 'Contact Us', 'icon' => 'fa-phone'],
    ];
@endphp
<header class="main-header">
    <div class="container jel-front-container">
        <div class="row align-items-center g-3">
            <div class="col-7 col-md-6 d-flex align-items-center gap-3">
                <a id="slide" class="menu-icon" href="javascript:void(0)" aria-label="Open navigation" aria-expanded="false" aria-controls="menhupartsectipon">
                    <i class="fa fa-bars"></i>
                </a>
                <a class="site-logo" href="{{ route('competitionlist') }}">
                    <img src="{{ asset('public/frontend/images/logo.png') }}" alt="Just Exam League">
                    <span class="site-logo-text d-none d-sm-inline">Just Exam League</span>
                </a>
            </div>
            <div class="col-5 col-md-6">
                <div class="header-right">
                    <span class="exam-header-badge">
                        <i class="fa fa-pencil-square-o"></i>
                        <span>Exam Mode</span>
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>
<div class="sidebar-overlay" id="sidebarOverlay"></div>
<div class="sidebar-menu" id="menhupartsectipon">
    <div class="sidebar-header">
        <div class="profile-section">
            <img src="{{ $portalProfileImageUrl }}" alt="{{ $portalStudent?->name }}">
            <div class="profile-info">
                <small class="profile-label">Live Exam</small>
                <span><a href="{{ route('editprofile') }}">{{ $portalStudent?->name ?? 'Student Portal' }}</a></span>
                <p>Mobile No.: {{ $portalStudent?->mobileno ?? 'Not available' }}</p>
            </div>
        </div>
        <button class="close-sidebar" id="closeSidebar" type="button" aria-label="Close menu">
            <i class="fa fa-times"></i>
        </button>
    </div>
    <div class="sidebar-body">
        <div class="sidebar-section-title">Exam Zone</div>
        <ul>
            @foreach($examLinks as $link)
                <li class="{{ $link['active'] ? 'active' : '' }}">
                    <a href="{{ route($link['route']) }}">
                        <i class="fa {{ $link['icon'] }}"></i>
                        <span class="sidebar-link-copy">
                            <strong>{{ $link['label'] }}</strong>
                            <small>{{ $link['helper'] }}</small>
                        </span>
                    </a>
                </li>
            @endforeach
        </ul>

        <div class="sidebar-section-title">Wallet & Account</div>
        <ul>
            @foreach($accountLinks as $link)
                <li class="{{ $link['active'] ? 'active' : '' }}">
                    <a href="{{ route($link['route']) }}">
                        <i class="fa {{ $link['icon'] }}"></i>
                        <span class="sidebar-link-copy">
                            <strong>{{ $link['label'] }}</strong>
                            <small>{{ $link['helper'] }}</small>
                        </span>
                    </a>
                </li>
            @endforeach
        </ul>

        <div class="sidebar-section-title">Help & Policies</div>
        <ul>
            @foreach($policyLinks as $link)
                <li class="{{ $isFrontRoute('pagesdetails') && $currentPageSlug === $link['slug'] ? 'active' : '' }}">
                    <a href="{{ route('pagesdetails', ['slug' => $link['slug']]) }}">
                        <i class="fa {{ $link['icon'] }}"></i>
                        <span class="sidebar-link-copy">
                            <strong>{{ $link['label'] }}</strong>
                        </span>
                    </a>
                </li>
            @endforeach
        </ul>

        <div class="sidebar-footer">
            <a class="sidebar-logout-link" href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
                <i class="fa fa-sign-out"></i>
                <span>Logout</span>
            </a>
            <form id="logout-form" action="{{ route('studentslogout') }}" method="POST" class="d-none">
                @csrf
            </form>
        </div>
    </div>
</div>
