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

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 14px;
  padding: 20px 16px;
  background: linear-gradient(160deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  font-family: "Segoe UI", system-ui, sans-serif;
  color: #eee;
}

#app {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.sprite-gallery {
  width: min(960px, 100%);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px;
  padding: 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(12, 20, 40, 0.7);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

.sprite-card {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.sprite-preview {
  width: 88px;
  height: 64px;
  image-rendering: pixelated;
  border-radius: 6px;
  background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.01));
}

.sprite-name {
  width: 100%;
  text-align: center;
  font-size: 10px;
  font-weight: 700;
  color: #f8f9fa;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sprite-meta {
  width: 100%;
  text-align: center;
  font-size: 10px;
  color: #adb5bd;
}

.hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  gap: 16px 24px;
  padding: 12px 20px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.6) 0%, transparent 100%);
  font-size: 14px;
  font-weight: 600;
  pointer-events: none;
}

.hud span::before {
  opacity: 0.7;
  margin-right: 6px;
}

#lives {
  color: #ff4d6d;
  font-size: 16px;
  letter-spacing: 2px;
}

#lives::before { content: "Lives "; color: #eee; opacity: 0.7; font-size: 14px; }

#coins::before { content: "Coins "; }
#score::before { content: "Score "; }
#power::before { content: "Power "; }
#level::before { content: "Level "; }

#mission {
  flex: 1 1 100%;
  color: #4cc9f0;
  font-size: 13px;
  max-width: 100%;
}

#mission::before { content: "Mission "; }

#mission.done {
  color: #95d5b2;
}

.mission-hud {
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.sound-toggle {
  pointer-events: auto;
  margin-left: auto;
  align-self: flex-start;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 18px;
  line-height: 1;
  padding: 6px 10px;
  cursor: pointer;
}

.sound-toggle:hover {
  background: rgba(255, 255, 255, 0.12);
}

.sound-toggle.muted {
  opacity: 0.65;
}

#coins {
  color: #ffd93d;
}

#power {
  color: #ff9f1c;
}

.pu-fire { color: #ff6b35; }
.pu-life { color: #ff4d6d; }

.game-viewport {
  position: relative;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0b1628;
  overflow: hidden;
}

#game {
  display: block;
  width: 100%;
  height: auto;
  max-width: 100%;
  background: #5c94fc;
  image-rendering: pixelated;
  touch-action: none;
}

.rotate-hint {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 12;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  text-align: center;
  background: rgba(8, 12, 24, 0.9);
  pointer-events: auto;
}

.rotate-hint.visible {
  display: flex;
}

.rotate-hint.hidden {
  display: none;
}

.rotate-hint-icon {
  font-size: 3rem;
  color: #ffd93d;
  animation: rotate-pulse 1.6s ease-in-out infinite;
}

.rotate-hint-title {
  font-size: 1.2rem;
  font-weight: 800;
  color: #ffd93d;
}

.rotate-hint-sub {
  font-size: 0.9rem;
  color: #cbd5e1;
  max-width: 260px;
}

@keyframes rotate-pulse {
  0%, 100% { transform: rotate(-12deg); }
  50% { transform: rotate(12deg); }
}

.touch-controls {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom, 0px));
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
}

.touch-cluster {
  display: flex;
  pointer-events: none;
  align-items: flex-end;
}

.touch-cluster-move {
  gap: 10px;
}

.touch-cluster-action {
  flex-direction: column-reverse;
  align-items: center;
  gap: 10px;
}

.touch-btn {
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  border: none;
  color: #fff;
  font-weight: 700;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.08s ease, filter 0.08s ease;
}

.touch-btn-move {
  width: 72px;
  height: 72px;
  border-radius: 18px;
  font-size: 1.55rem;
  background: rgba(15, 23, 42, 0.62);
  border: 2px solid rgba(255, 255, 255, 0.38);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}

.touch-btn-jump {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  font-size: 1.85rem;
  background: rgba(234, 88, 12, 0.72);
  border: 2px solid rgba(255, 217, 61, 0.85);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.touch-btn-shoot {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  font-size: 1.3rem;
  background: rgba(185, 28, 28, 0.72);
  border: 2px solid rgba(252, 165, 165, 0.85);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.touch-btn.active,
.touch-btn:active {
  transform: scale(0.93);
  filter: brightness(1.12);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 10px 12px 16px;
  background: rgba(8, 12, 24, 0.94);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transition: opacity 0.3s ease;
}

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

.overlay.visible {
  opacity: 1;
}

#app:has(.overlay.visible) .hud,
#app:has(.overlay.visible) .touch-controls {
  visibility: hidden;
  pointer-events: none;
}

.menu-card {
  width: min(400px, 100%);
  margin: auto 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 14px 18px;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(36, 48, 68, 0.98), rgba(22, 30, 46, 0.98));
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

.menu-header {
  text-align: center;
}

.menu-header h1 {
  font-size: 1.85rem;
  color: #ffd93d;
  text-shadow: 2px 2px 0 #e85d04;
  letter-spacing: 1px;
  line-height: 1.1;
}

.subtitle {
  color: #9ca3af;
  font-size: 0.88rem;
  margin-top: 4px;
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-label {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #9ca3af;
  margin: 0;
}

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: #ccc;
  margin: 8px 0;
  max-width: 90%;
  text-align: center;
}

.hero-picker {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.hero-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  border: 1px solid #5c6672;
  background: #1f2937;
  color: #e5e7eb;
  border-radius: 16px;
  padding: 8px 12px 10px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  min-width: 78px;
}

.hero-preview {
  width: 56px;
  height: 44px;
  image-rendering: pixelated;
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0));
}

.hero-option.selected {
  background: linear-gradient(180deg, #ffd93d 0%, #f4a261 100%);
  color: #1f2937;
  border-color: #ffd166;
}

.level-select {
  width: 100%;
  border-radius: 10px;
  border: 1px solid #5c6672;
  background: #1f2937;
  color: #e5e7eb;
  font-size: 14px;
  font-weight: 700;
  padding: 10px 12px;
}

.level-progress {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: #ffd93d;
  letter-spacing: 0.02em;
}

.level-map {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 3px;
  width: 100%;
  max-height: 88px;
  overflow-y: auto;
  padding: 6px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.level-map-cell {
  width: 100%;
  min-height: 26px;
  height: 26px;
  border: none;
  border-radius: 5px;
  font-size: 9px;
  font-weight: 800;
  cursor: pointer;
  color: #1f2937;
  background: #374151;
  opacity: 0.45;
  padding: 0;
  line-height: 1;
}

.level-map-cell.unlocked {
  opacity: 1;
  background: linear-gradient(180deg, #6ee7b7, #34d399);
  color: #064e3b;
}

.level-map-cell.completed {
  background: linear-gradient(180deg, #ffd93d, #f4a261);
  color: #7c2d12;
}

.level-map-cell.selected {
  outline: 2px solid #fff;
  outline-offset: 0;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
}

.level-map-cell.boss.unlocked {
  background: linear-gradient(180deg, #f87171, #dc2626);
  color: #fff;
}

.daily-panel {
  font-size: 12px;
  color: #cbd5e1;
  text-align: center;
  line-height: 1.4;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(76, 201, 240, 0.2);
}

.daily-panel strong {
  color: #4cc9f0;
}

.menu-actions-primary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
}

.menu-btn {
  padding: 11px 8px;
  font-size: 0.88rem;
  font-weight: 700;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  min-height: 42px;
  white-space: nowrap;
}

.menu-btn.primary {
  color: #1a1a2e;
  background: linear-gradient(180deg, #ffd93d 0%, #f4a261 100%);
  box-shadow: 0 3px 0 #e85d04;
}

.menu-btn.primary:hover {
  filter: brightness(1.05);
}

.menu-btn.primary:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #e85d04;
}

.menu-btn.secondary {
  color: #f8fafc;
  background: linear-gradient(180deg, #4b5563, #374151);
  box-shadow: 0 3px 0 #1f2937;
}

.menu-btn.secondary:disabled {
  opacity: 0.38;
  cursor: not-allowed;
  box-shadow: none;
}

.menu-btn.ghost {
  color: #e5e7eb;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: none;
}

.menu-btn-full {
  width: 100%;
}

.menu-panel {
  width: 100%;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 8px 10px;
  color: #e5e7eb;
  font-size: 13px;
}

.menu-panel summary {
  cursor: pointer;
  font-weight: 700;
  color: #ffd93d;
  user-select: none;
}

.panel-body {
  margin-top: 8px;
  max-height: 100px;
  overflow-y: auto;
}

.ach-item {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.ach-item.done {
  color: #95d5b2;
}

.ach-item .ach-title {
  font-weight: 700;
}

.lb-row {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.pu-star { color: #ffd93d; }
.pu-shroom { color: #e63946; }
.pu-jump { color: #4cc9f0; }
.pu-q { color: #c45c26; font-weight: bold; }

kbd {
  display: inline-block;
  padding: 2px 8px;
  margin: 0 2px;
  background: #333;
  border-radius: 4px;
  border: 1px solid #555;
  font-family: inherit;
  font-size: 0.85em;
}

.message {
  position: absolute;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  padding: 10px 24px;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 8px;
  font-size: 0.95rem;
  white-space: nowrap;
}

.message.hidden {
  display: none;
}

body.game-fullscreen {
  padding: 0;
  gap: 0;
}

body.game-fullscreen #app {
  width: 100vw;
  height: 100vh;
  max-height: 100dvh;
  border-radius: 0;
  display: flex;
  flex-direction: column;
}

body.game-fullscreen #game {
  flex: 1;
  width: 100%;
  height: auto;
  max-height: none;
}

body.game-active {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

@media (max-width: 1000px), (hover: none) {
  body {
    padding: 0;
    gap: 0;
  }

  #app {
    border-radius: 0;
    width: 100vw;
    max-width: 100vw;
  }

  body.game-active #app {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    display: flex;
    flex-direction: column;
  }

  body.game-active .game-viewport {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
  }

  .overlay {
    padding: 8px;
  }

  .menu-card {
    padding: 14px 12px 16px;
    gap: 10px;
  }

  .menu-header h1 {
    font-size: 1.55rem;
  }

  .hero-option {
    min-width: 68px;
    padding: 6px 8px 8px;
  }

  .menu-actions-primary {
    grid-template-columns: 1fr;
  }

  .menu-btn {
    font-size: 0.95rem;
    min-height: 44px;
  }

  .sprite-gallery {
    border-radius: 0;
    width: 100vw;
    padding: 8px 6px;
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    max-height: 32vh;
    overflow-y: auto;
  }

  .touch-controls {
    display: flex;
  }

  .message {
    bottom: 108px;
    max-width: 92vw;
    white-space: normal;
    text-align: center;
  }
}

@media (orientation: landscape) and (max-width: 1100px) {
  body.game-active .game-viewport {
    align-items: stretch;
    justify-content: center;
  }

  body.game-active #game {
    width: auto;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 16 / 9;
  }

  .touch-btn-move {
    width: 68px;
    height: 68px;
  }

  .touch-btn-jump {
    width: 84px;
    height: 84px;
  }

  .touch-btn-shoot {
    width: 58px;
    height: 58px;
  }
}

@media (orientation: portrait) and (max-width: 900px) {
  body.game-active #app {
    height: 100dvh;
    max-height: 100dvh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "hud"
      "game"
      "controls";
  }

  body.game-active .hud {
    position: relative;
    grid-area: hud;
    z-index: 3;
    font-size: 11px;
    gap: 4px 10px;
    padding: calc(6px + env(safe-area-inset-top, 0px)) 10px 6px;
    line-height: 1.2;
    background: rgba(8, 12, 24, 0.92);
    pointer-events: auto;
  }

  body.game-active .hud span::before {
    display: none;
  }

  body.game-active #lives {
    font-size: 14px;
    letter-spacing: 1px;
  }

  body.game-active #coins,
  body.game-active #score,
  body.game-active #level {
    font-size: 11px;
  }

  body.game-active #power {
    display: none;
  }

  body.game-active .sound-toggle {
    padding: 4px 8px;
    font-size: 15px;
  }

  body.game-active .game-viewport {
    grid-area: game;
    align-items: center;
    justify-content: center;
    min-height: 0;
    overflow: hidden;
    padding: 0;
  }

  body.game-active #game {
    display: block;
    width: 100%;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 16 / 9;
    transform-origin: center center;
    will-change: transform;
  }

  body.game-active .touch-controls {
    position: relative;
    grid-area: controls;
    z-index: 4;
    display: flex;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
    background: rgba(8, 12, 24, 0.92);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }

  .touch-btn-move {
    width: 58px;
    height: 58px;
    font-size: 1.25rem;
    border-radius: 14px;
    background: rgba(15, 23, 42, 0.78);
  }

  .touch-btn-jump {
    width: 74px;
    height: 74px;
    font-size: 1.55rem;
  }

  .touch-btn-shoot {
    width: 52px;
    height: 52px;
    font-size: 1.05rem;
  }

  body.game-active .message {
    bottom: auto;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 0.85rem;
    padding: 8px 14px;
    z-index: 6;
  }

  .overlay .menu-card {
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }
}
