/* Heading font: put the Whirly Wood file in assets/fonts/ (any of these names/formats works) */
@font-face {
  font-family: "Whirly Wood";
  src: url("../assets/fonts/WhirlyWood.woff2") format("woff2"),
       url("../assets/fonts/WhirlyWood.woff") format("woff"),
       url("../assets/fonts/WhirlyWood.ttf") format("truetype"),
       url("../assets/fonts/WhirlyWood.otf") format("opentype");
  font-display: swap;
}

:root {
  --font: "Cormorant Garamond", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-heading: "Whirly Wood", var(--font);

  --ink: #23211d;
  --ink-soft: #5a4d48;
  --paper: rgba(253, 253, 252, 0.62);
  --line: rgba(35, 33, 29, 0.35);

  --smoke-ink: #e8e3d8;
  --smoke-soft: #a9a399;
  --smoke-paper: rgba(12, 12, 14, 0.45);
  --smoke-line: rgba(232, 227, 216, 0.28);

  --glow: #ffedcd;
  --glow-soft: rgba(255, 196, 102, 0.55);
  --fail: #8e2f24;

  --fade: 700ms;

  --pad-t: max(16px, env(safe-area-inset-top));
  --pad-r: max(16px, env(safe-area-inset-right));
  --pad-b: max(20px, env(safe-area-inset-bottom));
  --pad-l: max(16px, env(safe-area-inset-left));
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  font-size: 18px;
  color: var(--ink);
  background: #141517;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

img { display: block; max-width: 100%; user-select: none; -webkit-user-select: none; }

:focus-visible { outline: 2px solid currentColor; outline-offset: 3px; }
.screen:focus { outline: none; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ================= Fog background ================= */

.fog {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.fog__set {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity 1.4s ease;
}
body[data-fog="smoke"] .fog__set--smoke,
body[data-fog="pale"] .fog__set--pale { opacity: 1; }

.fog__set--smoke { background: radial-gradient(ellipse at 50% 110%, #2c2d31 0%, #151618 55%, #0c0c0e 100%); }
/* Pale = opalescent pastel cloud: lilac-grey base with rose, peach and periwinkle patches */
.fog__set--pale  { background: radial-gradient(ellipse at 50% 45%, #ddd8e2 0%, #c8bfcc 50%, #a99cad 100%); }
.fog__set--pale::after {
  /* faint cloudy mottling over the colour patches */
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='600' height='600'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.006 0.012' numOctaves='4' seed='7'/%3E%3CfeColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 1.4 -0.45'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: cover;
  opacity: 0.35;
  mix-blend-mode: soft-light;
}

.fog__layer {
  position: absolute;
  inset: -50%;
  will-change: transform;
}

.fog__set--smoke .fog__layer--1 {
  background:
    radial-gradient(28% 18% at 30% 45%, rgba(170, 170, 180, 0.16), transparent 70%),
    radial-gradient(24% 16% at 68% 58%, rgba(150, 150, 160, 0.14), transparent 70%);
  animation: drift-a 48s ease-in-out infinite alternate;
}
.fog__set--smoke .fog__layer--2 {
  background:
    radial-gradient(30% 14% at 55% 70%, rgba(190, 188, 180, 0.12), transparent 70%),
    radial-gradient(20% 22% at 38% 30%, rgba(160, 160, 170, 0.10), transparent 70%);
  animation: drift-b 70s ease-in-out infinite alternate;
}
.fog__set--smoke .fog__layer--3 {
  background:
    radial-gradient(40% 12% at 50% 62%, rgba(210, 205, 195, 0.09), transparent 70%),
    radial-gradient(18% 14% at 72% 36%, rgba(180, 180, 190, 0.10), transparent 70%);
  animation: drift-c 38s ease-in-out infinite alternate;
}

.fog__set--pale .fog__layer--1 {
  background:
    radial-gradient(22% 16% at 30% 38%, rgba(228, 168, 176, 0.55), transparent 70%),
    radial-gradient(20% 14% at 68% 34%, rgba(236, 196, 172, 0.5), transparent 70%),
    radial-gradient(18% 12% at 40% 68%, rgba(222, 160, 170, 0.4), transparent 70%);
  animation: drift-a 52s ease-in-out infinite alternate;
}
.fog__set--pale .fog__layer--2 {
  background:
    radial-gradient(24% 14% at 62% 56%, rgba(168, 174, 214, 0.5), transparent 70%),
    radial-gradient(18% 18% at 32% 54%, rgba(188, 168, 208, 0.45), transparent 70%),
    radial-gradient(16% 10% at 58% 26%, rgba(176, 186, 220, 0.4), transparent 70%);
  animation: drift-b 74s ease-in-out infinite alternate;
}
.fog__set--pale .fog__layer--3 {
  background:
    radial-gradient(16% 12% at 47% 50%, rgba(230, 238, 250, 0.65), transparent 70%),
    radial-gradient(14% 10% at 72% 70%, rgba(214, 150, 160, 0.4), transparent 70%),
    radial-gradient(12% 9% at 26% 28%, rgba(238, 214, 200, 0.45), transparent 70%);
  animation: drift-c 40s ease-in-out infinite alternate;
}

@keyframes drift-a {
  from { transform: translate3d(-8%, -3%, 0) rotate(0deg); }
  to   { transform: translate3d(7%, 4%, 0) rotate(8deg); }
}
@keyframes drift-b {
  from { transform: translate3d(6%, 3%, 0) rotate(0deg) scale(1.05); }
  to   { transform: translate3d(-7%, -4%, 0) rotate(-10deg) scale(1); }
}
@keyframes drift-c {
  from { transform: translate3d(0, 5%, 0) scale(1); }
  to   { transform: translate3d(4%, -5%, 0) scale(1.12); }
}

/* ================= Screens ================= */

#app {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--pad-t) var(--pad-r) var(--pad-b) var(--pad-l);
  opacity: 0;
  transition: opacity var(--fade) ease;
}
.screen.is-active { opacity: 1; }
.screen[data-fog="smoke"] { color: var(--smoke-ink); }

/* Staggered entrance: lock first, then each key left to right */
.is-staged .reveal {
  opacity: 0;
  animation: reveal 1.1s ease forwards;
  animation-delay: var(--d, 0s);
}
@keyframes reveal {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}

.topbar {
  width: 100%;
  max-width: 40rem;
  min-height: 44px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  flex-shrink: 0;
  transition: opacity .4s ease, background-color .2s ease;
}
.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
@media (hover: hover) {
  .icon-btn:hover { background: rgba(127, 127, 127, 0.14); }
}

.btn {
  min-height: 44px;
  padding: 0.55rem 2rem;
  border: 1px solid currentColor;
  border-radius: 2px;
  font-size: 1.1rem;
  letter-spacing: 0.12em;
  text-transform: lowercase;
  transition: background-color .2s ease, color .2s ease;
}
@media (hover: hover) {
  .btn:hover { background: rgba(127, 127, 127, 0.16); }
}

.text-btn {
  min-height: 44px;
  padding: 0.5rem 1rem;
  letter-spacing: 0.18em;
  text-transform: lowercase;
  font-size: 1rem;
}

.prose p { margin: 0 0 1em; }
.prose p:last-child { margin-bottom: 0; }
.prose a {
  color: inherit;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
  transition: color .2s ease;
}
@media (hover: hover) {
  .prose a:hover { color: var(--glow); }
}

/* Scrollable text box (story, epilogue, info) */
.textbox {
  width: min(100%, 34rem);
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 1.75rem 1.5rem;
  border: 1px solid var(--line);
  background: var(--paper);
  font-size: 1.2rem;
  line-height: 1.6;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
}
[data-fog="smoke"] .textbox {
  border-color: var(--smoke-line);
  background: var(--smoke-paper);
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.4);
}
.textbox__title {
  margin: 0 0 1.1rem;
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: 1.1rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  text-align: center;
}

/* ================= Intro ================= */

/* Auto margins centre the text when it fits; when it doesn't, it scrolls from the top */
.screen--intro,
.screen--epilogue {
  gap: 2rem;
  text-align: center;
  cursor: pointer;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.intro__text {
  margin-top: auto;
  flex-shrink: 0;
  max-width: 30rem;
  font-size: clamp(1.05rem, min(5vw, 2.7dvh), 1.6rem);
  line-height: 1.45;
}
.intro__text p { margin-bottom: 0.75em; }
.intro__text p,
.intro__continue {
  opacity: 0;
  animation: fade-in 2s ease forwards;
  animation-delay: calc(var(--i) * 1.6s + 0.6s);
}
.intro__continue {
  margin-bottom: auto;
  flex-shrink: 0;
  color: var(--smoke-soft);
  animation: fade-in 1.4s ease forwards, pulse 3.2s ease-in-out infinite;
  animation-delay: calc(var(--i) * 1.6s + 1s), calc(var(--i) * 1.6s + 2.4s);
}
.show-all .intro__text p,
.show-all .intro__continue {
  animation-delay: 0s;
  animation-duration: .5s, 3.2s;
}

@keyframes fade-in { to { opacity: 1; } }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

/* ================= Main ================= */

.screen--main { justify-content: space-between; }

.lock-area {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lock { display: block; }

.lock__art {
  position: relative;
  display: block;
  height: min(52dvh, 100vw);
  aspect-ratio: 200 / 260;
  transition: filter 1.2s ease;
}
.lock__art img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.lock__art model-viewer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: transparent;
  --poster-color: transparent;
  --progress-bar-color: rgba(35, 33, 29, 0.35);
  --min-hotspot-opacity: 0; /* points round the back of the lock disappear */
}

.lock.is-shaking { animation: shake .55s ease; }
.lock.is-complete .lock__art { filter: drop-shadow(0 0 18px var(--glow-soft)); }
.lock.is-flaring .lock__art { animation: lock-flare 2s ease; }

@keyframes lock-flare {
  0%   { filter: drop-shadow(0 0 0 transparent); }
  40%  { filter: drop-shadow(0 0 40px var(--glow)) brightness(1.15); }
  100% { filter: drop-shadow(0 0 18px var(--glow-soft)); }
}

.lock__points { position: absolute; inset: 0; }
.lock__point {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: 6.5%;
  aspect-ratio: 1;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: #8f8a80;
  box-shadow: inset 0 0 0 1.5px #3a3731, inset 0 2px 3px rgba(0, 0, 0, 0.35);
  transition: background-color .8s ease, box-shadow .8s ease;
}
.lock__point.is-lit {
  background: var(--glow);
  box-shadow: inset 0 0 0 1.5px #8a5a14, 0 0 10px 3px var(--glow-soft), 0 0 24px 6px rgba(255, 196, 102, 0.3);
}
/* Points pinned to the 3D lock model (model-viewer positions these itself) */
model-viewer .lock__point {
  position: relative;
  left: auto;
  top: auto;
  display: block;
  width: clamp(12px, 2.6dvh, 22px);
  transform: none; /* model-viewer already centres hotspots on their point */
  pointer-events: none;
  transition: background-color .8s ease, box-shadow .8s ease, opacity .3s ease;
}

.lock__point.is-flaring { animation: point-flare 1.6s ease; }
@keyframes point-flare {
  0%   { scale: 0.6; }
  35%  { scale: 1.9; box-shadow: 0 0 30px 12px var(--glow); }
  100% { scale: 1; }
}

.key-row {
  list-style: none;
  margin: 0;
  padding: 0 0 0.5rem;
  width: min(100%, 38rem);
  display: flex;
  justify-content: space-evenly;
  align-items: flex-end;
  flex-shrink: 0;
}

.key-btn {
  display: block;
  padding: 10px 8px;
  border-radius: 8px;
}
.key-btn img {
  height: clamp(96px, 19dvh, 190px);
  aspect-ratio: 80 / 240;
  object-fit: contain;
  transition: filter .6s ease, transform .3s ease;
}
.key-btn.is-solved img { filter: drop-shadow(0 0 8px var(--glow-soft)); }
@media (hover: hover) {
  .key-btn:hover img { transform: translateY(-4px); }
}

/* Gentle bobbing, shared by lock and keys */
.bob {
  display: block;
  animation: bob 4.5s ease-in-out infinite;
  animation-delay: var(--bob-delay, 0s);
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-9px); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0) rotate(0); }
  15% { transform: translateX(-10px) rotate(-3deg); }
  30% { transform: translateX(9px) rotate(2.5deg); }
  45% { transform: translateX(-7px) rotate(-1.5deg); }
  60% { transform: translateX(5px) rotate(1deg); }
  75% { transform: translateX(-3px); }
}

/* ================= Key view ================= */

.screen--key { text-align: center; }

.key__count {
  margin: 0;
  font-family: var(--font-heading);
  font-size: 1rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.clue {
  margin: 0.4rem 0 0;
  max-width: 24rem;
  min-height: 3em;
  font-style: italic;
  font-size: clamp(1.1rem, 4.4vw, 1.35rem);
  line-height: 1.35;
}

.key-stage {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 34rem;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.key-model {
  position: relative;
  flex: 1;
  align-self: stretch;
  min-height: 140px;
  perspective: 900px;
  touch-action: none;
  cursor: grab;
}
.key-model.is-dragging { cursor: grabbing; }

.key-spin {
  position: absolute;
  inset: 4% 0;
  transform-style: preserve-3d;
}
.key-spin img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: filter 1s ease;
}

.key-model model-viewer {
  position: absolute;
  top: 4%;
  left: 0;
  width: 100%;
  height: 92%;
  background: transparent;
  --poster-color: transparent;
  --progress-bar-color: rgba(35, 33, 29, 0.35);
  transition: filter 1s ease;
}

.key-halo {
  position: absolute;
  inset: 10% 5%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 214, 140, 0.75), rgba(255, 196, 102, 0.2) 55%, transparent);
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 1.2s ease, transform 1.4s ease;
}

.drag-hint {
  margin: 0;
  font-size: 0.8rem;
  letter-spacing: 0.2em;
  color: var(--ink-soft);
}

.status {
  margin: 0.5rem 0 0.4rem;
  min-height: 1.5em;
  font-size: 1.15rem;
  letter-spacing: 0.25em;
  text-transform: lowercase;
}
.status[data-state="ok"] { color: #7a5412; }
.status[data-state="fail"] { color: var(--fail); animation: fade-in .3s ease both; opacity: 0; }

.code-form { width: min(100%, 19rem); }

.code-field {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--ink);
  background: var(--paper);
  transition: border-color .3s ease, box-shadow .3s ease;
}
.code-field:focus-within { box-shadow: 0 0 0 3px rgba(35, 33, 29, 0.12); }
.code-field.is-shaking { animation: shake .5s ease; border-color: var(--fail); }

.code-field input {
  flex: 1;
  min-width: 0;
  padding: 0.6rem 0 0.6rem 44px; /* balances the submit button so text is centred */
  border: 0;
  background: transparent;
  color: var(--ink);
  font-family: var(--font);
  font-size: max(16px, 1.15rem); /* ≥16px stops iOS zooming on focus */
  letter-spacing: 0.14em;
  text-align: center;
}
.code-field input:focus { outline: none; }
.code-field input::placeholder { color: var(--ink-soft); font-style: italic; letter-spacing: 0.08em; }

.code-submit {
  width: 44px;
  display: grid;
  place-items: center;
}
.code-submit svg {
  width: 18px; height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Success: controls disappear, key glows */
.screen--key.is-success .fades {
  opacity: 0;
  pointer-events: none;
}
.screen--key.is-success .key-model { cursor: default; }
.screen--key.is-success .key-halo { opacity: 1; transform: scale(1); }
.screen--key.is-success .key-spin img,
.screen--key.is-success model-viewer {
  animation: key-glow 2.4s ease forwards;
}
@keyframes key-glow {
  0%   { filter: none; }
  45%  { filter: drop-shadow(0 0 22px var(--glow)) brightness(1.25) sepia(.4); }
  100% { filter: drop-shadow(0 0 14px var(--glow)) brightness(1.1) sepia(.3); }
}
.screen--key.is-solved .key-spin img,
.screen--key.is-solved model-viewer { filter: drop-shadow(0 0 10px var(--glow-soft)); }

/* ================= Story ================= */

.screen--story .textbox { margin-bottom: 0.5rem; }

/* ================= Final ================= */

.screen--final {
  justify-content: center;
  gap: 1.5rem;
  padding-top: max(32px, env(safe-area-inset-top));
}

/* About text sits straight on the fog; scrolls if it's taller than the screen */
.info {
  width: min(100%, 34rem);
  flex: 0 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 0.25rem 1rem;
  font-size: 1.2rem;
  line-height: 1.6;
  text-shadow: 0 1px 14px rgba(0, 0, 0, 0.7);
}
.info:focus { outline: none; }
.prose .ornament { text-align: center; }

.page-title {
  margin: 0;
  flex-shrink: 0;
  text-align: center;
  font-family: var(--font-heading);
  font-weight: 400;
  font-size: clamp(1.3rem, 5vw, 2rem);
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

/* ================= Larger screens ================= */

@media (min-width: 768px) {
  .lock__art { height: min(56dvh, 520px); }
  .textbox { padding: 2.5rem 2.75rem; font-size: 1.3rem; }
  .info { font-size: 1.3rem; }
  .screen--story, .screen--final { padding-block: 32px; }
}

/* ================= Reduced motion ================= */

@media (prefers-reduced-motion: reduce) {
  .fog__layer, .bob, .intro__continue { animation: none !important; }
  .is-staged .reveal { animation-duration: .01s; animation-delay: 0s; }
  .intro__text p { animation-delay: 0s; animation-duration: .6s; }
  .intro__continue { opacity: 1; }
}
