<?php
require_once __DIR__ . '/functions.php';
$db = db();
$slug = $_GET['slug'] ?? '';

$page = $db->prepare("SELECT * FROM pages WHERE slug = ? AND is_published = 1");
$page->execute([$slug]);
$page = $page->fetch();

if (!$page) {
    http_response_code(404);
    $page_title = 'Page Not Found';
    require_once __DIR__ . '/includes/header.php';
    echo '<div class="text-center py-20"><h1 class="text-2xl font-bold text-gray-800">Page Not Found</h1></div>';
    require_once __DIR__ . '/includes/footer.php';
    exit;
}

$page_title = $page['title'];
$meta_description = $page['meta_description'];

require_once __DIR__ . '/includes/header.php';
?>
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
    <h1 class="text-3xl font-bold text-gray-800 mb-6"><?= h($page['title']) ?></h1>
    <div class="text-gray-600 leading-relaxed whitespace-pre-line"><?= h($page['content']) ?></div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
