/* ===== RESET & BASE ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: #0a0a0a;
  color: #eee;
  font-family: 'Segoe UI Emoji', 'Noto Color Emoji', 'Yu Gothic', sans-serif;
  height: 100vh;
  height: 100dvh; /* Use dynamic viewport height for mobile browsers */
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== GAME CONTAINER ===== */
#game-container {
  width: 100%;
  height: 100%;
  max-width: 100vw;
  max-height: 100vh;
  max-height: 100dvh; /* Use dynamic viewport height for mobile browsers */
  display: flex;
  flex-direction: column;
  padding: 8px;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}

/* ===== TITLE ===== */
#game-title {
  text-align: center;
  font-size: clamp(16px, 3vw, 20px);
  padding: 4px 0;
  color: #ffcc00;
  letter-spacing: 2px;
  flex-shrink: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  max-width: 600px;
  margin: 0 auto;
  width: fit-content;
}

#version {
  font-size: 10px;
  color: #888;
  letter-spacing: 0;
  font-weight: normal;
  position: static;
}

#help-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #444;
  color: #ffcc00;
  border: 2px solid #ffcc00;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: all 0.2s ease;
  position: static;
  flex-shrink: 0;
}

#help-btn:hover {
  background: #ffcc00;
  color: #000;
  transform: scale(1.1);
}

#debug-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #444;
  color: #ff4444;
  border: 2px solid #ff4444;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: all 0.2s ease;
  position: static;
  flex-shrink: 0;
}

#debug-btn:hover {
  background: #ff4444;
  color: #000;
  transform: scale(1.1);
}

#lang-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #444;
  color: #66ccff;
  border: 2px solid #66ccff;
  font-size: 10px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: all 0.2s ease;
  position: static;
  flex-shrink: 0;
}

#lang-btn:hover {
  background: #66ccff;
  color: #000;
  transform: scale(1.1);
}

/* ===== MAIN CONTENT WRAPPER ===== */
#main-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 8px;
}

/* ===== STATUS PANEL ===== */
#status-panel {
  background: #1a1a2e;
  border-radius: 8px;
  padding: 6px 12px;
  flex-shrink: 0;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
  position: relative;
  z-index: 1;
}
#hp-display {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.stat-label {
  font-weight: bold;
  font-size: 14px;
  min-width: 32px;
}
#hp-bar-container {
  background: #333;
  border-radius: 4px;
  height: 18px;
  flex: 1;
  overflow: hidden;
}
#hp-bar {
  background: linear-gradient(90deg, #ff4444, #ff8800);
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
}
#hp-text {
  font-size: 13px;
  min-width: 70px;
  text-align: right;
}
#stamina-display {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  position: relative;
}
#stamina-bar-container {
  background: #333;
  border-radius: 4px;
  height: 14px;
  flex: 1;
  overflow: hidden;
  position: relative;
}
#stamina-bar {
  background: linear-gradient(90deg, #44ccff, #88ffff);
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
}
#stamina-text {
  font-size: 12px;
  min-width: 70px;
  text-align: right;
}
#stamina-display.stamina-depleted {
  animation: staminaDepleted 1s ease-in-out infinite;
}
@keyframes staminaDepleted {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
#stamina-bar-container.stamina-depleted {
  animation: staminaBarPulse 1s ease-in-out infinite;
  border: 2px solid #ff4444;
}
@keyframes staminaBarPulse {
  0%, 100% { 
    background: #660000;
    box-shadow: 0 0 5px rgba(255, 68, 68, 0.5);
  }
  50% { 
    background: #440000;
    box-shadow: 0 0 10px rgba(255, 68, 68, 0.8);
  }
}
#stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 4px;
  margin-top: 4px;
  padding-bottom: 6px;
  margin-bottom: 6px;
  border-bottom: 1px solid #333;
}
.stat-item-with-bar {
  display: grid;
  grid-template-columns: 20px 30px 18px 32px 1fr;
  align-items: center;
  gap: 4px;
  padding-right: 4px;
  border-right: 1px solid #333;
}
.stat-item-with-bar:nth-child(3),
.stat-item-with-bar:nth-child(6) {
  border-right: none;
}
.stat-icon {
  font-size: 14px;
  width: 20px;
}
.stat-value {
  font-size: 14px;
  color: #ffcc00;
  font-weight: bold;
  width: 30px;
  text-align: right;
}
.stat-buff {
  font-size: 11px;
  color: #88ff88;
  width: 18px;
  text-align: left;
}
.meter-text {
  font-size: 9px;
  color: #888;
  width: 32px;
  text-align: right;
}
.meter-bar-container {
  background: #333;
  border-radius: 2px;
  height: 8px;
  overflow: hidden;
  max-width: 100px;
}
.meter-bar {
  background: linear-gradient(90deg, #8844ff, #aa66ff);
  height: 100%;
  border-radius: 2px;
  transition: width 0.3s ease;
}

.meter-bar-container.level-up {
  animation: meterLevelUp 0.5s ease-out;
}

@keyframes meterLevelUp {
  0% {
    box-shadow: 0 0 0 rgba(170, 102, 255, 0);
  }
  50% {
    box-shadow: 0 0 15px rgba(170, 102, 255, 1), inset 0 0 10px rgba(255, 255, 255, 0.5);
  }
  100% {
    box-shadow: 0 0 0 rgba(170, 102, 255, 0);
  }
}
#info-row {
  display: flex;
  justify-content: space-around;
  margin-top: 4px;
  font-size: 11px;
}
.stat-item {
  white-space: nowrap;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 2px;
}
.stat-item strong {
  font-size: 15px;
  color: #ffcc00;
  font-weight: bold;
}

/* ===== SPELLS & BUFFS ROW ===== */
#spells-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 6px;
  margin-top: 6px;
  min-height: 56px;
}
.spell-slot {
  background: #2a2a3e;
  border: 1px solid #444;
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 60px;
  flex-shrink: 0;
  min-height: 24px;
  transition: all 0.2s ease;
}
.spell-slot:not(.empty):hover {
  background: #3a3a5e;
  border-color: #ffcc00;
  transform: translateY(-1px);
}
.spell-slot:not(.empty):active {
  transform: translateY(0);
  background: #4a4a6e;
}
.spell-slot.empty {
  opacity: 0.3;
  border-style: dashed;
}
.spell-slot .spell-key {
  background: #444;
  border-radius: 2px;
  padding: 1px 4px;
  font-size: 9px;
  color: #aaa;
}
#buffs-row {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  flex-wrap: wrap;
  min-height: 20px;
}
.buff-item {
  background: rgba(255, 204, 0, 0.1);
  border: 1px solid #ffcc00;
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 3px;
  transition: all 0.3s ease;
}
.buff-item.active {
  background: rgba(255, 204, 0, 0.15);
  border-color: #ffcc00;
}
.buff-item .buff-turns {
  color: #ffcc00;
  font-weight: bold;
}

/* ===== GAME AREA (Grid + Side Panel) ===== */
#game-area {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 8px;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

/* ===== GRID ===== */
#grid-container {
  display: grid;
  gap: 2px;
  background: #111;
  border-radius: 8px;
  padding: 4px;
  width: fit-content;
  height: fit-content;
  max-width: 100%;
  max-height: 100%;
  position: relative;
  z-index: 10;
}
.cell {
  width: 100%;
  height: 100%;
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid #333;
  border-radius: 3px;
  background: #1e1e1e;
  position: relative;
  overflow: visible;
  box-sizing: border-box;
}
.cell.is-wall {
  background: 
    linear-gradient(135deg, #555 25%, transparent 25%),
    linear-gradient(225deg, #555 25%, transparent 25%),
    linear-gradient(45deg, #555 25%, transparent 25%),
    linear-gradient(315deg, #555 25%, transparent 25%),
    #444;
  background-size: 8px 8px;
  background-position: 0 0, 4px 0, 4px -4px, 0px 4px;
  border-color: #555;
  box-shadow: inset 0 0 8px rgba(0,0,0,0.6);
}
.cell.has-player {
  background: #1a3a5c;
  border-color: #4a9eff;
  box-shadow: 0 0 6px rgba(74, 158, 255, 0.4);
}
/* Player inside wall - show wall pattern with player highlight */
.cell.is-wall.has-player {
  background: 
    linear-gradient(135deg, #444 25%, transparent 25%),
    linear-gradient(225deg, #444 25%, transparent 25%),
    linear-gradient(45deg, #444 25%, transparent 25%),
    linear-gradient(315deg, #444 25%, transparent 25%),
    #1a3a5c;
  background-size: 8px 8px;
  background-position: 0 0, 4px 0, 4px -4px, 0px 4px;
  border-color: #4a9eff;
  box-shadow: 
    inset 0 0 8px rgba(0,0,0,0.6),
    0 0 8px rgba(74, 158, 255, 0.6);
}
/* Passer monster inside wall - show wall pattern with monster highlight */
.cell.is-wall.has-monster {
  background: 
    linear-gradient(135deg, #444 25%, transparent 25%),
    linear-gradient(225deg, #444 25%, transparent 25%),
    linear-gradient(45deg, #444 25%, transparent 25%),
    linear-gradient(315deg, #444 25%, transparent 25%),
    #3a1a1a;
  background-size: 8px 8px;
  background-position: 0 0, 4px 0, 4px -4px, 0px 4px;
  border-color: #ff4a4a;
  box-shadow: 
    inset 0 0 8px rgba(0,0,0,0.6),
    0 0 8px rgba(255, 74, 74, 0.6);
}
.cell.has-player.stamina-depleted {
  /* Keep player cell appearance unchanged when stamina is depleted */
  /* Only stamina bar shows the depleted state */
}
@keyframes playerStaminaDepleted {
  0%, 100% { 
    box-shadow: 0 0 8px rgba(255, 74, 74, 0.6);
  }
  50% { 
    box-shadow: 0 0 15px rgba(255, 74, 74, 0.9);
  }
}
.cell.has-monster {
  background: #3a1a1a;
  border-color: #ff4a4a;
}
.cell.is-frozen {
  background: #1a2a3a;
  border-color: #4ac8ff;
  box-shadow: 0 0 8px rgba(74, 200, 255, 0.5);
  animation: frozenPulse 2s ease-in-out infinite;
}
@keyframes frozenPulse {
  0%, 100% { 
    box-shadow: 0 0 8px rgba(74, 200, 255, 0.5);
  }
  50% { 
    box-shadow: 0 0 15px rgba(74, 200, 255, 0.8);
  }
}
.cell .frozen-icon {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 0.8em;
  z-index: 3;
  filter: drop-shadow(0 0 2px rgba(74, 200, 255, 0.8));
}
.cell.is-asleep {
  background: #3a2a1a;
  border-color: #ff9944;
  box-shadow: 0 0 8px rgba(255, 153, 68, 0.5);
  animation: asleepPulse 2s ease-in-out infinite;
}
@keyframes asleepPulse {
  0%, 100% { 
    box-shadow: 0 0 8px rgba(255, 153, 68, 0.5);
  }
  50% { 
    box-shadow: 0 0 15px rgba(255, 153, 68, 0.8);
  }
}
.cell .asleep-icon {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 0.8em;
  z-index: 3;
  filter: drop-shadow(0 0 2px rgba(255, 153, 68, 0.8));
  animation: asleepFloat 2s ease-in-out infinite;
}
@keyframes asleepFloat {
  0%, 100% { 
    transform: translateY(0px) scale(1);
    opacity: 1;
  }
  50% { 
    transform: translateY(-4px) scale(1.1);
    opacity: 0.8;
  }
}
.cell.has-stairs {
  background: #1a3a1a;
  border-color: #4aff4a;
}
.cell .entity {
  font-size: 1.8em;
  line-height: 1;
  z-index: 2;
  transition: opacity 0.3s ease, filter 0.3s ease;
}
.cell .entity.inactive-turn {
  opacity: 0.3;
  filter: grayscale(80%);
}
.monster-hp-bar-container {
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  height: 4px;
  background: #333;
  border-radius: 2px;
  overflow: hidden;
  z-index: 3;
}
.monster-hp-bar {
  height: 100%;
  /* Background color is set dynamically via JavaScript */
  border-radius: 2px;
  transition: width 0.3s ease;
}
.cell .powerup-label {
  font-size: 0.55em;
  color: #aaa;
  line-height: 1.1;
  z-index: 1;
  text-align: center;
  max-width: 100%;
  padding: 0 1px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}
.cell .powerup-label.powerup-instant {
  /* Instant recovery items (HP, Stamina) - circular border */
  border: 2px solid #44ff88;
  border-radius: 50%;
  background: rgba(68, 255, 136, 0.1);
  padding: 2px;
  width: 85%;
  aspect-ratio: 1;
  justify-content: center;
}
.cell .powerup-label.powerup-stack {
  /* Stack items (ATK, DEF, MAG, MaxHP) - square border */
  border: 2px solid #ffaa44;
  border-radius: 4px;
  background: rgba(255, 170, 68, 0.1);
  padding: 2px;
}
.cell .powerup-label.powerup-chest {
  /* Spell chest - circular border with purple color */
  border: 2px solid #aa44ff;
  border-radius: 50%;
  background: rgba(170, 68, 255, 0.1);
  padding: 2px;
  width: 85%;
  aspect-ratio: 1;
  justify-content: center;
}
.cell .powerup-label.powerup-key {
  /* Key - diamond border with blue color */
  border: 2px solid #4488ff;
  border-radius: 4px;
  background: rgba(68, 136, 255, 0.1);
  padding: 2px;
  width: 70%;
  aspect-ratio: 1;
  justify-content: center;
  transform: rotate(45deg);
}
.cell .powerup-label.powerup-key .powerup-emoji {
  transform: rotate(-45deg);
}
.cell .powerup-emoji {
  font-size: 1.2em;
}
.cell .powerup-value {
  font-size: 0.9em;
  font-weight: bold;
  color: #ffcc66;
}

/* ===== SIDE PANEL (Log + Controls) ===== */
#side-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

/* ===== LOG PANEL ===== */
#log-panel {
  background: #111;
  color: #bbb;
  font-size: 11px;
  padding: 6px;
  border-radius: 8px;
  height: 150px; /* Fixed height to match D-PAD area */
  overflow-y: auto;
  line-height: 1.4;
  flex-shrink: 0;
}
.log-line {
  border-bottom: 1px solid #1a1a1a;
  padding: 1px 0;
}
.log-line.turn-even {
  background: rgba(255, 255, 255, 0.05);
}
.log-line.turn-odd {
  background: rgba(0, 0, 0, 0.3);
}
.log-line.log-new {
  animation: logFadeIn 0.3s ease-out;
}
@keyframes logFadeIn {
  0% {
    opacity: 0;
    transform: translateX(-10px);
    background: rgba(255, 204, 0, 0.3);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
.log-line.player-attack { color: #ffaa66; }
.log-line.monster-attack { color: #ff6666; }
.log-line.heal { color: #66ff66; }
.log-line.powerup { color: #ffcc66; }
.log-line.info { color: #66ccff; }
.log-line.kill { color: #ff66ff; }

/* ===== CONTROLS ===== */
#touch-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.dpad-middle-row {
  display: flex;
  gap: 3px;
}
.dpad-btn {
  width: 48px;
  height: 48px;
  font-size: 20px;
  border-radius: 8px;
  border: 2px solid #444;
  background: #222;
  color: #fff;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s;
}
.dpad-btn:active {
  background: #444;
}
.controls-hint {
  text-align: center;
  font-size: 10px;
  color: #555;
}

/* ===== GAME OVER ===== */
#game-over {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.88);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#game-over.active {
  display: flex;
}
#game-over h2 {
  font-size: 32px;
  color: #ff4444;
  margin-bottom: 16px;
}
#game-over p {
  font-size: 18px;
  margin: 4px;
}
#restart-btn {
  margin-top: 20px;
  padding: 12px 32px;
  font-size: 18px;
  border: 2px solid #ffcc00;
  background: #333;
  color: #ffcc00;
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}
#restart-btn:hover {
  background: #555;
}

/* ===== GAME CLEAR ===== */
#game-clear {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.88);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#game-clear.active {
  display: flex;
}
#game-clear h2 {
  font-size: 32px;
  color: #44ff44;
  margin-bottom: 16px;
}
#game-clear p {
  font-size: 18px;
  margin: 4px;
}
#clear-restart-btn {
  margin-top: 20px;
  padding: 12px 32px;
  font-size: 18px;
  border: 2px solid #44ff44;
  background: #333;
  color: #44ff44;
  border-radius: 8px;
  cursor: pointer;
  touch-action: manipulation;
}
#clear-restart-btn:hover {
  background: #555;
}

/* ===== HELP MENU ===== */
.help-panel {
  background: #1a1a2e;
  border-radius: 12px;
  padding: 24px;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.help-panel h2 {
  color: #ffcc00;
  margin-bottom: 16px;
  text-align: center;
}

.help-panel h3 {
  color: #66ccff;
  margin-top: 16px;
  margin-bottom: 8px;
  font-size: 16px;
}

.help-panel p {
  margin: 4px 0;
  line-height: 1.6;
  font-size: 14px;
}

.help-panel strong {
  color: #ffcc66;
}

.close-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  border: none;
  background: #444;
  color: #fff;
  font-size: 24px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #ff4444;
  transform: scale(1.1);
}

.help-content {
  margin-top: 8px;
}

/* ===== SPELL SELECTION ===== */
.overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: transparent;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 150;
  pointer-events: none;
}
.overlay.active {
  display: flex;
  pointer-events: auto;
}
/* Darken only the grid area when overlay is active */
#game-area.dimmed::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 10;
  pointer-events: none;
  border-radius: 8px;
}
.spell-selection-panel {
  background: #1a1a2e;
  border: 2px solid #ffcc00;
  border-radius: 12px;
  padding: 24px;
  max-width: 90%;
  text-align: center;
  position: relative;
  z-index: 151;
}
.spell-selection-panel h2 {
  color: #ffcc00;
  margin-bottom: 8px;
  font-size: 24px;
}
.spell-selection-panel p {
  color: #aaa;
  margin-bottom: 16px;
  font-size: 14px;
}
#spell-choices, #floor-bonus-choices {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}
.spell-card {
  background: #2a2a3e;
  border: 2px solid #444;
  border-radius: 8px;
  padding: 16px;
  min-width: 120px;
  max-width: 150px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.spell-card:hover {
  transform: translateY(-4px);
  border-color: #666;
  box-shadow: 0 4px 12px rgba(100, 100, 100, 0.3);
}
.spell-card.selected {
  transform: translateY(-4px);
  border-color: #ffcc00;
  box-shadow: 0 4px 12px rgba(255, 204, 0, 0.5);
  background: rgba(255, 204, 0, 0.1);
}
.spell-emoji {
  font-size: 48px;
  margin-bottom: 8px;
}
.spell-name {
  font-weight: bold;
  color: #ccc;
  margin-bottom: 4px;
  font-size: 14px;
}
.spell-card.selected .spell-name {
  color: #ffcc00;
}
.spell-desc {
  font-size: 11px;
  color: #aaa;
  line-height: 1.3;
}

/* ===== ANIMATIONS ===== */
@keyframes playerSlash {
  0%   { transform: scale(1) rotate(0deg); }
  25%  { transform: scale(1.5) rotate(20deg); }
  50%  { transform: scale(1.3) rotate(-10deg); }
  100% { transform: scale(1) rotate(0deg); }
}
@keyframes monsterSlash {
  0%   { transform: scale(1) rotate(0deg); }
  25%  { transform: scale(1.4) rotate(-15deg); }
  50%  { transform: scale(1.2) rotate(10deg); }
  100% { transform: scale(1) rotate(0deg); }
}
@keyframes takeDamage {
  0%   { background-color: inherit; }
  15%  { background-color: #ff0000; }
  30%  { background-color: #880000; }
  50%  { background-color: #ff2222; }
  70%  { background-color: #660000; }
  100% { background-color: inherit; }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15%  { transform: translateX(-4px); }
  30%  { transform: translateX(4px); }
  45%  { transform: translateX(-3px); }
  60%  { transform: translateX(3px); }
  75%  { transform: translateX(-2px); }
}
@keyframes killFade {
  0%   { opacity: 1; transform: scale(1); }
  40%  { opacity: 0.7; transform: scale(1.4); }
  100% { opacity: 0; transform: scale(0.3) rotate(30deg); }
}
@keyframes floatUp {
  0%   { opacity: 1; transform: translate(-50%, 0) scale(1); }
  40%  { opacity: 1; transform: translate(-50%, -15px) scale(1.3); }
  100% { opacity: 0; transform: translate(-50%, -50px) scale(0.8); }
}
@keyframes crushPowerup {
  0%   { opacity: 1; transform: scale(1) rotate(0deg); }
  25%  { opacity: 0.8; transform: scale(0.7) rotate(-15deg); }
  50%  { opacity: 0.6; transform: scale(0.4) rotate(15deg); }
  75%  { opacity: 0.3; transform: scale(0.2) rotate(-30deg); }
  100% { opacity: 0; transform: scale(0) rotate(45deg); }
}

@keyframes spellCast {
  0% {
    box-shadow: 
      0 0 0 0 rgba(138, 43, 226, 0.9),
      0 0 0 0 rgba(255, 0, 255, 0.7),
      inset 0 0 0 0 rgba(138, 43, 226, 0.5);
    background: radial-gradient(circle, rgba(138, 43, 226, 0.3), rgba(255, 0, 255, 0.1));
    transform: scale(1) rotate(0deg);
  }
  25% {
    box-shadow: 
      0 0 30px 15px rgba(138, 43, 226, 0.8),
      0 0 60px 30px rgba(255, 0, 255, 0.5),
      inset 0 0 20px 10px rgba(138, 43, 226, 0.6);
    background: radial-gradient(circle, rgba(138, 43, 226, 0.5), rgba(255, 0, 255, 0.3));
    transform: scale(1.15) rotate(90deg);
  }
  50% {
    box-shadow: 
      0 0 50px 25px rgba(138, 43, 226, 0.6),
      0 0 100px 50px rgba(255, 0, 255, 0.4),
      inset 0 0 30px 15px rgba(138, 43, 226, 0.7);
    background: radial-gradient(circle, rgba(138, 43, 226, 0.6), rgba(255, 0, 255, 0.4));
    transform: scale(1.25) rotate(180deg);
  }
  75% {
    box-shadow: 
      0 0 40px 20px rgba(138, 43, 226, 0.5),
      0 0 80px 40px rgba(255, 0, 255, 0.3),
      inset 0 0 25px 12px rgba(138, 43, 226, 0.5);
    background: radial-gradient(circle, rgba(138, 43, 226, 0.4), rgba(255, 0, 255, 0.2));
    transform: scale(1.1) rotate(270deg);
  }
  100% {
    box-shadow: 
      0 0 0 0 rgba(138, 43, 226, 0),
      0 0 0 0 rgba(255, 0, 255, 0),
      inset 0 0 0 0 rgba(138, 43, 226, 0);
    background: radial-gradient(circle, rgba(138, 43, 226, 0), rgba(255, 0, 255, 0));
    transform: scale(1) rotate(360deg);
  }
}

@keyframes spellPulse {
  0%, 100% {
    filter: brightness(1);
  }
  50% {
    filter: brightness(1.5) saturate(1.5);
  }
}

.anim-spell-cast {
  animation: 
    spellCast 0.6s ease-in-out,
    spellPulse 0.3s ease-in-out 2;
  position: relative;
  z-index: 10;
}

.anim-spell-cast::before {
  content: '✨';
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.5em;
  animation: sparkle 0.6s ease-out;
  pointer-events: none;
}

@keyframes sparkle {
  0% {
    opacity: 0;
    transform: translateX(-50%) translateY(0) scale(0);
  }
  50% {
    opacity: 1;
    transform: translateX(-50%) translateY(-10px) scale(1.5);
  }
  100% {
    opacity: 0;
    transform: translateX(-50%) translateY(-20px) scale(0.5);
  }
}

@keyframes crushFlash {
  0%   { background-color: inherit; }
  20%  { background-color: #886600; }
  40%  { background-color: #664400; }
  60%  { background-color: #886600; }
  100% { background-color: inherit; }
}
@keyframes monsterSpawn {
  0%   { opacity: 0; transform: scale(0) rotate(0deg); }
  30%  { opacity: 0.7; transform: scale(1.5) rotate(180deg); }
  60%  { opacity: 0.9; transform: scale(0.9) rotate(360deg); }
  100% { opacity: 1; transform: scale(1) rotate(360deg); }
}
@keyframes spawnFlash {
  0%   { background-color: inherit; box-shadow: 0 0 0 rgba(255, 100, 255, 0); }
  25%  { background-color: #ff44ff; box-shadow: 0 0 20px rgba(255, 100, 255, 0.8); }
  50%  { background-color: #cc22cc; box-shadow: 0 0 30px rgba(255, 100, 255, 1); }
  75%  { background-color: #ff44ff; box-shadow: 0 0 20px rgba(255, 100, 255, 0.8); }
  100% { background-color: inherit; box-shadow: 0 0 0 rgba(255, 100, 255, 0); }
}
@keyframes eliteSpawnFlash {
  0%   { background-color: inherit; box-shadow: 0 0 0 rgba(255, 215, 0, 0); }
  25%  { background-color: #ffd700; box-shadow: 0 0 25px rgba(255, 215, 0, 0.9); }
  50%  { background-color: #ffaa00; box-shadow: 0 0 35px rgba(255, 215, 0, 1); }
  75%  { background-color: #ffd700; box-shadow: 0 0 25px rgba(255, 215, 0, 0.9); }
  100% { background-color: inherit; box-shadow: 0 0 0 rgba(255, 215, 0, 0); }
}
@keyframes moveFlash {
  0%   { opacity: 1; transform: scale(1); }
  50%  { opacity: 0.6; transform: scale(1.15); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes slideIn {
  0%   { opacity: 0.3; transform: scale(0.7); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes trailFade {
  0%   { opacity: 0.4; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.5); }
}
.cell.anim-move .entity {
  animation: slideIn 0.15s ease-out;
}
.move-trail {
  animation: trailFade 0.15s ease-out forwards;
  position: absolute;
}
.cell.anim-monster-spawn {
  animation: spawnFlash 0.6s ease-out;
}
.cell.anim-monster-spawn.elite-spawn {
  animation: eliteSpawnFlash 0.6s ease-out;
}
.cell.anim-monster-spawn .entity {
  animation: monsterSpawn 0.6s ease-out;
}
/* Elite monster visual effect */
.entity.elite-monster {
  filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.8));
  animation: eliteGlow 2s ease-in-out infinite;
}
@keyframes eliteGlow {
  0%, 100% {
    filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.8));
  }
  50% {
    filter: drop-shadow(0 0 8px rgba(255, 215, 0, 1));
  }
}
.cell.anim-player-attack .entity {
  animation: playerSlash 0.35s ease-out;
}
.cell.anim-monster-attack .entity {
  animation: monsterSlash 0.35s ease-out;
}
.cell.anim-take-damage {
  animation: takeDamage 0.35s ease-out, shake 0.35s ease-out;
}
.cell.anim-kill .entity {
  animation: killFade 0.4s ease-out forwards;
}
.cell.anim-crush-powerup {
  animation: crushFlash 0.15s ease-out;
}
.cell.anim-crush-powerup .powerup-label {
  animation: crushPowerup 0.15s ease-out forwards;
}
.damage-popup {
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.4em;
  font-weight: bold;
  pointer-events: none;
  z-index: 9999;
  animation: floatUp 1.2s ease-out forwards;
  text-shadow: 0 0 6px rgba(0,0,0,0.9), 0 0 3px rgba(0,0,0,0.9);
  white-space: nowrap;
}
.damage-popup.player-dealt {
  color: #ffff44;
}
.damage-popup.critical-hit {
  color: #ff4400;
  font-size: 2em;
  top: -40px; /* Larger size needs more offset */
  animation: criticalFloat 1.2s ease-out forwards;
  text-shadow: 
    0 0 10px rgba(255, 68, 0, 1),
    0 0 20px rgba(255, 136, 0, 0.8),
    0 0 30px rgba(255, 204, 0, 0.6),
    0 0 6px rgba(0,0,0,0.9);
}
.damage-popup.monster-dealt {
  color: #ff4444;
}
.damage-popup.kill-text {
  color: #ff66ff;
  font-size: 0.9em;
}
.damage-popup.crush-text {
  color: #aa8844;
  font-size: 0.9em;
}
.damage-popup.powerup-text {
  color: #ffcc00;
  font-size: 1.6em;
  animation: floatUpBig 1.2s ease-out forwards;
}
.damage-popup.miss-text {
  color: #aaaaaa;
  font-size: 1.2em;
  font-style: italic;
}

.cell.anim-powerup {
  animation: powerupGlow 0.3s ease-out;
}

@keyframes powerupGlow {
  0% {
    box-shadow: 0 0 0 rgba(255, 204, 0, 0);
    background: #1a3a5c;
  }
  50% {
    box-shadow: 0 0 20px rgba(255, 204, 0, 0.8), 0 0 40px rgba(255, 204, 0, 0.4);
    background: #3a5a7c;
  }
  100% {
    box-shadow: 0 0 6px rgba(74, 158, 255, 0.4);
    background: #1a3a5c;
  }
}

@keyframes floatUpBig {
  0% {
    opacity: 1;
    transform: translateX(-50%) translateY(0) scale(1);
  }
  40% {
    opacity: 1;
    transform: translateX(-50%) translateY(-20px) scale(1.3);
  }
  100% {
    opacity: 0;
    transform: translateX(-50%) translateY(-70px) scale(1.3);
  }
}

@keyframes criticalFloat {
  0% {
    opacity: 1;
    transform: translateX(-50%) translateY(0) scale(1) rotate(-5deg);
  }
  25% {
    opacity: 1;
    transform: translateX(-50%) translateY(-10px) scale(1.3) rotate(5deg);
  }
  50% {
    opacity: 1;
    transform: translateX(-50%) translateY(-25px) scale(1.5) rotate(-3deg);
  }
  100% {
    opacity: 0;
    transform: translateX(-50%) translateY(-80px) scale(1.2) rotate(0deg);
  }
}

/* ===== RESPONSIVE: LANDSCAPE MODE ===== */
/* ===== RESPONSIVE: UNIFIED LAYOUT ===== */
/* All devices use the same layout: portrait style */
#main-content {
  flex-direction: column;
}

#game-area {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#side-panel {
  flex-shrink: 0;
  flex-direction: row;
  gap: 8px;
  align-items: stretch;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
  height: auto; /* Allow natural height based on content */
}

#log-panel {
  flex: 1;
  height: 150px; /* Match D-PAD height */
  font-size: 13px;
  padding: 4px;
}

#touch-controls {
  flex-shrink: 0;
  width: auto;
}

.controls-hint.desktop-only { display: none; }
  
  /* Compact status panel for mobile */
  #status-panel {
    padding: 2px 4px;
    font-size: 10px;
  }
  
  #hp-display, #stamina-display {
    gap: 3px;
    margin-bottom: 1px;
  }
  
  .stat-label {
    font-size: 11px;
    min-width: 20px;
  }
  
  #hp-bar-container, #stamina-bar-container {
    height: 12px;
  }
  
  #hp-text, #stamina-text {
    font-size: 10px;
    min-width: 55px;
  }
  
  #stats-row {
    gap: 2px;
    margin-top: 2px;
  }
  
  .stat-item-with-bar {
    gap: 2px;
    grid-template-columns: 16px 26px 16px 26px 1fr;
  }
  
  .stat-icon {
    font-size: 11px;
    width: 16px;
  }
  
  .stat-value {
    font-size: 11px;
    width: 26px;
  }
  
  .stat-buff {
    font-size: 9px;
    width: 16px;
  }
  
  .meter-text {
    font-size: 7px;
    width: 26px;
  }
  
  .meter-bar-container {
    height: 5px;
    max-width: 70px;
  }
  
  #spells-row {
    gap: 2px;
    margin-top: 2px;
    min-height: 56px; /* Fixed height for 2-row grid */
  }
  
  .spell-slot {
    padding: 2px 4px;
    font-size: 9px;
    min-height: 22px;
    gap: 2px;
  }
  
  .spell-slot .spell-key {
    font-size: 7px;
    padding: 0px 2px;
  }
  
  #buffs-row {
    gap: 2px;
    margin-top: 2px;
    min-height: 20px;
  }
  
  .buff-item {
    padding: 0px 3px;
    font-size: 8px;
  }
  
  .stat-item {
    font-size: 9px;
  }
  
  .stat-item strong {
    font-size: 11px;
  }
  
  /* Thinner borders for mobile */
  .cell .powerup-label.powerup-instant {
    border-width: 1px;
  }
  
  .cell .powerup-label.powerup-stack {
    border-width: 1px;
  }
  
  .cell .powerup-label.powerup-chest {
    border-width: 1px;
  }
  
  .cell .powerup-label.powerup-key {
    border-width: 1px;
  }
  
  .monster-hp-bar-container {
    height: 3px;
  }
  
  /* Spell selection panel for mobile */
  .spell-selection-panel {
    padding: 12px;
    max-width: 95%;
  }
  
  .spell-selection-panel h2 {
    font-size: 18px;
    margin-bottom: 6px;
  }
  
  .spell-selection-panel p {
    font-size: 12px;
    margin-bottom: 12px;
  }
  
  #spell-choices, #floor-bonus-choices {
    gap: 8px;
    flex-wrap: nowrap;
  }
  
  .spell-card {
    padding: 8px;
    min-width: 90px;
    max-width: 110px;
    flex: 1;
  }
  
  .spell-emoji {
    font-size: 32px;
    margin-bottom: 4px;
  }
  
  .spell-name {
    font-size: 11px;
    margin-bottom: 2px;
  }
  
  .spell-desc {
    font-size: 9px;
  }
}

/* ===== SMALL SCREENS ===== */
@media (max-height: 600px) {
  #game-title {
    font-size: 14px;
    padding: 2px 0;
  }

  #status-panel {
    padding: 4px 8px;
  }

  #log-panel {
    height: 50px; /* Reduced from 60px */
    font-size: 10px;
  }

  .dpad-btn {
    width: 36px; /* Reduced from 40px */
    height: 36px; /* Reduced from 40px */
    font-size: 16px; /* Reduced from 18px */
  }
}


/* ===== DEBUG MENU ===== */
.debug-panel {
  background: #1a1a2e;
  border: 2px solid #ff4444;
  border-radius: 12px;
  padding: 24px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
}

.debug-panel h2 {
  color: #ff4444;
  margin-bottom: 8px;
  font-size: 24px;
}

.debug-section {
  margin-bottom: 20px;
}

.debug-section-title {
  color: #ffcc66;
  font-size: 16px;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid #333;
}

.debug-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
  max-width: 800px;
  margin: 0 auto;
}

.debug-btn {
  background: #2a2a3e;
  border: 2px solid #ff4444;
  border-radius: 8px;
  padding: 12px 16px;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.debug-btn:hover {
  background: #3a3a4e;
  border-color: #ff6666;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 68, 68, 0.3);
}

.debug-btn.selected {
  background: #3a3a4e;
  border-color: #ff6666;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 68, 68, 0.5);
}

.debug-btn:active {
  transform: translateY(0);
}

/* ===== GA DEBUG TOAST ===== */
#ga-toast {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10000;
  pointer-events: none;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 20px;
  padding-bottom: 170px; /* Position above D-PAD controls */
}

#ga-toast .toast-box {
  background: rgba(0, 0, 0, 0.95);
  border: 2px solid #4CAF50;
  border-radius: 8px;
  padding: 12px 20px;
  color: #fff;
  font-size: 13px;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.7);
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.3s ease;
  pointer-events: none;
}

#ga-toast.show .toast-box {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

#ga-toast .toast-title {
  font-weight: bold;
  color: #4CAF50;
  margin-bottom: 4px;
  font-size: 14px;
}

#ga-toast .toast-content {
  font-size: 12px;
  color: #ccc;
  line-height: 1.4;
}

/* ===== HELP TABS ===== */
.help-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  border-bottom: 2px solid #333;
}

.help-tab {
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  padding: 8px 16px;
  color: #888;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  flex: 1;
}

.help-tab:hover {
  color: #aaa;
  background: rgba(255, 255, 255, 0.05);
}

.help-tab.active {
  color: #ffcc00;
  border-bottom-color: #ffcc00;
  font-weight: bold;
}

.help-tab-content {
  display: none;
}

.help-tab-content.active {
  display: block;
}

/* ===== AUTHOR ICON ===== */
.copyright-text {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author-icon {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  vertical-align: middle;
}

/* ===== SPELLS TAB ===== */
.spells-content {
  max-width: 100%;
}

.spell-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
  margin-bottom: 20px;
}

.spell-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: #2a2a3e;
  border: 1px solid #444;
  border-radius: 8px;
  padding: 12px;
  transition: all 0.2s ease;
}

.spell-item:hover {
  background: #3a3a4e;
  border-color: #666;
  transform: translateX(4px);
}

.spell-item .spell-emoji {
  font-size: 32px;
  flex-shrink: 0;
  width: 40px;
  text-align: center;
}

.spell-item .spell-info {
  flex: 1;
}

.spell-item .spell-info strong {
  display: block;
  color: #ffcc66;
  font-size: 16px;
  margin-bottom: 4px;
}

.spell-item .spell-info p {
  color: #aaa;
  font-size: 13px;
  margin: 0;
  line-height: 1.4;
}

@media (max-width: 768px) {
  .spell-item {
    padding: 8px;
    gap: 8px;
  }
  
  .spell-item .spell-emoji {
    font-size: 24px;
    width: 32px;
  }
  
  .spell-item .spell-info strong {
    font-size: 14px;
  }
  
  .spell-item .spell-info p {
    font-size: 11px;
  }
}
