<?php
// WinGo_30S.php
session_start();
?>
<!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">
    <title>WinGo 30s - PakClub</title>
    
    <link rel="stylesheet" href="https://pakclub24.club/css/global.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        :root { 
            --primary-orange: #f26d0f; 
            --primary-orange-dark: #da5f07;
            --bg-dark: #120a06;
            --card-dark: #242424; 
            --text-muted: #92837b;
            --green: #4caf50;
            --red: #f44336;
            --violet: #9c27b0;
            --blue: #40adff;
            --gold-dark: #cda434;
        }
        
        body { 
            background: #000; 
            color: #fff; 
            font-family: 'Segoe UI', Roboto, sans-serif; 
            margin: 0; 
            display: flex;
            justify-content: center;
        }
        
        * { box-sizing: border-box; }

        .mobile-wrapper {
            width: 100%;
            max-width: 420px;
            background: var(--bg-dark);
            min-height: 100vh;
            position: relative;
            overflow-x: hidden;
            padding-bottom: 50px;
            box-shadow: 0 0 15px rgba(0,0,0,0.5);
        }
        
        .header-wrapper { display: none !important; position: absolute; width: 0; height: 0; overflow: hidden; opacity: 0; pointer-events: none; z-index: -999; }
        
        .top-nav { display: flex; align-items: center; justify-content: space-between; padding: 15px; background: #1c100a; border-bottom: 1px solid #2e1c12; }
        .top-nav-left { display: flex; align-items: center; gap: 12px; }
        .top-nav .back-btn { color: #fff; font-size: 20px; text-decoration: none; }
        .top-nav .logo { height: 28px; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.5)); }
        .top-nav .deposit-btn { background: transparent; color: #fff; border: 1px solid var(--primary-orange); padding: 6px 14px; border-radius: 20px; font-weight: 700; font-size: 13px; cursor: pointer; transition: 0.2s; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
        .top-nav .deposit-btn:active { transform: scale(0.95); background: rgba(242, 109, 15, 0.1); }

        #toastContainer {
            position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
            z-index: 9999; display: flex; flex-direction: column; gap: 10px;
            pointer-events: none; width: auto; align-items: center;
        }
        .ios-toast {
            background: rgba(35, 35, 35, 0.95); color: #fff; padding: 10px 22px;
            border-radius: 30px; font-size: 14px; font-weight: 500; text-align: center;
            opacity: 0; transform: scale(0.9); transition: all 0.2s ease-out;
            display: flex; align-items: center; gap: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.5);
            white-space: nowrap;
        }
        .ios-toast.show { opacity: 1; transform: scale(1); }

        .balance-box { 
            position: relative;
            margin: 15px; 
            border-radius: 20px; 
            padding: 25px 20px; 
            text-align: center; 
            background: linear-gradient(180deg, #2b180d 0%, #150a04 100%);
            border: 1px solid #3d2414;
            box-shadow: 0 8px 25px rgba(0,0,0,0.8);
            overflow: hidden;
            z-index: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        .balance-box::before {
            content: ''; position: absolute; top: -60px; left: 50%; transform: translateX(-50%);
            width: 120%; height: 120px;
            background: radial-gradient(ellipse at center, rgba(242, 109, 15, 0.2) 0%, rgba(242, 109, 15, 0) 70%);
            z-index: -1;
            pointer-events: none;
        }

        .balance-title { 
            color: #92837b; 
            font-size: 12px; 
            font-weight: 700; 
            display: flex; align-items: center; justify-content: center; gap: 6px; 
            white-space: nowrap; 
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 8px;
        }
        
        .balance-amount { 
            font-size: 38px; 
            font-weight: 900; 
            color: #fff; 
            display: flex; align-items: center; justify-content: center; gap: 5px; 
            margin-bottom: 25px; 
            white-space: nowrap; 
            text-shadow: 0 2px 10px rgba(255, 255, 255, 0.2);
            letter-spacing: -0.5px;
        }

        .action-btns { display: flex; gap: 15px; width: 100%; }
        
        .btn-action { 
            flex: 1; padding: 14px 5px; border-radius: 12px; border: none; 
            font-weight: 800; font-size: 15px; cursor: pointer; transition: 0.15s; 
            white-space: nowrap; 
            text-transform: uppercase;
            letter-spacing: 0.5px;
            display: flex; align-items: center; justify-content: center; gap: 8px;
        }
        .btn-action:active { transform: scale(0.95); }
        
        .btn-withdraw { 
            background: #2a201a; 
            color: #fff; 
            border: 1px solid #483528;
            box-shadow: 0 4px 6px rgba(0,0,0,0.3);
        } 
        
        .btn-recharge { 
            background: linear-gradient(180deg, #f28b32 0%, #da5f07 100%); 
            color: #fff; 
            border: none;
            box-shadow: 0 4px 15px rgba(218, 95, 7, 0.4);
        } 

        .category-row { display: flex; justify-content: space-between; background: var(--card-dark); margin: 15px; border-radius: 20px; padding: 5px; gap: 5px; }
        .cat-item { flex: 1; text-align: center; padding: 10px 2px; cursor: pointer; border-radius: 15px; transition: 0.3s; display: flex; flex-direction: column; align-items: center; justify-content: center; overflow: hidden; }
        .cat-item img.icon { width: 26px; height: 26px; margin-bottom: 5px; filter: grayscale(1); opacity: 0.5; }
        .cat-item .text { font-size: 11px; color: var(--text-muted); font-weight: 700; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; width: 100%; text-align: center; }
        .cat-item.active { background: linear-gradient(135deg, var(--primary-orange), var(--primary-orange-dark)); }
        .cat-item.active img.icon { filter: grayscale(0); opacity: 1; }
        .cat-item.active .text { color: #fff; }

        .timer-banner { 
            background: url('https://pakclub.club/assets/deepOrange/wingo_bg-ec81392b.webp') center/cover;
            margin: 15px; border-radius: 15px; display: flex; position: relative; padding: 20px 15px; align-items: center;
        }
        .timer-banner::after { content: ''; position: absolute; top: 0; bottom: 0; left: 50%; border-left: 2px dashed rgba(255,255,255,0.4); transform: translateX(-50%); }
        .timer-banner::before, .timer-cutout-bottom { content: ''; position: absolute; left: calc(50% - 10px); width: 20px; height: 20px; background: var(--bg-dark); border-radius: 50%; z-index: 2; }
        .timer-banner::before { top: -10px; }
        .timer-cutout-bottom { bottom: -10px; }
        .timer-left { flex: 1; padding-right: 10px; display: flex; flex-direction: column; align-items: flex-start; overflow: hidden; }
        .timer-right { flex: 1; text-align: right; padding-left: 10px; overflow: hidden; }
        .how-to-play-btn { background: transparent; border: 1px solid rgba(255,255,255,0.8); color: #fff; border-radius: 20px; padding: 4px 10px; font-size: 11px; white-space: nowrap; cursor: pointer; }
        .how-to-play-btn:active { background: rgba(255,255,255,0.1); }
        .timer-title { margin-top:10px; font-size:14px; font-weight:700; white-space: nowrap; }
        .countdown-digits { display: flex; gap: 4px; justify-content: flex-end; margin-top: 8px; align-items: center; flex-wrap: nowrap; }
        .digit { background: #fff; color: var(--primary-orange-dark); padding: 4px 6px; border-radius: 5px; font-weight: 900; font-size: 18px; line-height: 1; }
        .last-balls { display: flex; gap: 4px; margin-top: 8px; flex-wrap: nowrap; }
        .ball-sm { width: 20px; height: 20px; }
        .period-text { font-weight:700; margin-top:8px; font-size:14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

        .bet-section { position: relative; background: var(--card-dark); margin: 15px; border-radius: 15px; padding: 20px; }
        
        #countDownOverlay { position: absolute; inset: 0; background: rgba(0,0,0,0.85); z-index: 1000; display: none; align-items: center; justify-content: center; backdrop-filter: blur(5px); border-radius: 15px; }
        .big-countdown-box { display: flex; gap: 15px; }
        .big-digit { background: #3c2a1e; color: var(--primary-orange); font-size: 110px; font-weight: 900; padding: 15px 20px; border-radius: 20px; box-shadow: 0 10px 25px rgba(0,0,0,0.6); line-height: 1; display: flex; align-items: center; justify-content: center; }

        .color-btns { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-bottom: 25px; }
        .btn-color { padding: 10px 5px; border: none; border-radius: 10px; font-weight: 700; color: #fff; cursor: pointer; font-size: 14px; transition: 0.1s; white-space: nowrap; }
        .btn-color:active { transform: scale(0.95); }
        .btn-green { background: var(--green); }
        .btn-violet { background: var(--violet); }
        .btn-red { background: var(--red); }
        .number-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px 8px; margin-bottom: 25px; }
        .num-ball { width: 100%; cursor: pointer; transition: transform 0.1s; filter: drop-shadow(0 4px 4px rgba(0,0,0,0.3)); }
        .num-ball:active { transform: scale(0.9); }
        .size-btns { display: flex; border-radius: 12px; overflow: hidden; margin-top: 20px; }
        .btn-size { flex: 1; padding: 12px 5px; border: none; font-weight: 800; font-size: 15px; cursor: pointer; transition: 0.1s; white-space: nowrap; }
        .btn-size:active { filter: brightness(0.9); }
        .btn-big { background: var(--primary-orange); color: #fff; }
        .btn-small { background: var(--blue); color: #fff; }

        .tabs-header { display: flex; margin: 15px; border-radius: 10px; background: var(--card-dark); }
        .tab-trigger { flex: 1; padding: 10px 5px; text-align: center; color: var(--text-muted); font-weight: 600; cursor: pointer; border-radius: 10px; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .tab-trigger.active { background: linear-gradient(180deg, #f28b32 0%, #da5f07 100%); color: #fff; }

        .hist-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 20px; }
        .hist-table th { background: var(--primary-orange-dark); color: #fff; padding: 10px 5px; font-weight: 600; white-space: nowrap; }
        .hist-table td { padding: 10px 5px; text-align: center; border-bottom: 1px solid #333; color: #fff; font-weight: 500; white-space: nowrap; }
        .color-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin: 0 2px; }
        .bg-green { background: var(--green); }
        .bg-red { background: var(--red); }
        .bg-violet { background: var(--violet); }

        .my-hist-list { padding: 0 15px; display: flex; flex-direction: column; gap: 10px; }
        .my-hist-item { background: var(--card-dark); padding: 12px; border-radius: 10px; display: flex; justify-content: space-between; align-items: center; }
        .my-hist-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 13px; text-transform: capitalize; color: #fff; flex-shrink: 0; }
        .my-hist-details { flex: 1; padding-left: 12px; overflow: hidden; }
        .my-hist-period { font-size: 13px; font-weight: 700; color: #fff; margin-bottom: 2px; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
        .my-hist-time { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
        .my-hist-status { text-align: right; flex-shrink: 0; }
        .status-badge { display: inline-block; padding: 2px 8px; border-radius: 15px; border: 1px solid; font-size: 11px; font-weight: 600; margin-bottom: 4px; white-space: nowrap; }
        .status-win { color: var(--green); border-color: var(--green); }
        .status-lose { color: var(--red); border-color: var(--red); }
        .status-pending { color: var(--primary-orange); border-color: var(--primary-orange); }
        .my-hist-amt { font-size: 13px; font-weight: 800; white-space: nowrap; }

        .bottom-sheet { position: fixed; bottom: -100%; left: 50%; transform: translateX(-50%); width: 100%; max-width: 420px; background: #1e1e1e; z-index: 2000; border-top-left-radius: 20px; border-top-right-radius: 20px; transition: bottom 0.3s ease; padding-bottom: 20px; }
        .bottom-sheet.open { bottom: 0; }
        .sheet-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 1999; display: none; backdrop-filter: blur(2px); }
        .sheet-header { padding: 15px; text-align: center; font-weight: 700; font-size: 16px; color: #fff; border-top-left-radius: 20px; border-top-right-radius: 20px; position: relative; white-space: nowrap; }
        .sheet-header::after { content: ''; position: absolute; bottom: -15px; left: 50%; transform: translateX(-50%); border-width: 15px 20px 0; border-style: solid; border-color: inherit; border-bottom-color: transparent !important; border-left-color: transparent !important; border-right-color: transparent !important; }
        .sheet-body { padding: 30px 15px 10px; }
        .row-flex { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .amt-btn, .qty-btn, .mult-btn { background: #333; border: none; color: #fff; padding: 6px 10px; border-radius: 5px; cursor: pointer; font-weight: 600; font-size: 13px; white-space: nowrap; }
        .amt-btn.active { background: var(--green); }
        .qty-input { width: 50px; text-align: center; background: #222; border: 1px solid #444; color: #fff; padding: 6px; font-weight: bold; font-size: 14px; }
        .sheet-footer { display: flex; padding: 0 15px; gap: 10px; }
        .btn-cancel, .btn-confirm { flex: 1; padding: 12px 5px; border: none; font-weight: bold; font-size: 14px; cursor: pointer; border-radius: 8px; white-space: nowrap; }
        .btn-cancel { background: #333; color: #fff; }
        .btn-confirm { background: var(--green); color: #fff; }

        .htp-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 4000; display: none; align-items: center; justify-content: center; backdrop-filter: blur(4px); }
        .htp-overlay.show { display: flex; }
        .htp-content { background: var(--card-dark); width: 90%; max-width: 360px; border-radius: 15px; overflow: hidden; display: flex; flex-direction: column; animation: popIn 0.3s ease; box-shadow: 0 10px 30px rgba(0,0,0,0.8); }
        @keyframes popIn { 0% { transform: scale(0.9); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
        .htp-header { background: linear-gradient(135deg, var(--primary-orange), var(--primary-orange-dark)); color: #fff; padding: 15px 20px; display: flex; justify-content: space-between; align-items: center; font-weight: 800; font-size: 16px; }
        .htp-close { cursor: pointer; font-size: 20px; transition: 0.2s; }
        .htp-close:active { transform: scale(0.8); }
        .htp-body { padding: 20px; color: #ddd; font-size: 13px; line-height: 1.6; max-height: 60vh; overflow-y: auto; }
        .htp-body p { margin: 0 0 10px 0; }
        .htp-body strong { color: #fff; }
        .htp-body .highlight-green { color: var(--green); font-weight: bold; }
        .htp-body .highlight-red { color: var(--red); font-weight: bold; }
        .htp-body .highlight-violet { color: var(--violet); font-weight: bold; }

        .g-res-ov{position:fixed;inset:0;background:rgba(0,0,0,.6);backdrop-filter:blur(4px);display:none;flex-direction:column;align-items:center;justify-content:center;z-index:3000;padding:16px;gap:10px}
        .g-res-ov.show{display:flex}
        .g-res-imgwrap{position:relative;width:100%;max-width:300px;animation:gResPop .35s ease}
        @keyframes gResPop{0%{transform:scale(.8);opacity:0}100%{transform:scale(1);opacity:1}}
        .g-res-imgwrap img{width:100%;display:block}
        .g-res-txt{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;text-align:center}
        .g-res-txt .r-top{flex:0 0 38%;display:flex;align-items:flex-end;justify-content:center;padding:0 20px 0}
        .g-res-txt .r-title{font-size:22px;font-weight:900;color:#fff; white-space: nowrap;}
        .g-res-txt.lose .r-title{color:#768b9e;padding-top:6px}
        .g-res-txt .r-mid{flex:0 0 13%;display:flex;flex-direction:row;align-items:center;justify-content:center;gap:8px;padding:0 20px}
        .g-res-txt .r-sub{font-size:13px;font-weight:500;color:rgba(255,255,255,.95); white-space: nowrap;}
        .g-res-txt.lose .r-sub{color:#768b9e}
        .g-res-txt .r-pills{display:flex;gap:6px;align-items:center}
        .g-res-txt .r-pill{padding:4px 10px;border-radius:4px;font-size:13px;font-weight:600;color:#fff; text-transform: capitalize; white-space: nowrap;}
        .g-res-txt .r-pill.num{padding:0; width:22px; height:22px; border-radius:50%; display:flex; align-items:center; justify-content:center;}
        .g-res-txt .r-bot{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:flex-start;padding:32px 40px 4px}
        .g-res-txt .r-label{font-size:10px;color:var(--primary-orange);font-weight:700;text-transform:uppercase;letter-spacing:.5px; white-space: nowrap;}
        .g-res-txt .r-amt{font-size:20px;font-weight:900;color:var(--red);margin-top:2px; white-space: nowrap;}
        .g-res-txt.lose .r-amt{color:#b0705b; font-size: 24px;}
        .g-res-txt .r-pn{font-size:9px;color:var(--text-muted);margin-top:4px;line-height:1.3; font-weight:500;}
        .g-res-foot { position: absolute; bottom: 25px; left: 0; width: 100%; display: flex; align-items: center; justify-content: center; gap: 6px; color: #fff; font-size: 12px; cursor: pointer; z-index: 3001; user-select: none; white-space: nowrap; }
        .g-res-foot i { font-size: 18px; transition: 0.2s; }
        .g-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; z-index:3001; margin-top: 10px;}
        .g-res-x:active{opacity:.7; transform:scale(0.9);}

        @media (max-width: 360px) {
            .balance-amount { font-size: 32px; }
            .btn-action { font-size: 12px; }
            .digit { padding: 4px; font-size: 15px; }
            .timer-title { font-size: 13px; }
            .period-text { font-size: 13px; }
            .big-digit { font-size: 80px; padding: 10px 15px; height: auto; }
            .cat-item .text { font-size: 10px; }
            .btn-color { font-size: 13px; }
            .btn-size { font-size: 14px; }
        }
    </style>
</head>
<body>

<div id="toastContainer"></div>

<div class="mobile-wrapper">

    <div class="header-wrapper">
        <?php echo @file_get_contents('https://pakclub24.club/external/header.php'); ?>
    </div>

    <div class="top-nav">
        <div class="top-nav-left">
            <a href="https://pakclub24.club/index.php" class="back-btn"><i class="fa-solid fa-chevron-left"></i></a>
            <img src="https://pakclub.club/img/6004/other/114917714-29322-file_20260402114917694.webp" alt="Logo" class="logo">
        </div>
        <button class="deposit-btn" onclick="actionDeposit()">
            <i class="fa-solid fa-wallet"></i> Deposit
        </button>
    </div>

    <div class="balance-box">
        <div class="balance-title">
            <i class="fa-solid fa-wallet"></i> Wallet balance <i class="fa-solid fa-rotate-right" onclick="updateBalance()" style="cursor:pointer; margin-left:5px;"></i>
        </div>
        <div class="balance-amount" id="userBalance">Rs0.00</div>
        
        <div class="action-btns">
            <button class="btn-action btn-withdraw" onclick="actionWithdraw()">Withdraw</button>
            <button class="btn-action btn-recharge" onclick="actionDeposit()">Recharge</button>
        </div>
    </div>

    <div class="category-row">
        <div class="cat-item active">
            <img src="https://pakclub.club/assets/deepOrange/lottery_category_active-c2a17b39.webp" class="icon">
            <div class="text">WinGo 30s</div>
        </div>
        <div class="cat-item" onclick="showToast('Hack does not work in 1 Min mode')">
            <img src="https://pakclub.club/assets/deepOrange/lottery_category_active-c2a17b39.webp" class="icon">
            <div class="text">WinGo 1m</div>
        </div>
        <div class="cat-item" onclick="showToast('Hack does not work in 3 Min mode')">
            <img src="https://pakclub.club/assets/deepOrange/lottery_category_active-c2a17b39.webp" class="icon">
            <div class="text">WinGo 3m</div>
        </div>
        <div class="cat-item" onclick="showToast('Hack does not work in 5 Min mode')">
            <img src="https://pakclub.club/assets/deepOrange/lottery_category_active-c2a17b39.webp" class="icon">
            <div class="text">WinGo 5m</div>
        </div>
    </div>

    <div class="timer-banner">
        <div class="timer-cutout-bottom"></div>
        <div class="timer-left">
            <button class="how-to-play-btn" onclick="openHowToPlay()"><i class="fa-solid fa-file-lines"></i> How To Play</button>
            <div class="timer-title">WinGo 30sec</div>
            <div class="last-balls" id="lastBallsRow"></div>
        </div>
        <div class="timer-right">
            <div style="font-size:11px; font-weight:600; white-space:nowrap;">Time remaining</div>
            <div class="countdown-digits">
                <div class="digit">0</div><div class="digit">0</div><div style="font-weight:900;">:</div>
                <div class="digit" id="sec1">0</div><div class="digit" id="sec2">0</div>
            </div>
            <div id="currentPeriod" class="period-text">--</div>
        </div>
    </div>

    <div class="bet-section">
        
        <div id="countDownOverlay">
            <div class="big-countdown-box">
                <div class="big-digit" id="bigSec1">0</div>
                <div class="big-digit" id="bigSec2">5</div>
            </div>
        </div>

        <div class="color-btns">
            <button class="btn-color btn-green" onclick="openBetSheet('green')">Green</button>
            <button class="btn-color btn-violet" onclick="openBetSheet('violet')">Violet</button>
            <button class="btn-color btn-red" onclick="openBetSheet('red')">Red</button>
        </div>

        <div class="number-grid">
            <?php for($i=0; $i<=9; $i++): ?>
                <img src="https://pakclub.club/images/ball_<?= $i ?>-<?= ['Ca74Ns3T','CA5nKxei','COP-h3Xp','PCU_ynUn','BL5_7sSn','DgoWY--i','CQQWyfmL','CPGDphqq','BRnYUrpp','BXzIaJqy'][$i] ?>.webp" class="num-ball" onclick="openBetSheet('<?= $i ?>')">
            <?php endfor; ?>
        </div>

        <div class="size-btns">
            <button class="btn-size btn-big" onclick="openBetSheet('big')">Big</button>
            <button class="btn-size btn-small" onclick="openBetSheet('small')">Small</button>
        </div>
    </div>

    <div class="tabs-header">
        <div class="tab-trigger active" onclick="switchTab('game')">Game history</div>
        <div class="tab-trigger" onclick="switchTab('my')">My history</div>
    </div>

    <div id="gameHistory" style="padding: 0 15px;">
        <table class="hist-table">
            <thead><tr><th style="border-top-left-radius:8px;">Period</th><th>Number</th><th>Big Small</th><th style="border-top-right-radius:8px;">Color</th></tr></thead>
            <tbody id="gameHistoryBody"></tbody>
        </table>
    </div>

    <div id="myHistory" class="my-hist-list" style="display:none;"></div>

    <div class="sheet-overlay" id="sheetOverlay" onclick="closeBetSheet()"></div>
    <div class="bottom-sheet" id="betSheet">
        <div class="sheet-header" id="sheetHeader">Select <span id="betTypeText">Green</span></div>
        <div class="sheet-body">
            <div class="row-flex">
                <span style="color:#aaa; white-space:nowrap;">Amount</span>
                <div style="display:flex; gap:5px; flex-wrap:nowrap; overflow-x:auto;">
                    <button class="amt-btn active" onclick="setBaseAmt(1, this)">1</button>
                    <button class="amt-btn" onclick="setBaseAmt(10, this)">10</button>
                    <button class="amt-btn" onclick="setBaseAmt(100, this)">100</button>
                    <button class="amt-btn" onclick="setBaseAmt(1000, this)">1000</button>
                </div>
            </div>
            <div class="row-flex">
                <span style="color:#aaa; white-space:nowrap;">Quantity</span>
                <div style="display:flex; gap:5px; align-items:center; flex-wrap:nowrap;">
                    <button class="qty-btn" onclick="changeQty(-1)">-</button>
                    <input type="text" class="qty-input" id="betQty" value="1" readonly>
                    <button class="qty-btn" onclick="changeQty(1)">+</button>
                </div>
            </div>
            <div style="display:flex; gap:5px; justify-content:flex-end; margin-bottom:20px; flex-wrap:nowrap; overflow-x:auto; padding-bottom:5px;">
                <button class="mult-btn" onclick="setQty(1)">X1</button>
                <button class="mult-btn" onclick="setQty(5)">X5</button>
                <button class="mult-btn" onclick="setQty(10)">X10</button>
                <button class="mult-btn" onclick="setQty(20)">X20</button>
                <button class="mult-btn" onclick="setQty(50)">X50</button>
                <button class="mult-btn" onclick="setQty(100)">X100</button>
            </div>
        </div>
        <div class="sheet-footer">
            <button class="btn-cancel" onclick="closeBetSheet()">Cancel</button>
            <button class="btn-confirm" id="confirmBetBtn" onclick="submitBet()">Total Rs1.00</button>
        </div>
    </div>

    <div class="htp-overlay" id="htpModal" onclick="closeHowToPlay(event)">
        <div class="htp-content" onclick="event.stopPropagation()">
            <div class="htp-header">
                <span>How to play</span>
                <i class="fa-solid fa-xmark htp-close" onclick="closeHowToPlay()"></i>
            </div>
            <div class="htp-body">
                <p><strong>1. Minutes:</strong> 30 seconds per issue (25s to order, 5s to show result). Open all day, total 2880 issues.</p>
                <p><strong>2. Guess Color:</strong><br>
                • If result is <span class="highlight-green">1, 3, 7, 9</span>, <span class="highlight-green">Green</span> wins.<br>
                • If result is <span class="highlight-red">2, 4, 6, 8</span>, <span class="highlight-red">Red</span> wins.<br>
                • If result is <span class="highlight-violet">0 or 5</span>, <span class="highlight-violet">Violet</span> wins.</p>
                <p><strong>3. Guess Number:</strong><br>
                Choose 0-9. If the selected number matches the lottery result, you win a 9x payout.</p>
                <p><strong>4. Guess Size:</strong><br>
                • Big: 5, 6, 7, 8, 9<br>
                • Small: 0, 1, 2, 3, 4</p>
            </div>
        </div>
    </div>

    <div class="g-res-ov" id="resultModal">
        <div class="g-res-imgwrap">
            <img id="resultBgImg" src="" alt="Result Background">
            
            <div class="g-res-txt" id="resultTxtWrap">
                <div class="r-top">
                    <span class="r-title" id="resultTitle">Congratulations</span>
                </div>
                
                <div class="r-mid">
                    <span class="r-sub">winTips3</span>
                    <span class="r-pills" id="resultBadges">
                        </span>
                </div>
                
                <div class="r-bot">
                    <span class="r-label" id="resultSubtitle">Bonus</span>
                    <span class="r-amt" id="resultAmt">+Rs0.00</span>
                    <span class="r-pn">Period WinGo 30sec<br><span id="resultPeriod">--</span></span>
                </div>
                
                <div class="g-res-foot" onclick="toggleAutoClose()">
                    <i class="fa-regular fa-circle-check" id="autoCloseIcon"></i>
                    <span class="r-cd-txt"><span id="autoCloseSec">5</span>s Automatic Shutdown</span>
                </div>
            </div>
        </div>
        
        <button type="button" class="g-res-x" onclick="closeResultModal()"><i class="fa-solid fa-xmark"></i></button>
    </div>

</div> 
<script>
    const sec1El = document.getElementById('sec1');
    const sec2El = document.getElementById('sec2');
    const bigSec1El = document.getElementById('bigSec1');
    const bigSec2El = document.getElementById('bigSec2');
    const overlayEl = document.getElementById('countDownOverlay');
    const toastContainer = document.getElementById('toastContainer');
    const userBalanceEl = document.getElementById('userBalance');

    let currentPeriod = '';
    let isBettingClosed = false;
    let selectedBetType = '';
    let baseAmt = 1;
    let betQty = 1;
    let gameTimer = null;
    let modalCountdownTimer = null;
    let autoCloseEnabled = true;

    const ucFirst = s => s.charAt(0).toUpperCase() + s.slice(1);

    function getLocalBalance() {
        let bal = localStorage.getItem('localBalance');
        if(!bal) { 
            bal = 10000; 
            localStorage.setItem('localBalance', bal); 
        }
        return parseFloat(bal);
    }

    function updateBalance() {
        let bal = getLocalBalance();
        userBalanceEl.innerText = 'Rs' + bal.toFixed(2);
    }

    function addBalance(amt) {
        let bal = getLocalBalance() + parseFloat(amt);
        localStorage.setItem('localBalance', bal);
        updateBalance();
    }

    function deductBalance(amt) {
        let bal = getLocalBalance() - parseFloat(amt);
        localStorage.setItem('localBalance', bal);
        updateBalance();
    }

    function actionDeposit() {
        let amt = prompt("How much would you like to deposit?", "1000");
        let parsed = parseFloat(amt);
        if(!isNaN(parsed) && parsed > 0) {
            addBalance(parsed);
            showToast("Successfully deposited Rs" + parsed.toFixed(2));
        } else if(amt !== null) {
            showToast("Invalid amount!", "fa-solid fa-circle-exclamation");
        }
    }

    function actionWithdraw() {
        let amt = prompt("How much would you like to withdraw?", "500");
        let parsed = parseFloat(amt);
        if(!isNaN(parsed) && parsed > 0) {
            if(parsed > getLocalBalance()) {
                showToast("Insufficient balance!", "fa-solid fa-circle-exclamation");
            } else {
                deductBalance(parsed);
                showToast("Successfully withdrew Rs" + parsed.toFixed(2));
            }
        } else if(amt !== null) {
            showToast("Invalid amount!", "fa-solid fa-circle-exclamation");
        }
    }

    function openHowToPlay() {
        document.getElementById('htpModal').classList.add('show');
    }
    function closeHowToPlay(e) {
        document.getElementById('htpModal').classList.remove('show');
    }

    function showToast(msg, icon = 'fa-solid fa-circle-check') {
        const existingToasts = document.querySelectorAll('.ios-toast span');
        for (let t of existingToasts) {
            if (t.innerText === msg && t.parentElement.classList.contains('show')) return;
        }

        const toast = document.createElement('div');
        toast.className = 'ios-toast';
        toast.innerHTML = `<i class="${icon}" style="color: var(--primary-orange); font-size: 16px;"></i> <span>${msg}</span>`;
        toastContainer.appendChild(toast);
        
        void toast.offsetWidth;
        toast.classList.add('show');
        
        setTimeout(() => {
            toast.classList.remove('show');
            setTimeout(() => toast.remove(), 300);
        }, 2000);
    }

    function toggleAutoClose() {
        autoCloseEnabled = !autoCloseEnabled;
        const icon = document.getElementById('autoCloseIcon');
        if (autoCloseEnabled) {
            icon.className = 'fa-regular fa-circle-check';
        } else {
            icon.className = 'fa-regular fa-circle';
        }
    }

    function resolveLocalBets(historyData) {
        let bets = JSON.parse(localStorage.getItem('localBets') || '[]');
        let needsUpdate = false;
        const PLATFORM_FEE = 0.02; // 2% Fee
        
        bets.forEach(bet => {
            if(bet.status === 'pending') {
                let result = historyData.find(h => String(h.period) === String(bet.period));
                if(result) {
                    let isWin = false;
                    let payout = 0;
                    let num = parseInt(result.number);
                    
                    if(!isNaN(bet.bet_on)) { 
                        if(parseInt(bet.bet_on) === num) { isWin = true; payout = bet.amount * 9; }
                    } else if(bet.bet_on === 'green') {
                        if([1,3,7,9].includes(num)) { isWin = true; payout = bet.amount * 2; }
                        if(num === 5) { isWin = true; payout = bet.amount * 1.5; }
                    } else if(bet.bet_on === 'red') {
                        if([2,4,6,8].includes(num)) { isWin = true; payout = bet.amount * 2; }
                        if(num === 0) { isWin = true; payout = bet.amount * 1.5; }
                    } else if(bet.bet_on === 'violet') {
                        if([0,5].includes(num)) { isWin = true; payout = bet.amount * 4.5; }
                    } else if(bet.bet_on === 'big') {
                        if(num >= 5) { isWin = true; payout = bet.amount * 2; }
                    } else if(bet.bet_on === 'small') {
                        if(num <= 4) { isWin = true; payout = bet.amount * 2; }
                    }
                    
                    if(isWin) {
                        // Apply 2% platform fee on the winning payout
                        let finalPayout = payout * (1 - PLATFORM_FEE);
                        
                        bet.status = 'win';
                        bet.payout = finalPayout.toFixed(2);
                        addBalance(finalPayout);
                    } else {
                        bet.status = 'loss';
                        bet.payout = 0;
                    }
                    needsUpdate = true;
                    showResultModal(bet); 
                }
            }
        });
        
        if(needsUpdate) {
            localStorage.setItem('localBets', JSON.stringify(bets));
            loadMyHistory();
        }
    }

    function syncGame() {
        fetch('wingo_api.php?action=get_game_state').then(res => res.json()).then(data => {
            if(!data.ok) return; 
            
            resolveLocalBets(data.history);

            if(currentPeriod !== data.period) {
                currentPeriod = data.period;
                document.getElementById('currentPeriod').innerText = currentPeriod;
                
                const ballHashes = ['Ca74Ns3T','CA5nKxei','COP-h3Xp','PCU_ynUn','BL5_7sSn','DgoWY--i','CQQWyfmL','CPGDphqq','BRnYUrpp','BXzIaJqy'];
                let histHtml = '';
                let ballsHtml = '';
                
                data.history.forEach((h, index) => {
                    let colorDots = '';
                    let numColor = '';
                    
                    if(h.number === 0) { 
                        numColor = 'var(--violet)'; 
                        colorDots = '<span class="color-dot bg-red"></span><span class="color-dot bg-violet"></span>'; 
                    }
                    else if(h.number === 5) { 
                        numColor = 'var(--violet)'; 
                        colorDots = '<span class="color-dot bg-green"></span><span class="color-dot bg-violet"></span>'; 
                    }
                    else if(h.number % 2 === 0) { 
                        numColor = 'var(--red)'; 
                        colorDots = '<span class="color-dot bg-red"></span>'; 
                    }
                    else { 
                        numColor = 'var(--green)'; 
                        colorDots = '<span class="color-dot bg-green"></span>'; 
                    }

                    histHtml += `<tr>
                        <td>${h.period}</td>
                        <td style="color:${numColor}; font-weight:900; font-size:18px;">${h.number}</td>
                        <td>${ucFirst(h.size)}</td>
                        <td>${colorDots}</td>
                    </tr>`;
                    
                    if(index < 5) ballsHtml += `<img src="https://pakclub.club/images/ball_${h.number}-${ballHashes[h.number]}.webp" class="ball-sm">`;
                });
                
                document.getElementById('gameHistoryBody').innerHTML = histHtml;
                document.getElementById('lastBallsRow').innerHTML = ballsHtml;
            }

            startTimer(data.time_left);
        }).catch(err => console.error('Failed to sync API:', err));
    }

    function startTimer(seconds) {
        if(gameTimer) clearInterval(gameTimer);

        gameTimer = setInterval(() => {
            seconds--;
            
            if(seconds === 0) {
                isBettingClosed = true;
                if(overlayEl.style.display !== 'none') overlayEl.style.display = 'none';
                clearInterval(gameTimer);
                syncGame();
                return;
            }

            let s = seconds.toString().padStart(2, '0');
            
            if(sec1El.innerText !== s[0]) sec1El.innerText = s[0];
            if(sec2El.innerText !== s[1]) sec2El.innerText = s[1];

            if(seconds <= 6 && seconds > 0) {
                isBettingClosed = true;
                closeBetSheet();
                if(overlayEl.style.display !== 'flex') overlayEl.style.display = 'flex';
                
                if(bigSec1El.innerText !== s[0]) bigSec1El.innerText = s[0];
                if(bigSec2El.innerText !== s[1]) bigSec2El.innerText = s[1];
            } else if(seconds > 6) {
                isBettingClosed = false;
                if(overlayEl.style.display !== 'none') overlayEl.style.display = 'none';
            }
        }, 1000);
    }

    function showResultModal(betData) {
        const modal = document.getElementById('resultModal');
        const txtWrap = document.getElementById('resultTxtWrap');
        const isWin = betData.status === 'win';
        
        document.getElementById('resultBgImg').src = isWin ? 'https://pakclub.club/images/missningBg-CVBJxzJu.webp' : 'https://pakclub.club/images/missningLBg-CJY3-D31.webp';
        
        document.getElementById('resultTitle').innerText = isWin ? 'Congratulations' : "It's a pity";
        
        const subtitle = document.getElementById('resultSubtitle');
        const amtEl = document.getElementById('resultAmt');

        if(isWin) {
            txtWrap.classList.remove('lose');
            subtitle.style.display = 'block';
            subtitle.innerText = 'Bonus';
            amtEl.innerText = '+Rs' + betData.payout;
        } else {
            txtWrap.classList.add('lose');
            subtitle.style.display = 'none';
            amtEl.innerText = "It's a pity"; 
        }

        document.getElementById('resultPeriod').innerText = betData.period;
        
        let bColor = 'var(--gold-dark)';
        if(['green','small'].includes(betData.bet_on)) bColor = 'var(--green)';
        if(['red','big'].includes(betData.bet_on)) bColor = 'var(--red)';
        if(betData.bet_on === 'violet') bColor = 'var(--violet)';
        
        let badgesHtml = ``;
        if(!isNaN(betData.bet_on)) {
            badgesHtml += `<span class="r-pill num" style="background:var(--gold-dark);">${betData.bet_on}</span>`;
        } else {
            badgesHtml += `<span class="r-pill" style="background:${bColor}">${ucFirst(betData.bet_on)}</span>`;
        }
        document.getElementById('resultBadges').innerHTML = badgesHtml;
        
        modal.classList.add('show');
        
        let secs = 5;
        document.getElementById('autoCloseSec').innerText = secs;
        
        autoCloseEnabled = true;
        document.getElementById('autoCloseIcon').className = 'fa-regular fa-circle-check';

        if(modalCountdownTimer) clearInterval(modalCountdownTimer);
        modalCountdownTimer = setInterval(() => {
            if(autoCloseEnabled) {
                secs--;
                if(secs <= 0) {
                    closeResultModal();
                } else {
                    document.getElementById('autoCloseSec').innerText = secs;
                }
            }
        }, 1000);
    }
    
    function closeResultModal() {
        document.getElementById('resultModal').classList.remove('show');
        if(modalCountdownTimer) clearInterval(modalCountdownTimer);
    }

    function openBetSheet(type) {
        if(isBettingClosed) {
            showToast("Betting closed for this period!", "fa-solid fa-lock");
            return;
        }
        selectedBetType = type;
        
        let headerBg = 'var(--primary-orange)';
        if(type === 'green') headerBg = 'var(--green)';
        if(type === 'red') headerBg = 'var(--red)';
        if(type === 'violet') headerBg = 'var(--violet)';
        if(type === 'small') headerBg = 'var(--blue)';

        let header = document.getElementById('sheetHeader');
        header.style.backgroundColor = headerBg;
        header.style.borderColor = headerBg;
        document.getElementById('betTypeText').innerText = ucFirst(type);
        document.getElementById('confirmBetBtn').style.backgroundColor = headerBg;

        document.getElementById('sheetOverlay').style.display = 'block';
        setTimeout(() => document.getElementById('betSheet').classList.add('open'), 10);
        updateBetTotal();
    }

    function closeBetSheet() {
        document.getElementById('betSheet').classList.remove('open');
        setTimeout(() => document.getElementById('sheetOverlay').style.display = 'none', 300);
    }

    function setBaseAmt(amt, btn) {
        baseAmt = amt;
        document.querySelectorAll('.amt-btn').forEach(b => b.classList.remove('active'));
        btn.classList.add('active');
        updateBetTotal();
    }

    function changeQty(val) {
        betQty += val;
        if(betQty < 1) betQty = 1;
        updateBetTotal();
    }
    
    function setQty(val) {
        betQty = val;
        updateBetTotal();
    }

    function updateBetTotal() {
        document.getElementById('betQty').value = betQty;
        document.getElementById('confirmBetBtn').innerText = `Total Rs${(baseAmt * betQty).toFixed(2)}`;
    }

    function submitBet() {
        let total = baseAmt * betQty;
        
        if(total > getLocalBalance()) {
            showToast("Insufficient balance!", "fa-solid fa-circle-exclamation");
            return;
        }

        deductBalance(total);

        let newBet = {
            id: Date.now(),
            period: currentPeriod,
            bet_on: selectedBetType,
            amount: total,
            status: 'pending',
            payout: 0,
            timestamp: new Date().toISOString()
        };

        let bets = JSON.parse(localStorage.getItem('localBets') || '[]');
        bets.unshift(newBet);
        localStorage.setItem('localBets', JSON.stringify(bets));

        showToast("Bet placed successfully", "fa-solid fa-circle-check");
        closeBetSheet();
        loadMyHistory();
    }

    function switchTab(t) {
        document.getElementById('gameHistory').style.display = (t=='game'?'block':'none');
        document.getElementById('myHistory').style.display = (t=='my'?'flex':'none');
        document.querySelectorAll('.tab-trigger').forEach(el => el.classList.remove('active'));
        event.target.classList.add('active');
        if(t == 'my') loadMyHistory();
    }

    function loadMyHistory() {
        let bets = JSON.parse(localStorage.getItem('localBets') || '[]');
        let html = '';

        bets.forEach(h => {
            let sClass = h.status==='win' ? 'status-win' : (h.status==='loss'?'status-lose':'status-pending');
            let sText = h.status==='win' ? 'Success' : (h.status==='loss'?'Lose':'Pending');
            let amtText = h.status==='win' ? `+Rs${h.payout}` : (h.status==='loss'?`-Rs${h.amount}`:`Rs${h.amount}`);
            
            let iconBg = 'var(--primary-orange)';
            if(['green','small'].includes(h.bet_on)) iconBg = 'var(--green)';
            if(h.bet_on === 'red') iconBg = 'var(--red)';
            if(h.bet_on === 'violet') iconBg = 'var(--violet)';
            if(h.bet_on === 'small') iconBg = 'var(--blue)';

            html += `
            <div class="my-hist-item">
                <div class="my-hist-icon" style="background:${iconBg}">${h.bet_on}</div>
                <div class="my-hist-details">
                    <div class="my-hist-period">${h.period}</div>
                    <div class="my-hist-time">WinGo 30sec</div>
                </div>
                <div class="my-hist-status">
                    <div class="status-badge ${sClass}">${sText}</div>
                    <div class="my-hist-amt" style="color:${h.status==='win'?'var(--green)':'var(--red)'}">${amtText}</div>
                </div>
            </div>`;
        });
        document.getElementById('myHistory').innerHTML = html;
    }

    updateBalance();
    syncGame();
</script>
</body>
</html>