<!doctype html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= e($settings['site_name'] ?? config_value('app_name', 'CFC Autoescola Pro')) ?></title>
    <?php $pwaIconVersion=substr(sha1(($settings['logo_path']??'').'|'.($settings['site_name']??'')),0,12); ?>
    <meta name="theme-color" content="#e7b708">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link rel="manifest" href="<?= e(asset('manifest.php')) ?>?v=<?= e($pwaIconVersion) ?>">
    <link rel="icon" href="<?= e(asset('pwa-icon.php')) ?>?size=192&v=<?= e($pwaIconVersion) ?>">
    <link rel="apple-touch-icon" href="<?= e(asset('pwa-icon.php')) ?>?size=192&v=<?= e($pwaIconVersion) ?>">
    <script>(function(){try{var t=localStorage.getItem('cfc-theme')||'light';document.documentElement.setAttribute('data-cfc-theme',t);document.documentElement.setAttribute('data-bs-theme',t);}catch(e){}})();</script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link href="<?= e(asset('assets/css/style.css')) ?>?v=20260718-20" rel="stylesheet">
</head>
<body>
<?php
$currentProfessional=class_exists('Professional')?Professional::current():null;
$currentBeginner=class_exists('BeginnerInstructor')?BeginnerInstructor::current():null;
$whatsText=trim($settings['whatsapp_support']??'');$whatsDigits=preg_replace('/\D+/','',$whatsText);if($whatsDigits!==''&&!str_starts_with($whatsDigits,'55'))$whatsDigits='55'.$whatsDigits;$whatsUrl=$whatsDigits?'https://wa.me/'.$whatsDigits:'';
$showSupport=$whatsUrl!==''&&(($currentUser&&($currentUser['role']??'')!=='admin')||$currentProfessional||$currentBeginner);
?>
<header class="clean-site-header">
    <nav class="navbar navbar-expand-lg main-navbar border-bottom bg-white">
        <div class="container">
            <a class="navbar-brand fw-bold text-cfc-dark d-flex align-items-center gap-2" href="<?= e(url('home')) ?>">
                <?php if(!empty($settings['logo_path'])): ?><img src="<?= e(asset($settings['logo_path'])) ?>" alt="Logo"><?php else: ?><span class="brand-fallback">CFC</span><?php endif; ?>
                <span><?= e($settings['site_name']??'AUTOESCOLA') ?></span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navMain" aria-controls="navMain" aria-expanded="false" aria-label="Abrir menu"><span class="navbar-toggler-icon"></span></button>
            <div class="collapse navbar-collapse" id="navMain"><ul class="navbar-nav ms-auto align-items-lg-center gap-lg-2">
                <?php if($currentUser): ?>
                    <?php if(($currentUser['role']??'')==='admin'): ?>
                        <li class="nav-item"><a class="nav-link" href="<?= e(url('admin/dashboard')) ?>">Painel</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('admin/messages')) ?>">Mensagens</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/contacts')) ?>">Contatos</a></li>
                    <?php else: ?>
                        <li class="nav-item"><a class="nav-link" href="<?= e(url('dashboard')) ?>">Início</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('lessons')) ?>">Aulas</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/messages')) ?>">Mensagens</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/contacts')) ?>">Contatos</a></li>
                    <?php endif; ?>
                    <li class="nav-item"><a class="btn btn-outline-danger btn-sm rounded-pill px-3" href="<?= e(url('logout')) ?>">Sair</a></li>
                <?php elseif($currentProfessional): ?>
                    <li class="nav-item"><a class="nav-link" href="<?= e(url('professional/panel')) ?>">Painel</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('professional/appointments')) ?>">Aulas</a></li><?php if(!empty($currentProfessional['can_manage_theory_groups'])): ?><li class="nav-item"><a class="nav-link" href="<?= e(url('professional/theory-groups')) ?>">Turmas</a></li><?php endif; ?><?php if(!empty($currentProfessional['can_manage_theory_videos'])): ?><li class="nav-item"><a class="nav-link" href="<?= e(url('professional/theory-videos')) ?>">Vídeos</a></li><?php endif; ?><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/messages')) ?>">Mensagens</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/contacts')) ?>">Contatos</a></li><li class="nav-item"><a class="btn btn-outline-danger btn-sm rounded-pill px-3" href="<?= e(url('professional/logout')) ?>">Sair</a></li>
                <?php elseif($currentBeginner): ?>
                    <li class="nav-item"><a class="nav-link" href="<?= e(url('beginner/panel')) ?>">Formação</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/messages')) ?>">Mensagens</a></li><li class="nav-item"><a class="nav-link" href="<?= e(url('portal/contacts')) ?>">Contatos</a></li><li class="nav-item"><a class="btn btn-outline-danger btn-sm rounded-pill px-3" href="<?= e(url('beginner/logout')) ?>">Sair</a></li>
                <?php else: ?>
                    <li class="nav-item"><a class="nav-link" href="<?= e(url('login')) ?>">Entrar</a></li>
                    <li class="nav-item"><a class="nav-link" href="<?= e(url('professional/login')) ?>">Instrutor</a></li>
                    <li class="nav-item"><a class="nav-link" href="<?= e(url('beginner/login')) ?>">Formação</a></li>
                <?php endif; ?>
            </ul></div>
        </div>
    </nav>
</header>

<?php foreach(flashes() as $flash): ?><div class="container mt-3"><div class="alert alert-<?= e($flash['type']) ?> rounded-4 shadow-soft mb-0"><?= e($flash['message']) ?></div></div><?php endforeach; ?>
<?= $content ?>
<footer class="py-4 bg-white border-top mt-5"><div class="container text-muted small">&copy; <?= date('Y') ?> <?= e($settings['site_name']??'AUTOESCOLA') ?>. Todos os direitos reservados.</div></footer>
<button type="button" id="themeToggle" class="floating-theme-toggle" aria-label="Ativar modo escuro" title="Alternar modo claro e escuro"><i class="bi bi-moon-stars-fill"></i></button>
<?php if($showSupport): ?><a class="floating-whatsapp" target="_blank" href="<?= e($whatsUrl) ?>" aria-label="Falar no WhatsApp"><i class="bi bi-whatsapp"></i><span class="floating-whatsapp-label">Suporte</span></a><?php endif; ?>
<button type="button" id="pwaInstallButton" class="pwa-install-button <?= $showSupport ? 'has-whatsapp' : '' ?> d-none"><i class="bi bi-phone"></i><span>Instalar APP</span></button>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="<?= e(asset('assets/js/pwa-install.js')) ?>?v=20260718-20"></script>
<script src="<?= e(asset('assets/js/image-tools.js')) ?>?v=20260718-20"></script>
<script src="<?= e(asset('assets/js/internal-messages.js')) ?>?v=20260718-20"></script>
<script src="<?= e(asset('assets/js/audio-messages.js')) ?>?v=20260719-21"></script>
<script>
(() => {
    const root = document.documentElement;
    const button = document.getElementById('themeToggle');
    if (!button) return;
    const apply = theme => {
        const value = theme === 'dark' ? 'dark' : 'light';
        root.setAttribute('data-cfc-theme', value);
        root.setAttribute('data-bs-theme', value);
        const themeMeta = document.querySelector('meta[name="theme-color"]');
        if (themeMeta) themeMeta.setAttribute('content', value === 'dark' ? '#09111f' : '#e7b708');
        button.innerHTML = value === 'dark' ? '<i class="bi bi-sun-fill"></i>' : '<i class="bi bi-moon-stars-fill"></i>';
        button.setAttribute('aria-label', value === 'dark' ? 'Ativar modo claro' : 'Ativar modo escuro');
        try { localStorage.setItem('cfc-theme', value); } catch (_) {}
    };
    apply(root.getAttribute('data-cfc-theme') || 'light');
    button.addEventListener('click', () => apply(root.getAttribute('data-cfc-theme') === 'dark' ? 'light' : 'dark'));
})();
</script>
</body></html>
