: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,.5);
}

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

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1448 / 1240;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  touch-action: none;
}

/*
  CONTROLS ARE NORMAL DOCUMENT FLOW, BELOW THE CANVAS.
  No absolute/fixed positioning is used here, so the D-pad cannot cover gameplay.
*/
.control-deck {
  position: static !important;
  inset: auto !important;
  transform: none !important;
  width: 100%;
  min-height: 176px;
  background: #000;
  border-top: 4px solid #2d2d2d;
  display: grid;
  grid-template-columns: minmax(290px, .95fr) minmax(260px, 1.25fr) minmax(250px, .8fr);
  align-items: center;
  gap: clamp(18px, 2vw, 34px);
  padding: 18px clamp(18px, 2.2vw, 34px) 20px;
  line-height: 1.15;
  z-index: auto;
}

.dpad {
  position: static !important;
  width: min(100%, 330px);
  display: grid;
  grid-template-columns: repeat(3, 88px);
  grid-template-rows: repeat(2, 68px);
  justify-content: start;
  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,
.utility-controls button {
  border: 3px solid #fff;
  background: #101010;
  color: #fff;
  font-weight: 900;
  cursor: pointer;
  min-height: 54px;
  border-radius: 7px;
}
.dpad button { font-size: 30px; }
.dpad button:active,
.utility-controls button:active { background: #52ee49; color: #000; transform: translateY(1px); }

.control-copy {
  text-align: center;
  color: #cfcfcf;
  display: grid;
  gap: 9px;
  line-height: 1.35;
}
.control-copy strong { color: #f7ef42; font-size: clamp(19px, 1.7vw, 29px); }
.control-copy span { font-size: clamp(12px, 1.05vw, 17px); }

.utility-controls { display: grid; gap: 11px; }
.utility-controls button { padding: 0 16px; font-size: clamp(13px, 1.15vw, 18px); }

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

.panel {
  width: min(700px, 92%);
  max-height: 92%;
  overflow: auto;
  border: 5px solid #fff;
  background: #080808;
  padding: clamp(18px, 4vw, 38px);
  text-align: center;
  box-shadow: 10px 10px 0 #000;
}
.panel h2 {
  margin: 0 0 14px;
  color: #fff;
  letter-spacing: .04em;
  font-size: clamp(28px, 5vw, 54px);
  line-height: 1.05;
}
.button-stack { display: grid; gap: 10px; }
.button-stack button {
  min-height: 56px;
  border: 3px solid #fff;
  background: #47e83f;
  color: #000;
  font-weight: 900;
  cursor: pointer;
}
.button-stack button.secondary { background: #161616; color: #fff; }
.save-status { color: #62f05a; min-height: 2.8em; line-height: 1.4; }
.pause-help { color: #ddd; border: 2px solid #333; padding: 12px; margin-top: 14px; line-height: 1.45; font-size: 13px; }
.pause-help strong { color: #f7ef42; }
.fine { color: #aaa; font-size: 12px; line-height: 1.4; margin: 14px 0 0; }

.toast {
  position: absolute;
  left: 50%;
  bottom: 4%;
  transform: translate(-50%, 20px);
  background: #000;
  border: 3px solid #62f05a;
  color: #fff;
  padding: 10px 16px;
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  z-index: 20;
  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; }

/* Fullscreen still reserves a real black control strip underneath the taller board. */
.game-shell:fullscreen {
  width: 100vw;
  min-height: 100vh;
  border: 0;
  overflow: auto;
  background: #000;
}
.game-shell:fullscreen .game-wrap {
  width: min(100vw, calc((100vh - 182px) * 1448 / 1240));
  margin: 0 auto;
}
.game-shell:fullscreen .control-deck {
  width: min(100vw, 1880px);
  margin: 0 auto;
}

@media (max-width: 900px) {
  .control-deck {
    grid-template-columns: 1fr auto;
    grid-template-areas: "dpad util" "copy copy";
    min-height: 205px;
  }
  .dpad { grid-area: dpad; }
  .utility-controls { grid-area: util; min-width: 165px; }
  .control-copy { grid-area: copy; }
}

@media (max-width: 600px) {
  .game-shell { width: 100%; border-left: 0; border-right: 0; }
  .control-deck {
    grid-template-columns: 1fr;
    grid-template-areas: "dpad" "util" "copy";
    justify-items: center;
    min-height: 310px;
    gap: 14px;
    padding: 14px 10px 18px;
  }
  .dpad {
    grid-area: dpad;
    width: auto;
    grid-template-columns: repeat(3, 68px);
    grid-template-rows: repeat(2, 58px);
    justify-content: center;
  }
  .dpad button { min-height: 58px; font-size: 25px; }
  .utility-controls { grid-area: util; width: min(92vw, 360px); grid-template-columns: 1fr 1fr; }
  .utility-controls button { min-height: 50px; padding: 0 8px; }
  .control-copy { grid-area: copy; }
  .panel { border-width: 3px; }
}
