* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: #000;
  color: #fff;
  font-family: "MS Gothic", "Yu Gothic", "Hiragino Kaku Gothic Pro", monospace, sans-serif;
  -webkit-font-smoothing: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  display: flex;
  justify-content: center;
  min-height: 100dvh;
  padding: 6px;
}

#game {
  width: 100%;
  /* 画面(canvas)+操作パッドが縦に収まるように制限 (480/416 ≒ 1.153) */
  max-width: min(500px, calc((100dvh - 185px) * 1.153));
  display: flex;
  flex-direction: column;
  align-items: center;
}

#screen-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 480 / 416;
  background: #000;
  border: 2px solid #333;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* --- ウィンドウ（ドラクエ風の黒地に白枠） --- */
.window {
  position: absolute;
  background: #000;
  border: 3px solid #fff;
  border-radius: 6px;
  color: #fff;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.7;
  box-shadow: 0 0 0 2px #000, 0 0 12px rgba(0,0,0,.8);
  z-index: 10;
}

#message-box {
  left: 4%;
  right: 4%;
  bottom: 4%;
  min-height: 70px;
  white-space: pre-wrap;
}

#message-box .cursor {
  animation: blink 0.8s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

#menu-box {
  top: 4%;
  left: 4%;
  min-width: 120px;
}
#menu-box.right { left: auto; right: 4%; }
#menu-box.bottom { top: auto; bottom: 26%; }

#menu-box .title {
  border-bottom: 1px solid #666;
  margin-bottom: 4px;
  padding-bottom: 2px;
  font-size: 13px;
  color: #ffd;
}

.menu-item {
  padding: 1px 6px 1px 20px;
  position: relative;
  white-space: nowrap;
}
.menu-item.sel::before {
  content: "▶";
  position: absolute;
  left: 2px;
}
.menu-item.disabled { color: #888; }

.row-between { display: flex; justify-content: space-between; gap: 18px; }

/* --- タイトル --- */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 30%, #10233f 0%, #000 80%);
  z-index: 20;
}
.overlay h1 {
  font-size: 40px;
  margin: 0;
  letter-spacing: 4px;
  color: #ffcf3f;
  text-shadow: 3px 3px 0 #7a4a00;
}
.overlay h1 .ii { color: #fff; }
.subtitle {
  color: #9fd3ff;
  letter-spacing: 6px;
  margin: 6px 0 26px;
}
#title-menu .menu-item {
  font-size: 18px;
  padding: 4px 6px 4px 24px;
  cursor: pointer;
}
.overlay .hint {
  position: absolute;
  bottom: 12px;
  font-size: 11px;
  color: #789;
  text-align: center;
  padding: 0 10px;
}

.hidden { display: none !important; }

/* --- 操作パッド --- */
#controls {
  width: 100%;
  max-width: 500px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  padding: 0 6px;
}
#dpad {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 4px;
}
#dpad .pad:nth-child(1) { grid-column: 2; grid-row: 1; }
#dpad .pad:nth-child(2) { grid-column: 1; grid-row: 2; }
#dpad .pad:nth-child(3) { grid-column: 2; grid-row: 2; }
#dpad .pad:nth-child(4) { grid-column: 3; grid-row: 2; }
#dpad .pad:nth-child(5) { grid-column: 2; grid-row: 3; }

.pad {
  background: #2a2a35;
  color: #fff;
  border: 2px solid #555;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.pad:active { background: #4a4a60; }
.pad.center { background: #1a1a22; }
#action { display: flex; flex-direction: column; gap: 8px; }
.pad.wide { width: 110px; height: 40px; font-size: 14px; }

.pad { touch-action: none; }   /* 押しっぱなし中のスクロールを防ぐ */

@media (max-width: 420px) {
  .overlay h1 { font-size: 30px; }
  .window { font-size: 13px; }
}

/* タッチ端末: パッドを大きく */
@media (pointer: coarse) {
  #game { max-width: min(500px, calc((100dvh - 235px) * 1.153)); }
  #dpad {
    grid-template-columns: repeat(3, 56px);
    grid-template-rows: repeat(3, 56px);
    gap: 6px;
  }
  .pad { font-size: 22px; }
  .pad.wide { width: 124px; height: 50px; font-size: 15px; }
}
