document.addEventListener('DOMContentLoaded', function () {

  /* ---------------- Mobile hamburger nav ---------------- */
  var hamburger = document.getElementById('hamburger');
  var mainNav = document.getElementById('main-nav');

  if (hamburger && mainNav) {
    hamburger.addEventListener('click', function () {
      var isOpen = mainNav.classList.toggle('open');
      hamburger.classList.toggle('open', isOpen);
      hamburger.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
    });

    // Close the mobile menu after a nav link is tapped
    mainNav.querySelectorAll('a').forEach(function (link) {
      link.addEventListener('click', function () {
        mainNav.classList.remove('open');
        hamburger.classList.remove('open');
        hamburger.setAttribute('aria-expanded', 'false');
      });
    });
  }

  /* ---------------- Instant estimate calculator ---------------- */
  var sizeButtons = document.querySelectorAll('#size-group .pill-btn');
  var serviceCheckboxes = document.querySelectorAll('.calc-service');
  var estimateAmountEl = document.getElementById('estimate-amount');
  var progressFill = document.getElementById('progress-fill');

  var selectedRate = 55; // default matches the pre-selected 1,001-3,000 sq ft pill

  function calculateEstimate() {
    var total = selectedRate;

    serviceCheckboxes.forEach(function (checkbox) {
      if (checkbox.checked) {
        total += parseFloat(checkbox.getAttribute('data-amount')) || 0;
      }
    });

    var low = Math.round(total);
    var high = Math.round(total * 1.3);

    estimateAmountEl.textContent = '$' + low + '–$' + high;

    // Simple visual feedback: fill grows a bit with more selections (purely decorative)
    if (progressFill) {
      var checkedCount = document.querySelectorAll('.calc-service:checked').length;
      var pct = 60 + Math.min(checkedCount * 3, 40);
      progressFill.style.width = pct + '%';
    }
  }

  sizeButtons.forEach(function (btn) {
    btn.addEventListener('click', function () {
      sizeButtons.forEach(function (b) { b.classList.remove('active'); });
      btn.classList.add('active');
      selectedRate = parseFloat(btn.getAttribute('data-rate')) || 0;
      calculateEstimate();
    });
  });

  serviceCheckboxes.forEach(function (checkbox) {
    checkbox.addEventListener('change', calculateEstimate);
  });

  // Run once on load so the display matches the default selections
  calculateEstimate();

  /* ---------------- "Get This Estimate" -> scroll + carry selections to form ---------------- */
  var getEstimateBtn = document.getElementById('get-estimate-btn');
  var contactSection = document.getElementById('contact');
  var formSize = document.getElementById('form-size');
  var formServiceCheckboxes = document.querySelectorAll('.form-service');

  if (getEstimateBtn && contactSection) {
    getEstimateBtn.addEventListener('click', function () {
      // Carry the selected property size across to the full quote form
      var activeSizeBtn = document.querySelector('#size-group .pill-btn.active');
      if (activeSizeBtn && formSize) {
        var sizeLabel = activeSizeBtn.textContent.trim();
        var matched = false;
        for (var i = 0; i < formSize.options.length; i++) {
          if (formSize.options[i].value === sizeLabel) {
            formSize.selectedIndex = i;
            matched = true;
            break;
          }
        }
        if (!matched) {
          formSize.value = sizeLabel;
        }
      }

      // Carry the selected services across to the full quote form
      var checkedValues = [];
      serviceCheckboxes.forEach(function (checkbox) {
        if (checkbox.checked) {
          checkedValues.push(checkbox.value);
        }
      });

      formServiceCheckboxes.forEach(function (checkbox) {
        checkbox.checked = checkedValues.indexOf(checkbox.value) !== -1;
      });

      contactSection.scrollIntoView({ behavior: 'smooth' });
    });
  }

});
