<?php
if (!defined('SECURE_ACCESS')) {
    header("HTTP/1.1 403 Forbidden");
    exit("Direct access not allowed.");
}
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/auth.php';

$prefix = '';
$project_root = '';
if (isset($_SERVER['REQUEST_URI'])) {
    $script_dir = dirname($_SERVER['SCRIPT_NAME']);
    if (stripos($_SERVER['SCRIPT_NAME'], '/admin/') !== false || stripos($_SERVER['SCRIPT_NAME'], '/api/') !== false) {
        $project_root = dirname($script_dir);
    } else {
        $project_root = $script_dir;
    }
    $project_root = rtrim(str_replace('\\', '/', $project_root), '/');
    
    // Adjust for the new 'public' document root fallback
    if (strtolower(substr($project_root, -7)) === '/public') {
        $project_root = substr($project_root, 0, -7);
    }
    
    $request_path = strtok($_SERVER['REQUEST_URI'], '?');
    $request_path = rtrim(str_replace('\\', '/', $request_path), '/');
    
    $relative = '';
    if ($project_root !== '') {
        if (stripos($request_path, $project_root) === 0) {
            $relative = substr($request_path, strlen($project_root));
        }
    } else {
        $relative = $request_path;
    }
    $relative = trim($relative, '/');
    if ($relative !== '') {
        $depth = count(explode('/', $relative)) - 1;
        $prefix = str_repeat('../', $depth);
    }
}
$currentUser = getCurrentUser();

$site_name = 'EventPortal';
$site_tagline = 'Event Management & Matrix Network';
$site_logo = '';
$site_favicon = '';
$hide_site_name = false;

try {
    global $pdo;
    if ($pdo) {
        $stmt_general = $pdo->query("SELECT key, value FROM system_settings WHERE key IN ('site_name', 'site_tagline', 'site_logo', 'site_favicon', 'hide_site_name', 'google_analytics_enabled', 'google_analytics_id', 'registration_mode', 'company_address', 'contact_email', 'contact_phone', 'appointment_letter_template')");
        $site_cfg = $stmt_general->fetchAll(PDO::FETCH_KEY_PAIR) ?: [];
        
        $site_name = $site_cfg['site_name'] ?? 'EventPortal';
        $site_tagline = $site_cfg['site_tagline'] ?? 'Event Management & Matrix Network';
        $site_logo = $site_cfg['site_logo'] ?? '';
        $site_favicon = $site_cfg['site_favicon'] ?? '';
        $hide_site_name = ($site_cfg['hide_site_name'] ?? '0') === '1';
        $registration_mode = $site_cfg['registration_mode'] ?? 'invite_only';
    }
} catch (PDOException $e) {
    // Ignore
}

$unread_count = 0;
$bell_notifications = [];

if ($currentUser) {
    global $pdo;
    
    // Handle Mark all read from the bell dropdown
    if (isset($_GET['action']) && $_GET['action'] === 'read_bell_notifications') {
        if (!verifyCSRFToken($_GET['csrf_token'] ?? '')) {
            $_SESSION['flash_error'] = "Security check failed (CSRF). Please try again.";
            $redirect_url = strtok($_SERVER["REQUEST_URI"], '?');
            header("Location: " . $redirect_url);
            exit();
        }
        try {
            if ($pdo) {
                $stmt_read = $pdo->prepare("UPDATE notifications SET is_read = TRUE WHERE user_id = ? AND channels_sent LIKE '%in_app%'");
                $stmt_read->execute([$currentUser['id']]);
                $_SESSION['flash_success'] = "All notifications marked as read.";
            }
        } catch (PDOException $e) {
            $_SESSION['flash_error'] = "Failed to mark notifications as read.";
        }
        $redirect_url = strtok($_SERVER["REQUEST_URI"], '?');
        header("Location: " . $redirect_url);
        exit();
    }

    try {
        if ($pdo) {
            $stmt_bell = $pdo->prepare("
                SELECT * FROM notifications 
                WHERE user_id = ? AND channels_sent LIKE '%in_app%' 
                ORDER BY created_at DESC 
                LIMIT 5
            ");
            $stmt_bell->execute([$currentUser['id']]);
            $bell_notifications = $stmt_bell->fetchAll();
            
            $stmt_count = $pdo->prepare("
                SELECT COUNT(*) FROM notifications 
                WHERE user_id = ? AND channels_sent LIKE '%in_app%' AND is_read = FALSE
            ");
            $stmt_count->execute([$currentUser['id']]);
            $unread_count = $stmt_count->fetchColumn();
        }
    } catch (PDOException $e) {
        // Ignore
    }

    // Fetch User details for navigation bar avatar
    try {
        if ($pdo) {
            $stmt_avatar = $pdo->prepare("SELECT profile_picture_url, first_name, last_name FROM users WHERE id = ?");
            $stmt_avatar->execute([$currentUser['id']]);
            $userData = $stmt_avatar->fetch(PDO::FETCH_ASSOC) ?: [];
            $nav_profile_pic = $userData['profile_picture_url'] ?? '';
            $nav_first_name = $userData['first_name'] ?? '';
            $nav_last_name = $userData['last_name'] ?? '';
            $nav_full_name = trim($nav_first_name . ' ' . $nav_last_name);
            
            $avatar_exists = !empty($nav_profile_pic) && file_exists(__DIR__ . '/../' . $nav_profile_pic);
            $nav_avatar_img = $avatar_exists 
                ? $prefix . $nav_profile_pic 
                : 'https://ui-avatars.com/api/?name=' . urlencode(!empty($nav_full_name) ? $nav_full_name : $currentUser['username']) . '&background=0d6efd&color=fff&size=100';
        }
    } catch (PDOException $e) {
        // Fallback
    }
}
if (!isset($nav_avatar_img)) {
    $nav_avatar_img = '';
}
?>
<!DOCTYPE html>
<html lang="en" class="h-100">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        window.projectRoot = "<?php echo htmlspecialchars($project_root ?? ''); ?>";
    </script>
    
    <!-- Google Analytics -->
    <?php if (($site_cfg['google_analytics_enabled'] ?? '0') === '1' && !empty($site_cfg['google_analytics_id'])): ?>
        <script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo htmlspecialchars($site_cfg['google_analytics_id']); ?>"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', '<?php echo htmlspecialchars($site_cfg['google_analytics_id']); ?>');
        </script>
    <?php endif; ?>
    <title><?php echo htmlspecialchars($site_name) . " - " . htmlspecialchars($site_tagline); ?></title>
    <?php if (!empty($site_favicon)): ?>
        <link class="favicon-link" rel="icon" type="image/x-icon" href="<?php echo htmlspecialchars($prefix . $site_favicon); ?>?v=<?php echo file_exists(__DIR__ . '/../' . $site_favicon) ? filemtime(__DIR__ . '/../' . $site_favicon) : time(); ?>">
    <?php endif; ?>

    <!-- SEO and Social Media Meta Tags -->
    <?php
    $site_name_val = $site_name ?? 'EventPortal';
    $og_title = isset($meta_title) ? $meta_title : (isset($pageTitle) ? $pageTitle . " - " . $site_name_val : $site_name_val);
    $og_desc = isset($meta_description) ? $meta_description : ($site_tagline ?? 'Premium Event Portal and Corporate Hierarchy Network');
    $og_img = isset($meta_image) ? $meta_image : (!empty($site_logo) ? $site_logo : 'https://images.unsplash.com/photo-1618005182384-a83a8bd57fbe?w=1000&auto=format&fit=crop');
    // Ensure absolute image URL for OG preview compatibility
    if ($og_img && !preg_match('/^https?:\/\//i', $og_img)) {
        $base = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/';
        $og_img = $base . ltrim($og_img, '/');
    }
    $og_url = isset($meta_url) ? $meta_url : ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    ?>
    <meta name="description" content="<?php echo htmlspecialchars($og_desc); ?>">
    
    <!-- Open Graph / Facebook -->
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?php echo htmlspecialchars($og_url); ?>">
    <meta property="og:title" content="<?php echo htmlspecialchars($og_title); ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars($og_desc); ?>">
    <meta property="og:image" content="<?php echo htmlspecialchars($og_img); ?>">
    <meta property="og:site_name" content="<?php echo htmlspecialchars($site_name_val); ?>">

    <!-- Twitter -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:url" content="<?php echo htmlspecialchars($og_url); ?>">
    <meta name="twitter:title" content="<?php echo htmlspecialchars($og_title); ?>">
    <meta name="twitter:description" content="<?php echo htmlspecialchars($og_desc); ?>">
    <meta name="twitter:image" content="<?php echo htmlspecialchars($og_img); ?>">

    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    
    <style>
        /* Responsive font sizes based on screen width */
        html {
            font-size: 16px;
        }
        @media (max-width: 576px) {
            html { font-size: 14px; }
        }
        @media (min-width: 576px) and (max-width: 992px) {
            html { font-size: 15px; }
        }
        @media (min-width: 992px) {
            html { font-size: 16px; }
        }

        body {
            font-size: 1rem;
            background-color: #f8f9fa;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            padding-top: 76px; /* Offset for fixed-top navbar */
        }
        
        .navbar-brand {
            font-weight: 700;
            letter-spacing: 0.5px;
        }

        /* Smooth hover effects for interactive elements */
        .nav-link {
            transition: color 0.2s ease-in-out;
        }

        .btn {
            transition: all 0.2s ease-in-out;
        }

        main {
            flex: 1 0 auto;
        }

        footer {
            flex-shrink: 0;
        }
    </style>
</head>
<body class="d-flex flex-column h-100">

<header>
    <!-- Full width fluid navbar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom shadow-sm py-3 fixed-top">
        <div class="container-fluid px-md-5">
            <a class="navbar-brand text-primary d-flex align-items-center" href="<?php echo $prefix; ?>dashboard">
                <?php if (!empty($site_logo)): ?>
                    <img src="<?php echo htmlspecialchars($prefix . $site_logo); ?>?v=<?php echo file_exists(__DIR__ . '/../' . $site_logo) ? filemtime(__DIR__ . '/../' . $site_logo) : time(); ?>" alt="Logo" class="d-inline-block align-top me-2" style="height: 32px; max-width: 150px; object-fit: contain;">
                <?php else: ?>
                    <i class="bi bi-calendar-event-fill me-2"></i>
                <?php endif; ?>
                
                <?php if (empty($site_logo) || !$hide_site_name): ?>
                    <span class="fw-bold"><?php echo htmlspecialchars($site_name); ?></span>
                <?php endif; ?>
            </a>
            
            <div class="d-flex align-items-center gap-2 gap-sm-3 order-lg-last ms-auto me-2 me-lg-0">
                <?php if (isLoggedIn()): ?>
                    <!-- Notification Bell Dropdown -->
                    <div class="dropdown">
                        <button class="btn btn-light position-relative p-2 rounded-circle border-0 d-flex align-items-center justify-content-center" type="button" id="bellDropdown" data-bs-toggle="dropdown" aria-expanded="false" style="width: 40px; height: 40px;">
                            <i class="bi bi-bell-fill fs-5 text-secondary"></i>
                            <?php if ($unread_count > 0): ?>
                                <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger border border-white" style="font-size: 0.65rem;">
                                    <?php echo $unread_count; ?>
                                </span>
                            <?php endif; ?>
                        </button>
                        <div class="dropdown-menu dropdown-menu-end shadow-lg border-0 mt-2 p-0" aria-labelledby="bellDropdown" style="width: 320px; z-index: 1050;">
                            <div class="p-3 bg-light border-bottom d-flex justify-content-between align-items-center rounded-top">
                                <span class="fw-bold text-dark small">Inbox Notifications</span>
                                <?php if ($unread_count > 0): ?>
                                    <a href="?action=read_bell_notifications&csrf_token=<?php echo getCSRFToken(); ?>" class="text-primary text-decoration-none small fw-semibold" style="font-size: 0.75rem;">Mark all read</a>
                                <?php endif; ?>
                            </div>
                            <div class="list-group list-group-flush" style="max-height: 280px; overflow-y: auto;">
                                <?php if (empty($bell_notifications)): ?>
                                    <div class="text-center py-4 text-muted">
                                        <i class="bi bi-bell-slash fs-4 mb-1"></i>
                                        <p class="mb-0 small">No notifications yet</p>
                                    </div>
                                <?php else: ?>
                                    <?php foreach ($bell_notifications as $bn): ?>
                                        <div class="list-group-item p-3 bg-white">
                                            <div class="d-flex justify-content-between align-items-center mb-1">
                                                <span class="fw-bold text-dark small text-truncate d-flex align-items-center" style="max-width: 180px;">
                                                    <?php if (!$bn['is_read']): ?>
                                                        <span class="bg-primary rounded-circle me-2" style="width: 8px; height: 8px; flex-shrink: 0; display: inline-block;"></span>
                                                    <?php endif; ?>
                                                    <?php echo htmlspecialchars($bn['title']); ?>
                                                </span>
                                                <small class="text-muted font-monospace" style="font-size: 0.65rem;"><?php echo date('M d, H:i', strtotime($bn['created_at'])); ?></small>
                                            </div>
                                            <p class="text-secondary small mb-0 lh-sm text-truncate-2" style="font-size: 0.75rem;"><?php echo htmlspecialchars($bn['message']); ?></p>
                                        </div>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </div>
                            <div class="p-2 border-top text-center bg-white rounded-bottom">
                                <a href="<?php echo $prefix; ?>notifications" class="text-decoration-none small fw-bold text-primary py-1 d-block" style="font-size: 0.8rem;">View All Notifications</a>
                            </div>
                        </div>
                    </div>

                    <div class="dropdown">
                        <button class="btn btn-light border-0 dropdown-toggle d-flex align-items-center gap-2" type="button" id="userMenu" data-bs-toggle="dropdown" aria-expanded="false">
                            <img src="<?php echo htmlspecialchars($nav_avatar_img); ?>" alt="Avatar" class="rounded-circle" style="width: 26px; height: 26px; object-fit: cover;">
                            <span class="fw-semibold text-secondary d-none d-lg-inline"><?php echo htmlspecialchars(!empty($nav_first_name) ? $nav_first_name : $currentUser['username']); ?></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-end shadow-sm border-0 mt-2" aria-labelledby="userMenu">
                            <li>
                                <div class="dropdown-header border-bottom pb-2 mb-1" style="text-transform: none;">
                                    <div class="fw-bold text-dark fs-6"><?php echo htmlspecialchars(!empty($nav_full_name) ? $nav_full_name : $currentUser['username']); ?></div>
                                    <div class="text-secondary small font-monospace" style="font-size: 0.72rem;"><?php echo htmlspecialchars($currentUser['email']); ?></div>
                                </div>
                            </li>
                            <li>
                                <a class="dropdown-item d-flex align-items-center gap-2 <?php echo basename($_SERVER['PHP_SELF']) == 'profile.php' ? 'active' : ''; ?>" href="<?php echo $prefix; ?>profile">
                                    <i class="bi bi-person-badge text-primary"></i> My Profile
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item d-flex align-items-center gap-2 <?php echo basename($_SERVER['PHP_SELF']) == 'documents.php' ? 'active' : ''; ?>" href="<?php echo $prefix; ?>documents">
                                    <i class="bi bi-file-earmark-person text-primary"></i> My Documents
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item d-flex align-items-center gap-2 <?php echo basename($_SERVER['PHP_SELF']) == 'kyc.php' ? 'active' : ''; ?>" href="<?php echo $prefix; ?>kyc">
                                    <i class="bi bi-shield-check text-primary"></i> KYC Verification
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item d-flex align-items-center gap-2 <?php echo basename($_SERVER['PHP_SELF']) == 'payouts.php' ? 'active' : ''; ?>" href="<?php echo $prefix; ?>payouts">
                                    <i class="bi bi-cash-stack text-success"></i> Withdraw Payout
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item text-danger d-flex align-items-center gap-2" href="<?php echo $prefix; ?>logout">
                                    <i class="bi bi-box-arrow-right"></i> Log Out
                                </a>
                            </li>
                        </ul>
                    </div>
                <?php else: ?>
                    <?php if (($registration_mode ?? 'invite_only') === 'open'): ?>
                        <a href="<?php echo $prefix; ?>register" class="btn btn-outline-primary px-3 px-md-3 me-1" title="Register">
                            <span class="d-none d-md-inline">Register</span>
                            <i class="bi bi-person-plus d-inline d-md-none"></i>
                        </a>
                    <?php endif; ?>
                    <a href="<?php echo $prefix; ?>login" class="btn btn-primary px-3 px-md-4" title="Log In">
                        <span class="d-none d-md-inline"><i class="bi bi-box-arrow-in-right me-1"></i> Log In</span>
                        <i class="bi bi-box-arrow-in-right d-inline d-md-none"></i>
                    </a>
                <?php endif; ?>
            </div>

            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarContent">
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <?php if (isLoggedIn()): ?>
                        <li class="nav-item">
                            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'dashboard.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>dashboard">
                                <i class="bi bi-speedometer2 me-1"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'events.php' || basename($_SERVER['PHP_SELF']) == 'event_detail.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>events">
                                <i class="bi bi-calendar-check me-1"></i> Start Here
                            </a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle <?php echo in_array(basename($_SERVER['PHP_SELF']), ['our_team.php', 'speakers.php', 'organizers.php', 'sponsors.php']) ? 'active fw-bold text-primary' : ''; ?>" href="#" id="loggedNetworkDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="bi bi-people me-1"></i> Directory
                            </a>
                            <ul class="dropdown-menu shadow border-0 mt-2" aria-labelledby="loggedNetworkDropdown">
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>our_team"><i class="bi bi-award text-primary me-2"></i>Corporate Team</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>speakers"><i class="bi bi-mic text-success me-2"></i>Keynote Speakers</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>organizers"><i class="bi bi-person-workspace text-info me-2"></i>Event Organizers</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>sponsors"><i class="bi bi-briefcase text-secondary me-2"></i>Corporate Sponsors</a></li>
                            </ul>
                        </li>
                        
                        <?php if ($currentUser['role'] === 'admin'): ?>
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle <?php echo in_array(basename($_SERVER['PHP_SELF']), ['users.php', 'designations.php', 'admin_payouts.php', 'reports.php', 'notifications_admin.php', 'settings.php', 'kyc_approvals.php', 'cms.php', 'contact_messages.php']) ? 'active fw-bold text-primary' : ''; ?>" href="#" id="adminSystemDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                    <i class="bi bi-sliders me-1"></i> Administration
                                </a>
                                <ul class="dropdown-menu shadow border-0 mt-2" aria-labelledby="adminSystemDropdown">
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/users"><i class="bi bi-people-fill text-primary me-2"></i>User Management</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/pins"><i class="bi bi-key-fill text-danger me-2"></i>PIN Management</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/designations"><i class="bi bi-award-fill text-warning me-2"></i>Designations</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/kyc_approvals"><i class="bi bi-shield-fill-check text-info me-2"></i>KYC Approvals</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/payouts"><i class="bi bi-cash-coin text-success me-2"></i>Payout Approvals</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>reports"><i class="bi bi-bar-chart-line-fill text-info me-2"></i>System Reports</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/notifications"><i class="bi bi-bell-fill text-danger me-2"></i>Notifications Admin</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/cms"><i class="bi bi-file-earmark-richtext-fill text-dark me-2"></i>CMS Pages Manager</a></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/contact_messages"><i class="bi bi-chat-left-text-fill text-dark me-2"></i>Contact Inquiries</a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>admin/settings"><i class="bi bi-gear-fill text-secondary me-2"></i>System Settings</a></li>
                                </ul>
                            </li>
                            
                        <?php elseif ($currentUser['role'] === 'organizer'): ?>
                            <!-- Organizer Navigation Links -->
                            <li class="nav-item">
                                <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'reports.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>reports">
                                    <i class="bi bi-bar-chart-line me-1"></i> Event Analytics Reports
                                </a>
                            </li>
                            
                        <?php elseif ($currentUser['role'] === 'user'): ?>
                            <!-- Associate Member Navigation Links -->
                            <li class="nav-item">
                                <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'team.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>team">
                                    <i class="bi bi-people-fill me-1"></i> My Team
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'reports.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>reports">
                                    <i class="bi bi-bar-chart-line me-1"></i> My Earnings
                                </a>
                            </li>
                        <?php endif; ?>
                        <?php if (isLoggedIn()): ?>
                            <!-- Mobile-only User profile info drawer item -->
                            <li class="nav-item d-lg-none py-2 border-top mt-2">
                                <div class="d-flex align-items-center px-3 py-2 bg-light rounded-3">
                                    <i class="bi bi-person-circle fs-3 text-secondary me-2"></i>
                                    <div>
                                        <div class="fw-bold text-dark mb-0"><?php echo htmlspecialchars($currentUser['username']); ?></div>
                                        <div class="text-muted small" style="font-size: 0.75rem;"><?php echo htmlspecialchars($currentUser['email']); ?></div>
                                        <span class="badge bg-secondary-subtle text-secondary-emphasis font-monospace text-uppercase mt-1" style="font-size: 0.65rem;"><?php echo $currentUser['role']; ?></span>
                                    </div>
                                </div>
                            </li>
                        <?php endif; ?>
                    <?php else: ?>
                        <li class="nav-item">
                            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>index">
                                <i class="bi bi-house-door me-1"></i> Home
                            </a>
                        </li>

                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle <?php echo in_array(basename($_SERVER['PHP_SELF']), ['our_team.php', 'speakers.php', 'organizers.php', 'sponsors.php']) ? 'active fw-bold text-primary' : ''; ?>" href="#" id="guestNetworkDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                <i class="bi bi-people me-1"></i> Directory
                            </a>
                            <ul class="dropdown-menu shadow border-0 mt-2" aria-labelledby="guestNetworkDropdown">
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>our_team"><i class="bi bi-award text-primary me-2"></i>Corporate Team</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>speakers"><i class="bi bi-mic text-success me-2"></i>Keynote Speakers</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>organizers"><i class="bi bi-person-workspace text-info me-2"></i>Event Organizers</a></li>
                                <li><a class="dropdown-item py-2 px-3 rounded" href="<?php echo $prefix; ?>sponsors"><i class="bi bi-briefcase text-secondary me-2"></i>Corporate Sponsors</a></li>
                            </ul>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link <?php echo basename($_SERVER['PHP_SELF']) == 'contact.php' ? 'active fw-bold text-primary' : ''; ?>" href="<?php echo $prefix; ?>contact">
                                <i class="bi bi-envelope me-1"></i> Contact Us
                            </a>
                        </li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </nav>
</header>

<?php
$is_home = basename($_SERVER['PHP_SELF']) === 'index.php';
?>
<main class="<?php echo $is_home ? 'pt-5' : 'py-4'; ?>">
    <!-- Full-width container-fluid for main layout content -->
    <div class="<?php echo $is_home ? 'p-0 m-0 w-100 overflow-hidden' : 'container-fluid px-md-5'; ?>">
        
        <!-- Flash Alerts Disabled -->
        <?php 
        if (isset($_SESSION['flash_success'])) unset($_SESSION['flash_success']); 
        if (isset($_SESSION['flash_error'])) unset($_SESSION['flash_error']); 
        ?>
