/* Encounter rewrite: one CSS-background scene, one fixed camera overlay. */

.phase5-encounter {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(260px, 48dvh) minmax(0, 1fr);
  gap: 8px;
  width: min(560px, 100%);
  margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) 8px calc(8px + env(safe-area-inset-bottom));
  overflow: hidden;
  background: #17100d;
  color: #fff5d6;
}

.encounter-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.encounter-topbar > * {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  background: rgba(16, 17, 20, 0.86);
  color: #fff5d6;
  border: 2px solid rgba(255, 245, 214, 0.5);
  font-size: 0.78rem;
  font-weight: 1000;
  text-transform: uppercase;
}

.encounter-scene {
  position: relative;
  width: 100%;
  height: clamp(260px, 48dvh, 480px);
  overflow: hidden;
  isolation: isolate;
  border: 3px solid #fff5d6;
  border-radius: 14px;
  background-color: #2c3320;
  background-image:
    linear-gradient(180deg, rgba(16, 17, 20, 0.08), rgba(16, 17, 20, 0.42)),
    var(--encounter-scene-image, radial-gradient(circle at 42% 28%, rgba(244, 197, 66, 0.24), transparent 28%), linear-gradient(180deg, #416747, #2c3320 62%, #17100d));
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  box-shadow: 0 8px 0 #080605, 0 22px 46px rgba(0, 0, 0, 0.36);
}

.encounter-scene::after {
  content: "";
  position: absolute;
  z-index: 1;
  left: 50%;
  bottom: 5%;
  width: min(190px, 38%);
  height: 7%;
  border-radius: 50%;
  background: rgba(7, 5, 3, 0.36);
  filter: blur(8px);
  transform: translateX(-50%);
}

.encounter-fuggler {
  position: absolute;
  z-index: 2;
  left: 50%;
  bottom: 6%;
  width: auto;
  height: auto;
  max-width: 55%;
  max-height: 75%;
  object-fit: contain;
  filter: drop-shadow(0 10px 8px rgba(0, 0, 0, 0.34));
  transform: translateX(-50%);
}

.encounter-fuggler--fallback {
  display: grid;
  place-items: center;
  width: clamp(118px, 38vw, 178px);
  aspect-ratio: 1;
  border-radius: 22px;
  background: #f4c542;
  color: #17100d;
  font-size: 3rem;
  font-weight: 1000;
}

.encounter-expired-message,
.cam-expired-message {
  position: absolute;
  z-index: 8;
  left: 50%;
  bottom: 10%;
  width: min(310px, calc(100% - 28px));
  display: grid;
  gap: 4px;
  justify-items: center;
  padding: 12px 14px;
  border: 3px solid #17100d;
  border-radius: 14px;
  background: rgba(255, 245, 214, 0.94);
  color: #17100d;
  text-align: center;
  box-shadow: 0 8px 0 rgba(8, 6, 5, 0.76), 0 18px 34px rgba(0, 0, 0, 0.36);
  transform: translateX(-50%);
}

.cam-expired-message {
  z-index: 70;
  bottom: calc(132px + env(safe-area-inset-bottom));
  border-color: rgba(255, 245, 214, 0.9);
  background: rgba(16, 17, 20, 0.9);
  color: #fff5d6;
  backdrop-filter: blur(10px);
}

.encounter-expired-message[hidden],
.cam-expired-message[hidden] {
  display: none;
}

.encounter-expired-message strong,
.cam-expired-message strong {
  font-size: 1.12rem;
  line-height: 1;
  text-transform: uppercase;
}

.encounter-expired-message span,
.cam-expired-message span {
  font-size: 0.88rem;
  font-weight: 900;
}

.phase5-encounter.is-wandering-off .encounter-fuggler {
  pointer-events: none;
  animation: encounter-fuggler-wander-off 760ms ease-in both;
}

.cam-screen.is-wandering-off .cam-scene__fuggler {
  pointer-events: none;
  animation: camera-fuggler-wander-off 760ms ease-in both;
}

.phase5-encounter.is-wandering-off .encounter-scene::after,
.cam-screen.is-wandering-off .cam-scene__shadow {
  opacity: 0;
  transition: opacity 460ms ease;
}

.encounter-controls {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 8px;
  padding: 10px;
  border-radius: 14px 14px 8px 8px;
  background: var(--ff-panel);
  color: #17100d;
  border: 3px solid #17100d;
  box-shadow: 0 -3px 0 #f4c542 inset;
}

.encounter-copy {
  display: grid;
  gap: 3px;
}

.encounter-copy .rarity,
.cam-riddle .rarity {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 1000;
  text-transform: uppercase;
}

.encounter-copy .rarity {
  color: #7a4c00;
}

.encounter-copy h1 {
  margin: 0;
  font-size: clamp(1.05rem, 5.6vw, 1.5rem);
  line-height: 1;
}

.encounter-copy p {
  margin: 0;
  line-height: 1.25;
}

.encounter-controls blockquote {
  margin: 0;
  padding: 11px;
  border-radius: 10px;
  background: #17100d;
  color: #fff5d6;
  font-size: clamp(1rem, 5vw, 1.26rem);
  font-weight: 950;
  line-height: 1.14;
}

.encounter-controls .stack,
.cam-riddle .stack {
  gap: 8px;
}

.encounter-controls input,
.cam-riddle input {
  min-height: 44px;
  font-size: 16px;
  font-weight: 900;
}

.encounter-controls input {
  border: 3px solid #17100d;
  background: #fffdf0;
}

.encounter-tools {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.encounter-tools form,
.encounter-tools button {
  width: 100%;
}

.cam-screen {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  z-index: 1002;
  overflow: hidden;
  isolation: isolate;
  background: #000;
  color: #fff5d6;
}

.cam-screen-body {
  overflow: hidden;
}

.cam-scene {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  isolation: isolate;
  background: #000;
}

.cam-scene__shadow {
  content: "";
  position: absolute;
  z-index: 10;
  left: 50%;
  top: calc(48% + min(20vw, 105px));
  width: min(150px, 34vw);
  height: 22px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.42);
  filter: blur(8px);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.cam-scene::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 30;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.48), transparent 28%, transparent 54%, rgba(0, 0, 0, 0.82));
  pointer-events: none;
}

.cam-screen[data-camera-state="READY_TO_REQUEST"] .cam-scene__video,
.cam-screen[data-camera-state="REQUESTING"] .cam-scene__video,
.cam-screen[data-camera-state="STARTING_VIDEO"] .cam-scene__video,
.cam-screen[data-camera-state="READY_TO_PLAY"] .cam-scene__video,
.cam-screen[data-camera-state="DENIED"] .cam-scene__video,
.cam-screen[data-camera-state="UNAVAILABLE"] .cam-scene__video,
.cam-screen[data-camera-state="ERROR"] .cam-scene__video {
  opacity: 0;
}

.cam-scene__video {
  position: absolute;
  z-index: 0;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: radial-gradient(circle at 50% 30%, #35453b, #050505 68%);
}

.cam-start {
  position: absolute;
  z-index: 60;
  left: 50%;
  top: 50%;
  width: min(340px, calc(100% - 28px));
  display: grid;
  gap: 12px;
  justify-items: stretch;
  padding: 18px;
  border-radius: 18px;
  border: 2px solid rgba(255, 245, 214, 0.72);
  background: rgba(16, 17, 20, 0.9);
  color: #fff5d6;
  text-align: center;
  backdrop-filter: blur(12px);
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.42);
  transform: translate(-50%, -50%);
}

.cam-start[hidden] {
  display: none;
}

.cam-start strong {
  font-size: clamp(1.4rem, 8vw, 2.1rem);
  line-height: 0.95;
  text-transform: uppercase;
}

.cam-start p {
  margin: 0;
  font-weight: 900;
}

.cam-scene__fuggler {
  position: absolute;
  z-index: 20;
  left: 50%;
  top: 48%;
  width: min(44vw, 270px);
  height: auto;
  max-height: 48dvh;
  object-fit: contain;
  filter: drop-shadow(0 12px 10px rgba(0, 0, 0, 0.38));
  transform: translate(-50%, -50%);
}

.cam-scene__fuggler--fallback {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border-radius: 22px;
  background: #f4c542;
  color: #17100d;
  font-size: 3rem;
  font-weight: 1000;
}

.cam-hud {
  position: absolute;
  z-index: 40;
  top: calc(10px + env(safe-area-inset-top));
  left: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  border-radius: 999px;
  background: rgba(16, 17, 20, 0.76);
  color: #fff5d6;
  border: 2px solid rgba(255, 245, 214, 0.7);
  backdrop-filter: blur(8px);
  font-weight: 1000;
  text-transform: uppercase;
}

.cam-hud > * {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
}

.cam-error,
.cam-riddle {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  display: grid;
  gap: 8px;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  background: rgba(16, 17, 20, 0.9);
  color: #fff5d6;
  backdrop-filter: blur(12px);
  border-top: 2px solid rgba(255, 245, 214, 0.72);
  box-shadow: 0 -18px 38px rgba(0, 0, 0, 0.34);
}

.cam-error[hidden] {
  display: none;
}

.cam-error strong {
  font-size: 1.2rem;
  text-transform: uppercase;
}

.cam-error p {
  margin: 0;
  font-weight: 800;
}

.cam-riddle {
  display: none;
  max-height: min(45dvh, 330px);
  overflow: auto;
}

.cam-screen[data-camera-state="ACTIVE"] .cam-riddle {
  display: grid;
}

.cam-riddle .rarity {
  color: #f4c542;
}

.cam-riddle blockquote {
  margin: 0;
  padding: 10px;
  border-radius: 10px;
  background: rgba(255, 245, 214, 0.96);
  color: #17100d;
  font-size: clamp(0.96rem, 4.8vw, 1.22rem);
  font-weight: 950;
  line-height: 1.12;
}

.cam-riddle label {
  color: #fff5d6;
}

.cam-riddle input {
  border: 3px solid #fff5d6;
  background: #fffdf0;
  color: #17100d;
}

.encounter-fuggler.fuggler-motion-twitch {
  animation: encounter-fuggler-twitch 380ms ease both;
}

.encounter-fuggler.fuggler-motion-wrong {
  animation: encounter-fuggler-wrong 500ms ease both;
}

.encounter-fuggler.fuggler-motion-excited,
.encounter-fuggler.fuggler-motion-caught {
  animation: encounter-fuggler-excited 420ms ease both;
}

.cam-scene__fuggler.fuggler-motion-twitch {
  animation: camera-fuggler-twitch 380ms ease both;
}

.cam-scene__fuggler.fuggler-motion-wrong {
  animation: camera-fuggler-wrong 500ms ease both;
}

.cam-scene__fuggler.fuggler-motion-excited,
.cam-scene__fuggler.fuggler-motion-caught {
  animation: camera-fuggler-excited 420ms ease both;
}

@keyframes encounter-fuggler-twitch {
  0%, 100% { transform: translateX(-50%) rotate(0) scale(1); }
  28% { transform: translateX(calc(-50% - 3px)) translateY(1px) rotate(-1.5deg) scale(1.01); }
  58% { transform: translateX(calc(-50% + 3px)) translateY(-2px) rotate(1deg) scale(0.995); }
}

@keyframes encounter-fuggler-wrong {
  0%, 100% { transform: translateX(-50%) rotate(0); }
  20% { transform: translateX(calc(-50% - 8px)) rotate(-2deg); }
  45% { transform: translateX(calc(-50% + 7px)) translateY(1px) rotate(2deg); }
  70% { transform: translateX(calc(-50% - 4px)) rotate(-1deg); }
}

@keyframes encounter-fuggler-excited {
  0%, 100% { transform: translateX(-50%) translateY(0) scale(1); }
  35% { transform: translateX(-50%) translateY(-8px) scale(1.04); }
  68% { transform: translateX(-50%) translateY(2px) scale(0.98); }
}

@keyframes encounter-fuggler-wander-off {
  0% { opacity: 1; transform: translateX(-50%) translateY(0) rotate(0) scale(1); }
  30% { opacity: 1; transform: translateX(calc(-50% + 16px)) translateY(-4px) rotate(3deg) scale(0.98); }
  100% { opacity: 0; transform: translateX(64vw) translateY(10px) rotate(11deg) scale(0.72); }
}

@keyframes camera-fuggler-twitch {
  0%, 100% { transform: translate(-50%, -50%) rotate(0) scale(1); }
  28% { transform: translate(calc(-50% - 3px), calc(-50% + 1px)) rotate(-1.5deg) scale(1.01); }
  58% { transform: translate(calc(-50% + 3px), calc(-50% - 2px)) rotate(1deg) scale(0.995); }
}

@keyframes camera-fuggler-wrong {
  0%, 100% { transform: translate(-50%, -50%) rotate(0); }
  20% { transform: translate(calc(-50% - 8px), -50%) rotate(-2deg); }
  45% { transform: translate(calc(-50% + 7px), calc(-50% + 1px)) rotate(2deg); }
  70% { transform: translate(calc(-50% - 4px), -50%) rotate(-1deg); }
}

@keyframes camera-fuggler-excited {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  35% { transform: translate(-50%, calc(-50% - 8px)) scale(1.04); }
  68% { transform: translate(-50%, calc(-50% + 2px)) scale(0.98); }
}

@keyframes camera-fuggler-wander-off {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(0) scale(1); }
  30% { opacity: 1; transform: translate(calc(-50% + 18px), calc(-50% - 4px)) rotate(3deg) scale(0.98); }
  100% { opacity: 0; transform: translate(62vw, calc(-50% + 12px)) rotate(11deg) scale(0.72); }
}

@media (prefers-reduced-motion: reduce) {
  .phase5-encounter.is-wandering-off .encounter-fuggler,
  .cam-screen.is-wandering-off .cam-scene__fuggler {
    animation-duration: 1ms;
    opacity: 0;
  }
}

@media (max-height: 620px), (max-width: 340px) {
  .phase5-encounter {
    grid-template-rows: auto minmax(220px, 42dvh) minmax(0, 1fr);
  }

  .encounter-scene {
    height: clamp(220px, 42dvh, 360px);
  }

  .encounter-fuggler {
    max-width: 52%;
    max-height: 70%;
  }

  .cam-scene__fuggler {
    width: min(38vw, 190px);
    max-height: 38dvh;
  }

  .cam-riddle {
    max-height: 48dvh;
  }
}
