<?php
session_start();
 $isLogin = isset($_SESSION['extplayer']);

// ================= POPUP LOGIN (DB) =================
 $popup_login = null;

 $q_popup = mysqli_query(
  $koneksi,
  "SELECT * FROM tb_popup WHERE status = 'active' ORDER BY id DESC LIMIT 1"
);

if (mysqli_num_rows($q_popup) > 0) {
  $popup_login = mysqli_fetch_assoc($q_popup);
}

 $showLoginPopup = false;

if ($popup_login && $isLogin) {
  if (!isset($_SESSION['login_popup_shown'])) {
    $showLoginPopup = true;
    $_SESSION['login_popup_shown'] = true;
  }
}

// ================= AGREEMENT MODAL =================
 $showAgreementModal = false;
if ($isLogin) {
    if (!isset($_SESSION['agreement_shown'])) {
        $showAgreementModal = true;
        $_SESSION['agreement_shown'] = true;
    }
}

 $userList = [];

 $qUser = mysqli_query(
  $koneksi,
  "SELECT username, avatar 
   FROM tb_user 
   WHERE username IS NOT NULL 
   ORDER BY RAND()
   LIMIT 500"
);

while ($u = mysqli_fetch_assoc($qUser)) {
  $userList[] = [
    'username' => $u['username'],
    'avatar'   => !empty($u['avatar'])
      ? '../uploads/avatar/' . $u['avatar']
      : '../uploads/avatar/sm_avatar4.png'
  ];
}

function maskUsername($username) {
  if (!$username) return '';
  $len = strlen($username);
  if ($len <= 5) {
    return substr($username, 0, 2) . '***';
  }
  return substr($username, 0, 3) . '***' . substr($username, -2);
}

?>

  <main id="main-route">
  <div class="main-content home">
    
    <!-- ================= SLIDER / BANNER ============ -->
     <section class="home__slider">
        <div class="swiper-container main-slider">
          <div class="swiper-wrapper">
            <?php
            $query = mysqli_query($koneksi, "SELECT * FROM tb_banner WHERE status = 'active' ");
            while ($data = mysqli_fetch_array($query)) {
            ?>
              <div class="swiper-slide">
                <a href="#">
                  <img src="../uploads/fotobanner/<?php echo $data['gambar'] ?>" alt="Banner Image">
                </a>
              </div>
            <?php } ?>
          </div>
          <div class="swiper-pagination"></div>
        </div>
      </section>
    <!-- ================= END SLIDER ================= -->
    
    <!-- ================= HOME CATEGORY ============== -->
    <section class="home-category">
      <div class="sgx-container">
        <div class="category-scroll">
          <a class="cat-item active" href="#"><i class="fas fa-fire"></i><span>Top</span></a>
          <a class="cat-item" href="?page=slot"><i class="fas fa-dice"></i><span>Slots</span></a>
          <a class="cat-item" href="?page=togel"><i class="fas fa-sort-numeric-up"></i><span>Togel</span></a>
          <a class="cat-item" href="?page=casino"><i class="fas fa-chess"></i><span>Kasino</span></a>
          <a class="cat-item" href="?page=arcade"><i class="fas fa-gamepad"></i><span>E-Game</span></a>
          <a class="cat-item" href="?page=fishing"><i class="fas fa-fish"></i><span>Fishing</span></a>
          <a class="cat-item" href="?page=sportbok"><i class="fas fa-futbol"></i><span>Sportbok</span></a>
        </div>
      </div>
    </section>
    <!-- ================= END CATEGORY =============== -->
    
    <!-- ============ JACKPOT TERBESAR (DI ATAS, CSS ANIMATION MULUS) ================ -->
    <section class="jackpot-section">
      <div class="container">
        <div class="jackpot-header">
          <h5>Jackpot Terbesar</h5>
          <a href="#" class="btn-semua">
            <span>Semua</span>
            <span class="arrow">▶</span>
          </a>
        </div>
        <div class="jackpot-viewport">
          <div class="jackpot-track" id="jackpotTrack"></div>
        </div>
      </div>
    </section>
    <!-- ================= END JACKPOT ================ -->
    
    <!-- ================= TOGEL SECTION ============== -->
    <section class="togel-section">
      <div class="container">
        <div class="maxwin-header">
          <h5>Togel</h5>
          <a href="?page=togel" class="btn-semua"> 
            <span>Semua</span>
            <span class="arrow">▶</span>
          </a>
        </div>
        <div class="togel-scroll" id="togelGrid"></div>
      </div>
    </section>
    <!-- ================= END TOGEL =============== -->
  
    <!--========= LINK ALTERNATIF SECTION ============= -->
    <section class="altlink-section">
      <div class="sgx-container">
        <div class="altlink-card">
          <div class="altlink-left">
            <h4>Pastikan Selalu Login</h4>
            <p class="sub">Dengan Link Resmi Kami</p>
            <div class="altlink-main">anti wala : akseskoi.rf.gd</div>
            <label class="altlink-label">Pilih Link Alternatif:</label>
            <div class="altlink-select">
              <select>
                <option>koiwin01.rf.gd</option>
                <option>akseskoi.rf.gd</option>
              </select>
              <button>Salin</button>
            </div>
            <div class="altlink-note">* Klik tombol untuk menyalin link yang dipilih</div>
          </div>
          <div class="altlink-right">
            <div class="qr-box">
              <img src="https://storage3.me-qr.com/qr/309518294.png" alt="QR Link">
            </div>
            <div class="apk-btn">
              <i class="bi bi-phone"></i>
              <span>INSTALL APK</span>
            </div>
          </div>
        </div>
      </div>
    </section>
    <!-- ================= END LINK ALTERNATIF ======== -->
    
    <!-- ================= PROVIDER SECTION (BAWAAN, TIDAK DIUBAH) =========== -->
    <section class="provider-section">
      <div class="container">
        <div class="provider-header">
          <h5>Penyedia Game <span class="badge">28</span></h5>
          <a href="#" class="btn-semua">
            <span>Semua</span>
            <span class="arrow">▶</span>
          </a>
        </div>
        <div class="provider-grid">
        <?php
        $folder = "../uploads/game_pic/provider/";
        $allowed = ['png','jpg','jpeg','webp'];
        
        if (is_dir($folder)) {
            $files = scandir($folder);
            foreach ($files as $file) {
                $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                if (in_array($ext, $allowed)) {
                    echo '<div class="provider-card"><img data-src="'.$folder.$file.'" alt="provider"></div>';
                }
            }
        }
        ?>
        </div>
      </div>
    </section>
    <!-- ================= END PROVIDER =============== -->

    <!-- ================= POPULAR GAME =============== -->
    <section class="popular-section">
      <div class="container">
        <div class="popular-header">
          <h5>
            Popular
            <span class="badge">
              <?php
              $q = mysqli_query($koneksi, "SELECT COUNT(*) AS total FROM tb_popular WHERE status='active'");
              echo mysqli_fetch_assoc($q)['total'];
              ?>
            </span>
          </h5>
          <a href="?page=blabla" class="btn-semua">
            <span>Semua</span>
            <span class="arrow">▶</span>
          </a>
        </div>
        <div class="popular-scroll">
          <?php
          $popular = mysqli_query($koneksi, "SELECT * FROM tb_popular WHERE status='active' ORDER BY sort ASC, id DESC");
          while ($p = mysqli_fetch_array($popular)) {
          ?>
            <div class="popular-card">
              <div class="popular-thumb">
                <img src="../uploads/popular/<?php echo $p['image']; ?>">
              </div>
            </div>
          <?php } ?>
        </div>
      </div>
    </section>
    <!-- ================= END POPULAR ================ -->
    
    <!-- ================= FLOATING SLOT MENU ========= -->
    <div class="slot-float-wrap" id="slotFloat">
      <div class="slot-handle" onclick="toggleSlotMenu()"><i class="fas fa-box"></i></div>
      <div class="slot-float-box">
        <a href="?page=rtp" class="slot-item">
          <img data-src="../uploads/game_pic/floating-ico/LiveRTP.png">
          <span>RTP</span>
        </a>
        <div class="divider"></div>
        <a href="?page=roda" class="slot-item">
          <img data-src="../uploads/game_pic/floating-ico/Roulette.png">
          <span>Roda</span>
        </a>
        <div class="slot-collapse" onclick="toggleSlotMenu()"><i class="fas fa-chevron-down"></i></div>
      </div>
    </div>
    <!-- ================= END FLOAT MENU ============= -->
    
    <!-- ================= HOME FOOTER ================ -->
    <footer class="ft-wrap">
      <div class="ft-logo">
        <img data-src="../assets/img/<?php echo $logo ?>" alt="KOIWIN88">
      </div>
      <div class="ft-highlights">
        <div class="hl-item"><i class="fas fa-shield-alt"></i><span>Aman & Terpercaya</span></div>
        <div class="hl-item"><i class="fas fa-bolt"></i><span>Withdraw Cepat</span></div>
        <div class="hl-item"><i class="fas fa-headset"></i><span>CS 24 Jam</span></div>
        <div class="hl-item"><i class="fas fa-gift"></i><span>Bonus & Jackpot</span></div>
      </div>
      <div class="ft-stats">
        <div><strong>80K+</strong><span>Pemain Aktif</span></div>
        <div><strong>1000+</strong><span>Game Slot</span></div>
        <div><strong>24/7</strong><span>Live Support</span></div>
      </div>
      <div class="ft-responsible">🎯 Bermainlah secara bertanggung jawab</div>
      <div class="ft-divider"></div>
      <h4 class="ft-title">ABOUT US</h4>
      <div class="ft-links"><p class="ft-desc"><?php echo $deskripsi ?></p></div>
      <h4 class="ft-title">Informasi</h4>
      <div class="ft-links">
        <a href="#">Tentang Kami</a>
        <a href="#">Situs yang bertanggung jawab</a>
        <a href="#">Pengecualian / Self Exclusion</a>
        <a href="#">Syarat & Ketentuan</a>
        <a href="#">Kebijakan Privasi</a>
        <a href="#">Bantuan</a>
      </div>
      <h4 class="ft-title">Hubungi kami</h4>
      <a href="mailto:helpkoiwin@gmail.com" class="ft-email">helpkoiwin@gmail.com</a>
      <div class="ft-social">
        <a href="<?php echo $facebok ?>"><i class="fab fa-facebook-f"></i></a>
        <a href="<?php echo $telegram ?>"><i class="fab fa-telegram-plane"></i></a>
        <a href="https://wa.me/<?php echo $whatsapp ?>"><i class="fab fa-whatsapp"></i></a>
      </div>
      <div class="ft-info">
        <span>✔ Berlisensi</span>
        <span>🔒 Keamanan Privasi</span>
        <span class="ft-age">18+</span>
      </div>
      <div class="ft-copy">© 2025 KOIWIN. Hak cipta dilindungi undang-undang.</div>
    </footer>
    <!-- ================= END FOOTER ================= -->
    
  </div>
</div>
</div>
  <!-- ================= END MAIN CONTENT ================= -->
</main>

<?php if ($showLoginPopup): ?>
<div class="home-popup-overlay" id="loginPopup">
  <div class="home-popup">
    <button class="popup-close" onclick="closeLoginPopup()">✕</button>
    <?php if (!empty($popup_login['gambar'])): ?>
      <div class="popup-image">
        <img data-src="../uploads/popup/<?= htmlspecialchars($popup_login['gambar']); ?>" alt="Popup Image">
      </div>
    <?php endif; ?>
    <h3><?= htmlspecialchars($popup_login['title']); ?></h3>
    <p><?= nl2br(htmlspecialchars($popup_login['description'])); ?></p>
    <div class="popup-actions">
      <a href="#" onclick="closeLoginPopup()" class="btn-popup ghost">Tutup</a>
    </div>
  </div>
</div>
<?php endif; ?>

<?php if ($showAgreementModal): ?>
<div id="agreementModal" class="agreement-modal">
  <div class="agreement-box">
    <div class="agreement-header">
      <img src="../assets/img/<?php echo $logo ?>" alt="Logo" class="agreement-logo">
      <h3>PERATURAN PERMAINAN</h3>
      <p>Mohon dibaca dengan seksama sebelum melanjutkan</p>
    </div>
    <div class="agreement-content">
      <p class="intro">Selamat datang! Dengan melakukan pendaftaran dan bermain di platform kami, Anda dianggap telah membaca, memahami, dan menyetujui seluruh peraturan yang tercantum di bawah ini tanpa adanya paksaan dari pihak manapun.</p>
      <h3>I. Pendaftaran dan Keamanan Akun</h3>
      <ul>
        <li>Seluruh data yang diisikan pada saat pendaftaran wajib <b>BENAR, LENGKAP, dan ASLI</b>.</li>
        <li>Kesalahan atau ketidaksesuaian data sepenuhnya menjadi tanggung jawab member.</li>
        <li>Satu identitas hanya diperbolehkan untuk satu akun.</li>
      </ul>
      <h3>II. Deposit dan Withdraw</h3>
      <ul>
        <li>Proses <b>Deposit dan Withdraw</b> hanya dilakukan sesuai dengan nama dan nomor rekening yang terdaftar.</li>
        <li>Deposit yang dikirim menggunakan rekening atas nama orang lain / pihak ketiga <b>tidak dapat diproses</b> dan akan dianggap <b>HANGUS / DONASI</b>.</li>
        <li>Member diwajibkan untuk selalu memeriksa <b>nama dan nomor rekening tujuan</b> sebelum melakukan transfer.</li>
        <li>Member dengan nama rekening bank tunggal <b>WAJIB</b> melakukan deposit menggunakan <b>nominal unik / acak</b>.</li>
        <li>Ketentuan nominal unik juga berlaku untuk <b>transfer antar bank (Online Transfer)</b>.</li>
      </ul>
      <h3>III. Aturan Betting dan Permainan</h3>
      <ul>
        <li>Betting dinyatakan <b>SAH</b> hanya pada saat pasaran betting telah dibuka.</li>
        <li>Seluruh betting atau pembelian yang telah dilakukan <b>tidak dapat dibatalkan</b> dengan alasan apapun.</li>
        <li>Kesalahan dalam penginputan taruhan sepenuhnya menjadi <b>tanggung jawab member</b>.</li>
      </ul>
      <h3>IV. Tindakan Tegas dan Mutlak</h3>
      <ul>
        <li>Kami akan melakukan tindakan tegas terhadap segala bentuk <b>penipuan deposit berulang</b> maupun <b>permainan tidak wajar</b>.</li>
        <li>Setiap akun yang terbukti melakukan pelanggaran akan dikenakan <b>BANNED PERMANEN</b> tanpa pengecualian.</li>
        <li>Seluruh keputusan <b>KOIWIN</b> bersifat <b>MUTLAK dan FINAL</b>.</li>
      </ul>
      <p class="closing">Dengan melanjutkan permainan, Anda menyatakan <b>SETUJU dan PATUH</b> terhadap seluruh peraturan yang berlaku.</p>
      <p class="thanks">Terima kasih — Selamat bermain dan Salam JP! 🎉</p>
    </div>
    <div class="agreement-footer">
      <button class="btn-decline" onclick="rejectAgreement()">Tidak Setuju</button>
      <button class="btn-accept" onclick="acceptAgreement()">Setuju</button>
    </div>
  </div>
</div>
<style>
    .agreement-modal { position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.85); z-index:9999; display:flex; justify-content:center; align-items:center; padding:15px; box-sizing:border-box; }
    .agreement-box { background:#0f0f15; width:100%; max-width:400px; max-height:90vh; border-radius:15px; border:1px solid #333; display:flex; flex-direction:column; overflow:hidden; box-shadow:0 0 20px rgba(0,0,0,0.5); }
    .agreement-header { text-align:center; padding:20px 15px 10px; border-bottom:1px solid #222; }
    .agreement-header img { height:45px; margin-bottom:10px; }
    .agreement-header h3 { color:#ffd700; font-size:18px; margin:0 0 5px 0; text-transform:uppercase; }
    .agreement-header p { color:#666; font-size:11px; margin:0; }
    .agreement-content { padding:15px 20px; color:#ccc; font-size:12px; line-height:1.6; overflow-y:auto; flex:1; text-align:justify; }
    .agreement-content::-webkit-scrollbar { width:4px; }
    .agreement-content::-webkit-scrollbar-track { background:#111; }
    .agreement-content::-webkit-scrollbar-thumb { background:#ffd700; border-radius:10px; }
    .agreement-content h3 { color:#fff; font-size:13px; margin-top:15px; margin-bottom:8px; border-left:3px solid #ffd700; padding-left:8px; }
    .agreement-content ul { margin:0; padding-left:20px; }
    .agreement-content li { margin-bottom:5px; color:#aaa; }
    .agreement-content b, .agreement-content strong { color:#fff; }
    .agreement-content .intro, .agreement-content .closing, .agreement-content .thanks { color:#aaa; font-style:italic; }
    .agreement-footer { display:flex; gap:10px; padding:15px 20px; border-top:1px solid #222; background:#0b0b10; }
    .btn-decline, .btn-accept { flex:1; padding:12px; border:none; border-radius:8px; font-weight:bold; font-size:13px; cursor:pointer; transition:0.2s; }
    .btn-decline { background:#222; color:#888; border:1px solid #333; }
    .btn-decline:hover { background:#333; color:#fff; }
    .btn-accept { background:linear-gradient(to right, #b8860b, #ffd700); color:#000; box-shadow:0 4px 10px rgba(255,215,0,0.2); }
    .btn-accept:hover { transform:scale(1.02); box-shadow:0 4px 15px rgba(255,215,0,0.4); }
</style>

<script>
  document.addEventListener("DOMContentLoaded", function () {
    var m = document.getElementById("agreementModal");
    if(m){ m.style.display="flex"; document.body.style.overflow="hidden"; }
  });
  function acceptAgreement() { document.getElementById("agreementModal").style.display="none"; document.body.style.overflow=""; }
  function rejectAgreement() { window.location.href="function/logout.php"; }
</script>
<?php endif; ?>

<!-- ================= HOME PAGE STYLE ================= -->
<link rel="stylesheet" id="templateStyle" type="text/css" href="luxury-them/custom/css/home.css?v=2.6.0.0">
<link rel="stylesheet" id="templateStyle2" type="text/css" href="luxury-them/custom/css/slot.css?v=2.6.0.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css?v=2.6.0.0" rel="stylesheet">

<script>
    function toggleSlotMenu() {
      document.getElementById("slotFloat").classList.toggle("closed");
    }
</script>

<script>
    const games = [
      { local: "../uploads/game_pic/MAXWIN/Mahjong-Ways.jpeg",  weight: 5 },
      { local: "../uploads/game_pic/MAXWIN/gatesolympus.jpeg",  weight: 3 },
      { local: "../uploads/game_pic/MAXWIN/wildbounty.jpeg",    weight: 2 },
      { local: "../uploads/game_pic/MAXWIN/Wild_Bandito.jpeg",  weight: 2 },
      { local: "../uploads/game_pic/MAXWIN/Mahjong-Ways2.jpeg", weight: 1 }
    ];
</script>

<script>
const userList = <?php echo json_encode($userList, JSON_UNESCAPED_SLASHES); ?>;
</script>

<script>
function maskUsername(username) {
  if (!username) return '';
  if (username.length <= 5) return username.substring(0, 2) + '***';
  return username.substring(0, 3) + '***' + username.slice(-2);
}
function randomUser() {
  var u = userList[Math.floor(Math.random() * userList.length)];
  return { name: maskUsername(u.username), avatar: u.avatar };
}
</script>

<script>
function randomJackpot() {
  var tiers = [
    function(){ return 300000 + Math.floor(Math.random() * 200000); },
    function(){ return 400000 + Math.floor(Math.random() * 300000); },
    function(){ return 500000 + Math.floor(Math.random() * 300000); },
    function(){ return 800000 + Math.floor(Math.random() * 700000); },
    function(){ return 1500000 + Math.floor(Math.random() * 1500000); }
  ];
  var w = [3,3,3,2,1], pool = [];
  tiers.forEach(function(fn,i){ for(var j=0;j<w[i];j++) pool.push(fn); });
  return "Rp " + pool[Math.floor(Math.random()*pool.length)]().toLocaleString("id-ID");
}
</script>

<script>
var track = document.getElementById("maxwinTrack");
function generateCards() {
  var html = "";
  for (var i = 0; i < 8; i++) {
    var g = games[Math.floor(Math.random() * games.length)];
    var u = randomUser();
    html += '<div class="maxwin-card"><img src="'+g.local+'"><div class="maxwin-amount">'+randomJackpot()+'</div><div class="maxwin-user"><img src="'+u.avatar+'" class="avatar"><span>'+u.name+'</span></div></div>';
  }
  return html;
}
track.innerHTML = generateCards() + generateCards();
setInterval(function(){ track.innerHTML = generateCards() + generateCards(); }, 15000);
</script>

<script>
document.addEventListener("DOMContentLoaded", function () {
  var popup = document.getElementById("loginPopup");
  if (popup) popup.style.display = "flex";
});
function closeLoginPopup() {
  var popup = document.getElementById("loginPopup");
  if (popup) popup.style.display = "none";
}
</script>

<!-- ================= JACKPOT INFINITE SCROLL ================= -->
<style>
.jackpot-section {
  padding: 10px 0 2px;
  position: relative;
}
.jackpot-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding: 0 4px;
}
.jackpot-header h5 {
  color: #ffffff;
  font-size: 15px;
  font-weight: 700;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 7px;
}
.jackpot-header h5::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 16px;
  background: #ffffff;
  border-radius: 2px;
}

.jackpot-viewport {
  overflow: hidden;
  position: relative;
  mask-image: linear-gradient(to right, transparent 0%, black 4%, black 96%, transparent 100%);
  -webkit-mask-image: linear-gradient(to right, transparent 0%, black 4%, black 96%, transparent 100%);
}

.jackpot-track {
  display: flex;
  gap: 10px;
  width: max-content;
  will-change: transform;
  animation: jpScroll var(--jp-dur, 60s) linear infinite;
}
.jackpot-viewport:hover .jackpot-track {
  animation-play-state: paused;
}
@keyframes jpScroll {
  0%   { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}

.jackpot-card {
  flex: 0 0 120px;
  background: linear-gradient(155deg, rgba(26,26,46,0.96), rgba(14,14,30,0.98));
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 10px;
  overflow: hidden;
  position: relative;
  transition: transform 0.3s cubic-bezier(0.22,1,0.36,1), border-color 0.3s, box-shadow 0.3s;
  cursor: default;
}
.jackpot-card:hover {
  transform: translateY(-3px) scale(1.02);
  border-color: rgba(255,255,255,0.2);
  box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}

.jackpot-thumb {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: #0a0a14;
}
.jackpot-thumb img {
  width: 100%;
  height: auto;
  display: block;
}

.rank-badge {
  position: absolute;
  top: 5px;
  left: 5px;
  background: rgba(0,0,0,0.6);
  color: #ffffff;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 5px;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  z-index: 2;
  line-height: 1;
}
.rank-badge i { font-size: 7px; color: #f5c842; }
.rank-badge.rank-1 {
  background: rgba(0,0,0,0.7);
  animation: rPulse 2s ease-in-out infinite;
}
@keyframes rPulse {
  0%,100% { box-shadow: 0 2px 8px rgba(245,200,66,0.3); }
  50% { box-shadow: 0 2px 14px rgba(245,200,66,0.6); }
}

.jackpot-amount {
  padding: 6px 8px 2px;
  color: #ffffff;
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.jackpot-amount.big {
  color: #ffffff;
  font-size: 12px;
}

.jackpot-user {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px 7px;
}
.jackpot-user img {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}
.jackpot-user span {
  color: rgba(255,255,255,0.55);
  font-size: 9px;
  font-weight: 500;
  white-space: nowrap;
}

@media (max-width: 480px) {
  .jackpot-card { flex: 0 0 105px; }
  .jackpot-amount { font-size: 10px; }
  .jackpot-amount.big { font-size: 11px; }
  .jackpot-user span { font-size: 8px; }
}
</style>

<!-- ================= JACKPOT ENGINE — POOL GABUNGAN ================= -->
<script>
(function(){
  var jpGames = [
    {img:'../uploads/game_pic/MAXWIN/Mahjong-Ways.jpeg',name:'Mahjong Ways'},
    {img:'../uploads/game_pic/MAXWIN/Mahjong-Ways2.jpeg',name:'Mahjong Ways 2'},
    {img:'../uploads/game_pic/MAXWIN/gatesolympus.jpeg',name:'Gates of Olympus'},
    {img:'../uploads/game_pic/MAXWIN/gatesuper.jpeg',name:'Gates Super'},
    {img:'../uploads/game_pic/MAXWIN/wildbounty.jpeg',name:'Wild Bounty'},
    {img:'../uploads/game_pic/MAXWIN/Wild_Bandito.jpeg',name:'Wild Bandito'},
    {img:'../uploads/game_pic/MAXWIN/Garuda_Gems.jpeg',name:'Garuda Gems'},
    {img:'../uploads/game_pic/MAXWIN/grims_bounty.jpeg',name:"Grim's Bounty"},
    {img:'../uploads/game_pic/MAXWIN/jack_the.jpeg',name:'Jack the Giant'},
    {img:'../uploads/game_pic/MAXWIN/kraken.jpeg',name:'Kraken'},
    {img:'../uploads/game_pic/MAXWIN/Leprechaun_Riches.jpeg',name:'Leprechaun Riches'},
    {img:'../uploads/game_pic/MAXWIN/Lucky_Neko.jpeg',name:'Lucky Neko'},
    {img:'../uploads/game_pic/MAXWIN/majestic_empire.jpeg',name:'Majestic Empire'},
    {img:'../uploads/game_pic/MAXWIN/medusa.jpeg',name:'Medusa'},
    {img:'../uploads/game_pic/MAXWIN/pharoah.jpeg',name:'Pharaoh'},
    {img:'../uploads/game_pic/MAXWIN/Piggy_Gold.jpeg',name:'Piggy Gold'},
    {img:'../uploads/game_pic/MAXWIN/Caishen_Wins.jpeg',name:'Caishen Wins'},
    {img:'../uploads/game_pic/MAXWIN/The_Great_Icescape.jpeg',name:'The Great Icescape'},
    {img:'../uploads/game_pic/MAXWIN/Treasures_of_Aztec.jpeg',name:'Treasures of Aztec'},
    {img:'../uploads/game_pic/MAXWIN/MegaSicBo.webp',name:'Mega Sic Bo'}
  ];

  var jpAvatars = [
    '../uploads/avatar/sm_avatar2.png',
    '../uploads/avatar/sm_avatar3.png',
    '../uploads/avatar/sm_avatar4.png',
    '../uploads/avatar/sm_avatar5.png'
  ];

  var jpNames = ['Aki','an','gib','bil','bis','And','Sob','Bak','Afr','Ren','Jua','Kir','Dad','Mas','eko','jok','faj','gal','bud','don','had','ind','riz','ilm'];
  var jpSuffixes = ['14','89','id','jp','as','in','us','al','88','90','85','74','76','51','53','69','87','67','91','17','35','13','99','26','77','32','07','02','05'];

  function R(a){ return a[Math.floor(Math.random()*a.length)]; }

  function jpAmt(){
    var v = Math.floor(Math.random()*90000000)+10000000;
    return 'Rp '+v.toLocaleString('id-ID');
  }

  function buildHTML(){
    var h = '';
    for(var i=0;i<20;i++){
      var g = R(jpGames);
      var amt = jpAmt();
      var big = parseInt(amt.replace(/[^\d]/g,''),10) >= 50000000;
      var rank = i+1;
      h += '<div class="jackpot-card">'
        +'<div class="jackpot-thumb">'
        +'<img src="'+g.img+'" alt="'+g.name+'" loading="lazy">'
        +'<span class="rank-badge'+(rank===1?' rank-1':'')+'"><i class="fas fa-crown"></i> '+rank+'</span>'
        +'</div>'
        +'<div class="jackpot-amount'+(big?' big':'')+'">'+amt+'</div>'
        +'<div class="jackpot-user">'
        +'<img src="'+R(jpAvatars)+'" alt="avatar">'
        +'<span>'+R(jpNames)+'***'+R(jpSuffixes)+'</span>'
        +'</div>'
        +'</div>';
    }
    return h;
  }

  var track = document.getElementById('jackpotTrack');
  var oneSet = buildHTML();
  track.innerHTML = oneSet + oneSet;

  function calcDur(){
    var w = track.scrollWidth / 2;
    var sec = Math.ceil(w / 35);
    track.style.setProperty('--jp-dur', sec+'s');
  }
  calcDur();
  window.addEventListener('resize', calcDur);

})();
</script>

<!-- ================= TOGEL STYLE (BAWAAN) ================= -->
<style>
.togel-section { margin: 12px 0; }
.togel-scroll { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; }
.togel-scroll::-webkit-scrollbar { display: none; }
.togel-card {
  flex: 0 0 calc(50% - 5px); width: calc(50% - 5px); padding: 14px;
  border-radius: 8px; color: #fff; position: relative; overflow: hidden; min-height: 150px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.08), 0 8px 18px rgba(0,0,0,.4);
}
.togel-card::after {
  content: ""; position: absolute; inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='120' height='120' filter='url(%23n)' opacity='0.05'/%3E%3C/svg%3E");
  pointer-events: none;
}
.togel-card.theme-wuhan { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #6a2b2b 0%, #2a1212 60%, #120707 100%); }
.togel-card.theme-macau { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #2c3f63 0%, #121e34 60%, #070c16 100%); }
.togel-card.theme-bull { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #4e2f6a 0%, #241238 60%, #0f0716 100%); }
.togel-card.theme-sydney { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #2a5e5e 0%, #133535 60%, #071616 100%); }
.togel-card.theme-singapore { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #6a5330 0%, #3a2a16 60%, #161007 100%); }
.togel-card.theme-pcso { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #6a2f2f 0%, #351515 60%, #160707 100%); }
.togel-card.theme-taiwan { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #3a4f6a 0%, #1b273a 60%, #0a0f16 100%); }
.togel-card.theme-hongkong { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #5a2a2a 0%, #2a1414 60%, #120707 100%); }
.togel-card.theme-macau-p1 { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.10), transparent 35%), linear-gradient(145deg, #3a4f6a 0%, #1b273a 60%, #0a0f16 100%); }
.togel-card.theme-oregon { background: radial-gradient(circle at 85% 10%, rgba(255,255,255,.08), transparent 35%), linear-gradient(145deg, #444 0%, #222 60%, #0f0f0f 100%); }
.togel-top { display: flex; justify-content: space-between; align-items: center; }
.togel-title { font-size: 11px; letter-spacing: .5px; opacity: .85; }
.togel-logo { width: 53px; height: 53px; border-radius: 50%; background: rgba(255,255,255,.9); padding: 2px; }
.togel-number { font-size: 24px; font-weight: 700; margin: 8px 0; letter-spacing: 1px; }
.togel-number::after { content: ""; display: block; height: 1px; margin-top: 6px; background: linear-gradient(to right, transparent, rgba(255,255,255,.2), transparent); }
.togel-timer { display: flex; gap: 5px; margin-top: 6px; }
.togel-timer div { background: rgba(0,0,0,.35); border-radius: 6px; padding: 4px 5px; min-width: 36px; text-align: center; font-size: 9px; }
.togel-timer strong { display: block; font-size: 12px; font-weight: 600; }
.togel-btn { width: 100%; margin-top: 35px; padding: 7px; border-radius: 6px; background: linear-gradient(to bottom, rgba(255,255,255,.18), rgba(255,255,255,.08)); color: #fff; border: none; font-size: 12px; font-weight: 600; cursor: pointer; transition: all .2s ease; }
.togel-btn:hover { background: linear-gradient(to bottom, rgba(255,255,255,.28), rgba(255,255,255,.12)); }
</style>

<script>
document.addEventListener('DOMContentLoaded', function(){
  var grid = document.getElementById('togelGrid');
  if (!grid) return;
  var shimmer = function(){ grid.innerHTML=''; for(var i=0;i<4;i++) grid.innerHTML+='<div class="sgx-shimmer"></div>'; };
  var loadTogel = function(){
    shimmer();
    fetch('template/page/game/ajax/ajax-togel.php')
      .then(function(r){ return r.text(); })
      .then(function(html){
        var temp = document.createElement('div');
        temp.innerHTML = html;
        var content = temp.querySelector('.togel-scroll');
        if (content) { grid.innerHTML = content.innerHTML; startTogelTimer(); checkResults(); }
        else { grid.innerHTML = 'Gagal memuat togel'; }
      })
      .catch(function(){ grid.innerHTML = 'Gagal memuat togel'; });
  };
  loadTogel();
});
</script>

<script>
function checkResults() {
  var cards = document.querySelectorAll(".togel-card");
  cards.forEach(function(card){
    var title = card.querySelector(".togel-title").textContent;
    var numberEl = card.querySelector(".togel-number");
    var mapping = { "HONGKONG":"p23530", "4D-MACAU":"m17", "SYDNEY":"p23517", "SINGAPORE":"p23521", "OREGON 4":"p23514", "PSCO":"p23526", "BULLSEYE":"p23516", "TAIWAN":"p23527" };
    var code = mapping[title];
    if (!code) return;
    fetch("template/page/game/ajax/check-result.php?code=" + code)
      .then(function(res){ return res.json(); })
      .then(function(data){ if(data.updated) numberEl.textContent = data.result; });
  });
}
</script>

<!-- SCRIPT LIVECHAT -->
<script>
    window.__lc = window.__lc || {};
    window.__lc.license = <?php echo $id_livechat; ?>;
    ;(function(n,t,c){function i(n){return e._h?e._h.apply(null,n):e._q.push(n)}var e={_q:[],_h:null,_v:"2.0",on:function(){i(["on",c.call(arguments)])},once:function(){i(["once",c.call(arguments)])},off:function(){i(["off",c.call(arguments)])},get:function(){if(!e._h)throw new Error("[LiveChatWidget] You can't use getters before load.");return i(["get",c.call(arguments)])},call:function(){i(["call",c.call(arguments)])},init:function(){var n=t.createElement("script");n.async=!0;n.type="text/javascript";n.src="https://cdn.livechatinc.com/tracking.js",t.head.appendChild(n)}};!n.__lc.asyncInit&&e.init(),n.LiveChatWidget=n.LiveChatWidget||e}(window,document,[].slice))
</script>

<script>
    $(document).ready(function() {
        setTimeout(function() {
            var chatBtn = $('#LiveChatWidget');
            chatBtn.css({ 'bottom':'70px', 'right':'15px', 'z-index':'999 !important', 'transform':'scale(0.8)', 'transform-origin':'bottom right' });
            $('div[id^="livechat-full"]').css('bottom', '140px');
        }, 2000);
    });
</script>