<template>
  <Teleport to="body">
    <Transition name="drawer">
      <div v-if="modelValue" class="drawer-overlay" @click.self="close">
        <nav class="drawer-menu" ref="drawerRef">
          <!-- Header with Logo -->
          <div class="drawer-header">
            <div class="drawer-logo">
              <i class="pi pi-building"></i>
              <span class="logo-text">ROA2WEB</span>
            </div>
          </div>

          <!-- Company & Period Selection (below header, above navigation) -->
          <div v-if="companiesStore" class="drawer-selectors">
            <!-- Company Selector -->
            <div class="selector-group">
              <label class="selector-label">Firma</label>
              <button
                class="selector-trigger"
                @click="toggleCompanyDropdown"
                :aria-expanded="companyDropdownOpen"
              >
                <div class="selector-value">
                  <span class="selector-main">{{ selectedCompanyName }}</span>
                  <span v-if="selectedCompanyCode" class="selector-sub">{{ selectedCompanyCode }}</span>
                </div>
                <i class="pi pi-chevron-down" :class="{ 'rotate-180': companyDropdownOpen }"></i>
              </button>
              <!-- Company Dropdown Panel -->
              <div v-if="companyDropdownOpen" class="selector-panel">
                <div class="selector-search">
                  <i class="pi pi-search"></i>
                  <input
                    ref="companySearchInput"
                    type="text"
                    v-model="companySearchQuery"
                    placeholder="Caută firmă..."
                    class="selector-search-input"
                  />
                </div>
                <div class="selector-list">
                  <div
                    v-for="company in filteredCompanies"
                    :key="company.id_firma"
                    class="selector-item"
                    :class="{ active: company.id_firma === companiesStore.selectedCompany?.id_firma }"
                    @click="selectCompany(company)"
                  >
                    <div class="selector-item-content">
                      <span class="selector-item-name">{{ company.name }}</span>
                      <span v-if="company.fiscal_code" class="selector-item-sub">CUI: {{ company.fiscal_code }}</span>
                    </div>
                    <i v-if="company.id_firma === companiesStore.selectedCompany?.id_firma" class="pi pi-check"></i>
                  </div>
                  <div v-if="filteredCompanies.length === 0" class="selector-empty">
                    <i class="pi pi-info-circle"></i>
                    <span>Nu s-au găsit firme</span>
                  </div>
                </div>
              </div>
            </div>

            <!-- Period Selector -->
            <div v-if="periodStore && companiesStore.selectedCompany" class="selector-group">
              <label class="selector-label">Perioada</label>
              <button
                class="selector-trigger"
                @click="togglePeriodDropdown"
                :aria-expanded="periodDropdownOpen"
              >
                <div class="selector-value">
                  <span class="selector-main">{{ selectedPeriodDisplay }}</span>
                </div>
                <i class="pi pi-chevron-down" :class="{ 'rotate-180': periodDropdownOpen }"></i>
              </button>
              <!-- Period Dropdown Panel -->
              <div v-if="periodDropdownOpen" class="selector-panel">
                <div class="selector-list">
                  <div
                    v-for="period in availablePeriods"
                    :key="`${period.an}-${period.luna}`"
                    class="selector-item"
                    :class="{ active: isPeriodSelected(period) }"
                    @click="selectPeriod(period)"
                  >
                    <span class="selector-item-name">{{ period.display_name }}</span>
                    <i v-if="isPeriodSelected(period)" class="pi pi-check"></i>
                  </div>
                  <div v-if="availablePeriods.length === 0" class="selector-empty">
                    <i class="pi pi-info-circle"></i>
                    <span>Nu sunt perioade disponibile</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- Section Divider after selectors -->
          <div v-if="companiesStore" class="drawer-divider"></div>

          <!-- Navigation Sections (scrollable) -->
          <div class="drawer-sections">
            <!-- PRINCIPALE Section -->
            <div class="drawer-section">
              <div class="section-header">PRINCIPALE</div>
              <ul class="drawer-nav">
                <li v-for="item in principaleItems" :key="item.to">
                  <router-link
                    :to="item.to"
                    class="drawer-link"
                    :class="{ 'active': isActive(item.to, item.exactMatch) }"
                    @click="handleNavClick"
                  >
                    <i :class="['drawer-icon', item.icon]"></i>
                    <span class="drawer-label">{{ item.label }}</span>
                  </router-link>
                </li>
              </ul>
            </div>

            <!-- Section Divider -->
            <div class="drawer-divider"></div>

            <!-- RAPOARTE Section -->
            <div class="drawer-section">
              <div class="section-header">RAPOARTE</div>
              <ul class="drawer-nav">
                <li v-for="item in rapoarteItems" :key="item.to">
                  <router-link
                    :to="item.to"
                    class="drawer-link"
                    :class="{ 'active': isActive(item.to, item.exactMatch) }"
                    @click="handleNavClick"
                  >
                    <i :class="['drawer-icon', item.icon]"></i>
                    <span class="drawer-label">{{ item.label }}</span>
                  </router-link>
                </li>
              </ul>
            </div>

            <!-- Section Divider -->
            <div class="drawer-divider"></div>

            <!-- ANALIZE Section -->
            <div class="drawer-section">
              <div class="section-header">ANALIZE</div>
              <ul class="drawer-nav">
                <li v-for="item in analizeItems" :key="item.to">
                  <router-link
                    :to="item.to"
                    class="drawer-link"
                    :class="{ 'active': isActive(item.to, item.exactMatch) }"
                    @click="handleNavClick"
                  >
                    <i :class="['drawer-icon', item.icon]"></i>
                    <span class="drawer-label">{{ item.label }}</span>
                  </router-link>
                </li>
              </ul>
            </div>

            <!-- Section Divider -->
            <div class="drawer-divider"></div>

            <!-- ADMINISTRARE Section -->
            <div class="drawer-section">
              <div class="section-header">ADMINISTRARE</div>
              <ul class="drawer-nav">
                <li v-for="item in administrareItems" :key="item.to">
                  <router-link
                    :to="item.to"
                    class="drawer-link"
                    :class="{ 'active': isActive(item.to, item.exactMatch) }"
                    @click="handleNavClick"
                  >
                    <i :class="['drawer-icon', item.icon]"></i>
                    <span class="drawer-label">{{ item.label }}</span>
                  </router-link>
                </li>
              </ul>
            </div>
          </div>

          <!-- Profile Section (at bottom) -->
          <div class="drawer-profile">
            <div class="profile-header">
              <div class="profile-avatar">
                <i class="pi pi-user"></i>
              </div>
              <div class="profile-info">
                <span class="profile-name">{{ displayName }}</span>
                <span class="profile-role">Utilizator</span>
              </div>
            </div>
            <button
              type="button"
              class="drawer-link logout-link"
              @click="handleLogout"
            >
              <i class="drawer-icon pi pi-sign-out"></i>
              <span class="drawer-label">Deconectare</span>
            </button>
          </div>
        </nav>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { computed, ref, watch, nextTick } from 'vue'
import { useRoute, useRouter } from 'vue-router'

/**
 * MobileDrawerMenu - Material Design 3 inspired navigation drawer for mobile (v3)
 *
 * Props:
 * - modelValue (v-model): Controls visibility of the drawer
 * - user: Optional user object with { username } for profile display
 * - onLogout: Optional callback function for logout action
 * - companiesStore: Optional Pinia store instance for company selection
 * - periodStore: Optional Pinia store instance for accounting period selection
 *
 * Events:
 * - update:modelValue: Emitted when visibility changes (for v-model support)
 * - logout: Emitted when logout is clicked (if no onLogout prop)
 * - company-changed: Emitted when company selection changes
 * - period-changed: Emitted when period selection changes
 *
 * Features:
 * - Slide-in animation from left
 * - Header with ROA2WEB logo
 * - Company & Period selectors (below header, like desktop)
 * - Navigation organized into 4 category sections:
 *   - PRINCIPALE: Dashboard, Bonuri
 *   - RAPOARTE: Facturi, Balanță, Casa și Banca
 *   - ANALIZE: Scadențe, Facturi Detaliate
 *   - ADMINISTRARE: Setări
 * - Visual separators between sections
 * - Active state highlighting based on current route
 * - Profile section with user name and logout button (footer)
 * - Close on tap outside or on link click
 * - Full dark mode support
 * - Teleported to body to avoid z-index issues
 */

const props = defineProps({
  /**
   * Controls visibility of the drawer (v-model support)
   */
  modelValue: {
    type: Boolean,
    default: false
  },
  /**
   * User object for profile display
   * Expected shape: { username: string }
   */
  user: {
    type: Object,
    default: () => null
  },
  /**
   * Optional callback for logout action
   * If not provided, 'logout' event is emitted
   */
  onLogout: {
    type: Function,
    default: null
  },
  /**
   * Companies store instance for company selection
   * Expected: Pinia store with companies, selectedCompany, setSelectedCompany
   */
  companiesStore: {
    type: Object,
    default: null
  },
  /**
   * Accounting period store instance for period selection
   * Expected: Pinia store with periods, selectedPeriod, setSelectedPeriod
   */
  periodStore: {
    type: Object,
    default: null
  }
})

const emit = defineEmits(['update:modelValue', 'logout', 'company-changed', 'period-changed'])

const route = useRoute()
const router = useRouter()
const drawerRef = ref(null)

// Company selector state
const companyDropdownOpen = ref(false)
const companySearchQuery = ref('')
const companySearchInput = ref(null)

// Period selector state
const periodDropdownOpen = ref(false)

// Computed properties for company selector
const selectedCompanyName = computed(() => {
  return props.companiesStore?.selectedCompany?.name || 'Selectare firmă'
})

const selectedCompanyCode = computed(() => {
  const code = props.companiesStore?.selectedCompany?.fiscal_code
  return code ? `CUI: ${code}` : ''
})

const filteredCompanies = computed(() => {
  const companies = props.companiesStore?.companies || []
  if (!companySearchQuery.value?.trim()) {
    return companies
  }
  const query = companySearchQuery.value.toLowerCase().trim()
  return companies.filter(
    (company) =>
      company.name?.toLowerCase().includes(query) ||
      company.fiscal_code?.toLowerCase().includes(query)
  )
})

// Computed properties for period selector
const selectedPeriodDisplay = computed(() => {
  return props.periodStore?.selectedPeriod?.display_name || 'Selectare perioadă'
})

const availablePeriods = computed(() => {
  return props.periodStore?.periods || []
})

// Company selector methods
const toggleCompanyDropdown = async () => {
  companyDropdownOpen.value = !companyDropdownOpen.value
  periodDropdownOpen.value = false // Close other dropdown
  if (companyDropdownOpen.value) {
    companySearchQuery.value = ''
    await nextTick()
    companySearchInput.value?.focus()
  }
}

const selectCompany = (company) => {
  if (props.companiesStore) {
    props.companiesStore.setSelectedCompany(company)
    emit('company-changed', company)
  }
  companyDropdownOpen.value = false
  companySearchQuery.value = ''
}

// Period selector methods
const togglePeriodDropdown = () => {
  periodDropdownOpen.value = !periodDropdownOpen.value
  companyDropdownOpen.value = false // Close other dropdown
}

const isPeriodSelected = (period) => {
  const selected = props.periodStore?.selectedPeriod
  if (!selected) return false
  return period.an === selected.an && period.luna === selected.luna
}

const selectPeriod = (period) => {
  if (props.periodStore) {
    props.periodStore.setSelectedPeriod(period)
    emit('period-changed', period)
  }
  periodDropdownOpen.value = false
}

// Close dropdowns when drawer closes
watch(() => props.modelValue, (isOpen) => {
  if (!isOpen) {
    companyDropdownOpen.value = false
    periodDropdownOpen.value = false
    companySearchQuery.value = ''
  }
})

/**
 * Navigation items organized by category
 * Based on US-308 acceptance criteria
 */

// PRINCIPALE: Dashboard, Bonuri
const principaleItems = [
  { to: '/dashboard', icon: 'pi pi-home', label: 'Dashboard', exactMatch: true },
  { to: '/data-entry', icon: 'pi pi-receipt', label: 'Bonuri', exactMatch: false }
]

// RAPOARTE: Facturi, Balanță, Casa și Banca
const rapoarteItems = [
  { to: '/reports/invoices', icon: 'pi pi-file', label: 'Facturi', exactMatch: true },
  { to: '/reports/trial-balance', icon: 'pi pi-calculator', label: 'Balanță', exactMatch: true },
  { to: '/reports/bank-cash', icon: 'pi pi-money-bill', label: 'Casa și Banca', exactMatch: true }
]

// ANALIZE: Scadențe, Facturi Detaliate
const analizeItems = [
  { to: '/reports/maturity-analysis', icon: 'pi pi-clock', label: 'Scadențe', exactMatch: true },
  { to: '/reports/detailed-invoices', icon: 'pi pi-list', label: 'Facturi Detaliate', exactMatch: true }
]

// ADMINISTRARE: Setări
const administrareItems = [
  { to: '/settings', icon: 'pi pi-cog', label: 'Setări', exactMatch: false }
]

/**
 * Display name for profile section
 * Falls back to 'Utilizator' if no user provided
 */
const displayName = computed(() => {
  return props.user?.username || 'Utilizator'
})

/**
 * Check if a navigation item is active based on current route
 */
const isActive = (to, exactMatch) => {
  if (exactMatch) {
    return route.path === to
  }
  // For non-exact match, check if current path starts with the route
  return route.path.startsWith(to)
}

/**
 * Close the drawer menu
 */
const close = () => {
  emit('update:modelValue', false)
}

/**
 * Handle navigation link click - close drawer after navigation
 */
const handleNavClick = () => {
  close()
}

/**
 * Handle logout action
 * Calls onLogout prop if provided, otherwise emits 'logout' event
 */
const handleLogout = async () => {
  if (props.onLogout) {
    await props.onLogout()
  } else {
    emit('logout')
  }
  close()
}
</script>

<style scoped>
/* ================================================
   MobileDrawerMenu Component Styles
   Material Design 3 inspired navigation drawer
   ================================================ */

/* Overlay background */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: var(--z-modal-backdrop);
  display: flex;
  align-items: stretch;
  justify-content: flex-start;
}

/* Main drawer container */
.drawer-menu {
  width: 280px;
  max-width: 85vw;
  height: 100%;
  background: var(--surface-card);
  box-shadow: var(--shadow-xl);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: var(--z-modal);
  /* Extra padding at bottom to ensure content is above MobileBottomNav (56px) */
  padding-bottom: calc(var(--header-height) + var(--space-md));
}

/* ================================================
   Header with Logo
   ================================================ */

.drawer-header {
  padding: var(--space-lg) var(--space-md);
  border-bottom: 1px solid var(--surface-border);
  background: var(--surface-ground);
}

.drawer-logo {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.drawer-logo i {
  font-size: var(--text-2xl);
  color: var(--color-primary);
}

.logo-text {
  font-size: var(--text-xl);
  font-weight: var(--font-bold);
  color: var(--text-color);
}

/* ================================================
   Company & Period Selectors
   ================================================ */

.drawer-selectors {
  padding: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  background: var(--surface-ground);
}

.selector-group {
  position: relative;
}

.selector-label {
  display: block;
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  color: var(--text-color-secondary);
  margin-bottom: var(--space-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.selector-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  background: var(--surface-card);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast);
  min-height: 48px;
  text-align: left;
}

.selector-trigger:hover {
  border-color: var(--color-primary);
  background: var(--surface-hover);
}

.selector-value {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.selector-main {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.selector-sub {
  font-size: var(--text-xs);
  color: var(--text-color-secondary);
  margin-top: 2px;
}

.selector-trigger .pi-chevron-down {
  font-size: var(--text-xs);
  color: var(--text-color-secondary);
  transition: transform var(--transition-fast);
  flex-shrink: 0;
}

.selector-trigger .rotate-180 {
  transform: rotate(180deg);
}

/* Selector Dropdown Panel */
.selector-panel {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: var(--space-xs);
  background: var(--surface-card);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: 10;
  max-height: 250px;
  overflow: hidden;
}

.selector-search {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm);
  border-bottom: 1px solid var(--surface-border);
}

.selector-search .pi-search {
  color: var(--text-color-secondary);
  font-size: var(--text-sm);
}

.selector-search-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: var(--text-sm);
  color: var(--text-color);
  outline: none;
}

.selector-search-input::placeholder {
  color: var(--text-color-secondary);
}

.selector-list {
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.selector-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  cursor: pointer;
  transition: background var(--transition-fast);
  min-height: 48px;
  border-bottom: 1px solid var(--surface-border);
}

.selector-item:last-child {
  border-bottom: none;
}

.selector-item:hover {
  background: var(--surface-hover);
}

.selector-item.active {
  background: var(--blue-50);
  color: var(--color-primary);
}

.selector-item.active .pi-check {
  color: var(--color-primary);
}

.selector-item-content {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.selector-item-name {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.selector-item-sub {
  font-size: var(--text-xs);
  color: var(--text-color-secondary);
  margin-top: 2px;
}

.selector-item .pi-check {
  font-size: var(--text-sm);
  flex-shrink: 0;
}

.selector-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-lg);
  color: var(--text-color-secondary);
  font-size: var(--text-sm);
}

/* ================================================
   Navigation Sections Container (scrollable)
   ================================================ */

.drawer-sections {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--space-sm) 0;
}

/* ================================================
   Individual Section
   ================================================ */

.drawer-section {
  padding: var(--space-xs) 0;
}

/* Section header label (PRINCIPALE, RAPOARTE, etc.) */
.section-header {
  padding: var(--space-sm) var(--space-lg);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  color: var(--text-color-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.drawer-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.drawer-link {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  color: var(--text-color);
  text-decoration: none;
  font-size: var(--text-base);
  font-weight: var(--font-medium);
  min-height: 48px;
  transition: background var(--transition-fast), color var(--transition-fast);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.drawer-link:hover {
  background: var(--surface-hover);
}

.drawer-link:active {
  background: var(--surface-hover);
}

/* Active state */
.drawer-link.active {
  background: var(--blue-50);
  color: var(--color-primary);
}

.drawer-link.active .drawer-icon {
  color: var(--color-primary);
}

.drawer-icon {
  font-size: var(--text-xl);
  width: 24px;
  text-align: center;
  color: var(--text-color-secondary);
  flex-shrink: 0;
}

.drawer-label {
  flex: 1;
}

/* ================================================
   Divider
   ================================================ */

.drawer-divider {
  height: 1px;
  background: var(--surface-border);
  margin: var(--space-xs) var(--space-md);
}

/* ================================================
   Profile Section
   ================================================ */

.drawer-profile {
  padding: var(--space-md) 0;
  border-top: 1px solid var(--surface-border);
  margin-top: auto;
}

.profile-header {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-sm) var(--space-lg);
  margin-bottom: var(--space-xs);
}

.profile-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--blue-100);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.profile-avatar i {
  font-size: var(--text-lg);
  color: var(--color-primary);
}

.profile-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.profile-name {
  font-size: var(--text-base);
  font-weight: var(--font-semibold);
  color: var(--text-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.profile-role {
  font-size: var(--text-sm);
  color: var(--text-color-secondary);
}

/* Logout link styling */
.logout-link {
  color: var(--color-error);
}

.logout-link .drawer-icon {
  color: var(--color-error);
}

.logout-link:hover {
  background: var(--red-50);
}

/* ================================================
   Slide Animation (Vue Transition)
   ================================================ */

.drawer-enter-active,
.drawer-leave-active {
  transition: opacity var(--transition-normal);
}

.drawer-enter-active .drawer-menu,
.drawer-leave-active .drawer-menu {
  transition: transform var(--transition-normal);
}

/* Initial state: invisible + drawer off-screen to left */
.drawer-enter-from,
.drawer-leave-to {
  opacity: 0;
}

.drawer-enter-from .drawer-menu,
.drawer-leave-to .drawer-menu {
  transform: translateX(-100%);
}

/* Final state: visible + drawer in place */
.drawer-enter-to,
.drawer-leave-from {
  opacity: 1;
}

.drawer-enter-to .drawer-menu,
.drawer-leave-from .drawer-menu {
  transform: translateX(0);
}

/* ================================================
   Dark Mode Support
   ================================================ */

/* Manual dark mode via data-theme attribute */
[data-theme="dark"] .drawer-overlay {
  background: rgba(0, 0, 0, 0.7);
}

[data-theme="dark"] .drawer-menu {
  background: var(--surface-card);
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .drawer-header {
  background: var(--surface-ground);
  border-bottom-color: var(--surface-border);
}

[data-theme="dark"] .drawer-logo i {
  color: var(--blue-400);
}

[data-theme="dark"] .logo-text {
  color: var(--text-color);
}

/* Dark mode: Selectors */
[data-theme="dark"] .drawer-selectors {
  background: var(--surface-ground);
}

[data-theme="dark"] .selector-label {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-trigger {
  background: var(--surface-card);
  border-color: var(--surface-border);
}

[data-theme="dark"] .selector-trigger:hover {
  background: var(--surface-hover);
  border-color: var(--blue-400);
}

[data-theme="dark"] .selector-main {
  color: var(--text-color);
}

[data-theme="dark"] .selector-sub {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-trigger .pi-chevron-down {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-panel {
  background: var(--surface-card);
  border-color: var(--surface-border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .selector-search {
  border-bottom-color: var(--surface-border);
}

[data-theme="dark"] .selector-search .pi-search {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-search-input {
  color: var(--text-color);
}

[data-theme="dark"] .selector-search-input::placeholder {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-item {
  border-bottom-color: var(--surface-border);
}

[data-theme="dark"] .selector-item:hover {
  background: var(--surface-hover);
}

[data-theme="dark"] .selector-item.active {
  background: var(--blue-900);
  color: var(--blue-400);
}

[data-theme="dark"] .selector-item.active .pi-check {
  color: var(--blue-400);
}

[data-theme="dark"] .selector-item-name {
  color: var(--text-color);
}

[data-theme="dark"] .selector-item.active .selector-item-name {
  color: var(--blue-400);
}

[data-theme="dark"] .selector-item-sub {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .selector-empty {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .drawer-link {
  color: var(--text-color);
}

[data-theme="dark"] .drawer-link:hover {
  background: var(--surface-hover);
}

[data-theme="dark"] .drawer-link.active {
  background: var(--blue-900);
  color: var(--blue-400);
}

[data-theme="dark"] .drawer-link.active .drawer-icon {
  color: var(--blue-400);
}

[data-theme="dark"] .drawer-icon {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .drawer-divider {
  background: var(--surface-border);
}

[data-theme="dark"] .section-header {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .drawer-profile {
  border-top-color: var(--surface-border);
}

[data-theme="dark"] .profile-avatar {
  background: var(--blue-900);
}

[data-theme="dark"] .profile-avatar i {
  color: var(--blue-400);
}

[data-theme="dark"] .profile-name {
  color: var(--text-color);
}

[data-theme="dark"] .profile-role {
  color: var(--text-color-secondary);
}

[data-theme="dark"] .logout-link {
  color: var(--red-400);
}

[data-theme="dark"] .logout-link .drawer-icon {
  color: var(--red-400);
}

[data-theme="dark"] .logout-link:hover {
  background: var(--red-900);
}

/* Auto dark mode (when no manual theme is set) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .drawer-overlay {
    background: rgba(0, 0, 0, 0.7);
  }

  :root:not([data-theme]) .drawer-menu {
    background: var(--surface-card);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }

  :root:not([data-theme]) .drawer-header {
    background: var(--surface-ground);
    border-bottom-color: var(--surface-border);
  }

  :root:not([data-theme]) .drawer-logo i {
    color: var(--blue-400);
  }

  :root:not([data-theme]) .logo-text {
    color: var(--text-color);
  }

  /* Auto dark mode: Selectors */
  :root:not([data-theme]) .drawer-selectors {
    background: var(--surface-ground);
  }

  :root:not([data-theme]) .selector-label {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-trigger {
    background: var(--surface-card);
    border-color: var(--surface-border);
  }

  :root:not([data-theme]) .selector-trigger:hover {
    background: var(--surface-hover);
    border-color: var(--blue-400);
  }

  :root:not([data-theme]) .selector-main {
    color: var(--text-color);
  }

  :root:not([data-theme]) .selector-sub {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-trigger .pi-chevron-down {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-panel {
    background: var(--surface-card);
    border-color: var(--surface-border);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  }

  :root:not([data-theme]) .selector-search {
    border-bottom-color: var(--surface-border);
  }

  :root:not([data-theme]) .selector-search .pi-search {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-search-input {
    color: var(--text-color);
  }

  :root:not([data-theme]) .selector-search-input::placeholder {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-item {
    border-bottom-color: var(--surface-border);
  }

  :root:not([data-theme]) .selector-item:hover {
    background: var(--surface-hover);
  }

  :root:not([data-theme]) .selector-item.active {
    background: var(--blue-900);
    color: var(--blue-400);
  }

  :root:not([data-theme]) .selector-item.active .pi-check {
    color: var(--blue-400);
  }

  :root:not([data-theme]) .selector-item-name {
    color: var(--text-color);
  }

  :root:not([data-theme]) .selector-item.active .selector-item-name {
    color: var(--blue-400);
  }

  :root:not([data-theme]) .selector-item-sub {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .selector-empty {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .drawer-link {
    color: var(--text-color);
  }

  :root:not([data-theme]) .drawer-link:hover {
    background: var(--surface-hover);
  }

  :root:not([data-theme]) .drawer-link.active {
    background: var(--blue-900);
    color: var(--blue-400);
  }

  :root:not([data-theme]) .drawer-link.active .drawer-icon {
    color: var(--blue-400);
  }

  :root:not([data-theme]) .drawer-icon {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .drawer-divider {
    background: var(--surface-border);
  }

  :root:not([data-theme]) .section-header {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .drawer-profile {
    border-top-color: var(--surface-border);
  }

  :root:not([data-theme]) .profile-avatar {
    background: var(--blue-900);
  }

  :root:not([data-theme]) .profile-avatar i {
    color: var(--blue-400);
  }

  :root:not([data-theme]) .profile-name {
    color: var(--text-color);
  }

  :root:not([data-theme]) .profile-role {
    color: var(--text-color-secondary);
  }

  :root:not([data-theme]) .logout-link {
    color: var(--red-400);
  }

  :root:not([data-theme]) .logout-link .drawer-icon {
    color: var(--red-400);
  }

  :root:not([data-theme]) .logout-link:hover {
    background: var(--red-900);
  }
}
</style>
