:root {
  --bg: #07070c;
  --ink: #c9c4b8;
  --dim: #2c2f45;
  --torch: #ffb84d;
  --hp: #ff4d5e;
  --xp: #8a7dff;
  --gold: #ffd34d;
  --font: ui-monospace, "JetBrains Mono", "Fira Code", "Cascadia Code", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  touch-action: manipulation;
}

#app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: radial-gradient(ellipse at 50% 40%, #12121f 0%, var(--bg) 70%);
}

/* CRT-ish scanlines + vignette over everything. */
body::before,
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}
body::before {
  background: repeating-linear-gradient(to bottom, rgb(255 255 255 / 0.025) 0 1px, transparent 1px 3px);
  mix-blend-mode: overlay;
}
body::after {
  background: radial-gradient(ellipse at center, transparent 55%, rgb(0 0 0 / 0.75) 100%);
  transition: box-shadow 0.6s;
}
body.low-hp::after {
  animation: heartbeat 1.1s ease-in-out infinite;
}
@keyframes heartbeat {
  0%, 100% { box-shadow: inset 0 0 60px 10px rgb(200 0 20 / 0.25); }
  15% { box-shadow: inset 0 0 120px 30px rgb(220 0 30 / 0.55); }
  30% { box-shadow: inset 0 0 70px 10px rgb(200 0 20 / 0.3); }
  45% { box-shadow: inset 0 0 110px 25px rgb(220 0 30 / 0.45); }
}

/* ───────────── HUD ───────────── */

.hud {
  display: flex;
  align-items: center;
  gap: 1.4rem;
  padding: 0.6rem 1.2rem;
  border-bottom: 1px solid #1c1c2c;
  background: linear-gradient(to bottom, #0d0d17, #0a0a12);
  font-size: 0.85rem;
  flex-wrap: wrap;
  z-index: 10;
}
.brand {
  font-weight: 800;
  letter-spacing: 0.4em;
  color: var(--torch);
  text-shadow: 0 0 8px rgb(255 160 60 / 0.6);
}
.meter { display: flex; align-items: center; gap: 0.5rem; }
.meter .label { color: #77738a; font-size: 0.75rem; }
.meter .bar {
  position: relative;
  width: clamp(80px, 14vw, 180px);
  height: 10px;
  background: #16151f;
  border: 1px solid #2a2838;
  border-radius: 2px;
  overflow: hidden;
}
.meter .fill, .meter .ghost {
  position: absolute;
  inset: 0 auto 0 0;
  width: 100%;
}
.hp .fill {
  background: linear-gradient(to bottom, #ff7a86, var(--hp) 50%, #b8223a);
  box-shadow: 0 0 10px rgb(255 77 94 / 0.6);
  transition: width 0.18s ease-out;
}
.hp .ghost {
  background: #fff3d6;
  transition: width 0.5s cubic-bezier(.6,0,.4,1) 0.35s;
}
.xp .fill {
  background: linear-gradient(to bottom, #b5abff, var(--xp));
  box-shadow: 0 0 8px rgb(138 125 255 / 0.6);
  transition: width 0.4s ease-out;
}
.meter .num { min-width: 4.5em; font-variant-numeric: tabular-nums; display: inline-block; }
.stats { display: flex; gap: 1rem; margin-left: auto; color: #8d889c; }
.stats b { color: var(--ink); margin-left: 0.25em; font-weight: 600; }
.stats .gold b { color: var(--gold); }

/* ───────────── message log ───────────── */

.log {
  height: 8.2em;
  padding: 0.5rem 1.2rem 0.7rem;
  border-top: 1px solid #1c1c2c;
  background: linear-gradient(to top, #0d0d17, #0a0a12);
  font-size: 0.82rem;
  line-height: 1.32;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
  z-index: 10;
}
.msg {
  animation: msg-in 0.28s cubic-bezier(.2,.8,.3,1);
  opacity: 0.45;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg:nth-last-child(1) { opacity: 1; }
.msg:nth-last-child(2) { opacity: 0.8; }
.msg:nth-last-child(3) { opacity: 0.62; }
.log.dim-old .msg:not(:last-child) { opacity: 0.25; }
.msg[data-count]::after { content: " " attr(data-count); color: #77738a; }
.msg.good { color: #8fe3a5; }
.msg.bad { color: #ff7f7f; }
.msg.crit { color: #ffd34d; text-shadow: 0 0 8px rgb(255 200 60 / 0.5); }
.msg.loot { color: #f2cf73; }
.msg.lore { color: #a99cf2; font-style: italic; }
@keyframes msg-in {
  from { transform: translateX(-12px); opacity: 0; filter: blur(3px); }
}

/* ───────────── play field ───────────── */

.play { position: relative; overflow: hidden; min-height: 0; }
.viewport { position: absolute; inset: 0; overflow: hidden; cursor: crosshair; }
.shaker { position: absolute; inset: 0; }
.stage {
  position: absolute;
  left: 0;
  top: 0;
  --cell: 24px;
  font-size: calc(var(--cell) * 0.8);
  line-height: var(--cell);
  transition: transform 0.22s cubic-bezier(.2,.7,.3,1);
}
.layer { position: absolute; inset: 0; }
.tiles { display: grid; grid-auto-rows: var(--cell); }
.items, .actors, .fx { pointer-events: none; }

.hurt-flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at center, transparent 30%, rgb(255 0 30 / 0.6));
  z-index: 5;
}

/* Warm flickering pool of light following the player. */
.torchlight {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(var(--cell) * 18);
  height: calc(var(--cell) * 18);
  margin: calc(var(--cell) * -9) 0 0 calc(var(--cell) * -9);
  border-radius: 50%;
  background: radial-gradient(circle, rgb(255 170 70 / 0.16) 0%, rgb(255 120 40 / 0.07) 35%, transparent 65%);
  mix-blend-mode: screen;
  pointer-events: none;
  transition: transform 0.12s ease-out;
}
.torchlight::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: inherit;
  animation: flicker 2.3s infinite;
}
@keyframes flicker {
  0%, 100% { opacity: 0.6; scale: 1; }
  12% { opacity: 1; scale: 1.04; }
  20% { opacity: 0.75; scale: 0.98; }
  38% { opacity: 0.95; scale: 1.02; }
  55% { opacity: 0.55; scale: 0.97; }
  70% { opacity: 1; scale: 1.03; }
  85% { opacity: 0.8; scale: 1; }
}

/* ───────────── tiles ───────────── */

.tiles span {
  width: var(--cell);
  height: var(--cell);
  text-align: center;
  color: transparent;
  transition: color 0.35s, opacity 0.35s, text-shadow 0.35s;
}

/* Lit (visible) cells fade with distance from the torch. */
.tiles .v { color: var(--c, var(--ink)); }
.tiles .l1 { opacity: 1; }
.tiles .l2 { opacity: 0.86; }
.tiles .l3 { opacity: 0.68; }
.tiles .l4 { opacity: 0.5; }
/* Remembered cells: cold, desaturated memory. */
.tiles .e { color: var(--dim); opacity: 1; text-shadow: none; }

.t-wall { --c: #8a7f6c; }
.t-wall.v { background: rgb(138 127 108 / 0.06); }
.t-wall.e { background: rgb(60 60 90 / 0.05); }
.t-floor { --c: #5d586e; }
.t-rubble { --c: #6b6152; }
.t-door { --c: #d28a3c; font-weight: 700; }
.t-door.v { text-shadow: 0 0 6px rgb(210 138 60 / 0.5); }
.t-pillar { --c: #a39a88; font-weight: 700; }
.t-stairs { --c: #fff2b0; font-weight: 800; }
.t-stairs.v {
  text-shadow: 0 0 10px #ffd34d, 0 0 22px rgb(255 211 77 / 0.5);
  animation: stairs-pulse 2s ease-in-out infinite;
}
.t-stairs.e { color: #5c5a3a; }
@keyframes stairs-pulse { 50% { text-shadow: 0 0 4px #ffd34d; } }

.t-water { --c: #3f8fd8; }
.t-water.v {
  animation: water 3.2s ease-in-out infinite;
  animation-delay: var(--phase);
  text-shadow: 0 0 6px rgb(63 143 216 / 0.5);
  background: rgb(40 100 200 / 0.08);
}
@keyframes water {
  0%, 100% { color: #3f8fd8; transform: translateX(0); }
  50% { color: #79c4ff; transform: translateX(8%); }
}
.t-grass { --c: #4f9a52; }
.t-grass.v { animation: sway 4s ease-in-out infinite; animation-delay: var(--phase); }
@keyframes sway {
  0%, 100% { transform: skewX(0); }
  50% { transform: skewX(-14deg); }
}

.tiles .blood.v { --c: #8c2230; text-shadow: 0 0 4px rgb(160 20 40 / 0.4); }
.tiles .decal.v { --c: #9e3a3a; }

/* ───────────── entities ───────────── */

.ent {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell);
  height: var(--cell);
  text-align: center;
  transform: translate(calc(var(--x) * var(--cell)), calc(var(--y) * var(--cell)));
  transition: transform 0.11s cubic-bezier(.3,.7,.4,1), opacity 0.25s;
  opacity: 0;
}
.ent .g { display: inline-block; width: 100%; font-weight: 700; }
.ent.seen { opacity: 1; }

.item.remembered { opacity: 0.3; filter: grayscale(1); }
.item.seen .g { animation: bob 1.6s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-12%); } }
.i-potion { color: #ff6ad5; text-shadow: 0 0 8px rgb(255 106 213 / 0.7); }
.i-gold { color: var(--gold); text-shadow: 0 0 8px rgb(255 211 77 / 0.7); }
.i-weapon { color: #9fe6ff; text-shadow: 0 0 8px rgb(159 230 255 / 0.7); }
.i-armor { color: #c8d0e0; text-shadow: 0 0 8px rgb(200 208 224 / 0.6); }

.player { z-index: 2; }
.player .g {
  color: #fff3d0;
  text-shadow: 0 0 6px var(--torch), 0 0 16px rgb(255 140 40 / 0.6);
  animation: breathe 2.6s ease-in-out infinite;
}
@keyframes breathe { 50% { text-shadow: 0 0 3px var(--torch), 0 0 10px rgb(255 140 40 / 0.4); } }

.m-rat .g { color: #b59a7a; }
.m-bat .g { color: #b78cff; animation: flutter 0.5s ease-in-out infinite alternate; }
@keyframes flutter {
  from { transform: translateY(-10%) scaleX(1); }
  to { transform: translateY(8%) scaleX(0.75); }
}
.m-goblin .g { color: #7fd35a; text-shadow: 0 0 6px rgb(127 211 90 / 0.4); }
.m-skeleton .g { color: #e9e4d4; text-shadow: 0 0 6px rgb(233 228 212 / 0.35); }
.m-orc .g { color: #e0763a; text-shadow: 0 0 6px rgb(224 118 58 / 0.4); }
.m-wraith .g {
  color: #7ae0ff;
  text-shadow: 0 0 10px #39b8ff, 0 0 20px rgb(57 184 255 / 0.5);
  animation: wraith 2.4s ease-in-out infinite;
}
@keyframes wraith {
  0%, 100% { opacity: 0.9; transform: translateY(0); }
  40% { opacity: 0.45; transform: translateY(-8%); }
  60% { opacity: 1; }
}
/* Awake monsters glare. */
.actor.awake:not(.player) .g { filter: drop-shadow(0 0 3px rgb(255 60 60 / 0.55)); }
.actor.wounded:not(.player) .g { animation: tremble 0.35s steps(2) infinite; }
@keyframes tremble { 50% { transform: translateX(4%); } }

/* Tiny health pip under damaged monsters. */
.hpbar {
  position: absolute;
  left: 15%;
  bottom: 4%;
  width: 70%;
  height: 2px;
  background: linear-gradient(to right, #ff4d5e calc(var(--hp, 1) * 100%), rgb(255 255 255 / 0.12) 0);
  opacity: 0;
  transition: opacity 0.3s;
}
.actor.awake .hpbar { opacity: 0.85; }

/* ───────────── effects ───────────── */

.fx {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  text-align: center;
  pointer-events: none;
  will-change: transform, opacity;
}
.float { display: flex; justify-content: center; font-weight: 800; z-index: 3; white-space: nowrap; font-size: 0.8em; }
.dmg { color: #fff; text-shadow: 0 0 4px #000, 0 0 8px rgb(255 255 255 / 0.5); }
.dmg-crit { color: #ffd34d; text-shadow: 0 0 6px #ff8a00, 0 0 14px #ff5a00; }
.dmg-player { color: #ff4d5e; text-shadow: 0 0 6px #ff0022, 0 1px 0 #000; }
.miss { color: #8a8698; font-style: italic; font-weight: 400; font-size: 0.6em; }
.heal { color: #7dffa8; text-shadow: 0 0 8px #2dff6a; }
.lvl { color: #c9bfff; text-shadow: 0 0 8px #8a7dff, 0 0 18px #6a5dff; font-size: 0.7em; letter-spacing: 0.15em; }

.particle { font-size: 0.6em; font-weight: 700; }
.p-blood { color: #d8344a; }
.p-blood-player { color: #ff2a3f; text-shadow: 0 0 4px #ff0022; }
.p-heal { color: #7dffa8; text-shadow: 0 0 6px #2dff6a; }
.p-lvl { color: #c9bfff; text-shadow: 0 0 6px #8a7dff; }

.shard { font-weight: 700; }
.shard.player { color: #fff3d0; text-shadow: 0 0 8px var(--torch); }
.shard.m-rat { color: #b59a7a; }
.shard.m-bat { color: #b78cff; }
.shard.m-goblin { color: #7fd35a; }
.shard.m-skeleton { color: #e9e4d4; }
.shard.m-orc { color: #e0763a; }
.shard.m-wraith { color: #7ae0ff; }

.ring {
  border-radius: 50%;
  border: 2px solid currentColor;
  box-shadow: 0 0 12px currentColor, inset 0 0 12px currentColor;
}
.ring.lvl { color: #8a7dff; }

.notice {
  color: #ff5a5a;
  font-weight: 900;
  text-shadow: 0 0 6px #ff0000;
  z-index: 4;
}

/* ───────────── overlays ───────────── */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at center, rgb(10 8 20 / 0.82), rgb(3 3 8 / 0.97));
  backdrop-filter: blur(3px);
  transition: opacity 0.45s, visibility 0.45s;
}
.overlay.hidden { opacity: 0; visibility: hidden; }
.panel { text-align: center; padding: 2rem; animation: panel-in 0.8s cubic-bezier(.2,.8,.3,1); }
@keyframes panel-in { from { opacity: 0; transform: scale(0.94); filter: blur(8px); } }

.title {
  margin: 0;
  font-size: clamp(3rem, 12vw, 7rem);
  font-weight: 900;
  letter-spacing: 0.2em;
  color: #ffe3a8;
}
.title span {
  display: inline-block;
  text-shadow: 0 0 12px var(--torch), 0 0 40px rgb(255 120 30 / 0.5);
  animation: letter-in 0.9s cubic-bezier(.2,.9,.3,1.3) both, glitch 5s steps(1) infinite;
  animation-delay: calc(var(--i) * 0.09s), calc(var(--i) * 0.7s + 1.5s);
}
@keyframes letter-in {
  from { opacity: 0; transform: translateY(-0.6em) scale(1.6); filter: blur(10px); }
}
@keyframes glitch {
  0%, 92%, 100% { transform: none; color: inherit; }
  93% { transform: translate(-3px, 1px) skewX(10deg); color: #7ae0ff; }
  95% { transform: translate(3px, -1px); color: #ff4d5e; }
  97% { transform: none; opacity: 0.6; }
}
.title.dead { color: #ff6a6a; }
.title.dead span { text-shadow: 0 0 12px #ff1a2e, 0 0 40px rgb(255 0 30 / 0.5); }

.subtitle { color: #8d86a8; letter-spacing: 0.3em; text-transform: lowercase; margin: 0.5rem 0 2rem; }
.controls { color: #8d889c; font-size: 0.85rem; line-height: 2; }
.controls .at { color: #fff3d0; text-shadow: 0 0 6px var(--torch); }
kbd {
  display: inline-block;
  padding: 0 0.4em;
  border: 1px solid #3a3750;
  border-bottom-width: 2px;
  border-radius: 3px;
  background: #15141f;
  color: #d8d2e8;
  font-family: inherit;
  font-size: 0.85em;
  line-height: 1.5;
}
.prompt {
  margin-top: 2.2rem;
  color: var(--torch);
  letter-spacing: 0.25em;
  animation: blink 1.6s ease-in-out infinite;
}
@keyframes blink { 50% { opacity: 0.25; } }
.summary {
  display: grid;
  grid-template-columns: repeat(5, auto);
  gap: 1.5rem;
  justify-content: center;
  margin-top: 2rem;
}
.summary div { display: flex; flex-direction: column; gap: 0.3rem; }
.summary span { color: #77738a; font-size: 0.75rem; letter-spacing: 0.2em; }
.summary b { font-size: 1.6rem; color: #f0e6d0; }

@media (max-width: 640px) {
  .hud { gap: 0.8rem; padding: 0.5rem 0.7rem; font-size: 0.75rem; }
  .brand { display: none; }
  .stats { margin-left: 0; gap: 0.6rem; }
  .log { height: 6.6em; font-size: 0.72rem; padding: 0.4rem 0.7rem; }
  .summary { grid-template-columns: repeat(3, auto); }
}
@media (hover: hover) { .controls .touch { display: none; } }

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