// ── тема ──────────────────────────────────────────────────
(function () {
  const root = document.documentElement;
  const saved = localStorage.getItem('remont-theme');
  const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  root.setAttribute('data-theme', saved || (prefersDark ? 'dark' : 'light'));

  const btn = document.getElementById('themeBtn');
  btn.addEventListener('click', () => {
    const next = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    root.setAttribute('data-theme', next);
    localStorage.setItem('remont-theme', next);
    document.querySelector('meta[name=theme-color]')
      .setAttribute('content', next === 'dark' ? '#201F1B' : '#EDE8DE');
  });
})();

// ── тень шапки при скролле ────────────────────────────────
(function () {
  const bar = document.getElementById('topbar');
  const onScroll = () => bar.classList.toggle('scrolled', window.scrollY > 8);
  onScroll();
  window.addEventListener('scroll', onScroll, { passive: true });
})();

// ── появление секций ──────────────────────────────────────
(function () {
  const io = new IntersectionObserver((entries) => {
    entries.forEach((e) => {
      if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); }
    });
  }, { threshold: 0.12, rootMargin: '0px 0px -8% 0px' });
  document.querySelectorAll('.reveal').forEach((el) => io.observe(el));
})();

// ── активный пункт навигации ──────────────────────────────
(function () {
  const links = [...document.querySelectorAll('#nav a')];
  const map = new Map(links.map((a) => [a.getAttribute('href').slice(1), a]));
  const io = new IntersectionObserver((entries) => {
    entries.forEach((e) => {
      if (e.isIntersecting) {
        links.forEach((l) => l.classList.remove('active'));
        map.get(e.target.id)?.classList.add('active');
      }
    });
  }, { rootMargin: '-45% 0px -50% 0px' });
  ['plan', 'living', 'bedroom'].forEach((id) => {
    const el = document.getElementById(id);
    if (el) io.observe(el);
  });
})();

// ── лайтбокс ──────────────────────────────────────────────
(function () {
  const lb = document.getElementById('lightbox');
  const lbImg = document.getElementById('lbImg');
  const close = document.getElementById('lbClose');

  function open(src, alt) {
    lbImg.src = src; lbImg.alt = alt || '';
    lb.classList.add('open'); lb.setAttribute('aria-hidden', 'false');
    document.body.style.overflow = 'hidden';
  }
  function hide() {
    lb.classList.remove('open'); lb.setAttribute('aria-hidden', 'true');
    document.body.style.overflow = '';
    lbImg.src = '';
  }

  document.querySelectorAll('[data-zoom]').forEach((img) => {
    img.style.cursor = 'zoom-in';
    img.addEventListener('click', (ev) => { ev.stopPropagation(); open(img.src, img.alt); });
  });

  lb.addEventListener('click', (e) => { if (e.target !== lbImg) hide(); });
  close.addEventListener('click', hide);
  document.addEventListener('keydown', (e) => { if (e.key === 'Escape') hide(); });
})();
