(function () {
  var cal = document.querySelector(".diary-cal");
  if (!cal) return;

  var toggle = document.createElement("button");
  toggle.type = "button";
  toggle.className = "panel-toggle";
  toggle.textContent = "Kalender";
  toggle.setAttribute("aria-expanded", "false");
  toggle.addEventListener("click", function () {
    var open = cal.classList.toggle("is-open");
    toggle.setAttribute("aria-expanded", open ? "true" : "false");
  });

  var main = document.querySelector("main");
  if (main) {
    main.parentNode.insertBefore(toggle, main);
    main.parentNode.insertBefore(cal, main);
  }

  cal.querySelectorAll(".diary-cal-drop > button").forEach(wireDropdown);

  var pageHeader = document.querySelector("header");
  function updateTop() {
    if (!pageHeader || getComputedStyle(cal).position !== "fixed") return;
    var rect = pageHeader.getBoundingClientRect();
    cal.style.top = Math.max(8, rect.bottom + 8) + "px";
  }
  window.addEventListener("scroll", updateTop, { passive: true });
  window.addEventListener("resize", updateTop);
  updateTop();
})();
