<%- include('partials/public-header') %>

<!-- =============================================
     COLLECTION / ITEMLIST SCHEMA (CATEGORY SEO)
     ============================================= -->
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "CollectionPage",
  "name": "<%= category.name %> Tools - HeySwan",
  "description": "<%= category.description || 'Collection of free online ' + category.name + ' tools with instant client-side execution.' %>",
  "url": "https://tools.heyswan.cloud/category/<%= category.slug %>",
  "mainEntity": {
    "@type": "ItemList",
    "itemListElement": [
      <% (tools || []).forEach(function(tool, index) { %>
      {
        "@type": "ListItem",
        "position": <%= index + 1 %>,
        "name": "<%- tool.name.replace(/"/g, '\\"') %>",
        "url": "https://tools.heyswan.cloud/tool/<%= tool.slug %>"
      }<%= (index < tools.length - 1) ? ',' : '' %>
      <% }); %>
    ]
  }
}
</script>

<style>
  *, *::before, *::after {
    box-sizing: border-box;
  }

  #main-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 clamp(12px, 3vw, 24px) 64px;
    width: 100%;
    overflow-x: hidden;
  }

  /* ── BREADCRUMBS ── */
  .cat-breadcrumb-nav { padding: 14px 0 6px; }
  .cat-breadcrumbs {
    display: flex;
    align-items: center;
    gap: 6px;
    list-style: none;
    font-size: 0.82rem;
    color: var(--text-muted);
    overflow-x: auto;
    white-space: nowrap;
    scrollbar-width: none;
    margin: 0;
    padding: 0;
  }
  .cat-breadcrumbs::-webkit-scrollbar { display: none; }
  .cat-breadcrumbs a {
    color: var(--text-muted);
    text-decoration: none !important;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: color 0.15s;
  }
  .cat-breadcrumbs a:hover { color: var(--primary); }
  .cat-breadcrumbs .crumb-sep { color: var(--text-muted); opacity: 0.4; }
  .cat-breadcrumbs .crumb-active {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--text);
    font-weight: 700;
  }

  /* ── CATEGORY HERO ── */
  .cat-hero {
    position: relative;
    padding: clamp(24px, 4vw, 40px) 0 clamp(20px, 3.5vw, 30px);
    text-align: center;
    width: 100%;
  }
  .ambient-glow {
    position: absolute;
    top: -30px;
    left: 50%;
    transform: translateX(-50%);
    width: min(650px, 92vw);
    height: 240px;
    background: radial-gradient(ellipse at center, rgba(59, 130, 246, 0.22) 0%, rgba(16, 185, 129, 0.08) 45%, transparent 70%);
    filter: blur(50px);
    z-index: -1;
    pointer-events: none;
  }
  [data-theme="light"] .ambient-glow {
    background: radial-gradient(ellipse at center, rgba(37, 99, 235, 0.12) 0%, rgba(16, 185, 129, 0.05) 45%, transparent 70%);
  }

  .cat-hero-badge {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 5px 14px;
    background: var(--badge-bg);
    border: 1px solid var(--border);
    color: var(--badge-text);
    border-radius: 30px;
    font-size: 0.78rem;
    font-weight: 700;
    margin-bottom: 14px;
  }
  .cat-hero-badge .pulse-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 8px var(--accent);
    flex-shrink: 0;
  }

  .cat-hero h1 {
    font-size: clamp(1.85rem, 5.5vw, 2.85rem);
    font-weight: 900;
    letter-spacing: -0.035em;
    margin: 0 auto 12px;
    line-height: 1.2;
    color: var(--text);
    text-wrap: balance;
    word-break: break-word;
    max-width: 900px;
  }
  .cat-hero h1 span {
    background: linear-gradient(135deg, #60a5fa 0%, #34d399 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    display: inline;
  }
  .cat-hero p {
    color: var(--text-muted);
    font-size: clamp(0.9rem, 2.2vw, 1.05rem);
    max-width: 640px;
    margin: 0 auto 22px;
    line-height: 1.65;
    text-wrap: balance;
  }

  /* ── SEARCH & SIBLING PILLS ── */
  .cat-controls {
    max-width: 580px;
    margin: 0 auto 26px;
    position: relative;
  }
  .cat-search-wrap {
    position: relative;
    display: flex;
    align-items: center;
  }
  .cat-search-icon {
    position: absolute;
    left: 16px;
    color: var(--text-muted);
    pointer-events: none;
    display: flex;
  }
  .cat-search-input {
    width: 100%;
    height: 48px;
    padding: 0 42px 0 44px;
    font-size: 0.94rem;
    font-family: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1.5px solid var(--border);
    border-radius: 12px;
    outline: none;
    box-shadow: 0 6px 20px -4px rgba(0, 0, 0, 0.25);
    transition: all 0.2s ease;
  }
  .cat-search-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
  }
  .cat-clear-search {
    position: absolute;
    right: 12px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    display: none;
    padding: 4px;
  }
  .cat-clear-search:hover { color: var(--text); }

  /* Sibling Category Switcher Pills */
  .sibling-categories {
    display: flex;
    gap: 8px;
    margin-bottom: 32px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 4px 0;
    justify-content: flex-start;
  }
  .sibling-categories::-webkit-scrollbar { display: none; }
  @media (min-width: 680px) {
    .sibling-categories { justify-content: center; flex-wrap: wrap; }
  }

  .cat-pill {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 7px 15px;
    border-radius: 20px;
    font-size: 0.82rem;
    font-weight: 700;
    text-decoration: none !important;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all 0.18s ease;
    flex-shrink: 0;
  }
  .cat-pill:hover { border-color: var(--primary); color: var(--text); }
  .cat-pill.active {
    background: var(--primary);
    color: #ffffff !important;
    border-color: var(--primary);
    box-shadow: 0 4px 12px var(--primary-glow);
  }

  /* ── TOOLS GRID ── */
  .tools-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
    margin-bottom: 56px;
  }
  @media (min-width: 600px) {
    .tools-grid { grid-template-columns: repeat(2, 1fr); }
  }
  @media (min-width: 980px) {
    .tools-grid { grid-template-columns: repeat(3, 1fr); gap: 24px; }
  }

  .tool-card {
    background: var(--surface-card);
    border: 1px solid var(--border);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: var(--radius);
    padding: 22px;
    display: flex;
    flex-direction: column;
    text-decoration: none !important;
    color: inherit;
    box-shadow: 0 8px 20px -6px rgba(0, 0, 0, 0.25);
    transition: transform 0.22s ease, border-color 0.22s ease, box-shadow 0.22s ease;
    position: relative;
  }
  .tool-card:hover, .tool-card:focus-visible {
    transform: translateY(-4px);
    border-color: var(--border-hover);
    box-shadow: 0 16px 32px -8px var(--primary-glow);
    outline: none;
    text-decoration: none !important;
  }

  .card-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
  }
  
  /* Dynamic SVG Tool Icon */
  .tool-icon-box {
    width: 46px;
    height: 46px;
    border-radius: 12px;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.15) 0%, rgba(16, 185, 129, 0.15) 100%);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--badge-text);
    flex-shrink: 0;
  }
  .tool-icon-box svg {
    width: 24px !important;
    height: 24px !important;
    display: block;
  }

  .tool-tag {
    font-size: 0.72rem;
    font-weight: 800;
    padding: 3px 9px;
    border-radius: 12px;
    background: var(--badge-bg);
    color: var(--badge-text);
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }

  .tool-card h2 {
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    margin-bottom: 6px;
    color: var(--text);
  }
  .tool-card p {
    font-size: 0.88rem;
    color: var(--text-muted);
    line-height: 1.5;
    margin-bottom: 18px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: calc(1.5em * 2);
  }

  .card-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 14px;
    border-top: 1px solid var(--border);
    margin-top: auto;
  }
  .tool-usage {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--glass-bg);
    border: 1px solid var(--border);
    padding: 5px 10px;
    border-radius: 20px;
    font-size: 0.76rem;
    font-weight: 700;
    color: var(--text-muted);
  }
  .tool-usage svg { color: var(--accent); }

  .open-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--surface-hover);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 0.82rem;
    font-weight: 700;
    transition: all 0.2s ease;
    text-decoration: none !important;
  }
  .open-arrow {
    transition: transform 0.2s ease;
    color: var(--text-muted);
  }
  .tool-card:hover .open-btn {
    background: var(--primary);
    border-color: var(--primary);
    color: #ffffff;
  }
  .tool-card:hover .open-arrow {
    color: #ffffff;
    transform: translate(2px, -2px);
  }

  #noCategoryResults {
    grid-column: 1 / -1;
    text-align: center;
    padding: 48px 16px;
    display: none;
    color: var(--text-muted);
    font-size: 1.05rem;
    font-weight: 600;
  }

  /* ── CATEGORY SEO CARD ── */
  .cat-seo-section {
    max-width: 920px;
    margin: 0 auto;
    content-visibility: auto;
    contain-intrinsic-size: 1px 1000px;
  }
  .cat-content-card {
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: clamp(20px, 4vw, 32px);
    margin-bottom: 22px;
    transition: border-color 0.2s;
  }
  .cat-content-card:hover { border-color: var(--border-hover); }

  .cat-card-heading {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 14px;
  }
  .cat-heading-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--badge-bg);
    color: var(--badge-text);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border: 1px solid var(--border);
    margin-top: 2px;
  }
  .cat-content-card h2 {
    font-size: clamp(1.15rem, 3vw, 1.4rem);
    font-weight: 800;
    color: var(--text);
    margin: 0;
    line-height: 1.35;
    flex: 1;
  }
  .cat-content-card p {
    color: var(--text-muted);
    line-height: 1.7;
    margin-bottom: 14px;
    font-size: 0.92rem;
  }

  .cat-features-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-top: 14px;
  }
  @media (min-width: 640px) { .cat-features-grid { grid-template-columns: repeat(3, 1fr); } }

  .cat-feature-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .cat-feature-box-top {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    color: var(--text);
    font-size: 0.88rem;
  }
  .cat-feature-box-top svg { color: var(--accent); flex-shrink: 0; }
  .cat-feature-box p {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.52;
  }
</style>

<main id="main-content">

  <!-- BREADCRUMBS -->
  <nav class="cat-breadcrumb-nav" aria-label="Breadcrumb">
    <ol class="cat-breadcrumbs">
      <li>
        <a href="/">
          <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>
          <span>Home</span>
        </a>
      </li>
      <li class="crumb-sep">/</li>
      <li>
        <a href="/#categories"><span>Categories</span></a>
      </li>
      <li class="crumb-sep">/</li>
      <li class="crumb-active" aria-current="page">
        <span><%= category.name %></span>
      </li>
    </ol>
  </nav>

  <!-- CATEGORY HERO SECTION -->
  <section class="cat-hero">
    <div class="ambient-glow" aria-hidden="true"></div>
    
    <div class="cat-hero-badge">
      <span class="pulse-dot"></span>
      <span><%= (tools || []).length %> Free <%= category.name %> Tools Available</span>
    </div>

    <h1>Online <span><%= category.name %> Tools</span> Hub</h1>
    
    <p>
      <%= category.description || 'Explore our suite of lightning-fast ' + category.name.toLowerCase() + ' tools. 100% free, private in-browser processing with zero server latency.' %>
    </p>

    <!-- In-Category Instant Search Box -->
    <div class="cat-controls">
      <div class="cat-search-wrap">
        <span class="cat-search-icon" aria-hidden="true">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
        </span>
        <input 
          type="search" 
          id="catToolSearch" 
          class="cat-search-input" 
          placeholder="Filter <%= category.name.toLowerCase() %> tools..." 
          aria-label="Filter category tools"
          autocomplete="off"
        >
        <button type="button" class="cat-clear-search" id="catClearSearch" aria-label="Clear search">
          <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
        </button>
      </div>
    </div>

    <!-- Sibling Category Navigation Pills -->
    <nav class="sibling-categories" aria-label="Explore other categories">
      <a href="/" class="cat-pill">All Tools</a>
      <% (allCategories || ['Developer', 'Text', 'Security', 'Image', 'Utility']).forEach(function(catItem) { 
           const cName = typeof catItem === 'object' ? catItem.name : catItem;
           const cSlug = typeof catItem === 'object' ? catItem.slug : cName.toLowerCase();
           const isActive = cName.toLowerCase() === category.name.toLowerCase();
      %>
        <a href="/category/<%= cSlug %>" class="cat-pill <%= isActive ? 'active' : '' %>">
          <%= cName %>
        </a>
      <% }); %>
    </nav>
  </section>

  <!-- =============================================
       CATEGORY TOOLS GRID
       ============================================= -->
  <section class="tools-grid" id="catToolsGrid" aria-label="<%= category.name %> Tools">
    
    <% if (tools && tools.length > 0) { %>
      <% tools.forEach(function(tool) { %>
        <a href="/tool/<%= tool.slug %>" class="tool-card">
          <div class="card-top">
            
            <!-- Dynamic SVG Icon from Admin -->
            <div class="tool-icon-box" aria-hidden="true">
              <% if (tool.iconSvg && tool.iconSvg.trim().startsWith('<svg')) { %>
                <%- tool.iconSvg %>
              <% } else { %>
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>
              <% } %>
            </div>

            <span class="tool-tag"><%= tool.badge || 'Instant' %></span>
          </div>
          
          <h2><%= tool.name %></h2>
          <p><%= (tool.seo && tool.seo.metaDescription) ? tool.seo.metaDescription : (tool.name + ' utility engine with instant in-browser execution.') %></p>
          
          <div class="card-bottom">
            <div class="tool-usage">
              <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"></path></svg>
              <span><%= (tool.usageCount || 0).toLocaleString() %> uses</span>
            </div>
            <span class="open-btn">
              <span>Open Tool</span>
              <svg class="open-arrow" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><line x1="7" y1="17" x2="17" y2="7"></line><polyline points="7 7 17 7 17 17"></polyline></svg>
            </span>
          </div>
        </a>
      <% }); %>
    <% } else { %>
      <div style="grid-column: 1 / -1; text-align: center; padding: 48px; color: var(--text-muted);">
        <p>No tools found in this category yet. Create tools from Admin Panel!</p>
      </div>
    <% } %>

    <div id="noCategoryResults">
      <p>No <%= category.name.toLowerCase() %> tools found matching your search.</p>
    </div>

  </section>

  <!-- =============================================
       CATEGORY SEO ARTICLE
       ============================================= -->
  <article class="cat-seo-section">
    <div class="cat-content-card">
      <div class="cat-card-heading">
        <div class="cat-heading-icon">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
        </div>
        <h2>Why Use Our Online <%= category.name %> Tools?</h2>
      </div>

      <p>
        HeySwan provides high-performance <strong><%= category.name %> utilities</strong> engineered specifically for speed, simplicity, and absolute data privacy. Unlike traditional web services that process your files on remote servers, our tool architecture operates 100% locally inside your browser memory.
      </p>

      <div class="cat-features-grid">
        <div class="cat-feature-box">
          <div class="cat-feature-box-top">
            <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
            <span>100% Private</span>
          </div>
          <p>Zero cloud storage, server logs, or telemetry tracking.</p>
        </div>
        <div class="cat-feature-box">
          <div class="cat-feature-box-top">
            <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>
            <span>Zero Latency</span>
          </div>
          <p>Instantaneous execution powered by browser engine APIs.</p>
        </div>
        <div class="cat-feature-box">
          <div class="cat-feature-box-top">
            <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><line x1="1" y1="1" x2="23" y2="23"/><path d="M16.72 11.06A10.94 10.94 0 0 1 19 12.55"/><path d="M5 12.55a10.94 10.94 0 0 1 5.17-2.39"/><path d="M10.71 5.05A16 16 0 0 1 22.58 9"/><path d="M1.42 9a15.91 15.91 0 0 1 4.7-2.88"/><path d="M8.53 16.11a6 6 0 0 1 6.95 0"/><line x1="12" y1="20" x2="12.01" y2="20"/></svg>
            <span>Offline Capable</span>
          </div>
          <p>Tools remain fully functional even without active internet.</p>
        </div>
      </div>
    </div>
  </article>

</main>

<script>
(function() {
  'use strict';
  
  const searchInput = document.getElementById('catToolSearch');
  const clearBtn = document.getElementById('catClearSearch');
  const cards = document.querySelectorAll('#catToolsGrid .tool-card');
  const noResults = document.getElementById('noCategoryResults');

  if (searchInput) {
    searchInput.addEventListener('input', function() {
      const q = this.value.toLowerCase().trim();
      let matched = 0;

      if (clearBtn) clearBtn.style.display = q.length > 0 ? 'inline-flex' : 'none';

      cards.forEach(function(card) {
        const title = card.querySelector('h2') ? card.querySelector('h2').textContent.toLowerCase() : '';
        const desc = card.querySelector('p') ? card.querySelector('p').textContent.toLowerCase() : '';
        
        if (title.includes(q) || desc.includes(q)) {
          card.style.display = 'flex';
          matched++;
        } else {
          card.style.display = 'none';
        }
      });

      if (noResults) {
        noResults.style.display = (matched === 0 && cards.length > 0) ? 'block' : 'none';
      }
    });
  }

  if (clearBtn && searchInput) {
    clearBtn.addEventListener('click', function() {
      searchInput.value = '';
      searchInput.dispatchEvent(new Event('input'));
      searchInput.focus();
    });
  }
})();
</script>

<%- include('partials/public-footer') %>