@php
    $isMobile = $mobile ?? false;
    $currentUser = auth()->user();
    $initials = collect(preg_split('/\s+/', trim($currentUser?->name ?? 'User')))->filter()->take(2)->map(fn ($part) => mb_strtoupper(mb_substr($part, 0, 1)))->implode('');
    $moduleManager = app(\App\Services\ModuleManager::class);
    $fixedAssetsEnabled = $moduleManager->enabled('fixed_assets');
    $financialReportsEnabled = $moduleManager->enabled('financial_reports');
    $tasksEnabled = $moduleManager->enabled('tasks');
    $calendarEnabled = $moduleManager->enabled('calendar');
    $loanWriteoffsEnabled = $moduleManager->enabled('loan_writeoffs');
    $payrollEnabled = $moduleManager->enabled('payroll');
    $remotePaymentsEnabled = $moduleManager->enabled('remote_payments');
@endphp
<div class="{{ $isMobile ? 'flex h-full flex-col' : 'mf-sidebar fixed top-0 z-40 hidden h-screen flex-col bg-sidebar lg:flex' }} border-e border-sidebar-border">
<div class="flex h-16 shrink-0 items-center gap-3 border-b border-sidebar-border px-4"><div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground">MF</div><div class="min-w-0 flex-1"><div class="truncate text-sm font-bold tracking-tight text-sidebar-foreground">{{ __('ui.microfinance') }}</div><div class="text-[10px] font-medium uppercase tracking-widest text-sidebar-foreground/40">{{ __('ui.management') }}</div></div>@if($isMobile)<button type="button" data-close-sidebar class="mf-btn mf-icon-btn border-0 bg-transparent lg:hidden" aria-label="{{ __('ui.close_navigation') }}">×</button>@endif</div>
<nav class="flex-1 overflow-y-auto px-3 py-4" aria-label="Primary navigation">
@if($currentUser?->can('dashboard.view'))<div class="mb-4"><div class="mf-nav-label">{{ __('ui.overview') }}</div><a href="{{ route('dashboard') }}" class="mf-nav-item {{ request()->routeIs('dashboard') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.dashboard') }}</span></a></div>@endif
@if($currentUser?->can('customers.view') || $currentUser?->can('loan-products.view') || $currentUser?->can('loans.view') || $currentUser?->can('loans.approve') || $currentUser?->can('loans.top-up.view') || $currentUser?->can('loans.adjustments.view') || ($loanWriteoffsEnabled && $currentUser?->can('loans.writeoff.view')) || $currentUser?->can('collateral.view') || $currentUser?->can('notifications.view') || $currentUser?->can('arrears.view'))
<div class="mb-4"><div class="mf-nav-label">{{ __('ui.operations') }}</div>
@if($currentUser?->can('customers.view'))<a href="{{ route('customers.index') }}" class="mf-nav-item {{ request()->routeIs('customers.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.customers') }}</span></a>@endif
@if($currentUser?->can('loan-products.view'))<a href="{{ route('loan-products.index') }}" class="mf-nav-item {{ request()->routeIs('loan-products.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_products') }}</span></a>@endif
@if($currentUser?->can('loans.view'))<a href="{{ route('loans.index') }}" class="mf-nav-item {{ request()->routeIs('loans.index','loans.create','loans.show','loans.edit') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_applications') }}</span></a>@endif
@if($currentUser?->can('loans.approve'))<a href="{{ route('loans.approvals.index') }}" class="mf-nav-item {{ request()->routeIs('loans.approvals.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_approvals') }}</span></a>@endif
@if($currentUser?->can('loans.top-up.view'))<a href="{{ route('loan-top-ups.index') }}" class="mf-nav-item {{ request()->routeIs('loan-top-ups.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_top_ups') }}</span></a>@endif
@if($currentUser?->can('loans.adjustments.view'))<a href="{{ route('loan-adjustments.index') }}" class="mf-nav-item {{ request()->routeIs('loan-adjustments.*','loan-early-settlements.*','loan-restructurings.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_adjustments') }}</span></a>@endif
@if($loanWriteoffsEnabled && $currentUser?->can('loans.writeoff.view'))<a href="{{ route('loan-write-offs.index') }}" class="mf-nav-item {{ request()->routeIs('loan-write-offs.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_write_offs') }}</span></a>@endif
@if($currentUser?->can('arrears.view'))<a href="{{ route('arrears.index') }}" class="mf-nav-item {{ request()->routeIs('arrears.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.arrears_collections') }}</span></a>@endif
@if($currentUser?->can('collateral.view'))<a href="{{ route('collateral.index') }}" class="mf-nav-item {{ request()->routeIs('collateral.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.collateral') }}</span></a>@endif
@if($currentUser?->can('notifications.view'))<a href="{{ route('notifications.index') }}" class="mf-nav-item {{ request()->routeIs('notifications.*','notification-settings.*','notification-templates.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.reminders_notifications') }}</span></a>@endif
</div>@endif
@if(($tasksEnabled && $currentUser?->can('tasks.view')) || ($calendarEnabled && $currentUser?->can('calendar.view')) || $currentUser?->can('management-alerts.view'))
<div class="mb-4"><div class="mf-nav-label">{{ __('ui.work') }}</div>
@if($tasksEnabled && $currentUser?->can('tasks.view'))<a href="{{ route('tasks.index') }}" class="mf-nav-item {{ request()->routeIs('tasks.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.tasks') }}</span></a>@endif
@if($calendarEnabled && $currentUser?->can('calendar.view'))<a href="{{ route('calendar.index') }}" class="mf-nav-item {{ request()->routeIs('calendar.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.calendar') }}</span></a>@endif
@if($currentUser?->can('management-alerts.view'))<a href="{{ route('management-alerts.index') }}" class="mf-nav-item {{ request()->routeIs('management-alerts.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.management_alerts') }}</span></a>@endif
</div>@endif
@if($currentUser?->can('accounts.view') || $currentUser?->can('accounting.view') || $currentUser?->can('loans.disburse') || $currentUser?->can('payments.receive') || ($remotePaymentsEnabled && $currentUser?->can('payments.remote.submit')) || $currentUser?->can('expenses.view') || ($payrollEnabled && $currentUser?->can('payroll.view')) || $currentUser?->can('cash-closing.view') || ($financialReportsEnabled && $currentUser?->can('reports.view')) || ($fixedAssetsEnabled && $currentUser?->can('fixed-assets.view')))
<div class="mb-4"><div class="mf-nav-label">{{ __('ui.finance') }}</div>
@if($currentUser?->can('loans.disburse'))<a href="{{ route('loan-disbursements.index') }}" class="mf-nav-item {{ request()->routeIs('loan-disbursements.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_disbursement') }}</span></a>@endif
@if($currentUser?->can('payments.receive'))<a href="{{ route('loan-payments.index') }}" class="mf-nav-item {{ request()->routeIs('loan-payments.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_payments') }}</span></a>@endif
@if($remotePaymentsEnabled && $currentUser?->can('payments.remote.submit'))<a href="{{ route('remote-payments.index') }}" class="mf-nav-item {{ request()->routeIs('remote-payments.index','remote-payments.create','remote-payments.show') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.remote_payments') }}</span></a>@endif
@if($currentUser?->can('expenses.view'))<a href="{{ route('expenses.index') }}" class="mf-nav-item {{ request()->routeIs('expenses.*','expense-categories.*','recurring-expenses.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.expenses') }}</span></a>@endif
@if($payrollEnabled && $currentUser?->can('payroll.view'))<a href="{{ route('payroll.index') }}" class="mf-nav-item {{ request()->routeIs('payroll.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.payroll') }}</span></a>@endif
@if($fixedAssetsEnabled && $currentUser?->can('fixed-assets.view'))<a href="{{ route('fixed-assets.index') }}" class="mf-nav-item {{ request()->routeIs('fixed-assets.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.fixed_assets') }}</span></a>@endif
@if($currentUser?->can('cash-closing.view'))<a href="{{ route('cash-closing.index') }}" class="mf-nav-item {{ request()->routeIs('cash-closing.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.cash_closing') }}</span></a>@endif
@if($financialReportsEnabled && $currentUser?->can('reports.view'))<a href="{{ route('reports.index') }}" class="mf-nav-item {{ request()->routeIs('reports.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.financial_reports') }}</span></a><a href="{{ route('loan-branch-reports.index') }}" class="mf-nav-item {{ request()->routeIs('loan-branch-reports.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.loan_branch_reports') }}</span></a>@endif
@if($currentUser?->can('accounts.view'))<a href="{{ route('accounts.index') }}" class="mf-nav-item {{ request()->routeIs('accounts.*','capital-injections.*','account-transfers.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.financial_accounts') }}</span></a>@endif
@if($currentUser?->can('accounting.view'))<a href="{{ route('accounting-ledger.index') }}" class="mf-nav-item {{ request()->routeIs('accounting-ledger.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.accounting_ledger') }}</span></a>@endif
</div>@endif
@if($currentUser?->can('branches.view') || $currentUser?->can('users.view') || $currentUser?->can('roles.view'))<div class="mb-4"><div class="mf-nav-label">{{ __('ui.organization') }}</div>@if($currentUser?->can('branches.view'))<a href="{{ route('admin.branches.index') }}" class="mf-nav-item {{ request()->routeIs('admin.branches.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.branches') }}</span></a>@endif @if($currentUser?->can('users.view'))<a href="{{ route('admin.users.index') }}" class="mf-nav-item {{ request()->routeIs('admin.users.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.staff') }}</span></a>@endif @if($currentUser?->can('roles.view'))<a href="{{ route('admin.roles.index') }}" class="mf-nav-item {{ request()->routeIs('admin.roles.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.roles_permissions') }}</span></a>@endif</div>@endif
@if($currentUser?->can('business.settings.view') || $currentUser?->can('system.settings.manage') || $currentUser?->can('audit.view'))
<div class="mb-4">
<div class="mf-nav-label">{{ __('ui.administration') }}</div>
@if($currentUser?->can('business.settings.view'))
<a href="{{ route('admin.business-settings.edit') }}" class="mf-nav-item {{ request()->routeIs('admin.business-settings.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.business_settings') }}</span></a>
<a href="{{ route('admin.contract-templates.index') }}" class="mf-nav-item {{ request()->routeIs('admin.contract-templates.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.contract_templates') }}</span></a>
@endif
@if($currentUser?->can('system.settings.manage'))
<a href="{{ route('admin.system-settings.edit') }}" class="mf-nav-item {{ request()->routeIs('admin.system-settings.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.system_settings') }}</span></a>
<a href="{{ route('admin.system-backups.index') }}" class="mf-nav-item {{ request()->routeIs('admin.system-backups.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.system_backups') }}</span></a>
<a href="{{ route('admin.laravel-logs.index') }}" class="mf-nav-item {{ request()->routeIs('admin.laravel-logs.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.laravel_logs') }}</span></a>
@if($currentUser?->hasRole('super-administrator'))
<a href="{{ route('admin.system-reset.index') }}" class="mf-nav-item {{ request()->routeIs('admin.system-reset.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.system_reset') }}</span></a>
@endif
<a href="{{ route('admin.daily-reports.index') }}" class="mf-nav-item {{ request()->routeIs('admin.daily-reports.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.daily_branch_reports') }}</span></a>
<a href="{{ route('admin.languages.index') }}" class="mf-nav-item {{ request()->routeIs('admin.languages.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.languages') }}</span></a>
@endif
@if($currentUser?->can('audit.view'))
<a href="{{ route('admin.audit.index') }}" class="mf-nav-item {{ request()->routeIs('admin.audit.*') ? 'is-active' : '' }}"><span class="flex-1">{{ __('ui.audit_log') }}</span></a>
@endif
</div>
@endif
</nav>
<div class="border-t border-sidebar-border p-3"><div class="flex items-center gap-3 rounded-lg px-2 py-2"><div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-sidebar-accent text-xs font-semibold text-sidebar-foreground">{{ $initials ?: 'U' }}</div><div class="min-w-0 flex-1"><div class="truncate text-xs font-semibold text-sidebar-foreground">{{ $currentUser?->name }}</div><div class="truncate text-[10px] text-sidebar-foreground/40">{{ $currentUser?->email }}</div></div><form method="POST" action="{{ route('logout') }}">@csrf<button type="submit" class="flex h-8 w-8 items-center justify-center rounded-md text-sidebar-foreground/40 hover:bg-sidebar-accent" aria-label="{{ __('ui.sign_out') }}">↪</button></form></div></div>
</div>