<?php
$appConfig = require ROOT_PATH . '/config/app.php';

// Path computation
$basePath  = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '/')), '/');
$assetBase = $basePath;
if (str_ends_with($assetBase, '/public')) {
    $assetBase = substr($assetBase, 0, -7) ?: '/';
}
$savitBase = dirname(dirname($basePath)); // /Savit

// Shared sidebar variables
$activeEngine      = 'audit';
$engineBadgeLabel  = 'Audit Engine';
$engineBadgeColor  = 'rgba(8,145,178,.1);color:#0891b2;';
$currentPath       = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?? '/';
$engineSubnav = [
    ['label' => 'Health Board',   'icon' => 'grid-3x3-gap',   'url' => $basePath . '/projects', 'active' => str_ends_with($currentPath, '/projects')],
    ['label' => 'Website Audit',  'icon' => 'globe2',          'url' => $basePath . '/',         'active' => ($currentPath === $basePath || $currentPath === $basePath . '/')],
    ['label' => 'SERP Tracker',   'icon' => 'graph-up-arrow',  'url' => $basePath . '/serp',     'active' => str_starts_with($currentPath, $basePath . '/serp')],
];
?>
<!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="<?= htmlspecialchars($basePath) ?>">
  <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="<?= htmlspecialchars($savitBase) ?>/Common/assets/css/savit.css?v=4">
  <script src="<?= htmlspecialchars($savitBase) ?>/Common/assets/js/project-persistence.js" defer></script>
</head>
<body>
<div class="d-flex" id="app-wrapper">

  <?php require SAVIT_ROOT . '/Common/views/layout/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 ?? 'Website Audit') ?></span>
      </div>
      <div class="topbar-right">
        <span class="topbar-engine-badge" style="background:<?= $engineBadgeColor ?>">
          <i class="bi bi-search me-1"></i><?= htmlspecialchars($engineBadgeLabel) ?>
        </span>
      </div>
    </div>

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