:root {
  --cell: min(45px, calc((100vw - 36px) / 12));
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  background: #2a2a2a;
  color: #eee;
  font-family: Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
}

main {
  width: min(560px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.back {
  align-self: flex-start;
  margin-bottom: 12px;
  color: #ffe66d;
  font-weight: 700;
  text-decoration: none;
}

.back:hover {
  color: #fff;
}

h1 {
  color: #ff6b6b;
  margin: 0 0 10px;
  text-align: center;
}

#gameInfo {
  background: rgba(0, 0, 0, 0.4);
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
  text-align: center;
  width: 100%;
  font-size: clamp(13px, 3.6vw, 16px);
  line-height: 1.3;
}

#grid {
  display: inline-grid;
  grid-auto-rows: var(--cell);
  gap: 1px;
  background: #1a1a1a;
  padding: 2px;
  border-radius: 4px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  touch-action: manipulation;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  min-width: 0;
  min-height: 0;
  line-height: 1;
  background: #3a3a3a;
  border: 1px solid #2a2a2a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.62);
  transition: background 0.15s, transform 0.15s;
  cursor: pointer;
  position: relative;
  user-select: none;
  padding: 0;
}

@media (hover: hover) {
  .cell:hover:not(.wall) {
    background: #4a4a4a;
    transform: scale(1.05);
  }
}

.cell:focus-visible {
  outline: 2px solid #ffe66d;
  outline-offset: -2px;
  z-index: 1;
}

.wall {
  background: linear-gradient(135deg, #1a1a1a 0%, #0a0a0a 100%);
  border-color: #0a0a0a;
  cursor: default;
}

.floor {
  background: #4a4a4a;
}

.player {
  background: #2c5f8d;
  box-shadow: 0 0 15px rgba(44, 95, 141, 0.6);
  z-index: 1;
}

.player.spotted {
  box-shadow: 0 0 0 3px #ff6b6b, 0 0 15px rgba(255, 107, 107, 0.8);
}

.guard {
  background: #8d2c2c;
  box-shadow: 0 0 15px rgba(141, 44, 44, 0.6);
  z-index: 1;
}

.key {
  background: radial-gradient(circle, #ffd700 0%, #d4af37 100%);
  box-shadow: 0 0 40px rgba(255, 215, 0, 1), 0 0 60px rgba(255, 215, 0, 0.8);
  animation: keyPulse 1s infinite;
  border: 3px solid #ffea00;
  font-size: calc(var(--cell) * 0.7);
}

@keyframes keyPulse {
  0%, 100% {
    box-shadow: 0 0 40px rgba(255, 215, 0, 1), 0 0 60px rgba(255, 215, 0, 0.8);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 50px rgba(255, 215, 0, 1), 0 0 80px rgba(255, 215, 0, 1);
    transform: scale(1.08);
  }
}

.mine {
  background: #1a1a1a;
}

.mine::after,
.covered-mine::after {
  content: '💣';
  position: absolute;
  font-size: calc(var(--cell) * 0.45);
  opacity: 0.4;
}

.covered-mine::after {
  right: 0;
  bottom: 0;
  font-size: calc(var(--cell) * 0.34);
  opacity: 0.9;
}

@media (hover: hover) {
  .mine:hover::after {
    opacity: 0.8;
  }
}

.mine-exploded {
  background: #ff4444;
  animation: explosion 0.5s;
}

.mine-exploded::after {
  opacity: 1;
}

@keyframes explosion {
  0% { transform: scale(1); }
  50% { transform: scale(1.5); background: #ffaa00; }
  100% { transform: scale(1); background: #ff4444; }
}

.exit {
  background: #2c8d5f;
}

.guard-vision::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(141, 44, 44, 0.28);
  pointer-events: none;
}

#controls {
  margin-top: 20px;
  text-align: center;
  width: 100%;
}

button {
  background: #ff6b6b;
  color: white;
  border: none;
  padding: 12px 24px;
  margin: 5px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
}

button:hover {
  background: #ff5252;
}

#message {
  margin-top: 15px;
  font-size: 16px;
  min-height: 25px;
  color: #ffe66d;
  font-weight: bold;
}

.instructions {
  background: rgba(0, 0, 0, 0.3);
  padding: 15px;
  border-radius: 5px;
  margin-top: 15px;
  width: 100%;
  font-size: 14px;
  line-height: 1.6;
}
