// Accessibility helpers — ARIA live region, focus management, reduced motion.

// Global ARIA live region — Pandička „promluví" textově pro čtečky.
function ensureLiveRegion() {
  if (typeof document === 'undefined') return null;
  let el = document.getElementById('pandicka-live');
  if (!el) {
    el = document.createElement('div');
    el.id = 'pandicka-live';
    el.setAttribute('role', 'status');
    el.setAttribute('aria-live', 'polite');
    el.setAttribute('aria-atomic', 'true');
    el.style.cssText = 'position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0;';
    document.body.appendChild(el);
  }
  return el;
}

function announce(msg) {
  const el = ensureLiveRegion();
  if (!el) return;
  el.textContent = '';
  // mikro pauza aby čtečka znovu zareagovala
  setTimeout(() => { el.textContent = msg; }, 20);
}

function announceAssertive(msg) {
  const el = ensureLiveRegion();
  if (!el) return;
  el.setAttribute('aria-live', 'assertive');
  setTimeout(() => { el.textContent = msg; }, 20);
  setTimeout(() => { el.setAttribute('aria-live', 'polite'); }, 800);
}

function prefersReducedMotion() {
  try {
    return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  } catch { return false; }
}

// Fokusový ring — přidá class na html když se hýbe klávesnicí, sundá ji při kliku myší.
function installFocusRing() {
  if (typeof document === 'undefined') return;
  if (document.documentElement.dataset.focusRingInstalled === '1') return;
  document.documentElement.dataset.focusRingInstalled = '1';
  const onKey = (e) => {
    if (e.key === 'Tab') document.documentElement.classList.add('is-keyboard');
  };
  const onMouse = () => document.documentElement.classList.remove('is-keyboard');
  window.addEventListener('keydown', onKey);
  window.addEventListener('mousedown', onMouse);

  const style = document.createElement('style');
  style.textContent = `
    html.is-keyboard *:focus { outline: 3px solid #F49B63 !important; outline-offset: 3px; border-radius: 6px; }
    html:not(.is-keyboard) *:focus { outline: none; }
    @media (prefers-reduced-motion: reduce) {
      *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
      }
    }
  `;
  document.head.appendChild(style);
}

// Skip-link — umožní uživateli přeskočit navigaci.
function ensureSkipLink() {
  if (typeof document === 'undefined') return;
  if (document.getElementById('skip-link')) return;
  const a = document.createElement('a');
  a.id = 'skip-link';
  a.href = '#main';
  a.textContent = 'Přeskočit na obsah';
  a.style.cssText = `
    position: absolute; top: -100px; left: 10px; z-index: 9999;
    padding: 10px 14px; background: #F49B63; color: #fff;
    font-family: 'Baloo 2', 'Baloo 2', system-ui; font-weight: 600;
    border-radius: 12px; text-decoration: none;
    transition: top 0.2s;
  `;
  a.addEventListener('focus', () => { a.style.top = '10px'; });
  a.addEventListener('blur', () => { a.style.top = '-100px'; });
  document.body.insertBefore(a, document.body.firstChild);
}

function initA11y() {
  if (typeof document === 'undefined') return;
  installFocusRing();
  ensureSkipLink();
  ensureLiveRegion();
}

Object.assign(window, { announce, announceAssertive, prefersReducedMotion, initA11y });
