:root {
  color-scheme: dark;
  font-family: "Courier New", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  background: #050505;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: #050505; }
body { overscroll-behavior: none; touch-action: manipulation; overflow-x: hidden; }
button { font: inherit; }

.app {
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: max(2px, env(safe-area-inset-top)) max(2px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(2px, env(safe-area-inset-left));
  background: #050505;
}

.game-shell {
  width: min(99.5vw, 1880px);
  background: #000;
  border: 2px solid #242424;
  box-shadow: 0 16px 50px rgba(0,0,0,.55);
}

.game-wrap {
  position: relative;
  width: 100%;
  background: #000;
  overflow: hidden;
  line-height: 0;
}

#phaser-game {
  width: 100%;
  aspect-ratio: 1448 / 1240;
  background: #000;
}

#phaser-game canvas {
  display: block;
  width: 100% !important;
  height: auto !important;
  aspect-ratio: 1448 / 1240;
  background: #000;
  touch-action: none;
}

/* Controls are normal document flow below the game. */
.control-deck {
  position: static !important;
  inset: auto !important;
  transform: none !important;
  width: 100%;
  min-height: 190px;
  background: #000;
  border-top: 4px solid #2d2d2d;
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(300px, .9fr) minmax(280px, .78fr);
  align-items: center;
  gap: clamp(18px, 2vw, 38px);
  padding: 18px clamp(18px, 2.2vw, 34px) 20px;
  line-height: 1.15;
}

.direction-zone {
  display: grid;
  justify-items: start;
  gap: 8px;
  color: #bfc4cc;
  font-size: 13px;
  font-weight: 900;
  letter-spacing: .06em;
}

.dpad {
  width: 314px;
  display: grid;
  grid-template-columns: repeat(3, 96px);
  grid-template-rows: repeat(2, 68px);
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }

.dpad button,
.action-zone button,
.utility-controls button {
  border: 3px solid #fff;
  background: linear-gradient(#1a1d21, #090a0c);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
  border-radius: 9px;
  min-height: 56px;
  box-shadow: inset 0 2px 0 rgba(255,255,255,.15), 0 5px 0 #000;
}
.dpad button { font-size: 30px; }
.dpad button.active,
.dpad button:active,
.action-zone button:active,
.utility-controls button:active {
  background: linear-gradient(#5ef566, #1fb92b);
  color: #000;
  transform: translateY(2px);
  box-shadow: inset 0 2px 0 rgba(255,255,255,.45), 0 2px 0 #000;
}

.action-zone {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) minmax(120px, .7fr);
  gap: 16px;
}
.action-zone .attack-btn {
  min-height: 104px;
  font-size: clamp(22px, 2vw, 34px);
  border-radius: 52px;
  background: radial-gradient(circle at 40% 30%, #ff6d5e, #da1515 55%, #750000 100%);
  border-color: #ffd7d1;
}
.action-zone .magic-btn {
  min-height: 82px;
  align-self: center;
  font-size: clamp(17px, 1.4vw, 24px);
  color: #fff7a5;
  border-color: #ffcf40;
  background: radial-gradient(circle at 40% 30%, #ffb43c, #9d3100 58%, #381100 100%);
}

.utility-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.utility-controls button { padding: 0 12px; font-size: clamp(12px, 1vw, 17px); }

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0,0,0,.80);
  z-index: 20;
  line-height: 1.2;
}
.overlay.show { display: flex; }

.panel {
  width: min(760px, 92%);
  max-height: 92%;
  overflow: auto;
  border: 5px solid #fff;
  background: linear-gradient(180deg, #15171c, #050506);
  padding: clamp(18px, 4vw, 40px);
  text-align: center;
  box-shadow: 10px 10px 0 #000, inset 0 0 40px rgba(255,255,255,.04);
}
.panel h1,
.panel h2 {
  margin: 0 0 14px;
  color: #fff;
  letter-spacing: .05em;
  font-size: clamp(32px, 5vw, 60px);
  line-height: 1.05;
}
.panel h1 { color: #ffd44c; text-shadow: 0 4px 0 #7d3600; }
.menu-copy { color: #eee; line-height: 1.5; }
.button-stack { display: grid; gap: 10px; }
.button-stack button {
  min-height: 58px;
  border: 3px solid #fff;
  background: linear-gradient(#55f05c, #1cab25);
  color: #000;
  font-weight: 900;
  cursor: pointer;
}
.button-stack button.secondary { background: linear-gradient(#25282e, #101114); color: #fff; }
.save-status { color: #62f05a; min-height: 2.8em; line-height: 1.4; }
.pause-help, .fine { color: #bbb; line-height: 1.45; font-size: 13px; }
.pause-help { border: 2px solid #333; padding: 12px; margin-top: 14px; }
.pause-help strong { color: #f7ef42; }

.toast {
  position: absolute;
  left: 50%;
  bottom: 4%;
  transform: translate(-50%, 20px);
  background: #000;
  border: 3px solid #62f05a;
  color: #fff;
  padding: 10px 18px;
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  z-index: 30;
  transition: opacity .15s ease, transform .15s ease;
  text-align: center;
  line-height: 1.2;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
button:focus-visible { outline: 4px solid #f7ef42; outline-offset: 2px; }

.game-shell:fullscreen {
  width: 100vw;
  min-height: 100vh;
  border: 0;
  overflow: auto;
  background: #000;
}
.game-shell:fullscreen .game-wrap {
  width: min(100vw, calc((100vh - 196px) * 1448 / 1240));
  margin: 0 auto;
}
.game-shell:fullscreen .control-deck {
  width: min(100vw, 1880px);
  margin: 0 auto;
}

@media (max-width: 950px) {
  .control-deck {
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "dir action" "util util";
    min-height: 250px;
  }
  .direction-zone { grid-area: dir; }
  .action-zone { grid-area: action; }
  .utility-controls { grid-area: util; grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 640px) {
  .game-shell { width: 100%; border-left: 0; border-right: 0; }
  .control-deck {
    grid-template-columns: 1fr;
    grid-template-areas: "dir" "action" "util";
    justify-items: center;
    min-height: 420px;
    gap: 14px;
    padding: 14px 10px 18px;
  }
  .direction-zone { justify-items: center; text-align: center; }
  .dpad { width: auto; grid-template-columns: repeat(3, 78px); grid-template-rows: repeat(2, 58px); }
  .dpad button { min-height: 58px; font-size: 24px; }
  .action-zone { width: min(94vw, 430px); grid-template-columns: 1.25fr .75fr; }
  .action-zone .attack-btn { min-height: 82px; }
  .utility-controls { width: min(94vw, 430px); grid-template-columns: 1fr 1fr; }
  .panel { border-width: 3px; }
}
