<?php
// ini_set('display_errors', 1);
// error_reporting(E_ALL);
// // rest of your code...


$category = $_GET['category'] ?? 'Latest';
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">

  <style>
    * { box-sizing: border-box; }
    html {
      -webkit-text-size-adjust: 100%;
    }
    body {
      font-family: 'Inter', sans-serif;
      background: #f4f6f8;
      margin: 0;
      padding: clamp(16px, 3vw, 32px);
      color: #2c3e50;
      line-height: 1.5;
    }
    h2 { margin: 0; }

    .articles-page {
      max-width: 1400px;
      margin: 0 auto;
      width: 100%;
    }

    .nav-tabs {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 10px;
      margin-bottom: 24px;
      padding: 0;
    }

    .nav-tabs a {
      padding: 10px 18px;
      background-color: white;
      border: 1px solid #d0d7de;
      border-radius: 30px;
      text-decoration: none;
      color: #34495e;
      font-weight: 600;
      font-size: 0.95rem;
      transition: all 0.3s ease;
      white-space: nowrap;
    }

    .nav-tabs a:hover {
      background-color: #1a73e8;
      color: white;
    }

    .nav-tabs a.active {
      background-color: #1a73e8;
      color: white;
      box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    }
    #articleslist {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
      gap: 20px;
      align-items: start;
      width: 100%;
    }

    .home-list {
      display: block;
      background-color: white;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0,0,0,0.05);
      overflow: hidden;
      text-decoration: none;
      color: inherit;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
    }

    .home-list:hover {
      transform: translateY(-6px);
      box-shadow: 0 10px 25px rgba(26, 115, 232, 0.15);
    }

    .article-item {
      display: flex;
      flex-direction: column;
      height: 100%;
      background: #fff;
    }

    .r-img1 {
      width: 100%;
      aspect-ratio: 16 / 9;
      background-color: #ecf0f1;
      overflow: hidden;
    }

    .r-img1 img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
      transition: transform 0.3s ease;
    }

    .home-list:hover .r-img1 img {
      transform: scale(1.05);
    }

    .home-list h2 {
      font-size: 1.15rem;
      padding: 15px 20px 5px;
      color: #1a1a1a;
    }

    .home-list p {
      font-size: 0.95rem;
      color: #555;
      padding: 0 20px 15px;
      flex-grow: 1;
    }

    .home-list small {
      padding: 15px 20px;
      font-size: 0.85rem;
      color: #7f8c8d;
      background-color: #f9f9f9;
      border-top: 1px solid #eee;
    }

    .home-list small strong {
      color: #1a73e8;
    }

    @media (max-width: 768px) {
      body {
        padding: 14px;
      }

      .nav-tabs {
        justify-content: flex-start;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        padding-bottom: 6px;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
      }

      .nav-tabs::-webkit-scrollbar {
        display: none;
      }

      .nav-tabs a {
        flex: 0 0 auto;
        padding: 10px 16px;
        font-size: 0.9rem;
      }

      #articleslist {
        grid-template-columns: 1fr;
        gap: 16px;
      }

      .home-list h2 {
        font-size: 1rem;
      }

      .home-list p {
        font-size: 0.92rem;
      }
    }

    @media (max-width: 480px) {
      body { padding: 12px; }

      .nav-tabs a {
        padding: 9px 14px;
        font-size: 0.85rem;
      }

      .home-list h2 {
        font-size: 0.98rem;
        padding: 12px 14px 4px;
      }

      .home-list p {
        padding: 0 14px 12px;
        font-size: 0.88rem;
      }

      .home-list small {
        padding: 12px 14px;
        font-size: 0.8rem;
      }
    }
  </style>
  
</head>
<body>

<main class="articles-page">
  <nav class="nav-tabs" aria-label="Article categories">
    <?php
    $categories = ['Latest', 'Original', 'Industry', 'Broker', 'Exposure', 'Trader Safety Survey'];
    foreach ($categories as $cat) {
      $active = $category === $cat ? 'active' : '';
      echo "<a href=\"#\" data-category=\"" . htmlspecialchars($cat) . "\" class=\"$active\">$cat</a>";
    }
    ?>
  </nav>

  <div id="articleslist">
    <!-- Content will be loaded dynamically -->
  </div>
</main>

<script>
document.addEventListener("DOMContentLoaded", function () {
  const loadArticles = (category) => {
    fetch('fetch_articles.php?category=' + encodeURIComponent(category))
      .then(response => response.text())
      .then(html => {
        document.getElementById('articleslist').innerHTML = html;
      })
      .catch(err => console.error("Error loading articles:", err));
  };

  // Initially load default category
  const defaultActive = document.querySelector('.nav-tabs a.active');
  if (defaultActive) {
    loadArticles(defaultActive.getAttribute('data-category'));
  }

  document.querySelectorAll('.nav-tabs a').forEach(tab => {
    tab.addEventListener('click', function (e) {
      e.preventDefault();

      // Remove active class and set new one
      document.querySelectorAll('.nav-tabs a').forEach(t => t.classList.remove('active'));
      this.classList.add('active');

      const category = this.getAttribute('data-category');
      loadArticles(category);
    });
  });
});
</script>

</body>
</html>
