:root {
  --sky: #5c94fc;
  --sky-light: #94b8ff;
  --ground: #c84c0c;
  --panel: #1565c0;
  --panel-dark: #0d47a1;
  --accent: #ffc107;
  --accent-dark: #ff8f00;
  --success: #43a047;
  --font: "Fredoka", system-ui, sans-serif;
}

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

html, body {
  height: 100%;
  font-family: var(--font);
  background: linear-gradient(180deg, var(--sky) 0%, var(--sky-light) 100%);
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: clamp(6px, 1.5vw, 12px);
  padding-top: max(clamp(6px, 1.5vw, 12px), env(safe-area-inset-top));
  padding-bottom: max(clamp(6px, 1.5vw, 12px), env(safe-area-inset-bottom));
  padding-left: max(clamp(6px, 1.5vw, 12px), env(safe-area-inset-left));
  padding-right: max(clamp(6px, 1.5vw, 12px), env(safe-area-inset-right));
  height: 100dvh;
  height: 100svh;
  max-height: 100dvh;
  max-height: 100svh;
  display: flex;
  flex-direction: column;
  gap: clamp(4px, 1vw, 10px);
  overflow: hidden;
}

body[data-mode="play"] header.top,
body[data-mode="editor"] header.top {
  padding: 6px 12px;
}

body[data-mode="play"] header.top h1,
body[data-mode="editor"] header.top h1 {
  font-size: 1rem;
}

body[data-mode="play"] header.top .subtitle,
body[data-mode="editor"] header.top .subtitle {
  display: none;
}

body[data-mode="play"] #promptBar {
  min-height: 56px;
  padding: 6px 12px;
}

body[data-mode="play"] .hear-btn {
  width: 44px;
  height: 44px;
  font-size: 1.25rem;
}

@media (max-width: 480px), (max-height: 700px) {
  .app {
    padding: 4px;
    padding-top: max(4px, env(safe-area-inset-top));
    padding-bottom: max(4px, env(safe-area-inset-bottom));
    gap: 4px;
  }

  body[data-mode="play"] header.top,
  body[data-mode="editor"] header.top {
    padding: 4px 8px;
    border-radius: 10px;
    border-width: 2px;
    box-shadow: 0 2px 0 var(--panel-dark);
  }

  body[data-mode="play"] header.top h1,
  body[data-mode="editor"] header.top h1 {
    font-size: 0.85rem;
  }

  body[data-mode="play"] .btn-small {
    padding: 4px 8px;
    font-size: 0.65rem;
  }

  #canvasWrap {
    border-width: 2px;
    border-radius: 8px;
  }

  body[data-mode="play"] #promptBar {
    min-height: 44px;
    padding: 4px 8px;
    gap: 8px;
    border-width: 2px;
    border-radius: 8px;
  }

  body[data-mode="play"] .hear-btn {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }

  .obj-letter,
  .obj-number {
    font-size: clamp(1.75rem, 8vw, 2.5rem);
  }

  .objective-visual {
    min-height: 40px;
  }

  body[data-mode="play"] #touchControls {
    padding: 4px 0;
    gap: 8px;
  }

  body[data-mode="play"] .touch-btn {
    width: 52px;
    height: 52px;
    font-size: 1.2rem;
    border-radius: 12px;
  }

  body[data-mode="play"] .touch-btn.jump-btn {
    width: 64px;
    height: 64px;
    font-size: 0.85rem;
  }

}

header.top {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border-radius: 16px;
  padding: 10px 16px;
  border: 4px solid var(--accent);
  box-shadow: 0 4px 0 var(--panel-dark);
}

header.top h1 {
  color: #fff;
  font-size: clamp(1.1rem, 4vw, 1.6rem);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

header.top .subtitle {
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: bold;
}

.header-btns {
  display: flex;
  gap: 8px;
}

.btn {
  font-family: var(--font);
  font-weight: bold;
  font-size: 0.85rem;
  padding: 8px 14px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  border-bottom: 4px solid rgba(0,0,0,0.2);
  transition: transform 0.1s, filter 0.1s;
}

.btn:active {
  transform: translateY(2px);
  border-bottom-width: 2px;
}

.btn-primary {
  background: var(--accent);
  color: #1a1a1a;
}

.btn-secondary {
  background: #fff;
  color: var(--panel);
}

.btn-ghost {
  background: rgba(255,255,255,0.2);
  color: #fff;
  border-bottom-color: rgba(0,0,0,0.15);
}

.btn-small {
  padding: 6px 10px;
  font-size: 0.75rem;
}

#canvasWrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  overflow: hidden;
  border: 4px solid #333;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  background: #5c94fc;
}

#gameCanvas {
  display: block;
  flex-shrink: 0;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Menu overlay */
#menuScreen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(92, 148, 252, 0.92);
  gap: 16px;
  z-index: 10;
  padding: 20px;
}

#menuScreen.hidden { display: none; }

.menu-title {
  font-size: clamp(2rem, 8vw, 3rem);
  color: #fff;
  text-shadow: 3px 3px 0 var(--panel-dark);
  text-align: center;
}

.menu-stats {
  display: flex;
  gap: 20px;
  color: #fff;
  font-size: 1.1rem;
  font-weight: bold;
}

.menu-stats span {
  background: rgba(0,0,0,0.2);
  padding: 8px 16px;
  border-radius: 12px;
}

.menu-btns {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(280px, 90%);
}

.menu-btns .btn {
  font-size: 1.2rem;
  padding: 16px;
  width: 100%;
}

/* Win overlay */
#winScreen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(67, 160, 71, 0.92);
  gap: 16px;
  z-index: 10;
  padding: 20px;
}

#winScreen.hidden { display: none; }

.win-title {
  font-size: 2.5rem;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

#winMessage {
  color: #fff;
  font-size: 1.2rem;
  text-align: center;
  max-width: 320px;
}

.win-btns {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

/* HUD */
#hud {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 5;
}

#hud.hidden { display: none; }

.hud-chip {
  background: rgba(0,0,0,0.55);
  color: #fff;
  padding: 6px 12px;
  border-radius: 10px;
  font-weight: bold;
  font-size: 0.85rem;
  pointer-events: auto;
}

#promptBar {
  position: relative;
  flex-shrink: 0;
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 16px;
  color: #fff;
  border: 3px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-height: 72px;
}

#promptBar.hidden { display: none; }

#promptBar.wrong-flash {
  background: #c62828;
  border-color: #ff8a80;
}

.hear-btn {
  flex-shrink: 0;
  width: 52px;
  height: 52px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--accent-dark);
  transition: transform 0.1s;
}

.hear-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--accent-dark);
}

.objective-visual {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 56px;
}

.obj-letter,
.obj-number {
  font-size: clamp(2.5rem, 10vw, 3.5rem);
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-shadow: 3px 3px 0 rgba(0, 0, 0, 0.25);
  font-family: var(--font);
}

.obj-color {
  display: block;
  width: clamp(48px, 12vw, 64px);
  height: clamp(48px, 12vw, 64px);
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.obj-stars,
.obj-coins {
  display: flex;
  gap: 6px;
  font-size: clamp(1.6rem, 6vw, 2.2rem);
  line-height: 1;
}

.obj-star.filled,
.obj-coin.filled {
  transform: scale(1.05);
}

.obj-flag {
  font-size: 2.5rem;
}

.collect-pop {
  position: absolute;
  right: 16px;
  font-size: 2rem;
  animation: pop-in 0.3s ease-out;
}

.collect-pop.wrong {
  filter: drop-shadow(0 0 6px rgba(229, 57, 53, 0.8));
}

#collectPop.hidden { display: none; }

@keyframes pop-in {
  0% { transform: scale(0); opacity: 0; }
  70% { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

/* Editor bar */
#editorBar {
  background: #fff;
  border-radius: 12px;
  padding: 12px;
  border: 3px solid var(--panel);
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
  max-height: min(38vh, 280px);
  overflow-y: auto;
}

#editorBar.hidden { display: none; }

.editor-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

#toolPalette {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 8px;
  border: 2px solid #ddd;
  border-radius: 8px;
  background: #f5f5f5;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.65rem;
  min-width: 52px;
  transition: border-color 0.15s, background 0.15s;
}

.tool-btn.active {
  border-color: var(--panel);
  background: #e3f2fd;
}

.tool-icon { font-size: 1.2rem; }

.tool-input {
  font-family: var(--font);
  padding: 4px 8px;
  border: 2px solid #ccc;
  border-radius: 6px;
  font-size: 0.9rem;
}

.level-name-input {
  flex: 1;
  min-width: 120px;
  font-family: var(--font);
  padding: 8px 12px;
  border: 2px solid #ccc;
  border-radius: 8px;
  font-size: 0.95rem;
}

/* Touch controls */
#touchControls {
  display: none;
  flex-shrink: 0;
  justify-content: space-between;
  align-items: flex-end;
  padding: 8px 0;
  gap: 16px;
}

body.touch-controls-active #touchControls {
  display: flex;
}

#touchControls.hidden { display: none !important; }

.dpad {
  display: flex;
  gap: 8px;
}

.touch-btn {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  border: none;
  background: rgba(255,255,255,0.85);
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0,0,0,0.2);
  touch-action: manipulation;
}

.touch-btn.pressed {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0,0,0,0.2);
  background: var(--accent);
}

.touch-btn.jump-btn {
  width: 80px;
  height: 80px;
  background: var(--success);
  color: #fff;
  font-size: 1rem;
  font-weight: bold;
  font-family: var(--font);
}

/* Toast */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 10px 20px;
  border-radius: 10px;
  font-weight: bold;
  z-index: 100;
  pointer-events: none;
}

#toast.hidden { display: none; }

.celebration-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.8);
  background: rgba(67, 160, 71, 0.95);
  color: #fff;
  padding: 20px 32px;
  border-radius: 16px;
  font-size: clamp(1rem, 3vw, 1.4rem);
  font-weight: bold;
  text-align: center;
  z-index: 15;
  border: 4px solid var(--accent);
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  max-width: 90%;
}

.celebration-banner.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.hidden { display: none !important; }

footer {
  flex-shrink: 0;
  text-align: center;
  color: rgba(255,255,255,0.7);
  font-size: 0.65rem;
  padding: 2px 8px 4px;
}

body[data-mode="play"] footer,
body[data-mode="editor"] footer {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* Sprite picker modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal.hidden { display: none !important; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(13, 71, 161, 0.65);
  backdrop-filter: blur(4px);
}

.modal-panel {
  position: relative;
  background: #fff;
  border-radius: 20px;
  border: 4px solid var(--accent);
  padding: 24px 20px 20px;
  max-width: 520px;
  width: 100%;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.35);
  animation: modal-in 0.25s ease-out;
}

@keyframes modal-in {
  from { transform: scale(0.92) translateY(12px); opacity: 0; }
  to { transform: scale(1) translateY(0); opacity: 1; }
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: #eee;
  font-size: 1.1rem;
  cursor: pointer;
  font-family: var(--font);
  line-height: 1;
}

.modal-close:hover { background: #ddd; }

.modal-title {
  font-size: 1.5rem;
  color: var(--panel);
  text-align: center;
  margin-bottom: 4px;
}

.modal-subtitle {
  text-align: center;
  color: #666;
  font-size: 0.95rem;
  margin-bottom: 20px;
}

.sprite-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.sprite-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 8px;
  border: 3px solid #ddd;
  border-radius: 14px;
  background: #f8f8f8;
  cursor: pointer;
  font-family: var(--font);
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.sprite-option:hover {
  border-color: var(--panel);
  background: #e3f2fd;
  transform: translateY(-2px);
}

.sprite-option.selected {
  border-color: var(--accent);
  background: #fff8e1;
  box-shadow: 0 0 0 2px var(--accent);
}

.sprite-preview {
  width: 96px;
  height: 96px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: linear-gradient(180deg, #94b8ff 55%, #c84c0c 55%);
  border-radius: 10px;
  border: 2px solid rgba(0,0,0,0.1);
}

.sprite-preview-custom {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #e1bee7, #ce93d8);
}

.custom-icon { font-size: 2rem; }

.sprite-name {
  font-weight: bold;
  font-size: 0.9rem;
  color: #333;
}

.sprite-desc {
  font-size: 0.7rem;
  color: #888;
}

@media (max-width: 480px) {
  .sprite-picker {
    grid-template-columns: 1fr;
  }

  .sprite-option {
    flex-direction: row;
    justify-content: flex-start;
    padding: 12px 16px;
  }

  .sprite-preview,
  .sprite-preview-custom {
    width: 64px;
    height: 64px;
    flex-shrink: 0;
  }
}
