/* ============================================================
   Poop Scratcher — full-page scratch experience.
   Whole screen = realistic poop. Scratch it off -> bright payoff.
   ============================================================ */

:root {
  --brown: #2a1c0c;
  --brown-2: #1a1206;
  --cream: #fff6e6;
  --ink: #3a2410;
  --ink-soft: #7a5836;
  --gold: #f4b942;
  --gold-hi: #ffd77a;
  --gold-deep: #b9832b;
  --teal: #16a67f;
  --coral: #ff6b4a;

  --ff-display: 'Bricolage Grotesque', 'Trebuchet MS', system-ui, sans-serif;
  --ff-body: 'Instrument Sans', system-ui, -apple-system, sans-serif;
  --ff-mono: 'Space Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;                 /* the whole page IS the scratch surface */
  overscroll-behavior: none;
  background: var(--brown-2);
  font-family: var(--ff-body);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
img { max-width: 100%; display: block; }
::selection { background: var(--gold); color: #3a2410; }

/* ---------------- full-viewport stage ---------------- */
#stage {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;                   /* dynamic viewport on mobile */
  overflow: hidden;
  touch-action: none;
  background: var(--brown-2);
}

/* ---------------- payoff (revealed underneath) ---------------- */
.payoff {
  position: absolute;
  inset: 0;
  z-index: 1;
  background: #0d0a06;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
/* full-screen red flash when the "SYSTEM ERROR" slams onto the revealed photo */
#redflash {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: #ff1a1a;
  opacity: 0;
  pointer-events: none;
}
#redflash.flash { animation: redFlash 0.9s ease both; }
@keyframes redFlash {
  0% { opacity: 0; }
  12% { opacity: 0.9; }
  26% { opacity: 0.12; }
  42% { opacity: 0.82; }
  60% { opacity: 0.06; }
  100% { opacity: 0; }
}

/* the photo hidden under the poop — fills the screen, biased to the face */
.reveal-photo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 30%;
  z-index: 0;
  -webkit-user-drag: none;
  user-drag: none;
}
/* layer visibility: round 1 shows the photo; round 2/final show the white void */
body.round2 .reveal-photo, body.final .reveal-photo { display: none; }
.white-screen { position: absolute; inset: 0; background: #fff; display: none; z-index: 0; overflow: hidden; }
body.round2 .white-screen, body.final .white-screen { display: block; }
/* the real payoff: sped-up drone footage, full bleed under the scratch layer */
#droneVid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #0b0b0c;
}

/* the big red nag that drops onto him (round-2 cover is the photo itself) */
.err-text {
  position: fixed;
  inset: 0;
  z-index: 36;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 6vw;
  font-family: var(--ff-display);
  font-weight: 800;
  font-size: clamp(2.4rem, 9vw, 6rem);
  line-height: 0.98;
  letter-spacing: -0.02em;
  color: #ff1e1e;
  text-shadow: 0 0 2px #000, 0 3px 0 rgba(0,0,0,0.55), 0 8px 34px rgba(0,0,0,0.7);
  pointer-events: none;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.err-text.show { opacity: 1; transform: none; }
.err-text.shake { animation: errShake 0.28s linear 4; }
@keyframes errShake {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(-7px, 3px); }
  50% { transform: translate(6px, -4px); }
  75% { transform: translate(-3px, -4px); }
}

/* final blank-screen anticlimax bar */
.void-bar {
  position: fixed;
  left: 50%;
  bottom: max(30px, env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 37;
  width: min(92vw, 680px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.1rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease 0.3s, transform 0.5s ease 0.3s;
}
body.final .void-bar { opacity: 1; transform: translateX(-50%); pointer-events: auto; }
/* scrim so the copy survives over the moving footage behind it */
.void-bar::before {
  content: '';
  position: absolute;
  inset: -28px -24px -34px;
  z-index: -1;
  border-radius: 22px;
  background: linear-gradient(to top, rgba(0,0,0,0.78), rgba(0,0,0,0.4) 62%, transparent);
  pointer-events: none;
}
.void-text {
  font-family: var(--ff-body);
  font-weight: 500;
  color: #f2efe8;
  text-shadow: 0 2px 14px rgba(0,0,0,0.9);
  font-size: clamp(0.98rem, 2.6vw, 1.2rem);
  max-width: 42ch;
}
.bar-actions { display: flex; gap: 0.7rem; flex-wrap: wrap; justify-content: center; }
.btn.dark { background: #1a1a1a; color: #fff; box-shadow: 0 10px 24px -8px rgba(0,0,0,0.45); }
.btn.dark:hover { background: #000; }
.btn.dark.ghost { background: transparent; color: #1a1a1a; border: 1.5px solid #cfc7bb; box-shadow: none; }
.btn.dark.ghost:hover { border-color: #1a1a1a; color: #000; background: #f2efe9; }

.sparkles { display: flex; gap: 1.2rem; font-size: clamp(2rem, 6vw, 3.4rem); margin-bottom: 0.4rem; }
.sparkles span { display: inline-block; animation: bob 2.6s ease-in-out infinite; }
.sparkles span:nth-child(2) { animation-delay: 0.5s; font-size: 1.3em; }
.sparkles span:nth-child(3) { animation-delay: 1s; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-10px) rotate(6deg); } }

.eyebrow {
  font-family: var(--ff-mono);
  font-size: clamp(0.7rem, 1.6vw, 0.85rem);
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--gold-deep);
}
.payoff h1 {
  font-family: var(--ff-display);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 0.98;
  font-size: clamp(2.6rem, 9vw, 6rem);
  margin: 0.4rem 0 0.2rem;
  max-width: 16ch;
}
.payoff h1 .hl { color: var(--coral); }
.payoff .sub {
  font-size: clamp(1.05rem, 2.6vw, 1.4rem);
  color: var(--ink-soft);
  max-width: 46ch;
  margin: 0.2rem 0 0;
  font-weight: 500;
}
.statline {
  margin-top: 1rem;
  font-family: var(--ff-mono);
  font-size: clamp(0.8rem, 2vw, 0.95rem);
  color: var(--ink);
  background: rgba(255,255,255,0.5);
  border: 1px dashed var(--gold-deep);
  padding: 0.5rem 1rem;
  border-radius: 999px;
}
.statline b { color: var(--gold-deep); }

.payoff-actions { display: flex; gap: 0.8rem; flex-wrap: wrap; justify-content: center; margin-top: 1.6rem; }

.btn {
  font-family: var(--ff-display);
  font-weight: 700;
  font-size: 1.05rem;
  padding: 0.9rem 1.6rem;
  border-radius: 999px;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  background: linear-gradient(180deg, var(--gold-hi), var(--gold));
  color: #3a2410;
  box-shadow: 0 12px 30px -8px rgba(185,131,43,0.6), inset 0 1px 0 rgba(255,255,255,0.5);
  transition: transform 0.15s ease, box-shadow 0.2s ease;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn.ghost { background: rgba(255,255,255,0.1); color: var(--cream); border: 1.5px solid rgba(255,255,255,0.4); box-shadow: none; backdrop-filter: blur(4px); }
.btn.ghost:hover { background: rgba(255,255,255,0.2); border-color: var(--gold-hi); color: var(--gold-hi); }
.btn.sm { padding: 0.65rem 1.15rem; font-size: 0.95rem; }
.btn:focus-visible { outline: 3px solid var(--gold-deep); outline-offset: 3px; }

.wordmark { margin-top: 2rem; font-family: var(--ff-display); font-weight: 700; color: var(--ink-soft); font-size: 0.95rem; opacity: 0.8; }
.wordmark b { color: var(--gold-deep); }

/* ---------------- floating chrome (above poop canvas) ---------------- */
.hint {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  font-family: var(--ff-display);
  font-weight: 700;
  font-size: clamp(0.95rem, 2.6vw, 1.25rem);
  color: var(--cream);
  background: rgba(20, 12, 4, 0.72);
  backdrop-filter: blur(6px);
  padding: 0.7rem 1.2rem;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.12);
  box-shadow: 0 10px 30px -8px rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  pointer-events: none;
  transition: opacity 0.5s ease, transform 0.5s ease;
  white-space: nowrap;
}
.hint .hand { font-size: 1.3em; animation: wiggle 1.5s ease-in-out infinite; }
@keyframes wiggle { 0%,100% { transform: rotate(-12deg); } 50% { transform: rotate(12deg) translateY(-3px); } }
body.scratched .hint { opacity: 0; transform: translateX(-50%) translateY(-14px); }

/* thin poop-cleared progress bar at very top */
.prog {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 6px;
  z-index: 31;
  background: rgba(0,0,0,0.35);
}
.prog-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--coral), var(--gold), var(--teal));
  transition: width 0.1s linear;
}
body.ps-done .prog, body.ps-done .hint, body.ps-done .skip { opacity: 0; pointer-events: none; transition: opacity 0.4s ease; }

/* keyboard / motor-impaired path */
.skip {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: 16px;
  z-index: 30;
  font-family: var(--ff-mono);
  font-size: 0.78rem;
  color: var(--cream);
  background: rgba(20, 12, 4, 0.7);
  border: 1px solid rgba(255,255,255,0.14);
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
  backdrop-filter: blur(6px);
  transition: background 0.2s, color 0.2s;
}
.skip:hover { background: rgba(20,12,4,0.9); color: var(--gold-hi); }
.skip:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ---------------- naan cursor ----------------
   72x72 (Chrome caps cursors at 128); the hotspot sits on the pointed tip.
   The scratch canvas needs !important because the library sets cursor inline. */
html, body { cursor: url('naan.png') 22 15, auto; }
button, .btn, .skip, .sound, a { cursor: url('naan.png') 22 15, pointer; }
.ps-canvas { cursor: url('naan.png') 22 15, grab !important; }
.ps-canvas:active { cursor: url('naan-bite.png') 22 15, grabbing !important; }

/* ---------------- beat-synced grade ----------------
   All of this is driven by custom properties the FX loop writes each frame:
     --fx      master gate (0 unless the music is actually audible)
     --punch   0..1 sharp attack on every beat, scaled by that beat's strength
     --energy  0..1 slow section intensity, so the drop hits harder
     --flash   0..1 downbeat strobe
     --shakeX/Y  kick offset in px
   Nothing here uses @keyframes, so when the music stops everything settles. */
/* #stage stays untransformed on purpose — it is the reference frame the
   pointer mapping inverts against. All movement happens on #warp, and the
   exact same numbers are fed to PoopScratcher's mapPoint so rotation cannot
   desync the scratching. Order here (translate, rotate, scale) must match the
   inverse applied in app.js. */
#warp {
  position: absolute;
  inset: 0;
  transform: translate3d(calc(var(--shakeX, 0) * 1px), calc(var(--shakeY, 0) * 1px), 0)
             rotate(calc(var(--rot, 0) * 1deg))
             scale(calc(1 + 0.115 * var(--punch, 0))); /* keep ZOOM in app.js in sync */
  filter: saturate(calc(1 + 0.7 * var(--energy, 0) + 0.6 * var(--punch, 0)))
          contrast(calc(1 + 0.12 * var(--energy, 0) + 0.24 * var(--punch, 0)))
          brightness(calc(1 + 0.13 * var(--punch, 0)))
          hue-rotate(calc(var(--hue, 0) * 1deg));
  will-change: transform, filter;
}
/* blur and invert are expensive full-screen passes — measured ~9fps to leave
   them in the chain permanently at zero. The FX loop only adds this class for
   the handful of frames a slam actually lasts. */
#warp.slam {
  filter: saturate(calc(1 + 0.7 * var(--energy, 0) + 0.6 * var(--punch, 0)))
          contrast(calc(1 + 0.12 * var(--energy, 0) + 0.24 * var(--punch, 0)))
          brightness(calc(1 + 0.13 * var(--punch, 0)))
          hue-rotate(calc(var(--hue, 0) * 1deg))
          blur(calc(var(--blur, 0) * 1px))
          invert(var(--invert, 0));
}

#fx {
  position: fixed;
  inset: 0;
  z-index: 25; /* over the scratch canvas, under the hint/progress/buttons */
  pointer-events: none;
  opacity: var(--fx, 0);
  contain: layout paint style; /* not size — the insets define the box */
}
#fx > * { position: absolute; inset: 0; }

/* warm light leak that blooms out of the corners on the downbeat */
.fx-leak {
  background:
    radial-gradient(60% 80% at 12% 8%, rgba(255,196,120,0.55), transparent 60%),
    radial-gradient(55% 75% at 88% 92%, rgba(120,190,255,0.42), transparent 62%);
  mix-blend-mode: screen;
  opacity: calc(0.10 + 0.75 * var(--flash, 0) + 0.18 * var(--punch, 0));
}

/* VHS scanlines — subtle, they read as texture rather than stripes */
.fx-scan {
  background: repeating-linear-gradient(
    to bottom,
    rgba(0,0,0,0.16) 0px, rgba(0,0,0,0.16) 1px,
    transparent 1px, transparent 3px
  );
  mix-blend-mode: multiply;
  opacity: calc(0.30 + 0.35 * var(--energy, 0));
}

/* film grain: one static noise tile, jittered a frame at a time */
.fx-grain {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E");
  background-repeat: repeat;
  transform: translate3d(calc(var(--grainX, 0) * 1px), calc(var(--grainY, 0) * 1px), 0);
  mix-blend-mode: overlay;
  opacity: calc(0.22 + 0.30 * var(--energy, 0) + 0.22 * var(--punch, 0));
}

/* vignette that breathes with the section energy */
.fx-vig {
  background: radial-gradient(circle at 50% 48%, transparent 38%, rgba(0,0,0,0.85) 100%);
  opacity: calc(0.35 + 0.45 * var(--energy, 0) - 0.18 * var(--punch, 0));
}

/* short white strobe on the biggest hits */
.fx-flash {
  background: #fff;
  mix-blend-mode: overlay;
  opacity: calc(0.55 * var(--flash, 0));
}

/* Motion-sensitive visitors get the music and the grade, but no movement,
   no strobe and no inversion. */
@media (prefers-reduced-motion: reduce) {
  #warp { transform: none; filter: saturate(calc(1 + 0.4 * var(--energy, 0))); }
  .fx-flash, .fx-leak { opacity: 0; }
  #droneVid { animation: none; }
}

/* ---------------- background music ---------------- */
/* The YouTube iframe has to stay in the layout for the player to start, so it
   lives as a 1px square tucked behind the scratch stage instead of hidden. */
#bgm {
  position: fixed;
  bottom: 0; left: 0;
  width: 1px; height: 1px;
  overflow: hidden;
  z-index: 0;
  pointer-events: none;
}
#bgm iframe { width: 1px; height: 1px; border: 0; }

/* mute toggle — top right, clear of the hint (top centre) and the 6px prog bar */
.sound {
  position: fixed;
  top: calc(max(18px, env(safe-area-inset-top)) + 6px);
  right: 16px;
  z-index: 38;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-family: var(--ff-mono);
  font-size: 0.78rem;
  color: var(--cream);
  background: rgba(20, 12, 4, 0.7);
  border: 1px solid rgba(255,255,255,0.14);
  padding: 0.55rem 0.9rem;
  border-radius: 999px;
  cursor: pointer;
  backdrop-filter: blur(6px);
  transition: background 0.2s, color 0.2s, border-color 0.2s, opacity 0.3s;
}
.sound:hover { background: rgba(20,12,4,0.9); color: var(--gold-hi); }
.sound:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.sound.off .sound-ico { opacity: 0.5; }
/* the final payoff is a blank white screen — the dark pill would shout on it */
/* the final screen is moving footage, so keep the dark pill for contrast */
body.final .sound {
  background: rgba(10, 8, 6, 0.62);
  border-color: rgba(255,255,255,0.22);
  color: var(--cream);
}
body.final .sound:hover { background: rgba(10,8,6,0.85); color: var(--gold-hi); }
/* no player (blocked script / no network) -> nothing to toggle */
.sound.unavailable { display: none; }
@media (max-width: 430px) {
  .sound { padding: 0.5rem 0.7rem; }
  .sound .sound-txt { display: none; }
}

/* ---------------- boot cover (prevents payoff flash) ---------------- */
#boot {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: radial-gradient(circle at 50% 40%, #3a2810, #160f05);
  color: #c9a86f;
  font-family: var(--ff-mono);
  letter-spacing: 0.1em;
  transition: opacity 0.4s ease;
}
#boot .boot-emoji { font-size: 3.5rem; animation: bob 1.8s ease-in-out infinite; }
#boot .boot-txt { font-size: 0.85rem; }
body.booted #boot { opacity: 0; pointer-events: none; }

/* toast */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 60;
  background: var(--gold);
  color: #3a2410;
  font-family: var(--ff-display);
  font-weight: 700;
  padding: 0.8rem 1.4rem;
  border-radius: 999px;
  box-shadow: 0 16px 40px -12px rgba(185,131,43,0.7);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* payoff pop on reveal */
.ps-revealed .payoff { animation: payoffPop 0.7s cubic-bezier(0.16, 1, 0.3, 1); }
@keyframes payoffPop { 0% { transform: scale(0.94); opacity: 0.5; } 60% { transform: scale(1.015); } 100% { transform: none; opacity: 1; } }

/* On viewports wider than the near-square photo (desktop, landscape tablets)
   cover would crop his head off — letterbox him over a blurred fill instead.
   Keep the 9/10 threshold in sync with drawSelfie() in app.js. */
@media (min-aspect-ratio: 9/10) {
  .payoff::before {
    content: '';
    position: absolute;
    inset: -48px;
    z-index: 0;
    background: url('reveal.webp') center / cover no-repeat;
    filter: blur(26px) brightness(0.5);
  }
  body.round2 .payoff::before, body.final .payoff::before { display: none; }
  .reveal-photo {
    z-index: 1;
    object-fit: contain;
    object-position: center;
  }
  .white-screen { z-index: 2; }
}

/* keep the payoff readable on very short (landscape phone) viewports */
@media (max-height: 560px) {
  .payoff { gap: 0.3rem; padding: 4vh 6vw; }
  .payoff h1 { font-size: clamp(1.8rem, 7vw, 3rem); }
  .sparkles { font-size: 1.8rem; margin-bottom: 0; }
  .wordmark { display: none; }
  .statline { margin-top: 0.5rem; }
  .payoff-actions { margin-top: 0.9rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}
