<?php
declare(strict_types=1);

require_once __DIR__ . '/functions.php';
require_once __DIR__ . '/icons.php';
require_once __DIR__ . '/i18n.php';
require_once __DIR__ . '/referrals.php';
require_once __DIR__ . '/notifications.php';

// Ensure $pdo is available for early hooks like referral tracking
if (!isset($pdo)) {
    $pdo = require_once __DIR__ . '/../config/database.php';
}

start_session();
$user = $_SESSION['user'] ?? null;
$flashes = get_flashes();
// capture ref param early
if (isset($_GET['ref'])) {
  capture_referral_visit($pdo, $_GET['ref']);
}

// Fetch site logo once
$stmt = $pdo->prepare("SELECT value FROM settings WHERE `key` = 'site_logo'");
$stmt->execute();
$siteLogo = $stmt->fetchColumn();

// Admin Menu Structure for consistent management
$adminMenu = [];
if ($user && ($user['role'] ?? '') === 'admin') {
    $adminMenu = [
        'Operations' => [
            ['url' => 'admin/dashboard.php', 'label' => 'Dashboard', 'icon' => 'dashboard'],
            ['url' => 'admin/applications.php', 'label' => 'Applications', 'icon' => 'applications'],
            ['url' => 'admin/users.php', 'label' => 'Users', 'icon' => 'users'],
            ['url' => 'admin/messages.php', 'label' => 'Support Chat', 'icon' => 'faq'],
        ],
        'Growth' => [
            ['url' => 'admin/seo_hub.php', 'label' => 'SEO Hub', 'icon' => 'seo'],
            ['url' => 'admin/blog.php', 'label' => 'Blog Editor', 'icon' => 'blog'],
            ['url' => 'admin/commissions.php', 'label' => 'Referrals', 'icon' => 'pricing'],
        ],
        'System' => [
            ['url' => 'admin/settings.php', 'label' => 'Settings', 'icon' => 'settings'],
            ['url' => 'admin/notifications.php', 'label' => 'Mail Queue', 'icon' => 'notifications'],
            ['url' => 'admin/packages.php', 'label' => 'Packages', 'icon' => 'services'],
        ]
    ];
}
?>


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php $meta = $meta ?? []; ?>
<title><?= isset($meta['title']) ? e($meta['title']) : 'Go Legal, Business Registration in Nigeria' ?></title>
<?php if (!empty($meta['description'])): ?><meta name="description" content="<?=e($meta['description'])?>"><?php endif; ?>
<?php if (!empty($meta['canonical'])): ?><link rel="canonical" href="<?=e($meta['canonical'])?>"><?php endif; ?>
<?php include __DIR__ . '/seo_header.php'; ?>
<meta name="theme-color" content="#10b981">
<link rel="manifest" href="<?=url('manifest.webmanifest')?>">
<?php include __DIR__ . '/analytics_simple.php'; ?>
<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=Outfit:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
/* ─── Design Tokens ─── */
:root {
  --bg: #fdfdfd;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --subtle: #94a3b8;
  --border: #f1f5f9;
  --hairline: #f8fafc;
  --primary: #059669;
  --primary-hover: #047857;
  --primary-tint: rgba(5,150,105,0.06);
  --red: #ef4444;
  --indigo: #6366f1;
  --blue: #3b82f6;
  --purple: #8b5cf6;
  --orange: #f59e0b;
  --teal: #14b8a6;
  --shadow-xs: 0 1px 2px rgba(0,0,0,.03);
  --shadow-sm: 0 4px 12px rgba(0,0,0,.04);
  --shadow: 0 12px 30px -4px rgba(0,0,0,.04), 0 4px 12px -2px rgba(0,0,0,.02);
  --shadow-lg: 0 20px 40px -8px rgba(0,0,0,0.06), 0 0 1px rgba(0,0,0,0.1);
  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 24px;
  --radius-xl: 32px;
  --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ─── Reset & Base ─── */
*, *::before, *::after { box-sizing: border-box; -webkit-font-smoothing: antialiased; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: 'Outfit', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.6;
  letter-spacing: -0.01em;
  overflow-x: hidden;
  width: 100%;
}

/* ─── Header ─── */
header {
  background: white;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: all var(--transition);
}
header.scrolled {
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.05);
}
header.scrolled .nav {
  height: 64px;
}

.container { max-width: 1200px; margin: 0 auto; padding: 0 24px; }

.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80px;
}

.brand-zone {
  flex: 0 0 240px;
  display: flex;
  align-items: center;
}

/* ─── Brand ─── */
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  color: var(--text);
  font-weight: 800;
  font-size: 22px;
  letter-spacing: -0.04em;
  transition: transform 0.2s ease;
}
.brand img { height: 32px; width: auto; object-fit: contain; }

/* ─── Nav Links ─── */
.nav-links {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  justify-content: center;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  padding: 10px 16px;
  text-decoration: none;
  color: var(--muted);
  font-weight: 500;
  font-size: 15px;
  border-radius: 12px;
  transition: all var(--transition);
  white-space: nowrap;
}
.nav-link:hover { color: var(--primary); background: var(--primary-tint); }
.nav-link.active { color: var(--primary); font-weight: 800; background: var(--primary-tint); }

.nav-actions {
  display: flex;
  align-items: center;
  gap: 16px;
  flex: 0 0 240px;
  justify-content: flex-end;
}

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px 28px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: all var(--transition);
  line-height: 1.4;
}
.btn:hover { background: var(--hairline); border-color: var(--subtle); transform: translateY(-1px); }
.btn:active { transform: scale(0.97); }

.btn.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  box-shadow: 0 10px 20px -5px rgba(5,150,105,0.25);
}
.btn.primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
  box-shadow: 0 15px 30px -8px rgba(5,150,105,0.35);
}

.btn.secondary {
  background: var(--hairline);
  border-color: transparent;
}
.btn.secondary:hover { background: #f1f5f9; }

.ghost { background: transparent; border: 1px solid transparent; }
.ghost:hover { background: var(--hairline); border-color: var(--border); }

/* ─── Mobile Nav ─── */
.nav-toggle-btn {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--hairline);
  border: none;
  cursor: pointer;
  z-index: 1100;
  gap: 5px;
}
.nav-toggle-btn span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: all 0.3s ease;
}

  .drawer-overlay, .mobile-drawer { display: none; }

@media (max-width: 991px) {
  .nav { height: 64px; }
  .nav-links, .nav-actions .btn, .nav-actions .login-link { display: none !important; }
  .nav-actions { flex: 0 0 auto; width: auto; }
  .nav-toggle-btn { display: flex; background: transparent; }
  .brand-zone { flex: 1; }
  .brand { font-size: 18px; }
  
  .drawer-overlay, .mobile-drawer { display: flex; }
  .mobile-drawer {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: 280px;
    max-width: 85%;
    background: white;
    z-index: 2001;
    transform: translateX(100%);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    box-shadow: -10px 0 30px rgba(0,0,0,0.1);
  }
  .mobile-drawer.open { transform: translateX(0); }
  
  .drawer-header { padding: 20px; border-bottom: 1px solid var(--border); overflow: hidden; }
  .drawer-content { flex: 1; padding: 20px; display: flex; flex-direction: column; gap: 4px; overflow-y: auto; overflow-x: hidden; }
  .drawer-footer { padding: 20px; border-top: 1px solid var(--border); }

  .drawer-link {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    border-radius: 12px;
    text-decoration: none;
    color: var(--text);
    font-weight: 600;
    font-size: 15px;
    transition: all var(--transition);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .drawer-link:hover { background: var(--hairline); color: var(--primary); }
  .drawer-link.active { color: var(--primary); background: var(--primary-tint); }

  .drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(8px);
    z-index: 2000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.4s ease;
  }
  .drawer-overlay.open { opacity: 1; visibility: visible; }

  .nav-toggle-btn.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
  .nav-toggle-btn.open span:nth-child(2) { opacity: 0; }
  .nav-toggle-btn.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
}
header.scrolled .brand img { height: 28px; }

/* ─── User Menu ─── */
.user-menu { position: relative; }
.user-avatar {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--indigo));
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
  cursor: pointer;
  border: 2px solid white;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition);
}
.user-avatar:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.user-avatar:active { transform: scale(0.95); }
.user-avatar:focus { outline: 2px solid var(--primary); outline-offset: 2px; }
.user-dropdown {
  position: absolute;
  top: calc(100% + 10px); right: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  min-width: 230px;
  padding: 8px;
  z-index: 200;
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px) scale(0.97);
  transition: all 0.17s ease;
}
.user-dropdown.show { opacity: 1; visibility: visible; transform: translateY(0) scale(1); }
.user-dropdown::before {
  content: '';
  position: absolute;
  top: -6px; right: 12px;
  width: 12px; height: 12px;
  background: white;
  border: 1px solid var(--border);
  border-bottom: none; border-right: none;
  transform: rotate(45deg);
}
.user-dropdown-header {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--hairline);
  margin-bottom: 4px;
}
.user-dropdown-header strong { display: block; font-size: 13px; color: var(--text); margin-bottom: 2px; }
.user-dropdown-header small { font-size: 12px; color: var(--muted); word-break: break-all; }
.user-dropdown-divider { height: 1px; background: var(--border); margin: 6px 0; }
.user-dropdown-link {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--text); text-decoration: none;
  font-size: 13.5px;
  transition: background var(--transition);
}
.user-dropdown-link:hover { background: var(--hairline); color: var(--primary); }
.user-dropdown-link.logout { color: var(--red); }
.user-dropdown-link.logout:hover { background: #fef2f2; color: #dc2626; }
.user-dropdown-link svg { width: 15px; height: 15px; flex-shrink: 0; opacity: 0.65; }

/* ─── Notification Badge ─── */
.notification-badge {
  position: absolute; top: -4px; right: -4px;
  background: var(--red); color: white;
  border-radius: 50%;
  width: 16px; height: 16px;
  font-size: 9px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid white;
}

/* ─── Layout & Sidebar ─── */
.layout { display: grid; grid-template-columns: 200px 1fr; gap: 16px; }
.sidebar {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 12px;
  position: sticky;
  top: 80px;
  height: fit-content;
}
.side-link {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-weight: 500; font-size: 13.5px;
  text-decoration: none;
  transition: all var(--transition);
  margin-bottom: 2px;
}
.side-link:hover { background: var(--hairline); color: var(--text); }
.side-active { background: var(--primary-tint); color: var(--primary); font-weight: 600; }
.side-head {
  font-size: 10px;
  text-transform: uppercase;
  color: var(--subtle);
  font-weight: 700;
  margin: 20px 0 8px;
  padding-left: 12px;
  letter-spacing: 0.08em;
}
@media (max-width: 767px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; }
}

/* ─── Card ─── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition);
}
.card:hover { box-shadow: var(--shadow); border-color: var(--border); transform: translateY(-2px); }
.card:focus-within { box-shadow: 0 0 0 4px rgba(5,150,105,.05); }

/* ─── Forms ─── */
input, select, textarea {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: white;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  transition: border-color var(--transition), box-shadow var(--transition);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(22,163,74,.1);
}
label { display: block; margin: 10px 0 5px; font-size: 13.5px; font-weight: 500; color: var(--text); }

/* ─── Tables ─── */
table { width: 100%; border-collapse: separate; border-spacing: 0; }
th, td { padding: 12px 16px; border-bottom: 1px solid var(--hairline); text-align: left; font-size: 13.5px; }
th { background: var(--hairline); font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; font-size: 11px; }
tr:last-child td { border-bottom: none; }
.table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius); border: 1px solid var(--border); background: white; }
.table-responsive table { margin: 0; border: none; }
.table { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.striped tbody tr:nth-child(even), .table.striped tbody tr:nth-child(even) { background: var(--hairline); }

/* ─── Typography & Helpers ─── */
.lead { font-size: 16px; color: var(--muted); font-weight: 400; line-height: 1.7; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.gradient-text { background: linear-gradient(90deg, var(--indigo), var(--teal)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.flash { padding: 12px 18px; border-radius: 12px; margin: 16px 0; font-size: 14.5px; font-weight: 500; border: 1px solid transparent; }
.success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* ─── Grid ─── */
.grid { display: grid; gap: 16px; }
@media (min-width: 480px) { .grid-sm-2 { grid-template-columns: 1fr 1fr; } }
@media (min-width: 768px) { .grid-2 { grid-template-columns: 1fr 1fr; } .grid-3 { grid-template-columns: repeat(3,1fr); } }
@media (min-width: 1024px) { .grid-4 { grid-template-columns: repeat(4,1fr); } }

/* ─── Hero ─── */
.hero {
  padding: 72px 48px;
  border-radius: var(--radius-xl);
  background: white;
  border: 1px solid var(--border);
  position: relative;
  overflow: hidden;
}
.hero::before {
  content: '';
  position: absolute; inset: 0;
  background: radial-gradient(circle at 85% 15%, rgba(22,163,74,.04), transparent 50%),
              radial-gradient(circle at 10% 85%, rgba(79,70,229,.03), transparent 50%);
  pointer-events: none;
}
.section { padding: 64px 0; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
@media (min-width: 768px) { .hero { padding: 64px; } .hero .grid { gap: 24px; } }
@media (min-width: 1024px) { .hero h1 { font-size: 48px; } }
@media (max-width: 479px) {
  .hero { padding: 36px 20px; }
  .hero h1 { font-size: 28px; }
  .hero-actions { flex-direction: column; gap: 8px; }
  .input-bar { flex-direction: column; }
  .section { padding: 48px 0; }
  .pills { gap: 8px; }
}

/* ─── Badges & Chips ─── */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--hairline);
  border: 1px solid var(--border);
  padding: 4px 13px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}
.badge-reco { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px; background: #f0fdf4; border: 1px solid #bbf7d0; color: #166534; font-size: 12px; font-weight: 600; }
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border); font-size: 12px; font-weight: 500; }
.chip-draft { background: #f8fafc; color: #475569; }
.chip-submitted { background: #eef2ff; color: #3730a3; border-color: #c7d2fe; }
.chip-processing { background: #ecfeff; color: #155e75; border-color: #a5f3fc; }
.chip-approved { background: #f0fdf4; color: #166534; border-color: #bbf7d0; }
.chip-rejected { background: #fef2f2; color: #991b1b; border-color: #fecaca; }
.chip-pending { background: #fefce8; color: #854d0e; border-color: #fde68a; }
.chip-confirmed { background: #f0fdf4; color: #166534; border-color: #bbf7d0; }
.chip-failed { background: #fef2f2; color: #991b1b; border-color: #fecaca; }

/* ─── Icons & Service Icons ─── */
.icon { width: 18px; height: 18px; display: inline-block; vertical-align: middle; }
.service-icon { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--hairline); margin-right: 8px; flex-shrink: 0; }
.list-clean { list-style: none; margin: 0; padding: 0; }

/* ─── Pills ─── */
.pills { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.pill { display: inline-flex; align-items: center; gap: 7px; padding: 8px 14px; background: white; border: 1px solid var(--border); border-radius: 999px; font-size: 13px; }

/* ─── Input Bar ─── */
.input-bar { display: flex; gap: 10px; align-items: center; background: white; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; margin-top: 14px; }
.input-bar select, .input-bar input { border: 1px solid var(--hairline); background: var(--hairline); border-radius: var(--radius-sm); padding: 9px 12px; }
.input-bar input { flex: 1; min-width: 120px; }

/* ─── KPI ─── */
.kpi { display: flex; align-items: center; gap: 10px; }
.kpi .num { font-size: 26px; font-weight: 700; letter-spacing: -0.03em; }

/* ─── Misc ─── */
.glass { background: rgba(255,255,255,.75); backdrop-filter: blur(8px); border: 1px solid var(--border); }
.subtle-bg { background: radial-gradient(circle at 20% 10%, rgba(79,70,229,.04), transparent 40%), radial-gradient(circle at 80% 0%, rgba(13,148,136,.04), transparent 40%); }
.g-indigo { background: linear-gradient(135deg, var(--indigo), var(--blue)); }
.g-purple { background: linear-gradient(135deg, var(--purple), #ec4899); }
.g-orange { background: linear-gradient(135deg, #fbbf24, var(--orange)); }
.g-teal { background: linear-gradient(135deg, var(--teal), #06b6d4); }
.trust { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.avatar { width: 22px; height: 22px; border-radius: 50%; background: #e5e7eb; display: inline-block; }
.img-icon { position: absolute; right: 12px; bottom: 12px; background: white; border: 1px solid var(--border); border-radius: 999px; padding: 5px; }
.hero .grid { gap: 16px; }

/* ─── Reveal ─── */
.reveal { opacity: 0; transform: translateY(10px); transition: opacity 0.4s ease, transform 0.4s ease; }
.reveal.in { opacity: 1; transform: translateY(0); }

/* ─── FAQ / Details ─── */
details summary { cursor: pointer; list-style: none; padding: 14px 0; border-bottom: 1px solid var(--hairline); display: flex; align-items: center; gap: 8px; font-weight: 500; }
details[open] summary .caret { transform: rotate(180deg); }
.caret { transition: transform 0.2s ease; }

/* ─── Testimonials ─── */
.testimonials { overflow: hidden; --fade: linear-gradient(to bottom, transparent 0%, rgba(0,0,0,.9) 15%, rgba(0,0,0,.9) 85%, transparent 100%); -webkit-mask-image: var(--fade); mask-image: var(--fade); }
.twrap { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 768px) { .twrap { grid-template-columns: repeat(3, minmax(0,1fr)); } }
.tcol { will-change: transform; animation: marquee var(--dur,22s) linear infinite; }
.tcol:hover { animation-play-state: paused; }
.tlist { display: flex; flex-direction: column; gap: 10px; padding-bottom: 10px; }
.tcard { display: flex; flex-direction: column; justify-content: space-between; padding: 16px; border: 1px solid var(--border); border-radius: var(--radius-lg); background: white; width: 100%; min-height: 190px; }
.ttext { flex: 1; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; font-size: 14px; line-height: 1.65; }
.tmeta { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.tavatar { width: 30px; height: 30px; border-radius: 50%; background: var(--hairline); display: inline-flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 600; color: var(--muted); flex-shrink: 0; }
@keyframes marquee { 0% { transform: translateY(0); } 100% { transform: translateY(-50%); } }
#testimonials .testimonials { padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-lg); background: linear-gradient(180deg, var(--hairline), white); }
@media (max-width: 767px) {
  .testimonials { mask-image: none!important; -webkit-mask-image: none!important; overflow: visible!important; }
  .tcol { animation: none!important; transform: none!important; }
  .tlist[aria-hidden="true"] { display: none!important; }
  .nav-actions .btn.secondary { display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) { .tcol { animation: none; } }

/* ─── Progress Bar ─── */
.progress-wrap { width: 100%; max-width: 680px; margin: 8px auto; }
.progress-track { width: 100%; height: 8px; background: var(--hairline); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--primary), var(--primary-hover)); transition: width 0.3s ease; }
.progress-fill.complete { background: linear-gradient(90deg, var(--primary), #166534); }
.progress-fill.error { background: linear-gradient(90deg, #ef4444, #b91c1c); }
.progress-fill.stalled { background: linear-gradient(90deg, #f59e0b, #d97706); }
.progress-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 5px; font-size: 12px; color: var(--muted); }
.progress-actions { display: flex; gap: 8px; }
.progress-cancel { padding: 5px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: white; font-size: 13px; cursor: pointer; }

/* ─── Bottom Nav (Mobile) ─── */
.bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  height: 64px;
  z-index: 1000;
  padding: 0 10px;
}

@media (max-width: 768px) {
  .bottom-nav {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  body { padding-bottom: 64px; } /* Space for bottom nav */
}

.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--muted);
  gap: 4px;
  flex: 1;
  transition: all var(--transition);
}

.bottom-nav-item svg {
  width: 20px;
  height: 20px;
  transition: transform var(--transition);
}

.bottom-nav-item span {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.bottom-nav-item.active {
  color: var(--primary);
}

.bottom-nav-item.active svg {
  transform: translateY(-2px);
}

</style>
</head>
<body>
<header id="main-header">
  <div class="container">
    <nav class="nav">
      <div class="brand-zone">
        <a href="<?=url('/')?>" class="brand">
          <?php 
            $logoPath = !empty($siteLogo) ? $siteLogo : 'assets/logo.svg';
            if (file_exists(__DIR__ . '/../' . $logoPath)): 
          ?>
            <img src="<?=url($logoPath)?>" alt="Go Legal">
          <?php else: ?>
            <span>Go Legal</span>
          <?php endif; ?>
        </a>
      </div>

      <div class="nav-links">
        <a class="nav-link" href="<?=url('services.php')?>">Services</a>
        <a class="nav-link" href="<?=url('pricing.php')?>">Pricing</a>
        <a class="nav-link" href="<?=url('blog')?>">Blog</a>
      </div>
      
      <div class="nav-actions">
        <?php if ($user): ?>
          <a class="nav-link icon-link" href="<?=url('notifications.php')?>" title="Notifications">
            <?=ui_icon('notifications',22)?>
            <?php 
              $unreadCount = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = ? AND status = 'unread'");
              $unreadCount->execute([$user['id']]);
              $count = (int)$unreadCount->fetchColumn();
              if ($count > 0):
            ?>
              <span class="notification-badge"><?=$count?></span>
            <?php endif; ?>
          </a>
          <div class="user-menu">
            <button class="user-avatar">
              <?=substr(($user['first_name'] ?? 'U'),0,1)?><?=substr(($user['last_name'] ?? 'N'),0,1)?>
            </button>
            <div class="user-dropdown">
              <div class="user-dropdown-header">
                <strong><?=e(($user['first_name'] ?? '').' '.($user['last_name'] ?? ''))?></strong>
                <small><?=e($user['email'] ?? '')?></small>
              </div>
              <div class="user-dropdown-divider"></div>
              
              <?php if ($user && ($user['role'] ?? '') === 'admin'): ?>
                <a href="<?=url('admin/dashboard.php')?>" class="user-dropdown-link"><?=ui_icon('dashboard',16)?> Admin Dashboard</a>
                <a href="<?=url('admin/settings.php')?>" class="user-dropdown-link"><?=ui_icon('settings',16)?> System Settings</a>
                <div class="user-dropdown-divider"></div>
              <?php elseif ($user): ?>
                <a href="<?=url('customer/dashboard.php')?>" class="user-dropdown-link"><?=ui_icon('dashboard',16)?> My Dashboard</a>
                <a href="<?=url('customer/profile.php')?>" class="user-dropdown-link"><?=ui_icon('users',16)?> My Profile</a>
                <div class="user-dropdown-divider"></div>
              <?php endif; ?>
              
              <a href="<?=url('logout.php')?>" class="user-dropdown-link logout"><?=ui_icon('logout',16)?> Logout</a>
            </div>
          </div>
        <?php else: ?>
          <a class="nav-link login-link" href="<?=url('login.php')?>">Log In</a>
          <a class="btn primary" href="<?=url('register.php')?>">Get Started</a>
        <?php endif; ?>

        <button class="nav-toggle-btn" id="drawer-toggle" aria-label="Toggle menu">
          <span></span><span></span><span></span>
        </button>
      </div>
    </nav>
  </div>
</header>

<div class="drawer-overlay" id="drawer-overlay"></div>
<div class="mobile-drawer" id="mobile-drawer">
  <div class="drawer-header">
    <a href="<?=url('/')?>" class="brand">
      <?php 
        $logoPath = !empty($siteLogo) ? $siteLogo : 'assets/logo.svg';
        if (file_exists(__DIR__ . '/../' . $logoPath)): 
      ?>
        <img src="<?=url($logoPath)?>" alt="Go Legal" style="height: 32px;">
      <?php else: ?>
        <span>Go Legal</span>
      <?php endif; ?>
    </a>
  </div>
  <div class="drawer-content">
    <a class="drawer-link" href="<?=url('/')?>">Home</a>
    <a class="drawer-link" href="<?=url('services.php')?>">Services</a>
    <a class="drawer-link" href="<?=url('pricing.php')?>">Pricing</a>
    <a class="drawer-link" href="<?=url('blog')?>">Blog</a>
    
    <?php if (!empty($adminMenu)): ?>
      <div style="margin: 20px 0 10px; padding: 0 16px; font-size: 12px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 1px;">Admin Management</div>
      <?php foreach ($adminMenu as $category => $items): ?>
        <?php foreach ($items as $item): ?>
          <a class="drawer-link" href="<?=url($item['url'])?>">
            <?=ui_icon($item['icon'], 18)?> <?=e($item['label'])?>
          </a>
        <?php endforeach; ?>
      <?php endforeach; ?>
    <?php endif; ?>
  </div>
  <div class="drawer-footer">
    <?php if ($user): ?>
      <a class="btn secondary" href="<?=url('logout.php')?>" style="width: 100%;">Logout</a>
    <?php else: ?>
      <a class="btn primary" href="<?=url('login.php')?>" style="width: 100%;">Login / Sign Up</a>
    <?php endif; ?>
  </div>
</div>
<?php
$current_page = $_SERVER['REQUEST_URI'];
$is_admin = $user && ($user['role'] ?? '') === 'admin';
$is_customer = $user && ($user['role'] ?? '') === 'customer';
?>
<nav class="bottom-nav">
  <?php if ($is_admin): ?>
    <a href="<?=url('admin/dashboard.php')?>" class="bottom-nav-item <?= strpos($current_page, '/admin/dashboard.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('dashboard', 20) ?>
      <span>Home</span>
    </a>
    <a href="<?=url('admin/applications.php')?>" class="bottom-nav-item <?= strpos($current_page, '/admin/applications.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('applications', 20) ?>
      <span>Apps</span>
    </a>
    <a href="<?=url('admin/messages.php')?>" class="bottom-nav-item <?= strpos($current_page, '/admin/messages.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('faq', 20) ?>
      <span>Chat</span>
    </a>
    <a href="<?=url('admin/settings.php')?>" class="bottom-nav-item <?= strpos($current_page, '/admin/settings.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('settings', 20) ?>
      <span>Settings</span>
    </a>
  <?php elseif ($is_customer): ?>
    <a href="<?=url('customer/dashboard.php')?>" class="bottom-nav-item <?= strpos($current_page, '/customer/dashboard.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('dashboard', 20) ?>
      <span>Home</span>
    </a>
    <a href="<?=url('services.php')?>" class="bottom-nav-item <?= strpos($current_page, '/services.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('services', 20) ?>
      <span>Services</span>
    </a>
    <a href="<?=url('customer/profile.php')?>" class="bottom-nav-item <?= strpos($current_page, '/customer/profile.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('users', 20) ?>
      <span>Profile</span>
    </a>
    <a href="<?=url('notifications.php')?>" class="bottom-nav-item <?= strpos($current_page, '/notifications.php') !== false ? 'active' : '' ?>" style="position:relative">
      <?= ui_icon('notifications', 20) ?>
      <span>Alerts</span>
      <?php if (($count ?? 0) > 0): ?>
        <span class="notification-badge" style="top:2px;right:18px"><?= $count ?></span>
      <?php endif; ?>
    </a>
  <?php else: ?>
    <a href="<?=url('/')?>" class="bottom-nav-item <?= $current_page === '/' || $current_page === '/index.php' ? 'active' : '' ?>">
      <?= ui_icon('home', 20) ?>
      <span>Home</span>
    </a>
    <a href="<?=url('services.php')?>" class="bottom-nav-item <?= strpos($current_page, '/services.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('services', 20) ?>
      <span>Services</span>
    </a>
    <a href="<?=url('pricing.php')?>" class="bottom-nav-item <?= strpos($current_page, '/pricing.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('pricing', 20) ?>
      <span>Pricing</span>
    </a>
    <a href="<?=url('login.php')?>" class="bottom-nav-item <?= strpos($current_page, '/login.php') !== false ? 'active' : '' ?>">
      <?= ui_icon('login', 20) ?>
      <span>Login</span>
    </a>
  <?php endif; ?>
</nav>

<script>
// Header scroll effect
window.addEventListener('scroll', function() {
    const header = document.getElementById('main-header');
    if (window.scrollY > 20) {
        header.classList.add('scrolled');
    } else {
        header.classList.remove('scrolled');
    }
});

// Mobile drawer toggle
document.addEventListener('DOMContentLoaded', function() {
    const drawerToggle = document.getElementById('drawer-toggle');
    const mobileDrawer = document.getElementById('mobile-drawer');
    const drawerOverlay = document.getElementById('drawer-overlay');
    
    function toggleDrawer() {
        drawerToggle.classList.toggle('open');
        mobileDrawer.classList.toggle('open');
        drawerOverlay.classList.toggle('open');
        document.body.style.overflow = mobileDrawer.classList.contains('open') ? 'hidden' : '';
    }
    
    if (drawerToggle) {
        drawerToggle.addEventListener('click', toggleDrawer);
    }
    
    if (drawerOverlay) {
        drawerOverlay.addEventListener('click', toggleDrawer);
    }

    // Active navigation link highlighting
    const currentPath = window.location.pathname;
    const navLinks = document.querySelectorAll('.nav-link, .drawer-link');
    
    navLinks.forEach(link => {
        const href = link.getAttribute('href');
        if (href && currentPath.includes(href) && href !== '/') {
            link.classList.add('active');
        } else if (href === '/' && currentPath === '/') {
            link.classList.add('active');
        }
    });

    // User dropdown functionality
    const userAvatar = document.querySelector('.user-avatar');
    const userDropdown = document.querySelector('.user-dropdown');
    
    if (userAvatar && userDropdown) {
        userAvatar.addEventListener('click', function(e) {
            e.stopPropagation();
            userDropdown.classList.toggle('show');
        });
        
        document.addEventListener('click', function(e) {
            if (!userAvatar.contains(e.target)) {
                userDropdown.classList.remove('show');
            }
        });
    }
});
</script>

<main class="container">
  <?php
    $uri = $_SERVER['REQUEST_URI'] ?? '';
    $isAdminArea = strpos($uri, '/admin/') !== false;
    $isCustomerArea = strpos($uri, '/customer/') !== false;
    $isRootNotifs = strpos($uri, '/notifications.php') !== false;

    if ($isAdminArea || $isCustomerArea || ($isRootNotifs && $user)) {
      echo '<div class="layout">';
      echo '<aside class="sidebar">';
      
      $role = $user['role'] ?? 'customer';
      if ($isAdminArea || ($isRootNotifs && $role === 'admin')) {
        $active = [
          'dashboard' => strpos($uri,'/admin/dashboard.php')!==false,
          'seo' => strpos($uri,'/admin/seo_')!==false,
          'blog' => strpos($uri,'/admin/blog.php')!==false,
          'services' => strpos($uri,'/admin/services.php')!==false,
          'packages' => strpos($uri,'/admin/packages.php')!==false,
          'apps' => strpos($uri,'/admin/applications.php')!==false,
          'payments' => strpos($uri,'/admin/payments.php')!==false,
          'commissions' => strpos($uri,'/admin/commissions.php')!==false,
          'users' => strpos($uri,'/admin/users.php')!==false,
          'messages' => strpos($uri,'/admin/messages.php')!==false,
          'notifs' => strpos($uri,'/admin/notifications.php')!==false || $isRootNotifs,
          'settings' => strpos($uri,'/admin/settings.php')!==false,
        ];
        
        $unreadCount = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = ? AND status = 'unread'");
        $unreadCount->execute([$user['id']]);
        $notifCount = (int)$unreadCount->fetchColumn();
        
        echo '<a class="side-link '.($active['dashboard']?'side-active':'').'" href="'.url('admin/dashboard.php').'">'.ui_icon('dashboard',18).' Dashboard</a>';
        echo '<a class="side-link '.($active['notifs']?'side-active':'').'" href="'.url('admin/notifications.php').'" style="position:relative">'.ui_icon('faq',18).' Notifications '.($notifCount > 0 ? '<span style="background:#ef4444; color:#fff; font-size:10px; padding:2px 6px; border-radius:10px; margin-left:auto">'.$notifCount.'</span>' : '').'</a>';

        echo '<div class="side-head">Operations</div>';
        echo '<a class="side-link '.($active['seo']?'side-active':'').'" href="'.url('admin/seo_hub.php').'">'.ui_icon('dashboard',18).' SEO Hub</a>';
        echo '<a class="side-link '.($active['blog']?'side-active':'').'" href="'.url('admin/blog.php').'">'.ui_icon('blog',18).' Blog Management</a>';
        echo '<a class="side-link '.($active['apps']?'side-active':'').'" href="'.url('admin/applications.php').'">'.ui_icon('applications',18).' Applications</a>';
        echo '<a class="side-link '.($active['services']?'side-active':'').'" href="'.url('admin/services.php').'">'.ui_icon('services',18).' Services</a>';
        echo '<a class="side-link '.($active['packages']?'side-active':'').'" href="'.url('admin/packages.php').'">'.ui_icon('applications',18).' Packages</a>';

        echo '<div class="side-head">Finance</div>';
        echo '<a class="side-link '.($active['payments']?'side-active':'').'" href="'.url('admin/payments.php').'">'.ui_icon('pricing',18).' Payment Approval</a>';
        echo '<a class="side-link '.($active['commissions']?'side-active':'').'" href="'.url('admin/commissions.php').'">'.ui_icon('pricing',18).' Commissions</a>';

        echo '<div class="side-head">Administration</div>';
        echo '<a class="side-link '.($active['users']?'side-active':'').'" href="'.url('admin/users.php').'">'.ui_icon('users',18).' Users</a>';
        echo '<a class="side-link '.($active['messages']?'side-active':'').'" href="'.url('admin/messages.php').'">'.ui_icon('faq',18).' Support Messages</a>';
        echo '<a class="side-link '.($active['notifs']?'side-active':'').'" href="'.url('admin/notifications.php').'">'.ui_icon('faq',18).' Notifications</a>';
        echo '<a class="side-link '.($active['settings']?'side-active':'').'" href="'.url('admin/settings.php').'">'.ui_icon('services',18).' Settings</a>';
        
        echo '<div class="side-divider" style="height: 1px; background: var(--border); margin: 20px 0;"></div>';
        echo '<a class="side-link logout" href="'.url('logout.php').'" style="color: var(--red);">'.ui_icon('logout',18).' Logout</a>';
      } else {
        $active = [
          'dashboard' => strpos($uri,'/customer/dashboard.php')!==false,
          'payments' => strpos($uri,'/customer/payments.php')!==false,
          'notifs' => $isRootNotifs,
        ];

        $unreadCount = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = ? AND status = 'unread'");
        $unreadCount->execute([$user['id']]);
        $notifCount = (int)$unreadCount->fetchColumn();

        echo '<a class="side-link '.($active['dashboard']?'side-active':'').'" href="'.url('customer/dashboard.php').'">'.ui_icon('dashboard',18).' My Dashboard</a>';
        echo '<a class="side-link" href="'.url('customer/referrals.php').'">'.ui_icon('users',18).' Referrals</a>';
        echo '<a class="side-link '.($active['payments']?'side-active':'').'" href="'.url('customer/payments.php').'">'.ui_icon('pricing',18).' Payments</a>';
        
        echo '<div class="side-divider" style="height: 1px; background: var(--border); margin: 20px 0;"></div>';
        echo '<a class="side-link logout" href="'.url('logout.php').'" style="color: var(--red);">'.ui_icon('logout',18).' Logout</a>';
      }
      echo '</aside><div class="content">';
    }
    foreach ($flashes as $type=>$msgs): foreach ($msgs as $m): ?>
      <div class="flash <?=$type==='success'?'success':'error'?>"><?=e($m)?></div>
    <?php endforeach; endforeach; ?>

    <!-- Chat Widget -->
    <?php if ($user): ?>
    <div class="chat-widget-container" data-user-id="<?= $user['id'] ?>" data-guest-id=""></div>
    <?php else: ?>
    <div class="chat-widget-container" data-user-id="" data-guest-id="<?= get_or_create_guest_id() ?>"></div>
    <?php endif; ?>
    <?php include __DIR__ . '/chat_widget.php'; ?>
