<section class="py-16 md:py-24 bg-background text-foreground{% if class %} {{ class }}{% endif %}"{% if countUp %} data-stats{% endif %}>
  <div class="mx-auto max-w-6xl px-4">
    {% if heading %}<h2 class="pd-display text-center text-3xl text-primary md:text-4xl">{{ heading }}</h2>{% endif %}
    {% if intro %}<p class="pd-body mx-auto mt-4 max-w-2xl text-center text-lg text-muted-foreground">{{ intro }}</p>{% endif %}
    <dl class="mt-12 grid grid-cols-2 gap-8 {% if columns == 2 %}md:grid-cols-2{% elif columns == 3 %}md:grid-cols-3{% else %}md:grid-cols-4{% endif %}">
      {% for item in items %}
      <div class="text-center">
        <dd class="pd-display text-4xl text-accent md:text-5xl">{% if item.prefix %}{{ item.prefix }}{% endif %}<span{% if countUp %} data-countup-value="{{ item.value }}"{% endif %}>{{ item.value }}</span>{% if item.suffix %}{{ item.suffix }}{% endif %}</dd>
        {% if item.label %}<dt class="pd-hand mt-2 text-xl text-muted-foreground">{{ item.label }}</dt>{% endif %}
      </div>
      {% endfor %}
    </dl>
  </div>
</section>
{% if countUp %}<script>
(function(){
  var sections = document.querySelectorAll('[data-stats]');
  var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  function animate(section){
    section.querySelectorAll('[data-countup-value]').forEach(function(node){
      var raw = node.getAttribute('data-countup-value') || '';
      var target = parseFloat(raw.replace(/[^0-9.\-]/g, ''));
      if (isNaN(target)) return;
      var decimals = (raw.split('.')[1] || '').length;
      var start = null, dur = 1200;
      function step(ts){
        if (start === null) start = ts;
        var p = Math.min((ts - start) / dur, 1);
        var eased = 1 - Math.pow(1 - p, 3);
        node.textContent = (target * eased).toFixed(decimals);
        if (p < 1) requestAnimationFrame(step); else node.textContent = target.toFixed(decimals);
      }
      requestAnimationFrame(step);
    });
  }
  sections.forEach(function(section){
    if (reduce || !('IntersectionObserver' in window)) return;
    var io = new IntersectionObserver(function(entries){
      entries.forEach(function(e){ if (e.isIntersecting){ animate(section); io.disconnect(); } });
    }, { threshold: 0.3 });
    io.observe(section);
  });
})();
</script>{% endif %}
