<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/api.php';

// Fetch user balance
$user_balance = 15000.00;
$user_exposure = $_SESSION['exposure'] ?? 0.00;
$user_display_name = 'demo123';

if (isLoggedIn() && isset($_SESSION['user_id']) && $db_connected && $conn) {
    $uid = (int)$_SESSION['user_id'];
    $u_res = $conn->query("SELECT * FROM users WHERE id=$uid");
    if ($u_res && $u_row = $u_res->fetch_assoc()) {
        $user_balance = (float)(!empty($u_row['wallet_balance']) ? $u_row['wallet_balance'] : ($u_row['balance'] ?? 15000));
        $user_display_name = $u_row['username'];
    }
} elseif (isset($_SESSION['demo_balance'])) {
    $user_balance = (float)$_SESSION['demo_balance'];
}

$live_matches = get_live_fixtures();
$upcoming_matches = get_upcoming_fixtures();
$recent_matches = get_recent_fixtures(6);

// CricxBet99 matches for exchange table
$exchange_matches = [
    [
        'id' => 1001,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Afghanistan v Bangladesh',
        'datetime' => 'Oct 09 2026 11:30 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => false,
        'm1_back' => '130',
        'm1_lay' => '150',
        'mx_back' => '11.5',
        'mx_lay' => '12.5',
        'm2_back' => '1.09',
        'm2_lay' => '1.10'
    ],
    [
        'id' => 1002,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'South Africa v Australia',
        'datetime' => 'Oct 09 2026 13:00 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => false,
        'm1_back' => '50',
        'm1_lay' => '55',
        'mx_back' => '7',
        'mx_lay' => '7.2',
        'm2_back' => '1.18',
        'm2_lay' => '1.19'
    ],
    [
        'id' => 1003,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Bengal v Delhi',
        'datetime' => 'Oct 11 2026 09:00 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => false,
        'm1_back' => '-',
        'm1_lay' => '-',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '-',
        'm2_lay' => '-'
    ],
    [
        'id' => 1004,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Punjab v Assam',
        'datetime' => 'Oct 11 2026 09:30 (IST)',
        'live' => true,
        'tv' => false,
        'bm' => true,
        'fancy' => true,
        'toss' => false,
        'm1_back' => '-',
        'm1_lay' => '-',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '-',
        'm2_lay' => '-'
    ],
    [
        'id' => 1005,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Melbourne Stars SRL vs Sydney Sixers SRL',
        'datetime' => 'Oct 12 2026 23:30 (IST)',
        'live' => false,
        'tv' => false,
        'bm' => false,
        'fancy' => false,
        'toss' => false,
        'm1_back' => '-',
        'm1_lay' => '-',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '-',
        'm2_lay' => '-'
    ],
    [
        'id' => 1006,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Western Australia v Queensland',
        'datetime' => 'Oct 13 2026 11:30 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '1.68',
        'm1_lay' => '1.84',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '2.18',
        'm2_lay' => '2.48'
    ],
    [
        'id' => 1007,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'England Legends v South Africa Legends',
        'datetime' => 'Oct 13 2026 17:00 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '2.52',
        'm1_lay' => '2.94',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '1.52',
        'm2_lay' => '1.65'
    ],
    [
        'id' => 1008,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Pakistan v Sri Lanka',
        'datetime' => 'Oct 13 2026 18:30 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '1.42',
        'm1_lay' => '1.43',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '3.35',
        'm2_lay' => '3.40'
    ],
    [
        'id' => 1009,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'India Legends v West Indies Legends',
        'datetime' => 'Oct 13 2026 21:00 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '1.43',
        'm1_lay' => '1.64',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '2.56',
        'm2_lay' => '3.35'
    ],
    [
        'id' => 1010,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'Australia W v Bangladesh W',
        'datetime' => 'Oct 14 2026 09:20 (IST)',
        'live' => true,
        'tv' => false,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '1.01',
        'm1_lay' => '1.02',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '70',
        'm2_lay' => '95'
    ],
    [
        'id' => 1011,
        'sport' => 'cricket',
        'icon' => '🏏',
        'title' => 'India v West Indies',
        'datetime' => 'Oct 14 2026 19:00 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => true,
        'toss' => true,
        'm1_back' => '1.30',
        'm1_lay' => '1.31',
        'mx_back' => '-',
        'mx_lay' => '-',
        'm2_back' => '4.20',
        'm2_lay' => '4.40'
    ],
    [
        'id' => 2001,
        'sport' => 'football',
        'icon' => '⚽',
        'title' => 'Torino v Udinese',
        'datetime' => 'Oct 12 2026 20:05 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => false,
        'toss' => false,
        'm1_back' => '2.48',
        'm1_lay' => '2.50',
        'mx_back' => '3.05',
        'mx_lay' => '3.10',
        'm2_back' => '3.60',
        'm2_lay' => '3.65'
    ],
    [
        'id' => 2002,
        'sport' => 'football',
        'icon' => '⚽',
        'title' => 'Coventry v Newcastle',
        'datetime' => 'Oct 12 2026 20:05 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => false,
        'toss' => false,
        'm1_back' => '3.85',
        'm1_lay' => '3.90',
        'mx_back' => '3.85',
        'mx_lay' => '3.90',
        'm2_back' => '2.06',
        'm2_lay' => '2.08'
    ],
    [
        'id' => 2003,
        'sport' => 'football',
        'icon' => '⚽',
        'title' => 'Levante v Sevilla',
        'datetime' => 'Oct 12 2026 20:05 (IST)',
        'live' => true,
        'tv' => true,
        'bm' => true,
        'fancy' => false,
        'toss' => false,
        'm1_back' => '3.05',
        'm1_lay' => '3.10',
        'mx_back' => '3.40',
        'mx_lay' => '3.45',
        'm2_back' => '2.60',
        'm2_lay' => '2.62'
    ]
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cricxbet99 - Cricket Exchange & Fast Live Line</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500;600;700;800&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --cx-gold: #f59e0b;
            --cx-gold-light: #fbbf24;
            --cx-header-bg: #1e1915;
            --cx-nav-bg: #0b5e40;
            --cx-nav-active: #084c35;
            --cx-sidebar-header: #7c2d12;
            --cx-sidebar-bg: #141210;
            --cx-sidebar-item: #1a1714;
            --cx-body-bg: #12100e;
            --cx-table-header: #211c18;
            --cx-table-row: #181512;
            --cx-table-hover: #221e1a;
            --cx-border: rgba(255, 255, 255, 0.08);
            --cx-back-blue: #72bbef;
            --cx-back-border: #4da3e0;
            --cx-back-text: #042440;
            --cx-lay-pink: #faa9ba;
            --cx-lay-border: #f07b93;
            --cx-lay-text: #450815;
            --cx-text: #f3f4f6;
            --cx-text-muted: #9ca3af;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Inter', sans-serif;
            background: var(--cx-body-bg);
            color: var(--cx-text);
            min-height: 100vh;
            line-height: 1.4;
        }

        /* ===== 1. TOP HEADER ===== */
        .cricx-top-header {
            background: var(--cx-header-bg);
            border-bottom: 1px solid rgba(245, 158, 11, 0.2);
            padding: 8px 16px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        .cricx-logo-area {
            display: flex;
            align-items: center;
            gap: 10px;
            text-decoration: none;
        }
        .cricx-brand-logo {
            font-family: 'Rajdhani', sans-serif;
            font-size: 30px;
            font-weight: 900;
            letter-spacing: 1px;
            color: var(--cx-gold);
            text-shadow: 0 2px 8px rgba(245, 158, 11, 0.4);
            display: flex;
            align-items: center;
            gap: 4px;
        }
        .cricx-brand-logo span { color: #fff; }

        /* Marquee Ticker */
        .cricx-ticker-container {
            flex: 1;
            max-width: 650px;
            background: rgba(245, 158, 11, 0.12);
            border: 1px solid rgba(245, 158, 11, 0.3);
            border-radius: 4px;
            padding: 4px 10px;
            overflow: hidden;
            white-space: nowrap;
        }
        .cricx-ticker-text {
            display: inline-block;
            font-size: 13px;
            font-weight: 700;
            color: var(--cx-gold-light);
            animation: cricx-marquee 22s linear infinite;
        }
        @keyframes cricx-marquee {
            0% { transform: translateX(100%); }
            100% { transform: translateX(-100%); }
        }

        /* Top Right Account Controls */
        .cricx-user-controls {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        .cricx-btn-rules {
            background: #2a241e;
            border: 1px solid rgba(255,255,255,0.15);
            color: #e5e7eb;
            font-size: 12px;
            font-weight: 700;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .cricx-balance-pill {
            display: flex;
            flex-direction: column;
            text-align: right;
            font-size: 12px;
            font-family: 'Rajdhani', sans-serif;
            font-weight: 700;
        }
        .cricx-balance-val { color: #fff; font-size: 15px; }
        .cricx-exposure-val { color: var(--cx-gold); font-size: 12px; }

        .cricx-user-dropdown {
            background: #2a241e;
            border: 1px solid rgba(245, 158, 11, 0.3);
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 700;
            color: #fff;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
        }

        /* ===== 2. SPORTS NAVIGATION RIBBON ===== */
        .cricx-nav-ribbon {
            background: var(--cx-nav-bg);
            border-bottom: 2px solid #063d2b;
            overflow-x: auto;
            white-space: nowrap;
            display: flex;
            align-items: center;
            scrollbar-width: none;
        }
        .cricx-nav-ribbon::-webkit-scrollbar { display: none; }
        .cricx-nav-item {
            display: inline-block;
            padding: 9px 14px;
            font-size: 12px;
            font-weight: 800;
            text-transform: uppercase;
            color: #e2e8f0;
            text-decoration: none;
            border-right: 1px solid rgba(255, 255, 255, 0.1);
            transition: all 0.2s;
        }
        .cricx-nav-item:hover, .cricx-nav-item.active {
            background: var(--cx-nav-active);
            color: #fff;
        }
        .cricx-nav-item.highlight {
            background: var(--cx-gold);
            color: #000;
            font-weight: 900;
        }

        /* ===== 3. MAIN WORKSPACE LAYOUT ===== */
        .cricx-layout-container {
            display: grid;
            grid-template-columns: 220px 1fr 310px;
            min-height: calc(100vh - 100px);
        }

        /* Left Sidebar */
        .cricx-sidebar {
            background: var(--cx-sidebar-bg);
            border-right: 1px solid var(--cx-border);
            font-size: 12px;
        }
        .cricx-side-header {
            background: var(--cx-sidebar-header);
            color: #fff;
            padding: 8px 12px;
            font-weight: 800;
            font-size: 13px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: pointer;
            border-bottom: 1px solid rgba(0,0,0,0.3);
        }
        .cricx-side-menu {
            list-style: none;
        }
        .cricx-side-menu li a {
            display: block;
            padding: 7px 12px;
            color: #cbd5e1;
            text-decoration: none;
            border-bottom: 1px solid rgba(255,255,255,0.03);
            transition: 0.15s;
        }
        .cricx-side-menu li a:hover {
            background: var(--cx-sidebar-item);
            color: var(--cx-gold);
            padding-left: 16px;
        }

        /* Middle Content Area */
        .cricx-main-content {
            padding: 8px 12px 60px;
            overflow-x: hidden;
        }

        /* Top Match Pills Carousel */
        .cricx-match-pills-row {
            display: flex;
            gap: 6px;
            overflow-x: auto;
            padding-bottom: 8px;
            margin-bottom: 8px;
            scrollbar-width: thin;
        }
        .cricx-match-pill {
            flex-shrink: 0;
            background: #0f3d30;
            border: 1px solid rgba(255,255,255,0.1);
            border-radius: 4px;
            padding: 5px 10px;
            font-size: 11px;
            color: #fff;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: 0.2s;
        }
        .cricx-match-pill:hover { background: #145341; border-color: var(--cx-gold); }
        .cricx-pill-time { color: var(--cx-gold-light); font-size: 10px; font-weight: 700; }

        /* Secondary Category Ribbon */
        .cricx-inplay-ribbon {
            background: #1c1815;
            border: 1px solid var(--cx-border);
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 4px 8px;
            margin-bottom: 8px;
            font-size: 12px;
        }
        .cricx-inplay-tabs {
            display: flex;
            gap: 2px;
            overflow-x: auto;
        }
        .cricx-inplay-tab {
            padding: 5px 10px;
            background: #28221c;
            color: #d1d5db;
            text-decoration: none;
            font-weight: 800;
            font-size: 11px;
            border-radius: 2px;
            text-transform: uppercase;
        }
        .cricx-inplay-tab.active { background: #0c4e38; color: #fff; }
        .cricx-filter-tags { display: flex; gap: 6px; align-items: center; }
        .cricx-filter-pill { font-size: 10px; font-weight: 700; color: #9ca3af; background: #26201b; padding: 3px 8px; border-radius: 3px; cursor: pointer; }

        /* Exchange Match Table */
        .cricx-exchange-table {
            width: 100%;
            border-collapse: collapse;
            background: var(--cx-table-row);
            border: 1px solid var(--cx-border);
            font-size: 12px;
        }
        .cricx-exchange-table thead tr:first-child th {
            background: var(--cx-table-header);
            padding: 6px 10px;
            font-weight: 800;
            color: #e5e7eb;
            border: 1px solid var(--cx-border);
            text-align: center;
        }
        .cricx-exchange-table thead tr:first-child th:first-child { text-align: left; }
        .cricx-exchange-table thead tr.sub-header th {
            font-size: 10px;
            font-weight: 800;
            padding: 3px;
            border: 1px solid var(--cx-border);
            text-align: center;
        }
        .cricx-sub-back { background: var(--cx-back-blue); color: var(--cx-back-text); }
        .cricx-sub-lay { background: var(--cx-lay-pink); color: var(--cx-lay-text); }

        .cricx-exchange-table tbody tr {
            border-bottom: 1px solid var(--cx-border);
            transition: background 0.15s;
        }
        .cricx-exchange-table tbody tr:hover { background: var(--cx-table-hover); }

        .cricx-game-cell {
            padding: 8px 10px;
            vertical-align: middle;
        }
        .cricx-game-link {
            text-decoration: none;
            color: #f3f4f6;
            font-weight: 700;
            font-size: 12px;
            display: inline-block;
            margin-right: 6px;
        }
        .cricx-game-link:hover { color: var(--cx-gold); text-decoration: underline; }
        .cricx-game-date { color: #9ca3af; font-size: 11px; }
        .cricx-game-badges {
            display: inline-flex;
            gap: 4px;
            align-items: center;
            margin-left: 8px;
            vertical-align: middle;
        }
        .cricx-badge {
            font-size: 9px;
            font-weight: 800;
            padding: 1px 4px;
            border-radius: 2px;
            border: 1px solid transparent;
        }
        .cricx-badge-dot { width: 7px; height: 7px; border-radius: 50%; background: #10b981; animation: pulse 1.5s infinite; }
        @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.4} }
        .cricx-badge-bm { background: #1e3a8a; color: #93c5fd; }
        .cricx-badge-fancy { background: #4c1d95; color: #d8b4fe; }
        .cricx-badge-toss { background: #14532d; color: #86efac; }

        /* Odds Cell Buttons */
        .cricx-odds-box {
            width: 52px;
            height: 34px;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Rajdhani', sans-serif;
            font-weight: 800;
            font-size: 13px;
            cursor: pointer;
            user-select: none;
            transition: 0.15s;
            border: 1px solid rgba(0,0,0,0.15);
        }
        .cricx-odds-box:hover { filter: brightness(1.1); transform: scale(1.02); }
        .cricx-odds-box.back { background: var(--cx-back-blue); color: var(--cx-back-text); }
        .cricx-odds-box.lay { background: var(--cx-lay-pink); color: var(--cx-lay-text); }
        .cricx-odds-box.empty { background: transparent; color: #4b5563; cursor: default; }

        /* ===== 4. RIGHT SIDEBAR (BET SLIP) ===== */
        .cricx-right-sidebar {
            background: var(--cx-sidebar-bg);
            border-left: 1px solid var(--cx-border);
            padding: 8px 10px;
        }
        .cricx-top-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            font-size: 12px;
            font-weight: 700;
        }
        .cricx-1click-toggle { display: flex; align-items: center; gap: 6px; cursor: pointer; color: #e5e7eb; }
        .cricx-btn-dragon {
            background: #b45309;
            color: #fff;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 11px;
            font-weight: 800;
            text-decoration: none;
        }

        /* Bet Slip Card */
        .cricx-betslip-card {
            background: #1e1915;
            border: 1px solid rgba(245, 158, 11, 0.3);
            border-radius: 6px;
            overflow: hidden;
            margin-bottom: 12px;
        }
        .cricx-betslip-header {
            background: #0b5e40;
            color: #fff;
            padding: 8px 12px;
            font-weight: 800;
            font-size: 13px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .cricx-betslip-body { padding: 12px; }
        .cricx-bet-runner-tag {
            background: #2a221b;
            padding: 6px 10px;
            border-radius: 4px;
            font-weight: 800;
            font-size: 12px;
            color: var(--cx-gold-light);
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
        }
        .cricx-input-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-bottom: 10px;
        }
        .cricx-input-group label {
            display: block;
            font-size: 11px;
            color: #9ca3af;
            font-weight: 700;
            margin-bottom: 4px;
        }
        .cricx-input-group input {
            width: 100%;
            background: #12100e;
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 4px;
            padding: 6px 8px;
            color: #fff;
            font-family: 'Rajdhani', sans-serif;
            font-size: 16px;
            font-weight: 800;
        }
        .cricx-profit-display {
            font-size: 12px;
            font-weight: 700;
            color: #10b981;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
        }

        /* Stake Quick Buttons Grid */
        .cricx-stakes-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 4px;
            margin-bottom: 10px;
        }
        .cricx-stake-btn {
            background: #2a241e;
            border: 1px solid rgba(255,255,255,0.1);
            color: #e5e7eb;
            padding: 6px 2px;
            border-radius: 3px;
            font-size: 11px;
            font-weight: 700;
            cursor: pointer;
            text-align: center;
        }
        .cricx-stake-btn:hover { background: #3d342b; border-color: var(--cx-gold); }

        .cricx-actions-row {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 4px;
            margin-bottom: 12px;
        }
        .cricx-action-btn {
            background: #201a15;
            border: 1px solid rgba(255,255,255,0.08);
            color: #9ca3af;
            padding: 4px;
            font-size: 10px;
            font-weight: 800;
            cursor: pointer;
            border-radius: 3px;
            text-align: center;
        }

        .cricx-submit-row {
            display: grid;
            grid-template-columns: 1fr 2fr;
            gap: 8px;
        }
        .cricx-btn-reset {
            background: #dc2626;
            color: #fff;
            border: none;
            padding: 9px;
            border-radius: 4px;
            font-weight: 800;
            cursor: pointer;
            font-size: 12px;
        }
        .cricx-btn-submit {
            background: #059669;
            color: #fff;
            border: none;
            padding: 9px;
            border-radius: 4px;
            font-weight: 800;
            cursor: pointer;
            font-size: 13px;
        }

        /* My Bets Card */
        .cricx-mybets-card {
            background: #1e1915;
            border: 1px solid var(--cx-border);
            border-radius: 6px;
            overflow: hidden;
        }
        .cricx-mybets-header {
            background: #0f3d30;
            color: #fff;
            padding: 8px 12px;
            font-size: 12px;
            font-weight: 800;
        }
        .cricx-mybets-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
        }
        .cricx-mybets-table th {
            background: #26201a;
            padding: 6px 8px;
            color: #9ca3af;
            text-align: left;
        }
        .cricx-mybets-table td {
            padding: 6px 8px;
            border-bottom: 1px solid rgba(255,255,255,0.04);
        }

        @media(max-width: 1100px) {
            .cricx-layout-container { grid-template-columns: 1fr; }
            .cricx-sidebar, .cricx-right-sidebar { display: none; }
        }
    </style>
</head>
<body>

<!-- 1. Top Header -->
<header class="cricx-top-header">
    <a href="scores.php" class="cricx-logo-area">
        <div class="cricx-brand-logo">Cricx<span>bet99</span></div>
    </a>

    <div class="cricx-ticker-container">
        <span class="cricx-ticker-text">
            🏆 AVAILABLE IN OUR EXCHANGE 🏆 DREAM BIG WIN BIG 🏆 FASTEST CRICKET LIVE LINE 🏆 INSTANT WITHDRAWALS 🏆
        </span>
    </div>

    <div class="cricx-user-controls">
        <button class="cricx-btn-rules" onclick="alert('Cricket Exchange Rules: Match Odds, Sessions & Bookmaker rates are settled automatically at innings close.')">
            <i class="fas fa-search"></i> Rules
        </button>

        <div class="cricx-balance-pill">
            <span class="cricx-balance-val">Balance: <strong id="userBalanceVal"><?= number_format($user_balance, 2) ?></strong></span>
            <span class="cricx-exposure-val">Exposure: <strong id="userExposureVal"><?= number_format($user_exposure, 2) ?></strong></span>
        </div>

        <div class="cricx-user-dropdown" onclick="location.href='admin/user_login.php'">
            <i class="fas fa-user-circle" style="color:var(--cx-gold)"></i>
            <span><?= htmlspecialchars($user_display_name) ?></span>
            <i class="fas fa-caret-down"></i>
        </div>
    </div>
</header>

<!-- 2. Main Sports Ribbon Bar -->
<nav class="cricx-nav-ribbon">
    <a href="scores.php" class="cricx-nav-item active">HOME</a>
    <a href="scores.php?sport=cricket" class="cricx-nav-item">CRICKET</a>
    <a href="scores.php?sport=football" class="cricx-nav-item">FOOTBALL</a>
    <a href="scores.php?sport=tennis" class="cricx-nav-item">TENNIS</a>
    <a href="scores.php?sport=fantasy" class="cricx-nav-item highlight">FANTASY 11</a>
    <a href="#" class="cricx-nav-item">COCK FIGHT</a>
    <a href="#" class="cricx-nav-item">CASINO</a>
    <a href="#" class="cricx-nav-item">SPORTS BOOK</a>
    <a href="#" class="cricx-nav-item">HORSE RACING</a>
    <a href="#" class="cricx-nav-item">GREYHOUND RACING</a>
    <a href="#" class="cricx-nav-item">OTHER</a>
    <a href="#" class="cricx-nav-item">BINARY</a>
    <a href="#" class="cricx-nav-item">KABADDI</a>
    <a href="#" class="cricx-nav-item">POLITICS</a>
    <a href="#" class="cricx-nav-item">BASKETBALL</a>
    <a href="#" class="cricx-nav-item">BASEBALL</a>
    <a href="#" class="cricx-nav-item">TABLE TENNIS</a>
    <a href="#" class="cricx-nav-item">VOLLEYBALL</a>
    <a href="#" class="cricx-nav-item">ICE HOCKEY</a>
</nav>

<!-- 3. Three-Column Workspace Layout -->
<div class="cricx-layout-container">

    <!-- Left Sidebar -->
    <aside class="cricx-sidebar">
        <!-- Others Menu -->
        <div class="cricx-side-header">
            <span>Others</span>
            <i class="fas fa-chevron-down"></i>
        </div>
        <ul class="cricx-side-menu">
            <li><a href="#">TP T20</a></li>
            <li><a href="#">TP 1 DAY</a></li>
            <li><a href="#">TP TEST</a></li>
            <li><a href="#">DT 1 DAY</a></li>
            <li><a href="#">32 CARDS</a></li>
            <li><a href="#">HI LOW</a></li>
            <li><a href="#">POKER</a></li>
            <li><a href="#">QUEEN</a></li>
            <li><a href="#">BACCARAT</a></li>
            <li><a href="#">AMAR AKBAR ANTHONY</a></li>
            <li><a href="#">BOLLYWOOD</a></li>
            <li><a href="#">TRIO</a></li>
            <li><a href="#">ANDAR BAHAR</a></li>
            <li><a href="#">MUFLIS TP</a></li>
            <li><a href="#">ROULETTE</a></li>
            <li><a href="#">WORLI MATKA</a></li>
        </ul>

        <!-- All Sports Menu -->
        <div class="cricx-side-header" style="margin-top:4px;">
            <span>All Sports</span>
            <i class="fas fa-chevron-down"></i>
        </div>
        <ul class="cricx-side-menu">
            <li><a href="scores.php?sport=cricket">＋ Cricket</a></li>
            <li><a href="scores.php?sport=football">＋ Football</a></li>
            <li><a href="scores.php?sport=tennis">＋ Tennis</a></li>
            <li><a href="#">＋ Horse Racing</a></li>
            <li><a href="#">＋ Greyhound Racing</a></li>
            <li><a href="#">＋ Binary</a></li>
            <li><a href="#">＋ Basketball</a></li>
            <li><a href="#">＋ Baseball</a></li>
            <li><a href="#">＋ Table Tennis</a></li>
            <li><a href="#">＋ Volleyball</a></li>
            <li><a href="#">＋ Ice Hockey</a></li>
            <li><a href="#">＋ Darts</a></li>
        </ul>
    </aside>

    <!-- Center Content -->
    <main class="cricx-main-content">
        <!-- Top Match Pills Carousel -->
        <div class="cricx-match-pills-row">
            <?php foreach ($exchange_matches as $m): ?>
            <a href="match.php?id=<?= $m['id'] ?>" class="cricx-match-pill">
                <span><?= $m['icon'] ?></span>
                <span><?= htmlspecialchars($m['title']) ?></span>
                <span class="cricx-pill-time"><?= substr($m['datetime'], 0, 11) ?></span>
            </a>
            <?php endforeach; ?>
        </div>

        <!-- In-Play Subcategory Bar -->
        <div class="cricx-inplay-ribbon">
            <div class="cricx-inplay-tabs">
                <a href="#" class="cricx-inplay-tab active">INPLAY</a>
                <a href="#" class="cricx-inplay-tab">CRICKET</a>
                <a href="#" class="cricx-inplay-tab">FOOTBALL</a>
                <a href="#" class="cricx-inplay-tab">TENNIS</a>
                <a href="#" class="cricx-inplay-tab">FANTASY 11</a>
                <a href="#" class="cricx-inplay-tab">COCK FIGHT</a>
                <a href="#" class="cricx-inplay-tab">CASINO</a>
                <a href="#" class="cricx-inplay-tab">HORSE RACING</a>
                <a href="#" class="cricx-inplay-tab">GREYHOUND RACING</a>
                <a href="#" class="cricx-inplay-tab">OTHER</a>
                <a href="#" class="cricx-inplay-tab">BINARY</a>
                <a href="#" class="cricx-inplay-tab">KABADDI</a>
            </div>

            <div class="cricx-filter-tags">
                <span class="cricx-filter-pill">- LIVE</span>
                <span class="cricx-filter-pill">- VIRTUAL</span>
                <span class="cricx-filter-pill">- PREMIUM</span>
                <span style="font-size:11px;color:#9ca3af">View by:</span>
                <select style="background:#26201b;color:#fff;border:1px solid #444;font-size:11px;padding:2px 4px;border-radius:3px;">
                    <option>TIME</option>
                    <option>COMPETITION</option>
                </select>
            </div>
        </div>

        <!-- Exchange Odds Table -->
        <table class="cricx-exchange-table">
            <thead>
                <tr>
                    <th rowspan="2" style="width:50%;">Game</th>
                    <th colspan="2" style="width:16%;">1</th>
                    <th colspan="2" style="width:16%;">X</th>
                    <th colspan="2" style="width:16%;">2</th>
                </tr>
                <tr class="sub-header">
                    <th class="cricx-sub-back">Back</th>
                    <th class="cricx-sub-lay">Lay</th>
                    <th class="cricx-sub-back">Back</th>
                    <th class="cricx-sub-lay">Lay</th>
                    <th class="cricx-sub-back">Back</th>
                    <th class="cricx-sub-lay">Lay</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($exchange_matches as $m): ?>
                <tr>
                    <td class="cricx-game-cell">
                        <span style="margin-right:6px;"><?= $m['icon'] ?></span>
                        <a href="match.php?id=<?= $m['id'] ?>" class="cricx-game-link">
                            <?= htmlspecialchars($m['title']) ?>
                        </a>
                        <span class="cricx-game-date">/ <?= $m['datetime'] ?></span>

                        <div class="cricx-game-badges">
                            <?php if ($m['live']): ?><span class="cricx-badge-dot"></span><?php endif; ?>
                            <?php if ($m['tv']): ?><i class="fas fa-tv" style="color:#60a5fa;font-size:11px;"></i><?php endif; ?>
                            <i class="fas fa-thumbtack" style="color:#9ca3af;font-size:10px;"></i>
                            <?php if ($m['bm']): ?><span class="cricx-badge cricx-badge-bm">BM</span><?php endif; ?>
                            <?php if ($m['fancy']): ?><span class="cricx-badge cricx-badge-fancy">F</span><?php endif; ?>
                            <?php if ($m['toss']): ?><span class="cricx-badge cricx-badge-toss">T</span><?php endif; ?>
                        </div>
                    </td>

                    <!-- 1 (Team 1) -->
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['m1_back'] !== '-'): ?>
                        <div class="cricx-odds-box back" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', '<?= explode(' v ', $m['title'])[0] ?>', '<?= $m['m1_back'] ?>', 'back', <?= $m['id'] ?>)">
                            <?= $m['m1_back'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['m1_lay'] !== '-'): ?>
                        <div class="cricx-odds-box lay" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', '<?= explode(' v ', $m['title'])[0] ?>', '<?= $m['m1_lay'] ?>', 'lay', <?= $m['id'] ?>)">
                            <?= $m['m1_lay'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>

                    <!-- X (Draw) -->
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['mx_back'] !== '-'): ?>
                        <div class="cricx-odds-box back" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', 'The Draw', '<?= $m['mx_back'] ?>', 'back', <?= $m['id'] ?>)">
                            <?= $m['mx_back'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['mx_lay'] !== '-'): ?>
                        <div class="cricx-odds-box lay" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', 'The Draw', '<?= $m['mx_lay'] ?>', 'lay', <?= $m['id'] ?>)">
                            <?= $m['mx_lay'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>

                    <!-- 2 (Team 2) -->
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['m2_back'] !== '-'): ?>
                        <div class="cricx-odds-box back" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', '<?= explode(' v ', $m['title'])[1] ?? 'Team 2' ?>', '<?= $m['m2_back'] ?>', 'back', <?= $m['id'] ?>)">
                            <?= $m['m2_back'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>
                    <td style="padding:2px;text-align:center;">
                        <?php if ($m['m2_lay'] !== '-'): ?>
                        <div class="cricx-odds-box lay" onclick="openBetSlip('<?= htmlspecialchars($m['title']) ?>', '<?= explode(' v ', $m['title'])[1] ?? 'Team 2' ?>', '<?= $m['m2_lay'] ?>', 'lay', <?= $m['id'] ?>)">
                            <?= $m['m2_lay'] ?>
                        </div>
                        <?php else: ?><div class="cricx-odds-box empty">-</div><?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </main>

    <!-- Right Sidebar (Interactive Bet Slip) -->
    <aside class="cricx-right-sidebar">
        <div class="cricx-top-actions">
            <label class="cricx-1click-toggle">
                <input type="checkbox" id="oneClickBetToggle"> 1 Click Betting
            </label>
            <a href="#" class="cricx-btn-dragon">👉 DRAGON TIGER</a>
        </div>

        <!-- Place Bet Card -->
        <div class="cricx-betslip-card" id="betSlipCard">
            <div class="cricx-betslip-header">
                <span>Place Bet</span>
                <span id="betTypeBadge" style="font-size:10px;padding:2px 6px;border-radius:3px;background:var(--cx-back-blue);color:#042440;">BACK</span>
            </div>
            <div class="cricx-betslip-body">
                <div class="cricx-bet-runner-tag">
                    <span id="betRunnerName">Afghanistan - Match Odds</span>
                    <span id="betOddTag" style="color:var(--cx-gold)">@ 130</span>
                </div>

                <div class="cricx-input-row">
                    <div class="cricx-input-group">
                        <label>Odds</label>
                        <input type="number" step="0.01" id="slipOddsInput" value="130" oninput="calculateProfit()">
                    </div>
                    <div class="cricx-input-group">
                        <label>Stake</label>
                        <input type="number" id="slipStakeInput" value="2000" oninput="calculateProfit()">
                    </div>
                </div>

                <div class="cricx-profit-display">
                    <span>Est. Profit / Win:</span>
                    <strong id="slipProfitVal">+₹258,000.00</strong>
                </div>

                <!-- Quick Stake Buttons Grid -->
                <div class="cricx-stakes-grid">
                    <button class="cricx-stake-btn" onclick="setStake(100)">100</button>
                    <button class="cricx-stake-btn" onclick="setStake(2000)">2000</button>
                    <button class="cricx-stake-btn" onclick="setStake(30000)">30000</button>
                    <button class="cricx-stake-btn" onclick="setStake(40000)">40000</button>
                    <button class="cricx-stake-btn" onclick="setStake(50000)">50000</button>
                    <button class="cricx-stake-btn" onclick="setStake(60000)">60000</button>
                    <button class="cricx-stake-btn" onclick="setStake(80000)">80000</button>
                    <button class="cricx-stake-btn" onclick="setStake(10000)">10000</button>
                </div>

                <!-- Stake Actions -->
                <div class="cricx-actions-row">
                    <button class="cricx-action-btn" onclick="setStake(100)">MIN STAKE</button>
                    <button class="cricx-action-btn" onclick="setStake(50000)">MAX STAKE</button>
                    <button class="cricx-action-btn" onclick="document.getElementById('slipStakeInput').focus()">EDIT STAKE</button>
                    <button class="cricx-action-btn" onclick="setStake(0)">CLEAR</button>
                </div>

                <!-- Form Controls -->
                <div class="cricx-submit-row">
                    <button class="cricx-btn-reset" onclick="resetBetSlip()">Reset</button>
                    <button class="cricx-btn-submit" id="btnSubmitBet" onclick="submitBet()">Submit</button>
                </div>
            </div>
        </div>

        <!-- My Bet Table -->
        <div class="cricx-mybets-card">
            <div class="cricx-mybets-header">
                <i class="fas fa-list"></i> My Bet
            </div>
            <table class="cricx-mybets-table">
                <thead>
                    <tr><th>Nation</th><th>Odds</th><th>Stake</th></tr>
                </thead>
                <tbody id="myBetsTableBody">
                    <tr><td colspan="3" style="text-align:center;color:#6b7280;padding:20px;">No records Found</td></tr>
                </tbody>
            </table>
        </div>
    </aside>

</div>

<script>
let currentBet = {
    match_id: 1001,
    match_name: 'Afghanistan v Bangladesh',
    runner_name: 'Afghanistan',
    bet_type: 'back',
    odds: 130,
    stake: 2000
};

function openBetSlip(matchName, runner, odds, betType, matchId) {
    currentBet.match_name = matchName;
    currentBet.runner_name = runner;
    currentBet.odds = parseFloat(odds) || 1.50;
    currentBet.bet_type = betType;
    currentBet.match_id = matchId;

    document.getElementById('betRunnerName').innerText = runner;
    document.getElementById('betOddTag').innerText = '@ ' + odds;
    document.getElementById('slipOddsInput').value = odds;

    const badge = document.getElementById('betTypeBadge');
    badge.innerText = betType.toUpperCase();
    if (betType === 'lay') {
        badge.style.background = 'var(--cx-lay-pink)';
        badge.style.color = '#450815';
    } else {
        badge.style.background = 'var(--cx-back-blue)';
        badge.style.color = '#042440';
    }

    calculateProfit();
}

function setStake(amount) {
    document.getElementById('slipStakeInput').value = amount;
    currentBet.stake = amount;
    calculateProfit();
}

function calculateProfit() {
    const odds = parseFloat(document.getElementById('slipOddsInput').value) || 1;
    const stake = parseFloat(document.getElementById('slipStakeInput').value) || 0;
    currentBet.odds = odds;
    currentBet.stake = stake;

    let profit = 0;
    if (currentBet.bet_type === 'back') {
        profit = (odds - 1) * stake;
    } else {
        profit = stake;
    }

    document.getElementById('slipProfitVal').innerText = (profit >= 0 ? '+' : '') + '₹' + profit.toLocaleString('en-IN', {minimumFractionDigits:2, maximumFractionDigits:2});
}

function resetBetSlip() {
    document.getElementById('slipStakeInput').value = 0;
    calculateProfit();
}

async function submitBet() {
    const btn = document.getElementById('btnSubmitBet');
    const stake = parseFloat(document.getElementById('slipStakeInput').value) || 0;
    const odds = parseFloat(document.getElementById('slipOddsInput').value) || 0;

    if (stake <= 0) {
        alert('Please enter a stake amount.');
        return;
    }

    btn.disabled = true;
    btn.innerText = 'Placing...';

    try {
        const res = await fetch('api/place_bet.php', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                match_id: currentBet.match_id,
                match_name: currentBet.match_name,
                market_type: 'match_odds',
                runner_name: currentBet.runner_name,
                bet_type: currentBet.bet_type,
                odds: odds,
                stake: stake
            })
        });

        const data = await res.json();
        if (data.success) {
            alert('🎉 Bet Placed Successfully on ' + currentBet.runner_name + ' @ ' + odds + '!');
            if (data.formatted_balance) {
                document.getElementById('userBalanceVal').innerText = data.new_balance.toLocaleString('en-IN', {minimumFractionDigits:2});
                document.getElementById('userExposureVal').innerText = data.exposure.toLocaleString('en-IN', {minimumFractionDigits:2});
            }

            // Append to My Bets
            const tbody = document.getElementById('myBetsTableBody');
            const row = `<tr>
                <td><strong>${data.bet.runner_name}</strong><br><small style="color:#9ca3af">${data.bet.bet_type.toUpperCase()}</small></td>
                <td style="color:var(--cx-gold)">${data.bet.odds}</td>
                <td>₹${data.bet.stake.toLocaleString('en-IN')}</td>
            </tr>`;
            if (tbody.innerHTML.includes('No records Found')) {
                tbody.innerHTML = row;
            } else {
                tbody.innerHTML = row + tbody.innerHTML;
            }
        } else {
            alert('❌ ' + (data.error || 'Could not place bet'));
        }
    } catch (e) {
        alert('Error placing bet. Please try again.');
    } finally {
        btn.disabled = false;
        btn.innerText = 'Submit';
    }
}

// Initial calculation
calculateProfit();
</script>

</body>
</html>
