<!DOCTYPE html>
<html lang="<?= e(setting('locale', 'en')) ?>">
<head>
    <meta charset="UTF-8">
    <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
    // ── Canonical URL ──────────────────────────────────────────────────────────
    $canonicalUrl = $canonical_url ?? '';
    if ($canonicalUrl):
    ?>
    <link rel="canonical" href="<?= e($canonicalUrl) ?>">
    <?php endif; ?>

    <?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="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 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">
                        <?php if (!empty($main_menu)): ?>
                        <?php foreach ($main_menu as $item): ?>
                        <li>
                            <a href="<?= e($item['url']) ?>"
                               target="<?= e($item['target'] ?? '_self') ?>"
                               class="<?= ($current_uri ?? '') === $item['url'] ? 'active' : '' ?>">
                                <?= e($item['label']) ?>
                            </a>
                        </li>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </ul>
                </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-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"></script>
    <script src="<?= theme_asset('js/theme.js') ?>"></script>

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