* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0b0b10;
  --panel: #11141c;
  --line: #2a3142;
  --text: #e8ecf4;
  --muted: #8b93a7;
  --accent: #e52521;
  --sky: #5c94fc;
}

html, body {
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", "Cascadia Code", "Courier New", monospace;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Boot ---- */
#boot {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    linear-gradient(180deg, #4a7fd4 0 58%, #3d9a3d 58% 100%);
}

.panel {
  width: min(360px, 100%);
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 28px 24px 22px;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0,0,0,.35);
}

.panel.compact { padding: 28px 36px; width: auto; }

.logo {
  font-size: clamp(1.8rem, 6vw, 2.2rem);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: 0.08em;
  color: #fff;
  text-shadow:
    3px 3px 0 #000,
    -1px 0 0 var(--accent),
    0 -1px 0 #f5c518;
  margin-bottom: 18px;
}

.hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
  margin-bottom: 20px;
}

.fine {
  margin-top: 14px;
  font-size: 10px;
  color: var(--muted);
  opacity: .85;
}

#btn-start,
#btn-continue {
  appearance: none;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 700;
  letter-spacing: 0.12em;
  padding: 12px 36px;
  cursor: pointer;
  box-shadow: 0 3px 0 #8b1210;
}
#btn-start:active,
#btn-continue:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #8b1210;
}

#portrait-gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  display: grid;
  place-items: center;
}
#portrait-gate[hidden] { display: none !important; }

/* ---- Game shell ---- */
#game-wrap {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#game-wrap[hidden] { display: none !important; }

#stage {
  position: relative;
  width: min(100vw - 16px, calc((100vh - 16px) * 512 / 448));
  max-height: min(100vh - 16px, calc((100vw - 16px) * 448 / 512));
  aspect-ratio: 512 / 448;
  border: 2px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #000;
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: var(--sky);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(8, 10, 16, .78);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  text-align: center;
}
.overlay[hidden] { display: none !important; }
#overlay-msg {
  font-size: clamp(1rem, 3.5vw, 1.35rem);
  line-height: 1.55;
  white-space: pre-line;
  letter-spacing: 0.04em;
  text-shadow: 2px 2px 0 #000;
}

/* ---- Touch: NES-style, only coarse pointers ---- */
.touch {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(10px, env(safe-area-inset-bottom));
  padding: 0 18px;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 30;
}

.touch .dpad,
.touch .actions {
  pointer-events: auto;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(2, 52px);
  gap: 6px;
}

.dpad button,
.actions button {
  width: 52px;
  height: 52px;
  border: 2px solid rgba(255,255,255,.22);
  border-radius: 10px;
  background: rgba(20, 24, 34, .55);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  backdrop-filter: blur(4px);
  touch-action: none;
}

.dpad button.ghost {
  visibility: hidden;
  pointer-events: none;
}

.dpad button:disabled {
  opacity: .25;
}

.actions {
  display: flex;
  gap: 14px;
  align-items: flex-end;
  padding-bottom: 4px;
}

.actions button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  line-height: 1;
}

.actions button span { font-size: 18px; }
.actions button small {
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: rgba(255,255,255,.7);
}

.actions .btn-a {
  width: 68px;
  height: 68px;
  background: rgba(229, 37, 33, .55);
  border-color: rgba(255,255,255,.35);
}

.actions .btn-b {
  background: rgba(60, 100, 220, .45);
}

.touch button:active,
.touch button.active {
  background: rgba(255,255,255,.28);
  transform: scale(0.96);
}
.actions .btn-a:active,
.actions .btn-a.active {
  background: rgba(229, 37, 33, .85);
}

@media (pointer: coarse), (hover: none) {
  .touch { display: flex; }
  #stage {
    /* leave room for pads */
    max-height: min(calc(100vh - 110px), calc((100vw - 16px) * 448 / 512));
    margin-bottom: 88px;
  }
}

@media (min-width: 900px) and (pointer: fine) {
  #stage {
    width: min(768px, 100vw - 24px);
  }
}
