<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}{{ site_name() }}{% endblock %}</title>

    {# SEO Meta Tags #}
    {% block seo_meta %}
    {% if seo_meta %}
    {% if seo_meta.description %}<meta name="description" content="{{ seo_meta.description }}">{% endif %}
    {% if seo_meta.robots %}<meta name="robots" content="{{ seo_meta.robots }}">{% endif %}
    <link rel="canonical" href="{{ seo_meta.canonical_url }}">
    {% endif %}
    {% endblock %}

    {# OpenGraph Tags #}
    {% block og_meta %}
    {% if og_meta %}
    <meta property="og:title" content="{{ og_meta.title }}">
    {% if og_meta.description %}<meta property="og:description" content="{{ og_meta.description }}">{% endif %}
    {% if og_meta.image %}<meta property="og:image" content="{{ og_meta.image }}">{% endif %}
    <meta property="og:url" content="{{ og_meta.url }}">
    <meta property="og:site_name" content="{{ og_meta.site_name }}">
    <meta property="og:type" content="{{ og_meta.type }}">
    {% endif %}
    {% endblock %}

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@500;600&family=Lora:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/static/css/style.css">
    {% block head %}{% endblock %}
</head>
<body>
    <header class="container">
        <nav>
            <ul>
                <li><a href="/" class="site-name">{{ site_name() }}</a></li>
            </ul>
            <ul>
                {% if user %}
                    <li class="user-info">
                        {% if user.picture_url %}
                            <img src="{{ user.picture_url }}" alt="{{ user.name }}" referrerpolicy="no-referrer">
                        {% endif %}
                        <span>{{ user.name or user.email }}</span>
                    </li>
                    <li><a href="/auth/logout" role="button" class="outline">Logout</a></li>
                {% else %}
                    <li><a href="/auth/login" role="button">Login</a></li>
                {% endif %}
            </ul>
        </nav>
    </header>

    <main class="container{% block main_class %}{% endblock %}">
        {# Enhanced flash messages with types #}
        {% if flash_messages %}
        <div class="flash-messages" role="status" aria-live="polite">
            {% for msg in flash_messages %}
            <article role="alert" class="flash flash-{{ msg.type.value }}" {% if msg.dismissible %}data-dismissible{% endif %}>
                <span class="flash-message">{{ msg.message }}</span>
                {% if msg.dismissible %}
                <button type="button" class="flash-dismiss" aria-label="Dismiss">&times;</button>
                {% endif %}
            </article>
            {% endfor %}
        </div>
        {# Backwards compatibility: old single flash string #}
        {% elif flash %}
        <div class="flash-messages" role="status" aria-live="polite">
            <article role="alert" class="flash flash-info" data-dismissible>
                <span class="flash-message">{{ flash }}</span>
                <button type="button" class="flash-dismiss" aria-label="Dismiss">&times;</button>
            </article>
        </div>
        {% endif %}

        {% block content %}{% endblock %}
    </main>

    <footer class="container">
        <hr>
        <p><small>&copy; {% if site_copyright_start_year() and site_copyright_start_year() != now().year %}{{ site_copyright_start_year() }}-{% endif %}{{ now().year }} {{ site_copyright_holder() or site_name() }}. All rights reserved.</small></p>
    </footer>

    {% block scripts %}{% endblock %}

    <script>
    // Flash message dismissal
    document.querySelectorAll('.flash-dismiss').forEach(btn => {
        btn.addEventListener('click', () => {
            btn.closest('.flash').remove();
        });
    });
    // Auto-dismiss success messages after 5 seconds
    document.querySelectorAll('.flash-success[data-dismissible]').forEach(flash => {
        setTimeout(() => {
            flash.style.opacity = '0';
            setTimeout(() => flash.remove(), 300);
        }, 5000);
    });
    </script>
</body>
</html>
