<template>
  <div id="app">
    <nav class="navbar">
      <div class="navbar-brand">
        <span class="brand-icon">🧠</span>
        <span class="brand-name">AI Teacher</span>
        <span class="brand-subtitle">Neurosurgeon Learning Platform</span>
      </div>
      <template v-if="authStore.isAuthenticated">
        <button class="burger" :class="{ open: menuOpen }" @click="menuOpen = !menuOpen" aria-label="Menu">
          <span></span><span></span><span></span>
        </button>
        <div class="nav-drawer" :class="{ open: menuOpen }" @click="menuOpen = false">
          <ul class="navbar-links">
            <li>
              <RouterLink to="/" :class="{ active: $route.path === '/' }">
                <span class="nav-icon">📚</span> Library
              </RouterLink>
            </li>
            <li>
              <RouterLink to="/topics" :class="{ active: $route.path === '/topics' }">
                <span class="nav-icon">🗂</span> Topics
              </RouterLink>
            </li>
            <li>
              <RouterLink to="/chat" :class="{ active: $route.path === '/chat' }">
                <span class="nav-icon">💬</span> Chat
              </RouterLink>
            </li>
          </ul>
          <button class="btn btn-logout" @click.stop="logout">Sign out</button>
        </div>
      </template>
    </nav>

    <main class="main-content">
      <div v-if="toastMessage" class="toast" :class="toastType" @click="toastMessage = ''">
        {{ toastMessage }}
      </div>
      <RouterView />
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, provide, watch } from 'vue'
import { RouterLink, RouterView, useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/authStore'

const authStore = useAuthStore()
const router = useRouter()
const route = useRoute()

const menuOpen = ref(false)
const toastMessage = ref('')
const toastType = ref<'toast-error' | 'toast-success'>('toast-error')

// Close menu on navigation
watch(() => route.path, () => { menuOpen.value = false })

function logout() {
  authStore.clearCredentials()
  router.push({ name: 'login' })
}

function showToast(message: string, type: 'error' | 'success' = 'error') {
  toastMessage.value = message
  toastType.value = type === 'error' ? 'toast-error' : 'toast-success'
  setTimeout(() => {
    toastMessage.value = ''
  }, 5000)
}

provide('showToast', showToast)
</script>

<style>
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
  background: #f0f4f8;
  color: #2d3748;
  height: 100vh;
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.navbar {
  background: #1a365d;
  color: white;
  padding: 0 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  position: sticky;
  top: 0;
  z-index: 100;
}

.navbar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.brand-icon {
  font-size: 1.5rem;
}

.brand-name {
  font-size: 1.2rem;
  font-weight: 700;
  color: #bee3f8;
}

.brand-subtitle {
  font-size: 0.8rem;
  color: #90cdf4;
  margin-left: 0.25rem;
}

/* Desktop: links inline */
.nav-drawer {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.navbar-links {
  list-style: none;
  display: flex;
  gap: 0.5rem;
}

.navbar-links a {
  color: #bee3f8;
  text-decoration: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 500;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.navbar-links a:hover,
.navbar-links a.active {
  background: #2b6cb0;
  color: white;
}

/* Burger button — hidden on desktop */
.burger {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 36px;
  height: 36px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}

.burger span {
  display: block;
  height: 2px;
  background: #bee3f8;
  border-radius: 2px;
  transition: transform 0.2s, opacity 0.2s;
}

.burger.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.burger.open span:nth-child(2) { opacity: 0; }
.burger.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.main-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
  position: relative;
}

.toast {
  position: fixed;
  top: 80px;
  right: 2rem;
  padding: 1rem 1.5rem;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  z-index: 1000;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.2s ease;
}

.toast-error {
  background: #fed7d7;
  color: #c53030;
  border: 1px solid #fc8181;
}

.toast-success {
  background: #c6f6d5;
  color: #276749;
  border: 1px solid #68d391;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* Shared utility classes */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: #3182ce;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #2b6cb0;
}

.btn-danger {
  background: #e53e3e;
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: #c53030;
}

.btn-secondary {
  background: #e2e8f0;
  color: #4a5568;
}

.btn-secondary:hover:not(:disabled) {
  background: #cbd5e0;
}

.btn-logout {
  background: transparent;
  color: #bee3f8;
  border: 1px solid #4a90b8;
  font-size: 0.85rem;
  padding: 0.4rem 0.9rem;
  margin-left: 1rem;
}

.btn-logout:hover {
  background: #2b6cb0;
  color: white;
}

.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.4);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.spinner-dark {
  border-color: rgba(49, 130, 206, 0.3);
  border-top-color: #3182ce;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.card {
  background: white;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
}

.page-title {
  font-size: 1.8rem;
  font-weight: 700;
  color: #1a365d;
  margin-bottom: 0.5rem;
}

.page-subtitle {
  color: #718096;
  margin-bottom: 2rem;
}

.empty-state {
  text-align: center;
  padding: 3rem 2rem;
  color: #a0aec0;
}

.empty-state-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.empty-state-text {
  font-size: 1.1rem;
  font-weight: 500;
}

.empty-state-hint {
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

@media (max-width: 768px) {
  .navbar {
    padding: 0 1rem;
  }

  .brand-subtitle {
    display: none;
  }

  /* Show burger, hide desktop drawer */
  .burger {
    display: flex;
  }

  .nav-drawer {
    display: none;
    position: absolute;
    top: 64px;
    right: 0;
    left: 0;
    background: #1a365d;
    flex-direction: column;
    align-items: stretch;
    padding: 0.5rem 0 1rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 99;
  }

  .nav-drawer.open {
    display: flex;
  }

  .navbar-links {
    flex-direction: column;
    gap: 0;
  }

  .navbar-links a {
    padding: 0.85rem 1.5rem;
    border-radius: 0;
    font-size: 1rem;
  }

  .navbar-links a:hover,
  .navbar-links a.active {
    background: #2b6cb0;
  }

  .btn-logout {
    margin: 0.5rem 1.5rem 0;
    width: calc(100% - 3rem);
    justify-content: center;
  }

  .main-content {
    padding: 1rem;
  }
}
</style>
