* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  touch-action: manipulation;
}

#game-container, #gameCanvas {
  touch-action: none;
}

html {
  height: 100%;
  height: 100dvh;
  height: -webkit-fill-available;
  overflow: hidden;
}

body {
  background: radial-gradient(circle at top, #1f3b7b 0%, #152347 45%, #0f1530 100%);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  overscroll-behavior: none;
  padding: 0;
}

#game-container {
  position: relative;
  width: 95%;
  max-width: 100vw;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 4px solid #0066cc;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.45);
}

/* Fullscreen button (PC only) */
.fullscreen-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 60;
  border-radius: 18px;
  border: 2px solid rgba(255, 215, 0, 0.85);
  background: radial-gradient(circle at top, #004499 0%, #001933 100%);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 4px 10px;
  cursor: pointer;
  opacity: 0.9;
  transition: opacity 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
}

.fullscreen-btn:hover {
  opacity: 1;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
  transform: scale(1.06);
}

.fullscreen-btn:active {
  transform: scale(0.94);
}

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

.overlay {
  position: absolute;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.84);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  padding: 16px 20px 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transition: opacity 0.35s ease;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

#title-screen {
  background:
    linear-gradient(to bottom, rgba(0,0,0,0.75) 0%, rgba(15,21,48,0.88) 100%),
    url('../assets/backgrounds/bg-level1-os.jpg') center/cover no-repeat;
}

@keyframes floatUp {
  0% { transform: translateY(0) scale(1); opacity: 0.6; }
  100% { transform: translateY(-100%) scale(0.5); opacity: 0; }
}

#title-screen::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(2px 2px at 15% 80%, #ffd700 50%, transparent 50%),
    radial-gradient(2px 2px at 45% 90%, #fff 50%, transparent 50%),
    radial-gradient(2px 2px at 75% 85%, #ffd700 50%, transparent 50%),
    radial-gradient(1.5px 1.5px at 30% 70%, #fff 50%, transparent 50%),
    radial-gradient(1.5px 1.5px at 85% 75%, #ffd700 50%, transparent 50%),
    radial-gradient(1.5px 1.5px at 55% 95%, #ffd700 50%, transparent 50%),
    radial-gradient(2px 2px at 10% 60%, #fff 50%, transparent 50%);
  background-size: 100% 100%;
  animation: floatUp 8s linear infinite;
  pointer-events: none;
  z-index: 0;
}

/* Ensure title content is above the ::before sparkles */
#title-screen .title-step {
  position: relative;
  z-index: 1;
}

@keyframes titleGlow {
  0%, 100% { text-shadow: 2px 2px 0 #0059b3, 0 0 20px rgba(255, 215, 0, 0.3); }
  50% { text-shadow: 2px 2px 0 #0059b3, 0 0 36px rgba(255, 215, 0, 0.6); }
}

#title-screen h1 {
  color: #ffd700;
  text-shadow: 2px 2px 0 #0059b3;
  animation: titleGlow 3s ease-in-out infinite;
}

.high-score-display {
  color: #ffd700;
  font-weight: 700;
}

.story {
  font-size: 0.85rem;
  line-height: 1.5;
  max-width: 460px;
  color: #d9e2ff;
}

.profile-card,
.leaderboard-card,
.audio-controls {
  width: min(100%, 430px);
  border: 2px solid rgba(255, 215, 0, 0.5);
  border-radius: 10px;
  background: rgba(8, 20, 44, 0.7);
  padding: 10px;
}

.how-to-play-card {
  width: min(100%, 460px);
  border: 2px solid rgba(255, 215, 0, 0.5);
  border-radius: 10px;
  background: rgba(8, 20, 44, 0.9);
  padding: 10px 12px;
  color: #d9e2ff;
  text-align: left;
}

.how-to-play-card h2 {
  margin-bottom: 6px;
  font-size: 1rem;
  color: #ffd700;
}

.title-step {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.title-step-hidden {
  display: none;
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 6px;
}

.controls-column {
  flex: 1 1 160px;
  font-size: 0.78rem;
}

.controls-label {
  font-weight: 700;
  color: #cfe0ff;
  margin-bottom: 2px;
}

.controls-detail span {
  color: #ffd700;
}

.controls-note {
  font-size: 0.78rem;
  color: #cfe0ff;
  margin-top: 4px;
}

.profile-card label {
  display: block;
  text-align: left;
  font-size: 0.8rem;
  color: #ffd700;
  margin-bottom: 6px;
}

.name-row {
  display: flex;
  gap: 8px;
}

#player-name-input {
  flex: 1;
  min-width: 0;
  border: 2px solid #3b62aa;
  border-radius: 6px;
  background: #0d1e42;
  color: #fff;
  padding: 7px 8px;
  font-size: 0.95rem;
  -webkit-user-select: text;
  user-select: text;
}

#save-name-btn {
  border: 2px solid #ffd700;
  border-radius: 6px;
  background: linear-gradient(180deg, #0066cc 0%, #004499 100%);
  color: #fff;
  font-weight: 700;
  padding: 7px 12px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease;
}

#save-name-btn:hover {
  box-shadow: 0 0 12px rgba(255, 215, 0, 0.4);
  transform: scale(1.05);
}

#save-name-btn:active {
  transform: scale(0.94);
}

.input-hint {
  min-height: 18px;
  margin-top: 6px;
  text-align: left;
  color: #ff8e8e;
  font-size: 0.75rem;
}

.audio-controls {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px 10px;
}

.audio-controls button {
  border: 2px solid #ffd700;
  border-radius: 6px;
  background: #0056ad;
  color: #fff;
  font-weight: 700;
  padding: 6px 10px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease;
}

.audio-controls button:hover {
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
  transform: scale(1.05);
}

.audio-controls button:active {
  transform: scale(0.94);
}

.audio-controls label {
  justify-self: start;
  color: #cfe0ff;
  font-size: 0.85rem;
}

.audio-controls input[type="range"] {
  width: 100%;
}

.leaderboard-card h3 {
  color: #ffd700;
  font-size: 0.95rem;
  margin-bottom: 6px;
}

.leaderboard-card ol {
  text-align: left;
  margin-left: 18px;
  display: grid;
  gap: 4px;
  font-size: 0.8rem;
}

.start-btn {
  border: 3px solid #ffd700;
  border-radius: 8px;
  background: linear-gradient(180deg, #0066cc 0%, #004499 100%);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 700;
  padding: 10px 32px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.start-btn:hover {
  box-shadow: 0 0 16px rgba(255, 215, 0, 0.5);
  transform: scale(1.04);
}

.start-btn:active {
  transform: scale(0.95);
  box-shadow: 0 0 8px rgba(255, 215, 0, 0.3);
}

.start-btn.secondary {
  background: linear-gradient(180deg, #666 0%, #444 100%);
  border-color: #888;
}

.instructions {
  color: #8da2cc;
  font-size: 0.75rem;
}

.instructions span {
  color: #ffd700;
}

#level-transition h1 {
  color: #4caf50;
  font-size: 1.7rem;
}

#level-name {
  color: #ffd700;
  font-size: 1.15rem;
  font-weight: 700;
}

#level-intro {
  color: #dfe8ff;
  max-width: 460px;
  line-height: 1.5;
}

#transition-skip-hint {
  min-height: 18px;
  color: #9fc0ff;
  font-size: 0.78rem;
}

#retry-overlay h1 {
  color: #ffd700;
}

#retry-message {
  color: #e2ecff;
}

#prize-screen h1 {
  color: #ffd700;
}

.prize-subtitle {
  color: #6ee276;
}

#prize-image {
  width: min(65%, 320px);
  max-height: 140px;
  object-fit: contain;
  border: 3px solid #ffd700;
  border-radius: 10px;
}

#prize-name {
  color: #ffd700;
  font-weight: 700;
}

.prize-location {
  color: #cbdaf7;
}

.stats {
  display: grid;
  gap: 4px;
  color: #ffd700;
}

.bonus-prizes {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
}

.bonus-prizes-title {
  width: 100%;
  color: #ffd700;
  font-size: 0.85rem;
}

.bonus-prize-card {
  width: 94px;
  text-align: center;
}

.bonus-prize-card img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border: 2px solid #ffd700;
  border-radius: 8px;
}

.bonus-prize-card p {
  color: #fff;
  font-size: 0.75rem;
  margin-top: 4px;
}

#touch-controls {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: none;
  justify-content: space-between;
  z-index: 50;
  pointer-events: none;
}

#dpad {
  display: flex;
  gap: 12px;
  pointer-events: auto;
}

#dpad button,
#jump-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 3px solid #ffd700;
  background: rgba(255, 215, 0, 0.75);
  color: #fff;
  font-weight: 700;
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  transition: transform 0.1s ease, background 0.1s ease, box-shadow 0.1s ease;
}

#dpad button:active,
#jump-btn:active {
  transform: scale(0.88);
  background: rgba(255, 215, 0, 1);
  box-shadow: 0 0 12px rgba(255, 215, 0, 0.6);
}

#jump-btn {
  width: 72px;
  border-radius: 36px;
}

#loading {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #13264d;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.loading-bar {
  width: 230px;
  height: 16px;
  border: 2px solid #0066cc;
  border-radius: 8px;
  overflow: hidden;
  background: #20325f;
}

.loading-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, #0066cc 0%, #ffd700 100%);
  transition: width 0.3s ease-out;
}

#rotate-prompt {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  color: #fff;
  background: radial-gradient(circle at top, #1f3b7b 0%, #0f1530 80%);
  padding: 24px;
}

#rotate-prompt h2 {
  color: #ffd700;
}

@media (max-width: 1024px) {
  body {
    background: #000;
  }
  #game-container {
    /* Fit the largest 16:9 rectangle inside the mobile viewport */
    width: min(100vw, calc(100vh * 16 / 9));
    width: min(100vw, calc(100dvh * 16 / 9));
    height: min(100vh, calc(100vw * 9 / 16));
    height: min(100dvh, calc(100vw * 9 / 16));
    max-width: 100vw;
    max-height: 100vh;
    max-height: 100dvh;
    aspect-ratio: 16 / 9;
    border: none;
    border-radius: 0;
    box-shadow: none;
  }
  #touch-controls {
    display: flex;
  }
  .fullscreen-btn {
    display: none;
  }
}

/* Scale touch controls on very short landscape screens (small phones) */
@media (max-height: 500px) {
  #dpad button,
  #jump-btn {
    width: 44px;
    height: 44px;
    font-size: 0.8rem;
  }
  #jump-btn {
    width: 60px;
  }
  #touch-controls {
    bottom: 8px;
    left: 8px;
    right: 8px;
  }
}

@media (min-width: 1025px) {
  body {
    padding: 8px;
  }
  #game-container {
    width: min(100vw - 16px, calc((100vh - 16px) * 16 / 9));
    max-width: none;
    max-height: calc(100vh - 16px);
  }
}

/* === Coupon reward overlay (Level 6) === */
.coupon-reward-card {
  width: min(100%, 420px);
  border: 2px solid #ffd700;
  border-radius: 10px;
  background: rgba(8, 20, 44, 0.9);
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.coupon-reward-card img.coupon-img {
  width: min(70%, 260px);
  max-height: 120px;
  object-fit: contain;
  border: 2px solid #ffd700;
  border-radius: 8px;
}

.coupon-reward-card .coupon-name {
  color: #ffd700;
  font-weight: 700;
  font-size: 0.95rem;
}

.coupon-reward-card .coupon-location {
  color: #cbdaf7;
  font-size: 0.8rem;
}

.coupon-reward-card .coupon-url {
  color: #6ee276;
  font-size: 0.75rem;
  word-break: break-all;
}

.coupon-reward-card .coupon-url a {
  color: #6ee276;
  text-decoration: underline;
}

.line-section {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 2px solid #06c755;
  border-radius: 10px;
  background: rgba(6, 199, 85, 0.12);
  padding: 8px 12px;
  width: min(100%, 420px);
}

.line-section img {
  width: 64px;
  height: 64px;
  border-radius: 6px;
}

.line-section .line-text {
  color: #fff;
  font-size: 0.82rem;
  text-align: left;
}

.line-section .line-text strong {
  color: #06c755;
}

.line-section .line-text a {
  color: #6ee276;
  text-decoration: underline;
  font-size: 0.75rem;
}

/* Small LINE banner for title menu */
.line-banner-mini {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid #06c755;
  border-radius: 8px;
  background: rgba(6, 199, 85, 0.1);
  padding: 6px 10px;
  width: min(100%, 430px);
}

.line-banner-mini img {
  width: 48px;
  height: 48px;
  border-radius: 4px;
}

.line-banner-mini span {
  color: #06c755;
  font-size: 0.78rem;
  font-weight: 700;
}

.line-banner-mini a {
  color: #6ee276;
  font-size: 0.7rem;
  text-decoration: underline;
}

@media (max-width: 650px) and (orientation: portrait) {
  #rotate-prompt {
    display: flex;
  }
}

@media (max-height: 390px) and (orientation: landscape) {
  .overlay {
    padding: 10px;
  }
  .story {
    font-size: 0.72rem;
  }
  .leaderboard-card ol {
    font-size: 0.72rem;
  }
}
