<template>
  <div class="app-layout">
    <header class="app-header">
      <div class="container header-inner">
        <NuxtLink to="/" class="logo">SejeteralO</NuxtLink>
        <div class="header-right">
          <template v-if="authStore.isAuthenticated && authStore.isAdmin">
            <NuxtLink
              v-if="authStore.isSuperAdmin"
              to="/admin"
              class="header-link"
            >
              Administration
            </NuxtLink>
            <NuxtLink
              v-else-if="authStore.communeSlug"
              :to="`/admin/communes/${authStore.communeSlug}`"
              class="header-link"
            >
              Gestion commune
            </NuxtLink>
            <button class="btn btn-secondary btn-sm" @click="logout">Deconnexion</button>
          </template>
          <DisplaySettings />
        </div>
      </div>
    </header>
    <main class="app-main container">
      <slot />
    </main>
    <footer class="app-footer">
      <div class="container">
        SejeteralO — Outil de democratie participative pour la tarification de l'eau
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
const authStore = useAuthStore()
const router = useRouter()

function logout() {
  authStore.logout()
  router.push('/')
}
</script>

<style scoped>
.app-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: 0.75rem 0;
  position: sticky;
  top: 0;
  z-index: 40;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  font-size: clamp(1.05rem, 3vw, 1.25rem);
  font-weight: 700;
  color: var(--color-primary);
}
.logo:hover { text-decoration: none; }

.header-right {
  display: flex;
  align-items: center;
  gap: clamp(0.5rem, 2vw, 1rem);
}

.header-link {
  font-size: clamp(0.75rem, 2vw, 0.85rem);
  white-space: nowrap;
  color: var(--color-text-muted);
}
.header-link:hover { color: var(--color-primary); text-decoration: none; }

.btn-sm {
  padding: 0.25rem 0.75rem;
  font-size: 0.8rem;
}

.app-main {
  flex: 1;
  padding-top: clamp(1rem, 3vw, 2rem);
  padding-bottom: clamp(1rem, 3vw, 2rem);
}

.app-footer {
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding: 1rem 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}
</style>
