<!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       ?? '';

    // Fallback: branding logo (Phase 6d) or legacy logo for 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') ?>">

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

    <?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 if (setting('header_show_site_name', '0') === '1'): ?>
                    <span class="logo-text"><?= e(setting('site_name', 'ITSWEBER CMS')) ?></span>
                    <?php endif; ?>
                    <?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 ────────────────────────────────────────────────────────
        // $breadcrumbs = [['label' => '...', 'url' => '...|null'], ...]
        // Last item has url=null. Only render when >=2 crumbs and globally enabled.
        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-grid">

                <!-- Brand column -->
                <div class="footer-brand">
                    <?php
                    $footerLogoUrl = function_exists('branding_logo_url') ? branding_logo_url('footer') : null;
                    if ($footerLogoUrl): ?>
                    <a href="<?= base_url() ?>" class="footer-brand-logo">
                        <img src="<?= e($footerLogoUrl) ?>" alt="<?= e(setting('site_name', '')) ?>">
                    </a>
                    <?php else: ?>
                    <a href="<?= base_url() ?>" class="footer-brand-logo footer-brand-text"><?= e(setting('site_name', 'ITSWEBER CMS')) ?></a>
                    <?php endif; ?>

                    <?php if ($tagline = setting('site_tagline')): ?>
                    <p class="footer-tagline"><?= e($tagline) ?></p>
                    <?php elseif ($footerText = setting('footer_text')): ?>
                    <p class="footer-tagline"><?= sanitize_html($footerText) ?></p>
                    <?php endif; ?>

                    <?php
                    $socialLinks = [
                        'social_facebook'  => ['facebook',  'Facebook'],
                        'social_instagram' => ['instagram', 'Instagram'],
                        'social_linkedin'  => ['linkedin',  'LinkedIn'],
                        'social_x'         => ['twitter',   'X'],
                        'social_youtube'   => ['youtube',   'YouTube'],
                        'social_discord'   => ['discord',   'Discord'],
                        'social_twitch'    => ['twitch',    'Twitch'],
                        'social_github'    => ['github',    'GitHub'],
                        'social_tiktok'    => ['tiktok',    'TikTok'],
                    ];
                    $hasSocial = false;
                    foreach ($socialLinks as $sk => $v) { if (setting($sk)) { $hasSocial = true; break; } }
                    if ($hasSocial): ?>
                    <nav class="footer-social" aria-label="Social Media">
                        <?php foreach ($socialLinks as $settingKey => [$iconName, $label]):
                            if ($url = setting($settingKey)): ?>
                        <a href="<?= e($url) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= e($label) ?>" title="<?= e($label) ?>">
                            <?= render_icon($iconName, 20) ?>
                        </a>
                        <?php endif; endforeach; ?>
                    </nav>
                    <?php endif; ?>
                </div>

                <!-- Navigation column -->
                <?php if (!empty($footer_menu)): ?>
                <div class="footer-col">
                    <h3 class="footer-col-title"><?= __('theme.footer_navigation') ?></h3>
                    <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>
                </div>
                <?php endif; ?>

                <!-- Contact column -->
                <?php if (setting('footer_show_address', '0') === '1' || setting('contact_email') || setting('contact_phone')): ?>
                <div class="footer-col">
                    <h3 class="footer-col-title"><?= __('theme.footer_contact') ?></h3>
                    <?php if (setting('footer_show_address', '0') === '1'): ?>
                        <?php if ($street = setting('local_business_street')): ?>
                        <p class="footer-contact-line"><?= e($street) ?></p>
                        <?php if ($city = setting('local_business_city')): ?>
                        <p class="footer-contact-line"><?= e(setting('local_business_postal', '')) ?> <?= e($city) ?></p>
                        <?php endif; endif; ?>
                        <?php if ($hours = setting('local_business_hours')): ?>
                        <p class="footer-contact-line footer-hours"><?= e($hours) ?></p>
                        <?php endif; ?>
                    <?php endif; ?>
                    <?php if ($email = setting('contact_email')): ?>
                    <p class="footer-contact-line"><a href="mailto:<?= e($email) ?>"><?= e($email) ?></a></p>
                    <?php endif; ?>
                    <?php if ($phone = setting('contact_phone')): ?>
                    <p class="footer-contact-line"><a href="tel:<?= e(preg_replace('/[^+0-9]/', '', $phone)) ?>"><?= e($phone) ?></a></p>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

            </div><!-- /.footer-grid -->

            <div class="footer-bottom">
                <p class="footer-copyright"><?= sanitize_html(setting('copyright', '&copy; ' . date('Y') . ' ' . setting('site_name', 'ITSWEBER CMS') . '. ' . (__('theme.all_rights')))) ?></p>
                <?= consent_reopen_link() ?>
            </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 (Phase 6d) */ ?>
    <?= consent_banner() ?>
</body>
</html>
