/* ==========================================================================
   AP3 — shared mobile + menu layer.
   Loaded after each page's inline <style>, so it refines rather than restyles.
   ========================================================================== */

:root {
  --ease-out-soft: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-icon: cubic-bezier(0.2, 0, 0, 1);
}

html { -webkit-text-size-adjust: 100%; }
html.menu-open, html.menu-open body { overflow: hidden; }

a, button, label, summary { -webkit-tap-highlight-color: transparent; }

/* ---------- Safe areas (viewport-fit=cover) ---------- */
header { padding-top: env(safe-area-inset-top); }
header nav.wrap,
footer .wrap {
  padding-left: max(var(--gutter, 32px), env(safe-area-inset-left));
  padding-right: max(var(--gutter, 32px), env(safe-area-inset-right));
}
footer { padding-bottom: calc(56px + env(safe-area-inset-bottom)); }

/* ---------- Menu sheet ---------- */
/* The sheet itself only fades: a transform here would become the containing block
   for the fixed close button and drag it off the hamburger's position mid-transition.
   Visibility flips instantly on open so focus can land on the close button. */
.site-menu,
.site-menu.is-open { transform: none; }
.site-menu.is-open { transition: opacity 300ms var(--ease-out-soft), visibility 0s; }
.site-menu {
  overscroll-behavior: contain;
  padding-top: calc(96px + env(safe-area-inset-top));
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
  /* "Safe" centering: auto margins center when there's room, but on a short
     screen (landscape phone) the first link stays reachable instead of being
     pushed above the scroll origin as justify-content: center does. */
  justify-content: flex-start;
}
.site-menu > .site-menu-links { margin-top: auto; }
.site-menu > .site-menu-foot { margin-bottom: auto; }
.site-menu-links,
.site-menu-foot {
  padding-left: max(var(--gutter, 32px), env(safe-area-inset-left));
  padding-right: max(var(--gutter, 32px), env(safe-area-inset-right));
}

/* Links rise in, one after another — only on open; close is a single soft fade */
.site-menu-links a,
.menu-cta,
.site-menu-foot {
  opacity: 0;
  transform: translateY(10px);
  transition:
    opacity 420ms var(--ease-out-soft),
    transform 420ms var(--ease-out-soft),
    color 200ms ease,
    padding-left 250ms var(--ease-out-soft);
  transition-delay: calc(60ms + var(--i, 0) * 35ms);
}
.site-menu-foot { --i: 9; }
.site-menu.is-open .site-menu-links a,
.site-menu.is-open .menu-cta,
.site-menu.is-open .site-menu-foot { opacity: 1; transform: none; }
.site-menu:not(.is-open) .site-menu-links a,
.site-menu:not(.is-open) .menu-cta,
.site-menu:not(.is-open) .site-menu-foot { transition-delay: 0ms; transition-duration: 150ms; }

.site-menu-links a.current { color: var(--accent); }
.site-menu-links a.current::after {
  content: ''; display: inline-block; width: 7px; height: 7px; margin-left: 12px;
  border-radius: 50%; background: var(--accent); vertical-align: 0.3em;
}
.site-menu-links a:active { color: var(--accent); }

/* ---------- Close (X) — glass disc that sits exactly where the hamburger was ---------- */
.site-menu .menu-close {
  position: fixed;
  top: var(--close-top, calc(14px + env(safe-area-inset-top)));
  right: var(--close-right, max(var(--gutter, 32px), env(safe-area-inset-right)));
  left: auto;
  inset-inline-end: var(--close-right, max(var(--gutter, 32px), env(safe-area-inset-right)));
  z-index: 41;
  display: grid; place-items: center;
  width: 44px; height: 44px; min-height: 0; padding: 0; gap: 0;
  border-radius: 999px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(18px) saturate(1.4);
  -webkit-backdrop-filter: blur(18px) saturate(1.4);
  border: 0;
  box-shadow:
    0 0 0 1px rgba(20, 22, 26, 0.08),
    0 1px 2px rgba(20, 22, 26, 0.04),
    0 4px 18px rgba(20, 22, 26, 0.07);
  cursor: pointer;
  transition-property: background-color, box-shadow, scale;
  transition-duration: 150ms;
  transition-timing-function: var(--ease-icon);
}
.site-menu .menu-close svg {
  display: block;
  stroke: currentColor; stroke-width: 1.5; stroke-linecap: round;
  opacity: 0; scale: 0.25; rotate: -90deg; filter: blur(4px);
  transition: opacity 300ms var(--ease-icon), scale 300ms var(--ease-icon), rotate 300ms var(--ease-icon), filter 300ms var(--ease-icon);
}
.site-menu.is-open .menu-close svg { opacity: 1; scale: 1; rotate: 0deg; filter: blur(0); transition-delay: 40ms; }
.site-menu .menu-close:active { scale: 0.96; transition-duration: 100ms; }
@media (hover: hover) {
  .site-menu .menu-close:hover {
    background: #fff;
    box-shadow: 0 0 0 1px rgba(20, 22, 26, 0.14), 0 1px 2px rgba(20, 22, 26, 0.05), 0 8px 24px rgba(20, 22, 26, 0.1);
  }
}

/* ---------- In-menu CTA (header CTA is hidden on phones) ---------- */
.menu-cta { display: none; }
@media (max-width: 640px) {
  .menu-cta {
    display: flex; align-items: center; justify-content: space-between;
    min-height: 54px; padding: 0 24px;
    margin: 28px max(var(--gutter, 32px), env(safe-area-inset-right)) 0 max(var(--gutter, 32px), env(safe-area-inset-left));
    font-family: var(--mono); font-size: 14px; letter-spacing: 0.02em;
    color: var(--bg); background: var(--ink); border-radius: 999px;
    box-shadow: 0 1px 2px rgba(20, 22, 26, 0.08), 0 8px 24px -8px rgba(20, 22, 26, 0.35);
    transition-property: opacity, transform, background-color, scale;
  }
  .menu-cta:hover { text-decoration: none; }
  .menu-cta:active { background: var(--accent); }
  .site-menu-foot { margin-top: 28px; }
  .site-menu-foot a { padding-block: 10px; }
}

/* ---------- Phone layout ---------- */
@media (max-width: 640px) {
  :root { --gutter: 20px; }
  .wrap {
    padding-left: max(20px, env(safe-area-inset-left));
    padding-right: max(20px, env(safe-area-inset-right));
  }
  .site-menu-links a { padding-block: 12px; }

  /* Primary form actions go full-width and thumb-sized */
  .form-submit-row { flex-direction: column; align-items: stretch; gap: 12px; }
  .form-submit-row .btn-primary,
  .form-submit-row .btn-secondary { width: 100%; text-align: center; padding-block: 16px; }
  .form-status { text-align: center; }
  .newsletter-form { flex-direction: column; align-items: stretch; }
  .newsletter-form input[type="email"] { min-width: 0; width: 100%; }
  .newsletter-form .btn-primary { width: 100%; padding-block: 15px; }
  .cta-actions { flex-direction: column; align-items: stretch; }
  .cta-actions > a, .cta-actions > button { text-align: center; }

  /* Footer links become comfortable tap rows */
  .footer-links { column-gap: 22px; row-gap: 0; }
  .footer-links a { display: inline-block; padding-block: 11px; }
}

/* ---------- Touch devices ---------- */
@media (pointer: coarse) {
  /* Below 16px, iOS Safari zooms the page on focus */
  body :is(input:not([type="range"]):not([type="radio"]):not([type="checkbox"]):not([type="hidden"]), select, textarea) {
    font-size: 16px;
  }
  .score-row input[type="range"] { height: 32px; }
  .scale span { height: 44px; }
  .carousel-dot { padding: 11px; }
  .carousel { touch-action: pan-y; }
}

/* Hover lifts stick after a tap on touch screens — neutralize them, and give presses instant feedback instead */
@media (hover: none) {
  .nav-cta:hover, .btn-primary:hover, .btn-secondary:hover, .btn-glass:hover,
  .carousel-nav:hover, .carousel-slide:hover .carousel-card, .carousel-slide:hover img,
  .factor-card:hover, .framework-row:hover, .hero-card:hover, .methodology-card:hover,
  .methodology-link:hover, .output-card:hover, .principle-quote:hover, .principle:hover,
  .tag:hover, .thesis-box:hover, .workflow-card:hover, .tilt-card:hover .card-cover img,
  .scale label:hover span, .score-row input[type="range"]:hover {
    transform: none;
    box-shadow: none;
  }
  .btn-glass:hover .carousel-arrow, .carousel-slide:hover .carousel-arrow { transform: none; }
  .site-menu-links a:hover { padding-left: 0; color: var(--ink); }
  .site-menu-links a.current:hover { color: var(--accent); }
  .scale label:hover span { color: var(--ink-faint); border-radius: 0; }
  .scale label:hover input:checked + span { color: var(--bg); }

  .nav-cta, .btn-primary, .btn-secondary, .btn-glass, .carousel-nav, .menu-cta, .tag {
    transition-property: background, background-color, border-color, color, box-shadow, scale;
  }
  .nav-cta:active, .btn-primary:active, .btn-secondary:active, .btn-glass:active,
  .carousel-nav:active, .menu-cta:active, .tag:active { scale: 0.96; transition-duration: 100ms; }
}

/* ---------- Ambient hero backgrounds (assets/hero-motion.js) ---------- */
.page-hero[data-hero-motion] { position: relative; overflow: hidden; isolation: isolate; }
.hero-motion {
  position: absolute; inset: 0; z-index: -1;
  width: 100%; height: 100%; display: block; pointer-events: none;
  opacity: 0; transition: opacity 900ms cubic-bezier(0.23, 1, 0.32, 1);
}
.hero-motion.is-ready { opacity: 1; }
/* Scrim sits above the canvas and below the copy, so text never fights the motion */
.page-hero[data-hero-motion]::after {
  content: ''; position: absolute; inset: 0; z-index: -1; pointer-events: none;
  background: var(--hero-scrim, none);
}

/* Hero copy settles in once, on load */
@keyframes hero-rise { from { opacity: 0; transform: translateY(14px); } }
@keyframes hero-fade { from { opacity: 0; } }
.hero-intro > * { animation: hero-rise 700ms cubic-bezier(0.23, 1, 0.32, 1) backwards; }
.hero-intro > :nth-child(2) { animation-delay: 70ms; }
.hero-intro > :nth-child(3) { animation-delay: 140ms; }
.hero-intro > :nth-child(4) { animation-delay: 210ms; }
.hero-intro > :nth-child(5) { animation-delay: 280ms; }

/* ---------- Header over dark heroes ---------- */
header { transition: background-color 250ms ease, border-color 250ms ease; }
header .logo img { transition: filter 250ms ease; }
header .menu-toggle span { transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease, background-color 250ms ease; }
header.on-dark {
  background: var(--header-dark-bg, rgba(20, 22, 26, 0.45));
  border-bottom-color: rgba(255, 255, 255, 0.1);
}
header.on-dark .logo img { filter: invert(1); }
header.on-dark .menu-toggle span { background: #FAFAF8; }
header.on-dark .nav-cta { background: #FAFAF8; color: var(--ink); }
header.on-dark .nav-cta:hover { background: var(--accent-soft); }

@media print { .hero-motion { display: none; } }

/* ---------- Preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  .hero-intro > * { animation-name: hero-fade; }
  .site-menu { transform: none !important; }
  .site-menu-links a, .menu-cta, .site-menu-foot { transform: none; transition-delay: 0ms; }
  .site-menu .menu-close svg { rotate: 0deg; scale: 1; filter: none; }
  .site-menu .menu-close:active, .nav-cta:active, .btn-primary:active, .btn-secondary:active,
  .btn-glass:active, .carousel-nav:active, .menu-cta:active, .tag:active { scale: 1; }
}
@media (prefers-reduced-transparency: reduce) {
  header { background: var(--bg); backdrop-filter: none; -webkit-backdrop-filter: none; }
  header.on-dark { background: var(--ink); }
  .site-menu .menu-close { background: var(--bg-raised); backdrop-filter: none; -webkit-backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  .site-menu .menu-close { background: var(--bg); box-shadow: 0 0 0 1px var(--ink); }
}
