{% load django_htmx %}
<!DOCTYPE html>
<html lang="en">
    {% load static %}
    <head>
        <meta charset="utf-8" />
        <meta name="description" content="Self-hosted time-tracker." />
        <meta name="keywords" content="time, tracking, video games, self-hosted" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Timetracker - {{ title }}</title>
        <script src="{% static 'js/htmx.min.js' %}"></script>
        <script>
            htmx.config.scrollBehavior = 'smooth';
            htmx.config.selfRequestsOnly = false;
        </script>
        <script src="{% static 'js/htmx-redirect-toast.js' %}"></script>
        {% django_htmx_script %}
        <link rel="stylesheet" href="{% static 'base.css' %}" />
        <script src="https://cdn.jsdelivr.net/npm/flowbite@2.4.1/dist/flowbite.min.js"></script>
        {% comment %} <script src="//unpkg.com/alpinejs" defer></script>
        <script src="//unpkg.com/@alpinejs/mask" defer></script> {% endcomment %}
        <script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/mask@3.x.x/dist/cdn.min.js"></script>
        <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
        <script>
        // On page load or when changing themes, best to add inline in `head` to avoid FOUC
            if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
                document.documentElement.classList.add('dark');
            } else {
                document.documentElement.classList.remove('dark')
            }
        </script>
    </head>
    <body hx-indicator="#indicator" class="bg-neutral-primary">
        <script id="django-messages" type="application/json">
        [
            {% for message in messages %}
                {"message": "{{ message|escapejs }}", "type": "{{ message.tags|default:'info' }}"}{% if not forloop.last %},{% endif %}
            {% endfor %}
        ]
        </script>
        <img id="indicator"
             src="{% static 'icons/loading.png' %}"
             class="absolute right-3 top-3 animate-spin htmx-indicator"
             height="24"
             width="24"
             alt="loading indicator" />
        <div class="flex flex-col min-h-screen">
            {% include "navbar.html" %}
            <div class="flex flex-1 flex-col pt-8 pb-16">{{ slot }}</div>
            {% load version %}
            <span class="fixed left-2 bottom-2 text-xs text-slate-300 dark:text-slate-600">{% version %} ({% version_date %})</span>
        </div>
        {{ scripts }}
        <script type="module">
            document.addEventListener('DOMContentLoaded', () => {
                if (window.mountCrownIcon) {
                    window.mountCrownIcon('#crown-icon-mount-point', {
                        mastered: {{ game.mastered|yesno:"true,false" }}
                    });
                }

                // Theme toggle logic
                const themeToggleDarkIcon = document.getElementById('theme-toggle-dark-icon');
                const themeToggleLightIcon = document.getElementById('theme-toggle-light-icon');
                const themeToggleBtn = document.getElementById('theme-toggle');

                // Ensure all elements are found before proceeding
                if (themeToggleDarkIcon && themeToggleLightIcon && themeToggleBtn) {
                    // Initial state of icons based on current theme
                    // The FOUC script in <head> already set document.documentElement.classList.add/remove('dark')
                    // So we just need to set the icon visibility based on that.
                    if (document.documentElement.classList.contains('dark')) {
                        themeToggleLightIcon.classList.remove('hidden');
                        themeToggleDarkIcon.classList.add('hidden');
                    } else {
                        themeToggleDarkIcon.classList.remove('hidden');
                        themeToggleLightIcon.classList.add('hidden');
                    }

                    themeToggleBtn.addEventListener('click', function () {
                        // toggle icons inside button
                        themeToggleDarkIcon.classList.toggle('hidden');
                        themeToggleLightIcon.classList.toggle('hidden');

                        // if set via local storage previously
                        if (localStorage.getItem('color-theme')) {
                            if (localStorage.getItem('color-theme') === 'light') {
                                document.documentElement.classList.add('dark');
                                localStorage.setItem('color-theme', 'dark');
                            } else { // current theme is dark, switch to light
                                document.documentElement.classList.remove('dark');
                                localStorage.setItem('color-theme', 'light');
                            }

                        // if NOT set via local storage previously
                        } else { // no theme in local storage, use system preference
                            if (document.documentElement.classList.contains('dark')) { // currently dark, switch to light
                                document.documentElement.classList.remove('dark');
                                localStorage.setItem('color-theme', 'light');
                            } else { // currently light, switch to dark
                                document.documentElement.classList.add('dark');
                                localStorage.setItem('color-theme', 'dark');
                            }
                        }
                    });
                }
            });
        </script>
   <div id="global-modal-container"></div>

    <div x-data="toastStore()"
         role="region"
         aria-label="Notifications"
         aria-atomic="true"
         class="fixed z-50 bottom-0 right-0 flex flex-col items-end pointer-events-none p-4">
        <template x-for="toast in $store.toasts.toasts" :key="toast.id">
            <div x-show="toast.visible"
                 x-transition:enter="transition ease-out duration-300"
                 x-transition:enter-start="opacity-0 translate-x-8"
                 x-transition:enter-end="opacity-100 translate-x-0"
                 x-transition:leave="transition ease-in duration-200"
                 x-transition:leave-start="opacity-100 translate-x-0"
                 x-transition:leave-end="opacity-0 translate-x-8"
                 :role="toast.type === 'error' || toast.type === 'warning' ? 'alert' : 'status'"
                  :aria-live="toast.type === 'error' ? 'assertive' : 'polite'"
                 tabindex="0"
                 class="pointer-events-auto max-w-sm w-72 cursor-pointer mb-3 last:mb-0"
     :class="{
                      'success': toast.type === 'success',
                      'error': toast.type === 'error',
                      'info': toast.type === 'info',
                      'warning': toast.type === 'warning',
                      'debug': toast.type === 'debug'
                  }"
                 @click="dismissToast(toast.id)"
                 @mouseenter="$store.toasts.clearToastTimer(toast.id)"
                 @mouseleave="$store.toasts.resumeToastTimer(toast.id, 5000)"
                 @keydown.escape="dismissToast(toast.id)">
                <div class="rounded-lg shadow-lg p-4 flex items-start gap-3"
                     :class="{
                          'bg-green-50 dark:bg-green-900 border border-green-200 dark:border-green-700': toast.type === 'success',
                          'bg-red-50 dark:bg-red-900 border border-red-200 dark:border-red-700': toast.type === 'error',
                          'bg-blue-50 dark:bg-blue-900 border border-blue-200 dark:border-blue-700': toast.type === 'info',
                          'bg-amber-50 dark:bg-amber-900 border border-amber-200 dark:border-amber-700': toast.type === 'warning',
                          'bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700': toast.type === 'debug'
                      }">
                    <span class="flex-shrink-0 mt-0.5"
                   :class="{
                               'text-green-500': toast.type === 'success',
                               'text-red-500': toast.type === 'error',
                               'text-blue-500': toast.type === 'info',
                               'text-amber-500': toast.type === 'warning',
                               'text-gray-500': toast.type === 'debug'
                           }">
                        <template x-if="toast.type === 'success'">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                            </svg>
                        </template>
                        <template x-if="toast.type === 'error'">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                        </template>
                        <template x-if="toast.type === 'info'">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M12 20a8 8 0 100-16 8 8 0 000 16z"/>
                            </svg>
                        </template>
                        <template x-if="toast.type === 'warning'">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 13l5 5 5-5M7 6l5 5 5-5"/>
                            </svg>
                        </template>
                        <template x-if="toast.type === 'debug'">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/>
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                            </svg>
                        </template>
                    </span>
                    <p class="flex-1 text-sm"
                        :class="{
                            'text-green-800 dark:text-green-200': toast.type === 'success',
                            'text-red-800 dark:text-red-200': toast.type === 'error',
                            'text-blue-800 dark:text-blue-200': toast.type === 'info',
                            'text-amber-800 dark:text-amber-200': toast.type === 'warning',
                            'text-gray-800 dark:text-gray-200': toast.type === 'debug'
                        }"
                       x-text="toast.message"></p>
                    <button @click.stop="dismissToast(toast.id)"
                            class="flex-shrink-0"
                            :class="{
                                'text-green-400 hover:text-green-600 dark:text-green-500 dark:hover:text-green-300': toast.type === 'success',
                                'text-red-400 hover:text-red-600 dark:text-red-500 dark:hover:text-red-300': toast.type === 'error',
                                'text-blue-400 hover:text-blue-600 dark:text-blue-500 dark:hover:text-blue-300': toast.type === 'info',
                                'text-amber-400 hover:text-amber-600 dark:text-amber-500 dark:hover:text-amber-300': toast.type === 'warning',
                                'text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300': toast.type === 'debug'
                            }">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                        </svg>
                    </button>
                </div>
            </div>
        </template>
    </div>

    <script src="{% static 'js/toast.js' %}"></script>
    </body>
</html>
