<style>
  .recommendations-section {
    max-width: 1200px;
    margin: 3rem auto;
    padding: 0 20px;
  }
  .recommendations-section.hidden { display: none; }
  .rec-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
  }
  .rec-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }
  .rec-loading {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #64748b;
    font-size: 0.9rem;
  }
  .rec-spinner {
    width: 18px;
    height: 18px;
    border: 2px solid rgba(99, 102, 241, 0.3);
    border-top-color: #6366f1;
    border-radius: 50%;
    animation: rec-spin 0.8s linear infinite;
  }
  @keyframes rec-spin { to { transform: rotate(360deg); } }
  .rec-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1.25rem;
  }
  @media (max-width: 1024px) { .rec-grid { grid-template-columns: repeat(3, 1fr); } }
  @media (max-width: 768px) { .rec-grid { grid-template-columns: repeat(2, 1fr); } }
  @media (max-width: 480px) { .rec-grid { grid-template-columns: 1fr; } }
  .rec-card {
    background: rgba(30, 41, 59, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
    text-decoration: none;
    display: block;
  }
  .rec-card:hover {
    border-color: #6366f1;
    transform: translateY(-4px);
    box-shadow: 0 10px 30px rgba(99, 102, 241, 0.2);
  }
  .rec-card-img {
    width: 100%;
    aspect-ratio: 16/9;
    overflow: hidden;
    background: rgba(15, 23, 42, 0.6);
  }
  .rec-card-img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
  }
  .rec-card:hover .rec-card-img img { transform: scale(1.05); }
  .rec-card-img-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.2) 0%, rgba(139, 92, 246, 0.2) 100%);
  }
  .rec-card-body { padding: 1rem; }
  .rec-card-type {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 0.25rem 0.6rem;
    border-radius: 4px;
    margin-bottom: 0.5rem;
  }
  .rec-card-type.post { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
  .rec-card-type.product { background: rgba(16, 185, 129, 0.2); color: #34d399; }
  .rec-card-type.course { background: rgba(139, 92, 246, 0.2); color: #a78bfa; }
  .rec-card-title {
    font-size: 0.95rem;
    font-weight: 600;
    color: #fff;
    margin-bottom: 0.4rem;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .rec-card-excerpt {
    font-size: 0.8rem;
    color: #94a3b8;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    margin-bottom: 0.5rem;
  }
  .rec-card-price {
    font-size: 1rem;
    font-weight: 700;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }
  .rec-empty { display: none; }
</style>
<section class="recommendations-section hidden" id="recommendationsSection" data-source-type="{{sourceType}}" data-source-id="{{sourceId}}" data-content-type="{{contentType}}">
  <div class="rec-header">
    <h2 class="rec-title">{{#if recTitle}}{{recTitle}}{{else}}✨ Recommended for You{{/if}}</h2>
    <div class="rec-loading" id="recLoading"><div class="rec-spinner"></div><span>Loading...</span></div>
  </div>
  <div class="rec-grid" id="recGrid"></div>
</section>
<script>
(function() {
  var section = document.getElementById('recommendationsSection');
  if (!section) return;
  var sourceType = section.dataset.sourceType;
  var sourceId = section.dataset.sourceId;
  var contentType = section.dataset.contentType;
  var grid = document.getElementById('recGrid');
  var loading = document.getElementById('recLoading');
  var endpoint = '/api/recommendations/' + contentType + 's/' + sourceId + '?limit=4';
  if (contentType === 'personalized') {
    endpoint = '/api/recommendations/personalized/post?limit=4';
  }
  fetch(endpoint, { credentials: 'include' })
    .then(function(r) { return r.json(); })
    .then(function(data) {
      loading.style.display = 'none';
      if (!data.items || data.items.length === 0) return;
      section.classList.remove('hidden');
      grid.innerHTML = data.items.map(function(item, idx) {
        var type = item.type || contentType;
        var url = type === 'post' ? '/post/' + item.slug : (type === 'product' ? '/shop/product/' + item.slug : '/courses/' + item.slug);
        var emoji = type === 'post' ? '📝' : (type === 'product' ? '🛍️' : '📚');
        var imgHtml = item.image ? '<img src="' + item.image + '" alt="' + item.title + '">' : '<div class="rec-card-img-placeholder">' + emoji + '</div>';
        var priceHtml = item.metadata && item.metadata.price ? '<div class="rec-card-price">$' + parseFloat(item.metadata.price).toFixed(2) + '</div>' : '';
        return '<a href="' + url + '" class="rec-card" data-source-type="' + sourceType + '" data-source-id="' + sourceId + '" data-clicked-type="' + type + '" data-clicked-id="' + item.id + '" data-position="' + idx + '">' +
          '<div class="rec-card-img">' + imgHtml + '</div>' +
          '<div class="rec-card-body">' +
            '<span class="rec-card-type ' + type + '">' + type + '</span>' +
            '<div class="rec-card-title">' + item.title + '</div>' +
            (item.excerpt ? '<div class="rec-card-excerpt">' + item.excerpt + '</div>' : '') +
            priceHtml +
          '</div></a>';
      }).join('');
    })
    .catch(function() { loading.style.display = 'none'; });

  // Event delegation for tracking clicks (CSP-compliant)
  grid.addEventListener('click', function(e) {
    var card = e.target.closest('.rec-card');
    if (!card) return;
    var data = card.dataset;
    fetch('/api/recommendations/track-click', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      credentials: 'include',
      body: JSON.stringify({
        sourceType: data.sourceType,
        sourceId: data.sourceId,
        recommendationType: 'related',
        clickedType: data.clickedType,
        clickedId: data.clickedId,
        position: parseInt(data.position, 10)
      })
    }).catch(function() {});
  });
})();
</script>

