/* ───────────────────────── base ───────────────────────── */
:root {
  --bg: #0b0d12;
  --fg: #c9cfdb;
  --dim: #6b7385;
  --accent: #8ec5ff;
  --gold: #ffd36e;
  --red: #ff5d6c;
  --green: #7fe0a0;
  --violet: #c49bff;
  --panel: rgb(18 21 30 / 0.72);
  --line: rgb(255 255 255 / 0.07);
  --font: ui-monospace, "JetBrains Mono", "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  --tint: transparent; /* per-room colour wash, set from JS */
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.6 var(--font);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 250px;
  gap: 0;
  overflow: hidden;
}

body::before { /* room tint wash, cross-fades between rooms */
  content: "";
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at 50% 120%, var(--tint), transparent 70%);
  transition: background 1.2s ease;
  pointer-events: none;
  z-index: 0;
}

#ambient {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

/* No z-index on #app: its children join the root stacking context so the
   HUD and prompt can sit above the darkness overlay while the log stays under it. */
#app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
  padding: 0 1.25rem;
}

/* ───────────────────────── HUD ───────────────────────── */
#hud, #combat, #typing, #prompt { position: relative; z-index: 6; }
#output { position: relative; z-index: 1; }

#hud {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.8rem 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  background: linear-gradient(90deg, var(--gold), #ff9a5a, var(--violet), var(--gold));
  background-size: 300% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 8s linear infinite;
}

@keyframes shimmer { to { background-position: 300% 0; } }

.bars { display: flex; gap: 1.2rem; flex: 1; }

.bar { display: flex; align-items: center; gap: 0.5rem; }
.bar .label { color: var(--dim); font-size: 11px; }
.bar .val { color: var(--dim); font-size: 11px; min-width: 4.5em; font-variant-numeric: tabular-nums; }

.track {
  position: relative;
  width: 120px;
  height: 7px;
  border-radius: 4px;
  background: rgb(255 255 255 / 0.06);
  overflow: hidden;
}
.track .fill, .track .lag {
  position: absolute;
  inset: 0;
  transform-origin: left;
  transform: scaleX(var(--pct, 1));
  border-radius: inherit;
}
.track .fill { transition: transform 0.25s ease-out; }
.track .lag { background: #fff; opacity: 0.55; transition: transform 0.6s ease-in 0.35s; }
.hp .fill { background: linear-gradient(90deg, #ff4d6a, #ff8a5c); }
.hp.low .fill { animation: pulse-low 0.9s ease-in-out infinite; }
.xp .fill { background: linear-gradient(90deg, #6aa8ff, var(--violet)); }

@keyframes pulse-low { 50% { filter: brightness(1.8); } }

#room-name {
  color: var(--gold);
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* ───────────────────────── combat bar ───────────────────────── */
#combat {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 0.6rem;
  padding: 0.5rem 0.9rem;
  border: 1px solid rgb(255 93 108 / 0.35);
  border-radius: 8px;
  background: linear-gradient(90deg, rgb(255 60 80 / 0.12), transparent);
  animation: combat-in 0.35s cubic-bezier(.2,1.4,.4,1) both;
}
#combat[hidden] { display: none; }
#combat.out { animation: combat-out 0.4s ease-in both; }
#combat .foe-name { color: var(--red); font-weight: 700; letter-spacing: 0.08em; }
#combat .track { flex: 1; height: 9px; width: auto; }
#combat .fill { background: linear-gradient(90deg, #b3122e, var(--red)); }

@keyframes combat-in { from { opacity: 0; transform: translateY(-12px) scale(0.97); } }
@keyframes combat-out { to { opacity: 0; transform: translateY(-8px); } }

/* ───────────────────────── output log ───────────────────────── */
#output {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0 0.5rem;
  scrollbar-width: thin;
  scrollbar-color: #2a2f3c transparent;
  mask-image: linear-gradient(to bottom, transparent 0, #000 2.5rem);
  overflow-anchor: none;
}

.entry {
  margin: 0 0 0.55rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: entry-in 0.35s ease-out both;
  transition: opacity 0.8s, filter 0.8s;
}
.entry.past { opacity: 0.38; filter: saturate(0.4); }
.entry.past:hover { opacity: 0.8; }

@keyframes entry-in { from { opacity: 0; transform: translateY(6px); } }

.entry.echo { color: var(--dim); }
.entry.echo::before { content: "› "; color: var(--accent); }
.entry.sys { color: var(--dim); font-size: 13px; }
.entry.err { color: #ff9aa5; }
.entry.good { color: var(--green); }
.entry.combat { color: #e8c3c7; }
.entry.combat.you-hit { color: #ffe1a8; }
.entry.combat.crit { color: var(--gold); font-weight: 700; }
.entry.combat.hurt { color: #ff8b97; }

/* word-by-word reveal for room descriptions */
.reveal .w {
  display: inline-block;
  white-space: pre;
  animation: word-in 0.5s ease-out both;
  animation-delay: calc(var(--w) * 14ms);
}
@keyframes word-in { from { opacity: 0; filter: blur(4px); transform: translateY(3px); } }

/* room title banner */
.room-title {
  display: block;
  margin-top: 0.4rem;
  font-size: 1.35em;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.06em;
  animation: title-in 0.9s cubic-bezier(.2,.8,.2,1) both;
}
.room-title::after {
  content: "";
  display: block;
  height: 1px;
  margin-top: 0.25rem;
  background: linear-gradient(90deg, var(--gold), transparent);
  transform-origin: left;
  animation: rule-in 1.1s 0.15s cubic-bezier(.2,.8,.2,1) both;
}
@keyframes title-in { from { letter-spacing: 0.5em; opacity: 0; filter: blur(6px); } }
@keyframes rule-in { from { transform: scaleX(0); } }

.exits { color: var(--dim); }

/* ───────────────────────── tokens ───────────────────────── */
.tok { cursor: pointer; border-radius: 3px; transition: background 0.15s, text-shadow 0.15s; }
.tok:hover { background: rgb(255 255 255 / 0.07); }
.exit { color: var(--accent); text-decoration: underline dotted rgb(142 197 255 / 0.5); text-underline-offset: 3px; }
.item { color: var(--gold); }
.mob { color: var(--red); font-weight: 600; }
.npc { color: var(--green); }
.player { color: var(--c, var(--violet)); font-weight: 600; }
.cmd { color: var(--accent); }
.dim { color: var(--dim); }
.big { font-size: 1.6em; font-weight: 800; display: inline-block; }

/* ───────────────────────── text effects ─────────────────────────
   Letter effects wrap each character in <span class="ch" style="--i:n">. */
.ch { display: inline-block; white-space: pre; }

.fx-wave .ch { animation: wave 1.8s ease-in-out infinite; animation-delay: calc(var(--i) * -0.12s); }
@keyframes wave { 50% { transform: translateY(-0.22em); } }

.fx-fire .ch {
  animation: fire 0.9s ease-in-out infinite alternate;
  animation-delay: calc(var(--i) * -0.17s);
}
@keyframes fire {
  0%   { color: #ffd36e; text-shadow: 0 0 6px #ff7b00; transform: translateY(0); }
  50%  { color: #ff8a3c; text-shadow: 0 -2px 10px #ff3d00; transform: translateY(-1px) skewX(-4deg); }
  100% { color: #ff4d3a; text-shadow: 0 -4px 14px #ff2200; transform: translateY(-2px) skewX(4deg); }
}

.fx-shake .ch { animation: shake 0.28s steps(2) infinite; animation-delay: calc(var(--i) * -0.07s); }
@keyframes shake {
  0% { transform: translate(0.5px, -0.5px) rotate(-1deg); }
  50% { transform: translate(-0.5px, 0.6px) rotate(1.5deg); }
  100% { transform: translate(0.4px, 0.3px); }
}

.fx-ghost .ch { animation: ghost 3.2s ease-in-out infinite; animation-delay: calc(var(--i) * -0.33s); color: #b8c6ff; }
@keyframes ghost { 50% { opacity: 0.15; filter: blur(2px); transform: translateY(-2px); } }

.fx-glow { color: #fff6c9; animation: glow 2.4s ease-in-out infinite; }
@keyframes glow {
  0%, 100% { text-shadow: 0 0 4px rgb(255 220 120 / 0.5); }
  50% { text-shadow: 0 0 14px rgb(255 220 120 / 0.95), 0 0 30px rgb(255 180 80 / 0.5); }
}

.fx-rainbow .ch { animation: rainbow 2.5s linear infinite; animation-delay: calc(var(--i) * -0.1s); }
@keyframes rainbow {
  0% { color: #ff6b6b; } 20% { color: #ffd36e; } 40% { color: #7fe0a0; }
  60% { color: #6ec8ff; } 80% { color: #c49bff; } 100% { color: #ff6b6b; }
}

.fx-frost {
  background: linear-gradient(100deg, #9fd8ff 20%, #fff 40%, #9fd8ff 60%);
  background-size: 250% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: frost 3s linear infinite;
}
@keyframes frost { from { background-position: 100% 0; } to { background-position: -150% 0; } }

.fx-blood .ch { color: #d0213c; animation: drip 2.6s ease-in infinite; animation-delay: calc(var(--i) * -0.41s); }
@keyframes drip { 0%, 70% { transform: none; } 85% { transform: translateY(0.12em) scaleY(1.15); } }

.fx-glitch { position: relative; display: inline-block; color: #e0f0ff; }
.fx-glitch::before, .fx-glitch::after {
  content: attr(data-text);
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.fx-glitch::before { color: #ff3d6e; animation: glitch 2.2s steps(1) infinite; }
.fx-glitch::after { color: #3df2ff; animation: glitch 1.7s steps(1) infinite reverse; }
@keyframes glitch {
  0%   { clip-path: inset(0 0 85% 0); transform: translate(-2px, 0); }
  10%  { clip-path: inset(40% 0 40% 0); transform: translate(2px, 0); }
  20%  { clip-path: inset(80% 0 0 0); transform: translate(-1px, 0); }
  30%, 100% { clip-path: inset(0 0 100% 0); transform: none; }
}

/* ───────────────────────── prompt ───────────────────────── */
#typing {
  height: 1.3rem;
  font-size: 12px;
  color: var(--dim);
  font-style: italic;
  transition: opacity 0.3s;
}
#typing:empty { opacity: 0; }
#typing .dots::after { content: "…"; animation: dots 1.2s steps(4) infinite; display: inline-block; width: 1.2em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } }

#prompt {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0 0 1rem;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  backdrop-filter: blur(6px);
  transition: border-color 0.2s, box-shadow 0.2s;
}
#prompt:focus-within { border-color: rgb(142 197 255 / 0.35); box-shadow: 0 0 0 3px rgb(142 197 255 / 0.08), 0 0 30px rgb(142 197 255 / 0.06); }
#prompt.nope { animation: nope 0.35s; }
@keyframes nope {
  25% { transform: translateX(-5px); border-color: rgb(255 93 108 / 0.6); }
  75% { transform: translateX(5px); border-color: rgb(255 93 108 / 0.6); }
}

.caret { color: var(--accent); font-weight: 700; animation: caret 1.4s ease-in-out infinite; }
@keyframes caret { 50% { opacity: 0.35; } }

.input-wrap { position: relative; flex: 1; }
#cmd, #ghost {
  font: inherit;
  letter-spacing: inherit;
  width: 100%;
  padding: 0;
  border: 0;
}
#cmd { position: relative; background: transparent; color: var(--fg); outline: none; caret-color: var(--accent); }
#cmd::placeholder { color: #444b5a; }
#ghost { position: absolute; inset: 0; color: #4a5266; white-space: pre; pointer-events: none; overflow: hidden; }

/* ───────────────────────── sidebar ───────────────────────── */
#side {
  position: relative;
  z-index: 1;
  height: 100dvh;
  overflow-y: auto;
  padding: 1rem 1rem 1rem 0;
  font-size: 12px;
}
#side section {
  margin-bottom: 1rem;
  padding: 0.8rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  backdrop-filter: blur(6px);
}
#side h2 {
  margin: 0 0 0.5rem;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--dim);
}
#side ul { list-style: none; margin: 0; padding: 0; }
#side li { padding: 0.1rem 0; animation: entry-in 0.4s ease-out both; }
#side li.leaving { animation: li-out 0.35s ease-in both; }
@keyframes li-out { to { opacity: 0; transform: translateX(10px); } }
#who li::before { content: "●"; margin-right: 0.45em; color: var(--green); font-size: 0.7em; vertical-align: 0.15em; }

/* minimap: an absolutely positioned grid of room nodes */
#map { position: relative; height: 170px; }
#map .node {
  position: absolute;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-radius: 4px;
  background: #2a3040;
  border: 1px solid #3a4256;
  transition: background 0.4s, box-shadow 0.4s, transform 0.4s;
}
#map .node.seen { background: #3b4560; }
#map .node.here {
  background: var(--gold);
  border-color: var(--gold);
  box-shadow: 0 0 12px var(--gold);
  transform: scale(1.25);
  animation: here 1.6s ease-in-out infinite;
}
#map .node.unknown { opacity: 0.25; }
@keyframes here { 50% { box-shadow: 0 0 4px var(--gold); } }
#map .edge { position: absolute; height: 2px; background: #2a3040; transform-origin: 0 50%; }
#map .label {
  position: absolute;
  transform: translate(-50%, 9px);
  font-size: 9px;
  color: var(--dim);
  white-space: nowrap;
  pointer-events: none;
}

/* ───────────────────────── overlays ───────────────────────── */
#darkness {
  position: fixed;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  opacity: 0;
  transition: opacity 1.4s ease;
  --mx: 50%;
  --my: 80%;
  --r: 110px;
  background: radial-gradient(circle at var(--mx) var(--my), transparent 0, rgb(0 0 0 / 0.55) calc(var(--r) * 0.6), rgb(2 2 4 / 0.985) var(--r));
}
#darkness.on { opacity: 1; }
#darkness.torch {
  --r: 340px;
  background: radial-gradient(circle at var(--mx) var(--my), rgb(255 150 50 / 0.07) 0, rgb(0 0 0 / 0.35) calc(var(--r) * 0.55), rgb(2 2 4 / 0.95) var(--r));
  animation: torch 0.18s steps(2) infinite alternate;
}
@keyframes torch { to { --r: 325px; } }
@property --r { syntax: "<length>"; inherits: true; initial-value: 110px; }

#vignette {
  position: fixed;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  box-shadow: inset 0 0 160px rgb(255 0 30 / 0.7);
  opacity: 0;
}
#vignette.hit { animation: vignette 0.5s ease-out; }
#vignette.heal { box-shadow: inset 0 0 160px rgb(80 255 160 / 0.5); animation: vignette 1s ease-out; }
#vignette.lowhp { opacity: 0.35; animation: heartbeat 1.1s ease-in-out infinite; }
@keyframes vignette { 0% { opacity: 1; } 100% { opacity: 0; } }
@keyframes heartbeat { 0%, 100% { opacity: 0.2; } 15% { opacity: 0.55; } 30% { opacity: 0.25; } 45% { opacity: 0.45; } }

#fx { position: fixed; inset: 0; z-index: 7; pointer-events: none; overflow: hidden; }

/* floating damage / heal numbers */
.float {
  position: absolute;
  font-weight: 800;
  font-size: 18px;
  text-shadow: 0 2px 6px rgb(0 0 0 / 0.8);
  animation: float-up 1.1s cubic-bezier(.2,.7,.3,1) forwards;
  white-space: nowrap;
}
.float.crit { font-size: 28px; color: var(--gold); animation-name: float-crit; }
.float.dmg { color: #fff; }
.float.hurt { color: var(--red); }
.float.heal { color: var(--green); }
.float.miss { color: var(--dim); font-size: 14px; font-style: italic; }
.float.xp { color: var(--violet); font-size: 14px; }
@keyframes float-up {
  0% { opacity: 0; transform: translate(-50%, 0) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -14px) scale(1.15); }
  100% { opacity: 0; transform: translate(calc(-50% + var(--dx, 0px)), -70px) scale(1); }
}
@keyframes float-crit {
  0% { opacity: 0; transform: translate(-50%, 0) scale(2.5) rotate(-8deg); }
  15% { opacity: 1; transform: translate(-50%, -10px) scale(1) rotate(-4deg); }
  100% { opacity: 0; transform: translate(calc(-50% + var(--dx, 0px)), -80px) scale(1.1) rotate(2deg); }
}

/* letters that crumble away when a monster dies */
.crumble { position: fixed; z-index: 7; pointer-events: none; color: var(--red); font-weight: 700; animation: crumble 1.3s cubic-bezier(.4,0,1,1) forwards; }
@keyframes crumble {
  to { opacity: 0; transform: translate(var(--dx), var(--dy)) rotate(var(--rot)); filter: blur(1px); }
}

/* screen shake */
/* applied to #app's children, not #app, so no stacking context is created */
body.shake #app > * { animation: screen-shake 0.4s cubic-bezier(.36,.07,.19,.97); }
body.shake-big #app > * { animation: screen-shake-big 0.6s cubic-bezier(.36,.07,.19,.97); }
@keyframes screen-shake {
  10%, 90% { transform: translate(-1px, 1px); }
  20%, 80% { transform: translate(3px, -1px); }
  30%, 50%, 70% { transform: translate(-4px, 2px); }
  40%, 60% { transform: translate(4px, -2px); }
}
@keyframes screen-shake-big {
  10%, 90% { transform: translate(-2px, 2px) rotate(-0.3deg); }
  20%, 80% { transform: translate(6px, -3px) rotate(0.4deg); }
  30%, 50%, 70% { transform: translate(-8px, 4px) rotate(-0.5deg); }
  40%, 60% { transform: translate(8px, -4px) rotate(0.3deg); }
}

/* death: world drains of colour */
body.dead #app, body.dead #side { filter: grayscale(1) brightness(0.6); transition: filter 1.5s; }
body #app, body #side { transition: filter 1.5s; }

.death-card {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-size: clamp(2rem, 7vw, 4.5rem);
  font-weight: 800;
  letter-spacing: 0.2em;
  color: #b3122e;
  text-shadow: 0 0 30px rgb(179 18 46 / 0.6);
  animation: death-card 3.5s ease-in-out forwards;
}
@keyframes death-card {
  0% { opacity: 0; letter-spacing: 1em; filter: blur(10px); }
  25%, 70% { opacity: 1; letter-spacing: 0.2em; filter: none; }
  100% { opacity: 0; filter: blur(6px); }
}

.levelup-card {
  position: fixed;
  left: 50%;
  top: 35%;
  z-index: 8;
  pointer-events: none;
  font-size: 2.4rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  color: var(--gold);
  text-shadow: 0 0 20px var(--gold), 0 0 60px #ff9a5a;
  animation: levelup 2.4s cubic-bezier(.2,.9,.3,1) forwards;
}
@keyframes levelup {
  0% { opacity: 0; transform: translate(-50%, 20px) scale(0.5); }
  20% { opacity: 1; transform: translate(-50%, 0) scale(1.1); }
  30%, 75% { transform: translate(-50%, 0) scale(1); opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -40px) scale(1.05); }
}

/* speech: chat lines from other players */
.entry.say .player::after, .entry.chat .player::after { content: ""; }
.entry.say { padding-left: 0.8rem; border-left: 2px solid var(--c, var(--violet)); }
.entry.chat { color: #9fb3d9; }
.entry.chat .chan { color: #5f7ab0; }
.entry.shout { font-size: 1.15em; }
.entry.shout .msg { display: inline-block; animation: shout 0.5s cubic-bezier(.2,2,.4,1) both; }
@keyframes shout { from { transform: scale(1.8); opacity: 0; filter: blur(3px); } }
.entry.emote { font-style: italic; color: #b9a8d8; }
.entry.ambient { color: #7f8aa3; font-style: italic; }

/* ───────────────────────── responsive / a11y ───────────────────────── */
@media (max-width: 900px) {
  body { grid-template-columns: 1fr; }
  #side { display: none; }
  #app { padding: 0 0.8rem; }
  #hud { gap: 0.8rem; flex-wrap: wrap; }
  .track { width: 70px; }
}

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

.entry.title-line { font-size: 1.9em; font-weight: 800; letter-spacing: 0.08em; margin: 0.6rem 0 0; }
