<?php
// Ensure this path jumps up from public/ to the root for config
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/includes/ad-display.php';

$settings_file = __DIR__ . '/../config/site-settings.json';
$site_name = 'tipspac';
if (file_exists($settings_file)) {
    $loaded = json_decode(file_get_contents($settings_file), true);
    if (!empty($loaded['site_name'])) $site_name = $loaded['site_name'];
}

$is_vip = isset($_SESSION['user_tier']) && $_SESSION['user_tier'] === 'VIP';

// Helper: get trust score from confidence
function getTrust($row) {
    $conf = isset($row['confidence']) ? (int)$row['confidence'] : 75;
    return number_format($conf / 10, 1);
}

// Helper: Outcome badge
function outcomeBadge($status) {
    if ($status === 'WON') {
        return '<span class="bg-emerald-500/10 text-emerald-400 border border-emerald-500/20 px-2.5 py-0.5 rounded-full text-[9px] font-black tracking-wider uppercase flex items-center gap-1"><svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg> Won</span>';
    } elseif ($status === 'LOST') {
        return '<span class="bg-rose-500/10 text-rose-400 border border-rose-500/20 px-2.5 py-0.5 rounded-full text-[9px] font-black tracking-wider uppercase flex items-center gap-1"><svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg> Lost</span>';
    } else {
        return '<span class="bg-slate-800 text-slate-400 border border-slate-700 px-2.5 py-0.5 rounded-full text-[9px] font-black tracking-wider uppercase flex items-center gap-1"><span class="w-1.5 h-1.5 rounded-full bg-slate-500 animate-pulse"></span> Pending</span>';
    }
}

// ==========================================
// DATE FILTERING LOGIC
// ==========================================
$view_day = $_GET['day'] ?? 'today';
$query = "SELECT * FROM admin_manual_tips";

if ($view_day === 'yesterday') {
    $query .= " WHERE match_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY)";
} elseif ($view_day === 'upcoming') {
    $query .= " WHERE match_date > CURDATE()";
} elseif ($view_day === 'history') {
    $query .= " WHERE 1=1";
} else {
    $query .= " WHERE match_date = CURDATE()";
}

$query .= " ORDER BY match_date ASC, match_time ASC LIMIT 50";

try {
    $stmt = $pdo->query($query);
    $all_tips = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $all_tips = [];
}

$bankers = array_filter($all_tips, function($t) { return ($t['tip_type'] ?? 'BANKER') === 'BANKER'; });
$slip_of_the_day = array_filter($all_tips, function($t) { return ($t['tip_type'] ?? '') === 'SLIP'; });

$today_timestamp = strtotime(date('Y-m-d'));
?>

<?php require_once __DIR__ . '/includes/header.php'; ?>

<!-- Neural Background -->
<div class="fixed inset-0 z-0 pointer-events-none opacity-20 bg-cover bg-center bg-no-repeat" style="background-image: url('https://images.unsplash.com/photo-1579952363873-27f3bade9f55?q=80&w=2000&auto=format&fit=crop');"></div>
<div class="fixed inset-0 z-0 bg-gradient-to-t from-[#020617] via-[#020617]/85 to-transparent pointer-events-none"></div>
<canvas id="neuralCanvas" class="fixed inset-0 w-full h-full pointer-events-none z-0 opacity-40"></canvas>

<main class="relative z-10 flex-1 w-full max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 pt-8 pb-20 space-y-10">

    <!-- Header -->
    <div class="flex flex-col md:flex-row md:items-end justify-between gap-6 border-b border-slate-800/60 pb-6">
        <div>
            <div class="inline-flex items-center space-x-2 bg-emerald-500/10 border border-emerald-500/20 px-3 py-1 rounded-full text-[9px] font-bold tracking-widest uppercase text-emerald-400 mb-2">
                <span class="w-1.5 h-1.5 rounded-full bg-emerald-400 animate-pulse"></span>
                <span>Neural Odds Database</span>
            </div>
            <h1 class="text-4xl font-black text-white tracking-tight">Bet of the <span class="text-transparent bg-clip-text bg-gradient-to-r from-emerald-400 to-cyan-400">Day</span></h1>
            <p class="text-xs text-slate-400 mt-1 max-w-xl">Complete administration‑managed tracking of core selections.</p>
        </div>
        
        <div class="flex bg-slate-900/80 backdrop-blur border border-slate-800 rounded-xl p-1 shadow-lg overflow-x-auto">
            <a href="?day=yesterday" class="px-3 md:px-4 py-2 text-[10px] md:text-xs font-bold transition rounded-lg whitespace-nowrap <?php echo $view_day === 'yesterday' ? 'text-slate-950 bg-cyan-400 shadow uppercase tracking-wider' : 'text-slate-400 hover:text-white'; ?>">Yesterday</a>
            <a href="?day=today" class="px-3 md:px-4 py-2 text-[10px] md:text-xs font-bold transition rounded-lg whitespace-nowrap <?php echo $view_day === 'today' ? 'text-slate-950 bg-cyan-400 shadow uppercase tracking-wider' : 'text-slate-400 hover:text-white'; ?>">Today</a>
            <a href="?day=upcoming" class="px-3 md:px-4 py-2 text-[10px] md:text-xs font-bold transition rounded-lg whitespace-nowrap <?php echo $view_day === 'upcoming' ? 'text-slate-950 bg-emerald-400 shadow uppercase tracking-wider' : 'text-slate-400 hover:text-white'; ?>">Upcoming</a>
            <a href="?day=history" class="px-3 md:px-4 py-2 text-[10px] md:text-xs font-bold transition rounded-lg whitespace-nowrap <?php echo $view_day === 'history' ? 'text-slate-950 bg-cyan-400 shadow uppercase tracking-wider' : 'text-slate-400 hover:text-white'; ?>">History</a>
        </div>
    </div>

    <!-- Tabs -->
    <div class="space-y-6">
        <div class="flex space-x-8 border-b border-slate-800 pb-3 text-xs font-black tracking-[0.2em] uppercase overflow-x-auto whitespace-nowrap">
            <button id="tab-bankers" onclick="switchTab('bankers')" class="tab-btn text-emerald-400 border-b-2 border-emerald-400 pb-3 -mb-3.5 focus:outline-none transition">Bankers</button>
            <button id="tab-slip" onclick="switchTab('slip')" class="tab-btn text-slate-500 hover:text-slate-300 focus:outline-none transition">Slip of the Day</button>
        </div>

        <!-- ==================== BANKERS FEED ==================== -->
        <div id="feed-bankers" class="feed-container bg-slate-900/60 backdrop-blur-xl border border-slate-800 rounded-3xl overflow-hidden shadow-2xl">
            
            <!-- DESKTOP HEADER -->
            <div class="hidden md:grid md:grid-cols-6 gap-3 p-4 border-b border-slate-800 bg-slate-950/40 text-[9px] font-black tracking-[0.2em] uppercase text-slate-500">
                <div class="pl-2">Tournament / Match</div>
                <div>Prediction</div>
                <div class="text-center">Status</div>
                <div class="text-center">Trust Score</div>
                <div class="text-right pr-2">Tier</div>
            </div>

            <?php if (empty($bankers)): ?>
                <div class="p-12 text-center text-[11px] font-bold text-slate-600 uppercase tracking-widest">No active bankers logged for this timeframe.</div>
            <?php else: ?>
                <?php foreach ($bankers as $b): 
                    $is_vip_item = ($b['access_tier'] ?? 'FREE') === 'VIP';
                    $match_timestamp = strtotime($b['match_date']);
                    $is_future = $match_timestamp > $today_timestamp;
                    $trust = getTrust($b);
                    $conf = (int)($b['confidence'] ?? 75);
                    $conf_color = $conf >= 80 ? 'emerald' : ($conf >= 60 ? 'cyan' : 'amber');
                    $time_display = date('H:i', $match_timestamp);
                    $date_display = date('M d, Y', $match_timestamp);
                ?>
                    
                    <?php if ($is_future): ?>
                        <!-- Future match – locked -->
                        <div class="flex flex-col md:grid md:grid-cols-6 gap-3 p-4 md:p-5 border-b border-slate-800/50 relative overflow-hidden bg-slate-900/40 items-start md:items-center">
                            <div class="absolute inset-0 z-10 flex items-center justify-center pointer-events-none">
                                <div class="bg-slate-900/80 backdrop-blur-sm border border-slate-700/50 px-4 py-2 rounded-xl flex items-center space-x-2 shadow-2xl">
                                    <span class="text-cyan-400 text-sm animate-pulse">⏳</span>
                                    <span class="text-[10px] font-black text-cyan-400 uppercase tracking-[0.2em]">Unlocks <?php echo date("l, M d", $match_timestamp); ?></span>
                                </div>
                            </div>
                            <div class="opacity-40 blur-[2px] select-none pointer-events-none flex flex-col md:grid md:grid-cols-6 gap-3 w-full">
                                <div class="pl-0 md:pl-2 text-[10px] font-mono text-cyan-400"><?php echo $date_display; ?></div>
                                <div class="opacity-30 blur-[4px] select-none">████████████</div>
                                <div class="text-center opacity-30 blur-[4px] select-none">████████</div>
                                <div class="text-center opacity-30 blur-[4px] select-none">████████</div>
                                <div class="text-right pr-0 md:pr-2 opacity-50 blur-[2px] select-none flex justify-end">
                                    <span class="bg-slate-800 text-slate-400 border border-slate-600 px-2.5 py-1.5 rounded-xl text-[8px] font-black tracking-widest uppercase">LOCKED</span>
                                </div>
                            </div>
                        </div>

                    <?php else: ?>
                        <!-- ============================================================ -->
                        <!-- MOBILE CARD VIEW -->
                        <!-- ============================================================ -->
                        <div class="md:hidden bg-slate-800/20 rounded-xl p-4 mb-3 border border-slate-700/50">
                            <!-- Title & Tier -->
                            <div class="flex items-center justify-between mb-1">
                                <span class="text-[11px] font-black text-amber-500 uppercase tracking-widest">
                                    <?php echo htmlspecialchars($b['match_title'] ?? 'MATCH EVENT'); ?>
                                </span>
                                <?php if($is_vip_item): ?>
                                    <span class="text-[8px] font-black text-amber-500 uppercase bg-amber-500/10 border border-amber-500/30 px-2 py-0.5 rounded-full">VIP</span>
                                <?php else: ?>
                                    <span class="text-[8px] font-black text-emerald-400 uppercase bg-emerald-500/10 border border-emerald-500/30 px-2 py-0.5 rounded-full">FREE</span>
                                <?php endif; ?>
                            </div>

                            <!-- Date & Time -->
                            <div class="text-[9px] text-slate-400 mb-2">
                                <span><?php echo $date_display; ?></span>
                                <span class="mx-1">•</span>
                                <span><?php echo $time_display; ?></span>
                            </div>

                            <!-- Teams -->
                            <div class="text-white font-bold text-sm mb-3">
                                <?php echo htmlspecialchars($b['team_a']); ?> <span class="text-slate-500 text-[10px] mx-1">vs</span> <?php echo htmlspecialchars($b['team_b']); ?>
                            </div>

                            <!-- Prediction -->
                            <div class="mb-2">
                                <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Prediction</span>
                                <span class="text-sm font-bold text-cyan-400"><?php echo htmlspecialchars($b['game_option']); ?></span>
                            </div>

                            <!-- Status & Trust -->
                            <div class="flex items-center justify-between">
                                <div>
                                    <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Status</span>
                                    <?php echo outcomeBadge($b['outcome'] ?? 'PENDING'); ?>
                                </div>
                                <div class="text-right">
                                    <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Trust Score</span>
                                    <span class="text-sm font-black text-<?php echo $conf_color; ?>-400">★ <?php echo $trust; ?>/10</span>
                                </div>
                            </div>
                        </div>

                        <!-- ============================================================ -->
                        <!-- DESKTOP TABLE ROW -->
                        <!-- ============================================================ -->
                        <div class="hidden md:grid md:grid-cols-6 gap-3 p-4 border-b border-slate-800/50 hover:bg-slate-800/20 transition items-center">
                            <!-- Tournament / Match -->
                            <div class="pl-2">
                                <div class="text-[10px] font-black text-amber-500 uppercase tracking-widest"><?php echo htmlspecialchars($b['match_title'] ?? 'N/A'); ?></div>
                                <div class="text-[9px] font-mono text-slate-400"><?php echo $date_display; ?> • <?php echo $time_display; ?></div>
                                <div class="text-sm font-bold text-white">
                                    <?php echo htmlspecialchars($b['team_a']); ?> <span class="text-slate-500 text-[10px] mx-1">vs</span> <?php echo htmlspecialchars($b['team_b']); ?>
                                </div>
                            </div>
                            <!-- Prediction -->
                            <div class="font-bold text-cyan-400"><?php echo htmlspecialchars($b['game_option']); ?></div>
                            <!-- Status -->
                            <div class="text-center"><?php echo outcomeBadge($b['outcome'] ?? 'PENDING'); ?></div>
                            <!-- Trust Score -->
                            <div class="text-center">
                                <div class="flex items-center justify-center gap-2">
                                    <div class="w-20 h-1.5 bg-slate-800 rounded-full overflow-hidden">
                                        <div class="h-full rounded-full bg-<?php echo $conf_color; ?>-400" style="width: <?php echo $conf; ?>%;"></div>
                                    </div>
                                    <span class="text-[11px] font-black text-<?php echo $conf_color; ?>-400">★ <?php echo $trust; ?></span>
                                </div>
                            </div>
                            <!-- Tier -->
                            <div class="text-right pr-2">
                                <?php if($is_vip_item): ?>
                                    <span class="text-[9px] font-black text-amber-500 uppercase bg-amber-500/10 border border-amber-500/30 px-2.5 py-1 rounded-full">VIP</span>
                                <?php else: ?>
                                    <span class="text-[9px] font-black text-emerald-400 uppercase bg-emerald-500/10 border border-emerald-500/30 px-2.5 py-1 rounded-full">FREE</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>

                <?php endforeach; ?>
            <?php endif; ?>
        </div>

        <!-- ==================== SLIP OF THE DAY FEED ==================== -->
        <div id="feed-slip" class="feed-container bg-slate-900/60 backdrop-blur-xl border border-slate-800 rounded-3xl overflow-hidden shadow-2xl hidden relative">
            
            <!-- DESKTOP HEADER -->
            <div class="hidden md:grid md:grid-cols-6 gap-3 p-4 border-b border-slate-800 bg-slate-950/40 text-[9px] font-black tracking-[0.2em] uppercase text-slate-500">
                <div class="pl-2">Tournament / Match</div>
                <div>Prediction</div>
                <div class="text-center">Status</div>
                <div class="text-center">Trust Score</div>
                <div class="text-right pr-2">Tier</div>
            </div>

            <?php if (empty($slip_of_the_day)): ?>
                <div class="p-12 text-center text-[11px] font-bold text-slate-600 uppercase tracking-widest">No active system combo slips deployed.</div>
            <?php else: ?>
                <?php foreach ($slip_of_the_day as $s): 
                    $is_vip_item = ($s['access_tier'] ?? 'FREE') === 'VIP';
                    $match_timestamp = strtotime($s['match_date']);
                    $is_future = $match_timestamp > $today_timestamp;
                    $trust = getTrust($s);
                    $conf = (int)($s['confidence'] ?? 75);
                    $conf_color = $conf >= 80 ? 'emerald' : ($conf >= 60 ? 'cyan' : 'amber');
                    $time_display = date('H:i', $match_timestamp);
                    $date_display = date('M d, Y', $match_timestamp);
                    
                    if ($is_vip_item && !$is_vip):
                ?>
                    <!-- VIP Slip Locked for non-VIP -->
                    <div class="w-full h-56 flex items-center justify-center relative overflow-hidden backdrop-blur-md bg-slate-950/10 border-b border-slate-800/50">
                        <div class="absolute inset-0 bg-slate-950/60 backdrop-blur-[10px]"></div>
                        <div class="relative z-10 text-center space-y-4 max-w-md px-4">
                            <div class="w-14 h-14 bg-amber-500/10 border border-amber-500/30 rounded-2xl flex items-center justify-center mx-auto text-2xl animate-pulse">👑</div>
                            <h4 class="text-white text-xs font-black uppercase tracking-widest">Premium Slip Masked</h4>
                            <p class="text-[9px] text-slate-400 leading-relaxed">Admin accumulator combos and VIP combo tips are strictly protected under premium subscription matrices.</p>
                            <a href="login-register.php" class="inline-block bg-gradient-to-r from-amber-500 to-orange-500 text-slate-950 px-6 py-3 rounded-xl text-[9px] font-black tracking-widest uppercase hover:opacity-90 transition shadow-lg shadow-amber-500/20">Unlock Premium Tips</a>
                        </div>
                    </div>
                
                <?php elseif ($is_future): ?>
                    <!-- Future slip – locked -->
                    <div class="flex flex-col md:grid md:grid-cols-6 gap-3 p-4 md:p-5 border-b border-slate-800/50 relative overflow-hidden bg-slate-900/40 items-start md:items-center">
                        <div class="absolute inset-0 z-10 flex items-center justify-center pointer-events-none">
                            <div class="bg-amber-950/80 backdrop-blur-sm border border-amber-700/50 px-4 py-2 rounded-xl flex items-center space-x-2 shadow-2xl">
                                <span class="text-amber-500 text-sm animate-pulse">⏳</span>
                                <span class="text-[10px] font-black text-amber-500 uppercase tracking-[0.2em]">Unlocks <?php echo date("l, M d", $match_timestamp); ?></span>
                            </div>
                        </div>
                        <div class="opacity-40 blur-[2px] select-none pointer-events-none flex flex-col md:grid md:grid-cols-6 gap-3 w-full">
                            <div class="pl-0 md:pl-2 text-[10px] font-mono text-amber-400"><?php echo $date_display; ?></div>
                            <div class="opacity-30 blur-[4px] select-none">████████████</div>
                            <div class="text-center opacity-30 blur-[4px] select-none">████████</div>
                            <div class="text-center opacity-30 blur-[4px] select-none">████████</div>
                            <div class="text-right pr-0 md:pr-2 opacity-50 blur-[2px] select-none flex justify-end">
                                <span class="bg-slate-800 text-slate-400 border border-slate-600 px-2.5 py-1.5 rounded-xl text-[8px] font-black tracking-widest uppercase">LOCKED</span>
                            </div>
                        </div>
                    </div>

                <?php else: ?>
                    <!-- ============================================================ -->
                    <!-- MOBILE CARD VIEW - SLIP -->
                    <!-- ============================================================ -->
                    <div class="md:hidden bg-slate-800/20 rounded-xl p-4 mb-3 border border-amber-700/30">
                        <div class="flex items-center justify-between mb-1">
                            <span class="text-[11px] font-black text-amber-500 uppercase tracking-widest">
                                <?php echo htmlspecialchars($s['match_title'] ?? 'VIP SLIP'); ?>
                            </span>
                            <span class="text-[8px] font-black text-amber-500 uppercase bg-amber-500/10 border border-amber-500/30 px-2 py-0.5 rounded-full">VIP</span>
                        </div>

                        <div class="text-[9px] text-slate-400 mb-2">
                            <span><?php echo $date_display; ?></span>
                            <span class="mx-1">•</span>
                            <span><?php echo $time_display; ?></span>
                        </div>

                        <div class="text-white font-bold text-sm mb-3">
                            <?php echo htmlspecialchars($s['team_a']); ?> <span class="text-amber-500 text-[10px] mx-1">vs</span> <?php echo htmlspecialchars($s['team_b']); ?>
                        </div>

                        <div class="mb-2">
                            <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Prediction</span>
                            <span class="text-sm font-bold text-amber-400"><?php echo htmlspecialchars($s['game_option']); ?></span>
                        </div>

                        <div class="flex items-center justify-between">
                            <div>
                                <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Status</span>
                                <?php echo outcomeBadge($s['outcome'] ?? 'PENDING'); ?>
                            </div>
                            <div class="text-right">
                                <span class="text-[8px] text-slate-500 uppercase tracking-widest block">Trust Score</span>
                                <span class="text-sm font-black text-<?php echo $conf_color; ?>-400">★ <?php echo $trust; ?>/10</span>
                            </div>
                        </div>
                    </div>

                    <!-- ============================================================ -->
                    <!-- DESKTOP TABLE ROW - SLIP -->
                    <!-- ============================================================ -->
                    <div class="hidden md:grid md:grid-cols-6 gap-3 p-4 border-b border-amber-900/20 hover:bg-slate-800/20 transition items-center">
                        <div class="pl-2">
                            <div class="text-[10px] font-black text-amber-500 uppercase tracking-widest"><?php echo htmlspecialchars($s['match_title'] ?? 'N/A'); ?></div>
                            <div class="text-[9px] font-mono text-slate-400"><?php echo $date_display; ?> • <?php echo $time_display; ?></div>
                            <div class="text-sm font-bold text-white">
                                <?php echo htmlspecialchars($s['team_a']); ?> <span class="text-amber-500 text-[10px] mx-1">vs</span> <?php echo htmlspecialchars($s['team_b']); ?>
                            </div>
                        </div>
                        <div class="font-bold text-amber-400"><?php echo htmlspecialchars($s['game_option']); ?></div>
                        <div class="text-center"><?php echo outcomeBadge($s['outcome'] ?? 'PENDING'); ?></div>
                        <div class="text-center">
                            <div class="flex items-center justify-center gap-2">
                                <div class="w-20 h-1.5 bg-slate-800 rounded-full overflow-hidden">
                                    <div class="h-full rounded-full bg-<?php echo $conf_color; ?>-400" style="width: <?php echo $conf; ?>%;"></div>
                                </div>
                                <span class="text-[11px] font-black text-<?php echo $conf_color; ?>-400">★ <?php echo $trust; ?></span>
                            </div>
                        </div>
                        <div class="text-right pr-2">
                            <span class="text-[9px] font-black text-amber-500 uppercase bg-amber-500/10 border border-amber-500/30 px-2.5 py-1 rounded-full">VIP</span>
                        </div>
                    </div>
                <?php endif; ?>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <!-- Ad Slot -->
    <div class="w-full flex justify-center py-4">
        <?php display_ad_slot($pdo, 'in_content'); ?>
    </div>

</main>

<?php require_once __DIR__ . '/includes/footer.php'; ?>

<script>
    function switchTab(tabName) {
        const btnBankers = document.getElementById('tab-bankers');
        const btnSlip = document.getElementById('tab-slip');
        const feedBankers = document.getElementById('feed-bankers');
        const feedSlip = document.getElementById('feed-slip');

        if(tabName === 'bankers') {
            btnBankers.className = "tab-btn text-emerald-400 border-b-2 border-emerald-400 pb-3 -mb-3.5 focus:outline-none transition";
            btnSlip.className = "tab-btn text-slate-500 hover:text-slate-300 focus:outline-none transition";
            feedBankers.classList.remove('hidden');
            feedSlip.classList.add('hidden');
        } else {
            btnSlip.className = "tab-btn text-emerald-400 border-b-2 border-emerald-400 pb-3 -mb-3.5 focus:outline-none transition";
            btnBankers.className = "tab-btn text-slate-500 hover:text-slate-300 focus:outline-none transition";
            feedSlip.classList.remove('hidden');
            feedBankers.classList.add('hidden');
        }
    }

    // Neural Canvas Background Engine (unchanged)
    const canvas = document.getElementById("neuralCanvas");
    if(canvas) {
        const ctx = canvas.getContext("2d");
        let width, height;
        let particles = [];
        let mouse = { x: null, y: null, radius: 150 };
        window.addEventListener('mousemove', function(e) {
            mouse.x = e.x;
            mouse.y = e.y;
        });

        function initCanvas() {
            width = canvas.width = window.innerWidth;
            height = canvas.height = window.innerHeight;
            particles = [];
            let particleCount = (width * height) / 15000;
            for (let i = 0; i < particleCount; i++) {
                particles.push({
                    x: Math.random() * width,
                    y: Math.random() * height,
                    vx: (Math.random() - 0.5) * 1.5,
                    vy: (Math.random() - 0.5) * 1.5,
                    radius: Math.random() * 1.5 + 0.5
                });
            }
        }

        function animateCanvas() {
            ctx.clearRect(0, 0, width, height);
            for (let i = 0; i < particles.length; i++) {
                let p = particles[i];
                p.x += p.vx;
                p.y += p.vy;
                if (p.x < 0 || p.x > width) p.vx *= -1;
                if (p.y < 0 || p.y > height) p.vy *= -1;
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
                ctx.fillStyle = "rgba(16, 185, 129, 0.3)";
                ctx.fill();
                for (let j = i + 1; j < particles.length; j++) {
                    let p2 = particles[j];
                    let dist = Math.sqrt(Math.pow(p.x - p2.x, 2) + Math.pow(p.y - p2.y, 2));
                    if (dist < 100) {
                        ctx.beginPath();
                        ctx.moveTo(p.x, p.y);
                        ctx.lineTo(p2.x, p2.y);
                        ctx.strokeStyle = `rgba(16, 185, 129, ${1 - dist/100})`;
                        ctx.lineWidth = 0.5;
                        ctx.stroke();
                    }
                }
                if (mouse.x && mouse.y) {
                    let mouseDist = Math.sqrt(Math.pow(p.x - mouse.x, 2) + Math.pow(p.y - mouse.y, 2));
                    if (mouseDist < mouse.radius) {
                        ctx.beginPath();
                        ctx.moveTo(p.x, p.y);
                        ctx.lineTo(mouse.x, mouse.y);
                        ctx.strokeStyle = `rgba(16, 185, 129, ${0.8 - mouseDist/mouse.radius})`;
                        ctx.lineWidth = 1;
                        ctx.stroke();
                    }
                }
            }
            requestAnimationFrame(animateCanvas);
        }
        window.addEventListener("resize", initCanvas);
        initCanvas();
        animateCanvas();
    }
</script>