document.addEventListener("DOMContentLoaded", function () {
  var widget = document.querySelector(".bird-sightings-widget");
  if (!widget) return;

  var list = widget.querySelector(".bird-sightings-list");

  widget.addEventListener("click", function (e) {
    if (e.target.classList.contains("add-sighting-row")) {
      var rows = list.querySelectorAll(".bird-sighting-row");
      var template = rows[rows.length - 1];
      if (!template) return;
      var clone = template.cloneNode(true);
      clone.querySelectorAll("select, input").forEach(function (el) {
        el.value = "";
      });
      clone.querySelector('input[name$="_quantity"]').value = "1";
      list.appendChild(clone);
    }

    if (e.target.classList.contains("remove-sighting")) {
      var rows = list.querySelectorAll(".bird-sighting-row");
      if (rows.length > 1) {
        e.target.closest(".bird-sighting-row").remove();
      }
    }
  });
});
