// FOUC prevention: runs in <head> before paint. Sets the theme on <html> so
// the page renders with the right colors on first load. The theme switcher
// button is wired up separately, after it is parsed, in Header.astro.
//
// THEME_BG is substituted by Base.astro from src/lib/site.ts (the single
// source of truth). The values drive the browser-chrome
// <meta name="theme-color"> so it follows the user's manual toggle (the
// static media-keyed metas only tracked OS preference).
(function () {
  document.documentElement.classList.remove('no-js');
  document.documentElement.classList.add('js');

  var STORAGE_KEY = 'theme';
  // The placeholder string is replaced with the serialized colors at build
  // time; parsing keeps this file valid standalone JavaScript.
  var THEME_BG = JSON.parse('__THEME_BG__');
  var saved = null;
  try {
    var value = localStorage.getItem(STORAGE_KEY);
    if (value === 'light' || value === 'dark') saved = value;
  } catch (e) {}
  var theme =
    saved || (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
  document.documentElement.dataset.theme = theme;
  document.documentElement.style.colorScheme = theme;
  var themeColorMetas = document.querySelectorAll('meta[name="theme-color"]');
  for (var i = 0; i < themeColorMetas.length; i += 1) {
    themeColorMetas[i].setAttribute('content', THEME_BG[theme]);
  }
})();
