:root {
  --bg: #0d0e14;
  --bg-alt: #151823;
  --grid: #1f2333;
  --tile: #232838;
  --tile-edge: #2e3448;
  --player: #f5d06b;
  --enemy: #e24b4b;
  --enemy-strong: #b32d7a;
  --trap: #8a5cf6;
  --loot: #4bd4a0;
  --text: #e7ecf3;
  --muted: #8891a8;
  --danger: #e24b4b;
  --shadow: 0 2px 0 rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

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

body {
  background: radial-gradient(circle at 50% 0%, #1b1f2e 0%, var(--bg) 60%);
  color: var(--text);
  font-family: 'Courier New', ui-monospace, monospace;
  font-weight: bold;
  letter-spacing: 0.02em;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 16px;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.app {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  background: var(--bg-alt);
  padding: 10px;
  border: 2px solid var(--grid);
  border-radius: 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat .label {
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.15em;
}

.stat span:last-child {
  font-size: 20px;
  color: var(--text);
}

.stat.hp-low span:last-child {
  color: var(--danger);
  animation: pulse 0.6s ease-in-out infinite alternate;
}

@keyframes pulse {
  from { opacity: 0.6; }
  to { opacity: 1; }
}

.next-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--bg-alt);
  border: 1px dashed var(--grid);
  border-radius: 6px;
  padding: 8px 12px;
}

.next-caption {
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.2em;
}

.next-sprite {
  width: 32px;
  height: 32px;
  background: var(--tile);
  border: 1px solid var(--tile-edge);
  border-radius: 4px;
  padding: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.next-sprite svg {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.next-name {
  font-size: 12px;
  color: var(--text);
  letter-spacing: 0.15em;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--grid);
  border: 2px solid #2a2f42;
  border-radius: 10px;
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 6px;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.4);
}

.cell {
  background: var(--tile);
  border-radius: 6px;
  border: 1px solid var(--tile-edge);
  position: relative;
}

.entity {
  position: absolute;
  width: calc((100% - 8px * 2 - 6px * 4) / 5);
  height: calc((100% - 8px * 2 - 6px * 4) / 5);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: transform 0.14s ease-out, opacity 0.18s ease-out;
  will-change: transform;
  pointer-events: none;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.5));
}

.entity .sprite {
  width: 92%;
  height: 92%;
  display: flex;
  align-items: center;
  justify-content: center;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.entity .sprite svg {
  width: 100%;
  height: 100%;
  display: block;
}

.entity.player {
  z-index: 3;
}

.entity.player::before {
  content: '';
  position: absolute;
  inset: 6%;
  border-radius: 6px;
  background: radial-gradient(circle, rgba(245, 208, 107, 0.25), transparent 70%);
  z-index: -1;
}

.entity.enemy {
  z-index: 2;
}

.entity.enemy::before {
  content: '';
  position: absolute;
  inset: 10%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(226, 75, 75, 0.18), transparent 70%);
  z-index: -1;
}

.entity.enemy.tier-3::before,
.entity.enemy.tier-4::before,
.entity.enemy.tier-5::before {
  background: radial-gradient(circle, rgba(179, 45, 122, 0.25), transparent 70%);
}

.entity.trap {
  z-index: 1;
}

.entity.trap::before {
  content: '';
  position: absolute;
  inset: 20%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(138, 92, 246, 0.25), transparent 70%);
  z-index: -1;
}

.entity.loot {
  z-index: 2;
  animation: bob 1.6s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { translate: 0 0; }
  50% { translate: 0 -2px; }
}

.entity.loot::before {
  content: '';
  position: absolute;
  inset: 15%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(75, 212, 160, 0.3), transparent 70%);
  z-index: -1;
}

.entity .atk-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 14px;
  height: 14px;
  padding: 0 3px;
  background: #1a1a1a;
  color: #fff;
  border: 1px solid var(--enemy);
  border-radius: 50%;
  font-size: 9px;
  line-height: 12px;
  text-align: center;
  box-shadow: var(--shadow);
}

.entity.enemy.tier-3 .atk-badge,
.entity.enemy.tier-4 .atk-badge,
.entity.enemy.tier-5 .atk-badge {
  border-color: var(--enemy-strong);
}

.entity .atk-badge.trap-badge {
  border-color: var(--trap);
  color: #fff;
}


.entity .hpbar {
  position: absolute;
  bottom: 2px;
  left: 3px;
  right: 3px;
  height: 3px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 2px;
  overflow: hidden;
}

.entity .hpbar > div {
  height: 100%;
  background: #fff;
  transition: width 0.14s ease-out;
}

.entity.fading {
  opacity: 0;
  transform: translate(var(--tx, 0), var(--ty, 0)) scale(0.4);
}

.entity.hit {
  animation: hit 0.22s ease-out;
}

@keyframes hit {
  0% { filter: brightness(1.8); transform: translate(var(--tx, 0), var(--ty, 0)) scale(1.1); }
  100% { filter: brightness(1); transform: translate(var(--tx, 0), var(--ty, 0)) scale(1); }
}

.entity.spawn {
  animation: spawn 0.25s ease-out;
}

@keyframes spawn {
  from { opacity: 0; transform: translate(var(--tx, 0), var(--ty, 0)) scale(0.2); }
  to { opacity: 1; transform: translate(var(--tx, 0), var(--ty, 0)) scale(1); }
}

.log {
  margin: 0;
  min-height: 1.4em;
  padding: 8px 12px;
  background: var(--bg-alt);
  border: 1px solid var(--grid);
  border-radius: 6px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

.log.good { color: var(--loot); }
.log.bad { color: var(--danger); }
.log.info { color: #8ab4f8; }

.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
  align-items: center;
}

.icon-btn {
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button {
  font-family: inherit;
  font-weight: bold;
  font-size: 13px;
  letter-spacing: 0.15em;
  padding: 10px 18px;
  background: var(--tile);
  color: var(--text);
  border: 2px solid var(--tile-edge);
  border-radius: 6px;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
}

button:hover {
  background: var(--tile-edge);
}

button:active {
  transform: translateY(1px);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.overlay.hidden {
  display: none;
}

.overlay-panel {
  background: var(--bg-alt);
  border: 2px solid var(--grid);
  border-radius: 10px;
  padding: 24px 32px;
  text-align: center;
  max-width: 320px;
}

.overlay-panel h1 {
  margin: 0 0 12px;
  font-size: 28px;
  letter-spacing: 0.08em;
  color: var(--danger);
}

.overlay-panel.win h1 {
  color: var(--loot);
}

.overlay-panel p {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.board-shake {
  animation: shake 0.3s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* Rules modal */
.rules-panel {
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  text-align: left;
  padding: 20px 22px;
}

.rules-panel h1 {
  color: var(--player);
  text-align: center;
  margin-bottom: 4px;
}

.rules-panel .tagline {
  text-align: center;
  color: var(--muted);
  margin-bottom: 16px;
  font-size: 12px;
  font-style: italic;
}

.rules-section {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px dashed var(--grid);
}

.rules-section:last-of-type {
  border-bottom: none;
}

.rules-section h2 {
  font-size: 11px;
  color: var(--loot);
  letter-spacing: 0.2em;
  margin: 0 0 8px;
}

.rules-section ul {
  list-style: none;
  padding: 0;
  margin: 0;
  font-size: 12px;
  color: var(--text);
  line-height: 1.55;
}

.rules-section ul:not(.unit-list) li {
  padding-left: 14px;
  position: relative;
}

.rules-section ul:not(.unit-list) li::before {
  content: '▸';
  position: absolute;
  left: 0;
  color: var(--muted);
}

.rules-section .unit-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rules-section .unit-list li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.rules-section .unit-list li b {
  color: var(--text);
}

.rules-section .unit-list li small {
  color: var(--muted);
  font-size: 11px;
}

.sprite-box {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  background: var(--tile);
  border: 1px solid var(--tile-edge);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
}

.sprite-box svg {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.rule-note, .principle {
  margin: 8px 0 0;
  padding: 8px 10px;
  font-size: 11.5px;
  line-height: 1.55;
  color: var(--text);
  background: rgba(255, 255, 255, 0.03);
  border-left: 2px solid var(--loot);
  border-radius: 4px;
}

.principle {
  border-left-color: var(--player);
}

.rules-panel button {
  display: block;
  margin: 16px auto 0;
  background: var(--player);
  color: #1a1406;
  border-color: var(--player);
}

.rules-panel button:hover {
  background: #ffdf85;
  border-color: #ffdf85;
}
