(() => {
  document.querySelectorAll('[data-feed-media]').forEach((container) => {
    const source = container.querySelector('.feed-media-source');
    const feature = container.querySelector('.feed-media-feature');
    if (!source || !feature) return;

    const videos = [...source.querySelectorAll('video')].map((item) => {
      const video = item.cloneNode(true);
      video.removeAttribute('poster');
      video.controls = true;
      video.playsInline = true;
      video.preload = 'metadata';
      return video;
    });
    const images = [...source.querySelectorAll('.kg-gallery-card img, .kg-image-card img')].map((item) => {
      const link = document.createElement('a');
      link.href = item.currentSrc || item.src;
      link.setAttribute('aria-label', item.alt ? `Открыть: ${item.alt}` : 'Открыть изображение');
      link.appendChild(item.cloneNode(true));
      return link;
    });

    const items = videos.length ? [...videos, ...images] : [feature, ...images];
    if (items.length === 1 && !videos.length) return;
    const track = document.createElement('div');
    track.className = 'feed-media-track';
    items.forEach((item) => {
      const slide = document.createElement('div');
      slide.className = 'feed-media-slide';
      slide.appendChild(item);
      track.appendChild(slide);
    });
    container.replaceChildren(track, source);
    if (items.length > 1) {
      const count = document.createElement('span');
      count.className = 'feed-media-count';
      count.textContent = `1 / ${items.length}`;
      container.appendChild(count);
      track.addEventListener('scroll', () => {
        const index = Math.round(track.scrollLeft / Math.max(track.clientWidth, 1));
        count.textContent = `${Math.min(index + 1, items.length)} / ${items.length}`;
      }, { passive: true });
    }
  });
})();

(() => {
  const button = document.querySelector('[data-menu-toggle]');
  const nav = document.querySelector('#main-nav');
  if (!button || !nav) return;
  button.addEventListener('click', () => {
    const open = nav.classList.toggle('is-open');
    button.setAttribute('aria-expanded', String(open));
  });
})();

(() => {
  const toggle = document.querySelector('[data-category-toggle]');
  const menu = document.querySelector('[data-category-menu]');
  if (!toggle || !menu) return;
  const close = () => { menu.classList.remove('is-open'); toggle.setAttribute('aria-expanded', 'false'); };
  toggle.addEventListener('click', (event) => {
    event.stopPropagation();
    const open = menu.classList.toggle('is-open');
    toggle.setAttribute('aria-expanded', String(open));
  });
  document.addEventListener('click', (event) => { if (!menu.contains(event.target)) close(); });
  document.addEventListener('keydown', (event) => { if (event.key === 'Escape') close(); });
})();

(() => {
  const images = document.querySelectorAll('.gh-content .kg-gallery-image img');
  if (!images.length || !('HTMLDialogElement' in window)) return;
  const dialog = document.createElement('dialog');
  dialog.className = 'media-lightbox';
  dialog.innerHTML = '<button type="button" aria-label="Закрыть">×</button><img alt="">';
  const preview = dialog.querySelector('img');
  dialog.querySelector('button').addEventListener('click', () => dialog.close());
  dialog.addEventListener('click', (event) => { if (event.target === dialog) dialog.close(); });
  document.body.appendChild(dialog);
  images.forEach((image) => image.addEventListener('click', () => {
    preview.src = image.currentSrc || image.src;
    preview.alt = image.alt || '';
    dialog.showModal();
  }));
})();
