/* cube.css — the spatial layer.
 *
 * Loaded on top of styles.css. EVERY rule that repositions content into 3D is
 * scoped behind `.cube-ready`, a class main.js adds only after the module boots.
 * With JS off (or if the module fails), none of this applies: the six panels
 * fall back to normal block flow and index.html reads as the same flat document
 * styles.css already styles. The cube is pure progressive enhancement.
 *
 * Reader content is styled by styles.css defaults, not by `.panel` rules, so a
 * flattened face reads like a clean document (that's the whole point of the
 * focus -> flatten split).
 */

/* ---- shared chrome (hidden until the cube is live) ---------------------- */

.skip-link {
  position: fixed;
  top: -60px;
  left: 12px;
  z-index: 20;
  padding: 8px 12px;
  border-radius: 6px;
  background: var(--ink);
  color: var(--paper);
  transition: top 140ms ease;
}
.skip-link:focus { top: 12px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.face-rail { display: none; }

/* ---- cube stage --------------------------------------------------------- */

.cube-ready {
  /* Sized to actually fit. At min(72vmin, 620px) the projected silhouette ran off
   * the bottom of the viewport at every shipped size — the contact shadow fell
   * off-screen and the backdrop was only ever visible as a thin side margin. The
   * subject needs air around it more than it needs to be big. */
  --cube: min(58vmin, 520px);
  --half: calc(var(--cube) / 2);
  --rx: -22deg;
  --ry: 34deg;
  --scale: 1;

  --cube-face: #ffffff;
  --cube-edge: rgba(26, 26, 26, 0.14);
  /* Cyclorama: the studio sweep the cube is lit inside of. --cube-sky is the
   * pool of light behind it, --cube-floor the falloff that becomes the vignette
   * and the ground. Both are derived from --paper so the two schemes stay in
   * the same family. */
  --cube-sky: color-mix(in oklab, var(--paper) 88%, #ffffff 12%);
  --cube-floor: color-mix(in oklab, var(--paper) 82%, var(--ink) 18%);
  --cube-rim: #ffffff;
  /* Shadow is always *dark*, in both schemes. --ink can't be used for this: it
   * inverts to near-white in dark mode, which would light the faces that turn
   * away from the lamp and make the contact shadow glow. */
  --cube-shade: #14100c;
  --shade-depth: 32%;
  /* On a near-white sweep the contact shadow has little range — it can only
   * darken a few percent before it reads as dirt — so it stays close to 1. */
  --ground-boost: 1.15;
}

@media (prefers-color-scheme: dark) {
  .cube-ready {
    --cube-face: #242424;
    --cube-edge: rgba(255, 255, 255, 0.12);
    --cube-sky: color-mix(in oklab, var(--paper) 86%, var(--ink) 14%);
    --cube-floor: #0c0c0c;
    --cube-rim: color-mix(in oklab, #ffffff 70%, transparent);
    --cube-shade: #000000;
    /* A #242424 face has far less room to fall toward black than a #ffffff one
     * has toward ink, so dark mode needs a deeper mix to get the same read. */
    --shade-depth: 58%;
  }
}

html:has(body.cube-ready),
body.cube-ready {
  height: 100%;
  overflow: hidden;
}

/* The cyclorama. Page mode keeps styles.css's 7px halftone (it's the document's
 * tactile signature); cube mode drops it — a repeating 7px dot behind a lit
 * object reads as sensor noise, not atmosphere. What replaces it is pure value:
 * a pool of light behind the cube, falling off to a floor and a vignette, so
 * the cube is *in* a space instead of pasted on one. */
body.cube-ready {
  background-color: var(--paper);
  background-image:
    radial-gradient(70vmax 52vmax at 50% 34%, var(--cube-sky), transparent 66%),
    radial-gradient(96vmax 78vmax at 50% 42%, transparent 28%, var(--cube-floor) 96%),
    linear-gradient(180deg, transparent 38%, var(--cube-floor) 100%);
  background-size: auto;
}

/* ---- ground ------------------------------------------------------------- */

/* z-scale: ground 1 · cube 2 · rail 12 · mode switch 14 */

/* Contact shadow. cube/light.js drives the three custom properties from the live
 * rig matrix; the defaults here are a sane at-rest pose so it looks right before
 * (and without) JS. Anchoring the cube to a ground does more for "solid object"
 * than any surface treatment. */
.ground {
  position: fixed;
  z-index: 1;
  left: 50%;
  top: 50%;
  width: calc(var(--cube) * 1.15);
  height: calc(var(--cube) * 0.34);
  /* 1.45, not 1.0: the *projected* silhouette is much taller than --half. A
   * corner pose spreads the cube toward half·√3, and perspective magnifies the
   * near face by another ~1.2, so the contact point sits around half·1.5. At
   * anything less the shadow renders behind the cube and is never seen. */
  transform:
    translate(-50%, calc(var(--half) * 1.45))
    skewX(var(--shadow-skew, 0deg))
    scale(var(--shadow-scale, 1));
  border-radius: 50%;
  background: radial-gradient(closest-side ellipse,
    color-mix(in oklab, var(--cube-shade) 62%, transparent), transparent 74%);
  opacity: calc(var(--shadow-opacity, 0.18) * var(--ground-fade, 1) * var(--ground-boost, 1));
  filter: blur(20px);
  pointer-events: none;
  transition: opacity 320ms cubic-bezier(.2, .9, .2, 1);
}
body:not(.cube-ready) .ground { display: none; }
/* Reading a face: the cube is no longer the subject, so the ground steps back. */
body.is-reading .ground { --ground-fade: 0; }

.cube-ready main {
  max-width: none;
  margin: 0;
  padding: 0;
}

.cube-ready .viewport {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  /* 3200px over a 620px cube is a 5:1 ratio — effectively orthographic, which is
   * why the cube used to read isometric and flat. ~2.8:1 is a normal lens: real
   * foreshortening, no fisheye. */
  perspective: 1750px;
  perspective-origin: 50% 46%;
  touch-action: none;
  z-index: 2;
  outline: none;
  cursor: grab; /* the cube always reads as grabbable */
}
body.is-dragging { cursor: grabbing; }

/* The library owns the rig transform (an inline matrix3d it writes each frame);
 * this is just the initial pose + the 3D context. In page mode we neutralise the
 * leftover inline transform so the same markup lays out as a normal document. */
.cube-ready .rig {
  position: relative;
  width: var(--cube);
  height: var(--cube);
  transform: rotateX(var(--rx)) rotateY(var(--ry));
  transform-style: preserve-3d;
}
.cube-ready .rig.is-moving { will-change: transform; }
body:not(.cube-ready) #rig { transform: none !important; }

/* Faces carry NO drop-shadow: a box-shadow on a 3D-transformed element renders
 * in the face's own plane and smears out past the silhouette. Depth comes from
 * the border + an inset edge instead. */
/* Material: matte vellum, lit.
 *
 * --lum (0..1) and --rim (0..1) are written per face, per frame, by
 * cube/light.js from the live rig matrix. The defaults below are the unlit
 * fallback, so with JS off this is exactly the flat white card it always was.
 *
 * Two things make this read as a solid rather than hinged paper panels:
 *   1. --lum darkens the face toward --cube-shade as it turns away from the
 *      light. This is the whole trick — six identically-lit faces never read as
 *      a cube, no matter what texture you put on them.
 *   2. The face colour is slightly translucent, so the cyclorama modulates
 *      *through* the paper. Translucency lives on the background colour, never
 *      on `opacity` — an opacity group would fade the type too and cost text
 *      crispness inside the 3D context.
 *
 * The mottle is three very large soft radials. The 7px dot grain tried in
 * bd0cc42 was reverted because a high-frequency repeat on a rotating 3D plane
 * shimmers and aliases; at 100%+ scale it's uneven paper stock instead, and
 * resolution-independent, so there's nothing to moiré. */
.cube-ready .panel {
  --lum: 1;
  --rim: 0;
  position: absolute;
  inset: 0;
  margin: 0;
  padding: clamp(1rem, 3.2vmin, 1.6rem);
  overflow: hidden;
  border: 1px solid var(--cube-edge);
  border-radius: 16px;
  background-color: color-mix(in oklab,
    color-mix(in oklab, var(--cube-face), var(--cube-shade) calc((1 - var(--lum)) * var(--shade-depth))) 96%,
    transparent);
  background-image:
    /* Specular bloom, at (--lx, --ly) — where light.js says the key light
     * actually lands on this face. The cast shadow under the cube needs an
     * on-surface counterpart, or the lighting reads as a single trick. */
    radial-gradient(72% 72% at calc(50% + var(--lx, 0) * 44%) calc(50% + var(--ly, 0) * 44%),
      color-mix(in oklab, var(--cube-rim) calc(var(--sheen, 0) * var(--surface-mod, 1) * 30%), transparent), transparent 70%),
    /* Occlusion, at the opposite corner. Same vector, negated: the far side of
     * the face falls away from the lamp. */
    radial-gradient(88% 88% at calc(50% - var(--lx, 0) * 40%) calc(50% - var(--ly, 0) * 40%),
      color-mix(in oklab, var(--cube-shade) calc((6% + var(--sheen, 0) * 7%) * var(--surface-mod, 1)), transparent), transparent 76%),
    radial-gradient(120% 95% at 16% 10%, color-mix(in oklab, var(--cube-shade) 3%, transparent), transparent 62%),
    radial-gradient(95% 125% at 84% 72%, color-mix(in oklab, var(--cube-shade) 2.5%, transparent), transparent 58%),
    radial-gradient(140% 105% at 52% 104%, color-mix(in oklab, var(--cube-shade) 2%, transparent), transparent 66%);
  box-shadow:
    inset 0 1px 0 0 color-mix(in oklab, var(--cube-rim) calc(var(--rim) * 55%), transparent),
    inset 0 0 0 1px color-mix(in oklab, var(--cube-face) 100%, var(--ink) 3%);
  backface-visibility: hidden;
  transform-style: preserve-3d;
  cursor: grab;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

.cube-ready .panel--front { transform: translateZ(var(--half)); }
.cube-ready .panel--back { transform: rotateY(180deg) translateZ(var(--half)); }
.cube-ready .panel--right { transform: rotateY(90deg) translateZ(var(--half)); }
.cube-ready .panel--left { transform: rotateY(-90deg) translateZ(var(--half)); }
.cube-ready .panel--top { transform: rotateX(90deg) translateZ(var(--half)); }
.cube-ready .panel--bottom { transform: rotateX(-90deg) translateZ(var(--half)); }

/* Hover ring — a crisp inset ring on the face the pointer is geometrically over
 * (JS-picked). No outer bleed, so nothing smears past the cube silhouette. */
.cube-ready .panel.is-hovered {
  border-color: color-mix(in oklab, var(--link) 55%, var(--cube-edge));
  box-shadow: inset 0 0 0 2px color-mix(in oklab, var(--link) 45%, transparent);
}

.cube-ready .panel.is-focused {
  border-color: color-mix(in oklab, var(--link) 55%, var(--cube-edge));
  box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--link) 30%, transparent);
}

/* Unfocused faces preview their content: inert (so a press orbits/focuses) and
 * clamped with a fade. */
.cube-ready .panel .panel__content {
  height: 100%;
  overflow: hidden;
  pointer-events: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-mask-image: linear-gradient(180deg, #000 68%, transparent 98%);
  mask-image: linear-gradient(180deg, #000 68%, transparent 98%);
}

/* Focused face: read it in place. Selectable text, clickable links, and it
 * scrolls with a visible scrollbar when the content overflows the face. */
/* Reading it: the face is square to the camera and it's a page now, not a
 * surface. JS writes --sheen inline (which no stylesheet rule can outrank), so
 * the modelling is dialled back through a multiplier CSS still owns. */
.cube-ready .panel.is-focused { --surface-mod: 0.3; }

.cube-ready .panel.is-focused .panel__content {
  pointer-events: auto;
  -webkit-user-select: text;
  user-select: text;
  overflow-y: auto;
  -webkit-mask-image: none;
  mask-image: none;
  scrollbar-width: thin;
  scrollbar-color: color-mix(in oklab, var(--ink) 32%, transparent) transparent;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}
.cube-ready .panel.is-focused .panel__content::-webkit-scrollbar { width: 9px; }
.cube-ready .panel.is-focused .panel__content::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: color-mix(in oklab, var(--ink) 30%, transparent);
}
.cube-ready .panel.is-focused iframe { pointer-events: auto; }

/* Media has to take the light too. A portrait or an embed sitting at full
 * brightness on a face that's turned away from the light breaks the illusion
 * instantly — the face reads as a window rather than a surface. Same --lum, in
 * the one form that works on replaced elements. Unfocused faces are teasers, so
 * chroma is held back there too; focusing the face restores true colour. */
.cube-ready .panel img,
.cube-ready .panel iframe {
  filter: brightness(calc(0.60 + var(--lum) * 0.35)) saturate(calc(0.42 + var(--lum) * 0.36));
}
/* Reading it: the face is square to the camera and the media should be true. */
.cube-ready .panel.is-focused img,
.cube-ready .panel.is-focused iframe {
  filter: none;
  transition: filter 320ms cubic-bezier(.2, .9, .2, 1);
}

.cube-ready .panel .panel__content > :first-child { margin-top: 0; }
.cube-ready .panel h2 { margin-top: 1.25rem; font-size: 1.15rem; }
.cube-ready .panel h2:first-child { margin-top: 0; }
.cube-ready .panel h3 { margin-top: 1rem; }
.cube-ready .panel iframe { pointer-events: none; }

.cube-ready .panel--front .panel__content {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.cube-ready .panel--front h1 { font-size: clamp(1.6rem, 5vmin, 2.3rem); }
.cube-ready .panel--front .hero { margin: 0; }
.cube-ready .panel--front .tagline {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.5;
}
.cube-ready .panel--front .social-banner {
  justify-content: center;
  gap: 0.15rem;
  margin: 0.4rem 0 0.1rem;
  border: 0;
  padding: 0;
}
.cube-ready .panel--front .social-banner a { width: 34px; height: 34px; font-size: 1.05rem; }
/* the contact table lost its `header dd { margin:0 }` when it moved out of the
 * hero — restore tight rows. */
.panel--front dl { gap: 0.15rem 1rem; margin: 0; }
.panel--front dd { margin: 0; }

/* ---- face rail ---------------------------------------------------------- */

.cube-ready .face-rail {
  position: fixed;
  z-index: 12;
  left: 50%;
  bottom: max(1.1rem, env(safe-area-inset-bottom));
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem;
  max-width: calc(100vw - 1.5rem);
  padding: 0.3rem;
  border: 1px solid var(--cube-edge);
  border-radius: 999px;
  background: color-mix(in oklab, var(--paper) 82%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  transform: translateX(-50%);
}

.rail-button {
  min-height: 36px;
  padding: 7px 13px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease, border-color 140ms ease;
}
.rail-button:hover { color: var(--ink); background: color-mix(in oklab, var(--ink) 8%, transparent); }
.rail-button:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.rail-button.is-active {
  color: var(--paper);
  background: var(--ink);
  border-color: var(--ink);
}

/* ---- mode switch (cube ⇄ reading document) ------------------------------ */

.mode-switch {
  position: fixed;
  z-index: 14;
  top: max(1rem, env(safe-area-inset-top));
  right: max(1rem, env(safe-area-inset-right));
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--muted);
  border-radius: 999px;
  background: color-mix(in oklab, var(--paper) 82%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  cursor: pointer;
}
.mode-switch:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.mode-switch__icon {
  display: grid;
  place-items: center;
  width: 34px;
  height: 30px;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.82rem;
  transition: background-color 160ms ease, color 160ms ease;
}
/* the active side (keyed on the mode) is filled; clicking anywhere flips it */
body.cube-ready .mode-switch__icon[data-mode="cube"],
body:not(.cube-ready) .mode-switch__icon[data-mode="page"] {
  background: var(--ink);
  color: var(--paper);
}

/* Portrait on the identity face. */
.portrait {
  width: 100%;
  max-height: 260px;
  margin: 0.75rem 0 0.25rem;
  border-radius: 12px;
  object-fit: cover;
  object-position: 52% 20%;
}
.cube-ready .panel--front .portrait { max-height: 180px; }

@media (max-width: 600px) {
  .cube-ready { --cube: min(76vmin, 400px); }
  .cube-ready .panel { border-radius: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  .cube-ready .rig { transition: none; }
}
