<!doctype html>
<html lang="<?= cw_h((string)($site['language'] ?? 'de')) ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="theme-color" content="#102532">
<title><?= cw_h($pageTitle) ?><?= $currentRoute !== 'home' ? ' · ' . cw_h((string)($site['name'] ?? 'CW')) : '' ?></title>
<meta name="description" content="<?= cw_h($pageDescription) ?>">
<?php if (!empty($pageCanonical)): ?><link rel="canonical" href="<?= cw_h((string)$pageCanonical) ?>"><?php endif; ?>
<?php if (!empty($pageOgTitle)): ?><meta property="og:title" content="<?= cw_h((string)$pageOgTitle) ?>"><?php endif; ?>
<?php if (!empty($pageOgDescription)): ?><meta property="og:description" content="<?= cw_h((string)$pageOgDescription) ?>"><?php endif; ?>
<?php if (!empty($pageOgUrl)): ?><meta property="og:url" content="<?= cw_h((string)$pageOgUrl) ?>"><?php endif; ?>
<?php if (!empty($pageOgType)): ?><meta property="og:type" content="<?= cw_h((string)$pageOgType) ?>"><?php endif; ?>
<?php if (!empty($pageOgImage)): ?><meta property="og:image" content="<?= cw_h((string)$pageOgImage) ?>"><?php endif; ?>
<link rel="icon" href="<?= cw_h(cw_asset('img/cw-control-watch-logo.svg')) ?>" type="image/svg+xml">
<link rel="stylesheet" href="<?= cw_h(cw_base_path()) ?>/assets/css/bootstrap.min.css?v=5.3.6">
<link rel="stylesheet" href="<?= cw_h(cw_asset('css/site.css')) ?>">
<?php if ($currentRoute === 'home' && is_file(project_root() . '/project/assets/css/blog.css')): ?><link rel="stylesheet" href="<?= cw_h(cw_asset('css/blog.css')) ?>"><?php endif; ?>
<link rel="stylesheet" href="<?= cw_h(cw_base_path()) ?>/assets/css/downloads.css?v=<?= cw_h((string)(@filemtime(project_root() . '/assets/css/downloads.css') ?: '1')) ?>">
<?= cw_search_frontend_style_html() ?>
<?php foreach ((array)($pageStylesheets ?? []) as $stylesheet): ?><link rel="stylesheet" href="<?= cw_h((string)$stylesheet) ?>"><?php endforeach; ?>
<?php if (!empty($pageInlineCss)): ?><style><?= (string)$pageInlineCss ?></style><?php endif; ?>
</head>
<body class="cw-site route-<?= cw_h(preg_replace('/[^a-z0-9_-]/', '-', strtolower($currentRoute)) ?: 'page') ?>">
<a class="cw-skip-link" href="#content">Zum Inhalt</a>
<?php
$mainMenuSettings = cw_menu_settings('main');
$mainMenuTrigger = (string)($mainMenuSettings['dropdown_trigger'] ?? 'hover');
$navContent = static function (array $item): string {
    $label = cw_h((string)($item['label'] ?? ''));
    $display = (string)($item['display'] ?? 'text');
    $icon = (string)($item['icon'] ?? '');
    $position = (string)($item['icon_position'] ?? 'left');
    $showIcon = $icon !== '' && $display !== 'text';
    $iconHtml = $showIcon ? cw_icon_svg($icon, 18, 'cw-nav-item-icon') : '';
    if ($display === 'icon_only' && $iconHtml !== '') {
        return '<span class="cw-nav-item-content cw-nav-icon-only" aria-hidden="true">' . $iconHtml . '</span><span class="visually-hidden">' . $label . '</span>';
    }
    $classes = 'cw-nav-item-content' . ($showIcon ? ' cw-nav-icon-pos-' . cw_h($position) : '');
    if (!$showIcon) return '<span class="' . $classes . '"><span class="cw-nav-item-label">' . $label . '</span></span>';
    if (in_array($position, ['right','bottom'], true)) {
        return '<span class="' . $classes . '"><span class="cw-nav-item-label">' . $label . '</span>' . $iconHtml . '</span>';
    }
    return '<span class="' . $classes . '">' . $iconHtml . '<span class="cw-nav-item-label">' . $label . '</span></span>';
};
$navClass = static function (array $item, bool $active, string $base = 'nav-link'): string {
    $display = (string)($item['display'] ?? 'text');
    $style = (string)($item['button_style'] ?? 'standard');
    $classes = [$base];
    if (!empty($item['as_button'])) {
        $classes[] = 'cw-nav-display-button';
        $classes[] = 'cw-nav-button-style-' . (in_array($style, ['standard','outline','accent','ghost'], true) ? $style : 'standard');
    }
    if ($display === 'icon_only') $classes[] = 'cw-nav-display-icon-only';
    if ($active) $classes[] = 'active';
    return implode(' ', $classes);
};
$navTitleValue = static function (array $item): string {
    $title = trim((string)($item['title'] ?? ''));
    if ($title === '' && (string)($item['display'] ?? 'text') === 'icon_only') {
        $title = trim((string)($item['label'] ?? ''));
    }
    return $title;
};
$navTitleAttr = static function (array $item) use ($navTitleValue): string {
    $title = $navTitleValue($item);
    return $title !== '' ? ' title="' . cw_h($title) . '"' : '';
};
$navLinkA11yAttrs = static function (array $item) use ($navTitleAttr): string {
    $attrs = $navTitleAttr($item);
    if ((string)($item['display'] ?? 'text') === 'icon_only') {
        $label = trim((string)($item['label'] ?? ''));
        if ($label !== '') $attrs .= ' aria-label="' . cw_h($label) . '"';
    }
    return $attrs;
};
?>
<header class="cw-header sticky-top" data-cw-header>
    <nav class="navbar navbar-expand-xl navbar-light" aria-label="Hauptnavigation" data-cw-dropdown-trigger="<?= cw_h($mainMenuTrigger) ?>">
        <div class="container-xxl px-3 px-lg-4">
            <a class="navbar-brand cw-brand" href="<?= cw_h(cw_url('home')) ?>" data-cw-track="navigation" data-cw-track-id="logo-home" aria-label="CW – Control & Watch Startseite">
                <img src="<?= cw_h(cw_asset('img/cw-control-watch-logo.svg')) ?>" alt="CW – Control & Watch" width="226" height="85">
            </a>
            <button class="navbar-toggler cw-navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#cwMainNav" aria-controls="cwMainNav" aria-expanded="false" aria-label="Navigation öffnen">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="cwMainNav">
                <ul class="navbar-nav ms-auto align-items-xl-center gap-xl-1 pt-3 pt-xl-0">
                    <?php foreach (cw_navigation_tree() as $item):
                        $children = (array)($item['children'] ?? []);
                        $selfActive = (string)($item['route'] ?? '') !== '' && (string)$item['route'] === $currentRoute;
                        $childActive = false;
                        foreach ($children as $child) {
                            if ((string)($child['route'] ?? '') !== '' && (string)$child['route'] === $currentRoute) { $childActive = true; break; }
                        }
                        $active = $selfActive || $childActive;
                        $hasUrl = trim((string)($item['url'] ?? '')) !== '' && (string)($item['type'] ?? '') !== 'group';
                    ?>
                        <?php if ((string)($item['type'] ?? '') === 'action' && (string)($item['action'] ?? '') === 'search'): ?>
                            <li class="nav-item"><a href="<?= cw_h((string)$item['url']) ?>" class="<?= cw_h($navClass($item, false, 'nav-link')) ?>"<?= $navLinkA11yAttrs($item) ?> data-cw-search-trigger="<?= cw_h((string)($item['search_profile'] ?? 'header')) ?>" aria-haspopup="dialog"><?= $navContent($item) ?></a></li>
                        <?php elseif ($children): ?>
                            <li class="nav-item dropdown cw-nav-dropdown">
                                <?php if (!$hasUrl): ?>
                                    <button class="<?= cw_h($navClass($item, $active, 'nav-link')) ?> cw-nav-group-toggle dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="<?= cw_h((string)$item['label']) ?> Menü öffnen"<?= $navTitleAttr($item) ?>><?= $navContent($item) ?></button>
                                <?php else: ?>
                                    <div class="cw-nav-parent<?= !empty($item['as_button']) ? ' cw-nav-parent--button' : '' ?>">
                                        <a href="<?= cw_h((string)$item['url']) ?>" class="<?= cw_h($navClass($item, $active, 'nav-link')) ?>"<?= $navLinkA11yAttrs($item) ?> <?= $selfActive ? 'aria-current="page"' : '' ?><?= !empty($item['new_window']) ? ' target="_blank" rel="noopener"' : '' ?>><?= $navContent($item) ?></a>
                                        <button class="cw-nav-caret dropdown-toggle dropdown-toggle-split<?= !empty($item['as_button']) ? ' cw-nav-caret--button cw-nav-button-style-' . cw_h((string)($item['button_style'] ?? 'standard')) : '' ?>" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Untermenü <?= cw_h((string)$item['label']) ?> öffnen"></button>
                                    </div>
                                <?php endif; ?>
                                <ul class="dropdown-menu cw-nav-dropdown-menu">
                                    <?php foreach ($children as $child):
                                        $isChildActive = (string)($child['route'] ?? '') !== '' && (string)$child['route'] === $currentRoute;
                                        $childHasUrl = trim((string)($child['url'] ?? '')) !== '' && (string)($child['type'] ?? '') !== 'group';
                                    ?>
                                        <?php if (!$childHasUrl): ?>
                                            <li><span class="dropdown-item-text cw-nav-dropdown-label"<?= $navTitleAttr($child) ?>><?= $navContent($child) ?></span></li>
                                        <?php else: ?>
                                            <li><a class="<?= cw_h($navClass($child, $isChildActive, 'dropdown-item')) ?>" href="<?= cw_h((string)$child['url']) ?>"<?= $navLinkA11yAttrs($child) ?> <?= $isChildActive ? 'aria-current="page"' : '' ?><?= !empty($child['new_window']) ? ' target="_blank" rel="noopener"' : '' ?>><?= $navContent($child) ?></a></li>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </ul>
                            </li>
                        <?php elseif (!$hasUrl): ?>
                            <li class="nav-item"><span class="<?= cw_h($navClass($item, false, 'nav-link')) ?> cw-nav-group-label"<?= $navTitleAttr($item) ?>><?= $navContent($item) ?></span></li>
                        <?php else: ?>
                            <li class="nav-item"><a href="<?= cw_h((string)$item['url']) ?>" class="<?= cw_h($navClass($item, $active, 'nav-link')) ?>"<?= $navLinkA11yAttrs($item) ?> <?= $active ? 'aria-current="page"' : '' ?><?= !empty($item['new_window']) ? ' target="_blank" rel="noopener"' : '' ?>><?= $navContent($item) ?></a></li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </nav>
</header>

<main id="content">
