<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="base-url" content="<?= BASE_URL ?>">
  <title><?= isset($pageTitle) ? htmlspecialchars($pageTitle) . ' — ' : '' ?>Savit</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap5.min.css">
  <link rel="stylesheet" href="<?= dirname(BASE_URL) ?>/Common/assets/css/savit.css">
  <!-- Cross-engine project persistence -->
  <script src="<?= dirname(BASE_URL) ?>/Common/assets/js/project-persistence.js" defer></script>
</head>
<body>
<div class="d-flex" id="app-wrapper">

  <?php require __DIR__ . '/sidebar.php'; ?>

  <div class="flex-grow-1 d-flex flex-column min-vh-100" id="main-content">

    <!-- Topbar -->
    <div class="savit-topbar">
      <div class="topbar-left">
        <button class="topbar-toggle" id="sidebar-toggle" title="Toggle sidebar">
          <i class="bi bi-list" style="font-size:18px;"></i>
        </button>
        <span class="topbar-title"><?= htmlspecialchars($pageTitle ?? 'Dashboard') ?></span>
      </div>
      <div class="topbar-right">
        <span class="topbar-engine-badge"><i class="bi bi-lightning-fill me-1"></i>Content Engine</span>
        <a href="<?= BASE_URL ?>/content/generate" class="btn btn-primary btn-sm">
          <i class="bi bi-lightning-fill me-1"></i>Generate
        </a>
      </div>
    </div>

    <!-- Flash messages -->
    <?php
    $flash = \CAE\Lib\View::getFlash();
    if ($flash):
        $icons  = ['success' => 'check-circle-fill', 'error' => 'x-circle-fill', 'warning' => 'exclamation-triangle-fill', 'info' => 'info-circle-fill'];
        $bsType = $flash['type'] === 'error' ? 'danger' : $flash['type'];
    ?>
    <div class="alert alert-<?= $bsType ?> alert-dismissible fade show mx-4 mt-3 d-flex align-items-center" role="alert">
      <i class="bi bi-<?= $icons[$flash['type']] ?? 'info-circle' ?> me-2"></i>
      <span><?= $flash['message'] ?></span>
      <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <!-- Page content -->
    <div class="p-4 flex-grow-1">
