<ul class="navbar-nav me-auto mb-2 mb-lg-0" aria-label="Navigation principale">
    <li class="nav-group-label d-lg-none mt-2">Accueil</li>
    {% if has_permission('dashboard.view') %}
    <li class="nav-item"><a class="nav-link {% if request.path == '/' %}active{% endif %}" href="{{ url_for('dashboard.index') }}"><i class="bi bi-speedometer2"></i> Tableau de bord</a></li>
    {% endif %}

    {% set work_access = has_permission('intervention.view') or has_permission('planning.view') or has_permission('prevention.view') %}
    {% if work_access %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if '/interventions' in request.path or '/planning' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-wrench-adjustable"></i> Mon travail</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            {% if has_permission('intervention.view') %}<li><a class="dropdown-item" href="{{ url_for('interventions.index') }}"><i class="bi bi-list-ul"></i> Interventions</a></li>{% endif %}
            {% if has_permission('planning.view') %}
            <li><a class="dropdown-item fw-semibold" href="{{ url_for('planning.my_day') }}"><i class="bi bi-calendar2-week"></i> Ma journée</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.room_schedules') }}"><i class="bi bi-door-open"></i> Planning des salles</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.index') }}"><i class="bi bi-calendar-check"></i> Planning unifié</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.tasks') }}"><i class="bi bi-list-check"></i> Tâches préventives</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.scheduled') }}"><i class="bi bi-calendar-week"></i> Tâches planifiées</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.admin_tasks') }}"><i class="bi bi-clipboard"></i> Tâches administratives</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.availability') }}"><i class="bi bi-clock"></i> Disponibilités</a></li>
            <li><a class="dropdown-item" href="{{ url_for('planning.time_tracking') }}"><i class="bi bi-clock-history"></i> Horaires de travail</a></li>
            <li><a class="dropdown-item" href="{{ url_for('lots.index') }}"><i class="bi bi-layers"></i> Lots préventifs</a></li>
            {% endif %}
            {% if has_permission('prevention.view') %}<li><a class="dropdown-item" href="{{ url_for('prevention.index') }}"><i class="bi bi-shield-check"></i> Prévention</a></li>{% endif %}
        </ul>
    </li>
    {% endif %}

    {% set heritage_access = has_permission('patrimoine.view') %}
    {% if heritage_access %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if '/equipments' in request.path or '/buildings' in request.path or '/zones' in request.path or '/rooms' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-buildings"></i> Patrimoine</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            <li><a class="dropdown-item" href="{{ url_for('buildings.index') }}"><i class="bi bi-buildings"></i> Bâtiments</a></li>
            <li><a class="dropdown-item" href="{{ url_for('zones.index') }}"><i class="bi bi-diagram-3"></i> Zones</a></li>
            <li><a class="dropdown-item" href="{{ url_for('rooms.index') }}"><i class="bi bi-door-open"></i> Salles / locaux</a></li>
            <li><a class="dropdown-item" href="{{ url_for('equipments.index') }}"><i class="bi bi-pc-display"></i> Équipements</a></li>
            <li><a class="dropdown-item" href="{{ url_for('equipments.categories') }}"><i class="bi bi-tags"></i> Catégories</a></li>
            <li><a class="dropdown-item" href="{{ url_for('room_types.index') }}"><i class="bi bi-tags"></i> Types de salles</a></li>
            {% if has_permission('housing.private') %}<li><a class="dropdown-item" href="{{ url_for('housing.index') }}"><i class="bi bi-house-door"></i> Logements de fonction</a></li>{% endif %}
            <li><a class="dropdown-item" href="{{ url_for('equipments.lifecycle_dashboard') }}"><i class="bi bi-activity"></i> Cycle de vie</a></li>
        </ul>
    </li>
    {% endif %}

    {% if has_permission('stock.view') %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if '/cleaning' in request.path or '/parts' in request.path or '/meters' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-boxes"></i> Produits &amp; stocks</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            <li><a class="dropdown-item" href="{{ url_for('cleaning.products') }}"><i class="bi bi-flask"></i> Produits</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.references') }}"><i class="bi bi-upc-scan"></i> Références commerciales</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.stock') }}"><i class="bi bi-boxes"></i> État du stock</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.locations') }}"><i class="bi bi-geo-alt"></i> Emplacements</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.movements') }}"><i class="bi bi-arrow-left-right"></i> Mouvements</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.receive') }}"><i class="bi bi-box-arrow-in-down"></i> Réceptions</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.issue') }}"><i class="bi bi-box-arrow-up"></i> Sorties agents</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.transvasement_page') }}"><i class="bi bi-droplet"></i> Transvasements / dilutions</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.request_profiles') }}"><i class="bi bi-printer"></i> Fiches de demande</a></li>
            <li><a class="dropdown-item" href="{{ url_for('cleaning.documents') }}"><i class="bi bi-file-earmark-text"></i> FDS et fiches techniques</a></li>
            {% if has_permission('stock.configure') %}<li><a class="dropdown-item" href="{{ url_for('cleaning.forecast_config') }}"><i class="bi bi-graph-up-arrow"></i> Prévisions</a></li>{% endif %}
        </ul>
    </li>
    {% endif %}

    {% if has_permission('contract.view') %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if '/companies' in request.path or '/contracts' in request.path or '/services' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-building"></i> Entreprises</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            <li><a class="dropdown-item" href="{{ url_for('companies.index') }}"><i class="bi bi-building"></i> Entreprises</a></li>
            <li><a class="dropdown-item" href="{{ url_for('contracts.index') }}"><i class="bi bi-file-earmark-text"></i> Contrats</a></li>
            <li><a class="dropdown-item" href="{{ url_for('contracts.visits_dashboard') }}"><i class="bi bi-calendar2-check"></i> Passages &amp; contrôles</a></li>
            <li><a class="dropdown-item" href="{{ url_for('services.index') }}"><i class="bi bi-building-gear"></i> Services</a></li>
            <li><a class="dropdown-item" href="{{ url_for('parts.index') }}"><i class="bi bi-box-seam"></i> Pièces</a></li>
        </ul>
    </li>
    {% endif %}

    {% if has_permission('documents.view') %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if request.path.startswith('/documents') or request.path.startswith('/cleaning/documents') %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-folder2-open"></i> Documents</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            <li><a class="dropdown-item" href="{{ url_for('documents.index') }}"><i class="bi bi-search"></i> Centre documentaire</a></li>
            <li><a class="dropdown-item" href="{{ url_for('documents.fds') }}"><i class="bi bi-file-earmark-medical"></i> FDS accessibles</a></li>
        </ul>
    </li>
    {% endif %}

    {% if has_permission('system.admin') %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if '/admin/' in request.path or '/auth/users' in request.path or '/status' in request.path or '/logs' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-gear"></i> Administration</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e;">
            <li><a class="dropdown-item" href="{{ url_for('auth.list_users') }}"><i class="bi bi-people"></i> Utilisateurs</a></li>
            <li><a class="dropdown-item" href="{{ url_for('admin.roles') }}"><i class="bi bi-shield-lock"></i> Rôles et permissions</a></li>
            <li><a class="dropdown-item" href="{{ url_for('admin.permissions') }}"><i class="bi bi-key"></i> Catalogue des droits</a></li>
            <li><a class="dropdown-item" href="{{ url_for('status.index') }}"><i class="bi bi-activity"></i> État du système</a></li>
            {% if has_permission('watchdog_dnd.view') %}<li><a class="dropdown-item" href="{{ url_for('logs.index') }}"><i class="bi bi-journal-text"></i> Journaux</a></li>{% endif %}
        </ul>
    </li>
    {% endif %}

    {# L'ancien menu est une rubrique principale parallèle. Il reprend les
       groupes historiques de base.html avant la Phase 1.6, sans réorganiser
       leurs fonctions. #}
    {% if has_permission('system.admin') %}
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle {% if request.path in ['/messagerie'] or '/pronote' in request.path or '/gmao-config' in request.path %}active{% endif %}" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi bi-list-columns-reverse"></i> Ancien menu</a>
        <ul class="dropdown-menu dropdown-menu-dark" style="background:#34495e; min-width:18rem;" aria-label="Navigation historique temporaire">
            <li class="px-2"><small class="text-muted">Référence historique — temporaire</small></li>
            <li><hr class="dropdown-divider" style="border-color:rgba(255,255,255,0.15)"></li>
            <li><details class="px-2 py-1"><summary class="text-light" style="cursor:pointer"><i class="bi bi-house"></i> Principal</summary><div class="mt-1">
                <a class="dropdown-item" href="{{ url_for('dashboard.index') }}">Tableau de bord</a>
                <a class="dropdown-item" href="/messagerie">Messagerie</a>
            </div></details></li>
            <li><details class="px-2 py-1"><summary class="text-light" style="cursor:pointer"><i class="bi bi-wrench-adjustable"></i> Interventions</summary><div class="mt-1">
                        <a class="dropdown-item" href="{{ url_for('interventions.index') }}">Toutes les interventions</a>
                        <a class="dropdown-item" href="{{ url_for('planning.index') }}">Planning unifié</a>
                        <a class="dropdown-item" href="{{ url_for('planning.room_schedules') }}">Planning des salles</a>
                        <a class="dropdown-item" href="{{ url_for('planning.tasks') }}">Tâches préventives</a>
                        <a class="dropdown-item" href="{{ url_for('planning.scheduled') }}">Tâches planifiées</a>
                        <a class="dropdown-item" href="{{ url_for('planning.meters') }}">Compteurs</a>
                        <a class="dropdown-item" href="{{ url_for('planning.consumables') }}">Consommables</a>
                        <a class="dropdown-item" href="{{ url_for('planning.availability') }}">Disponibilités</a>
                        <a class="dropdown-item" href="{{ url_for('planning.time_tracking') }}">Horaires et suivi annuel</a>
                        <a class="dropdown-item" href="{{ url_for('planning.admin_tasks') }}">Tâches administratives</a>
                        <a class="dropdown-item" href="{{ url_for('planning.rules') }}">Règles d'accès</a>
                        <a class="dropdown-item" href="{{ url_for('prevention.index') }}">Prévention / DUERP</a>
                        <a class="dropdown-item" href="{{ url_for('trainings.index') }}">Formations</a>
                        <a class="dropdown-item" href="{{ url_for('trainings.my_trainings') }}">Mes formations</a>
                        <a class="dropdown-item" href="{{ url_for('exports.export_interventions') }}">Export interventions</a>
                        <a class="dropdown-item" href="{{ url_for('exports.export_costs_csv') }}">Export coûts et stock</a>
                        <a class="dropdown-item" href="{{ url_for('scheduler.index') }}">Génération automatique</a>
            </div></details></li>
            <li><details class="px-2 py-1"><summary class="text-light" style="cursor:pointer"><i class="bi bi-pc-display"></i> Équipements</summary><div class="mt-1">
                        <a class="dropdown-item" href="{{ url_for('equipments.index') }}">Tous les équipements</a>
                        <a class="dropdown-item" href="{{ url_for('equipments.categories') }}">Catégories</a>
                        <a class="dropdown-item" href="{{ url_for('buildings.index') }}">Bâtiments</a>
                        <a class="dropdown-item" href="{{ url_for('zones.index') }}">Zones</a>
                        <a class="dropdown-item" href="{{ url_for('rooms.index') }}">Salles</a>
                        <a class="dropdown-item" href="{{ url_for('housing.index') }}">Logements de fonction</a>
                        <a class="dropdown-item" href="{{ url_for('room_types.index') }}">Types de salles</a>
                        <a class="dropdown-item" href="{{ url_for('companies.index') }}">Entreprises</a>
                        <a class="dropdown-item" href="{{ url_for('contracts.index') }}">Contrats</a>
                        <a class="dropdown-item" href="{{ url_for('lots.index') }}">Lots</a>
                        <a class="dropdown-item" href="{{ url_for('services.index') }}">Services</a>
                        <a class="dropdown-item" href="{{ url_for('parts.index') }}">Pièces</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.dashboard') }}">Matériel &amp; Entretien</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.products') }}">Produits génériques</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.references') }}">Références / conditionnements</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.documents') }}">FDS / fiches techniques</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.stock') }}">État du stock</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.receive') }}">Réceptions</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.issue') }}">Sorties agents</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.transvasement_page') }}">Transvasements / dilutions</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.request_profiles') }}">Fiches papier</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.material') }}">Matériel durable</a>
                        <a class="dropdown-item" href="{{ url_for('cleaning.forecast_config') }}">Prévision / permanences</a>
                        <a class="dropdown-item" href="{{ url_for('meters.index') }}">Historique des compteurs</a>
                        <a class="dropdown-item" href="{{ url_for('equipments.to_trash') }}">À jeter</a>
                        <a class="dropdown-item" href="{{ url_for('equipments.lifecycle_dashboard') }}">Cycle de vie</a>
                        <a class="dropdown-item" href="{{ url_for('equipments.trashed') }}">Anciens équipements</a>
            </div></details></li>
            <li><details class="px-2 py-1"><summary class="text-light" style="cursor:pointer"><i class="bi bi-folder2-open"></i> Documents</summary><div class="mt-1">
                        <a class="dropdown-item" href="{{ url_for('documents.index') }}">Centre documentaire</a>
                        <a class="dropdown-item" href="{{ url_for('documents.fds') }}">FDS accessibles</a>
            </div></details></li>
            <li><details class="px-2 py-1"><summary class="text-light" style="cursor:pointer"><i class="bi bi-gear"></i> Configuration</summary><div class="mt-1">
                        <a class="dropdown-item" href="{{ url_for('gmao_config.index') }}">Contexte GMAO</a>
                        <a class="dropdown-item" href="{{ url_for('ai_config.index') }}">Configuration IA</a>
                        <a class="dropdown-item" href="{{ url_for('constraints.index') }}">Contraintes de zone</a>
                        <a class="dropdown-item" href="{{ url_for('outlook_pages.index') }}">Comptes Outlook</a>
                        <a class="dropdown-item" href="{{ url_for('outlook_pages.add_account') }}">Ajouter un compte Outlook</a>
                        <a class="dropdown-item" href="{{ url_for('outlook_dashboard.processed_folders_config') }}">Dossiers Outlook surveillés</a>
                        <a class="dropdown-item" href="{{ url_for('ent.index') }}">ENT77</a>
                        <a class="dropdown-item" href="{{ url_for('ent.messages') }}">Messages ENT</a>
                        <a class="dropdown-item" href="{{ url_for('ent.interpretations') }}">Interprétations ENT</a>
                        <a class="dropdown-item" href="{{ url_for('ent.staff') }}">Personnels ENT</a>
                        <a class="dropdown-item" href="{{ url_for('pronote.connect') }}">PRONOTE (QR)</a>
                        <li><form method="post" action="{{ url_for('pronote.sync_salles') }}"><button class="dropdown-item" type="submit"><i class="bi bi-download"></i> PRONOTE (sync salles)</button></form></li>
                        <a class="dropdown-item" href="{{ url_for('pronote.planning') }}">PRONOTE (plannings)</a>
                        <a class="dropdown-item" href="{{ url_for('pronote.rooms') }}">Salles PRONOTE</a>
                        <a class="dropdown-item" href="{{ url_for('pronote.personnels') }}">Personnels PRONOTE</a>
                        <a class="dropdown-item" href="{{ url_for('pronote.professeurs') }}">Professeurs PRONOTE</a>
                        <a class="dropdown-item" href="{{ url_for('yeastar.index') }}">Téléphone P520</a>
                        <a class="dropdown-item" href="{{ url_for('yeastar.config_page') }}">Config téléphone</a>
                        <a class="dropdown-item" href="{{ url_for('yeastar.dnd_config_page') }}">Watchdog DND</a>
                        <a class="dropdown-item" href="{{ url_for('status.index') }}">Status système</a>
                        <a class="dropdown-item" href="{{ url_for('logs.index') }}">Logs watchdogs</a>
                        <a class="dropdown-item" href="{{ url_for('chatbot.chatbot_interface') }}">Chatbot</a>
                        <a class="dropdown-item" href="{{ url_for('auth.list_users') }}">Utilisateurs</a>
                        <a class="dropdown-item" href="{{ url_for('admin.roles') }}">Rôles et permissions</a>
                        <a class="dropdown-item" href="{{ url_for('admin.data_management') }}">Gestion des données</a>
                        <a class="dropdown-item" href="{{ url_for('admin.template_debug') }}">Audit des templates</a>
                        <a class="dropdown-item" href="{{ url_for('setup_wizard.admin_panel') }}">Gestion bases</a>
                        <a class="dropdown-item" href="{{ url_for('setup_wizard.index') }}">Setup Wizard</a>
            </div></details></li>
        </ul>
    </li>
    {% endif %}

    <li class="nav-item d-lg-none"><a class="nav-link" href="{{ url_for('dashboard.alerts') }}"><i class="bi bi-bell"></i> Alertes{% if unread %} <span class="badge bg-danger ms-1">{{ unread }}</span>{% endif %}</a></li>
</ul>
