(function ($) {
  'use strict';

  function getConfig(key, fallback) {
    if (window.eepConfig && Object.prototype.hasOwnProperty.call(window.eepConfig, key)) {
      return window.eepConfig[key];
    }
    return fallback;
  }

  function getMessage(key, fallback) {
    const messages = getConfig('messages', {});
    if (messages && Object.prototype.hasOwnProperty.call(messages, key)) {
      return messages[key];
    }
    return fallback;
  }

  function toBool(value, fallback) {
    if (typeof value === 'boolean') {
      return value;
    }
    if (typeof value === 'number') {
      return value !== 0;
    }
    if (typeof value === 'string') {
      const normalized = String(value || '').trim().toLowerCase();
      if (['1', 'true', 'yes', 'on', 'enabled'].indexOf(normalized) >= 0) {
        return true;
      }
      if (['0', 'false', 'no', 'off', 'disabled', ''].indexOf(normalized) >= 0) {
        return false;
      }
    }
    return !!fallback;
  }

  let pagePreloaderController = null;

  function initPagePreloader() {
    if (pagePreloaderController && typeof pagePreloaderController.syncFromConfig === 'function') {
      pagePreloaderController.syncFromConfig();
      return;
    }

    const clampNumber = function (value, min, max, fallback) {
      const numeric = Number(value);
      if (!Number.isFinite(numeric)) {
        return fallback;
      }
      if (numeric < min) {
        return min;
      }
      if (numeric > max) {
        return max;
      }
      return numeric;
    };

    const ensureStyleNode = function () {
      let styleNode = document.getElementById('eep-page-preloader-style');
      if (styleNode) {
        return styleNode;
      }
      styleNode = document.createElement('style');
      styleNode.id = 'eep-page-preloader-style';
      styleNode.type = 'text/css';
      styleNode.textContent = '' +
        '#eep-page-preloader{position:fixed;inset:0;z-index:2147483646;display:flex;align-items:center;justify-content:center;' +
        'background:var(--eep-preloader-bg,#fff);opacity:0;visibility:hidden;pointer-events:none;' +
        'transition:opacity var(--eep-preloader-fade,420ms) ease,visibility 0s linear var(--eep-preloader-fade,420ms);}' +
        '#eep-page-preloader.is-visible{opacity:1;visibility:visible;pointer-events:auto;transition-delay:0s;}' +
        '#eep-page-preloader .eep-page-preloader-stage{width:min(var(--eep-preloader-art-width,420px),86vw);max-height:72vh;display:flex;align-items:center;justify-content:center;}' +
        '#eep-page-preloader .eep-page-preloader-stage img,#eep-page-preloader .eep-page-preloader-stage svg{display:block;width:100%;height:auto;max-width:100%;}' +
        '#eep-page-preloader .eep-page-preloader-stage img{object-fit:contain;}' +
        'html.eep-preloader-active,body.eep-preloader-active{overflow:hidden !important;}' +
        '@media (max-width:767px){#eep-page-preloader .eep-page-preloader-stage{width:min(var(--eep-preloader-art-width-mobile,300px),82vw);}}';
      document.head.appendChild(styleNode);
      return styleNode;
    };

    const defaultSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 200" width="100%" height="100%"><rect width="100%" height="100%" fill="#FFFFFF"/><text x="120" y="130" textLength="530" lengthAdjust="spacing" fill="#0A0A0A" style="font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif;font-weight:900;font-size:85px;letter-spacing:-1.5px;">NexDirectory</text><circle cy="120" cx="90" r="14" fill="#C3FF29"></circle></svg>';

    let overlay = null;
    let stage = null;
    let minVisibleMs = 400;
    let fadeMs = 420;
    let showOnInitial = true;
    let showOnSpa = true;
    let showOnlyHome = false;
    let showOnce = false;
    let homePath = '/';
    let isHomePageRequest = false;
    let seenStorageKey = 'eep_preloader_seen';
    let disableOnMobile = false;
    let respectReducedMotion = true;
    let enabled = true;
    let seenInMemory = false;
    let visibleSince = 0;
    let hideTimer = 0;

    const clearHideTimer = function () {
      if (hideTimer) {
        window.clearTimeout(hideTimer);
        hideTimer = 0;
      }
    };

    const shouldDisable = function () {
      if (!enabled) {
        return true;
      }
      if (disableOnMobile && window.matchMedia && window.matchMedia('(max-width: 767px)').matches) {
        return true;
      }
      if (respectReducedMotion && window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
        return true;
      }
      return false;
    };

    const normalizePath = function (pathValue) {
      let path = String(pathValue || '').trim();
      if (!path) {
        return '/';
      }
      if (path.charAt(0) !== '/') {
        path = '/' + path;
      }
      path = path.replace(/\/{2,}/g, '/');
      if (path.length > 1) {
        path = path.replace(/\/+$/, '');
      }
      return path || '/';
    };

    const persistedState = (window.__eepPreloaderPersist && typeof window.__eepPreloaderPersist === 'object')
      ? window.__eepPreloaderPersist
      : null;
    if (persistedState) {
      enabled = toBool(persistedState.enabled, enabled);
      minVisibleMs = clampNumber(persistedState.minVisibleMs, 0, 8000, minVisibleMs);
      fadeMs = clampNumber(persistedState.fadeMs, 80, 2000, fadeMs);
      showOnInitial = toBool(persistedState.showOnInitial, showOnInitial);
      showOnSpa = toBool(persistedState.showOnSpa, showOnSpa);
      showOnlyHome = toBool(persistedState.showOnlyHome, showOnlyHome);
      showOnce = toBool(persistedState.showOnce, showOnce);
      homePath = normalizePath(persistedState.homePath || homePath);
      isHomePageRequest = toBool(persistedState.isHomePageRequest, isHomePageRequest);
      seenStorageKey = String(persistedState.seenStorageKey || seenStorageKey).trim() || seenStorageKey;
      disableOnMobile = toBool(persistedState.disableOnMobile, disableOnMobile);
      respectReducedMotion = toBool(persistedState.respectReducedMotion, respectReducedMotion);
      seenInMemory = toBool(persistedState.seenInMemory, seenInMemory);
    }

    const resolvePathFromUrl = function (urlValue) {
      try {
        const parsed = new URL(String(urlValue || ''), window.location.origin);
        return normalizePath(parsed.pathname);
      } catch (error) {
        return normalizePath(window.location.pathname || '/');
      }
    };

    const currentPathIsHome = function () {
      const currentPath = normalizePath(window.location && window.location.pathname ? window.location.pathname : '/');
      return currentPath === homePath;
    };

    const getSafeLocalStorage = function () {
      try {
        return window.localStorage || null;
      } catch (error) {
        return null;
      }
    };

    const getSafeSessionStorage = function () {
      try {
        return window.sessionStorage || null;
      } catch (error) {
        return null;
      }
    };

    const getSeenCookieName = function () {
      return 'eep_seen_' + String(seenStorageKey || 'default').replace(/[^a-z0-9_-]/gi, '_');
    };

    const hasSeenCookie = function () {
      const cookieName = getSeenCookieName();
      const pairs = String(document.cookie || '').split(';');
      for (let i = 0; i < pairs.length; i++) {
        const pair = String(pairs[i] || '').trim();
        if (!pair) {
          continue;
        }
        const eqIndex = pair.indexOf('=');
        const key = eqIndex >= 0 ? pair.slice(0, eqIndex).trim() : pair;
        const value = eqIndex >= 0 ? pair.slice(eqIndex + 1).trim() : '';
        if (key === cookieName && value === '1') {
          return true;
        }
      }
      return false;
    };

    const setSeenCookie = function () {
      try {
        const cookieName = getSeenCookieName();
        const oneYearSeconds = 60 * 60 * 24 * 365;
        document.cookie = cookieName + '=1; path=/; max-age=' + String(oneYearSeconds) + '; SameSite=Lax';
      } catch (error) {
        // Ignore cookie write failures.
      }
    };

    const hasBeenSeen = function () {
      if (!showOnce) {
        return false;
      }
      if (seenInMemory) {
        return true;
      }
      const storage = getSafeLocalStorage();
      if (storage && seenStorageKey) {
        try {
          if (String(storage.getItem(seenStorageKey) || '') === '1') {
            return true;
          }
        } catch (error) {
          // Ignore storage read failures.
        }
      }
      const session = getSafeSessionStorage();
      if (session && seenStorageKey) {
        try {
          if (String(session.getItem(seenStorageKey) || '') === '1') {
            return true;
          }
        } catch (error) {
          // Ignore session read failures.
        }
      }
      return hasSeenCookie();
    };

    const markSeen = function () {
      if (!showOnce) {
        return;
      }
      seenInMemory = true;
      const storage = getSafeLocalStorage();
      if (!storage || !seenStorageKey) {
        const sessionFallback = getSafeSessionStorage();
        if (sessionFallback && seenStorageKey) {
          try {
            sessionFallback.setItem(seenStorageKey, '1');
          } catch (error) {
            // Ignore storage write failures.
          }
        }
        setSeenCookie();
        return;
      }
      try {
        storage.setItem(seenStorageKey, '1');
      } catch (error) {
        // Ignore storage write failures.
      }
      const session = getSafeSessionStorage();
      if (session && seenStorageKey) {
        try {
          session.setItem(seenStorageKey, '1');
        } catch (error) {
          // Ignore session write failures.
        }
      }
      setSeenCookie();
    };

    const canShowForReason = function (reason, contextUrl) {
      if (shouldDisable()) {
        return false;
      }
      if (String(reason || '') === 'initial' && !showOnInitial) {
        return false;
      }
      if (String(reason || '') === 'spa' && !showOnSpa) {
        return false;
      }
      if (showOnlyHome) {
        const contextPath = contextUrl
          ? resolvePathFromUrl(contextUrl)
          : normalizePath(window.location && window.location.pathname ? window.location.pathname : '/');
        if (contextPath !== homePath) {
          return false;
        }
      }
      if (showOnce && hasBeenSeen()) {
        return false;
      }
      return true;
    };

    const hydrateArtwork = function () {
      if (!stage) {
        return;
      }
      const source = String(getConfig('preloaderSource', 'svg') || 'svg').toLowerCase();
      const imageUrl = String(getConfig('preloaderImageUrl', '') || '').trim();
      const svgMarkup = String(getConfig('preloaderSvg', '') || '').trim();
      stage.innerHTML = '';

      if (source === 'image' && imageUrl) {
        const img = document.createElement('img');
        img.src = imageUrl;
        img.alt = 'Loading';
        img.decoding = 'async';
        stage.appendChild(img);
        return;
      }

      stage.innerHTML = svgMarkup || defaultSvg;
    };

    const syncFromConfig = function () {
      const hasConfigKey = function (key) {
        return !!(window.eepConfig && Object.prototype.hasOwnProperty.call(window.eepConfig, key));
      };

      if (hasConfigKey('preloaderEnabled')) {
        enabled = toBool(getConfig('preloaderEnabled', enabled), enabled);
      }
      if (hasConfigKey('preloaderMinDurationMs')) {
        minVisibleMs = clampNumber(getConfig('preloaderMinDurationMs', minVisibleMs), 0, 8000, minVisibleMs);
      }
      if (hasConfigKey('preloaderFadeDurationMs')) {
        fadeMs = clampNumber(getConfig('preloaderFadeDurationMs', fadeMs), 80, 2000, fadeMs);
      }
      if (hasConfigKey('preloaderShowOnInitial')) {
        showOnInitial = toBool(getConfig('preloaderShowOnInitial', showOnInitial), showOnInitial);
      }
      if (hasConfigKey('preloaderShowOnSpa')) {
        showOnSpa = toBool(getConfig('preloaderShowOnSpa', showOnSpa), showOnSpa);
      }
      if (hasConfigKey('preloaderShowOnlyHome')) {
        showOnlyHome = toBool(getConfig('preloaderShowOnlyHome', showOnlyHome), showOnlyHome);
      }
      if (hasConfigKey('preloaderShowOnce')) {
        showOnce = toBool(getConfig('preloaderShowOnce', showOnce), showOnce);
      }
      if (hasConfigKey('preloaderDisableOnMobile')) {
        disableOnMobile = toBool(getConfig('preloaderDisableOnMobile', disableOnMobile), disableOnMobile);
      }
      if (hasConfigKey('preloaderRespectReducedMotion')) {
        respectReducedMotion = toBool(getConfig('preloaderRespectReducedMotion', respectReducedMotion), respectReducedMotion);
      }
      if (hasConfigKey('preloaderIsHomePage')) {
        isHomePageRequest = toBool(getConfig('preloaderIsHomePage', isHomePageRequest), isHomePageRequest);
      }
      if (hasConfigKey('preloaderHomeUrl')) {
        const configuredHomeUrl = String(getConfig('preloaderHomeUrl', '/') || '/').trim() || '/';
        homePath = resolvePathFromUrl(configuredHomeUrl);
      }
      if (hasConfigKey('preloaderSeenStorageKey')) {
        seenStorageKey = String(getConfig('preloaderSeenStorageKey', seenStorageKey) || seenStorageKey).trim() || seenStorageKey;
      }

      const bgColor = String(getConfig('preloaderBackgroundColor', '#FFFFFF') || '#FFFFFF').trim() || '#FFFFFF';
      const artWidth = clampNumber(getConfig('preloaderLogoMaxWidth', 420), 120, 900, 420);
      ensureStyleNode();
      if (overlay) {
        overlay.style.setProperty('--eep-preloader-bg', bgColor);
        overlay.style.setProperty('--eep-preloader-fade', String(fadeMs) + 'ms');
        overlay.style.setProperty('--eep-preloader-art-width', String(artWidth) + 'px');
        overlay.style.setProperty('--eep-preloader-art-width-mobile', String(Math.max(160, Math.round(artWidth * 0.72))) + 'px');
      }
      hydrateArtwork();

      window.__eepPreloaderPersist = {
        enabled: enabled,
        minVisibleMs: minVisibleMs,
        fadeMs: fadeMs,
        showOnInitial: showOnInitial,
        showOnSpa: showOnSpa,
        showOnlyHome: showOnlyHome,
        showOnce: showOnce,
        homePath: homePath,
        isHomePageRequest: isHomePageRequest,
        seenStorageKey: seenStorageKey,
        disableOnMobile: disableOnMobile,
        respectReducedMotion: respectReducedMotion,
        seenInMemory: seenInMemory
      };
    };

    const ensureOverlay = function () {
      if (overlay && document.body && document.body.contains(overlay)) {
        return overlay;
      }
      overlay = document.getElementById('eep-page-preloader');
      if (!overlay) {
        overlay = document.createElement('div');
        overlay.id = 'eep-page-preloader';
        overlay.setAttribute('aria-hidden', 'true');
        overlay.innerHTML = '<div class="eep-page-preloader-stage"></div>';
      }
      stage = overlay.querySelector('.eep-page-preloader-stage');
      if (!stage) {
        stage = document.createElement('div');
        stage.className = 'eep-page-preloader-stage';
        overlay.appendChild(stage);
      }
      if (document.body && !document.body.contains(overlay)) {
        document.body.appendChild(overlay);
      }
      syncFromConfig();
      return overlay;
    };

    const show = function (reason, contextUrl) {
      syncFromConfig();
      if (!canShowForReason(reason, contextUrl)) {
        return;
      }
      const node = ensureOverlay();
      if (!node) {
        return;
      }
      clearHideTimer();
      visibleSince = Date.now();
      markSeen();
      node.dataset.reason = String(reason || 'manual').trim();
      node.classList.add('is-visible');
      document.documentElement.classList.add('eep-preloader-active');
      if (document.body) {
        document.body.classList.add('eep-preloader-active');
      }
    };

    const hide = function () {
      if (!overlay || !overlay.classList.contains('is-visible')) {
        return;
      }
      clearHideTimer();
      const elapsed = visibleSince > 0 ? (Date.now() - visibleSince) : 0;
      const delay = Math.max(0, minVisibleMs - elapsed);
      hideTimer = window.setTimeout(function () {
        overlay.classList.remove('is-visible');
        document.documentElement.classList.remove('eep-preloader-active');
        if (document.body) {
          document.body.classList.remove('eep-preloader-active');
        }
        hideTimer = 0;
      }, delay);
    };

    pagePreloaderController = {
      show: show,
      hide: hide,
      syncFromConfig: syncFromConfig,
      setVisible: function (active, reason, contextUrl) {
        if (active) {
          show(reason, contextUrl);
        } else {
          hide();
        }
      }
    };
    window.__eepPagePreloader = pagePreloaderController;

    syncFromConfig();
    if (canShowForReason('initial')) {
      show('initial');
      const dismissInitial = function () {
        hide();
      };
      if (document.readyState === 'complete') {
        window.setTimeout(dismissInitial, 0);
      } else {
        window.addEventListener('load', dismissInitial, { once: true });
      }
      window.setTimeout(dismissInitial, Math.max(minVisibleMs + fadeMs + 300, 2400));
    }
  }

  function buildMapTileLayerConfig() {
    const providerRaw = String(getConfig('mapProvider', 'openstreetmap') || 'openstreetmap').toLowerCase();
    const styleRaw = String(getConfig('mapStyle', 'osm-standard') || 'osm-standard').toLowerCase();
    const provider = ['openstreetmap', 'mapbox', 'google'].indexOf(providerRaw) >= 0 ? providerRaw : 'openstreetmap';
    const style = styleRaw || 'osm-standard';
    const googleKey = String(getConfig('googleMapsApiKey', '') || '').trim();
    const mapboxToken = String(getConfig('mapboxAccessToken', '') || '').trim();
    const mapboxStyleId = String(getConfig('mapboxStyleId', 'mapbox/streets-v12') || 'mapbox/streets-v12').trim().replace(/^\/+|\/+$/g, '');
    const customUrl = String(getConfig('mapCustomTileUrl', '') || '').trim();
    const customAttribution = String(getConfig('mapCustomTileAttribution', '') || '').trim() || '&copy; OpenStreetMap contributors';
    const subdomainsRaw = String(getConfig('mapCustomTileSubdomains', 'a,b,c') || 'a,b,c').trim();
    const customSubdomains = subdomainsRaw ? subdomainsRaw.split(',').map(function (item) { return String(item || '').trim(); }).filter(Boolean) : ['a', 'b', 'c'];
    const osmFallback = {
      url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      options: {
        attribution: '&copy; OpenStreetMap contributors',
        className: 'eep-map-tiles'
      }
    };

    if (style === 'custom' && customUrl) {
      return {
        url: customUrl,
        options: {
          attribution: customAttribution,
          subdomains: customSubdomains,
          className: 'eep-map-tiles'
        }
      };
    }

    if (provider === 'mapbox' || style.indexOf('mapbox-') === 0) {
      if (mapboxToken !== '') {
        const styleByPreset = {
          'mapbox-streets': 'mapbox/streets-v12',
          'mapbox-outdoors': 'mapbox/outdoors-v12',
          'mapbox-satellite': 'mapbox/satellite-streets-v12'
        };
        const stylePath = styleByPreset[style] || mapboxStyleId || 'mapbox/streets-v12';
        return {
          url: 'https://api.mapbox.com/styles/v1/' + stylePath + '/tiles/{z}/{x}/{y}?access_token=' + encodeURIComponent(mapboxToken),
          options: {
            attribution: '&copy; <a href="https://www.mapbox.com/about/maps/">Mapbox</a> &copy; OpenStreetMap contributors',
            tileSize: 512,
            zoomOffset: -1,
            className: 'eep-map-tiles'
          }
        };
      }
      return osmFallback;
    }

    if (provider === 'google' || style.indexOf('google-') === 0) {
      const layerCode = style === 'google-satellite' ? 's' : 'm';
      const keyPart = googleKey ? '&key=' + encodeURIComponent(googleKey) : '';
      return {
        url: 'https://{s}.google.com/vt/lyrs=' + layerCode + '&x={x}&y={y}&z={z}' + keyPart,
        options: {
          attribution: '&copy; Google',
          subdomains: ['mt0', 'mt1', 'mt2', 'mt3'],
          maxZoom: 20,
          className: 'eep-map-tiles'
        }
      };
    }

    const skinMap = {
      'osm-standard': {
        url: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        attribution: '&copy; OpenStreetMap contributors'
      },
      'osm-hot': {
        url: 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
        attribution: '&copy; OpenStreetMap contributors, HOT'
      },
      'carto-light': {
        url: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png',
        attribution: '&copy; OpenStreetMap contributors, &copy; CARTO'
      },
      'carto-dark': {
        url: 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png',
        attribution: '&copy; OpenStreetMap contributors, &copy; CARTO'
      },
      'topo': {
        url: 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
        attribution: '&copy; OpenStreetMap contributors, SRTM'
      }
    };
    const skin = skinMap[style] || skinMap['osm-standard'];
    return {
      url: skin.url,
      options: {
        attribution: skin.attribution,
        className: 'eep-map-tiles'
      }
    };
  }

  function getMapFocusPreset() {
    const focusRaw = String(getConfig('mapFocusRegion', 'global') || 'global').trim().toLowerCase();
    const focusPresets = {
      'uk': {
        center: [54.3, -3.2],
        zoom: 5,
        bounds: [[49.8, -11.5], [61.3, 2.8]]
      },
      'europe': {
        center: [53.5, 14.5],
        zoom: 4,
        bounds: [[34.0, -25.0], [72.5, 45.0]]
      },
      'north-america': {
        center: [46.0, -98.0],
        zoom: 3,
        bounds: [[7.0, -168.0], [83.0, -52.0]]
      },
      'america': {
        center: [46.0, -98.0],
        zoom: 3,
        bounds: [[7.0, -168.0], [83.0, -52.0]]
      },
      'americas': {
        center: [16.0, -90.0],
        zoom: 2,
        bounds: [[-56.0, -170.0], [83.0, -30.0]]
      },
      'south-america': {
        center: [-15.0, -60.0],
        zoom: 3,
        bounds: [[-57.0, -82.0], [14.0, -34.0]]
      },
      'africa': {
        center: [3.0, 20.0],
        zoom: 3,
        bounds: [[-36.0, -19.0], [38.0, 53.0]]
      },
      'middle-east': {
        center: [28.0, 45.0],
        zoom: 4,
        bounds: [[12.0, 25.0], [42.0, 64.0]]
      },
      'asia': {
        center: [35.0, 95.0],
        zoom: 2,
        bounds: [[-10.0, 25.0], [81.0, 180.0]]
      },
      'oceania': {
        center: [-24.0, 145.0],
        zoom: 3,
        bounds: [[-50.0, 108.0], [8.0, 180.0]]
      }
    };

    if (!Object.prototype.hasOwnProperty.call(focusPresets, focusRaw)) {
      return null;
    }
    return Object.assign({ key: focusRaw }, focusPresets[focusRaw]);
  }

  function escapeHtml(value) {
    return String(value || '').replace(/[&<>'\"]/g, function (char) {
      if (char === '&') return '&amp;';
      if (char === '<') return '&lt;';
      if (char === '>') return '&gt;';
      if (char === '"') return '&quot;';
      return '&#39;';
    });
  }

  function normalizePathname(input) {
    const raw = String(input || '').trim();
    let pathname = '/';
    if (raw) {
      try {
        pathname = String(new URL(raw, window.location.origin).pathname || '/');
      } catch (err) {
        pathname = raw;
      }
    }
    if (pathname.charAt(0) !== '/') {
      pathname = '/' + pathname;
    }
    pathname = pathname.replace(/\/{2,}/g, '/');
    if (pathname.length > 1) {
      pathname = pathname.replace(/\/+$/g, '');
    }
    return pathname || '/';
  }

  function syncEditors() {
    if (window.tinymce && typeof window.tinymce.triggerSave === 'function') {
      window.tinymce.triggerSave();
    }
  }

  function showToast(message, type, options) {
    const toastType = type || 'info';
    const text = String(message || '').trim();
    if (!text) {
      return;
    }
    const ensureToastStyles = function () {
      if (document.getElementById('eep-toast-inline-style')) {
        return;
      }
      const style = document.createElement('style');
      style.id = 'eep-toast-inline-style';
      style.textContent = [
        '#eep-toast-root{position:fixed;right:max(14px,env(safe-area-inset-right));bottom:max(14px,env(safe-area-inset-bottom));z-index:2147483647!important;display:grid;gap:10px;justify-items:end;pointer-events:none;}',
        '#eep-toast-root .eep-toast{opacity:0;transform:translateY(12px);transition:opacity .28s ease,transform .28s ease;pointer-events:auto;width:min(420px,calc(100vw - max(28px,env(safe-area-inset-left) + env(safe-area-inset-right) + 28px)));margin:0;border-radius:14px;padding:12px 13px;border:1px solid rgba(255,255,255,.14);background:linear-gradient(145deg,#0f172a,#1f2937);color:#fff;box-shadow:0 16px 34px rgba(15,23,42,.34);font-family:var(--eep-font,Inter,system-ui,-apple-system,sans-serif);line-height:1.4;}',
        '#eep-toast-root .eep-toast:not(.eep-toast-hide){opacity:1;transform:translateY(0);}',
        '#eep-toast-root .eep-toast.eep-toast-hide{opacity:0;transform:translateY(10px);}',
        '#eep-toast-root .eep-toast.eep-toast-success{background:linear-gradient(145deg,#14532d,#166534);}',
        '#eep-toast-root .eep-toast.eep-toast-error{background:linear-gradient(145deg,#7f1d1d,#991b1b);}',
        '#eep-toast-root .eep-toast.eep-toast-warning{background:linear-gradient(145deg,#92400e,#b45309);}',
        '#eep-toast-root .eep-toast.eep-toast-info{background:linear-gradient(145deg,#1e3a8a,#1d4ed8);}',
        '#eep-toast-root .eep-toast-head{display:flex;align-items:center;justify-content:space-between;gap:10px;margin-bottom:3px;}',
        '#eep-toast-root .eep-toast-title{font-family:var(--eep-font-display,Inter,sans-serif);font-size:.8rem;font-weight:700;letter-spacing:.08em;text-transform:uppercase;}',
        '#eep-toast-root .eep-toast-subtitle{font-size:.74rem;font-weight:500;letter-spacing:.03em;opacity:.87;margin-bottom:6px;}',
        '#eep-toast-root .eep-toast-message{font-size:.94rem;font-weight:500;line-height:1.45;color:#f8fafc;}',
        '#eep-toast-root .eep-toast-message a{color:#fff;text-decoration:underline;text-underline-offset:2px;font-weight:700;}',
        '#eep-toast-root .eep-toast-message a:hover{opacity:.9;}',
        '#eep-toast-root .eep-toast-close{border:1px solid rgba(255,255,255,.25);border-radius:999px;width:24px;height:24px;padding:0;background:rgba(255,255,255,.12);color:#fff;font-size:18px;line-height:1;cursor:pointer;}',
        '#eep-toast-root .eep-toast-close:hover{background:rgba(255,255,255,.2);}',
        '#eep-toast-root .eep-toast.eep-toast-imported-wide{position:fixed!important;left:max(5vw,14px)!important;right:max(5vw,14px)!important;bottom:max(14px,env(safe-area-inset-bottom))!important;width:auto!important;max-width:none!important;margin:0 auto!important;}',
        '.eep-notice.eep-toasted{display:none!important;}',
        '@media (max-width:640px){#eep-toast-root{left:max(12px,env(safe-area-inset-left));right:max(12px,env(safe-area-inset-right));bottom:max(12px,env(safe-area-inset-bottom));justify-items:stretch;}#eep-toast-root .eep-toast{width:100%;max-width:100%;padding:12px;border-radius:13px;}#eep-toast-root .eep-toast-title{font-size:.78rem;}#eep-toast-root .eep-toast-message{font-size:.92rem;line-height:1.42;}}'
      ].join('');
      document.head.appendChild(style);
    };
    ensureToastStyles();
    const config = options && typeof options === 'object' ? options : {};
    const defaultTitle = (function () {
      if (toastType === 'success') return 'Success';
      if (toastType === 'error') return 'Action Needed';
      if (toastType === 'warning') return 'Heads Up';
      return 'Notice';
    })();
    const defaultSubtitle = (function () {
      if (toastType === 'success') return 'Your request has been completed.';
      if (toastType === 'error') return 'Please review the details below.';
      if (toastType === 'warning') return 'Please check this update.';
      return 'Platform update';
    })();
    const titleText = String(config.title || defaultTitle).trim();
    const subtitleText = String(config.subtitle || defaultSubtitle).trim();
    const duration = Math.max(3500, parseInt(config.duration, 10) || (toastType === 'error' ? 9800 : 7600));
    const allowHtml = config.allowHtml === true;
    const extraClassName = String(config.className || '').trim();
    const onClose = typeof config.onClose === 'function' ? config.onClose : null;

    let root = document.getElementById('eep-toast-root');
    if (!root) {
      root = document.createElement('div');
      root.id = 'eep-toast-root';
      document.body.appendChild(root);
    }

    const toast = document.createElement('div');
    toast.className = 'eep-toast eep-toast-' + toastType;
    if (extraClassName) {
      extraClassName.split(/\s+/).forEach(function (className) {
        if (/^[a-z0-9_-]+$/i.test(className)) {
          toast.classList.add(className);
        }
      });
    }
    toast.setAttribute('role', 'status');
    toast.setAttribute('aria-live', toastType === 'error' ? 'assertive' : 'polite');

    const head = document.createElement('div');
    head.className = 'eep-toast-head';
    if (titleText) {
      const titleNode = document.createElement('strong');
      titleNode.className = 'eep-toast-title';
      titleNode.textContent = titleText;
      head.appendChild(titleNode);
    }
    const closeBtn = document.createElement('button');
    closeBtn.type = 'button';
    closeBtn.className = 'eep-toast-close';
    closeBtn.setAttribute('aria-label', 'Dismiss notification');
    closeBtn.textContent = '×';
    head.appendChild(closeBtn);
    toast.appendChild(head);

    if (subtitleText) {
      const subtitleNode = document.createElement('div');
      subtitleNode.className = 'eep-toast-subtitle';
      subtitleNode.textContent = subtitleText;
      toast.appendChild(subtitleNode);
    }

    const messageNode = document.createElement('div');
    messageNode.className = 'eep-toast-message';
    if (allowHtml) {
      messageNode.innerHTML = text;
    } else {
      messageNode.textContent = text;
    }
    toast.appendChild(messageNode);

    root.appendChild(toast);

    let isDismissed = false;
    const dismiss = function () {
      if (isDismissed) {
        return;
      }
      isDismissed = true;
      toast.classList.add('eep-toast-hide');
      if (onClose) {
        try {
          onClose();
        } catch (err) {
          // Ignore toast callback failures.
        }
      }
      window.setTimeout(function () {
        if (toast.parentNode) {
          toast.parentNode.removeChild(toast);
        }
      }, 300);
    };
    closeBtn.addEventListener('click', dismiss);

    window.setTimeout(dismiss, duration);
  }

  window.eepShowToast = showToast;

  let spaRouterBound = false;
  let spaRouteSet = null;
  let spaSelector = '[data-eep-spa-main="1"]';
  let spaActiveController = null;
  let spaRequestSeq = 0;
  let stickyNavSmartState = null;
  let spaFeedbackBootstrapped = false;
  const spaFeedbackStorageKey = 'eepSpaFeedbackStatus';
  let lastSpaFeedbackSignature = '';
  let lastSpaFeedbackAt = 0;

  function isSpaFeedbackBadgeEnabled() {
    return toBool(getConfig('spaFeedbackBadge', false), false);
  }

  function getSpaFeedbackNode() {
    const existing = document.getElementById('eep-spa-feedback');
    if (existing && existing.parentNode) {
      existing.parentNode.removeChild(existing);
    }
    return null;
  }

  function setSpaFeedbackStatus(message, tone, detail, title) {
    if (!isSpaFeedbackBadgeEnabled()) {
      return;
    }
    getSpaFeedbackNode();

    const toneRaw = String(tone || '').toLowerCase();
    const safeTone = ['ok', 'warn', 'error'].indexOf(toneRaw) >= 0 ? toneRaw : 'warn';

    const titleText = String(title || 'SPA Monitor').trim() || 'SPA Monitor';
    const messageText = String(message || '').trim() || 'Status update';
    const detailText = String(detail || '').trim();
    const signature = safeTone + '|' + titleText + '|' + messageText + '|' + detailText;
    const now = Date.now();
    if (signature === lastSpaFeedbackSignature && (now - lastSpaFeedbackAt) < 1800) {
      return;
    }
    lastSpaFeedbackSignature = signature;
    lastSpaFeedbackAt = now;

    const toastTypeMap = { ok: 'success', warn: 'warning', error: 'error' };
    showToast(messageText, {
      type: toastTypeMap[safeTone] || 'info',
      title: titleText,
      subtitle: detailText,
      duration: safeTone === 'error' ? 5200 : 4200
    });
  }

  function getSessionStorageSafe() {
    try {
      return window.sessionStorage || null;
    } catch (err) {
      return null;
    }
  }

  function queueSpaFeedbackForNextLoad(payload) {
    const storage = getSessionStorageSafe();
    if (!isSpaFeedbackBadgeEnabled() || !storage) {
      return;
    }
    const data = payload && typeof payload === 'object' ? payload : {};
    const toneRaw = String(data.tone || '').toLowerCase();
    const safeTone = ['ok', 'warn', 'error'].indexOf(toneRaw) >= 0 ? toneRaw : 'warn';
    const record = {
      message: String(data.message || 'Full page load').trim() || 'Full page load',
      tone: safeTone,
      detail: String(data.detail || '').trim(),
      title: String(data.title || 'SPA Monitor').trim() || 'SPA Monitor',
      target: String(data.target || '').trim(),
      queuedAt: Date.now()
    };
    try {
      storage.setItem(spaFeedbackStorageKey, JSON.stringify(record));
    } catch (err) {
      // ignore storage failures
    }
  }

  function consumeQueuedSpaFeedback() {
    const storage = getSessionStorageSafe();
    if (!storage) {
      return null;
    }
    let raw = '';
    try {
      raw = String(storage.getItem(spaFeedbackStorageKey) || '');
      storage.removeItem(spaFeedbackStorageKey);
    } catch (err) {
      return null;
    }
    if (!raw) {
      return null;
    }
    try {
      const parsed = JSON.parse(raw);
      if (!parsed || typeof parsed !== 'object') {
        return null;
      }
      return parsed;
    } catch (err) {
      return null;
    }
  }

  function initSpaFeedbackBadge() {
    const enabled = isSpaFeedbackBadgeEnabled();
    const existing = document.getElementById('eep-spa-feedback');
    if (!enabled) {
      if (existing && existing.parentNode) {
        existing.parentNode.removeChild(existing);
      }
      spaFeedbackBootstrapped = false;
      try {
        const storage = getSessionStorageSafe();
        if (storage) {
          storage.removeItem(spaFeedbackStorageKey);
        }
      } catch (err) {
        // ignore storage failures
      }
      return;
    }

    getSpaFeedbackNode();
    if (spaFeedbackBootstrapped) {
      return;
    }
    spaFeedbackBootstrapped = true;

    const queued = consumeQueuedSpaFeedback();
    if (queued) {
      setSpaFeedbackStatus(queued.message || 'Full page load', queued.tone || 'warn', queued.detail || '', queued.title || 'SPA Monitor');
      return;
    }

    if (!toBool(getConfig('spaEnabled', false), false)) {
      setSpaFeedbackStatus('SPA is disabled', 'warn', 'Using full page navigation mode');
      return;
    }

    setSpaFeedbackStatus('SPA monitor ready', 'ok', 'Waiting for route navigation events');
  }

  function copyTextToClipboard(text) {
    const value = String(text || '').trim();
    if (!value) {
      return Promise.reject(new Error('empty_value'));
    }

    if (navigator.clipboard && typeof navigator.clipboard.writeText === 'function') {
      return navigator.clipboard.writeText(value);
    }

    return new Promise(function (resolve, reject) {
      const field = document.createElement('textarea');
      field.value = value;
      field.setAttribute('readonly', 'readonly');
      field.style.position = 'fixed';
      field.style.top = '-9999px';
      field.style.left = '-9999px';
      document.body.appendChild(field);
      field.focus();
      field.select();

      let copied = false;
      try {
        copied = document.execCommand('copy');
      } catch (err) {
        copied = false;
      }

      document.body.removeChild(field);
      if (copied) {
        resolve();
        return;
      }
      reject(new Error('copy_failed'));
    });
  }

  function initCopyLinkButtons() {
    const copiedMessage = String(getMessage('copyLinkSuccess', 'Link copied to clipboard.')).trim() || 'Link copied to clipboard.';
    const failedMessage = String(getMessage('copyLinkFailed', 'Could not copy the link. Please copy it manually.')).trim() || 'Could not copy the link. Please copy it manually.';
    const copiedTitle = String(getMessage('copyLinkTitle', 'Copied')).trim() || 'Copied';

    $(document)
      .off('click.eepCopyLink', '[data-eep-copy-link]')
      .on('click.eepCopyLink', '[data-eep-copy-link]', function (event) {
        event.preventDefault();
        const $button = $(this);
        const link = String($button.attr('data-eep-copy-link') || '').trim();
        if (!link) {
          return;
        }
        copyTextToClipboard(link)
          .then(function () {
            showToast(copiedMessage, 'success', {
              title: copiedTitle,
              subtitle: 'Share link ready'
            });
          })
          .catch(function () {
            showToast(failedMessage, 'error', {
              title: 'Action Needed',
              subtitle: 'Copy link failed'
            });
          });
      });
  }

  function restPost(endpoint, payload) {
    const url = String(endpoint || '').trim();
    if (!url) {
      return Promise.reject(new Error(getMessage('restActionFailed', 'Request failed. Please try again.')));
    }
    return fetch(url, {
      method: 'POST',
      credentials: 'same-origin',
      headers: {
        'Content-Type': 'application/json',
        'X-WP-Nonce': String(getConfig('restNonce', '') || '')
      },
      body: JSON.stringify(payload || {})
    })
      .then(async function (res) {
        let data = {};
        try {
          data = await res.json();
        } catch (err) {
          data = {};
        }
        if (!res.ok) {
          throw new Error(String((data && data.message) || getMessage('restActionFailed', 'Request failed. Please try again.')));
        }
        return data || {};
      });
  }

  function initEventAnalyticsTracking() {
    const endpoint = String(getConfig('analyticsTrackUrl', '') || '').trim();
    if (!endpoint) {
      return;
    }
    const sentKeys = new Set();

    $(document)
      .off('click.eepAnalyticsTrack', '[data-eep-event-id] a')
      .on('click.eepAnalyticsTrack', '[data-eep-event-id] a', function () {
        const $anchor = $(this);
        const $eventRoot = $anchor.closest('[data-eep-event-id]');
        if (!$eventRoot.length) {
          return;
        }

        const href = String($anchor.attr('href') || '').trim();
        let metric = String($anchor.attr('data-eep-analytics-metric') || '').trim();
        if (!metric && /^tel:/i.test(href)) {
          metric = 'click_phone';
        }
        if (!metric && /(wa\.me|api\.whatsapp\.com|whatsapp)/i.test(href)) {
          metric = 'click_whatsapp';
        }
        if (!metric) {
          return;
        }

        const eventId = parseInt(
          String($anchor.attr('data-eep-track-event-id') || $eventRoot.attr('data-eep-event-id') || '0'),
          10
        );
        if (!Number.isFinite(eventId) || eventId <= 0) {
          return;
        }
        const source = String($anchor.attr('data-eep-track-source') || 'single_link').trim();
        const dedupeKey = [metric, eventId, source, href].join('|');
        if (sentKeys.has(dedupeKey)) {
          return;
        }
        sentKeys.add(dedupeKey);

        fetch(endpoint, {
          method: 'POST',
          credentials: 'same-origin',
          keepalive: true,
          headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': String(getConfig('restNonce', '') || '')
          },
          body: JSON.stringify({
            event_id: eventId,
            metric: metric,
            source: source
          })
        }).catch(function () {
          // Silent failure to avoid interrupting navigation.
        });
      });
  }

  function uploadAssetFile(file, kind, target) {
    return new Promise(function (resolve, reject) {
      if (!file) {
        reject(new Error('missing_file'));
        return;
      }
      const endpoint = String(getConfig('uploadEndpoint', '/wp-admin/admin-post.php') || '');
      const nonce = String(getConfig('uploadNonce', '') || '').trim();
      if (!endpoint || !nonce) {
        reject(new Error('uploader_unavailable'));
        return;
      }

      const fd = new FormData();
      fd.append('action', 'eep_upload_image');
      fd.append('eep_upload_nonce', nonce);
      fd.append('eep_upload_kind', String(kind || 'image'));
      fd.append('eep_upload_target', String(target || 'gallery'));
      fd.append('file', file, file.name || 'upload');

      fetch(endpoint, {
        method: 'POST',
        body: fd,
        credentials: 'same-origin'
      })
        .then(async function (res) {
          let payload = null;
          try {
            payload = await res.json();
          } catch (err) {
            payload = null;
          }
          if (!res.ok || !payload || payload.success !== true || !payload.data || !payload.data.url) {
            throw new Error((payload && payload.data && payload.data.message) ? payload.data.message : 'upload_failed');
          }
          resolve(payload.data);
        })
        .catch(function (error) {
          reject(error || new Error('upload_failed'));
        });
    });
  }

  window.eepSubmitForm = function (initialState) {
    const initial = initialState && typeof initialState === 'object' ? initialState : {};
    const initialGallery = Array.isArray(initial.galleryImages) ? initial.galleryImages.map((row) => String(row || '').trim()).filter((row) => row !== '') : [];
    const initialVideos = Array.isArray(initial.videoEmbeds) ? initial.videoEmbeds.map((row) => String(row || '').trim()).filter((row) => row !== '') : [];
    const normalizeSession = (session) => ({
      title: String((session && session.title) || ''),
      start: String((session && session.start) || ''),
      end: String((session && session.end) || ''),
      location: String((session && session.location) || ''),
      speaker: String((session && session.speaker) || ''),
      day: String((session && session.day) || ''),
      notes: String((session && session.notes) || ''),
      attachments: Array.isArray(session && session.attachments)
        ? session.attachments.map((item) => String(item || '').trim()).filter((item) => item !== '').join('\n')
        : String((session && session.attachments) || ''),
      session_key: String((session && (session.session_key || session.id)) || '')
    });
    const initialSessions = Array.isArray(initial.sessions) ? initial.sessions.map(normalizeSession) : [];
    const initialKeySpeakers = Array.isArray(initial.keySpeakers) ? initial.keySpeakers.map((speaker) => ({
      name: String((speaker && speaker.name) || ''),
      profession: String((speaker && (speaker.profession || speaker.professional)) || ''),
      subject: String((speaker && speaker.subject) || ''),
      image: String((speaker && (speaker.image || speaker.image_url)) || ''),
      details: String((speaker && speaker.details) || '')
    })) : [];
    return {
      coverImage: String(initial.coverImage || ''),
      galleryImages: initialGallery,
      galleryTextarea: String(initialGallery.join('\n') || ''),
      videoUrlsTextarea: String(initial.videoUrlsTextarea || ''),
      videoEmbeds: initialVideos.length ? initialVideos : [''],
      sessions: initialSessions,
      keySpeakers: initialKeySpeakers,
      isRecurring: String(initial.isRecurring || '0') === '1' ? '1' : '0',
      recurrenceUntil: String(initial.recurrenceUntil || ''),
      streamUrl: String(initial.streamUrl || ''),
      modeOption: String(initial.modeOption || 'in-person'),
      youtubeIdOne: '',
      youtubeIdTwo: '',
      coverMaxBytes: parseInt(getConfig('uploadCoverMaxBytes', 512000), 10) || 512000,
      galleryMaxBytes: parseInt(getConfig('uploadGalleryMaxBytes', 2048000), 10) || 2048000,
      galleryMaxFiles: parseInt(getConfig('uploadGalleryMaxFiles', 10), 10) || 10,
      fileMaxBytes: parseInt(getConfig('uploadFileMaxBytes', 2097152), 10) || 2097152,
      titleValue: String(initial.titleValue || ''),
      excerptValue: String(initial.excerptValue || ''),
      metaTitleValue: String(initial.metaTitleValue || ''),
      metaDescriptionValue: String(initial.metaDescriptionValue || ''),
      serpUrl: String(initial.serpUrl || ''),
      excerptError: '',
      excerptMaxChars: parseInt(getConfig('excerptMaxChars', 220), 10) || 220,
      startValue: String(initial.startValue || ''),
      startTimeValue: String(initial.startTimeValue || ''),
      endValue: String(initial.endValue || ''),
      endTimeValue: String(initial.endTimeValue || ''),
      titleError: '',
      dateError: '',
      dateWarning: '',
      timeWarning: '',
      timezoneHint: '',
      cityValue: String(initial.cityValue || ''),
      countryValue: String(initial.countryValue || ''),
      latValue: String(initial.latValue || ''),
      lngValue: String(initial.lngValue || ''),
      autosavePostId: '',
      autosaveMessage: 'Autosave ready',
      autosaveTimer: null,
      autosaveQueuedTimer: null,
      autosaveBusy: false,
      autosaveEnabled: true,
      pastStartConfirmed: false,
      formStateStorageKey: 'eep_submit_form_state_v1',
      formStateTtlMs: 12 * 60 * 60 * 1000,
      init() {
        const form = document.getElementById('eep-submit-form');
        const isEditing = !!(form && String(form.getAttribute('data-eep-editing') || '') === '1');
        let forceFreshSubmit = false;
        try {
          const initialParams = new URLSearchParams(String(window.location.search || ''));
          forceFreshSubmit = String(initialParams.get('eep_new') || '') === '1';
        } catch (e) {
          forceFreshSubmit = false;
        }
        if (forceFreshSubmit) {
          try {
            window.localStorage.removeItem('eep_submit_draft');
          } catch (e) {}
          try {
            const cleanedUrl = new URL(window.location.href);
            cleanedUrl.searchParams.delete('eep_new');
            window.history.replaceState({}, '', cleanedUrl.toString());
          } catch (e) {}
        }
        if (form) {
          const readSeededValue = (field, fallback) => {
            const fallbackValue = String(fallback || '');
            if (!field) {
              return fallbackValue;
            }
            const liveValue = String(field.value || '');
            if (liveValue.trim() !== '') {
              return liveValue;
            }
            const defaultValue = String(field.defaultValue || field.getAttribute('value') || '');
            if (defaultValue.trim() !== '') {
              return defaultValue;
            }
            return fallbackValue;
          };
          const titleInput = form.querySelector('[name="post_title"]');
          const excerptInput = form.querySelector('[name="post_excerpt"]');
          const metaTitleInput = form.querySelector('[name="eep_meta_title"]');
          const metaDescriptionInput = form.querySelector('[name="eep_meta_description"]');
          const startInput = form.querySelector('[name="eep_start_date"]') || form.querySelector('[name="eep_start"]');
          const endInput = form.querySelector('[name="eep_end_date"]') || form.querySelector('[name="eep_end"]');
          const startTimeInput = form.querySelector('[name="eep_start_time"]');
          const endTimeInput = form.querySelector('[name="eep_end_time"]');
          const cityInput = form.querySelector('[name="eep_city"]');
          const countryInput = form.querySelector('[name="eep_country"]');
          const latInput = form.querySelector('[name="eep_lat"]');
          const lngInput = form.querySelector('[name="eep_lng"]');
          const streamInput = form.querySelector('[name="eep_event_stream_url"]');
          const videoTextarea = form.querySelector('[name="eep_video_urls"]');
          const coverInput = form.querySelector('[name="eep_cover_image_url"]');
          const galleryTextarea = form.querySelector('[name="eep_gallery_urls"]');
          const recurringChecked = form.querySelector('input[name="eep_is_recurring"]:checked');
          const modeChecked = form.querySelector('input[name="eep_event_mode_option"]:checked');
          this.titleValue = readSeededValue(titleInput, this.titleValue);
          this.excerptValue = readSeededValue(excerptInput, this.excerptValue);
          this.metaTitleValue = readSeededValue(metaTitleInput, this.metaTitleValue);
          this.metaDescriptionValue = readSeededValue(metaDescriptionInput, this.metaDescriptionValue);
          this.startValue = readSeededValue(startInput, this.startValue);
          this.endValue = readSeededValue(endInput, this.endValue);
          this.startTimeValue = readSeededValue(startTimeInput, this.startTimeValue);
          this.endTimeValue = readSeededValue(endTimeInput, this.endTimeValue);
          this.cityValue = readSeededValue(cityInput, this.cityValue);
          this.countryValue = readSeededValue(countryInput, this.countryValue);
          this.latValue = readSeededValue(latInput, this.latValue);
          this.lngValue = readSeededValue(lngInput, this.lngValue);
          this.streamUrl = readSeededValue(streamInput, this.streamUrl);
          this.coverImage = readSeededValue(coverInput, this.coverImage);
          const galleryTextValue = readSeededValue(galleryTextarea, this.galleryTextarea || '');
          if (galleryTextarea && String(galleryTextValue).trim() !== '') {
            const galleryRows = String(galleryTextValue).split(/\r?\n/).map((row) => String(row || '').trim()).filter((row) => row !== '');
            if (galleryRows.length) {
              this.galleryImages = Array.from(new Set(galleryRows));
            }
          }
          if (recurringChecked) {
            this.isRecurring = String(recurringChecked.value || '0');
          }
          if (modeChecked) {
            this.modeOption = String(modeChecked.value || 'in-person');
          }
          if (form.getAttribute('data-eep-field-highlight-bound') !== '1') {
            form.setAttribute('data-eep-field-highlight-bound', '1');
            const clearFieldHighlight = (event) => {
              const target = event && event.target ? event.target : null;
              this.clearFieldErrorState(target);
            };
            form.addEventListener('input', clearFieldHighlight, true);
            form.addEventListener('change', clearFieldHighlight, true);
          }
          if (startInput) {
            startInput.addEventListener('change', () => {
              this.pastStartConfirmed = false;
              this.validateDates();
            });
            startInput.addEventListener('input', () => {
              this.pastStartConfirmed = false;
            });
          }
          if (endInput) {
            endInput.addEventListener('change', () => {
              this.validateDates();
            });
          }
          const videoTextValue = readSeededValue(videoTextarea, this.videoUrlsTextarea || '');
          if (videoTextarea && String(videoTextValue).trim() !== '') {
            const rows = String(videoTextValue).split(/\r?\n/).map((row) => String(row || '').trim()).filter((row) => row !== '');
            if (rows.length) {
              this.videoEmbeds = rows;
            }
          }
          const sessionsRaw = String(form.getAttribute('data-eep-initial-sessions') || '').trim();
          if (sessionsRaw !== '') {
            try {
              const parsedSessions = JSON.parse(sessionsRaw);
              if (Array.isArray(parsedSessions) && parsedSessions.length) {
                this.sessions = parsedSessions.map(normalizeSession);
                this.isRecurring = '1';
              }
            } catch (err) {
              // ignore malformed prefill
            }
          }
          const recurrenceSeed = String(form.getAttribute('data-eep-recurrence-until') || '').trim();
          if (recurrenceSeed) {
            this.recurrenceUntil = recurrenceSeed;
          }
          if (form.getAttribute('data-eep-past-date-check-bound') !== '1') {
            form.setAttribute('data-eep-past-date-check-bound', '1');
            form.addEventListener('submit', (event) => {
              this.persistFormState(form);
              if (this.hasBlockingErrors(form)) {
                if (event && typeof event.preventDefault === 'function') {
                  event.preventDefault();
                }
                if (event && typeof event.stopImmediatePropagation === 'function') {
                  event.stopImmediatePropagation();
                }
                showToast('Please fix the highlighted fields before submitting.', 'error');
                return false;
              }
              if (!this.validateRequiredLocation(form, event)) {
                return false;
              }
              this.validateDates();
              if (!this.confirmPastStartDate(event)) {
                return false;
              }
              return true;
            }, true);
          }
        }

        const saved = (!isEditing && !forceFreshSubmit) ? window.localStorage.getItem('eep_submit_draft') : null;
        if (saved) {
          try {
            const parsed = JSON.parse(saved);
            if (String((parsed && parsed.sourceEditId) || '0') !== '0') {
              throw new Error('stale-edit-draft');
            }
            if (parsed.coverImage) this.coverImage = parsed.coverImage;
            if (Array.isArray(parsed.galleryImages)) this.galleryImages = parsed.galleryImages;
            if (Array.isArray(parsed.sessions)) {
              this.sessions = parsed.sessions.map(normalizeSession);
            }
            if (Array.isArray(parsed.keySpeakers)) {
              this.keySpeakers = parsed.keySpeakers.map((speaker) => ({
                name: String((speaker && speaker.name) || ''),
                profession: String((speaker && (speaker.profession || speaker.professional)) || ''),
                subject: String((speaker && speaker.subject) || ''),
                image: String((speaker && (speaker.image || speaker.image_url)) || ''),
                details: String((speaker && speaker.details) || '')
              }));
            }
            if (parsed.isRecurring === '1') this.isRecurring = '1';
            if (parsed.streamUrl) this.streamUrl = String(parsed.streamUrl);
            if (parsed.excerptValue) this.excerptValue = String(parsed.excerptValue);
            if (parsed.metaTitleValue) this.metaTitleValue = String(parsed.metaTitleValue);
            if (parsed.metaDescriptionValue) this.metaDescriptionValue = String(parsed.metaDescriptionValue);
            if (parsed.cityValue) this.cityValue = String(parsed.cityValue);
            if (parsed.countryValue) this.countryValue = String(parsed.countryValue);
            if (parsed.latValue) this.latValue = String(parsed.latValue);
            if (parsed.lngValue) this.lngValue = String(parsed.lngValue);
            if (parsed.youtubeIdOne) this.youtubeIdOne = String(parsed.youtubeIdOne);
            if (parsed.youtubeIdTwo) this.youtubeIdTwo = String(parsed.youtubeIdTwo);
            if (Array.isArray(parsed.videoEmbeds) && parsed.videoEmbeds.length) this.videoEmbeds = parsed.videoEmbeds;
            if (parsed.autosavePostId) this.autosavePostId = String(parsed.autosavePostId);
            this.syncGalleryTextarea();
            this.syncVideoUrls();
            this.syncRecurrenceMeta();
          } catch (e) {}
        }
        this.restoreFormState(form);

        this.autodetectTimezone();
        this.validateTitle();
        this.validateExcerpt();
        this.validateDates();
        if (this.modeOption !== 'video-stream') {
          this.streamUrl = '';
        }
        this.syncGalleryTextarea();
        this.syncVideoUrls();
        this.syncRecurrenceMeta();

        this.autosaveEnabled = toBool(getConfig('enableSubmitAutosave', true), true);
        if (this.autosaveEnabled) {
          this.bindAutosaveTriggers(form);
          this.autosaveMessage = getMessage('autosaveEnabled', 'Autosave active. Changes save after you finish editing each field.');
        } else {
          this.autosaveMessage = 'Autosave disabled by admin settings';
        }
      },
      bindAutosaveTriggers(form) {
        if (!form || form.getAttribute('data-eep-autosave-bound') === '1') {
          return;
        }
        form.setAttribute('data-eep-autosave-bound', '1');

        const shouldTrackField = (field) => {
          if (!field || typeof field !== 'object') {
            return false;
          }
          const tag = String(field.tagName || '').toLowerCase();
          if (['input', 'textarea', 'select'].indexOf(tag) === -1) {
            return false;
          }
          const type = String(field.type || '').toLowerCase();
          if (['hidden', 'submit', 'button', 'reset', 'file'].indexOf(type) >= 0) {
            return false;
          }
          return true;
        };

        form.addEventListener('focusout', (event) => {
          if (!this.autosaveEnabled || !shouldTrackField(event.target)) {
            return;
          }
          this.queueAutosave(420);
        }, true);

        form.addEventListener('input', (event) => {
          if (!this.autosaveEnabled || !shouldTrackField(event.target)) {
            return;
          }
          this.queueAutosave(260);
        }, true);

        form.addEventListener('change', (event) => {
          if (!this.autosaveEnabled || !shouldTrackField(event.target)) {
            return;
          }
          this.queueAutosave(320);
        }, true);

        if (window.tinymce && typeof window.tinymce.get === 'function') {
          const editor = window.tinymce.get('eep_post_content_editor');
          if (editor && editor.on) {
            editor.on('input keyup change', () => {
              this.queueAutosave(260);
            });
            editor.on('blur', () => {
              this.queueAutosave(320);
            });
          }
        }
      },
      queueAutosave(delayMs) {
        if (!this.autosaveEnabled) {
          return;
        }
        const delay = Number.isFinite(delayMs) ? Math.max(80, parseInt(delayMs, 10)) : 320;
        if (this.autosaveQueuedTimer) {
          window.clearTimeout(this.autosaveQueuedTimer);
        }
        this.autosaveQueuedTimer = window.setTimeout(() => {
          this.autosaveDraft();
        }, delay);
      },
      titleCounterText() {
        const len = String(this.titleValue || '').trim().length;
        return String(len) + ' chars';
      },
      excerptCounterText() {
        const chars = String(this.excerptValue || '').length;
        return String(chars) + ' / ' + String(this.excerptMaxChars) + ' chars';
      },
      seoTitleCount() {
        return String(this.metaTitleValue || '').trim().length;
      },
      seoDescriptionCount() {
        return String(this.metaDescriptionValue || '').trim().length;
      },
      seoTitleStatusText() {
        const count = this.seoTitleCount();
        if (count === 0) {
          return 'Optional';
        }
        if (count > 60) {
          return 'Too long';
        }
        if (count < 50) {
          return 'Add more detail';
        }
        return 'Ideal length';
      },
      seoTitleStatusClass() {
        const count = this.seoTitleCount();
        if (count === 0) {
          return 'text-gray-500';
        }
        if (count > 60) {
          return 'text-red-600';
        }
        if (count < 50) {
          return 'text-amber-600';
        }
        return 'text-green-600';
      },
      seoDescriptionStatusText() {
        const count = this.seoDescriptionCount();
        if (count === 0) {
          return 'Optional';
        }
        if (count > 158) {
          return 'Too long';
        }
        if (count < 120) {
          return 'Add more detail';
        }
        return 'Ideal length';
      },
      seoDescriptionStatusClass() {
        const count = this.seoDescriptionCount();
        if (count === 0) {
          return 'text-gray-500';
        }
        if (count > 158) {
          return 'text-red-600';
        }
        if (count < 120) {
          return 'text-amber-600';
        }
        return 'text-green-600';
      },
      stripHtml(value) {
        return String(value || '').replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
      },
      truncateText(value, maxLength) {
        const text = String(value || '').trim();
        if (!text) {
          return '';
        }
        const limit = Math.max(1, parseInt(maxLength, 10) || 0);
        if (!limit || text.length <= limit) {
          return text;
        }
        return text.slice(0, Math.max(0, limit - 1)).trim() + '…';
      },
      readSerpDescriptionFallback() {
        const excerpt = String(this.excerptValue || '').trim();
        if (excerpt) {
          return excerpt;
        }
        const form = document.getElementById('eep-submit-form');
        if (!form) {
          return '';
        }
        const contentInput = form.querySelector('[name="post_content"]');
        if (contentInput && String(contentInput.value || '').trim() !== '') {
          return this.stripHtml(contentInput.value);
        }
        if (window.tinymce && typeof window.tinymce.get === 'function') {
          const editor = window.tinymce.get('eep_post_content_editor');
          if (editor && typeof editor.getContent === 'function') {
            const content = this.stripHtml(editor.getContent({ format: 'raw' }));
            if (content) {
              return content;
            }
          }
        }
        return '';
      },
      fillSeoFromContent() {
        const titleSeed = String(this.titleValue || '').trim();
        const descriptionSeed = this.readSerpDescriptionFallback();
        if (titleSeed) {
          this.metaTitleValue = this.truncateText(titleSeed, 60).replace(/…$/, '');
        }
        if (descriptionSeed) {
          this.metaDescriptionValue = this.truncateText(descriptionSeed, 158).replace(/…$/, '');
        }
        this.queueAutosave(180);
      },
      serpUrlPreview() {
        const seed = String(this.serpUrl || window.location.origin || '').trim();
        if (!seed) {
          return '';
        }
        return this.truncateText(seed.replace(/^https?:\/\//i, ''), 70);
      },
      serpTitlePreview() {
        const custom = String(this.metaTitleValue || '').trim();
        const fallback = String(this.titleValue || '').trim();
        const value = custom || fallback || 'Untitled Listing';
        return this.truncateText(value, 60);
      },
      serpDescriptionPreview() {
        const custom = String(this.metaDescriptionValue || '').trim();
        const fallback = this.readSerpDescriptionFallback();
        const value = custom || fallback || 'Add a concise summary to improve discovery in search results.';
        return this.truncateText(value, 158);
      },
      countWords(value) {
        const text = String(value || '').replace(/\s+/g, ' ').trim();
        if (!text) {
          return 0;
        }
        return text.split(' ').length;
      },
      validateExcerpt() {
        const chars = String(this.excerptValue || '').length;
        if (chars > this.excerptMaxChars) {
          this.excerptError = 'Short excerpt must be ' + String(this.excerptMaxChars) + ' characters or fewer.';
          return false;
        }
        this.excerptError = '';
        return true;
      },
      parseLocalDate(value, timeValue) {
        const dateText = String(value || '').trim();
        if (!dateText) {
          return null;
        }
        const timeText = String(timeValue || '').trim();
        const text = timeText ? (dateText + 'T' + timeText) : (dateText + 'T00:00');
        const parsed = new Date(text);
        if (!parsed || Number.isNaN(parsed.getTime())) {
          const fallback = new Date(dateText);
          if (!fallback || Number.isNaN(fallback.getTime())) {
            return null;
          }
          return fallback;
        }
        return parsed;
      },
      getMissingTimeWarning() {
        const startDate = String(this.startValue || '').trim();
        const startTime = String(this.startTimeValue || '').trim();
        const endDate = String(this.endValue || '').trim();
        const endTime = String(this.endTimeValue || '').trim();
        const messages = [];
        if (startDate && !startTime) {
          messages.push('No start time added. This will be shown as a date-only listing.');
        }
        if (endDate && !endTime) {
          messages.push('No end time added. End date will be shown without a time.');
        }
        return messages.join(' ');
      },
      validateTitle() {
        const len = String(this.titleValue || '').trim().length;
        if (len > 0 && len < 8) {
          this.titleError = 'Title should be at least 8 characters.';
          return false;
        }
        this.titleError = '';
        return true;
      },
      validateSeoFields() {
        const titleChars = this.seoTitleCount();
        const descChars = this.seoDescriptionCount();
        if (titleChars > 60 || descChars > 158) {
          return false;
        }
        return true;
      },
      validateDates() {
        const startDt = this.parseLocalDate(this.startValue, this.startTimeValue);
        const endDt = this.parseLocalDate(this.endValue, this.endTimeValue);
        this.dateWarning = '';
        this.timeWarning = this.getMissingTimeWarning();
        if (startDt && startDt.getTime() < (Date.now() - 60000)) {
          this.dateWarning = getMessage('pastDateWarning', 'Start date is in the past. You can still submit if this is intentional.');
        }
        if (startDt && endDt && endDt.getTime() < startDt.getTime()) {
          this.dateError = 'End date must be after start date.';
          return false;
        }
        if (
          startDt
          && endDt
          && endDt.getTime() === startDt.getTime()
          && String(this.startTimeValue || '').trim() !== ''
          && String(this.endTimeValue || '').trim() !== ''
        ) {
          this.dateError = 'End date must be after start date.';
          return false;
        }
        this.dateError = '';
        return true;
      },
      isStartDateInPast() {
        const startDt = this.parseLocalDate(this.startValue, this.startTimeValue);
        if (!startDt) {
          return false;
        }
        return startDt.getTime() < (Date.now() - 60000);
      },
      confirmPastStartDate(event) {
        if (!this.isStartDateInPast()) {
          this.pastStartConfirmed = false;
          return true;
        }
        if (this.pastStartConfirmed) {
          return true;
        }
        const message = getMessage('pastDateConfirm', 'The start date is in the past. Are you sure you want to continue?');
        const proceed = window.confirm(String(message || ''));
        if (!proceed) {
          if (event && typeof event.preventDefault === 'function') {
            event.preventDefault();
          }
          if (event && typeof event.stopImmediatePropagation === 'function') {
            event.stopImmediatePropagation();
          }
          const $form = $('#eep-submit-form');
          if ($form.length) {
            $form.data('eepSubmitting', false).removeClass('eep-is-submitting');
            $form.find('button[type="submit"], input[type="submit"]').prop('disabled', false).removeClass('eep-loading');
          }
          showToast(getMessage('pastDateCancelled', 'Submission cancelled. Adjust the date or submit again to confirm.'), 'error');
          return false;
        }
        this.pastStartConfirmed = true;
        return true;
      },
      isFieldVisible(field) {
        if (!field || typeof field.getClientRects !== 'function') {
          return false;
        }
        if (field.closest('[hidden], [aria-hidden="true"], .eep-is-hidden')) {
          return false;
        }
        const style = typeof window.getComputedStyle === 'function' ? window.getComputedStyle(field) : null;
        if (style && (style.display === 'none' || style.visibility === 'hidden')) {
          return false;
        }
        if (field.getClientRects().length === 0 && (!style || style.position !== 'fixed')) {
          return false;
        }
        return true;
      },
      isFieldEligibleForValidation(field) {
        if (!field || field.disabled) {
          return false;
        }
        const type = String(field.type || '').toLowerCase();
        if (['hidden', 'submit', 'button', 'reset', 'file'].indexOf(type) >= 0) {
          return false;
        }
        return this.isFieldVisible(field);
      },
      clearFieldErrorState(field) {
        if (!field || !field.classList) {
          return;
        }
        if (field.getAttribute('data-eep-invalid-flag') === '1') {
          field.classList.remove('eep-field-invalid');
          field.removeAttribute('data-eep-invalid-flag');
          field.removeAttribute('aria-invalid');
        }
        const group = field.closest('.eep-field-invalid-group[data-eep-invalid-flag-group="1"]');
        if (group) {
          group.classList.remove('eep-field-invalid-group');
          group.removeAttribute('data-eep-invalid-flag-group');
        }
      },
      clearValidationHighlights(form) {
        if (!form) {
          return;
        }
        const nodes = form.querySelectorAll('[data-eep-invalid-flag="1"]');
        nodes.forEach((node) => {
          if (!node || !node.classList) {
            return;
          }
          node.classList.remove('eep-field-invalid');
          node.removeAttribute('data-eep-invalid-flag');
          node.removeAttribute('aria-invalid');
        });
        const groups = form.querySelectorAll('.eep-field-invalid-group[data-eep-invalid-flag-group="1"]');
        groups.forEach((group) => {
          if (!group || !group.classList) {
            return;
          }
          group.classList.remove('eep-field-invalid-group');
          group.removeAttribute('data-eep-invalid-flag-group');
        });
      },
      markFieldInvalid(field) {
        if (!field || !field.classList) {
          return;
        }
        field.classList.add('eep-field-invalid');
        field.setAttribute('data-eep-invalid-flag', '1');
        field.setAttribute('aria-invalid', 'true');
        const group = field.closest('.eep-radio-group, .eep-checkbox-row, fieldset');
        if (group && group.classList) {
          group.classList.add('eep-field-invalid-group');
          group.setAttribute('data-eep-invalid-flag-group', '1');
        }
      },
      focusFirstInvalidField(fields) {
        if (!Array.isArray(fields) || !fields.length) {
          return;
        }
        const target = fields.find((field) => this.isFieldVisible(field)) || fields[0];
        if (!target) {
          return;
        }
        if (typeof target.scrollIntoView === 'function') {
          target.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }
        if (typeof target.focus === 'function') {
          try {
            target.focus({ preventScroll: true });
          } catch (error) {
            target.focus();
          }
        }
      },
      collectRequiredFieldErrors(form) {
        if (!form) {
          return [];
        }
        const invalidFields = [];
        const requiredFields = Array.from(form.querySelectorAll('[required]')).filter((field) => this.isFieldEligibleForValidation(field));
        const radioHandled = {};

        const addInvalid = (field) => {
          if (!field || invalidFields.indexOf(field) >= 0) {
            return;
          }
          invalidFields.push(field);
          this.markFieldInvalid(field);
        };

        requiredFields.forEach((field) => {
          const type = String(field.type || '').toLowerCase();
          const tag = String(field.tagName || '').toLowerCase();

          if (type === 'radio') {
            const name = String(field.name || '');
            if (!name || radioHandled[name]) {
              return;
            }
            radioHandled[name] = true;
            const radioGroup = requiredFields.filter((candidate) => String(candidate.type || '').toLowerCase() === 'radio' && String(candidate.name || '') === name);
            const hasChecked = radioGroup.some((candidate) => !!candidate.checked);
            if (!hasChecked && radioGroup.length) {
              radioGroup.forEach((candidate) => this.markFieldInvalid(candidate));
              addInvalid(radioGroup[0]);
            }
            return;
          }

          if (type === 'checkbox') {
            if (!field.checked) {
              addInvalid(field);
            }
            return;
          }

          let isInvalid = false;
          if (typeof field.checkValidity === 'function') {
            isInvalid = !field.checkValidity();
          }

          const textLike = tag === 'textarea' || (tag === 'input' && ['text', 'search', 'email', 'url', 'tel', 'password', 'number'].indexOf(type) >= 0);
          if (!isInvalid && textLike && String(field.value || '').trim() === '') {
            isInvalid = true;
          }
          if (isInvalid) {
            addInvalid(field);
          }
        });

        return invalidFields;
      },
      collectBlockingFieldErrors(form) {
        if (!form) {
          return [];
        }
        this.clearValidationHighlights(form);
        const invalidFields = [];
        const addInvalid = (field) => {
          if (!field || invalidFields.indexOf(field) >= 0) {
            return;
          }
          invalidFields.push(field);
          this.markFieldInvalid(field);
        };

        this.collectRequiredFieldErrors(form).forEach((field) => addInvalid(field));

        if (!this.validateTitle()) {
          addInvalid(form.querySelector('[name="post_title"]'));
        }
        if (!this.validateExcerpt()) {
          addInvalid(form.querySelector('[name="post_excerpt"]'));
        }
        if (!this.validateDates()) {
          const dateField = form.querySelector('[name="eep_end_date"]')
            || form.querySelector('[name="eep_end"]')
            || form.querySelector('[name="eep_start_date"]')
            || form.querySelector('[name="eep_start"]')
            || form.querySelector('[name="eep_end_time"]')
            || form.querySelector('[name="eep_start_time"]');
          addInvalid(dateField);
        }
        if (!this.validateSeoFields()) {
          if (this.seoTitleCount() > 60) {
            addInvalid(form.querySelector('[name="eep_meta_title"]'));
          }
          if (this.seoDescriptionCount() > 158) {
            addInvalid(form.querySelector('[name="eep_meta_description"]'));
          }
        }

        return invalidFields;
      },
      hasBlockingErrors(form) {
        const invalidFields = this.collectBlockingFieldErrors(form);
        if (!invalidFields.length) {
          return false;
        }
        this.focusFirstInvalidField(invalidFields);
        return true;
      },
      validateRequiredLocation(form, event) {
        if (!form || String(form.getAttribute('data-eep-requires-address') || '0') !== '1') {
          return true;
        }
        const addressInput = form.querySelector('[name="eep_address"]');
        if (!addressInput) {
          return true;
        }
        const addressValue = String(addressInput.value || '').trim();
        if (addressValue !== '') {
          this.clearFieldErrorState(addressInput);
          return true;
        }
        if (event && typeof event.preventDefault === 'function') {
          event.preventDefault();
        }
        if (event && typeof event.stopImmediatePropagation === 'function') {
          event.stopImmediatePropagation();
        }
        this.markFieldInvalid(addressInput);
        this.focusFirstInvalidField([addressInput]);
        showToast('Address is required.', 'error');
        const $form = $('#eep-submit-form');
        if ($form.length) {
          $form.data('eepSubmitting', false).removeClass('eep-is-submitting');
          $form.find('button[type="submit"], input[type="submit"]').prop('disabled', false).removeClass('eep-loading');
        }
        return false;
      },
      buildFormStateSnapshot(form) {
        if (!form || !form.elements) {
          return null;
        }
        syncEditors();
        const editField = form.querySelector('[name="eep_edit_post_id"]');
        const snapshot = {
          v: 1,
          ts: Date.now(),
          path: String(window.location.pathname || ''),
          editEventId: String(editField && editField.value ? editField.value : '0'),
          fields: {},
          checkboxNames: [],
          radioNames: []
        };
        const elements = Array.from(form.elements || []);
        elements.forEach((el) => {
          if (!el || el.disabled || !el.name) {
            return;
          }
          const name = String(el.name || '').trim();
          if (!name) {
            return;
          }
          if (
            name === 'eep_nonce'
            || name === '_wp_http_referer'
            || name === 'eep_recaptcha_token'
            || name === 'action'
          ) {
            return;
          }
          const tag = String(el.tagName || '').toLowerCase();
          const type = String(el.type || '').toLowerCase();
          if (type === 'file' || type === 'submit' || type === 'button' || type === 'reset') {
            return;
          }
          if (type === 'checkbox') {
            if (snapshot.checkboxNames.indexOf(name) === -1) {
              snapshot.checkboxNames.push(name);
            }
            if (!el.checked) {
              return;
            }
            if (!Array.isArray(snapshot.fields[name])) {
              snapshot.fields[name] = [];
            }
            snapshot.fields[name].push(String(el.value || '1'));
            return;
          }
          if (type === 'radio') {
            if (snapshot.radioNames.indexOf(name) === -1) {
              snapshot.radioNames.push(name);
            }
            if (!el.checked) {
              return;
            }
            snapshot.fields[name] = [String(el.value || '')];
            return;
          }
          if (tag === 'select' && el.multiple) {
            const selected = Array.from(el.options || []).filter((opt) => opt && opt.selected).map((opt) => String(opt.value || ''));
            snapshot.fields[name] = selected;
            return;
          }
          snapshot.fields[name] = [String(el.value || '')];
        });
        return snapshot;
      },
      persistFormState(form) {
        if (!form) {
          return;
        }
        try {
          const snapshot = this.buildFormStateSnapshot(form);
          if (!snapshot) {
            return;
          }
          window.localStorage.setItem(this.formStateStorageKey, JSON.stringify(snapshot));
        } catch (e) {
          // ignore persistence errors
        }
      },
      restoreFormState(form) {
        if (!form) {
          return;
        }
        const isEditing = String(form.getAttribute('data-eep-editing') || '') === '1';
        // Never let stale browser snapshots overwrite server-hydrated values on edit screens.
        if (isEditing) {
          return;
        }
        let params;
        try {
          params = new URLSearchParams(String(window.location.search || ''));
        } catch (e) {
          return;
        }
        const noticeType = String(
          params.get('eep_notice_type')
          || window.__eepNoticeType
          || ''
        ).toLowerCase();
        if (noticeType !== 'error') {
          return;
        }
        try {
          const raw = window.localStorage.getItem(this.formStateStorageKey);
          if (!raw) {
            return;
          }
          const parsed = JSON.parse(raw);
          if (!parsed || typeof parsed !== 'object') {
            return;
          }
          const ageMs = Date.now() - parseInt(parsed.ts || '0', 10);
          if (!Number.isFinite(ageMs) || ageMs < 0 || ageMs > this.formStateTtlMs) {
            window.localStorage.removeItem(this.formStateStorageKey);
            return;
          }
          if (String(parsed.path || '') !== String(window.location.pathname || '')) {
            return;
          }
          const currentEditField = form.querySelector('[name="eep_edit_post_id"]');
          const currentEditEventId = String(currentEditField && currentEditField.value ? currentEditField.value : '0');
          const savedEditEventId = String(parsed.editEventId || '0');
          if (savedEditEventId !== currentEditEventId) {
            return;
          }
          const elements = Array.from(form.elements || []);
          const checkboxNames = Array.isArray(parsed.checkboxNames) ? parsed.checkboxNames.map((n) => String(n || '')) : [];
          const radioNames = Array.isArray(parsed.radioNames) ? parsed.radioNames.map((n) => String(n || '')) : [];
          elements.forEach((el) => {
            if (!el || !el.name) {
              return;
            }
            const name = String(el.name || '');
            const type = String(el.type || '').toLowerCase();
            if (checkboxNames.indexOf(name) >= 0 && type === 'checkbox') {
              el.checked = false;
            }
            if (radioNames.indexOf(name) >= 0 && type === 'radio') {
              el.checked = false;
            }
          });
          const fields = (parsed.fields && typeof parsed.fields === 'object') ? parsed.fields : {};
          Object.keys(fields).forEach((name) => {
            const values = Array.isArray(fields[name]) ? fields[name].map((v) => String(v || '')) : [String(fields[name] || '')];
            if (!values.length) {
              return;
            }
            elements.forEach((el) => {
              if (!el || String(el.name || '') !== name) {
                return;
              }
              const tag = String(el.tagName || '').toLowerCase();
              const type = String(el.type || '').toLowerCase();
              if (type === 'checkbox' || type === 'radio') {
                el.checked = values.indexOf(String(el.value || '')) >= 0;
                return;
              }
              if (tag === 'select' && el.multiple) {
                Array.from(el.options || []).forEach((opt) => {
                  if (!opt) {
                    return;
                  }
                  opt.selected = values.indexOf(String(opt.value || '')) >= 0;
                });
                return;
              }
              el.value = String(values[0] || '');
            });
          });
          syncEditors();
          this.syncGalleryTextarea();
          this.syncVideoUrls();
          this.syncRecurrenceMeta();
          this.validateTitle();
          this.validateExcerpt();
          this.validateDates();
          showToast('Your previous form values were restored after validation error.', 'success');
        } catch (e) {
          // ignore malformed state
        }
      },
      autodetectTimezone() {
        const form = document.getElementById('eep-submit-form');
        if (!form || typeof Intl === 'undefined' || !Intl.DateTimeFormat) {
          return;
        }
        const tzSelect = form.querySelector('select[name="eep_timezone"]');
        if (!tzSelect || tzSelect.value) {
          return;
        }
        try {
          const guessed = Intl.DateTimeFormat().resolvedOptions().timeZone;
          if (!guessed) {
            return;
          }
          const hasOption = Array.from(tzSelect.options || []).some((opt) => opt.value === guessed);
          if (!hasOption) {
            return;
          }
          tzSelect.value = guessed;
          this.timezoneHint = 'Timezone auto-selected from your browser.';
        } catch (err) {
          // ignore
        }
      },
      getUploadNonce() {
        const nonceFromMedia = window.wp && window.wp.media && window.wp.media.model && window.wp.media.model.settings && window.wp.media.model.settings.post
          ? String(window.wp.media.model.settings.post.nonce || '')
          : '';
        if (nonceFromMedia) {
          return nonceFromMedia;
        }
        const fallback = window._wpPluploadSettings && window._wpPluploadSettings.defaults && window._wpPluploadSettings.defaults.multipart_params
          ? String(window._wpPluploadSettings.defaults.multipart_params._wpnonce || '')
          : '';
        return fallback;
      },
      openMedia(mode) {
        this.triggerFileInput(mode);
      },
      triggerFileInput(mode) {
        if (mode === 'cover' && this.$refs && this.$refs.coverFileInput) {
          this.$refs.coverFileInput.click();
          return;
        }
        if (mode === 'gallery' && this.$refs && this.$refs.galleryFileInput) {
          this.$refs.galleryFileInput.click();
        }
      },
      uploadFile(file, target, kind) {
        const uploadTarget = String(target || 'gallery');
        const uploadKind = String(kind || 'image');
        return uploadAssetFile(file, uploadKind, uploadTarget).then((payload) => {
          return payload && payload.url ? String(payload.url) : '';
        });
      },
      async uploadFiles(files, mode) {
        const incoming = Array.from(files || []);
        if (!incoming.length) {
          return;
        }

        const queue = mode === 'cover' ? [incoming[0]] : incoming;
        for (let i = 0; i < queue.length; i += 1) {
          const file = queue[i];
          if (!file) {
            continue;
          }
          if (mode === 'cover' && file.size > this.coverMaxBytes) {
            showToast('Cover image is too large.', 'error');
            continue;
          }
          if (mode === 'gallery' && file.size > this.galleryMaxBytes) {
            showToast('One of the gallery images is too large.', 'error');
            continue;
          }
          if (mode === 'gallery' && this.galleryImages.length >= this.galleryMaxFiles) {
            showToast('Gallery upload limit reached.', 'error');
            break;
          }
          try {
            const url = await this.uploadFile(file, mode, 'image');
            if (!url) {
              continue;
            }
            if (mode === 'cover') {
              this.coverImage = url;
            } else if (this.galleryImages.indexOf(url) === -1) {
              this.galleryImages.push(url);
            }
            this.syncGalleryTextarea();
            this.queueAutosave(160);
          } catch (error) {
            showToast(error && error.message ? error.message : 'Upload failed. Please try again.', 'error');
          }
        }
      },
      handleFileSelect(event, mode) {
        const files = event && event.target && event.target.files ? event.target.files : [];
        this.uploadFiles(files, mode);
        if (event && event.target) {
          event.target.value = '';
        }
      },
      handleDrop(event, mode) {
        const files = event.dataTransfer && event.dataTransfer.files ? event.dataTransfer.files : [];
        if (!files.length) {
          return;
        }
        this.uploadFiles(files, mode);
      },
      removeGallery(index) {
        this.galleryImages.splice(index, 1);
        this.syncGalleryTextarea();
        this.queueAutosave(160);
      },
      removeCover() {
        this.coverImage = '';
        this.queueAutosave(160);
      },
      syncGalleryTextarea() {
        this.galleryTextarea = this.galleryImages.join('\n');
      },
      extractYoutubeId(value) {
        const raw = String(value || '').trim();
        if (!raw) {
          return '';
        }
        const idPattern = /^[A-Za-z0-9_-]{6,32}$/;
        if (idPattern.test(raw) && raw.indexOf('http') !== 0) {
          return raw;
        }
        const ytMatch = raw.match(/[?&]v=([A-Za-z0-9_-]{6,32})/i)
          || raw.match(/youtu\.be\/([A-Za-z0-9_-]{6,32})/i)
          || raw.match(/youtube\.com\/embed\/([A-Za-z0-9_-]{6,32})/i)
          || raw.match(/youtube\.com\/shorts\/([A-Za-z0-9_-]{6,32})/i);
        return ytMatch && ytMatch[1] ? ytMatch[1] : '';
      },
      normalizeVideoValue(value) {
        const raw = String(value || '').trim();
        if (!raw) {
          return '';
        }
        if (/^https?:\/\//i.test(raw)) {
          return raw;
        }
        const id = this.extractYoutubeId(raw);
        return id || '';
      },
      syncVideoUrls() {
        const urls = [];
        const streamUrl = String(this.streamUrl || '').trim();
        if (streamUrl) {
          urls.push(streamUrl);
        }

        (Array.isArray(this.videoEmbeds) ? this.videoEmbeds : []).forEach((entry) => {
          const normalized = this.normalizeVideoValue(entry);
          if (!normalized) {
            return;
          }
          if (/^https?:\/\//i.test(normalized)) {
            urls.push(normalized);
            return;
          }
          urls.push('https://www.youtube.com/watch?v=' + normalized);
        });
        if (!Array.isArray(this.videoEmbeds) || !this.videoEmbeds.length) {
          this.videoEmbeds = [''];
        }

        const uniqueUrls = Array.from(new Set(urls.map((url) => String(url || '').trim()).filter((url) => url !== '')));
        this.videoUrlsTextarea = uniqueUrls.join('\n');

        const ytIds = uniqueUrls.map((url) => this.extractYoutubeId(url)).filter((id) => id !== '');
        this.youtubeIdOne = ytIds[0] || '';
        this.youtubeIdTwo = ytIds[1] || '';
      },
      addVideoEmbed() {
        if (!Array.isArray(this.videoEmbeds)) {
          this.videoEmbeds = [''];
        }
        this.videoEmbeds.push('');
        this.queueAutosave(220);
      },
      removeVideoEmbed(index) {
        if (!Array.isArray(this.videoEmbeds)) {
          this.videoEmbeds = [''];
          return;
        }
        this.videoEmbeds.splice(index, 1);
        if (!this.videoEmbeds.length) {
          this.videoEmbeds = [''];
        }
        this.syncVideoUrls();
        this.queueAutosave(220);
      },
      syncRecurrenceMeta() {
        if (this.isRecurring !== '1' || !Array.isArray(this.sessions) || !this.sessions.length) {
          this.recurrenceUntil = '';
          return;
        }
        let latest = '';
        this.sessions.forEach((session) => {
          const endValue = String((session && session.end) || '');
          const startValue = String((session && session.start) || '');
          const candidate = (endValue || startValue || '').trim();
          if (candidate && (latest === '' || candidate > latest)) {
            latest = candidate;
          }
        });
        this.recurrenceUntil = latest ? latest.slice(0, 10) : '';
      },
      getSessionAttachmentRows(index) {
        const session = Array.isArray(this.sessions) ? this.sessions[index] : null;
        const rawValue = String((session && session.attachments) || '');
        if (!rawValue.trim()) {
          return [];
        }
        return rawValue
          .split(/\r?\n/)
          .map((row) => String(row || '').trim())
          .filter((row) => row !== '');
      },
      normalizeSessionAttachmentUrl(value) {
        const raw = String(value || '').trim();
        if (!raw) {
          return '';
        }

        if (/^[A-Za-z0-9_-]{11}$/.test(raw)) {
          return 'https://www.youtube.com/watch?v=' + raw;
        }
        if (/^(?:youtube\.com|youtu\.be|vimeo\.com)\//i.test(raw)) {
          return 'https://' + raw;
        }
        if (/^[a-z0-9.-]+\.[a-z]{2,}(?:\/|$)/i.test(raw) && !/^[a-z]+:\/\//i.test(raw)) {
          return 'https://' + raw;
        }
        return raw;
      },
      appendSessionAttachmentUrl(index, rawUrl) {
        const normalizedUrl = this.normalizeSessionAttachmentUrl(rawUrl);
        if (!normalizedUrl) {
          return false;
        }
        if (!Array.isArray(this.sessions) || !this.sessions[index]) {
          return false;
        }

        const existing = this.getSessionAttachmentRows(index);
        if (existing.indexOf(normalizedUrl) !== -1) {
          return false;
        }

        existing.push(normalizedUrl);
        this.sessions[index].attachments = existing.join('\n');
        this.syncRecurrenceMeta();
        this.queueAutosave(180);
        return true;
      },
      triggerSessionAttachmentInput(index) {
        const input = document.getElementById('eep-session-attachment-upload-' + String(index));
        if (input && typeof input.click === 'function') {
          input.click();
        }
      },
      async handleSessionAttachmentUpload(event, index) {
        if (!Array.isArray(this.sessions) || !this.sessions[index]) {
          return;
        }
        const files = event && event.target && event.target.files ? Array.from(event.target.files) : [];
        if (!files.length) {
          return;
        }

        let addedCount = 0;
        for (let i = 0; i < files.length; i += 1) {
          const file = files[i];
          if (!file) {
            continue;
          }
          const mime = String(file.type || '').toLowerCase();
          const isImage = mime.indexOf('image/') === 0;
          const maxBytes = isImage ? this.galleryMaxBytes : this.fileMaxBytes;
          if (maxBytes > 0 && file.size > maxBytes) {
            showToast(isImage ? 'Image file is too large.' : 'File is too large.', 'error');
            continue;
          }

          try {
            const uploadTarget = isImage ? 'gallery' : 'file';
            const uploadKind = isImage ? 'image' : 'file';
            const uploadedUrl = await this.uploadFile(file, uploadTarget, uploadKind);
            if (this.appendSessionAttachmentUrl(index, uploadedUrl)) {
              addedCount += 1;
            }
          } catch (error) {
            showToast(error && error.message ? error.message : 'Upload failed. Please try again.', 'error');
          }
        }

        if (addedCount > 0) {
          showToast('Attachment added.', 'success');
        }
        if (event && event.target) {
          event.target.value = '';
        }
      },
      openSessionAttachmentMediaPicker(index) {
        if (!Array.isArray(this.sessions) || !this.sessions[index]) {
          return;
        }
        if (!window.wp || !window.wp.media) {
          showToast('Media library is unavailable right now.', 'error');
          return;
        }

        const frame = window.wp.media({
          title: 'Select Attachments',
          button: { text: 'Add attachments' },
          multiple: true
        });
        frame.on('select', () => {
          const selection = frame.state().get('selection');
          if (!selection || typeof selection.toJSON !== 'function') {
            return;
          }
          const items = selection.toJSON();
          if (!Array.isArray(items) || !items.length) {
            return;
          }
          let addedCount = 0;
          items.forEach((item) => {
            const candidateUrl = item && item.url ? String(item.url).trim() : '';
            if (this.appendSessionAttachmentUrl(index, candidateUrl)) {
              addedCount += 1;
            }
          });
          if (addedCount > 0) {
            showToast('Attachment added from media library.', 'success');
          }
        });
        frame.open();
      },
      triggerSpeakerImageInput(index) {
        const input = document.getElementById('eep-speaker-image-upload-' + String(index));
        if (input && typeof input.click === 'function') {
          input.click();
        }
      },
      async handleSpeakerImageUpload(event, index) {
        if (!Array.isArray(this.keySpeakers) || !this.keySpeakers[index]) {
          return;
        }
        const files = event && event.target && event.target.files ? Array.from(event.target.files) : [];
        if (!files.length) {
          return;
        }
        const file = files[0];
        const mime = String((file && file.type) || '').toLowerCase();
        if (mime.indexOf('image/') !== 0) {
          showToast('Please choose an image file.', 'error');
          if (event && event.target) {
            event.target.value = '';
          }
          return;
        }
        if (this.galleryMaxBytes > 0 && file.size > this.galleryMaxBytes) {
          showToast('Image file is too large.', 'error');
          if (event && event.target) {
            event.target.value = '';
          }
          return;
        }
        try {
          const uploadedUrl = await this.uploadFile(file, 'gallery', 'image');
          if (uploadedUrl) {
            this.keySpeakers[index].image = uploadedUrl;
            this.queueAutosave(180);
            showToast('Speaker image added.', 'success');
          }
        } catch (error) {
          showToast(error && error.message ? error.message : 'Upload failed. Please try again.', 'error');
        }
        if (event && event.target) {
          event.target.value = '';
        }
      },
      openSpeakerImageMediaPicker(index) {
        if (!Array.isArray(this.keySpeakers) || !this.keySpeakers[index]) {
          return;
        }
        if (!window.wp || !window.wp.media) {
          showToast('Media library is unavailable right now.', 'error');
          return;
        }
        const frame = window.wp.media({
          title: 'Select Speaker Image',
          button: { text: 'Use image' },
          multiple: false,
          library: { type: 'image' }
        });
        frame.on('select', () => {
          const selection = frame.state().get('selection');
          if (!selection || typeof selection.toJSON !== 'function') {
            return;
          }
          const items = selection.toJSON();
          const firstItem = Array.isArray(items) && items.length ? items[0] : null;
          const candidateUrl = firstItem && firstItem.url ? String(firstItem.url).trim() : '';
          if (!candidateUrl) {
            return;
          }
          this.keySpeakers[index].image = candidateUrl;
          this.queueAutosave(180);
          showToast('Speaker image selected.', 'success');
        });
        frame.open();
      },
      removeSpeakerImage(index) {
        if (!Array.isArray(this.keySpeakers) || !this.keySpeakers[index]) {
          return;
        }
        this.keySpeakers[index].image = '';
        this.queueAutosave(160);
      },
      addSession() {
        this.sessions.push({ title: '', start: '', end: '', location: '', speaker: '', day: '', notes: '', attachments: '', session_key: '' });
        this.isRecurring = '1';
        this.syncRecurrenceMeta();
        this.queueAutosave(220);
      },
      removeSession(index) {
        this.sessions.splice(index, 1);
        if (!this.sessions.length) {
          this.isRecurring = '0';
        }
        this.syncRecurrenceMeta();
        this.queueAutosave(220);
      },
      addKeySpeaker() {
        if (!Array.isArray(this.keySpeakers)) {
          this.keySpeakers = [];
        }
        this.keySpeakers.push({ name: '', profession: '', subject: '', image: '', details: '' });
        this.queueAutosave(220);
      },
      removeKeySpeaker(index) {
        if (!Array.isArray(this.keySpeakers)) {
          this.keySpeakers = [];
          return;
        }
        this.keySpeakers.splice(index, 1);
        this.queueAutosave(220);
      },
      autosaveDraft() {
        if (!this.autosaveEnabled) {
          return;
        }
        const form = document.getElementById('eep-submit-form');
        if (!form || this.autosaveBusy) {
          return;
        }
        const titleInput = form.querySelector('[name="post_title"]');
        if (!titleInput || !titleInput.value.trim()) {
          return;
        }

        syncEditors();
        this.autosaveBusy = true;
        this.validateExcerpt();
        this.syncGalleryTextarea();
        this.syncVideoUrls();

        const fd = new FormData(form);
        fd.set('action', 'eep_autosave_event');
        fd.set('nonce', getConfig('autosaveNonce', ''));
        const editField = form.querySelector('[name="eep_edit_post_id"]');
        const sourceEditId = String(editField && editField.value ? editField.value : '0');

        fetch(getConfig('autosaveEndpoint', '/wp-admin/admin-post.php'), {
          method: 'POST',
          body: fd,
          credentials: 'same-origin'
        })
          .then((res) => res.json())
          .then((json) => {
            if (json && json.success) {
              this.autosaveMessage = 'Autosaved at ' + new Date().toLocaleTimeString();
              this.autosavePostId = json.data && json.data.post_id ? String(json.data.post_id) : this.autosavePostId;
              const state = {
                coverImage: this.coverImage,
                galleryImages: this.galleryImages,
                sessions: this.sessions,
                keySpeakers: this.keySpeakers,
                isRecurring: this.isRecurring,
                streamUrl: this.streamUrl,
                excerptValue: this.excerptValue,
                metaTitleValue: this.metaTitleValue,
                metaDescriptionValue: this.metaDescriptionValue,
                cityValue: this.cityValue,
                countryValue: this.countryValue,
                latValue: this.latValue,
                lngValue: this.lngValue,
                youtubeIdOne: this.youtubeIdOne,
                youtubeIdTwo: this.youtubeIdTwo,
                videoEmbeds: this.videoEmbeds,
                autosavePostId: this.autosavePostId,
                sourceEditId: sourceEditId
              };
              if (sourceEditId === '0') {
                window.localStorage.setItem('eep_submit_draft', JSON.stringify(state));
              } else {
                window.localStorage.removeItem('eep_submit_draft');
              }
            } else {
              this.autosaveMessage = 'Autosave failed';
            }
          })
          .catch(() => {
            this.autosaveMessage = 'Autosave failed';
          })
          .finally(() => {
            this.autosaveBusy = false;
          });
      }
    };
  };

  function initQuickEdit() {
    $(document).off('click.eepQuickEdit', '[data-eep-edit-toggle]').on('click.eepQuickEdit', '[data-eep-edit-toggle]', function () {
      const target = $(this).attr('data-eep-edit-toggle');
      $('#' + target).toggle();
    });

    $(document).off('click.eepMediaPicker', '[data-eep-media-picker]').on('click.eepMediaPicker', '[data-eep-media-picker]', function (event) {
      event.preventDefault();
      const targetId = String($(this).attr('data-eep-media-picker') || '').trim();
      if (!targetId) {
        return;
      }
      const $target = $('#' + targetId);
      if (!$target.length) {
        return;
      }
      if (!window.wp || !window.wp.media) {
        showToast('Media picker is unavailable right now.', 'error');
        return;
      }

      const frame = window.wp.media({
        title: 'Select Gallery Images',
        multiple: true,
        library: { type: 'image' }
      });
      frame.on('select', function () {
        const selection = frame.state().get('selection').toJSON();
        if (!Array.isArray(selection) || !selection.length) {
          return;
        }
        const existing = String($target.val() || '').split(/\r?\n/).map((row) => String(row || '').trim()).filter((row) => row !== '');
        selection.forEach(function (item) {
          const url = item && item.url ? String(item.url).trim() : '';
          if (url && existing.indexOf(url) === -1) {
            existing.push(url);
          }
        });
        $target.val(existing.join('\n'));
        showToast('Selected images added to gallery field.', 'success');
      });
      frame.open();
    });
  }

  function initCardLinks() {
    $(document).off('click.eepCardLink', '[data-eep-card-url]').on('click.eepCardLink', '[data-eep-card-url]', function (event) {
      const $target = $(event.target);
      if ($target.closest('a, button, input, select, textarea, label, form').length) {
        return;
      }
      const url = String($(this).attr('data-eep-card-url') || '').trim();
      if (!url) {
        return;
      }
      window.location.href = url;
    });

    $(document).off('keydown.eepCardLink', '[data-eep-card-url]').on('keydown.eepCardLink', '[data-eep-card-url]', function (event) {
      if (event.key !== 'Enter' && event.key !== ' ') {
        return;
      }
      event.preventDefault();
      const url = String($(this).attr('data-eep-card-url') || '').trim();
      if (!url) {
        return;
      }
      window.location.href = url;
    });
  }

  function initCalendarOverflowModal() {
    const $modal = $('#eep-calendar-modal');
    if (!$modal.length) {
      return;
    }
    const $title = $('#eep-calendar-modal-title');
    const $list = $('#eep-calendar-modal-list');

    const closeModal = function () {
      $modal.attr('hidden', 'hidden');
      $('body').removeClass('eep-lightbox-open');
    };

    const openModal = function (dayLabel, events) {
      if (!$list.length) {
        return;
      }
      $list.empty();
      const titleText = decodeHtmlEntities(dayLabel || '').trim() || getMessage('calendarEvents', 'Events');
      if ($title.length) {
        $title.text(titleText);
      }

      if (!Array.isArray(events) || !events.length) {
        const $empty = $('<p/>', {
          class: 'eep-calendar-modal-item-city',
          text: getMessage('calendarNoEvents', 'No results found for this day.')
        });
        $list.append($empty);
      } else {
        events.forEach(function (event) {
          const eventUrl = String(event && event.url ? event.url : '#').trim() || '#';
          const time = String(event && event.time ? event.time : '').trim();
          const title = decodeHtmlEntities(event && event.title ? event.title : '').trim();
          const city = decodeHtmlEntities(event && event.city ? event.city : '').trim();

          const $item = $('<a/>', {
            class: 'eep-calendar-modal-item',
            href: eventUrl
          });
          if (time) {
            $item.append($('<span/>', {
              class: 'eep-calendar-modal-item-time',
              text: time
            }));
          }
          $item.append($('<span/>', {
            class: 'eep-calendar-modal-item-title',
            text: title || getMessage('calendarUntitled', 'Event')
          }));
          if (city) {
            $item.append($('<span/>', {
              class: 'eep-calendar-modal-item-city',
              text: city
            }));
          }
          $list.append($item);
        });
      }

      $modal.removeAttr('hidden');
      $('body').addClass('eep-lightbox-open');
    };

    $(document).off('click.eepCalendarMore', '[data-eep-calendar-open="1"]').on('click.eepCalendarMore', '[data-eep-calendar-open="1"]', function () {
      const dayLabel = String($(this).attr('data-day-label') || '').trim();
      const payload = String($(this).attr('data-events') || '[]');
      let events = [];
      try {
        const decoded = JSON.parse(payload);
        if (Array.isArray(decoded)) {
          events = decoded;
        }
      } catch (_error) {
        events = [];
      }
      openModal(dayLabel, events);
    });

    $(document).off('click.eepCalendarClose', '#eep-calendar-modal-close').on('click.eepCalendarClose', '#eep-calendar-modal-close', function () {
      closeModal();
    });

    $modal.off('click.eepCalendarBackdrop').on('click.eepCalendarBackdrop', function (event) {
      if (event.target === this) {
        closeModal();
      }
    });

    $(document).off('keydown.eepCalendarClose').on('keydown.eepCalendarClose', function (event) {
      if (event.key === 'Escape' && !$modal.attr('hidden')) {
        closeModal();
      }
    });
  }

  function initEventContactModal() {
    if (!$('.eep-contact-modal').length) {
      return;
    }

    const refreshBodyLock = function () {
      const hasVisibleOverlay = $('.eep-lightbox:not([hidden]), .eep-contact-modal:not([hidden])').length > 0;
      $('body').toggleClass('eep-lightbox-open', hasVisibleOverlay);
    };

    const openModal = function ($modal) {
      if (!$modal.length) {
        return;
      }
      $modal.removeAttr('hidden');
      window.requestAnimationFrame(function () {
        $modal.addClass('is-open');
      });
      refreshBodyLock();
      const $focusField = $modal.find('input[type="text"], input[type="email"], textarea').first();
      if ($focusField.length) {
        window.setTimeout(function () {
          $focusField.trigger('focus');
        }, 120);
      }
    };

    const closeModal = function ($modal) {
      if (!$modal.length) {
        return;
      }
      $modal.removeClass('is-open');
      window.setTimeout(function () {
        $modal.attr('hidden', 'hidden');
        refreshBodyLock();
      }, 200);
    };

    $(document).off('click.eepContactModalOpen', '[data-eep-contact-open]').on('click.eepContactModalOpen', '[data-eep-contact-open]', function (event) {
      event.preventDefault();
      const modalId = String($(this).attr('data-eep-contact-open') || '').trim();
      if (!modalId) {
        return;
      }
      const modalElement = document.getElementById(modalId);
      if (!modalElement) {
        return;
      }
      openModal($(modalElement));
    });

    $(document).off('click.eepContactModalClose', '[data-eep-contact-close]').on('click.eepContactModalClose', '[data-eep-contact-close]', function (event) {
      event.preventDefault();
      closeModal($(this).closest('.eep-contact-modal'));
    });

    $(document).off('click.eepContactModalBackdrop', '.eep-contact-modal').on('click.eepContactModalBackdrop', '.eep-contact-modal', function (event) {
      if (event.target === this) {
        closeModal($(this));
      }
    });

    $(document).off('keydown.eepContactModalEsc').on('keydown.eepContactModalEsc', function (event) {
      if (event.key !== 'Escape') {
        return;
      }
      const $activeModal = $('.eep-contact-modal:not([hidden])').last();
      if ($activeModal.length) {
        closeModal($activeModal);
      }
    });
  }

  let revealObserver = null;
  let revealMutationObserver = null;
  let revealRaf = null;

  function applyScrollReveal(scope) {
    const root = scope && scope.querySelectorAll ? scope : document;
    if (!root || !root.querySelectorAll) {
      return;
    }
    const selector = [
      '.eep-page-banner',
      '.eep-card',
      '.eep-account-panel',
      '.eep-auth-panel',
      '.eep-form-section',
      '.eep-event-section',
      '.eep-map-item-card',
      '.map-card',
      '.eep-empty-state',
      '.eep-calendar-cell',
      '.eep-admin-table tbody tr'
    ].join(', ');

    const nodes = root.querySelectorAll(selector);
    if (!nodes.length) {
      return;
    }

    let revealIndex = 0;
    nodes.forEach(function (node) {
      if (!node || node.classList.contains('eep-reveal-ready')) {
        return;
      }
      node.classList.add('eep-reveal', 'eep-reveal-ready');
      node.style.setProperty('--eep-reveal-delay', String(Math.min((revealIndex % 8) * 32, 224)) + 'ms');
      revealIndex += 1;

      if (!revealObserver) {
        node.classList.add('is-visible');
        return;
      }
      revealObserver.observe(node);
    });
  }

  function initScrollReveal() {
    const body = document.body;
    if (!body) {
      return;
    }

    if (!revealObserver && 'IntersectionObserver' in window) {
      revealObserver = new IntersectionObserver(function (entries) {
        entries.forEach(function (entry) {
          if (!entry || !entry.target || !entry.isIntersecting) {
            return;
          }
          entry.target.classList.add('is-visible');
          revealObserver.unobserve(entry.target);
        });
      }, {
        root: null,
        rootMargin: '0px 0px -8% 0px',
        threshold: 0.08
      });
    }

    applyScrollReveal(document);

    if (revealMutationObserver || !('MutationObserver' in window)) {
      return;
    }

    revealMutationObserver = new MutationObserver(function (mutations) {
      let shouldRefresh = false;
      for (let index = 0; index < mutations.length; index += 1) {
        const mutation = mutations[index];
        if (!mutation || !mutation.addedNodes || !mutation.addedNodes.length) {
          continue;
        }
        for (let nodeIndex = 0; nodeIndex < mutation.addedNodes.length; nodeIndex += 1) {
          const added = mutation.addedNodes[nodeIndex];
          if (added && added.nodeType === 1) {
            shouldRefresh = true;
            break;
          }
        }
        if (shouldRefresh) {
          break;
        }
      }

      if (!shouldRefresh) {
        return;
      }

      if (revealRaf) {
        window.cancelAnimationFrame(revealRaf);
      }
      revealRaf = window.requestAnimationFrame(function () {
        applyScrollReveal(document);
      });
    });

    revealMutationObserver.observe(body, {
      childList: true,
      subtree: true
    });
  }

  let smoothScrollEngineBound = false;
  let smoothScrollRafId = 0;
  let smoothScrollLenisInstance = null;

  function initSmoothScrollingEngine() {
    if (smoothScrollEngineBound) {
      return;
    }
    if (!toBool(getConfig('smoothScrolling', false), false)) {
      return;
    }
    if (!document.body || !document.documentElement) {
      return;
    }
    if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
      return;
    }
    if (typeof window.Lenis !== 'function') {
      return;
    }

    const clampNumber = function (value, min, max) {
      const numeric = Number(value);
      if (!Number.isFinite(numeric)) {
        return min;
      }
      if (numeric < min) {
        return min;
      }
      if (numeric > max) {
        return max;
      }
      return numeric;
    };

    const profileRaw = String(getConfig('smoothScrollProfile', 'balanced') || 'balanced').toLowerCase();
    const profile = ['gentle', 'balanced', 'snappy', 'custom'].indexOf(profileRaw) >= 0 ? profileRaw : 'balanced';
    const profileDefaults = {
      gentle: { duration: 1.45, multiplier: 0.92 },
      balanced: { duration: 1.2, multiplier: 1 },
      snappy: { duration: 0.9, multiplier: 1.12 },
      custom: { duration: 1.2, multiplier: 1 }
    };
    const fallbackProfile = profileDefaults.balanced;
    const profileConfig = Object.prototype.hasOwnProperty.call(profileDefaults, profile)
      ? profileDefaults[profile]
      : fallbackProfile;

    let duration = profileConfig.duration;
    let mouseMultiplier = profileConfig.multiplier;

    if (profile === 'custom') {
      duration = clampNumber(getConfig('smoothScrollEase', 1.2), 0.4, 3);
      mouseMultiplier = clampNumber(getConfig('smoothScrollWheelMultiplier', 1), 0.4, 3);
    }

    const touchpadModeRaw = String(getConfig('smoothScrollTouchpadBehavior', 'native') || 'native').toLowerCase();
    const touchpadMode = ['native', 'assist', 'force'].indexOf(touchpadModeRaw) >= 0 ? touchpadModeRaw : 'native';
    const hasTouchInput = (
      typeof window.matchMedia === 'function' &&
      (window.matchMedia('(pointer: coarse)').matches || window.matchMedia('(hover: none) and (pointer: coarse)').matches)
    );
    const enableOnTouch = toBool(getConfig('smoothScrollEnableOnTouch', false), false);
    if (hasTouchInput && !enableOnTouch) {
      return;
    }

    if (touchpadMode === 'assist') {
      mouseMultiplier = clampNumber(mouseMultiplier * 0.92, 0.4, 3);
    } else if (touchpadMode === 'force') {
      mouseMultiplier = clampNumber(mouseMultiplier * 1.1, 0.4, 3);
    }

    try {
      smoothScrollLenisInstance = new window.Lenis({
        duration: duration,
        easing: function (t) {
          return Math.min(1, 1.001 - Math.pow(2, -10 * t));
        },
        direction: 'vertical',
        gestureDirection: 'vertical',
        smooth: true,
        mouseMultiplier: mouseMultiplier,
        smoothTouch: enableOnTouch,
        prevent: function (node) {
          if (!node || typeof node.closest !== 'function') {
            return false;
          }
          return !!node.closest(
            '.leaflet-container, .leaflet-control-container, .leaflet-popup, [data-eep-smooth-ignore="1"], [data-no-smooth-scroll="1"], .no-smooth-scroll, input, textarea, select, option, [contenteditable=""], [contenteditable="true"]'
          );
        }
      });
    } catch (error) {
      smoothScrollLenisInstance = null;
      smoothScrollEngineBound = false;
      return;
    }

    smoothScrollEngineBound = true;
    window.__eepLenis = smoothScrollLenisInstance;

    const raf = function (time) {
      if (!smoothScrollLenisInstance) {
        return;
      }
      smoothScrollLenisInstance.raf(time);
      smoothScrollRafId = window.requestAnimationFrame(raf);
    };
    smoothScrollRafId = window.requestAnimationFrame(raf);

    document.addEventListener('click', function (event) {
      const link = event && event.target && typeof event.target.closest === 'function'
        ? event.target.closest('a[href^="#"]')
        : null;
      if (!link || !smoothScrollLenisInstance) {
        return;
      }
      const href = String(link.getAttribute('href') || '');
      if (href.length < 2) {
        return;
      }
      const target = document.getElementById(href.slice(1));
      if (!target) {
        return;
      }
      smoothScrollLenisInstance.scrollTo(target, { duration: duration });
      event.preventDefault();
    }, true);

    window.addEventListener('beforeunload', function () {
      if (smoothScrollRafId) {
        window.cancelAnimationFrame(smoothScrollRafId);
        smoothScrollRafId = 0;
      }
      if (smoothScrollLenisInstance && typeof smoothScrollLenisInstance.destroy === 'function') {
        smoothScrollLenisInstance.destroy();
      }
      smoothScrollLenisInstance = null;
      smoothScrollEngineBound = false;
      window.__eepLenis = null;
    }, { once: true });
  }

  function initSubmitNavSpy() {
    const nav = document.getElementById('eep-submit-nav');
    if (!nav) {
      return;
    }
    if (nav.getAttribute('data-eep-bound') === '1') {
      return;
    }
    nav.setAttribute('data-eep-bound', '1');
    const links = Array.from(nav.querySelectorAll('.eep-submit-nav-link[href^="#"]'));
    if (!links.length) {
      return;
    }

    const sectionMap = links.map((link) => {
      const targetId = String(link.getAttribute('href') || '').replace('#', '');
      const section = targetId ? document.getElementById(targetId) : null;
      return {
        link,
        targetId,
        section
      };
    }).filter((item) => item.section);
    if (!sectionMap.length) {
      return;
    }

    function setActive(targetId) {
      sectionMap.forEach((item) => {
        if (item.targetId === targetId) {
          item.link.classList.add('is-active');
          item.section.classList.add('is-current');
        } else {
          item.link.classList.remove('is-active');
          item.section.classList.remove('is-current');
        }
      });
    }

    sectionMap.forEach((item) => {
      item.link.addEventListener('click', function (event) {
        event.preventDefault();
        item.section.scrollIntoView({ behavior: 'smooth', block: 'start' });
        setActive(item.targetId);
      });
    });

    setActive(sectionMap[0].targetId);

    if ('IntersectionObserver' in window) {
      const observer = new IntersectionObserver(function (entries) {
        entries.forEach((entry) => {
          if (entry.isIntersecting && entry.target && entry.target.id) {
            setActive(String(entry.target.id));
          }
        });
      }, {
        root: null,
        rootMargin: '-35% 0px -55% 0px',
        threshold: [0.1, 0.3, 0.6]
      });
      sectionMap.forEach((item) => observer.observe(item.section));
      return;
    }

    window.addEventListener('scroll', function () {
      const anchor = window.scrollY + (window.innerHeight * 0.35);
      let activeId = sectionMap[0].targetId;
      sectionMap.forEach((item) => {
        if (!item.section) {
          return;
        }
        if (item.section.offsetTop <= anchor) {
          activeId = item.targetId;
        }
      });
      setActive(activeId);
    }, { passive: true });
  }

  function initUserManagementSearch() {
    const $form = $('.eep-user-search-form').first();
    const $input = $('#eep-user-search-input');
    const $tbody = $('#eep-user-management-body');
    const $count = $('#eep-user-search-count');
    const endpoint = String(getConfig('userSearchRestUrl', '') || '');
    if (!$form.length || !$input.length || !$tbody.length || !endpoint) {
      return;
    }

    let requestController = null;
    let debounceTimer = null;

    function setLoading(state) {
      $tbody.toggleClass('eep-loading', state === true);
      $form.toggleClass('eep-loading', state === true);
    }

    function runSearch(immediate) {
      const query = String($input.val() || '').trim();
      const limit = parseInt($form.attr('data-limit') || '100', 10);
      const safeLimit = Number.isFinite(limit) && limit > 0 ? Math.min(200, limit) : 100;

      if (requestController) {
        requestController.abort();
      }

      const execute = function () {
        const params = new URLSearchParams();
        params.set('q', query);
        params.set('limit', String(safeLimit));
        requestController = new AbortController();
        setLoading(true);
        fetch(endpoint + '?' + params.toString(), {
          credentials: 'same-origin',
          signal: requestController.signal,
          headers: {
            'X-WP-Nonce': String(getConfig('restNonce', '') || '')
          }
        })
          .then(async (res) => {
            const payload = await res.json();
            if (!res.ok) {
              throw new Error((payload && payload.message) ? payload.message : getMessage('userSearchFailed', 'Could not search users right now.'));
            }
            $tbody.html(payload && payload.html ? payload.html : '');
            if ($count.length) {
              const found = payload && Number.isFinite(parseInt(payload.count, 10)) ? parseInt(payload.count, 10) : 0;
              $count.text(String(found) + ' users loaded');
            }
          })
          .catch((err) => {
            if (err && err.name === 'AbortError') {
              return;
            }
            showToast(err && err.message ? err.message : getMessage('userSearchFailed', 'Could not search users right now.'), 'error');
          })
          .finally(() => {
            setLoading(false);
          });
      };

      if (immediate) {
        execute();
        return;
      }
      clearTimeout(debounceTimer);
      debounceTimer = window.setTimeout(execute, 260);
    }

    $form.off('submit.eepUserSearch').on('submit.eepUserSearch', function (event) {
      event.preventDefault();
      runSearch(true);
    });

    $input.off('input.eepUserSearch').on('input.eepUserSearch', function () {
      runSearch(false);
    });
  }

  function initNoticeToasts() {
    $('[data-eep-toast-message]').each(function () {
      const $node = $(this);
      const text = String($node.attr('data-eep-toast-message') || '').trim();
      if (!text) {
        return;
      }
      const rawType = String($node.attr('data-eep-toast-type') || 'success').trim().toLowerCase();
      const type = ['success', 'error', 'warning', 'info'].indexOf(rawType) >= 0 ? rawType : 'success';
      showToast(text, type);
      $node.attr('data-eep-toast-message', '');
    });

    $('.eep-notice').each(function () {
      const $notice = $(this);
      const text = String($notice.text() || '').trim();
      if (!text) {
        return;
      }
      const type = $notice.hasClass('eep-notice-error') ? 'error' : 'success';
      showToast(text, type);
      $notice.addClass('eep-toasted');
    });
  }

  function initUrlNoticeToast() {
    let url;
    try {
      url = new URL(window.location.href);
    } catch (error) {
      return;
    }

    const params = url.searchParams;
    const rawNotice = String(params.get('eep_notice') || '').trim();
    const rawType = String(params.get('eep_notice_type') || 'success').trim().toLowerCase();
    const hasNoticeParams = rawNotice !== '' || params.has('eep_notice_type');
    if (!hasNoticeParams) {
      window.__eepNoticeType = '';
      return;
    }

    const validTypes = ['success', 'error', 'warning', 'info'];
    const noticeType = rawType === 'warn'
      ? 'warning'
      : (validTypes.indexOf(rawType) >= 0 ? rawType : 'success');

    window.__eepNoticeType = noticeType;

    const inlineToastNode = Array.from(document.querySelectorAll('[data-eep-toast-message]')).find((node) => {
      return String(node.getAttribute('data-eep-toast-message') || '').trim() !== '';
    });
    const inlineNoticeNode = Array.from(document.querySelectorAll('.eep-notice')).find((node) => {
      return String((node.textContent || '')).trim() !== '';
    });
    const hasInlineNoticeNodes = !!inlineToastNode || !!inlineNoticeNode;
    if (rawNotice !== '' && !hasInlineNoticeNodes) {
      let noticeText = rawNotice;
      try {
        noticeText = decodeURIComponent(rawNotice.replace(/\+/g, '%20'));
      } catch (error) {
        noticeText = rawNotice;
      }
      noticeText = String(noticeText || '').trim();
      if (noticeText) {
        showToast(noticeText, noticeType);
      }
    }

    params.delete('eep_notice');
    params.delete('eep_notice_type');

    const nextUrl = url.pathname + (params.toString() ? ('?' + params.toString()) : '') + (url.hash || '');
    if (window.history && typeof window.history.replaceState === 'function') {
      window.history.replaceState(window.history.state || null, document.title, nextUrl);
    }
  }

  function initAccountTabsAjax() {
    const tabs = document.querySelector('.eep-account-tabs[data-eep-ajax="1"]');
    if (!tabs) {
      return;
    }
    if (tabs.getAttribute('data-eep-bound') === '1') {
      return;
    }
    tabs.setAttribute('data-eep-bound', '1');

    tabs.addEventListener('click', function (event) {
      const link = event.target && event.target.closest ? event.target.closest('a[href]') : null;
      if (!link || !tabs.contains(link)) {
        return;
      }
      if (link.getAttribute('data-eep-no-ajax') === '1') {
        return;
      }
      if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) {
        return;
      }

      const href = String(link.href || '').trim();
      if (!href || href.indexOf(window.location.origin) !== 0) {
        return;
      }
      event.preventDefault();

      tabs.classList.add('eep-loading');
      fetch(href, { credentials: 'same-origin' })
        .then((res) => res.text())
        .then((html) => {
          const parser = new window.DOMParser();
          const doc = parser.parseFromString(String(html || ''), 'text/html');
          const nextTabs = doc.querySelector('.eep-account-tabs');
          const nextShell = doc.querySelector('.eep-account-shell');
          const currentTabs = document.querySelector('.eep-account-tabs');
          const currentShell = document.querySelector('.eep-account-shell');
          if (!nextTabs || !nextShell || !currentTabs || !currentShell) {
            window.location.href = href;
            return;
          }

          currentTabs.innerHTML = nextTabs.innerHTML;
          currentShell.innerHTML = nextShell.innerHTML;
          window.history.pushState({}, '', href);
          window.scrollTo({ top: 0, behavior: 'smooth' });

          if (window.Alpine && typeof window.Alpine.initTree === 'function') {
            window.Alpine.initTree(currentShell);
          }

          runFrontendInitializers();
        })
        .catch(() => {
          window.location.href = href;
        })
        .finally(() => {
          tabs.classList.remove('eep-loading');
        });
    });
  }

  function resolveSpaRoot(preferredSelector, rootDocument) {
    const doc = (rootDocument && typeof rootDocument.querySelector === 'function') ? rootDocument : document;
    const normalizedPreferred = String(preferredSelector || '').trim() || '[data-eep-spa-main="1"]';
    let root = doc.querySelector(normalizedPreferred);
    if (root) {
      return {
        node: root,
        selector: normalizedPreferred
      };
    }

    const fallbackSelectors = [
      '[data-eep-spa-main="1"]',
      '.eep-spa-view',
      '[data-eep-uniform-page="1"]',
      '.eep-uniform-page-gutter'
    ];

    for (let i = 0; i < fallbackSelectors.length; i += 1) {
      const fallbackSelector = String(fallbackSelectors[i] || '').trim();
      if (!fallbackSelector) {
        continue;
      }
      root = doc.querySelector(fallbackSelector);
      if (!root) {
        continue;
      }
      if (!root.hasAttribute('data-eep-spa-main')) {
        root.setAttribute('data-eep-spa-main', '1');
      }
      return {
        node: root,
        selector: '[data-eep-spa-main="1"]'
      };
    }

    return null;
  }

  function initHybridSpaRouter() {
    if (spaRouterBound) {
      return;
    }
    if (!toBool(getConfig('spaEnabled', false), false)) {
      setSpaFeedbackStatus('SPA is disabled', 'warn', 'Enable SPA hybrid navigation in plugin settings');
      return;
    }
    if (typeof window.fetch !== 'function' || typeof window.DOMParser !== 'function' || !window.history || typeof window.history.pushState !== 'function') {
      setSpaFeedbackStatus('SPA is unavailable in this browser', 'error', 'Required fetch/history APIs are missing');
      return;
    }

    spaSelector = String(getConfig('spaSelector', '[data-eep-spa-main="1"]') || '[data-eep-spa-main="1"]').trim();
    if (!spaSelector) {
      spaSelector = '[data-eep-spa-main="1"]';
    }

    const configuredRoutes = getConfig('spaRoutes', []);
    const routeSet = new Set();
    if (Array.isArray(configuredRoutes)) {
      configuredRoutes.forEach(function (routeUrl) {
        const value = String(routeUrl || '').trim();
        if (!value) {
          return;
        }
        try {
          const parsed = new URL(value, window.location.origin);
          if (parsed.origin !== window.location.origin) {
            return;
          }
          routeSet.add(normalizePathname(parsed.pathname));
        } catch (err) {
          // ignore invalid route entries
        }
      });
    }
    const currentPath = normalizePathname(window.location.pathname);
    const currentPathConfigured = routeSet.has(currentPath);
    routeSet.add(currentPath);

    const spaRoot = resolveSpaRoot(spaSelector);
    if (!spaRoot) {
      if (!currentPathConfigured) {
        setSpaFeedbackStatus('SPA monitor ready', 'ok', 'Current page is outside SPA route scope');
        return;
      }
      setSpaFeedbackStatus('SPA container not found on this page', 'warn', 'Falling back to full page loads');
      return;
    }

    spaSelector = String(spaRoot.selector || '[data-eep-spa-main="1"]');
    if (!spaSelector) {
      spaSelector = '[data-eep-spa-main="1"]';
    }

    spaRouteSet = routeSet;
    spaRouterBound = true;
    setSpaFeedbackStatus('SPA router active', 'ok', 'Intercepting configured plugin routes');

    let announcer = document.getElementById('eep-spa-announcer');
    if (!announcer) {
      announcer = document.createElement('div');
      announcer.id = 'eep-spa-announcer';
      announcer.className = 'eep-spa-announcer';
      announcer.setAttribute('aria-live', 'polite');
      announcer.setAttribute('aria-atomic', 'true');
      document.body.appendChild(announcer);
    }

    const setLoadingState = function (active, targetUrl) {
      const root = document.querySelector(spaSelector);
      if (root) {
        root.classList.toggle('eep-spa-loading', active === true);
      }
      document.documentElement.classList.toggle('eep-spa-loading', active === true);
      if (window.__eepPagePreloader && typeof window.__eepPagePreloader.setVisible === 'function') {
        window.__eepPagePreloader.setVisible(active === true, 'spa', targetUrl);
      }
    };

    const announce = function (message) {
      if (!announcer) {
        return;
      }
      announcer.textContent = String(message || '').trim();
    };

    const shouldSkipSpaLink = function (link, url) {
      if (!link || !url) {
        return true;
      }
      if (link.getAttribute('data-eep-no-spa') === '1' || link.closest('[data-eep-no-spa="1"]')) {
        return true;
      }
      if (link.closest('.eep-account-tabs[data-eep-ajax="1"]')) {
        return true;
      }
      if (link.target && String(link.target).toLowerCase() === '_blank') {
        return true;
      }
      if (link.hasAttribute('download')) {
        return true;
      }
      if (url.origin !== window.location.origin) {
        return true;
      }
      if (url.protocol !== 'http:' && url.protocol !== 'https:') {
        return true;
      }
      if (url.pathname.indexOf('/wp-admin/') === 0 || /\/wp-login\.php$/i.test(url.pathname)) {
        return true;
      }
      if (url.searchParams.get('action') === 'logout') {
        return true;
      }
      if (url.hash && normalizePathname(url.pathname) === normalizePathname(window.location.pathname) && url.search === window.location.search) {
        return true;
      }
      return false;
    };

    const canHandleSpaLink = function (link, url) {
      const targetPath = normalizePathname(url.pathname);
      if (spaRouteSet && spaRouteSet.has(targetPath)) {
        return true;
      }
      return false;
    };

    const describeTarget = function (targetUrl) {
      try {
        const parsed = new URL(String(targetUrl || ''), window.location.origin);
        return normalizePathname(parsed.pathname) + String(parsed.search || '');
      } catch (err) {
        return String(targetUrl || '').trim();
      }
    };

    const hydrateSpaView = function (html, targetUrl, options) {
      const parser = new window.DOMParser();
      const doc = parser.parseFromString(String(html || ''), 'text/html');
      const nextResolved = resolveSpaRoot(spaSelector, doc);
      const currentResolved = resolveSpaRoot(spaSelector, document);
      if (!nextResolved || !currentResolved) {
        return false;
      }
      const nextRoot = nextResolved.node;
      const currentRoot = currentResolved.node;

      const importedRoot = document.importNode(nextRoot, true);
      currentRoot.replaceWith(importedRoot);
      importedRoot.classList.add('eep-spa-enter');
      window.setTimeout(function () {
        importedRoot.classList.remove('eep-spa-enter');
      }, 260);

      const nextTitle = doc.querySelector('title');
      const nextTitleText = nextTitle ? String(nextTitle.textContent || '').trim() : '';
      if (nextTitleText) {
        document.title = nextTitleText;
      }

      if (!options || options.push !== false) {
        window.history.pushState({ eepSpa: true }, '', targetUrl);
      }

      if (!options || options.scroll !== false) {
        window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
      }

      document.body.classList.remove('eep-map-filters-open');
      runFrontendInitializers();
      try {
        document.dispatchEvent(new CustomEvent('eep:spa:navigated', {
          detail: {
            url: String(targetUrl || ''),
            selector: String(spaSelector || '')
          }
        }));
      } catch (error) {
        // Ignore event dispatch failures in very old browsers.
      }

      try {
        const parsed = new URL(String(targetUrl || ''), window.location.origin);
        if (spaRouteSet) {
          spaRouteSet.add(normalizePathname(parsed.pathname));
        }
      } catch (err) {
        // ignore parse failures
      }
      return true;
    };

    const navigateSpa = function (targetUrl, options) {
      const requestUrl = String(targetUrl || '').trim();
      if (!requestUrl) {
        return;
      }
      const targetLabel = describeTarget(requestUrl);

      if (spaActiveController) {
        spaActiveController.abort();
      }
      spaActiveController = new AbortController();
      const requestId = ++spaRequestSeq;
      const loadingMessage = String(getMessage('spaLoading', 'Loading page...') || 'Loading page...');
      const failMessage = String(getMessage('spaLoadFailed', 'Could not load that page. Opening full page view.') || 'Could not load that page. Opening full page view.');
      setSpaFeedbackStatus('SPA navigation in progress', 'warn', targetLabel);

      let loadingShown = false;
      const loadingTimer = window.setTimeout(function () {
        loadingShown = true;
        setLoadingState(true, requestUrl);
        announce(loadingMessage);
      }, 120);
      const clearLoading = function () {
        window.clearTimeout(loadingTimer);
        if (loadingShown) {
          setLoadingState(false, requestUrl);
          announce('');
        }
      };

      fetch(requestUrl, {
        credentials: 'same-origin',
        signal: spaActiveController.signal,
        headers: {
          'X-Requested-With': 'XMLHttpRequest'
        }
      })
        .then(function (res) {
          if (!res.ok) {
            throw new Error('http_' + String(res.status || 0));
          }
          return res.text();
        })
        .then(function (html) {
          if (requestId !== spaRequestSeq) {
            return;
          }
          const hydrated = hydrateSpaView(html, requestUrl, options || {});
          if (!hydrated) {
            queueSpaFeedbackForNextLoad({
              tone: 'error',
              message: 'SPA fallback to full page load',
              detail: 'Could not locate SPA container in fetched response',
              target: targetLabel
            });
            window.location.href = requestUrl;
            return;
          }
          setSpaFeedbackStatus('SPA navigation completed', 'ok', targetLabel);
        })
        .catch(function (error) {
          if (error && error.name === 'AbortError') {
            return;
          }
          queueSpaFeedbackForNextLoad({
            tone: 'error',
            message: 'SPA request failed, full page load used',
            detail: targetLabel
          });
          showToast(failMessage, 'warning', {
            title: 'Notice',
            subtitle: 'Switching to full page load'
          });
          setSpaFeedbackStatus('SPA request failed', 'error', targetLabel);
          window.location.href = requestUrl;
        })
        .finally(function () {
          if (requestId !== spaRequestSeq) {
            return;
          }
          clearLoading();
          spaActiveController = null;
        });
    };

    document.addEventListener('click', function (event) {
      if (!event || event.defaultPrevented) {
        return;
      }
      if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
        return;
      }
      const link = event.target && event.target.closest ? event.target.closest('a[href]') : null;
      if (!link) {
        return;
      }

      let url;
      try {
        const href = String(link.getAttribute('href') || '').trim();
        if (!href || href.indexOf('javascript:') === 0) {
          return;
        }
        url = new URL(href, window.location.href);
      } catch (err) {
        return;
      }
      if (shouldSkipSpaLink(link, url)) {
        return;
      }
      if (!canHandleSpaLink(link, url)) {
        queueSpaFeedbackForNextLoad({
          tone: 'warn',
          message: 'Full page load (route not registered for SPA)',
          detail: describeTarget(url.toString()),
          target: url.toString()
        });
        return;
      }

      event.preventDefault();
      navigateSpa(url.toString(), { push: true, scroll: true });
    });

    window.addEventListener('popstate', function () {
      let parsed;
      try {
        parsed = new URL(window.location.href, window.location.origin);
      } catch (err) {
        window.location.reload();
        return;
      }
      const targetPath = normalizePathname(parsed.pathname);
      if (!spaRouteSet || !spaRouteSet.has(targetPath)) {
        queueSpaFeedbackForNextLoad({
          tone: 'warn',
          message: 'Back/forward triggered full page load',
          detail: targetPath + String(parsed.search || ''),
          target: window.location.href
        });
        window.location.reload();
        return;
      }
      navigateSpa(window.location.href, { push: false, scroll: true });
    });
  }

  function initAuthTabs() {
    const shell = document.querySelector('.eep-auth-shell');
    if (!shell) {
      return;
    }
    if (shell.getAttribute('data-eep-bound') === '1') {
      return;
    }
    shell.setAttribute('data-eep-bound', '1');

    const tabs = Array.from(shell.querySelectorAll('[data-eep-auth-tab]'));
    const panels = Array.from(shell.querySelectorAll('[data-eep-auth-panel]'));
    if (!tabs.length || !panels.length) {
      return;
    }

    const activate = function (key) {
      tabs.forEach((tab) => {
        const isActive = String(tab.getAttribute('data-eep-auth-tab') || '') === key;
        tab.classList.toggle('is-active', isActive);
        tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
      });
      panels.forEach((panel) => {
        const isActive = String(panel.getAttribute('data-eep-auth-panel') || '') === key;
        panel.classList.toggle('is-active', isActive);
        if (isActive) {
          panel.removeAttribute('hidden');
        } else {
          panel.setAttribute('hidden', 'hidden');
        }
      });
    };

    tabs.forEach((tab) => {
      tab.addEventListener('click', function () {
        const key = String(tab.getAttribute('data-eep-auth-tab') || '').trim();
        if (!key) {
          return;
        }
        activate(key);
      });
    });

    const initial = String(shell.getAttribute('data-eep-default-tab') || '').trim();
    const allowed = ['login', 'signup', 'forgot', 'reset'];
    activate(allowed.indexOf(initial) >= 0 ? initial : 'login');
  }

  function normalizeMapDateLabel(event) {
    const shortLabel = String((event && event.date_short) || '').trim();
    if (shortLabel) {
      return shortLabel;
    }

    const startRaw = String((event && event.start) || '').trim();
    if (startRaw) {
      const isoCandidate = startRaw.includes('T') ? startRaw : startRaw.replace(' ', 'T');
      const timestamp = Date.parse(isoCandidate);
      if (!Number.isNaN(timestamp)) {
        const dateText = new Date(timestamp).toLocaleDateString([], {
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        });
        const timeText = new Date(timestamp).toLocaleTimeString([], {
          hour: 'numeric',
          minute: '2-digit'
        }).toLowerCase();
        return (dateText + ' • ' + timeText).trim();
      }
    }

    const fallback = String((event && event.date) || '').trim();
    if (!fallback) {
      return '';
    }
    return fallback
      .replace(/\s*\(.*\)\s*$/u, '')
      .replace(/\s*-\s*.*$/u, '')
      .replace(/\s{2,}/gu, ' ')
      .trim();
  }

  function decodeHtmlEntities(value) {
    const raw = String(value || '');
    if (!raw || raw.indexOf('&') === -1) {
      return raw;
    }
    let decoded = raw;
    const decoder = document.createElement('textarea');
    for (let index = 0; index < 3; index += 1) {
      decoder.innerHTML = decoded;
      const next = String(decoder.value || decoded);
      if (!next || next === decoded) {
        break;
      }
      decoded = next;
      if (decoded.indexOf('&') === -1) {
        break;
      }
    }
    return decoded
      .replace(/&#8211;?/gi, '–')
      .replace(/&ndash;/gi, '–');
  }

  function normalizeMapTitle(value) {
    return decodeHtmlEntities(value).replace(/\s+/g, ' ').trim();
  }

  function normalizeMapLocationLabel(event) {
    const city = String((event && event.city) || '').trim();
    if (city) {
      return decodeHtmlEntities(city);
    }
    const raw = String((event && event.location) || '').trim();
    if (!raw) {
      return '';
    }
    const parts = raw.split(',').map((part) => String(part || '').trim()).filter(Boolean);
    if (parts.length >= 3) {
      return decodeHtmlEntities(parts[parts.length - 2]);
    }
    if (parts.length >= 1) {
      return decodeHtmlEntities(parts[0]);
    }
    return decodeHtmlEntities(raw);
  }

  const MAP_RESULTS_LIST_SELECTOR = '[data-map-results-list], #eep-map-list';
  const MAP_LOAD_MORE_WRAP_SELECTOR = '[data-map-load-more-wrap], #eep-map-load-more-wrap';
  const MAP_LOAD_MORE_BUTTON_SELECTOR = '[data-map-load-more], #eep-map-load-more';

  function getMapListElement() {
    return $(MAP_RESULTS_LIST_SELECTOR).first();
  }

  function renderMapList(items) {
    const $list = getMapListElement();
    if (!$list.length) {
      return;
    }
    $list.attr('role', 'list');
    $list.attr('aria-live', 'polite');
    $list.removeAttr('aria-busy');
    $list.empty();

    if (!items.length) {
      $list.append($('<p>', {
        class: 'col-span-full m-0 rounded-xl border border-gray-200 bg-white p-4 text-sm text-gray-500',
        text: getMessage('noMapResults', 'No matching results found.')
      }));
      return;
    }

    const fallbackThumb = String(getConfig('fallbackThumbUrl', '') || '').trim();

    items.forEach((event) => {
      const isBoosted = String(event.boosted || '') === '1' || event.boosted === 1 || event.boosted === true;
      const hasVerified = String(event.verified_badge || '') === '1' || event.verified_badge === 1 || event.verified_badge === true;
      const boostHighlight = String(event.boost_highlight || '') === '1' || event.boost_highlight === 1 || event.boost_highlight === true;
      const $item = $('<article>', {
        class: 'map-card group flex min-w-0 flex-col overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm transition duration-200 hover:-translate-y-0.5 hover:border-orange-300 hover:shadow-lg focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-orange-500'
          + (isBoosted && boostHighlight ? ' map-card--boosted ring-1 ring-orange-200' : '')
          + (hasVerified ? ' map-card--verified ring-1 ring-teal-200' : ''),
        role: 'listitem',
        tabindex: 0
      });
      const eventTitle = normalizeMapTitle(event.title || '');
      const eventUrl = String(event.url || '').trim();
      if (eventUrl) {
        $item.attr('data-eep-card-url', eventUrl);
        $item.addClass('cursor-pointer');
      }
      const thumbSrc = String(event.cover || '').trim() || fallbackThumb;
      if (thumbSrc) {
        const $thumb = $('<div>', { class: 'map-card-thumb relative h-44 w-full overflow-hidden bg-slate-100', 'aria-label': eventTitle });
        $thumb.append($('<img>', {
          src: thumbSrc,
          alt: event.title || '',
          loading: 'lazy',
          decoding: 'async',
          fetchpriority: 'low',
          class: 'h-full w-full object-cover transition duration-500 group-hover:scale-105'
        }));
        $item.append($thumb);
      } else {
        const initial = eventTitle ? eventTitle.charAt(0).toUpperCase() : 'E';
        const $thumbFallback = $('<div>', {
          class: 'map-card-thumb map-card-thumb--placeholder flex h-44 w-full items-center justify-center bg-slate-100 text-3xl font-bold text-slate-500',
          'aria-label': eventTitle
        });
        $thumbFallback.append($('<span>', { text: initial }));
        $item.append($thumbFallback);
      }

      const $body = $('<div>', { class: 'map-card-body flex min-h-0 flex-1 flex-col gap-2 p-4' });
      if (isBoosted) {
        $body.append($('<span>', {
          class: 'map-card-badge map-card-badge--featured inline-flex w-fit items-center rounded-full border border-orange-200 bg-orange-50 px-2.5 py-1 text-[10px] font-semibold uppercase tracking-wide text-orange-700',
          text: String(event.boost_badge || getConfig('boostBadgeLabel', 'Featured'))
        }));
      }
      if (hasVerified) {
        $body.append($('<span>', {
          class: 'map-card-badge map-card-badge--verified inline-flex w-fit items-center rounded-full border border-teal-200 bg-teal-50 px-2.5 py-1 text-[10px] font-semibold uppercase tracking-wide text-teal-700',
          text: getMessage('verifiedLabel', 'Verified')
        }));
      }
      const $title = $('<h4>', { class: 'map-card-title m-0 text-base font-semibold leading-snug text-slate-900 break-words', text: eventTitle });
      $body.append($title);
      const eventDate = normalizeMapDateLabel(event);
      const eventLocation = normalizeMapLocationLabel(event);
      if (eventDate) {
        $body.append($('<p>', { class: 'map-card-date m-0 text-sm leading-snug text-slate-500', text: eventDate }));
      }
      if (eventLocation) {
        $body.append($('<p>', { class: 'map-card-location m-0 text-sm leading-snug text-slate-500', text: eventLocation }));
      }
      if (eventUrl) {
        $body.append($('<a>', {
          class: 'map-card-view mt-auto inline-flex min-h-10 w-full items-center justify-center rounded-xl border border-slate-200 bg-slate-50 px-3 py-2 text-sm font-semibold text-slate-700 no-underline transition hover:border-orange-300 hover:bg-orange-50 hover:text-orange-700',
          href: eventUrl,
          text: getMessage('viewDetails', 'View Details')
        }));
      }
      $item.append($body);
      $list.append($item);
    });

    initScrollReveal();
  }

  function initMap() {
    const $mapEl = $('#eep-events-map');
    const $listEl = getMapListElement();
    const hasMapContainer = $mapEl.length > 0;
    const canUseLeaflet = typeof window.L !== 'undefined';
    if (!hasMapContainer && !$listEl.length) {
      return;
    }
    if (hasMapContainer && !canUseLeaflet) {
      const $mapPanel = $mapEl.closest('[data-eep-map-panel]');
      if ($mapPanel.length) {
        $mapPanel.attr('hidden', 'hidden');
      } else {
        $mapEl.attr('hidden', 'hidden');
      }
    }

    const $layout = (hasMapContainer ? $mapEl.closest('[data-eep-map-layout="1"]') : $('[data-eep-map-layout="1"]').first());
    const filterPanel = document.getElementById('eep-map-sidebar-panel');
    const filterBackdrop = document.getElementById('eep-map-sidebar-backdrop');
    const filterOpenBtn = document.getElementById('eep-map-filters-open');
    const filterCloseBtn = document.getElementById('eep-map-filters-close');
    const filterApplyBtn = document.getElementById('eep-map-filters-apply');
    const mobileMediaQuery = window.matchMedia ? window.matchMedia('(max-width: 900px)') : null;
    const drawerMode = !!($layout.length && String($layout.attr('data-eep-mobile-filter-mode') || '').trim() === 'drawer');
    function isMobileFilterMode() {
      return !!(drawerMode && mobileMediaQuery && mobileMediaQuery.matches);
    }
    function closeFilterPanel(force) {
      if (!filterPanel) {
        return;
      }
      if (!drawerMode) {
        return;
      }
      if (!force && !isMobileFilterMode()) {
        return;
      }
      filterPanel.classList.remove('is-open');
      if (filterBackdrop) {
        filterBackdrop.classList.remove('is-open');
        filterBackdrop.setAttribute('hidden', 'hidden');
      }
      if (filterOpenBtn) {
        filterOpenBtn.setAttribute('aria-expanded', 'false');
      }
      filterPanel.setAttribute('aria-hidden', 'true');
      document.body.classList.remove('eep-map-filters-open');
    }
    function openFilterPanel() {
      if (!filterPanel || !isMobileFilterMode()) {
        return;
      }
      filterPanel.classList.add('is-open');
      filterPanel.setAttribute('aria-hidden', 'false');
      if (filterBackdrop) {
        filterBackdrop.removeAttribute('hidden');
        window.requestAnimationFrame(function () {
          filterBackdrop.classList.add('is-open');
        });
      }
      if (filterOpenBtn) {
        filterOpenBtn.setAttribute('aria-expanded', 'true');
      }
      document.body.classList.add('eep-map-filters-open');
    }
    function maybeCloseFilterPanel() {
      if (isMobileFilterMode()) {
        closeFilterPanel(true);
      }
    }
    if (filterPanel && filterPanel.getAttribute('data-eep-panel-bound') !== '1') {
      filterPanel.setAttribute('data-eep-panel-bound', '1');
      if (drawerMode) {
        filterPanel.setAttribute('aria-hidden', isMobileFilterMode() ? 'true' : 'false');
      }
      if (filterOpenBtn) {
        filterOpenBtn.addEventListener('click', function () {
          openFilterPanel();
        });
      }
      if (filterCloseBtn) {
        filterCloseBtn.addEventListener('click', function () {
          closeFilterPanel(true);
        });
      }
      if (filterBackdrop) {
        filterBackdrop.addEventListener('click', function () {
          closeFilterPanel(true);
        });
      }
      if (filterApplyBtn) {
        filterApplyBtn.addEventListener('click', function () {
          fetchEvents({ reset: true, closeFilters: true });
          closeFilterPanel(true);
        });
      }
      document.addEventListener('keydown', function (event) {
        if (event && event.key === 'Escape') {
          closeFilterPanel(true);
        }
      });
      if (mobileMediaQuery) {
        const mediaHandler = function (event) {
          if (!event.matches) {
            closeFilterPanel(true);
            if (filterPanel) {
              filterPanel.setAttribute('aria-hidden', 'false');
            }
          } else if (drawerMode && filterPanel) {
            filterPanel.setAttribute('aria-hidden', 'true');
          }
        };
        if (typeof mobileMediaQuery.addEventListener === 'function') {
          mobileMediaQuery.addEventListener('change', mediaHandler);
        } else if (typeof mobileMediaQuery.addListener === 'function') {
          mobileMediaQuery.addListener(mediaHandler);
        }
      }
    }
    const maxEvents = parseInt($layout.attr('data-eep-map-max-events') || '200', 10);
    const hasMap = hasMapContainer && canUseLeaflet;
    let map = null;
    let markerLayer = null;
    const mapFocusPreset = getMapFocusPreset();
    const defaultMapCenter = mapFocusPreset && Array.isArray(mapFocusPreset.center) && mapFocusPreset.center.length === 2
      ? { lat: Number(mapFocusPreset.center[0]), lng: Number(mapFocusPreset.center[1]) }
      : { lat: 20, lng: 0 };
    const defaultMapZoomRaw = mapFocusPreset ? Number(mapFocusPreset.zoom) : 2;
    const defaultMapZoom = Number.isFinite(defaultMapZoomRaw) ? defaultMapZoomRaw : 2;
    let mapFocusBounds = null;
    let mapFocusMinZoom = null;
    const pinUrl = String(getConfig('mapPinUrl', '') || '').trim();
    const markerOptions = {};
    let currentCenter = { lat: defaultMapCenter.lat, lng: defaultMapCenter.lng };

    function applyMapFocusConstraints(forceCenter) {
      if (!hasMap || !map || !mapFocusPreset || typeof window.L === 'undefined' || !Array.isArray(mapFocusPreset.bounds) || mapFocusPreset.bounds.length !== 2) {
        return;
      }
      if (!mapFocusBounds) {
        mapFocusBounds = L.latLngBounds(mapFocusPreset.bounds);
      }
      if (!mapFocusBounds || (typeof mapFocusBounds.isValid === 'function' && !mapFocusBounds.isValid())) {
        return;
      }

      if (typeof map.setMaxBounds === 'function') {
        map.setMaxBounds(mapFocusBounds);
      }
      if (typeof map.getBoundsZoom === 'function') {
        const minZoomCandidate = map.getBoundsZoom(mapFocusBounds, false);
        if (Number.isFinite(minZoomCandidate)) {
          mapFocusMinZoom = Math.max(1, Math.floor(minZoomCandidate));
          if (typeof map.setMinZoom === 'function') {
            map.setMinZoom(mapFocusMinZoom);
          }
          if (typeof map.getZoom === 'function' && typeof map.setZoom === 'function' && map.getZoom() < mapFocusMinZoom) {
            map.setZoom(mapFocusMinZoom);
          }
        }
      }
      if (forceCenter === true) {
        if (typeof map.panInsideBounds === 'function') {
          map.panInsideBounds(mapFocusBounds, { animate: false });
        }
      }
    }
    if (pinUrl && hasMap) {
      markerOptions.icon = L.icon({
        iconUrl: pinUrl,
        iconSize: [36, 48],
        iconAnchor: [18, 46],
        popupAnchor: [0, -42]
      });
    }
    if (hasMap) {
      const mapOptions = {};
      if (mapFocusPreset) {
        mapOptions.maxBoundsViscosity = 1.0;
      }
      map = L.map('eep-events-map', mapOptions).setView([defaultMapCenter.lat, defaultMapCenter.lng], defaultMapZoom);
      const mapContainer = map.getContainer ? map.getContainer() : document.getElementById('eep-events-map');
      if (mapContainer && mapContainer.getAttribute('data-eep-kbd-bound') !== '1') {
        mapContainer.setAttribute('data-eep-kbd-bound', '1');
        mapContainer.setAttribute('tabindex', mapContainer.getAttribute('tabindex') || '0');
        mapContainer.setAttribute('role', mapContainer.getAttribute('role') || 'region');
        if (!mapContainer.getAttribute('aria-label')) {
          mapContainer.setAttribute('aria-label', String(getMessage('mapAriaLabel', 'Locations map')));
        }
        mapContainer.addEventListener('keydown', function (event) {
          if (!event) {
            return;
          }
          const key = String(event.key || '');
          if (key === 'ArrowUp') {
            event.preventDefault();
            map.panBy([0, -120]);
            return;
          }
          if (key === 'ArrowDown') {
            event.preventDefault();
            map.panBy([0, 120]);
            return;
          }
          if (key === 'ArrowLeft') {
            event.preventDefault();
            map.panBy([-120, 0]);
            return;
          }
          if (key === 'ArrowRight') {
            event.preventDefault();
            map.panBy([120, 0]);
            return;
          }
          if (key === '+' || key === '=' || key === 'Add') {
            event.preventDefault();
            map.zoomIn();
            return;
          }
          if (key === '-' || key === '_' || key === 'Subtract') {
            event.preventDefault();
            map.zoomOut();
            return;
          }
          if (key === 'Home') {
            event.preventDefault();
            map.setView([defaultMapCenter.lat, defaultMapCenter.lng], defaultMapZoom);
            applyMapFocusConstraints(true);
          }
        });
      }
      const tileConfig = buildMapTileLayerConfig();
      L.tileLayer(tileConfig.url, tileConfig.options || {}).addTo(map);
      applyMapFocusConstraints(true);
      markerLayer = (typeof L.markerClusterGroup === 'function') ? L.markerClusterGroup() : L.layerGroup();
      markerLayer.addTo(map);
    }

    let hasInitialBounds = false;
    let fetchTimer = null;
    let loading = false;
    let currentPage = 1;
    let hasMorePages = false;
    let aggregatedEvents = [];
    const perPageRaw = parseInt(getConfig('mapResultsPerPage', 120), 10);
    const perPage = Number.isFinite(perPageRaw) ? Math.max(12, Math.min(250, perPageRaw)) : 120;
    const $mapList = getMapListElement();
    const mobileSliderContainer = document.getElementById('eep-mobile-slider-container');
    let $loadMoreWrap = $(MAP_LOAD_MORE_WRAP_SELECTOR).first();
    let markerEntries = [];
    let activeMobileEventId = null;
    let mobileSliderProgrammaticScrollUntil = 0;
    let mobileSliderScrollRaf = 0;
    let suppressMapMoveFetchUntil = 0;
    let mapFocusAnimationToken = 0;
    let mobileSliderLoopState = {
      enabled: false,
      loopSize: 0,
      realCount: 0
    };
    if (!$loadMoreWrap.length && $mapList.length) {
      $loadMoreWrap = $('<div>', {
        id: 'map-load-more-wrap',
        class: 'mt-4 flex justify-center',
        'data-map-load-more-wrap': '1',
        hidden: 'hidden'
      });
      $loadMoreWrap.append($('<button>', {
        type: 'button',
        id: 'map-load-more',
        'data-map-load-more': '1',
        class: 'inline-flex min-w-[190px] items-center justify-center rounded-xl border border-gray-300 bg-white px-4 py-2.5 text-sm font-semibold text-gray-700 transition hover:bg-gray-50',
        text: getMessage('mapLoadMore', 'Load more results')
      }));
      $mapList.after($loadMoreWrap);
    }
    const $loadMoreBtn = $(MAP_LOAD_MORE_BUTTON_SELECTOR).first();

    function resetMobileSliderLoopState() {
      mobileSliderLoopState = {
        enabled: false,
        loopSize: 0,
        realCount: 0
      };
    }

    function getMobileSliderCards() {
      if (!mobileSliderContainer) {
        return [];
      }
      return Array.prototype.slice.call(
        mobileSliderContainer.querySelectorAll('[data-eep-mobile-card-index]')
      );
    }

    function getSliderContainerCenter() {
      if (!mobileSliderContainer) {
        return 0;
      }
      return mobileSliderContainer.scrollLeft + (mobileSliderContainer.clientWidth / 2);
    }

    function getCardCenter(card) {
      if (!card) {
        return 0;
      }
      return card.offsetLeft + (card.offsetWidth / 2);
    }

    function getClosestCardToCenter(cards) {
      const list = Array.isArray(cards) ? cards : [];
      if (!list.length) {
        return null;
      }
      const centerX = getSliderContainerCenter();
      let bestCard = null;
      let bestDistance = Number.POSITIVE_INFINITY;
      list.forEach(function (card) {
        const distance = Math.abs(getCardCenter(card) - centerX);
        if (distance < bestDistance) {
          bestDistance = distance;
          bestCard = card;
        }
      });
      return bestCard;
    }

    function centerMobileSliderOnCard(card, smooth) {
      if (!mobileSliderContainer || !card) {
        return;
      }
      const targetLeft = card.offsetLeft - Math.max(0, ((mobileSliderContainer.clientWidth - card.offsetWidth) / 2));
      if (smooth && typeof mobileSliderContainer.scrollTo === 'function') {
        mobileSliderContainer.scrollTo({
          left: targetLeft,
          behavior: 'smooth'
        });
        return;
      }
      mobileSliderContainer.scrollLeft = targetLeft;
    }

    function maybeRecenterMobileSliderLoop() {
      if (!mobileSliderContainer || !mobileSliderLoopState.enabled || mobileSliderLoopState.realCount < 2) {
        return false;
      }
      const cards = getMobileSliderCards();
      if (!cards.length) {
        return false;
      }
      const centeredCard = getClosestCardToCenter(cards);
      if (!centeredCard) {
        return false;
      }
      const centeredIndex = parseInt(centeredCard.getAttribute('data-eep-mobile-card-index') || '', 10);
      if (!Number.isFinite(centeredIndex)) {
        return false;
      }
      const firstRealIndex = mobileSliderLoopState.loopSize;
      const afterRealIndex = firstRealIndex + mobileSliderLoopState.realCount;
      let targetIndex = null;
      if (centeredIndex < firstRealIndex) {
        targetIndex = centeredIndex + mobileSliderLoopState.realCount;
      } else if (centeredIndex >= afterRealIndex) {
        targetIndex = centeredIndex - mobileSliderLoopState.realCount;
      }
      if (!Number.isFinite(targetIndex) || targetIndex < 0 || targetIndex >= cards.length) {
        return false;
      }
      const targetCard = cards[targetIndex];
      if (!targetCard) {
        return false;
      }
      mobileSliderProgrammaticScrollUntil = Date.now() + 140;
      centerMobileSliderOnCard(targetCard, false);
      return true;
    }

    function setActiveMobileSliderCard(eventId, options) {
      if (!mobileSliderContainer) {
        return;
      }
      const opts = options && typeof options === 'object' ? options : {};
      const shouldScrollIntoView = opts.scrollIntoView !== false;
      const idText = String(eventId || '').trim();
      activeMobileEventId = idText || null;
      mobileSliderContainer.querySelectorAll('[data-eep-mobile-event-id]').forEach(function (card) {
        const isActive = idText !== '' && String(card.getAttribute('data-eep-mobile-event-id') || '') === idText;
        card.classList.toggle('ring-2', isActive);
        card.classList.toggle('ring-brand-400', isActive);
        card.classList.toggle('border-brand-300', isActive);
      });
      if (!idText) {
        return;
      }
      const matchingCards = Array.prototype.slice.call(
        mobileSliderContainer.querySelectorAll('[data-eep-mobile-event-id="' + idText + '"]')
      );
      const activeCard = getClosestCardToCenter(matchingCards) || matchingCards[0] || null;
      if (shouldScrollIntoView && activeCard) {
        mobileSliderProgrammaticScrollUntil = Date.now() + 520;
        centerMobileSliderOnCard(activeCard, true);
      }
    }

    function animateMapFocusToEntry(entry, options) {
      if (!hasMap || !map || !entry || !entry.marker || typeof entry.marker.getLatLng !== 'function') {
        return;
      }
      const opts = options && typeof options === 'object' ? options : {};
      const token = ++mapFocusAnimationToken;
      const markerLatLng = entry.marker.getLatLng();
      if (!markerLatLng) {
        return;
      }
      const currentZoom = (typeof map.getZoom === 'function') ? Number(map.getZoom()) : 10;
      const mapMinZoom = (typeof map.getMinZoom === 'function') ? Number(map.getMinZoom()) : 1;
      const effectiveMinZoom = Number.isFinite(mapMinZoom) ? mapMinZoom : 1;
      const detailZoomRaw = Number(getConfig('mapDetailFocusZoom', 12));
      const detailZoom = Number.isFinite(detailZoomRaw) ? Math.max(effectiveMinZoom, detailZoomRaw) : Math.max(effectiveMinZoom, 12);
      const zoomOutBy = 1.5;
      const zoomOutTarget = Math.max(effectiveMinZoom, Math.min(currentZoom, detailZoom) - zoomOutBy);
      const shouldOpenPopup = opts.openPopup === true;
      suppressMapMoveFetchUntil = Date.now() + 1800;
      if (typeof map.closePopup === 'function') {
        map.closePopup();
      }
      if (typeof map.flyTo === 'function') {
        map.flyTo(map.getCenter(), zoomOutTarget, {
          animate: true,
          duration: 0.22,
          easeLinearity: 0.45
        });
      } else if (typeof map.setView === 'function') {
        map.setView(map.getCenter(), zoomOutTarget, { animate: true });
      }
      window.setTimeout(function () {
        if (token !== mapFocusAnimationToken) {
          return;
        }
        if (typeof map.flyTo === 'function') {
          map.flyTo(markerLatLng, detailZoom, {
            animate: true,
            duration: 0.42,
            easeLinearity: 0.3
          });
        } else if (typeof map.setView === 'function') {
          map.setView(markerLatLng, detailZoom, { animate: true });
        }
        if (shouldOpenPopup && typeof entry.marker.openPopup === 'function') {
          window.setTimeout(function () {
            if (token !== mapFocusAnimationToken) {
              return;
            }
            entry.marker.openPopup();
          }, 180);
        }
      }, 150);
    }

    function focusMarkerEntry(entry, options) {
      if (!entry || !entry.event) {
        return;
      }
      const opts = options && typeof options === 'object' ? options : {};
      const idText = String((entry.id != null ? entry.id : (entry.event && entry.event.id)) || '').trim();
      setActiveMobileSliderCard(idText);
      animateMapFocusToEntry(entry, opts);
    }

    function getCenteredMobileSliderEntry() {
      if (!mobileSliderContainer) {
        return null;
      }
      const centeredCard = getClosestCardToCenter(getMobileSliderCards());
      if (!centeredCard) {
        return null;
      }
      const bestId = String(centeredCard.getAttribute('data-eep-mobile-event-id') || '').trim();
      if (!bestId) {
        return null;
      }
      return markerEntries.find(function (entry) {
        const entryId = String((entry && (entry.id != null ? entry.id : (entry.event && entry.event.id))) || '').trim();
        return entryId === bestId;
      }) || null;
    }

    function bindMobileSliderFocusSync() {
      if (!mobileSliderContainer || mobileSliderContainer.getAttribute('data-eep-focus-sync-bound') === '1') {
        return;
      }
      mobileSliderContainer.setAttribute('data-eep-focus-sync-bound', '1');
      const clearProgrammaticWindow = function () {
        mobileSliderProgrammaticScrollUntil = 0;
      };
      mobileSliderContainer.addEventListener('touchstart', clearProgrammaticWindow, { passive: true });
      mobileSliderContainer.addEventListener('mousedown', clearProgrammaticWindow, { passive: true });
      const onScroll = function () {
        if (mobileSliderScrollRaf) {
          window.cancelAnimationFrame(mobileSliderScrollRaf);
        }
        mobileSliderScrollRaf = window.requestAnimationFrame(function () {
          const isProgrammatic = Date.now() < mobileSliderProgrammaticScrollUntil;
          if (isProgrammatic) {
            return;
          }
          if (maybeRecenterMobileSliderLoop()) {
            return;
          }
          if (hasMap && map && typeof map.closePopup === 'function') {
            map.closePopup();
          }
          const centeredEntry = getCenteredMobileSliderEntry();
          if (!centeredEntry) {
            return;
          }
          const centeredId = String((centeredEntry.id != null ? centeredEntry.id : (centeredEntry.event && centeredEntry.event.id)) || '').trim();
          if (!centeredId || centeredId === activeMobileEventId) {
            return;
          }
          activeMobileEventId = centeredId;
          setActiveMobileSliderCard(centeredId, { scrollIntoView: false });
          animateMapFocusToEntry(centeredEntry, { openPopup: false });
        });
      };
      mobileSliderContainer.addEventListener('scroll', onScroll, { passive: true });
    }

    function renderMobileSlider(entries) {
      if (!mobileSliderContainer) {
        return;
      }
      const rows = Array.isArray(entries) ? entries : [];
      mobileSliderContainer.innerHTML = '';
      resetMobileSliderLoopState();
      if (!rows.length) {
        const emptyCard = document.createElement('article');
        emptyCard.className = 'min-w-[84vw] sm:min-w-[320px] snap-center bg-white/95 shadow-lg rounded-2xl border border-gray-200 p-4';
        emptyCard.textContent = getMessage('noMapResults', 'No matching results found.');
        mobileSliderContainer.appendChild(emptyCard);
        return;
      }

      const fallbackThumb = String(getConfig('fallbackThumbUrl', '') || '').trim();
      let renderedRows = rows.slice();
      if (rows.length > 1) {
        const loopSize = Math.max(1, Math.min(3, rows.length));
        const lead = rows.slice(rows.length - loopSize);
        const trail = rows.slice(0, loopSize);
        renderedRows = lead.concat(rows, trail);
        mobileSliderLoopState = {
          enabled: true,
          loopSize: loopSize,
          realCount: rows.length
        };
      }

      renderedRows.forEach(function (entry, renderedIndex) {
        const event = entry && entry.event ? entry.event : null;
        if (!event) {
          return;
        }
        const eventId = String((entry.id != null ? entry.id : event.id) || '').trim();
        const eventTitle = normalizeMapTitle(event.title || '') || getMessage('untitledEvent', 'Untitled event');
        const eventDate = normalizeMapDateLabel(event);
        const eventLocation = normalizeMapLocationLabel(event);
        const thumbSrc = String(event.cover || '').trim() || fallbackThumb;

        const card = document.createElement('article');
        card.className = 'min-w-[84vw] sm:min-w-[320px] snap-center bg-white shadow-xl rounded-2xl overflow-hidden flex flex-row h-[130px] border border-gray-100 transition-all cursor-pointer';
        card.setAttribute('tabindex', '0');
        card.setAttribute('data-eep-mobile-card-index', String(renderedIndex));
        if (eventId) {
          card.setAttribute('data-eep-mobile-event-id', eventId);
        }
        if (
          mobileSliderLoopState.enabled &&
          (
            renderedIndex < mobileSliderLoopState.loopSize ||
            renderedIndex >= (mobileSliderLoopState.loopSize + mobileSliderLoopState.realCount)
          )
        ) {
          card.setAttribute('data-eep-mobile-clone', '1');
        }

        const mediaWrap = document.createElement('div');
        mediaWrap.className = 'w-2/5 h-full relative flex-shrink-0 bg-slate-100';
        if (thumbSrc) {
          const image = document.createElement('img');
          image.src = thumbSrc;
          image.alt = eventTitle;
          image.className = 'w-full h-full object-cover';
          image.loading = 'lazy';
          image.decoding = 'async';
          mediaWrap.appendChild(image);
        } else {
          const placeholder = document.createElement('div');
          placeholder.className = 'w-full h-full flex items-center justify-center text-3xl font-semibold text-slate-400';
          placeholder.textContent = eventTitle.charAt(0).toUpperCase();
          mediaWrap.appendChild(placeholder);
        }

        const content = document.createElement('div');
        content.className = 'w-3/5 p-3.5 flex flex-col justify-center';

        const title = document.createElement('h3');
        title.className = 'font-bold text-gray-900 text-[15px] leading-snug mb-1.5 line-clamp-2';
        title.textContent = eventTitle;
        content.appendChild(title);

        if (eventDate) {
          const date = document.createElement('p');
          date.className = 'text-[12px] mb-1 text-gray-500 truncate';
          date.textContent = eventDate;
          content.appendChild(date);
        }
        if (eventLocation) {
          const location = document.createElement('p');
          location.className = 'text-[12px] text-gray-500 truncate';
          location.textContent = eventLocation;
          content.appendChild(location);
        }

        card.appendChild(mediaWrap);
        card.appendChild(content);
        card.addEventListener('click', function () {
          focusMarkerEntry(entry, { openPopup: true });
        });
        card.addEventListener('keydown', function (eventObj) {
          if (!eventObj) {
            return;
          }
          if (eventObj.key === 'Enter' || eventObj.key === ' ') {
            eventObj.preventDefault();
            focusMarkerEntry(entry, { openPopup: true });
          }
        });

        mobileSliderContainer.appendChild(card);
      });

      if (mobileSliderLoopState.enabled) {
        const cards = getMobileSliderCards();
        const firstRealCard = cards[mobileSliderLoopState.loopSize] || null;
        if (firstRealCard) {
          centerMobileSliderOnCard(firstRealCard, false);
        }
      }

      if (activeMobileEventId) {
        setActiveMobileSliderCard(activeMobileEventId);
        const stillExists = !!mobileSliderContainer.querySelector('[data-eep-mobile-event-id="' + activeMobileEventId + '"]');
        if (!stillExists) {
          activeMobileEventId = null;
        }
      }
      if (!activeMobileEventId) {
        const initiallyCenteredEntry = getCenteredMobileSliderEntry() || rows[0] || null;
        if (initiallyCenteredEntry && initiallyCenteredEntry.event) {
          const firstId = String((initiallyCenteredEntry.id != null ? initiallyCenteredEntry.id : initiallyCenteredEntry.event.id) || '').trim();
          if (firstId) {
            activeMobileEventId = firstId;
            setActiveMobileSliderCard(firstId);
            animateMapFocusToEntry(initiallyCenteredEntry, { openPopup: false });
          }
        }
      }
      bindMobileSliderFocusSync();
    }

    function syncMobileSliderToMapBounds() {
      if (!mobileSliderContainer) {
        return;
      }
      renderMobileSlider(markerEntries);
    }

    function setLoading(state, preserveList) {
      loading = state;
      if (hasMapContainer) {
        $mapEl.toggleClass('opacity-70', state);
      }
      if ($mapList.length) {
        if (state) {
          $mapList.attr('aria-busy', 'true');
          if (preserveList !== true) {
            $mapList.empty().append($('<p>', {
              class: 'col-span-full m-0 rounded-xl border border-gray-200 bg-white p-4 text-sm text-gray-500',
              text: getMessage('mapLoading', 'Loading events...')
            }));
          }
        } else {
          $mapList.removeAttr('aria-busy');
        }
      }
      if ($loadMoreBtn.length) {
        $loadMoreBtn.prop('disabled', state).toggleClass('opacity-60 cursor-not-allowed', state);
      }
    }

    function updateLoadMoreUi() {
      if (!$loadMoreWrap.length) {
        return;
      }
      if (hasMorePages && aggregatedEvents.length > 0) {
        $loadMoreWrap.removeAttr('hidden');
        if ($loadMoreBtn.length) {
          $loadMoreBtn.text(getMessage('mapLoadMore', 'Load more results'));
        }
        return;
      }
      $loadMoreWrap.attr('hidden', 'hidden');
      if ($loadMoreBtn.length && aggregatedEvents.length > 0) {
        $loadMoreBtn.text(getMessage('mapLoadMoreDone', 'No more results'));
      }
    }

    function collectFilters() {
      return {
        s: ($('#eep-map-search').val() || '').toString().trim(),
        type: ($('#eep-map-type').val() || '').toString().trim(),
        mode: ($('#eep-map-mode').val() || '').toString().trim(),
        tag: ($('#eep-map-tag').val() || '').toString().trim(),
        price: ($('#eep-map-price').val() || '').toString().trim(),
        official: ($('#eep-map-official').val() || '').toString().trim(),
        dateFrom: ($('#eep-map-date').val() || '').toString().trim(),
        distanceKm: ($('#eep-map-distance').val() || '').toString().trim()
      };
    }

    function buildMapRequestUrl(requestPage) {
      const base = String(getConfig('mapRestUrl', '') || '');
      if (!base) {
        return '';
      }
      const bounds = hasMap && map ? map.getBounds() : null;
      const center = hasMap && map ? map.getCenter() : currentCenter;
      const filters = collectFilters();
      const params = new URLSearchParams();
      if (bounds) {
        params.set('north', bounds.getNorth().toFixed(6));
        params.set('south', bounds.getSouth().toFixed(6));
        params.set('east', bounds.getEast().toFixed(6));
        params.set('west', bounds.getWest().toFixed(6));
        currentCenter = { lat: center.lat, lng: center.lng };
      } else {
        params.set('north', '85.000000');
        params.set('south', '-85.000000');
        params.set('east', '180.000000');
        params.set('west', '-180.000000');
      }
      params.set('max_events', String(Number.isFinite(maxEvents) ? Math.max(1, maxEvents) : 200));
      params.set('page', String(Math.max(1, parseInt(requestPage, 10) || 1)));
      params.set('per_page', String(perPage));
      if (filters.s) params.set('s', filters.s);
      if (filters.type) params.set('event_type', filters.type);
      if (filters.mode) params.set('event_mode', filters.mode);
      if (filters.tag) params.set('event_tag', filters.tag);
      if (filters.price) params.set('event_price', filters.price);
      if (filters.official) params.set('official', filters.official);
      if (filters.dateFrom) params.set('date_from', filters.dateFrom);
      if (filters.distanceKm) {
        const centerLatValue = Number(center && center.lat);
        const centerLngValue = Number(center && center.lng);
        const centerLat = Number.isFinite(centerLatValue) ? centerLatValue : Number(currentCenter.lat);
        const centerLng = Number.isFinite(centerLngValue) ? centerLngValue : Number(currentCenter.lng);
        params.set('distance_km', filters.distanceKm);
        params.set('center_lat', centerLat.toFixed(6));
        params.set('center_lng', centerLng.toFixed(6));
      }
      return base + (base.indexOf('?') === -1 ? '?' : '&') + params.toString();
    }

    function mergeEvents(existing, incoming) {
      const byId = {};
      (Array.isArray(existing) ? existing : []).forEach(function (event) {
        const id = parseInt(event && event.id, 10);
        if (Number.isFinite(id) && id > 0) {
          byId[id] = event;
        }
      });
      (Array.isArray(incoming) ? incoming : []).forEach(function (event) {
        const id = parseInt(event && event.id, 10);
        if (Number.isFinite(id) && id > 0) {
          byId[id] = event;
        }
      });
      return Object.keys(byId).map(function (key) {
        return byId[key];
      });
    }

    function drawEvents(events) {
      if (markerLayer && typeof markerLayer.clearLayers === 'function') {
        markerLayer.clearLayers();
      }
      markerEntries = [];
      activeMobileEventId = null;
      const list = [];

      events.forEach((event) => {
        const lat = parseFloat(event.lat);
        const lng = parseFloat(event.lng);
        const eventId = parseInt(event && event.id, 10);
        const eventTitle = normalizeMapTitle(event && event.title ? event.title : '');
        if (hasMap) {
          if (!Number.isFinite(lat) || !Number.isFinite(lng)) {
            return;
          }
          const marker = L.marker([lat, lng], markerOptions);
          const popupUrl = String(event.url || '#').trim() || '#';
          const popupThumb = String(event.cover || '').trim() || String(getConfig('fallbackThumbUrl', '') || '').trim();
          const popupLocation = normalizeMapLocationLabel(event);
          const popupDate = normalizeMapDateLabel(event);
          const popupTitle = eventTitle || getMessage('untitledEvent', 'Untitled event');
          const popupParts = ['<article class="eep-map-popup-card">'];
          if (popupThumb) {
            popupParts.push(
              '<a class="eep-map-popup-image-link" href="' + escapeHtml(popupUrl) + '">' +
              '<img class="eep-map-popup-image" src="' + escapeHtml(popupThumb) + '" alt="' + escapeHtml(popupTitle) + '" loading="lazy" decoding="async" fetchpriority="low">' +
              '</a>'
            );
          }
          popupParts.push('<div class="eep-map-popup-body">');
          popupParts.push('<h3 class="eep-map-popup-title"><a href="' + escapeHtml(popupUrl) + '">' + escapeHtml(popupTitle) + '</a></h3>');
          if (popupDate || popupLocation) {
            popupParts.push('<div class="eep-map-popup-meta-row">');
            if (popupDate) {
              popupParts.push('<span class="eep-map-popup-meta-chip">' + escapeHtml(popupDate) + '</span>');
            }
            if (popupLocation) {
              popupParts.push('<span class="eep-map-popup-meta-chip">' + escapeHtml(popupLocation) + '</span>');
            }
            popupParts.push('</div>');
          }
          popupParts.push('<a class="eep-map-popup-cta" href="' + escapeHtml(popupUrl) + '" style="color:#ffffff !important;">' + escapeHtml(getMessage('viewDetails', 'View Details')) + '</a>');
          popupParts.push('</div></article>');
          marker.bindPopup(popupParts.join(''), {
            className: 'eep-map-popup-frame',
            minWidth: 260,
            maxWidth: 360
          });
          const entry = {
            id: Number.isFinite(eventId) ? eventId : null,
            event: event,
            marker: marker
          };
          marker.on('popupopen', function () {
            const entryId = entry.id != null ? entry.id : (entry.event && entry.event.id);
            setActiveMobileSliderCard(entryId);
          });
          markerEntries.push(entry);
          if (markerLayer) {
            markerLayer.addLayer(marker);
          }
        }
        list.push(event);
      });

      renderMapList(list);
      syncMobileSliderToMapBounds();

      if (hasMap && list.length && !hasInitialBounds) {
        const bounds = markerLayer.getBounds && markerLayer.getBounds();
        if (bounds && bounds.isValid && bounds.isValid()) {
          map.fitBounds(bounds.pad(0.25));
          hasInitialBounds = true;
          window.requestAnimationFrame(syncMobileSliderToMapBounds);
        }
      }
    }

    function fetchEvents(options) {
      const opts = options && typeof options === 'object' ? options : {};
      const reset = opts.reset !== false;
      const closeFilters = opts.closeFilters === true;
      if (loading) {
        return;
      }
      if (reset) {
        currentPage = 1;
      }
      const requestPage = Math.max(1, parseInt(currentPage, 10) || 1);
      const url = buildMapRequestUrl(requestPage);
      if (!url) {
        return;
      }

      setLoading(true, !reset);
      fetch(url, {
        credentials: 'same-origin',
        headers: {
          'X-WP-Nonce': String(getConfig('restNonce', '') || '')
        }
      })
        .then(async (res) => {
          const payload = await res.json();
          if (!res.ok) {
            throw new Error((payload && payload.message) ? payload.message : getMessage('mapLoadFailed', 'Could not load map results.'));
          }
          const events = payload && Array.isArray(payload.events) ? payload.events : [];
          if (reset) {
            aggregatedEvents = events;
          } else {
            aggregatedEvents = mergeEvents(aggregatedEvents, events);
          }
          const pagination = payload && payload.pagination && typeof payload.pagination === 'object' ? payload.pagination : null;
          hasMorePages = !!(pagination && pagination.has_more === true);
          drawEvents(aggregatedEvents);
          updateLoadMoreUi();
        })
        .catch((err) => {
          showToast(err && err.message ? err.message : getMessage('mapLoadFailed', 'Could not load map results.'), 'error');
          if (reset) {
            aggregatedEvents = [];
            hasMorePages = false;
            drawEvents([]);
          }
          updateLoadMoreUi();
        })
        .finally(() => {
          setLoading(false, true);
          $('#eep-map-search-area').removeClass('ring-2 ring-orange-300 border-orange-300 text-orange-700 bg-orange-50');
          if (closeFilters) {
            maybeCloseFilterPanel();
          }
        });
    }

    function queueFetch(delay) {
      clearTimeout(fetchTimer);
      fetchTimer = window.setTimeout(function () {
        fetchEvents({ reset: true });
      }, delay || 250);
    }

    if ($('#eep-map-search').length) $('#eep-map-search').on('input', function () { queueFetch(300); });
    if ($('#eep-map-type').length) $('#eep-map-type').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-mode').length) $('#eep-map-mode').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-tag').length) $('#eep-map-tag').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-price').length) $('#eep-map-price').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-official').length) $('#eep-map-official').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-date').length) $('#eep-map-date').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });
    if ($('#eep-map-distance').length) $('#eep-map-distance').on('change', function () { queueFetch(200); maybeCloseFilterPanel(); });

    $('#eep-map-reset, #eep-map-reset-head').on('click', function () {
      const fields = ['#eep-map-search', '#eep-map-type', '#eep-map-mode', '#eep-map-tag', '#eep-map-price', '#eep-map-official', '#eep-map-date', '#eep-map-distance'];
      fields.forEach(function (selector) {
        const $field = $(selector);
        if ($field.length) {
          $field.val('');
        }
      });
      queueFetch(80);
      maybeCloseFilterPanel();
    });

    $('#eep-map-apply').on('click', function () {
      fetchEvents({ reset: true, closeFilters: true });
      maybeCloseFilterPanel();
    });

    if ($loadMoreBtn.length) {
      $loadMoreBtn.on('click', function () {
        if (loading || !hasMorePages) {
          return;
        }
        currentPage += 1;
        fetchEvents({ reset: false, closeFilters: true });
      });
    }

    $('#eep-map-use-location').on('click', function () {
      const $btn = $(this);
      if (!$btn.length) {
        return;
      }
      if (!navigator.geolocation || typeof navigator.geolocation.getCurrentPosition !== 'function') {
        showToast(getMessage('mapLocateUnsupported', 'Your browser does not support location services.'), 'error');
        return;
      }

      const originalText = String($btn.text() || '').trim() || getMessage('mapNearMeLabel', 'Find Near Me');
      $btn.prop('disabled', true).addClass('opacity-70 cursor-not-allowed').text(getMessage('mapLocateInProgress', 'Finding your location...'));
      navigator.geolocation.getCurrentPosition(function (pos) {
        if (!pos || !pos.coords) {
          showToast(getMessage('mapLocateDenied', 'Could not access your location.'), 'error');
          return;
        }
        const lat = parseFloat(pos.coords.latitude);
        const lng = parseFloat(pos.coords.longitude);
        if (!Number.isFinite(lat) || !Number.isFinite(lng)) {
          showToast(getMessage('mapLocateDenied', 'Could not access your location.'), 'error');
          return;
        }
        currentCenter = { lat: lat, lng: lng };
        hasInitialBounds = true;
        if (hasMap && map) {
          map.setView([lat, lng], 10);
          applyMapFocusConstraints(true);
        }
        queueFetch(150);
        maybeCloseFilterPanel();
      }, function () {
        showToast(getMessage('mapLocateDenied', 'Could not access your location.'), 'error');
      }, {
        enableHighAccuracy: true,
        maximumAge: 60000,
        timeout: 10000
      });

      window.setTimeout(function () {
        $btn.prop('disabled', false).removeClass('opacity-70 cursor-not-allowed').text(originalText);
      }, 1200);
    });

    $('#eep-map-search-area').on('click', function () {
      fetchEvents({ reset: true, closeFilters: true });
      maybeCloseFilterPanel();
    });

    if (hasMap && map) {
      map.on('moveend zoomend', function () {
        applyMapFocusConstraints(false);
        if (Date.now() < suppressMapMoveFetchUntil) {
          return;
        }
        if (getConfig('mapAutoSearch', true) === true) {
          queueFetch(500);
        } else {
          $('#eep-map-search-area').addClass('ring-2 ring-orange-300 border-orange-300 text-orange-700 bg-orange-50');
        }
      });

      // Handle map resize on window resize or potential tab switch
      window.addEventListener('resize', function() {
        map.invalidateSize();
        applyMapFocusConstraints(true);
      });
    }

    // Initial fetch
    fetchEvents({ reset: true });
  }

  function initGeocode() {
    const $btn = $('#eep-geocode-btn');
    if (!$btn.length) {
      return;
    }
    const $address = $('#eep-address-input');
    const $city = $('[name="eep_city"]');
    const $country = $('[name="eep_country"]');
    const $lat = $('#eep-lat-input');
    const $lng = $('#eep-lng-input');
    const endpoint = String(getConfig('geocodeRestUrl', '') || '');
    const suggestEndpoint = String(getConfig('addressSuggestRestUrl', '') || '');
    const btnDefaultText = ($btn.text() || '').trim() || 'Find Address On Map';
    let autoTimer = null;
    let lastAutoAddress = '';
    let suggestTimer = null;
    let suggestController = null;
    const suggestionMap = {};

    let previewMap = null;
    let previewMarker = null;
    const datalistId = 'eep-address-suggestions';
    let datalist = document.getElementById(datalistId);
    if (!datalist) {
      datalist = document.createElement('datalist');
      datalist.id = datalistId;
      document.body.appendChild(datalist);
    }
    if ($address.length) {
      $address.attr('list', datalistId);
    }

    function setSuggestions(items) {
      if (!datalist) {
        return;
      }
      datalist.innerHTML = '';
      Object.keys(suggestionMap).forEach(function (key) {
        delete suggestionMap[key];
      });
      (Array.isArray(items) ? items : []).forEach(function (item) {
        const label = String((item && item.label) || '').trim();
        if (!label) {
          return;
        }
        suggestionMap[label] = item;
        const option = document.createElement('option');
        option.value = label;
        datalist.appendChild(option);
      });
    }

    function fetchSuggestions(queryText) {
      const query = String(queryText || '').trim();
      if (query.length < 3 || !suggestEndpoint) {
        setSuggestions([]);
        return;
      }
      if (suggestController) {
        suggestController.abort();
      }
      suggestController = new AbortController();
      const qs = new URLSearchParams();
      qs.set('q', query);
      qs.set('limit', '6');
      fetch(suggestEndpoint + (suggestEndpoint.indexOf('?') === -1 ? '?' : '&') + qs.toString(), {
        credentials: 'same-origin',
        signal: suggestController.signal,
        headers: {
          'X-WP-Nonce': String(getConfig('restNonce', '') || '')
        }
      })
        .then(function (res) {
          return res.json().then(function (payload) {
            if (!res.ok) {
              throw new Error((payload && payload.message) ? payload.message : getMessage('addressSuggestFailed', 'Could not load address suggestions right now.'));
            }
            return payload;
          });
        })
        .then(function (payload) {
          setSuggestions(payload && Array.isArray(payload.suggestions) ? payload.suggestions : []);
        })
        .catch(function () {
          // silent - geocode still works without suggestions
        });
    }

    function updatePreviewMap(lat, lng) {
      if (typeof window.L === 'undefined' || !$('#eep-location-preview').length) {
        return;
      }
      const latNum = parseFloat(lat);
      const lngNum = parseFloat(lng);
      if (Number.isNaN(latNum) || Number.isNaN(lngNum)) {
        return;
      }
      if (!previewMap) {
        previewMap = L.map('eep-location-preview').setView([latNum, lngNum], 13);
        const previewTileConfig = buildMapTileLayerConfig();
        L.tileLayer(previewTileConfig.url, previewTileConfig.options || {}).addTo(previewMap);
        previewMarker = L.marker([latNum, lngNum], { draggable: true }).addTo(previewMap);
        previewMarker.on('dragend', function () {
          const pos = previewMarker.getLatLng();
          $lat.val(pos.lat.toFixed(6)).trigger('input');
          $lng.val(pos.lng.toFixed(6)).trigger('input');
        });
      } else {
        previewMap.setView([latNum, lngNum], 13);
        previewMarker.setLatLng([latNum, lngNum]);
      }
      window.setTimeout(function () {
        previewMap.invalidateSize();
      }, 50);
    }

    function applyGeocodePayload(payload) {
      if (!payload) {
        return;
      }
      if (Number.isFinite(parseFloat(payload.lat)) && Number.isFinite(parseFloat(payload.lng))) {
        $lat.val(String(payload.lat)).trigger('input');
        $lng.val(String(payload.lng)).trigger('input');
        updatePreviewMap(payload.lat, payload.lng);
      }
      if (payload.city) {
        $city.val(String(payload.city)).trigger('input');
      }
      if (payload.country) {
        $country.val(String(payload.country)).trigger('input');
      }
    }

    function geocodeAddress(address, silent) {
      if (!address) {
        if (!silent) {
          showToast(getMessage('enterAddress', 'Enter an address first.'), 'error');
        }
        return Promise.reject(new Error('missing_address'));
      }
      if (!endpoint) {
        if (!silent) {
          showToast(getMessage('geocodeFailed', 'Could not locate that address right now.'), 'error');
        }
        return Promise.reject(new Error('missing_endpoint'));
      }

      $btn.prop('disabled', true).addClass('eep-loading').text('Locating...');

      const qs = new URLSearchParams();
      qs.set('address', address);
      return fetch(endpoint + (endpoint.indexOf('?') === -1 ? '?' : '&') + qs.toString(), {
        credentials: 'same-origin',
        headers: {
          'X-WP-Nonce': String(getConfig('restNonce', '') || '')
        }
      })
        .then(async (res) => {
          const payload = await res.json();
          if (!res.ok) {
            throw new Error((payload && payload.message) ? payload.message : getMessage('geocodeNoResult', 'No location found for that address.'));
          }
          if (!payload || !Number.isFinite(parseFloat(payload.lat)) || !Number.isFinite(parseFloat(payload.lng))) {
            throw new Error(getMessage('geocodeNoResult', 'No location found for that address.'));
          }
          applyGeocodePayload(payload);
          return payload;
        })
        .catch((err) => {
          if (!silent) {
            showToast(err && err.message ? err.message : getMessage('geocodeFailed', 'Could not locate that address right now.'), 'error');
          }
          throw err;
        })
        .finally(() => {
          $btn.prop('disabled', false).removeClass('eep-loading').text(btnDefaultText);
        });
    }

    $lat.add($lng).on('change input', function () {
      updatePreviewMap($lat.val(), $lng.val());
    });

    $address.on('input', function () {
      clearTimeout(autoTimer);
      clearTimeout(suggestTimer);
      const current = String($address.val() || '').trim();
      suggestTimer = window.setTimeout(function () {
        fetchSuggestions(current);
      }, 180);
      if (current.length < 6) {
        return;
      }
      autoTimer = window.setTimeout(function () {
        if (current === lastAutoAddress) {
          return;
        }
        geocodeAddress(current, true)
          .then(() => {
            lastAutoAddress = current;
          })
          .catch(() => {});
      }, 550);
    });

    $address.on('blur', function () {
      const current = String($address.val() || '').trim();
      if (suggestionMap[current]) {
        const selected = suggestionMap[current];
        if (selected.city) {
          $city.val(String(selected.city)).trigger('input');
        }
        if (selected.country) {
          $country.val(String(selected.country)).trigger('input');
        }
        if (Number.isFinite(parseFloat(selected.lat)) && Number.isFinite(parseFloat(selected.lng))) {
          $lat.val(String(selected.lat)).trigger('input');
          $lng.val(String(selected.lng)).trigger('input');
          updatePreviewMap(selected.lat, selected.lng);
          lastAutoAddress = current;
          return;
        }
      }
      if (current.length < 6 || current === lastAutoAddress) {
        return;
      }
      geocodeAddress(current, true)
        .then(() => {
          lastAutoAddress = current;
        })
        .catch(() => {});
    });

    $address.on('change', function () {
      const current = String($address.val() || '').trim();
      if (!current) {
        return;
      }
      geocodeAddress(current, true)
        .then(() => {
          lastAutoAddress = current;
        })
        .catch(() => {});
    });

    $btn.on('click', function () {
      const address = ($address.val() || '').trim();
      geocodeAddress(address, false)
        .then(() => {
          lastAutoAddress = address;
        })
        .catch(() => {});
    });

    updatePreviewMap($lat.val(), $lng.val());
  }

  function initArchiveFiltersAjax() {
    const $form = $('.eep-archive-filter-form').first();
    const $results = $('#eep-archive-results');
    const $saveFiltersForm = $('.eep-save-filters-form').first();
    const $resetFilters = $form.find('.eep-reset-filters').first();
    const $saveFiltersLocal = $form.find('.eep-save-filters-local').first();
    const $useFiltersLocal = $form.find('.eep-use-filters-local').first();
    const endpoint = String(getConfig('archiveRestUrl', '') || '');
    const saveFiltersEndpoint = String(getConfig('saveFiltersRestUrl', '') || '');
    const currentUserId = parseInt(getConfig('currentUserId', 0), 10) || 0;
    if (!$form.length || !$results.length || !endpoint) {
      return;
    }

    let debounceTimer = null;
    let requestController = null;
    const localStorageKey = 'eep_archive_saved_filters';
    const filterKeys = ['s', 'event_type', 'event_mode', 'event_price', 'event_tag', 'city', 'date_from', 'official'];
    const selectDefaults = {};

    $form.find('select[name]').each(function () {
      const key = String($(this).attr('name') || '');
      if (!key) {
        return;
      }
      const dataDefault = String($(this).attr('data-default-label') || '').trim();
      if (dataDefault) {
        selectDefaults[key] = dataDefault;
        return;
      }
      const firstText = String($(this).find('option').first().text() || '').trim();
      if (firstText) {
        selectDefaults[key] = firstText;
      }
    });

    function collectParams(extra) {
      const params = {};
      $form.serializeArray().forEach((field) => {
        const key = String(field.name || '');
        const value = String(field.value || '');
        if (!key || value === '') {
          return;
        }
        params[key] = value;
      });
      const ppp = parseInt($results.attr('data-posts-per-page') || '12', 10);
      const organizer = parseInt($results.attr('data-organizer') || '0', 10);
      params.posts_per_page = Number.isFinite(ppp) && ppp > 0 ? ppp : 12;
      if (organizer > 0) {
        params.organizer = organizer;
      }
      if (!params.paged) {
        params.paged = 1;
      }
      if (extra && typeof extra === 'object') {
        Object.keys(extra).forEach((key) => {
          params[key] = extra[key];
        });
      }
      return params;
    }

    function hasActiveFilters(params) {
      const keysToCheck = ['s', 'event_type', 'event_mode', 'event_price', 'event_tag', 'city', 'date_from', 'official'];
      for (let i = 0; i < keysToCheck.length; i += 1) {
        const key = keysToCheck[i];
        if (String((params && params[key]) || '').trim() !== '') {
          return true;
        }
      }
      return false;
    }

    function toggleHidden($el, hidden) {
      if (!$el || !$el.length) {
        return;
      }
      $el.toggleClass('eep-is-hidden', hidden === true);
      if (hidden) {
        $el.attr('aria-hidden', 'true');
      } else {
        $el.removeAttr('aria-hidden');
      }
    }

    function syncFilterActionVisibility(params, serverActiveFlag) {
      const active = typeof serverActiveFlag === 'boolean' ? serverActiveFlag : hasActiveFilters(params);
      toggleHidden($resetFilters, !active);
      toggleHidden($saveFiltersForm, !active);
      toggleHidden($saveFiltersLocal, !active);
    }

    function syncSaveFiltersForm(params) {
      if (!$saveFiltersForm.length) {
        return;
      }
      ['s', 'event_type', 'event_mode', 'event_price', 'event_tag', 'city', 'date_from', 'official'].forEach((key) => {
        if (!$saveFiltersForm.find('input[name="' + key + '"]').length) {
          return;
        }
        $saveFiltersForm.find('input[name="' + key + '"]').val(String(params[key] || ''));
      });
    }

    function updateFilterSelect(name, options, params) {
      const $select = $form.find('select[name="' + name + '"]').first();
      if (!$select.length || !Array.isArray(options)) {
        return;
      }

      const defaultLabel = String($select.attr('data-default-label') || selectDefaults[name] || $select.find('option').first().text() || '').trim();
      const selectedValue = String((params && params[name]) || $select.val() || '').trim();
      const selectedText = String($select.find('option:selected').text() || '').trim();
      const seen = new Set();
      const prepared = [];

      options.forEach((option) => {
        const slug = String((option && option.slug) || '').trim();
        const label = String((option && option.name) || '').trim();
        const count = parseInt((option && option.count) || 0, 10);
        if (!slug || !label) {
          return;
        }
        if (count <= 0 && slug !== selectedValue) {
          return;
        }
        if (seen.has(slug)) {
          return;
        }
        seen.add(slug);
        prepared.push({ slug, label });
      });

      $select.empty();
      $select.append($('<option>', { value: '', text: defaultLabel || 'All' }));
      prepared.forEach((option) => {
        $select.append($('<option>', { value: option.slug, text: option.label }));
      });

      if (selectedValue) {
        if (seen.has(selectedValue)) {
          $select.val(selectedValue);
        } else {
          const fallbackText = selectedText || selectedValue;
          $select.append($('<option>', { value: selectedValue, text: fallbackText }));
          $select.val(selectedValue);
        }
      } else {
        $select.val('');
      }
    }

    function updateFilterOptions(payload, params) {
      if (!payload || typeof payload !== 'object' || !payload.filters || typeof payload.filters !== 'object') {
        return;
      }
      updateFilterSelect('event_type', payload.filters.event_type || [], params);
      updateFilterSelect('event_mode', payload.filters.event_mode || [], params);
      updateFilterSelect('event_price', payload.filters.event_price || [], params);
      updateFilterSelect('event_tag', payload.filters.event_tag || [], params);
    }

    function setLoading(state) {
      $results.toggleClass('eep-loading', state === true);
      if (state) {
        $results.attr('aria-busy', 'true');
      } else {
        $results.removeAttr('aria-busy');
      }
    }

    function decodeEscapedHtml(markup) {
      const source = String(markup || '');
      if (!source) {
        return '';
      }
      if (source.indexOf('<') !== -1) {
        return source;
      }
      if (source.indexOf('&lt;') === -1 && source.indexOf('&gt;') === -1) {
        return source;
      }
      const textarea = document.createElement('textarea');
      textarea.innerHTML = source;
      return String(textarea.value || source);
    }

    function updateUrl(params) {
      const current = new URL(window.location.href);
      ['s', 'event_type', 'event_mode', 'event_price', 'event_tag', 'city', 'date_from', 'official', 'paged', 'organizer', 'author'].forEach((key) => {
        current.searchParams.delete(key);
      });
      Object.keys(params).forEach((key) => {
        if (['s', 'event_type', 'event_mode', 'event_price', 'event_tag', 'city', 'date_from', 'official', 'paged', 'organizer'].indexOf(key) === -1) {
          return;
        }
        const value = String(params[key] || '');
        if (!value || (key === 'paged' && value === '1')) {
          return;
        }
        current.searchParams.set(key, value);
      });
      window.history.replaceState({}, '', current.toString());
    }

    function bindPagination() {
      $results.find('.eep-pagination a').off('click.eepArchive').on('click.eepArchive', function (e) {
        e.preventDefault();
        const href = String($(this).attr('href') || '');
        if (!href) {
          return;
        }
        const url = new URL(href, window.location.origin);
        let paged = parseInt(url.searchParams.get('paged') || '1', 10);
        if (!Number.isFinite(paged) || paged <= 1) {
          const match = url.pathname.match(/\/page\/(\d+)\/?$/i);
          if (match && match[1]) {
            paged = parseInt(match[1], 10);
          }
        }
        requestResults({ paged: Number.isFinite(paged) && paged > 0 ? paged : 1 });
      });
    }

    function requestResults(extra) {
      if (requestController) {
        requestController.abort();
      }
      const params = collectParams(extra);
      const query = new URLSearchParams();
      Object.keys(params).forEach((key) => {
        const value = String(params[key] || '');
        if (!value) {
          return;
        }
        query.set(key, value);
      });

      const currentController = new AbortController();
      requestController = currentController;
      setLoading(true);
      fetch(endpoint + '?' + query.toString(), {
        credentials: 'same-origin',
        signal: currentController.signal,
        headers: {
          'X-WP-Nonce': String(getConfig('restNonce', '') || '')
        }
      })
        .then(async (res) => {
          const payload = await res.json();
          if (!res.ok) {
            throw new Error((payload && payload.message) ? payload.message : getMessage('archiveLoadFailed', 'Could not update events list.'));
          }
          const rawHtml = payload && payload.html ? payload.html : '';
          $results.html(decodeEscapedHtml(rawHtml));
          document.dispatchEvent(new CustomEvent('eep:archiveResultsUpdated'));
          bindPagination();
          updateUrl(params);
          syncSaveFiltersForm(params);
          updateFilterOptions(payload, params);
          syncFilterActionVisibility(params, payload && payload.has_active_filters === true);
          initCardLinks();
        })
        .catch((err) => {
          if (err && err.name === 'AbortError') {
            return;
          }
          showToast(err && err.message ? err.message : getMessage('archiveLoadFailed', 'Could not update events list.'), 'error');
        })
        .finally(() => {
          if (requestController === currentController) {
            requestController = null;
            setLoading(false);
          }
        });
    }

    $form.on('submit', function (e) {
      e.preventDefault();
      requestResults({ paged: 1 });
    });

    $form.on('change', 'select,input[type="date"]', function () {
      requestResults({ paged: 1 });
    });

    $form.on('input', 'input[type="text"]', function () {
      clearTimeout(debounceTimer);
      debounceTimer = window.setTimeout(function () {
        requestResults({ paged: 1 });
      }, 260);
    });

    $resetFilters.on('click', function (e) {
      e.preventDefault();
      $form[0].reset();
      $form.find('input[name="paged"]').val('1');
      requestResults({ paged: 1 });
    });

    if ($saveFiltersForm.length) {
      $saveFiltersForm.on('submit', function (e) {
        if (!saveFiltersEndpoint || currentUserId <= 0) {
          return;
        }
        e.preventDefault();
        const params = collectParams({ paged: 1 });
        if (!hasActiveFilters(params)) {
          showToast(getMessage('archiveNoFiltersToSave', 'Apply at least one filter before saving.'), 'error');
          return;
        }
        syncSaveFiltersForm(params);
        restPost(saveFiltersEndpoint, {
          s: String(params.s || ''),
          event_type: String(params.event_type || ''),
          event_mode: String(params.event_mode || ''),
          event_price: String(params.event_price || ''),
          event_tag: String(params.event_tag || ''),
          city: String(params.city || ''),
          date_from: String(params.date_from || ''),
          official: String(params.official || '')
        })
          .then(function (payload) {
            showToast((payload && payload.message) ? payload.message : getMessage('archiveFiltersSaved', 'Filters saved to your account.'), 'success');
          })
          .catch(function (err) {
            showToast(err && err.message ? err.message : getMessage('archiveSaveFailed', 'Could not save filters right now.'), 'error');
          })
          .finally(function () {
            $saveFiltersForm.data('eepSubmitting', false).removeClass('eep-is-submitting');
            $saveFiltersForm.find('button[type="submit"], input[type="submit"]').prop('disabled', false).removeClass('eep-loading');
          });
      });
    }

    if ($saveFiltersLocal.length) {
      $saveFiltersLocal.on('click', function (e) {
        e.preventDefault();
        const params = collectParams({ paged: 1 });
        if (!hasActiveFilters(params)) {
          showToast(getMessage('archiveNoFiltersToSave', 'Apply at least one filter before saving.'), 'error');
          return;
        }
        const payload = {};
        filterKeys.forEach((key) => {
          payload[key] = String(params[key] || '').trim();
        });
        try {
          window.localStorage.setItem(localStorageKey, JSON.stringify(payload));
          showToast(getMessage('archiveFiltersSaved', 'Filters saved on this device.'), 'success');
        } catch (err) {
          showToast(getMessage('archiveSaveFailed', 'Could not save filters on this device.'), 'error');
        }
      });
    }

    if ($useFiltersLocal.length) {
      $useFiltersLocal.on('click', function (e) {
        e.preventDefault();
        let payload = null;
        try {
          payload = JSON.parse(String(window.localStorage.getItem(localStorageKey) || '{}'));
        } catch (err) {
          payload = null;
        }
        if (!payload || typeof payload !== 'object') {
          showToast(getMessage('archiveNoSavedFilters', 'No saved filters found on this device.'), 'error');
          return;
        }
        filterKeys.forEach((key) => {
          const value = String(payload[key] || '');
          const $field = $form.find('[name="' + key + '"]').first();
          if ($field.length) {
            $field.val(value);
          }
        });
        requestResults({ paged: 1 });
      });
    }

    bindPagination();
    syncFilterActionVisibility(collectParams());
  }

  function ensureAlpineReady() {
    if (typeof window.__eepDeferredAlpineInit === 'function') {
      try {
        window.__eepDeferredAlpineInit();
      } catch (err) {
        // noop
      }
      window.__eepDeferredAlpineInit = null;
      return;
    }

    if (!window.Alpine || typeof window.Alpine.initTree !== 'function') {
      return;
    }
    const roots = document.querySelectorAll('[x-data], #eep-submit-form');
    roots.forEach(function (root) {
      if (!root) {
        return;
      }
      if (root.getAttribute('data-eep-alpine-bound') === '1') {
        return;
      }
      if (root.__x || root._x_dataStack) {
        root.setAttribute('data-eep-alpine-bound', '1');
        return;
      }
      try {
        window.Alpine.initTree(root);
      } catch (err) {
        return;
      }
      root.setAttribute('data-eep-alpine-bound', '1');
    });
  }

  function pruneLegacyFrontendCssForMapTemplates() {
    const hasTailwindMapTemplate = document.getElementById('eep-map-default-wrapper') || document.getElementById('eep-map-triple-wrapper');
    if (!hasTailwindMapTemplate) {
      return;
    }
    const legacyNodes = document.querySelectorAll(
      'link[id^="eep-frontend"], style[id^="eep-frontend"], link[id^="eep-tailwind"], style[id^="eep-tailwind"], link[id^="eep-fonts"], link[href*="assets/css/eep-frontend.css"], link[href*="assets/css/eep-tailwind.css"]'
    );
    legacyNodes.forEach(function (node) {
      if (node && node.parentNode) {
        node.parentNode.removeChild(node);
      }
    });
  }

  function initEventHeroParallax() {
    if (!getConfig('enableHeroParallax', true)) {
      return;
    }
    const heroes = document.querySelectorAll('.eep-event-hero-has-image');
    const archiveBanners = document.querySelectorAll('.eep-page-banner-has-image[data-eep-hero-parallax="1"]');
    if (!heroes.length && !archiveBanners.length) {
      return;
    }
    const mobileMedia = window.matchMedia ? window.matchMedia('(max-width: 900px)') : null;
    if (mobileMedia && mobileMedia.matches) {
      heroes.forEach(function (hero) {
        hero.style.setProperty('--eep-hero-bg-pos', 'center center');
      });
      archiveBanners.forEach(function (banner) {
        banner.style.setProperty('--eep-page-hero-bg-pos', 'center center');
      });
      return;
    }

    const updateParallax = function () {
      heroes.forEach(function (hero) {
        const rect = hero.getBoundingClientRect();
        const centerDelta = rect.top + rect.height / 2 - window.innerHeight / 2;
        const shift = Math.round(centerDelta * -0.08);
        hero.style.setProperty('--eep-hero-bg-pos', 'center calc(50% + ' + shift + 'px)');
      });
      archiveBanners.forEach(function (banner) {
        const rect = banner.getBoundingClientRect();
        const centerDelta = rect.top + rect.height / 2 - window.innerHeight / 2;
        const shift = Math.round(centerDelta * -0.06);
        banner.style.setProperty('--eep-page-hero-bg-pos', 'center calc(50% + ' + shift + 'px)');
      });
    };

    updateParallax();
    window.addEventListener('scroll', updateParallax, { passive: true });
    window.addEventListener('resize', updateParallax);
  }

  function initPageBannerHeroBackground() {
    const heroUrl = String(getConfig('fallbackHeroUrl', '') || '').trim();
    if (!heroUrl) {
      return;
    }
    const banners = document.querySelectorAll('.eep-page-banner');
    if (!banners.length) {
      return;
    }
    const escapedUrl = heroUrl.replace(/["\\]/g, '\\$&');
    banners.forEach(function (banner) {
      if (!(banner instanceof HTMLElement)) {
        return;
      }
      banner.classList.add('eep-page-banner-has-image');
      banner.style.setProperty('--eep-page-hero-image', 'url("' + escapedUrl + '")');
      if (!banner.hasAttribute('data-eep-hero-parallax')) {
        banner.setAttribute('data-eep-hero-parallax', '1');
      }
    });
  }

  function initFrontendStickyNav() {
    const nav = document.querySelector('.eep-front-sticky-nav[data-eep-sticky-nav="1"]');
    if (!nav) {
      if (stickyNavSmartState && stickyNavSmartState.onScroll && stickyNavSmartState.onResize) {
        window.removeEventListener('scroll', stickyNavSmartState.onScroll);
        window.removeEventListener('resize', stickyNavSmartState.onResize);
      }
      stickyNavSmartState = null;
      return;
    }

    const behavior = String(nav.getAttribute('data-eep-sticky-behavior') || 'sticky').trim();
    const toggleButton = nav.querySelector('[data-eep-sticky-nav-toggle="1"]');
    const menu = nav.querySelector('.eep-front-sticky-nav-main');
    const navLinks = Array.from(nav.querySelectorAll('.eep-front-sticky-nav-menu-link[data-eep-nav-link="1"]'));
    const groupDetails = Array.from(nav.querySelectorAll('.eep-front-sticky-nav-group[data-eep-nav-group="1"]'));
    const isDesktopHoverMode = function () {
      const supportsHover = typeof window.matchMedia === 'function'
        ? window.matchMedia('(hover: hover) and (pointer: fine)').matches
        : true;
      return window.innerWidth > 900 && supportsHover;
    };

    if (groupDetails.length && nav.getAttribute('data-eep-sticky-groups-bound') !== '1') {
      nav.setAttribute('data-eep-sticky-groups-bound', '1');
      groupDetails.forEach(function (group) {
        group.addEventListener('toggle', function () {
          if (!group.open) {
            return;
          }
          groupDetails.forEach(function (other) {
            if (other !== group) {
              other.removeAttribute('open');
            }
          });
        });
        group.addEventListener('mouseenter', function () {
          if (!isDesktopHoverMode()) {
            return;
          }
          groupDetails.forEach(function (other) {
            if (other !== group) {
              other.removeAttribute('open');
            }
          });
          group.setAttribute('open', 'open');
        });
        group.addEventListener('mouseleave', function () {
          if (!isDesktopHoverMode()) {
            return;
          }
          group.removeAttribute('open');
        });
        group.addEventListener('focusout', function (event) {
          const nextTarget = event && event.relatedTarget ? event.relatedTarget : null;
          if (nextTarget && group.contains(nextTarget)) {
            return;
          }
          if (window.innerWidth > 900) {
            group.removeAttribute('open');
          }
        });
      });
    }

    if (toggleButton && menu && nav.getAttribute('data-eep-sticky-toggle-bound') !== '1') {
      nav.setAttribute('data-eep-sticky-toggle-bound', '1');
      const closeMenu = function () {
        nav.classList.remove('is-open');
        toggleButton.setAttribute('aria-expanded', 'false');
      };
      const closeDropdowns = function () {
        groupDetails.forEach(function (group) {
          group.removeAttribute('open');
        });
      };
      toggleButton.addEventListener('click', function () {
        const willOpen = !nav.classList.contains('is-open');
        nav.classList.toggle('is-open', willOpen);
        toggleButton.setAttribute('aria-expanded', willOpen ? 'true' : 'false');
      });
      document.addEventListener('click', function (event) {
        if (!nav.contains(event.target)) {
          closeMenu();
          closeDropdowns();
        }
      });
      document.addEventListener('keydown', function (event) {
        if (event.key === 'Escape') {
          closeMenu();
          closeDropdowns();
        }
      });
      menu.querySelectorAll('a').forEach(function (link) {
        link.addEventListener('click', function () {
          closeMenu();
        });
      });
    }

    navLinks.forEach(function (link) {
      link.classList.remove('is-current');
      link.removeAttribute('aria-current');
    });
    groupDetails.forEach(function (group) {
      group.classList.remove('is-current');
      if (window.innerWidth > 900) {
        group.removeAttribute('open');
      }
    });

    if (behavior !== 'smart') {
      nav.classList.remove('is-scroll-hidden');
      if (stickyNavSmartState && stickyNavSmartState.onScroll && stickyNavSmartState.onResize) {
        window.removeEventListener('scroll', stickyNavSmartState.onScroll);
        window.removeEventListener('resize', stickyNavSmartState.onResize);
      }
      stickyNavSmartState = null;
      return;
    }

    if (stickyNavSmartState && stickyNavSmartState.nav === nav) {
      return;
    }
    if (stickyNavSmartState && stickyNavSmartState.onScroll && stickyNavSmartState.onResize) {
      window.removeEventListener('scroll', stickyNavSmartState.onScroll);
      window.removeEventListener('resize', stickyNavSmartState.onResize);
      stickyNavSmartState = null;
    }

    let lastY = Math.max(window.scrollY || 0, 0);
    const threshold = 8;
    const onScroll = function () {
      const nextY = Math.max(window.scrollY || 0, 0);
      if (window.innerWidth <= 900 && nav.classList.contains('is-open')) {
        lastY = nextY;
        return;
      }
      if (nextY <= 24 || nextY < lastY - threshold) {
        nav.classList.remove('is-scroll-hidden');
      } else if (nextY > lastY + threshold) {
        nav.classList.add('is-scroll-hidden');
      }
      lastY = nextY;
    };
    const onResize = function () {
      if (window.innerWidth > 900) {
        nav.classList.remove('is-open');
        if (toggleButton) {
          toggleButton.setAttribute('aria-expanded', 'false');
        }
      }
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onResize);
    stickyNavSmartState = {
      nav: nav,
      onScroll: onScroll,
      onResize: onResize
    };
  }

  function initStickyColumns() {
    const selector = [
      '.eep-layout.eep-archive-layout .eep-archive-sidebar',
      '.eep-layout.eep-archive-layout .eep-sidebar',
      '.eep-layout.eep-map-layout .eep-map-sidebar',
      '.eep-layout.eep-map-layout .eep-map-results-col',
      '.eep-event-page .eep-event-aside',
      '.eep-submit-wrap .eep-submit-nav'
    ].join(',');
    const targets = Array.from(document.querySelectorAll(selector));
    if (!targets.length) {
      return;
    }

    const canUseNativeSticky = !!(window.CSS && typeof window.CSS.supports === 'function' && (window.CSS.supports('position', 'sticky') || window.CSS.supports('position', '-webkit-sticky')));

    const resetNode = function (node) {
      if (!node) {
        return;
      }
      node.classList.remove('eep-sticky-js-fixed', 'eep-sticky-js-bottom');
      node.style.position = '';
      node.style.top = '';
      node.style.left = '';
      node.style.right = '';
      node.style.bottom = '';
      node.style.width = '';
      node.style.maxHeight = '';
      node.style.overflow = '';
      node.style.zIndex = '';
      node.style.transform = '';
      node.removeAttribute('data-eep-sticky-bound');

      const previous = node.previousElementSibling;
      if (previous && previous.classList && previous.classList.contains('eep-sticky-placeholder')) {
        previous.remove();
      }
    };

    targets.forEach(resetNode);

    // Keep sticky behavior CSS-only to avoid JS placeholder/fixed-position collisions.
    if (!canUseNativeSticky) {
      return;
    }
  }

  function initEventLocalTime() {
    const nodes = document.querySelectorAll('[data-eep-local-time]');
    if (!nodes.length || typeof Intl === 'undefined' || !Intl.DateTimeFormat) {
      return;
    }

    const viewerTimezone = (function () {
      try {
        return String(Intl.DateTimeFormat().resolvedOptions().timeZone || '').trim();
      } catch (err) {
        return '';
      }
    })();

    nodes.forEach(function (node) {
      if (!node || node.getAttribute('data-eep-local-time-bound') === '1') {
        return;
      }
      node.setAttribute('data-eep-local-time-bound', '1');
      const startRaw = String(node.getAttribute('data-start') || '').trim();
      const endRaw = String(node.getAttribute('data-end') || '').trim();
      const eventTz = String(node.getAttribute('data-event-timezone') || '').trim();
      if (!startRaw) {
        node.textContent = '';
        return;
      }
      const startDate = new Date(startRaw);
      const endDate = endRaw ? new Date(endRaw) : null;
      if (!startDate || Number.isNaN(startDate.getTime())) {
        node.textContent = '';
        return;
      }

      const dateFormatter = new Intl.DateTimeFormat(undefined, {
        dateStyle: 'medium',
        timeStyle: 'short'
      });
      const timeFormatter = new Intl.DateTimeFormat(undefined, {
        timeStyle: 'short'
      });
      let text = 'Your local time: ' + dateFormatter.format(startDate);
      if (endDate && !Number.isNaN(endDate.getTime())) {
        const startDay = startDate.toDateString();
        const endDay = endDate.toDateString();
        if (startDay === endDay) {
          text = 'Your local time: ' + dateFormatter.format(startDate) + ' - ' + timeFormatter.format(endDate);
        } else {
          text = 'Your local time: ' + dateFormatter.format(startDate) + ' - ' + dateFormatter.format(endDate);
        }
      }
      const tzBits = [];
      if (viewerTimezone) {
        tzBits.push(viewerTimezone);
      }
      if (eventTz && eventTz.toLowerCase() !== viewerTimezone.toLowerCase()) {
        tzBits.push('event timezone: ' + eventTz);
      }
      if (tzBits.length) {
        text += ' (' + tzBits.join(' · ') + ')';
      }
      node.textContent = text;
    });
  }

  function initAutogeneratedDisclaimer() {
    const disclaimers = document.querySelectorAll('[data-eep-autogenerated-disclaimer]');
    if (!disclaimers.length) {
      return;
    }

    disclaimers.forEach(function (banner) {
      if (!banner || banner.getAttribute('data-eep-disclaimer-bound') === '1') {
        return;
      }
      banner.setAttribute('data-eep-disclaimer-bound', '1');
      const eventId = String(banner.getAttribute('data-eep-event-id') || '0').trim() || '0';
      const storageKey = 'eep_disclaimer_dismissed_' + eventId;

      let isDismissed = false;
      try {
        isDismissed = window.localStorage.getItem(storageKey) === '1';
      } catch (err) {
        isDismissed = false;
      }
      if (isDismissed) {
        banner.setAttribute('hidden', 'hidden');
        return;
      }

      const messageText = String(banner.getAttribute('data-eep-disclaimer-message') || '').trim();
      if (!messageText) {
        banner.setAttribute('hidden', 'hidden');
        return;
      }

      const sourceUrlRaw = String(banner.getAttribute('data-eep-disclaimer-source-url') || '').trim();
      let sourceUrl = '';
      if (sourceUrlRaw) {
        try {
          const parsed = new URL(sourceUrlRaw, window.location.origin);
          if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
            sourceUrl = parsed.href;
          }
        } catch (err) {
          sourceUrl = '';
        }
      }
      const sourceHtml = sourceUrl
        ? ' <a href="' + escapeHtml(sourceUrl) + '" target="_blank" rel="noopener noreferrer nofollow ugc">Source</a>'
        : '';
      const messageHtml = escapeHtml(messageText) + sourceHtml;

      showToast(messageHtml, 'warning', {
        title: 'Imported Listing Notice',
        subtitle: 'Please verify listing details with the original host.',
        duration: 12000,
        className: 'eep-toast-imported-wide',
        allowHtml: true,
        onClose: function () {
          try {
            window.localStorage.setItem(storageKey, '1');
          } catch (err) {
            // Ignore storage failures in privacy-restricted contexts.
          }
        }
      });

      banner.setAttribute('hidden', 'hidden');
    });
  }

  function initEventGalleryLightbox() {
    const $lightbox = $('#eep-lightbox');
    const $image = $('#eep-lightbox-image');
    if (!$lightbox.length || !$image.length) {
      return;
    }
    const refreshMediaLightboxBodyState = function () {
      const hasOpenMediaLightbox = $('#eep-lightbox:not([hidden]), #eep-video-lightbox:not([hidden])').length > 0;
      $('body').toggleClass('eep-media-lightbox-open', hasOpenMediaLightbox);
    };
    let galleryItems = [];
    let galleryIndex = -1;
    let ignoreBackdropCloseUntil = 0;

    function parseGalleryItemsFromJsonAttr($container) {
      if (!$container || !$container.length) {
        return [];
      }
      const raw = String($container.attr('data-eep-gallery-items') || '').trim();
      if (!raw) {
        return [];
      }
      try {
        const parsed = JSON.parse(raw);
        if (!Array.isArray(parsed)) {
          return [];
        }
        return parsed.map(function (item) {
          return String(item || '').trim();
        }).filter(function (item) {
          return item !== '';
        });
      } catch (error) {
        return [];
      }
    }

    function gatherGalleryItems($scope) {
      if (!$scope || !$scope.length) {
        return [];
      }
      return $scope.find('[data-eep-lightbox]').map(function () {
        return String($(this).attr('data-eep-lightbox') || '').trim();
      }).get().filter(function (src) {
        return src !== '';
      });
    }

    function syncGalleryItems($trigger) {
      const $eventGallery = $trigger && $trigger.length
        ? $trigger.closest('[data-eep-gallery-grid], .eep-event-gallery-grid')
        : $();

      if ($eventGallery.length) {
        const fromJson = parseGalleryItemsFromJsonAttr($eventGallery.first());
        if (fromJson.length) {
          galleryItems = fromJson;
          return;
        }
        const fromEventGrid = gatherGalleryItems($eventGallery.first());
        if (fromEventGrid.length) {
          galleryItems = fromEventGrid;
          return;
        }
      }

      const $customGallery = $trigger && $trigger.length
        ? $trigger.closest('[data-eep-custom-gallery], .eep-custom-display-gallery')
        : $();
      if ($customGallery.length) {
        const fromCustom = gatherGalleryItems($customGallery.first());
        if (fromCustom.length) {
          galleryItems = fromCustom;
          return;
        }
      }

      galleryItems = $('[data-eep-lightbox]').map(function () {
        return String($(this).attr('data-eep-lightbox') || '').trim();
      }).get().filter(function (src) {
        return src !== '';
      });
    }

    function openAt(index) {
      if (!galleryItems.length) {
        return;
      }
      const size = galleryItems.length;
      galleryIndex = ((index % size) + size) % size;
      $image.attr('src', galleryItems[galleryIndex] || '');
      $lightbox.removeAttr('hidden');
      $('body').addClass('eep-lightbox-open');
      refreshMediaLightboxBodyState();
    }

    function move(step) {
      if (!galleryItems.length || galleryIndex < 0) {
        return;
      }
      openAt(galleryIndex + step);
    }

    function closeLightbox() {
      $lightbox.attr('hidden', 'hidden');
      $image.attr('src', '');
      galleryIndex = -1;
      $('body').removeClass('eep-lightbox-open');
      refreshMediaLightboxBodyState();
    }

    $(document).off('click.eepGalleryLightbox', '.eep-gallery-item[data-eep-lightbox], [data-eep-gallery-grid] [data-eep-lightbox], [data-eep-custom-gallery] [data-eep-lightbox]').on('click.eepGalleryLightbox', '.eep-gallery-item[data-eep-lightbox], [data-eep-gallery-grid] [data-eep-lightbox], [data-eep-custom-gallery] [data-eep-lightbox]', function (event) {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      const $trigger = $(this);
      const src = String($trigger.attr('data-eep-lightbox') || '').trim();
      if (!src) {
        return;
      }
      syncGalleryItems($trigger);
      const explicitIndex = parseInt(String($trigger.attr('data-eep-gallery-index') || ''), 10);
      if (Number.isFinite(explicitIndex) && explicitIndex >= 0) {
        openAt(explicitIndex);
        return;
      }
      const clickedIndex = galleryItems.indexOf(src);
      openAt(clickedIndex >= 0 ? clickedIndex : 0);
    });

    $('#eep-lightbox-close').off('click.eepGalleryLightboxClose').on('click.eepGalleryLightboxClose', function () {
      closeLightbox();
    });
    $('#eep-lightbox-prev').off('click.eepGalleryLightboxPrev').on('click.eepGalleryLightboxPrev', function () {
      move(-1);
    });
    $('#eep-lightbox-next').off('click.eepGalleryLightboxNext').on('click.eepGalleryLightboxNext', function () {
      move(1);
    });

    $lightbox.off('click.eepGalleryLightboxBackdrop').on('click.eepGalleryLightboxBackdrop', function (e) {
      if (Date.now() < ignoreBackdropCloseUntil) {
        return;
      }
      if (e.target === this) {
        closeLightbox();
      }
    });

    const lightboxNode = $lightbox.get(0);
    if (lightboxNode) {
      bindHorizontalSwipe(lightboxNode, {
        boundKey: 'data-eep-gallery-swipe-bound',
        ignoreSelector: '#eep-lightbox-close, #eep-lightbox-prev, #eep-lightbox-next',
        isEnabled: function () {
          return $lightbox.is(':visible') && !$lightbox.attr('hidden');
        },
        beforeSwipe: function () {
          ignoreBackdropCloseUntil = Date.now() + 320;
        },
        onSwipeLeft: function () {
          move(1);
        },
        onSwipeRight: function () {
          move(-1);
        }
      });
    }

    $(document).off('keydown.eepGalleryLightboxKeys').on('keydown.eepGalleryLightboxKeys', function (e) {
      if ($lightbox.is(':visible') && !$lightbox.attr('hidden')) {
        if (e.key === 'ArrowLeft') {
          e.preventDefault();
          move(-1);
          return;
        }
        if (e.key === 'ArrowRight') {
          e.preventDefault();
          move(1);
          return;
        }
      }
      if (e.key === 'Escape') {
        closeLightbox();
      }
    });

    refreshMediaLightboxBodyState();
  }

  function bindHorizontalSwipe(node, options) {
    if (!node || !options || typeof options !== 'object') {
      return;
    }
    const onSwipeLeft = typeof options.onSwipeLeft === 'function' ? options.onSwipeLeft : null;
    const onSwipeRight = typeof options.onSwipeRight === 'function' ? options.onSwipeRight : null;
    if (!onSwipeLeft || !onSwipeRight) {
      return;
    }
    const boundKey = String(options.boundKey || 'data-eep-swipe-bound').trim() || 'data-eep-swipe-bound';
    if (node.getAttribute(boundKey) === '1') {
      return;
    }
    node.setAttribute(boundKey, '1');

    const threshold = Number(options.threshold) > 0 ? Number(options.threshold) : 52;
    const verticalThreshold = Number(options.verticalThreshold) > 0 ? Number(options.verticalThreshold) : 92;
    const maxDurationMs = Number(options.maxDurationMs) > 0 ? Number(options.maxDurationMs) : 900;
    const ignoreSelector = String(options.ignoreSelector || '').trim();
    const isEnabled = typeof options.isEnabled === 'function' ? options.isEnabled : function () {
      return true;
    };
    const beforeSwipe = typeof options.beforeSwipe === 'function' ? options.beforeSwipe : null;

    let startX = 0;
    let startY = 0;
    let startTime = 0;
    let tracking = false;

    const readPoint = function (event) {
      if (!event) {
        return null;
      }
      const touchList = event.changedTouches && event.changedTouches.length
        ? event.changedTouches
        : event.touches;
      if (!touchList || !touchList.length) {
        return null;
      }
      const point = touchList[0];
      return {
        x: Number(point.clientX || 0),
        y: Number(point.clientY || 0)
      };
    };

    const onStart = function (event) {
      if (!isEnabled()) {
        tracking = false;
        return;
      }
      if (ignoreSelector && event && event.target && $(event.target).closest(ignoreSelector).length) {
        tracking = false;
        return;
      }
      const point = readPoint(event);
      if (!point) {
        tracking = false;
        return;
      }
      startX = point.x;
      startY = point.y;
      startTime = Date.now();
      tracking = true;
    };

    const onMove = function (event) {
      if (!tracking) {
        return;
      }
      const point = readPoint(event);
      if (!point) {
        return;
      }
      const deltaX = point.x - startX;
      const deltaY = point.y - startY;
      if (Math.abs(deltaX) > 8 && Math.abs(deltaX) > Math.abs(deltaY) && event.cancelable) {
        event.preventDefault();
      }
    };

    const onEnd = function (event) {
      if (!tracking) {
        return;
      }
      tracking = false;
      if (!isEnabled()) {
        return;
      }
      const point = readPoint(event);
      if (!point) {
        return;
      }
      const elapsed = Date.now() - startTime;
      const deltaX = point.x - startX;
      const deltaY = point.y - startY;
      if (elapsed > maxDurationMs) {
        return;
      }
      if (Math.abs(deltaX) < threshold || Math.abs(deltaY) > verticalThreshold) {
        return;
      }
      if (beforeSwipe) {
        beforeSwipe();
      }
      if (deltaX < 0) {
        onSwipeLeft();
      } else {
        onSwipeRight();
      }
    };

    const onCancel = function () {
      tracking = false;
    };

    node.addEventListener('touchstart', onStart, { passive: true });
    node.addEventListener('touchmove', onMove, { passive: false });
    node.addEventListener('touchend', onEnd, { passive: true });
    node.addEventListener('touchcancel', onCancel, { passive: true });
  }

  function resolveVideoEmbedUrl(rawValue) {
    const raw = String(rawValue || '').trim();
    if (!raw) {
      return '';
    }

    let parsed = null;
    try {
      parsed = new URL(raw, window.location.origin);
    } catch (error) {
      return '';
    }
    if (!parsed) {
      return '';
    }

    const host = String(parsed.hostname || '').toLowerCase();
    const path = String(parsed.pathname || '').replace(/\/+$/g, '');
    const segments = path.replace(/^\/+/, '').split('/').filter(Boolean);
    const extension = (function () {
      const dotIndex = path.lastIndexOf('.');
      if (dotIndex < 0) {
        return '';
      }
      return String(path.slice(dotIndex + 1) || '').toLowerCase();
    })();

    if (['mp4', 'webm', 'ogg', 'mov', 'm4v'].indexOf(extension) >= 0) {
      return parsed.toString();
    }

    if (
      (host.indexOf('youtube.com') >= 0 || host.indexOf('youtube-nocookie.com') >= 0) &&
      segments[0] === 'embed' &&
      segments[1]
    ) {
      return 'https://www.youtube.com/embed/' + encodeURIComponent(segments[1]) + '?autoplay=1&rel=0';
    }

    if (host.indexOf('youtu.be') >= 0 && segments[0]) {
      return 'https://www.youtube.com/embed/' + encodeURIComponent(segments[0]) + '?autoplay=1&rel=0';
    }

    if (host.indexOf('youtube.com') >= 0 || host.indexOf('youtube-nocookie.com') >= 0) {
      const queryId = String(parsed.searchParams.get('v') || '').trim();
      if (queryId) {
        return 'https://www.youtube.com/embed/' + encodeURIComponent(queryId) + '?autoplay=1&rel=0';
      }
      if (segments.length >= 2 && ['shorts', 'live', 'v'].indexOf(segments[0]) >= 0) {
        return 'https://www.youtube.com/embed/' + encodeURIComponent(segments[1]) + '?autoplay=1&rel=0';
      }
    }

    if (host.indexOf('player.vimeo.com') >= 0 && segments[0] === 'video' && segments[1]) {
      return 'https://player.vimeo.com/video/' + encodeURIComponent(segments[1]) + '?autoplay=1';
    }

    if (host.indexOf('vimeo.com') >= 0 && segments.length) {
      let videoId = '';
      for (let i = segments.length - 1; i >= 0; i -= 1) {
        const candidate = String(segments[i] || '').trim();
        if (/^\d+$/.test(candidate)) {
          videoId = candidate;
          break;
        }
      }
      if (videoId) {
        return 'https://player.vimeo.com/video/' + encodeURIComponent(videoId) + '?autoplay=1';
      }
    }

    return '';
  }

  function initEventVideoLightbox() {
    const $lightbox = $('#eep-video-lightbox');
    const $frame = $('#eep-video-lightbox-iframe');
    if (!$lightbox.length || !$frame.length) {
      return;
    }
    const refreshMediaLightboxBodyState = function () {
      const hasOpenMediaLightbox = $('#eep-lightbox:not([hidden]), #eep-video-lightbox:not([hidden])').length > 0;
      $('body').toggleClass('eep-media-lightbox-open', hasOpenMediaLightbox);
    };
    let videoEmbeds = [];
    let videoIndex = -1;

    function syncVideoItems() {
      videoEmbeds = $('[data-eep-video-embed], [data-eep-video-url]').map(function () {
        const $item = $(this);
        const explicitEmbed = String($item.attr('data-eep-video-embed') || '').trim();
        const rawUrl = String($item.attr('data-eep-video-url') || '').trim();
        const embed = explicitEmbed || resolveVideoEmbedUrl(rawUrl);
        if (!embed) {
          return null;
        }
        return {
          embed: embed,
          url: rawUrl
        };
      }).get().filter(function (item) {
        return !!item && !!item.embed;
      });
    }

    function openVideoAt(index) {
      if (!videoEmbeds.length) {
        return;
      }
      const size = videoEmbeds.length;
      videoIndex = ((index % size) + size) % size;
      const current = videoEmbeds[videoIndex] || null;
      if (!current || !current.embed) {
        return;
      }
      $frame.attr('src', current.embed);
      $lightbox.removeAttr('hidden');
      $('body').addClass('eep-lightbox-open');
      refreshMediaLightboxBodyState();
    }

    function moveVideo(step) {
      if (!videoEmbeds.length || videoIndex < 0) {
        return;
      }
      openVideoAt(videoIndex + step);
    }

    let ignoreBackdropCloseUntil = 0;

    function closeVideoLightbox() {
      $lightbox.attr('hidden', 'hidden');
      $frame.attr('src', '');
      videoIndex = -1;
      $('body').removeClass('eep-lightbox-open');
      refreshMediaLightboxBodyState();
    }

    $(document).off('click.eepVideoLightboxOpen', '[data-eep-video-embed], [data-eep-video-url]').on('click.eepVideoLightboxOpen', '[data-eep-video-embed], [data-eep-video-url]', function (event) {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      const $trigger = $(this);
      const explicitEmbed = String($trigger.attr('data-eep-video-embed') || '').trim();
      const rawUrl = String($trigger.attr('data-eep-video-url') || '').trim();
      const embedUrl = explicitEmbed || resolveVideoEmbedUrl(rawUrl);
      if (!embedUrl) {
        if (rawUrl) {
          window.open(rawUrl, '_blank', 'noopener,noreferrer');
        }
        return;
      }
      syncVideoItems();
      const clickedIndex = videoEmbeds.findIndex(function (item) {
        return !!item && String(item.embed || '') === embedUrl;
      });
      openVideoAt(clickedIndex >= 0 ? clickedIndex : 0);
    });

    $('#eep-video-lightbox-close').off('click.eepVideoLightboxClose').on('click.eepVideoLightboxClose', function () {
      closeVideoLightbox();
    });
    $('#eep-video-lightbox-prev').off('click.eepVideoLightboxPrev').on('click.eepVideoLightboxPrev', function () {
      moveVideo(-1);
    });
    $('#eep-video-lightbox-next').off('click.eepVideoLightboxNext').on('click.eepVideoLightboxNext', function () {
      moveVideo(1);
    });

    $lightbox.off('click.eepVideoLightboxBackdrop').on('click.eepVideoLightboxBackdrop', function (e) {
      if (Date.now() < ignoreBackdropCloseUntil) {
        return;
      }
      if (e.target === this) {
        closeVideoLightbox();
      }
    });

    const bindVideoSwipeOnNode = function (node) {
      bindHorizontalSwipe(node, {
        boundKey: 'data-eep-video-swipe-bound',
        ignoreSelector: '#eep-video-lightbox-close, #eep-video-lightbox-prev, #eep-video-lightbox-next',
        isEnabled: function () {
          return $lightbox.is(':visible') && !$lightbox.attr('hidden');
        },
        beforeSwipe: function () {
          ignoreBackdropCloseUntil = Date.now() + 320;
        },
        onSwipeLeft: function () {
          moveVideo(1);
        },
        onSwipeRight: function () {
          moveVideo(-1);
        }
      });
    };
    const lightboxNode = $lightbox.get(0);
    if (lightboxNode) {
      bindVideoSwipeOnNode(lightboxNode);
    }
    $lightbox.find('[data-eep-video-swipe-zone]').each(function () {
      bindVideoSwipeOnNode(this);
    });

    $(document).off('keydown.eepVideoLightboxKeys').on('keydown.eepVideoLightboxKeys', function (e) {
      if ($lightbox.is(':visible') && !$lightbox.attr('hidden')) {
        if (e.key === 'ArrowLeft') {
          e.preventDefault();
          moveVideo(-1);
          return;
        }
        if (e.key === 'ArrowRight') {
          e.preventDefault();
          moveVideo(1);
          return;
        }
      }
      if (e.key === 'Escape') {
        closeVideoLightbox();
      }
    });

    refreshMediaLightboxBodyState();
  }

  function initEventDocumentLightbox() {
    const $lightbox = $('#eep-doc-lightbox');
    const $frame = $('#eep-doc-lightbox-iframe');
    const $title = $('#eep-doc-lightbox-title');
    const $downloads = $('[data-eep-doc-download], #eep-doc-lightbox-download');
    if (!$lightbox.length || !$frame.length || !$downloads.length) {
      return;
    }

    let docs = [];
    let docIndex = -1;

    function resolveDocumentEmbedUrl(url, embedCandidate) {
      const rawUrl = String(url || '').trim();
      const explicitEmbed = String(embedCandidate || '').trim();
      if (explicitEmbed) {
        return explicitEmbed;
      }
      if (!rawUrl) {
        return '';
      }
      let path = '';
      try {
        path = String(new URL(rawUrl, window.location.origin).pathname || '');
      } catch (error) {
        path = rawUrl;
      }
      const dotIndex = path.lastIndexOf('.');
      const ext = dotIndex >= 0 ? String(path.slice(dotIndex + 1) || '').toLowerCase() : '';
      if (ext === 'pdf') {
        return rawUrl;
      }
      return 'https://docs.google.com/gview?embedded=1&url=' + encodeURIComponent(rawUrl);
    }

    function syncDocuments() {
      docs = $('[data-eep-doc-url]').map(function () {
        const $el = $(this);
        const url = String($el.attr('data-eep-doc-url') || '').trim();
        const embed = resolveDocumentEmbedUrl(url, $el.attr('data-eep-doc-embed'));
        const title = String($el.attr('data-eep-doc-title') || '').trim();
        if (!url || !embed) {
          return null;
        }
        return {
          url: url,
          embed: embed,
          title: title || 'Document'
        };
      }).get().filter(function (item) {
        return !!item;
      });
    }

    function renderDoc(index) {
      if (!docs.length) {
        return;
      }
      const size = docs.length;
      docIndex = ((index % size) + size) % size;
      const current = docs[docIndex] || null;
      if (!current) {
        return;
      }
      $frame.attr('src', current.embed);
      $downloads.attr('href', current.url);
      $title.text(current.title);
      $lightbox.removeAttr('hidden');
      $('body').addClass('eep-lightbox-open');
    }

    function move(step) {
      if (!docs.length || docIndex < 0) {
        return;
      }
      renderDoc(docIndex + step);
    }

    function closeDocLightbox() {
      $lightbox.attr('hidden', 'hidden');
      $frame.attr('src', '');
      $downloads.attr('href', '#');
      $title.text('');
      docIndex = -1;
      $('body').removeClass('eep-lightbox-open');
    }

    $(document).off('click.eepDocLightbox', '[data-eep-doc-url]').on('click.eepDocLightbox', '[data-eep-doc-url]', function (event) {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      const $trigger = $(this);
      const docUrl = String($trigger.attr('data-eep-doc-url') || '').trim();
      if (!docUrl) {
        return;
      }
      syncDocuments();
      const explicitIndex = parseInt(String($trigger.attr('data-eep-doc-index') || ''), 10);
      if (Number.isFinite(explicitIndex) && explicitIndex >= 0) {
        renderDoc(explicitIndex);
        return;
      }
      const foundIndex = docs.findIndex(function (doc) {
        return String(doc && doc.url || '') === docUrl;
      });
      renderDoc(foundIndex >= 0 ? foundIndex : 0);
    });

    $('#eep-doc-lightbox-close').off('click.eepDocLightboxClose').on('click.eepDocLightboxClose', function () {
      closeDocLightbox();
    });
    $('#eep-doc-lightbox-prev').off('click.eepDocLightboxPrev').on('click.eepDocLightboxPrev', function () {
      move(-1);
    });
    $('#eep-doc-lightbox-next').off('click.eepDocLightboxNext').on('click.eepDocLightboxNext', function () {
      move(1);
    });
    $lightbox.off('click.eepDocLightboxBackdrop').on('click.eepDocLightboxBackdrop', function (event) {
      if (event.target === this) {
        closeDocLightbox();
      }
    });
    $(document).off('keydown.eepDocLightboxKeys').on('keydown.eepDocLightboxKeys', function (event) {
      if (!$lightbox.is(':visible') || !!$lightbox.attr('hidden')) {
        return;
      }
      if (event.key === 'ArrowLeft') {
        event.preventDefault();
        move(-1);
        return;
      }
      if (event.key === 'ArrowRight') {
        event.preventDefault();
        move(1);
        return;
      }
      if (event.key === 'Escape') {
        closeDocLightbox();
      }
    });
  }

  function initRsvpAjax() {
    const endpoint = String(getConfig('rsvpRestUrl', '') || '');
    if (!endpoint) {
      return;
    }

    $(document).off('submit.eepRsvpAjax', 'form.eep-rsvp-form').on('submit.eepRsvpAjax', 'form.eep-rsvp-form', function (event) {
      const $form = $(this);
      const $button = $form.find('button[type="submit"]').first();
      const postId = parseInt($form.find('input[name="post_id"]').val() || '0', 10);
      const intent = String($form.find('input[name="intent"]').val() || 'join').trim() || 'join';
      if (!postId) {
        return;
      }

      event.preventDefault();
      if ($button.length) {
        $button.prop('disabled', true).addClass('eep-loading');
      }

      restPost(endpoint, {
        post_id: postId,
        intent: intent
      })
        .then(function (payload) {
          const nextIntent = String((payload && payload.next_intent) || '').trim();
          const buttonLabel = String((payload && payload.button_label) || '').trim();
          const $formsForPost = $('form.eep-rsvp-form').filter(function () {
            const candidatePostId = parseInt($(this).find('input[name="post_id"]').val() || '0', 10);
            return Number.isFinite(candidatePostId) && candidatePostId === postId;
          });
          if (nextIntent) {
            $formsForPost.find('input[name="intent"]').val(nextIntent);
          }
          if (buttonLabel) {
            const $targetButtons = $formsForPost.length ? $formsForPost.find('button[type="submit"]') : $button;
            $targetButtons.each(function () {
              const $btn = $(this);
              const $label = $btn.find('[data-eep-rsvp-label]').first();
              if ($label.length) {
                $label.text(buttonLabel);
              } else {
                $btn.text(buttonLabel);
              }
            });
          }
          const rsvpCount = parseInt(payload && payload.rsvp_count, 10);
          const waitlistCount = parseInt(payload && payload.waitlist_count, 10);
          const $meta = $('.eep-rsvp-meta').filter(function () {
            const metaPostId = parseInt($(this).attr('data-post-id') || '0', 10);
            return !Number.isFinite(metaPostId) || metaPostId <= 0 || metaPostId === postId;
          });
          if ($meta.length && Number.isFinite(rsvpCount) && Number.isFinite(waitlistCount)) {
            $meta.each(function () {
              const $metaNode = $(this);
              const primaryLabel = String($metaNode.attr('data-primary-label') || 'RSVP').trim() || 'RSVP';
              const secondaryLabel = String($metaNode.attr('data-secondary-label') || '').trim();
              if (secondaryLabel) {
                $metaNode.text(primaryLabel + ': ' + String(rsvpCount) + ' | ' + secondaryLabel + ': ' + String(waitlistCount));
              } else {
                $metaNode.text(primaryLabel + ': ' + String(rsvpCount));
              }
              if ((rsvpCount + waitlistCount) > 0) {
                $metaNode.removeClass('hidden');
              } else {
                $metaNode.addClass('hidden');
              }
            });
          }
          const fallbackMessage = (function () {
            const primaryLabel = String(($meta.first().attr('data-primary-label') || '')).trim().toLowerCase();
            if (primaryLabel === 'saved') {
              return 'Saved list updated.';
            }
            return 'RSVP updated.';
          })();
          showToast((payload && payload.message) ? payload.message : fallbackMessage, 'success');
        })
        .catch(function (err) {
          showToast(err && err.message ? err.message : getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
        })
        .finally(function () {
          $form.data('eepSubmitting', false).removeClass('eep-is-submitting');
          if ($button.length) {
            $button.prop('disabled', false).removeClass('eep-loading');
          }
        });
    });
  }

  function initSubmitCustomFieldBuilder() {
    const form = document.getElementById('eep-submit-form');
    if (!form) {
      return;
    }
    const section = form.querySelector('[data-eep-custom-builder]');
    if (!section || section.getAttribute('data-eep-builder-bound') === '1') {
      return;
    }
    section.setAttribute('data-eep-builder-bound', '1');

    const body = section.querySelector('#eep-custom-builder-body');
    const addBtn = section.querySelector('#eep-custom-builder-add');
    const hiddenInput = form.querySelector('#eep-custom-builder-json');
    const typesInput = form.querySelector('#eep-custom-field-types-json');
    if (!body || !hiddenInput) {
      return;
    }

    let fieldTypes = {
      text: 'Text',
      textarea: 'Textarea',
      wysiwyg: 'Rich Text (WYSIWYG)',
      url: 'URL',
      email: 'Email',
      number: 'Number',
      date: 'Date',
      'datetime-local': 'Date + Time',
      select: 'Select Dropdown',
      checkbox: 'Yes/No Toggle',
      image: 'Image URL',
      gallery: 'Gallery URLs',
      file: 'File URL'
    };
    if (typesInput) {
      try {
        const parsedTypes = JSON.parse(String(typesInput.value || '{}'));
        if (parsedTypes && typeof parsedTypes === 'object' && !Array.isArray(parsedTypes)) {
          fieldTypes = parsedTypes;
        }
      } catch (err) {
        // keep defaults
      }
    }

    const normalizeKey = function (raw, fallbackIndex, fallbackLabel) {
      let normalized = String(raw || '').toLowerCase().replace(/[^a-z0-9_]+/g, '_').replace(/^_+|_+$/g, '');
      if (!normalized) {
        normalized = String(fallbackLabel || '').toLowerCase().replace(/[^a-z0-9_]+/g, '_').replace(/^_+|_+$/g, '');
      }
      if (!normalized) {
        normalized = 'custom_field_' + String(fallbackIndex);
      }
      return normalized.slice(0, 64);
    };

    const buildTypeOptions = function (selected) {
      return Object.keys(fieldTypes).map(function (typeKey) {
        const selectedAttr = String(typeKey) === String(selected || 'text') ? ' selected' : '';
        return '<option value="' + escapeHtml(typeKey) + '"' + selectedAttr + '>' + escapeHtml(String(fieldTypes[typeKey] || typeKey)) + '</option>';
      }).join('');
    };

    const valueControlTemplate = function (type, value, placeholder) {
      const safeType = String(type || 'text');
      const safeValue = String(value || '');
      const safePlaceholder = String(placeholder || '');
      if (safeType === 'checkbox') {
        return '<select class="eep-cbf-value"><option value="0"' + (safeValue === '1' ? '' : ' selected') + '>No</option><option value="1"' + (safeValue === '1' ? ' selected' : '') + '>Yes</option></select>';
      }
      if (safeType === 'textarea' || safeType === 'wysiwyg' || safeType === 'gallery') {
        return '<textarea class="eep-cbf-value" rows="3" placeholder="' + escapeHtml(safePlaceholder || 'Value') + '">' + escapeHtml(safeValue) + '</textarea>';
      }
      const typedInputs = ['url', 'email', 'number', 'date', 'datetime-local'];
      const inputType = typedInputs.indexOf(safeType) >= 0 ? safeType : 'text';
      let displayValue = safeValue;
      if (inputType === 'datetime-local' && displayValue.indexOf(' ') !== -1) {
        displayValue = displayValue.replace(' ', 'T').slice(0, 16);
      }
      return '<input type="' + escapeHtml(inputType) + '" class="eep-cbf-value" value="' + escapeHtml(displayValue) + '" placeholder="' + escapeHtml(safePlaceholder || 'Value') + '">';
    };

    const rowTemplate = function (seed) {
      const data = seed && typeof seed === 'object' ? seed : {};
      const type = String(data.type || 'text');
      const placeholder = String(data.placeholder || '');
      return ''
        + '<tr class="eep-custom-field-row eep-custom-builder-row">'
        + '<td><input type="text" class="eep-cbf-id" value="' + escapeHtml(String(data.id || '')) + '" placeholder="speaker_name"></td>'
        + '<td><input type="text" class="eep-cbf-label" value="' + escapeHtml(String(data.label || '')) + '" placeholder="Speaker Name"></td>'
        + '<td><select class="eep-cbf-type">' + buildTypeOptions(type) + '</select></td>'
        + '<td class="eep-cbf-value-wrap">' + valueControlTemplate(type, String(data.value || ''), placeholder) + '</td>'
        + '<td><input type="text" class="eep-cbf-placeholder" value="' + escapeHtml(placeholder) + '" placeholder="Optional placeholder"></td>'
        + '<td><input type="text" class="eep-cbf-help" value="' + escapeHtml(String(data.help || '')) + '" placeholder="Short instruction"></td>'
        + '<td><textarea class="eep-cbf-options" rows="2" placeholder="One option per line">' + escapeHtml(String(data.options || '')) + '</textarea></td>'
        + '<td>'
        + '<label style="display:block;"><input type="checkbox" class="eep-cbf-required" value="1"' + (String(data.required || '0') === '1' ? ' checked' : '') + '> Req</label>'
        + '<label style="display:block;"><input type="checkbox" class="eep-cbf-show" value="1"' + (String(data.show_in_event || '1') === '1' ? ' checked' : '') + '> Show</label>'
        + '</td>'
        + '<td><button type="button" class="button button-link-delete eep-cbf-remove">Remove</button></td>'
        + '</tr>';
    };

    const ensureEmptyState = function () {
      if (body.querySelectorAll('.eep-custom-builder-row').length > 0) {
        const empty = body.querySelector('.eep-custom-fields-empty-row');
        if (empty) {
          empty.remove();
        }
        return;
      }
      body.innerHTML = '<tr class="eep-custom-fields-empty-row"><td colspan="9" class="eep-custom-fields-empty">No additional fields yet. Click Add Field to create one.</td></tr>';
    };

    const refreshValueControl = function (row, preserveValue) {
      if (!row) {
        return;
      }
      const typeEl = row.querySelector('.eep-cbf-type');
      const wrap = row.querySelector('.eep-cbf-value-wrap');
      const placeholderEl = row.querySelector('.eep-cbf-placeholder');
      if (!typeEl || !wrap) {
        return;
      }
      const valueEl = row.querySelector('.eep-cbf-value');
      const currentValue = preserveValue !== undefined ? preserveValue : (valueEl ? String(valueEl.value || '') : '');
      const placeholder = placeholderEl ? String(placeholderEl.value || '') : '';
      wrap.innerHTML = valueControlTemplate(String(typeEl.value || 'text'), currentValue, placeholder);
    };

    const collectRows = function () {
      const rows = body.querySelectorAll('.eep-custom-builder-row');
      const seen = {};
      const payload = [];
      rows.forEach(function (row, index) {
        const idInput = row.querySelector('.eep-cbf-id');
        const labelInput = row.querySelector('.eep-cbf-label');
        const typeInput = row.querySelector('.eep-cbf-type');
        const valueInput = row.querySelector('.eep-cbf-value');
        const placeholderInput = row.querySelector('.eep-cbf-placeholder');
        const helpInput = row.querySelector('.eep-cbf-help');
        const optionsInput = row.querySelector('.eep-cbf-options');
        const requiredInput = row.querySelector('.eep-cbf-required');
        const showInput = row.querySelector('.eep-cbf-show');

        const label = labelInput ? String(labelInput.value || '').trim() : '';
        const keyRaw = idInput ? String(idInput.value || '').trim() : '';
        const type = typeInput ? String(typeInput.value || 'text').trim() : 'text';
        const value = valueInput ? String(valueInput.value || '').trim() : '';
        const placeholder = placeholderInput ? String(placeholderInput.value || '').trim() : '';
        const help = helpInput ? String(helpInput.value || '').trim() : '';
        const options = optionsInput ? String(optionsInput.value || '').trim() : '';
        const required = requiredInput && requiredInput.checked ? '1' : '0';
        const showInEvent = showInput && showInput.checked ? '1' : '0';

        if (keyRaw === '' && label === '' && value === '' && options === '' && placeholder === '' && help === '') {
          return;
        }

        const normalizedKey = normalizeKey(keyRaw, index + 1, label);
        if (!normalizedKey || seen[normalizedKey]) {
          return;
        }
        seen[normalizedKey] = true;
        if (idInput) {
          idInput.value = normalizedKey;
        }

        payload.push({
          id: normalizedKey,
          label: label,
          type: type,
          value: value,
          placeholder: placeholder,
          help: help,
          options: options,
          required: required,
          show_in_event: showInEvent
        });
      });
      return payload;
    };

    const syncHidden = function () {
      hiddenInput.value = JSON.stringify(collectRows());
    };

    if (addBtn) {
      addBtn.addEventListener('click', function (event) {
        event.preventDefault();
        const empty = body.querySelector('.eep-custom-fields-empty-row');
        if (empty) {
          empty.remove();
        }
        body.insertAdjacentHTML('beforeend', rowTemplate({}));
        syncHidden();
      });
    }

    body.addEventListener('click', function (event) {
      const removeBtn = event.target && event.target.closest ? event.target.closest('.eep-cbf-remove') : null;
      if (!removeBtn) {
        return;
      }
      event.preventDefault();
      const row = removeBtn.closest('.eep-custom-builder-row');
      if (row) {
        row.remove();
      }
      ensureEmptyState();
      syncHidden();
    });

    body.addEventListener('change', function (event) {
      const target = event.target;
      if (!target) {
        return;
      }
      if (target.classList.contains('eep-cbf-type')) {
        const row = target.closest('.eep-custom-builder-row');
        const currentValueEl = row ? row.querySelector('.eep-cbf-value') : null;
        const currentValue = currentValueEl ? String(currentValueEl.value || '') : '';
        refreshValueControl(row, currentValue);
      } else if (target.classList.contains('eep-cbf-placeholder')) {
        const row = target.closest('.eep-custom-builder-row');
        refreshValueControl(row);
      }
      syncHidden();
    });

    body.addEventListener('input', function () {
      syncHidden();
    });

    form.addEventListener('submit', function () {
      syncHidden();
    });

    ensureEmptyState();
    syncHidden();
  }

  function renderCustomGalleryPreview(targetId) {
    const input = document.getElementById(targetId);
    if (!input) {
      return;
    }
    const preview = document.querySelector('[data-eep-custom-gallery-preview="' + targetId + '"]');
    if (!preview) {
      return;
    }
    const rows = String(input.value || '')
      .split(/\r?\n/)
      .map(function (row) { return String(row || '').trim(); })
      .filter(function (row) { return row !== ''; });
    preview.innerHTML = '';
    rows.forEach(function (url) {
      const img = document.createElement('img');
      img.src = url;
      img.alt = 'Gallery preview';
      img.loading = 'lazy';
      preview.appendChild(img);
    });
  }

  function initCustomFieldUploads() {
    $(document).off('click.eepCustomUpload', '[data-eep-custom-upload-btn]').on('click.eepCustomUpload', '[data-eep-custom-upload-btn]', function (event) {
      event.preventDefault();
      const targetId = String($(this).attr('data-eep-target-input') || '').trim();
      if (!targetId) {
        return;
      }
      const input = document.querySelector('[data-eep-custom-upload-input][data-eep-target-input="' + targetId + '"]');
      if (input) {
        input.click();
      }
    });

    $(document).off('change.eepCustomUpload', '[data-eep-custom-upload-input]').on('change.eepCustomUpload', '[data-eep-custom-upload-input]', async function () {
      const fileInput = this;
      const files = Array.from(fileInput.files || []);
      const targetId = String(fileInput.getAttribute('data-eep-target-input') || '').trim();
      const kind = String(fileInput.getAttribute('data-eep-kind') || 'image').trim();
      const target = String(fileInput.getAttribute('data-eep-target') || 'custom_gallery').trim();
      const multiple = String(fileInput.getAttribute('data-eep-multiple') || '0') === '1';
      const targetInput = document.getElementById(targetId);
      if (!files.length || !targetInput) {
        fileInput.value = '';
        return;
      }

      const galleryMaxBytes = parseInt(getConfig('uploadGalleryMaxBytes', 2048000), 10) || 2048000;
      const galleryMaxFiles = parseInt(getConfig('uploadGalleryMaxFiles', 10), 10) || 10;
      const fileMaxBytes = parseInt(getConfig('uploadFileMaxBytes', 2097152), 10) || 2097152;
      const coverMaxBytes = parseInt(getConfig('uploadCoverMaxBytes', 512000), 10) || 512000;
      const maxBytes = kind === 'file' ? fileMaxBytes : (target === 'custom_image' ? coverMaxBytes : galleryMaxBytes);

      const existing = String(targetInput.value || '')
        .split(/\r?\n/)
        .map(function (row) { return String(row || '').trim(); })
        .filter(function (row) { return row !== ''; });

      for (let i = 0; i < files.length; i += 1) {
        const file = files[i];
        if (!file) {
          continue;
        }
        if (maxBytes > 0 && file.size > maxBytes) {
          showToast('Selected file is too large.', 'error');
          continue;
        }
        try {
          const payload = await uploadAssetFile(file, kind, target);
          const uploadedUrl = payload && payload.url ? String(payload.url) : '';
          if (!uploadedUrl) {
            continue;
          }
          if (multiple) {
            if (galleryMaxFiles > 0 && existing.length >= galleryMaxFiles) {
              showToast('Gallery upload limit reached.', 'error');
              break;
            }
            if (existing.indexOf(uploadedUrl) === -1) {
              existing.push(uploadedUrl);
            }
            targetInput.value = existing.join('\n');
            renderCustomGalleryPreview(targetId);
          } else {
            targetInput.value = uploadedUrl;
          }
        } catch (error) {
          showToast(error && error.message ? error.message : 'Upload failed.', 'error');
        }
      }

      fileInput.value = '';
    });

    $(document).off('input.eepCustomGalleryPreview', '[data-eep-custom-field][data-eep-field-type="gallery"] textarea')
      .on('input.eepCustomGalleryPreview', '[data-eep-custom-field][data-eep-field-type="gallery"] textarea', function () {
        const id = String(this.id || '').trim();
        if (!id) {
          return;
        }
        renderCustomGalleryPreview(id);
      });

    $('[data-eep-custom-gallery-preview]').each(function () {
      const targetId = String($(this).attr('data-eep-custom-gallery-preview') || '').trim();
      if (targetId) {
        renderCustomGalleryPreview(targetId);
      }
    });
  }

  function initSubmitUploaderFallback() {
    const form = document.getElementById('eep-submit-form');
    if (!form) {
      return;
    }
    const fallbackRetryCount = parseInt(form.getAttribute('data-eep-fallback-retries') || '0', 10) || 0;
    if (window.Alpine && !form._x_dataStack && !form.__x && form.getAttribute('data-eep-alpine-bound') !== '1' && fallbackRetryCount < 4) {
      form.setAttribute('data-eep-fallback-retries', String(fallbackRetryCount + 1));
      window.setTimeout(function () {
        initSubmitUploaderFallback();
      }, 350);
      return;
    }
    const alpineActive = !!(window.Alpine && (form._x_dataStack || form.__x));
    if (alpineActive) {
      return;
    }
    if (form.getAttribute('data-eep-fallback-upload-bound') === '1') {
      return;
    }
    form.setAttribute('data-eep-fallback-upload-bound', '1');

    const coverInput = form.querySelector('input[name="eep_cover_file"]');
    const galleryInput = form.querySelector('input[name="eep_gallery_files[]"]');
    const coverField = form.querySelector('input[name="eep_cover_image_url"]');
    const galleryField = form.querySelector('textarea[name="eep_gallery_urls"]');
    const coverZone = form.querySelector('.eep-dropzone[data-eep-upload-target="cover"]');
    const galleryZone = form.querySelector('.eep-dropzone[data-eep-upload-target="gallery"]');
    const galleryPreview = form.querySelector('.eep-gallery');

    const coverMaxBytes = parseInt(getConfig('uploadCoverMaxBytes', 512000), 10) || 512000;
    const galleryMaxBytes = parseInt(getConfig('uploadGalleryMaxBytes', 2048000), 10) || 2048000;
    const galleryMaxFiles = parseInt(getConfig('uploadGalleryMaxFiles', 10), 10) || 10;

    function parseGalleryRows() {
      return String((galleryField && galleryField.value) || '')
        .split(/\r?\n/)
        .map(function (row) { return String(row || '').trim(); })
        .filter(function (row) { return row !== ''; });
    }

    function renderGalleryRows(rows) {
      if (galleryField) {
        galleryField.value = rows.join('\n');
      }
      if (!galleryPreview) {
        return;
      }
      galleryPreview.innerHTML = '';
      rows.forEach(function (url) {
        const item = document.createElement('div');
        const img = document.createElement('img');
        img.src = url;
        img.alt = 'Gallery preview';
        img.loading = 'lazy';
        item.appendChild(img);
        galleryPreview.appendChild(item);
      });
    }

    function renderCover(url) {
      if (!coverZone || !url) {
        return;
      }
      coverZone.innerHTML = '';
      const img = document.createElement('img');
      img.className = 'eep-upload-preview';
      img.src = url;
      img.alt = 'Cover preview';
      coverZone.appendChild(img);
    }

    async function uploadFiles(files, mode) {
      const queue = Array.from(files || []);
      if (!queue.length) {
        return;
      }
      let rows = parseGalleryRows();
      for (let i = 0; i < queue.length; i += 1) {
        const file = queue[i];
        if (!file) {
          continue;
        }
        if (mode === 'cover' && file.size > coverMaxBytes) {
          showToast('Cover image is too large.', 'error');
          continue;
        }
        if (mode === 'gallery' && file.size > galleryMaxBytes) {
          showToast('One of the gallery images is too large.', 'error');
          continue;
        }
        if (mode === 'gallery' && galleryMaxFiles > 0 && rows.length >= galleryMaxFiles) {
          showToast('Gallery upload limit reached.', 'error');
          break;
        }
        try {
          const payload = await uploadAssetFile(file, 'image', mode);
          const url = payload && payload.url ? String(payload.url) : '';
          if (!url) {
            continue;
          }
          if (mode === 'cover') {
            if (coverField) {
              coverField.value = url;
            }
            renderCover(url);
          } else if (rows.indexOf(url) === -1) {
            rows.push(url);
            renderGalleryRows(rows);
          }
        } catch (error) {
          showToast(error && error.message ? error.message : 'Upload failed. Please try again.', 'error');
        }
      }
    }

    if (coverZone && coverInput) {
      coverZone.addEventListener('click', function () {
        coverInput.click();
      });
      coverZone.addEventListener('dragover', function (event) {
        event.preventDefault();
      });
      coverZone.addEventListener('drop', function (event) {
        event.preventDefault();
        uploadFiles(event.dataTransfer && event.dataTransfer.files ? event.dataTransfer.files : [], 'cover');
      });
      coverInput.addEventListener('change', function () {
        uploadFiles(coverInput.files || [], 'cover');
        coverInput.value = '';
      });
    }

    if (galleryZone && galleryInput) {
      galleryZone.addEventListener('click', function () {
        galleryInput.click();
      });
      galleryZone.addEventListener('dragover', function (event) {
        event.preventDefault();
      });
      galleryZone.addEventListener('drop', function (event) {
        event.preventDefault();
        uploadFiles(event.dataTransfer && event.dataTransfer.files ? event.dataTransfer.files : [], 'gallery');
      });
      galleryInput.addEventListener('change', function () {
        uploadFiles(galleryInput.files || [], 'gallery');
        galleryInput.value = '';
      });
    }

    if (coverField && String(coverField.value || '').trim() !== '') {
      renderCover(String(coverField.value || '').trim());
    }
    renderGalleryRows(parseGalleryRows());
  }

  function initEventCountdown() {
    const nodes = document.querySelectorAll('[data-eep-countdown]');
    if (!nodes.length) {
      return;
    }

    nodes.forEach(function (node) {
      const targetUnix = parseInt(String(node.getAttribute('data-eep-countdown') || '0'), 10);
      if (!targetUnix || targetUnix <= 0) {
        node.remove();
        return;
      }
      const dayNode = node.querySelector('[data-eep-days]');
      const hourNode = node.querySelector('[data-eep-hours]');
      const minNode = node.querySelector('[data-eep-mins]');
      const secNode = node.querySelector('[data-eep-secs]');

      const tick = function () {
        const now = Math.floor(Date.now() / 1000);
        let diff = targetUnix - now;
        if (diff <= 0) {
          node.setAttribute('data-ended', '1');
          if (dayNode) dayNode.textContent = '0';
          if (hourNode) hourNode.textContent = '0';
          if (minNode) minNode.textContent = '0';
          if (secNode) secNode.textContent = '0';
          return;
        }
        const days = Math.floor(diff / 86400);
        diff -= days * 86400;
        const hours = Math.floor(diff / 3600);
        diff -= hours * 3600;
        const mins = Math.floor(diff / 60);
        const secs = diff - mins * 60;
        if (dayNode) dayNode.textContent = String(days);
        if (hourNode) hourNode.textContent = String(hours).padStart(2, '0');
        if (minNode) minNode.textContent = String(mins).padStart(2, '0');
        if (secNode) secNode.textContent = String(secs).padStart(2, '0');
      };

      tick();
      window.setInterval(tick, 1000);
    });
  }

  function ensureRecaptchaLoaded(siteKey, version, cb) {
    const callback = typeof cb === 'function' ? cb : function () {};
    const requiredMethod = version === 'v2' ? 'render' : 'execute';
    const hasRequiredApi = function () {
      return !!(window.grecaptcha && typeof window.grecaptcha[requiredMethod] === 'function');
    };
    if (!siteKey) {
      callback(false);
      return;
    }
    if (hasRequiredApi()) {
      callback(true);
      return;
    }

    let resolved = false;
    const done = function (ok) {
      if (resolved) {
        return;
      }
      resolved = true;
      callback(ok !== false);
    };
    const waitForApi = function () {
      if (hasRequiredApi()) {
        done(true);
        return;
      }
      let attempts = 0;
      const timer = window.setInterval(function () {
        attempts += 1;
        if (hasRequiredApi()) {
          window.clearInterval(timer);
          done(true);
          return;
        }
        if (attempts > 80) {
          window.clearInterval(timer);
          done(false);
        }
      }, 100);
    };

    const existing = document.getElementById('eep-recaptcha-script');
    if (existing) {
      if (hasRequiredApi()) {
        done(true);
        return;
      }
      existing.addEventListener('load', waitForApi, { once: true });
      existing.addEventListener('error', function () { done(false); }, { once: true });
      waitForApi();
      return;
    }
    const script = document.createElement('script');
    script.id = 'eep-recaptcha-script';
    if (version === 'v2') {
      script.src = 'https://www.google.com/recaptcha/api.js?render=explicit';
    } else {
      script.src = 'https://www.google.com/recaptcha/api.js?render=' + encodeURIComponent(siteKey);
    }
    script.async = true;
    script.defer = true;
    script.addEventListener('load', waitForApi, { once: true });
    script.addEventListener('error', function () { done(false); }, { once: true });
    document.head.appendChild(script);
  }

  function initRecaptchaForms() {
    const siteKey = String(getConfig('recaptchaSiteKey', '') || '').trim();
    const version = String(getConfig('recaptchaVersion', 'v3') || 'v3').toLowerCase() === 'v2' ? 'v2' : 'v3';
    const showSubmitField = !!getConfig('recaptchaShowSubmit', false);
    const bypassSubmitRecaptcha = !!getConfig('recaptchaBypassSubmitForPrivileged', false);
    if (!siteKey) {
      return;
    }

    let $forms = $('form').has('.eep-recaptcha-token');
    if (bypassSubmitRecaptcha) {
      $forms = $forms.filter(function () {
        return String(this.id || '') !== 'eep-submit-form';
      });
    }
    if (!$forms.length) {
      return;
    }

    if (version === 'v2') {
      ensureRecaptchaLoaded(siteKey, version, function (loaded) {
        if (!loaded || !window.grecaptcha || typeof window.grecaptcha.render !== 'function') {
          showToast(getMessage('recaptchaLoadFailed', 'reCAPTCHA failed to load.'), 'error');
          return;
        }
        $forms.each(function () {
          const $form = $(this);
          if (String($form.attr('data-eep-recaptcha-ready') || '') === '1') {
            return;
          }
          const $token = $form.find('.eep-recaptcha-token').first();
          if (!$token.length) {
            return;
          }
          const mustShow = true;
          let $slot = $form.find('.eep-recaptcha-v2-slot').first();
          if (!$slot.length) {
            $slot = $('<div class="eep-recaptcha-v2-slot"></div>');
            const $submitArea = $form.find('button[type="submit"], input[type="submit"]').first().closest('.eep-form-actions, .eep-actions, .eep-submit-actions, p, div');
            if ($submitArea.length) {
              $submitArea.before($slot);
            } else {
              $form.append($slot);
            }
          }
          if (!mustShow) {
            $slot.hide();
          }
          const widgetId = window.grecaptcha.render($slot.get(0), {
            sitekey: siteKey,
            callback: function (tokenValue) {
              $token.val(String(tokenValue || ''));
            },
            'expired-callback': function () {
              $token.val('');
            }
          });
          $form.attr('data-eep-recaptcha-widget-id', String(widgetId));
          $form.attr('data-eep-recaptcha-ready', '1');
        });
      });

      $forms.on('submit', function (e) {
        const $form = $(this);
        const $token = $form.find('.eep-recaptcha-token').first();
        if ($token.length && String($token.val() || '').trim() !== '') {
          return;
        }
        e.preventDefault();
        showToast(getMessage('recaptchaRequired', 'Please complete the reCAPTCHA challenge.'), 'error');
      });
      return;
    }

    if (showSubmitField) {
      const $submitForm = $forms.filter('#eep-submit-form');
      if ($submitForm.length && !$submitForm.find('.eep-recaptcha-v3-hint').length) {
        const $hint = $('<p class="eep-recaptcha-v3-hint">').text('Protected by reCAPTCHA v3');
        const $actions = $submitForm.find('.eep-form-actions, .eep-submit-actions').first();
        if ($actions.length) {
          $actions.before($hint);
        } else {
          $submitForm.append($hint);
        }
      }
    }

    $forms.on('submit', function (e) {
      const $form = $(this);
      const $token = $form.find('.eep-recaptcha-token');
      if ($token.val()) {
        return;
      }
      e.preventDefault();

      ensureRecaptchaLoaded(siteKey, version, function (loaded) {
        if (!loaded || !window.grecaptcha || typeof window.grecaptcha.execute !== 'function') {
          showToast(getMessage('recaptchaLoadFailed', 'reCAPTCHA failed to load.'), 'error');
          return;
        }
        const executeCaptcha = function () {
          window.grecaptcha.execute(siteKey, { action: 'submit' }).then(function (token) {
            $token.val(token || '');
            $form.trigger('submit');
          }).catch(function () {
            showToast(getMessage('recaptchaLoadFailed', 'reCAPTCHA failed to load.'), 'error');
          });
        };
        if (typeof window.grecaptcha.ready === 'function') {
          window.grecaptcha.ready(executeCaptcha);
        } else {
          executeCaptcha();
        }
      });
    });
  }

  function initFormLoadingStates() {
    $(document).off('click.eepSubmitterHint', 'form button[type="submit"], form input[type="submit"]').on('click.eepSubmitterHint', 'form button[type="submit"], form input[type="submit"]', function () {
      const $btn = $(this);
      const $form = $btn.closest('form');
      if (!$form.length) {
        return;
      }
      const name = String($btn.attr('name') || '').trim();
      const value = String($btn.val() || $btn.attr('value') || $btn.text() || '').trim();
      $form.data('eepSubmitterName', name);
      $form.data('eepSubmitterValue', value);
    });

    $('form').filter(function () {
      return $(this).closest('.eep-layout, .eep-submit-wrap, .eep-my-events, .eep-auth-grid, .eep-event-details').length > 0;
    }).off('submit.eepFormLoading').on('submit.eepFormLoading', function (e) {
      if (typeof e.isDefaultPrevented === 'function' && e.isDefaultPrevented()) {
        return;
      }
      const $form = $(this);
      if ($form.data('eepSubmitting')) {
        return;
      }

      syncEditors();
      $form.data('eepSubmitting', true);
      $form.addClass('eep-is-submitting');

      const submitterName = String($form.data('eepSubmitterName') || '').trim();
      const submitterValue = String($form.data('eepSubmitterValue') || '').trim();
      if (submitterName !== '') {
        const $existingMirror = $form.find('input[data-eep-submitter-clone="1"][name="' + submitterName + '"]');
        if (!$existingMirror.length) {
          $('<input>', {
            type: 'hidden',
            name: submitterName,
            value: submitterValue,
            'data-eep-submitter-clone': '1'
          }).appendTo($form);
        } else {
          $existingMirror.val(submitterValue);
        }
      }

      $form.find('button[type="submit"], input[type="submit"]').each(function () {
        const $btn = $(this);
        if ($btn.prop('disabled')) {
          return;
        }
        if (!$btn.data('eepOriginalText')) {
          $btn.data('eepOriginalText', $btn.text());
        }
        $btn.prop('disabled', true).addClass('eep-loading');
      });
    });
  }

  function initSubmitEventAjax() {
    const form = document.getElementById('eep-submit-form');
    if (!form || form.getAttribute('data-eep-submit-ajax-bound') === '1') {
      return;
    }
    form.setAttribute('data-eep-submit-ajax-bound', '1');

    const parseNoticeFromUrl = function (url) {
      try {
        const parsedUrl = new URL(String(url || ''), window.location.origin);
        const message = String(parsedUrl.searchParams.get('eep_notice') || '').trim();
        if (!message) {
          return null;
        }
        let type = String(parsedUrl.searchParams.get('eep_notice_type') || 'success').trim().toLowerCase();
        if (type === 'warn') {
          type = 'warning';
        }
        if (['success', 'error', 'warning', 'info'].indexOf(type) < 0) {
          type = 'success';
        }
        return {
          message: message,
          type: type
        };
      } catch (error) {
        return null;
      }
    };

    form.addEventListener('submit', function (event) {
      const isEditing = String(form.getAttribute('data-eep-editing') || '0') === '1';
      if (!isEditing) {
        return;
      }
      if (event.defaultPrevented) {
        return;
      }

      let intent = '';
      const submitter = event.submitter || null;
      if (submitter && String(submitter.name || '') === 'eep_intent') {
        intent = String(submitter.value || '').trim().toLowerCase();
      }
      if (!intent) {
        const $formData = $(form);
        const submitterName = String($formData.data('eepSubmitterName') || '').trim();
        const submitterValue = String($formData.data('eepSubmitterValue') || '').trim();
        if (submitterName === 'eep_intent') {
          intent = submitterValue.toLowerCase();
        }
      }
      if (!intent) {
        intent = 'pending';
      }
      if (intent !== 'pending') {
        return;
      }

      event.preventDefault();
      if (typeof event.stopImmediatePropagation === 'function') {
        event.stopImmediatePropagation();
      }
      if (form.getAttribute('data-eep-ajax-submit-pending') === '1') {
        return;
      }
      form.setAttribute('data-eep-ajax-submit-pending', '1');

      syncEditors();
      const $form = $(form);
      const $buttons = $form.find('button[type="submit"], input[type="submit"]');
      $form.data('eepSubmitting', true).addClass('eep-is-submitting');
      $buttons.prop('disabled', true).addClass('eep-loading');

      const formData = new FormData(form);
      formData.set('eep_ajax', '1');
      formData.set('eep_intent', 'pending');
      if (submitter && String(submitter.name || '') !== '') {
        formData.set(String(submitter.name), String(submitter.value || 'pending'));
      }

      const actionUrl = String(form.getAttribute('action') || '/wp-admin/admin-post.php').trim() || '/wp-admin/admin-post.php';
      fetch(actionUrl, {
        method: 'POST',
        body: formData,
        credentials: 'same-origin',
        headers: {
          Accept: 'application/json',
          'X-Requested-With': 'XMLHttpRequest'
        }
      })
        .then(function (response) {
          return response.text().then(function (text) {
            return { response: response, text: text };
          });
        })
        .then(function (result) {
          const response = result.response;
          const text = String(result.text || '');
          let parsed = null;
          try {
            parsed = JSON.parse(text);
          } catch (error) {
            parsed = null;
          }

          if (parsed && typeof parsed === 'object' && Object.prototype.hasOwnProperty.call(parsed, 'success')) {
            const payload = parsed && typeof parsed.data === 'object' && parsed.data ? parsed.data : {};
            const defaultMessage = parsed.success ? 'Listing updated.' : getMessage('restActionFailed', 'Request failed. Please try again.');
            const message = String(payload.message || defaultMessage).trim();
            if (parsed.success) {
              showToast(message || 'Listing updated.', 'success');
              const eventIdRaw = payload.post_id || payload.edit_event_id || payload.event_id || '';
              const eventId = parseInt(String(eventIdRaw || ''), 10);
              if (Number.isFinite(eventId) && eventId > 0) {
                const editField = form.querySelector('input[name="eep_edit_post_id"]');
                if (editField) {
                  editField.value = String(eventId);
                }
                const autosaveField = form.querySelector('input[name="eep_autosave_post_id"]');
                if (autosaveField) {
                  autosaveField.value = String(eventId);
                }
                if (window.Alpine && form._x_dataStack && form._x_dataStack.length > 0) {
                  const scope = form._x_dataStack[0];
                  if (scope && typeof scope === 'object') {
                    scope.autosavePostId = String(eventId);
                  }
                }
                try {
                  const nextUrl = new URL(window.location.href);
                  nextUrl.searchParams.set('eep_tab', 'submit');
                  nextUrl.searchParams.set('edit_event_id', String(eventId));
                  nextUrl.hash = 'submit';
                  window.history.replaceState(window.history.state || {}, '', nextUrl.toString());
                } catch (error) {
                  // ignore malformed URL state
                }
              }
            } else {
              let failType = String(payload.type || 'error').toLowerCase();
              if (['warning', 'info'].indexOf(failType) < 0) {
                failType = 'error';
              }
              showToast(message || getMessage('restActionFailed', 'Request failed. Please try again.'), failType);
            }
            return;
          }

          const notice = parseNoticeFromUrl(response && response.url ? response.url : '');
          if (notice && notice.message) {
            showToast(notice.message, notice.type || 'info');
            return;
          }

          if (response && response.ok) {
            showToast('Listing updated.', 'success');
            return;
          }

          showToast(getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
        })
        .catch(function (error) {
          showToast(error && error.message ? error.message : getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
        })
        .finally(function () {
          form.removeAttribute('data-eep-ajax-submit-pending');
          $form.data('eepSubmitting', false).removeClass('eep-is-submitting');
          $buttons.prop('disabled', false).removeClass('eep-loading');
        });
    }, false);
  }

  function initModerationActionsAjax() {
    const ajaxEnabled = !!getConfig('enableModerationAjax', false);
    if (!ajaxEnabled) {
      return;
    }
    const singleEndpoint = String(getConfig('moderationRestUrl', '') || '');
    const bulkEndpoint = String(getConfig('moderationBulkRestUrl', '') || '');

    $(document).off('click.eepModerationAction', '[data-eep-moderation-submit]').on('click.eepModerationAction', '[data-eep-moderation-submit]', function (event) {
      const $btn = $(this);
      const $form = $btn.closest('form.eep-moderation-row-form');
      if (!$form.length) {
        return;
      }
      if (String($form.attr('data-eep-bypass-rest') || '') === '1') {
        $form.removeAttr('data-eep-bypass-rest');
        return;
      }
      const decision = String($btn.attr('data-eep-moderation-submit') || '').trim();
      const $decisionInput = $form.find('input[name="decision"]').first();
      if ($decisionInput.length) {
        $decisionInput.val(decision);
      }
      if (!singleEndpoint) {
        return;
      }

      event.preventDefault();
      event.stopPropagation();

      const postId = parseInt($form.find('input[name="post_id"]').val() || $form.find('input[name="event_id"]').val() || '0', 10);
      const note = String($form.find('[name="moderator_note"]').val() || '').trim();
      if (!postId || !decision) {
        showToast(getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
        return;
      }

      const $row = $form.closest('tr');
      const $buttons = $form.find('[data-eep-moderation-submit]');
      $buttons.prop('disabled', true).addClass('eep-loading');

      restPost(singleEndpoint, {
        post_id: postId,
        event_id: postId,
        decision: decision,
        moderator_note: note
      })
        .then(function (payload) {
          showToast((payload && payload.message) ? payload.message : 'Moderation updated.', 'success');
          if ($row.length) {
            $row.fadeOut(180, function () {
              $row.remove();
            });
          }
        })
        .catch(function (err) {
          if ($form.length) {
            $form.attr('data-eep-bypass-rest', '1');
            if ($decisionInput.length) {
              $decisionInput.val(decision);
            }
            const formEl = $form.get(0);
            const btnEl = $btn.get(0);
            if (formEl && typeof formEl.requestSubmit === 'function' && btnEl) {
              formEl.requestSubmit(btnEl);
            } else if (formEl && typeof formEl.submit === 'function') {
              formEl.submit();
            }
            return;
          }
          showToast(err && err.message ? err.message : getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
        })
        .finally(function () {
          $buttons.prop('disabled', false).removeClass('eep-loading');
        });
    });

    $(document).off('submit.eepModerationBulkAjax', '.eep-moderation-bulk-form').on('submit.eepModerationBulkAjax', '.eep-moderation-bulk-form', function (event) {
      const $form = $(this);
      if (String($form.attr('data-eep-bypass-rest') || '') === '1') {
        $form.removeAttr('data-eep-bypass-rest');
        return;
      }
      if (!bulkEndpoint) {
        return;
      }
      event.preventDefault();

      const formId = String($form.attr('id') || '').trim();
      const $decisionField = $form.find('[name="bulk_decision"], [name="decision"]').first();
      const decision = String($decisionField.val() || '').trim();
      const note = String($form.find('[name="moderator_note"]').val() || '').trim();
      const selector = formId ? 'input[name="post_ids[]"][form="' + formId + '"]:checked' : 'input[name="post_ids[]"]:checked';
      const ids = $(selector).map(function () {
        return parseInt($(this).val() || '0', 10);
      }).get().filter(function (id) {
        return Number.isFinite(id) && id > 0;
      });

      if (!ids.length || !decision) {
        showToast('Select events and a valid action first.', 'error');
        return;
      }

      const $submit = $form.find('button[type="submit"]').first();
      if ($submit.length) {
        $submit.prop('disabled', true).addClass('eep-loading');
      }

      restPost(bulkEndpoint, {
        post_ids: ids,
        decision: decision,
        moderator_note: note
      })
        .then(function (payload) {
          const errors = Array.isArray(payload && payload.errors) ? payload.errors : [];
          const failedIds = {};
          errors.forEach(function (row) {
            const id = parseInt(row && row.post_id, 10);
            if (Number.isFinite(id) && id > 0) {
              failedIds[id] = true;
            }
          });

          ids.forEach(function (postId) {
            if (failedIds[postId]) {
              return;
            }
            const $checkbox = $('input[name="post_ids[]"][value="' + String(postId) + '"]');
            const $row = $checkbox.closest('tr');
            if ($row.length) {
              $row.fadeOut(180, function () {
                $row.remove();
              });
            }
          });

          showToast((payload && payload.message) ? payload.message : 'Bulk moderation complete.', (payload && payload.failed) ? 'error' : 'success');
        })
        .catch(function (err) {
          $form.attr('data-eep-bypass-rest', '1');
          const formEl = $form.get(0);
          if (formEl && typeof formEl.requestSubmit === 'function') {
            formEl.requestSubmit();
          } else if (formEl && typeof formEl.submit === 'function') {
            formEl.submit();
          } else {
            showToast(err && err.message ? err.message : getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
          }
        })
        .finally(function () {
          $form.data('eepSubmitting', false).removeClass('eep-is-submitting');
          if ($submit.length) {
            $submit.prop('disabled', false).removeClass('eep-loading');
          }
        });
    });
  }

  function initModerationBulkActions() {
    const syncMasterCheckbox = function (formId) {
      const selector = 'input[name="post_ids[]"][form="' + formId + '"]';
      const $rows = $(selector);
      const $master = $('[data-eep-check-all][data-eep-target-form="' + formId + '"]');
      if (!$rows.length || !$master.length) {
        return;
      }
      const total = $rows.length;
      const checked = $rows.filter(':checked').length;
      const masterEl = $master.get(0);
      if (!masterEl) {
        return;
      }
      masterEl.indeterminate = checked > 0 && checked < total;
      masterEl.checked = checked > 0 && checked === total;
    };

    $(document).off('change.eepModerationAll', '[data-eep-check-all]').on('change.eepModerationAll', '[data-eep-check-all]', function () {
      const formId = String($(this).attr('data-eep-target-form') || 'eep-moderation-bulk-form').trim();
      if (!formId) {
        return;
      }
      const checked = !!this.checked;
      $('input[name="post_ids[]"][form="' + formId + '"]').prop('checked', checked);
      syncMasterCheckbox(formId);
    });

    $(document).off('change.eepModerationRow', 'input[name="post_ids[]"][form]').on('change.eepModerationRow', 'input[name="post_ids[]"][form]', function () {
      const formId = String($(this).attr('form') || '').trim();
      if (!formId) {
        return;
      }
      syncMasterCheckbox(formId);
    });

    $('[data-eep-check-all]').each(function () {
      const formId = String($(this).attr('data-eep-target-form') || 'eep-moderation-bulk-form').trim();
      if (formId) {
        syncMasterCheckbox(formId);
      }
    });
  }

  function initMyEventsActionsAjax() {
    const $scope = $('.eep-my-events-actions');
    if (!$scope.length) {
      return;
    }

    const setStatusPill = function ($row, status, statusLabel) {
      const $pill = $row.find('.eep-status-pill').first();
      if (!$pill.length) {
        return;
      }
      const pillEl = $pill.get(0);
      if (!pillEl) {
        return;
      }
      Array.from(pillEl.classList || []).forEach(function (className) {
        if (String(className || '').indexOf('eep-status-') === 0 && className !== 'eep-status-pill') {
          pillEl.classList.remove(className);
        }
      });
      const cleanStatus = String(status || '').trim().toLowerCase();
      if (cleanStatus) {
        pillEl.classList.add('eep-status-' + cleanStatus);
      }
      const nextLabel = String(statusLabel || '').trim();
      if (nextLabel) {
        $pill.text(nextLabel);
      }
    };

    const setPendingMeta = function ($form, pending) {
      let $meta = $form.find('.eep-live-toggle-meta').first();
      if (pending) {
        if (!$meta.length) {
          const metaNode = document.createElement('small');
          metaNode.className = 'eep-live-toggle-meta';
          metaNode.textContent = 'Pending moderation';
          const input = $form.find('[data-eep-live-toggle-input="1"]').first();
          if (input.length) {
            input.closest('.eep-live-toggle').after(metaNode);
          } else {
            $form.append(metaNode);
          }
          $meta = $(metaNode);
        } else {
          $meta.text('Pending moderation');
        }
      } else if ($meta.length) {
        $meta.remove();
      }
    };

    const submitNative = function (form) {
      form.setAttribute('data-eep-my-events-bypass', '1');
      if (typeof form.requestSubmit === 'function') {
        form.requestSubmit();
        return;
      }
      if (typeof form.submit === 'function') {
        form.submit();
      }
    };

    $(document)
      .off('submit.eepMyEventsActionsAjax', '.eep-my-events-actions form')
      .on('submit.eepMyEventsActionsAjax', '.eep-my-events-actions form', function (event) {
        const form = this;
        if (!form) {
          return;
        }
        if (String(form.getAttribute('data-eep-my-events-bypass') || '') === '1') {
          form.removeAttribute('data-eep-my-events-bypass');
          return;
        }

        const $form = $(form);
        const actionName = String($form.find('input[name="action"]').first().val() || '').trim();
        if (['eep_toggle_event_live_draft', 'eep_delete_event'].indexOf(actionName) < 0) {
          return;
        }

        event.preventDefault();
        if (form.getAttribute('data-eep-my-events-pending') === '1') {
          return;
        }
        form.setAttribute('data-eep-my-events-pending', '1');

        const $row = $form.closest('tr');
        const $controls = $form.find('button, input, select');
        $controls.prop('disabled', true).addClass('eep-loading');

        const liveToggleInput = form.querySelector('[data-eep-live-toggle-input="1"]');
        const previousChecked = !!(liveToggleInput && liveToggleInput.checked);

        const formData = new FormData(form);
        formData.set('eep_ajax', '1');

        const actionUrl = String(form.getAttribute('action') || '/wp-admin/admin-post.php').trim() || '/wp-admin/admin-post.php';
        fetch(actionUrl, {
          method: 'POST',
          body: formData,
          credentials: 'same-origin',
          headers: {
            Accept: 'application/json',
            'X-Requested-With': 'XMLHttpRequest'
          }
        })
          .then(function (response) {
            return response.text().then(function (text) {
              return { response: response, text: text };
            });
          })
          .then(function (result) {
            let parsed = null;
            try {
              parsed = JSON.parse(String(result && result.text ? result.text : ''));
            } catch (error) {
              parsed = null;
            }

            if (!parsed || typeof parsed !== 'object' || !Object.prototype.hasOwnProperty.call(parsed, 'success')) {
              submitNative(form);
              return;
            }

            const payload = parsed && typeof parsed.data === 'object' && parsed.data ? parsed.data : {};
            const message = String(payload.message || (parsed.success ? 'Saved.' : getMessage('restActionFailed', 'Request failed. Please try again.'))).trim();
            if (!parsed.success) {
              showToast(message || getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
              if (liveToggleInput) {
                liveToggleInput.checked = previousChecked;
              }
              return;
            }

            showToast(message || 'Saved.', 'success');

            if (actionName === 'eep_delete_event') {
              if ($row.length) {
                $row.fadeOut(180, function () {
                  $row.remove();
                });
              }
              return;
            }

            if (actionName === 'eep_toggle_event_live_draft') {
              const nextStatus = String(payload.status || '').trim().toLowerCase();
              const nextStatusLabel = String(payload.status_label || '').trim();
              setStatusPill($row, nextStatus, nextStatusLabel);

              if (liveToggleInput) {
                if (Object.prototype.hasOwnProperty.call(payload, 'state_live')) {
                  liveToggleInput.checked = !!payload.state_live;
                } else {
                  liveToggleInput.checked = nextStatus === 'publish';
                }
              }

              setPendingMeta($form, !!payload.pending_moderation);
            }
          })
          .catch(function (error) {
            showToast(error && error.message ? error.message : getMessage('restActionFailed', 'Request failed. Please try again.'), 'error');
            if (liveToggleInput) {
              liveToggleInput.checked = previousChecked;
            }
          })
          .finally(function () {
            form.removeAttribute('data-eep-my-events-pending');
            $controls.prop('disabled', false).removeClass('eep-loading');
          });
      });
  }

  function initMyEventsAttendeesModal() {
    const $modal = $('#eep-attendees-modal');
    if (!$modal.length) {
      return;
    }
    const $close = $('#eep-attendees-modal-close');

    function closeModal() {
      $modal.attr('hidden', 'hidden');
      $('body').removeClass('eep-lightbox-open');
      try {
        const url = new URL(window.location.href);
        if (url.searchParams.has('attendees_event')) {
          url.searchParams.delete('attendees_event');
          window.history.replaceState({}, document.title, url.toString());
        }
      } catch (_err) {
        // no-op
      }
    }

    if ($modal.attr('data-open') === '1') {
      $modal.removeAttr('hidden');
      $('body').addClass('eep-lightbox-open');
    }

    $close.off('click.eepAttendeesClose').on('click.eepAttendeesClose', function () {
      closeModal();
    });

    $modal.off('click.eepAttendeesBackdrop').on('click.eepAttendeesBackdrop', function (event) {
      if (event.target === $modal.get(0)) {
        closeModal();
      }
    });

    $(document).off('keydown.eepAttendeesEsc').on('keydown.eepAttendeesEsc', function (event) {
      if (event.key === 'Escape' && !$modal.attr('hidden')) {
        closeModal();
      }
    });
  }

  function getInboxLastSeenStorageKey(userId) {
    return 'eepInboxLastSeenMessageId:' + String(userId || 0);
  }

  function scrollInboxChatToBottom(scope) {
    const root = scope && typeof scope.querySelector === 'function' ? scope : document;
    const chatArea = root.querySelector('#eep-chat-scroll-area');
    if (chatArea) {
      chatArea.scrollTop = chatArea.scrollHeight;
      return;
    }

    const inboxMessages = root.querySelector('[data-eep-inbox-messages], .eep-inbox-messages');
    if (inboxMessages) {
      inboxMessages.scrollTop = inboxMessages.scrollHeight;
    }
  }

  function initInboxMessageFocus() {
    const highlighted = document.querySelector('.eep-inbox-message-highlight');
    if (highlighted && typeof highlighted.scrollIntoView === 'function') {
      window.setTimeout(function () {
        highlighted.scrollIntoView({ behavior: 'smooth', block: 'center' });
      }, 120);
      return;
    }

    window.setTimeout(function () {
      scrollInboxChatToBottom(document);
    }, 80);
  }

  function initInboxThreadNavigationAjax() {
    if (typeof window.fetch !== 'function' || typeof window.DOMParser !== 'function') {
      return;
    }

    const getInboxPanel = function (root) {
      if (!root || typeof root.querySelector !== 'function') {
        return null;
      }
      return root.querySelector('[data-eep-inbox-panel="1"], .eep-inbox-panel') || null;
    };

    const replaceInboxShellSections = function (currentShell, nextShell) {
      const hydratedNodes = [];
      if (!currentShell || !nextShell) {
        return hydratedNodes;
      }

      const currentPanel = getInboxPanel(currentShell);
      const nextPanel = getInboxPanel(nextShell);
      if (currentPanel && nextPanel) {
        const nextPanelClone = nextPanel.cloneNode(true);
        currentPanel.replaceWith(nextPanelClone);
        hydratedNodes.push(nextPanelClone);
      }

      return hydratedNodes;
    };

    const inboxShells = document.querySelectorAll('[data-eep-inbox-shell="1"]');
    if (!inboxShells.length) {
      return;
    }

    inboxShells.forEach(function (shell) {
      if (!shell || shell.getAttribute('data-eep-inbox-ajax-bound') === '1') {
        return;
      }
      shell.setAttribute('data-eep-inbox-ajax-bound', '1');

      shell.addEventListener('click', function (event) {
        const link = event.target && event.target.closest
          ? event.target.closest('a[data-eep-inbox-thread-link="1"][href]')
          : null;
        if (!link || !shell.contains(link)) {
          return;
        }
        if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) {
          return;
        }

        const href = String(link.getAttribute('href') || link.href || '').trim();
        if (!href || href === '#') {
          return;
        }

        let requestUrl;
        try {
          requestUrl = new URL(href, window.location.href);
        } catch (_err) {
          return;
        }
        if (requestUrl.origin !== window.location.origin) {
          return;
        }
        try {
          const currentUrl = new URL(window.location.href);
          const threadValue = String(requestUrl.searchParams.get('thread') || '').trim();
          const messageValue = String(requestUrl.searchParams.get('message') || '').trim();
          const nextUrl = new URL(currentUrl.toString());
          if (threadValue !== '') {
            nextUrl.searchParams.set('thread', threadValue);
          } else {
            nextUrl.searchParams.delete('thread');
          }
          if (messageValue !== '') {
            nextUrl.searchParams.set('message', messageValue);
          } else {
            nextUrl.searchParams.delete('message');
          }
          nextUrl.searchParams.set('eep_tab', 'inbox');
          nextUrl.hash = 'inbox';
          requestUrl = nextUrl;
        } catch (_err) {
          return;
        }

        event.preventDefault();
        if (shell.getAttribute('data-eep-inbox-loading') === '1') {
          return;
        }
        shell.setAttribute('data-eep-inbox-loading', '1');
        shell.classList.add('is-loading');

        fetch(requestUrl.toString(), { credentials: 'same-origin' })
          .then(function (res) {
            if (!res.ok) {
              throw new Error('inbox_thread_fetch_failed');
            }
            return res.text();
          })
          .then(function (html) {
            const parser = new window.DOMParser();
            const doc = parser.parseFromString(String(html || ''), 'text/html');
            const nextShell = doc.querySelector('[data-eep-inbox-shell="1"]');
            const currentShell = shell && shell.isConnected
              ? shell
              : document.querySelector('[data-eep-inbox-shell="1"]');
            if (!nextShell || !currentShell) {
              throw new Error('inbox_thread_shell_missing');
            }

            const hydratedNodes = replaceInboxShellSections(currentShell, nextShell);
            if (!hydratedNodes.length) {
              throw new Error('inbox_thread_panel_missing');
            }

            if (window.history && typeof window.history.pushState === 'function') {
              window.history.pushState({}, '', requestUrl.toString());
            }

            if (window.Alpine && typeof window.Alpine.initTree === 'function') {
              hydratedNodes.forEach(function (node) {
                if (node && typeof node.querySelector === 'function') {
                  window.Alpine.initTree(node);
                }
              });
            }

            try {
              window.dispatchEvent(new window.CustomEvent('eep-inbox-open-thread'));
            } catch (_err) {
              // no-op
            }

            scrollInboxChatToBottom(document);
            initInboxMessageFocus();
            initFormLoadingStates();
            initInboxReplyAjax();
          })
          .catch(function () {
            window.location.href = requestUrl.toString();
          })
          .finally(function () {
            const activeShell = shell && shell.isConnected
              ? shell
              : document.querySelector('[data-eep-inbox-shell="1"]');
            if (activeShell) {
              activeShell.removeAttribute('data-eep-inbox-loading');
              activeShell.classList.remove('is-loading');
            }
          });
      });
    });
  }

  function initInboxReplyAjax() {
    if (typeof window.fetch !== 'function' || typeof window.DOMParser !== 'function') {
      return;
    }

    const getInboxPanel = function (root) {
      if (!root || typeof root.querySelector !== 'function') {
        return null;
      }
      return root.querySelector('[data-eep-inbox-panel="1"], .eep-inbox-panel') || null;
    };

    const getInboxThreadList = function (root) {
      if (!root || typeof root.querySelector !== 'function') {
        return null;
      }
      return root.querySelector('[data-eep-inbox-thread-list="1"], .eep-inbox-thread-list') || null;
    };

    const replaceInboxShellSections = function (currentShell, nextShell) {
      const hydratedNodes = [];
      if (!currentShell || !nextShell) {
        return hydratedNodes;
      }

      const currentPanel = getInboxPanel(currentShell);
      const nextPanel = getInboxPanel(nextShell);
      if (currentPanel && nextPanel) {
        const nextPanelClone = nextPanel.cloneNode(true);
        currentPanel.replaceWith(nextPanelClone);
        hydratedNodes.push(nextPanelClone);
      }

      const currentThreadList = getInboxThreadList(currentShell);
      const nextThreadList = getInboxThreadList(nextShell);
      if (currentThreadList && nextThreadList) {
        const nextThreadListClone = nextThreadList.cloneNode(true);
        currentThreadList.replaceWith(nextThreadListClone);
        hydratedNodes.push(nextThreadListClone);
      }

      return hydratedNodes;
    };

    $(document)
      .off('submit.eepInboxReplyAjax', 'form[data-eep-inbox-reply-form="1"]')
      .on('submit.eepInboxReplyAjax', 'form[data-eep-inbox-reply-form="1"]', function (event) {
        const form = this;
        if (!form) {
          return;
        }
        if (String(form.getAttribute('data-eep-inbox-reply-bypass') || '') === '1') {
          form.removeAttribute('data-eep-inbox-reply-bypass');
          return;
        }
        if (String(form.getAttribute('data-eep-inbox-reply-pending') || '') === '1') {
          event.preventDefault();
          return;
        }

        const shell = form.closest('[data-eep-inbox-shell="1"]');
        if (!shell) {
          return;
        }

        event.preventDefault();
        form.setAttribute('data-eep-inbox-reply-pending', '1');

        const $form = $(form);
        const $controls = $form.find('button, textarea');
        $controls.prop('disabled', true).addClass('eep-loading');

        const actionUrl = String(form.getAttribute('action') || '/wp-admin/admin-post.php').trim() || '/wp-admin/admin-post.php';
        const formData = new FormData(form);

        fetch(actionUrl, {
          method: 'POST',
          body: formData,
          credentials: 'same-origin',
          headers: {
            'X-Requested-With': 'XMLHttpRequest'
          }
        })
          .then(function (res) {
            return res.text().then(function (text) {
              return { response: res, text: text };
            });
          })
          .then(function (result) {
            const response = result && result.response ? result.response : null;
            const finalUrl = response && typeof response.url === 'string' ? response.url : '';
            const parser = new window.DOMParser();
            const doc = parser.parseFromString(String((result && result.text) || ''), 'text/html');
            const nextShell = doc.querySelector('[data-eep-inbox-shell="1"]');
            const currentShell = shell && shell.isConnected
              ? shell
              : document.querySelector('[data-eep-inbox-shell="1"]');

            if (nextShell && currentShell) {
              const hydratedNodes = replaceInboxShellSections(currentShell, nextShell);
              if (hydratedNodes.length) {
                if (finalUrl && window.history && typeof window.history.replaceState === 'function') {
                  window.history.replaceState({}, '', finalUrl);
                }
                if (window.Alpine && typeof window.Alpine.initTree === 'function') {
                  hydratedNodes.forEach(function (node) {
                    if (node && typeof node.querySelector === 'function') {
                      window.Alpine.initTree(node);
                    }
                  });
                }
                try {
                  window.dispatchEvent(new window.CustomEvent('eep-inbox-open-thread'));
                } catch (_err) {
                  // no-op
                }
                const notice = parseNoticeFromUrl(finalUrl);
                if (notice && notice.message) {
                  showToast(notice.message, notice.type || 'success');
                } else {
                  showToast('Reply sent.', 'success');
                }
                scrollInboxChatToBottom(document);
                initInboxMessageFocus();
                initFormLoadingStates();
                return;
              }
            }

            if (finalUrl) {
              window.location.href = finalUrl;
              return;
            }

            window.location.reload();
          })
          .catch(function () {
            form.setAttribute('data-eep-inbox-reply-bypass', '1');
            if (typeof form.requestSubmit === 'function') {
              form.requestSubmit();
            } else if (typeof form.submit === 'function') {
              form.submit();
            }
          })
          .finally(function () {
            form.removeAttribute('data-eep-inbox-reply-pending');
            $controls.prop('disabled', false).removeClass('eep-loading');
          });
      });
  }

  let inboxPollTimer = null;
  let inboxPollBusy = false;
  let inboxPollSeeded = false;

  function initInboxPolling() {
    const endpoint = String(getConfig('inboxPollRestUrl', '') || '').trim();
    const restNonce = String(getConfig('restNonce', '') || '').trim();
    const userId = parseInt(String(getConfig('currentUserId', 0) || '0'), 10) || 0;
    if (!endpoint || !restNonce || userId <= 0) {
      return;
    }
    if (inboxPollTimer) {
      return;
    }

    const readStoredLastSeen = function () {
      let value = 0;
      try {
        value = parseInt(window.localStorage.getItem(getInboxLastSeenStorageKey(userId)) || '0', 10) || 0;
      } catch (_err) {
        value = 0;
      }
      return value > 0 ? value : 0;
    };

    const writeStoredLastSeen = function (messageId) {
      const cleanId = parseInt(String(messageId || '0'), 10) || 0;
      if (cleanId <= 0) {
        return;
      }
      try {
        window.localStorage.setItem(getInboxLastSeenStorageKey(userId), String(cleanId));
      } catch (_err) {
        // ignore storage failures
      }
    };

    let lastSeenMessageId = readStoredLastSeen();
    const poll = function () {
      if (inboxPollBusy) {
        return;
      }
      const syncedSeen = readStoredLastSeen();
      if (syncedSeen > lastSeenMessageId) {
        lastSeenMessageId = syncedSeen;
      }

      inboxPollBusy = true;
      const query = new URLSearchParams();
      if (lastSeenMessageId > 0) {
        query.set('last_seen_message_id', String(lastSeenMessageId));
      }
      const requestUrl = endpoint + (endpoint.indexOf('?') === -1 ? '?' : '&') + query.toString();
      fetch(requestUrl, {
        method: 'GET',
        credentials: 'same-origin',
        headers: {
          'X-WP-Nonce': restNonce
        }
      })
        .then(async function (res) {
          let payload = {};
          try {
            payload = await res.json();
          } catch (_err) {
            payload = {};
          }
          if (!res.ok) {
            throw new Error(String((payload && payload.message) || 'poll_failed'));
          }
          return payload || {};
        })
        .then(function (payload) {
          const latestMessageId = parseInt(String((payload && payload.latest_message_id) || '0'), 10) || 0;
          const hasNew = !!(payload && payload.has_new);
          const toastBody = String((payload && payload.toast_message) || getMessage('inboxToastBody', 'You have a new message in your inbox.')).trim();

          // Baseline unread state so existing unread history does not spam toasts on first page load.
          if (!inboxPollSeeded && lastSeenMessageId <= 0) {
            if (latestMessageId > 0) {
              lastSeenMessageId = latestMessageId;
              writeStoredLastSeen(lastSeenMessageId);
            }
            inboxPollSeeded = true;
            return;
          }

          inboxPollSeeded = true;
          if (latestMessageId > lastSeenMessageId) {
            if (hasNew) {
              showToast(toastBody, 'info', {
                title: String(getMessage('inboxToastTitle', 'New Inbox Message') || 'New Inbox Message').trim(),
                subtitle: String(getMessage('inboxToastSubtitle', 'Open your inbox to view and reply.') || 'Open your inbox to view and reply.').trim(),
                duration: 8500
              });
            }
            lastSeenMessageId = latestMessageId;
            writeStoredLastSeen(lastSeenMessageId);
          }
        })
        .catch(function () {
          // Silent failure to avoid noisy polling errors for users.
        })
        .finally(function () {
          inboxPollBusy = false;
        });
    };

    poll();
    inboxPollTimer = window.setInterval(poll, 25000);
  }

  function initElementorEventsAjaxSearch() {
    const restNonce = String(getConfig('restNonce', '') || '').trim();
    const fallbackError = String(getMessage('restActionFailed', 'Request failed. Please try again.') || '').trim();
    const noResultsLabel = String(getMessage('quickSearchNoResults', 'No matches yet. Try different keywords.') || '').trim();

    $('[data-eep-widget-search]').each(function () {
      const $widget = $(this);
      if ($widget.data('eepWidgetSearchBound') === 1) {
        return;
      }
      $widget.data('eepWidgetSearchBound', 1);

      let config = {};
      const configRaw = String($widget.attr('data-eep-widget-config') || '').trim();
      if (configRaw) {
        try {
          config = JSON.parse(configRaw) || {};
        } catch (_err) {
          config = {};
        }
      }

      const endpoint = String(config.endpoint || '').trim();
      if (!endpoint) {
        return;
      }
      const $form = $widget.find('[data-eep-widget-search-form]').first();
      const $resultsWrap = $widget.find('[data-eep-widget-search-results]').first();
      const $count = $widget.find('[data-eep-widget-search-count]').first();
      const defaults = config.defaults && typeof config.defaults === 'object' ? config.defaults : {};
      const minChars = Math.max(0, parseInt(config.minChars || 2, 10) || 0);
      let timer = null;
      let activeController = null;

      const updateCount = function (value) {
        if (!$count.length) {
          return;
        }
        const countNum = Math.max(0, parseInt(value || 0, 10) || 0);
        const suffix = countNum === 1 ? 'result' : 'results';
        $count.text(String(countNum) + ' ' + suffix);
      };

      const serializePayload = function () {
        const payload = {
          template: String(config.template || '1'),
          view: String(config.view || 'search'),
          limit: Math.max(1, parseInt(config.limit || 8, 10) || 8),
          orderby: String(config.orderby || 'start'),
          order: String(config.order || 'ASC'),
          tax_relation: String(config.taxRelation || 'AND'),
          custom_taxonomy: String(config.customTaxonomy || ''),
          custom_terms: String(config.customTerms || ''),
          include_past: String(config.includePast || 'no'),
          show_excerpt: String(config.showExcerpt || 'no'),
          show_meta: String(config.showMeta || 'yes'),
          excerpt_length: Math.max(8, parseInt(config.excerptLength || 14, 10) || 14)
        };
        const fields = ['search', 'event_type', 'event_mode', 'event_tag', 'event_price', 'city', 'date_from', 'official'];
        fields.forEach(function (fieldName) {
          const $field = $form.find('[name="' + fieldName + '"]').first();
          payload[fieldName] = $field.length ? String($field.val() || '').trim() : String(defaults[fieldName] || '').trim();
        });
        return payload;
      };

      const renderNoResults = function () {
        const label = noResultsLabel || 'No matches yet. Try different keywords.';
        $resultsWrap.html('<div class="eep-widget-empty">' + escapeHtml(label) + '</div>');
      };

      const requestResults = function () {
        const payload = serializePayload();
        if (minChars > 0 && payload.search && payload.search.length < minChars) {
          updateCount(0);
          renderNoResults();
          return;
        }

        if (activeController && typeof activeController.abort === 'function') {
          activeController.abort();
        }
        activeController = new AbortController();
        $widget.addClass('is-loading');

        const endpointUrl = new URL(endpoint, window.location.origin);
        Object.keys(payload).forEach(function (key) {
          const value = payload[key];
          if (value === '' || value === null || typeof value === 'undefined') {
            return;
          }
          endpointUrl.searchParams.set(key, String(value));
        });

        fetch(endpointUrl.toString(), {
          method: 'GET',
          credentials: 'same-origin',
          headers: restNonce ? { 'X-WP-Nonce': restNonce } : {},
          signal: activeController.signal
        })
          .then(async function (res) {
            let data = {};
            try {
              data = await res.json();
            } catch (_err) {
              data = {};
            }
            if (!res.ok) {
              throw new Error(String((data && data.message) || fallbackError || 'Request failed.'));
            }
            return data || {};
          })
          .then(function (data) {
            const html = String((data && data.html) || '').trim();
            if (html) {
              $resultsWrap.html(html);
            } else {
              renderNoResults();
            }
            const found = parseInt((data && (data.found || data.count)) || 0, 10) || 0;
            updateCount(found);
          })
          .catch(function (error) {
            if (error && error.name === 'AbortError') {
              return;
            }
            showToast((error && error.message) ? error.message : (fallbackError || 'Request failed.'), 'error');
          })
          .finally(function () {
            $widget.removeClass('is-loading');
          });
      };

      const queueRequest = function (delay) {
        if (timer) {
          window.clearTimeout(timer);
        }
        timer = window.setTimeout(requestResults, Math.max(40, parseInt(delay || 160, 10) || 160));
      };

      $form.off('submit.eepWidgetSearch').on('submit.eepWidgetSearch', function (event) {
        event.preventDefault();
        requestResults();
      });

      $form.off('input.eepWidgetSearch change.eepWidgetSearch').on('input.eepWidgetSearch change.eepWidgetSearch', 'input, select', function () {
        const fieldName = String($(this).attr('name') || '').trim();
        if (fieldName === 'search') {
          queueRequest(300);
          return;
        }
        queueRequest(120);
      });

      $widget.find('[data-eep-widget-search-reset]').off('click.eepWidgetSearch').on('click.eepWidgetSearch', function () {
        ['search', 'event_type', 'event_mode', 'event_tag', 'event_price', 'city', 'date_from', 'official'].forEach(function (fieldName) {
          const $field = $form.find('[name="' + fieldName + '"]').first();
          if (!$field.length) {
            return;
          }
          $field.val(typeof defaults[fieldName] !== 'undefined' ? String(defaults[fieldName]) : '');
        });
        requestResults();
      });
    });
  }

  function initEventChatbot() {
    const widgets = document.querySelectorAll('[data-eep-chatbot]');
    if (!widgets.length) {
      return;
    }

    const chatbotEnabled = toBool(getConfig('chatbotEnabled', false), false);
    const endpoint = String(getConfig('chatbotRestUrl', '') || '').trim();
    const askPlaceholder = String(getMessage('chatbotAskPlaceholder', 'Ask about listings, dates, locations, tags, or recommendations...') || '').trim();
    const emptyPrompt = String(getMessage('chatbotEmptyPrompt', 'Type a question first.') || '').trim();
    const typingLabel = String(getMessage('chatbotTyping', 'Thinking...') || '').trim();
    const genericError = String(getMessage('chatbotError', 'The assistant is unavailable right now. Please try again in a moment.') || '').trim();
    const botName = String(getConfig('chatbotName', 'Listing Assistant') || 'Listing Assistant').trim();
    const chatbotDebugMode = toBool(getConfig('chatbotDebugMode', false), false);
    const conversationLimit = 8;
    const panelTransitionMs = 220;

    if (!initEventChatbot._observerBound && 'MutationObserver' in window) {
      initEventChatbot._observerBound = true;
      const body = document.body;
      if (body) {
        const observer = new MutationObserver(function (mutations) {
          let shouldRebind = false;
          mutations.forEach(function (mutation) {
            if (shouldRebind) {
              return;
            }
            const nodes = mutation && mutation.addedNodes ? mutation.addedNodes : [];
            for (let i = 0; i < nodes.length; i += 1) {
              const node = nodes[i];
              if (!node || node.nodeType !== 1) {
                continue;
              }
              if ((node.matches && node.matches('[data-eep-chatbot]')) || (node.querySelector && node.querySelector('[data-eep-chatbot]'))) {
                shouldRebind = true;
                break;
              }
            }
          });
          if (shouldRebind) {
            window.requestAnimationFrame(function () {
              initEventChatbot();
            });
          }
        });
        observer.observe(body, { childList: true, subtree: true });
      }
    }

    function escapeHtml(value) {
      const input = String(value || '');
      return input
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#039;');
    }

    widgets.forEach(function (widget) {
      if (widget.getAttribute('data-eep-chatbot-bound') === '1') {
        return;
      }
      widget.setAttribute('data-eep-chatbot-bound', '1');

      const panel = widget.querySelector('[data-eep-chatbot-panel]');
      const toggle = widget.querySelector('[data-eep-chatbot-toggle]');
      const closeBtn = widget.querySelector('[data-eep-chatbot-close]');
      const form = widget.querySelector('[data-eep-chatbot-form]');
      const input = widget.querySelector('[data-eep-chatbot-input]');
      const sendBtn = widget.querySelector('[data-eep-chatbot-send]');
      const messages = widget.querySelector('[data-eep-chatbot-messages]');
      const quickFiltersWrap = widget.querySelector('[data-eep-chatbot-quick-filters]');
      const quickFilterButtons = quickFiltersWrap ? Array.from(quickFiltersWrap.querySelectorAll('[data-eep-chatbot-filter-key]')) : [];
      const desktopModes = ['bottom-right', 'bottom-left', 'right-edge-center', 'left-edge-center', 'inline'];
      const mobileModes = ['bottom-right', 'bottom-left', 'bottom-center', 'right-edge-center', 'left-edge-center', 'inline'];
      const allModes = ['bottom-right', 'bottom-left', 'bottom-center', 'right-edge-center', 'left-edge-center', 'inline'];
      const desktopModeRaw = String(
        widget.getAttribute('data-eep-chatbot-position-desktop') ||
        widget.getAttribute('data-eep-chatbot-position') ||
        getConfig('chatbotPositionDesktop', getConfig('chatbotPosition', 'bottom-right')) ||
        'bottom-right'
      ).trim();
      const mobileModeRaw = String(
        widget.getAttribute('data-eep-chatbot-position-mobile') ||
        getConfig('chatbotPositionMobile', 'right-edge-center') ||
        desktopModeRaw
      ).trim();
      const positionMediaQuery = window.matchMedia ? window.matchMedia('(max-width: 900px)') : null;
      const sanitizeMode = function (value, allowed, fallback) {
        const modeValue = String(value || '').trim();
        if (allowed.indexOf(modeValue) >= 0) {
          return modeValue;
        }
        return fallback;
      };
      const desktopMode = sanitizeMode(desktopModeRaw, desktopModes, 'bottom-right');
      const mobileMode = sanitizeMode(mobileModeRaw, mobileModes, 'right-edge-center');
      let activeMode = '';
      let inlineMode = false;
      let loading = false;
      let typingNode = null;
      let history = [];
      let typeTimer = null;
      let closeTimer = null;
      let lastTopicQuery = '';
      const assistantState = {
        mode: 'chat',
        last_query: null,
        last_results: null,
        user_name: null,
        preferred_location: null,
        preferred_format: null
      };
      const sessionPrefs = {
        mode: '',
        price: '',
        date: '',
        near_me: '',
        location: ''
      };

      if (!panel || !toggle) {
        return;
      }

      const chatUiReady = !!(form && input && sendBtn && messages);

      if (askPlaceholder && input) {
        input.placeholder = askPlaceholder;
      }

      function scrollMessages() {
        if (!messages) {
          return;
        }
        messages.scrollTop = messages.scrollHeight + 120;
      }

      function clearCloseTimer() {
        if (closeTimer) {
          window.clearTimeout(closeTimer);
          closeTimer = null;
        }
      }

      function setOpen(openState, options) {
        const opts = options && typeof options === 'object' ? options : {};
        const immediate = !!opts.immediate;
        const open = !!openState;
        clearCloseTimer();

        if (open) {
          panel.removeAttribute('hidden');
          panel.classList.remove('is-closing');
          widget.classList.remove('is-closing');
          widget.classList.add('is-open');
          panel.classList.add('is-open');

          if (!immediate) {
            window.requestAnimationFrame(function () {
              widget.classList.add('is-visible');
              panel.classList.add('is-visible');
            });
          } else {
            widget.classList.add('is-visible');
            panel.classList.add('is-visible');
          }
        } else if (!inlineMode) {
          panel.classList.remove('is-visible');
          widget.classList.remove('is-visible');
          panel.classList.add('is-closing');
          widget.classList.add('is-closing');

          const finishClose = function () {
            panel.setAttribute('hidden', 'hidden');
            panel.classList.remove('is-open', 'is-closing');
            widget.classList.remove('is-open', 'is-closing');
          };

          if (immediate) {
            finishClose();
          } else {
            closeTimer = window.setTimeout(finishClose, panelTransitionMs);
          }
        } else {
          panel.classList.remove('is-visible');
          widget.classList.remove('is-visible');
          panel.classList.add('is-closing');
          widget.classList.add('is-closing');
          const finishInlineClose = function () {
            panel.setAttribute('hidden', 'hidden');
            panel.classList.remove('is-open', 'is-closing');
            widget.classList.remove('is-open', 'is-closing');
          };
          if (immediate) {
            finishInlineClose();
          } else {
            closeTimer = window.setTimeout(finishInlineClose, panelTransitionMs);
          }
        }
        toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
        if (open) {
          window.setTimeout(function () {
            if (input && document.activeElement !== input) {
              input.focus();
            }
            scrollMessages();
          }, 40);
        }
      }

      function applyChatbotPosition(options) {
        const opts = options && typeof options === 'object' ? options : {};
        const immediate = !!opts.immediate;
        const force = !!opts.force;
        const useMobile = positionMediaQuery ? !!positionMediaQuery.matches : window.innerWidth <= 900;
        const nextMode = useMobile ? mobileMode : desktopMode;

        if (!force && nextMode === activeMode) {
          return;
        }

        allModes.forEach(function (modeName) {
          widget.classList.remove('eep-chatbot--' + modeName);
        });

        activeMode = nextMode;
        inlineMode = activeMode === 'inline';
        widget.classList.add('eep-chatbot--' + activeMode);
        widget.classList.toggle('is-inline', inlineMode);
        widget.setAttribute('data-eep-chatbot-active-position', activeMode);

        if (inlineMode) {
          setOpen(true, { immediate: true });
          return;
        }

        if (!widget.classList.contains('is-open')) {
          panel.setAttribute('hidden', 'hidden');
          panel.classList.remove('is-open', 'is-visible', 'is-closing');
          widget.classList.remove('is-open', 'is-visible', 'is-closing');
          toggle.setAttribute('aria-expanded', 'false');
          return;
        }

        if (immediate) {
          panel.removeAttribute('hidden');
          panel.classList.add('is-open', 'is-visible');
          widget.classList.add('is-open', 'is-visible');
          toggle.setAttribute('aria-expanded', 'true');
        }
      }

      applyChatbotPosition({ immediate: true, force: true });
      if (positionMediaQuery) {
        const onPositionChange = function () {
          applyChatbotPosition({ immediate: true });
        };
        if (typeof positionMediaQuery.addEventListener === 'function') {
          positionMediaQuery.addEventListener('change', onPositionChange);
        } else if (typeof positionMediaQuery.addListener === 'function') {
          positionMediaQuery.addListener(onPositionChange);
        }
      }

      toggle.addEventListener('click', function (event) {
        event.preventDefault();
        if (inlineMode) {
          if (widget.classList.contains('is-open')) {
            setOpen(false);
          } else {
            setOpen(true);
          }
          return;
        }
        setOpen(!widget.classList.contains('is-open'));
      });

      if (closeBtn) {
        closeBtn.addEventListener('click', function (event) {
          event.preventDefault();
          event.stopPropagation();
          setOpen(false);
        });
      }

      document.addEventListener('keydown', function (event) {
        if (event.key !== 'Escape') {
          return;
        }
        if (widget.classList.contains('is-open')) {
          setOpen(false);
        }
      });

      if (!chatUiReady) {
        if (inlineMode) {
          setOpen(true, { immediate: true });
        }
        return;
      }

      function normalizeBotText(text) {
        let output = String(text || '').replace(/\r\n?/g, '\n').trim();
        if (!output) {
          return '';
        }
        output = output
          .replace(/\*\*(.*?)\*\*/g, '$1')
          .replace(/__(.*?)__/g, '$1')
          .replace(/`([^`]+)`/g, '$1')
          .replace(/\[(.*?)\]\((https?:\/\/[^\s)]+)\)/g, '$1 - $2')
          .replace(/^\s{0,3}#{1,6}\s*/gm, '')
          .replace(/\n{3,}/g, '\n\n')
          .trim();
        return output;
      }

      function absorbPrefsFromQuery(queryText) {
        const q = String(queryText || '').toLowerCase();
        if (!q) {
          return;
        }
        if (/\bonline|virtual|webinar|stream\b/.test(q)) {
          sessionPrefs.mode = 'online';
        } else if (/\bin[-\s]?person|onsite|on-site\b/.test(q)) {
          sessionPrefs.mode = 'in_person';
        }
        if (/\bfree\b/.test(q)) {
          sessionPrefs.price = 'free';
        } else if (/\bpaid|premium|tickets?\b/.test(q)) {
          sessionPrefs.price = 'paid';
        }
        if (/\bthis week\b/.test(q)) {
          sessionPrefs.date = 'this_week';
        } else if (/\bnext 14 days|next two weeks\b/.test(q)) {
          sessionPrefs.date = 'next_14_days';
        } else if (/\blater this year\b/.test(q)) {
          sessionPrefs.date = 'later_this_year';
        }
        const locationMatch = q.match(/\b(?:in|near|around)\s+([a-z][a-z\s\-]{1,32})\b/);
        if (locationMatch && locationMatch[1]) {
          sessionPrefs.location = String(locationMatch[1]).trim();
        }
      }

      function updateQuickFilterUI() {
        quickFilterButtons.forEach(function (btn) {
          const key = String(btn.getAttribute('data-eep-chatbot-filter-key') || '').trim();
          const val = String(btn.getAttribute('data-eep-chatbot-filter-value') || '').trim();
          const active = key && String(sessionPrefs[key] || '') === val;
          btn.classList.toggle('is-active', !!active);
        });
      }

      function applyQuickFilter(key, value) {
        if (!key) {
          return;
        }
        if (key === 'mode') {
          sessionPrefs.mode = sessionPrefs.mode === value ? '' : value;
        } else if (key === 'price') {
          sessionPrefs.price = sessionPrefs.price === value ? '' : value;
        } else if (key === 'date') {
          sessionPrefs.date = sessionPrefs.date === value ? '' : value;
        } else if (key === 'near_me') {
          sessionPrefs.near_me = sessionPrefs.near_me === '1' ? '' : '1';
        }
        updateQuickFilterUI();
      }

      function pushHistory(role, text) {
        const safeRole = role === 'assistant' ? 'assistant' : 'user';
        const content = String(text || '').replace(/\s+/g, ' ').trim();
        if (!content) {
          return;
        }
        history.push({
          role: safeRole,
          content: content.slice(0, 700)
        });
        if (history.length > conversationLimit) {
          history = history.slice(-conversationLimit);
        }
      }

      function getHistoryPayload() {
        return history.slice(-conversationLimit);
      }

      function appendMessage(role, text) {
        const safeRole = role === 'user' ? 'user' : 'bot';
        const contentRaw = String(text || '').trim();
        const content = safeRole === 'bot' ? normalizeBotText(contentRaw) : contentRaw;
        const article = document.createElement('article');
        article.className = 'eep-chatbot-message eep-chatbot-message--' + safeRole;

        const bubble = document.createElement('div');
        bubble.className = 'eep-chatbot-bubble';
        bubble.textContent = content !== '' ? content : genericError;
        article.appendChild(bubble);

        messages.appendChild(article);
        scrollMessages();
        return article;
      }

      function appendDebugMeta(article, payload) {
        if (!chatbotDebugMode || !article || !payload || typeof payload !== 'object') {
          return;
        }
        const meta = payload.meta && typeof payload.meta === 'object' ? payload.meta : {};
        const incoming = meta.assistant_state && typeof meta.assistant_state === 'object'
          ? meta.assistant_state
          : {};
        const mode = String(meta.mode || incoming.mode || assistantState.mode || 'chat').trim() || 'chat';
        const intent = String(meta.intent || '').trim() || 'unknown';
        const searchPermitted = meta.search_permitted === false ? 'off' : 'on';
        const awaiting = meta.awaiting_event_details ? 'yes' : 'no';

        const debug = document.createElement('div');
        debug.className = 'eep-chatbot-debug-meta';
        debug.textContent = 'intent: ' + intent + ' • mode: ' + mode + ' • search: ' + searchPermitted + ' • awaiting_details: ' + awaiting;
        article.appendChild(debug);
      }

      function createChip(label, modifier) {
        if (!label) {
          return null;
        }
        const chip = document.createElement('span');
        chip.className = 'eep-chatbot-result-chip' + (modifier ? (' eep-chatbot-result-chip--' + modifier) : '');
        chip.textContent = label;
        return chip;
      }

      function createCompactEventRow(item, options) {
        const cfg = options && typeof options === 'object' ? options : {};
        if (!item || typeof item !== 'object') {
          return null;
        }
        const title = String(item.title || '').trim();
        const url = String(item.url || '').trim();
        if (!title || !url) {
          return null;
        }
        const dateLabel = String(item.date_label || '').trim();
        const modeLabel = String(item.mode || '').trim();
        const locationLabel = String(item.location || '').trim();
        const priceLabel = String(item.price || '').trim();
        const contentType = String(item.content_type || '').trim();
        const contentTypeLabel = String(item.content_type_label || '').trim();
        const detailsLabel = String(item.details_label || '').trim();
        const openLabel = String(item.open_label || '').trim();
        const reasonLabel = String(item.why || item.reason || '').trim();
        const summaryText = String(item.summary || '').replace(/\[[a-z0-9_-]+(?:\s[^\]]*)?\]/ig, ' ').replace(/\s+/g, ' ').trim();

        const row = document.createElement('div');
        row.className = 'eep-chatbot-result-row';
        row.classList.remove('is-expanded');
        row.setAttribute('data-expanded', '0');

        const head = document.createElement('div');
        head.className = 'eep-chatbot-result-head';

        const main = document.createElement('div');
        main.className = 'eep-chatbot-result-main';
        main.setAttribute('tabindex', '0');
        main.setAttribute('role', 'button');
        main.setAttribute('aria-expanded', 'false');
        const titleEl = document.createElement('div');
        titleEl.className = 'eep-chatbot-result-title';
        titleEl.textContent = title;
        main.appendChild(titleEl);

        const chips = document.createElement('div');
        chips.className = 'eep-chatbot-result-chips';
        [createChip(contentTypeLabel, contentType || 'type'), createChip(modeLabel, 'mode'), createChip(dateLabel, 'date'), createChip(priceLabel, 'price'), createChip(locationLabel, 'location')].forEach(function (chip) {
          if (chip) {
            chips.appendChild(chip);
          }
        });
        if (chips.children.length) {
          main.appendChild(chips);
        }
        if (reasonLabel) {
          const whyInline = document.createElement('div');
          whyInline.className = 'eep-chatbot-result-why-inline';
          whyInline.textContent = reasonLabel;
          main.appendChild(whyInline);
        }
        head.appendChild(main);

        const toggleBtn = document.createElement('button');
        toggleBtn.type = 'button';
        toggleBtn.className = 'eep-chatbot-result-toggle';
        toggleBtn.textContent = 'View details ▾';
        head.appendChild(toggleBtn);
        row.appendChild(head);

        const details = document.createElement('div');
        details.className = 'eep-chatbot-result-details';
        details.hidden = true;
        details.setAttribute('aria-hidden', 'true');
        details.style.display = 'none';

        const detailGrid = document.createElement('div');
        detailGrid.className = 'eep-chatbot-result-detail-grid';
        const cover = String(item.cover || '').trim();
        if (cover) {
          const thumb = document.createElement('img');
          thumb.className = 'eep-chatbot-result-thumb';
          thumb.loading = 'lazy';
          thumb.decoding = 'async';
          thumb.src = cover;
          thumb.alt = title;
          detailGrid.appendChild(thumb);
        } else {
          const fallback = document.createElement('div');
          fallback.className = 'eep-chatbot-result-thumb';
          detailGrid.appendChild(fallback);
        }
        const summary = document.createElement('div');
        summary.className = 'eep-chatbot-result-summary';
        const summaryBits = [];
        if (modeLabel) {
          summaryBits.push(modeLabel);
        }
        if (locationLabel) {
          summaryBits.push(locationLabel);
        }
        if (priceLabel) {
          summaryBits.push(priceLabel);
        }
        if (String(item.type || '').trim()) {
          summaryBits.push(String(item.type || '').trim());
        }
        const fallbackDetailsLabel = detailsLabel || (contentTypeLabel ? (contentTypeLabel + ' details') : 'Listing details');
        summary.textContent = summaryBits.join(' • ') || fallbackDetailsLabel;
        detailGrid.appendChild(summary);
        details.appendChild(detailGrid);

        if (summaryText) {
          const detailSummary = document.createElement('div');
          detailSummary.className = 'eep-chatbot-result-why';
          detailSummary.textContent = summaryText;
          details.appendChild(detailSummary);
        }

        const openLink = document.createElement('a');
        openLink.className = 'eep-chatbot-result-open';
        openLink.href = url;
        openLink.target = '_blank';
        openLink.rel = 'nofollow noopener noreferrer ugc';
        openLink.textContent = openLabel || (contentTypeLabel ? ('Open ' + contentTypeLabel.toLowerCase()) : 'Open listing');
        details.appendChild(openLink);

        row.appendChild(details);

        const toggleDetails = function () {
          const nextOpen = !row.classList.contains('is-expanded');
          row.classList.toggle('is-expanded', nextOpen);
          row.setAttribute('data-expanded', nextOpen ? '1' : '0');
          details.hidden = !nextOpen;
          details.setAttribute('aria-hidden', nextOpen ? 'false' : 'true');
          details.style.display = nextOpen ? 'grid' : 'none';
          toggleBtn.textContent = nextOpen ? 'Hide details ▴' : 'View details ▾';
          main.setAttribute('aria-expanded', nextOpen ? 'true' : 'false');
        };
        toggleBtn.addEventListener('click', function (event) {
          event.preventDefault();
          event.stopPropagation();
          toggleDetails();
        });
        main.addEventListener('click', function (event) {
          event.preventDefault();
          toggleDetails();
        });
        main.addEventListener('keydown', function (event) {
          if (event.key === 'Enter' || event.key === ' ') {
            event.preventDefault();
            toggleDetails();
          }
        });
        if (cfg.expandInitially) {
          row.classList.add('is-expanded');
          row.setAttribute('data-expanded', '1');
          details.hidden = false;
          details.setAttribute('aria-hidden', 'false');
          details.style.display = 'grid';
          toggleBtn.textContent = 'Hide details ▴';
          main.setAttribute('aria-expanded', 'true');
        }
        return row;
      }

      function addRefineActions(container, recommended, queryText, assistantText) {
        if (!container) {
          return;
        }
        const questionText = String(assistantText || '').toLowerCase();
        if (!questionText) {
          return;
        }
        const queryBase = String(queryText || assistantState.last_query || '').trim();
        const actions = [];
        const seen = new Set();

        function pushAction(label, makeQuery) {
          const key = String(label || '').toLowerCase().trim();
          if (!key || seen.has(key)) {
            return;
          }
          seen.add(key);
          actions.push({
            label: String(label || '').trim(),
            makeQuery: makeQuery,
          });
        }

        function withTopic(topic) {
          const base = queryBase || 'events';
          const normalizedTopic = String(topic || '').trim();
          if (!normalizedTopic) {
            return base;
          }
          if (base.toLowerCase().indexOf(normalizedTopic.toLowerCase()) !== -1) {
            return base;
          }
          return (base + ' ' + normalizedTopic).trim();
        }

        if ((/online/.test(questionText) && /in[-\s]?person/.test(questionText)) || /online.*or.*in[-\s]?person|in[-\s]?person.*or.*online/.test(questionText)) {
          pushAction('Online', function () {
            sessionPrefs.mode = 'online';
            updateQuickFilterUI();
            return queryBase ? (queryBase + ' online') : 'online events';
          });
          pushAction('In-person', function () {
            sessionPrefs.mode = 'in_person';
            updateQuickFilterUI();
            return queryBase ? (queryBase + ' in-person') : 'in-person events';
          });
        }

        if ((/free/.test(questionText) && /paid/.test(questionText)) || /free.*or.*paid|paid.*or.*free/.test(questionText)) {
          pushAction('Free', function () {
            sessionPrefs.price = 'free';
            updateQuickFilterUI();
            return queryBase ? (queryBase + ' free') : 'free events';
          });
          pushAction('Paid', function () {
            sessionPrefs.price = 'paid';
            updateQuickFilterUI();
            return queryBase ? (queryBase + ' paid') : 'paid events';
          });
        }

        if (/this week/.test(questionText) || /next 14 days/.test(questionText) || /later this year/.test(questionText)) {
          if (/this week/.test(questionText)) {
            pushAction('This week', function () {
              sessionPrefs.date = 'this_week';
              updateQuickFilterUI();
              return queryBase ? (queryBase + ' this week') : 'events this week';
            });
          }
          if (/next 14 days/.test(questionText)) {
            pushAction('Next 14 days', function () {
              sessionPrefs.date = 'next_14_days';
              updateQuickFilterUI();
              return queryBase ? (queryBase + ' next 14 days') : 'events next 14 days';
            });
          }
          if (/later this year/.test(questionText)) {
            pushAction('Later this year', function () {
              sessionPrefs.date = 'later_this_year';
              updateQuickFilterUI();
              return queryBase ? (queryBase + ' later this year') : 'events later this year';
            });
          }
        }

        if (/ai/.test(questionText) && /sustainability/.test(questionText) && /careers?/.test(questionText)) {
          pushAction('AI', function () {
            return withTopic('AI');
          });
          pushAction('Sustainability', function () {
            return withTopic('sustainability');
          });
          pushAction('Careers', function () {
            return withTopic('careers');
          });
          if (/student/.test(questionText)) {
            pushAction('Student', function () {
              return withTopic('student');
            });
          }
        } else if (/event/.test(questionText) && /(data\/ml|data|ml|machine learning)/.test(questionText) && /business ai/.test(questionText)) {
          pushAction('Event AI', function () {
            return withTopic('event AI');
          });
          pushAction('Data/ML', function () {
            return withTopic('data ML');
          });
          pushAction('Business AI', function () {
            return withTopic('business AI');
          });
        }

        if (!actions.length) {
          return;
        }

        const actionsWrap = document.createElement('div');
        actionsWrap.className = 'eep-chatbot-refine-actions';
        actions.forEach(function (cfg) {
          const btn = document.createElement('button');
          btn.type = 'button';
          btn.className = 'eep-chatbot-refine-btn';
          btn.textContent = cfg.label;
          btn.addEventListener('click', function () {
            if (loading) {
              return;
            }
            const nextQuery = String((typeof cfg.makeQuery === 'function' ? cfg.makeQuery() : '') || '').trim();
            if (!nextQuery) {
              return;
            }
            input.value = nextQuery;
            submitQuestion(nextQuery);
          });
          actionsWrap.appendChild(btn);
        });
        container.appendChild(actionsWrap);
      }

      function appendBotResults(article, payload, userQuery, assistantText) {
        const meta = payload && typeof payload === 'object' && payload.meta && typeof payload.meta === 'object'
          ? payload.meta
          : {};
        const intent = String(meta.intent || '').trim();
        const mode = String(meta.mode || assistantState.mode || 'chat').trim();
        const searchPermitted = meta.search_permitted !== false;
        const isSearchIntent = intent === 'event_query' || intent === 'content_query' || intent === 'refinement';
        if (!isSearchIntent || !searchPermitted || mode !== 'search') {
          addRefineActions(article, null, userQuery, assistantText);
          return null;
        }

        const eventsObj = payload && typeof payload === 'object' && payload.events && typeof payload.events === 'object'
          ? payload.events
          : {};
        const recommended = eventsObj.recommended && typeof eventsObj.recommended === 'object'
          ? eventsObj.recommended
          : null;
        let more = Array.isArray(eventsObj.more) ? eventsObj.more.slice() : [];

        if (!recommended && Array.isArray(payload && payload.recommendations) && payload.recommendations.length) {
          more = payload.recommendations.slice(1);
        }
        const recommendedFallback = recommended || (Array.isArray(payload && payload.recommendations) ? payload.recommendations[0] : null);
        if (!recommendedFallback) {
          addRefineActions(article, null, userQuery, assistantText);
          return null;
        }

        const wrap = document.createElement('div');
        wrap.className = 'eep-chatbot-result-wrap';
        const primaryRow = createCompactEventRow(recommendedFallback, { expandInitially: false });
        if (primaryRow) {
          wrap.appendChild(primaryRow);
        }

        const wantsMore = !!meta.wants_more;
        const strongTieContext = !!meta.is_broad && more.length > 0;
        if ((wantsMore || strongTieContext) && more.length) {
          const moreToggle = document.createElement('button');
          moreToggle.type = 'button';
          moreToggle.className = 'eep-chatbot-more-toggle';
          moreToggle.textContent = 'More options (' + more.length + ')';
          wrap.appendChild(moreToggle);

          const moreList = document.createElement('div');
          moreList.className = 'eep-chatbot-more-list';
          moreList.hidden = true;
          moreList.style.display = 'none';
          more.forEach(function (item) {
            const row = createCompactEventRow(item, { expandInitially: false });
            if (row) {
              moreList.appendChild(row);
            }
          });
          if (moreList.children.length) {
            wrap.appendChild(moreList);
            moreToggle.addEventListener('click', function () {
              const nextOpen = !!moreList.hidden;
              moreList.hidden = !nextOpen;
              moreList.style.display = nextOpen ? 'grid' : 'none';
              moreToggle.textContent = nextOpen ? 'Hide options ▴' : ('More options (' + more.length + ')');
            });
          }
        }

        addRefineActions(wrap, recommendedFallback, userQuery, assistantText);
        article.appendChild(wrap);
        scrollMessages();
        return wrap;
      }

      function clearRenderedResults() {
        const rendered = messages ? messages.querySelectorAll('.eep-chatbot-result-wrap') : [];
        if (!rendered || !rendered.length) {
          return;
        }
        rendered.forEach(function (node) {
          if (node && node.parentNode) {
            node.parentNode.removeChild(node);
          }
        });
      }

      function applyAssistantStateFromPayload(payload) {
        const meta = payload && typeof payload === 'object' && payload.meta && typeof payload.meta === 'object'
          ? payload.meta
          : {};
        const incoming = meta.assistant_state && typeof meta.assistant_state === 'object'
          ? meta.assistant_state
          : {};
        const intent = String(meta.intent || '').trim();

        assistantState.mode = String(incoming.mode || meta.mode || assistantState.mode || 'chat').trim();
        if (assistantState.mode !== 'search') {
          assistantState.mode = 'chat';
        }
        assistantState.last_query = typeof incoming.last_query === 'string'
          ? incoming.last_query
          : assistantState.last_query;
        assistantState.last_results = Array.isArray(incoming.last_results)
          ? incoming.last_results.slice()
          : assistantState.last_results;
        assistantState.user_name = typeof incoming.user_name === 'string' && incoming.user_name.trim()
          ? incoming.user_name.trim()
          : assistantState.user_name;
        assistantState.preferred_location = typeof incoming.preferred_location === 'string' && incoming.preferred_location.trim()
          ? incoming.preferred_location.trim()
          : assistantState.preferred_location;
        assistantState.preferred_format = typeof incoming.preferred_format === 'string' && incoming.preferred_format.trim()
          ? incoming.preferred_format.trim()
          : assistantState.preferred_format;

        if (assistantState.mode === 'chat' || intent === 'stop_command') {
          clearRenderedResults();
        }
      }

      function typeBotText(bubble, text) {
        const content = normalizeBotText(text);
        const finalText = content !== '' ? content : genericError;
        const tokens = finalText.split(/(\s+)/).filter(function (token) {
          return token !== '';
        });
        bubble.textContent = '';

        return new Promise(function (resolve) {
          if (!tokens.length) {
            bubble.textContent = genericError;
            resolve(genericError);
            return;
          }
          let pointer = 0;
          const step = function () {
            if (pointer >= tokens.length) {
              resolve(finalText);
              return;
            }
            const token = tokens[pointer];
            bubble.textContent += token;
            pointer += 1;
            scrollMessages();

            let delay = 20;
            if (/^\s+$/.test(token)) {
              delay = 8;
            } else if (/[.!?]$/.test(token)) {
              delay = 78;
            } else if (/[,;:]$/.test(token)) {
              delay = 45;
            }
            typeTimer = window.setTimeout(step, delay);
          };
          step();
        });
      }

      function appendBotMessageAnimated(text, payload, userQuery) {
        const article = document.createElement('article');
        article.className = 'eep-chatbot-message eep-chatbot-message--bot';
        const bubble = document.createElement('div');
        bubble.className = 'eep-chatbot-bubble';
        article.appendChild(bubble);
        messages.appendChild(article);
        scrollMessages();

        return typeBotText(bubble, text).then(function (renderedText) {
          appendDebugMeta(article, payload);
          appendBotResults(article, payload, userQuery, renderedText);
          scrollMessages();
          return renderedText;
        });
      }

      function setLoading(isLoading) {
        loading = !!isLoading;
        form.classList.toggle('is-loading', loading);
        input.disabled = loading;
        sendBtn.disabled = loading;
      }

      function addTyping() {
        if (typingNode) {
          return;
        }
        const article = document.createElement('article');
        article.className = 'eep-chatbot-message eep-chatbot-message--bot eep-chatbot-message--typing';
        const bubble = document.createElement('div');
        bubble.className = 'eep-chatbot-bubble';
        const typingText = typingLabel || 'Thinking...';
        bubble.innerHTML = '<span class="eep-chatbot-thinking-label">' + escapeHtml(typingText) + '</span><span class="eep-chatbot-typing-dots" aria-hidden="true"><i></i><i></i><i></i></span>';
        article.appendChild(bubble);
        messages.appendChild(article);
        typingNode = article;
        scrollMessages();
      }

      function removeTyping() {
        if (typeTimer) {
          window.clearTimeout(typeTimer);
          typeTimer = null;
        }
        if (typingNode && typingNode.parentNode) {
          typingNode.parentNode.removeChild(typingNode);
        }
        typingNode = null;
      }

      function submitQuestion(queryOverride) {
        let query = String(typeof queryOverride === 'string' ? queryOverride : (input.value || '')).trim();
        const prefOnly = /^(online|in[-\s]?person|free|paid|this week|near me|more|show more|more options)$/i.test(query);
        if (prefOnly && lastTopicQuery) {
          query = (lastTopicQuery + ' ' + query).trim();
        }
        if (query !== '' && !prefOnly && query.split(/\s+/).length >= 2) {
          lastTopicQuery = query;
        } else if (query === '' && lastTopicQuery !== '') {
          query = lastTopicQuery;
        }
        if (!query) {
          showToast(emptyPrompt || 'Type a question first.', 'warning');
          return;
        }
        if (!chatbotEnabled || !endpoint) {
          showToast(genericError || 'Assistant unavailable.', 'error');
          return;
        }

        absorbPrefsFromQuery(query);
        updateQuickFilterUI();

        appendMessage('user', query);
        pushHistory('user', query);
        input.value = '';
        setLoading(true);
        addTyping();

        restPost(endpoint, {
          query: query,
          history: getHistoryPayload(),
          preferences: sessionPrefs
        })
          .then(function (payload) {
            applyAssistantStateFromPayload(payload || {});
            removeTyping();
            const answer = String((payload && (payload.assistant_text || payload.answer)) || '').trim();
            return appendBotMessageAnimated(
              answer || 'Tell me what kind of event you want, and I can help.',
              payload || {},
              query
            ).then(function (renderedText) {
              pushHistory('assistant', renderedText);
            });
          })
          .catch(function (error) {
            removeTyping();
            const message = String((error && error.message) || genericError || 'Assistant unavailable.').trim();
            appendMessage('bot', message);
            pushHistory('assistant', message);
            showToast(message, 'error', {
              title: botName || 'Assistant',
              subtitle: 'Chatbot request failed'
            });
          })
          .finally(function () {
            setLoading(false);
            input.focus();
          });
      }

      updateQuickFilterUI();

      form.addEventListener('submit', function (event) {
        event.preventDefault();
        if (loading) {
          return;
        }
        submitQuestion();
      });

      input.addEventListener('keydown', function (event) {
        if (event.key === 'Enter' && !event.shiftKey) {
          event.preventDefault();
          if (!loading) {
            submitQuestion();
          }
        }
      });

    });
  }

  function runFrontendInitializers() {
    const steps = [
      ['ensureAlpineReady', ensureAlpineReady],
      ['pruneLegacyFrontendCssForMapTemplates', pruneLegacyFrontendCssForMapTemplates],
      ['initUrlNoticeToast', initUrlNoticeToast],
      ['initSpaFeedbackBadge', initSpaFeedbackBadge],
      ['initPagePreloader', initPagePreloader],
      ['initSmoothScrollingEngine', initSmoothScrollingEngine],
      ['initFrontendStickyNav', initFrontendStickyNav],
      ['initHybridSpaRouter', initHybridSpaRouter],
      ['initQuickEdit', initQuickEdit],
      ['initCardLinks', initCardLinks],
      ['initNoticeToasts', initNoticeToasts],
      ['initAccountTabsAjax', initAccountTabsAjax],
      ['initAuthTabs', initAuthTabs],
      ['initSubmitNavSpy', initSubmitNavSpy],
      ['initUserManagementSearch', initUserManagementSearch],
      ['initArchiveFiltersAjax', initArchiveFiltersAjax],
      ['initMap', initMap],
      ['initStickyColumns', initStickyColumns],
      ['initGeocode', initGeocode],
      ['initSubmitCustomFieldBuilder', initSubmitCustomFieldBuilder],
      ['initCustomFieldUploads', initCustomFieldUploads],
      ['initSubmitUploaderFallback', initSubmitUploaderFallback],
      ['initPageBannerHeroBackground', initPageBannerHeroBackground],
      ['initEventHeroParallax', initEventHeroParallax],
      ['initEventLocalTime', initEventLocalTime],
      ['initAutogeneratedDisclaimer', initAutogeneratedDisclaimer],
      ['initEventGalleryLightbox', initEventGalleryLightbox],
      ['initEventVideoLightbox', initEventVideoLightbox],
      ['initEventDocumentLightbox', initEventDocumentLightbox],
      ['initEventContactModal', initEventContactModal],
      ['initEventCountdown', initEventCountdown],
      ['initCalendarOverflowModal', initCalendarOverflowModal],
      ['initRecaptchaForms', initRecaptchaForms],
      ['initFormLoadingStates', initFormLoadingStates],
      ['initSubmitEventAjax', initSubmitEventAjax],
      ['initCopyLinkButtons', initCopyLinkButtons],
      ['initRsvpAjax', initRsvpAjax],
      ['initEventAnalyticsTracking', initEventAnalyticsTracking],
      ['initModerationActionsAjax', initModerationActionsAjax],
      ['initModerationBulkActions', initModerationBulkActions],
      ['initMyEventsActionsAjax', initMyEventsActionsAjax],
      ['initMyEventsAttendeesModal', initMyEventsAttendeesModal],
      ['initInboxThreadNavigationAjax', initInboxThreadNavigationAjax],
      ['initInboxReplyAjax', initInboxReplyAjax],
      ['initInboxMessageFocus', initInboxMessageFocus],
      ['initInboxPolling', initInboxPolling],
      ['initElementorEventsAjaxSearch', initElementorEventsAjaxSearch],
      ['initEventChatbot', initEventChatbot],
      ['initScrollReveal', initScrollReveal]
    ];

    steps.forEach(function (step) {
      const name = Array.isArray(step) ? String(step[0] || '') : '';
      const fn = Array.isArray(step) ? step[1] : null;
      if (typeof fn !== 'function') {
        return;
      }
      try {
        fn();
      } catch (error) {
        if (window.console && typeof window.console.error === 'function') {
          window.console.error('[EEP init] ' + name + ' failed', error);
        }
      }
    });
  }

  $(document).ready(function () {
    runFrontendInitializers();
  });
})(jQuery);
