:root {
  color-scheme: dark;
  --ink: #f7f4ea;
  --muted: #9ca3af;
  --panel: #15161a;
  --line: rgba(255, 255, 255, 0.14);
  --pink: #ff4fb8;
  --cyan: #23d7ff;
  --green: #8cff53;
  --yellow: #ffd84d;
  --brick: #9b4a3f;
  --bg: #0c1012;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  overflow-x: hidden;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 12% 18%, rgba(35, 215, 255, 0.16), transparent 30%),
    radial-gradient(circle at 88% 72%, rgba(255, 79, 184, 0.13), transparent 28%),
    linear-gradient(135deg, #071417 0%, #181112 52%, #0b0d10 100%);
  display: grid;
  place-items: center;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}

.shell {
  width: min(1120px, 100%);
  min-width: 0;
}

.hud,
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.hud {
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--cyan);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 2.7vw, 2.35rem);
  line-height: 1;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.38);
}

.stats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
  min-width: min(100%, 560px);
}

.stats span,
.stats button,
.controls span,
.controls button,
.level-header button {
  border: 1px solid var(--line);
  background: rgba(12, 13, 18, 0.82);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
}

.controls button,
.stats button,
.level-header button {
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}

.controls button:hover,
.stats button:hover,
.level-header button:hover {
  border-color: rgba(255, 255, 255, 0.34);
  background: rgba(35, 215, 255, 0.16);
}

.hud-button {
  min-height: 52px;
}

.stats strong {
  display: block;
  color: var(--ink);
  font-size: 1.04rem;
}

.goal-pill {
  min-width: 148px;
}

.goal-pill strong {
  color: var(--green);
}

.stage-wrap {
  position: relative;
  width: 100%;
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  overflow: hidden;
  background: #111417;
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.46), inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.stage-level-button {
  position: absolute;
  left: 14px;
  top: 14px;
  z-index: 2;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 8px;
  background: rgba(8, 9, 12, 0.78);
  color: var(--ink);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 900;
  padding: 8px 10px;
}

.stage-level-button:hover {
  border-color: rgba(255, 255, 255, 0.44);
  background: rgba(35, 215, 255, 0.18);
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  max-height: min(68svh, calc(100svh - 190px));
  object-fit: contain;
  aspect-ratio: 16 / 9;
  image-rendering: auto;
}

.toast {
  position: absolute;
  left: 50%;
  top: 20px;
  transform: translateX(-50%);
  max-width: min(560px, calc(100% - 32px));
  padding: 10px 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(16, 17, 20, 0.9);
  color: var(--ink);
  font-weight: 800;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.toast.show {
  opacity: 1;
}

.controls {
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.touch-controls {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: 10px;
  touch-action: manipulation;
  user-select: none;
}

.touch-pad {
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 42px);
  gap: 6px;
}

.touch-pad button,
.touch-actions button {
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(12, 13, 18, 0.88);
  color: var(--ink);
  cursor: pointer;
  font-weight: 900;
}

.touch-pad button[data-touch="KeyW"] {
  grid-column: 2;
  grid-row: 1;
}

.touch-pad button[data-touch="KeyA"] {
  grid-column: 1;
  grid-row: 2;
}

.touch-pad button[data-touch="KeyS"] {
  grid-column: 2;
  grid-row: 3;
}

.touch-pad button[data-touch="KeyD"] {
  grid-column: 3;
  grid-row: 2;
}

.touch-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(76px, 1fr));
  gap: 8px;
  min-width: 170px;
}

.touch-actions button {
  min-height: 56px;
  padding: 10px;
}

.touch-pad button:active,
.touch-actions button:active,
.touch-pad button.active,
.touch-actions button.active {
  border-color: rgba(35, 215, 255, 0.55);
  background: rgba(35, 215, 255, 0.2);
}

.level-select {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  place-items: center;
  padding: 20px;
  background: rgba(5, 7, 9, 0.74);
  backdrop-filter: blur(8px);
}

.level-select.open {
  display: grid;
}

.level-panel {
  width: min(980px, 100%);
  max-height: min(760px, calc(100vh - 40px));
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(16, 18, 21, 0.96);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.54);
  padding: 18px;
}

.level-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.level-header h2 {
  margin: 0;
  font-size: clamp(1.2rem, 2vw, 1.8rem);
  line-height: 1;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.legend span {
  border-radius: 999px;
  padding: 5px 9px;
  color: #0d1114;
  font-size: 0.78rem;
  font-weight: 900;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 8px;
  max-height: min(560px, calc(100vh - 220px));
  overflow: auto;
  padding-right: 4px;
}

.level-tile {
  min-height: 58px;
  border: 2px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  cursor: pointer;
  text-align: left;
  padding: 8px;
}

.level-tile:hover,
.level-tile.current {
  border-color: rgba(255, 255, 255, 0.48);
  transform: translateY(-1px);
}

.level-tile.current {
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.22);
}

.level-tile strong {
  display: block;
  font-size: 0.92rem;
}

.level-tile span {
  display: block;
  margin-top: 3px;
  color: rgba(247, 244, 234, 0.84);
  font-size: 0.7rem;
  font-weight: 800;
}

.level-tile em {
  display: block;
  margin-top: 4px;
  color: rgba(247, 244, 234, 0.62);
  font-size: 0.67rem;
  font-style: normal;
}

.tutorial { background: #d9e3ec; }
.easy { background: #8cff53; }
.medium { background: #23d7ff; }
.hard { background: #ffd84d; }
.expert { background: #ff4fb8; }

.level-tile.tutorial {
  border-color: #d9e3ec;
  background: linear-gradient(135deg, rgba(217, 227, 236, 0.38), rgba(217, 227, 236, 0.12));
}

.level-tile.easy {
  border-color: #8cff53;
  background: linear-gradient(135deg, rgba(140, 255, 83, 0.38), rgba(140, 255, 83, 0.1));
}

.level-tile.medium {
  border-color: #23d7ff;
  background: linear-gradient(135deg, rgba(35, 215, 255, 0.4), rgba(35, 215, 255, 0.1));
}

.level-tile.hard {
  border-color: #ffd84d;
  background: linear-gradient(135deg, rgba(255, 216, 77, 0.42), rgba(255, 216, 77, 0.11));
}

.level-tile.expert {
  border-color: #ff4fb8;
  background: linear-gradient(135deg, rgba(255, 79, 184, 0.46), rgba(255, 79, 184, 0.12));
}

.level-tile.tutorial::before,
.level-tile.easy::before,
.level-tile.medium::before,
.level-tile.hard::before,
.level-tile.expert::before {
  content: "";
  display: block;
  height: 5px;
  border-radius: 999px;
  margin-bottom: 6px;
}

.level-tile.tutorial::before { background: #d9e3ec; }
.level-tile.easy::before { background: #8cff53; }
.level-tile.medium::before { background: #23d7ff; }
.level-tile.hard::before { background: #ffd84d; }
.level-tile.expert::before { background: #ff4fb8; }


@media (pointer: coarse) {
  .controls {
    display: none;
  }

  .touch-controls {
    display: flex;
  }
}

@media (max-width: 760px) {
  body {
    align-items: start;
    padding: 8px;
  }

  .hud {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
  }

  h1 {
    font-size: clamp(1.28rem, 8vw, 2rem);
  }

  .stats {
    justify-content: flex-start;
    gap: 6px;
  }

  .stats span,
  .stats button,
  .controls span,
  .controls button {
    font-size: 0.72rem;
    padding: 6px 7px;
  }

  .stats strong {
    font-size: 0.9rem;
  }

  .hud-button {
    min-height: 43px;
  }

  .goal-pill {
    min-width: 116px;
  }

  canvas {
    max-height: 48svh;
  }

  .stage-level-button {
    left: 8px;
    top: 8px;
    font-size: 0.74rem;
    padding: 6px 8px;
  }

  .controls {
    display: none;
  }

  .touch-controls {
    display: flex;
  }

  .level-panel {
    padding: 14px;
  }

  .level-grid {
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  }
}

@media (max-width: 520px) {
  .eyebrow {
    font-size: 0.68rem;
  }

  .stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .stats span,
  .stats button {
    white-space: normal;
    min-width: 0;
  }

  .arena-pill,
  .goal-pill {
    grid-column: span 2;
  }

  .stage-wrap {
    border-width: 1px;
  }

  canvas {
    max-height: 42svh;
  }

  .touch-controls {
    align-items: stretch;
  }

  .touch-pad {
    grid-template-columns: repeat(3, 40px);
    grid-template-rows: repeat(3, 38px);
    gap: 5px;
  }

  .touch-actions {
    min-width: 136px;
  }

  .touch-actions button {
    min-height: 48px;
  }

  .toast {
    top: 8px;
    font-size: 0.8rem;
    padding: 8px 10px;
  }
}

@media (max-height: 620px) and (orientation: landscape) {
  body {
    place-items: start center;
  }

  h1 {
    font-size: 1.35rem;
  }

  .hud {
    margin-bottom: 6px;
  }

  .stats span,
  .stats button {
    padding: 5px 7px;
    font-size: 0.7rem;
  }

  .stats strong {
    font-size: 0.86rem;
  }

  canvas {
    max-height: 62svh;
  }

  .controls {
    display: none;
  }

  .touch-controls {
    display: flex;
    margin-top: 6px;
  }

  .touch-pad {
    grid-template-columns: repeat(3, 38px);
    grid-template-rows: repeat(3, 34px);
  }

  .touch-actions button {
    min-height: 42px;
  }
}
