<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% if titel %}{{ titel }} – {% endif %}AufmaßWeb</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/modul.css') }}">
    <script src="https://unpkg.com/htmx.org@2.0.4"></script>
    <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.6/Sortable.min.js"></script>
    <style>
        :root { --bulma-font-size: {{ session.get('font_size', '1') }}rem; }
    </style>
    {% block head %}{% endblock %}
</head>
<body>
    {% if current_user.is_authenticated %}
    <nav class="navbar is-dark" role="navigation">
        <div class="navbar-brand">
            <a class="navbar-item has-text-weight-bold" href="{{ url_for('admin.dashboard') }}">AufmaßWeb</a>
            <a role="button" class="navbar-burger" data-target="navMenu">
                <span aria-hidden="true"></span><span aria-hidden="true"></span><span aria-hidden="true"></span>
            </a>
        </div>
        <div id="navMenu" class="navbar-menu">
            <div class="navbar-start">
                 {% if current_user.is_superadmin() %}
                 <a class="navbar-item" href="{{ url_for('superadmin.dashboard') }}">🌐 Superadmin</a>
                 <a class="navbar-item" href="{{ url_for('superadmin.dashboard') }}">🏢 Firmenverwaltung</a>
                 <a class="navbar-item" href="{{ url_for('superadmin.dashboard') }}">🔑 Lizenzverwaltung</a>
                 {% endif %}
                  <a class="navbar-item" href="{{ url_for('admin.dashboard') }}">Dashboard</a>
                  <a class="navbar-item" href="{{ url_for('aufmass.index') }}">Projekte / Aufmaße</a>
            </div>
            {% set initials = (current_user.vorname[0] if current_user.vorname else '') + (current_user.nachname[0] if current_user.nachname else '') %}
            <div class="navbar-end">
                <div class="navbar-item has-dropdown is-hoverable">
                    <a class="navbar-link" style="cursor:pointer;align-items:center;gap:6px">
                        {% if current_user.profile_image %}
                        <img src="{{ url_for('static', filename='avatars/'+current_user.profile_image) }}" alt="" style="width:28px;height:28px;border-radius:50%;object-fit:cover">
                        {% else %}
                        <span style="display:inline-block;width:28px;height:28px;border-radius:50%;background:#2F5496;color:#fff;font-size:12px;font-weight:bold;line-height:28px;text-align:center">{{ initials or '?' }}</span>
                        {% endif %}
                        <span>{{ current_user.full_name }}</span>
                        <span class="tag is-info is-light">{{ current_user.rolle }}</span>
                    </a>
                    <div class="navbar-dropdown is-right">
                        <a class="navbar-item" href="{{ url_for('admin.profil') }}">👤 Profil</a>
                        <a class="navbar-item" href="{{ url_for('admin.profil') }}">⚙ Einstellungen</a>
                        <hr class="navbar-divider">
                        {% if current_user.is_superadmin() %}
                        <a class="navbar-item" href="{{ url_for('superadmin.dashboard') }}">🏢 Firmenverwaltung</a>
                        <a class="navbar-item" href="{{ url_for('superadmin.dashboard') }}">🔑 Lizenzverwaltung</a>
                        {% endif %}
                        {% if current_user.is_firmadmin() %}
                        <a class="navbar-item" href="{{ url_for('admin.firma') }}">🏢 Firmenverwaltung</a>
                        {% endif %}
                        <a class="navbar-item" href="{{ url_for('contracts.index') }}">📋 Verträge</a>
                        <a class="navbar-item" href="{{ url_for('custom_modules.index') }}">🔧 Modul-Builder</a>
                        <a class="navbar-item" href="{{ url_for('lv.index') }}">📂 LV-Verwaltung</a>
                        <hr class="navbar-divider">
                        <a class="navbar-item" href="{{ url_for('auth.logout') }}" style="color:#e74c3c">🚪 Logout</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>
    {% endif %}

    <section class="section">
        <div class="container is-fluid">
            {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
            {% for category, message in messages %}
            <div class="notification is-{{ category }} is-light py-3">{{ message }}</div>
            {% endfor %}
            {% endif %}
            {% endwith %}
            {% block content %}{% endblock %}
        </div>
    </section>

    <script>
    document.addEventListener('DOMContentLoaded', () => {
        document.querySelectorAll('.navbar-burger').forEach(el => {
            el.addEventListener('click', () => {
                const target = document.getElementById(el.dataset.target);
                el.classList.toggle('is-active');
                target.classList.toggle('is-active');
            });
        });
    });
    </script>
    {% block scripts %}{% endblock %}
</body>
</html>
