---
import BaseLayout from './BaseLayout.astro';
import type { UserPublic } from '../lib/db';

interface Props {
  title: string;
  user: UserPublic;
}

const { title, user } = Astro.props;

const navLinks = [
  { href: '/pulse',   label: 'Pulse'   },
  { href: '/roadmap', label: 'Roadmap' },
];

const footerLinks = [
  { href: '/vision', label: 'Vision' },
];

const currentPath = Astro.url.pathname;
const year = new Date().getFullYear();
---
<BaseLayout title={title}>
  <div class="app">
    <header class="nav" role="banner">
      <div class="nav-inner">
        <a href="/" class="wordmark-link" aria-label="Project Bifrost — home">
          <img src="/logo.svg" alt="Fenja AI" class="wordmark" />
          <span class="wordmark-sep" aria-hidden="true">·</span>
          <span class="wordmark-project">Project <em class="wordmark-bifrost">Bifrost</em></span>
        </a>

        <nav class="nav-right" aria-label="Main navigation">
          {navLinks.map(({ href, label }) => (
            <a
              href={href}
              class:list={['nav-link', { active: currentPath === href || (href !== '/' && currentPath.startsWith(href)) }]}
            >
              {label}
            </a>
          ))}
          {user.role === 'fenja' && (
            <a
              href="/admin"
              class:list={['nav-link', { active: currentPath.startsWith('/admin') }]}
            >
              Admin
            </a>
          )}
          <span class="nav-divider" aria-hidden="true"></span>
          <a href="/account" class="nav-user-name body-sm">{user.name}</a>
          <form method="POST" action="/api/logout" class="nav-logout-form">
            <button type="submit" class="logout-btn label-sm">Sign out</button>
          </form>
        </nav>
      </div>
    </header>

    <main class="main-content">
      <slot />
    </main>

    <footer class="footer" role="contentinfo">
      <div class="footer-inner">
        <div class="footer-left">
          <img src="/logo-icon.svg" alt="" class="footer-mark" />
          <span class="footer-copy label-sm">© {year} Fenja AI</span>
        </div>
        <nav class="footer-links" aria-label="Secondary">
          {footerLinks.map(({ href, label }) => (
            <a href={href} class="footer-link label-sm">{label}</a>
          ))}
        </nav>
      </div>
    </footer>
  </div>
</BaseLayout>

<style>
  .app {
    min-height: 100vh;
    background: var(--background);
    display: flex;
    flex-direction: column;
  }

  /* ── Nav ────────────────────────────────────────────────────────── */
  .nav {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--glass-surface);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border-bottom: var(--ghost-border);
  }

  .nav-inner {
    display: flex;
    align-items: center;
    gap: var(--space-6);
    padding: 0 var(--space-10);
    height: 56px;
    max-width: var(--content-max);
    margin: 0 auto;
    width: 100%;
  }

  .wordmark-link {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: none;
    color: var(--on-surface);
    line-height: 1;       /* belt + braces — no nav-row leading on the lockup */
  }
  .wordmark-link:hover {
    border-bottom: none;
    color: var(--on-surface);
  }
  .wordmark {
    height: 20px;
    width: auto;
    display: block;
  }
  .wordmark-sep {
    color: var(--on-surface-muted);
    font-family: var(--font-serif);
    font-size: 18px;
    line-height: 1;
    /* Optical kern — the bullet's typographic centre sits slightly above
       its baseline in Newsreader; this nudges it onto the visual midline. */
    transform: translateY(-2px);
  }
  /* Italic Newsreader renders ~10% visually taller than regular at the
     same font-size — the cursive B has a flourish extending past the
     cap line. Drop Bifrost to 16px so its cap+flourish optical height
     matches Project's 18px cap, and use inline-block + tiny vertical
     padding so the gradient-clip bbox doesn't chop the flourish off. */
  .wordmark-project,
  .wordmark-bifrost {
    font-family: var(--font-serif);
    font-weight: 400;
    letter-spacing: var(--tracking-snug);
    line-height: 1.4;
  }
  .wordmark-project {
    font-size: 18px;
    color: var(--on-surface);
  }
  .wordmark-bifrost {
    display: inline-block;
    font-size: 16px;
    font-style: italic;
    padding: 3px 0 1px;
    vertical-align: baseline;
    background-image: linear-gradient(
      90deg,
      var(--pigment-terracotta) 0%,
      var(--pigment-ochre)      28%,
      var(--pigment-copper)     54%,
      var(--pigment-indigo)     78%,
      var(--pigment-heather)    100%
    );
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
  }

  /* ── Nav links ──────────────────────────────────────────────────── */
  .nav-right {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    margin-left: auto;
  }
  .nav-divider {
    display: inline-block;
    width: 1px;
    height: 18px;
    background: rgba(0, 0, 0, 0.15);
    margin: 0 18px;
    transform: scaleX(0.5);
    transform-origin: center;
  }
  .nav-logout-form { display: inline-flex; }

  .nav-link {
    position: relative;
    font-family: var(--font-sans);
    font-size: var(--text-label-md);
    font-weight: 500;
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    color: var(--on-surface-variant);
    text-decoration: none;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    border-bottom: none;
    transition: color var(--duration-fast) var(--ease-standard);
  }
  .nav-link:hover {
    color: var(--on-surface);
    border-bottom: none;
  }
  /* Active nav link: terracotta + slightly heavier weight. Colour alone
     is the indicator — no badge, bullet, italic, or family swap. */
  .nav-link.active {
    color: var(--pigment-terracotta);
    font-weight: 500;
  }

  /* ── User zone ──────────────────────────────────────────────────── */
  .nav-user-name {
    color: var(--on-surface-variant);
    text-decoration: none;
    border-bottom: none;
    transition: color var(--duration-fast) var(--ease-standard);
  }
  .nav-user-name:hover {
    color: var(--on-surface);
    border-bottom: none;
  }

  .logout-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-sans);
    font-size: var(--text-label-md);
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    color: var(--on-surface-muted);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    transition: color var(--duration-fast) var(--ease-standard),
                background var(--duration-fast) var(--ease-standard);
  }
  .logout-btn:hover {
    color: var(--on-surface);
    background: var(--surface-container-low);
  }

  /* ── Content ────────────────────────────────────────────────────── */
  .main-content {
    flex: 1;
  }

  /* ── Footer ─────────────────────────────────────────────────────── */
  .footer {
    margin-top: var(--space-16);
    padding: var(--space-6) 0;
    border-top: var(--ghost-border);
  }

  .footer-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-6);
    padding: 0 var(--space-10);
    max-width: var(--content-max);
    margin: 0 auto;
    width: 100%;
  }

  .footer-left {
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }

  .footer-mark {
    height: 16px;
    width: auto;
    display: block;
    opacity: 0.7;
  }

  .footer-copy {
    color: var(--on-surface-muted);
    letter-spacing: var(--tracking-wide);
    font-weight: 400;
  }

  .footer-links {
    display: flex;
    gap: var(--space-5);
  }

  .footer-link {
    color: var(--on-surface-muted);
    text-decoration: none;
    border-bottom: none;
    letter-spacing: var(--tracking-wide);
    text-transform: uppercase;
    transition: color var(--duration-fast) var(--ease-standard);
  }
  .footer-link:hover {
    color: var(--on-surface-variant);
    border-bottom: none;
  }
</style>
