<link href="https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/font-face.css" rel="stylesheet" type="text/css" />

<style>

@font-face {
  font-family: "IRANSans";
  src: url("/download/attachments/85045416/IRANSansXVF.woff2") format("woff2");
}

@font-face {
  font-family: "Vazir";
  src: url("/download/attachments/85045416/Vazir-Regular.woff2") format("woff2");
}


/* ============================================
   پس‌زمینه‌ی اصلی صفحه
============================================ */

body {
  background: 
    radial-gradient(circle at 20% 10%, rgba(139, 92, 246, 0.08) 0%, transparent 40%),
    radial-gradient(circle at 80% 60%, rgba(236, 72, 153, 0.06) 0%, transparent 40%),
    radial-gradient(circle at 50% 100%, rgba(59, 130, 246, 0.05) 0%, transparent 50%),
    #fafbff;
  min-height: 100vh;
  background-attachment: fixed;
}


/* ============================================
   هدر زیبا با گرادیانت
============================================ */

.library-hero {
  max-width: 1100px;
  margin: 0 auto 25px;
  padding: 0 20px;
}

.hero-inner {
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 35%, #ec4899 75%, #f43f5e 100%);
  border-radius: 28px;
  padding: 50px 40px;
  text-align: center;
  position: relative;
  overflow: hidden;
  box-shadow:
    0 25px 50px -12px rgba(99, 102, 241, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.1) inset;
}

.hero-inner::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, rgba(255,255,255,0.15) 0%, transparent 70%);
  border-radius: 50%;
  animation: float 8s ease-in-out infinite;
}

.hero-inner::after {
  content: '';
  position: absolute;
  bottom: -30%;
  left: -10%;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  border-radius: 50%;
  animation: float 10s ease-in-out infinite reverse;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-20px, 20px) scale(1.05); }
}

.hero-content {
  position: relative;
  z-index: 2;
}

.hero-title {
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 38px;
  font-weight: 800;
  color: #fff;
  margin: 0;
  text-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  letter-spacing: -0.5px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.hero-title-icon {
  font-size: 42px;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,0.2));
  animation: bounce 2s ease-in-out infinite;
  display: inline-block;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.hero-subtitle {
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 15px;
  color: rgba(255, 255, 255, 0.95);
  margin: 14px 0 0;
  font-weight: 400;
  letter-spacing: 0.3px;
}

.hero-stats {
  display: flex;
  justify-content: center;
  gap: 30px;
  margin-top: 20px;
  flex-wrap: wrap;
}

.hero-stat {
  background: rgba(255, 255, 255, 0.18);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.25);
  padding: 8px 20px;
  border-radius: 999px;
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 13px;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
}


/* ============================================
   نوار جستجوی پیشرفته
============================================ */

#book-toolbar {
  width: 100%;
  max-width: 700px;
  margin: 0 auto 30px;
  padding: 0 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  direction: rtl;
}

.search-wrapper {
  position: relative;
  width: 100%;
}

#book-search {
  width: 100%;
  height: 54px;
  padding: 0 60px 0 50px;
  border-radius: 999px;
  border: 2px solid transparent;
  background: #fff;
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 14px;
  color: #2d3748;
  outline: none;
  box-shadow:
    0 10px 40px rgba(99, 102, 241, 0.12),
    0 2px 8px rgba(0, 0, 0, 0.04),
    0 0 0 1px rgba(99, 102, 241, 0.08);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

#book-search::placeholder {
  color: #a0aec0;
  font-weight: 400;
}

#book-search:hover {
  box-shadow:
    0 15px 50px rgba(99, 102, 241, 0.18),
    0 4px 12px rgba(0, 0, 0, 0.06),
    0 0 0 1px rgba(99, 102, 241, 0.12);
}

#book-search:focus {
  border-color: #8b5cf6;
  box-shadow:
    0 20px 60px rgba(139, 92, 246, 0.25),
    0 0 0 4px rgba(139, 92, 246, 0.1);
  transform: translateY(-2px);
}

.search-icon {
  position: absolute;
  right: 22px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  color: #8b5cf6;
  pointer-events: none;
  transition: transform 0.3s ease;
}

#book-search:focus + .search-icon {
  transform: translateY(-50%) scale(1.1);
}

.search-clear {
  position: absolute;
  left: 18px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #f1f5f9;
  border: none;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  color: #64748b;
  font-size: 14px;
  transition: all 0.2s ease;
}

.search-clear:hover {
  background: #e2e8f0;
  color: #1e293b;
}

.search-clear.visible {
  display: flex;
}


/* ============================================
   گرید کتاب‌ها
============================================ */

#book-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 24px;
  padding: 0 40px 60px;
  direction: rtl;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  font-family: Vazir, Tahoma, sans-serif;
}


/* ============================================
   کارت کتاب - طراحی مدرن
============================================ */

.card {
  background: #fff;
  border-radius: 22px;
  box-shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.05),
    0 10px 30px -5px rgba(99, 102, 241, 0.08);
  padding: 20px;
  min-height: 220px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-direction: row-reverse;
  font-family: Vazir, Tahoma, sans-serif;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(99, 102, 241, 0.06);
  animation: cardEnter 0.5s cubic-bezier(0.4, 0, 0.2, 1) backwards;
}

@keyframes cardEnter {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* خط رنگی بالای کارت */
.card::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  height: 4px;
  background: linear-gradient(90deg, #6366f1, #8b5cf6, #ec4899);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.card::after {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(139, 92, 246, 0.04) 0%, transparent 50%);
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.card:hover {
  transform: translateY(-8px);
  box-shadow:
    0 20px 40px -10px rgba(99, 102, 241, 0.2),
    0 10px 20px -5px rgba(0, 0, 0, 0.08);
  border-color: rgba(139, 92, 246, 0.2);
}

.card:hover::before {
  opacity: 1;
}

.card:hover::after {
  opacity: 1;
}

.card:hover .card-icon {
  background: linear-gradient(135deg, #ede9fe, #fce7f3);
  transform: rotate(-5deg) scale(1.05);
}

.card:hover .card-icon img {
  transform: scale(1.1);
}

.card:hover .download-btn {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
}


/* ============================================
   محتوای کارت
============================================ */

.card-content {
  flex: 1;
  min-width: 0;
  text-align: right;
  padding-right: 15px;
  direction: rtl;
  display: flex;
  flex-direction: column;
}

.card-content h3 {
  margin: 0 0 12px;
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.65;
  font-weight: 700;
  color: #1e293b;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 50px;
}

.card-content .dot-line {
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 12.5px;
  line-height: 1.9;
  color: #64748b;
  margin: 2px 0;
  word-break: break-word;
  display: flex;
  align-items: center;
  gap: 5px;
}

.card-content .dot-line::before {
  content: '';
  width: 4px;
  height: 4px;
  background: #cbd5e1;
  border-radius: 50%;
  flex-shrink: 0;
}

.card-meta {
  margin-top: auto;
  padding-top: 10px;
}


/* ============================================
   آیکون / تصویر کتاب
============================================ */

.card-icon {
  flex: 0 0 80px;
  width: 80px;
  height: 96px;
  background: linear-gradient(135deg, #f8fafc, #f1f5f9);
  border-radius: 16px;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 0;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.card-icon::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 16px;
  border: 1px solid rgba(99, 102, 241, 0.08);
}

.card-icon img {
  width: 60px;
  height: 60px;
  object-fit: contain;
  display: block;
  transition: transform 0.4s ease;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.08));
}


/* ============================================
   دکمه‌ی دانلود
============================================ */

.download-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(135deg, #08a9c5, #0891b2);
  color: #fff !important;
  border: none;
  border-radius: 25px;
  padding: 7px 18px;
  font-family: Vazir, Tahoma, sans-serif;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.5;
  text-decoration: none !important;
  margin-top: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(8, 169, 197, 0.25);
  align-self: flex-start;
}

.download-btn:hover {
  transform: translateY(-2px);
  background: linear-gradient(135deg, #0891b2, #0e7490) !important;
  box-shadow: 0 8px 20px rgba(8, 169, 197, 0.35);
}

.download-btn:active {
  transform: translateY(0);
}

.download-btn::before {
  content: '⬇';
  font-size: 11px;
}


/* ============================================
   حالت‌های خالی / خطا / لودینگ
============================================ */

.state-message {
  grid-column: 1 / -1;
  text-align: center;
  font-family: Vazir, Tahoma, sans-serif !important;
  padding: 60px 30px !important;
  border-radius: 22px !important;
  animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.state-empty {
  background: linear-gradient(135deg, #fafbff, #f5f3ff) !important;
  border: 2px dashed #d8b4fe !important;
  color: #6b7280 !important;
}

.state-empty-icon {
  font-size: 56px;
  margin-bottom: 15px;
  display: block;
  opacity: 0.6;
}

.state-empty-title {
  font-size: 16px;
  font-weight: 600;
  color: #4b5563;
  margin-bottom: 6px;
}

.state-empty-sub {
  font-size: 13px;
  color: #9ca3af;
}

.state-error {
  background: linear-gradient(135deg, #fef2f2, #fee2e2) !important;
  border: 2px dashed #fca5a5 !important;
  color: #b91c1c !important;
}

.state-loading {
  background: #fff !important;
  border: 1px solid rgba(99, 102, 241, 0.1) !important;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  margin: 0 auto 15px;
  border: 4px solid #f3f4f6;
  border-top: 4px solid #8b5cf6;
  border-right: 4px solid #ec4899;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}


/* ============================================
   ریسپانسیو
============================================ */

@media (max-width: 1100px) {
  #book-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding-left: 25px;
    padding-right: 25px;
    gap: 20px;
  }
  .hero-inner { padding: 40px 30px; }
  .hero-title { font-size: 32px; }
}

@media (max-width: 700px) {
  #book-toolbar { padding: 0 15px; margin-bottom: 20px; }
  
  #book-search {
    height: 48px;
    font-size: 13px;
    padding: 0 50px 0 45px;
  }
  
  .search-icon { right: 18px; width: 18px; height: 18px; }
  
  #book-grid {
    grid-template-columns: 1fr;
    padding: 0 15px 40px;
    gap: 16px;
  }
  
  .card {
    min-height: auto;
    padding: 16px;
  }
  
  .hero-inner { 
    padding: 35px 20px;
    border-radius: 22px;
  }
  
  .hero-title { 
    font-size: 26px;
    gap: 10px;
  }
  
  .hero-title-icon { font-size: 32px; }
  
  .hero-subtitle { font-size: 13px; }
  
  .hero-stats { gap: 10px; }
  
  .hero-stat { font-size: 12px; padding: 6px 14px; }
}

@media (max-width: 400px) {
  .card { padding: 14px; }
  .card-icon { flex: 0 0 70px; width: 70px; height: 84px; }
  .card-icon img { width: 50px; height: 50px; }
  .card-content h3 { font-size: 14px; }
}

</style>


<!-- ============================================
     HTML
============================================ -->

<!-- هدر زیبا -->
<div class="library-hero">
  <div class="hero-inner">
    <div class="hero-content">
      <h1 class="hero-title">
        <span class="hero-title-icon">📚</span>
        <span>کتابخانه</span>
      </h1>
      <p class="hero-subtitle">دروازه‌ای به دنیای دانش و مطالعه</p>
      <div class="hero-stats" id="hero-stats"></div>
    </div>
  </div>
</div>

<!-- نوار جستجو -->
<div id="book-toolbar">
  <div class="search-wrapper">
    <input id="book-search" type="text"
           placeholder="جستجو در عنوان، حیطه، نویسنده یا سال..."
           autocomplete="off">
    <svg class="search-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
      <circle cx="11" cy="11" r="8"/>
      <line x1="21" y1="21" x2="16.65" y2="16.65"/>
    </svg>
    <button class="search-clear" id="search-clear" type="button" aria-label="پاک کردن جستجو">✕</button>
  </div>
</div>

<div id="book-grid"></div>


<script>
(() => {
  /* ===== تنظیمات ===== */
  const LIB_ID             = "nikan-west-library";
  const FORCED_DATA_PAGE_ID = 85045416;
  const JSON_FILE_NAME     = `library-${LIB_ID}.json`;
  /* =================== */

  function detectBase(){
    try{ if (window.AJS && typeof AJS.contextPath==="function") return AJS.contextPath(); }catch(e){}
    const meta = document.querySelector('meta[name="ajs-context-path"]')?.content;
    if (meta) return meta;
    const m = location.pathname.match(/^(.*)\/(pages|display|download)\//);
    if (m) return m;
    return '';
  }

  const BASE        = detectBase();
  const DATA_PAGE_ID = FORCED_DATA_PAGE_ID;
  const ENDPOINT_ATTACH_LIST =
    `${BASE}/rest/api/content/${DATA_PAGE_ID}/child/attachment`;

  const grid        = document.getElementById("book-grid");
  const searchInput = document.getElementById("book-search");
  const searchClear = document.getElementById("search-clear");
  const heroStats   = document.getElementById("hero-stats");

  let allBooks = [];

  /* ---------- نمایش لودینگ ---------- */
  function showLoading(){
    grid.innerHTML = `
      <div class="state-message state-loading">
        <div class="loading-spinner"></div>
        <div style="color:#6b7280; font-size:14px;">در حال بارگذاری کتاب‌ها...</div>
      </div>`;
  }

  /* ---------- آمار هدر ---------- */
  function updateHeroStats(count){
    if (!heroStats) return;
    heroStats.innerHTML = `
      <div class="hero-stat">📖 ${count} کتاب</div>
      <div class="hero-stat">✨ بروزرسانی شده</div>
    `;
  }

  /* ---------- ساخت کارت ---------- */
  function createBookCard(book, index){
    const authorsBlock = (book.authors && book.authors.length)
      ? book.authors.map(a => `<div class="dot-line">${a}</div>`).join("")
      : `<div class="dot-line">بدون نویسنده</div>`;

    const card = document.createElement("div");
    card.className = "card";
    card.style.animationDelay = `${Math.min(index * 0.05, 0.6)}s`;
    card.innerHTML = `
      <div class="card-content">
        <h3>${book.title || "-"}</h3>
        <div class="dot-line">${book.category || "بدون دسته"}</div>
        ${authorsBlock}
        <div class="dot-line">${book.year || "نامشخص"}</div>
        <div class="card-meta">
          ${book.fileUrl ? `<a href="${book.fileUrl}" target="_blank" rel="noopener" class="download-btn">دانلود</a>` : ""}
        </div>
      </div>
      <div class="card-icon">
        <img src="https://kb.h-soft.ir/download/thumbnails/85045416/12%20-%20Copy.png?api=v2&nonce=1787954132399" alt="کتاب" loading="lazy">
      </div>
    `;
    return card;
  }

  /* ---------- رندر لیست ---------- */
  function renderBooks(list){
    grid.innerHTML = "";
    if (!list.length){
      grid.innerHTML = `
        <div class="state-message state-empty">
          <span class="state-empty-icon">📭</span>
          <div class="state-empty-title">کتابی پیدا نشد</div>
          <div class="state-empty-sub">لطفاً عبارت دیگری جستجو کنید یا فیلتر را پاک کنید.</div>
        </div>`;
      return;
    }
    list.forEach((b, i) => grid.appendChild(createBookCard(b, i)));
  }

  /* ---------- دریافت JSON ---------- */
  async function getExistingAttachmentMeta() {
    const url = `${ENDPOINT_ATTACH_LIST}?filename=${encodeURIComponent(JSON_FILE_NAME)}&expand=results`;
    const res = await fetch(url, { credentials: "include" });
    if (!res.ok) return null;
    const data = await res.json();
    if (!data.results || !data.results.length) return null;
    return data.results;
  }

  async function loadJsonFromAttachment() {
    const meta = await getExistingAttachmentMeta();
    if (!meta) return { libId: LIB_ID, books: [] };

    const downloadLink = meta._links && meta._links.download;
    if (!downloadLink) return { libId: LIB_ID, books: [] };

    const res = await fetch(BASE + downloadLink, { credentials: "include" });
    if (!res.ok) throw new Error("خطا در دانلود JSON: " + res.status);
    const text = await res.text();
    if (!text.trim()) return { libId: LIB_ID, books: [] };

    try {
      const parsed = JSON.parse(text.replace(/^\uFEFF/, '').trim());
      if (!parsed.books) parsed.books = [];
      if (!parsed.libId) parsed.libId = LIB_ID;
      return parsed;
    } catch(e) {
      console.error("JSON parse error:", e);
      return { libId: LIB_ID, books: [] };
    }
  }

  /* ---------- بارگذاری اولیه ---------- */
  async function loadBooks() {
    showLoading();
    try {
      const data = await loadJsonFromAttachment();
      allBooks = data.books || [];
      
      updateHeroStats(allBooks.length);
      
      if (!allBooks.length) {
        grid.innerHTML = `
          <div class="state-message state-empty">
            <span class="state-empty-icon">📚</span>
            <div class="state-empty-title">کتابخانه خالی است</div>
            <div class="state-empty-sub">از طریق فرم ثبت، اولین کتاب را به کتابخانه اضافه کنید.</div>
          </div>`;
        return;
      }
      setTimeout(() => renderBooks(allBooks), 250);
    } catch (e) {
      console.error(e);
      grid.innerHTML = `
        <div class="state-message state-error">
          <span class="state-empty-icon">⚠️</span>
          <div class="state-empty-title">خطا در بارگذاری کتاب‌ها</div>
          <div class="state-empty-sub">${e.message}</div>
        </div>`;
    }
  }

  /* ---------- جستجو ---------- */
  function handleSearch(){
    const q = (searchInput.value || "").trim().toLowerCase();
    
    // نمایش/مخفی کردن دکمه‌ی پاک کردن
    if (q) {
      searchClear.classList.add("visible");
    } else {
      searchClear.classList.remove("visible");
    }

    if (!q){
      renderBooks(allBooks);
      return;
    }

    const filtered = allBooks.filter(b => {
      const title    = (b.title    || "").toLowerCase();
      const category = (b.category || "").toLowerCase();
      const year     = (b.year     || "").toLowerCase();
      const authors  = Array.isArray(b.authors) ? b.authors.join(" ") : (b.authors || "");
      const authorsL = authors.toLowerCase();

      const haystack = `${title} ${category} ${year} ${authorsL}`;
      return haystack.includes(q);
    });

    renderBooks(filtered);
  }

  /* ---------- رویدادها ---------- */
  let searchTimeout;
  searchInput.addEventListener("input", () => {
    clearTimeout(searchTimeout);
    searchTimeout = setTimeout(handleSearch, 180);
  });

  searchClear.addEventListener("click", () => {
    searchInput.value = "";
    searchClear.classList.remove("visible");
    searchInput.focus();
    renderBooks(allBooks);
  });

  loadBooks();
})();
</script>