
  (() => {
    const btn = document.getElementById('theme-toggle');
    const label = document.getElementById('theme-label');
    if (!btn || !label) return;
    const applyTheme = (theme) => {
      document.documentElement.dataset.theme = theme;
      label.textContent = theme === 'dark' ? 'Dark' : 'Light';
    };
    applyTheme(document.documentElement.dataset.theme === 'light' ? 'light' : 'dark');
    btn.addEventListener('click', () => {
      const cur = document.documentElement.dataset.theme || 'dark';
      const next = cur === 'dark' ? 'light' : 'dark';
      applyTheme(next);
      try { localStorage.setItem('lvonce-theme', next); } catch {}
      if (window.parent !== window) {
        window.parent.postMessage({ type: 'lvonce-theme-sync', theme: next }, window.location.origin);
      }
    });
    window.addEventListener('message', (event) => {
      const theme = event.data?.theme;
      if (event.origin === window.location.origin && event.data?.type === 'lvonce-theme-sync' && (theme === 'dark' || theme === 'light')) {
        applyTheme(theme);
      }
    });
    window.addEventListener('storage', (event) => {
      if (event.key === 'lvonce-theme' && (event.newValue === 'dark' || event.newValue === 'light')) {
        applyTheme(event.newValue);
      }
    });
  })();

