{% extends 'base.html' %}
{% load i18n %}
{% load static %}

{% block extra_css %}
<style>
  [x-cloak] { display: none !important; }

  /* ============================================================
     Apple-inspired design system — Collections list
     Mirrors the Files page tokens so the whole app shares one
     visual language.
     ============================================================ */
  :root {
    --apple-bg: #f5f5f7;
    --apple-text: #1d1d1f;
    --apple-gray: #6e6e73;
    --apple-blue: #0071e3;
    --apple-blue-hover: #0077ed;
    --apple-red: #ff3b30;
    --apple-green: #1e7b34;
    --apple-separator: rgba(0, 0, 0, 0.06);
    --apple-ease: cubic-bezier(0.28, 0.11, 0.32, 1);
    --apple-radius-lg: 28px;
    --apple-radius-md: 22px;
    --apple-shadow-card: 0 4px 24px rgba(0, 0, 0, 0.05);
    --apple-shadow-lift: 0 18px 44px rgba(0, 0, 0, 0.10);
  }

  body {
    background-color: var(--apple-bg) !important;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
      "Helvetica Neue", "Segoe UI", Roboto, Arial, sans-serif;
    color: var(--apple-text);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  .apple-wrap { max-width: 76rem; margin: 0 auto; padding: 0 1rem; }

  /* ---------- Page header ---------- */
  .page-header {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
    gap: 1rem; padding: 1.9rem 0.25rem 1.4rem;
  }
  .page-title-group { display: flex; flex-direction: column; gap: 0.2rem; }
  .section-title { font-size: clamp(1.5rem, 3.2vw, 2rem); font-weight: 700; letter-spacing: -0.025em; margin: 0; }
  .section-sub { font-size: 0.92rem; color: var(--apple-gray); margin: 0; }

  /* ---------- Buttons ---------- */
  .apple-btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.45rem;
    min-height: 44px; padding: 0.6rem 1.4rem; border-radius: 980px;
    font-weight: 600; font-size: 0.95rem; border: none; cursor: pointer; text-decoration: none;
    transition: background 0.3s var(--apple-ease), transform 0.3s var(--apple-ease), box-shadow 0.3s var(--apple-ease);
  }
  .apple-btn:active { transform: scale(0.97); }
  .apple-btn-primary { background: var(--apple-blue); color: #fff; box-shadow: 0 6px 18px rgba(0, 113, 227, 0.28); }
  .apple-btn-primary:hover { background: var(--apple-blue-hover); color: #fff; }
  .apple-btn svg { width: 1.05rem; height: 1.05rem; }

  /* ---------- Collection cards ---------- */
  .collection-grid {
    display: grid; gap: 1.1rem;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
  .collection-card {
    position: relative; display: block; overflow: hidden;
    background: #fff; border-radius: var(--apple-radius-lg);
    border: 1px solid rgba(0, 0, 0, 0.04); box-shadow: var(--apple-shadow-card);
    transition: transform 0.45s var(--apple-ease), box-shadow 0.45s var(--apple-ease);
    text-decoration: none; color: inherit;
  }
  .collection-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--apple-shadow-lift);
  }
  .collection-card:active { transform: translateY(-1px) scale(0.99); }

  .cover-wrap {
    position: relative; aspect-ratio: 16 / 10; overflow: hidden;
    background: linear-gradient(135deg, #ececf0, #fafafa);
  }
  .cover-img {
    width: 100%; height: 100%; object-fit: cover;
    transition: transform 0.6s var(--apple-ease);
  }
  .collection-card:hover .cover-img { transform: scale(1.045); }
  .cover-scrim {
    position: absolute; inset: 0;
    background: linear-gradient(to top, rgba(0,0,0,0.42), transparent 55%);
  }
  .cover-badge {
    position: absolute; bottom: 0.8rem; left: 1rem; right: 1rem;
    display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
    color: #fff;
  }
  .cover-count {
    display: inline-flex; align-items: center; gap: 0.35rem;
    font-size: 0.78rem; font-weight: 600;
    background: rgba(255,255,255,0.22); backdrop-filter: blur(8px);
    padding: 0.3rem 0.7rem; border-radius: 980px;
  }
  .cover-count svg { width: 0.85rem; height: 0.85rem; }

  /* Cover placeholder when a collection has no images */
  .cover-empty {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.4rem;
    height: 100%; color: var(--apple-gray);
  }
  .cover-empty svg { width: 2.4rem; height: 2.4rem; opacity: 0.5; }
  .cover-empty span { font-size: 0.82rem; font-weight: 500; }

  /* Card body */
  .card-body { padding: 0.95rem 1.1rem 1.1rem; }
  .card-title {
    font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; margin: 0 0 0.2rem;
    display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden;
  }
  .card-desc {
    font-size: 0.85rem; color: var(--apple-gray); margin: 0; line-height: 1.45;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
    min-height: 2.45em;
  }
  .card-footer {
    display: flex; align-items: center; justify-content: space-between;
    margin-top: 0.75rem; padding-top: 0.7rem; border-top: 1px solid var(--apple-separator);
  }
  .card-updated { font-size: 0.74rem; color: #a1a1a6; }

  /* Overlay action pills (top-right of cover) */
  .card-actions {
    position: absolute; top: 0.7rem; right: 0.7rem;
    display: flex; gap: 0.35rem;
    opacity: 1;
    transition: opacity 0.25s ease;
  }
  .act-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px; border-radius: 50%;
    background: rgba(255,255,255,0.92); backdrop-filter: blur(8px);
    color: #3a3a3c; border: none; cursor: pointer; text-decoration: none;
    box-shadow: 0 2px 10px rgba(0,0,0,0.12);
    transition: background 0.25s ease, color 0.25s ease, transform 0.25s ease;
  }
  .act-btn:hover { transform: translateY(-1px); }
  .act-btn.blue:hover { background: var(--apple-blue); color: #fff; }
  .act-btn.red:hover { background: var(--apple-red); color: #fff; }
  .act-btn svg { width: 1.05rem; height: 1.05rem; }

  @media (min-width: 640px) {
    .card-actions { opacity: 0; }
    .collection-card:hover .card-actions { opacity: 1; }
    .collection-card:focus-within .card-actions { opacity: 1; }
  }

  /* ---------- Empty state ---------- */
  .empty-state {
    grid-column: 1 / -1;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    padding: 4rem 1.5rem; text-align: center;
    background: #fff; border-radius: var(--apple-radius-lg);
    border: 1px dashed rgba(0, 0, 0, 0.14);
  }
  .empty-state svg { width: 3.4rem; height: 3.4rem; color: #c7c7cc; }
  .empty-title { font-size: 1.15rem; font-weight: 700; margin: 1rem 0 0.25rem; }
  .empty-sub { font-size: 0.9rem; color: var(--apple-gray); margin: 0 0 1.4rem; }

  /* ---------- Pagination (Apple pills) ---------- */
  .pagination { display: flex; justify-content: center; gap: 0.4rem; padding: 2.2rem 0 1rem; flex-wrap: wrap; }
  .pagination a, .pagination span.current {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 38px; height: 38px; padding: 0 0.9rem;
    border-radius: 980px; font-size: 0.88rem; font-weight: 600; text-decoration: none;
    background: #fff; color: var(--apple-blue);
    border: 1px solid rgba(0, 0, 0, 0.06); box-shadow: 0 1px 4px rgba(0,0,0,0.04);
    transition: background 0.25s ease, transform 0.25s ease;
  }
  .pagination a:hover { background: #f0f0f3; }
  .pagination span.current { background: var(--apple-blue); color: #fff; border-color: var(--apple-blue); }
</style>
{% endblock %}

{% block content %}
<div class="apple-wrap">
    <div class="page-header">
        <div class="page-title-group">
            <h1 class="section-title">{% trans "Image Collections" %}</h1>
            <p class="section-sub">{{ collections.paginator.count }} {% trans "collections" %} · {% trans "photos, wallpapers & memories" %}</p>
        </div>
        <a href="{% url 'collection-create' %}" class="apple-btn apple-btn-primary">
            <svg fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/>
            </svg>
            {% trans "New Collection" %}
        </a>
    </div>

    <div id="paginated-content">
    {% include "links/includes/collection_list_items.html" %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

function deleteCollection(collectionId) {
    if (confirm('{% trans "Are you sure you want to delete this collection? All images in this collection will be deleted." %}')) {
        fetch(`/api/collections/${collectionId}`, {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': getCookie('csrftoken')
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            location.reload();
        })
        .catch(error => {
            console.error('Error:', error);
            alert('Failed to delete collection');
        });
    }
}
</script>
{% endblock %}
