/* 塊魂リスペクトの陽気ポップUI */
* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  /* 100% はブラウザUI(URLバー・タブバー)を含んだ高さを指すため、
     スマホでは実際に見える範囲とズレる。dvh は表示中のUIを除いた高さに追従する。 */
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: "Hiragino Maru Gothic ProN", "Rounded Mplus 1c", system-ui, sans-serif;
  background: #a0e7ff;
  user-select: none;
  /* スマホ：スワイプでページが動いたり、長押しでメニューが出るのを防ぐ */
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#game {
  position: fixed;
  inset: 0;
  height: 100dvh;
}
#game canvas { display: block; }

/* HUD 共通 */
#hud, #controls {
  position: fixed;
  z-index: 10;
  pointer-events: none;
}

/* プレイ中以外はゲーム用HUDを隠す（ホーム／結果画面ではじゃま） */
body:not(.playing) #hud,
body:not(.playing) #controls,
body:not(.playing) #gather-btn,
body:not(.playing) #stick-zone,
body:not(.playing) #stick-base,
body:not(.playing) #stick-knob { display: none !important; }

/* タイトル＆カウンター（左上） */
#hud {
  top: max(16px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
}
#hud h1 {
  font-size: 20px;
  color: #fff;
  text-shadow: 2px 2px 0 #ff6b6b, 4px 4px 0 rgba(0, 0, 0, 0.15);
  letter-spacing: 0.02em;
}
#hud .counter {
  margin-top: 8px;
  display: inline-block;
  padding: 6px 14px;
  font-size: 22px;
  font-weight: 700;
  color: #5b3b1a;
  background: #ffe9a8;
  border: 3px solid #fff;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
}

/* 増殖ゲージ */
.gauge {
  margin-top: 8px;
  width: 210px;
}
.gauge-label {
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.25);
  margin-bottom: 3px;
}
.gauge-bar {
  height: 16px;
  padding: 3px;
  background: rgba(255, 255, 255, 0.75);
  border: 3px solid #fff;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
}
#gauge-fill {
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd93d, #ff6b6b);
  transition: width 0.12s linear;
}
/* 満タン直前に脈打たせて「もうすぐ増える」を伝える */
#gauge-fill.hot {
  animation: gauge-pulse 0.5s ease-in-out infinite;
}
@keyframes gauge-pulse {
  0%, 100% { filter: brightness(1); }
  50%      { filter: brightness(1.5); }
}

/* 操作ヒント（下中央） */
#controls {
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 999px;
  font-size: 14px;
  color: #444;
}
/* 汚れ度メーター（餌の量に直結する） */
.gauge.dirt { margin-top: 6px; }
#dirt-fill {
  width: 0%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #b7d67a, #7a5230);
  transition: width 0.3s ease;
}

/* 集合ボタン（押している間だけ仲間が付いてくる） */
#gather-btn {
  display: none;
  position: fixed;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 22;
  width: 92px;
  height: 92px;
  align-items: center;
  justify-content: center;
  text-align: center;
  white-space: pre-line;
  line-height: 1.15;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  color: #5b3b1a;
  background: rgba(255, 217, 61, 0.92);
  border: 4px solid #fff;
  border-radius: 50%;
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.2);
  touch-action: none;
  cursor: pointer;
}
#gather-btn.on {
  background: #2ec4b6;
  color: #fff;
  transform: translateY(4px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}

/* 今挑戦中のミッション */
#mission {
  margin-top: 8px;
  display: inline-block;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  background: rgba(108, 92, 231, 0.85);
  border: 2px solid #fff;
  border-radius: 999px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15);
}
#mission.done { background: rgba(46, 196, 182, 0.9); }

/* サウンド切替ボタン（右上） */
#mute-btn {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 22;
  width: 48px;
  height: 48px;
  font-size: 22px;
  line-height: 1;
  background: rgba(255, 255, 255, 0.8);
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
#mute-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.15); }

/* 危険の出現・ミッション達成の通知 */
#toast {
  position: fixed;
  top: 22%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  padding: 12px 26px;
  font-size: 19px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.72);
  border: 3px solid #ffd93d;
  border-radius: 16px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
  text-align: center;
}
#toast.show { opacity: 1; }
#toast.danger { border-color: #ff3b3b; }

/* ===== ホーム画面 ===== */
#home {
  position: fixed;
  inset: 0;
  z-index: 40;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  background: linear-gradient(160deg, rgba(255,179,198,0.72), rgba(160,231,255,0.72));
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
#home[hidden] { display: none; }
.home-inner {
  width: min(560px, 92vw);
  padding: 28px 20px 40px;
  text-align: center;
}
.home-title {
  font-size: clamp(26px, 7vw, 40px);
  line-height: 1.15;
  color: #fff;
  text-shadow: 3px 3px 0 #ff6b6b, 6px 6px 0 rgba(0, 0, 0, 0.15);
}
.home-sub {
  margin: 12px 0 20px;
  font-size: 13px;
  font-weight: 700;
  color: #5b3b1a;
}
.stage-select {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-bottom: 18px;
}
.stage-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 150px;
  padding: 14px 10px;
  font: inherit;
  background: rgba(255, 255, 255, 0.9);
  border: 4px solid #fff;
  border-radius: 18px;
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
.stage-card.selected { border-color: #ffd93d; background: #fffdf3; }
.stage-card.locked { opacity: 0.55; cursor: default; }
.stage-name { font-size: 17px; font-weight: 700; color: #5b3b1a; }
.stage-goal { font-size: 12px; color: #7a6350; }
.stage-best { font-size: 11px; color: #b0895a; }
#start-btn {
  padding: 16px 48px;
  font: inherit;
  font-size: 22px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, #ff8fa3, #ff6b6b);
  border: 4px solid #fff;
  border-radius: 999px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.2);
  cursor: pointer;
  animation: start-pulse 1.4s ease-in-out infinite;
}
#start-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2); }
@keyframes start-pulse { 0%,100%{transform:scale(1);} 50%{transform:scale(1.05);} }
.home-panels {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 24px;
}
.home-panel {
  flex: 1 1 220px;
  padding: 14px 16px;
  text-align: left;
  background: rgba(255, 255, 255, 0.9);
  border: 3px solid #fff;
  border-radius: 16px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.12);
}
.home-panel h3 { font-size: 15px; color: #5b3b1a; margin-bottom: 8px; }
.home-panel ul { list-style: none; }
.home-panel li { font-size: 12px; color: #6a5540; padding: 3px 0 3px 16px; position: relative; }
.home-panel li::before { content: '🪳'; position: absolute; left: 0; font-size: 10px; }
.home-panel li b { color: #d9534f; }
#home-stats { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; }
#home-stats dt { font-size: 12px; color: #7a6350; }
#home-stats dd { font-size: 15px; font-weight: 700; color: #5b3b1a; text-align: right; }

/* ===== 結果発表画面 ===== */
#result {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  animation: result-in 0.4s ease-out;
}
#result[hidden] { display: none; }
@keyframes result-in { from { opacity: 0; } to { opacity: 1; } }
.result-card {
  padding: 28px 34px;
  text-align: center;
  background: #fff;
  border: 5px solid #ffd93d;
  border-radius: 24px;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.2);
  max-width: 90vw;
  animation: result-pop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes result-pop { from { transform: scale(0.7); } to { transform: scale(1); } }
#result-icon { font-size: 50px; line-height: 1; }
.result-card h2 { margin: 6px 0 2px; font-size: 28px; color: #5b3b1a; }
#result-time { font-size: 16px; font-weight: 700; color: #7a6350; margin-bottom: 10px; }
#result-rank {
  margin: 8px auto 6px;
  padding: 12px;
  background: linear-gradient(180deg, #fff7d6, #ffe9a8);
  border-radius: 16px;
}
.rank-stars { font-size: 26px; color: #ffb000; letter-spacing: 2px; }
.rank-name { font-size: 22px; font-weight: 700; color: #5b3b1a; margin-top: 2px; }
#result-note { font-size: 13px; color: #7a6350; margin-top: 8px; min-height: 18px; }
.result-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.result-buttons button {
  padding: 12px 24px;
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  border: 3px solid #fff;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
  cursor: pointer;
}
#result-retry { color: #5b3b1a; background: #ffd93d; }
#result-home { color: #fff; background: #4ecdc4; }
.result-buttons button:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2); }

/* --- 仮想スティック（スマホのみ表示。main.js が display を切り替える） --- */
#stick-zone {
  display: none;
  position: fixed;
  left: 0;
  top: 0;
  width: 45%;
  height: 100%;
  z-index: 20;
  touch-action: none;
}
#stick-base, #stick-knob {
  position: fixed;
  z-index: 21;
  pointer-events: none;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.12s ease;
}
/* 台座はつまみより十分大きくすること。
   （つまみの可動域は input.js が「台座の半径 − つまみの半径」で自動計算する） */
#stick-base {
  width: 140px;
  height: 140px;
  background: rgba(255, 255, 255, 0.22);
  border: 3px solid rgba(255, 255, 255, 0.6);
}
#stick-knob {
  width: 56px;
  height: 56px;
  background: rgba(255, 217, 61, 0.9);
  border: 3px solid #fff;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
}

/* --- 画面が狭い端末向けの調整 --- */
@media (max-width: 640px) {
  #hud { top: 10px; left: 10px; }
  #hud h1 { font-size: 14px; }
  #hud .counter { margin-top: 5px; padding: 4px 10px; font-size: 17px; }
  .gauge { width: 150px; }
  /* 画面が狭いとヒントが横に長くなりHUDと衝突する。折り返して下部中央に収める */
  #controls {
    max-width: 92vw;
    flex-wrap: wrap;
    justify-content: center;
    font-size: 11px;
    padding: 5px 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
  }
}
/* 横持ちで画面が低いときはヒントを消して視界を確保 */
@media (max-height: 450px) {
  #controls { display: none; }
}

#controls kbd {
  display: inline-block;
  min-width: 22px;
  padding: 2px 6px;
  text-align: center;
  background: #fff;
  border: 2px solid #ccc;
  border-bottom-width: 3px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 700;
  color: #555;
}
