<?php
include 'db.php';
date_default_timezone_set('Asia/Kolkata');

// etch latest update time
$last = $conn->query("SELECT MAX(updated_at) AS last_update FROM satta_results")->fetch_assoc();

// Fetch all game results
$results = $conn->query("SELECT * FROM satta_results ORDER BY id ASC");

// Fetch disclaimer configuration
$disclaimer_config = $conn->query("SELECT * FROM disclaimer_config WHERE id = 1")->fetch_assoc();
if (!$disclaimer_config) {
    // Insert default configuration if not exists
    $conn->query("INSERT INTO disclaimer_config (disclaimer_text, show_disclaimer, footer_image) VALUES ('This is for informational purposes only.', 1, '')");
    $disclaimer_config = $conn->query("SELECT * FROM disclaimer_config WHERE id = 1")->fetch_assoc();
}

$today = date("F d, Y");
$time = date("h:i:s A");

// Check if we have results
$has_results = $results->num_rows > 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BHUTAN FATAFAT - <?php echo $today; ?></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<meta name="google-site-verification" content="cDVatqLjHOLJyML7p5bs8CJ3iCdLbkUmiy0qPjGtOIM" />

<style>

.result-card{
  border:1px solid #999;
  margin-bottom:15px;
  background:#fff;
  font-family: Arial, sans-serif;
}

.result-date{
  background:#e5e5e5;
  text-align:center;
  font-weight:bold;
  padding:8px;
  border-bottom:1px solid #999;
}

.result-table{
  width:100%;
  border-collapse:collapse;
}

.result-table td{
  border:1px solid #999;
  text-align:center;
  padding:10px 4px;
  font-size:14px;
}

/* Grey row */
.grey-row td{
  background:#f0f0f0;
  color:#000;
  font-weight:600;
}

/* Yellow row */
.yellow-row td{
  background:#fff4b2;
  font-size:18px;
  font-weight:bold;
}



  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    margin: 0;
    padding: 0;
    color: #333;
    min-height: 100vh;
  }

  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
  }

  header {
    background: linear-gradient(45deg, #dc2626, #b91c1c);
    color: #fff;
    text-align: center;
    padding: 18px 15px;
    font-size: 18px;
    border-radius: 12px 12px 0 0;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    line-height: 1.5;
  }

  .updated {
    background: linear-gradient(45deg, #059669, #047857);
    color: #fff;
    text-align: center;
    padding: 15px;
    font-size: 18px;
    font-weight: bold;
    box-shadow: 0 3px 12px rgba(0,0,0,0.1);
    line-height: 1.5;
  }

  .date-display {
    background: #1f2937;
    color: #facc15;
    text-align: center;
    padding: 15px;
    font-size: 22px;
    font-weight: bold;
    line-height: 1.4;
  }

  .result-table {
    width: 100%;
    margin: 0 auto;
    border-collapse: collapse;
    background: #fff;
    box-shadow: 0 6px 20px rgba(0,0,0,0.15);
    border-radius: 0 0 12px 12px;
    overflow: hidden;
    font-size: 16px;
  }

  .result-table th {
    background: #1f2937;
    color: #fff;
    padding: 18px 12px;
    text-transform: uppercase;
    font-size: 16px;
    font-weight: 700;
    text-align: center;
    letter-spacing: 0.5px;
  }

  .result-table td {
    padding: 16px 12px;
    text-align: center;
    border-bottom: 2px solid #e5e7eb;
    transition: all 0.3s ease;
  }

  .result-table tr:nth-child(even) {
    background: #f8fafc;
  }

  .result-table tr:hover {
    background: #f1f5f9;
    transform: translateY(-2px);
  }

  .region {
    text-align: left;
    font-weight: bold;
    color: #1f2937;
  }

  .game-name {
    font-size: 18px;
    color: #1f2937;
    margin-bottom: 6px;
    line-height: 1.4;
  }

  .time-label {
    color: #000;
    font-size: 14px;
    font-weight: normal;
    margin-bottom: 8px;
  }

  .record {
    color: #3b82f6;
    font-size: 14px;
    text-decoration: none;
    display: inline-block;
    margin-top: 6px;
    padding: 6px 12px;
    border: 2px solid #3b82f6;
    border-radius: 6px;
    transition: all 0.3s ease;
    font-weight: 600;
  }

  .record:hover {
    background: #3b82f6;
    color: white;
    text-decoration: none;
    transform: translateY(-2px);
  }

  /* ✅ UPDATED: EVEN BIGGER RESULT NUMBERS */
  .result-number {
    font-size: 32px;
    font-weight: 900;
    color: #000;
    background: linear-gradient(135deg, #fef2f2, #fecaca);
    padding: 15px 20px;
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 80px;
    min-height: 70px;
    border: 4px solid #fecaca;
    box-shadow: 0 6px 15px rgba(220, 38, 38, 0.3);
    transition: all 0.3s ease;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
  }

  .result-number:hover {
    transform: scale(1.1);
    box-shadow: 0 8px 20px rgba(220, 38, 38, 0.4);
    background: linear-gradient(135deg, #fecaca, #fef2f2);
  }

  .footer-btn {
    display: block;
    width: 100%;
    background: linear-gradient(45deg, #374151, #1f2937);
    color: #fff;
    text-align: center;
    padding: 18px;
    font-weight: bold;
    text-decoration: none;
    font-size: 18px;
    transition: all 0.3s ease;
    border-radius: 0 0 12px 12px;
    line-height: 1.4;
    letter-spacing: 0.5px;
  }

  .footer-btn:hover {
    background: linear-gradient(45deg, #1f2937, #111827);
    color: #facc15;
    text-decoration: none;
    transform: translateY(-2px);
  }

  /* ✅ UPDATED: Bigger Full Screen Footer */
  .fullscreen-footer {
    width: 100%;
    background: linear-gradient(45deg, #059669, #047857);
    color: #fff;
    text-align: center;
    padding: 0;
    margin-top: 30px;
    box-shadow: 0 -4px 15px rgba(0,0,0,0.1);
  }

  .footer-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 30px 20px;
  }

  .footer-fullscreen-image {
    width: 100%;
    max-height: 200px;
    object-fit: cover;
    border: none;
    box-shadow: none;
    transition: all 0.3s ease;
    display: block;
    border-radius: 10px;
  }

  .footer-fullscreen-image:hover {
    transform: scale(1.02);
    opacity: 0.95;
  }

  .footer-text {
    margin-top: 15px;
    z-index: 2;
    position: relative;
    font-size: 18px;
  }

  .footer-text small {
    opacity: 0.9;
    font-size: 16px;
  }

  .stats-bar {
    display: flex;
    justify-content: space-around;
    background: #1e40af;
    color: white;
    padding: 15px;
    margin-bottom: 25px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  }

  .stat-item {
    text-align: center;
  }

  .stat-number {
    font-size: 28px;
    font-weight: bold;
    color: #facc15;
  }

  .stat-label {
    font-size: 16px;
    opacity: 0.9;
    font-weight: 600;
  }

  .no-results {
    text-align: center;
    padding: 50px 30px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  }

  .no-results i {
    font-size: 64px;
    color: #6b7280;
    margin-bottom: 20px;
  }

  .no-results h3 {
    font-size: 28px;
    margin-bottom: 15px;
    color: #1f2937;
  }

  .no-results p {
    font-size: 18px;
    color: #6b7280;
  }

  .auto-refresh {
    background: #fbbf24;
    color: #78350f;
    padding: 8px 15px;
    border-radius: 20px;
    font-size: 14px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: 12px;
    font-weight: 600;
  }

  /* ✅ MOBILE STYLES WITH BIGGER EVERYTHING */
  @media (max-width: 768px) {
    .container {
      padding: 15px;
    }
    
    header {
      font-size: 16px;
      padding: 15px 12px;
      border-radius: 10px 10px 0 0;
    }
    
    .updated {
      font-size: 16px;
      padding: 12px;
    }
    
    .date-display {
      font-size: 18px;
      padding: 12px;
    }
    
    .result-table {
      font-size: 14px;
      border-radius: 0 0 10px 10px;
    }
    
    .result-table th {
      padding: 14px 8px;
      font-size: 14px;
    }
    
    .result-table td {
      padding: 14px 8px;
    }
    
    .game-name {
      font-size: 16px;
    }
    
    .time-label {
      font-size: 16px;
    }
    
    /* ✅ BIG RESULT NUMBERS ON MOBILE */
    .result-number {
      font-size: 24px;
      padding: 12px 16px;
      min-width: 65px;
      min-height: 55px;
      border: 3px solid #fecaca;
    }
    
    .record {
      font-size: 12px;
      padding: 4px 8px;
    }
    
    .footer-btn {
      font-size: 16px;
      padding: 15px;
    }
    
    .footer-fullscreen-image {
      max-height: 150px;
    }
    
    .footer-text {
      font-size: 16px;
    }
    
    .footer-text small {
      font-size: 14px;
    }
    
    .stats-bar {
      padding: 12px;
      margin-bottom: 20px;
      border-radius: 10px;
    }
    
    .stat-number {
      font-size: 22px;
    }
    
    .stat-label {
      font-size: 12px;
    }
    
    .no-results {
      padding: 40px 25px;
    }
    
    .no-results i {
      font-size: 48px;
    }
    
    .no-results h3 {
      font-size: 22px;
    }
    
    .no-results p {
      font-size: 16px;
    }
  }

  @media (max-width: 480px) {
    .container {
      padding: 12px;
    }
    
    header {
      font-size: 20px;
      padding: 25px 30px;
    }
    
    .updated {
      font-size: 14px;
      padding: 10px;
    }
    
    .date-display {
      font-size: 16px;
      padding: 10px;
    }
    
    .result-table {
      font-size: 16px;
    }
    
    .result-table th {
      padding: 12px 6px;
      font-size: 16px;
    }
    
    .result-table td {
      padding: 12px 6px;
    }
    
    .game-name {
      font-size: 14px;
    }
    
    .time-label {
      font-size: 18px;
    }
    
    /* ✅ BIG RESULT NUMBERS ON SMALL PHONES */
    .result-number {
      font-size: 20px;
      padding: 10px 14px;
      min-width: 55px;
      min-height: 45px;
    }
    
    .record {
      font-size: 11px;
      padding: 3px 6px;
    }
    
    .footer-btn {
      font-size: 14px;
      padding: 12px;
    }
    
    .footer-fullscreen-image {
      max-height: 200px;
    }
    
    .footer-content {
      padding: 25px 15px;
      gap: 15px;
    }
    
    .footer-text {
      font-size: 14px;
    }
    
    .footer-text small {
      font-size: 12px;
    }
    
    .stats-bar {
      flex-direction: flex;
      gap: 8px;
      padding: 15px;
    }
    
    .stat-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .stat-number {
      font-size: 18px;
    }
    
    .stat-label {
      font-size: 12px;
    }
    
    .auto-refresh {
      font-size: 12px;
      padding: 6px 12px;
      margin-left: 8px;
    }
  }

  /* ✅ Very small phones */
  @media (max-width: 360px) {
    .container {
      padding: 10px;
    }
    
    header {
      font-size: 13px;
      padding: 10px 8px;
    }
    
    .updated {
      font-size: 13px;
      padding: 8px;
    }
    
    .date-display {
      font-size: 15px;
      padding: 8px;
    }
    
    .result-table {
      font-size: 12px;
    }
    
    .result-table th {
      padding: 10px 4px;
      font-size: 11px;
    }
    
    .result-table td {
      padding: 10px 4px;
    }
    
    .game-name {
      font-size: 13px;
    }
    
    /* ✅ BIG RESULT NUMBERS ON VERY SMALL PHONES */
    .result-number {
      font-size: 18px;
      padding: 8px 12px;
      min-width: 50px;
      min-height: 40px;
    }
    
    .footer-btn {
      font-size: 13px;
      padding: 10px;
    }
    
    .footer-fullscreen-image {
      max-height: 300px;
    }
  }

  /* ✅ Tablet and larger screens */
  @media (min-width: 769px) {
    .footer-fullscreen-image {
      max-height: 250px;
    }
    
    /* ✅ EVEN BIGGER ON DESKTOP */
    .result-number {
      font-size: 36px;
      padding: 18px 24px;
      min-width: 90px;
      min-height: 80px;
    }
  }

  /* ✅ For very large screens */
  @media (min-width: 1200px) {
    .footer-fullscreen-image {
      max-height: 300px;
    }
    
    .result-number {
      font-size: 40px;
      padding: 20px 28px;
      min-width: 100px;
      min-height: 90px;
    }
    
    .container {
      max-width: 1400px;
    }
  }

  /* ✅ Improve touch targets for mobile */
  @media (max-width: 768px) {
    .record, .footer-btn {
      min-height: 32px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
    }
    
    .result-number {
      min-height: 55px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
    }
  }

  /* ✅ Prevent horizontal scroll */
  html, body {
    overflow-x: hidden;
    max-width: 100%;
  }

  /* ✅ Animation for result numbers */
  @keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
  }

  .result-number.pulse {
    animation: pulse 0.6s ease-in-out;
  }

  /* ✅ Smooth scrolling */
  html {
    scroll-behavior: smooth;
  }
</style>
</head>
<body>

<div class="container">
  <?php if ($disclaimer_config['show_disclaimer']): ?>
  <header>
    <i class="fas fa-exclamation-triangle"></i>
    <?php echo htmlspecialchars($disclaimer_config['disclaimer_text']); ?>
  </header>
  <?php endif; ?>

  <div class="updated">
    <i class="fas fa-sync-alt"></i>
    Last Updated: 
    <?php 
      echo date("F d, Y, h:i:s A", strtotime($last['last_update'] ?? date('Y-m-d H:i:s'))); 
    ?> IST
    <span class="auto-refresh">
      <i class="fas fa-redo"></i> Auto-refresh: 5min
    </span>
  </div>

  <div class="date-display">
    <i class="fas fa-calendar-alt"></i>
    <?php echo $today; ?> - Bhutan Fatafat Results
  </div>

  <?php if ($has_results): ?>
    <!-- Stats Bar -->
    <div class="stats-bar">
      <div class="stat-item">
        <div class="stat-number"><?php echo $results->num_rows; ?></div>
        <div class="stat-label">Total Games</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">24/7</div>
        <div class="stat-label">Live Updates</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">100%</div>
        <div class="stat-label">Official Results</div>
      </div>
    </div>

    <!--<table class="result-table">
      <tr>
        <th colspan="3">
          <i class="fas fa-trophy"></i>
          New Bhutan Fatafat Result of <?php echo $today; ?>
        </th>
      </tr>
      <tr style="background:#374151;color:#fff;">
        <th>Regional Offline Draw Results</th>
        <th>YESTERDAY</th>
        <th>TODAY</th>
      </tr>

      <?php 
      // Reset pointer and loop through results
      $results->data_seek(0);
      while($row = $results->fetch_assoc()) { 
      ?>
      <tr>
        <td class="region">
          <div class="game-name">
            <i class="fas fa-chess-king"></i>
            <?php echo htmlspecialchars($row['game_name']); ?>
          </div>
          <div class="time-label">
            <?php echo htmlspecialchars($row['time_label']); ?>
          </div>
          <a href="record_chart.php?game=<?php echo urlencode($row['game_name']); ?>" class="record">
            <i class="fas fa-chart-line"></i> Record Chart
          </a>
        </td>
        <td>
          <span class="result-number">
            <?php echo htmlspecialchars($row['sat_result']); ?>
          </span>
        </td>
        <td>
          <span class="result-number">
            <?php echo htmlspecialchars($row['sun_result']); ?>
          </span>
        </td>
      </tr>
      <?php } ?>
    </table>
  <?php else: ?>
    <div class="no-results">
      <i class="fas fa-inbox"></i>
      <h3>No Results Available</h3>
      <p>Please check back later for updated results.</p>
    </div>
  <?php endif; ?>

  <a href="full_result.php" class="footer-btn">
    <i class="fas fa-list"></i>
    Click here for all Regional Offline Draw Results
  </a>
</div> -->

<?php

$results = $conn->query("
  SELECT *
  FROM full_result
  ORDER BY result_date DESC
");

?>

<?php while($row = $results->fetch_assoc()): ?>
<div class="result-card">

  <!-- DATE TOP -->
  <div class="result-date">
    <?php echo date("d F Y", strtotime($row['result_date'])); ?>
  </div>

  <table class="result-table">
    
    <!-- GREY ROW : COLUMN NAMES -->
    <tr class="grey-row">
      <td>Good Morning Sikkim</td>
      <td>Kanchan Junga</td>
      <td>My Dear</td>
      <td>Delhi Rajdhani</td>
      <td>Singham</td>
      <td>Nagaland Super</td>
      <td>Bhutan Bazar</td>
    </tr>

    <!-- YELLOW ROW : VALUES -->
    <tr class="yellow-row">
      <td><?php echo htmlspecialchars($row['good_morning_sikkim']); ?></td>
      <td><?php echo htmlspecialchars($row['kanchan_junga']); ?></td>
      <td><?php echo htmlspecialchars($row['my_dear']); ?></td>
      <td><?php echo htmlspecialchars($row['delhi_rajdhani']); ?></td>
      <td><?php echo htmlspecialchars($row['singham']); ?></td>
      <td><?php echo htmlspecialchars($row['nagaland_super']); ?></td>
      <td><?php echo htmlspecialchars($row['bhutan_bazar']); ?></td>
    </tr>

  </table>
</div>
<?php endwhile; ?>




<!-- ✅ UPDATED: Bigger Full Screen Footer -->
<div class="fullscreen-footer">
  <div class="footer-content">
    <?php if (!empty($disclaimer_config['footer_image'])): ?>
      <img src="<?php echo htmlspecialchars($disclaimer_config['footer_image']); ?>" 
           alt="Official Bhutan Fatafat" 
           class="footer-fullscreen-image"
           title="Official Bhutan Fatafat"
           loading="lazy">
    <?php else: ?>
      <!-- Default fullscreen banner if no image -->
      <div style="width: 100%; height: 150px; background: linear-gradient(45deg, #1f2937, #374151); display: flex; align-items: center; justify-content: center; color: white; font-size: 24px; font-weight: bold; border-radius: 8px;">
        <i class="fas fa-crown"></i> Official Bhutan Fatafat
      </div>
    <?php endif; ?>
    
    <div class="footer-text">
      <i class="fas fa-copyright"></i>
      <?php echo date("Y"); ?> Lucky Number India | All Rights Reserved
      <br>
      <small>Official Bhutan Fatafat Results Portal</small>
    </div>
  </div>
</div>

<script>
// Auto-refresh every 5 minutes
setTimeout(function() {
  window.location.reload();
}, 300000);

// Add animation to result numbers
document.addEventListener('DOMContentLoaded', function() {
  const resultNumbers = document.querySelectorAll('.result-number');
  resultNumbers.forEach((number, index) => {
    setTimeout(() => {
      number.classList.add('pulse');
      setTimeout(() => {
        number.classList.remove('pulse');
      }, 600);
    }, index * 200);
  });

  // Add hover effect to footer image
  const footerImage = document.querySelector('.footer-fullscreen-image');
  if (footerImage) {
    footerImage.addEventListener('mouseenter', function() {
      this.style.transform = 'scale(1.03)';
      this.style.opacity = '0.95';
    });
    footerImage.addEventListener('mouseleave', function() {
      this.style.transform = 'scale(1)';
      this.style.opacity = '1';
    });
    
    // Touch events for mobile
    footerImage.addEventListener('touchstart', function() {
      this.style.transform = 'scale(1.03)';
      this.style.opacity = '0.95';
    });
    footerImage.addEventListener('touchend', function() {
      this.style.transform = 'scale(1)';
      this.style.opacity = '1';
    });
  }
});

// Update time every second
function updateTime() {
  const now = new Date();
  const timeString = now.toLocaleTimeString('en-IN', {
    hour12: true,
    timeZone: 'Asia/Kolkata'
  });
  const dateString = now.toLocaleDateString('en-IN', {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    timeZone: 'Asia/Kolkata'
  });
  
  const dateDisplay = document.querySelector('.date-display');
  if (dateDisplay) {
    dateDisplay.innerHTML = `<i class="fas fa-calendar-alt"></i> ${dateString} - Bhutan Fatafat Results <small>(${timeString})</small>`;
  }
}

updateTime();
setInterval(updateTime, 1000);

// Improve mobile experience
document.addEventListener('touchstart', function() {}, {passive: true});

// Add click effect to result numbers
document.querySelectorAll('.result-number').forEach(number => {
  number.addEventListener('click', function() {
    this.classList.add('pulse');
    setTimeout(() => {
      this.classList.remove('pulse');
    }, 600);
  });
});
</script>

</body>
</html>