<template>
  <div class="level-select">
    <header>
      <router-link to="/" class="back">&larr; Home</router-link>
      <div class="player-info">
        <span class="player-name">{{ store.playerName }}</span>
        <span class="total-stars">{{ store.totalStars }} &#11088;</span>
      </div>
    </header>

    <h1>Select Level</h1>
    <p class="desc">Complete a level to unlock the next one. Up to 3 stars per level.</p>

    <div class="levels-grid">
      <div
        v-for="level in levels"
        :key="level.id"
        class="level-card"
        :class="{ locked: !unlocked(level.id), completed: !!store.progress[level.id]?.completed }"
        @click="go(level)"
      >
        <div class="level-num">{{ level.id }}</div>
        <h3>{{ level.title }}</h3>
        <p class="card-subtitle">{{ level.subtitle }}</p>
        <p class="card-desc">{{ level.description }}</p>
        <div class="stars">
          <span v-for="s in 3" :key="s" :class="{ earned: s <= (store.progress[level.id]?.stars || 0) }">&#9733;</span>
        </div>
        <div v-if="!unlocked(level.id)" class="lock-overlay">&#128274;</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useGameStore } from '../stores/game.js'
import { levels } from '../lib/levels.js'

const router = useRouter()
const store = useGameStore()
if (!store.isLoggedIn) router.replace('/')

const unlocked = (id) => store.isLevelUnlocked(id)

function go(level) {
  if (unlocked(level.id)) router.push(`/level/${level.id}`)
}
</script>

<style scoped>
.level-select { max-width: 960px; margin: 0 auto; padding: 24px; }
header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 32px; }
.back { color: var(--text-secondary); font-size: 14px; }
.player-info { display: flex; gap: 16px; align-items: center; }
.player-name { color: var(--accent-cyan); font-weight: 600; }
.total-stars { color: var(--accent-yellow); }
h1 { font-size: 28px; margin-bottom: 8px; text-align: center; }
.desc { text-align: center; color: var(--text-secondary); margin-bottom: 36px; }

.levels-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}
.level-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
  overflow: hidden;
}
.level-card:hover:not(.locked) {
  border-color: var(--accent-blue);
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(59,130,246,0.12);
}
.level-card.completed { border-color: rgba(16,185,129,0.4); }
.level-card.locked { opacity: 0.45; cursor: not-allowed; }
.level-num {
  position: absolute;
  top: 10px;
  right: 16px;
  font-family: var(--font-mono);
  font-size: 52px;
  font-weight: 700;
  color: var(--border);
  line-height: 1;
}
.level-icon { font-size: 32px; margin-bottom: 10px; }
.level-card h3 { font-size: 18px; margin-bottom: 4px; }
.card-subtitle { font-size: 13px; color: var(--accent-cyan); margin-bottom: 8px; }
.card-desc { font-size: 13px; color: var(--text-secondary); margin-bottom: 16px; line-height: 1.4; }
.stars { display: flex; gap: 4px; font-size: 20px; }
.stars span { color: var(--border); transition: color 0.3s; }
.stars span.earned { color: var(--accent-yellow); }
.lock-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: rgba(10,14,26,0.5);
  backdrop-filter: blur(2px);
}
</style>
