(function(){
  var translations = {
    bs_lat:{
      nav_home:"Početna", nav_about:"O nama", nav_scena:"SCENA+", nav_planning:"Planiramo", nav_recommended:"Preporučujemo",
      nav_contact:"Kontakt", search_ph:"Pretraga...", lang_label:"Jezik",
      script_lat:"Latinica", script_cyr:"Ćirilica"
    },
    bs_cyr:{
      nav_home:"Почетна", nav_about:"О нама", nav_scena:"СЦЕНА+", nav_planning:"Планирамо", nav_recommended:"Препоручујемо",
      nav_contact:"Контакт", search_ph:"Претрага...", lang_label:"Језик",
      script_lat:"Latinica", script_cyr:"Ćirilica"
    },
    en:{
      nav_home:"Home", nav_about:"About Us", nav_scena:"SCENA+", nav_planning:"Planning", nav_recommended:"Recommended",
      nav_contact:"Contact", search_ph:"Search...", lang_label:"Language"
    },
    de:{
      nav_home:"Startseite", nav_about:"Über uns", nav_scena:"SCENA+", nav_planning:"Planung", nav_recommended:"Empfohlen",
      nav_contact:"Kontakt", search_ph:"Suche...", lang_label:"Sprache"
    },
    tr:{
      nav_home:"Ana Sayfa", nav_about:"Hakkımızda", nav_scena:"SCENA+", nav_planning:"Planlama", nav_recommended:"Önerilenler",
      nav_contact:"İletişim", search_ph:"Ara...", lang_label:"Dil"
    }
  };

  function applyLang(code){
    var dict = translations[code] || translations.bs_lat;
    document.documentElement.setAttribute("lang", code.split("_")[0]);
    document.querySelectorAll("[data-i18n]").forEach(function(el){
      var key = el.getAttribute("data-i18n");
      if(dict[key]) el.textContent = dict[key];
    });
    var current = document.querySelector(".lang-current");
    if(current){
      var labels = {bs_lat:"BS · Lat", bs_cyr:"BS · Ćir", en:"EN", de:"DE", tr:"TR"};
      current.textContent = labels[code] || "BS · Lat";
    }
    localStorage.setItem("bh_lang", code);
  }

  document.addEventListener("DOMContentLoaded", function(){
    var saved = localStorage.getItem("bh_lang") || "bs_lat";
    applyLang(saved);

    var langBtn = document.querySelector(".lang-btn");
    var langMenu = document.querySelector(".lang-menu");
    if(langBtn && langMenu){
      langBtn.addEventListener("click", function(e){
        e.stopPropagation();
        langMenu.classList.toggle("open");
      });
      document.addEventListener("click", function(){ langMenu.classList.remove("open"); });
      langMenu.querySelectorAll("[data-lang]").forEach(function(btn){
        btn.addEventListener("click", function(){
          applyLang(btn.getAttribute("data-lang"));
          langMenu.classList.remove("open");
        });
      });
    }

    var mobileToggle = document.querySelector(".mobile-toggle");
    var mainNav = document.querySelector(".main-nav");
    if(mobileToggle && mainNav){
      mobileToggle.addEventListener("click", function(){
        mainNav.classList.toggle("open-mobile");
        mainNav.style.display = mainNav.classList.contains("open-mobile") ? "flex" : "";
        if(mainNav.classList.contains("open-mobile")){
          mainNav.style.position="absolute";
          mainNav.style.top="64px";
          mainNav.style.left="0";
          mainNav.style.right="0";
          mainNav.style.background="#fff";
          mainNav.style.flexDirection="column";
          mainNav.style.padding="16px 24px";
          mainNav.style.borderBottom="1px solid #E2E8F0";
          mainNav.style.gap="14px";
        }
      });
    }

    var newsTabs = document.querySelectorAll(".news-tab");
    var newsFeeds = document.querySelectorAll(".news-feed-panel");
    if(newsTabs.length){
      newsTabs.forEach(function(tab){
        tab.addEventListener("click", function(){
          newsTabs.forEach(function(t){ t.classList.remove("active"); });
          tab.classList.add("active");
          var target = tab.getAttribute("data-feed");
          newsFeeds.forEach(function(panel){
            panel.style.display = panel.getAttribute("data-feed") === target ? "block" : "none";
          });
        });
      });
    }

    var contactForm = document.querySelector("#contact-form");
    if(contactForm){
      contactForm.addEventListener("submit", function(e){
        e.preventDefault();
        var status = contactForm.querySelector(".form-status");
        if(status){
          status.textContent = "Hvala! Vaša poruka je zabilježena. Za sada ovaj obrazac ne šalje e-mail automatski — molimo dovršite integraciju u WordPress admin panelu (npr. WPForms / Contact Form 7) nakon uploada.";
          status.style.color = "#16A34A";
        }
        contactForm.reset();
      });
    }
  });
})();
