<!DOCTYPE html>
<html lang="<?= e(setting('locale', 'en')) ?>"<?php if (setting('dark_mode_enabled', '0') === '1'): ?> data-dark-mode<?php endif; ?><?php if (setting('sticky_header_enabled', '0') === '1'): ?> data-sticky-header<?php endif; ?><?php if (setting('scroll_animations_enabled', '0') === '1'): ?> data-scroll-animations<?php endif; ?>>
<head>
    <meta charset="UTF-8">
    <?php if (setting('dark_mode_enabled', '0') === '1'): ?><script>try{var t=localStorage.getItem('iw-theme');if(t)document.documentElement.setAttribute('data-theme',t)}catch(e){}</script><?php endif; ?>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <?php
    // ── Title ──────────────────────────────────────────────────────────────────
    $siteTitle = setting('site_name', 'ITSWEBER CMS');
    $pageTitle = $seo_title ?? $page_title ?? $siteTitle;
    ?>
    <title><?= e($pageTitle) ?></title>

    <?php if (!empty($seo_description)): ?>
    <meta name="description" content="<?= e($seo_description) ?>">
    <?php endif; ?>
    <?php if (!empty($robots_meta ?? '')): ?>
    <meta name="robots" content="<?= e($robots_meta) ?>">
    <?php endif; ?>

    <?php
    // ── Canonical URL ──────────────────────────────────────────────────────────
    $canonicalUrl = $canonical_url ?? '';
    if ($canonicalUrl):
    ?>
    <link rel="canonical" href="<?= e($canonicalUrl) ?>">
    <?php endif; ?>
    <link rel="alternate" type="application/rss+xml" title="<?= e(setting('site_name', 'Blog')) ?> RSS" href="<?= base_url('/rss') ?>">

    <?php
    // ── Open Graph ─────────────────────────────────────────────────────────────
    $ogType        = $og_type        ?? 'website';
    $ogTitle       = $og_title       ?? $pageTitle;
    $ogDescription = $og_description ?? ($seo_description ?? '');
    $ogUrl         = $canonicalUrl ?: base_url($current_uri ?? '/');
    $ogImage       = $og_image       ?? '';

    if (empty($ogImage)) {
        $ogLogoUrl = branding_logo_url('header');
        if ($ogLogoUrl !== null) {
            $ogImage = base_url($ogLogoUrl);
        }
    }
    ?>
    <meta property="og:type"      content="<?= e($ogType) ?>">
    <meta property="og:title"     content="<?= e($ogTitle) ?>">
    <?php if ($ogDescription !== ''): ?>
    <meta property="og:description" content="<?= e($ogDescription) ?>">
    <?php endif; ?>
    <meta property="og:url"       content="<?= e($ogUrl) ?>">
    <?php if ($ogImage !== ''): ?>
    <meta property="og:image"     content="<?= e($ogImage) ?>">
    <?php endif; ?>
    <?php if ($siteName = setting('site_name')): ?>
    <meta property="og:site_name" content="<?= e($siteName) ?>">
    <?php endif; ?>

    <?php
    // ── Twitter Card ───────────────────────────────────────────────────────────
    if ($ogTitle !== '' || $ogDescription !== ''):
    ?>
    <meta name="twitter:card"        content="<?= $ogImage !== '' ? 'summary_large_image' : 'summary' ?>">
    <meta name="twitter:title"       content="<?= e($ogTitle) ?>">
    <?php if ($ogDescription !== ''): ?>
    <meta name="twitter:description" content="<?= e($ogDescription) ?>">
    <?php endif; ?>
    <?php if ($ogImage !== ''): ?>
    <meta name="twitter:image"       content="<?= e($ogImage) ?>">
    <?php endif; ?>
    <?php endif; ?>

    <link rel="preload" href="/admin/assets/fonts/inter/inter-latin-400.woff2" as="font" type="font/woff2" crossorigin>
    <link rel="preload" href="/admin/assets/fonts/inter/inter-latin-600.woff2" as="font" type="font/woff2" crossorigin>
    <link rel="stylesheet" href="/assets/css/fonts.css?v=<?= cms_version() ?>">
    <link rel="stylesheet" href="<?= theme_asset('css/style.css') ?>">

    <?php if (consent_enabled()): ?>
    <link rel="stylesheet" href="/assets/css/consent.css">
    <?php endif; ?>
    <link rel="stylesheet" href="/assets/css/gallery-lightbox.css">

    <?php if ($favicon = setting('favicon')): ?>
    <link rel="icon" href="/uploads/<?= e($favicon) ?>">
    <?php else: ?>
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <?php endif; ?>

    <?php
    // ── JSON-LD Structured Data (v3.2.0) ──────────────────────────────────
    echo json_ld_website();
    echo json_ld_organization();
    if (!empty($post) && isset($post['slug'])):
        echo json_ld_article($post);
    endif;
    if (!empty($event) && isset($event['slug'])):
        echo json_ld_event($event);
    endif;
    if (!empty($breadcrumbs) && count($breadcrumbs) >= 2):
        echo json_ld_breadcrumbs($breadcrumbs);
    endif;
    ?>

    <?php if (setting('custom_css_js_enabled', '1') === '1' && !empty($custom_css ?? '')): ?>
    <style><?= $custom_css ?></style>
    <?php endif; ?>
    <?php run_hook('frontend.head'); ?>
</head>
<body>
    <a href="#main-content" class="skip-link">Skip to main content</a>
    <header class="site-header">
        <div class="container">
            <div class="header-inner">
                <a href="/" class="site-logo" aria-label="<?= e(setting('site_name', 'Website')) ?> – Startseite">
                    <?php
                    $headerLogoUrl = branding_logo_url('header');
                    $headerMode    = branding_logo_mode('header');
                    if ($headerLogoUrl !== null):
                    ?>
                    <img src="<?= e($headerLogoUrl) ?>"
                         alt="<?= e(setting('logo_alt') ?: setting('site_name', 'Logo')) ?>"
                         class="logo-mode-<?= e($headerMode) ?>">
                    <?php else: ?>
                    <span class="logo-text"><?= e(setting('site_name', 'ITSWEBER CMS')) ?></span>
                    <?php endif; ?>
                </a>

                <nav class="main-nav" aria-label="Main navigation">
                    <button class="nav-toggle" id="navToggle" aria-label="Menu" aria-expanded="false" aria-controls="navList">&#9776;</button>
                    <ul class="nav-list" id="navList" role="menubar">
                        <?php if (!empty($main_menu_tree)): ?>
                            <?= render_nav_items($main_menu_tree, $current_uri ?? '') ?>
                        <?php elseif (!empty($main_menu)): ?>
                            <?php foreach ($main_menu as $item): ?>
                            <li role="none">
                                <a href="<?= e($item['url']) ?>"
                                   target="<?= e($item['target'] ?? '_self') ?>"
                                   role="menuitem"
                                   class="<?= ($current_uri ?? '') === $item['url'] ? 'active' : '' ?>">
                                    <?= e($item['label']) ?>
                                </a>
                            </li>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </ul>
                    <?php if (setting('search_enabled', '1') === '1' && setting('search_in_header', '1') === '1'): ?>
                    <div class="header-search">
                        <button type="button" class="header-search-toggle" aria-label="<?= e(__('theme.search_title')) ?>" aria-expanded="false">
                            <?= render_icon('search', 20) ?>
                        </button>
                        <div class="header-search-dropdown" hidden>
                            <form action="/search" method="get" role="search">
                                <input type="search" name="q" placeholder="<?= e(__('theme.search_placeholder')) ?>"
                                       aria-label="<?= e(__('theme.search_placeholder')) ?>" autocomplete="off">
                                <button type="submit" aria-label="<?= e(__('theme.search_button')) ?>">
                                    <?= render_icon('search', 18) ?>
                                </button>
                            </form>
                        </div>
                    </div>
                    <?php endif; ?>
                    <?php if (setting('dark_mode_enabled', '0') === '1'): ?>
                    <button class="theme-toggle" id="themeToggle" type="button" aria-label="Toggle dark mode">
                        <svg class="icon-sun" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><path d="M12 1v2M12 21v2M4.22 4.22l1.42 1.42M18.36 18.36l1.42 1.42M1 12h2M21 12h2M4.22 19.78l1.42-1.42M18.36 5.64l1.42-1.42"/></svg>
                        <svg class="icon-moon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
                    </button>
                    <?php endif; ?>
                </nav>
            </div>
        </div>
    </header>

    <?php run_hook('frontend.after_header'); ?>

    <main class="site-main" id="main-content">
        <?php
        // ── Breadcrumbs ────────────────────────────────────────────────────────
        // Only render when globally enabled and >=2 crumbs.
        if (breadcrumbs_enabled() && !empty($breadcrumbs) && count($breadcrumbs) > 1):
        ?>
        <nav class="breadcrumbs" aria-label="Breadcrumb">
            <div class="container">
                <ol class="breadcrumb-list" itemscope itemtype="https://schema.org/BreadcrumbList">
                    <?php foreach ($breadcrumbs as $i => $crumb):
                        $isLast = ($i === count($breadcrumbs) - 1);
                    ?>
                    <li class="breadcrumb-item<?= $isLast ? ' breadcrumb-current' : '' ?>"
                        itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
                        <?php if (!$isLast && !empty($crumb['url'])): ?>
                        <a href="<?= e($crumb['url']) ?>" itemprop="item">
                            <span itemprop="name"><?= e($crumb['label']) ?></span>
                        </a>
                        <?php else: ?>
                        <span itemprop="name"><?= e($crumb['label']) ?></span>
                        <?php endif; ?>
                        <meta itemprop="position" content="<?= $i + 1 ?>">
                    </li>
                    <?php endforeach; ?>
                </ol>
            </div>
        </nav>
        <?php endif; ?>

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

    <?php run_hook('frontend.before_footer'); ?>

    <footer class="site-footer">
        <div class="container">
            <div class="footer-inner">
                <div class="footer-info">
                    <p class="footer-copyright"><?= sanitize_html(setting('copyright', '© ITSWEBER. All rights reserved.')) ?></p>
                    <?php if ($footerText = setting('footer_text')): ?>
                    <p class="footer-text"><?= sanitize_html($footerText) ?></p>
                    <?php endif; ?>
                </div>

                <?php if (!empty($footer_menu)): ?>
                <nav class="footer-nav" aria-label="Footer navigation">
                    <?php foreach ($footer_menu as $item): ?>
                    <a href="<?= e($item['url']) ?>" target="<?= e($item['target'] ?? '_self') ?>"><?= e($item['label']) ?></a>
                    <?php endforeach; ?>
                </nav>
                <?php endif; ?>

                <div class="footer-social">
                    <?php if ($fb = setting('social_facebook')): ?><a href="<?= e($fb) ?>" target="_blank" rel="noopener">Facebook</a><?php endif; ?>
                    <?php if ($ig = setting('social_instagram')): ?><a href="<?= e($ig) ?>" target="_blank" rel="noopener">Instagram</a><?php endif; ?>
                    <?php if ($li = setting('social_linkedin')): ?><a href="<?= e($li) ?>" target="_blank" rel="noopener">LinkedIn</a><?php endif; ?>
                    <?php if ($xUrl = setting('social_x')): ?><a href="<?= e($xUrl) ?>" target="_blank" rel="noopener">X</a><?php endif; ?>
                    <?php if ($yt = setting('social_youtube')): ?><a href="<?= e($yt) ?>" target="_blank" rel="noopener">YouTube</a><?php endif; ?>
                    <?= consent_reopen_link() ?>
                </div>
            </div>
        </div>
    </footer>

    <!-- Scroll-to-Top Button (v3.0.4) -->
    <button class="scroll-to-top" id="scrollToTop" aria-label="Scroll to top" title="<?= __('theme.scroll_to_top') ?? 'Back to top' ?>">
        <svg width="20" height="20" viewBox="0 0 20 20" fill="none" aria-hidden="true">
            <path d="M10 15V5M10 5L5 10M10 5L15 10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
    </button>

    <script src="/assets/js/gallery-lightbox.js" defer></script>
    <script src="<?= theme_asset('js/theme.js') ?>" defer></script>
    <script src="/assets/js/header-search.js" defer></script>

    <?php if (setting('custom_css_js_enabled', '1') === '1' && !empty($custom_js ?? '')): ?>
    <script><?= $custom_js ?></script>
    <?php endif; ?>
    <?php run_hook('frontend.footer'); ?>

    <?php /* Consent Banner */ ?>
    <?= consent_banner() ?>
</body>
</html>
