<?php
$currentUser  = Auth::user();
$theme        = $_SESSION['theme'] ?? ($currentUser['theme'] ?? 'light');
$unreadNotifs    = 0;
$pendingGroupReqs = 0;
if ($currentUser) {
    $unreadNotifs = Database::getInstance()->fetch(
        'SELECT COUNT(*) as c FROM notifications WHERE user_id = ? AND is_read = 0',
        [$currentUser['id']]
    )['c'] ?? 0;
    $pendingGroupReqs = Auth::isAdmin() ? (int)PluginManager::filter('admin_pending_count', 0) : 0;
}
$appName   = Helper::getSetting('app_name', 'Plenvo');
$csrfToken = Auth::generateCsrfToken();

// Current URI (strip base for active-link detection)
$rawUri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$base   = defined('APP_BASE') ? APP_BASE : '';
$uri    = ($base !== '' && str_starts_with($rawUri, $base))
          ? substr($rawUri, strlen($base))
          : $rawUri;

// ── Output buffer: auto-prefix all internal absolute links with APP_BASE ──────
if ($base !== '') {
    ob_start(function (string $html) use ($base): string {
        // Skip URLs that already start with $base/, so a render-time APP_BASE
        // prefix (used e.g. in the WebAuthn fetch() calls) is not doubled to
        // /mp/mp/foo on subfolder installs.
        $skip = preg_quote(ltrim($base, '/'), '/');
        // href="/...", action="/...", src="/..." but NOT href="//..." (protocol-relative)
        $html = preg_replace('/(href|action|src)="\/(?!\/|' . $skip . '\/)/', '$1="' . $base . '/', $html);
        // JS: fetch('/api/...'), fetch("/api/...")
        $html = preg_replace("/fetch\('\/(?!\/|" . $skip . "\/)/",  "fetch('" . $base . '/', $html);
        $html = preg_replace('/fetch\("\/(?!\/|' . $skip . '\/)/',  'fetch("' . $base . '/', $html);
        // JS: location.href = '/...'  or  location.href='/...'
        $html = preg_replace("/(location\.href\s*=\s*)'\/(?!\/|" . $skip . "\/)/", '$1\'' . $base . '/', $html);
        $html = preg_replace('/(location\.href\s*=\s*)"\/(?!\/|' . $skip . '\/)/', '$1"' . $base . '/', $html);
        return $html;
    });
}
?>
<?php
$_themeMode = in_array($theme, ['dark', 'midnight', 'ocean', 'matrix', 'bluescreen'], true) ? 'dark' : 'light';
?>
<!DOCTYPE html>
<html lang="<?= Lang::current() ?>" data-theme="<?= Helper::e($theme) ?>" data-theme-mode="<?= $_themeMode ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= Helper::e($appName) ?><?= isset($pageTitle) ? ' – ' . Helper::e($pageTitle) : '' ?></title>
    <link rel="stylesheet" href="/assets/css/app.css?v=<?= APP_VERSION ?>">
    <link rel="icon" href="/assets/img/favicon.svg" type="image/svg+xml">
    <meta name="csrf-token" content="<?= $csrfToken ?>">
</head>
<body>
<div class="app-layout">
    <!-- Sidebar -->
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <a href="/dashboard" class="sidebar-logo">
                <svg viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg" width="28" height="28">
                    <rect width="40" height="40" rx="8" fill="#4f46e5"/>
                    <path d="M10 14h20M10 20h14M10 26h18" stroke="white" stroke-width="2.5" stroke-linecap="round"/>
                </svg>
                <span class="sidebar-logo-text"><?= Helper::e($appName) ?></span>
            </a>
            <button class="sidebar-toggle" id="sidebarToggle" aria-label="Menü">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M18 6L6 18M6 6l12 12"/></svg>
            </button>
        </div>

        <nav class="sidebar-nav">
            <a href="/dashboard" class="nav-item <?= $uri === '/dashboard' ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>
                <?= t('dashboard') ?>
            </a>
            <a href="/projects" class="nav-item <?= str_starts_with($uri, '/project') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z"/></svg>
                <?= t('projects') ?>
            </a>
            <a href="/my-tasks" class="nav-item <?= str_starts_with($uri, '/my-tasks') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11"/></svg>
                <?= t('tasks') ?>
            </a>
            <?php if (Auth::isAdmin()): ?>
            <div class="nav-separator"></div>
            <span class="nav-label"><?= t('admin') ?></span>
            <a href="/admin/users" class="nav-item <?= str_starts_with($uri, '/admin/users') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 00-3-3.87M16 3.13a4 4 0 010 7.75"/></svg>
                <?= t('users') ?>
            </a>
            <?php PluginManager::fire('admin_nav_groups', ['uri' => $uri, 'pending' => $pendingGroupReqs]); ?>
            <a href="/admin/security-log" class="nav-item <?= str_starts_with($uri, '/admin/security-log') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
                <?= t('security_log_title') ?>
            </a>
            <a href="/admin/plugins" class="nav-item <?= str_starts_with($uri, '/admin/plugins') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M20.24 12.24a6 6 0 00-8.49-8.49L5 10.5V19h8.5z"/><line x1="16" y1="8" x2="2" y2="22"/><line x1="17.5" y1="15" x2="9" y2="15"/></svg>
                <?= t('plugins') ?>
            </a>
            <a href="/admin/settings" class="nav-item <?= str_starts_with($uri, '/admin/settings') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><circle cx="12" cy="12" r="3"/><path d="M19.07 4.93A10 10 0 000 11.22M4.93 19.07A10 10 0 0012 22a10 10 0 007.07-2.93"/></svg>
                <?= t('system_settings') ?>
            </a>
            <?php if (str_starts_with($uri, '/admin/settings')): ?>
            <div class="nav-subitems">
                <a href="/admin/settings#healthCheckCard"     class="nav-subitem"><?= t('health_check') ?></a>
                <a href="/admin/settings#inactiveCleanupCard" class="nav-subitem"><?= t('nav_inactive_cleanup') ?></a>
                <a href="/admin/settings#systemInfoCard"      class="nav-subitem"><?= t('system_info') ?></a>
                <a href="/admin/settings#scheduledUpdatesCard" class="nav-subitem"><?= t('auto_update_title') ?></a>
                <a href="/admin/settings#backupCard"          class="nav-subitem"><?= t('backup_title') ?></a>
                <a href="/admin/settings#appSettingsCard"     class="nav-subitem"><?= t('app_settings') ?></a>
                <a href="/admin/settings#domainLanguagesCard" class="nav-subitem"><?= t('domain_lang_title') ?></a>
                <a href="/admin/settings#securitySettingsCard" class="nav-subitem"><?= t('security_settings') ?></a>
                <a href="/admin/settings#mailQueueCard"       class="nav-subitem"><?= t('mail_queue_title') ?></a>
                <a href="/admin/settings#mailSettingsCard"    class="nav-subitem"><?= t('mail_settings') ?></a>
                <a href="/admin/settings#aiSettingsCard"      class="nav-subitem"><?= t('ai_settings') ?></a>
            </div>
            <?php endif; ?>
            <div class="nav-separator"></div>
            <a href="https://plenvo.gp-server.com/#contact" target="_blank" rel="noopener" class="nav-item">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/></svg>
                <?= t('nav_support') ?>
            </a>
            <?php else: ?>
            <div class="nav-separator"></div>
            <span class="nav-label"><?= t('settings') ?></span>
            <?php $inProfileContext = $uri === '/settings' || $uri === '/settings/totp'; ?>
            <a href="/settings" class="nav-item <?= $inProfileContext ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                <?= t('profile') ?>
            </a>
            <?php if ($inProfileContext): ?>
            <div class="nav-subitems">
                <a href="/settings#profileInfoCard"    class="nav-subitem"><?= t('my_profile') ?></a>
                <a href="/settings#changePasswordCard" class="nav-subitem"><?= t('change_password') ?></a>
                <a href="/settings#twoFactorCard"      class="nav-subitem <?= $uri === '/settings/totp' ? 'active' : '' ?>"><?= t('totp_settings') ?></a>
                <a href="/settings#deleteAccountCard"  class="nav-subitem"><?= t('delete_account') ?></a>
            </div>
            <?php endif; ?>
            <a href="/settings/preferences" class="nav-item <?= str_starts_with($uri, '/settings/preferences') ? 'active' : '' ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>
                <?= t('preferences') ?>
            </a>
            <?php PluginManager::fire('user_nav_groups', ['uri' => $uri]); ?>
            <?php endif; ?>
        </nav>

        <?php if ($currentUser): ?>
        <div class="sidebar-user">
            <a href="/settings" class="sidebar-user-info">
                <?= Helper::avatar($currentUser, 32) ?>
                <div class="sidebar-user-text">
                    <span class="sidebar-user-name"><?= Helper::e($currentUser['username']) ?></span>
                    <span class="sidebar-user-role"><?= t('role_' . $currentUser['role']) ?></span>
                </div>
            </a>
        </div>
        <?php endif; ?>
    </aside>

    <!-- Main content -->
    <div class="main-wrapper">
        <!-- Topbar -->
        <header class="topbar">
            <button class="topbar-menu-btn" id="menuBtn" aria-label="Menü öffnen">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20"><path d="M3 12h18M3 6h18M3 18h18"/></svg>
            </button>

            <?php if ($currentUser): ?>
            <button type="button" class="topbar-search" id="topbarSearch" data-click="openCmdK" aria-label="<?= t('cmdk_placeholder') ?>">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16" class="topbar-search-icon"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>
                <span class="topbar-search-text"><?= t('cmdk_placeholder') ?></span>
                <span class="topbar-search-kbd" id="topbarSearchKbd"><span class="kbd-mod">Ctrl</span><span class="kbd-plus">+</span><span>K</span></span>
            </button>
            <?php endif; ?>

            <div class="topbar-spacer"></div>

            <div class="topbar-actions">
                <?php if (Auth::isAdmin()):
                    $_hcState = HealthCheck::lastCronState();
                    // Treat any persisted state older than 24 hours as unknown.
                    // Otherwise a stale cron result keeps the light orange long
                    // after the underlying issue was resolved.
                    $_hcAt    = isset($_hcState['at']) ? strtotime((string)$_hcState['at']) : 0;
                    $_hcStale = $_hcAt <= 0 || (time() - $_hcAt) > 86400;
                    $_hcAgg   = $_hcStale ? 'unknown' : ($_hcState['aggregate'] ?? 'unknown');
                    $_hcTip   = match ($_hcAgg) {
                        'ok'      => t('topbar_health_ok'),
                        'warn'    => t('topbar_health_warn'),
                        'error'   => t('topbar_health_error'),
                        default   => t('topbar_health_unknown'),
                    };
                    $_hcColor = match ($_hcAgg) {
                        'ok'    => '#16a34a',
                        'warn'  => '#d97706',
                        'error' => '#dc2626',
                        default => '#9ca3af',
                    };
                ?>
                <a href="/admin/settings#healthCheckCard" class="icon-btn topbar-health" title="<?= $_hcTip ?>" aria-label="<?= $_hcTip ?>">
                    <span style="display:inline-block;width:12px;height:12px;border-radius:50%;background:<?= $_hcColor ?>;box-shadow:0 0 0 2px rgba(0,0,0,.05)"></span>
                </a>
                <?php endif; ?>

                <!-- Notifications -->
                <div class="notif-wrapper">
                    <button class="icon-btn notif-btn" id="notifBtn" aria-label="<?= t('notifications') ?>">
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9M13.73 21a2 2 0 01-3.46 0"/></svg>
                        <?php if ($unreadNotifs > 0): ?>
                        <span class="notif-badge"><?= min($unreadNotifs, 99) ?></span>
                        <?php endif; ?>
                    </button>
                    <div class="notif-dropdown" id="notifDropdown">
                        <div class="notif-header">
                            <span><?= t('notifications') ?></span>
                            <button class="notif-bulk-btn notif-bulk-del" data-click="notifClearAll"><?= t('delete_all') ?></button>
                        </div>
                        <div class="notif-list" id="notifList">
                            <div class="notif-empty"><?= t('loading') ?></div>
                        </div>
                    </div>
                </div>

                <!-- User menu -->
                <div class="user-menu-wrapper">
                    <button class="user-menu-btn" id="userMenuBtn">
                        <?php if ($currentUser): echo Helper::avatar($currentUser, 32); endif; ?>
                    </button>
                    <div class="user-dropdown" id="userDropdown">
                        <a href="/settings" class="dropdown-item">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                            <?= t('profile') ?>
                        </a>
                        <?php if (Auth::isAdmin()): ?>
                        <a href="/admin/settings" class="dropdown-item">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><circle cx="12" cy="12" r="3"/><path d="M19.07 4.93A10 10 0 000 11.22M4.93 19.07A10 10 0 0012 22"/></svg>
                            <?= t('settings') ?>
                        </a>
                        <?php endif; ?>
                        <a href="/help" class="dropdown-item">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 015.83 1c0 2-3 3-3 3"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
                            <?= t('help') ?>
                        </a>
                        <div class="dropdown-divider"></div>
                        <form method="POST" action="/logout" style="margin:0">
                            <input type="hidden" name="csrf_token" value="<?= $csrfToken ?>">
                            <button type="submit" class="dropdown-item dropdown-item-danger" style="width:100%;text-align:left;background:none;border:none;cursor:pointer;font:inherit">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4M16 17l5-5-5-5M21 12H9"/></svg>
                                <?= t('logout') ?>
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </header>

        <?php
        $_privacyUrl = Helper::getSetting('privacy_url', '');
        $_imprintUrl = Helper::getSetting('imprint_url', '');
        ?>
        <?php PluginManager::fire('layout_banner'); ?>
        <!-- Page content -->
        <main class="main-content">
            <?php if ($flash = Helper::flash('success')): ?>
            <div class="alert alert-success" role="alert">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><path d="M22 4L12 14.01l-3-3"/></svg>
                <?= Helper::e($flash) ?>
                <button class="alert-close" data-remove-parent>×</button>
            </div>
            <?php endif; ?>
            <?php if ($flash = Helper::flash('error')): ?>
            <div class="alert alert-error" role="alert">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><circle cx="12" cy="12" r="10"/><path d="M15 9l-6 6M9 9l6 6"/></svg>
                <?= Helper::e($flash) ?>
                <button class="alert-close" data-remove-parent>×</button>
            </div>
            <?php endif; ?>

            <?= $content ?>
        </main>
        <footer class="app-footer">
            <div class="app-footer-left">
                <span class="footer-version">Plenvo v<?= APP_VERSION ?><?= PluginManager::isEnabled('provider-edition') ? ' Provider Edition' : '' ?></span>
                <?php if (PluginManager::filter('show_powered_by', true)): ?>
                <span class="footer-sep">·</span>
                <a href="https://plenvo.gp-server.com" target="_blank" rel="noopener"><?= t('powered_by_plenvo') ?></a>
                <?php endif; ?>
            </div>
            <?php if ($_privacyUrl || $_imprintUrl): ?>
            <div class="app-footer-right">
                <?php if ($_privacyUrl): ?>
                <a href="<?= Helper::e($_privacyUrl) ?>" target="_blank" rel="noopener"><?= t('privacy_policy') ?></a>
                <?php endif; ?>
                <?php if ($_privacyUrl && $_imprintUrl): ?>
                <span class="footer-sep">·</span>
                <?php endif; ?>
                <?php if ($_imprintUrl): ?>
                <a href="<?= Helper::e($_imprintUrl) ?>" target="_blank" rel="noopener"><?= t('imprint') ?></a>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </footer>
    </div>
</div>

<!-- Overlay for mobile sidebar -->
<div class="sidebar-overlay" id="sidebarOverlay"></div>

<!-- Command palette (Ctrl/Cmd+K) — global quick-jump to projects and tasks -->
<div id="cmdkOverlay" class="cmdk-overlay" style="display:none">
    <div class="cmdk-modal" role="dialog" aria-label="<?= t('cmdk_placeholder') ?>">
        <div class="cmdk-input-row">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18" style="opacity:.5;flex-shrink:0"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>
            <input type="text" id="cmdkInput" autocomplete="off" placeholder="<?= t('cmdk_placeholder') ?>">
            <span class="cmdk-hint">esc</span>
        </div>
        <div id="cmdkResults" class="cmdk-results"></div>
        <div class="cmdk-footer"><span class="cmdk-kbd">↑↓</span> <?= t('cmdk_navigate') ?> &nbsp; <span class="cmdk-kbd">↵</span> <?= t('cmdk_open') ?></div>
    </div>
</div>
<style>
.cmdk-overlay { position:fixed; inset:0; background:rgba(0,0,0,.5); z-index:9000; display:flex; align-items:flex-start; justify-content:center; padding-top:12vh }
.cmdk-modal { width:min(640px, 92vw); background:var(--bg-card,#fff); border:1px solid var(--border-color,#e5e7eb); border-radius:12px; box-shadow:0 20px 60px rgba(0,0,0,.25); display:flex; flex-direction:column; overflow:hidden }
.cmdk-input-row { display:flex; align-items:center; gap:10px; padding:12px 14px; border-bottom:1px solid var(--border-color,#e5e7eb) }
#cmdkInput { flex:1; border:0; outline:0; font-size:1rem; background:transparent; color:var(--text-primary,#111) }
.cmdk-hint { font-size:.7rem; color:var(--text-secondary,#666); padding:2px 6px; border:1px solid var(--border-color,#e5e7eb); border-radius:4px }
.cmdk-results { max-height:50vh; overflow-y:auto }
.cmdk-section { padding:6px 12px; font-size:.7rem; font-weight:600; color:var(--text-secondary,#666); text-transform:uppercase; letter-spacing:.06em }
.cmdk-item { display:flex; align-items:center; gap:10px; padding:8px 14px; cursor:pointer; font-size:.9rem; color:var(--text-primary,#111); text-decoration:none; border-left:2px solid transparent }
.cmdk-item:hover, .cmdk-item.active { background:var(--bg-secondary,#f1f5f9); border-left-color:var(--color-primary,#3b82f6) }
.cmdk-item-title { flex:1; white-space:nowrap; overflow:hidden; text-overflow:ellipsis }
.cmdk-item-sub { font-size:.78rem; color:var(--text-secondary,#666) }
.cmdk-dot { display:inline-block; width:8px; height:8px; border-radius:50%; flex-shrink:0 }
.cmdk-empty { padding:24px; text-align:center; color:var(--text-secondary,#666); font-size:.85rem }
.cmdk-avatar { display:inline-flex; align-items:center; justify-content:center; width:22px; height:22px; border-radius:50%; background:var(--color-primary,#3b82f6); color:#fff; font-size:.7rem; font-weight:700; flex-shrink:0 }
.cmdk-avatar--ext { background:#f59e0b }
.cmdk-counter { margin-left:auto; padding:2px 8px; border-radius:10px; background:var(--bg-secondary,#f1f5f9); color:var(--text-secondary,#666); font-size:.72rem; font-weight:600; flex-shrink:0 }
.topbar-search { display:flex; align-items:center; gap:10px; min-width:240px; max-width:420px; flex:0 1 320px; height:34px; padding:0 10px 0 12px; margin:0 12px; background:var(--bg-secondary,#f1f5f9); border:1px solid var(--border-color,#e5e7eb); border-radius:8px; color:var(--text-secondary,#666); font-size:.85rem; cursor:text; text-align:left; transition:border-color 150ms, background 150ms }
.topbar-search:hover { border-color:var(--color-primary,#3b82f6); background:var(--bg-card,#fff) }
.topbar-search-icon { flex-shrink:0; opacity:.65 }
.topbar-search-text { flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:var(--text-secondary,#666) }
.topbar-search-kbd { display:inline-flex; align-items:center; gap:2px; padding:2px 6px; border:1px solid var(--border-color,#e5e7eb); border-radius:4px; background:var(--bg-card,#fff); color:var(--text-secondary,#666); font-family:ui-monospace,SFMono-Regular,Menlo,monospace; font-size:.7rem; font-weight:600; flex-shrink:0 }
.topbar-search-kbd .kbd-plus { opacity:.5; padding:0 1px }
@media (max-width:720px) { .topbar-search { min-width:0; max-width:none; flex:1; margin:0 8px } .topbar-search-text, .topbar-search-kbd { display:none } }
.cmdk-footer { padding:8px 14px; border-top:1px solid var(--border-color,#e5e7eb); font-size:.72rem; color:var(--text-secondary,#666); display:flex; gap:14px; flex-wrap:wrap }
.cmdk-kbd { padding:1px 6px; border:1px solid var(--border-color,#e5e7eb); border-radius:3px; font-family:ui-monospace,monospace; font-size:.7rem }
</style>

<script nonce="<?= csp_nonce() ?>">
const CSRF_TOKEN   = <?= json_encode($csrfToken) ?>;
const CURRENT_THEME = <?= json_encode($theme) ?>;
const APP_LANG     = <?= json_encode(Lang::current()) ?>;
const BASE_URL     = <?= json_encode($base) ?>;
const CMDK_LABELS  = {
    projects:  <?= json_encode(t('projects')) ?>,
    tasks:     <?= json_encode(t('tasks')) ?>,
    people:    <?= json_encode(t('cmdk_people')) ?>,
    external:  <?= json_encode(t('external')) ?>,
    openTasks: <?= json_encode(t('cmdk_open_tasks')) ?>,
    empty:     <?= json_encode(t('cmdk_empty')) ?>,
    typeMore:  <?= json_encode(t('cmdk_type_more')) ?>,
};
</script>
<script nonce="<?= csp_nonce() ?>" src="/assets/js/app.js?v=<?= APP_VERSION ?>"></script>
<script nonce="<?= csp_nonce() ?>" src="/assets/js/cmdk.js?v=<?= APP_VERSION ?>"></script>
</body>
</html>
