<style>
  /* Container and horizontal scroll */
  .wiki-surveys {
    width: 100%;
    padding: 20px 0;
  }

  .wiki-surveys .list {
    display: flex;
    flex-wrap: nowrap;
    gap: 20px;
    overflow-x: auto;
    padding-bottom: 20px;
    margin-top: 20px;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: none; /* IE and Edge */
    scrollbar-width: none; /* Firefox */
  }

  .wiki-surveys .list::-webkit-scrollbar {
    display: none; /* Chrome, Safari and Opera */
  }

  /* Each survey card */
  .wiki-surveys .surveys-list {
    background: #fff;
    border: 1px solid #eee;
    border-radius: 14px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    flex: 0 0 auto;
    width: 320px;
    scroll-snap-align: start;
    display: flex;
    flex-direction: column;
    transition: box-shadow 0.3s ease, transform 0.3s ease;
    overflow: hidden;
    cursor: pointer;
  }

  .wiki-surveys .surveys-list:hover {
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    transform: translateY(-4px); /* Hover par thoda smooth upar aayega */
  }

  .wiki-surveys .surveys-list a {
    color: inherit;
    text-decoration: none;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: 0;
  }

  /* Broker Logo & Title Header */
  .wiki-surveys .surveys-list .title-l-t {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 20px 0;
  }

  .wiki-surveys .surveys-list .title-l-t img {
    height: 32px; /* Thoda bada kiya clarity ke liye */
    width: 32px;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
    background: #fff;
  }

  .wiki-surveys .surveys-list .title-l-t-text {
    font-weight: 700;
    font-size: 16px;
    color: #333;
    margin: 0;
  }

  /* Survey Main Title */
  .wiki-surveys .surveys-title {
    margin: 12px 20px 16px 20px;
    font-weight: 700;
    font-size: 18px;
    line-height: 1.3;
    color: #222;
    /* Agar title bada ho to 2 line me truncate ho jayega */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* Main Survey Image */
  .wiki-surveys .surveys-img {
    position: relative;
    height: 180px;
    border-radius: 0 0 12px 12px;
    overflow: hidden;
    margin: 0 20px 20px 20px;
  }

  .wiki-surveys .surveys-img img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    transition: transform 0.4s ease;
  }

  .wiki-surveys .surveys-list:hover .surveys-img img {
    transform: scale(1.08); /* Hover par image zoom effect */
  }

  /* Badges */
  .surveys-badge {
    position: absolute;
    top: 10px;
    left: 10px;
    padding: 5px 12px;
    border-radius: 6px;
    font-weight: 600;
    color: #fff;
    font-size: 12px;
    text-transform: uppercase;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    z-index: 2;
  }

  .surveys-badge.Danger, .surveys-badge.danger { background: #e74c3c; }
  .surveys-badge.Safe, .surveys-badge.safe { background: #28a745; }
  .surveys-badge.Warning, .surveys-badge.warning { background: #f39c12; }

  /* Footer Tools (Flag & Date) */
  .surveys-tool {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    color: #666;
    padding: 15px 20px;
    border-top: 1px solid #f0f0f0;
    margin-top: auto; /* Box ke sabse neeche set rakhega */
  }

  .surveys-tool-l {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .surveys-tool-l img {
    height: 16px;
    width: 24px;
    object-fit: contain;
    border-radius: 2px;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
  }

  .survey-tool-r {
    color: #888;
    font-weight: 500;
  }

  /* ===== TABLET BREAKPOINT (max-width: 1100px) ===== */
  @media (max-width: 1100px) {
    .wiki-surveys {
      padding: 16px 0;
    }

    .wiki-surveys .list {
      gap: 16px;
      padding-bottom: 16px;
      margin-top: 16px;
    }

    .wiki-surveys .surveys-list {
      width: 300px;
    }

    .wiki-surveys .surveys-list .title-l-t {
      gap: 10px;
      padding: 16px 16px 0;
    }

    .wiki-surveys .surveys-list .title-l-t img {
      height: 28px;
      width: 28px;
    }

    .wiki-surveys .surveys-list .title-l-t-text {
      font-size: 14px;
    }

    .wiki-surveys .surveys-title {
      margin: 10px 16px 12px 16px;
      font-size: 16px;
    }

    .wiki-surveys .surveys-img {
      height: 160px;
      margin: 0 16px 16px 16px;
    }

    .surveys-badge {
      font-size: 11px;
      padding: 4px 10px;
    }

    .surveys-tool {
      font-size: 12px;
      padding: 12px 16px;
    }

    .surveys-tool-l img {
      height: 14px;
      width: 21px;
    }
  }

  /* ===== MOBILE BREAKPOINT (max-width: 768px) ===== */
  @media (max-width: 768px) {
    .wiki-surveys {
      padding: 12px 0;
    }

    .wiki-surveys .list {
      gap: 12px;
      padding-bottom: 12px;
      margin-top: 12px;
    }

    .wiki-surveys .surveys-list {
      width: 280px;
      border-radius: 12px;
    }

    .wiki-surveys .surveys-list:hover {
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
      transform: translateY(-3px);
    }

    .wiki-surveys .surveys-list .title-l-t {
      gap: 8px;
      padding: 14px 14px 0;
    }

    .wiki-surveys .surveys-list .title-l-t img {
      height: 26px;
      width: 26px;
    }

    .wiki-surveys .surveys-list .title-l-t-text {
      font-size: 13px;
    }

    .wiki-surveys .surveys-title {
      margin: 8px 14px 10px 14px;
      font-size: 14px;
      -webkit-line-clamp: 2;
      line-clamp: 2;
    }

    .wiki-surveys .surveys-img {
      height: 140px;
      margin: 0 14px 14px 14px;
      border-radius: 0 0 10px 10px;
    }

    .surveys-badge {
      font-size: 10px;
      padding: 3px 8px;
      top: 8px;
      left: 8px;
    }

    .surveys-tool {
      font-size: 11px;
      padding: 10px 14px;
    }

    .surveys-tool-l {
      gap: 6px;
    }

    .surveys-tool-l img {
      height: 13px;
      width: 19px;
    }

    .expo-header {
      margin-bottom: 8px;
      padding-bottom: 8px;
    }

    .expo-title {
      font-size: 16px;
    }
  }

  /* ===== SMALL MOBILE BREAKPOINT (max-width: 480px) ===== */
  @media (max-width: 480px) {
    .wiki-surveys {
      padding: 8px 0;
      width: 100%;
    }

    .wiki-surveys .container {
      padding: 0 8px;
    }

    .wiki-surveys .list {
      gap: 10px;
      padding-bottom: 10px;
      margin-top: 10px;
      -webkit-overflow-scrolling: touch;
    }

    .wiki-surveys .surveys-list {
      width: calc(100vw - 36px);
      min-width: 260px;
      border-radius: 10px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
    }

    .wiki-surveys .surveys-list:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
      transform: translateY(-2px);
    }

    .wiki-surveys .surveys-list .title-l-t {
      gap: 6px;
      padding: 10px 10px 0;
    }

    .wiki-surveys .surveys-list .title-l-t img {
      height: 24px;
      width: 24px;
    }

    .wiki-surveys .surveys-list .title-l-t-text {
      font-size: 12px;
    }

    .wiki-surveys .surveys-title {
      margin: 6px 10px 8px 10px;
      font-size: 12px;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      line-height: 1.3;
    }

    .wiki-surveys .surveys-img {
      height: 120px;
      margin: 0 10px 10px 10px;
      border-radius: 0 0 8px 8px;
    }

    .surveys-badge {
      font-size: 9px;
      padding: 2px 6px;
      top: 6px;
      left: 6px;
    }

    .surveys-tool {
      font-size: 10px;
      padding: 8px 10px;
      gap: 6px;
    }

    .surveys-tool-l {
      gap: 4px;
    }

    .surveys-tool-l img {
      height: 12px;
      width: 18px;
    }

    .survey-tool-r {
      font-size: 9px;
    }

    .expo-header {
      margin-bottom: 6px;
      padding-bottom: 6px;
    }

    .expo-title {
      font-size: 14px;
    }
  }
</style>

<?php
include "config.php";

$query = "SELECT * FROM surveys ORDER BY survey_date DESC LIMIT 10";
$stmt = $dbh->prepare($query);
$stmt->execute();
$surveys = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<section class="wiki-surveys">
  <div class="container">
    
    <div class="expo-header" style="margin-bottom: 10px; border-bottom: 2px solid #eaeaea; padding-bottom: 10px;">
      <span class="expo-title" style="font-size: 18px; font-weight: bold; color: #333;">Field Survey</span>
    </div>

    <div class="list">
      <?php foreach ($surveys as $row): 
        
        // --- SMART IMAGE PATH LOGIC ---
        
        // 1. Broker Logo Path
        $raw_logo = ltrim($row['broker_logo'] ?? '', '/');
        $logo_url = (strpos($raw_logo, 'http') === 0) ? $raw_logo : './uploads/' . $raw_logo;

        // 2. Main Survey Image Path
        $raw_img = ltrim($row['image_url'] ?? '', '/');
        $main_img_url = (strpos($raw_img, 'http') === 0) ? $raw_img : './' . $raw_img;

        // 3. Country Flag Path
        $raw_flag = ltrim($row['country_flag_url'] ?? '', '/');
        $flag_url = (strpos($raw_flag, 'http') === 0) ? $raw_flag : './' . $raw_flag;

        // ------------------------------
      ?>
        <div class="surveys-list">
          <a href="<?= htmlspecialchars($row['detail_url'] ?? '#') ?>" target="_blank">

            <div class="title-l-t">
              <img alt="Logo" src="<?= htmlspecialchars($logo_url) ?>" loading="lazy" class="common-imgs">
              <h2 class="title-l-t-text"><?= htmlspecialchars($row['broker_name'] ?? 'Broker') ?></h2>
            </div>

            <h3 class="surveys-title"><?= htmlspecialchars($row['survey_title'] ?? '') ?></h3>

            <div class="surveys-img">
              <img alt="Survey Image" src="<?= htmlspecialchars($main_img_url) ?>" loading="lazy" class="common-imgs">
              <?php if(!empty($row['badge'])): ?>
                <span class="surveys-badge <?= htmlspecialchars($row['badge_class'] ?? '') ?>">
                  <?= htmlspecialchars($row['badge']) ?>
                </span>
              <?php endif; ?>
            </div>

            <div class="surveys-tool">
              <div class="surveys-tool-l">
                <img alt="Flag" src="<?= htmlspecialchars($flag_url) ?>" loading="lazy" class="common-imgs">
                <span><?= htmlspecialchars($row['country_name'] ?? '') ?></span>
              </div>
              <div class="survey-tool-r">
                <?= date('F j, Y', strtotime($row['survey_date'])) ?>
              </div>
            </div>

          </a>
        </div>
      <?php endforeach; ?>
    </div>
  </div>
</section>

<script>
  document.addEventListener("DOMContentLoaded", function() {
    // Mouse drag to scroll functionality if needed
    const slider = document.querySelector('.wiki-surveys .list');
    let isDown = false;
    let startX;
    let scrollLeft;

    slider.addEventListener('mousedown', (e) => {
      isDown = true;
      startX = e.pageX - slider.offsetLeft;
      scrollLeft = slider.scrollLeft;
    });
    slider.addEventListener('mouseleave', () => { isDown = false; });
    slider.addEventListener('mouseup', () => { isDown = false; });
    slider.addEventListener('mousemove', (e) => {
      if (!isDown) return;
      e.preventDefault();
      const x = e.pageX - slider.offsetLeft;
      const walk = (x - startX) * 2; // Scroll fast
      slider.scrollLeft = scrollLeft - walk;
    });
  });
</script>