import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import './index.css';

// Ensure theme is applied immediately to prevent flash
const applyInitialTheme = () => {
  const savedTheme = localStorage.getItem('magpie-dark-mode');
  const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  const isDarkMode = savedTheme ? JSON.parse(savedTheme) : prefersDark;

  const root = document.documentElement;
  if (isDarkMode) {
    root.classList.add('dark');
  } else {
    root.classList.remove('dark');
  }
  
  console.log('Initial theme applied:', isDarkMode ? 'dark' : 'light', 'Classes:', root.classList.toString());
};

// Apply theme before React renders
applyInitialTheme();

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <App />
  </StrictMode>
);