---
// Shared shell: head, nav, footer, theme handling.
// The whole site is this layout plus two pages (index and 404).
import '../styles/global.css';
import { SITE } from '../config';

interface Props {
  title: string;
  description?: string;
  /** rel=canonical path, e.g. "/" or "/404.html". Defaults to the current path. */
  canonical?: string;
}

const { title, description = SITE.description, canonical } = Astro.props;
const canonicalURL = new URL(canonical ?? Astro.url.pathname, Astro.site ?? SITE.url);
---

<!doctype html>
<html lang="en" data-theme="dark">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{title}</title>
    <meta name="description" content={description} />
    <link rel="canonical" href={canonicalURL} />
    <link rel="icon" href="/favicon.svg" type="image/svg+xml" />
    <meta name="color-scheme" content="dark light" />

    <meta property="og:type" content="website" />
    <meta property="og:site_name" content={SITE.domain} />
    <meta property="og:title" content={title} />
    <meta property="og:description" content={description} />
    <meta property="og:url" content={canonicalURL} />
    <meta name="twitter:card" content="summary" />

    <script is:inline>
      try {
        document.documentElement.dataset.theme = localStorage.getItem('hb-theme') || 'dark';
      } catch (e) {}
    </script>
  </head>
  <body>
    <a class="skip-link" href="#main">Skip to content</a>

    <header class="nav">
      <div class="nav-inner">
        <a class="brand" href="/">hanthebot<span class="dot">.com</span></a>
        <span class="nav-tag">lab</span>
        <div class="nav-right">
          <a class="nav-link" href={SITE.links.public}>hanthebot.dev <span aria-hidden="true">→</span></a>
          <button id="theme-btn" type="button" aria-label="Toggle color theme">Light</button>
        </div>
      </div>
    </header>

    <main class="wrap" id="main">
      <slot />
    </main>

    <footer>
      <div class="wrap">
        <div class="btns88" aria-hidden="true">
          <div class="btn88">engine room</div>
          <div class="btn88 alt1">no docker</div>
          <div class="btn88 alt2">no accounts</div>
          <div class="btn88 alt3">nginx · systemd</div>
        </div>
        <p class="foot-status">© <span id="year"></span> {SITE.name}</p>
        <p class="foot-links">
          <a href={SITE.links.public}>hanthebot.dev</a>
          <a href={SITE.links.notes} target="_blank" rel="noopener">Lecture notes</a>
          <a href={SITE.links.github} target="_blank" rel="noopener">GitHub</a>
        </p>
        <p class="foot-canonical">
          hanthebot.com is the lab; hanthebot.dev is the public site. Static HTML,
          no trackers, no external requests.
        </p>
      </div>
    </footer>

    <script>
      const yearEl = document.getElementById('year');
      if (yearEl) yearEl.textContent = String(new Date().getFullYear());

      const btn = document.getElementById('theme-btn');
      const rootEl = document.documentElement;
      const apply = (t: string) => {
        if (btn) btn.textContent = t === 'dark' ? 'Light' : 'Dark';
      };
      apply(rootEl.dataset.theme || 'dark');
      btn?.addEventListener('click', () => {
        const next = rootEl.dataset.theme === 'dark' ? 'light' : 'dark';
        rootEl.dataset.theme = next;
        apply(next);
        try { localStorage.setItem('hb-theme', next); } catch (e) {}
      });
    </script>
  </body>
</html>
