/* ============================================================================
   base.css — reset, typography, layout primitives, global chrome
   "Living product, editorial soul": warm paper, serif display, purposeful motion.
   Mobile-first; everything scales with clamp().
   ========================================================================== */

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* smooth in-page scrolling is JS-driven (eased, cancelable) — see initSmoothScroll();
     no CSS scroll-behavior so the per-frame scrollTo isn't double-animated. */
  scroll-padding-top: calc(var(--nav-h) + 1rem);
}
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-prose);
  font-size: var(--fs-base);
  line-height: var(--lh-prose);
  font-weight: 400;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-optical-sizing: none;   /* Fraunces: use the calmer text cut at all sizes (the display cut reads wonky) */
  overflow-x: hidden;
  transition: background var(--t) var(--ease), color var(--t) var(--ease);
}
img, svg, video, canvas { display: block; max-width: 100%; height: auto; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: none; cursor: pointer; }
a { color: inherit; text-decoration: none; }
ul, ol { list-style: none; padding: 0; }
::selection { background: var(--selection); }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 2px;
}
:focus:not(:focus-visible) { outline: none; }

/* ---------- A11y helpers ---------- */
.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}
.skip-link {
  position: fixed; left: 50%; top: -64px; transform: translateX(-50%);
  background: var(--accent); color: var(--on-accent);
  padding: 0.6rem 1.1rem; border-radius: var(--r); z-index: 1000;
  font-family: var(--font-sans); font-size: var(--fs-sm); font-weight: 600;
  transition: top var(--t) var(--ease);
}
.skip-link:focus { top: 0.75rem; }

/* ---------- Layout primitives ---------- */
.wrap { width: 100%; max-width: var(--maxw); margin-inline: auto; padding-inline: var(--gutter); }
.wrap--wide { max-width: var(--maxw-wide); }
.section { padding-block: clamp(3.5rem, 9vw, 7rem); }
.cluster { display: flex; flex-wrap: wrap; gap: var(--cluster-gap, 0.6rem); align-items: center; }

/* ---------- Scroll progress (top hairline) ---------- */
.scroll-progress {
  position: fixed; inset: 0 0 auto 0; height: 2px; z-index: 300;
  background: transparent; pointer-events: none;
}
.scroll-progress__bar {
  display: block; height: 100%; width: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-strong));
  transform: scaleX(0); transform-origin: left; transition: transform 90ms linear; will-change: transform;
}

/* ---------- Typography ---------- */
.display {
  font-family: var(--font-display);
  font-weight: var(--w-display);
  line-height: var(--lh-tight);
  letter-spacing: -0.025em;
  font-optical-sizing: none;
}
h1, h2, h3 { font-family: var(--font-display); font-weight: var(--w-heading); line-height: var(--lh-snug); letter-spacing: -0.025em; }
h2 { font-size: var(--fs-2xl); }
h3 { font-size: var(--fs-xl); }

.kicker {
  font-family: var(--font-mono);
  font-size: var(--fs-2xs);
  font-weight: 500;
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--text-mute);
  display: inline-flex; align-items: center; gap: 0.55em;
}
.kicker--center { justify-content: center; }
.kicker__dot { width: 7px; height: 7px; border-radius: var(--r-pill); background: var(--proj, var(--accent)); flex: none; }
.kicker__dot--live { background: var(--live); box-shadow: 0 0 0 0 color-mix(in srgb, var(--live) 70%, transparent); animation: live-ping 2.6s var(--ease) infinite; }
@keyframes live-ping {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--live) 55%, transparent); }
  70%, 100% { box-shadow: 0 0 0 7px transparent; }
}
.kicker--accent { color: var(--proj-ink, var(--accent)); }

.prose { color: var(--text-dim); max-width: var(--prose); text-wrap: pretty; }
.prose p + p { margin-top: 1rem; }
.prose strong { color: var(--text); font-weight: 600; }
.prose a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; text-decoration-color: var(--border-strong); transition: text-decoration-color var(--t); }
.prose a:hover { text-decoration-color: var(--accent); }

.num, .mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.count { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }

/* ---------- Top nav ---------- */
.site-nav {
  position: sticky; top: 0; z-index: 100;
  height: var(--nav-h);
  display: flex; align-items: center;
  background: var(--glass);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid transparent;
  transition: border-color var(--t) var(--ease), background var(--t) var(--ease);
}
.site-nav.is-scrolled { border-bottom-color: var(--border); }
.site-nav__inner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; width: 100%; }
.brand {
  font-family: var(--font-sans); font-weight: 700;
  font-size: var(--fs-sm); letter-spacing: 0.16em; text-transform: uppercase;
  display: inline-flex; align-items: center; gap: 0.5em; white-space: nowrap;
}
.brand__mark { width: 9px; height: 9px; border-radius: 2px; background: var(--accent); transform: rotate(45deg); transition: transform var(--t-slow) var(--ease-spring), background var(--t); }
.brand:hover .brand__mark { transform: rotate(135deg); }
.nav-links { display: flex; align-items: center; gap: clamp(0.7rem, 2.4vw, 1.6rem); }
.nav-links a {
  font-family: var(--font-sans); font-size: var(--fs-sm); font-weight: 500;
  color: var(--text-dim); position: relative; padding-block: 0.4rem;
  transition: color var(--t);
}
.nav-links > a::after {
  content: ""; position: absolute; left: 0; bottom: 0; width: 100%;
  height: 1px; background: var(--accent); transform: scaleX(0); transform-origin: left;
  transition: transform var(--t) var(--ease);
}
.nav-links > a:hover { color: var(--text); }
.nav-links > a:hover::after, .nav-links > a[aria-current="page"]::after { transform: scaleX(1); }
@media (max-width: 519px) { .nav-links > a { display: none; } }

/* ⌘K command trigger (pill) */
.cmdk-trigger {
  display: inline-flex; align-items: center; gap: 0.5em;
  height: 34px; padding: 0 0.6rem; border-radius: var(--r-pill);
  border: 1px solid var(--border); background: var(--surface); color: var(--text-mute);
  transition: color var(--t), border-color var(--t), background var(--t);
}
.cmdk-trigger:hover { color: var(--text); border-color: var(--border-strong); }
.cmdk-trigger svg { width: 15px; height: 15px; }
.cmdk-trigger__hint { display: inline-flex; gap: 2px; }
@media (max-width: 660px) { .cmdk-trigger__hint { display: none; } .cmdk-trigger { padding: 0; width: 34px; justify-content: center; } }

kbd {
  font-family: var(--font-mono); font-size: 0.7em; line-height: 1;
  min-width: 1.4em; height: 1.5em; padding: 0 0.35em;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border-strong); border-bottom-width: 2px;
  border-radius: 4px; background: var(--surface-2); color: var(--text-dim);
}

/* Theme toggle */
.theme-toggle {
  width: 34px; height: 34px; border-radius: var(--r-pill);
  display: grid; place-items: center; color: var(--text-mute);
  border: 1px solid var(--border); background: var(--surface);
  transition: color var(--t), border-color var(--t), transform var(--t-fast), background var(--t);
}
.theme-toggle:hover { color: var(--text); border-color: var(--border-strong); }
.theme-toggle:active { transform: scale(0.92); }
.theme-toggle svg { width: 16px; height: 16px; }
/* dark themes (Forest/Indigo) show the moon; Paper flips to the sun. The head script stamps
   data-theme pre-paint (saved pref, else Paper), so the attribute is always present. */
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
[data-theme="paper"] .theme-toggle .icon-sun { display: block; }
[data-theme="paper"] .theme-toggle .icon-moon { display: none; }

/* Theme picker popover (built by JS; wraps the toggle) */
.theme-menu { position: relative; display: inline-flex; }
.theme-pop {
  position: absolute; top: calc(100% + 10px); right: 0; z-index: 200;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-3); padding: 6px; min-width: 184px;
  display: flex; flex-direction: column; gap: 2px;
  animation: pop-in var(--t-fast) var(--ease-out);
}
.theme-pop[hidden] { display: none; }
@keyframes pop-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
.theme-opt {
  display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
  padding: 8px 10px; border-radius: var(--r); font-family: var(--font-sans);
  font-size: var(--fs-sm); color: var(--text-dim); transition: background var(--t-fast), color var(--t-fast);
}
.theme-opt:hover { background: var(--surface-2); color: var(--text); }
.theme-opt[aria-pressed="true"] { color: var(--text); font-weight: 600; }
.theme-opt .sw { width: 18px; height: 18px; border-radius: 50%; border: 1px solid rgba(127,127,127,0.4); flex: none; }
.theme-opt .check { margin-left: auto; opacity: 0; color: var(--accent); font-size: var(--fs-sm); }
.theme-opt[aria-pressed="true"] .check { opacity: 1; }
.sw-forest { background: linear-gradient(135deg, #0f1814 50%, #e0bc8c 50%); }
.sw-paper  { background: linear-gradient(135deg, #fbfaf7 50%, #1f6b49 50%); }
.sw-indigo { background: linear-gradient(135deg, #12152e 50%, #a6a2f7 50%); }

/* ---------- Command palette (⌘K) ---------- */
.cmdk {
  position: fixed; inset: 0; z-index: 500;
  display: grid; grid-template-rows: 1fr; justify-items: center; align-items: start;
  padding: clamp(3rem, 12vh, 8rem) 1rem 2rem;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  animation: cmdk-fade var(--t-fast) var(--ease-out);
}
.cmdk[hidden] { display: none; }
@keyframes cmdk-fade { from { opacity: 0; } to { opacity: 1; } }
.cmdk__panel {
  width: min(580px, 100%);
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--r-xl); box-shadow: var(--shadow-3);
  display: flex; flex-direction: column; overflow: hidden;
  max-height: min(64vh, 560px);
  animation: cmdk-rise var(--t) var(--ease-out);
}
@keyframes cmdk-rise { from { opacity: 0; transform: translateY(10px) scale(0.985); } to { opacity: 1; transform: none; } }
.cmdk__field { display: flex; align-items: center; gap: 0.7rem; padding: 0.95rem 1.1rem; border-bottom: 1px solid var(--border); }
.cmdk__field svg { width: 18px; height: 18px; color: var(--text-mute); flex: none; }
.cmdk__input { flex: 1; min-width: 0; border: none; background: none; outline: none; font-family: var(--font-sans); font-size: var(--fs-lg); color: var(--text); }
.cmdk__input::placeholder { color: var(--text-mute); }
.cmdk__esc { font-family: var(--font-mono); font-size: var(--fs-2xs); color: var(--text-mute); border: 1px solid var(--border); border-radius: 4px; padding: 2px 6px; }
.cmdk__list { overflow-y: auto; padding: 6px; overscroll-behavior: contain; }
.cmdk__group { font-family: var(--font-mono); font-size: var(--fs-2xs); letter-spacing: var(--tracking-label); text-transform: uppercase; color: var(--text-mute); padding: 0.65rem 0.7rem 0.3rem; }
.cmdk__group:first-child { padding-top: 0.35rem; }
.cmdk__item {
  display: flex; align-items: center; gap: 0.7rem; width: 100%; text-align: left;
  padding: 0.5rem 0.7rem; border-radius: var(--r); color: var(--text-dim);
  scroll-margin: 8px;
}
.cmdk__item[aria-selected="true"] { background: var(--surface-2); color: var(--text); }
.cmdk__item-ic { width: 30px; height: 30px; flex: none; display: grid; place-items: center; border: 1px solid var(--border); border-radius: var(--r); background: var(--bg); color: var(--proj-ink, var(--accent)); }
.cmdk__item-ic svg { width: 15px; height: 15px; }
.cmdk__item-ic .sw { width: 15px; height: 15px; border-radius: 50%; }
.cmdk__item-tx { flex: 1; min-width: 0; }
.cmdk__item-tx b { display: block; font-family: var(--font-sans); font-weight: 600; font-size: var(--fs-sm); color: inherit; }
.cmdk__item-tx span { font-size: var(--fs-xs); color: var(--text-mute); }
.cmdk__item-go { font-family: var(--font-mono); font-size: var(--fs-2xs); color: var(--text-mute); opacity: 0; }
.cmdk__item[aria-selected="true"] .cmdk__item-go { opacity: 1; }
.cmdk__empty { padding: 1.6rem 1rem; text-align: center; color: var(--text-mute); font-size: var(--fs-sm); }
.cmdk__foot { display: flex; flex-wrap: wrap; gap: 0.4rem 1.2rem; padding: 0.6rem 1rem; border-top: 1px solid var(--border); font-family: var(--font-mono); font-size: var(--fs-2xs); color: var(--text-mute); }
.cmdk__foot span { display: inline-flex; align-items: center; gap: 0.45em; }
@media (max-width: 460px) { .cmdk__foot { display: none; } }

/* inline ⌘K affordance in body copy */
.kbd-inline { display: inline-flex; gap: 2px; vertical-align: baseline; transition: transform var(--t-fast); }
.kbd-inline:hover { transform: translateY(-1px); }

/* tiny toast (e.g. "email copied") */
.toast {
  position: fixed; left: 50%; bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px)); z-index: 600;
  transform: translate(-50%, 1.2rem); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  font-family: var(--font-sans); font-size: var(--fs-sm); font-weight: 500;
  padding: 0.7rem 1.1rem; border-radius: var(--r-pill); box-shadow: var(--shadow-3);
  transition: opacity var(--t) var(--ease-out), transform var(--t) var(--ease-out);
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 0.5em;
  font-family: var(--font-sans); font-size: var(--fs-sm); font-weight: 600;
  padding: 0.72rem 1.2rem; border-radius: var(--r);
  border: 1px solid transparent; white-space: nowrap;
  transition: transform var(--t-fast) var(--ease), background var(--t), color var(--t), border-color var(--t), box-shadow var(--t);
}
.btn[hidden] { display: none; }   /* the `hidden` attribute must win over display:inline-flex */
.btn:active { transform: translateY(1px); }
.btn--primary { background: var(--accent); color: var(--on-accent); }
.btn--primary:hover { background: var(--accent-strong); box-shadow: var(--shadow-2); }
.btn--ghost { border-color: var(--border-strong); color: var(--text); background: var(--surface); }
.btn--ghost:hover { border-color: var(--text); background: var(--surface-2); }
.btn svg { width: 1em; height: 1em; }
.btn .arrow { transition: transform var(--t) var(--ease); }
.btn:hover .arrow { transform: translateX(3px); }

/* Inline text link with arrow */
.tlink {
  font-family: var(--font-sans); font-weight: 600; font-size: var(--fs-sm);
  color: var(--proj-ink, var(--accent)); display: inline-flex; align-items: center; gap: 0.4em;
}
.tlink .arrow { transition: transform var(--t) var(--ease); }
.tlink:hover .arrow { transform: translateX(4px); }

/* ---------- Chips & badges ---------- */
.chip {
  font-family: var(--font-mono); font-size: var(--fs-2xs); letter-spacing: 0.04em;
  color: var(--text-dim); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 0.28rem 0.55rem; white-space: nowrap;
}
.badge {
  font-family: var(--font-sans); font-size: var(--fs-2xs); font-weight: 700;
  letter-spacing: 0.08em; text-transform: uppercase;
  border-radius: var(--r-sm); padding: 0.25rem 0.5rem;
  display: inline-flex; align-items: center; gap: 0.4em;
}
/* neutral pill (e.g. "Private") — without a variant a bare .badge reads as floating uppercase text */
.badge--dim { color: var(--text-dim); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--border); }
.badge--live { color: var(--up); background: color-mix(in srgb, var(--up) 12%, transparent); }
.badge--live .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--up); animation: pulse-dot 2s var(--ease) infinite; }
/* Paper (light): var(--up) text over its own 12% tint on warm paper lands at ~4.3:1 — just under AA for
   this 11px bold label. Darken the TEXT to the paper accent-strong green (~7:1); dark themes keep --up.
   The attribute-less line is the no-JS fallback (an unstamped root always renders Paper). */
[data-theme="paper"] .badge--live { color: var(--accent-strong); }
:root:not([data-theme]) .badge--live { color: var(--accent-strong); }

@keyframes pulse-dot { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.45; transform: scale(0.82); } }

/* ---------- Reveal-on-scroll ---------- */
.reveal { opacity: 0; transform: translateY(18px); transition: opacity var(--t-slow) var(--ease-out), transform var(--t-slow) var(--ease-out); }
.reveal.is-in { opacity: 1; transform: none; }
/* Arrived through the portal: the portal already showed this content, so paint it in immediately (no second reveal). */
html.portal-in .reveal { opacity: 1 !important; transform: none !important; }
.reveal[data-delay="1"] { transition-delay: 70ms; }
.reveal[data-delay="2"] { transition-delay: 140ms; }
.reveal[data-delay="3"] { transition-delay: 210ms; }
.reveal[data-delay="4"] { transition-delay: 280ms; }

/* ---------- Signature seal (shared core — home floats/sizes it, case pages shrink it) ---------- */
.seal { aspect-ratio: 1; position: relative; color: var(--proj-ink, var(--accent)); flex: none; }
.seal svg { width: 100%; height: 100%; display: block; }
.seal__ring { animation: seal-spin 20s linear infinite; transform-origin: 50% 50%; }
.seal:hover .seal__ring { animation-duration: 6s; }
.seal__text { font-family: var(--font-mono); font-size: 6.3px; letter-spacing: 0.16em; text-transform: uppercase; fill: currentColor; }
.seal__edge { fill: none; stroke: currentColor; stroke-width: 0.6; opacity: 0.4; }
.seal__center { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.seal__mark { width: 22%; height: 22%; background: var(--proj, var(--accent)); transform: rotate(45deg); border-radius: 4px; transition: transform var(--t-slow) var(--ease-spring); }
.seal:hover .seal__mark { transform: rotate(225deg) scale(1.1); }
@keyframes seal-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .seal__ring { animation: none; } }

/* ---------- Footer ---------- */
.site-footer { border-top: 1px solid var(--border); padding-block: clamp(2.5rem, 6vw, 4rem); color: var(--text-mute); }
.site-footer a { color: var(--text-dim); transition: color var(--t); }
.site-footer a:hover { color: var(--text); }
.footer-grid { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 1.5rem 2rem; align-items: flex-start; }
.footer-cred { font-family: var(--font-mono); font-size: var(--fs-xs); }
.footer-grid .footer-cred { margin-top: 0.6rem; max-width: 34ch; }      /* the description under the brand */
.site-footer > .footer-cred { margin-top: 2rem; color: var(--text-mute); }   /* the copyright line */

/* ---------- Touch / coarse-pointer polish ---------- */
a, button { -webkit-tap-highlight-color: transparent; }
@media (pointer: coarse) {
  .theme-toggle { width: 44px; height: 44px; }
  .cmdk-trigger { height: 44px; }                 /* pill keeps its auto width where the ⌘K hint shows */
  .nav-links > a { padding-block: 0.6rem; }
  .index-rail a { padding-block: 0.5rem; }
  .site-footer .cluster a { padding-block: 0.55rem; }   /* footer links (GitHub/LinkedIn/Email…) reach ~44px on touch, like the rest of the UI */
  .tlink { padding-block: 0.55rem; }                     /* "Case study →" on home features + "Visit site" in case asides */
  .case-meta a { padding-block: 0.55rem; }               /* the live-site link in every case-study header */
  .case-back { min-height: 44px; }   /* the primary "back to work" control on every case page — the header Work link is hidden on phones, so it carries real weight */
  .orb-reset { min-height: 44px; }   /* the floating "Return SJ / Return o" reset button */
}
@media (pointer: coarse) and (max-width: 660px) {
  .cmdk-trigger { width: 44px; }                  /* icon-only on phones → comfortable square target */
}

/* ---------- Doctor Strange portal page transitions ----------
   A real portal, not a page swap: the destination loads into a clipped <iframe> (.portal-frame)
   laid over the current page, and a fiery ring (the .portal-fx canvas) opens it from the centre —
   the current page shows around the ring, the next page shows through it. Both layers sit above
   everything; the engine is in initPortal(). */
.portal-frame {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  margin: 0;
  z-index: 2147483646;
  pointer-events: none;   /* purely visual — the ring canvas above handles input blocking */
  background: transparent;
  will-change: clip-path;
}
.portal-fx {
  position: fixed;
  inset: 0;
  z-index: 2147483647;
  pointer-events: none;
  contain: layout style paint;
}
.portal-fx.is-block { pointer-events: auto; }   /* swallow input while the portal is opening */

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .reveal { opacity: 1; transform: none; }
}

/* ---------- Print ---------- */
@media print {
  .site-nav, .theme-toggle, .cmdk-trigger, .scroll-progress, .index-rail, .site-footer .cluster { display: none; }
  body { background: #fff; color: #000; }
}
