// components/gallery.el - "Things Neuron Said" gallery page.
// Uses the full site nav and styles — consistent with the rest of the site.

fn gallery_page(cards_json: String) -> String {
    let i: Int = 0
    let cards_html: String = ""
    let n: Int = json_array_len(cards_json)
    while i < n {
        let card: String = json_array_get(cards_json, i)
        let cid:   String = json_get(card, "id")
        let cq:    String = json_get(card, "question")
        let ca:    String = json_get(card, "answer")
        let score_raw: String = json_get(card, "score")
        let score: String = if str_eq(score_raw, "") { "0" } else { score_raw }
        let q_html: String = str_replace(str_replace(str_replace(cq, "&", "&amp;"), "<", "&lt;"), ">", "&gt;")
        let a_html: String = str_replace(str_replace(str_replace(ca, "&", "&amp;"), "<", "&lt;"), ">", "&gt;")
        let ts_raw: String = json_get(card, "created_at")
        let ts_short: String = str_slice(ts_raw, 0, 10)
        let votes_label: String = if str_eq(score, "1") { "1 vote" } else { score + " votes" }
        let card_open: String = "<a href=\"/share/" + cid + "\" class=\"gal-card\" data-score=\"" + score + "\" data-ts=\"" + cid + "\">"
        let card_html: String = card_open + "
  <div class=\"gal-q\">" + q_html + "</div>
  <div class=\"gal-a\">" + a_html + "</div>
  <div class=\"gal-meta\">
    <span>" + votes_label + "</span>
    <span>" + ts_short + " &#183; Read &#8599;</span>
  </div>
</a>"
        let cards_html = cards_html + card_html
        let i = i + 1
    }
    let empty_html: String = if str_eq(cards_html, "") {
        "<div style=\"grid-column:1/-1;padding:4rem 0;text-align:center\"><p class=\"label\" style=\"margin-bottom:1rem\">Nothing yet</p><p style=\"font-family:var(--body);font-weight:300;color:var(--t2);line-height:1.7\">Chat with Neuron and share a response to be the first.</p><a href=\"/?open=chat\" class=\"btn-primary\" style=\"margin-top:1.5rem;display:inline-flex\">Start chatting &#8594;</a></div>"
    } else { "" }

    return "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">
<title>Things Neuron Said</title>
<meta name=\"description\" content=\"Real conversations with Neuron, voted up by users.\">
<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/assets/favicon-32.png\">
<link rel=\"preconnect\" href=\"https://fonts.googleapis.com\">
<link rel=\"preconnect\" href=\"https://fonts.gstatic.com\" crossorigin>
<link href=\"https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;1,400&family=IBM+Plex+Sans:wght@300;400;500;600&display=swap\" rel=\"stylesheet\">
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #FAFAF8; --bg2: #F0F0EC; --card: #FFFFFF;
  --navy: #0052A0; --navy-65: rgba(0,82,160,.65); --navy-85: rgba(0,82,160,.85);
  --t1: #0D0D14; --t2: #3A3A4A; --t3: #6B6B7E;
  --border: rgba(0,0,0,.07); --border2: rgba(0,0,0,.13);
  --head: 'Playfair Display', Georgia, serif;
  --body: 'IBM Plex Sans', system-ui, sans-serif;
}
html { scroll-behavior: smooth; }
html, body { background: var(--bg); color: var(--t1); font-family: var(--body); -webkit-font-smoothing: antialiased; }
body::before {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image: linear-gradient(rgba(0,0,0,.022) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0,0,0,.022) 1px, transparent 1px);
  background-size: 48px 48px;
}
.label { font-family: var(--body); font-size: .75rem; font-weight: 500; letter-spacing: .2em; text-transform: uppercase; color: var(--navy-65); }
.display-lg { font-family: var(--head); font-size: clamp(2rem,4vw,3.25rem); font-weight: 600; line-height: 1.1; letter-spacing: -.02em; color: var(--t1); }
.btn-primary { display: inline-flex; align-items: center; gap: .75rem; font-family: var(--body); font-size: .75rem; font-weight: 500; letter-spacing: .15em; text-transform: uppercase; text-decoration: none; color: #fff; background: var(--navy); padding: 1rem 2rem; border: none; cursor: pointer; transition: background 300ms; }
.btn-primary:hover { background: #0078D4; }
.btn-ghost { display: inline-flex; align-items: center; gap: .75rem; font-family: var(--body); font-size: .75rem; font-weight: 400; letter-spacing: .15em; text-transform: uppercase; text-decoration: none; color: var(--t2); background: transparent; padding: 1rem 2rem; border: 1px solid var(--border2); cursor: pointer; transition: border-color 300ms, color 300ms; }
.btn-ghost:hover { border-color: rgba(0,82,160,.35); color: var(--t1); }
/* Nav */
#nav { position: fixed; top: 0; left: 0; right: 0; z-index: 100; transition: background 500ms, border-color 500ms; }
#nav.scrolled { background: rgba(250,250,248,.95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--border); }
.nav-inner { max-width: 1280px; margin: 0 auto; padding: 0 2rem; height: 4rem; display: flex; align-items: center; justify-content: space-between; position: relative; }
.nav-logo img { height: 2rem; width: auto; display: block; }
.nav-links { display: flex; align-items: center; gap: 1.25rem; flex-wrap: nowrap; }
.nav-link { font-family: var(--body); font-size: .675rem; font-weight: 400; letter-spacing: .16em; text-transform: uppercase; color: var(--t3); text-decoration: none; white-space: nowrap; transition: color 200ms; }
.nav-link:hover { color: var(--t1); }
.nav-dropdown { position: relative; display: flex; align-items: center; }
.nav-dropdown-btn { background: none; border: none; cursor: pointer; padding: 0; font-family: var(--body); font-size: .675rem; font-weight: 400; letter-spacing: .16em; text-transform: uppercase; color: var(--t3); transition: color 200ms; white-space: nowrap; }
.nav-dropdown-btn:hover { color: var(--t1); }
.nav-dropdown-menu { display: none; position: absolute; top: calc(100% + .75rem); left: 50%; transform: translateX(-50%); background: #fff; border: 1px solid var(--border2); box-shadow: 0 8px 32px rgba(0,0,0,.10); min-width: 160px; flex-direction: column; z-index: 200; padding: .375rem 0; }
.nav-dropdown.open .nav-dropdown-menu { display: flex; }
.nav-dropdown-item { font-family: var(--body); font-size: .8125rem; font-weight: 400; color: var(--t2); text-decoration: none; padding: .5rem 1.25rem; transition: background 150ms, color 150ms; white-space: nowrap; }
.nav-dropdown-item:hover { background: var(--bg2); color: var(--t1); }
.nav-cta { font-family: var(--body); font-size: .675rem; font-weight: 500; letter-spacing: .15em; text-transform: uppercase; color: #fff; background: var(--navy); text-decoration: none; padding: .575rem 1.1rem; white-space: nowrap; box-shadow: 0 2px 12px rgba(0,82,160,.20); transition: background 300ms; }
.nav-cta:hover { background: #0078D4; }
.nav-hamburger { display: none; flex-direction: column; justify-content: center; gap: 5px; background: none; border: none; cursor: pointer; padding: 6px 4px; z-index: 101; -webkit-tap-highlight-color: transparent; }
.nav-hamburger span { display: block; width: 22px; height: 2px; background: var(--t1); border-radius: 1px; transition: transform 280ms ease, opacity 200ms ease; transform-origin: center; }
.nav-hamburger[aria-expanded=\"true\"] span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.nav-hamburger[aria-expanded=\"true\"] span:nth-child(2) { opacity: 0; transform: scaleX(0); }
.nav-hamburger[aria-expanded=\"true\"] span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
.nav-mobile { display: none; position: absolute; top: 4rem; left: 0; right: 0; background: rgba(250,250,248,.98); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); border-bottom: 1px solid var(--border); padding: .5rem 0 1.5rem; flex-direction: column; box-shadow: 0 8px 32px rgba(0,0,0,.08); }
.nav-mobile.open { display: flex; }
.nav-mobile-link { display: block; padding: .875rem 2rem; font-family: var(--body); font-size: .75rem; font-weight: 400; letter-spacing: .16em; text-transform: uppercase; color: var(--t2); text-decoration: none; border-bottom: 1px solid rgba(0,0,0,.05); transition: color 150ms, background 150ms; }
.nav-mobile-link:last-child { border-bottom: none; }
.nav-mobile-link:hover { color: var(--t1); background: rgba(0,82,160,.03); }
.nav-mobile-cta { display: block; margin: 1rem 2rem 0; padding: .8rem 1.5rem; text-align: center; font-family: var(--body); font-size: .75rem; font-weight: 500; letter-spacing: .15em; text-transform: uppercase; color: #fff; background: var(--navy); text-decoration: none; }
@media (max-width: 1060px) { .nav-links { display: none; } .nav-hamburger { display: flex; } }
@media (min-width: 1061px) { .nav-mobile { display: none !important; } .nav-hamburger { display: none !important; } }
/* Gallery */
.gallery-wrap { max-width: 1100px; margin: 0 auto; padding: 7rem 2.5rem 5rem; position: relative; z-index: 1; }
.gallery-header { margin-bottom: 3rem; }
.gallery-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.25rem; }
@media (max-width: 900px) { .gallery-grid { grid-template-columns: 1fr 1fr; } }
@media (max-width: 600px) { .gallery-grid { grid-template-columns: 1fr; } }
.gal-card { background: var(--card); border: 1px solid var(--border); padding: 1.5rem; display: flex; flex-direction: column; gap: .875rem; text-decoration: none; transition: border-color .15s, box-shadow .15s; }
.gal-card:hover { border-color: rgba(0,82,160,.25); box-shadow: 0 4px 20px rgba(0,82,160,.07); }
.gal-q { font-family: var(--body); font-weight: 500; font-size: .875rem; color: var(--navy); line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.gal-a { font-family: var(--body); font-weight: 300; font-size: .8125rem; color: var(--t2); line-height: 1.7; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; flex: 1; }
.gal-meta { display: flex; justify-content: space-between; align-items: center; font-family: var(--body); font-size: .65rem; font-weight: 400; letter-spacing: .1em; text-transform: uppercase; color: var(--t3); margin-top: auto; padding-top: .75rem; border-top: 1px solid var(--border); }
.gallery-controls { display: flex; align-items: center; gap: 1rem; margin-bottom: 2rem; flex-wrap: wrap; }
.gallery-search { flex: 1; min-width: 200px; font-family: var(--body); font-size: .875rem; font-weight: 300; color: var(--t1); background: #fff; border: 1px solid var(--border2); padding: .625rem 1rem; outline: none; transition: border-color .2s; }
.gallery-search:focus { border-color: var(--navy); }
.gallery-search::placeholder { color: var(--t3); }
.sort-btn { font-family: var(--body); font-size: .7rem; font-weight: 400; letter-spacing: .1em; text-transform: uppercase; color: var(--t3); background: none; border: 1px solid var(--border2); padding: .5rem 1rem; cursor: pointer; transition: all .15s; }
.sort-btn:hover, .sort-btn.active { color: var(--navy); border-color: rgba(0,82,160,.3); background: rgba(0,82,160,.03); }
.gallery-empty { grid-column: 1/-1; padding: 4rem 0; text-align: center; }
.gal-card.hidden { display: none; }
</style>
</head>
<body>

<nav id=\"nav\">
  <div class=\"nav-inner\">
    <a href=\"/\" class=\"nav-logo\" aria-label=\"Neuron home\"><img src=\"/assets/brand/neuron-wordmark-on-light.png\" srcset=\"/assets/brand/neuron-wordmark-on-light@2x.png 2x\" alt=\"Neuron\" height=\"28\"></a>
    <div class=\"nav-links\">
      <a href=\"/#how-it-works\" class=\"nav-link\">How it works</a>
      <div class=\"nav-dropdown\">
        <button class=\"nav-link nav-dropdown-btn\" aria-haspopup=\"true\" aria-expanded=\"false\">Mission &#9662;</button>
        <div class=\"nav-dropdown-menu\">
          <a href=\"/#mission\"       class=\"nav-dropdown-item\">Our mission</a>
          <a href=\"/#safety\"        class=\"nav-dropdown-item\">Safety</a>
          <a href=\"/#environmental\" class=\"nav-dropdown-item\">Environment</a>
        </div>
      </div>
      <a href=\"/#pricing\"      class=\"nav-link\">Pricing</a>
      <a href=\"/#marketplace\" class=\"nav-link\">Marketplace</a>
      <a href=\"/#enterprise\"   class=\"nav-link\">Enterprise</a>
      <a href=\"/about\"         class=\"nav-link\">About</a>
      <a href=\"/said\"          class=\"nav-link\">Gallery</a>
      <a href=\"/account\"       class=\"nav-link\">Account</a>
      <a href=\"/#pricing\"      class=\"nav-cta\">Get Access</a>
    </div>
    <button class=\"nav-hamburger\" id=\"nav-hamburger\" aria-label=\"Open navigation\" aria-expanded=\"false\" aria-controls=\"nav-mobile\">
      <span></span><span></span><span></span>
    </button>
    <div class=\"nav-mobile\" id=\"nav-mobile\" role=\"navigation\" aria-label=\"Mobile navigation\">
      <a href=\"/#how-it-works\" class=\"nav-mobile-link\">How it works</a>
      <a href=\"/#mission\"       class=\"nav-mobile-link\">Mission</a>
      <a href=\"/#safety\"        class=\"nav-mobile-link\" style=\"padding-left:1.75rem;font-size:.8rem;color:var(--t3)\">- Safety</a>
      <a href=\"/#environmental\" class=\"nav-mobile-link\" style=\"padding-left:1.75rem;font-size:.8rem;color:var(--t3)\">- Environment</a>
      <a href=\"/#pricing\"       class=\"nav-mobile-link\">Pricing</a>
      <a href=\"/#enterprise\"    class=\"nav-mobile-link\">Enterprise</a>
      <a href=\"/about\"          class=\"nav-mobile-link\">About</a>
      <a href=\"/said\"           class=\"nav-mobile-link\">Gallery</a>
      <a href=\"/account\"        class=\"nav-mobile-link\">Account</a>
      <a href=\"/#pricing\"       class=\"nav-mobile-cta\">Get Access</a>
    </div>
  </div>
</nav>

<div class=\"gallery-wrap\">
  <div class=\"gallery-header\">
    <p class=\"label\" style=\"margin-bottom:1rem\">Things Neuron Said</p>
    <h1 class=\"display-lg\" style=\"margin-bottom:1rem\">Real conversations.<br>No curation.</h1>
    <p style=\"font-family:var(--body);font-weight:300;font-size:.9375rem;color:var(--t2);line-height:1.75;max-width:36rem\">
      People chatted with Neuron and shared the exchanges that mattered. Voted up by readers. The best ones rise.
    </p>
  </div>
  <div class=\"gallery-controls\">
    <input type=\"search\" id=\"gal-search\" class=\"gallery-search\" placeholder=\"Search conversations...\" autocomplete=\"off\">
    <div style=\"display:flex;gap:.5rem;flex-shrink:0\">
      <button class=\"sort-btn active\" id=\"sort-top\" onclick=\"setSort('top',this)\">Top</button>
      <button class=\"sort-btn\" id=\"sort-new\" onclick=\"setSort('new',this)\">Newest</button>
    </div>
  </div>
  <div class=\"gallery-grid\" id=\"gallery-grid\">" + cards_html + empty_html + "</div>
  <div id=\"no-results\" style=\"display:none;padding:3rem 0;text-align:center\">
    <p style=\"font-family:var(--body);font-weight:300;color:var(--t3)\">No conversations match that search.</p>
  </div>
  <div style=\"margin-top:3rem;display:flex;align-items:center;gap:1.5rem\">
    <a href=\"/?open=chat\" class=\"btn-primary\">Chat with Neuron &#8594;</a>
    <a href=\"/\" class=\"btn-ghost\">&#8592; Back to site</a>
  </div>
</div>

<script>
(function() {
  // Nav scroll effect
  var nav = document.getElementById('nav');
  if (nav) window.addEventListener('scroll', function() {
    nav.classList.toggle('scrolled', window.scrollY > 10);
  }, { passive: true });

  // Hamburger
  var btn  = document.getElementById('nav-hamburger');
  var menu = document.getElementById('nav-mobile');
  if (btn && menu) {
    function navClose() { menu.classList.remove('open'); btn.setAttribute('aria-expanded','false'); }
    function navOpen()  { menu.classList.add('open');    btn.setAttribute('aria-expanded','true');  }
    btn.addEventListener('click', function(e) { e.stopPropagation(); menu.classList.contains('open') ? navClose() : navOpen(); });
    menu.querySelectorAll('a').forEach(function(a) { a.addEventListener('click', navClose); });
    document.addEventListener('click', function(e) { if (!nav.contains(e.target)) navClose(); });
    document.addEventListener('keydown', function(e) { if (e.key === 'Escape') navClose(); });
    window.addEventListener('resize', function() { if (window.innerWidth > 1060) navClose(); });
  }

  // Dropdown - Mission
  var ddBtn = document.querySelector('.nav-dropdown-btn');
  var dd    = document.querySelector('.nav-dropdown');
  if (ddBtn && dd) {
    ddBtn.addEventListener('click', function(e) {
      e.stopPropagation();
      var isOpen = dd.classList.contains('open');
      dd.classList.toggle('open');
      ddBtn.setAttribute('aria-expanded', isOpen ? 'false' : 'true');
    });
    dd.querySelectorAll('.nav-dropdown-item').forEach(function(a) { a.addEventListener('click', function() { dd.classList.remove('open'); }); });
    document.addEventListener('click', function() { dd.classList.remove('open'); });
  }

  // Search
  var searchEl = document.getElementById('gal-search');
  var grid = document.getElementById('gallery-grid');
  var noResults = document.getElementById('no-results');

  function filterCards() {
    var q = (searchEl.value || '').toLowerCase().trim();
    var cards = grid.querySelectorAll('.gal-card');
    var visible = 0;
    cards.forEach(function(c) {
      var text = c.textContent.toLowerCase();
      var match = !q || text.indexOf(q) !== -1;
      c.classList.toggle('hidden', !match);
      if (match) visible++;
    });
    noResults.style.display = visible === 0 && q ? 'block' : 'none';
  }

  if (searchEl) searchEl.addEventListener('input', filterCards);

  // Sort
  var currentSort = 'top';
  window.setSort = function(mode, btn) {
    document.querySelectorAll('.sort-btn').forEach(function(b){ b.classList.remove('active'); });
    btn.classList.add('active');
    currentSort = mode;
    var cards = Array.from(grid.querySelectorAll('.gal-card'));
    cards.sort(function(a, b) {
      if (mode === 'top') {
        var aV = parseInt(a.getAttribute('data-score') || '0');
        var bV = parseInt(b.getAttribute('data-score') || '0');
        return bV - aV;
      } else {
        var aT = parseInt(a.getAttribute('data-ts') || '0');
        var bT = parseInt(b.getAttribute('data-ts') || '0');
        return bT - aT;
      }
    });
    cards.forEach(function(c){ grid.appendChild(c); });
  };
})();
</script>
</body>
</html>"
}
