:root {
  --ink: #16110e;
  --ink-2: #221a15;
  --panel: rgba(24, 18, 14, 0.88);
  --cream: #f3e8d4;
  --cream-dim: #cbbfa9;
  --brick: #a9472d;
  --brick-hi: #cf6a4c;
  --brass: #d1a954;
  --brass-dim: #9c7c3c;
  --canopy: #7fa08a;
  --spirit: #bfe6ff;
  --danger: #e0705a;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 14px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

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

#app { position: fixed; inset: 0; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
#pano { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#pano:active { cursor: grabbing; }

/* Film grain + vignette; intensifies during a time slip */
#grain { position: absolute; inset: 0; pointer-events: none; opacity: 0.35; transition: opacity .8s;
  background:
    radial-gradient(ellipse at center, transparent 55%, rgba(0,0,0,.55) 100%),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 .5 0 0 0 0 .45 0 0 0 0 .4 0 0 0 .35 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>"); }
.timeslip #grain { opacity: 1; animation: flicker .18s steps(2) infinite; }
@keyframes flicker { 50% { transform: translate(-1px, 1px); } }

/* ---------- Hotspots ---------- */
#hotspots { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.hs { position: absolute; left: 0; top: 0; pointer-events: auto; border: 0; padding: 0; background: none; cursor: pointer;
  transform: translate(-50%, -50%); will-change: transform; outline-offset: 4px; }
.hs:focus-visible { outline: 2px solid var(--brass); border-radius: 50%; }
.hs .tip { position: absolute; left: 50%; top: calc(100% + 8px); transform: translateX(-50%); white-space: nowrap;
  background: var(--panel); border: 1px solid rgba(209,169,84,.4); padding: 5px 10px; border-radius: 999px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .15s; }
.hs:hover .tip, .hs:focus-visible .tip { opacity: 1; }

.hs-link { width: 54px; height: 54px; border-radius: 50%; display: grid; place-items: center;
  background: radial-gradient(circle, rgba(22,17,14,.55) 0 60%, rgba(22,17,14,0) 61%);
  box-shadow: inset 0 0 0 2px rgba(243,232,212,.9), 0 4px 18px rgba(0,0,0,.45); transition: box-shadow .15s; }
.hs-link svg { width: 24px; height: 24px; stroke: var(--cream); fill: none; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.hs-link:hover { box-shadow: inset 0 0 0 2px var(--brass), 0 0 0 6px rgba(209,169,84,.18), 0 4px 18px rgba(0,0,0,.45); }
.hs-link:hover svg { stroke: var(--brass); }

.hs-info { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-family: var(--serif); font-style: italic; font-weight: 700; font-size: 21px;
  color: var(--ink); background: radial-gradient(circle at 35% 30%, #f6dc9a, var(--brass) 55%, var(--brass-dim));
  box-shadow: 0 0 0 3px rgba(22,17,14,.5), 0 0 0 9px rgba(209,169,84,.18), 0 4px 14px rgba(0,0,0,.5); animation: pulse 2.6s ease-in-out infinite; }
.hs-info.read { animation: none; background: rgba(22,17,14,.65); color: var(--cream-dim); box-shadow: inset 0 0 0 2px rgba(203,191,169,.6); font-size: 16px; }
@keyframes pulse { 50% { box-shadow: 0 0 0 3px rgba(22,17,14,.5), 0 0 0 14px rgba(209,169,84,.05), 0 4px 14px rgba(0,0,0,.5); } }

.hs-glint { width: 34px; height: 34px; display: grid; place-items: center; }
.hs-glint svg { width: 30px; height: 30px; filter: drop-shadow(0 0 4px rgba(255,240,200,.95)); animation: twinkle 3.2s ease-in-out infinite; transform-origin: center; }
@keyframes twinkle { 0%, 70%, 100% { opacity: .35; transform: scale(.55) rotate(0deg); } 82% { opacity: 1; transform: scale(1.05) rotate(25deg); } }

.hs-spirit { width: 86px; height: 135px; }
.hs-spirit svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(0 0 10px rgba(191,230,255,.85)) drop-shadow(0 0 26px rgba(150,200,255,.5)); animation: float 4s ease-in-out infinite; }
.hs-spirit.master svg { filter: drop-shadow(0 0 10px rgba(255,210,130,.9)) drop-shadow(0 0 28px rgba(255,180,80,.5)); }
.hs-spirit .badge { position: absolute; top: -6px; right: -4px; min-width: 22px; height: 22px; border-radius: 11px; display: grid; place-items: center; font-weight: 800; font-size: 14px;
  background: var(--brass); color: var(--ink); box-shadow: 0 2px 8px rgba(0,0,0,.5); animation: bob 1.4s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-8px); } }
@keyframes bob { 50% { transform: translateY(-3px); } }

/* ---------- HUD ---------- */
#hud { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  padding: calc(12px + var(--safe-t)) 14px 0; pointer-events: none; }
#hud > * { pointer-events: auto; }
.hud-left { display: flex; gap: 6px; background: var(--panel); border: 1px solid rgba(209,169,84,.25); border-radius: var(--radius); padding: 7px 12px; backdrop-filter: blur(6px); }
.stat { display: flex; flex-direction: column; align-items: flex-start; padding: 0 8px; }
.stat + .stat { border-left: 1px solid rgba(243,232,212,.14); }
.stat-label { font-size: 10px; letter-spacing: .14em; text-transform: uppercase; color: var(--cream-dim); }
.stat > span:last-child { font-family: var(--serif); font-size: 20px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
#lanterns { display: flex; gap: 3px; padding-top: 3px; }
#lanterns svg { width: 16px; height: 20px; }
#lanterns .off { opacity: .22; filter: grayscale(1); }
#timer { position: absolute; left: 50%; transform: translateX(-50%); top: calc(12px + var(--safe-t)); background: var(--brick); border-radius: var(--radius); padding: 7px 16px; text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.4); display: flex; flex-direction: column; }
#timer .stat-label { color: #ffd9cc; }
#timer-val { font-family: var(--serif); font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
#timer.urgent { animation: urgent .5s ease-in-out infinite alternate; }
@keyframes urgent { to { background: #d2492b; } }
.hud-right { display: flex; gap: 6px; }
.icon-btn { width: 42px; height: 42px; border-radius: 12px; border: 1px solid rgba(209,169,84,.25); background: var(--panel); display: grid; place-items: center; cursor: pointer; backdrop-filter: blur(6px); }
.icon-btn:hover { border-color: var(--brass); }
.icon-btn:focus-visible, .btn:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--cream); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#btn-sound .snd-off { display: none; }
#btn-sound[aria-pressed="true"] .snd-on { display: none; }
#btn-sound[aria-pressed="true"] .snd-off { display: inline; }
.icon-btn .dot { position: absolute; }
#btn-journal { position: relative; }
#btn-journal.new::after { content: ""; position: absolute; top: 6px; right: 6px; width: 9px; height: 9px; border-radius: 50%; background: var(--brick-hi); box-shadow: 0 0 0 2px var(--ink); }

body:has(#dialog:not([hidden])) #scene-label { opacity: 0; }
#scene-label { transition: opacity .3s; position: absolute; left: 16px; bottom: calc(16px + var(--safe-b)); display: flex; flex-direction: column; pointer-events: none; text-shadow: 0 2px 12px rgba(0,0,0,.8); }
#scene-title { font-family: var(--serif); font-size: 22px; font-weight: 700; }
#scene-caption { font-size: 13px; color: var(--cream-dim); letter-spacing: .02em; }

#toasts { position: absolute; top: calc(78px + var(--safe-t)); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; width: min(92vw, 520px); }
.toast { background: var(--panel); border: 1px solid rgba(209,169,84,.35); padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: 0 8px 28px rgba(0,0,0,.4); animation: toast-in .25s ease-out; text-align: center; }
.toast b { color: var(--brass); }
.toast.out { opacity: 0; transform: translateY(-6px); transition: all .3s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

/* ---------- Panels ---------- */
.panel { position: absolute; z-index: 20; background: var(--panel); border: 1px solid rgba(209,169,84,.35); border-radius: 18px; box-shadow: 0 20px 60px rgba(0,0,0,.55); backdrop-filter: blur(10px); color: var(--cream); }
.panel h2 { font-family: var(--serif); margin: 0; font-size: 24px; line-height: 1.2; }
.panel p { line-height: 1.6; font-size: 16px; }
.kicker, .era { font-size: 11px; letter-spacing: .2em; text-transform: uppercase; color: var(--brass); font-weight: 600; }

#dialog { left: 50%; bottom: calc(22px + var(--safe-b)); transform: translateX(-50%); width: min(94vw, 640px); padding: 20px 24px 18px; animation: rise .3s ease-out; }
#dialog .dlg-head { display: flex; flex-direction: column; gap: 2px; }
#dialog.master h2 { color: #ffd79a; }
#dialog.spirit h2 { color: var(--spirit); }
#dlg-text { margin: 10px 0 14px; min-height: 3em; }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 14px); } }

.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end; }
.actions.center { justify-content: center; }
.actions.stack { flex-direction: column; align-items: stretch; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 10px 18px; border-radius: 12px; border: 1px solid rgba(243,232,212,.3); background: rgba(243,232,212,.06); cursor: pointer; font-weight: 600; text-decoration: none; color: var(--cream); font-size: 15px; transition: background .15s, border-color .15s, transform .1s; }
.btn:hover { border-color: var(--brass); background: rgba(209,169,84,.12); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brick); border-color: var(--brick-hi); color: #fff; }
.btn.primary:hover { background: var(--brick-hi); }
.btn.ghost { background: transparent; }
.btn.big { min-height: 52px; padding: 12px 30px; font-size: 17px; }
.btn.option { justify-content: flex-start; text-align: left; font-weight: 500; }
.btn.option .letter { font-family: var(--serif); font-weight: 700; color: var(--brass); width: 1.4em; flex: none; }
.btn.option.wrong { border-color: var(--danger); background: rgba(224,112,90,.15); }
.btn.option.right { border-color: var(--canopy); background: rgba(127,160,138,.25); }
.btn[disabled] { opacity: .5; cursor: default; }

/* Archive card styled like a railway ticket */
.ticket { left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(92vw, 480px); padding: 26px 28px 22px; background: linear-gradient(#f4ead6, #e9dcc2); color: #2b201a; border: 0; animation: pop .25s ease-out;
  -webkit-mask: radial-gradient(circle 11px at 0 58%, transparent 98%, #000) , radial-gradient(circle 11px at 100% 58%, transparent 98%, #000);
  -webkit-mask-composite: source-in; mask-composite: intersect; }
.ticket::before { content: ""; position: absolute; left: 20px; right: 20px; top: 58%; border-top: 2px dashed rgba(43,32,26,.25); }
.ticket .kicker { color: var(--brick); }
.ticket h2 { color: #2b201a; margin-top: 6px; }
.ticket p { color: #3a2d24; position: relative; z-index: 1; }
.ticket .btn { color: #fff; }
.ticket .btn.ghost { color: #2b201a; border-color: rgba(43,32,26,.3); }
@keyframes pop { from { opacity: 0; transform: translate(-50%, -46%) scale(.97); } }

.sheet { left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(94vw, 560px); max-height: min(86vh, 760px); overflow: auto; padding: 20px 24px 22px; animation: pop .25s ease-out; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin: -20px -24px 8px; padding: 16px 24px 10px; position: sticky; top: -20px; background: #1f1813; z-index: 2; border-radius: 18px 18px 0 0; }
.sheet h3 { font-family: var(--serif); font-size: 17px; margin: 18px 0 8px; color: var(--brass); }
.sheet h3 small { color: var(--cream-dim); font-family: var(--sans); font-weight: 400; font-size: 13px; }
.close { width: 38px; height: 38px; }
.hint { color: var(--cream-dim); font-size: 14px; margin: 0 0 8px; }
.j-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.j-list li { display: flex; gap: 10px; align-items: baseline; padding: 8px 10px; border-radius: 10px; background: rgba(243,232,212,.05); font-size: 14px; line-height: 1.45; }
.j-list li .st { flex: none; width: 76px; font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--cream-dim); }
.j-list li.freed .st { color: var(--canopy); }
.j-list li.seeking .st { color: var(--brass); }
.j-list li.locked { opacity: .5; }
.j-list.cards li { cursor: pointer; }
.j-list.cards li:hover { background: rgba(209,169,84,.12); }
.j-list li button { all: unset; cursor: pointer; flex: 1; }
.j-list li button:focus-visible { outline: 2px solid var(--brass); }
.restore-meter { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; font-size: 13px; color: var(--cream-dim); }
.bar { height: 8px; background: rgba(243,232,212,.1); border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--brick), var(--brass)); transition: width .6s; }
.restore-meter b { color: var(--cream); font-family: var(--serif); }
#journal .actions { margin-top: 20px; justify-content: space-between; }

#map-svg { width: 100%; height: auto; display: block; background: rgba(243,232,212,.04); border-radius: 12px; }
#map-svg .node { cursor: pointer; }
#map-svg .node circle { fill: var(--ink-2); stroke: var(--cream-dim); stroke-width: .6; }
#map-svg .node.visited circle { fill: var(--brass-dim); stroke: var(--brass); }
#map-svg .node.here circle { fill: var(--brick-hi); stroke: #fff; stroke-width: .9; }
#map-svg .node.locked { cursor: default; opacity: .55; }
#map-svg .node:focus-visible circle { stroke: #fff; stroke-width: 1.4; }
#map-svg text { font-size: 3.1px; fill: var(--cream); font-family: var(--sans); pointer-events: none; }
#map-svg .ghost { fill: var(--spirit); }

.help-list { padding: 0; list-style: none; display: grid; gap: 10px; font-size: 15px; line-height: 1.5; }
.help-list li { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.legend { width: 26px; height: 26px; border-radius: 50%; display: inline-grid; place-items: center; flex: none; }
.legend.link { box-shadow: inset 0 0 0 2px var(--cream); }
.legend.info { background: var(--brass); color: var(--ink); font-family: var(--serif); font-style: italic; font-weight: 700; }
.legend.spirit { background: radial-gradient(circle, #fff, var(--spirit) 40%, transparent 70%); }
.legend.glint { background: radial-gradient(circle, #fff 0 15%, rgba(255,240,200,.6) 30%, transparent 65%); }

/* ---------- Title / end screens ---------- */
.screen { position: absolute; inset: 0; z-index: 30; display: grid; place-items: center; padding: 24px; background: radial-gradient(ellipse at 50% 40%, rgba(22,17,14,.35), rgba(22,17,14,.92) 75%); overflow: auto; }
.title-card, .end-card { text-align: center; max-width: 640px; animation: fadeup .8s ease-out; }
.screen h1 { font-family: var(--serif); font-weight: 700; font-size: clamp(38px, 8vw, 76px); line-height: 1; margin: 12px 0 18px; letter-spacing: .01em; text-shadow: 0 4px 30px rgba(0,0,0,.6); }
.screen h1 span { color: var(--brass); font-style: italic; }
.lede { font-size: clamp(16px, 2.2vw, 19px); line-height: 1.55; color: var(--cream); max-width: 34em; margin: 0 auto 26px; }
.small { font-size: 13px; color: var(--cream-dim); margin-top: 18px; }
.credits { position: absolute; bottom: calc(14px + var(--safe-b)); left: 0; right: 0; text-align: center; font-size: 12px; color: var(--cream-dim); padding: 0 16px; }
@keyframes fadeup { from { opacity: 0; transform: translateY(12px); } }
.end-card h1 { font-size: clamp(30px, 6vw, 54px); }
#depot { width: min(90vw, 460px); height: auto; margin: 0 auto 14px; display: block; }
#depot .layer { opacity: 0; transition: opacity 1.2s; }
#depot .layer.on { opacity: 1; }
.end-stats { display: flex; justify-content: center; gap: 22px; margin: 6px 0 16px; flex-wrap: wrap; }
.end-stats div { display: flex; flex-direction: column; }
.end-stats b { font-family: var(--serif); font-size: 28px; color: var(--brass); }
.end-stats span { font-size: 12px; letter-spacing: .12em; text-transform: uppercase; color: var(--cream-dim); }

#fade { position: absolute; inset: 0; background: #000; opacity: 0; pointer-events: none; transition: opacity .35s ease; z-index: 15; }
#fade.on { opacity: 1; }
.flash #fade { background: #fff6e0; }
#loading { position: absolute; right: 16px; bottom: calc(16px + var(--safe-b)); display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--cream-dim); background: var(--panel); padding: 6px 12px; border-radius: 999px; z-index: 16; }
.spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(243,232,212,.25); border-top-color: var(--brass); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.noscript, .fatal { position: absolute; inset: 0; display: grid; place-items: center; padding: 30px; text-align: center; }

@media (max-width: 640px) {
  .hud-left { padding: 6px 6px; }
  .stat { padding: 0 6px; }
  .stat > span:last-child { font-size: 17px; }
  .icon-btn { width: 38px; height: 38px; }
  .hud-right { flex-wrap: wrap; justify-content: flex-end; max-width: 130px; }
  #btn-full { display: none; }
  #timer { top: calc(66px + var(--safe-t)); }
  #toasts { top: calc(128px + var(--safe-t)); }
  #scene-title { font-size: 18px; }
  #dialog { padding: 16px 16px 14px; }
  .panel p { font-size: 15px; }
  .hs-link { width: 48px; height: 48px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
  .hs-glint svg { opacity: .8; transform: none; }
}
