// --- dentoto-hidden.js ---
// Animated gradient + subtle "goyang" for elements with background #8b002e
(function(){
  const targetRgb = 'rgb(139, 0, 46)'; // nilai dari #8b002e
  const found = [];

  // Cari elemen yang cocok (inline style atau computed style)
  function findTargets() {
    const all = document.querySelectorAll('body *');
    all.forEach(el => {
      try {
        const inline = (el.style && el.style.background) || (el.style && el.style.backgroundColor);
        const computed = getComputedStyle(el).backgroundColor || '';
        if ((inline && inline.includes('#8b002e')) ||
            (inline && inline.includes('rgb(139, 0, 46)')) ||
            computed === targetRgb) {
          if (!found.includes(el)) found.push(el);
        }
      } catch(e){}
    });
  }

  // Jika target belum ditemukan segera, tunggu sampai DOM siap dan ulang cari berkali
  function ensureTargets(maxAttempts = 8) {
    let attempts = 0;
    return new Promise(resolve => {
      const t = setInterval(() => {
        attempts++;
        findTargets();
        if (found.length || attempts >= maxAttempts) {
          clearInterval(t);
          resolve();
        }
      }, 250);
    });
  }

  // Animasi utama menggunakan requestAnimationFrame
  async function start() {
    await ensureTargets();

    if (!found.length) return; // gak ada target, exit quietly

    // Set beberapa parameter animasi
    const speedHue = 0.8;       // kecepatan rotasi hue
    const hueOffset = 40;      // jarak hue antar warna gradasi
    const moveAmp = 6;         // amplitude goyang (px)
    const moveFreq = 0.0025;   // frekuensi goyang

    // Tanda agar script gak dijalankan ganda pada elemen
    found.forEach(el => {
      if (el.__dentoto_anim) return;
      el.__dentoto_anim = true;
      el.style.willChange = 'background, transform';
    });

    let t0 = performance.now();
    function frame(t) {
      const dt = t - t0;
      const baseHue = (dt * speedHue * 0.06) % 360; // generator hue
      const h1 = Math.round(baseHue);
      const h2 = Math.round((baseHue + hueOffset) % 360);

      // buat gradien HSL lembut, mempertahankan nuansa gelap dari #8b002e
      const c1 = `hsl(${h1}, 65%, 22%)`;
      const c2 = `hsl(${h2}, 55%, 16%)`;

      // per-element update (agar aman jika ada beberapa elemen)
      found.forEach((el, i) => {
        // background bergerak: pilihan linear-gradient + posisi yang berubah sedikit
        el.style.background = `linear-gradient(135deg, ${c1}, ${c2})`;

        // efek goyang halus: gabungan sin & cos untuk variasi diagonal
        const dx = Math.sin(dt * (moveFreq + i*0.0002)) * moveAmp;
        const dy = Math.cos(dt * (moveFreq*1.1 + i*0.0003)) * (moveAmp * 0.6);
        el.style.transform = `translate3d(${dx.toFixed(2)}px, ${dy.toFixed(2)}px, 0)`;
      });

      requestAnimationFrame(frame);
    }
    requestAnimationFrame(frame);
  }

  // jalankan saat DOM siap
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', start);
  } else {
    start();
  }
})();