:root {
  --ink: #3a2618;
  --paper: #fff8ea;
  --paper2: #f3e2bd;
  --accent: #c0392b;
  --gold: #f1c40f;
  --u: 1;
  --font: "Fredoka", "Trebuchet MS", "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; background: #1d140e; overflow: hidden;
  font-family: var(--font); color: var(--ink);
  -webkit-user-select: none; user-select: none; touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
body { display: grid; place-items: center; }
#app {
  position: relative; width: 960px; height: 540px; overflow: hidden;
  font-size: calc(var(--u) * 16px);
  box-shadow: 0 0 calc(var(--u) * 40px) rgba(0, 0, 0, 0.6);
  background: #000;
}
#game { position: absolute; inset: 0; display: block; }
#overlay { position: absolute; inset: 0; pointer-events: none; }
#overlay > * { pointer-events: auto; }
button { font-family: var(--font); cursor: pointer; }
kbd {
  display: inline-block; min-width: 1.5em; padding: 0.05em 0.35em; margin: 0 0.1em;
  border: 0.12em solid var(--ink); border-bottom-width: 0.22em; border-radius: 0.35em;
  background: #fff; font: 700 0.8em var(--font); text-align: center;
}

/* ---------- buttons ---------- */
.btn {
  font: 700 1.05em var(--font); color: var(--ink);
  background: var(--paper); border: 0.16em solid var(--ink); border-bottom-width: 0.32em;
  border-radius: 0.9em; padding: 0.45em 1.1em; margin: 0.25em;
  transition: transform 0.08s ease, background 0.15s;
}
.btn:hover { background: #fff; transform: translateY(-0.08em); }
.btn:active { transform: translateY(0.1em); border-bottom-width: 0.18em; }
.btn:focus-visible, .opt:focus-visible, .stop:focus-visible { outline: 0.2em solid #2e86de; outline-offset: 0.15em; }
.btn.primary { background: #f7c548; }
.btn.primary:hover { background: #ffd862; }
.btn.big { font-size: 1.5em; padding: 0.4em 1.6em; }
.btn.small { font-size: 0.85em; padding: 0.35em 0.8em; }

/* ---------- HUD buttons ---------- */
#hud-buttons { position: absolute; top: calc(var(--u) * 10px); right: calc(var(--u) * 10px); display: flex; gap: calc(var(--u) * 6px); z-index: 20; }
#hud-buttons button {
  width: max(40px, calc(var(--u) * 46px)); height: max(40px, calc(var(--u) * 46px)); border-radius: 50%;
  border: max(2px, calc(var(--u) * 2.5px)) solid var(--ink); background: rgba(255, 250, 240, 0.92);
  font-size: max(18px, calc(var(--u) * 20px)); line-height: 1; color: var(--ink); padding: 0;
}
#hud-buttons button[hidden] { display: none; }
#hud-buttons button { display: grid; place-items: center; }
#hud-buttons svg { display: block; }

/* ---------- touch ---------- */
#touch { position: absolute; inset: 0; pointer-events: none; z-index: 15; }
#touch[hidden] { display: none; }
#touch .tleft, #touch .tright { position: absolute; bottom: calc(var(--u) * 92px); display: flex; gap: calc(var(--u) * 14px); align-items: flex-end; }
#touch .tleft { left: calc(var(--u) * 18px); bottom: calc(var(--u) * 96px); }
#touch .tright { right: calc(var(--u) * 18px); bottom: calc(var(--u) * 22px); }
#touch button {
  pointer-events: auto; touch-action: none;
  width: max(58px, calc(var(--u) * 86px)); height: max(58px, calc(var(--u) * 86px)); border-radius: 50%;
  background: rgba(255, 250, 240, 0.45); border: calc(var(--u) * 3px) solid rgba(58, 38, 24, 0.7);
  font-size: calc(var(--u) * 34px); color: var(--ink);
}
#touch button.on { background: rgba(247, 197, 72, 0.85); }
#touch button.big { width: max(72px, calc(var(--u) * 110px)); height: max(72px, calc(var(--u) * 110px)); font-size: calc(var(--u) * 44px); }
#touch button.small { width: max(46px, calc(var(--u) * 66px)); height: max(46px, calc(var(--u) * 66px)); font-size: calc(var(--u) * 28px); }

/* ---------- screens ---------- */
.screen { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.screen.dim { background: rgba(29, 20, 14, 0.55); }
.panel {
  background: var(--paper); border: 0.22em solid var(--ink); border-radius: 1.4em;
  box-shadow: 0 0.5em 0 rgba(0, 0, 0, 0.25); padding: 1.1em 1.4em; max-width: 92%; max-height: 92%; overflow: auto;
  animation: pop 0.25s ease-out;
}
@keyframes pop { from { transform: scale(0.92); opacity: 0; } to { transform: none; opacity: 1; } }

/* title */
.screen.title { flex-direction: column; justify-content: flex-start; }
.title-wrap { margin-top: 1.6em; text-align: center; }
.logo { display: flex; flex-direction: column; align-items: center; line-height: 0.95; }
.logo .l1, .logo .l2 {
  font-weight: 700; color: #fff4d6; letter-spacing: 0.02em;
  -webkit-text-stroke: 0.08em var(--ink); paint-order: stroke fill;
  text-shadow: 0 0.12em 0 var(--ink), 0 0.25em 0 rgba(0, 0, 0, 0.25);
}
.logo .l1 { font-size: 4.2em; color: #ffd36b; }
.logo .amp { font-size: 1.5em; font-weight: 700; color: var(--ink); margin: 0.1em 0; }
.logo .l2 { font-size: 3.2em; color: #ffe9c2; }
.tag { margin-top: 0.4em; font-size: 1.3em; font-weight: 600; color: var(--ink); font-style: italic; }
.menu { margin-top: 0.8em; display: flex; flex-direction: column; align-items: center; }
.title-foot { position: absolute; bottom: 0.6em; left: 0; right: 0; text-align: center; font-weight: 600; color: #fff8ea; text-shadow: 0 0.1em 0 rgba(0,0,0,0.5); font-size: 0.95em; }

/* map */
.screen.map { background: rgba(29, 20, 14, 0.35); }
.map-svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.map-title { position: absolute; top: 1.4em; left: 0; width: 66%; text-align: center; z-index: 1; font-size: 2em; font-weight: 700; color: #8b5a2b; }
.stop {
  position: absolute; transform: translate(-50%, -50%);
  display: flex; flex-direction: column; align-items: center; gap: 0.2em;
  background: none; border: none; padding: 0;
}
.stop .num {
  width: 2.6em; height: 2.6em; border-radius: 50%; display: grid; place-items: center;
  background: #f7c548; border: 0.2em solid var(--ink); font: 700 1.2em var(--font); color: var(--ink);
  box-shadow: 0 0.2em 0 rgba(0,0,0,0.3); transition: transform 0.15s;
}
.stop .lbl { font-weight: 700; background: var(--paper); border: 0.12em solid var(--ink); border-radius: 0.6em; padding: 0.1em 0.5em; font-size: 0.9em; }
.stop.done .num { background: #7dcea0; }
.stop.locked .num { background: #ccc; font-size: 1em; }
.stop.sel .num { transform: scale(1.18); background: #ff8a5c; }
.stop.sel.done .num { background: #48c9b0; }
.stop:hover .num { transform: scale(1.12); }
.map-card {
  position: absolute; right: 2.4em; top: 50%; transform: translateY(-50%); width: 17em;
  background: var(--paper); border: 0.18em solid var(--ink); border-radius: 1em; padding: 0.8em 1em; text-align: center;
  box-shadow: 0 0.3em 0 rgba(0,0,0,0.25);
}
.mc-level { font-weight: 700; color: #a0522d; font-size: 0.85em; letter-spacing: 0.1em; }
.mc-name { font-weight: 700; font-size: 1.25em; line-height: 1.15; margin: 0.15em 0 0.4em; }
.mc-stats { display: flex; flex-direction: column; gap: 0.15em; font-size: 0.9em; font-weight: 600; }
.mc-badges { margin: 0.35em 0; display: flex; flex-wrap: wrap; gap: 0.25em; justify-content: center; }
.badge { font-size: 0.75em; background: #fdebd0; border: 0.1em solid var(--ink); border-radius: 0.6em; padding: 0.05em 0.4em; font-weight: 700; }
.mc-locked { font-weight: 600; color: #7f6a58; }
.map-foot { position: absolute; right: 2.2em; bottom: 1.9em; display: flex; }

/* dialogue */
.screen.scene { align-items: flex-end; cursor: pointer; }
.dialogue {
  position: relative; display: flex; gap: 1em; align-items: center;
  width: 92%; margin-bottom: 1.2em; min-height: 9.5em;
  background: var(--paper); border: 0.22em solid var(--ink); border-radius: 1.3em; padding: 0.8em 1em;
  box-shadow: 0 0.4em 0 rgba(0,0,0,0.3); animation: pop 0.2s ease-out;
}
.dialogue .portrait { width: 8em; height: 8em; border-radius: 1em; border: 0.18em solid var(--ink); flex: none; }
.dialogue.narrator .portrait { display: none; }
.dialogue.narrator .dtext { font-style: italic; }
.dbody { flex: 1; min-width: 0; }
.dname { font-weight: 700; font-size: 1.15em; letter-spacing: 0.03em; margin-bottom: 0.15em; }
.dtext { font-size: 1.3em; font-weight: 600; line-height: 1.35; min-height: 3.5em; }
.dhint { font-size: 0.8em; color: #8a7562; margin-top: 0.4em; }
.dialogue .skip { position: absolute; top: -1.2em; right: 1em; }
.vocab { color: #b0306a; border-bottom: 0.12em dotted #b0306a; cursor: help; position: relative; font-weight: 700; }
.vocab-tip {
  position: absolute; left: 50%; bottom: 130%; transform: translateX(-50%); width: 15em;
  background: #2c1f14; color: #fff8ea; font-size: 0.75em; font-style: normal; font-weight: 500; line-height: 1.3;
  padding: 0.5em 0.7em; border-radius: 0.6em; z-index: 5; pointer-events: none;
}
.vocab-tip b { color: #ffd36b; }

/* puzzles */
.puzzle { width: 48em; text-align: center; padding: 0.8em 1.2em; }
.pz-head { display: flex; gap: 0.8em; align-items: center; text-align: left; }
.pz-portrait { width: 4.2em; height: 4.2em; border-radius: 0.8em; border: 0.15em solid var(--ink); flex: none; }
.pz-title { font-size: 1.5em; font-weight: 700; }
.pz-intro { font-size: 0.98em; font-weight: 500; line-height: 1.3; }
.tumblers { display: flex; align-items: center; justify-content: center; gap: 0.4em; margin: 0.4em 0 0.2em; }
.tumbler { width: 1.1em; height: 2em; border-radius: 0.5em; background: #95a5a6; border: 0.14em solid var(--ink); transition: transform 0.3s, background 0.3s; }
.tumbler.cur { background: #f7c548; }
.tumbler.open { background: #2ecc71; transform: translateY(-0.4em); }
.rpip { width: 1.8em; height: 1.8em; border-radius: 50%; display: grid; place-items: center; font-weight: 700; background: #ddd; border: 0.14em solid var(--ink); }
.rpip.cur { background: #f7c548; } .rpip.open { background: #2ecc71; }
.tlabel { margin-left: 0.5em; font-weight: 700; font-size: 0.9em; color: #7f6a58; }
.pz-q { font-size: 1.1em; font-weight: 600; margin: 0.4em 0; }
.pz-q.big { font-size: 1.3em; line-height: 1.35; padding: 0 0.5em; }
.seq { display: flex; justify-content: center; flex-wrap: wrap; gap: 0.4em; margin: 0.5em 0; }
.chip { min-width: 2.5em; padding: 0.3em 0.5em; border-radius: 0.6em; background: #fdebd0; border: 0.15em solid var(--ink); font-size: 1.6em; font-weight: 700; }
.chip.q { background: #f7c548; animation: pulse 1s infinite; }
.chip.solved { background: #7dcea0; animation: none; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.opts { display: grid; grid-template-columns: 1fr 1fr; gap: 0.45em; margin: 0.4em 0; }
.opts.row { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; }
.opts .count { font-weight: 700; padding: 0 0.6em; }
.opt {
  display: flex; align-items: center; gap: 0.5em; justify-content: center;
  font: 700 1.3em var(--font); color: var(--ink); background: #fff;
  border: 0.15em solid var(--ink); border-bottom-width: 0.28em; border-radius: 0.8em; padding: 0.4em 0.6em;
}
.opts.row .opt { font-size: 1.05em; }
.opt:hover:not(:disabled) { background: #fff4d6; }
.opt.right { background: #7dcea0; }
.opt.wrong { background: #f5b7b1; animation: shake 0.35s; opacity: 0.7; }
.opt.go { background: #f7c548; }
@keyframes shake { 25% { transform: translateX(-0.3em); } 75% { transform: translateX(0.3em); } }
.pz-feedback { min-height: 1.7em; font-size: 1em; font-weight: 600; line-height: 1.3; }
.pz-feedback .good { color: #1e8449; } .pz-feedback .bad { color: #a93226; } .pz-feedback .hint { color: #6c3483; }
.pz-actions { display: flex; justify-content: center; margin-top: 0.2em; }
.pz-svg { width: 100%; max-height: 14em; margin: 0.2em 0; }
.pz-svg.gears { max-height: 7.5em; }
.pz-svg .sw:hover rect:first-child, .pz-svg .mir:hover rect { filter: brightness(1.15); }

/* results etc */
.results, .pause, .help, .lexicon, .ending { text-align: center; }
.results { width: 36em; }
.res-kicker { font-weight: 700; letter-spacing: 0.12em; color: #a0522d; font-size: 0.9em; }
.res-title { font-size: 2.2em; font-weight: 700; }
.res-book { font-size: 1.2em; font-style: italic; margin-bottom: 0.6em; }
.res-rows { display: flex; flex-direction: column; gap: 0.3em; margin: 0.5em auto; max-width: 26em; }
.res-row { display: flex; align-items: center; gap: 0.6em; background: #f6ead2; border-radius: 0.6em; padding: 0.3em 0.7em; font-weight: 600; }
.res-row.ok { background: #d5f5e3; }
.res-row .ri { font-size: 1.3em; } .res-row .rn { flex: 1; text-align: left; } .res-row .rv { font-weight: 700; }
.res-row small { font-weight: 500; color: #7f6a58; }
.res-badges { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.4em; margin: 0.6em 0; }
.rb { display: flex; flex-direction: column; align-items: center; background: #eee; border: 0.13em dashed #aaa; border-radius: 0.8em; padding: 0.4em 0.2em; font-weight: 700; font-size: 0.85em; opacity: 0.55; }
.rb span { font-size: 1.8em; filter: grayscale(1); }
.rb small { font-weight: 500; font-size: 0.75em; color: #6d5a4a; }
.rb.earned { background: #fdebd0; border: 0.13em solid var(--ink); opacity: 1; animation: pop 0.4s ease-out; }
.rb.earned span { filter: none; }
.res-actions { display: flex; justify-content: center; flex-wrap: wrap; margin-top: 0.4em; }
.pause-btns { display: flex; flex-direction: column; align-items: stretch; min-width: 16em; }
.help { width: 52em; text-align: left; }
.help .res-title { text-align: center; }
.help-grid { display: grid; grid-template-columns: 1.15fr 1fr; gap: 1.2em; }
.help h3 { margin: 0.5em 0 0.3em; }
.help ul { margin: 0; padding-left: 1.2em; line-height: 1.35; }
.help p { line-height: 1.35; margin: 0.4em 0; }
.keys td { padding: 0.15em 0.4em; vertical-align: middle; }
.rc-row { display: flex; gap: 0.6em; align-items: center; margin-bottom: 0.4em; line-height: 1.2; }
.rc-port { width: 3.6em; height: 3.6em; border-radius: 0.6em; border: 0.13em solid var(--ink); }
.small { font-size: 0.9em; color: #6d5a4a; }
.lexicon { width: 40em; }
.lex-list { text-align: left; display: grid; grid-template-columns: 1fr 1fr; gap: 0.4em 1em; margin: 0.5em 0; }
.lex { background: #f6ead2; border-radius: 0.6em; padding: 0.35em 0.6em; line-height: 1.25; }
.lex b { color: #b0306a; display: block; }
.ending { width: 34em; }
.ending p { line-height: 1.4; }

.toast {
  position: absolute; top: 4.6em; left: 50%; transform: translateX(-50%);
  background: #2c1f14; color: #fff8ea; border: 0.15em solid #f7c548; border-radius: 1em;
  padding: 0.5em 1.1em; font-weight: 700; font-size: 1.1em; z-index: 30;
  animation: pop 0.3s ease-out; transition: opacity 0.5s;
}
.toast.out { opacity: 0; }

@media (prefers-reduced-motion: reduce) {
  .panel, .dialogue, .toast, .rb.earned { animation: none; }
  .chip.q { animation: none; }
}

/* ---------- portrait phones: ask to rotate ---------- */
#rotate {
  display: none; position: fixed; inset: 0; z-index: 100;
  flex-direction: column; align-items: center; justify-content: center; gap: 0.6em;
  padding: 24px; text-align: center; background: #2c1f14; color: #fff8ea; font-size: 18px;
}
#rotate h2 { margin: 0; font-size: 1.6em; color: #ffd36b; }
#rotate p { margin: 0; max-width: 20em; line-height: 1.4; }
#rotate .btn { font-size: 1.2em; margin-top: 0.6em; }
#rotate [hidden] { display: none; }
#rotate .ios-tip { font-size: 0.85em; opacity: 0.85; }
#rotate .phone { transform-origin: 60px 60px; animation: tilt 2.4s ease-in-out infinite; }
@keyframes tilt { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
@media (orientation: portrait) and (max-width: 820px) and (pointer: coarse) {
  #rotate { display: flex; }
}
