<!DOCTYPE html>
<html lang="<?= current_locale() ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e($pageTitle ?? 'Admin') ?> – ITSWEBER CMS</title>
    <link rel="stylesheet" href="/admin/assets/css/admin.css?v=<?= cms_version() ?>-<?= @filemtime($_SERVER['DOCUMENT_ROOT'].'/admin/assets/css/admin.css') ?: cms_version() ?>">
</head>
<body class="admin-body">
    <a href="#admin-main-content" class="skip-link"><?= __('admin.topbar.skip_to_content') ?></a>
    <aside class="admin-sidebar" id="adminSidebar" aria-label="Admin sidebar">
        <div class="sidebar-header">
            <a href="/admin" class="sidebar-logo" title="<?= __('admin.nav.dashboard') ?>">
                <img src="/admin/assets/images/brand/itsweber-logo.png"
                     alt="ITSWEBER"
                     class="sidebar-brand-logo"
                     onerror="this.style.display='none';this.nextElementSibling.style.display='inline'">
                <span style="display:none">ITSWEBER <span>CMS</span></span>
            </a>
        </div>
        <nav class="sidebar-nav" aria-label="Main navigation">
            <a href="/admin/dashboard" class="nav-item <?= ($pageTitle ?? '') === __('admin.nav.dashboard') ? 'active' : '' ?>">
                <span class="nav-icon">⊞</span> <?= __('admin.nav.dashboard') ?>
            </a>
            <a href="/admin/pages" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.pages')) ? 'active' : '' ?>">
                <span class="nav-icon">☰</span> <?= __('admin.nav.pages') ?>
            </a>
            <div class="nav-group <?= str_contains($pageTitle ?? '', __('admin.nav.blog')) || ($pageTitle ?? '') === __('admin.nav.categories') || ($pageTitle ?? '') === __('admin.nav.tags') ? 'open' : '' ?>">
                <button type="button" class="nav-item nav-group-toggle <?= str_contains($pageTitle ?? '', __('admin.nav.blog')) ? 'active' : '' ?>">
                    <span class="nav-icon">✎</span> <?= __('admin.nav.blog') ?>
                    <span class="nav-group-chevron"></span>
                </button>
                <div class="nav-group-children">
                    <a href="/admin/blog" class="nav-item nav-sub-item <?= ($pageTitle ?? '') === __('admin.blog.title') ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.blog.title') ?>
                    </a>
                    <a href="/admin/blog/categories" class="nav-item nav-sub-item <?= ($pageTitle ?? '') === __('admin.nav.categories') ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.nav.categories') ?>
                    </a>
                    <a href="/admin/blog/tags" class="nav-item nav-sub-item <?= ($pageTitle ?? '') === __('admin.nav.tags') ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.nav.tags') ?>
                    </a>
                </div>
            </div>
            <a href="/admin/gallery" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.gallery')) ? 'active' : '' ?>">
                <span class="nav-icon">▦</span> <?= __('admin.nav.gallery') ?>
            </a>
            <div class="nav-group <?= str_contains($pageTitle ?? '', __('admin.nav.events')) || str_contains($pageTitle ?? '', __('admin.event_categories.title')) ? 'open' : '' ?>">
                <button type="button" class="nav-item nav-group-toggle <?= str_contains($pageTitle ?? '', __('admin.nav.events')) ? 'active' : '' ?>">
                    <span class="nav-icon">◷</span> <?= __('admin.nav.events') ?>
                    <span class="nav-group-chevron"></span>
                </button>
                <div class="nav-group-children">
                    <a href="/admin/events" class="nav-item nav-sub-item <?= ($pageTitle ?? '') === __('admin.events.title') ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.events.title') ?>
                    </a>
                    <a href="/admin/events/categories" class="nav-item nav-sub-item <?= str_contains($pageTitle ?? '', __('admin.event_categories.title')) ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.event_categories.title') ?>
                    </a>
                </div>
            </div>
            <div class="nav-group <?= str_contains($pageTitle ?? '', __('admin.nav.downloads')) || str_contains($pageTitle ?? '', __('admin.download_categories.title')) ? 'open' : '' ?>">
                <button type="button" class="nav-item nav-group-toggle <?= str_contains($pageTitle ?? '', __('admin.nav.downloads')) ? 'active' : '' ?>">
                    <span class="nav-icon">↓</span> <?= __('admin.nav.downloads') ?>
                    <span class="nav-group-chevron"></span>
                </button>
                <div class="nav-group-children">
                    <a href="/admin/downloads" class="nav-item nav-sub-item <?= ($pageTitle ?? '') === __('admin.downloads.title') ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.downloads.title') ?>
                    </a>
                    <a href="/admin/downloads/categories" class="nav-item nav-sub-item <?= str_contains($pageTitle ?? '', __('admin.download_categories.title')) ? 'active' : '' ?>">
                        <span class="nav-icon">—</span> <?= __('admin.download_categories.title') ?>
                    </a>
                </div>
            </div>
            <a href="/admin/media" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.media')) ? 'active' : '' ?>">
                <span class="nav-icon">◫</span> <?= __('admin.nav.media') ?>
            </a>
            <a href="/admin/menus" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.menus')) ? 'active' : '' ?>">
                <span class="nav-icon">☰</span> <?= __('admin.nav.menus') ?>
            </a>
            <a href="/admin/homepage" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.homepage')) ? 'active' : '' ?>">
                <span class="nav-icon">⌂</span> <?= __('admin.nav.homepage') ?>
            </a>

            <div class="nav-divider"></div>

            <div class="nav-section-label"><?= __('admin.nav.system_section') ?? 'System' ?></div>

            <a href="/admin/themes" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.themes')) ? 'active' : '' ?>">
                <span class="nav-icon">◐</span> <?= __('admin.nav.themes') ?>
            </a>
            <a href="/admin/plugins" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.plugins')) ? 'active' : '' ?>">
                <span class="nav-icon">⊞</span> <?= __('admin.nav.plugins') ?>
            </a>
            <a href="/admin/users" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.users')) ? 'active' : '' ?>">
                <span class="nav-icon">◉</span> <?= __('admin.nav.users') ?>
            </a>
            <a href="/admin/settings" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.settings')) ? 'active' : '' ?>">
                <span class="nav-icon">⚙</span> <?= __('admin.nav.settings') ?>
            </a>
            <a href="/admin/consent" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.consent')) ? 'active' : '' ?>">
                <span class="nav-icon">✓</span> <?= __('admin.nav.consent') ?>
            </a>
            <?php $_iw_maintActive = defined('BASE_PATH') && is_file(BASE_PATH . '/storage/.maintenance_mode'); ?>
            <a href="/admin/maintenance" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.maintenance')) ? 'active' : '' ?> <?= $_iw_maintActive ? 'nav-item--warn' : '' ?>">
                <span class="nav-icon">🔧</span> <?= __('admin.nav.maintenance') ?><?= $_iw_maintActive ? ' <span class="nav-badge nav-badge--warn">' . __('admin.maintenance.nav_active') . '</span>' : '' ?>
            </a>
            <a href="/admin/system" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.system')) ? 'active' : '' ?>">
                <span class="nav-icon">⊕</span> <?= __('admin.nav.system') ?>
            </a>
            <a href="/admin/backup" class="nav-item <?= str_contains($pageTitle ?? '', __('admin.nav.backup')) ? 'active' : '' ?>">
                <span class="nav-icon">↧</span> <?= __('admin.nav.backup') ?>
            </a>
        </nav>
        <div class="sidebar-footer">
            <span class="sidebar-version">v<?= e(cms_version()) ?></span>
        </div>
    </aside>

    <div class="sidebar-backdrop" id="sidebarBackdrop" aria-hidden="true"></div>

    <main class="admin-main">
        <header class="admin-topbar">
            <button class="sidebar-toggle" id="sidebarToggle" aria-label="<?= __('admin.topbar.open_menu') ?>" aria-expanded="false" aria-controls="adminSidebar">☰</button>
            <div class="topbar-title"><?= e($pageTitle ?? 'Admin') ?></div>
            <div class="topbar-actions">
                <a href="/" class="topbar-link" target="_blank"><?= __('admin.topbar.view_site') ?></a>
                <form method="POST" action="/admin/settings/language" class="topbar-language-form">
                    <?= csrf_field() ?>
                    <select name="language" onchange="this.form.submit()" class="topbar-lang-select">
                        <option value="de" <?= current_locale() === 'de' ? 'selected' : '' ?>>Deutsch</option>
                        <option value="en" <?= current_locale() === 'en' ? 'selected' : '' ?>>English</option>
                    </select>
                </form>
                <span class="topbar-user"><?= e($auth->userName() ?? 'Admin') ?></span>
                <a href="/admin/logout" class="topbar-link"><?= __('admin.topbar.logout') ?></a>
            </div>
        </header>

        <div class="admin-content" id="admin-main-content">
            <?php $flashSuccess = flash('success'); ?>
            <?php if ($flashSuccess): ?>
            <div class="alert alert-success" role="status" aria-live="polite"><?= e($flashSuccess) ?></div>
            <?php endif; ?>

            <?php $flashWarning = flash('warning'); ?>
            <?php if ($flashWarning): ?>
            <div class="alert alert-warning" role="status" aria-live="polite"><?= e($flashWarning) ?></div>
            <?php endif; ?>

            <?php $flashError = flash('error'); ?>
            <?php if ($flashError): ?>
            <div class="alert alert-error" role="alert" aria-live="assertive"><?= e($flashError) ?></div>
            <?php endif; ?>

            <?php
            // Persistent theme-fallback warning (ADR-088): shown on every admin page
            // as long as the configured theme is missing from the filesystem.
            $_iw_themeFallback = isset($_SESSION['_iw_theme_fallback']) ? (string) $_SESSION['_iw_theme_fallback'] : '';
            ?>
            <?php if ($_iw_themeFallback !== ''): ?>
            <div class="alert alert-warning" role="alert" aria-live="polite" style="border-left:4px solid #d97706;">
                ⚠ <strong><?= __('admin.theme_fallback.title') ?></strong>
                <?= __('admin.theme_fallback.text', ['theme' => e($_iw_themeFallback)]) ?>
                <a href="/admin/themes" style="margin-left:0.5rem;font-weight:600;"><?= __('admin.theme_fallback.link') ?></a>
            </div>
            <?php endif; ?>

            <?= $content ?? '' ?>
        </div>
    </main>

    <script src="/admin/assets/js/admin.js?v=<?= cms_version() ?>-<?= @filemtime($_SERVER['DOCUMENT_ROOT'].'/admin/assets/js/admin.js') ?: cms_version() ?>"></script>
</body>
</html>
