/**
 * Render-blocking boot script — runs synchronously before first paint.
 *
 * Intentionally a classic IIFE (not an ES module) so the browser executes it
 * immediately while parsing <head>. This prevents layout flicker by applying
 * persisted UI state (sidebar collapsed, contrast mode) before any content
 * is rendered. Uses `var` for broadest compatibility in the sync path.
 */
(function () {
  var root = document.documentElement;
  root.classList.remove('no-js');
  root.classList.add('js');
  root.classList.add('aside-pending');

  var normalizeToken = function (value) {
    return String(value == null ? '' : value)
      .trim()
      .toLowerCase()
      .replace(/[^a-z0-9._-]+/g, '-')
      .replace(/^-+|-+$/g, '');
  };
  var buildStorageKey = function (scope, key) {
    var namespace = normalizeToken('app-ui') || 'app-ui';
    var version = normalizeToken('v1') || 'v1';
    var normalizedScope = normalizeToken(scope || '') || 'global';
    var normalizedKey = normalizeToken(key || '');
    if (!normalizedKey) {
      return '';
    }
    return namespace + ':' + version + ':' + normalizedScope + ':state:' + normalizedKey;
  };

  try {
    var sidebarCollapsedKey = buildStorageKey('sidebar', 'sidebar-collapsed');
    var sidebarHiddenKey = buildStorageKey('sidebar', 'sidebar-hidden');
    var contrastKey = buildStorageKey('contrast', 'contrast-mode');

    if (window.innerWidth >= 768) {
      if (sidebarCollapsedKey && window.localStorage.getItem(sidebarCollapsedKey) === '1') {
        root.classList.add('sidebar-collapsed');
      }
      if (sidebarHiddenKey && window.localStorage.getItem(sidebarHiddenKey) === '1') {
        root.classList.add('sidebar-hidden');
      }
    }
    var contrastValue = contrastKey ? window.localStorage.getItem(contrastKey) : null;
    if (contrastValue === 'high') {
      root.dataset.contrast = 'high';
    } else {
      root.dataset.contrast = 'normal';
    }
  } catch (e) {
    // ignore storage errors
  }
})();
