(function () {
  var key = 'theme';
  var legacyKey = 'dark-mode';
  var saved = null;
  try {
    var value = localStorage.getItem(key);
    if (value === 'light' || value === 'dark') {
      saved = value;
    } else {
      var legacyValue = localStorage.getItem(legacyKey);
      if (legacyValue !== null) {
        saved = JSON.parse(legacyValue) ? 'dark' : 'light';
      }
    }
  } catch (e) {
    saved = null;
  }
  var systemDark = matchMedia('(prefers-color-scheme: dark)').matches;
  var theme = saved || (systemDark ? 'dark' : 'light');
  document.documentElement.dataset.theme = theme;
  document.documentElement.style.colorScheme = theme;
})();
