* {
  box-sizing: border-box;
  margin: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #0b1412;
  color: #f4ead2;
  font-family: 'Fredoka', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  touch-action: none;
}

[hidden] { display: none !important; }

button {
  appearance: none;
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  font: inherit;
  touch-action: none;
}

img { display: block; width: 100%; height: auto; pointer-events: none; }

/* --- Gate ------------------------------------------------------------- */
#gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: #0b1412;
}

/* --- Rig: sized by game.js to the phone's portrait dimensions ----------- */
#rig {
  position: fixed;
  left: 50%;
  top: 50%;
  width: var(--rig-w, 100vw);
  height: var(--rig-h, 100vh);
  transform: translate(-50%, -50%) rotate(var(--rig-rot, 0deg));
  overflow: hidden;
  background: #0b1412 url('assets/background.webp') center / cover no-repeat;
}

#title {
  position: absolute;
  left: 16px;
  right: 16px;
  top: calc(env(safe-area-inset-top, 0px) + 3.2%);
  text-align: center;
  font-size: clamp(22px, calc(var(--rig-w, 390px) * 0.072), 32px);
  font-weight: 600;
  line-height: 1.15;
  text-shadow: 0 2px 10px rgb(0 0 0 / 70%);
  transition: opacity .5s;
  z-index: 21;
  pointer-events: none;
}
#title.gone { opacity: 0; }

/* --- Safe ------------------------------------------------------------- */
#safe {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 2.6%);
  width: var(--safe-w, 88vw);
  aspect-ratio: 900 / 1346;
  transform: translateX(-50%);
  perspective: 1500px;
  perspective-origin: 50% 50%;
  filter: drop-shadow(0 18px 22px rgb(0 0 0 / 55%));
}

#safe.rattle { animation: rattle .42s linear; }
@keyframes rattle {
  20% { transform: translateX(-50%) translate(-2px, 1px) rotate(-.35deg); }
  45% { transform: translateX(-50%) translate(2px, -1px) rotate(.35deg); }
  70% { transform: translateX(-50%) translate(-1px, 0) rotate(-.2deg); }
}

#body { position: absolute; inset: 0; }

/* --- Stethoscope hidden behind the safe: just the tips show ------------- */
#stethoHidden {
  position: absolute;
  left: 72%;
  bottom: 100%;
  width: 13%;
  transform: translateY(85%) rotate(10deg);
  transform-origin: 50% 100%;
  filter: brightness(.68) saturate(.75);
  transition: transform .3s ease-in, opacity .3s;
}
/* The visible part is tiny: give the fingertip more room than the eye gets. */
#stethoHidden::before {
  content: '';
  position: absolute;
  left: -70%;
  right: -70%;
  top: -18%;
  height: 40%;
}
#stethoHidden.found {
  transform: translateY(100%) rotate(10deg);
  opacity: 0;
  pointer-events: none;
}

/* --- Inside the safe ------------------------------------------------------ */
#cavity {
  position: absolute;
  left: 11%;
  top: 9%;
  width: 78%;
  height: 81%;
  overflow: hidden;
  border-radius: 9% / 6.5%;
  pointer-events: none;
}

#sheet {
  position: absolute;
  left: 2.5%;
  top: 4%;
  width: 95%;
  aspect-ratio: 900 / 1359;
  transform: translateY(-115%) rotate(-8deg);
  visibility: hidden;
  filter: drop-shadow(0 8px 10px rgb(0 0 0 / 65%));
}
#sheet.down {
  visibility: visible;
  transform: rotate(-1.5deg);
  animation: sheetFall .95s cubic-bezier(.35, .05, .5, 1);
}
@keyframes sheetFall {
  0% { transform: translateY(-115%) rotate(-10deg); }
  55% { transform: translateY(3%) rotate(3deg); }
  74% { transform: translateY(-2.5%) rotate(-3deg); }
  88% { transform: translateY(.8%) rotate(-.6deg); }
  100% { transform: rotate(-1.5deg); }
}

#rub {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  mix-blend-mode: multiply;
}

#pencil {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  width: 9.5%;
  aspect-ratio: 220 / 1228;
  transform-origin: 50% 100%;
  filter: drop-shadow(3px 6px 4px rgb(0 0 0 / 60%));
}
body.open #pencil { z-index: 5; }
#pencil.returning { transition: transform .4s cubic-bezier(.3, .8, .3, 1); }

/* --- Door -------------------------------------------------------------- */
#door {
  position: absolute;
  z-index: 2;
  left: 0;
  top: 0;
  width: 100%;
  aspect-ratio: 900 / 1351;
  transform-origin: 99% 50%;
  transform: rotateY(var(--door, 0deg));
  transform-style: preserve-3d;
  transition: transform .6s cubic-bezier(.3, .8, .3, 1);
  touch-action: none;
}
#door.dragging { transition: none; }

#doorFront, #doorBack {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
#doorBack { transform: rotateY(180deg); filter: brightness(.22) saturate(.3); }

/* --- Maker's plate with the school's logo ---------------------------------- */
#plate {
  position: absolute;
  left: 27%;
  width: 46%;
  top: 4.2%;
  filter: drop-shadow(0 3px 3px rgb(0 0 0 / 55%));
}

/* --- Dial --------------------------------------------------------------- */
#pointer {
  position: absolute;
  left: 50%;
  top: calc(21% - 16px);
  width: 0;
  height: 0;
  transform: translateX(-50%);
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-top: 13px solid #e9c36f;
  filter: drop-shadow(0 2px 2px rgb(0 0 0 / 70%));
}

#dial {
  position: absolute;
  left: 19%;
  width: 62%;
  top: 21%;
  aspect-ratio: 1;
  border-radius: 50%;
  box-shadow: 0 8px 16px rgb(0 0 0 / 60%), 0 2px 4px rgb(0 0 0 / 70%);
  touch-action: none;
}

/* Quick enough to feel like a click into each notch, without looking like a jump. */
#dialSpin { position: absolute; inset: 0; will-change: transform; transition: transform 45ms ease-out; }
#ring { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#ring line { stroke: #efd9a0; stroke-linecap: round; }
#ring text {
  fill: #f6e6bd;
  font: 600 12px 'Fredoka', sans-serif;
  text-anchor: middle;
  dominant-baseline: central;
  paint-order: stroke;
  stroke: rgb(0 0 0 / 55%);
  stroke-width: 1.4px;
}

/* Static light, so the highlight doesn't spin with the sprite. */
#dialGlare {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  background:
    radial-gradient(circle at 34% 26%, rgb(255 244 214 / 34%), transparent 42%),
    radial-gradient(circle at 70% 82%, rgb(0 0 0 / 34%), transparent 50%);
}

/* --- Handle ------------------------------------------------------------- */
#handleWrap {
  position: absolute;
  left: 32%;
  width: 36%;
  top: 66%;
  aspect-ratio: 1;
  filter: drop-shadow(0 6px 5px rgb(0 0 0 / 60%));
}
#handle { position: absolute; inset: 0; touch-action: none; will-change: transform; }
#handle.spring { transition: transform .35s cubic-bezier(.3, 1.6, .5, 1); }
/* All four numbers in: the handle gives a little by itself. */
#handle.kick { animation: handleKick .8s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes handleKick {
  35% { transform: rotate(16deg); }
}
body.unlocked #handleWrap { animation: handleCall 1.5s ease-in-out 2; }
@keyframes handleCall {
  50% { filter: drop-shadow(0 0 14px rgb(255 214 130 / 90%)); }
}

/* --- Stethoscope once found: hanging on the door -------------------------- */
#stetho {
  position: absolute;
  left: 7.5%;
  top: 62%;
  width: 13%;
  transform-origin: 50% 3%;
  filter: drop-shadow(2px 5px 3px rgb(0 0 0 / 55%));
  animation: stethoIn .5s cubic-bezier(.3, 1.4, .5, 1);
}
@keyframes stethoIn { from { opacity: 0; transform: translateY(-34px) rotate(-8deg); } }
#stetho.swing { animation: swing 1.4s ease-in-out; }
@keyframes swing {
  15% { transform: rotate(7deg); }
  35% { transform: rotate(-5.5deg); }
  55% { transform: rotate(3.5deg); }
  75% { transform: rotate(-2deg); }
}

/* --- The ending ------------------------------------------------------------ */
#reveal {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5%;
  padding: 24px 16px;
  text-align: center;
  background: rgb(4 8 7 / 90%);
  animation: revealIn .5s ease-out;
}
@keyframes revealIn { from { opacity: 0; } }

#revealTitle {
  font-size: clamp(34px, calc(var(--rig-w, 390px) * 0.11), 48px);
  font-weight: 600;
  line-height: 1.1;
  color: #ffd977;
}
#revealQuestion {
  max-width: 17em;
  margin: .4em auto 0;
  font-size: clamp(20px, calc(var(--rig-w, 390px) * 0.058), 26px);
  font-weight: 500;
  line-height: 1.25;
  color: #f6e6bd;
}

#bigCode {
  width: 100%;
  aspect-ratio: 1000 / 340;
  filter: drop-shadow(0 0 16px rgb(255 216 140 / 28%));
  animation: codeIn .6s cubic-bezier(.2, .9, .3, 1.15);
}
@keyframes codeIn { from { transform: scale(.55); opacity: 0; } }

#revealWho {
  font-size: clamp(20px, calc(var(--rig-w, 390px) * 0.06), 28px);
  font-weight: 500;
  color: #e9c36f;
}

/* Comes in on its own when they stay stuck on the number. */
#revealHint {
  max-width: 17em;
  font-size: clamp(20px, calc(var(--rig-w, 390px) * 0.058), 26px);
  font-weight: 600;
  line-height: 1.25;
  color: #e9c36f;
  animation: hintIn .8s ease-out;
}
@keyframes hintIn { from { opacity: 0; transform: translateY(12px); } }

#revealYes {
  font-size: clamp(34px, calc(var(--rig-w, 390px) * 0.11), 48px);
  font-weight: 600;
  color: #fff3d2;
}

/* Solved: they hold the phone upside down, so the words are turned to face them. */
#reveal.solved #revealTop { visibility: hidden; }
#reveal.solved #revealWin { transform: rotate(180deg); }
#reveal.solved #revealWin p { animation: winIn .7s cubic-bezier(.2, .9, .3, 1.3) both; }
#reveal.solved #revealWho { animation-delay: .25s; }
@keyframes winIn { from { opacity: 0; transform: scale(.6); } }
#reveal.solved #bigCode { animation: codeGlow 1.2s ease-in-out infinite alternate; }
@keyframes codeGlow {
  from { filter: drop-shadow(0 0 14px rgb(255 216 140 / 35%)); }
  to { filter: drop-shadow(0 0 34px rgb(255 216 140 / 85%)); }
}

/* Falls towards the floor, which is the top of the rig once it's upside down. */
#confetti {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  transform: rotate(180deg);
}
#confetti i {
  position: absolute;
  top: -4%;
  left: var(--x);
  width: 9px;
  height: 15px;
  border-radius: 2px;
  background: var(--c);
  animation: confettiFall var(--t) cubic-bezier(.25, .5, .55, 1) var(--d) both;
}
@keyframes confettiFall {
  to { top: 104%; transform: translateX(var(--s)) rotate(var(--r)); }
}

/* --- Start ------------------------------------------------------------------ */
#start {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
  justify-content: center;
  padding-inline: 24px;
  background: rgb(3 6 5 / 72%);
  transition: opacity .7s;
}
#start.gone { opacity: 0; pointer-events: none; }

#startLabel {
  padding: 13px 38px;
  border-radius: 999px;
  font-size: 21px;
  font-weight: 600;
  color: #2a1d06;
  background: linear-gradient(#f3d184, #c5933f);
  box-shadow: 0 6px 18px rgb(0 0 0 / 55%), inset 0 1px 0 rgb(255 255 255 / 50%);
}
#start:active #startLabel { transform: translateY(1px); }

#startLogo {
  width: min(72%, 300px);
  filter: drop-shadow(0 4px 10px rgb(0 0 0 / 70%));
}

#startNote {
  max-width: 19em;
  font-size: 17px;
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  text-shadow: 0 1px 6px rgb(0 0 0 / 90%);
}

/* Interaction is off until the game has started. */
body:not(.playing) #door { pointer-events: none; }

/* --- Dev HUD (only with ?dev) ------------------------------------------- */
#hud {
  position: absolute;
  left: 6px;
  top: 6px;
  z-index: 40;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgb(0 0 0 / 70%);
  font: 12px ui-monospace, monospace;
  white-space: pre;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  #door { transition-duration: .2s; }
  #stetho.swing, #safe.rattle { animation-iteration-count: 1; }
}
