<?php
require_once __DIR__ . '/lib/core.php';
require_once __DIR__ . '/lib/logos.php';

$SITE  = detect_site();
$LANG  = detect_lang();
$OTHER = $SITE === 'cd' ? 'aftp' : 'cd';

$all     = load_json('content');
$C       = $all[$SITE] ?? [];
$P       = $all[$OTHER] ?? [];
$team    = array_values(array_filter(load_json('team'), fn($m) => in_array($m['site'] ?? '', [$SITE, 'both'], true)));
$upcoming = events_for($SITE);
$past     = events_for($SITE, true);

$names = ['cd' => 'Circle Defense', 'aftp' => 'AFTP'];
$B = $C['book'] ?? [];
$bookLinks = array_values(array_filter($B['links'] ?? [], fn($l) => safe_url($l['url'] ?? '') !== ''));
$bookAll = array_values(array_filter($B['links'] ?? [], fn($l) => tr($l['label'] ?? '') !== ''));
// put the edition matching the visitor's language first
usort($bookAll, function ($a, $b) use ($LANG) {
    // available editions first; within those, the visitor's language, then English as the common fallback
    $score = fn($l) => (safe_url($l['url'] ?? '') ? 0 : 4) + (($l['lang'] ?? '') === $LANG ? 0 : (($l['lang'] ?? '') === 'en' ? 1 : 2));
    return $score($a) <=> $score($b);
});
// Library (PDF documents), filtered for this site
$library = array_values(array_filter(load_json('library'), fn($d) => !empty($d['visible']) && in_array($d['sites'] ?? 'cd', [$SITE, 'both'], true) && !empty($d['files'])));
$libLangs = array_values(array_intersect(LANGS, array_unique(array_column($library, 'lang'))));
$libCats = array_values(array_intersect(LIB_CATS, array_unique(array_column($library, 'cat'))));
$libDefault = in_array($LANG, $libLangs, true) ? $LANG : 'all';
// external resource links (anything that isn't replaced by the library)
$resItems = $C['resources']['items'] ?? [];

// Legal pages (?page=legal / ?page=privacy) reuse header + footer
$PAGE = in_array($_GET['page'] ?? '', ['legal', 'privacy'], true) ? $_GET['page'] : '';
$A = $PAGE ? site_url($SITE) : '';   // anchor prefix so the menu still works from a legal page
$page_url = function (string $pg) { $q = $_GET; unset($q['fstatus'], $q['fmsg'], $q['lang']); $q['page'] = $pg; return '?' . http_build_query($q); };
function fmt_bytes(int $b, string $lang): string {
    $u = $lang === 'de' ? ['KB', 'MB'] : ($lang === 'en' ? ['KB', 'MB'] : ['Ko', 'Mo']);
    return $b >= 1048576 ? number_format($b / 1048576, 1, $lang === 'en' ? '.' : ',', '') . ' ' . $u[1] : max(1, round($b / 1024)) . ' ' . $u[0];
}

$flash = null;
if (!empty($_GET['fstatus'])) $flash = ['ok' => $_GET['fstatus'] === 'ok', 'msg' => mb_substr($_GET['fmsg'] ?? '', 0, 200)];
$ts = time();

function book_icon(): string {
    return '<svg class="bk-ic" viewBox="0 0 20 20" width="18" height="18" aria-hidden="true"><path d="M3 4.5C5.5 3.5 8 3.6 10 5c2-1.4 4.5-1.5 7-.5v11c-2.5-1-5-.9-7 .5-2-1.4-4.5-1.5-7-.5z" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/><path d="M10 5v11" stroke="currentColor" stroke-width="1.5"/></svg>';
}

function site_switch(string $current, array $names, string $cls): string {
    $h = '<nav class="' . $cls . '" aria-label="' . e(t('choose_site')) . '">';
    foreach (['cd', 'aftp'] as $s) {
        $on = $s === $current;
        $h .= '<a href="' . e(site_url($s)) . '" class="sw' . ($on ? ' on' : '') . '"' . ($on ? ' aria-current="page"' : '') . '>'
            . site_mark($s, 'sw-mark') . '<span class="sw-name">' . e($names[$s]) . '</span></a>';
    }
    return $h . '</nav>';
}

function brand(string $site, string $cls = ''): string {
    if ($site === 'aftp') {
        return '<span class="brand brand-aftp ' . $cls . '" aria-label="AFTP">' . aftp_mark('brand-mark')
             . '<span class="brand-word"><span class="brand-name">AFTP</span><span class="brand-sub">Association Fribourgeoise<br>de Tir Pratique</span><span class="brand-motto">Exigence et bienveillance</span></span></span>';
    }
    return '<span class="brand brand-cd ' . $cls . '" aria-label="Circle Defense SNC">' . cd_mark('brand-mark')
         . '<span class="brand-word" aria-hidden="true">ircle Defense <small>SNC</small></span></span>';
}
?><!doctype html>
<html lang="<?= e($LANG) ?>" data-site="<?= e($SITE) ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= $PAGE ? e(t($PAGE)) . ' · ' : '' ?><?= e($C['meta']['title'] ?? $names[$SITE]) ?></title>
<?php if ($PAGE): ?><meta name="robots" content="noindex, follow"><?php endif; ?>
<script>document.documentElement.classList.add('js')</script>
<meta name="description" content="<?= e(tr($C['meta']['description'] ?? '')) ?>">
<meta property="og:title" content="<?= e($C['meta']['title'] ?? '') ?>">
<meta property="og:description" content="<?= e(tr($C['meta']['description'] ?? '')) ?>">
<meta name="theme-color" content="#1d1c1a">
<link rel="icon" href="/assets/img/favicon-<?= e($SITE) ?>.svg" type="image/svg+xml">
<?php foreach (LANGS as $l): ?><link rel="alternate" hreflang="<?= $l ?>" href="?lang=<?= $l ?>"><?php endforeach; ?>
<link rel="preload" href="/assets/fonts/noto-sans-latin-400-normal.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/assets/fonts/noto-sans-latin-700-normal.woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="/assets/css/site.css?v=<?= filemtime(__DIR__ . '/assets/css/site.css') ?>">
</head>
<body>
<a class="skip" href="#main">Skip to content</a>

<div class="meta-bar" id="top">
  <div class="wrap meta-inner">
    <p class="meta-label">Circle Defense <span aria-hidden="true">×</span> AFTP</p>
    <details class="lang-menu">
      <summary aria-label="Langue / Sprache / Language"><?= strtoupper($LANG) ?><svg viewBox="0 0 12 8" width="11" height="8" aria-hidden="true"><path d="M1 1.5l5 5 5-5" fill="none" stroke="currentColor" stroke-width="1.4"/></svg></summary>
      <ul>
        <?php foreach (['fr' => 'Français', 'de' => 'Deutsch', 'en' => 'English'] as $l => $ln): ?>
        <li><a href="<?= e(lang_url($l)) ?>" hreflang="<?= $l ?>" lang="<?= $l ?>" class="<?= $l === $LANG ? 'on' : '' ?>"><?= $ln ?></a></li>
        <?php endforeach; ?>
      </ul>
    </details>
  </div>
</div>

<header class="top">
  <div class="wrap top-inner">
    <a href="<?= e(site_url($SITE)) ?>" class="home-link"><?= brand($SITE) ?></a>
    <nav class="nav" id="nav" aria-label="Main">
      <a href="<?= e($A) ?>#about"><?= t($SITE === 'cd' ? 'nav_solutions' : 'nav_objectives') ?></a>
      <?php if ($team): ?><a href="<?= e($A) ?>#team"><?= t($SITE === 'cd' ? 'nav_founders' : 'nav_committee') ?></a><?php endif; ?>
      <a href="<?= e($A) ?>#events"><?= t('nav_agenda') ?></a>
      <a href="<?= e($A) ?>#resources"><?= t('nav_resources') ?></a>
      <a href="<?= e($A) ?>#contact"><?= t('nav_contact') ?></a>
      <div class="nav-mobile-extra">
        <p class="eyebrow"><?= t('choose_site') ?></p>
        <?= site_switch($SITE, $names, 'switch big') ?>
      </div>
    </nav>
    <?= site_switch($SITE, $names, 'switch head') ?>
    <button class="burger" aria-controls="nav" aria-expanded="false"><span></span><span></span><span class="sr"><?= t('menu') ?></span></button>
  </div>
</header>

<main id="main">
<?php if ($PAGE): $LG = $C['legal'] ?? []; ?>
<section class="legal-page">
  <div class="wrap legal-wrap">
    <a class="back-link" href="<?= e(site_url($SITE)) ?>">← <?= t('back_home') ?></a>
    <p class="eyebrow"><?= e($names[$SITE]) ?></p>
    <h1><?= e(t($PAGE)) ?></h1>
    <div class="prose legal-text"><?= rich(tr($LG[$PAGE === 'legal' ? 'impressum' : 'privacy'] ?? '')) ?></div>
    <?php if (!empty($LG['updated'])): ?><p class="fine"><?= t('updated') ?> : <?= e($LG['updated']) ?></p><?php endif; ?>
    <p class="legal-switch"><a href="<?= e($page_url($PAGE === 'legal' ? 'privacy' : 'legal')) ?>"><?= t($PAGE === 'legal' ? 'privacy' : 'legal') ?> →</a></p>
  </div>
</section>
<?php else: ?>

<!-- HERO -->
<section class="hero">
  <div class="wrap hero-grid">
    <div class="hero-copy">
      <p class="kicker"><?= e(tr($C['hero']['kicker'] ?? '')) ?></p>
      <h1><?= e(tr($C['hero']['title'] ?? '')) ?></h1>
      <p class="lead"><?= e(tr($C['hero']['text'] ?? '')) ?></p>
      <div class="hero-cta">
        <a class="btn primary" href="#events"><?= t('nav_agenda') ?> <span class="arr" aria-hidden="true">→</span></a>
        <a class="btn" href="#about"><?= t($SITE === 'cd' ? 'nav_solutions' : 'nav_objectives') ?> <span class="arr" aria-hidden="true">→</span></a>
        <?php if ($bookLinks): ?><a class="btn book-btn" href="#book"><?= book_icon() ?><?= e(tr($C['book']['buy_label'] ?? '')) ?></a><?php endif; ?>
      </div>
    </div>
    <div class="hero-art">
      <?= site_mark($SITE, 'hero-mark') ?>
    </div>
  </div>
</section>

<!-- ABOUT / SOLUTIONS -->
<?php $rings = array_slice($C['rings'] ?? [], 0, 5); ?>
<section class="about" id="about">
  <div class="wrap about-grid">
    <div class="about-copy">
      <p class="eyebrow"><?= t($SITE === 'cd' ? 'nav_solutions' : 'nav_objectives') ?></p>
      <h2><?= e(tr($C['about']['title'] ?? '')) ?></h2>
      <div class="prose"><?= rich(tr($C['about']['text'] ?? '')) ?></div>
    </div>
    <div class="about-art" data-rings>
      <div class="rings-wrap"><?= ring_diagram($SITE, $rings) ?></div>
      <p class="ring-hint"><span class="hint-hover"><?= t('hover_hint') ?></span><span class="hint-tap"><?= t('tap_hint') ?></span></p>
      <?php if ($SITE === 'cd'): ?><p class="ring-caption"><?= t('five_circles') ?></p><?php endif; ?>
      <ol class="ring-index">
        <?php foreach ($rings as $i => $r): ?>
        <li><button type="button" class="ring-tab<?= $i === 0 ? ' on' : '' ?>" data-ring="<?= $i ?>" aria-pressed="<?= $i === 0 ? 'true' : 'false' ?>"><span class="ring-no"><?= $i + 1 ?></span><span><?= e(tr($r['label'] ?? '')) ?></span></button></li>
        <?php endforeach; ?>
      </ol>
      <div class="ring-panels">
        <?php foreach ($rings as $i => $r): ?>
        <article class="ring-panel<?= $i === 0 ? ' on' : '' ?>" data-ring="<?= $i ?>" <?= $i === 0 ? '' : 'hidden' ?>>
          <p class="ring-panel-no"><?= $SITE === 'cd' ? ($i + 1) . ($LANG === 'fr' ? ($i === 0 ? 'er' : 'e') : ($LANG === 'de' ? '.' : ['st','nd','rd','th','th'][$i])) . ' ' . t('circle_n') : '0' . ($i + 1) ?></p>
          <h3><?= e(tr($r['label'] ?? '')) ?></h3>
          <div class="prose"><?= rich(tr($r['text'] ?? '')) ?></div>
        </article>
        <?php endforeach; ?>
      </div>
    </div>
  </div>

  <?php if (!empty($C['concepts']['items']) || !empty($C['timeline']['items'])): ?>
  <div class="wrap method">
  <?php if (!empty($C['concepts']['items'])): ?>
  <div class="concepts">
    <h2 class="h-sm"><?= e(tr($C['concepts']['title'] ?? '')) ?></h2>
    <div class="concept-grid">
      <?php foreach ($C['concepts']['items'] as $i => $c): ?>
      <div class="concept">
        <span class="concept-no" aria-hidden="true"><?= $i + 1 ?></span>
        <h3><?= e(tr($c['title'] ?? '')) ?></h3>
        <div class="prose"><?= rich(tr($c['text'] ?? '')) ?></div>
      </div>
      <?php endforeach; ?>
    </div>
  </div>
  <?php endif; ?>

  <?php if (!empty($C['timeline']['items'])): ?>
  <div class="timeline">
    <h2 class="h-sm"><?= e(tr($C['timeline']['title'] ?? '')) ?></h2>
    <ol class="tl">
      <?php foreach ($C['timeline']['items'] as $i => $step): ?>
      <li class="tl-step">
        <span class="tl-dot" aria-hidden="true"></span>
        <h3><?= e(tr($step['label'] ?? '')) ?></h3>
        <div class="prose"><?= rich(tr($step['text'] ?? '')) ?></div>
      </li>
      <?php endforeach; ?>
    </ol>
  </div>
  <?php endif; ?>
  </div>
  <?php endif; ?>
</section>

<!-- PEOPLE -->
<?php if ($team): ?>
<section class="team" id="team">
  <div class="wrap">
    <div class="section-head">
      <p class="eyebrow"><?= t($SITE === 'cd' ? 'nav_founders' : 'nav_committee') ?></p>
      <h2><?= e(tr($C['team']['title'] ?? '')) ?></h2>
      <?php if (tr($C['team']['text'] ?? '')): ?><div class="prose"><?= rich(tr($C['team']['text'])) ?></div><?php endif; ?>
    </div>
    <div class="people<?= count($team) === 1 ? ' single' : '' ?>">
      <?php foreach ($team as $m):
        $initials = implode('', array_map(fn($w) => mb_substr($w, 0, 1), array_slice(preg_split('/\s+/', trim($m['name'])), 0, 2)));
        $bio = trim(str_replace("\r", '', tr($m['bio'] ?? '')));
        $paras = $bio === '' ? [] : preg_split("/\n\s*\n/", $bio);
        $lead = array_shift($paras) ?? '';
        $tl = array_filter($m['timeline'] ?? [], fn($x) => tr($x['what'] ?? '') !== '');
        $tags = array_filter(array_map('trim', explode(',', tr($m['tags'] ?? ''))));
        $hasMore = $paras || $tl; ?>
      <article class="person">
        <div class="person-head">
          <div class="portrait">
            <?php if (!empty($m['photo'])): ?>
              <img src="<?= e($m['photo']) ?>" alt="<?= e($m['name']) ?>" loading="lazy" width="360" height="360">
            <?php else: ?>
              <span class="initials" aria-hidden="true"><?= e($initials) ?></span>
            <?php endif; ?>
          </div>
          <div>
            <h3><?= e($m['name']) ?></h3>
            <p class="role"><?= e(tr($m['role'] ?? '')) ?></p>
          </div>
        </div>
        <?php if ($lead !== ''): ?><div class="lead-bio"><?= rich($lead) ?></div><?php endif; ?>
        <?php if ($tags): ?>
        <ul class="tags" aria-label="<?= t('expertise') ?>"><?php foreach ($tags as $tg): ?><li><?= e($tg) ?></li><?php endforeach; ?></ul>
        <?php endif; ?>
        <?php if ($hasMore): ?>
        <details class="more">
          <summary><span class="when-closed"><?= t('read_more') ?></span><span class="when-open"><?= t('read_less') ?></span></summary>
          <div class="more-body">
            <?php if ($paras): ?><div class="prose"><?= rich(implode("\n\n", $paras)) ?></div><?php endif; ?>
            <?php if ($tl): ?>
            <h4><?= t('career') ?></h4>
            <ol class="career">
              <?php foreach ($tl as $x): ?>
              <li><span class="career-when"><?= e($x['when'] ?? '') ?></span><span class="career-what"><?= e(tr($x['what'])) ?></span></li>
              <?php endforeach; ?>
            </ol>
            <?php endif; ?>
          </div>
        </details>
        <?php endif; ?>
      </article>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif; ?>

<!-- EVENTS -->
<section class="events" id="events">
  <div class="wrap">
    <div class="section-head split">
      <div>
        <p class="eyebrow"><?= t('nav_agenda') ?></p>
        <h2><?= e(tr($C['events']['title'] ?? '')) ?></h2>
        <?php if (tr($C['events']['text'] ?? '')): ?><p class="sub"><?= e(tr($C['events']['text'])) ?></p><?php endif; ?>
      </div>
      <div class="tabs" role="tablist">
        <button role="tab" class="tab on" aria-selected="true" data-tab="up"><?= t('upcoming') ?> <span class="count"><?= count($upcoming) ?></span></button>
        <button role="tab" class="tab" aria-selected="false" data-tab="past"><?= t('past') ?></button>
      </div>
    </div>
    <?php foreach (['up' => $upcoming, 'past' => $past] as $key => $list): ?>
    <div class="ev-list" data-panel="<?= $key ?>" <?= $key === 'past' ? 'hidden' : '' ?>>
      <?php if (!$list): ?>
        <p class="empty"><?= t($key === 'up' ? 'no_events' : 'no_past') ?></p>
      <?php endif; ?>
      <?php foreach ($list as $ev):
        $d = fmt_date($ev['date'], $LANG);
        $d2 = !empty($ev['end_date']) && $ev['end_date'] !== $ev['date'] ? fmt_date($ev['end_date'], $LANG) : null; ?>
      <?php $isNews = ($ev['kind'] ?? 'event') === 'news'; ?>
      <article class="ev<?= $isNews ? ' ev-news' : '' ?>">
        <div class="ev-date">
          <span class="ev-day"><?= e($d['day']) ?><?php if ($d2): ?><em>–<?= e($d2['day']) ?></em><?php endif; ?></span>
          <span class="ev-month"><?= e($d2 && $d2['month'] !== $d['month'] ? $d['month'] . ' – ' . $d2['month'] : $d['month']) ?></span>
          <span class="ev-year"><?= e($d['weekday']) ?> · <?= e($d['year']) ?></span>
        </div>
        <div class="ev-body">
          <h3><?= e(tr($ev['title'] ?? '')) ?></h3>
          <p class="ev-meta">
            <?php if (!empty($ev['time'])): ?><span><?= e($ev['time']) ?></span><?php endif; ?>
            <?php if (!empty($ev['location'])): ?><span><?= e($ev['location']) ?></span><?php endif; ?>
            <?php if ($isNews): ?><span class="kind"><?= t('news') ?></span><?php endif; ?>
            <span class="orgs"><?php foreach ($ev['sites'] as $s): ?><span class="org org-<?= e($s) ?>"><?= e($names[$s] ?? $s) ?></span><?php endforeach; ?></span>
          </p>
          <?php if (tr($ev['description'] ?? '')): ?><div class="ev-desc"><?= rich(tr($ev['description'])) ?></div><?php endif; ?>
        </div>
        <?php if (!empty($ev['image'])): ?><img class="ev-img" src="<?= e($ev['image']) ?>" alt="" loading="lazy"><?php endif; ?>
        <?php if ($key === 'up' && safe_url($ev['link'] ?? '')): ?>
          <a class="btn ev-cta" href="<?= e(safe_url($ev['link'])) ?>" target="_blank" rel="noopener"><?= t('register') ?> <span class="arr" aria-hidden="true">→</span></a>
        <?php endif; ?>
      </article>
      <?php endforeach; ?>
    </div>
    <?php endforeach; ?>
  </div>
</section>

<!-- PUBLICATIONS / RESOURCES -->
<?php $hasBook = tr($B['title'] ?? '') !== '' && $bookAll; $hasRes = !empty($resItems); $hasLib = (bool)$library; ?>
<?php if ($hasBook || $hasRes || $hasLib): ?>
<section class="resources" id="resources">
  <div class="wrap">
    <?php if ($hasBook): ?>
    <div class="book-feature" id="book">
      <?php $covers = array_values(array_filter(array_map(fn($l) => safe_url($l['cover'] ?? ''), $bookAll))); ?>
      <div class="book-art<?= count($covers) > 1 ? ' two' : '' ?>">
        <?php if ($covers): ?>
          <?php foreach (array_slice($covers, 0, 2) as $i => $cv): ?>
            <img class="cover cover-<?= $i ?>" src="<?= e($cv) ?>" alt="<?= $i === 0 ? e(tr($B['title'])) : '' ?>" width="384" height="544" loading="lazy">
          <?php endforeach; ?>
        <?php else: ?>
        <div class="book" aria-hidden="true">
          <div class="book-spine"></div>
          <div class="book-cover">
            <?= cd_mark('book-mark') ?>
            <span class="book-kicker"><?= e(tr($B['kicker'] ?? '')) ?></span>
            <span class="book-title"><?= e(tr($B['title'])) ?></span>
            <span class="book-authors"><?= e($B['authors'] ?? '') ?></span>
          </div>
        </div>
        <?php endif; ?>
      </div>
      <div class="book-copy">
        <p class="eyebrow"><?= e(tr($B['kicker'] ?? '')) ?></p>
        <h2><?= e(tr($B['title'])) ?></h2>
        <?php if (!empty($B['authors'])): ?><p class="book-by"><?= e($B['authors']) ?></p><?php endif; ?>
        <?php if (tr($B['text'] ?? '')): ?><div class="prose"><?= rich(tr($B['text'])) ?></div><?php endif; ?>
        <div class="book-buy">
          <?php foreach ($bookAll as $i => $l): $u = safe_url($l['url'] ?? ''); ?>
            <?php if ($u): ?>
              <a class="btn<?= $i === 0 ? ' primary' : '' ?>" href="<?= e($u) ?>" target="_blank" rel="noopener"><?= $i === 0 ? e(tr($B['buy_label'] ?? '')) . ' · ' : '' ?><?= e(tr($l['label'])) ?> <span class="arr" aria-hidden="true">↗</span></a>
            <?php else: ?>
              <span class="book-soon"><?= e(tr($l['label'])) ?></span>
            <?php endif; ?>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
    <?php endif; ?>

    <?php if ($hasLib): ?>
    <div class="library<?= $hasBook ? ' after-book' : '' ?>" id="library" data-library>
      <div class="section-head split">
        <div>
          <p class="eyebrow"><?= t('nav_resources') ?> · <?= t('lib_title') ?></p>
          <h2><?= e(tr($C['resources']['title'] ?? '') ?: t('lib_title')) ?></h2>
          <?php if (tr($C['resources']['text'] ?? '')): ?><div class="prose sub"><?= rich(tr($C['resources']['text'])) ?></div><?php endif; ?>
        </div>
        <p class="lib-count"><b data-lib-count><?= count(array_filter($library, fn($d) => $libDefault === 'all' || $d['lang'] === $libDefault)) ?></b> <?= t('lib_count') ?></p>
      </div>
      <div class="lib-filters">
        <?php if (count($libLangs) > 1): ?>
        <div class="chips" role="group" aria-label="<?= t('lib_lang') ?>">
          <?php foreach (array_merge($libLangs, ['all']) as $l): ?>
          <button type="button" class="chip<?= $l === $libDefault ? ' on' : '' ?>" data-f="lang" data-v="<?= $l ?>" aria-pressed="<?= $l === $libDefault ? 'true' : 'false' ?>"><?= $l === 'all' ? t('lib_all') : strtoupper($l) ?></button>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
        <?php if (count($libCats) > 1): ?>
        <div class="chips" role="group" aria-label="<?= t('lib_cat') ?>">
          <button type="button" class="chip on" data-f="cat" data-v="all" aria-pressed="true"><?= t('lib_all') ?></button>
          <?php foreach ($libCats as $c): ?>
          <button type="button" class="chip" data-f="cat" data-v="<?= $c ?>" aria-pressed="false"><?= t('cat_' . $c) ?></button>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </div>
      <ul class="lib-grid">
        <?php foreach ($library as $d): $off = $libDefault !== 'all' && $d['lang'] !== $libDefault; ?>
        <li class="doc<?= $off ? ' off' : '' ?>" data-lang="<?= e($d['lang']) ?>" data-cat="<?= e($d['cat']) ?>">
          <p class="doc-meta"><span class="doc-cat"><?= t('cat_' . $d['cat']) ?></span><span class="doc-lang" title="<?= t('lang_' . $d['lang']) ?>"><?= strtoupper(e($d['lang'])) ?></span></p>
          <h3 class="doc-title"><?= e($d['title']) ?></h3>
          <?php if (!empty($d['credit'])): ?><p class="doc-credit"><?= t('lib_source') ?> : <?= e($d['credit']) ?></p><?php endif; ?>
          <div class="doc-files">
            <?php foreach ($d['files'] as $f): ?>
            <a class="doc-file" href="<?= e($f['url']) ?>" target="_blank" rel="noopener" type="application/pdf" aria-label="<?= t('lib_dl') ?> <?= e($d['title']) ?> (<?= $f['format'] === 'letter' ? t('lib_letter') : 'A4' ?>)">
              <svg viewBox="0 0 16 16" width="15" height="15" aria-hidden="true"><path d="M8 2v8m0 0l-3.2-3.2M8 10l3.2-3.2M3 13h10" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
              <span><?= $f['format'] === 'letter' ? t('lib_letter') : 'A4' ?></span><small>PDF · <?= fmt_bytes((int)($f['bytes'] ?? 0), $LANG) ?></small>
            </a>
            <?php endforeach; ?>
          </div>
        </li>
        <?php endforeach; ?>
      </ul>
      <p class="lib-empty" hidden><?= t('lib_empty') ?></p>
    </div>
    <?php endif; ?>

    <?php if ($hasRes): ?>
    <div class="res-grid<?= $hasBook || $hasLib ? ' after-book' : '' ?>">
      <div class="section-head">
        <p class="eyebrow"><?= t('nav_resources') ?></p>
        <h2><?= e(tr($C['resources'][$hasLib ? 'links_title' : 'title'] ?? '') ?: tr($C['resources']['title'] ?? '')) ?></h2>
        <?php if (!$hasLib && tr($C['resources']['text'] ?? '')): ?><div class="prose"><?= rich(tr($C['resources']['text'])) ?></div><?php endif; ?>
      </div>
      <ul class="res-list">
        <?php foreach ($resItems as $it): $u = safe_url($it['url'] ?? ''); $ext = $u && !str_starts_with($u, '#') && !str_starts_with($u, '/'); ?>
        <li><?php if ($u): ?><a href="<?= e($u) ?>"<?= $ext ? ' target="_blank" rel="noopener"' : '' ?>><?php else: ?><span class="res-off"><?php endif; ?>
          <span class="res-label"><?= e(tr($it['label'] ?? '')) ?></span>
          <span class="res-note"><?= e(tr($it['note'] ?? '')) ?></span>
          <span class="res-arrow" aria-hidden="true"><?= $u ? ($ext ? '↗' : '→') : '·' ?></span>
        <?= $u ? '</a>' : '</span>' ?></li>
        <?php endforeach; ?>
      </ul>
    </div>
    <?php endif; ?>
  </div>
</section>
<?php endif; ?>

<!-- PARTNER -->
<section class="partner">
  <a class="wrap partner-inner" href="<?= e(site_url($OTHER)) ?>">
    <?= site_mark($OTHER, 'partner-mark') ?>
    <span class="partner-copy">
      <span class="eyebrow"><?= t('partner_label') ?></span>
      <span class="partner-name"><?= e($P['meta']['title'] ?? $names[$OTHER]) ?></span>
      <span class="partner-text"><?= e(tr($C['partner']['text'] ?? '')) ?></span>
    </span>
    <span class="partner-go btn"><?= t('visit') ?> <span class="arr" aria-hidden="true">→</span></span>
  </a>
</section>

<!-- NEWSLETTER -->
<section class="newsletter" id="newsletter">
  <div class="wrap nl-grid">
    <div>
      <p class="eyebrow"><?= t('nav_newsletter') ?></p>
      <h2><?= e(tr($C['newsletter']['title'] ?? '')) ?></h2>
      <p class="sub"><?= e(tr($C['newsletter']['text'] ?? '')) ?></p>
    </div>
    <form class="nl-form js-form" action="/form.php" method="post" novalidate>
      <input type="hidden" name="action" value="newsletter">
      <input type="hidden" name="site" value="<?= e($SITE) ?>">
      <input type="hidden" name="lang" value="<?= e($LANG) ?>">
      <input type="hidden" name="ts" value="<?= $ts ?>">
      <div class="hp" aria-hidden="true"><label>Website <input type="text" name="website" tabindex="-1" autocomplete="off"></label></div>
      <fieldset class="nl-lists">
        <legend><?= t('nl_choose') ?></legend>
        <?php foreach ([$SITE, $OTHER] as $s): ?>
        <label class="check">
          <input type="checkbox" name="lists[]" value="<?= $s ?>" <?= $s === $SITE ? 'checked' : '' ?>>
          <span class="check-box"><?= site_mark($s, 'check-mark') ?></span>
          <span><?= e($names[$s]) ?></span>
        </label>
        <?php endforeach; ?>
      </fieldset>
      <div class="nl-row">
        <label class="sr" for="nl-email"><?= t('email') ?></label>
        <input id="nl-email" type="email" name="email" required placeholder="<?= t('email') ?>" autocomplete="email">
        <button class="btn primary" type="submit"><?= t('subscribe') ?> <span class="arr" aria-hidden="true">→</span></button>
      </div>
      <p class="fine"><?= t('nl_privacy') ?></p>
      <p class="form-msg" role="status" aria-live="polite"></p>
    </form>
  </div>
</section>

<!-- CONTACT -->
<section class="contact" id="contact">
  <div class="wrap contact-grid">
    <div class="contact-aside">
      <p class="eyebrow"><?= t('nav_contact') ?></p>
      <h2><?= e(tr($C['contact']['title'] ?? '')) ?></h2>
      <div class="prose"><?= rich(tr($C['contact']['text'] ?? '')) ?></div>
      <address>
        <?= nl2br(e(tr($C['contact']['address'] ?? ''))) ?><br>
        <?php if (!empty($C['contact']['email'])): ?>
        <span class="muted"><?= t('c_or') ?> :</span> <a href="mailto:<?= e($C['contact']['email']) ?>"><?= e($C['contact']['email']) ?></a>
        <?php endif; ?>
      </address>
    </div>
    <form class="contact-form js-form" action="/form.php" method="post" novalidate>
      <input type="hidden" name="action" value="contact">
      <input type="hidden" name="site" value="<?= e($SITE) ?>">
      <input type="hidden" name="lang" value="<?= e($LANG) ?>">
      <input type="hidden" name="ts" value="<?= $ts ?>">
      <div class="hp" aria-hidden="true"><label>Website <input type="text" name="website" tabindex="-1" autocomplete="off"></label></div>
      <div class="field-row">
        <label class="field"><span><?= t('c_name') ?></span><input type="text" name="name" required maxlength="120" autocomplete="name"></label>
        <label class="field"><span><?= t('c_email') ?></span><input type="email" name="email" required autocomplete="email"></label>
      </div>
      <div class="field-row">
        <label class="field"><span><?= t('c_to') ?></span>
          <select name="to">
            <?php foreach ([$SITE, $OTHER] as $s): ?><option value="<?= $s ?>"><?= e($names[$s]) ?></option><?php endforeach; ?>
          </select>
        </label>
        <label class="field"><span><?= t('c_subject') ?></span><input type="text" name="subject" required maxlength="160"></label>
      </div>
      <label class="field"><span><?= t('c_message') ?></span><textarea name="message" rows="6" required maxlength="5000"></textarea></label>
      <div class="form-foot">
        <p class="form-msg" role="status" aria-live="polite"></p>
        <button class="btn primary" type="submit"><?= t('c_send') ?> <span class="arr" aria-hidden="true">→</span></button>
      </div>
    </form>
  </div>
</section>

<?php endif; ?>
</main>

<?php $stk = tr($C['newsletter']['sticker'] ?? ''); if ($stk !== '' && !$PAGE): ?>
<div class="nl-sticker-wrap" id="nl-sticker">
  <a class="nl-sticker" href="#newsletter" aria-label="<?= e(t('nav_newsletter') . ' — ' . $stk) ?>">
    <span class="nl-sticker-in">
      <svg viewBox="0 0 24 24" width="22" height="22" aria-hidden="true"><path d="M3 6.5h18v11H3z M3 7l9 6.5L21 7" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/></svg>
      <b><?= t('nav_newsletter') ?></b>
      <span><?= e($stk) ?></span>
      <i aria-hidden="true">→</i>
    </span>
  </a>
  <button type="button" class="nl-sticker-x" aria-label="<?= t('close') ?>">×</button>
</div>
<?php endif; ?>

<footer class="foot">
  <div class="wrap foot-inner">
    <div class="foot-brands">
      <a href="<?= e(site_url('cd')) ?>"><?= brand('cd', 'small') ?></a>
      <span class="x">×</span>
      <a href="<?= e(site_url('aftp')) ?>"><?= brand('aftp', 'small') ?></a>
    </div>
    <?php $L = $C['links'] ?? []; $partners = array_filter($L['partners'] ?? [], fn($p) => !empty($p['visible']) && trim($p['name'] ?? '') !== ''); ?>
    <?php if ($partners || !empty($L['instagram'])): ?>
    <div class="foot-links">
      <?php if ($partners): ?>
      <div><p class="eyebrow"><?= e(tr($L['partners_title'] ?? '')) ?></p>
        <ul><?php foreach ($partners as $pt): $pu = safe_url($pt['url'] ?? ''); ?><li><?php if ($pu): ?><a href="<?= e($pu) ?>" target="_blank" rel="noopener"><?= e($pt['name']) ?> ↗</a><?php else: ?><?= e($pt['name']) ?><?php endif; ?></li><?php endforeach; ?></ul>
      </div>
      <?php endif; ?>
      <?php if (safe_url($L['instagram'] ?? '')): ?>
      <div><p class="eyebrow"><?= t('follow') ?></p>
        <a class="insta" href="<?= e(safe_url($L['instagram'])) ?>" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="5" fill="none" stroke="currentColor" stroke-width="1.6"/><circle cx="12" cy="12" r="4.2" fill="none" stroke="currentColor" stroke-width="1.6"/><circle cx="17.3" cy="6.7" r="1.1" fill="currentColor"/></svg> Instagram</a>
      </div>
      <?php endif; ?>
    </div>
    <?php endif; ?>
    <p class="foot-meta">© <?= date('Y') ?> <?= e($C['meta']['title'] ?? '') ?> · <a href="mailto:<?= e($C['contact']['email'] ?? '') ?>"><?= e($C['contact']['email'] ?? '') ?></a> · <a href="<?= e($page_url('legal')) ?>"><?= t('legal') ?></a> · <a href="<?= e($page_url('privacy')) ?>"><?= t('privacy') ?></a> · <a href="#top"><?= t('back_top') ?> ↑</a></p>
  </div>
</footer>

<?php if ($flash): ?>
<script>window.__flash = <?= json_encode($flash, JSON_HEX_TAG | JSON_HEX_AMP) ?>;</script>
<?php endif; ?>
<script src="/assets/js/site.js?v=<?= filemtime(__DIR__ . '/assets/js/site.js') ?>" defer></script>
</body>
</html>
