/* ═══════════════════════════════════════════
   GRID & CELLS
   ═══════════════════════════════════════════ */
.grid-wrap {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--grid-cols), var(--cell-size));
  grid-template-rows: repeat(var(--grid-cols), var(--cell-size));
  gap: var(--cell-gap);
  z-index: var(--z-grid);
  padding: 6px;
  /* The whole board sits on the fog tone. Unexplored cells are transparent,
     so they and the gaps between them read as one seamless dark expanse —
     the map geometry never leaks before you've seen it. */
  background: var(--fog-color);
  /* Depth grading (desaturation + hue drift) — static per floor, so the
     filter layer rasterizes once, not every frame like the old full-screen
     filter. Brightness is handled by the viewport veil (camera.css). */
  filter: saturate(var(--depth-saturation, 1)) hue-rotate(var(--depth-hue, 0deg));
}

/* ─── Cells ─── */
.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.5);
  border-radius: var(--cell-radius);
  transition: background var(--t-fast), box-shadow var(--t-fast);
  position: relative;
}

/* SVG icons inside cells */
.cell svg.gi {
  width: calc(var(--cell-size) * 0.55);
  height: calc(var(--cell-size) * 0.55);
  filter: drop-shadow(0 0 3px currentColor);
}

/* Walls — textured brick/stone */
.cell--wall {
  background: var(--wall-texture), linear-gradient(145deg, var(--c-wall-light) 0%, var(--c-wall) 50%, var(--c-wall-dark) 100%);
  background-size: var(--cell-size) var(--cell-size), 100% 100%;
  border: 1px solid var(--c-wall-edge);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.06),
    inset 0 -2px 4px rgba(0,0,0,0.6),
    0 1px 3px rgba(0,0,0,0.4);
}

/* Wall cracks — appear on deeper floors */
.cell--wall-cracked { position: relative; }

.cell--wall-cracked::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.2;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 60% 60%;
}

.cell--wall-cracked[data-crack="1"]::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23887799' stroke-width='0.8'%3E%3Cpath d='M8 2L10 8L7 12L11 16L9 22'/%3E%3C/svg%3E");
}

.cell--wall-cracked[data-crack="2"]::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23887799' stroke-width='0.8'%3E%3Cpath d='M12 1L10 7L14 11L11 17L13 23'/%3E%3Cpath d='M10 7L6 10'/%3E%3C/svg%3E");
}

.cell--wall-cracked[data-crack="3"]::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23887799' stroke-width='0.8'%3E%3Cpath d='M6 3L9 9L7 14L10 20'/%3E%3Cpath d='M9 9L15 7L17 12'/%3E%3C/svg%3E");
}

/* Floors — subtle stone tile */
.cell--floor {
  background: var(--floor-texture);
  background-size: var(--cell-size) var(--cell-size);
  border: 1px solid var(--c-floor-edge);
}

/* Visited floor — scuff marks from passage */
.cell--visited {
  background:
    radial-gradient(circle, var(--c-trail) 15%, transparent 55%),
    var(--floor-texture) !important;
  background-size: 100% 100%, var(--cell-size) var(--cell-size);
}

/* Fog — fully transparent so the grid-wrap fog backdrop shows through with no
   seams. No border, no radius, no per-cell fill: unexplored area is one solid
   dark expanse, not a grid of empty tiles. */
.cell--fog {
  background: transparent !important;
  border: none !important;
  border-radius: 0 !important;
  box-shadow: none !important;
  transition: none !important;
}

.cell--fog * { visibility: hidden; }

/* Memory — seen before, out of sight now: a faint terrain silhouette.
   Entities aren't rendered here at all (what moves in the dark is unknown). */
.cell--memory {
  opacity: 0.32;
}

/* Walked memory cells stay a notch brighter — the footprint trail must keep
   reading as "I've been here" even far from the torch. */
.cell--memory.cell--visited {
  opacity: 0.5;
}

/* Multi-step darkness inside the vision radius: cells dim toward the edge
   of sight instead of the old binary lit/unlit. Opacity is compositor-cheap. */
.cell--dim1 { opacity: 0.78; }
.cell--dim2 { opacity: 0.55; }

.cell--player {
  background: var(--floor-texture);
  background-size: var(--cell-size) var(--cell-size);
  z-index: 2;
  box-shadow:
    0 0 14px var(--c-accent-glow),
    0 0 4px var(--c-glow),
    inset 0 0 10px var(--c-glow);
  border: 1px solid var(--c-glow);
}

.cell--portal {
  background: radial-gradient(circle, var(--c-glow) 0%, transparent 70%),
              var(--floor-texture);
  background-size: 100% 100%, var(--cell-size) var(--cell-size);
  animation: portal-pulse 2s ease-in-out infinite;
  z-index: 2;
}

.cell--portal svg.gi {
  width: calc(var(--cell-size) * 0.65);
  height: calc(var(--cell-size) * 0.65);
  animation: portal-icon-breathe 2s ease-in-out infinite;
  filter: drop-shadow(0 0 6px currentColor);
}

@keyframes portal-pulse {
  0%, 100% {
    box-shadow: 0 0 12px var(--c-glow),
                0 0 30px var(--c-glow),
                inset 0 0 10px var(--c-glow);
    border-color: var(--c-glow);
  }
  50% {
    box-shadow: 0 0 20px var(--c-glow),
                0 0 50px var(--c-glow),
                0 0 8px var(--c-glow),
                inset 0 0 16px var(--c-glow);
    border-color: var(--c-glow);
  }
}

@keyframes portal-icon-breathe {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(1.15); opacity: 1; }
}

/* ─── Player sprite — one absolute element layered over the cells.
   Position comes from --px/--py set by playerSprite.js; the calc keeps it
   aligned with any cell size, and the camera transform carries it along
   since it lives inside .grid-wrap. 6px = .grid-wrap padding. ─── */
.player-sprite {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell-size);
  height: var(--cell-size);
  z-index: 4;
  pointer-events: none;
  transform: translate(
    calc(6px + var(--px, 1) * (var(--cell-size) + var(--cell-gap))),
    calc(6px + var(--py, 1) * (var(--cell-size) + var(--cell-gap))));
  /* Step tween — the figure glides between cells instead of teleporting.
     Fresh floors don't animate: transitions never fire on newly inserted
     elements, and renderGrid re-inserts the sprite. */
  transition: transform 110ms ease-out;
}

.player-sprite__body {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 90ms ease-out;
}

/* Facing left — mirror the figure. */
.player-sprite--flip .player-sprite__body {
  transform: scaleX(-1);
}

.player-sprite__portrait {
  border-radius: 4px;
  filter: drop-shadow(0 0 3px rgba(255, 255, 255, 0.3));
}

/* ─── Pursuer sprites — same positioning contract as .player-sprite, one per
   visible chasing monster (monsterSprites.js). Slightly slower tween than the
   player: a lumbering pursuit reads better than a mirrored glide. ─── */
.monster-sprite {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell-size);
  height: var(--cell-size);
  z-index: 3;
  pointer-events: none;
  transform: translate(
    calc(6px + var(--px, 1) * (var(--cell-size) + var(--cell-gap))),
    calc(6px + var(--py, 1) * (var(--cell-size) + var(--cell-gap))));
  transition: transform 150ms ease-out, opacity 200ms ease-out;
}

/* Light falloff — mirror the cell dim bands so the sprite fades with its cell. */
.monster-sprite[data-band="1"] { opacity: 0.78; }
.monster-sprite[data-band="2"] { opacity: 0.55; }

.monster-sprite__body {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.monster-sprite__body svg.gi {
  width: calc(var(--cell-size) * 0.55);
  height: calc(var(--cell-size) * 0.55);
}

/* Facing left — mirror the figure. */
.monster-sprite--flip .monster-sprite__body {
  transform: scaleX(-1);
}

/* ─── Idle life — visible enemies breathe, with a rare shiver. Uses the
   independent scale/rotate properties so the facing flip (transform) is
   untouched. Only in-sight cells carry entity icons, so this adds a handful
   of animations at most — within the P1.5 budget. ─── */
.cell--idle svg.gi,
.monster-sprite__body svg.gi {
  animation:
    idle-breathe 3.4s ease-in-out infinite,
    idle-twitch 9s ease-in-out infinite;
  animation-delay: var(--idle-delay, 0s);
}

@keyframes idle-breathe {
  0%, 100% { scale: 1; }
  50% { scale: 1.04; }
}

/* Two quick shivers late in a long cycle — alive, not a metronome. */
@keyframes idle-twitch {
  0%, 87%, 95%, 100% { rotate: 0deg; }
  89% { rotate: -7deg; }
  92% { rotate: 6deg; }
}

/* ─── Boss ─── */
.cell--boss {
  z-index: 2;
  animation: boss-pulse 2.5s ease-in-out infinite;
}

.cell--boss svg.gi {
  width: calc(var(--cell-size) * 0.65);
  height: calc(var(--cell-size) * 0.65);
  filter: drop-shadow(0 0 6px currentColor);
}

.cell--boss-aura {
  background: radial-gradient(circle, rgba(255, 69, 0, 0.12) 0%, transparent 80%),
              var(--floor-texture);
  background-size: 100% 100%, var(--cell-size) var(--cell-size);
  border-color: rgba(255, 69, 0, 0.15);
}

@keyframes boss-pulse {
  0%, 100% {
    box-shadow: 0 0 10px rgba(255, 69, 0, 0.4),
                inset 0 0 8px rgba(255, 69, 0, 0.15);
    border-color: rgba(255, 69, 0, 0.3);
  }
  50% {
    box-shadow: 0 0 20px rgba(255, 69, 0, 0.7),
                0 0 40px rgba(255, 69, 0, 0.2),
                inset 0 0 14px rgba(255, 69, 0, 0.3);
    border-color: rgba(255, 69, 0, 0.5);
  }
}

/* Exit name label below icon */
.cell__label {
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.45rem;
  font-family: var(--font-ui);
  color: var(--c-accent);
  white-space: nowrap;
  text-shadow: 0 0 4px rgba(0, 0, 0, 0.9);
  pointer-events: none;
  letter-spacing: 0.3px;
  opacity: 0.8;
}

/* Echo of another player (P4.3) — a grave that isn't quite here. */
.cell--echo {
  opacity: 0.55;
}

.cell--echo svg.gi {
  filter: drop-shadow(0 0 5px rgba(138, 136, 168, 0.6));
}
