<section>
  <h2 class="title-head" id="liveresults">Today Indian Matka Live Results</h2>
  <div class="card-box">
  
<?php
date_default_timezone_set("Asia/Kolkata");
$today = date('Y-m-d');
$currentTime = date('H:i:s');
$currentDay = date('N'); // 1 = Monday, 7 = Sunday

// ✅ Fetch games within -30min to +15min window of open/close time
$stmtLive = $db->prepare("
    SELECT * FROM games 
    WHERE (
        (TIME(time_open) BETWEEN SUBTIME(:now_open_start, '00:30:00') AND ADDTIME(:now_open_end, '00:15:00'))
        OR
        (TIME(time_close) BETWEEN SUBTIME(:now_close_start, '00:30:00') AND ADDTIME(:now_close_end, '00:15:00'))
    )
    AND is_holiday = 0 
    AND is_active = 1
");

$stmtLive->execute([
    ':now_open_start' => $currentTime,
    ':now_open_end' => $currentTime,
    ':now_close_start' => $currentTime,
    ':now_close_end' => $currentTime,
]);

$liveGames = $stmtLive->fetchAll(PDO::FETCH_ASSOC);

// ✅ Agar koi live game nahi mila
if (empty($liveGames)) {
    echo "<div style='text-align:center; color:var(--text-muted); font-size:16px; margin:15px 0;'>
            🕒 Abhi koi game chalu nahi hai
          </div>";
    // ❌ No 'exit;' here — so page continues loading
} else {
    
    $totalGames = count($liveGames);
    $counter = 0;

    foreach ($liveGames as $game) {
        $counter++;
        $gameId = $game['game_id'];
        $title = $game['title'];
        $alias = $game['alias'];
        $noteOpen = '';
        $noteClose = '';
        $passing = '';

        // Fetch today's result
        $stmtResult = $db->prepare("SELECT * FROM results WHERE game_id = ? AND draw_date = ?");
        $stmtResult->execute([$gameId, $today]);
        $result = $stmtResult->fetch(PDO::FETCH_ASSOC);

        $open = $result['open_digits'] ?? '';
        $close = $result['close_digits'] ?? '';
        $noteOpen = $result['note_open'] ?? '';
        $noteClose = $result['note_close'] ?? ''; 
        $passing = $result['passing'] ?? '';

        // ✅ Result text formatting
        if ($open || $close) {
            $sumOpen = $open ? array_sum(str_split($open)) % 10 : '';
            $sumClose = $close ? array_sum(str_split($close)) % 10 . '-' : '';
            $resultText = "$open-$sumOpen$sumClose$close";
        } else {
            $resultText = "<span style='color:var(--text-muted); font-style:italic;'>⏳ Loading...</span>";
        }

        // ✅ SET 1 structure implementation
        // Agar ye last item hai toh SET 1 ki tarah border-bottom hata denge
        $articleStyle = ($counter == $totalGames) ? " style='border-bottom:none; padding-bottom:15px;'" : "";

        echo "<article{$articleStyle}>";
        echo "  <div class='txt-lg color-secondary'>$title</div>";
        echo "  $resultText<br>";

        // Passing
        if ($passing) {
            echo "  <small><span style='color:green;'>$passing</span></small><br>";
        }

        // Notes
        if ($noteClose) echo "  <small>$noteClose</small><br>";
        if ($noteOpen) echo "  <small>$noteOpen</small><br>";

        // Refresh button
        echo "  <button class='btn-gradient' onclick='window.location.reload()' style='margin-top:10px;'>Refresh</button>";
        echo "</article>";
    }
}
?>

  </div>
</section>