:root {
  /* Gaslit London: soot, sepia paper, brass, and one colour for the scarlet thread. */
  --ink: #14110c; --void: #0b0906; --panel: #1f1a12; --slot: #2e2619; --line: #5a4a32;
  --text: #f1e6cc; --dim: #9a8a6a; --gas: #f0b04a; --scarlet: #c8323c; --brass: #e3c27a; --teal: #7fb5a8;
  --accent: var(--gas);
  --tap: 44px;
  --font: Georgia, "Times New Roman", serif;
  --mono: "Courier New", ui-monospace, monospace;
}
.app[data-hero="holmes"] { --accent: var(--teal); }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--void); color: var(--text); font: 700 14px/1.2 var(--font); }
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }
button, .button {
  font: inherit; text-transform: uppercase; letter-spacing: .04em; color: var(--dim); background: var(--slot);
  border: 2px solid var(--panel); border-top-color: var(--line); min-height: var(--tap); padding: 6px 10px;
  cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; touch-action: manipulation;
}
button:hover, button:focus-visible, .button:hover { color: var(--text); outline: none; border-color: var(--accent); }
button[aria-pressed="true"], button.active { color: var(--text); background: #4a1a1c; border-color: var(--scarlet); }
button:disabled { opacity: .4; cursor: default; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- layout: portrait / desktop ---------- */
.app {
  height: 100dvh; max-width: 1280px; margin: 0 auto; display: grid; gap: 0;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas: "bar bar" "stage stage" "sentence sentence" "verbs inv";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.bar { grid-area: bar; display: flex; align-items: center; gap: 12px; padding: 6px 8px; background: var(--ink); border-bottom: 2px solid var(--panel); }
.title small { color: var(--accent); font-size: 11px; letter-spacing: .12em; text-transform: uppercase; }
.title h1 { margin: 0; font: italic 700 20px/1 var(--font); letter-spacing: .01em; color: var(--text); text-shadow: 1px 1px var(--scarlet); }
.objective { flex: 1; margin: 0; color: var(--brass); font-size: 12px; font-weight: 400; min-width: 0; }
.tools { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.tools button { font-size: 11px; padding: 4px 8px; }
.stage { grid-area: stage; position: relative; display: grid; place-items: center; overflow: hidden; background: var(--void); min-height: 140px; }
#screen { image-rendering: pixelated; touch-action: none; cursor: crosshair; display: block; }
.loading { position: absolute; color: var(--scarlet); margin: 0; padding: 16px; text-align: center; }
.sentence { grid-area: sentence; margin: 0; min-height: 30px; padding: 7px 10px; text-align: center; color: var(--gas); background: #1a150e; border-top: 2px solid var(--line); text-transform: uppercase; letter-spacing: .05em; }
.verbs { grid-area: verbs; display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 6px; background: var(--ink); align-content: start; }
.verbs button { font-size: 12px; padding: 4px; }
.verbs button.special { color: var(--scarlet); }
.app[data-hero="holmes"] .verbs button.special { color: var(--teal); }
.inventory { grid-area: inv; display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 4px; padding: 6px; background: #191409; align-content: start; }
.slot { aspect-ratio: 1; width: 100%; min-height: var(--tap); padding: 4px; background: var(--slot); border: 2px solid var(--panel); border-top-color: var(--line); }
.slot img { width: 100%; height: 100%; max-width: 48px; max-height: 48px; image-rendering: pixelated; pointer-events: none; }
.slot.empty { opacity: .45; }
.slot.selected { border-color: var(--scarlet); background: #4a1a1c; }
.choices { grid-area: 4 / 1 / 5 / 3; display: flex; flex-direction: column; gap: 4px; padding: 6px; background: var(--ink); z-index: 2; overflow-y: auto; }
.choices button { justify-content: flex-start; text-align: left; text-transform: none; color: var(--accent); font-size: 14px; }
.choices button:hover, .choices button:focus-visible { color: var(--text); }
.app.choosing .verbs, .app.choosing .inventory { visibility: hidden; }
.app.busy .verbs, .app.busy .inventory { opacity: .5; pointer-events: none; }

/* ---------- landscape phones: verbs left, inventory right, scene in the middle ---------- */
@media (orientation: landscape) and (max-height: 540px) {
  :root { --tap: 40px; }
  .app {
    max-width: none;
    grid-template-columns: minmax(150px, 22vw) minmax(0, 1fr) minmax(130px, 19vw);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "bar bar bar" "verbs stage inv" "verbs sentence inv";
  }
  .bar { padding: 2px 8px; }
  .title h1 { font-size: 15px; }
  .title small { display: none; }
  .objective { font-size: 11px; }
  .tools button { min-height: 32px; }
  .verbs { grid-template-columns: 1fr 1fr; align-content: center; }
  .inventory { grid-template-columns: repeat(2, 1fr); align-content: center; }
  .sentence { min-height: 24px; padding: 3px; font-size: 12px; }
  .choices { grid-area: 2 / 2 / 4 / 3; justify-content: flex-end; background: rgba(11, 9, 6, .9); }
}
@media (max-width: 520px) and (orientation: portrait) {
  .bar { flex-wrap: wrap; }
  .objective { order: 3; flex-basis: 100%; }
  .app { grid-template-columns: 1fr; grid-template-areas: "bar" "stage" "sentence" "verbs" "inv"; grid-template-rows: auto minmax(0, 1fr) auto auto auto; }
  .choices { grid-area: 4 / 1 / 6 / 2; }
  .inventory { grid-template-columns: repeat(8, 1fr); }
  .slot { padding: 2px; }
}

/* ---------- dialogs ---------- */
.sheet { color: var(--text); background: var(--panel); border: 2px solid var(--scarlet); box-shadow: 6px 6px 0 var(--gas); width: min(560px, 92vw); max-height: 90dvh; padding: 16px; }
.sheet::backdrop { background: rgba(6, 5, 3, .85); }
.sheet h2 { margin: 0 0 12px; color: var(--scarlet); text-transform: uppercase; }
.sheet h3 { margin: 16px 0 6px; font-size: 13px; color: var(--gas); text-transform: uppercase; }
.sheet p { font-weight: 400; line-height: 1.5; }
.saves { display: grid; gap: 4px; }
.save-row { display: flex; gap: 4px; align-items: center; }
.save-name { flex: 1; font-size: 12px; font-weight: 400; color: var(--text); }
.sheet textarea { width: 100%; background: var(--void); color: var(--brass); border: 1px solid var(--line); font: 12px var(--mono); -webkit-user-select: text; user-select: text; }
.menu-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.menu-actions .primary { margin-left: auto; color: var(--brass); }

/* ---------- debug ---------- */
.debug { position: fixed; top: 4px; left: 4px; z-index: 50; max-width: 300px; max-height: 70dvh; overflow: auto; padding: 6px; font: 11px var(--mono); background: rgba(11, 9, 6, .92); border: 1px solid var(--scarlet); color: var(--brass); }
.debug select { width: 100%; margin: 4px 0; font: inherit; }
.debug .flags { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }

/* ---------- keyboard and screen-reader targets: hidden until focused ---------- */
.targets { position: absolute; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; gap: 4px; padding: 4px;
  background: rgba(11, 9, 6, .9); clip-path: inset(50%); }
.targets:focus-within { clip-path: none; }
.targets button { min-height: 36px; font-size: 12px; text-transform: none; }

/* ---------- library (bookshelf) ---------- */
.library .books { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; }
.library .book { width: 100%; flex-direction: column; align-items: flex-start; text-transform: none; text-align: left; gap: 2px; padding: 8px 12px; }
.library .book b { color: var(--text); font-size: 15px; }
.library .book small { color: var(--dim); font-weight: 400; font-size: 12px; }
.library .book.soon { display: flex; opacity: .5; border: 2px dashed var(--line); }

.reveal {
  width: min(96vw, 140dvh, 1100px);
  max-height: 96dvh;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--void);
  color: var(--text);
}
.reveal::backdrop { background: var(--void); }
.reveal-art { position: relative; overflow: hidden; container-type: inline-size; }
.reveal-art img { display: block; width: 100%; height: auto; }
.reveal-actor {
  position: absolute;
  transform-origin: 50% 95%;
  animation: reveal-actor-idle 5s ease-in-out infinite;
}
.reveal-actor::before {
  content: '';
  position: absolute;
  left: 13%;
  right: 21%;
  bottom: -1%;
  height: 2.5%;
  border-radius: 50%;
  background: #1d120b70;
  filter: blur(3px);
}
.reveal-actor img { position: relative; }
.reveal-actor.mirrored { scale: -1 1; }
.reveal-actor.walk-left { --reveal-entry-offset: 7cqw; }
.reveal-actor + .reveal-actor { animation-delay: -2s; }
.reveal-actor.entering { animation: reveal-actor-enter 1.25s linear both; }
.reveal-actor img.reveal-step { display: none; }
.reveal-actor.entering .reveal-step {
  display: block;
  position: absolute;
  inset: 0;
  transform: translateY(2%);
  animation: reveal-step-frame .42s steps(1, end) 3 both;
}
.reveal-actor.entering img:first-child {
  animation: reveal-base-frame .42s steps(1, end) 3 both;
}
.reveal-actor.entering .reveal-mouth, .reveal-actor.entering .reveal-glint { visibility: hidden; }
.reveal-actor.entering.speaking .reveal-mouth { display: none; }
@keyframes reveal-actor-enter {
  from { translate: var(--reveal-entry-offset, -7cqw) 0; }
  to { translate: 0 0; }
}
@keyframes reveal-step-frame {
  0%, 100% { opacity: 0; }
  50% { opacity: 1; }
}
@keyframes reveal-base-frame {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}
.reveal-actor .reveal-glint { width: 11%; }
.reveal-mouth {
  display: none;
  position: absolute;
  aspect-ratio: 1.7;
  border-radius: 45% 45% 48% 30%;
  background: #44201a;
  transform-origin: top center;
}
.reveal-actor.speaking .reveal-mouth {
  display: block;
  animation: reveal-mouth-talk .9s steps(1, end) 2 forwards;
}
@keyframes reveal-actor-idle {
  0%, 100% { transform: none; }
  50% { transform: translateX(.65cqw) rotate(1.1deg); }
}
@keyframes reveal-mouth-talk {
  0%, 38%, 76%, 100% { opacity: 0; transform: scaleY(.15); }
  12%, 57% { opacity: 1; transform: scaleY(1); }
  26%, 66%, 88% { opacity: 1; transform: scaleY(.55); }
}
.reveal-effect { position: absolute; pointer-events: none; transform: translate(-50%, -50%); }
.reveal-glint {
  width: 2.8%; aspect-ratio: 1;
  background: #fff3bd;
  clip-path: polygon(50% 0, 58% 42%, 100% 50%, 58% 58%, 50% 100%, 42% 58%, 0 50%, 42% 42%);
  animation: reveal-glint 4s ease-in-out infinite;
}
.reveal-flicker {
  width: 20%; aspect-ratio: 1;
  background: radial-gradient(ellipse, #ffba5a55, transparent 68%);
  mix-blend-mode: screen;
  animation: reveal-flicker 2.3s ease-in-out infinite alternate;
}
.reveal-controls { display: flex; align-items: center; gap: 12px; padding: 10px 14px; }
.reveal-controls p { flex: 1; margin: 0; line-height: 1.5; }
.reveal-controls button { flex-shrink: 0; }
.reveal-moth {
  width: clamp(10px, 1.6cqw, 18px);
  aspect-ratio: 1.3;
  background: linear-gradient(#f0c18a, #8b5634) center / 13% 67% no-repeat;
  filter: drop-shadow(0 0 2px #5c2c16);
  animation: reveal-moth-flight 5.8s ease-in-out infinite;
}
.reveal-moth::before, .reveal-moth::after {
  content: '';
  position: absolute;
  top: 8%;
  width: 49%;
  height: 74%;
  background: #edc393;
  animation: reveal-moth-wing .16s ease-in-out infinite alternate;
}
.reveal-moth::before {
  left: 0;
  clip-path: polygon(0 10%, 42% 0, 100% 55%, 100% 100%, 39% 87%, 12% 59%);
  transform-origin: right center;
}
.reveal-moth::after {
  right: 0;
  clip-path: polygon(100% 10%, 58% 0, 0 55%, 0 100%, 61% 87%, 88% 59%);
  transform-origin: left center;
}
@keyframes reveal-moth-wing {
  to { transform: scaleX(.28); }
}
@keyframes reveal-moth-flight {
  0% { transform: translate(-50%, -50%) rotate(22deg); }
  19% { transform: translate(3cqw, -4cqw) rotate(37deg); }
  37% { transform: translate(9cqw, -6cqw) rotate(60deg); }
  56% { transform: translate(12cqw, -2cqw) rotate(140deg); }
  76% { transform: translate(6cqw, 1cqw) rotate(255deg); }
  100% { transform: translate(-50%, -50%) rotate(382deg); }
}
@keyframes reveal-glint {
  0%, 16%, 34%, 100% { opacity: 0; }
  23% { opacity: .9; }
}
@keyframes reveal-flicker {
  0%, 41% { opacity: .2; }
  24%, 100% { opacity: .65; }
}
@media (prefers-reduced-motion: reduce) {
  .reveal-effect, .reveal-actor.speaking .reveal-mouth { display: none; }
  .reveal-actor, .reveal-actor.entering, .reveal-actor.entering img:first-child { animation: none; }
  .reveal-actor.entering .reveal-step { display: none; }
}
