{{ define "layout" }}
<!DOCTYPE html>
<html lang="en" data-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Decypharr - {{.Title}}</title>

    <link href="{{.URLBase}}assets/css/styles.css" rel="stylesheet" type="text/css" />

    <link rel="apple-touch-icon" sizes="180x180" href="{{.URLBase}}images/favicon/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="{{.URLBase}}images/favicon/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="{{.URLBase}}images/favicon/favicon-16x16.png">
    <link rel="manifest" href="{{.URLBase}}images/favicon/site.webmanifest">

    <!-- Preload JavaScript -->
    <link rel="preload" href="{{.URLBase}}assets/js/common.js" as="script">

    <script>
        // Early theme detection to prevent FOUC
        (function() {
            const savedTheme = localStorage.getItem('theme');
            if (savedTheme) {
                document.documentElement.setAttribute('data-theme', savedTheme);
            } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                document.documentElement.setAttribute('data-theme', 'dark');
            } else {
                document.documentElement.setAttribute('data-theme', 'light');
            }
        })();

        // Set global URL base
        window.urlBase = "{{.URLBase}}";
    </script>
</head>
<body class="min-h-screen bg-base-200 flex flex-col">
<!-- Toast Container -->
<div class="toast-container fixed bottom-4 right-4 z-50 space-y-2">
</div>

<!-- Navigation -->
<header class="navbar bg-base-100 shadow-lg sticky top-0 z-40 backdrop-blur-sm">
    <div class="navbar-start">
        <div class="dropdown">
            <div tabindex="0" role="button" class="btn btn-ghost lg:hidden">
                <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="M4 6h16M4 12h8m-8 6h16"/>
                </svg>
            </div>
            <ul class="menu menu-sm dropdown-content mt-3 z-[1] p-2 shadow-lg bg-base-100 rounded-box w-52 border border-base-300">
                <li><a href="{{.URLBase}}" class="{{if eq .Page "index"}}active{{end}}">
                    <i class="bi bi-grid-3x3-gap text-primary"></i>Dashboard
                    </a></li>
                <li><a href="{{.URLBase}}download" class="{{if eq .Page "download"}}active{{end}}">
                    <i class="bi bi-cloud-download text-secondary"></i>Download
                    </a></li>
                <li><a href="{{.URLBase}}repair" class="{{if eq .Page "repair"}}active{{end}}">
                    <i class="bi bi-wrench-adjustable text-accent"></i>Repair
                    </a></li>
                <li><a href="{{.URLBase}}config" class="{{if eq .Page "config"}}active{{end}}">
                    <i class="bi bi-gear text-info"></i>Settings
                    </a></li>
                <li><a href="{{.URLBase}}webdav" target="_blank">
                    <i class="bi bi-cloud text-success"></i>WebDAV
                </a></li>
                <li><a href="{{.URLBase}}logs" target="_blank">
                    <i class="bi bi-journal-text text-warning"></i>Logs
                </a></li>
            </ul>
        </div>
        <a class="btn btn-ghost text-xl font-bold text-primary group" href="{{.URLBase}}">
            <!-- Logo -->
            <img src="{{.URLBase}}images/logo.svg" alt="Decypharr Logo" class="w-8 h-8 inline-block mr-2">
            <span class="hidden sm:inline bg-clip-text text-transparent">Decypharr</span>
        </a>
    </div>

    <div class="navbar-center hidden lg:flex">
        <ul class="menu menu-horizontal px-1 gap-1">
            <li><a href="{{.URLBase}}" class="{{if eq .Page "index"}}active{{end}} tooltip tooltip-bottom" data-tip="Dashboard">
                <i class="bi bi-grid-3x3-gap"></i>
                <span class="hidden xl:inline">Dashboard</span>
                </a></li>
            <li><a href="{{.URLBase}}download" class="{{if eq .Page "download"}}active{{end}} tooltip tooltip-bottom" data-tip="Add Downloads">
                <i class="bi bi-cloud-download"></i>
                <span class="hidden xl:inline">Download</span>
                </a></li>
            <li><a href="{{.URLBase}}repair" class="{{if eq .Page "repair"}}active{{end}} tooltip tooltip-bottom" data-tip="Repair Media">
                <i class="bi bi-wrench-adjustable"></i>
                <span class="hidden xl:inline">Repair</span>
                </a></li>
            <li><a href="{{.URLBase}}config" class="{{if eq .Page "config"}}active{{end}} tooltip tooltip-bottom" data-tip="Settings">
                <i class="bi bi-gear"></i>
                <span class="hidden xl:inline">Settings</span>
                </a></li>
            <li><a href="{{.URLBase}}webdav" target="_blank" class="tooltip tooltip-bottom" data-tip="WebDAV Access">
                <i class="bi bi-cloud"></i>
                <span class="hidden xl:inline">WebDAV</span>
            </a></li>
            <li><a href="{{.URLBase}}logs" target="_blank" class="tooltip tooltip-bottom" data-tip="System Logs">
                <i class="bi bi-journal-text"></i>
                <span class="hidden xl:inline">Logs</span>
            </a></li>
        </ul>
    </div>

    <div class="navbar-end">
        <div class="flex items-center gap-3">
            <!-- Theme Toggle -->
            <div class="tooltip tooltip-left">
                <label class="swap swap-rotate btn btn-ghost btn-circle hover:bg-base-300 transition-colors">
                    <input type="checkbox" id="themeToggle" class="theme-controller" />
                    <!-- Sun icon for light mode -->
                    <i class="swap-off bi bi-sun text-lg text-warning"></i>
                    <!-- Moon icon for dark mode -->
                    <i class="swap-on bi bi-moon-stars text-lg text-info"></i>
                </label>
            </div>

            <!-- Stats Link -->
            <div class="tooltip tooltip-left" data-tip="System Statistics">
                <a href="{{.URLBase}}debug/stats" class="btn btn-ghost btn-sm hover:bg-base-300 transition-colors">
                    <i class="bi bi-graph-up text-lg"></i>
                    <span class="hidden md:inline ml-1">Stats</span>
                </a>
            </div>

            <!-- Version Badge -->
            <div class="tooltip tooltip-left" data-tip="Current Version">
                <div class="badge badge-primary font-mono text-xs hover:badge-primary-focus transition-colors cursor-pointer" id="version-badge">
                    Loading...
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Main Content -->
<main class="flex-1 container mx-auto px-4 py-6">
    {{ if eq .Page "index" }}
    {{ template "index" . }}
    {{ else if eq .Page "download" }}
    {{ template "download" . }}
    {{ else if eq .Page "repair" }}
    {{ template "repair" . }}
    {{ else if eq .Page "config" }}
    {{ template "config" . }}
    {{ else if eq .Page "login" }}
    {{ template "login" . }}
    {{ else if eq .Page "register" }}
    {{ template "register" . }}
    {{ else }}
    <div class="hero min-h-96">
        <div class="hero-content text-center">
            <div class="max-w-md">
                <h1 class="text-5xl font-bold text-error">404</h1>
                <p class="py-6">Page not found. The page you're looking for doesn't exist.</p>
                <a href="{{.URLBase}}" class="btn btn-primary">Go Home</a>
            </div>
        </div>
    </div>
    {{ end }}
</main>

<!-- Footer -->
<footer class="footer footer-center p-6 bg-base-300 text-base-content border-t border-base-200">
    <aside class="grid-flow-col gap-4">
        <a href="https://github.com/sirrobot01/decypharr" target="_blank"
           class="link link-hover flex items-center gap-2 hover:text-primary transition-colors">
            <i class="bi bi-github text-lg"></i>
            <span>GitHub</span>
        </a>
        <a href="https://sirrobot01.github.io/decypharr" target="_blank"
           class="link link-hover flex items-center gap-2 hover:text-primary transition-colors">
            <i class="bi bi-book text-lg"></i>
            <span>Documentation</span>
        </a>
    </aside>
</footer>

<!-- Scripts -->
<script src="{{.URLBase}}assets/js/jquery-3.7.1.min.js"></script>
<script src="{{.URLBase}}assets/js/common.js"></script>

<!-- Page-specific scripts -->
{{ if eq .Page "index" }}
<script src="{{.URLBase}}assets/js/dashboard.js"></script>
{{ else if eq .Page "download" }}
<script src="{{.URLBase}}assets/js/download.js"></script>
{{ else if eq .Page "repair" }}
<script src="{{.URLBase}}assets/js/repair.js"></script>
{{ else if eq .Page "config" }}
<script src="{{.URLBase}}assets/js/config.js"></script>
{{ end }}
</body>
</html>
{{ end }}