import { createRoot } from 'react-dom/client';
import { QueryClientProvider } from '@tanstack/react-query';
import { App } from './App';
import { queryClient } from './lib/queryClient';
import { initTheme } from './lib/theme';
import './i18n'; // initialize i18next (en/ja) before first render
import './index.css';

// Keep [data-theme] in sync with the OS while pref is 'system'. The initial
// attribute is already set pre-paint by the inline script in index.html.
initTheme();

createRoot(document.getElementById('root')!).render(
  <QueryClientProvider client={queryClient}>
    <App />
  </QueryClientProvider>
);

if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/ui/sw.js', { scope: '/ui/' }).catch(() => {
      // Registration failures are non-fatal — the app works without the SW.
    });
  });
}
