:root {
  --bg: #1c1c1c;
  --panel: #2a2a2a;
  --text: #f2f2f2;
  --muted: #a8a8a8;
  --accent: #6bbf4e;
  --accent-dark: #3d7a2e;
  --btn: #3a3a3a;
  --btn-border: #555;
  --danger: #c44;
  --pad-bg: #1f1f1f;
  --pad-btn: #3d5a3a;
  --pad-btn-blocked: #555;
  --pad-btn-blocked-text: #9a9a9a;
  --font-display: "Courier New", Courier, monospace;
  --font-ui: "Segoe UI", Tahoma, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  overscroll-behavior: none;
}

#app {
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  position: relative;
}

.screen {
  display: none;
  width: 100%;
  height: 100%;
}

.screen.active {
  display: flex;
}

#start-screen {
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 50% 30%, #2f3d28 0%, transparent 55%),
    linear-gradient(180deg, #222 0%, #151515 100%);
}

.start-panel {
  text-align: center;
  padding: 2rem;
}

.brand {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(3rem, 14vw, 5.5rem);
  letter-spacing: 0.08em;
  color: var(--accent);
  text-shadow: 3px 3px 0 #1a3a12, 0 0 24px rgba(107, 191, 78, 0.25);
}

.tagline {
  margin: 0.75rem 0 2rem;
  color: var(--muted);
  font-size: 1rem;
}

.btn {
  appearance: none;
  border: 2px solid var(--btn-border);
  background: var(--btn);
  color: var(--text);
  font-family: var(--font-display);
  font-size: 1.1rem;
  padding: 0.7rem 1.4rem;
  cursor: pointer;
  min-width: 9rem;
}

.btn.primary {
  background: var(--accent-dark);
  border-color: var(--accent);
  color: #fff;
}

.btn:active {
  transform: translateY(1px);
}

#game-screen {
  flex-direction: column;
  position: relative;
  background: #2f2f2f;
  width: 100%;
  height: 100%;
  min-height: 0;
}

#play-area {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  width: 100%;
  padding: 0.5rem;
  overflow: hidden;
}

#game {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #6e6e6e;
}

#mobile-controls {
  display: none;
  width: 100%;
  flex: 0 0 auto;
  padding: 0;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  border-top: 3px solid #111;
  background: var(--pad-bg);
}

/* Bottom pad: UP 33% full width, LEFT/RIGHT 50% x 33%, DOWN 33% full width */
.dpad {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-template-areas:
    "up up"
    "left right"
    "down down";
  gap: 0;
  width: 100%;
  height: 100%;
  min-height: 28dvh;
  margin: 0;
}

#btn-up { grid-area: up; }
#btn-left { grid-area: left; }
#btn-down { grid-area: down; }
#btn-right { grid-area: right; }

.ctrl-btn {
  margin: 0;
  border: none;
  border-radius: 0;
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 8vw, 2.4rem);
  line-height: 1;
  color: #fff;
  cursor: pointer;
  background: var(--pad-btn);
  touch-action: manipulation;
  min-height: 0;
  box-shadow: none;
}

#btn-up {
  border-bottom: 2px solid #111;
}

#btn-left {
  border-right: 1px solid #111;
  border-bottom: 2px solid #111;
}

#btn-right {
  border-left: 1px solid #111;
  border-bottom: 2px solid #111;
}

.ctrl-btn:active:not(:disabled) {
  filter: brightness(1.12);
}

.ctrl-btn:disabled,
.ctrl-btn.is-blocked {
  background: var(--pad-btn-blocked);
  color: var(--pad-btn-blocked-text);
  cursor: not-allowed;
  filter: none;
}

.popup {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.55);
  z-index: 5;
  padding: 1rem;
}

.popup.hidden {
  display: none;
}

.popup-card {
  background: var(--panel);
  border: 3px solid #111;
  padding: 1.25rem 1.5rem;
  text-align: center;
  min-width: min(90vw, 280px);
  box-shadow: 6px 6px 0 #111;
}

.popup-icon {
  width: 32px;
  height: 32px;
  margin: 0 auto 0.5rem;
  image-rendering: pixelated;
  background-repeat: no-repeat;
}

.popup-card h2 {
  margin: 0 0 0.75rem;
  font-family: var(--font-display);
  color: var(--danger);
  letter-spacing: 0.06em;
}

.popup-stats {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0 0 1.25rem;
  color: var(--muted);
  font-family: var(--font-display);
}

.popup-stats strong {
  color: var(--text);
}

.popup-actions {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: stretch;
}

/* Phone / touch: stage ~75%, pad ~25% */
@media (max-width: 900px), (pointer: coarse) {
  #game-screen.has-mobile-controls {
    display: grid;
    grid-template-rows: minmax(0, 75%) minmax(0, 25%);
    grid-template-columns: minmax(0, 1fr);
  }

  #game-screen.has-mobile-controls #play-area {
    flex: unset;
    width: 100%;
    height: 100%;
    max-height: none;
    min-height: 0;
  }

  #game-screen.has-mobile-controls #mobile-controls {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 0;
  }
}
