<script setup lang="ts">
const menuOpen = ref(false)
const fontReady = ref(false)
const navHidden = ref(false)

const route = useRoute()

const links = [
  { label: '发现', to: '/discover' },
  { label: '作品集', to: '/portfolio' },
  { label: '关于', to: '/about' },
]

let lastScrollY = 0
const HIDE_OFFSET = 80

function onScroll() {
  const y = window.scrollY
  if (y < HIDE_OFFSET) {
    navHidden.value = false
  } else if (y > lastScrollY + 8) {
    navHidden.value = true
  } else if (y < lastScrollY - 8) {
    navHidden.value = false
  }
  lastScrollY = y
}

onMounted(async () => {
  await useGoogleFont([{ family: 'Liu Jian Mao Cao', weight: 400 }])
  fontReady.value = true
  window.addEventListener('scroll', onScroll, { passive: true })
})

onUnmounted(() => {
  window.removeEventListener('scroll', onScroll)
})

watch(() => route.path, () => {
  menuOpen.value = false
})
</script>

<template>
  <nav class="top-nav" :style="{ transform: navHidden ? 'translateY(-100%)' : 'translateY(0)' }">
    <div class="nav-inner">
      <NuxtLink to="/" class="nav-logo" :class="{ ready: fontReady }">
        Dash
      </NuxtLink>

      <ul class="nav-links" :class="{ open: menuOpen }">
        <li v-for="link in links" :key="link.to">
          <NuxtLink
            :to="link.to"
            class="nav-link"
            active-class="active"
          >
            {{ link.label }}
          </NuxtLink>
        </li>
      </ul>

      <button
        class="nav-toggle"
        :class="{ open: menuOpen }"
        aria-label="菜单"
        @click="menuOpen = !menuOpen"
      >
        <span />
        <span />
        <span />
      </button>
    </div>

    <Teleport to="body">
      <div
        v-if="menuOpen"
        class="nav-overlay"
        @click="menuOpen = false"
      />
    </Teleport>
  </nav>
</template>

<style scoped>
.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  height: 64px;
  background: var(--color-canvas);
  border-bottom: 1px solid var(--color-hairline);
  transition: transform 0.35s ease;
  will-change: transform;
}

.nav-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ── Logo ── */

.nav-logo {
  font-family: 'Liu Jian Mao Cao', cursive;
  font-size: 28px;
  font-weight: 500;
  color: var(--color-ink);
  text-decoration: none;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.nav-logo.ready {
  opacity: 1;
}

/* ── Desktop links ── */

.nav-links {
  display: flex;
  gap: 32px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-link {
  font-family: var(--font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--color-muted);
  text-decoration: none;
  transition: color 0.2s ease;
  padding: 4px 0;
}

.nav-link:hover {
  color: var(--color-ink);
}

.nav-link.active {
  color: var(--color-ink);
}

/* ── Hamburger ── */

.nav-toggle {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
}

.nav-toggle span {
  display: block;
  height: 2px;
  background: var(--color-ink);
  border-radius: 1px;
  transition: transform 0.25s ease, opacity 0.25s ease;
}

.nav-toggle.open span:nth-child(1) {
  transform: translateY(7px) rotate(45deg);
}

.nav-toggle.open span:nth-child(2) {
  opacity: 0;
}

.nav-toggle.open span:nth-child(3) {
  transform: translateY(-7px) rotate(-45deg);
}

/* ── Overlay ── */

.nav-overlay {
  position: fixed;
  inset: 0;
  z-index: 99;
  background: rgba(20, 20, 19, 0.3);
  backdrop-filter: blur(4px);
}

/* ── Mobile ── */

@media (max-width: 768px) {
  .nav-links {
    position: fixed;
    top: 64px;
    right: 0;
    z-index: 100;
    flex-direction: column;
    gap: 0;
    width: 240px;
    background: var(--color-canvas);
    border-left: 1px solid var(--color-hairline);
    border-bottom: 1px solid var(--color-hairline);
    border-radius: 0 0 12px 0;
    padding: 8px 0;
    transform: translateX(100%);
    transition: transform 0.3s ease;
  }

  .nav-links.open {
    transform: translateX(0);
  }

  .nav-link {
    display: block;
    padding: 14px 24px;
    font-size: 15px;
    border-bottom: 1px solid var(--color-hairline-soft);
  }

  .nav-link:last-child {
    border-bottom: none;
  }

  .nav-toggle {
    display: flex;
  }
}
</style>
