:root {
  --bg: #f2efe6;
  --panel: #ffffff;
  --text: #2b2b2b;
  --accent: #e04b3a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Courier New", Courier, monospace;
  background: radial-gradient(circle at top, #ffffff 0%, var(--bg) 70%);
  color: var(--text);
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.game-shell {
  position: relative;
  background: var(--panel);
  padding: 16px;
  border: 4px solid var(--text);
  box-shadow: 10px 10px 0 #00000020;
}

#game {
  width: 960px;
  height: 540px;
  image-rendering: auto;
  background: #78c7f0;
  display: block;
}

.hud {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: bold;
  padding: 8px 16px;
  margin-top: 4px;
  background: linear-gradient(180deg, #2b2b2b 0%, #1a1a1a 100%);
  border-radius: 4px;
  color: #f5f5f5;
  text-transform: uppercase;
  letter-spacing: 1px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.hud-message {
  position: absolute;
  left: 50%;
  top: 28px;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 999px;
  border: 3px solid #1a1a1a;
  background: #fff7d6;
  color: #1a1a1a;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.2);
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease-out, transform 120ms ease-out;
  z-index: 3;
}

.hud-message.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(4px);
}

.hud.is-hidden {
  visibility: hidden;
}

.hud-group {
  display: flex;
  gap: 16px;
  align-items: center;
}

.hud-primary #hud-lives::before {
  content: "❤ ";
  color: #e04b3a;
}
.hud-primary #hud-coins::before {
  content: "● ";
  color: #f6d44d;
}
.hud-primary #hud-shards::before {
  content: "◆ ";
  color: #78c7f0;
}

.hud-center {
  text-align: center;
}

.hud-center #hud-time::before {
  content: "⏱ ";
}
.hud-center #hud-score::before {
  content: "★ ";
  color: #f6d44d;
}

.hud-secondary {
  font-size: 11px;
  opacity: 0.7;
  gap: 12px;
}

.start-overlay {
  position: absolute;
  inset: 16px;
  border: 3px solid var(--text);
  background: #ffffffcc;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  pointer-events: auto;
  cursor: pointer;
}

.pause-overlay {
  position: absolute;
  inset: 16px;
  border: 3px dashed var(--text);
  background: #ffffffcc;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  pointer-events: auto;
}

.complete-overlay {
  position: absolute;
  inset: 16px;
  border: 3px solid var(--accent);
  background: #ffffffcc;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  pointer-events: auto;
  cursor: pointer;
}

.intro-overlay {
  position: absolute;
  inset: 16px;
  border: 3px solid var(--text);
  background: #ffffffcc;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  pointer-events: auto;
  cursor: pointer;
}

.story-overlay {
  position: absolute;
  inset: 16px;
  border: 3px solid var(--text);
  background: linear-gradient(135deg, #fff6d6 0%, #ffffff 65%);
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  pointer-events: auto;
  cursor: pointer;
  gap: 8px;
  font-family: "Trebuchet MS", "Segoe Print", "Comic Sans MS", cursive;
  box-shadow: 6px 6px 0 #0000001a;
}

.story-overlay h2 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 3px;
  text-transform: uppercase;
}

#story-speaker {
  margin: 4px 0 0;
  padding: 4px 14px;
  border-radius: 999px;
  border: 2px solid var(--accent);
  background: #fff;
  color: var(--accent);
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 2px;
  box-shadow: 0 2px 0 #0000001a;
}

#story-text {
  margin: 6px 0 2px;
  padding: 10px 14px;
  max-width: 420px;
  border-radius: 14px;
  border: 2px solid var(--text);
  background: #fffdf2;
  font-size: 14px;
  line-height: 1.4;
}

.complete-overlay .yippee {
  margin: 4px 0 0;
  color: var(--accent);
  font-size: 18px;
  letter-spacing: 2px;
  animation: yippee-bounce 0.9s ease-in-out infinite;
}

@keyframes yippee-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-6px) scale(1.05);
  }
}

.is-hidden {
  display: none;
}

.start-overlay h1 {
  margin: 0 0 8px;
  font-size: 32px;
  letter-spacing: 2px;
  color: var(--accent);
}

.level-select {
  margin: 8px 0 4px;
  display: grid;
  gap: 6px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.level-options {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.level-option,
.difficulty-option {
  border: 2px solid var(--text);
  padding: 4px 8px;
  background: #ffffff;
}

.level-option.is-selected,
.difficulty-option.is-selected {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.difficulty-select {
  margin: 8px 0 4px;
  display: grid;
  gap: 6px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.difficulty-options {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.debug-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  text-transform: none;
  letter-spacing: 0;
}

.debug-toggle input {
  margin: 0;
}

.pause-options {
  display: grid;
  gap: 8px;
  margin: 8px 0;
}

.pause-option {
  border: 2px solid var(--text);
  padding: 6px 12px;
  background: #ffffff;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
}

.pause-option.is-selected {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.controls {
  font-size: 12px;
  opacity: 0.8;
}

.start-prompt-mobile,
.controls-mobile {
  display: none;
}

.prompt-mobile {
  display: none;
}

@media (pointer: coarse) {
  .start-prompt,
  .controls-keyboard,
  .prompt-keyboard {
    display: none;
  }
  .start-prompt-mobile,
  .controls-mobile,
  .prompt-mobile {
    display: block;
  }
  .touch-btn-dash {
    display: none;
  }
  .modding-toggle {
    display: none;
  }
}

/* Touch Controls */
.touch-controls {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  pointer-events: none;
  z-index: 100;
}

.touch-controls.is-visible {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.touch-dpad {
  display: grid;
  grid-template-columns: 48px 48px 48px;
  grid-template-rows: 48px 48px;
  gap: 4px;
  pointer-events: auto;
}

.touch-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
  border: 3px solid var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
  color: var(--text);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  pointer-events: auto;
}

.touch-btn:active,
.touch-btn.is-pressed {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.touch-btn-left {
  grid-column: 1;
  grid-row: 2;
}
.touch-btn-right {
  grid-column: 3;
  grid-row: 2;
}
.touch-btn-up {
  grid-column: 2;
  grid-row: 1;
  display: none;
}
.touch-btn-down {
  grid-column: 2;
  grid-row: 2;
  display: none;
}

.touch-actions {
  display: flex;
  gap: 12px;
  pointer-events: auto;
}

.touch-btn-action {
  width: 64px;
  height: 64px;
  font-size: 14px;
}

.touch-btn-jump {
  background: rgba(120, 199, 240, 0.8);
  width: 72px;
  height: 72px;
  font-size: 15px;
}

.touch-btn-rocket {
  background: rgba(255, 124, 58, 0.85);
  width: 56px;
  height: 56px;
  font-size: 12px;
}

.touch-btn-dash {
  background: rgba(224, 75, 58, 0.8);
  width: 52px;
  height: 52px;
  font-size: 12px;
}

.touch-btn-pause {
  position: fixed;
  top: 8px;
  right: 8px;
  width: 40px;
  height: 40px;
  font-size: 16px;
  z-index: 101;
  pointer-events: auto;
  display: none;
}

.touch-controls.is-visible .touch-btn-pause {
  display: flex;
}

.touch-btn-fullscreen {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 40px;
  height: 40px;
  font-size: 18px;
  z-index: 101;
  pointer-events: auto;
  display: none;
}

.touch-controls.is-visible .touch-btn-fullscreen {
  display: flex;
}

/* Hide fullscreen button when already in fullscreen */
:fullscreen .touch-btn-fullscreen,
:-webkit-full-screen .touch-btn-fullscreen {
  display: none;
}

/* Mobile Responsive */
@media (max-width: 980px) {
  body {
    min-height: 100dvh;
    padding: 8px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
  }

  .game-shell {
    padding: 8px;
    border-width: 3px;
    box-shadow: 5px 5px 0 #00000020;
    width: 100%;
    max-width: 100%;
  }

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

  .hud {
    font-size: 11px;
    padding: 6px 10px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .hud-message {
    top: 20px;
    font-size: 12px;
    padding: 8px 12px;
    max-width: calc(100% - 24px);
    text-align: center;
  }

  .hud-group {
    gap: 10px;
  }

  .hud-secondary {
    font-size: 9px;
    gap: 8px;
  }

  .start-overlay h1 {
    font-size: 24px;
  }

  .start-overlay,
  .pause-overlay,
  .complete-overlay,
  .intro-overlay,
  .story-overlay {
    inset: 8px;
    padding: 12px;
  }

  .controls {
    font-size: 10px;
  }

  #story-speaker {
    font-size: 12px;
  }

  #story-text {
    max-width: 320px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  body {
    padding: 4px;
  }

  .game-shell {
    padding: 4px;
    border-width: 2px;
  }

  .hud {
    font-size: 10px;
    padding: 4px 8px;
  }

  .hud-message {
    top: 16px;
    font-size: 11px;
  }

  .start-overlay h1 {
    font-size: 20px;
  }

  .touch-btn {
    width: 44px;
    height: 44px;
  }

  .touch-btn-action {
    width: 56px;
    height: 56px;
  }

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

  .touch-btn-rocket {
    width: 52px;
    height: 52px;
  }

  .touch-btn-dash {
    width: 44px;
    height: 44px;
  }
}

/* Landscape mobile optimization */
@media (max-height: 500px) and (orientation: landscape) {
  body {
    padding: 2px;
    justify-content: center;
    align-items: center;
  }

  .game-shell {
    padding: 2px;
    max-width: calc(100vw - 4px);
    max-height: calc(100dvh - 4px);
    display: flex;
    flex-direction: column;
  }

  #game {
    flex: 1;
    max-height: calc(100dvh - 40px);
    width: auto;
    max-width: 100%;
  }

  .hud {
    font-size: 9px;
    padding: 2px 6px;
    margin-top: 2px;
  }

  .hud-secondary {
    display: none;
  }

  .touch-controls {
    padding: 4px;
  }

  .touch-btn {
    width: 36px;
    height: 36px;
  }

  .touch-btn-action {
    width: 44px;
    height: 44px;
  }

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

  .touch-btn-rocket {
    width: 44px;
    height: 44px;
  }

  .touch-btn-dash {
    width: 36px;
    height: 36px;
  }

  .start-overlay,
  .pause-overlay,
  .complete-overlay,
  .intro-overlay,
  .story-overlay {
    inset: 2px;
    padding: 8px;
  }

  .start-overlay h1 {
    font-size: 18px;
    margin: 4px 0;
  }

  .controls {
    font-size: 9px;
  }

  #story-speaker {
    font-size: 11px;
  }

  #story-text {
    max-width: 260px;
    font-size: 12px;
  }
}

/* Modding UI */
.modding-overlay {
  position: absolute;
  inset: 16px;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  pointer-events: auto;
  z-index: 50;
}

.modding-overlay.is-hidden {
  display: none;
}

.modding-panel {
  background: #2b2b2b;
  color: #f5f5f5;
  width: 90%;
  max-width: 480px;
  border-radius: 8px;
  border: 2px solid #5dbb63;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.modding-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modding-header h3 {
  margin: 0;
  font-size: 14px;
  text-transform: uppercase;
  color: #5dbb63;
}

.modding-btn-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
}

.modding-history {
  background: #1a1a1a;
  height: 120px;
  overflow-y: auto;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #444;
  font-size: 12px;
  font-family: monospace;
}

.modding-message {
  margin-bottom: 4px;
}
.modding-message.system {
  color: #888;
}
.modding-message.user {
  color: #78c7f0;
}
.modding-message.agent {
  color: #5dbb63;
}
.modding-message.error {
  color: #e04b3a;
}

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

#modding-input {
  flex: 1;
  background: #1a1a1a;
  border: 1px solid #444;
  color: white;
  padding: 6px 8px;
  border-radius: 4px;
  font-family: inherit;
}

#modding-send,
#modding-reset {
  background: #5dbb63;
  border: none;
  color: #1a1a1a;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
  text-transform: uppercase;
  font-size: 11px;
}

#modding-reset {
  background: #e04b3a;
  color: white;
}

.modding-footer {
  display: flex;
  justify-content: flex-end;
}

.touch-btn-modding {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  font-size: 14px;
  z-index: 101;
  pointer-events: auto;
  display: none;
  background: rgba(43, 43, 43, 0.8);
  color: #5dbb63;
  border-color: #5dbb63;
}

.touch-controls.is-visible .touch-btn-modding {
  display: flex;
}

/* Always-visible modding toggle button */
.modding-toggle {
  position: absolute;
  top: 24px;
  right: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(43, 43, 43, 0.9);
  border: 2px solid #5dbb63;
  color: #5dbb63;
  font-size: 12px;
  font-weight: bold;
  font-family: inherit;
  cursor: pointer;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  box-shadow: 0 0 8px rgba(93, 187, 99, 0.4);
}

.modding-toggle:hover {
  background: #5dbb63;
  color: #1a1a1a;
  box-shadow: 0 0 16px rgba(93, 187, 99, 0.7);
  transform: scale(1.1);
}

.modding-toggle.is-active {
  background: #5dbb63;
  color: #1a1a1a;
  box-shadow: 0 0 20px rgba(93, 187, 99, 0.9);
}

.modding-toggle.is-hidden {
  display: none;
}

@media (max-width: 980px) {
  .modding-toggle {
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .modding-toggle {
    top: 8px;
    right: 8px;
    width: 32px;
    height: 32px;
    font-size: 10px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .modding-toggle {
    top: 6px;
    right: 52px;
    width: 28px;
    height: 28px;
    font-size: 9px;
  }
}
