// Theme override — OrbitCosmo renders dark (the Eragon look) by default, independent
// of the OS appearance; "light" opts into the bronze paper theme. The choice is
// remembered in localStorage, which the graph page and the side panel share
// (same extension origin), and the `storage` event re-themes other open OrbitCosmo
// pages live. CSS keys off <html data-theme="...">; the canvases read
// OrbitTheme.get() and subscribe via OrbitTheme.onChange().
// Loaded first, from <head>, so the attribute is set before the page paints.
(() => {
  const KEY = "orbitTheme";
  const read = () => {
    try { return localStorage.getItem(KEY) === "light" ? "light" : "dark"; }
    catch { return "dark"; }
  };
  const listeners = new Set();
  function apply(theme) {
    document.documentElement.dataset.theme = theme;
    for (const fn of listeners) fn(theme);
  }
  function set(theme) {
    try { localStorage.setItem(KEY, theme); } catch { /* private mode etc. */ }
    apply(theme);
  }
  window.addEventListener("storage", (e) => { if (e.key === KEY) apply(read()); });
  window.OrbitTheme = {
    get: read,
    set,
    toggle: () => set(read() === "light" ? "dark" : "light"),
    onChange: (fn) => listeners.add(fn),
  };
  apply(read());
})();
