<?php
/**
 * Shared site chrome (presentation only): theme FOUC guard + top bar + theme toggle.
 * All links are relative to the /public/ directory.
 */

$siteHeaderLogoUrl = '';
$siteHeaderLogoDarkUrl = '';
$siteHeaderLogoCandidates = [
    'assets/images/edinburgh-medical-school.png',
    'assets/img/edinburgh-logo.svg',
    'assets/img/edinburgh-logo.png',
];
$siteHeaderLogoDarkCandidates = [
    'assets/images/uoe-logo-white.png',
];
$publicRoot = dirname(__DIR__);
foreach ($siteHeaderLogoCandidates as $candidate) {
    $absolute = $publicRoot . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $candidate);
    if (is_readable($absolute)) {
        $siteHeaderLogoUrl = $candidate;
        break;
    }
}
foreach ($siteHeaderLogoDarkCandidates as $candidate) {
    $absolute = $publicRoot . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $candidate);
    if (is_readable($absolute)) {
        $siteHeaderLogoDarkUrl = $candidate;
        break;
    }
}
?>
<script>
(function () {
    try {
        var k = 'eqg-theme';
        var stored = localStorage.getItem(k);
        var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
        var theme = 'light';
        if (stored === 'dark' || stored === 'light') {
            theme = stored;
        } else if (prefersDark) {
            theme = 'dark';
        }
        document.documentElement.setAttribute('data-theme', theme);
    } catch (e) {
        document.documentElement.setAttribute('data-theme', 'light');
    }
})();
</script>
<header class="site-header" role="banner">
    <div class="site-header-inner">
        <a class="site-brand" href="index.php">
            <?php if ($siteHeaderLogoUrl !== '') : ?>
                <img
                    class="app-logo app-logo--theme-light"
                    src="<?php echo htmlspecialchars($siteHeaderLogoUrl, ENT_QUOTES, 'UTF-8'); ?>"
                    alt="Edinburgh Medical School"
                    width="180"
                    height="44"
                    decoding="async"
                >
                <?php if ($siteHeaderLogoDarkUrl !== '') : ?>
                <img
                    class="app-logo app-logo--theme-dark"
                    src="<?php echo htmlspecialchars($siteHeaderLogoDarkUrl, ENT_QUOTES, 'UTF-8'); ?>"
                    alt=""
                    width="180"
                    height="44"
                    decoding="async"
                    aria-hidden="true"
                >
                <?php endif; ?>
            <?php endif; ?>
            <span class="site-brand-text">MLA Question Builder</span>
        </a>
        <div class="site-header-actions">
            <button type="button" class="theme-toggle" id="theme-toggle" aria-label="Toggle colour theme" title="Light / dark theme">
                <svg class="theme-toggle-icon theme-icon-sun" width="20" height="20" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
                    <circle cx="12" cy="12" r="4" fill="currentColor"/>
                    <path fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
                </svg>
                <svg class="theme-toggle-icon theme-icon-moon" width="20" height="20" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
                    <path fill="currentColor" d="M21 14.5A8.5 8.5 0 0 1 9.5 3a8.5 8.5 0 1 0 11.5 11.5z"/>
                </svg>
            </button>
        </div>
    </div>
</header>
