:root {
  --ink: #fff8ec;
  --gold: #ffd978;
  --gold-deep: #e9a93c;
  --night: #0b1030;
  --panel: rgba(16, 18, 52, 0.84);
  --paper: #fbf3e2;
  --paper-ink: #45304f;
  --serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  --sans: 'Nunito', ui-rounded, system-ui, sans-serif;
  --hand: 'Caveat', 'Bradley Hand', cursive;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--night); color: var(--ink); font-family: var(--sans); }
button { font: inherit; }
[hidden] { display: none !important; }

.game { position: fixed; inset: 0; overflow: hidden; user-select: none; -webkit-user-select: none; cursor: pointer; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.game.over-target { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cg transform='rotate(-30 20 20)'%3E%3Crect x='12' y='8' width='16' height='26' rx='4' fill='%231f1d2b' stroke='white' stroke-width='2'/%3E%3Crect x='15' y='12' width='10' height='7' rx='1' fill='%238fd3ff'/%3E%3Crect x='15' y='3' width='10' height='5' fill='%23ff5a6e'/%3E%3C/g%3E%3C/svg%3E") 12 4, pointer; }
.no-webgl { position: absolute; inset: 0; z-index: 40; display: grid; place-content: center; padding: 24px; text-align: center; font-size: 1.2rem; background: var(--night); margin: 0; }
@keyframes bob { 50% { transform: translateY(-14px) rotate(-3deg); } }
@keyframes fade-out { to { opacity: 0; } }

/* ─── HUD & prompts ────────────────────────────────────────────── */
.hud { position: absolute; top: max(14px, env(safe-area-inset-top)); left: 16px; right: 16px; display: flex; justify-content: space-between; z-index: 40; pointer-events: none; }
.hud-button { pointer-events: auto; background: rgba(10, 12, 40, 0.5); color: rgba(255, 248, 236, 0.85); border: 1px solid rgba(255, 230, 170, 0.25);
  padding: 8px 14px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; text-decoration: none; cursor: pointer; backdrop-filter: blur(6px); }
.hud-button:hover, .hud-button:focus-visible { background: rgba(30, 32, 80, 0.8); color: #fff; outline: none; border-color: var(--gold); }
.prompt { position: absolute; left: 50%; top: 14vh; transform: translateX(-50%); z-index: 8; padding: 12px 22px; border-radius: 999px;
  background: rgba(20, 16, 44, 0.8); border: 1px solid rgba(255, 90, 110, 0.6); font-weight: 800; letter-spacing: 0.02em; white-space: nowrap;
  box-shadow: 0 0 30px rgba(255, 90, 110, 0.3); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 10px rgba(255, 90, 110, 0.15); } }

/* ─── Dialogue ─────────────────────────────────────────────────── */
.dialogue { position: absolute; z-index: 10; left: 50%; bottom: max(3vh, env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: min(920px, calc(100vw - 32px)); min-height: 132px; padding: 30px 38px 26px; border-radius: 24px;
  background: linear-gradient(180deg, rgba(28, 30, 78, 0.86), rgba(12, 12, 40, 0.92)); border: 1px solid rgba(255, 230, 170, 0.3);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.08); backdrop-filter: blur(10px);
  animation: rise 0.35s ease-out; --who: #ffd978; }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 10px); } }
.text { margin: 0; font-size: clamp(1.05rem, 1.1rem + 0.35vw, 1.4rem); line-height: 1.6; text-wrap: pretty; }
.unrevealed { opacity: 0; }
.nameplate { position: absolute; top: -18px; left: 30px; padding: 5px 18px 6px; border-radius: 999px; background: var(--who); color: #2a1d3c;
  font-family: var(--serif); font-weight: 700; font-size: 1.05rem; box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3), 0 0 20px color-mix(in srgb, var(--who) 50%, transparent); }
.next { position: absolute; right: 22px; bottom: 14px; color: var(--gold); opacity: 0; font-size: 1.1rem; }
.dialogue.done .next { opacity: 1; animation: next 1.2s ease-in-out infinite; }
@keyframes next { 50% { transform: translateY(-4px) rotate(36deg); } }
.dialogue[data-who="keeper"] { --who: #a8f0e6; }
.dialogue[data-who="mira"] { --who: #8ee6d8; }
.dialogue[data-who="pip"], .dialogue[data-who="oldpip"] { --who: #ffd84d; }
.dialogue[data-who="papa"] { --who: #eccb9a; }
.dialogue[data-who="papa"] .text { font-family: var(--serif); font-style: italic; }
.dialogue[data-who="blue"] { --who: #9fd0ff; }
.dialogue[data-who="orange"] { --who: #ffbb7a; }
.dialogue[data-who="cat"] { --who: #d8ccff; }
.dialogue[data-who="you"] { --who: #fff; }
.dialogue[data-who="you"] .text { font-style: italic; }
.dialogue.narration { background: linear-gradient(180deg, rgba(253, 246, 230, 0.95), rgba(244, 231, 204, 0.95)); color: var(--paper-ink);
  border-color: rgba(201, 160, 94, 0.6); text-align: center; padding: 30px 48px; border-radius: 8px 22px 22px 8px; }
.dialogue.narration .text { font-family: var(--serif); font-size: clamp(1.1rem, 1.15rem + 0.4vw, 1.5rem); }
.dialogue.narration .next { color: #b8863a; }

.choices { position: absolute; z-index: 10; left: 50%; bottom: max(4vh, env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: min(620px, calc(100vw - 32px)); display: grid; gap: 12px; }
.choices button { display: flex; gap: 14px; align-items: center; text-align: left; padding: 16px 22px; border-radius: 18px; cursor: pointer;
  color: var(--ink); font-size: 1.1rem; font-weight: 700; background: rgba(20, 22, 64, 0.88); border: 1px solid rgba(255, 230, 170, 0.35);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35); transition: transform 0.2s, background 0.2s, border-color 0.2s; animation: rise-in 0.4s ease-out backwards; }
.choices button:nth-child(2) { animation-delay: 0.08s; } .choices button:nth-child(3) { animation-delay: 0.16s; }
@keyframes rise-in { from { opacity: 0; transform: translateY(12px); } }
.choices button:hover, .choices button:focus-visible { transform: translateX(6px); background: rgba(46, 44, 110, 0.95); border-color: var(--gold); outline: none; }
.choice-key { display: grid; place-items: center; flex: none; width: 28px; height: 28px; border-radius: 50%; font-size: 0.85rem; background: var(--gold); color: #2a1d3c; }

/* Speech bubbles in the Keeper's room: the box moves beside the speaker and grows a tail. */
.dialogue.bubble { left: var(--bx); top: var(--by); bottom: auto; transform: none; width: min(460px, 36vw); min-height: 0;
  padding: 26px 28px 22px; border-radius: 22px; background: rgba(22, 22, 64, 0.95); backdrop-filter: none; animation: bubble-in 0.3s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes bubble-in { from { opacity: 0; scale: 0.9; } }
.dialogue.bubble .text { font-size: clamp(1rem, 0.9rem + 0.35vw, 1.2rem); }
.dialogue.bubble::after, .dialogue.tailed::after { content: ''; position: absolute; width: 22px; height: 22px; background: rgba(22, 22, 64, 0.95);
  border: 1px solid rgba(255, 230, 170, 0.3); border-width: 0 0 1px 1px; }
.dialogue.bubble[data-tail="left"]::after { left: -12px; top: calc(var(--ty) - 11px); transform: rotate(45deg); }
.dialogue.bubble[data-tail="right"]::after { right: -12px; top: calc(var(--ty) - 11px); transform: rotate(-135deg); }
.dialogue.bubble[data-tail="bottom"]::after { bottom: -12px; left: calc(var(--tx) - 11px); transform: rotate(-45deg); }
.dialogue.tailed { background: rgba(22, 22, 64, 0.95); }
.dialogue.name-right .nameplate { left: auto; right: 18px; }
.dialogue.tailed::after { top: -12px; left: calc(var(--tx) - 11px); transform: rotate(135deg); }

/* ─── Close-up cards ───────────────────────────────────────────── */
.card { position: absolute; inset: 0; z-index: 12; display: grid; place-content: center; justify-items: center; gap: 16px; padding: 16px;
  background: radial-gradient(circle, rgba(10, 10, 36, 0.55), rgba(5, 5, 20, 0.85)); backdrop-filter: blur(3px); animation: fade-in 0.4s ease-out; }
@keyframes fade-in { from { opacity: 0; } }
.card-body { animation: card-in 0.7s cubic-bezier(.2, .9, .3, 1.2); }
@keyframes card-in { from { transform: translateY(40px) rotate(-8deg) scale(0.8); opacity: 0; } }
.card-hint { margin: 0; opacity: 0; font-weight: 700; font-size: 0.9rem; color: rgba(255, 248, 236, 0.7); transition: opacity 0.4s; }
.card[data-ready] .card-hint { opacity: 1; }

.label { position: relative; width: min(420px, calc(100vw - 48px)); padding: 22px 24px 18px; border-radius: 10px; color: #2d2440; transform: rotate(-2deg);
  background: linear-gradient(180deg, #fffdf6, #f5eedc); box-shadow: 0 30px 70px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(0, 0, 0, 0.06); }
.label-head { display: flex; justify-content: space-between; font-weight: 800; font-size: 0.8rem; letter-spacing: 0.12em; padding-bottom: 10px; border-bottom: 3px solid #2d2440; }
.label-row { display: grid; gap: 2px; padding: 12px 0; border-bottom: 1px dashed rgba(45, 36, 64, 0.35); }
.label-row small { font-weight: 800; font-size: 0.7rem; letter-spacing: 0.14em; opacity: 0.6; }
.label-row b { font-family: var(--serif); font-size: 1.25rem; }
.label-row.ship b { font-size: 1.9rem; min-height: 2.4rem; }
.label .hand { font-family: var(--hand); font-weight: 700; font-size: 1.8rem; color: #3b3fa0; line-height: 1.1; }
.label-foot .hand { font-size: 1.5rem; }
.blank-line { display: inline-block; width: 80%; border-bottom: 2px solid rgba(45, 36, 64, 0.5); }
.smudge { filter: blur(4px); opacity: 0.6; letter-spacing: -0.1em; }
.barcode { height: 52px; margin: 14px 0 8px; background: repeating-linear-gradient(90deg, #2d2440 0 2px, transparent 2px 5px, #2d2440 5px 9px, transparent 9px 11px, #2d2440 11px 12px, transparent 12px 16px); }
.label-foot { font-size: 0.8rem; font-weight: 700; opacity: 0.75; }
.stamp { position: absolute; right: 22px; bottom: 58px; padding: 7px 16px; border-radius: 8px; font-weight: 800; letter-spacing: 0.1em; color: #fff;
  background: #2f9e6a; box-shadow: 0 0 0 3px #fffdf6, 0 0 0 5px #2f9e6a, 0 6px 16px rgba(0, 0, 0, 0.25); transform: rotate(-12deg); animation: stamp 0.5s 0.5s cubic-bezier(.3, 1.6, .5, 1) backwards; }
.stamp.gold { background: #c98a1e; box-shadow: 0 0 0 3px #fffdf6, 0 0 0 5px #c98a1e, 0 6px 16px rgba(0, 0, 0, 0.25); }
@keyframes stamp { from { transform: rotate(-12deg) scale(2.4); opacity: 0; } }

.ledger { width: min(440px, calc(100vw - 48px)); padding: 28px 32px 30px; color: #3a2c3c; font-family: var(--hand); font-size: 1.55rem; line-height: 1.25;
  background: repeating-linear-gradient(180deg, transparent 0 36px, rgba(80, 110, 180, 0.2) 36px 37px), linear-gradient(180deg, #fbf4e0, #efe2c2);
  border-radius: 4px 14px 14px 4px; box-shadow: 0 30px 70px rgba(0, 0, 0, 0.5), inset 18px 0 20px -18px rgba(90, 60, 30, 0.5); transform: rotate(1.5deg); }
.ledger-title { font-family: var(--serif); font-size: 1.2rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; margin-bottom: 10px; color: #7a4a3a; }
.ledger p { margin: 0 0 6px; }
.ledger p span { opacity: 0.7; }
.ledger .done { text-decoration: line-through; text-decoration-color: rgba(58, 44, 60, 0.45); opacity: 0.7; }
.ledger .done::after { content: ' ✓'; color: #2f9e6a; }
.ledger .last { margin-top: 14px; font-size: 2rem; font-weight: 700; color: #2c2a7a; }
.ledger .note { font-size: 1.4rem; color: #6a4a5a; }

/* ─── Chapter pages ────────────────────────────────────────────── */
.chapter { position: absolute; inset: 0; z-index: 14; display: grid; place-items: center; perspective: 1400px; padding: 16px;
  background: radial-gradient(circle, rgba(20, 16, 50, 0.6), rgba(4, 4, 16, 0.92)); }
.page { position: relative; width: min(520px, 100%); aspect-ratio: 4 / 5; max-height: 80vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: 40px; text-align: center; color: var(--paper-ink); border-radius: 6px 22px 22px 6px; transform-origin: 0 50%;
  background: radial-gradient(circle at 50% 40%, #fffaf0, #f3e4c6 80%), #f3e4c6;
  box-shadow: 0 40px 100px rgba(0, 0, 0, 0.6), inset 26px 0 30px -24px rgba(110, 70, 40, 0.55), 0 0 0 1px rgba(160, 120, 70, 0.4), 0 0 80px rgba(255, 220, 140, 0.2); }
.page::before { content: ''; position: absolute; inset: 16px; border: 2px solid rgba(185, 140, 70, 0.45); border-radius: 4px 14px 14px 4px; pointer-events: none; }
.chapter.turn .page { animation: page-turn 1.1s cubic-bezier(.2, .7, .2, 1); }
@keyframes page-turn { from { transform: rotateY(-100deg); opacity: 0; } 60% { opacity: 1; } }
.chapter-num { margin: 0; font-family: var(--serif); font-size: 0.95rem; letter-spacing: 0.3em; text-transform: uppercase; color: #a8773c; }
.chapter h2 { margin: 0; font-family: var(--serif); font-style: italic; font-weight: 600; font-size: clamp(2.2rem, 7vw, 3.4rem); line-height: 1.05; color: #4a2f55; }
.ornament { color: #c79a4c; letter-spacing: 0.3em; font-size: 0.9rem; }
.chapter[data-ready] .page::after { content: 'Tippen zum Umblättern'; position: absolute; bottom: 30px; font-family: var(--sans); font-weight: 700; font-size: 0.8rem; color: #a8773c; animation: fade-in 0.6s; }

/* ─── Title & end ──────────────────────────────────────────────── */
.title-screen, .end-screen { position: absolute; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px 16px; text-align: center; cursor: default; }
.title-screen { background: radial-gradient(ellipse at 50% 55%, rgba(10, 8, 40, 0.35), transparent 70%); transition: opacity 1.4s ease, visibility 1.4s; }
.title-screen.gone { opacity: 0; visibility: hidden; }
.end-screen { background: radial-gradient(circle, rgba(20, 16, 60, 0.7), rgba(4, 4, 16, 0.9)); animation: fade-in 1.6s ease-out; }
.title-pip { width: min(170px, 36vw); aspect-ratio: 1; filter: drop-shadow(0 0 40px rgba(255, 216, 77, 0.7)); animation: bob 3.4s ease-in-out infinite; }
.title-pip svg { width: 100%; height: 100%; }
.eyebrow { margin: 0; font-family: var(--serif); font-style: italic; font-size: 1.1rem; letter-spacing: 0.16em; color: rgba(255, 230, 180, 0.8); }
.title-screen h1, .end-screen h2 { margin: 0; font-family: var(--serif); font-weight: 700; line-height: 0.95; letter-spacing: -0.01em;
  font-size: clamp(3rem, 9vw, 6.2rem); background: linear-gradient(180deg, #fffbe8 10%, #ffd978 55%, #e9a93c); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 4px 30px rgba(255, 200, 100, 0.35)); text-wrap: balance; }
.title-screen h1 span { font-style: italic; font-weight: 400; font-size: 0.62em; }
.end-screen h2 { font-size: clamp(2.2rem, 6vw, 4rem); font-style: italic; }
.end-screen p:not(.eyebrow) { margin: 0; font-size: 1.1rem; color: rgba(255, 248, 236, 0.8); }
.title-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-top: 18px; }
.button { padding: 14px 28px; border-radius: 999px; border: 1px solid rgba(255, 230, 170, 0.45); background: rgba(20, 20, 60, 0.6); color: var(--ink);
  font-weight: 800; font-size: 1.05rem; cursor: pointer; text-decoration: none; transition: transform 0.2s, box-shadow 0.2s; }
.button.primary { background: linear-gradient(180deg, #ffe699, #f2b848); color: #3a2440; border-color: transparent; box-shadow: 0 0 40px rgba(255, 200, 90, 0.45); }
.button:hover, .button:focus-visible { transform: translateY(-2px); box-shadow: 0 0 50px rgba(255, 200, 90, 0.6); outline: none; }
.title-note { margin: 10px 0 0; font-size: 0.85rem; color: rgba(255, 248, 236, 0.6); }

@media (max-width: 640px) {
  .dialogue { padding: 26px 22px 22px; min-height: 120px; }
  .dialogue.narration { padding: 24px 22px; }
  .nameplate { left: 18px; }
  .prompt { top: 11vh; font-size: 0.95rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.2s !important; }
}
