<!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') ?>">
                <?php
                // Admin sidebar always shows the ITSWEBER CMS logo (white-filtered).
                // Theme-specific branding is intentionally NOT used here.
                ?>
                <img src="/admin/assets/images/brand/itsweber-logo.png"
                     alt="ITSWEBER CMS"
                     class="sidebar-brand-logo"
                     onerror="this.style.display='none';this.nextElementSibling.style.display='inline'">
                <span style="display:none">ITSWEBER CMS</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_bp = defined('BASE_PATH') ? BASE_PATH : dirname(__DIR__, 3);
            $_iw_maintUser   = is_file($_iw_bp . '/storage/.maintenance_mode');  // Manual via Admin UI
            $_iw_maintSystem = is_file($_iw_bp . '/storage/.maintenance');       // Auto via Backup/Update
            $_iw_maintActive = $_iw_maintUser || $_iw_maintSystem;
            ?>
            <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; ?>

            <?php
            // Plugin load error warning (Phase 2.4.1 hardening):
            // Shows which plugins failed to load and were auto-deactivated.
            if (class_exists('\\App\\Services\\PluginLoader')) {
                $_iw_pluginErrors = \App\Services\PluginLoader::getLoadErrors(defined('BASE_PATH') ? BASE_PATH : dirname(__DIR__, 3));
                if (!empty($_iw_pluginErrors)):
            ?>
            <div class="alert alert-error" role="alert" aria-live="assertive" style="border-left:4px solid #dc2626;">
                <strong><?= __('admin.plugins.load_error_title') ?></strong> <?= __('admin.plugins.load_error_text') ?>
                <ul style="margin:0.5rem 0 0;padding-left:1.25rem;">
                <?php foreach ($_iw_pluginErrors as $_iw_pSlug => $_iw_pErr): ?>
                    <li><strong><?= htmlspecialchars($_iw_pSlug, ENT_QUOTES, 'UTF-8') ?></strong>: <?= htmlspecialchars($_iw_pErr, ENT_QUOTES, 'UTF-8') ?></li>
                <?php endforeach; ?>
                </ul>
                <div style="margin-top:0.5rem;">
                    <a href="/admin/plugins" style="font-weight:600;"><?= __('admin.nav.plugins') ?> &rarr;</a>
                </div>
            </div>
            <?php
                endif;
                // Safe mode indicator
                if (\App\Services\PluginLoader::isSafeMode(defined('BASE_PATH') ? BASE_PATH : dirname(__DIR__, 3))):
            ?>
            <div class="alert alert-warning" role="alert" aria-live="polite" style="border-left:4px solid #d97706;">
                <strong><?= __('admin.plugins.safe_mode_title') ?></strong> <?= __('admin.plugins.safe_mode_text') ?>
            </div>
            <?php
                endif;
            }
            ?>

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

    <script>
    window.iwT = <?= json_encode([
        'search_images'   => __('admin.js.search_images'),
        'folder_label'    => __('admin.js.folder_label'),
        'all_media'       => __('admin.js.all_media'),
        'unassigned'      => __('admin.js.unassigned'),
        'loading'         => __('admin.js.loading'),
        'no_images'       => __('admin.js.no_images'),
        'load_failed'     => __('admin.js.load_failed'),
        'select_image'    => __('admin.js.select_image'),
        'upload_btn'      => __('admin.js.upload_btn'),
        'search'          => __('admin.js.search'),
        'file_too_large'  => __('admin.js.file_too_large'),
        'csrf_missing'    => __('admin.js.csrf_missing'),
        'uploading'       => __('admin.js.uploading'),
        'upload_failed'   => __('admin.js.upload_failed'),
        'upload_http_err' => __('admin.js.upload_http_err'),
        'network_error'   => __('admin.js.network_error'),
        'upload_timeout'  => __('admin.js.upload_timeout'),
    ], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG) ?>;
    </script>
    <script src="/admin/assets/js/admin.js?v=<?= cms_version() ?>-<?= @filemtime($_SERVER['DOCUMENT_ROOT'].'/admin/assets/js/admin.js') ?: cms_version() ?>"></script>
</body>
</html>
