<?php
ob_start(); // আউটপুট বাফারিং শুরু (হেডার এরর ফিক্স করার জন্য)
session_start();
require '../config/db.php';

// লগইন চেক
if (!isset($_SESSION['admin_logged_in'])) { 
    header("Location: index.php"); 
    exit; 
}

// Settings থেকে ডাটা ফেচ করা
$settings = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch();
$site_name = $settings['site_name'] ?? 'LudoPro';
$currency = $settings['currency'] ?? '৳';
$logo = $settings['logo'] ?? 'logo.png';

$msg = ""; 
$msgType = "";

// ১. টুর্নামেন্ট অ্যাড লজিক
if (isset($_POST['add_tournament'])) {
    $sql = "INSERT INTO tournaments (title, entry_fee, prize_pool, start_time, max_players, status) VALUES (?, ?, ?, ?, ?, 'open')";
    $stmt = $pdo->prepare($sql);
    if ($stmt->execute([$_POST['title'], $_POST['fee'], $_POST['prize'], $_POST['time'], $_POST['players']])) {
        $msg = "Tournament Created Successfully!"; 
        $msgType = "success";
    }
}

// ২. টুর্নামেন্ট এডিট লজিক
if (isset($_POST['edit_tournament'])) {
    $sql = "UPDATE tournaments SET title=?, entry_fee=?, prize_pool=?, start_time=?, max_players=?, status=? WHERE id=?";
    $stmt = $pdo->prepare($sql);
    if ($stmt->execute([$_POST['title'], $_POST['fee'], $_POST['prize'], $_POST['time'], $_POST['players'], $_POST['status'], $_POST['tour_id']])) {
        $msg = "Tournament Updated Successfully!"; 
        $msgType = "success";
    }
}

// ৩. টুর্নামেন্ট ডিলেট লজিক
if (isset($_GET['del_tour'])) {
    // চেক করুন টুর্নামেন্টে কোন ইউজার জয়েন করেছে কিনা
    $check = $pdo->prepare("SELECT COUNT(*) FROM tournament_participants WHERE tournament_id = ?");
    $check->execute([$_GET['del_tour']]);
    $participants = $check->fetchColumn();
    
    if($participants > 0) {
        $msg = "Cannot delete tournament with joined participants!"; 
        $msgType = "error";
    } else {
        $pdo->prepare("DELETE FROM tournaments WHERE id=?")->execute([$_GET['del_tour']]);
        header("Location: tournaments.php?deleted=1");
        exit;
    }
}
if(isset($_GET['deleted'])) { 
    $msg = "Tournament Deleted!"; 
    $msgType = "success"; 
}

// ৪. টুর্নামেন্ট স্ট্যাটাস টগল (open/closed)
if (isset($_GET['toggle_status'])) {
    $tour_id = $_GET['toggle_status'];
    $current = $pdo->query("SELECT status FROM tournaments WHERE id=$tour_id")->fetchColumn();
    $new_status = ($current == 'open') ? 'closed' : 'open';
    $pdo->prepare("UPDATE tournaments SET status=? WHERE id=?")->execute([$new_status, $tour_id]);
    header("Location: tournaments.php?toggled=1");
    exit;
}
if(isset($_GET['toggled'])) { 
    $msg = "Status Updated!"; 
    $msgType = "success"; 
}

// ৫. ডাটা ফেচিং - প্যার্টিসিপেন্ট কাউন্ট সহ
$tournaments = $pdo->query("SELECT t.*, 
    (SELECT COUNT(*) FROM tournament_participants WHERE tournament_id = t.id) as joined_players 
    FROM tournaments t 
    ORDER BY 
    CASE 
        WHEN t.status = 'open' THEN 1
        WHEN t.status = 'closed' THEN 2
        ELSE 3
    END, t.id DESC")->fetchAll();

// ৬. টুর্নামেন্ট কাউন্ট
$open_count = $pdo->query("SELECT COUNT(*) FROM tournaments WHERE status='open'")->fetchColumn();
$closed_count = $pdo->query("SELECT COUNT(*) FROM tournaments WHERE status='closed'")->fetchColumn();
$total_count = $pdo->query("SELECT COUNT(*) FROM tournaments")->fetchColumn();
$total_prize = $pdo->query("SELECT SUM(prize_pool) FROM tournaments")->fetchColumn();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tournament Management | <?= htmlspecialchars($site_name) ?> Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    
    <style>
        * {
            font-family: 'Inter', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #0a0f1e 0%, #0b1120 100%);
            color: #e2e8f0;
        }
        
        /* গ্লাসমর্ফিজম ইফেক্ট */
        .glass {
            background: rgba(18, 25, 40, 0.75);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.03);
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
        }
        
        .glass-card {
            background: rgba(15, 23, 42, 0.6);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(56, 189, 248, 0.1);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .glass-card:hover {
            border-color: rgba(56, 189, 248, 0.3);
            transform: translateY(-2px);
            box-shadow: 0 20px 40px -15px rgba(56, 189, 248, 0.2);
        }
        
        /* নেভিগেশন হোভার ইফেক্ট */
        .nav-item {
            position: relative;
            overflow: hidden;
        }
        
        .nav-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 3px;
            background: linear-gradient(180deg, #3b82f6, #8b5cf6);
            transform: scaleY(0);
            transition: transform 0.2s ease;
        }
        
        .nav-item:hover::before,
        .nav-item.active::before {
            transform: scaleY(1);
        }
        
        .nav-item.active {
            background: linear-gradient(90deg, rgba(59, 130, 246, 0.1), transparent);
        }
        
        /* স্ট্যাটাস ব্যাজ */
        .status-badge {
            padding: 4px 10px;
            border-radius: 100px;
            font-size: 11px;
            font-weight: 600;
            letter-spacing: 0.3px;
            text-transform: uppercase;
            display: inline-flex;
            align-items: center;
            gap: 4px;
        }
        
        .status-badge.open {
            background: rgba(16, 185, 129, 0.1);
            color: #34d399;
            border: 1px solid rgba(16, 185, 129, 0.2);
        }
        
        .status-badge.closed {
            background: rgba(239, 68, 68, 0.1);
            color: #f87171;
            border: 1px solid rgba(239, 68, 68, 0.2);
        }
        
        .status-badge.full {
            background: rgba(245, 158, 11, 0.1);
            color: #fbbf24;
            border: 1px solid rgba(245, 158, 11, 0.2);
        }
        
        /* টুর্নামেন্ট কার্ড */
        .tournament-card {
            background: linear-gradient(145deg, rgba(30, 41, 59, 0.5), rgba(15, 23, 42, 0.8));
            border-bottom: 2px solid transparent;
            border-image: linear-gradient(90deg, #3b82f6, #8b5cf6);
            border-image-slice: 1;
            transition: all 0.3s ease;
        }
        
        .tournament-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px -15px rgba(59, 130, 246, 0.3);
        }
        
        /* গ্রেডিয়েন্ট টেক্সট */
        .gradient-text {
            background: linear-gradient(135deg, #60a5fa, #c084fc);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* কাস্টম স্ক্রলবার */
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        
        ::-webkit-scrollbar-track {
            background: #0f172a;
        }
        
        ::-webkit-scrollbar-thumb {
            background: #334155;
            border-radius: 100px;
        }
        
        ::-webkit-scrollbar-thumb:hover {
            background: #475569;
        }
        
        /* ফিল্টার বাটন */
        .filter-btn {
            padding: 8px 16px;
            border-radius: 12px;
            font-size: 13px;
            font-weight: 500;
            transition: all 0.2s ease;
            background: rgba(51, 65, 85, 0.3);
            color: #94a3b8;
        }
        
        .filter-btn:hover {
            background: rgba(59, 130, 246, 0.2);
            color: #60a5fa;
        }
        
        .filter-btn.active {
            background: #3b82f6;
            color: white;
        }
        
        /* এনিমেশন */
        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }
        
        .live-indicator {
            animation: pulse 1.5s ease-in-out infinite;
        }
        
        /* প্রোগ্রেস বার */
        .progress-bar {
            height: 4px;
            background: rgba(51, 65, 85, 0.5);
            border-radius: 100px;
            overflow: hidden;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #3b82f6, #8b5cf6);
            border-radius: 100px;
            transition: width 0.3s ease;
        }
    </style>
</head>
<body class="flex min-h-screen overflow-hidden">
    
    <!-- সাইডবার - settings থেকে ডাইনামিক -->
    <aside class="w-72 bg-slate-900/50 backdrop-blur-xl border-r border-slate-800/50 hidden lg:flex flex-col fixed h-screen overflow-y-auto">
        <!-- লোগো সেকশন -->
        <div class="p-6 border-b border-slate-800/50">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-gradient-to-br from-blue-500 to-purple-600 rounded-xl flex items-center justify-center shadow-lg shadow-blue-500/20">
                    <?php if($logo && file_exists("../assets/img/".$logo)): ?>
                        <img src="../assets/img/<?= $logo ?>" class="w-8 h-8 object-contain" alt="Logo">
                    <?php else: ?>
                        <i class="fas fa-crown text-white text-lg"></i>
                    <?php endif; ?>
                </div>
                <div>
                    <h1 class="text-2xl font-black tracking-tight">
                        <span class="text-white"><?= htmlspecialchars(substr($site_name, 0, 4)) ?></span>
                        <span class="gradient-text"><?= htmlspecialchars(substr($site_name, 4)) ?></span>
                    </h1>
                    <p class="text-[10px] text-slate-500 tracking-wider">ADMIN PANEL v2.0</p>
                </div>
            </div>
        </div>
        
        <!-- নেভিগেশন মেনু -->
        <nav class="flex-1 p-4 space-y-1">
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2">Main Menu</div>
            
            <a href="dashboard.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-home w-5"></i>
                <span class="text-sm font-medium">Dashboard</span>
            </a>
            
            <a href="tournaments.php" class="nav-item active flex items-center gap-3 px-4 py-3 text-white rounded-xl transition-all duration-200">
                <i class="fas fa-trophy w-5 text-yellow-500"></i>
                <span class="text-sm font-medium">Tournaments</span>
                <span class="ml-auto bg-blue-500/20 text-blue-400 text-[10px] px-2 py-0.5 rounded-full"><?= $open_count ?> Open</span>
            </a>
            
            <a href="users.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-users w-5"></i>
                <span class="text-sm font-medium">Users</span>
            </a>
            
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2 mt-4">Transactions</div>
            
            <a href="deposits.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-arrow-down w-5 text-emerald-400"></i>
                <span class="text-sm font-medium">Deposits</span>
            </a>
            
            <a href="withdraws.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-arrow-up w-5 text-rose-400"></i>
                <span class="text-sm font-medium">Withdrawals</span>
            </a>
            
            <div class="text-[10px] font-semibold text-slate-500 uppercase tracking-wider px-4 py-2 mt-4">Settings</div>
            
            <a href="settings.php" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white rounded-xl transition-all duration-200">
                <i class="fas fa-cog w-5"></i>
                <span class="text-sm font-medium">Settings</span>
            </a>
        </nav>
        
        <!-- ইউজার প্রোফাইল -->
        <div class="p-4 border-t border-slate-800/50 mt-auto">
            <div class="flex items-center gap-3 p-3 bg-slate-800/30 rounded-2xl">
                <div class="w-10 h-10 bg-gradient-to-br from-blue-600 to-purple-600 rounded-xl flex items-center justify-center">
                    <span class="text-white font-bold text-lg">A</span>
                </div>
                <div class="flex-1">
                    <p class="text-sm font-semibold text-white">Admin</p>
                    <p class="text-[10px] text-slate-500">Super Admin</p>
                </div>
                <a href="index.php?logout=1" class="w-8 h-8 bg-rose-500/10 text-rose-400 rounded-lg flex items-center justify-center hover:bg-rose-500/20 transition-colors" title="Logout">
                    <i class="fas fa-sign-out-alt text-sm"></i>
                </a>
            </div>
        </div>
    </aside>

    <!-- মেইন কন্টেন্ট এরিয়া -->
    <main class="flex-1 lg:ml-72 p-6 lg:p-8 overflow-y-auto h-screen">
        
        <!-- টপ বার -->
        <header class="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8">
            <div>
                <h1 class="text-3xl font-black tracking-tight">
                    <span class="text-white">Tournament</span>
                    <span class="gradient-text">Management</span>
                </h1>
                <p class="text-slate-500 text-sm mt-1">Create, manage and monitor all tournaments from one place.</p>
            </div>
            
            <div class="flex items-center gap-3">
                <div class="glass-card px-4 py-2.5 rounded-xl flex items-center gap-3">
                    <i class="far fa-calendar-alt text-blue-400"></i>
                    <span class="text-sm font-medium text-white"><?= date('l, d M Y') ?></span>
                </div>
                <button onclick="document.getElementById('addModal').classList.remove('hidden')" class="bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-500 hover:to-purple-500 text-white px-5 py-2.5 rounded-xl font-bold transition flex items-center gap-2 shadow-lg shadow-blue-600/20">
                    <i class="fas fa-plus"></i>
                    <span class="hidden sm:inline">New Tournament</span>
                </button>
            </div>
        </header>

        <!-- এলার্ট মেসেজ -->
        <?php if($msg): ?>
        <script>
            Swal.fire({
                icon: '<?= $msgType ?>',
                title: '<?= $msg ?>',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000,
                background: '#1e293b',
                color: '#fff',
                iconColor: '<?= $msgType == "success" ? "#34d399" : ($msgType == "error" ? "#f87171" : "#60a5fa") ?>'
            });
        </script>
        <?php endif; ?>

        <!-- স্ট্যাটিস্টিক্যাল কার্ডস -->
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Total Tournaments</p>
                        <h2 class="text-3xl font-black text-white mt-2"><?= $total_count ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center text-blue-400 text-xl">
                        <i class="fas fa-trophy"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Open Tournaments</p>
                        <h2 class="text-3xl font-black text-white mt-2 text-emerald-400"><?= $open_count ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-emerald-500/10 rounded-xl flex items-center justify-center text-emerald-400 text-xl">
                        <i class="fas fa-door-open"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Closed Tournaments</p>
                        <h2 class="text-3xl font-black text-white mt-2 text-rose-400"><?= $closed_count ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-rose-500/10 rounded-xl flex items-center justify-center text-rose-400 text-xl">
                        <i class="fas fa-door-closed"></i>
                    </div>
                </div>
            </div>
            
            <div class="glass-card p-6 rounded-2xl">
                <div class="flex items-start justify-between">
                    <div>
                        <p class="text-slate-500 text-xs font-medium uppercase tracking-wider">Total Prize Pool</p>
                        <h2 class="text-3xl font-black text-white mt-2"><?= htmlspecialchars($currency) ?><?= number_format($total_prize ?? 0, 0) ?></h2>
                    </div>
                    <div class="w-12 h-12 bg-purple-500/10 rounded-xl flex items-center justify-center text-purple-400 text-xl">
                        <i class="fas fa-coins"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- ফিল্টার এবং সার্চ বার -->
        <div class="flex flex-col sm:flex-row gap-4 justify-between items-center mb-6">
            <div class="flex gap-2">
                <button onclick="filterTournaments('all')" class="filter-btn active" id="filter-all">All</button>
                <button onclick="filterTournaments('open')" class="filter-btn" id="filter-open">Open</button>
                <button onclick="filterTournaments('closed')" class="filter-btn" id="filter-closed">Closed</button>
            </div>
            <div class="relative w-full sm:w-64">
                <i class="fas fa-search absolute left-3 top-1/2 -translate-y-1/2 text-slate-500 text-sm"></i>
                <input type="text" id="searchInput" placeholder="Search tournaments..." class="w-full bg-slate-800/50 border border-slate-700 rounded-xl pl-10 pr-4 py-2.5 text-sm text-white placeholder-slate-500 focus:border-blue-500 outline-none">
            </div>
        </div>

        <!-- টুর্নামেন্ট গ্রিড -->
        <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6" id="tournamentGrid">
            <?php foreach($tournaments as $t): 
                $start_time = strtotime($t['start_time']);
                $now = time();
                $is_live = ($start_time <= $now && $t['status'] == 'open');
                $is_upcoming = ($start_time > $now && $t['status'] == 'open');
                $joined = $t['joined_players'] ?? 0;
                $percentage = $t['max_players'] > 0 ? ($joined / $t['max_players']) * 100 : 0;
                $is_full = ($joined >= $t['max_players'] && $t['status'] == 'open');
            ?>
            <div class="tournament-card glass-card rounded-2xl overflow-hidden group" data-status="<?= $t['status'] ?>">
                <!-- হেডার অংশ -->
                <div class="p-5 border-b border-slate-700/50 bg-slate-800/30">
                    <div class="flex justify-between items-start mb-3">
                        <div class="flex items-center gap-2">
                            <?php if($is_live): ?>
                            <span class="live-indicator flex h-2 w-2">
                                <span class="animate-ping absolute inline-flex h-2 w-2 rounded-full bg-rose-400 opacity-75"></span>
                                <span class="relative inline-flex rounded-full h-2 w-2 bg-rose-500"></span>
                            </span>
                            <span class="text-[10px] font-bold text-rose-500 uppercase tracking-wider">LIVE NOW</span>
                            <?php elseif($is_upcoming): ?>
                            <i class="fas fa-clock text-yellow-500 text-xs"></i>
                            <span class="text-[10px] font-bold text-yellow-500 uppercase tracking-wider">UPCOMING</span>
                            <?php endif; ?>
                            
                            <?php if($is_full): ?>
                            <span class="text-[10px] font-bold text-orange-500 uppercase tracking-wider ml-2">FULL</span>
                            <?php endif; ?>
                        </div>
                        <span class="status-badge <?= $t['status'] ?>">
                            <i class="fas fa-circle text-[6px]"></i>
                            <?= $t['status'] ?>
                        </span>
                    </div>
                    
                    <h3 class="text-xl font-black text-white mb-2 group-hover:text-blue-400 transition-colors">
                        <?= htmlspecialchars($t['title']) ?>
                    </h3>
                    
                    <div class="flex items-center gap-2 text-xs text-slate-500">
                        <i class="far fa-calendar-alt"></i>
                        <span><?= date('d M Y', $start_time) ?></span>
                        <i class="far fa-clock ml-2"></i>
                        <span><?= date('h:i A', $start_time) ?></span>
                    </div>
                </div>
                
                <!-- ডিটেইলস অংশ -->
                <div class="p-5">
                    <div class="grid grid-cols-3 gap-3 mb-4">
                        <div class="text-center p-2 bg-slate-800/50 rounded-xl">
                            <p class="text-[10px] text-slate-500 uppercase mb-1">Entry</p>
                            <p class="font-bold text-white text-sm"><?= htmlspecialchars($currency) ?><?= number_format($t['entry_fee']) ?></p>
                        </div>
                        <div class="text-center p-2 bg-slate-800/50 rounded-xl">
                            <p class="text-[10px] text-slate-500 uppercase mb-1">Prize</p>
                            <p class="font-bold text-emerald-400 text-sm"><?= htmlspecialchars($currency) ?><?= number_format($t['prize_pool']) ?></p>
                        </div>
                        <div class="text-center p-2 bg-slate-800/50 rounded-xl">
                            <p class="text-[10px] text-slate-500 uppercase mb-1">Players</p>
                            <p class="font-bold text-blue-400 text-sm"><?= $joined ?>/<?= $t['max_players'] ?></p>
                        </div>
                    </div>
                    
                    <!-- প্রোগ্রেস বার -->
                    <?php if($t['status'] == 'open'): ?>
                    <div class="progress-bar mb-4">
                        <div class="progress-fill" style="width: <?= $percentage ?>%"></div>
                    </div>
                    <?php endif; ?>
                    
                    <!-- অ্যাকশন বাটন -->
                    <div class="flex gap-2">
                        <button onclick="openEditModal(<?= htmlspecialchars(json_encode($t)) ?>)" class="flex-1 py-2.5 bg-blue-500/10 hover:bg-blue-500 text-blue-400 hover:text-white rounded-xl font-bold text-xs transition flex items-center justify-center gap-1">
                            <i class="fas fa-edit"></i>
                            Edit
                        </button>
                        
                        <a href="?toggle_status=<?= $t['id'] ?>" class="flex-1 py-2.5 <?= $t['status'] == 'open' ? 'bg-rose-500/10 hover:bg-rose-500 text-rose-400 hover:text-white' : 'bg-emerald-500/10 hover:bg-emerald-500 text-emerald-400 hover:text-white' ?> rounded-xl font-bold text-xs transition flex items-center justify-center gap-1">
                            <i class="fas <?= $t['status'] == 'open' ? 'fa-ban' : 'fa-check' ?>"></i>
                            <?= $t['status'] == 'open' ? 'Close' : 'Open' ?>
                        </a>
                        
                        <?php if($joined == 0): ?>
                        <a href="?del_tour=<?= $t['id'] ?>" onclick="return confirm('Are you sure you want to delete this tournament? This action cannot be undone.')" class="w-10 h-10 bg-rose-500/10 hover:bg-rose-500 text-rose-400 hover:text-white rounded-xl transition flex items-center justify-center" title="Delete Tournament">
                            <i class="fas fa-trash-alt"></i>
                        </a>
                        <?php else: ?>
                        <button class="w-10 h-10 bg-slate-600/10 text-slate-500 rounded-xl cursor-not-allowed" title="Cannot delete - players joined" disabled>
                            <i class="fas fa-trash-alt"></i>
                        </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
            
            <?php if(empty($tournaments)): ?>
            <div class="col-span-full text-center py-20">
                <div class="text-6xl mb-4">🏆</div>
                <h3 class="text-xl font-bold text-white mb-2">No Tournaments Found</h3>
                <p class="text-slate-500 mb-6">Get started by creating your first tournament</p>
                <button onclick="document.getElementById('addModal').classList.remove('hidden')" class="bg-blue-600 hover:bg-blue-500 text-white px-6 py-3 rounded-xl font-bold transition inline-flex items-center gap-2">
                    <i class="fas fa-plus"></i>
                    Create Tournament
                </button>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- ফুটার -->
        <div class="mt-12 text-center text-xs text-slate-600 border-t border-slate-800/50 pt-6">
            <p>© 2024 <?= htmlspecialchars($site_name) ?> Admin Panel. All rights reserved. | Tournament Management System v2.0</p>
        </div>
    </main>

    <!-- অ্যাড টুর্নামেন্ট মোডাল -->
    <div id="addModal" class="hidden fixed inset-0 bg-black/80 backdrop-blur-sm flex items-center justify-center z-50 p-4">
        <div class="glass-card rounded-2xl w-full max-w-md border border-slate-700">
            <div class="p-6 border-b border-slate-700 flex justify-between items-center">
                <h2 class="text-xl font-bold text-white flex items-center gap-2">
                    <i class="fas fa-plus-circle text-blue-500"></i>
                    Create New Tournament
                </h2>
                <button onclick="document.getElementById('addModal').classList.add('hidden')" class="w-8 h-8 bg-slate-700 rounded-lg hover:bg-slate-600 transition flex items-center justify-center">
                    <i class="fas fa-times text-slate-400"></i>
                </button>
            </div>
            
            <form method="POST" class="p-6 space-y-4">
                <div>
                    <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Tournament Title</label>
                    <input type="text" name="title" placeholder="e.g. Weekend Championship" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Entry Fee (<?= htmlspecialchars($currency) ?>)</label>
                        <input type="number" name="fee" placeholder="0" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                    </div>
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Prize Pool (<?= htmlspecialchars($currency) ?>)</label>
                        <input type="number" name="prize" placeholder="0" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                    </div>
                </div>
                
                <div>
                    <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Start Time</label>
                    <input type="datetime-local" name="time" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-slate-300" required>
                </div>
                
                <div>
                    <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Max Players</label>
                    <select name="players" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white">
                        <option value="2">2 Players (Head to Head)</option>
                        <option value="4">4 Players (Classic)</option>
                        <option value="6">6 Players (Battle)</option>
                        <option value="8">8 Players (Tournament)</option>
                    </select>
                </div>
                
                <div class="flex gap-3 pt-4">
                    <button type="button" onclick="document.getElementById('addModal').classList.add('hidden')" class="flex-1 py-3 bg-slate-700 hover:bg-slate-600 rounded-xl font-bold text-white transition">
                        Cancel
                    </button>
                    <button name="add_tournament" class="flex-1 py-3 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-500 hover:to-purple-500 rounded-xl font-bold text-white shadow-lg shadow-blue-600/20 transition">
                        Create Tournament
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- এডিট টুর্নামেন্ট মোডাল -->
    <div id="editModal" class="hidden fixed inset-0 bg-black/80 backdrop-blur-sm flex items-center justify-center z-50 p-4">
        <div class="glass-card rounded-2xl w-full max-w-md border border-slate-700">
            <div class="p-6 border-b border-slate-700 flex justify-between items-center">
                <h2 class="text-xl font-bold text-white flex items-center gap-2">
                    <i class="fas fa-edit text-blue-500"></i>
                    Edit Tournament
                </h2>
                <button onclick="document.getElementById('editModal').classList.add('hidden')" class="w-8 h-8 bg-slate-700 rounded-lg hover:bg-slate-600 transition flex items-center justify-center">
                    <i class="fas fa-times text-slate-400"></i>
                </button>
            </div>
            
            <form method="POST" class="p-6 space-y-4">
                <input type="hidden" name="tour_id" id="edit_tour_id">
                
                <div>
                    <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Tournament Title</label>
                    <input type="text" name="title" id="edit_title" placeholder="Tournament Title" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Entry Fee (<?= htmlspecialchars($currency) ?>)</label>
                        <input type="number" name="fee" id="edit_fee" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                    </div>
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Prize Pool (<?= htmlspecialchars($currency) ?>)</label>
                        <input type="number" name="prize" id="edit_prize" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white" required>
                    </div>
                </div>
                
                <div>
                    <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Start Time</label>
                    <input type="datetime-local" name="time" id="edit_time" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-slate-300" required>
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Max Players</label>
                        <select name="players" id="edit_players" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white">
                            <option value="2">2 Players</option>
                            <option value="4">4 Players</option>
                            <option value="6">6 Players</option>
                            <option value="8">8 Players</option>
                        </select>
                    </div>
                    
                    <div>
                        <label class="text-xs text-slate-500 font-medium uppercase tracking-wider mb-2 block">Status</label>
                        <select name="status" id="edit_status" class="w-full bg-slate-900/50 p-3 rounded-xl border border-slate-700 outline-none focus:border-blue-500 text-white">
                            <option value="open">Open</option>
                            <option value="closed">Closed</option>
                        </select>
                    </div>
                </div>
                
                <div class="flex gap-3 pt-4">
                    <button type="button" onclick="document.getElementById('editModal').classList.add('hidden')" class="flex-1 py-3 bg-slate-700 hover:bg-slate-600 rounded-xl font-bold text-white transition">
                        Cancel
                    </button>
                    <button name="edit_tournament" class="flex-1 py-3 bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-500 hover:to-purple-500 rounded-xl font-bold text-white shadow-lg shadow-blue-600/20 transition">
                        Update Tournament
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- মোবাইল মেনু টগল -->
    <button id="mobileMenuToggle" class="lg:hidden fixed bottom-6 right-6 w-14 h-14 bg-gradient-to-br from-blue-600 to-purple-600 rounded-2xl shadow-xl flex items-center justify-center text-white text-xl z-50">
        <i class="fas fa-bars"></i>
    </button>

    <script>
        // মোবাইল মেনু টগল
        const mobileToggle = document.getElementById('mobileMenuToggle');
        const sidebar = document.querySelector('aside');
        
        if(mobileToggle && sidebar) {
            mobileToggle.addEventListener('click', function() {
                sidebar.classList.toggle('hidden');
                sidebar.classList.toggle('flex');
                sidebar.classList.toggle('fixed');
                sidebar.classList.toggle('inset-0');
                sidebar.classList.toggle('z-40');
                sidebar.classList.toggle('w-full');
                sidebar.classList.toggle('lg:w-72');
                sidebar.classList.toggle('lg:flex');
            });
        }
        
        // এডিট মোডাল ওপেন ফাংশন
        function openEditModal(tournament) {
            document.getElementById('edit_tour_id').value = tournament.id;
            document.getElementById('edit_title').value = tournament.title;
            document.getElementById('edit_fee').value = tournament.entry_fee;
            document.getElementById('edit_prize').value = tournament.prize_pool;
            
            // ডেটটাইম ফরম্যাট ঠিক করা
            let date = new Date(tournament.start_time);
            let year = date.getFullYear();
            let month = String(date.getMonth() + 1).padStart(2, '0');
            let day = String(date.getDate()).padStart(2, '0');
            let hours = String(date.getHours()).padStart(2, '0');
            let minutes = String(date.getMinutes()).padStart(2, '0');
            document.getElementById('edit_time').value = `${year}-${month}-${day}T${hours}:${minutes}`;
            
            document.getElementById('edit_players').value = tournament.max_players;
            document.getElementById('edit_status').value = tournament.status;
            
            document.getElementById('editModal').classList.remove('hidden');
        }
        
        // ফিল্টার ফাংশন
        function filterTournaments(status) {
            const cards = document.querySelectorAll('.tournament-card');
            const filters = ['all', 'open', 'closed'];
            
            // ফিল্টার বাটন আপডেট
            filters.forEach(f => {
                document.getElementById(`filter-${f}`).classList.remove('active');
            });
            document.getElementById(`filter-${status}`).classList.add('active');
            
            // কার্ড ফিল্টার
            cards.forEach(card => {
                if(status === 'all') {
                    card.style.display = 'block';
                } else {
                    if(card.dataset.status === status) {
                        card.style.display = 'block';
                    } else {
                        card.style.display = 'none';
                    }
                }
            });
        }
        
        // সার্চ ফাংশন
        document.getElementById('searchInput').addEventListener('keyup', function() {
            const searchTerm = this.value.toLowerCase();
            const cards = document.querySelectorAll('.tournament-card');
            
            cards.forEach(card => {
                const title = card.querySelector('h3').textContent.toLowerCase();
                if(title.includes(searchTerm)) {
                    card.style.display = 'block';
                } else {
                    card.style.display = 'none';
                }
            });
        });
        
        // মোডাল বন্ধ করতে Escape কী প্রেস
        document.addEventListener('keydown', function(e) {
            if(e.key === 'Escape') {
                document.getElementById('addModal').classList.add('hidden');
                document.getElementById('editModal').classList.add('hidden');
            }
        });
        
        // মোডালের বাইরে ক্লিক করলে বন্ধ
        window.onclick = function(event) {
            if(event.target.classList.contains('fixed')) {
                document.getElementById('addModal').classList.add('hidden');
                document.getElementById('editModal').classList.add('hidden');
            }
        }
    </script>
</body>
</html>