<?php
$pageTitle   = $pageTitle   ?? 'FlixCooks | Food Blog & Recipes';
$description = $description ?? 'Seasonal recipes, tested tips, and approachable cooking for busy weeknights.';
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="<?php echo e($description); ?>">
  <title><?php echo e($pageTitle); ?></title>
  <link rel="icon" type="image/png" href="/assets/favicon.png">
  <link rel="alternate icon" type="image/x-icon" sizes="16x16 32x32" href="/favicon.ico">

  <!-- Google Fonts: Playfair Display + Manrope -->
  <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=Playfair+Display:ital,wght@0,400;0,500;0,600;0,700;1,400;1,500;1,600;1,700&family=Manrope:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">

  <!-- Main stylesheet -->
  <link rel="stylesheet" href="/assets/style.css">

  <!-- Firebase v10 compat SDKs -->
  <script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-app-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-auth-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/10.8.0/firebase-firestore-compat.js"></script>
  <script>
    (function() {
      var config = <?php echo json_encode(get_firebase_config()); ?>;
      firebase.initializeApp(config);
      window.auth = firebase.auth();
      window.db = firebase.firestore();
      
      // Auto-sync PHP Session with Firebase Auth State
      window.auth.onAuthStateChanged(function(user) {
        var phpUser = <?php echo isset($_SESSION['fc_user']) ? json_encode($_SESSION['fc_user']) : 'null'; ?>;
        if (user) {
          if (!phpUser || phpUser.uid !== user.uid) {
            user.getIdToken().then(function(token) {
              var xhr = new XMLHttpRequest();
              xhr.open('POST', '/api/session.php', true);
              xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
              xhr.send('action=login&uid=' + encodeURIComponent(user.uid) + '&email=' + encodeURIComponent(user.email) + '&token=' + encodeURIComponent(token));
              xhr.onload = function() {
                if (xhr.status === 200) {
                  if (!phpUser) { window.location.reload(); }
                }
              };
            });
          }
        } else {
          if (phpUser) {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/api/session.php', true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send('action=logout');
            xhr.onload = function() {
              window.location.reload();
            };
          }
        }
      });
    })();
  </script>

  <!-- GSAP + ScrollTrigger (CDN) -->
  <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/ScrollTrigger.min.js"></script>

  <!-- Lenis smooth scroll (CDN) -->
  <script src="https://cdn.jsdelivr.net/npm/@studio-freight/lenis@1.0.42/bundled/lenis.min.js"></script>

  <!-- Set theme instantly before paint to avoid FOUC (flash of unstyled content).
       FlixCooks is a dark-only design by choice — no light mode is offered.
       The theme attribute and localStorage flag are set unconditionally here. -->
  <script>
    (function(){
      document.documentElement.setAttribute('data-theme', 'dark');
      localStorage.setItem('fc-theme', 'dark');
    })();
  </script>
</head>
<body>

<!-- ── AuraMarbleBackground (fixed atmospheric grain layer) ── -->
<div class="bg-marble-container" aria-hidden="true">
  <div class="bg-marble-grain"></div>
</div>

