html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  background: #0a0d1c;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#wrap {
  box-sizing: border-box;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 横長の画面いっぱいに広げる(縦横比 4:3 〜 2.4:1 の範囲) */
#stage {
  /* iPhone のノッチ(Dynamic Island)とホームバーを避ける。ふつうの端末では 0 */
  --sw: calc(100vw - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
  --sh: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  position: relative;
  width: min(100vw, 240vh);
  height: min(100vh, 75vw);
  width: min(var(--sw), var(--sh) * 2.4);
  height: min(var(--sh), var(--sw) * 0.75);
}

/* スマホを縦に持っているときの案内 */
#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 10;
  background: #0a0d1c;
  color: #fff;
  font-family: sans-serif;
  text-align: center;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 0 16px;
}

#rotate .phone {
  width: 54px;
  height: 92px;
  border: 4px solid #ffe14d;
  border-radius: 10px;
  animation: turn 2s ease-in-out infinite;
}

#rotate p {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
}

#rotate small {
  color: #b0bec5;
  font-size: 14px;
}

@keyframes turn {
  0%, 20% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}

@media (orientation: portrait) and (pointer: coarse) {
  #rotate { display: flex; }
}

#stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#view {
  background: #0a0d1c;
}

/* 結果画面のシェアボタン(キャンバスの上に重ねる) */
#share {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translateX(-50%);
  width: min(480px, 64%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-family: sans-serif;
  z-index: 2;
}

#share[hidden] {
  display: none;
}

#share .row {
  display: flex;
  gap: 8px;
}

#share .sbtn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  border: 0;
  border-radius: 10px;
  padding: 7px 10px;
  font: inherit;
  font-weight: 800;
  font-size: clamp(15px, 3.4vh, 18px);
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  background: #dfe7f7;
  color: #1b2440;
}

#share .sbtn.main {
  padding: 9px 12px;
  font-size: clamp(16px, 4vh, 21px);
  font-weight: 900;
  background: linear-gradient(#7fe3ff, #2f9bff);
  color: #031a33;
  box-shadow: 0 4px 0 #11589b, 0 8px 18px rgba(0, 0, 0, 0.4);
}

#share .sbtn.main small {
  display: block;
  font-size: 0.65em;
  font-weight: 700;
  opacity: 0.75;
}

#share .sbtn:active {
  transform: translateY(2px);
}

/* タイトル画面の「プレイボール」「遊び方」、遊び方画面の「プレイボール」「もどる」 */
#titleBtns,
#helpBtns {
  position: absolute;
  left: 50%;
  top: 56%;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  gap: 14px;
  font-family: sans-serif;
}

#helpBtns {
  top: auto;
  bottom: 22%;
}

#titleBtns[hidden],
#helpBtns[hidden] {
  display: none;
}

.tbtn {
  font: inherit;
  font-weight: 900;
  font-size: clamp(17px, 4.6vh, 26px);
  padding: 0.45em 1.4em;
  border: 2px solid #ffe14d;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: #ffe14d;
  cursor: pointer;
  white-space: nowrap;
}

.tbtn.play {
  background: linear-gradient(#ffe36b, #ffb300);
  color: #1b1200;
  box-shadow: 0 4px 0 #a86a00, 0 8px 18px rgba(0, 0, 0, 0.4);
}

.tbtn:active {
  transform: translateY(2px);
}

/* Web 版のトップ・遊び方の画面: Google Play・App Store のアプリへのテキストリンク(横に並べる) */
#storeLink {
  position: absolute;
  left: 50%;
  top: 72%;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  gap: 10px;
}

#storeLink a {
  padding: 6px 14px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.6);
  color: #cfe8ff;
  font-family: sans-serif;
  font-weight: bold;
  font-size: clamp(13px, 3.2vh, 18px);
  white-space: nowrap;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* 遊び方の画面では「プレイボール」「もどる」の下 */
#storeLink.onHelp {
  top: auto;
  bottom: 9%;
}

#storeLink[hidden] {
  display: none;
}

/* タイトル画面の下: バージョン・プライバシーポリシー・広告の設定 */
#legal {
  position: absolute;
  right: 12px;
  bottom: 8px;
  z-index: 2;
  font-family: sans-serif;
  font-size: 12px;
  color: #b0bec5;
}

#legal[hidden],
#legal [hidden] {
  display: none;
}

#legal a {
  color: #cfe8ff;
}

#buildInfo {
  margin-right: 10px;
  opacity: 0.8;
}

/* 盗塁に失敗したときの「広告を見てやり直す」(アプリのみ) */
#redo {
  position: absolute;
  left: 50%;
  top: 58%;
  transform: translateX(-50%);
  width: min(560px, 76%);
  z-index: 3;
  padding: 10px 12px 12px;
  border-radius: 14px;
  background: rgba(8, 14, 28, 0.85);
  font-family: sans-serif;
  color: #e6eef8;
  text-align: center;
}

#redo[hidden] {
  display: none;
}

#redo p {
  margin: 0 0 8px;
  font-size: clamp(13px, 3vh, 16px);
}

#redo .row {
  display: flex;
  gap: 8px;
}

#redo .sbtn {
  flex: 1;
  border: 0;
  border-radius: 10px;
  padding: 10px 10px;
  font: inherit;
  font-weight: 800;
  font-size: clamp(14px, 3.4vh, 18px);
  cursor: pointer;
  background: #dfe7f7;
  color: #1b2440;
}

#redo .sbtn.main {
  flex: 2;
  background: linear-gradient(#7dffb0, #1fbf63);
  color: #04210f;
}

#redo .sbtn:disabled {
  opacity: 0.6;
}

/* タイトル画面の言語切り替え */
#langSel {
  position: absolute;
  left: 12px;
  bottom: 8px;
  z-index: 2;
  display: flex;
  gap: 6px;
  font-family: sans-serif;
}

#langSel[hidden] {
  display: none;
}

#langSel .langBtn {
  font: inherit;
  font-size: 15px;
  padding: 7px 16px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  cursor: pointer;
}

#langSel .langBtn.on {
  background: rgba(255, 225, 77, 0.9);
  border-color: #ffe14d;
  color: #222;
  font-weight: bold;
}

#share .sbtn.retry,
#pauseMenu .sbtn.retry {
  background: linear-gradient(#ffe36b, #ffb300);
  color: #1b1200;
  font-weight: 900;
  box-shadow: 0 3px 0 #a86a00;
}

/* ポーズ画面のボタン */
#pauseMenu {
  position: absolute;
  left: 50%;
  top: 44%;
  transform: translateX(-50%);
  width: min(340px, 50%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 3;
  font-family: sans-serif;
}

#pauseMenu[hidden] {
  display: none;
}

#pauseMenu .sbtn {
  border: 0;
  border-radius: 10px;
  padding: 11px 12px;
  font: inherit;
  font-weight: 900;
  font-size: clamp(17px, 4.2vh, 21px);
  cursor: pointer;
  background: #dfe7f7;
  color: #1b2440;
}

/* 横向きのスマホ(高さが低い)では、結果画面のボタンを詰めて下のバナー広告と重ならないようにする */
@media (max-height: 500px) {
  #share {
    top: 40%;
    gap: 5px;
  }

  #share .sbtn {
    padding: 5px 10px;
  }

  #share .sbtn.main {
    padding: 6px 12px;
  }

  #helpBtns {
    bottom: 17%;
  }

  #storeLink.onHelp {
    bottom: 3%;
  }
}
