<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ profile_name }}'s Feed — {{ date }}</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 100%);
  color: #e0e0e0;
  min-height: 100vh;
  padding: 0;
}

.header {
  text-align: center;
  padding: 2rem 1rem 1rem;
  border-bottom: 1px solid #2a2a4a;
  background: rgba(15, 15, 26, 0.8);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
}

.header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #f0f0f0;
  margin-bottom: 0.25rem;
}

.header .subtitle {
  font-size: 0.85rem;
  color: #888;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  align-items: center;
  padding: 0.75rem 1rem;
  max-width: 1200px;
  margin: 0 auto;
}

.search-box {
  background: #1e1e36;
  border: 1px solid #3a3a5c;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  color: #e0e0e0;
  font-size: 0.85rem;
  width: 220px;
  outline: none;
  transition: border-color 0.2s;
}

.search-box:focus {
  border-color: #6366f1;
}

.filter-btn {
  background: #1e1e36;
  border: 1px solid #3a3a5c;
  border-radius: 20px;
  padding: 0.35rem 0.75rem;
  color: #ccc;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.2s;
}

.filter-btn:hover { background: #2a2a4a; }
.filter-btn.active { background: #6366f1; border-color: #6366f1; color: #fff; }

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  padding: 1rem;
  max-width: 1200px;
  margin: 0 auto;
}

@media (max-width: 900px) { .grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 580px) { .grid { grid-template-columns: 1fr; } }

.card {
  background: #16162a;
  border: 1px solid #2a2a4a;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.15s, border-color 0.15s;
  display: flex;
  flex-direction: column;
}

.card:hover { transform: translateY(-2px); border-color: #4a4a6a; }
.card.focused { border-color: #6366f1; box-shadow: 0 0 0 2px rgba(99,102,241,0.3); }

.card-image {
  width: 100%;
  height: 140px;
  object-fit: cover;
  background: #1e1e36;
}

.card-placeholder {
  width: 100%;
  height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  font-weight: 700;
  color: rgba(255,255,255,0.15);
}

.card-body {
  padding: 0.75rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.card-title {
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.3;
}

.card-title a {
  color: #e0e0e0;
  text-decoration: none;
}

.card-title a:hover { color: #818cf8; }

.card-summary {
  font-size: 0.78rem;
  color: #999;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  margin-top: auto;
  padding-top: 0.5rem;
}

.tag-pill {
  font-size: 0.65rem;
  padding: 0.15rem 0.45rem;
  border-radius: 10px;
  font-weight: 500;
  white-space: nowrap;
}

.source-badge {
  font-size: 0.65rem;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}

.engagement {
  font-size: 0.7rem;
  color: #888;
  margin-left: auto;
  display: flex;
  gap: 0.5rem;
  white-space: nowrap;
}

.engagement span { display: inline-flex; align-items: center; gap: 0.15rem; }

.score-badge {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: rgba(0,0,0,0.6);
  color: #6366f1;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
}

.card-top {
  position: relative;
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 3rem;
  color: #666;
  font-size: 0.9rem;
}

.kbd-hint {
  font-size: 0.7rem;
  color: #555;
  padding: 0.5rem 1rem;
  text-align: center;
}
</style>
</head>
<body>

<div class="header">
  <h1>{{ profile_name }}'s Feed</h1>
  <div class="subtitle">{{ date }} &mdash; {{ total }} items</div>
  <div class="controls">
    <input type="text" class="search-box" id="searchInput" placeholder="Search items... (/)">
    {% for cat in all_categories %}
    <button class="filter-btn{% if cat == 'All' %} active{% endif %}" data-category="{{ cat }}">{{ cat }}</button>
    {% endfor %}
  </div>
  <div class="kbd-hint">j/k navigate &bull; o open &bull; / search</div>
</div>

<div class="grid" id="grid">
  {% for item in items %}
  <div class="card" data-category="{{ item.category }}" data-text="{{ item.title|lower }} {{ item.summary|lower }} {{ item.tags|join(' ')|lower }} {{ item.source|lower }}">
    <div class="card-top">
      {% if item.image_url %}
      <img class="card-image" src="{{ item.image_url }}" alt="" loading="lazy" data-source-color="{{ source_colors.get(item.source, '#333') }}" data-source-letter="{{ item.source[0]|upper }}" onerror="imgFallback(this)">
      {% else %}
      <div class="card-placeholder" style="background: {{ source_colors.get(item.source, '#333') }}22;">{{ item.source[0]|upper }}</div>
      {% endif %}
      <div class="score-badge">{{ "%.1f"|format(item.score) }}</div>
    </div>
    <div class="card-body">
      <div class="card-title"><a href="{{ item.url }}" target="_blank" rel="noopener">{{ item.title }}</a></div>
      {% if item.summary %}
      <div class="card-summary">{{ item.summary }}</div>
      {% endif %}
      <div class="card-footer">
        <span class="source-badge" style="background: {{ source_colors.get(item.source, '#555') }};">{{ item.source }}</span>
        {% for tag in item.tags[:4] %}
        <span class="tag-pill" style="background: {{ cat_colors.get(tag, '#3a3a5c') }}33; color: {{ cat_colors.get(tag, '#aaa') }};">{{ tag }}</span>
        {% endfor %}
        <div class="engagement">
          {% if item.engagement.get('upvotes') %}
          <span>&#9650; {{ item.engagement.upvotes }}</span>
          {% endif %}
          {% if item.engagement.get('comments') %}
          <span>&#x1F4AC; {{ item.engagement.comments }}</span>
          {% endif %}
          {% if item.engagement.get('stars') %}
          <span>&#11088; {{ item.engagement.stars }}</span>
          {% endif %}
          {% if item.engagement.get('citations') %}
          <span>&#x1F4D6; {{ item.engagement.citations }}</span>
          {% endif %}
        </div>
      </div>
    </div>
  </div>
  {% endfor %}
  <div class="empty-state" id="emptyState" style="display:none;">No items match your filter.</div>
</div>

<script>
function imgFallback(img) {
  var div = document.createElement('div');
  div.className = 'card-placeholder';
  div.style.background = (img.dataset.sourceColor || '#333') + '22';
  div.textContent = img.dataset.sourceLetter || '?';
  img.parentNode.replaceChild(div, img);
}
(function() {
  const cards = Array.from(document.querySelectorAll('.card'));
  const filterBtns = document.querySelectorAll('.filter-btn');
  const searchInput = document.getElementById('searchInput');
  const emptyState = document.getElementById('emptyState');
  let focusIndex = -1;
  let activeCategory = 'All';

  function getVisible() {
    return cards.filter(c => c.style.display !== 'none');
  }

  function applyFilters() {
    const query = searchInput.value.toLowerCase().trim();
    let visibleCount = 0;
    cards.forEach(card => {
      const cat = card.dataset.category;
      const text = card.dataset.text;
      const catMatch = activeCategory === 'All' || cat === activeCategory;
      const searchMatch = !query || text.includes(query);
      const show = catMatch && searchMatch;
      card.style.display = show ? '' : 'none';
      if (show) visibleCount++;
    });
    emptyState.style.display = visibleCount === 0 ? '' : 'none';
    focusIndex = -1;
    clearFocus();
  }

  function clearFocus() {
    cards.forEach(c => c.classList.remove('focused'));
  }

  function setFocus(idx) {
    const visible = getVisible();
    if (visible.length === 0) return;
    clearFocus();
    focusIndex = Math.max(0, Math.min(idx, visible.length - 1));
    visible[focusIndex].classList.add('focused');
    visible[focusIndex].scrollIntoView({ block: 'nearest', behavior: 'smooth' });
  }

  filterBtns.forEach(btn => {
    btn.addEventListener('click', () => {
      filterBtns.forEach(b => b.classList.remove('active'));
      btn.classList.add('active');
      activeCategory = btn.dataset.category;
      applyFilters();
    });
  });

  searchInput.addEventListener('input', applyFilters);

  document.addEventListener('keydown', e => {
    if (e.target === searchInput) {
      if (e.key === 'Escape') { searchInput.blur(); e.preventDefault(); }
      return;
    }
    if (e.key === '/') { e.preventDefault(); searchInput.focus(); return; }
    if (e.key === 'j') { setFocus(focusIndex + 1); e.preventDefault(); }
    if (e.key === 'k') { setFocus(focusIndex - 1); e.preventDefault(); }
    if (e.key === 'o' || e.key === 'Enter') {
      const visible = getVisible();
      if (focusIndex >= 0 && focusIndex < visible.length) {
        const link = visible[focusIndex].querySelector('.card-title a');
        if (link) window.open(link.href, '_blank');
        e.preventDefault();
      }
    }
  });
})();
</script>
</body>
</html>
