<?php
session_start();
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
if (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) {
    header('Location: /login.php');
    exit;
}
require_once __DIR__ . '/../config.php';
function e($str) { return htmlspecialchars((string)$str); }
$pdo = $GLOBALS['pdo'];
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user) {
    header('Location: /login.php');
    exit;
}
if ((int)($user['is_demo'] ?? 0) === 1) {
    $_SESSION['is_demo'] = 1;
    header('Location: ../index.php?err=demo_restriction');
    exit;
}
$balance = (float)$user['balance'];

$minBetUsd   = 10;
$cur         = 'Rs';
$fxRate      = 1;
$minBetLocal = $minBetUsd;
$balLocal    = number_format($balance, 2);

$jsData = [
    'csrf'        => '',
    'balUsd'      => $balance,
    'currency'    => $cur,
    'fxRate'      => $fxRate,
    'minBetUsd'   => $minBetUsd,
    'minBetLocal' => $minBetLocal,
];

$PAGE_CSS = <<<'CSS'
/* AAA Golden Royal & Dark Neon Theme */
:root {
    --bg-base: #050505;
    --bg-card: #121214;
    --gold-light: #FFDF00;
    --gold-main: #D4AF37;
    --gold-dark: #996515;
    --neon-red: #FF2A54;
    --neon-red-dark: #990011;
    --neon-blue: #00E5FF;
    --neon-blue-dark: #00509E;
    --border-subtle: rgba(255, 255, 255, 0.08);
    --text-main: #F8F8F8;
    --text-muted: #8A8A93;
    --radius-lg: 20px;
    --radius-md: 14px;
}

/* Wallet Section - Premium VIP Card Look */
.g-bal {
    background: linear-gradient(135deg, #1A1A1D, #000000);
    border-radius: var(--radius-lg);
    padding: 20px 16px;
    color: var(--gold-light);
    margin-bottom: 16px;
    position: relative;
    overflow: hidden;
    border: 1px solid rgba(212, 175, 55, 0.3);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8), inset 0 0 20px rgba(212, 175, 55, 0.05);
}
.g-bal::after {
    content: ''; position: absolute; top: -50%; right: -20%; width: 150px; height: 150px;
    background: radial-gradient(circle, rgba(212,175,55,0.15) 0%, transparent 70%); pointer-events: none;
}
.g-bal-amt { font-size: 28px; font-weight: 900; text-align: center; position: relative; z-index: 1; text-shadow: 0 2px 10px rgba(255, 223, 0, 0.4); letter-spacing: 1px; }
.g-bal-lbl { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1.5px; text-align: center; margin-top: 4px; position: relative; z-index: 1; }
.g-bal-row { display: flex; gap: 12px; margin-top: 16px; position: relative; z-index: 1; }
.g-bal-btn { flex: 1; border: none; border-radius: 12px; padding: 12px 0; font-size: 13px; font-weight: 800; cursor: pointer; text-decoration: none; text-align: center; transition: all 0.2s; text-transform: uppercase; letter-spacing: 1px; }
.g-bal-btn.w { background: rgba(255,255,255,0.05); color: var(--text-main); border: 1px solid var(--border-subtle); }
.g-bal-btn.w:active { background: rgba(255,255,255,0.1); }
.g-bal-btn.d { background: linear-gradient(180deg, var(--gold-light), var(--gold-dark)); color: #000; box-shadow: 0 4px 15px rgba(212, 175, 55, 0.4); }
.g-bal-btn.d:active { transform: translateY(2px); box-shadow: 0 2px 5px rgba(212, 175, 55, 0.4); }

/* Main Game Shell */
.lg-shell { background: var(--bg-card); border-radius: var(--radius-lg); box-shadow: 0 10px 40px rgba(0,0,0,0.9); border: 1px solid var(--border-subtle); margin-bottom: 16px; overflow: hidden; position: relative; }
.lg-tbar { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; background: rgba(0,0,0,0.4); border-bottom: 1px solid var(--border-subtle); }
.lg-tbar-right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.lg-tbar-label { font-size: 9px; font-weight: 800; color: var(--gold-main); text-transform: uppercase; letter-spacing: 1px; }

/* Glowing Timer */
.lg-timer { display: flex; align-items: center; gap: 4px; }
.lg-td { width: 32px; height: 40px; border-radius: 8px; background: #000; border: 1px solid rgba(255,255,255,0.1); color: var(--gold-light); display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 900; font-family: monospace; box-shadow: inset 0 0 10px rgba(212, 175, 55, 0.2); text-shadow: 0 0 8px rgba(255, 223, 0, 0.6); }
.lg-td.flip { animation: lgFlip 0.3s cubic-bezier(0.4, 0, 0.2, 1); }
@keyframes lgFlip { 0% { transform: rotateX(-90deg); filter: brightness(2); } 100% { transform: rotateX(0); filter: brightness(1); } }
.lg-sep { font-size: 20px; font-weight: 900; color: var(--gold-dark); animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

.lg-body { padding: 16px; }
.lg-info-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid rgba(255,255,255,0.05); }
.lg-pinfo { display: flex; flex-direction: column; gap: 2px; }
.lg-pmode { font-size: 13px; font-weight: 900; color: var(--text-main); display: flex; align-items: center; gap: 6px; text-transform: uppercase; letter-spacing: 1px; }
.lg-pmode i { color: var(--gold-main); }
.lg-pnum { font-size: 12px; color: var(--gold-light); font-weight: 800; letter-spacing: 1px; margin-top: 2px; background: rgba(212,175,55,0.1); padding: 2px 6px; border-radius: 4px; display: inline-block;}

/* History Dots */
.lg-dots-wrap { display: flex; align-items: center; gap: 6px; background: rgba(0,0,0,0.5); padding: 4px 8px; border-radius: 999px; border: 1px solid var(--border-subtle); }
.lg-dot { width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 10px; font-weight: 900; color: #fff; box-shadow: 0 0 10px rgba(0,0,0,0.5); }
.lg-dot.r { background: var(--neon-red); box-shadow: 0 0 8px rgba(255, 42, 84, 0.6); }
.lg-dot.b { background: var(--neon-blue); box-shadow: 0 0 8px rgba(0, 229, 255, 0.6); }

/* Lock Bar */
.lg-lock-bar { display: none; align-items: center; justify-content: center; gap: 8px; padding: 10px; margin-bottom: 16px; border-radius: 12px; font-size: 12px; font-weight: 800; background: rgba(255, 0, 0, 0.1); border: 1px solid rgba(255, 42, 84, 0.3); color: var(--neon-red); text-transform: uppercase; letter-spacing: 1px; backdrop-filter: blur(4px); }
.lg-lock-bar.on { display: flex; animation: slideDown 0.3s ease; }
@keyframes slideDown { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* The Stage & Card Flip */
.lg-viewport { display: flex; flex-direction: column; align-items: center; margin-bottom: 24px; position: relative; }
.lg-viewport::before { content: ''; position: absolute; bottom: 15px; width: 160px; height: 20px; background: radial-gradient(ellipse, rgba(212,175,55,0.3) 0%, transparent 70%); filter: blur(5px); z-index: 0; }
.lg-vp-label { margin-top: 16px; font-size: 11px; font-weight: 800; color: var(--text-muted); text-transform: uppercase; letter-spacing: 2px; }
.lg-viewport.reveal .lg-vp-label { color: var(--gold-light); text-shadow: 0 0 10px rgba(255, 223, 0, 0.5); }

.lg-vp-card { width: 150px; aspect-ratio: 2.5/3.5; max-height: 230px; margin: 0 auto; perspective: 1200px; z-index: 1; }
.lg-vp-card-inner { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
.lg-viewport.reveal .lg-vp-card-inner { transform: rotateY(180deg) scale(1.05); }

/* Premium Card Back */
.lg-vp-card-back, .lg-vp-card-front { position: absolute; inset: 0; border-radius: 14px; backface-visibility: hidden; -webkit-backface-visibility: hidden; overflow: hidden; box-shadow: 0 15px 35px rgba(0,0,0,0.8); border: 2px solid #222; }
.lg-vp-card-back { background: radial-gradient(circle at 50% 50%, #1a1a1a 0%, #000 100%); border-color: var(--gold-dark); }
.lg-vp-card-back .card-back-pattern { position: absolute; inset: 4px; background: repeating-linear-gradient(45deg, transparent 0, transparent 10px, rgba(212,175,55,0.05) 10px, rgba(212,175,55,0.05) 12px); border-radius: 10px; border: 1px solid rgba(212,175,55,0.2); }
.lg-vp-card-back .card-back-center { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); width: 60px; height: 60px; background: linear-gradient(135deg, var(--gold-main), var(--gold-dark)); border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 0 20px rgba(212,175,55,0.4); border: 2px solid #fff; }
.lg-vp-card-back .card-back-center span { font-size: 32px; font-weight: 900; color: #000; }

/* Metallic Card Front */
.lg-vp-card-front { transform: rotateY(180deg); display: flex; align-items: center; justify-content: center; background: #111; }
.lg-vp-card-front.vp-red { background: radial-gradient(circle at 50% 50%, var(--neon-red) 0%, var(--neon-red-dark) 100%); border-color: #ff8fa3; box-shadow: 0 0 30px rgba(255, 42, 84, 0.4); }
.lg-vp-card-front.vp-blue { background: radial-gradient(circle at 50% 50%, var(--neon-blue) 0%, var(--neon-blue-dark) 100%); border-color: #8ce8ff; box-shadow: 0 0 30px rgba(0, 229, 255, 0.4); }
.lg-vp-card-front::before { content: ''; position: absolute; inset: 6px; border: 2px solid rgba(255,255,255,0.4); border-radius: 8px; }
.lg-vp-card-front .card-index { position: absolute; font-size: 12px; font-weight: 900; color: #fff; text-shadow: 0 2px 4px rgba(0,0,0,0.8); letter-spacing: 1px; }
.lg-vp-card-front .card-index-tl { top: 12px; left: 12px; }
.lg-vp-card-front .card-index-br { bottom: 12px; right: 12px; transform: rotate(180deg); }
.lg-vp-card-front .vp-car { display: flex; align-items: center; justify-content: center; z-index: 2; }
.lg-vp-card-front .vp-car-icon { font-size: 64px; color: #fff; filter: drop-shadow(0 10px 10px rgba(0,0,0,0.6)); }

/* AAA Betting Buttons */
.lg-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.lg-btn { position: relative; border-radius: 16px; padding: 20px 10px; display: flex; flex-direction: column; align-items: center; gap: 6px; cursor: pointer; border: none; font-family: inherit; transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1); overflow: hidden; background: #111; }
.lg-btn::before { content: ''; position: absolute; inset: 0; border-radius: 16px; padding: 2px; -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0); -webkit-mask-composite: xor; mask-composite: exclude; pointer-events: none; }
.lg-btn:active { transform: translateY(4px) scale(0.98); }
.lg-btn.r { background: linear-gradient(180deg, rgba(255, 42, 84, 0.1) 0%, rgba(153, 0, 17, 0.4) 100%); box-shadow: 0 10px 20px rgba(0,0,0,0.5); }
.lg-btn.r::before { background: linear-gradient(180deg, var(--neon-red), transparent); }
.lg-btn.r:active { box-shadow: 0 2px 5px rgba(0,0,0,0.5), inset 0 0 20px rgba(255, 42, 84, 0.2); }
.lg-btn.r .lg-btn-ico { color: var(--neon-red); filter: drop-shadow(0 0 8px rgba(255, 42, 84, 0.6)); }

.lg-btn.b { background: linear-gradient(180deg, rgba(0, 229, 255, 0.1) 0%, rgba(0, 80, 158, 0.4) 100%); box-shadow: 0 10px 20px rgba(0,0,0,0.5); }
.lg-btn.b::before { background: linear-gradient(180deg, var(--neon-blue), transparent); }
.lg-btn.b:active { box-shadow: 0 2px 5px rgba(0,0,0,0.5), inset 0 0 20px rgba(0, 229, 255, 0.2); }
.lg-btn.b .lg-btn-ico { color: var(--neon-blue); filter: drop-shadow(0 0 8px rgba(0, 229, 255, 0.6)); }

.lg-btn-ico { font-size: 28px; z-index: 1; transition: transform 0.2s; }
.lg-btn:hover .lg-btn-ico { transform: scale(1.1); }
.lg-btn-name { font-size: 18px; font-weight: 900; text-transform: uppercase; letter-spacing: 2px; z-index: 1; color: #fff; }
.lg-btn-sub { font-size: 10px; font-weight: 800; color: var(--gold-main); z-index: 1; text-transform: uppercase; letter-spacing: 1px; }
.lg-btn.locked { opacity: 0.3; pointer-events: none; filter: grayscale(0.8); }

/* Result Text */
.lg-result { text-align: center; font-size: 14px !important; font-weight: 800 !important; background: rgba(0,0,0,0.4) !important; border: 1px solid var(--border-subtle) !important; padding: 14px !important; border-radius: 12px; margin-top: 16px !important; }

/* Modals - Glassmorphism */
.lg-modal { position: fixed; inset: 0; background: rgba(0,0,0,0.8); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); display: none; align-items: flex-end; justify-content: center; z-index: 400; }
.lg-modal.show { display: flex; }
.lg-mbox { background: linear-gradient(180deg, #1A1A1D 0%, #050505 100%); border-radius: 24px 24px 0 0; width: 100%; max-width: 460px; padding: 24px 20px 30px; animation: lgMUp 0.4s cubic-bezier(0.16, 1, 0.3, 1); border-top: 1px solid rgba(255,255,255,0.1); box-shadow: 0 -10px 40px rgba(0,0,0,0.8); }
.lg-mhd { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.lg-mhd-t { font-size: 18px; font-weight: 900; color: #fff; display: flex; align-items: center; gap: 8px; text-transform: uppercase; letter-spacing: 1px; }
.lg-mhd-t .lg-cdot { width: 12px; height: 12px; border-radius: 50%; box-shadow: 0 0 10px currentColor; }
.lg-mx { background: rgba(255,255,255,0.1); border: none; color: #fff; width: 36px; height: 36px; border-radius: 50%; font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: background 0.2s; }
.lg-mx:active { background: rgba(255,255,255,0.2); }
.lg-mbal { font-size: 13px; color: var(--text-muted); margin-bottom: 16px; background: rgba(0,0,0,0.3); padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border-subtle); display: flex; justify-content: space-between;}
.lg-mbal b { color: var(--gold-light); font-size: 14px; }
.lg-mf { margin-bottom: 16px; }
.lg-mf label { display: block; font-size: 11px; font-weight: 800; color: var(--text-muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 1px; }
.lg-mf input { width: 100%; border-radius: 12px; border: 2px solid rgba(255,255,255,0.1); padding: 14px; font-size: 18px; font-weight: 900; background: rgba(0,0,0,0.5); color: var(--gold-light); outline: none; transition: border-color 0.2s; font-family: monospace; }
.lg-mf input:focus { border-color: var(--gold-main); box-shadow: 0 0 15px rgba(212, 175, 55, 0.2); }
.lg-mhint { font-size: 11px; color: var(--text-muted); margin-top: 6px; font-weight: 600; }
.lg-mchips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.lg-mc { flex: 1; min-width: 60px; border: 1px solid rgba(255,255,255,0.1); border-radius: 10px; padding: 10px 0; font-size: 14px; font-weight: 800; background: rgba(0,0,0,0.3); color: var(--text-main); cursor: pointer; transition: all 0.2s; text-align: center; }
.lg-mc:active { transform: scale(0.95); }
.lg-mc.on { background: linear-gradient(135deg, var(--gold-light), var(--gold-dark)); color: #000; border-color: transparent; box-shadow: 0 4px 15px rgba(212, 175, 55, 0.3); }
.lg-mgo { width: 100%; border-radius: 14px; border: none; padding: 16px; font-size: 16px; font-weight: 900; color: #fff; cursor: pointer; text-transform: uppercase; letter-spacing: 2px; box-shadow: 0 10px 20px rgba(0,0,0,0.4); transition: transform 0.1s; }
.lg-mgo:active { transform: translateY(2px); box-shadow: 0 2px 5px rgba(0,0,0,0.4); }
.lg-mgo.r { background: linear-gradient(90deg, var(--neon-red), var(--neon-red-dark)); }
.lg-mgo.b { background: linear-gradient(90deg, var(--neon-blue), var(--neon-blue-dark)); }

/* Tabs & Tables */
.g-pills { display: flex; gap: 10px; margin: 20px 0 16px; background: rgba(0,0,0,0.4); padding: 6px; border-radius: 14px; border: 1px solid var(--border-subtle); }
.g-pill { flex: 1; border: none; border-radius: 10px; padding: 12px 0; font-size: 13px; font-weight: 800; cursor: pointer; background: transparent; color: var(--text-muted); transition: all 0.2s; text-transform: uppercase; letter-spacing: 1px; }
.g-pill.on { background: linear-gradient(135deg, #222, #111); color: var(--gold-light); box-shadow: 0 4px 10px rgba(0,0,0,0.5); border: 1px solid rgba(212,175,55,0.3); }

.g-htable, .g-trades { background: var(--bg-card); border-radius: 16px; overflow: hidden; border: 1px solid var(--border-subtle); box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
.g-hdr { display: grid; grid-template-columns: 1.6fr 0.8fr 0.5fr; padding: 14px 16px; background: #111; color: var(--gold-main); font-size: 11px; font-weight: 900; text-transform: uppercase; letter-spacing: 1px; border-bottom: 1px solid var(--border-subtle); }
.g-hr { display: grid; grid-template-columns: 1.6fr 0.8fr 0.5fr; padding: 14px 16px; border-bottom: 1px solid rgba(255,255,255,0.03); font-size: 13px; align-items: center; }
.g-hr:last-child { border-bottom: none; }
.g-hr-pn { font-weight: 700; color: var(--text-main); font-family: monospace; }
.g-hr-winner { font-weight: 900; text-align: center; text-transform: uppercase; }
.g-hr-winner.r { color: var(--neon-red); text-shadow: 0 0 8px rgba(255,42,84,0.3); }
.g-hr-winner.b { color: var(--neon-blue); text-shadow: 0 0 8px rgba(0,229,255,0.3); }
.g-hr-dot { display: flex; justify-content: center; }
.g-cdot { width: 12px; height: 12px; border-radius: 50%; box-shadow: 0 0 8px currentColor; }
.g-cdot.r { background: var(--neon-red); color: var(--neon-red); }
.g-cdot.b { background: var(--neon-blue); color: var(--neon-blue); }

.g-tcard { display: flex; align-items: center; gap: 14px; padding: 16px; border-bottom: 1px solid rgba(255,255,255,0.03); }
.g-tpill { width: 44px; height: 44px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 18px; font-weight: 900; color: #fff; box-shadow: 0 4px 10px rgba(0,0,0,0.3); }
.g-tpill.r { background: linear-gradient(135deg, var(--neon-red), var(--neon-red-dark)); }
.g-tpill.b { background: linear-gradient(135deg, var(--neon-blue), var(--neon-blue-dark)); }
.g-tmid { flex: 1; min-width: 0; }
.g-tmid-pn { font-size: 14px; font-weight: 800; color: var(--text-main); font-family: monospace; }
.g-tmid-date { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.g-tbadge { display: inline-block; font-size: 10px; font-weight: 800; padding: 4px 10px; border-radius: 6px; margin-bottom: 6px; text-transform: uppercase; letter-spacing: 1px; }
.g-tbadge.win { background: rgba(212,175,55,0.1); color: var(--gold-light); border: 1px solid rgba(212,175,55,0.3); }
.g-tbadge.lose { background: rgba(255,255,255,0.05); color: var(--text-muted); border: 1px solid var(--border-subtle); }
.g-tbadge.pend { background: rgba(0,229,255,0.1); color: var(--neon-blue); border: 1px solid rgba(0,229,255,0.3); }
.g-tamt { font-size: 14px; font-weight: 900; }
.g-tamt.w { color: #22C55E; }
.g-tamt.l { color: var(--neon-red); }
.g-tamt.p { color: var(--gold-main); }

/* Toasts */
.lg-toast { position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%) translateY(20px); background: rgba(10,10,10,0.9); border: 1px solid var(--gold-dark); color: var(--gold-light); padding: 14px 28px; border-radius: 999px; font-size: 14px; font-weight: 900; box-shadow: 0 10px 30px rgba(0,0,0,0.8), 0 0 20px rgba(212,175,55,0.2); z-index: 700; opacity: 0; pointer-events: none; transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1); text-transform: uppercase; letter-spacing: 1px; white-space: nowrap; }
.lg-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.lg-toast.error { border-color: var(--neon-red); color: #fff; box-shadow: 0 10px 30px rgba(0,0,0,0.8), 0 0 20px rgba(255, 42, 84, 0.3); }

/* How to play btn */
.lg-how-btn { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 800; color: var(--gold-main); background: rgba(212,175,55,0.1); border: 1px solid rgba(212,175,55,0.3); border-radius: 999px; padding: 6px 14px; cursor: pointer; transition: all 0.2s; text-transform: uppercase; letter-spacing: 0.5px; }
.lg-how-btn:active { background: rgba(212,175,55,0.2); }

/* HTP Overlays */
.lg-htp-ov { position: fixed; inset: 0; background: rgba(0,0,0,0.85); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); display: none; align-items: center; justify-content: center; z-index: 600; padding: 20px; flex-direction: column; gap: 16px; }
.lg-htp-ov.show { display: flex; }
.lg-htp-box { background: linear-gradient(180deg, #1A1A1D 0%, #050505 100%); border-radius: 20px; max-width: 380px; width: 100%; border: 1px solid rgba(255,255,255,0.1); box-shadow: 0 20px 50px rgba(0,0,0,0.9); overflow: hidden; }
.lg-htp-head { padding: 16px; background: #111; border-bottom: 1px solid var(--border-subtle); color: var(--gold-light); text-align: center; font-size: 16px; font-weight: 900; text-transform: uppercase; letter-spacing: 2px; }
.lg-htp-body { padding: 20px; font-size: 14px; line-height: 1.8; color: var(--text-muted); font-weight: 600; }
.lg-htp-body b { color: var(--text-main); }
.lg-htp-close { display: block; width: 100%; padding: 16px; border: none; background: linear-gradient(90deg, var(--gold-dark), var(--gold-main)); color: #000; font-size: 15px; font-weight: 900; cursor: pointer; text-transform: uppercase; letter-spacing: 2px; }

/* Result popup (Image based layout) */
.lg-res-ov{position:fixed;inset:0;background:rgba(0,0,0,.85);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);display:none;flex-direction:column;align-items:center;justify-content:center;z-index:600;padding:16px;gap:10px;}
.lg-res-ov.show{display:flex;}
.lg-res-imgwrap{position:relative;width:100%;max-width:300px;animation:lgResPop .35s ease;filter:drop-shadow(0 20px 30px rgba(0,0,0,0.8));}
@keyframes lgResPop{0%{transform:scale(.8);opacity:0}100%{transform:scale(1);opacity:1}}
.lg-res-imgwrap img{width:100%;display:block;}
.lg-res-txt{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;text-align:center;}
.lg-res-txt .r-top{flex:0 0 38%;display:flex;align-items:flex-end;justify-content:center;padding:0 20px 0;}
.lg-res-txt .r-title{font-size:22px;font-weight:900;color:#fff;}
.lg-res-txt.lose .r-title{color:#334155;padding-top:6px;}
.lg-res-txt .r-mid{flex:0 0 13%;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;padding:0 20px;}
.lg-res-txt .r-sub{font-size:11px;color:rgba(255,255,255,.95);}
.lg-res-txt.lose .r-sub{color:#64748b;}
.lg-res-txt .r-pills{display:flex;gap:6px;align-items:center;}
.lg-res-txt .r-pill{padding:3px 12px;border-radius:999px;font-size:11px;font-weight:800;color:#fff;}
.lg-res-txt .r-pill.red{background:var(--neon-red);}
.lg-res-txt .r-pill.blue{background:var(--neon-blue);}
.lg-res-txt .r-bot{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:flex-start;padding:32px 40px 4px;}
.lg-res-txt .r-label{font-size:10px;color:var(--gold-main);font-weight:700;text-transform:uppercase;letter-spacing:.5px;}
.lg-res-txt .r-amt{font-size:20px;font-weight:900;color:#22c55e;margin-top:2px;}
.lg-res-txt .r-amt.lose{color:#475569;}
.lg-res-txt .r-pn{font-size:9px;color:var(--text-muted);margin-top:4px;line-height:1.3;}
.lg-res-foot{display:flex;align-items:center;gap:8px;color:rgba(255,255,255,.9);font-size:11px;}
.lg-res-foot label{display:flex;align-items:center;gap:5px;cursor:pointer;user-select:none;}
.lg-res-foot input[type=checkbox]{accent-color:var(--gold-main);width:14px;height:14px;cursor:pointer;}
.lg-res-x{width:36px;height:36px;border-radius:50%;border:2px solid rgba(255,255,255,.6);background:none;color:#fff;font-size:18px;cursor:pointer;display:flex;align-items:center;justify-content:center;}
.lg-res-x:active{opacity:.7;}
CSS;

$PAGE_JS = "const CT=" . json_encode($jsData, JSON_UNESCAPED_UNICODE) . ";" . <<<'JS'
(function(){
const $=s=>document.querySelector(s);
let sOff=0,prev='0000',placing=false,lastRes=null,timerRAF=null;
let phase='betting';
let myPage=1,PER_PAGE=8,allMyBets=[];
let toastTimeout;

async function api(a,b){
  const o=b?{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({...b,csrf:CT.csrf})}:{};
  return (await fetch('api.php?action='+a,o)).json();
}
function fmt(u){return CT.currency+' '+Number(u*CT.fxRate).toFixed(2);}
function toUsd(l){return Number(l)/CT.fxRate;}
function now(){return Date.now()-sOff;}

function showToast(msg, isError = false) {
    const toast = $('#lg-toast');
    if (!toast) return;
    clearTimeout(toastTimeout);
    
    if (isError) {
        toast.classList.add('error');
        toast.innerHTML = `<i class="fa-solid fa-circle-exclamation" style="margin-right:6px;"></i> ${msg}`;
    } else {
        toast.classList.remove('error');
        toast.innerHTML = `<i class="fa-solid fa-circle-check" style="color:var(--gold-light); margin-right:6px;"></i> ${msg}`;
    }
    
    toast.classList.add('show');
    toastTimeout = setTimeout(() => toast.classList.remove('show'), 2200);
}

function updBal(){
  const s=fmt(CT.balUsd);
  const a=$('#g-bal');if(a)a.textContent=s;
  const h=document.getElementById('hdr-bal');if(h)h.textContent=s;
}

function setPhase(p){
  phase=p;
}

function renderDots(list){
  const w=$('#g-dots');if(!w)return;
  w.innerHTML='';
  (list||[]).filter(r=>r.result_color).slice(0,5).forEach(r=>{
    const d=document.createElement('span');
    d.className='lg-dot '+(r.result_color==='red'?'r':'b');
    d.textContent=r.result_color==='red'?'R':'B';
    w.appendChild(d);
  });
}

function renderHist(list){
  const b=$('#g-hist');if(!b)return;
  b.innerHTML='';
  if(!list||!list.length){b.innerHTML='<div class="g-empty">No rounds yet</div>';return;}
  list.filter(r=>r.result_color).slice(0,10).forEach(r=>{
    const row=document.createElement('div');row.className='g-hr';
    const c=r.result_color;
    row.innerHTML='<span class="g-hr-pn">'+(r.period_number||'')+'</span><span class="g-hr-winner '+(c==='red'?'r':'b')+'">'+(c==='red'?'Red':'Blue')+'</span><span class="g-hr-dot"><span class="g-cdot '+(c==='red'?'r':'b')+'"></span></span>';
    b.appendChild(row);
  });
}

function renderMy(){
  const w=$('#g-my');if(!w)return;
  w.innerHTML='';
  const total=allMyBets.length;
  const pages=Math.max(1,Math.ceil(total/PER_PAGE));
  if(myPage>pages)myPage=pages;
  const start=(myPage-1)*PER_PAGE;
  const slice=allMyBets.slice(start,start+PER_PAGE);
  if(!slice.length){w.innerHTML='<div class="g-empty">No bets yet</div>';renderPager(1,1);return;}
  const wrap=document.createElement('div');wrap.className='g-trades';
  slice.forEach(t=>{
    const card=document.createElement('div');card.className='g-tcard';
    const c=t.color;const label=c==='red'?'Red':'Blue';
    let badge='<span class="g-tbadge pend">Pending</span>';
    let amtHtml='<span class="g-tamt p">'+fmt(parseFloat(t.amount_usd))+'</span>';
    if(t.settled_at){
      if(Number(t.is_win)===1){badge='<span class="g-tbadge win">Win</span>';amtHtml='<span class="g-tamt w">+'+fmt(parseFloat(t.payout_usd||0))+'</span>';}
      else{badge='<span class="g-tbadge lose">Lose</span>';amtHtml='<span class="g-tamt l">-'+fmt(parseFloat(t.amount_usd))+'</span>';}
    }
    const date=(t.placed_at||'').replace('T',' ').slice(0,16);
    card.innerHTML='<div class="g-tpill '+(c==='red'?'r':'b')+'">'+label[0]+'</div><div class="g-tmid"><div class="g-tmid-pn">'+(t.period_number||'')+'</div><div class="g-tmid-date">'+date+'</div></div><div class="g-tright">'+badge+'<br>'+amtHtml+'</div>';
    wrap.appendChild(card);
  });
  w.appendChild(wrap);
  renderPager(myPage,pages);
}

function renderPager(cur,total){
  const p=$('#g-pager');if(!p)return;
  p.innerHTML='';
  const pb=document.createElement('button');pb.className='g-pg-btn';pb.innerHTML='&lsaquo;';
  pb.disabled=cur<=1;pb.onclick=()=>{myPage--;renderMy();};
  const nm=document.createElement('span');nm.className='g-pg-num';nm.textContent=cur+'/'+total;
  const nb=document.createElement('button');nb.className='g-pg-btn';nb.innerHTML='&rsaquo;';
  nb.disabled=cur>=total;nb.onclick=()=>{myPage++;renderMy();};
  p.appendChild(pb);p.appendChild(nm);p.appendChild(nb);
}

function calcUserResult(periodNumber,myBets){
  const bets=myBets.filter(b=>String(b.period_number||'').trim()===String(periodNumber||'').trim() && b.settled_at);
  if(!bets.length)return {won:false,net:0};
  const staked=bets.reduce((s,b)=>s+parseFloat(b.amount_usd||0),0);
  const payout=bets.reduce((s,b)=>s+parseFloat(b.payout_usd||0),0);
  return {won:payout>staked,net:payout-staked};
}

const WIN_IMG='https://www.fantasygems20.com/assets/png/missningBg-c1f02bcd.webp';
const LOSE_IMG='https://www.fantasygems20.com/assets/png/missningLBg-ca049a47.webp';
const lgAcKey='lg_autoclose';
function isAutoClose(){return localStorage.getItem(lgAcKey)!=='0';}
function initLgAcCheckbox(){const cb=$('#lg-res-ac');if(!cb)return;cb.checked=isAutoClose();cb.addEventListener('change',()=>{localStorage.setItem(lgAcKey,cb.checked?'1':'0');});}
function showResultPopup(roundPn,roundColor,myBets){
  const roundBets=myBets.filter(b=>String(b.period_number||'').trim()===String(roundPn||'').trim() && b.settled_at);
  if(!roundBets.length)return;
  const totalStaked=roundBets.reduce((s,b)=>s+parseFloat(b.amount_usd||0),0);
  const totalPayout=roundBets.reduce((s,b)=>s+parseFloat(b.payout_usd||0),0);
  const won=totalPayout>totalStaked;
  const netAmt=Math.abs(totalPayout-totalStaked);
  const ov=$('#lg-res-ov');if(!ov)return;
  $('#lg-res-img').src=won?WIN_IMG:LOSE_IMG;
  const txt=document.querySelector('.lg-res-txt');
  if(txt){txt.classList.remove('lose');if(!won)txt.classList.add('lose');}
  $('#lg-res-title').textContent=won?'Congratulations':'Sorry';
  const cl=roundColor==='red'?'red':'blue';
  $('#lg-res-pills').innerHTML='<span class="r-pill '+cl+'">'+(cl==='red'?'Red':'Blue')+'</span>';
  $('#lg-res-label').textContent=won?'Bonus':'Lose';
  const amtEl=$('#lg-res-amt');
  if(amtEl){amtEl.className='r-amt'+(won?'':' lose');amtEl.textContent=(won?'+':'-')+fmt(netAmt);}
  const pn=$('#lg-res-pn');if(pn)pn.innerHTML='Period: Cars Cards<br>'+roundPn;
  const cb=$('#lg-res-ac');if(cb)cb.checked=isAutoClose();
  ov.classList.add('show');
  if(ov._iv)clearInterval(ov._iv);
  const cEl=$('#lg-res-cd');
  if(isAutoClose()){
    let sec=3;if(cEl)cEl.textContent=sec+' seconds auto close';
    ov._iv=setInterval(()=>{sec--;if(cEl)cEl.textContent=sec+' seconds auto close';if(sec<=0){clearInterval(ov._iv);closeResult();}},1000);
  }else{if(cEl)cEl.textContent='auto close disabled';}
}
function closeResult(){const ov=$('#lg-res-ov');if(!ov)return;ov.classList.remove('show');if(ov._iv)clearInterval(ov._iv);}

function showReveal(winnerColor,userWon,netAmt){
  setPhase('result');
  const vp=$('#lg-viewport');const vpCar=$('#lg-vp-car');const cardFront=$('#lg-vp-card-front');const idxTl=$('#lg-vp-index-tl');const idxBr=$('#lg-vp-index-br');
  if(vp)vp.classList.add('reveal');
  if(vpCar){vpCar.classList.remove('red','blue');vpCar.classList.add(winnerColor);}
  if(cardFront){cardFront.classList.remove('vp-red','vp-blue');cardFront.classList.add(winnerColor==='red'?'vp-red':'vp-blue');}
  if(idxTl)idxTl.textContent=winnerColor==='red'?'RED':'BLUE';
  if(idxBr)idxBr.textContent=winnerColor==='red'?'RED':'BLUE';
  const res=$('#lg-result');
  if(res){
    if(netAmt>0)res.innerHTML='<span style="color:#22C55E">You won '+fmt(netAmt)+'!</span>';
    else if(netAmt<0)res.innerHTML='<span style="color:var(--neon-red)">You lost '+fmt(Math.abs(netAmt))+'.</span>';
    else res.textContent=(winnerColor==='red'?'Red':'Blue')+' wins this round.';
  }
  setTimeout(()=>sync(true),3200);
}

async function sync(fromReveal){
  try{
    const t0=Date.now();const d=await api('get_state');const t1=Date.now();
    if(d.server_time)sOff=Date.now()-(d.server_time*1000)-(t1-t0)/2;
    CT.balUsd=d.balance_usd??CT.balUsd;CT.fxRate=d.fx_rate??CT.fxRate;
    updBal();renderDots(d.history);renderHist(d.history);
    allMyBets=d.my_bets||[];renderMy();
    if(d.period){const pn=$('#g-pnum');if(pn)pn.textContent=d.period.number;startTimer(d.period);}
    if(d.history&&d.history.length){
      const top=d.history[0];
      if(top.result_color&&top.period_number!==lastRes){
        lastRes=top.period_number;
        const roundBets=(d.my_bets||[]).filter(b=>String(b.period_number||'').trim()===String(top.period_number||'').trim() && b.settled_at);
        if(roundBets.length)showResultPopup(top.period_number,top.result_color,d.my_bets||[]);
      }
    }
  }catch(e){console.error(e);}
}

function startTimer(p){
  const endMs=p.end*1000;
  if(timerRAF)cancelAnimationFrame(timerRAF);
  function tick(){
    const n=now();let rem=Math.max(0,Math.ceil((endMs-n)/1000));
    const mm=String(Math.floor(rem/60)).padStart(2,'0');
    const ss=String(rem%60).padStart(2,'0');
    const s=mm+ss;
    [0,1,2,3].forEach(i=>{
      const el=$('#td'+i);
      if(el&&s[i]!==prev[i]){el.textContent=s[i];el.classList.remove('flip');void el.offsetWidth;el.classList.add('flip');}
    });
    prev=s;
    const lockBar=$('#g-lock-bar');const btns=document.querySelectorAll('.lg-btn');
    if(rem>5){
      setPhase('betting');
      if(lockBar)lockBar.classList.remove('on');
      btns.forEach(b=>b.classList.remove('locked'));
      const vp=$('#lg-viewport');if(vp)vp.classList.remove('reveal');
      const res=$('#lg-result');if(res)res.textContent='';
    }else if(rem>0){
      setPhase('locked');
      if(lockBar){lockBar.classList.add('on');const se=$('#g-lock-sec');if(se)se.textContent=mm+':'+ss;}
      btns.forEach(b=>b.classList.add('locked'));
    }
    if(rem>0)timerRAF=requestAnimationFrame(tick);
    else{
      if(lockBar)lockBar.classList.remove('on');
      handleRoundEnd(p.number);
    }
  }
  tick();
}

async function handleRoundEnd(periodNumber){
  try{
    await api('settle');
    const d=await api('get_state');
    CT.balUsd=d.balance_usd??CT.balUsd;CT.fxRate=d.fx_rate??CT.fxRate;
    updBal();renderDots(d.history);renderHist(d.history);
    allMyBets=d.my_bets||[];renderMy();
    let winner='red';
    if(d.history&&d.history.length){const top=d.history[0];winner=top.result_color||'red';lastRes=top.period_number;}
    const {won,net}=calcUserResult(periodNumber,allMyBets);
    showReveal(winner,won,net);
    const roundBets=allMyBets.filter(b=>String(b.period_number||'').trim()===String(periodNumber||'').trim() && b.settled_at);
    if(roundBets.length)setTimeout(()=>showResultPopup(periodNumber,winner,allMyBets),400);
  }catch(e){console.error(e);setPhase('result');}
}

function openModal(color){
  if(phase!=='betting')return;
  const box=$('#lg-modal');if(!box)return;
  box.dataset.color=color;
  const lbl=color==='red'?'Red':'Blue';
  $('#lg-mt').innerHTML='<span class="lg-cdot '+(color==='red'?'r':'b')+'"></span> Bet '+lbl;
  $('#lg-mgo').className='lg-mgo '+(color==='red'?'r':'b');
  $('#lg-mgo').textContent='Place '+lbl+' Bet';
  $('#lg-amt').value='';$('#lg-mb').textContent=fmt(CT.balUsd);
  box.querySelectorAll('.lg-mc').forEach(c=>c.classList.remove('on'));
  box.classList.add('show');
}
async function placeFromModal(){
  if(placing)return;
  const box=$('#lg-modal');const color=box.dataset.color;
  const lv=parseFloat(($('#lg-amt').value||'0').replace(/,/g,''));
  if(!lv||lv<=0){showToast('Enter amount in '+CT.currency, true);return;}
  const usd=toUsd(lv);
  if(usd<0.10){showToast('Min bet '+CT.currency+' '+CT.minBetLocal.toFixed(2), true);return;}
  placing=true;
  try{
    const d=await api('place_bet',{color,amount:usd});
    if(d.ok){
      CT.balUsd=d.balance_usd;CT.fxRate=d.fx_rate??CT.fxRate;updBal();
      box.classList.remove('show');
      showToast('Bet Confirmed!');
      sync();
    }
    else showToast(d.error||'Error', true);
  }catch(e){showToast('Network error', true);}
  placing=false;
}

document.querySelectorAll('.g-pill').forEach(t=>{
  t.addEventListener('click',()=>{
    document.querySelectorAll('.g-pill').forEach(x=>x.classList.remove('on'));
    document.querySelectorAll('.g-panel').forEach(x=>x.style.display='none');
    t.classList.add('on');const p=$('#'+t.dataset.p);if(p)p.style.display='block';
  });
});

$('#lg-btn-red').addEventListener('click',()=>openModal('red'));
$('#lg-btn-blue').addEventListener('click',()=>openModal('blue'));
$('#lg-mx').addEventListener('click',()=>$('#lg-modal').classList.remove('show'));
$('#lg-modal').addEventListener('click',e=>{if(e.target.id==='lg-modal')e.target.classList.remove('show');});
$('#lg-mgo').addEventListener('click',placeFromModal);
$('#lg-how-btn').addEventListener('click',()=>$('#lg-htp').classList.add('show'));
$('#lg-htp-close').addEventListener('click',()=>$('#lg-htp').classList.remove('show'));
$('#lg-htp').addEventListener('click',e=>{if(e.target.id==='lg-htp')e.target.classList.remove('show');});
$('#lg-res-x').addEventListener('click',closeResult);
$('#lg-res-ov').addEventListener('click',e=>{if(e.target.id==='lg-res-ov')closeResult();});
document.querySelectorAll('.lg-mc').forEach(c=>{
  c.addEventListener('click',()=>{
    document.querySelectorAll('.lg-mc').forEach(x=>x.classList.remove('on'));
    c.classList.add('on');const inp=$('#lg-amt');if(inp)inp.value=c.dataset.v||'';
  });
});

initLgAcCheckbox();sync(false);setInterval(()=>sync(false),12000);
})();
JS;

$fmtMin = number_format($minBetLocal, 2);
$c1 = 10;
$c2 = 50;
$c3 = 100;
$c4 = 500;
$c5 = 1000;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
    <title>Car Race - PakClub</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@600;700;800;900&display=swap" rel="stylesheet">
    <style>
        .main-header { display: none !important; }
        body { margin: 0; background: #050505; color: #fff; font-family: 'Poppins', sans-serif; -webkit-tap-highlight-color: transparent; }
        .mobile-wrapper { max-width: 420px; margin: 0 auto; background: #050505; min-height: 100vh; position: relative; display: flex; flex-direction: column; }
        .top-bar { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; background: rgba(18, 18, 20, 0.8); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); position: sticky; top: 0; z-index: 100; border-bottom: 1px solid rgba(255,255,255,0.05); }
        .top-bar .back-btn { color: #fff; text-decoration: none; font-size: 1.4rem; background: rgba(255,255,255,0.05); width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border-radius: 50%; border: 1px solid rgba(255,255,255,0.1); }
        .top-bar .balance-wrapper { background: rgba(0, 0, 0, 0.6); border: 2px solid #D4AF37; border-radius: 999px; padding: 8px 16px; display: flex; align-items: center; gap: 8px; box-shadow: 0 0 15px rgba(212, 175, 55, 0.2); }
        .top-bar .balance { font-size: 1.1rem; font-weight: 900; color: #FFDF00; letter-spacing: 0.5px; }
        
        <?php echo $PAGE_CSS; ?>
    </style>
</head>
<body>
<?php require_once __DIR__ . '/../external/header.php'; ?>
<div class="mobile-wrapper">
    <div class="top-bar">
        <a href="javascript:history.back()" class="back-btn"><i class="fa-solid fa-chevron-left"></i></a>
        <div class="balance-wrapper" id="hdr-bal-wrap">
            <i class="fa-solid fa-coins" style="color:#D4AF37"></i> 
            <span class="balance" id="hdr-bal"><?= htmlspecialchars($balLocal) ?></span>
        </div>
    </div>
    <div style="padding: 16px; flex: 1;">


<section class="lg-shell">
    <div class="lg-tbar">
        <div style="display:flex;align-items:center;gap:8px;">
            <button type="button" class="lg-how-btn" id="lg-how-btn"><i class="fa-solid fa-circle-info"></i> Guide</button>
        </div>
        <div class="lg-tbar-right">
            <span class="lg-tbar-label">Time Remaining</span>
            <div class="lg-timer">
                <div class="lg-td" id="td0">0</div>
                <div class="lg-td" id="td1">0</div>
                <span class="lg-sep">:</span>
                <div class="lg-td" id="td2">0</div>
                <div class="lg-td" id="td3">0</div>
            </div>
        </div>
    </div>
    <div class="lg-body">
        <div class="lg-info-row">
            <div class="lg-pinfo">
                <div class="lg-pmode"><i class="fa-solid fa-car-side"></i> Cars Cards</div>
                <div style="font-size:10px;color:var(--text-muted);font-weight:700; margin-top:4px; text-transform:uppercase;">Round ID</div>
                <div class="lg-pnum" id="g-pnum">---</div>
            </div>
            <div class="lg-dots-wrap" id="g-dots"></div>
        </div>
        <div class="lg-lock-bar" id="g-lock-bar">
            <i class="fa-solid fa-lock"></i> Bets Locked — <span id="g-lock-sec">0:00</span>
        </div>
        <div class="lg-viewport" id="lg-viewport">
            <div class="lg-vp-card">
                <div class="lg-vp-card-inner">
                    <div class="lg-vp-card-back" aria-hidden="true">
                        <div class="card-back-pattern"></div>
                        <div class="card-back-center"><span><i class="fa-solid fa-crown"></i></span></div>
                    </div>
                    <div class="lg-vp-card-front" id="lg-vp-card-front">
                        <span class="card-index card-index-tl" id="lg-vp-index-tl"></span>
                        <span class="card-index card-index-br" id="lg-vp-index-br"></span>
                        <div class="vp-car red" id="lg-vp-car">
                            <span class="vp-car-icon"><i class="fa-solid fa-car-side"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="lg-vp-label" id="lg-vp-label">Revealing Winner</div>
        </div>
        <div class="lg-arena">
            <div class="lg-btns">
                <button type="button" class="lg-btn r" id="lg-btn-red">
                    <span class="lg-btn-ico"><i class="fa-solid fa-car"></i></span>
                    <span class="lg-btn-name">Red</span>
                    <span class="lg-btn-sub">2x Payout</span>
                </button>
                <button type="button" class="lg-btn b" id="lg-btn-blue">
                    <span class="lg-btn-ico"><i class="fa-solid fa-car"></i></span>
                    <span class="lg-btn-name">Blue</span>
                    <span class="lg-btn-sub">2x Payout</span>
                </button>
            </div>
        </div>
        <div class="lg-result" id="lg-result" style="display:none;"></div>
    </div>
</section>

<div class="g-pills">
    <button class="g-pill on" data-p="g-p-hist">Game History</button>
    <button class="g-pill" data-p="g-p-my">My Bets</button>
</div>

<div id="g-p-hist" class="g-panel" style="display:block">
    <div class="g-htable">
        <div class="g-hdr"><span>Period</span><span style="text-align:center">Winner</span><span style="text-align:center">Color</span></div>
        <div id="g-hist"></div>
    </div>
</div>

<div id="g-p-my" class="g-panel" style="display:none">
    <div id="g-my"></div>
    <div id="g-pager" class="g-pager"></div>
</div>

<div class="lg-htp-ov" id="lg-htp">
    <div class="lg-htp-box">
        <div class="lg-htp-head"><i class="fa-solid fa-book" style="margin-right:6px;"></i> How to Play</div>
        <div class="lg-htp-body">
            <b>30 seconds</b> per round. Place your bet before the timer runs out.<br><br>
            1. Tap <span style="color:var(--neon-red);font-weight:900">Red</span>: if Red wins, you get <b>2x</b> your bet.<br><br>
            2. Tap <span style="color:var(--neon-blue);font-weight:900">Blue</span>: if Blue wins, you get <b>2x</b> your bet.<br><br>
            The card flips to reveal the winner each round. Minimum bet is <b><?=e($cur)?> <?=e($fmtMin)?></b>.
        </div>
        <button type="button" class="lg-htp-close" id="lg-htp-close">Understood</button>
    </div>
</div>

<div class="lg-modal" id="lg-modal">
    <div class="lg-mbox">
        <div class="lg-mhd">
            <div class="lg-mhd-t" id="lg-mt"><span class="lg-cdot r"></span> Bet</div>
            <button type="button" class="lg-mx" id="lg-mx"><i class="fa-solid fa-xmark"></i></button>
        </div>
        <div class="lg-mbal"><span>Balance</span> <b id="lg-mb"><?=e($balLocal)?></b></div>
        <div class="lg-mf">
            <label for="lg-amt">Amount (<?=e($cur)?>)</label>
            <input type="number" id="lg-amt" min="<?=e($fmtMin)?>" step="any" placeholder="<?=e($fmtMin)?>" inputmode="decimal">
            <div class="lg-mhint">Min <?=e($cur)?> <?=e($fmtMin)?></div>
        </div>
        <div class="lg-mchips">
            <button type="button" class="lg-mc" data-v="<?=$c1?>"><?=number_format($c1,0)?></button>
            <button type="button" class="lg-mc" data-v="<?=$c2?>"><?=number_format($c2,0)?></button>
            <button type="button" class="lg-mc" data-v="<?=$c3?>"><?=number_format($c3,0)?></button>
            <button type="button" class="lg-mc" data-v="<?=$c4?>"><?=number_format($c4,0)?></button>
            <button type="button" class="lg-mc" data-v="<?=$c5?>"><?=number_format($c5,0)?></button>
        </div>
        <button type="button" class="lg-mgo r" id="lg-mgo">Confirm Bet</button>
    </div>
</div>

<div class="lg-res-ov" id="lg-res-ov">
    <div class="lg-res-imgwrap">
        <img id="lg-res-img" src="https://www.fantasygems20.com/assets/png/missningBg-c1f02bcd.webp" alt="">
        <div class="lg-res-txt">
            <div class="r-top"><span class="r-title" id="lg-res-title">Congratulations</span></div>
            <div class="r-mid">
                <span class="r-sub">Lottery results</span>
                <span class="r-pills" id="lg-res-pills"><span class="r-pill blue">Blue</span></span>
            </div>
            <div class="r-bot">
                <span class="r-label" id="lg-res-label">Bonus</span>
                <span class="r-amt" id="lg-res-amt">+0.00</span>
                <span class="r-pn" id="lg-res-pn" style="font-family: monospace; font-size: 11px;">Period: Cars Cards<br>---</span>
            </div>
        </div>
    </div>
    <div class="lg-res-foot">
        <label><input type="checkbox" id="lg-res-ac"> <span class="r-cd-txt" id="lg-res-cd">3 seconds auto close</span></label>
    </div>
    <button type="button" class="lg-res-x" id="lg-res-x" aria-label="Close"><i class="fa-solid fa-xmark"></i></button>
</div>

<div class="lg-toast" id="lg-toast"></div>

    </div>
</div>
<script>
<?php echo $PAGE_JS; ?>
</script>
</body>
</html>
