<aside class="portal-sidebar" id="portalSidebar" aria-label="Main navigation">
    <div class="portal-sidebar-brand">
        <a href="{{ default_home_url() }}" class="portal-sidebar-logo">
            <span class="portal-sidebar-logo-mark">C</span>
            <span class="portal-sidebar-logo-text">CRM</span>
        </a>
        <button type="button" class="portal-sidebar-close d-lg-none" id="portalSidebarClose" aria-label="Close menu">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6L6 18M6 6l12 12"/></svg>
        </button>
    </div>

    <nav class="portal-sidebar-nav">
        <div class="portal-sidebar-section">
            <span class="portal-sidebar-label">Overview</span>
            @if(can_access_module('leads.dashboard'))
                <a href="{{ route('leads.dashboard') }}"
                   class="portal-sidebar-link {{ request()->routeIs('leads.dashboard') ? 'active' : '' }}">
                    <span class="portal-sidebar-icon" aria-hidden="true">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/></svg>
                    </span>
                    Dashboard
                </a>
            @endif
        </div>

        <div class="portal-sidebar-section">
            <span class="portal-sidebar-label">Leads</span>
            @if(can_access_module('leads.listing'))
                <a href="{{ route('leads.listing') }}"
                   class="portal-sidebar-link {{ request()->routeIs('leads.listing', 'leads.show', 'leads.edit') && !request()->routeIs('leads.my-tasks', 'leads.design-tasks') ? 'active' : '' }}">
                    <span class="portal-sidebar-icon" aria-hidden="true">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01"/></svg>
                    </span>
                    Lead listing
                </a>
            @endif
            @if(can_access_module('leads.my_tasks'))
                <a href="{{ route('leads.my-tasks') }}"
                   class="portal-sidebar-link {{ request()->routeIs('leads.my-tasks') ? 'active' : '' }}">
                    <span class="portal-sidebar-icon" aria-hidden="true">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11"/></svg>
                    </span>
                    My tasks
                </a>
            @endif
            @if(can_access_module('leads.design_tasks') || can_access_module('leads.listing') || can_access_module('leads.dashboard'))
                <a href="{{ route('leads.design-tasks') }}"
                   class="portal-sidebar-link {{ request()->routeIs('leads.design-tasks') ? 'active' : '' }}">
                    <span class="portal-sidebar-icon" aria-hidden="true">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"/></svg>
                    </span>
                    {{ can_access_module('leads.design_tasks') && !can_access_module('leads.listing') ? 'My design tasks' : 'Design tracking' }}
                </a>
            @endif
            @if(can_access_module('leads.create'))
                <a href="{{ route('leads.create') }}"
                   class="portal-sidebar-link {{ request()->routeIs('leads.create') ? 'active' : '' }}">
                    <span class="portal-sidebar-icon" aria-hidden="true">
                        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 5v14M5 12h14"/></svg>
                    </span>
                    Add lead
                </a>
            @endif
        </div>

        @if(can_access_module('users.manage') || can_access_module('masters.roles') || can_access_module('masters.departments'))
            <div class="portal-sidebar-section">
                <button class="portal-sidebar-group-toggle {{ request()->routeIs('users.*', 'roles.*', 'departments.master.*') ? '' : 'collapsed' }}"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target="#sidebarAdmin"
                        aria-expanded="{{ request()->routeIs('users.*', 'roles.*', 'departments.master.*') ? 'true' : 'false' }}">
                    <span class="portal-sidebar-label mb-0">Administration</span>
                    <svg class="portal-sidebar-chevron" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
                </button>
                <div class="collapse {{ request()->routeIs('users.*', 'roles.*', 'departments.master.*') ? 'show' : '' }}" id="sidebarAdmin">
                    <div class="portal-sidebar-subnav">
                        @if(can_access_module('users.manage'))
                            <a href="{{ route('users.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('users.*') ? 'active' : '' }}">Users</a>
                        @endif
                        @if(can_access_module('masters.roles'))
                            <a href="{{ route('roles.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('roles.*') ? 'active' : '' }}">Roles</a>
                        @endif
                        @if(can_access_module('masters.departments'))
                            <a href="{{ route('departments.master.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('departments.master.*') ? 'active' : '' }}">Departments</a>
                        @endif
                    </div>
                </div>
            </div>
        @endif

        @if(can_access_module('geography.continents') || can_access_module('geography.countries') || can_access_module('geography.cities') || can_access_module('tradefairs.manage'))
            <div class="portal-sidebar-section">
                <button class="portal-sidebar-group-toggle {{ request()->routeIs('continents.*', 'countries.*', 'cities.*', 'tradefairs.*') ? '' : 'collapsed' }}"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target="#sidebarLocations"
                        aria-expanded="{{ request()->routeIs('continents.*', 'countries.*', 'cities.*', 'tradefairs.*') ? 'true' : 'false' }}">
                    <span class="portal-sidebar-label mb-0">Locations</span>
                    <svg class="portal-sidebar-chevron" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
                </button>
                <div class="collapse {{ request()->routeIs('continents.*', 'countries.*', 'cities.*', 'tradefairs.*') ? 'show' : '' }}" id="sidebarLocations">
                    <div class="portal-sidebar-subnav">
                        @if(can_access_module('geography.continents'))
                            <a href="{{ route('continents.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('continents.*') ? 'active' : '' }}">Continents</a>
                        @endif
                        @if(can_access_module('geography.countries'))
                            <a href="{{ route('countries.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('countries.*') ? 'active' : '' }}">Countries</a>
                        @endif
                        @if(can_access_module('geography.cities'))
                            <a href="{{ route('cities.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('cities.*') ? 'active' : '' }}">Cities</a>
                        @endif
                        @if(can_access_module('tradefairs.manage'))
                            <a href="{{ route('tradefairs.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('tradefairs.*') ? 'active' : '' }}">Trade fairs</a>
                        @endif
                    </div>
                </div>
            </div>
        @endif

        @if(auth()->user()->isPlatformAdmin())
            <div class="portal-sidebar-section">
                <button class="portal-sidebar-group-toggle {{ request()->routeIs('visibility.*', 'visibility-modules.*') ? '' : 'collapsed' }}"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target="#sidebarAccess"
                        aria-expanded="{{ request()->routeIs('visibility.*', 'visibility-modules.*') ? 'true' : 'false' }}">
                    <span class="portal-sidebar-label mb-0">Access control</span>
                    <svg class="portal-sidebar-chevron" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
                </button>
                <div class="collapse {{ request()->routeIs('visibility.*', 'visibility-modules.*') ? 'show' : '' }}" id="sidebarAccess">
                    <div class="portal-sidebar-subnav">
                        <a href="{{ route('visibility.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('visibility.index', 'visibility.departments.*', 'visibility.users.*') ? 'active' : '' }}">Visibility manager</a>
                        <a href="{{ route('visibility-modules.index') }}" class="portal-sidebar-sublink {{ request()->routeIs('visibility-modules.*') ? 'active' : '' }}">Permission modules</a>
                    </div>
                </div>
            </div>
        @endif
    </nav>

    <div class="portal-sidebar-footer d-none d-lg-block">
        <div class="portal-sidebar-user-mini">
            <span class="portal-sidebar-user-avatar">{{ strtoupper(substr(auth()->user()->name, 0, 1)) }}</span>
            <div class="portal-sidebar-user-meta">
                <span class="portal-sidebar-user-name">{{ auth()->user()->name }}</span>
                <span class="portal-sidebar-user-role">{{ optional(auth()->user()->department)->name ?: 'Staff' }}</span>
            </div>
        </div>
    </div>
</aside>
