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

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

<style scoped>
.layout-root {
  width: 100%;
  min-height: 100vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  background: var(--header-bg, linear-gradient(135deg, #667eea 0%, #764ba2 100%));
}

.topbar {
  display: flex;
  align-items: stretch;
  padding: 0;
  height: 48px;
  width: 100%;
  box-sizing: border-box;
}

.end-button-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 65px;
  min-width: 65px;
  max-width: 65px;
  height: 48px;
  min-height: 48px;
  max-height: 48px;
  margin-left: 5px;
  margin-right: 5px;
  box-sizing: border-box;
}

.back-btn {
  width: 65px;
  min-width: 65px;
  max-width: 65px;
  height: 48px;
  min-height: 48px;
  max-height: 48px;
  margin: 0;
  background: var(--button-bg, #fff);
  border: 0;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  color: var(--button-text, #667eea);
  font-weight: 600;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  overflow: hidden;
  padding: 0;
  transition:
    background 0.18s,
    color 0.18s;
}

@media (max-width: 480px) {
  .back-btn {
    font-size: 0.7rem;
  }
}

.spacer {
  flex: 1 1 auto;
  height: 100%;
  display: flex;
  align-items: center;
}

.main-content {
  flex: 1 1 auto;
  width: 100%;
  justify-content: center;
  align-items: flex-start;
  box-sizing: border-box;
  min-height: 0;
  height: 0;
  overflow: hidden;
  overflow-y: visible;
  padding: 1rem;
}
</style>
