(() => {
  const acceptanceKey = 'tm-assist-disclaimer-accepted-v1';
  if (window.location.pathname.toLowerCase().endsWith('/disclaimer.html')) return;
  if (localStorage.getItem(acceptanceKey) === 'yes') return;

  const showGate = () => {
    const gate = document.createElement('div');
    gate.className = 'disclaimer-gate';
    gate.setAttribute('role', 'dialog');
    gate.setAttribute('aria-modal', 'true');
    gate.setAttribute('aria-labelledby', 'disclaimerGateTitle');
    gate.innerHTML = `
      <div class="disclaimer-gate-panel">
        <div class="disclaimer-gate-mark" aria-hidden="true">TM</div>
        <p class="disclaimer-gate-kicker">Important Information</p>
        <h1 id="disclaimerGateTitle"><span>Disclaimer.</span></h1>
        <div class="disclaimer-gate-rule" aria-hidden="true"></div>
        <div class="disclaimer-gate-copy">
          <p>The information contained on this website is provided for general informational purposes and should not be construed as legal advice or a legal opinion in relation to any particular matter.</p>
          <p>Trademark availability, registrability, use and enforceability depend upon several legal and factual considerations, including prior rights, market use, applicable law, examination practice and the circumstances surrounding each mark.</p>
          <p>No trademark search, clearance exercise or professional assessment can guarantee registration or eliminate the possibility of objection, opposition, infringement claims or other disputes.</p>
          <p>Submission of an enquiry or information through this website does not by itself create a professional or adviser-client relationship. Any engagement shall arise only upon express acceptance of the assignment on mutually agreed terms.</p>
          <p>TM Assist focuses on trademarks, brand protection and related commercial matters concerning pharmaceutical and healthcare businesses. Nothing contained on this website constitutes medical, clinical, scientific or drug-regulatory advice.</p>
        </div>
        <div class="disclaimer-gate-actions">
          <button type="button" class="disclaimer-agree">I Agree <span>→</span></button>
          <button type="button" class="disclaimer-decline">Decline</button>
          <a href="disclaimer.html">Read the full disclaimer</a>
        </div>
      </div>`;

    document.body.appendChild(gate);
    document.documentElement.classList.add('disclaimer-locked');

    const agree = gate.querySelector('.disclaimer-agree');
    const decline = gate.querySelector('.disclaimer-decline');
    agree.focus();

    agree.addEventListener('click', () => {
      localStorage.setItem(acceptanceKey, 'yes');
      gate.classList.add('is-closing');
      document.documentElement.classList.remove('disclaimer-locked');
      setTimeout(() => gate.remove(), 320);
    });

    decline.addEventListener('click', () => {
      if (history.length > 1) history.back();
      else window.location.replace('about:blank');
    });

    document.addEventListener('keydown', event => {
      if (event.key === 'Escape') event.preventDefault();
    });
  };

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', showGate);
  else showGate();
})();
