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

body {
  font-family: 'Arial', sans-serif;
  background: url('image/宇宙1.png') center center / cover no-repeat, 
              linear-gradient(135deg, #0B1426 0%, #1B2A5E 100%);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('image/宇宙1.png') center center / cover no-repeat;
  z-index: -2;
  animation: starTwinkle 20s infinite linear;
}

body::after {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse at center, transparent 0%, rgba(0,0,0,0.3) 100%);
  z-index: -1;
}

@keyframes starTwinkle {
  0% { opacity: 0.8; }
  50% { opacity: 1; }
  100% { opacity: 0.8; }
}

#gameContainer {
  position: relative; /* モバイルD-pad用の配置基準 */
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  max-width: 860px;
  width: 100%;
}

header {
  text-align: center;
  margin-bottom: 20px;
}

header h1 {
  font-size: 2.5rem;
  margin-bottom: 15px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

#gameInfo {
  display: flex;
  justify-content: space-around;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 15px;
  padding: 15px;
  margin-bottom: 20px;
}

#gameInfo div {
  font-size: 1.2rem;
  font-weight: bold;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

#gameArea {
  position: relative;
  display: flex;
  justify-content: center;
}

#gameCanvas {
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 15px;
  background: url('image/宇宙2.png') center center / cover no-repeat, 
              linear-gradient(180deg, #000428 0%, #004e92 100%);
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5), 
              0 0 30px rgba(135, 206, 250, 0.3);
  position: relative;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

#gameCanvas::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('image/宇宙2.png') center center / cover no-repeat;
  opacity: 0.7;
  pointer-events: none;
  border-radius: 12px;
}

#startScreen, #gameOverScreen, #rankingScreen {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 15px;
  z-index: 10;
}

#startContent, #gameOverContent, #rankingContent {
  text-align: center;
  background: rgba(255, 255, 255, 0.95);
  color: #333;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  max-width: 400px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

#startContent h2, #gameOverContent h2, #rankingContent h2 {
  font-size: 2rem;
  margin-bottom: 20px;
  color: #667eea;
}

#startContent p, #gameOverContent p {
  margin: 15px 0;
  font-size: 1.1rem;
  line-height: 1.5;
}

/* 名前入力セクション */
#nameInputSection {
  margin: 15px 0;
  padding: 15px;
  background: rgba(102, 126, 234, 0.1);
  border-radius: 15px;
  border: 2px solid rgba(102, 126, 234, 0.3);
}

#nameInputSection label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  font-size: 1rem;
  color: #667eea;
}

#playerName {
  width: 100%;
  padding: 10px;
  font-size: 1rem;
  border: 2px solid #667eea;
  border-radius: 10px;
  margin-bottom: 12px;
  background: rgba(255, 255, 255, 0.9);
  color: #333;
  text-align: center;
}

#playerName:focus {
  outline: none;
  border-color: #764ba2;
  box-shadow: 0 0 10px rgba(102, 126, 234, 0.3);
}

#playerName:disabled {
  background: rgba(200, 200, 200, 0.5);
  color: #666;
}

#saveScoreBtn {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 10px 20px;
  font-size: 0.95rem;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 10px;
  width: 100%;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
}

#saveScoreBtn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

#saveScoreBtn:disabled {
  background: linear-gradient(45deg, #999, #bbb);
  cursor: not-allowed;
  transform: none;
}

#saveStatus {
  margin-top: 10px;
  font-weight: bold;
  min-height: 20px;
}

/* ランキング画面のスタイル */

/* --- カラムヘッダー視認性向上 --- */
.ranking-header span {
  color: #ffffff !important; /* 明瞭な白色で統一 */
}
/* Rank 見出しのみやや小さく */
.ranking-header .rank {
  font-size: 0.8rem;
}

#rankingContent {
  max-width: 600px;
  width: 95%;
  padding: 30px 20px;
}

#rankingList {
  max-height: 400px;
  overflow-y: auto;
  margin: 20px 0;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 15px;
  padding: 15px;
}

.ranking-header {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  padding: 15px;
  border-radius: 10px;
  font-weight: bold;
  margin-bottom: 15px;
  display: grid;
  grid-template-columns: 50px 1fr 80px 50px 70px 50px 80px;
  gap: 10px;
  font-size: 0.9rem;
}

.ranking-item {
  display: grid;
  grid-template-columns: 50px 1fr 80px 50px 70px 50px 80px;
  gap: 10px;
  padding: 12px 15px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  align-items: center;
  transition: all 0.2s ease;
  font-size: 0.9rem;
}

.ranking-item:hover {
  background: rgba(255, 255, 255, 0.9);
  transform: scale(1.02);
}

.ranking-item.rank-1 {
  background: linear-gradient(45deg, rgba(255, 215, 0, 0.8), rgba(255, 235, 59, 0.8));
  color: #333;
  font-weight: bold;
}

.ranking-item.rank-2 {
  background: linear-gradient(45deg, rgba(192, 192, 192, 0.8), rgba(224, 224, 224, 0.8));
  color: #333;
  font-weight: bold;
}

.ranking-item.rank-3 {
  background: linear-gradient(45deg, rgba(205, 127, 50, 0.8), rgba(222, 184, 135, 0.8));
  color: #333;
  font-weight: bold;
}

.rank {
  font-weight: bold;
  text-align: center;
  font-size: 1.2rem;
}

.name {
  font-weight: bold;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  color: #5865f2;
}

.score {
  font-weight: bold;
  text-align: right;
  color: #667eea;
}

.coins {
  text-align: center;
  color: #f39c12;
  font-weight: bold;
}

.distance {
  text-align: right;
  color: #27ae60;
}

.plays {
  text-align: center;
  color: #8e44ad;
  font-weight: bold;
}

.date {
  text-align: center;
  font-size: 0.8rem;
  color: #666;
}

.loading, .error, .no-data {
  text-align: center;
  padding: 40px 20px;
  font-size: 1.2rem;
  color: #667eea;
}

.error {
  color: #e74c3c;
}

.no-data {
  color: #95a5a6;
}

/* ボタンの配置調整 */
#gameOverContent button {
  margin: 10px 5px;
  width: 45%;
  min-width: 150px;
}

#startContent button {
  margin: 10px 5px;
  width: 45%;
  min-width: 150px;
}

button {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 15px 30px;
  font-size: 1.1rem;
  border-radius: 25px;
  cursor: pointer;
  margin-top: 20px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  white-space: normal;
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

button:active {
  transform: translateY(0);
}

#controls {
  text-align: center;
  margin-top: 20px;
  font-size: 1.1rem;
  opacity: 0.8;
}

small {
  font-size: 0.75em;
  opacity: 0.8;
  display: block;
  margin-top: 5px;
  font-style: italic;
}

button small {
  margin-top: 3px;
  font-size: 0.6em;
  line-height: 1.0;
}

h2 small {
  font-size: 0.6em;
  margin-top: 8px;
  color: #667eea;
}

.hidden {
  display: none !important;
}

.rocket {
  position: absolute;
  width: 20px;
  height: 40px;
  background: linear-gradient(to top, #ff4444, #ff6666);
  border-radius: 10px 10px 0 0;
  box-shadow: 0 2px 8px rgba(255, 68, 68, 0.4);
}

.rocket::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 15px solid #ff6666;
}

@media (max-width: 900px) {
  #gameContainer {
    padding: 15px;
    margin: 10px;
  }
  
  #gameCanvas {
    width: 100%;
    max-width: 800px;
    height: auto;
  }
  
  header h1 {
    font-size: 2rem;
  }
  
  #gameInfo {
    flex-direction: column;
    gap: 10px;
  }
  
  #gameInfo div {
    font-size: 1rem;
  }
  
  #startContent, #gameOverContent, #rankingContent {
    padding: 30px 20px;
    max-width: 350px;
  }
  
  .ranking-header, .ranking-item {
    grid-template-columns: 40px 1fr 60px 40px 40px 50px;
    gap: 8px;
    font-size: 0.8rem;
  }
  
  .ranking-header {
    text-align: center;
    align-items: center;
  }
  
  .date {
    display: none;
  }
  
  button {
    font-size: 1.1rem;
    padding: 12px 25px;
  }
}

@media (max-width: 600px) {
  /* モバイル D-pad を操作方法テキストのさらに下に配置 */
  .mobile-controls {
    bottom: calc(env(safe-area-inset-bottom) + 90px);
    left: 20px;
    transform: none;
  }
  body {
    padding: 5px;
  }
  
  #gameContainer {
    padding: 10px;
    margin: 5px;
    width: calc(100% - 10px);
    max-width: none;
  }
  
  header h1 {
    font-size: 1.5rem;
    margin-bottom: 10px;
  }
  
  #gameInfo {
    padding: 10px;
    margin-bottom: 15px;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  #gameInfo div {
    font-size: 0.9rem;
    margin: 5px;
  }
  
  #gameCanvas {
    width: 100%;
    max-width: 100%;
    height: auto;
    min-height: 250px;
  }
  
  #startContent, #gameOverContent, #rankingContent {
    padding: 20px 15px;
    max-width: 300px;
    width: 90%;
  }
  
  /* モバイルのゲームオーバー画面をより小さく */
  #gameOverContent {
    max-width: 280px;
    padding: 15px 12px;
    transform: translateY(-120px);
  }
  
  /* モバイル用ゲームオーバー表示位置調整 */
  #gameOverScreen {
    align-items: flex-start;
    padding-top: 2vh;
  }
  
  #rankingList {
    max-height: 300px;
  }
  
  .ranking-header, .ranking-item {
    grid-template-columns: 35px 1fr 50px 30px 35px;
    gap: 5px;
    font-size: 0.65rem;
    padding: 8px 10px;
  }
  
  .ranking-header {
    text-align: center;
    align-items: center;
    padding: 10px 8px;
  }
  
  .distance, .date {
    display: none;
  }
  
  #gameOverContent button, #startContent button {
    width: 100%;
    margin: 5px 0;
    min-height: 50px;
    white-space: normal;
    overflow: hidden;
  }
  
  #startContent h2, #gameOverContent h2, #rankingContent h2 {
    font-size: 1.5rem;
    margin-bottom: 15px;
  }
  
  #startContent p, #gameOverContent p {
    font-size: 1rem;
    margin: 10px 0;
    line-height: 1.4;
  }
  
  button {
    font-size: 0.9rem;
    padding: 10px 15px;
    width: 100%;
    max-width: 200px;
    line-height: 1.2;
  }
  
  #controls {
    font-size: 0.9rem;
    margin-top: 10px;
    padding: 0 10px;
  }
}

/* === Circular Joystick Control Styles === */
/* Hide legacy D-pad completely */
.mobile-controls .d-pad {
  display: none !important;
}

/* Joystick container scales well on different devices */
.joystick {
  width: 120px;
  height: 120px;
  position: relative;
  touch-action: none;
  display: none; /* スティックを非表示 */
}

/* Base circle */
#joystickBase {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  border: 2px solid rgba(255, 255, 255, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  backdrop-filter: blur(6px);
}

/* Thumb that moves inside base */
#joystickThumb {
  width: 50%;
  height: 50%;
  border-radius: 50%;
  background: linear-gradient(45deg, rgba(102, 126, 234, 0.8), rgba(118, 75, 162, 0.8));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none; /* Allow events to pass to base */
  transition: transform 0.05s linear;
}

/* Slightly smaller joystick on very small screens */
@media (max-width: 400px) {
  .joystick {
    width: 90px;
    height: 90px;
  }
}

/* モバイル用コントロール */
.mobile-controls {
  position: fixed; /* ビューポート基準で最下部に */
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  left: 20px;
  transform: none;
  z-index: 1000;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  transition: opacity 0.3s ease;
  opacity: 0.5;
}

.mobile-controls.game-playing {
  display: block;
  opacity: 0.5;
}

.mobile-controls.game-hidden {
  opacity: 0;
  pointer-events: none;
}

.d-pad {
  display: none; /* ジョイスティック採用のため非表示 */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.horizontal-controls {
  display: flex;
  align-items: center;
  gap: 5px;
}

.control-btn {
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(45deg, rgba(102, 126, 234, 0.6), rgba(118, 75, 162, 0.6));
  color: white;
  font-size: 1.5rem;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  margin: 0;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.control-btn:hover,
.control-btn:active,
.control-btn.active {
  background: linear-gradient(45deg, rgba(102, 126, 234, 0.8), rgba(118, 75, 162, 0.8));
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

/* タッチ時は親要素の透明度を一時的に上げて視認性向上 */
.mobile-controls:has(.control-btn.active) {
  opacity: 0.5 !important;
}

.center-btn {
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
}

/* モバイル環境でのみ表示 */
@media (max-width: 900px) {
  .mobile-controls {
    display: block;
  }
  
  .mobile-controls.hidden {
    display: none;
  }
}

@media (max-width: 400px) {
  #gameContainer {
    padding: 5px;
    margin: 2px;
  }
  
  header h1 {
    font-size: 1.3rem;
  }
  
  #gameInfo div {
    font-size: 0.8rem;
  }
  
  #startContent, #gameOverContent, #rankingContent {
    padding: 15px 10px;
    max-width: 280px;
  }
  
  #startContent h2, #gameOverContent h2, #rankingContent h2 {
    font-size: 1.3rem;
  }
  
  #startContent p, #gameOverContent p {
    font-size: 0.9rem;
  }
  
  button {
    font-size: 0.9rem;
    padding: 10px 15px;
  }
  
  /* 400px以下での追加調整 */
  #nameInputSection {
    margin: 10px 0;
    padding: 12px;
  }
  
  #nameInputSection label {
    font-size: 0.9rem;
    margin-bottom: 6px;
  }
  
  #playerName {
    padding: 8px;
    font-size: 0.9rem;
    margin-bottom: 10px;
  }
  
  #saveScoreBtn {
    padding: 8px 15px;
    font-size: 0.85rem;
  }
  
  /* ゲームオーバー表示をさらに上に */
  #gameOverScreen {
    padding-top: 1vh;
  }
  
  #gameOverContent {
    transform: translateY(-180px);
    max-height: 75vh;
  }

  /* ランキングリスト表示調整 */
  #rankingList {
    max-height: 250px;
  }

  #rankingList .ranking-header,
  #rankingList .ranking-item {
    grid-template-columns: 30px 1fr 40px 25px;
    font-size: 0.6rem;
  }
}