/* mat — the fixed layer that stays behind every section. */

#root {
  position: relative;
  height: 100vh;
  min-height: 600px;
  overflow-y: scroll;
  overflow-x: hidden;
  /* JS in scroll.js intercepts wheel + touch and calls scrollTo({behavior:'smooth'}).
     CSS snap is removed so it doesn't fight the JS-driven animation. */
  scroll-behavior: smooth;
  /* Prevent scroll-chaining to the browser (no rubber-band on macOS, no
     pull-to-refresh on Android) when reaching top/bottom of the site. */
  overscroll-behavior-y: contain;
  cursor: crosshair;
  background: var(--bg);
  user-select: none;
}
@media (prefers-reduced-motion: reduce) {
  #root { scroll-behavior: auto; }
}

.mat {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

/* --- grid: 120px major cells + 24px minor cells, offset 32px for the rulers --- */
.mat__grid {
  position: absolute;
  inset: 0;
  z-index: 0;
  background-color: var(--bg);
  background-image:
    repeating-linear-gradient(180deg, transparent 0 118px, var(--line) 118px 120px),
    repeating-linear-gradient(90deg, transparent 0 118px, var(--line) 118px 120px),
    repeating-linear-gradient(180deg, transparent 0 23px, var(--faint) 23px 24px),
    repeating-linear-gradient(90deg, transparent 0 23px, var(--faint) 23px 24px);
  background-position: 32px 32px;
}

/* --- grade overlays. All are driven by --grade-amount (0..1) via scene.js. --- */
.mat__grade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: var(--grade-amount);
  transition: opacity 400ms ease;
}

/* Teal-orange split, anchored at the live sun position. Warm bleeds from
   the sun, cool sits in the opposite quadrant. Debug picker drives both. */
.mat__grade--hue {
  background:
    radial-gradient(ellipse 150% 130% at var(--sun-x) var(--sun-y),
      var(--grade-warm) 0%,
      transparent 55%),
    radial-gradient(ellipse 150% 130% at calc(100% - var(--sun-x)) calc(100% - var(--sun-y)),
      var(--grade-cool) 0%,
      transparent 65%);
  mix-blend-mode: soft-light;
}

/* NOTE: `.mat__grade--window-shadow` was replaced by the top-level
   `.window-projection` element (defined below). See index.html. */

/* Warm key pool + window streaks — anchored to the live sun. Bigger radius
   so the light spills further across the mat. Color driven by --sun-r/g/b
   (comes from color-temperature). */
.mat__grade--warm-pool {
  background:
    radial-gradient(180% 140% at var(--sun-x) var(--sun-y),
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0.24),
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0) 62%),
    linear-gradient(38deg,
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0)    30%,
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0.08) 40%,
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0)    50%,
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0.06) 58%,
      rgba(var(--sun-r), var(--sun-g), var(--sun-b), 0)    66%);
}

/* Surface photographic grain — opacity now driven by --grain slider */
.mat__noise {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: var(--grain);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* --- rulers (32px band top + left, matched to the grid) --- */
.ruler {
  position: absolute;
  z-index: 1;
  pointer-events: none;
  display: flex;
  background: var(--band);
  font-family: 'JetBrains Mono', monospace;
}
.ruler--top {
  top: 0; left: 0; right: 0;
  height: 32px;
  padding-left: 32px;
  border-bottom: 1px solid var(--line);
}
.ruler--left {
  top: 0; left: 0; bottom: 0;
  width: 32px;
  padding-top: 32px;
  flex-direction: column;
  border-right: 1px solid var(--line);
}
.ruler__cell {
  position: relative;
  flex: none;
  color: var(--label);
  font-size: 11px;
  font-weight: 500;
}
.ruler--top .ruler__cell {
  width: 120px;
  border-right: 1px solid var(--line);
}
.ruler--top .ruler__cell span { position: absolute; left: 5px; top: 8px; }
.ruler--left .ruler__cell {
  height: 120px;
  border-bottom: 1px solid var(--line);
}
.ruler--left .ruler__cell span { position: absolute; left: 6px; top: 5px; }

/* --- brass section tag (top-right) --- */
.section-tag {
  position: absolute;
  top: 46px;
  right: 36px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 12px 10px 20px;
  border-radius: 7px;
  background: linear-gradient(168deg, #f8f5e9 0%, #f2eedd 62%, #eae5d0 100%);
  border: 1px solid rgba(34, 48, 31, 0.18);
  box-shadow:
    0 2px 3px rgba(6, 16, 10, 0.28),
    0 12px 28px rgba(6, 16, 10, 0.36),
    inset 0 1px 0 rgba(255, 255, 255, 0.7);
  pointer-events: none;
}
.section-tag__hole {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: rgba(20, 38, 28, 0.9);
  border: 2px solid rgba(34, 48, 31, 0.35);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.55), 0 1px 0 rgba(255, 255, 255, 0.55);
}
.section-tag__text {
  font-family: 'JetBrains Mono', monospace;
  writing-mode: vertical-rl;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.22em;
  color: #22301f;
  text-transform: uppercase;
  white-space: nowrap;
  /* Fade + slide on section change (js/scroll.js toggles .is-hiding just
     before swapping textContent, then clears it once the new text is in). */
  transition: opacity 220ms ease, transform 220ms ease;
}
.section-tag__text.is-hiding {
  opacity: 0;
  transform: translateY(-6px);
}

/* --- pencil nav banner (top-center) --- */
.pencil-nav {
  position: absolute;
  top: 34px;
  left: 50%;
  transform: translateX(-50%);
  width: min(760px, 80%);
  z-index: 7;
  pointer-events: auto;
}
.pencil-nav__inner {
  position: relative;
  width: 100%;
  aspect-ratio: 780 / 54;
  transition: transform 0.65s cubic-bezier(0.5, 0, 0.7, 0.4), opacity 0.45s ease;
}
.pencil-nav__img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter:
    drop-shadow(2px 3px 3px rgba(5, 13, 8, 0.45))
    drop-shadow(8px 14px 18px rgba(5, 13, 8, 0.32))
    drop-shadow(0 2px 4px rgba(6, 16, 10, 0.28));
  pointer-events: none;
}
.pencil-nav__links {
  position: absolute;
  top: 0; bottom: 0;
  left: 9.5%; right: 19%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: 'JetBrains Mono', monospace;
  font-size: clamp(9px, 1.15vw, 12px);
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #2c342c;
}
.pencil-nav__brand { font-weight: 700; }
.pencil-nav__right {
  display: flex;
  align-items: center;
  gap: clamp(16px, 2.6vw, 34px);
}
.pencil-nav__right a { opacity: 0.85; transition: opacity 0.2s ease; }
.pencil-nav__right a:hover { opacity: 1; }

/* --- section nav dots (right rail) --- */
.section-nav {
  position: absolute;
  z-index: 8;
  right: 44px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 14px;
  pointer-events: auto;
}
.section-nav button {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  border: 1.5px solid var(--fg);
  background: transparent;
  opacity: 0.55;
  transition: all 0.3s ease;
}
.section-nav button.is-active {
  width: 11px;
  height: 11px;
  background: var(--fg);
  opacity: 1;
}

/* --- copyright (bottom-right) --- */
.copyright {
  position: absolute;
  right: 48px;
  bottom: 30px;
  z-index: 4;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--muted);
  pointer-events: none;
}

/* --- global photo grade over the whole viewport, driven by --grade-amount --- */
.photo-grade {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  opacity: var(--grade-amount);
  transition: opacity 400ms ease;
  background:
    radial-gradient(115% 85% at 18% 8%, rgba(255, 206, 140, 0.13), rgba(255, 206, 140, 0) 52%),
    radial-gradient(140% 120% at 42% 38%, rgba(0, 0, 0, 0) 50%, rgba(9, 15, 6, 0.34) 82%, rgba(8, 13, 5, 0.58) 100%),
    linear-gradient(295deg, rgba(9, 15, 6, 0.26), rgba(9, 15, 6, 0) 40%);
}

/* --- Window muntin projection — TOP-LEVEL element (outside .mat).
   Sunlight coming through the window bars falls on the mat AND every
   object sitting on it. Because .mat has z-index:0 (its own stacking
   context), keeping the muntin overlay inside .mat trapped it below
   sections — now it lives outside so it can overlay everything below
   the frame border.

   Position/size/rotation/skew/blur/intensity are still driven live from
   the Scene rig via CSS custom properties. --- */
.window-projection {
  position: fixed;
  inset: 0;
  z-index: 5;             /* above sections (1) + WebGL layers (2, 5),
                             below frame (6) */
  pointer-events: none;
  overflow: hidden;
}
/* Physics: the SVG pattern IS the window frame (muntins) drawn dark on
   transparent. mix-blend-mode:multiply casts that as a shadow on the mat
   and everything sitting on it. Pane areas are transparent gaps → the mat
   shows through unchanged (physically correct: no extra light in panes,
   just no shadow either). Wide radial mask fades the projection's own
   edges so it doesn't cut off harshly. */
.window-projection {
  perspective: var(--window-distance);
  perspective-origin: 50% 100%;
}
.window-projection::before {
  content: "";
  position: absolute;
  left: var(--window-x);
  top: var(--window-y);
  width: var(--window-w);
  height: var(--window-h);
  opacity: var(--window-intensity);
  mix-blend-mode: multiply;
  filter: blur(var(--window-blur));
  transform:
    rotateX(var(--window-elevation))
    rotateY(var(--window-azimuth))
    rotate(var(--window-rot))
    skewY(var(--window-skew));
  transform-origin: 50% 100%;
  transform-style: preserve-3d;
  background-image: var(--window-pattern);
  /* SVG sized SMALLER than the box + centered — creates transparent padding
     around the muntin bars so their outermost edges don't touch the mask
     boundary. This is what removes the "hard rectangle inside a soft halo"
     look at the outer edges. The visible projection now feathers into a
     softer envelope than the muntin pattern itself. */
  background-size: 82% 82%;
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-color: transparent;
  /* Aggressive multi-stop feather: ellipse sized so the gradient completes
     WITHIN the container (fully transparent by the corners) rather than
     extending past it. Combined with the 82% background-size inset above,
     the outer 15-20% of the projection is genuinely gone — no sharp mask
     boundary anywhere. */
  -webkit-mask-image: radial-gradient(ellipse 125% 110% at 50% 50%,
    rgba(0,0,0,0.98) 0%,
    rgba(0,0,0,0.72) 40%,
    rgba(0,0,0,0.30) 72%,
    rgba(0,0,0,0.08) 90%,
    transparent      100%);
          mask-image: radial-gradient(ellipse 125% 110% at 50% 50%,
    rgba(0,0,0,0.98) 0%,
    rgba(0,0,0,0.72) 40%,
    rgba(0,0,0,0.30) 72%,
    rgba(0,0,0,0.08) 90%,
    transparent      100%);
}

/* --- Curtain light-play — a second overlay on the SAME window projection
   surface. Where ::before paints the muntin frame in multiply (dark
   shadow), ::after paints a soft, warm, subtly-wavy pattern in soft-light
   (bright lift). Physical read: sunlight has passed through a sheer
   curtain hanging outside the window; where the fabric is thin, more warm
   light reaches the mat. Uses the SAME 3D transform + mask envelope as the
   muntin projection so it's co-located with the light footprint. --- */
.window-projection::after {
  content: "";
  position: absolute;
  left: var(--window-x);
  top: var(--window-y);
  width: var(--window-w);
  height: var(--window-h);
  /* --curtain-strength published by js/motion.js (default 0.55) */
  opacity: calc(var(--window-intensity) * var(--curtain-strength, 0.55));
  mix-blend-mode: soft-light;
  filter: blur(calc(var(--window-blur) * 0.55));
  transform:
    rotateX(var(--window-elevation))
    rotateY(var(--window-azimuth))
    rotate(var(--window-rot))
    skewY(var(--window-skew));
  transform-origin: 50% 100%;
  transform-style: preserve-3d;
  /* Wavy vertical folds — one repeating gradient plus a slower second
     phase so peaks don't line up rigidly (fabric folds are aperiodic).
     Color driven by --curtain-r/g/b (published by js/motion.js) with a
     safe warm default so pattern still renders if vars aren't set yet. */
  background:
    repeating-linear-gradient(87deg,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.00)  0px,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.55) 24px,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.00) 48px),
    repeating-linear-gradient(92deg,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.00)  0px,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.35) 62px,
      rgba(var(--curtain-r, 255), var(--curtain-g, 220), var(--curtain-b, 175), 0.00) 124px);
  background-size: 240% 100%, 220% 100%;   /* extra width so sway has room */
  background-position: 0% 50%, 0% 50%;
  background-repeat: repeat;
  /* Ellipse is SMALLER than the container (78% × 72%) so the rect corners
     sit outside the mask entirely and fade to fully transparent. Soft
     multi-stop falloff — no perceptible boxed edge anywhere. */
  -webkit-mask-image: radial-gradient(ellipse 78% 72% at 50% 50%,
    #000 0%,
    rgba(0,0,0,0.85) 30%,
    rgba(0,0,0,0.45) 62%,
    rgba(0,0,0,0.12) 85%,
    transparent 100%);
          mask-image: radial-gradient(ellipse 78% 72% at 50% 50%,
    #000 0%,
    rgba(0,0,0,0.85) 30%,
    rgba(0,0,0,0.45) 62%,
    rgba(0,0,0,0.12) 85%,
    transparent 100%);
  pointer-events: none;
}

@keyframes curtainPlaySway {
  0%   { background-position:  0% 50%,  0% 50%; }
  100% { background-position: 10% 50%,  6% 50%; }
}
html.motion-curtain-play .window-projection::after {
  animation: curtainPlaySway 14s ease-in-out infinite alternate;
}
@media (prefers-reduced-motion: reduce) {
  html.motion-curtain-play .window-projection::after { animation: none; }
}

/* --- double border frame — top-most persistent layer --- */
.frame {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  box-shadow:
    inset 0 0 0 1px var(--line),
    inset 0 0 0 8px var(--bg),
    inset 0 0 0 9px var(--faint);
}

/* --- cursor trail layer — fixed, above mat but below frame --- */
.trail-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}

