:root { --pad-height: 150px; }
* { box-sizing: border-box; }
body {
  margin: 0; overflow: hidden; background: #1a1a2e;
  touch-action: none;
  font-family: 'Comic Sans MS', 'Hiragino Maru Gothic ProN', 'Arial Rounded MT Bold', sans-serif;
  width: 100vw; height: 100dvh;
}

/* ====== スクリーン管理 ====== */
.screen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; }
.screen.active { display: flex; flex-direction: column; align-items: center; justify-content: center; }

/* ====== タイトル ====== */
#title-screen {
  background: radial-gradient(circle at 50% 40%, #2d1b69 0%, #1a1a2e 70%);
  color: #fff; text-align: center;
}
.title-logo {
  font-size: 30px; font-weight: bold;
  text-shadow: 0 0 10px #00e5ff, 0 0 25px #00e5ff, 0 0 50px #0077ff;
  margin-bottom: 8px; line-height: 1.2;
  white-space: nowrap;
}
.title-sub { font-size: 15px; color: #aee; margin-bottom: 24px; }
.title-robot-preview {
  margin: 15px 0;
}
#title-canvas { border-radius: 20px; }

.big-btn {
  padding: 18px 60px; font-size: 28px; font-weight: bold; color: #fff;
  background: linear-gradient(to bottom, #ff6b35, #e63000);
  border: 3px solid #fff; border-radius: 50px; cursor: pointer;
  box-shadow: 0 8px 0 rgba(0,0,0,0.4), inset 0 2px 5px rgba(255,255,255,0.4);
  touch-action: manipulation; transition: transform 0.08s, box-shadow 0.08s;
  margin: 10px;
}
.big-btn:active { transform: translateY(8px); box-shadow: 0 0px 0 rgba(0,0,0,0.4); }
.big-btn.green {
  background: linear-gradient(to bottom, #4caf50, #2e7d32);
}
.big-btn.blue {
  background: linear-gradient(to bottom, #2196f3, #1565c0);
}

#mute-btn {
  margin-top: 14px; padding: 8px 24px; font-size: 16px; font-weight: bold;
  color: #fff; background: rgba(255,255,255,0.12); border: 2px solid rgba(255,255,255,0.5);
  border-radius: 30px; cursor: pointer; touch-action: manipulation;
  min-height: 48px; display: inline-flex; align-items: center; justify-content: center; box-sizing: border-box;
}

/* ====== くみたて画面 ====== */
#build-screen {
  background: linear-gradient(135deg, #0f3460 0%, #16213e 100%);
  color: #fff; padding: 8px; overflow-y: auto;
  justify-content: flex-start; padding-top: 8px;
}
.build-title {
  font-size: 20px; font-weight: bold; color: #ffeb3b;
  text-shadow: 0 0 8px #ff9800; margin-bottom: 4px; text-align: center;
}
.build-layout {
  display: flex; width: 100%; max-width: 700px;
  gap: 8px; align-items: flex-start;
}
.robot-display {
  flex: 0 0 120px; display: flex; flex-direction: column; align-items: center;
}
.robot-display canvas { border-radius: 12px; background: rgba(0,0,0,0.3); border: 2px solid rgba(0,229,255,0.3); }
.robot-name-display {
  margin-top: 4px; font-size: 12px; color: #aee; text-align: center; min-height: 16px;
}
.parts-panel {
  flex: 1; display: flex; flex-direction: column; gap: 4px;
}
.part-group { background: rgba(255,255,255,0.07); border-radius: 10px; padding: 4px 6px; }
.part-group-label {
  font-size: 11px; color: #80cbc4; font-weight: bold; margin-bottom: 3px;
}
.part-choices {
  display: flex; gap: 4px; flex-wrap: wrap;
}
.part-btn {
  padding: 5px 6px; border-radius: 8px; border: 2px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.08); color: #fff; font-size: 11px;
  cursor: pointer; touch-action: manipulation; text-align: center; min-width: 58px;
  min-height: 48px; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; box-sizing: border-box;
  transition: background 0.1s, border-color 0.1s;
  position: relative;
}
.part-btn.selected {
  background: rgba(0,229,255,0.25); border-color: #00e5ff;
  box-shadow: 0 0 6px rgba(0,229,255,0.5);
}
.part-btn.locked { opacity: 0.4; cursor: not-allowed; }
.part-btn .lock-icon { font-size: 9px; position: absolute; top: 1px; right: 2px; }
.part-btn .stat-line { font-size: 8px; color: #aee; margin-top: 1px; }

.build-actions { display: flex; gap: 8px; margin-top: 6px; justify-content: center; }
.build-actions .big-btn {
  padding: 8px 24px; font-size: 16px; margin: 4px; border-radius: 20px;
  box-shadow: 0 4px 0 rgba(0,0,0,0.4);
  min-height: 48px; display: inline-flex; align-items: center; justify-content: center; box-sizing: border-box;
}
.build-actions .big-btn:active { transform: translateY(4px); box-shadow: 0 0px 0 rgba(0,0,0,0.4); }

/* ====== コース選択 ====== */
.stage-select-panel {
  background: rgba(255,255,255,0.07); border-radius: 12px; padding: 4px 8px;
  width: 100%; max-width: 700px; margin-top: 4px; border: 2px dashed rgba(255,255,255,0.15);
}
.stage-choices {
  display: flex; gap: 4px; justify-content: center; flex-wrap: wrap; margin-top: 4px;
}
.stage-btn {
  padding: 4px 8px; border-radius: 10px; border: 2px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.08); color: #fff; font-size: 11px; font-weight: bold;
  cursor: pointer; touch-action: manipulation; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; box-sizing: border-box; gap: 1px;
  transition: all 0.1s; min-width: 80px; min-height: 48px;
}
.stage-btn.selected {
  background: rgba(255,235,59,0.25); border-color: #ffeb3b;
  box-shadow: 0 0 6px rgba(255,235,59,0.5); color: #ffeb3b;
}
.stage-btn .stage-desc { font-size: 8px; color: #aee; font-weight: normal; }

/* ====== ゲーム画面 ====== */
#game-screen { background: #000; }
#game-container {
  position: absolute; top: 0; left: 0; width: 100%; height: calc(100% - var(--pad-height));
  overflow: hidden;
}
#gameCanvas { display: block; width: 100%; height: 100%; }

/* ゲームUI */
#game-ui {
  position: absolute; top: 8px; left: 10px; right: 120px;
  display: flex; flex-wrap: wrap; gap: 4px; justify-content: space-between; align-items: flex-start;
  pointer-events: none; z-index: 50;
}
.hud-box {
  background: rgba(0,0,0,0.6); border: 1.5px solid rgba(0,229,255,0.5);
  border-radius: 10px; padding: 4px 6px; font-size: 11px; color: #fff; font-weight: bold;
  white-space: nowrap;
}

/* コントロールパネル */
#control-panel {
  position: absolute; bottom: 0; left: 0; width: 100%;
  height: var(--pad-height);
  background: linear-gradient(to top, rgba(0,0,0,0.95), rgba(10,20,40,0.8));
  border-top: 2px solid rgba(0,229,255,0.3);
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 20px; z-index: 100;
  user-select: none; -webkit-user-select: none;
}
.ctrl-btn {
  border-radius: 20px; border: 3px solid rgba(255,255,255,0.8);
  color: #fff; font-weight: bold; font-size: 20px;
  display: flex; justify-content: center; align-items: center;
  touch-action: none; cursor: pointer;
  box-shadow: 0 6px 0 rgba(0,0,0,0.5);
  transition: transform 0.05s, box-shadow 0.05s;
  user-select: none; -webkit-user-select: none;
}
.ctrl-btn:active, .ctrl-btn.pressed {
  transform: translateY(6px); box-shadow: 0 0 0 rgba(0,0,0,0.5);
}
#btn-move {
  width: 110px; height: 110px; border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #42a5f5, #1565c0);
  font-size: 28px;
}
#btn-jump {
  width: 110px; height: 110px; border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #66bb6a, #2e7d32);
  font-size: 24px;
}
.ctrl-center { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.ctrl-center-label { font-size: 12px; color: rgba(255,255,255,0.5); }

/* ====== おやこ協力モード用のレイアウト切り替え ====== */
#game-screen.coop-active {
  display: flex !important;
  flex-direction: column !important;
  height: 100dvh !important;
  justify-content: space-between !important;
}
#game-screen.coop-active #coop-upper-panel {
  display: flex !important;
}
#game-screen.coop-active #game-container {
  position: relative !important;
  flex: 1 !important;
  height: auto !important;
  width: 100% !important;
  top: auto !important;
  left: auto !important;
}
#game-screen.coop-active #control-panel {
  position: relative !important;
  height: 160px !important;
  bottom: auto !important;
  left: auto !important;
}
#game-screen.coop-active #control-panel-single {
  display: none !important;
}

/* ====== ごほうび画面 ====== */
#reward-screen {
  background: radial-gradient(circle at 50% 50%, #1b5e20 0%, #1a1a2e 70%);
  color: #fff; text-align: center; padding: 20px;
}
.reward-title {
  font-size: 38px; font-weight: bold;
  text-shadow: 0 0 12px #ffeb3b, 0 0 30px #ff9800;
  margin-bottom: 12px;
}
.reward-sub { font-size: 18px; color: #c8e6c9; margin-bottom: 16px; }
#reward-canvas { border-radius: 20px; margin: 10px auto; display: block; }
.new-part-label {
  font-size: 22px; color: #ffeb3b; font-weight: bold; margin-top: 10px;
}
#confetti-canvas {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 999;
}

/* ====== 小さい画面向けの圧縮レイアウト ====== */
@media (max-height: 750px), (max-width: 380px) {
  #build-screen { padding: 4px; padding-top: 4px; }
  .build-title { font-size: 16px; margin-bottom: 2px; }
  #build-canvas { height: 110px !important; width: 94px !important; }
  .part-btn { min-height: 40px; min-width: 50px; padding: 3px 4px; }
  .stage-btn { min-height: 40px; min-width: 68px; padding: 2px 4px; }
  .stage-select-panel { padding: 2px 4px; margin-top: 2px; }
  #stage-minimap-canvas { height: 30px !important; }
  .build-actions { margin-top: 2px; }
  .build-actions .big-btn { min-height: 40px; padding: 4px 20px; font-size: 15px; margin: 2px; }
  
  .title-logo { font-size: 24px; margin-bottom: 4px; }
  .title-sub { margin-bottom: 12px; }
  #title-logo-img { width: 100px !important; height: 100px !important; }
  .big-btn { padding: 12px 40px; font-size: 24px; margin: 6px; }
  
  .reward-title { font-size: 32px; margin-bottom: 8px; }
  #reward-canvas { height: 120px !important; width: 120px !important; margin: 4px auto; }
  #reward-score-details { padding: 6px 12px; font-size: 12px; margin-bottom: 6px; }
}
#audio-control-hud {
  position: absolute; top: 12px; right: 12px; z-index: 9999;
  pointer-events: auto; background: rgba(0,0,0,0.65); border: 2px solid rgba(255,255,255,0.4); border-radius: 24px; padding: 0 4px; display: flex; align-items: center; gap: 4px; box-shadow: 0 4px 10px rgba(0,0,0,0.3); height: 48px; box-sizing: border-box;
  transition: top 0.3s;
}
body:has(#game-screen.active.coop-active) #audio-control-hud {
  top: 170px;
}