:root {
  --ink: #2b1d0e;
  --paper: #fff6e0;
  --gold: #e8a33d;
  --panel: rgba(34, 22, 10, 0.86);
  --font-body: Georgia, 'Iowan Old Style', 'Times New Roman', serif;
  --font-ui: system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: #120c06;
  color: var(--paper);
  font-family: var(--font-body);
}

body {
  display: grid;
  place-items: center;
}

/* 16:9 stage that fits the window; everything inside is sized in container units. */
#stage {
  position: relative;
  width: min(100vw, 100vh * 16 / 9);
  aspect-ratio: 16 / 9;
  overflow: hidden;
  container-type: size;
  font-size: clamp(13px, 2.6cqh, 26px);
  background: #000;
}

#background {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  transition: opacity 0.4s;
}

/* ---- placeholder backgrounds: one gradient per story background id ---- */
.bg-bedroom {
  background-image:
    radial-gradient(circle at 78% 30%, #ffe9a8 0 6%, transparent 7%),
    linear-gradient(90deg, transparent 62%, rgba(255, 210, 130, 0.35) 62% 94%, transparent 94%),
    linear-gradient(#5b3a4a, #8a5a4a 60%, #4a2f25 60%);
}
.bg-tower {
  background-image:
    radial-gradient(circle at 50% 85%, #ffd27a 0 10%, transparent 40%),
    linear-gradient(#7fb6d9, #f4d7a1 70%, #c9a15f 70%);
}
.bg-courtyard {
  background-image:
    linear-gradient(transparent 62%, #c9a66b 62%),
    repeating-linear-gradient(90deg, transparent 0 9%, rgba(90, 55, 25, 0.35) 9% 10%),
    linear-gradient(#8fc3e0 0 30%, #d9a56a 30%);
}
.bg-grassland {
  background-image:
    radial-gradient(circle at 20% 22%, #fff3c4 0 5%, transparent 6%),
    repeating-linear-gradient(100deg, transparent 0 1.2%, rgba(120, 90, 20, 0.25) 1.2% 1.6%),
    linear-gradient(#9fd0e6 0 38%, #e9c35c 38% 70%, #c99a34);
}
.bg-baobab {
  background-image:
    radial-gradient(ellipse at 50% 18%, #5d7a3a 0 26%, transparent 27%),
    linear-gradient(90deg, transparent 40%, #6b4a2f 40% 60%, transparent 60%),
    linear-gradient(#f3b765 0 66%, #b98233 66%);
}
.bg-festival {
  background-image:
    radial-gradient(circle at 50% 20%, #ffd76a 0 3%, rgba(255, 190, 80, 0.35) 4% 12%, transparent 22%),
    radial-gradient(circle at 15% 12%, #fff 0 0.25%, transparent 0.4%),
    radial-gradient(circle at 82% 22%, #fff 0 0.25%, transparent 0.4%),
    radial-gradient(circle at 66% 8%, #fff 0 0.2%, transparent 0.35%),
    linear-gradient(#1b1740 0 64%, #3a2a1a 64%);
}

/* ---- characters ---- */
#cast {
  position: absolute;
  inset: 8% 6% 0 6%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 8%;
}

.sprite {
  height: 88%;
  max-width: 46%;
  object-fit: contain;
  object-position: bottom;
  filter: brightness(0.72);
  transition: filter 0.2s, transform 0.2s;
}
.sprite.speaking {
  filter: none;
  transform: translateY(-1.5%);
}
.sprite.no-art {
  aspect-ratio: 1 / 2;
  height: 62%;
  display: grid;
  place-items: start center;
  padding: 3em 0.6em 0;
  text-align: center;
  font-family: var(--font-ui);
  font-size: 0.8em;
  font-weight: 700;
  color: var(--paper);
  background: rgba(34, 22, 10, 0.55);
  border: 0.15em dashed rgba(255, 246, 224, 0.7);
  border-radius: 45% 45% 0.6em 0.6em / 25% 25% 0.6em 0.6em;
}

/* ---- dialogue ---- */
#dialogue {
  position: absolute;
  left: 4%;
  right: 4%;
  bottom: 4%;
  min-height: 27%;
  padding: 1.3em 1.5em 1.1em;
  background: var(--panel);
  border: 0.12em solid var(--gold);
  border-radius: 0.6em;
  cursor: pointer;
  user-select: none;
}

#speaker {
  position: absolute;
  top: -0.9em;
  left: 1.2em;
  padding: 0.15em 0.9em;
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.85em;
  color: var(--ink);
  background: var(--speaker-color, var(--gold));
  border-radius: 0.4em;
}

#text {
  margin: 0;
  line-height: 1.5;
  text-wrap: pretty;
}

/* Not yet typed: takes its final space so the visible words never reflow. */
#text .typing-rest { visibility: hidden; }

#advance-hint {
  position: absolute;
  right: 1em;
  bottom: 0.5em;
  font-size: 0.7em;
  color: var(--gold);
  animation: bob 1s ease-in-out infinite alternate;
}
#stage:not([data-can-advance='true']) #advance-hint,
#stage[data-typing='true'] #advance-hint { display: none; }

@keyframes bob { to { transform: translateY(0.3em); } }

#ending {
  margin-top: 0.8em;
  display: flex;
  align-items: center;
  gap: 1em;
  font-family: var(--font-ui);
  font-weight: 700;
  color: var(--gold);
}
#ending[hidden] { display: none; }

/* ---- choices ---- */
#choices {
  position: absolute;
  inset: 10% 14% 36% 14%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.7em;
  pointer-events: none;
  z-index: 1;
}
#choices button {
  pointer-events: auto;
  text-align: left;
  padding: 0.7em 1em;
}
#stage[data-typing='true'] #choices { visibility: hidden; }

/* ---- buttons, menu, title ---- */
button {
  font: inherit;
  font-family: var(--font-ui);
  font-size: 0.8em;
  color: var(--paper);
  background: var(--panel);
  border: 0.1em solid var(--gold);
  border-radius: 0.4em;
  padding: 0.35em 0.9em;
  cursor: pointer;
}
button:hover, button:focus-visible {
  background: var(--gold);
  color: var(--ink);
  outline: none;
}

#menu {
  position: absolute;
  top: 2.5%;
  right: 2.5%;
  display: flex;
  gap: 0.4em;
  font-size: 0.8em;
  z-index: 2;
}

#title-screen {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.8em;
  text-align: center;
  padding: 0 8%;
  background: linear-gradient(#f3b765, #c9762c 70%, #5b3413);
  color: var(--ink);
}
#title-screen h1 { margin: 0; font-size: 2.2em; text-wrap: balance; }
#title-screen p { margin: 0 0 1em; font-style: italic; }
#title-screen button { font-size: 1em; min-width: 9em; }
#title-screen button[hidden] { display: none; }

#stage[data-screen='title'] #title-screen { display: flex; }
#stage[data-screen='title'] #menu,
#stage[data-screen='title'] #dialogue,
#stage[data-screen='title'] #choices { display: none; }

#toast {
  position: absolute;
  top: 3%;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.3em 1em;
  font-family: var(--font-ui);
  font-size: 0.8em;
  background: var(--panel);
  border-radius: 0.4em;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  z-index: 4;
}
#toast.show { opacity: 1; }

/* ---- log ---- */
#log-dialog {
  width: min(42em, 90vw);
  max-height: 80vh;
  color: var(--paper);
  background: #22160a;
  border: 2px solid var(--gold);
  border-radius: 10px;
  font-family: var(--font-body);
}
#log-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
#log-dialog h2 { margin-top: 0; font-family: var(--font-ui); }
#log-entries { max-height: 55vh; overflow-y: auto; margin-bottom: 1em; }
#log-entries p { margin: 0 0 0.6em; line-height: 1.45; }
#log-entries .log-choice { color: var(--gold); font-family: var(--font-ui); }
#log-dialog button { font-size: 1rem; }

/* Portrait screens (phones): fill the screen instead of letterboxing to 16:9,
   and keep the choices clear of the menu and the dialogue box. */
@media (max-aspect-ratio: 1/1) {
  #stage {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    aspect-ratio: auto;
    font-size: clamp(14px, 4cqw, 22px);
  }
  #dialogue { min-height: 30%; }
  #choices { inset: 12% 6% 36% 6%; }
  /* Stand the cast on top of the dialogue box and shrink it to fit side by side. */
  #cast { inset: 10% 2% 34% 2%; gap: 2%; }
  .sprite { height: auto; max-height: 62%; max-width: 31%; min-width: 0; }
  .sprite.no-art { width: 27%; height: auto; padding-top: 1.5em; font-size: 0.7em; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
