// plugins/theme.client.ts
export default defineNuxtPlugin(() => {
  const initializeTheme = () => {
    const savedTheme = localStorage.getItem('theme')
    const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches

    const theme = savedTheme || (systemPrefersDark ? 'dark' : 'light')
    document.documentElement.setAttribute('data-theme', theme)
  }

  // Инициализируем при загрузке
  initializeTheme()

  // Слушаем изменения системной темы
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
    if (!localStorage.getItem('theme')) {
      document.documentElement.setAttribute('data-theme', e.matches ? 'dark' : 'light')
    }
  })
})