/* Dark cinematic birthday app. Portrait target 390x844, no horizontal scroll.
   Design system: near-black ground, warm off-white type, ONE brave accent
   (candle amber), translucent materials, strong ease-out curves. */

:root {
  --bg: #08080d;
  --bg-raised: #101018;
  --surface: rgba(255, 255, 255, 0.06);
  --surface-strong: rgba(255, 255, 255, 0.11);
  --hairline: rgba(255, 255, 255, 0.10);
  --text: #f4f1e9;
  --text-dim: rgba(244, 241, 233, 0.55);
  --text-faint: rgba(244, 241, 233, 0.32);
  --accent: #ffb45c;
  --accent-deep: #ff8b2a;
  --radius-lg: 28px;
  --radius-md: 18px;
  --radius-sm: 12px;
  /* Strong curves (animate skill tokens) — no invented beziers elsewhere. */
  --ease-out-curve: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out-curve: cubic-bezier(0.77, 0, 0.175, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);
}

* {
  box-sizing: border-box;
}

/* Several hidden-able elements carry display:flex — keep the attribute authoritative. */
[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  overflow-x: hidden;
}

body {
  min-height: 100dvh;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  -webkit-font-smoothing: antialiased;
}

#app {
  width: 100%;
  max-width: 480px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(env(safe-area-inset-top, 0px) + 20px) 20px calc(env(safe-area-inset-bottom, 0px) + 24px);
  position: relative;
}

/* --- Scene shell + typography --- */

.scene {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  flex: 1;
  justify-content: center;
  gap: 18px;
  position: relative;
  animation: scene-in 420ms var(--ease-out-curve);
}

@keyframes scene-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.scene-header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  z-index: 2;
}

.eyebrow {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: var(--accent);
}

.scene-title {
  margin: 0;
  font-size: clamp(2rem, 8.4vw, 2.6rem);
  font-weight: 750;
  line-height: 1.05;              /* tight leading for display type */
  letter-spacing: -0.025em;       /* negative tracking as type grows */
  color: var(--text);
  max-width: 20ch;
  text-wrap: balance;
}

.scene-title-sm {
  font-size: clamp(1.45rem, 6vw, 1.8rem);
  letter-spacing: -0.015em;
}

.scene-subtitle {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--text-dim);
  max-width: 32ch;
  text-wrap: balance;             /* no orphan word on the last line */
}

.quiet-status {
  margin: 0;
  min-height: 1.5em;
  font-size: 0.85rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  transition: color 200ms ease;
}

/* --- Buttons --- */

button {
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  color: var(--text);
  border: none;
  background: none;
  padding: 0;
}

.btn-primary {
  padding: 15px 32px;
  border-radius: 999px;
  background: var(--surface);
  -webkit-backdrop-filter: blur(16px) saturate(160%);
  backdrop-filter: blur(16px) saturate(160%);
  border: 1px solid var(--hairline);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
  font-weight: 600;
  font-size: 1rem;
  letter-spacing: 0.01em;
  transition: transform 140ms var(--ease-out-curve), background 200ms ease;
}

.btn-primary:active {
  transform: scale(0.97);
  background: var(--surface-strong);
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn-quiet {
  padding: 14px 18px; /* keeps the quiet link a ≥44px touch target */
  font-size: 0.85rem;
  color: var(--text-dim);
  text-decoration: underline;
  text-underline-offset: 3px;
  transition: transform 140ms var(--ease-out-curve), color 200ms ease;
}

.btn-quiet:active {
  transform: scale(0.97);
  color: var(--text);
}

/* ========================================================================
   Scene 1 — candles in the dark
   ======================================================================== */

.candle-scene {
  gap: 8px;
}

/* Faint warm pool of light behind the candles — the room isn't flat black. */
.candle-ambient {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(80% 46% at 50% 58%, rgba(255, 150, 60, 0.10), rgba(255, 150, 60, 0.03) 55%, transparent 78%);
  transition: opacity 1200ms var(--ease-out-curve);
}

.candle-scene.is-done .candle-ambient {
  opacity: 0;
}

.candle-scene .scene-header {
  margin-bottom: 4px;
  transition: opacity 700ms var(--ease-out-curve);
}

.candle-scene.is-done .scene-header,
.candle-scene.is-done .quiet-status {
  opacity: 0;
}

.candles-wrap {
  width: min(88vw, 390px);
  z-index: 1;
}

.candles-svg {
  width: 100%;
  height: auto;
  overflow: visible;
  display: block;
}

.candle-stick {
  fill: #272733;
  stroke: rgba(255, 255, 255, 0.07);
  stroke-width: 1;
}

.candle-wick {
  stroke: #4a4a58;
  stroke-width: 2;
  stroke-linecap: round;
}

/* Flicker: subtle, organic. Per-candle duration/delay offsets stop the five
   flames from breathing in unison. */
.flame-body {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  animation: flame-flicker 1.7s ease-in-out infinite alternate;
}

.flame-core {
  fill: #fff6da;
  opacity: 0.92;
}

.glow-outer,
.glow-mid {
  /* screen-blend so overlapping glows sum into one continuous pool of light */
  mix-blend-mode: screen;
  animation: glow-breathe 1.7s ease-in-out infinite alternate;
}

.candle:nth-child(1) .flame-body { animation-duration: 1.55s; animation-delay: -0.3s; }
.candle:nth-child(2) .flame-body { animation-duration: 1.9s;  animation-delay: -1.1s; }
.candle:nth-child(3) .flame-body { animation-duration: 1.65s; animation-delay: -0.7s; }
.candle:nth-child(4) .flame-body { animation-duration: 2.05s; animation-delay: -0.2s; }
.candle:nth-child(5) .flame-body { animation-duration: 1.75s; animation-delay: -1.5s; }
.candle:nth-child(1) .glow-outer, .candle:nth-child(1) .glow-mid { animation-duration: 1.55s; animation-delay: -0.3s; }
.candle:nth-child(2) .glow-outer, .candle:nth-child(2) .glow-mid { animation-duration: 1.9s;  animation-delay: -1.1s; }
.candle:nth-child(3) .glow-outer, .candle:nth-child(3) .glow-mid { animation-duration: 1.65s; animation-delay: -0.7s; }
.candle:nth-child(4) .glow-outer, .candle:nth-child(4) .glow-mid { animation-duration: 2.05s; animation-delay: -0.2s; }
.candle:nth-child(5) .glow-outer, .candle:nth-child(5) .glow-mid { animation-duration: 1.75s; animation-delay: -1.5s; }

@keyframes flame-flicker {
  from { transform: scaleY(0.97) scaleX(1.02) rotate(-1.4deg); }
  to   { transform: scaleY(1.06) scaleX(0.96) rotate(1.6deg); }
}

@keyframes glow-breathe {
  from { opacity: 0.78; }
  to   { opacity: 1; }
}

/* Blow detected: flames gutter hard for a beat before going out. */
.candle-scene.is-guttering .flame-body {
  animation: flame-gutter 120ms ease-in-out infinite alternate;
}

@keyframes flame-gutter {
  from { transform: scaleY(0.72) scaleX(1.15) rotate(-7deg); }
  to   { transform: scaleY(1.05) scaleX(0.85) rotate(6deg); }
}

/* Snuff: quick collapse (exit, ease-out), afterglow lingers then fades,
   thin smoke wisp rises. */
.flame-group,
.glow-group,
.smoke-group {
  transform-box: fill-box;
  transform-origin: 50% 100%;
}

.smoke-group {
  opacity: 0;
}

.candle.snuffed .flame-body {
  animation: none;
}

.candle.snuffed .flame-group {
  animation: flame-snuff 220ms var(--ease-out-curve) forwards;
}

.candle.snuffed .glow-outer,
.candle.snuffed .glow-mid {
  animation: afterglow-fade 1100ms var(--ease-out-curve) forwards;
}

.candle.snuffed .smoke-group {
  animation: smoke-rise 1600ms var(--ease-out-curve) forwards;
}

@keyframes flame-snuff {
  30% { transform: scale(0.85, 1.12); opacity: 1; }
  100% { transform: scale(0.5, 0.08); opacity: 0; }
}

@keyframes afterglow-fade {
  0% { opacity: 1; }
  25% { opacity: 0.55; }
  100% { opacity: 0; }
}

.smoke {
  fill: none;
  stroke: rgba(196, 196, 210, 0.42);
  stroke-width: 2.4;
  stroke-linecap: round;
}

@keyframes smoke-rise {
  0% { opacity: 0; transform: translateY(4px) scale(0.9); }
  18% { opacity: 0.85; }
  100% { opacity: 0; transform: translateY(-26px) scale(1.25, 1.4); }
}

.candle-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  z-index: 2;
}

/* Lights come on: warm bloom cross-fade into scene 2. Ease-out, no overshoot. */
.lights-up {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(120% 85% at 50% 34%, rgba(255, 205, 140, 0.30), rgba(255, 180, 92, 0.10) 45%, rgba(16, 16, 24, 0.9) 100%);
  transition: opacity 950ms var(--ease-out-curve);
  z-index: 5;
}

.lights-up.is-visible {
  opacity: 1;
}

/* ========================================================================
   Scene 2 — gacha capsule machine
   ======================================================================== */

.gacha-scene {
  gap: 12px;
}

.gacha-count {
  margin: 2px 0 0;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.gacha-stage {
  position: relative;
  width: min(80vw, 316px);
  aspect-ratio: 320 / 470;
  transition: opacity 450ms var(--ease-out-curve), filter 450ms var(--ease-out-curve);
  /* On an HTML element this reliably stops mobile Chrome from reclaiming the
     crank drag for scrolling (touch-action on inner SVG elements is not
     honored consistently) — without it the gesture dies in pointercancel. */
  touch-action: none;
}

.gacha-svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.gacha-led {
  fill: var(--accent);
  opacity: 0.9;
}

/* --- Crank --- */

.crank-hit {
  cursor: grab;
  touch-action: none;
}

.gacha-scene.is-cranking .crank-hit {
  cursor: grabbing;
}

.crank-knob {
  fill: var(--accent);
}

.crank-rot {
  transform-box: fill-box;
  transform-origin: center;
  will-change: transform;
}

/* Instant press acknowledgement on pointer-down. */
.gacha-scene.is-cranking .crank-knob {
  fill: var(--accent-deep);
}

/* --- Capsules in the dome --- */

.capsule-inner {
  transform-box: fill-box;
  transform-origin: center;
}

.gacha-scene.is-tumbling .capsule-inner {
  animation: capsule-jostle 460ms var(--ease-in-out-curve) infinite;
  animation-delay: var(--jd, 0ms);
}

@keyframes capsule-jostle {
  0%, 100% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(var(--jx, 4px), -7px) rotate(7deg); }
  55% { transform: translate(calc(var(--jx, 4px) * -0.6), -2px) rotate(-4deg); }
  80% { transform: translate(0, -8px) rotate(3deg); }
}

/* The whole machine buzzes, barely, while capsules churn. */
.gacha-scene.is-tumbling .gacha-svg {
  animation: machine-buzz 340ms linear infinite;
}

@keyframes machine-buzz {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(0.8px, -0.4px); }
  50% { transform: translate(-0.7px, 0.5px); }
  75% { transform: translate(0.5px, 0.6px); }
}

/* Chosen capsule sinks out of the pile toward the gate. */
.capsule-pos.is-dispensed .capsule-inner {
  animation: capsule-out 300ms var(--ease-out-curve) forwards;
}

@keyframes capsule-out {
  to { transform: translateY(30px) scale(0.5); opacity: 0; }
}

/* --- Tray capsule (HTML, positioned over the SVG tray) --- */

.tray-capsule {
  position: absolute;
  left: 50%;
  top: 83.5%;
  width: 19%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.5));
}

.tray-capsule-top,
.tray-capsule-bottom {
  display: block;
  width: 100%;
  height: 50%;
  transition: transform 380ms var(--ease-out-curve), opacity 380ms var(--ease-out-curve);
}

.tray-capsule-top {
  border-radius: 999px 999px 2px 2px;
  background: #f7a8c4;
  box-shadow: inset -4px 4px 6px rgba(255, 255, 255, 0.45), inset 2px -2px 5px rgba(10, 10, 16, 0.18);
}

.tray-capsule-bottom {
  border-radius: 2px 2px 999px 999px;
  background: #f4efe4;
  box-shadow: inset -3px -4px 6px rgba(10, 10, 16, 0.16), inset 2px 2px 4px rgba(255, 255, 255, 0.5);
}

/* Drop with a satisfying bounce. Fall segments accelerate (gravity),
   rebounds decelerate — timing functions set per keyframe. */
.tray-capsule.is-dropping {
  animation: capsule-drop 640ms both;
}

@keyframes capsule-drop {
  0%   { transform: translate(-50%, -215%); animation-timing-function: cubic-bezier(0.32, 0, 0.67, 0); }
  46%  { transform: translate(-50%, -50%);  animation-timing-function: cubic-bezier(0.33, 1, 0.68, 1); }
  64%  { transform: translate(-50%, -76%);  animation-timing-function: cubic-bezier(0.32, 0, 0.67, 0); }
  78%  { transform: translate(-50%, -50%);  animation-timing-function: cubic-bezier(0.33, 1, 0.68, 1); }
  88%  { transform: translate(-50%, -57%);  animation-timing-function: cubic-bezier(0.32, 0, 0.67, 0); }
  100% { transform: translate(-50%, -50%); }
}

/* Waiting to be tapped: a slow, quiet invitation. */
.tray-capsule.is-waiting {
  animation: capsule-invite 2.2s ease-in-out infinite;
}

@keyframes capsule-invite {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.05); }
}

.tray-capsule:active {
  transform: translate(-50%, -50%) scale(0.94);
  animation: none;
}

/* Crack open: the two halves spring apart. */
.tray-capsule.is-cracked {
  animation: none;
}

.tray-capsule.is-cracked .tray-capsule-top {
  transform: translateY(-26px) rotate(-22deg);
  opacity: 0;
}

.tray-capsule.is-cracked .tray-capsule-bottom {
  transform: translateY(12px) rotate(14deg);
  opacity: 0;
}

/* --- Reveal layer --- */

.reveal-layer {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 32px;
  text-align: center;
  /* Safety net: a long clue on a short landscape screen scrolls rather than
     centering itself out of reach at both ends. */
  overflow-y: auto;
  overscroll-behavior: contain;
  background: rgba(8, 8, 13, 0.72);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  backdrop-filter: blur(14px) saturate(140%);
  opacity: 0;
  transition: opacity 240ms var(--ease-out-curve);
}

.reveal-layer.is-open {
  opacity: 1;
}

.reveal-layer > * {
  transform: scale(0.95);
  opacity: 0;
  transition: transform 300ms var(--ease-out-curve), opacity 300ms var(--ease-out-curve);
}

.reveal-layer.is-open > * {
  transform: scale(1);
  opacity: 1;
}

.reveal-layer.is-open > .reveal-eyebrow { transition-delay: 40ms; }
.reveal-layer.is-open > .reveal-clue { transition-delay: 90ms; }
.reveal-layer.is-open > .reveal-continue { transition-delay: 180ms; }

/* The clue is a sentence, not a name: read-me type, not shout-me type.
   `pretty` (not `balance`) — balance is a heading tool browsers stop
   applying past ~6 lines, and these run long. */
.reveal-clue {
  margin: 0;
  font-size: clamp(1.3rem, 2.6vw, 1.7rem);
  font-weight: 600;
  line-height: 1.4;
  letter-spacing: -0.005em;
  max-width: 30ch;
  text-wrap: pretty;
}

.reveal-continue {
  margin-top: 14px;
  /* The label is a full sentence — let it wrap as two tidy centered lines
     instead of forcing one ultra-wide pill. */
  max-width: 30ch;
  font-size: 0.95rem;
  line-height: 1.35;
  text-align: center;
  text-wrap: balance;
}

/* Full-screen confetti: paper pieces burst up from the reveal point, then
   flutter down and off the bottom, rotating as they fall. */
.reveal-burst {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  top: 42%;
  left: 50%;
  border-radius: 2px;
  opacity: 0;
  will-change: transform, opacity;
}

.reveal-layer.is-open .confetti-piece {
  animation: confetti-fly var(--dur, 2200ms) forwards;
  animation-delay: var(--dl, 0ms);
}

@keyframes confetti-fly {
  0% {
    opacity: 0;
    transform: translate(0, 0) rotate(0deg);
    animation-timing-function: cubic-bezier(0.17, 0.84, 0.44, 1); /* fast launch */
  }
  8% { opacity: 1; }
  30% {
    transform: translate(calc(var(--dx) * 0.7), calc(-1 * var(--peak))) rotate(calc(var(--rot) * 0.35));
    animation-timing-function: cubic-bezier(0.45, 0.05, 0.55, 0.95); /* gravity takes over */
  }
  90% { opacity: 1; }
  100% {
    opacity: 0;
    transform: translate(var(--dx), var(--fall)) rotate(var(--rot));
  }
}

/* ========================================================================
   Finale / error / debug
   ======================================================================== */

.finale-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(90% 60% at 50% 36%, rgba(255, 180, 92, 0.14), rgba(255, 180, 92, 0.03) 55%, transparent 80%);
}

.finale-scene .scene-title,
.finale-scene .scene-subtitle,
.finale-scene .eyebrow {
  z-index: 1;
}

.error-scene {
  background: var(--bg-raised);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-md);
  padding: 24px;
  flex: 0;
  margin: auto 0;
}

.error-scene h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: -0.01em;
}

.error-details {
  text-align: left;
  white-space: pre-wrap;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 0.8rem;
  max-width: 100%;
  overflow-x: auto;
  color: var(--text-dim);
}

.error-hint {
  color: var(--text-dim);
  font-size: 0.85rem;
}

#mic-debug-overlay {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 45vh;
  overflow-y: auto;
  background: rgba(10, 10, 16, 0.92);
  border-top: 1px solid var(--hairline);
  color: #d7ffd7;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.7rem;
  padding: 8px 10px;
  z-index: 999;
}

#mic-debug-overlay .debug-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

#mic-debug-overlay .debug-log {
  margin-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  padding-top: 4px;
  color: #ffe08a;
}

/* --- Landscape degrade gracefully --- */

@media (orientation: landscape) and (max-height: 500px) {
  #app {
    justify-content: flex-start;
  }
  .candles-wrap {
    width: min(60vh, 320px);
  }
  .gacha-stage {
    width: min(46vh, 260px);
  }
}

/* --- Reduced motion: principled cross-fades, not a nuke-everything rule.
   Movement-based animation is removed or replaced with fades; opacity
   transitions that aid comprehension are kept at their normal pace. --- */

@media (prefers-reduced-motion: reduce) {
  .scene {
    animation: scene-fade 300ms ease;
  }

  @keyframes scene-fade {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  /* Candles: steady flames, fade-only snuff, no gutter shake. */
  .flame-body,
  .candle-scene.is-guttering .flame-body {
    animation: none;
  }
  .glow-outer, .glow-mid {
    animation: none;
    opacity: 0.9;
  }
  .candle.snuffed .flame-group {
    animation: none;
    opacity: 0;
    transition: opacity 300ms ease;
  }
  .candle.snuffed .smoke-group {
    animation: smoke-fade 1200ms ease forwards;
  }
  @keyframes smoke-fade {
    0% { opacity: 0; }
    25% { opacity: 0.6; }
    100% { opacity: 0; }
  }

  /* Gacha: no jostle/buzz/drop travel — capsules and reveal cross-fade. */
  .gacha-scene.is-tumbling .capsule-inner,
  .gacha-scene.is-tumbling .gacha-svg,
  .tray-capsule.is-waiting {
    animation: none;
  }
  .capsule-pos.is-dispensed .capsule-inner {
    animation: none;
    opacity: 0;
    transition: opacity 250ms ease;
  }
  .tray-capsule.is-dropping {
    animation: tray-fade 250ms ease both;
  }
  @keyframes tray-fade {
    from { opacity: 0; }
    to { opacity: 1; }
  }
  .tray-capsule.is-cracked .tray-capsule-top,
  .tray-capsule.is-cracked .tray-capsule-bottom {
    transform: none;
    opacity: 0;
  }
  .reveal-layer > * {
    transform: none;
  }
  .reveal-layer.is-open .confetti-piece {
    animation: none;
  }
}
