// 外观三态：system | light | dark；驱动根节点 data-theme（设计系统约定）。
const media = window.matchMedia('(prefers-color-scheme: dark)');
let pref = 'system';

function apply() {
  const dark = pref === 'dark' || (pref === 'system' && media.matches);
  document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light');
}

export function setThemePref(p) {
  pref = p;
  apply();
}

media.addEventListener('change', apply);
apply();
