<?php
ob_start();
if (!defined('INSTALLED')) require_once dirname(__DIR__, 2) . '/config/config.php';
require_once dirname(__DIR__, 2) . '/includes/functions.php';
startSecureSession();
requireAdmin();

// Logout
if (isset($_GET['logout'])) {
    $_SESSION = [];
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    session_destroy();
    $target = rtrim(SITE_URL, '/') . '/admin/login.php';
    header("Location: $target");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= isset($adminTitle) ? e($adminTitle) . ' — ' : '' ?>Rig N Dig Admin</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&family=Rajdhani:wght@600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="<?= (defined('SITE_URL') && SITE_URL) ? rtrim(SITE_URL, '/') : '' ?>/assets/css/style.css">
<script>
tailwind.config = {
  theme: {
    extend: {
      colors: {
        navy: { 900: '#0a0e1a', 800: '#111827', 700: '#1a1f2e', 600: '#1e2534', 500: '#2a3147' },
        orange: { 600: '#ff6b00', 500: '#ff7a15' }
      },
      fontFamily: { sans: ['Inter','sans-serif'], display: ['Rajdhani','sans-serif'] }
    }
  }
}
</script>
<script>
window.SITE_URL = '<?= SITE_URL ?>';
window.CSRF_TOKEN = '<?= csrfToken() ?>';
</script>
</head>
<body class="bg-navy-900 text-gray-100 font-sans">

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

<!-- Top Bar -->
<div class="admin-content">
  <div class="sticky top-0 z-30 bg-navy-800 border-b border-navy-500 px-6 py-3 flex items-center gap-4">
    <button id="sidebar-toggle" class="md:hidden p-2 rounded hover:bg-navy-600">☰</button>
    <h1 class="text-sm font-semibold text-gray-300"><?= isset($adminTitle) ? e($adminTitle) : 'Dashboard' ?></h1>
    <div class="ml-auto flex items-center gap-3 text-xs text-gray-400">
      <span><?= date('d M Y, H:i') ?></span>
      <span class="badge badge-orange">v<?= APP_VERSION ?></span>
    </div>
  </div>

  <!-- Flash -->
  <?php if (!empty($_SESSION['flash'])): foreach ($_SESSION['flash'] as $type => $msg): unset($_SESSION['flash'][$type]); ?>
  <div class="mx-6 mt-4">
    <div class="<?= $type === 'success' ? 'bg-green-900 border-green-600 text-green-200' : ($type === 'warning' ? 'bg-yellow-900 border-yellow-600 text-yellow-200' : 'bg-red-900 border-red-600 text-red-200') ?> border rounded px-4 py-3 text-sm">
      <?= e($msg) ?>
    </div>
  </div>
  <?php endforeach; endif; ?>

  <div class="p-6">
