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

$id = (int)($_GET['id'] ?? 1001);
$match = get_fixture($id);
if (!$match) {
    redirect('scores.php');
}

$local = $match['localteam'] ?? $match['localTeam'] ?? ['name' => 'Afghanistan', 'code' => 'AFG'];
$visitor = $match['visitorteam'] ?? $match['visitorTeam'] ?? ['name' => 'Bangladesh', 'code' => 'BAN'];
$match_title = strtoupper(($local['name'] ?? 'AFGHANISTAN') . ' V ' . ($visitor['name'] ?? 'BANGLADESH'));

// 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'];
}

$open_bets = $_SESSION['open_bets'] ?? [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($match_title) ?> - Cricxbet99 Game Detail</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-match-header: #0a442e;
            --cx-card-bg: #1a1714;
            --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;
        }

        /* 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);
        }
        .cricx-brand-logo span { color: #fff; }
        .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%)} }

        .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;
        }
        .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; cursor: pointer;
        }

        /* 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;
        }
        .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);
        }
        .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; }

        /* Workspace Grid */
        .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;
        }
        .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);
        }
        .cricx-side-menu li a:hover { background: var(--cx-sidebar-item); color: var(--cx-gold); padding-left: 16px; }

        /* Middle Detail Container */
        .cricx-detail-content { padding: 8px 12px 80px; overflow-x: hidden; }

        /* Match Title Strip */
        .cricx-match-topstrip {
            background: var(--cx-match-header);
            color: #fff;
            padding: 8px 14px;
            font-weight: 800;
            font-size: 14px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-radius: 4px 4px 0 0;
            letter-spacing: 0.5px;
        }

        /* ===== 2D PITCH VISUALIZER BANNER ===== */
        .cricx-pitch-visualizer {
            background: linear-gradient(180deg, #091322 0%, #0d2818 100%);
            border: 1px solid rgba(0, 212, 170, 0.2);
            position: relative;
            overflow: hidden;
            padding: 16px;
            box-shadow: inset 0 0 50px rgba(0,0,0,0.8);
            margin-bottom: 12px;
        }
        /* Stadium 2D Pitch Graphics */
        .cricx-stadium-canvas {
            position: absolute;
            top: 0; left: 0; right: 0; bottom: 0;
            pointer-events: none;
            opacity: 0.25;
            background: 
                radial-gradient(circle at 50% 120%, #166534 0%, transparent 65%),
                radial-gradient(circle at 50% -20%, #0284c7 0%, transparent 50%);
        }
        .cricx-pitch-rect {
            position: absolute;
            top: 25%; left: 35%; right: 35%; bottom: 15%;
            background: repeating-linear-gradient(0deg, #854d0e, #854d0e 8px, #713f12 8px, #713f12 16px);
            border: 2px solid rgba(255,255,255,0.2);
            border-radius: 6px;
            opacity: 0.35;
        }
        .cricx-player-silhouette-left {
            position: absolute; left: 60px; bottom: 20px; font-size: 70px; color: rgba(255,255,255,0.12);
        }
        .cricx-player-silhouette-right {
            position: absolute; right: 60px; bottom: 20px; font-size: 70px; color: rgba(255,255,255,0.12);
        }

        /* Scoreboard Layer */
        .cricx-scoreboard-overlay { position: relative; z-index: 2; }
        .cricx-teams-score-row {
            display: grid;
            grid-template-columns: 1fr auto 1fr;
            align-items: center;
            gap: 16px;
            margin-bottom: 10px;
        }
        .cricx-team-block { font-family: 'Rajdhani', sans-serif; }
        .cricx-team-title { font-size: 20px; font-weight: 800; color: #fff; letter-spacing: 0.5px; }
        .cricx-team-score-num { font-size: 26px; font-weight: 900; color: #fff; }
        .cricx-team-crr { font-size: 13px; color: var(--cx-gold-light); font-weight: 700; margin-left: 8px; }

        .cricx-match-target-badge {
            background: rgba(0,0,0,0.6);
            border: 1px solid rgba(245, 158, 11, 0.4);
            padding: 4px 12px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 800;
            color: var(--cx-gold);
            text-align: center;
        }

        /* Ball By Ball Over Strip */
        .cricx-over-strip {
            display: flex;
            align-items: center;
            gap: 6px;
            margin: 12px 0 10px;
            font-size: 11px;
            font-weight: 800;
        }
        .cricx-ball-pill {
            width: 22px; height: 22px; border-radius: 50%;
            display: inline-flex; align-items: center; justify-content: center;
            font-size: 10px; font-weight: 900; font-family: monospace;
            background: #27272a; color: #fff; border: 1px solid rgba(255,255,255,0.2);
        }
        .cricx-ball-pill.c-0 { background: #3f3f46; color: #a1a1aa; }
        .cricx-ball-pill.c-1 { background: #1e3a8a; color: #93c5fd; }
        .cricx-ball-pill.c-3 { background: #065f46; color: #6ee7b7; }
        .cricx-ball-pill.c-w { background: #991b1b; color: #fca5a5; }

        /* Batsmen & Bowler Cards Grid */
        .cricx-crease-stats-grid {
            display: grid;
            grid-template-columns: 2fr 1fr;
            gap: 12px;
            background: rgba(0,0,0,0.5);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 4px;
            padding: 8px 12px;
            font-size: 12px;
        }
        .cricx-stat-table { width: 100%; border-collapse: collapse; }
        .cricx-stat-table th { text-align: right; color: #9ca3af; font-size: 11px; padding: 2px 4px; font-weight: 700; }
        .cricx-stat-table th:first-child { text-align: left; }
        .cricx-stat-table td { text-align: right; padding: 2px 4px; font-family: 'Rajdhani', sans-serif; font-weight: 700; font-size: 13px; }
        .cricx-stat-table td:first-child { text-align: left; font-family: 'Inter', sans-serif; font-size: 12px; font-weight: 600; color: #fff; }

        /* ===== EXCHANGE MARKETS ACCORDIONS ===== */
        .cricx-market-card {
            background: var(--cx-card-bg);
            border: 1px solid var(--cx-border);
            border-radius: 4px;
            overflow: hidden;
            margin-bottom: 12px;
        }
        .cricx-market-header {
            background: #1e392d;
            padding: 6px 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            font-weight: 800;
            color: #fff;
            border-bottom: 1px solid var(--cx-border);
        }
        .cricx-market-limits { font-size: 11px; color: var(--cx-gold-light); font-weight: 700; }

        /* 3-Tier Match Odds Table */
        .cricx-matchodds-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
        }
        .cricx-matchodds-table thead th {
            padding: 6px;
            border: 1px solid var(--cx-border);
            text-align: center;
            font-size: 11px;
            font-weight: 800;
        }
        .cricx-th-back { background: var(--cx-back-blue); color: var(--cx-back-text); }
        .cricx-th-lay { background: var(--cx-lay-pink); color: var(--cx-lay-text); }

        .cricx-matchodds-table tbody tr {
            border-bottom: 1px solid var(--cx-border);
            transition: background 0.15s;
        }
        .cricx-matchodds-table tbody tr:hover { background: #26201b; }
        .cricx-runner-name-cell {
            padding: 8px 12px;
            font-weight: 700;
            color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .cricx-live-pnl {
            font-family: 'Rajdhani', sans-serif;
            font-weight: 800;
            font-size: 13px;
        }
        .cricx-live-pnl.green { color: #10b981; }
        .cricx-live-pnl.red { color: #ef4444; }

        /* 3-Tier Odds Depth Boxes */
        .cricx-tier-box {
            width: 54px; height: 38px;
            text-align: center;
            display: flex; flex-direction: column;
            align-items: center; justify-content: center;
            cursor: pointer; user-select: none;
            transition: 0.15s;
            border: 1px solid rgba(0,0,0,0.15);
        }
        .cricx-tier-box:hover { filter: brightness(1.12); transform: scale(1.02); }
        .cricx-tier-odds { font-family: 'Rajdhani', sans-serif; font-size: 14px; font-weight: 800; line-height: 1.1; }
        .cricx-tier-vol { font-size: 9px; opacity: 0.85; line-height: 1; }

        .cricx-tier-box.back-light { background: #9fd3f7; color: #042440; }
        .cricx-tier-box.back-mid { background: #89c7f3; color: #042440; }
        .cricx-tier-box.back-main { background: var(--cx-back-blue); color: #042440; font-weight: 900; }

        .cricx-tier-box.lay-main { background: var(--cx-lay-pink); color: #450815; font-weight: 900; }
        .cricx-tier-box.lay-mid { background: #fbbac8; color: #450815; }
        .cricx-tier-box.lay-light { background: #fccbd5; color: #450815; }

        /* Bookmaker Suspended Overlay */
        .cricx-suspended-wrap {
            position: relative;
        }
        .cricx-suspended-overlay {
            position: absolute;
            top: 0; left: 0; right: 0; bottom: 0;
            background: rgba(18, 16, 14, 0.88);
            display: flex;
            align-items: center;
            justify-content: center;
            color: #ef4444;
            font-family: 'Rajdhani', sans-serif;
            font-size: 18px;
            font-weight: 900;
            letter-spacing: 2px;
            z-index: 5;
            user-select: none;
        }

        /* ===== FANCY / SESSIONS 2-COLUMN SECTION ===== */
        .cricx-fancy-tabs-bar {
            display: flex;
            align-items: center;
            gap: 4px;
            background: #2a221b;
            padding: 6px;
            border-radius: 4px;
            margin-bottom: 8px;
            overflow-x: auto;
        }
        .cricx-fancy-tab {
            padding: 5px 12px;
            font-size: 11px;
            font-weight: 800;
            background: #1a1512;
            color: #d1d5db;
            text-decoration: none;
            border-radius: 3px;
        }
        .cricx-fancy-tab.active { background: var(--cx-gold); color: #000; }

        .cricx-fancy-subtabs {
            display: flex;
            gap: 4px;
            margin-bottom: 8px;
            overflow-x: auto;
        }
        .cricx-subtab {
            padding: 4px 10px;
            background: #201a15;
            color: #9ca3af;
            font-size: 10px;
            font-weight: 800;
            border-radius: 3px;
            text-decoration: none;
            cursor: pointer;
        }
        .cricx-subtab.active { background: #0c4e38; color: #fff; }

        .cricx-fancy-grid-2col {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-bottom: 12px;
        }

        .cricx-session-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
            background: var(--cx-card-bg);
            border: 1px solid var(--cx-border);
        }
        .cricx-session-table thead th {
            background: #1c1713;
            padding: 6px 8px;
            font-weight: 800;
            border: 1px solid var(--cx-border);
            text-align: center;
        }
        .cricx-session-table tbody td {
            padding: 6px 8px;
            border-bottom: 1px solid var(--cx-border);
        }
        .cricx-sess-box {
            width: 48px; height: 32px;
            display: flex; flex-direction: column;
            align-items: center; justify-content: center;
            font-family: 'Rajdhani', sans-serif;
            font-size: 13px; font-weight: 800;
            cursor: pointer;
            border-radius: 2px;
            user-select: none;
        }
        .cricx-sess-box.no { background: var(--cx-lay-pink); color: #450815; }
        .cricx-sess-box.yes { background: var(--cx-back-blue); color: #042440; }

        /* Over Last Digit Meter */
        .cricx-digit-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
            background: var(--cx-card-bg);
            border: 1px solid var(--cx-border);
            margin-bottom: 12px;
        }
        .cricx-digit-table th {
            background: #1c1713;
            padding: 6px 12px;
            font-weight: 800;
            text-align: left;
            border-bottom: 1px solid var(--cx-border);
        }
        .cricx-digit-table td {
            padding: 5px 12px;
            border-bottom: 1px solid rgba(255,255,255,0.04);
        }

        /* ===== 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; }

        .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;
        }
        .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; }

        .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; }

        .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; }
            .cricx-fancy-grid-2col { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>

<!-- 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">
            🏆 CRICKET FANTASY11 IS NOW AVAILABLE IN OUR EXCHANGE 🏆 DREAM BIG WIN BIG 🏆
        </span>
    </div>

    <div class="cricx-user-controls">
        <button class="cricx-btn-rules" onclick="alert('Match Odds & Sessions: Settled at match conclusion by official scorecard.')">
            <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>

<!-- Sports Ribbon Bar -->
<nav class="cricx-nav-ribbon">
    <a href="scores.php" class="cricx-nav-item">HOME</a>
    <a href="scores.php?sport=cricket" class="cricx-nav-item active">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>
</nav>

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

    <!-- Left Sidebar -->
    <aside class="cricx-sidebar">
        <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="#">WORLI MATKA</a></li>
        </ul>

        <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: Game Detail -->
    <main class="cricx-detail-content">

        <!-- Match Title Bar -->
        <div class="cricx-match-topstrip">
            <span><?= htmlspecialchars($match_title) ?></span>
            <span style="font-size:12px;color:var(--cx-gold-light)">09/10/2026 11:30:00</span>
        </div>

        <!-- 2D Pitch Visualizer Simulation -->
        <div class="cricx-pitch-visualizer">
            <div class="cricx-stadium-canvas"></div>
            <div class="cricx-pitch-rect"></div>
            <div class="cricx-player-silhouette-left">🏏</div>
            <div class="cricx-player-silhouette-right">🏃</div>

            <div class="cricx-scoreboard-overlay">
                <!-- Team Scores & Targets -->
                <div class="cricx-teams-score-row">
                    <div class="cricx-team-block">
                        <div class="cricx-team-title">BAN</div>
                        <div class="cricx-team-score-num">346/10 <small style="font-size:13px;color:#9ca3af">(92.3)</small>, 372/7 <small style="font-size:13px;color:#9ca3af">(105.2)</small></div>
                        <span class="cricx-team-crr">CRR: 3.53</span>
                    </div>

                    <div style="text-align:center;">
                        <div class="cricx-match-target-badge">
                            AFG need 347 runs to win<br><span style="color:#fff;font-size:10px;">DAY 4 STUMP</span>
                        </div>
                        <div style="font-size:10px;color:#9ca3af;margin-top:4px;">Last Wicket: Rahmat Shah 57 (124)</div>
                    </div>

                    <div class="cricx-team-block" style="text-align:right;">
                        <div class="cricx-team-title">AFG</div>
                        <div class="cricx-team-score-num">132/3 <small style="font-size:13px;color:#9ca3af">(50.0)</small>, 240/10 <small style="font-size:13px;color:#9ca3af">(89.1)</small></div>
                        <span class="cricx-team-crr">CRR: 2.64</span>
                    </div>
                </div>

                <!-- Ball By Ball Over Strip -->
                <div class="cricx-over-strip">
                    <span style="color:#d1d5db;margin-right:4px;">Over 50</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-1">1</span>
                    <span class="cricx-ball-pill c-1">1</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-0">0</span>

                    <span style="color:#d1d5db;margin:0 4px 0 16px;">Over 49</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-1">1</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-3">3</span>
                    <span class="cricx-ball-pill c-0">0</span>
                    <span class="cricx-ball-pill c-1">1</span>
                </div>

                <!-- Batsmen & Bowler Crease Cards -->
                <div class="cricx-crease-stats-grid">
                    <div>
                        <table class="cricx-stat-table">
                            <thead><tr><th>Batsmen</th><th>R</th><th>B</th><th>4s</th><th>6s</th><th>SR</th></tr></thead>
                            <tbody>
                                <tr>
                                    <td>Hashmatullah Shahidi 🏏</td>
                                    <td>19</td><td>27</td><td>2</td><td>1</td><td>70.37</td>
                                </tr>
                                <tr>
                                    <td>Sediqullah Atal</td>
                                    <td>35</td><td>126</td><td>1</td><td>1</td><td>27.78</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div>
                        <table class="cricx-stat-table">
                            <thead><tr><th>Bowler</th><th>O</th><th>R</th><th>M</th><th>W</th><th>Eco</th></tr></thead>
                            <tbody>
                                <tr>
                                    <td>Mehidy Hasan Miraz</td>
                                    <td>0.0</td><td>0</td><td>0</td><td>0</td><td>0.00</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- 1. MATCH ODDS (3-Tier Back & Lay) -->
        <div class="cricx-market-card">
            <div class="cricx-market-header">
                <div><span>Match Odds</span> <span class="cricx-market-limits">Min: 100 Max: 5k</span></div>
                <i class="fas fa-info-circle" style="color:#9ca3af;"></i>
            </div>
            <table class="cricx-matchodds-table">
                <thead>
                    <tr>
                        <th style="width:40%;text-align:left;padding-left:12px;">Market Runners</th>
                        <th colspan="3" class="cricx-th-back">BACK</th>
                        <th colspan="3" class="cricx-th-lay">LAY</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- Afghanistan -->
                    <tr>
                        <td class="cricx-runner-name-cell">
                            <div>
                                <span>Afghanistan</span>
                            </div>
                            <span class="cricx-live-pnl green" id="pnlRunner1">+258000.00</span>
                        </td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-light" onclick="pickMatchOdd('Afghanistan', 110, 'back')"><span class="cricx-tier-odds">110</span><span class="cricx-tier-vol">1.97</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-mid" onclick="pickMatchOdd('Afghanistan', 120, 'back')"><span class="cricx-tier-odds">120</span><span class="cricx-tier-vol">29.68</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-main" onclick="pickMatchOdd('Afghanistan', 130, 'back')"><span class="cricx-tier-odds">130</span><span class="cricx-tier-vol">13.1</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-main" onclick="pickMatchOdd('Afghanistan', 150, 'lay')"><span class="cricx-tier-odds">150</span><span class="cricx-tier-vol">13.17</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-mid" onclick="pickMatchOdd('Afghanistan', 160, 'lay')"><span class="cricx-tier-odds">160</span><span class="cricx-tier-vol">22.55</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-light" onclick="pickMatchOdd('Afghanistan', 170, 'lay')"><span class="cricx-tier-odds">170</span><span class="cricx-tier-vol">13.94</span></div></td>
                    </tr>

                    <!-- Bangladesh -->
                    <tr>
                        <td class="cricx-runner-name-cell">
                            <div>
                                <span>Bangladesh</span>
                            </div>
                            <span class="cricx-live-pnl red" id="pnlRunner2">-2000.00</span>
                        </td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-light" onclick="pickMatchOdd('Bangladesh', 1.07, 'back')"><span class="cricx-tier-odds">1.07</span><span class="cricx-tier-vol">5353.6</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-mid" onclick="pickMatchOdd('Bangladesh', 1.08, 'back')"><span class="cricx-tier-odds">1.08</span><span class="cricx-tier-vol">60832.5</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-main" onclick="pickMatchOdd('Bangladesh', 1.09, 'back')"><span class="cricx-tier-odds">1.09</span><span class="cricx-tier-vol">78109.7</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-main" onclick="pickMatchOdd('Bangladesh', 1.10, 'lay')"><span class="cricx-tier-odds">1.10</span><span class="cricx-tier-vol">18495.3</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-mid" onclick="pickMatchOdd('Bangladesh', 1.11, 'lay')"><span class="cricx-tier-odds">1.11</span><span class="cricx-tier-vol">12880.9</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-light" onclick="pickMatchOdd('Bangladesh', 1.12, 'lay')"><span class="cricx-tier-odds">1.12</span><span class="cricx-tier-vol">6975.5</span></div></td>
                    </tr>

                    <!-- The Draw -->
                    <tr>
                        <td class="cricx-runner-name-cell">
                            <div>
                                <span>The Draw</span>
                            </div>
                            <span class="cricx-live-pnl red" id="pnlRunner3">-2000.00</span>
                        </td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-light" onclick="pickMatchOdd('The Draw', 10.5, 'back')"><span class="cricx-tier-odds">10.5</span><span class="cricx-tier-vol">970.3</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-mid" onclick="pickMatchOdd('The Draw', 11, 'back')"><span class="cricx-tier-odds">11</span><span class="cricx-tier-vol">143.1</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box back-main" onclick="pickMatchOdd('The Draw', 11.5, 'back')"><span class="cricx-tier-odds">11.5</span><span class="cricx-tier-vol">2303.8</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-main" onclick="pickMatchOdd('The Draw', 12.5, 'lay')"><span class="cricx-tier-odds">12.5</span><span class="cricx-tier-vol">1353.6</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-mid" onclick="pickMatchOdd('The Draw', 13, 'lay')"><span class="cricx-tier-odds">13</span><span class="cricx-tier-vol">1144.5</span></div></td>
                        <td style="padding:1px;"><div class="cricx-tier-box lay-light" onclick="pickMatchOdd('The Draw', 13.5, 'lay')"><span class="cricx-tier-odds">13.5</span><span class="cricx-tier-vol">412.9</span></div></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- 2. BOOKMAKER MARKET -->
        <div class="cricx-market-card">
            <div class="cricx-market-header">
                <div><span>BOOKMAKER</span> <span class="cricx-market-limits">Min: 100 Max: 100k</span></div>
                <i class="fas fa-info-circle" style="color:#9ca3af;"></i>
            </div>
            <div class="cricx-suspended-wrap">
                <div class="cricx-suspended-overlay">SUSPENDED</div>
                <table class="cricx-matchodds-table">
                    <thead><tr><th style="width:40%;text-align:left;padding-left:12px;">Nation</th><th class="cricx-th-back">BACK</th><th class="cricx-th-lay">LAY</th></tr></thead>
                    <tbody>
                        <tr><td style="padding:8px 12px;">Afghanistan</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">-</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">-</div></td></tr>
                        <tr><td style="padding:8px 12px;">Bangladesh</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">8</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">11</div></td></tr>
                        <tr><td style="padding:8px 12px;">The Draw</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">-</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">-</div></td></tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 3. MINI BOOKMAKER -->
        <div class="cricx-market-card">
            <div class="cricx-market-header">
                <div><span>MINI BOOKMAKER</span> <span class="cricx-market-limits">Min: 100 Max: 50k</span></div>
                <i class="fas fa-info-circle" style="color:#9ca3af;"></i>
            </div>
            <div class="cricx-suspended-wrap">
                <div class="cricx-suspended-overlay">SUSPENDED</div>
                <table class="cricx-matchodds-table">
                    <thead><tr><th style="width:40%;text-align:left;padding-left:12px;">Nation</th><th class="cricx-th-back">BACK</th><th class="cricx-th-lay">LAY</th></tr></thead>
                    <tbody>
                        <tr><td style="padding:8px 12px;">Afghanistan</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">-</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">-</div></td></tr>
                        <tr><td style="padding:8px 12px;">Bangladesh</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">8</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">9</div></td></tr>
                        <tr><td style="padding:8px 12px;">The Draw</td><td style="text-align:center;"><div class="cricx-tier-box back-main" style="width:100%;">-</div></td><td style="text-align:center;"><div class="cricx-tier-box lay-main" style="width:100%;">-</div></td></tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 4. FANCY & SESSIONS SECTION -->
        <div class="cricx-fancy-tabs-bar">
            <a href="#" class="cricx-fancy-tab active">FANCY</a>
            <a href="#" class="cricx-fancy-tab" style="background:#0c4e38;">PREMIUM NEW</a>
        </div>
        <div class="cricx-fancy-subtabs">
            <span class="cricx-subtab active">ALL</span>
            <span class="cricx-subtab">SESSIONS</span>
            <span class="cricx-subtab">W/P MARKET</span>
            <span class="cricx-subtab">ODD/EVEN</span>
            <span class="cricx-subtab">XTRA MARKET</span>
            <span class="cricx-subtab">METER</span>
            <span class="cricx-subtab">KHADDA</span>
        </div>

        <div class="cricx-fancy-grid-2col">
            <!-- Sessions Column -->
            <table class="cricx-session-table">
                <thead>
                    <tr>
                        <th style="text-align:left;width:55%;">sessions</th>
                        <th style="width:22%;background:var(--cx-lay-pink);color:#450815;">No</th>
                        <th style="width:23%;background:var(--cx-back-blue);color:#042440;">Yes</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <div style="font-weight:700;">55 Over Runs AFG (2)</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 25k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('55 Over Runs AFG (2) - NO', 1.90, 'lay')">
                                <span>144</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('55 Over Runs AFG (2) - YES', 1.90, 'back')">
                                <span>146</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="font-weight:700;">2nd Inning Runs AFG</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 25k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('2nd Inning Runs AFG - NO', 1.90, 'lay')">
                                <span>272</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('2nd Inning Runs AFG - YES', 1.90, 'back')">
                                <span>275</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>

            <!-- W/P Market Column -->
            <table class="cricx-session-table">
                <thead>
                    <tr>
                        <th style="text-align:left;width:55%;">w/p market</th>
                        <th style="width:22%;background:var(--cx-lay-pink);color:#450815;">No</th>
                        <th style="width:23%;background:var(--cx-back-blue);color:#042440;">Yes</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <div style="font-weight:700;">Fall of 4th wkt Runs AFG (2)</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 25k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('Fall of 4th wkt - NO', 1.90, 'lay')">
                                <span>154</span><span style="font-size:9px;opacity:0.8;">110</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('Fall of 4th wkt - YES', 1.90, 'back')">
                                <span>154</span><span style="font-size:9px;opacity:0.8;">90</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="font-weight:700;">S Atal Runs (2)</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 25k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('S Atal Runs - NO', 1.90, 'lay')">
                                <span>59</span><span style="font-size:9px;opacity:0.8;">110</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('S Atal Runs - YES', 1.90, 'back')">
                                <span>59</span><span style="font-size:9px;opacity:0.8;">90</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="font-weight:700;">H Shahidi Runs (2)</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 25k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('H Shahidi Runs - NO', 1.90, 'lay')">
                                <span>39</span><span style="font-size:9px;opacity:0.8;">110</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('H Shahidi Runs - YES', 1.90, 'back')">
                                <span>39</span><span style="font-size:9px;opacity:0.8;">90</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div style="font-weight:700;">S Atal Boundaries (2)</div>
                            <div style="font-size:10px;color:#9ca3af;">Min: 100 Max: 10k</div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box no" onclick="pickMatchOdd('S Atal Boundaries - NO', 1.90, 'lay')">
                                <span>5</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                        <td style="text-align:center;padding:2px;">
                            <div class="cricx-sess-box yes" onclick="pickMatchOdd('S Atal Boundaries - YES', 1.90, 'back')">
                                <span>6</span><span style="font-size:9px;opacity:0.8;">100</span>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- 5. OVER LAST DIGIT METER MARKET -->
        <table class="cricx-digit-table">
            <thead>
                <tr>
                    <th>60 Over AFG (2) Last Digit <span class="cricx-market-limits" style="margin-left:8px;">Min: 100 Max: 50k</span></th>
                    <th style="width:100px;text-align:center;background:var(--cx-back-blue);color:#042440;">BACK</th>
                </tr>
            </thead>
            <tbody>
                <?php for($d = 0; $d <= 9; $d++): ?>
                <tr>
                    <td style="font-weight:700;"><?= $d ?> Number</td>
                    <td style="text-align:center;padding:2px;">
                        <div class="cricx-tier-box back-main" style="width:100%;height:30px;" onclick="pickMatchOdd('60 Over Digit <?= $d ?>', 9.80, 'back')">
                            <span class="cricx-tier-odds">9.80</span>
                        </div>
                    </td>
                </tr>
                <?php endfor; ?>
            </tbody>
        </table>

        <!-- 70 Over Last Digit -->
        <table class="cricx-digit-table">
            <thead>
                <tr>
                    <th>70 Over AFG (2) Last Digit <span class="cricx-market-limits" style="margin-left:8px;">Min: 100 Max: 50k</span></th>
                    <th style="width:100px;text-align:center;background:var(--cx-back-blue);color:#042440;">BACK</th>
                </tr>
            </thead>
            <tbody>
                <?php for($d = 0; $d <= 4; $d++): ?>
                <tr>
                    <td style="font-weight:700;"><?= $d ?> Number</td>
                    <td style="text-align:center;padding:2px;">
                        <div class="cricx-tier-box back-main" style="width:100%;height:30px;" onclick="pickMatchOdd('70 Over Digit <?= $d ?>', 9.80, 'back')">
                            <span class="cricx-tier-odds">9.80</span>
                        </div>
                    </td>
                </tr>
                <?php endfor; ?>
            </tbody>
        </table>

    </main>

    <!-- Right Sidebar (Interactive Bet Slip & My Bets) -->
    <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">
            <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 - EVEN</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="updateLivePnL()">
                    </div>
                    <div class="cricx-input-group">
                        <label>Stake</label>
                        <input type="number" id="slipStakeInput" value="2000" oninput="updateLivePnL()">
                    </div>
                </div>

                <div class="cricx-profit-display">
                    <span>Potential Profit:</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">
                    <?php if (!empty($open_bets)): ?>
                        <?php foreach (array_slice($open_bets, 0, 5) as $b): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($b['runner_name']) ?></strong><br><small style="color:#9ca3af"><?= strtoupper($b['bet_type']) ?></small></td>
                            <td style="color:var(--cx-gold)"><?= $b['odds'] ?></td>
                            <td>₹<?= number_format($b['stake']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                    <tr><td colspan="3" style="text-align:center;color:#6b7280;padding:20px;">No records Found</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </aside>

</div>

<script>
let currentBet = {
    match_id: <?= $id ?>,
    match_name: '<?= addslashes($match_title) ?>',
    runner_name: 'Afghanistan',
    bet_type: 'back',
    odds: 130,
    stake: 2000
};

function pickMatchOdd(runner, odds, betType) {
    currentBet.runner_name = runner;
    currentBet.odds = parseFloat(odds) || 1.50;
    currentBet.bet_type = betType;

    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';
    }

    updateLivePnL();
}

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

function updateLivePnL() {
    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;
    }

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

    // Update real-time runner P&L tags on Match Odds rows
    const pnl1 = document.getElementById('pnlRunner1');
    const pnl2 = document.getElementById('pnlRunner2');
    const pnl3 = document.getElementById('pnlRunner3');

    if (currentBet.runner_name.includes('Afghanistan')) {
        pnl1.innerText = '+₹' + profit.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl1.className = 'cricx-live-pnl green';
        pnl2.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl2.className = 'cricx-live-pnl red';
        pnl3.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl3.className = 'cricx-live-pnl red';
    } else if (currentBet.runner_name.includes('Bangladesh')) {
        pnl2.innerText = '+₹' + profit.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl2.className = 'cricx-live-pnl green';
        pnl1.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl1.className = 'cricx-live-pnl red';
        pnl3.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl3.className = 'cricx-live-pnl red';
    } else if (currentBet.runner_name.includes('Draw')) {
        pnl3.innerText = '+₹' + profit.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl3.className = 'cricx-live-pnl green';
        pnl1.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl1.className = 'cricx-live-pnl red';
        pnl2.innerText = '-₹' + stake.toLocaleString('en-IN', {minimumFractionDigits:2});
        pnl2.className = 'cricx-live-pnl red';
    }
}

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

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 valid 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
updateLivePnL();
</script>

</body>
</html>
