<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Code of Conquest - An AI-powered Dungeons & Dragons adventure game">
    <title>{% block title %}Code of Conquest{% endblock %}</title>

    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700&family=Lato:wght@300;400;700&display=swap" rel="stylesheet">

    <!-- Main CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">

    <!-- HTMX for dynamic interactions -->
    <script src="https://unpkg.com/htmx.org@1.9.10"></script>
    <!-- HTMX JSON encoding extension -->
    <script src="https://unpkg.com/htmx.org@1.9.10/dist/ext/json-enc.js"></script>
    <!-- Hyperscript for custom events -->
    <script src="https://unpkg.com/hyperscript.org@0.9.12"></script>

    {% block extra_head %}{% endblock %}
</head>
<body>
    <!-- Header -->
    <header class="header">
        <div class="header-content">
            <a href="/" class="logo">⚔️ Code of Conquest</a>

            {% if current_user %}
            <!-- Desktop Navigation Menu -->
            <nav class="nav-menu">
                <a href="{{ url_for('pages.profile') }}" class="nav-item {% if request.path == '/profile' %}active{% endif %}">Profile</a>
                <a href="{{ url_for('character_views.list_characters') }}" class="nav-item {% if request.path.startswith('/characters') %}active{% endif %}">Characters</a>
                <a href="{{ url_for('pages.sessions') }}" class="nav-item {% if request.path == '/sessions' %}active{% endif %}">Sessions</a>
                <a href="{{ url_for('pages.mechanics') }}" class="nav-item {% if request.path == '/mechanics' %}active{% endif %}">Mechanics</a>
                <a href="{{ url_for('pages.leaderboard') }}" class="nav-item {% if request.path == '/leaderboard' %}active{% endif %}">Leaderboard</a>
                <a href="{{ url_for('pages.settings') }}" class="nav-item {% if request.path == '/settings' %}active{% endif %}">Settings</a>
                <a href="{{ url_for('pages.help_page') }}" class="nav-item {% if request.path == '/help' %}active{% endif %}">Help</a>
            </nav>

            <!-- User Info & Logout -->
            <div class="header-user">
                <span class="user-greeting">{{ current_user.name or current_user.email }}</span>
                <form method="POST" action="{{ url_for('auth_views.logout') }}" class="logout-form">
                    <button type="submit" class="btn-link">Logout</button>
                </form>
            </div>

            <!-- Mobile Hamburger Button -->
            <button class="hamburger-btn" onclick="toggleMobileMenu()" aria-label="Toggle menu">
                <span class="hamburger-icon"></span>
            </button>
            {% endif %}
        </div>

        {% if current_user %}
        <!-- Mobile Navigation Menu -->
        <nav class="mobile-menu" id="mobileMenu">
            <a href="{{ url_for('pages.profile') }}" class="mobile-nav-item {% if request.path == '/profile' %}active{% endif %}">Profile</a>
            <a href="{{ url_for('character_views.list_characters') }}" class="mobile-nav-item {% if request.path.startswith('/characters') %}active{% endif %}">Characters</a>
            <a href="{{ url_for('pages.sessions') }}" class="mobile-nav-item {% if request.path == '/sessions' %}active{% endif %}">Sessions</a>
            <a href="{{ url_for('pages.mechanics') }}" class="mobile-nav-item {% if request.path == '/mechanics' %}active{% endif %}">Mechanics</a>
            <a href="{{ url_for('pages.leaderboard') }}" class="mobile-nav-item {% if request.path == '/leaderboard' %}active{% endif %}">Leaderboard</a>
            <a href="{{ url_for('pages.settings') }}" class="mobile-nav-item {% if request.path == '/settings' %}active{% endif %}">Settings</a>
            <a href="{{ url_for('pages.help_page') }}" class="mobile-nav-item {% if request.path == '/help' %}active{% endif %}">Help</a>
            <div class="mobile-menu-divider"></div>
            <span class="mobile-user-greeting">{{ current_user.name or current_user.email }}</span>
            <form method="POST" action="{{ url_for('auth_views.logout') }}" class="logout-form">
                <button type="submit" class="mobile-nav-item mobile-logout">Logout</button>
            </form>
        </nav>
        {% endif %}
    </header>

    <!-- Main Content -->
    <main>
        <!-- Flash Messages -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                <div class="flash-messages-container">
                    {% for category, message in messages %}
                        <div class="flash-message flash-{{ category }}">
                            {{ message }}
                            <button class="flash-close" onclick="this.parentElement.remove()">&times;</button>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
        {% endwith %}

        <!-- Page Content -->
        {% block content %}{% endblock %}
    </main>

    <!-- Footer -->
    <footer class="footer">
        <p class="footer-text">
            &copy; 2025 Code of Conquest. All rights reserved. | May your adventures be legendary.
        </p>
    </footer>

    <!-- Mobile Menu JavaScript -->
    <script>
        function toggleMobileMenu() {
            const menu = document.getElementById('mobileMenu');
            const btn = document.querySelector('.hamburger-btn');
            menu.classList.toggle('open');
            btn.classList.toggle('open');
        }

        // Close menu when clicking outside
        document.addEventListener('click', function(event) {
            const menu = document.getElementById('mobileMenu');
            const btn = document.querySelector('.hamburger-btn');
            if (menu && !menu.contains(event.target) && !btn.contains(event.target)) {
                menu.classList.remove('open');
                btn.classList.remove('open');
            }
        });

        // Close menu on HTMX navigation
        document.body.addEventListener('htmx:afterRequest', function() {
            const menu = document.getElementById('mobileMenu');
            const btn = document.querySelector('.hamburger-btn');
            if (menu) {
                menu.classList.remove('open');
                btn.classList.remove('open');
            }
        });
    </script>

    <!-- JavaScript -->
    {% block scripts %}{% endblock %}
</body>
</html>
