<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'
import LoginButton from '../components/LoginButton.vue'

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

const handleBack = () => {
  if (window.history.length > 1) {
    router.back()
  } else {
    router.push('/child')
  }
}

const showBack = computed(() => route.path !== '/child')
</script>

<template>
  <div class="layout-root">
    <header class="topbar">
      <div class="topbar-inner">
        <button v-if="showBack" class="back-btn" @click="handleBack">← Back</button>
        <div class="spacer"></div>
        <LoginButton />
      </div>
    </header>
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<style scoped>
.layout-root {
  width: 100%;
  box-sizing: border-box;
  min-height: 100vh;
  display: flex;
  flex-direction: column;

  /* Reduce top padding */
  padding: 0.5rem 2rem 2rem 2rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* top bar holds login button at top-right */
.topbar {
  width: 100%;
  padding: 12px 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
}

.topbar-inner {
  width: 100%;
  max-width: 1200px;
  display: flex;
  align-items: center;
}

/* spacer pushes button to the right */
.spacer {
  flex: 1;
}

/* main content remains centered */
.main-content {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start; /* content starts higher */
  box-sizing: border-box;

  /* Reduce top padding */
  padding: 4px 20px 40px;
}

/* back button style */
.back-btn {
  background: white;
  border: 0;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 0;
  color: #667eea;
  font-weight: 600;
  margin-top: 0;
}

.back-btn:hover {
  background-color: #764ba2;
}

@media (max-width: 480px) {
  .back-btn {
    padding: 0.45rem 0.75rem;
    font-size: 0.95rem;
    margin-bottom: 0.7rem;
  }
}
</style>
