class FAQAccordion {
  constructor() {
    this.faqItems = document.querySelectorAll('.faq-item');
    this.faqToggleAll = document.getElementById('faqToggleAll');
    this.allExpanded = false;
    if (!this.faqItems.length) return;
    document.querySelectorAll('.faq-question').forEach(btn => {
      btn.addEventListener('click', () => {
        btn.parentElement.classList.toggle('open');
        this.updateLabel();
      });
    });
    if (this.faqToggleAll) {
      this.faqToggleAll.addEventListener('click', () => this.toggleAll());
    }
  }

  toggleAll() {
    this.allExpanded = !this.allExpanded;
    if (this.allExpanded) {
      this.faqItems.forEach((item, i) => {
        setTimeout(() => item.classList.add('open'), i * 220);
      });
    } else {
      const total = this.faqItems.length;
      this.faqItems.forEach((item, i) => {
        setTimeout(() => item.classList.remove('open'), (total - 1 - i) * 60);
      });
    }
    setTimeout(() => this.updateLabel(), this.faqItems.length * 220 + 100);
  }

  updateLabel() {
    const openCount = document.querySelectorAll('.faq-item.open').length;
    this.allExpanded = openCount === this.faqItems.length;
    if (this.faqToggleAll) {
      this.faqToggleAll.textContent = this.allExpanded ? 'Collapse All' : 'Expand All';
    }
  }
}
