:root {
  --bg: #031330;
  --panel: #061d45;
  --blue: #1b82ff;
  --cyan: #63d8ff;
  --white: #f6fbff;
  --gold: #ffcb35;
  --green: #54df69;
  --red: #ff4a5c;
  --magenta: #b0175b;
  --app-height: 100dvh;
  --mobile-controls-height: 86px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background:
    radial-gradient(circle at 18% 12%, rgba(27, 130, 255, 0.2), transparent 28rem),
    radial-gradient(circle at 82% 18%, rgba(99, 216, 255, 0.12), transparent 24rem),
    linear-gradient(180deg, #020917 0%, var(--bg) 60%, #020712 100%);
  color: var(--white);
  font-family: "Courier New", Courier, monospace;
}

body {
  display: grid;
  place-items: center;
  min-height: 100svh;
  padding: 24px;
  touch-action: manipulation;
}

body.is-playing {
  overflow: hidden;
  padding: 0;
  background: #020917;
}

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

body.is-playing .game-shell {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  width: 100vw;
  height: var(--app-height);
  min-height: 0;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  padding:
    max(6px, env(safe-area-inset-top))
    max(8px, env(safe-area-inset-right))
    max(6px, env(safe-area-inset-bottom))
    max(8px, env(safe-area-inset-left));
  background: #020917;
}

.screen-frame {
  position: relative;
  overflow: hidden;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 3px solid rgba(99, 216, 255, 0.9);
  background: #020917;
  box-shadow:
    0 0 0 4px rgba(3, 19, 48, 0.9),
    0 0 36px rgba(27, 130, 255, 0.45);
  image-rendering: pixelated;
  touch-action: none;
}

body.is-playing .screen-frame {
  width: min(100%, calc((var(--app-height) - 20px) * 1.7778));
  max-height: calc(var(--app-height) - 20px);
  height: auto;
  aspect-ratio: 16 / 9;
  flex: 0 1 auto;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  image-rendering: pixelated;
  touch-action: none;
}

.menu-overlay,
.dialog-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  padding: clamp(14px, 4%, 42px);
  text-align: center;
  background:
    linear-gradient(rgba(3, 19, 48, 0.26) 1px, transparent 1px),
    linear-gradient(90deg, rgba(27, 130, 255, 0.22) 1px, transparent 1px),
    radial-gradient(circle at center, rgba(27, 130, 255, 0.18), rgba(2, 9, 23, 0.92) 68%);
  background-size: 42px 42px, 42px 42px, auto;
}

.menu-overlay::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, transparent, rgba(2, 7, 18, 0.65));
}

.menu-overlay > * {
  position: relative;
}

.menu-logo-wrap {
  position: absolute;
  top: 18px;
  left: 22px;
  width: clamp(128px, 22vw, 240px);
  padding: 8px;
  background: rgba(1, 9, 24, 0.45);
}

.menu-logo {
  display: block;
  width: 100%;
  filter: brightness(0) invert(1);
}

.kicker,
.subtitle,
.menu-note,
.help-line {
  margin: 0;
  color: #d6e8ff;
  text-shadow: 2px 2px 0 #00122c;
}

.kicker {
  margin-top: 42px;
  font-size: clamp(14px, 2vw, 22px);
}

h1 {
  margin: 10px 0 4px;
  color: var(--white);
  font-size: clamp(42px, 9vw, 104px);
  line-height: 0.86;
  letter-spacing: 0;
  text-shadow:
    5px 0 0 #0d4ea8,
    0 5px 0 #0d4ea8,
    7px 7px 0 #00122c,
    0 0 18px rgba(99, 216, 255, 0.8);
}

.subtitle {
  color: var(--gold);
  font-size: clamp(14px, 2.5vw, 26px);
  font-weight: 700;
}

.pixel-menu {
  display: grid;
  gap: 10px;
  width: min(320px, 78vw);
  margin: 28px auto 14px;
}

.pixel-button {
  position: relative;
  min-height: 44px;
  border: 3px solid #d9f4ff;
  border-radius: 0;
  background: linear-gradient(180deg, #164f98, #082a5f);
  color: var(--white);
  font: 700 18px/1 "Courier New", Courier, monospace;
  letter-spacing: 0;
  text-transform: uppercase;
  box-shadow:
    inset -4px -4px 0 rgba(0, 0, 0, 0.35),
    inset 4px 4px 0 rgba(255, 255, 255, 0.15),
    4px 4px 0 #000a1b;
  cursor: pointer;
}

.pixel-button:hover,
.pixel-button:focus-visible,
.pixel-button.is-selected {
  background: linear-gradient(180deg, #ffdf68, #d38a13);
  color: #241200;
  outline: none;
}

.pixel-button.is-selected::before {
  content: "▶";
  position: absolute;
  left: -30px;
  top: 50%;
  color: var(--gold);
  transform: translateY(-50%);
  animation: blink 0.8s steps(2, end) infinite;
}

.menu-note {
  font-size: clamp(13px, 1.8vw, 18px);
}

.dialog-overlay {
  background: rgba(2, 9, 23, 0.76);
}

.dialog {
  width: min(760px, 92%);
  padding: clamp(22px, 5vw, 42px);
  border: 3px solid var(--cyan);
  background: rgba(3, 19, 48, 0.95);
  box-shadow: 0 0 30px rgba(99, 216, 255, 0.35);
}

.dialog p {
  margin: 0 0 24px;
  color: var(--white);
  font-size: clamp(18px, 3vw, 30px);
  line-height: 1.45;
  text-shadow: 2px 2px 0 #00122c;
}

.hidden {
  display: none;
}

.touch-controls {
  width: 100%;
  display: none;
  align-items: center;
  justify-content: center;
  gap: 10px;
  pointer-events: none;
  z-index: 4;
  user-select: none;
  -webkit-user-select: none;
}

.end-actions {
  display: none;
  z-index: 3;
  width: min(320px, 70%);
  margin: 0 auto;
}

.end-actions:not(.hidden) {
  display: block;
}

.end-actions .pixel-button {
  width: 100%;
}

.end-message {
  position: absolute;
  left: 4%;
  right: 4%;
  bottom: 14px;
  z-index: 4;
  overflow: hidden;
  border: 2px solid rgba(99, 216, 255, 0.78);
  background: rgba(2, 9, 23, 0.86);
  color: var(--white);
  padding: 9px 12px;
  white-space: nowrap;
  text-shadow: 2px 2px 0 #00122c;
}

.end-message-track {
  display: inline-block;
  min-width: 100%;
  font: 700 clamp(12px, 2.2vw, 18px) / 1.2 "Courier New", Courier, monospace;
  will-change: transform;
}

.touch-controls button {
  width: 74px;
  height: 62px;
  border: 2px solid rgba(246, 251, 255, 0.78);
  background: rgba(6, 29, 69, 0.92);
  color: var(--white);
  font: 700 18px/1 "Courier New", Courier, monospace;
  pointer-events: auto;
  touch-action: none;
  box-shadow:
    inset -4px -4px 0 rgba(0, 0, 0, 0.32),
    inset 4px 4px 0 rgba(255, 255, 255, 0.12);
}

.touch-controls button[data-touch="jump"] {
  width: 116px;
  margin-left: auto;
}

.touch-controls button[data-touch="right"] {
  margin-right: auto;
}

.touch-controls .sound-touch {
  width: 96px;
  border-color: rgba(255, 203, 53, 0.82);
  color: var(--gold);
  font-size: 13px;
}

.touch-controls .sound-touch.is-on {
  background: linear-gradient(180deg, #ffdf68, #d38a13);
  color: #241200;
}

.help-line {
  margin-top: 14px;
  text-align: center;
  font-size: 14px;
}

body.is-playing .help-line {
  display: none;
}

body.is-playing.is-finished .touch-controls {
  display: none;
}

.rotate-hint {
  display: none;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

@media (hover: none), (pointer: coarse) {
  body {
    padding: 10px;
  }

  body.is-playing .touch-controls {
    display: flex;
    width: min(100%, 560px);
    height: var(--mobile-controls-height);
    min-height: var(--mobile-controls-height);
    flex: 0 0 var(--mobile-controls-height);
    padding-inline: 8px;
  }

  body.is-playing .screen-frame {
    width: min(100%, calc((var(--app-height) - var(--mobile-controls-height) - 18px) * 1.7778));
    max-height: calc(var(--app-height) - var(--mobile-controls-height) - 18px);
  }

  .help-line {
    font-size: 12px;
  }
}

@media (max-width: 860px) {
  body {
    align-content: start;
    padding: 10px;
  }

  .game-shell {
    width: 100%;
  }

  body:not(.is-playing) .screen-frame {
    max-height: calc(100svh - 78px);
  }

  body.is-playing .touch-controls {
    display: flex;
    width: min(100%, 560px);
    height: var(--mobile-controls-height);
    min-height: var(--mobile-controls-height);
    flex: 0 0 var(--mobile-controls-height);
    padding-inline: 8px;
  }

  body.is-playing .screen-frame {
    width: min(100%, calc((var(--app-height) - var(--mobile-controls-height) - 18px) * 1.7778));
    max-height: calc(var(--app-height) - var(--mobile-controls-height) - 18px);
  }

  .touch-controls button {
    width: 76px;
    height: 62px;
  }

  .touch-controls button[data-touch="jump"] {
    width: 118px;
  }

  .help-line {
    display: none;
  }
}

@media (max-width: 640px) {
  .menu-logo-wrap {
    width: 126px;
    top: 8px;
    left: 8px;
    padding: 5px;
  }

  .kicker {
    margin-top: 24px;
    font-size: 11px;
  }

  h1 {
    margin-top: 3px;
    margin-bottom: 2px;
    font-size: clamp(32px, 9vw, 46px);
    line-height: 0.78;
    text-shadow:
      3px 0 0 #0d4ea8,
      0 3px 0 #0d4ea8,
      5px 5px 0 #00122c,
      0 0 12px rgba(99, 216, 255, 0.8);
  }

  .subtitle {
    font-size: 12px;
  }

  .pixel-menu {
    gap: 5px;
    margin-top: 9px;
    margin-bottom: 0;
  }

  .pixel-button {
    min-height: 26px;
    font-size: 12px;
    border-width: 2px;
    box-shadow:
      inset -3px -3px 0 rgba(0, 0, 0, 0.35),
      inset 3px 3px 0 rgba(255, 255, 255, 0.15),
      3px 3px 0 #000a1b;
  }

  .pixel-button.is-selected::before {
    left: -22px;
  }

  .touch-controls button {
    width: 64px;
    height: 54px;
    font-size: 15px;
  }

  .touch-controls button[data-touch="jump"] {
    width: 98px;
  }

  .touch-controls .sound-touch {
    width: 82px;
    font-size: 11px;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --mobile-controls-height: 56px;
  }

  body.is-playing .game-shell {
    gap: 4px;
    justify-content: flex-start;
  }

  body:not(.is-playing) {
    padding: 8px;
  }

  body:not(.is-playing) .screen-frame {
    max-height: calc(var(--app-height) - 16px);
    width: min(100%, calc((var(--app-height) - 16px) * 1.7778));
    margin: 0 auto;
  }

  .menu-overlay {
    padding: 10px;
  }

  .menu-logo-wrap {
    top: 12px;
    left: 16px;
    width: min(150px, 19vw);
    padding: 6px;
  }

  .kicker {
    margin-top: 8px;
    font-size: clamp(13px, 4vh, 18px);
  }

  h1 {
    margin: 5px 0 2px;
    font-size: clamp(42px, 16vh, 74px);
    line-height: 0.78;
    text-shadow:
      4px 0 0 #0d4ea8,
      0 4px 0 #0d4ea8,
      6px 6px 0 #00122c,
      0 0 16px rgba(99, 216, 255, 0.8);
  }

  .subtitle {
    font-size: clamp(13px, 4vh, 19px);
  }

  .pixel-menu {
    gap: 8px;
    margin-top: 12px;
    margin-bottom: 0;
  }

  .pixel-button {
    min-height: 36px;
    font-size: 15px;
  }

  body.is-playing .screen-frame {
    width: min(100%, calc((var(--app-height) - var(--mobile-controls-height) - 12px) * 1.7778));
    max-height: calc(var(--app-height) - var(--mobile-controls-height) - 12px);
    border-width: 2px;
  }

  body.is-playing .touch-controls {
    width: min(100%, 540px);
    height: var(--mobile-controls-height);
    min-height: var(--mobile-controls-height);
  }

  .touch-controls button {
    width: 60px;
    height: 46px;
    font-size: 15px;
  }

  .touch-controls button[data-touch="jump"] {
    width: 96px;
  }

  .touch-controls .sound-touch {
    width: 82px;
    font-size: 11px;
  }

  .dialog {
    width: min(760px, 88%);
    padding: 18px 24px;
  }

  .dialog p {
    margin-bottom: 14px;
    font-size: clamp(16px, 3.8vh, 24px);
    line-height: 1.28;
  }

  .dialog .pixel-button {
    min-height: 36px;
    font-size: 14px;
  }

  .end-message {
    bottom: 8px;
    padding: 7px 10px;
  }

  .end-actions {
    width: min(300px, 58vw);
  }

  .end-actions .pixel-button {
    min-height: 40px;
    font-size: 14px;
  }
}

@media (orientation: portrait) and (max-width: 700px) {
  :root {
    --mobile-controls-height: 96px;
  }

  body.is-playing .screen-frame {
    width: 100%;
    height: auto;
    max-height: calc(var(--app-height) - var(--mobile-controls-height) - 18px);
    aspect-ratio: 16 / 9;
  }

  body.is-playing .touch-controls {
    height: var(--mobile-controls-height);
    min-height: var(--mobile-controls-height);
  }

  .rotate-hint {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: grid;
    place-items: center;
    padding: 28px;
    background: rgba(2, 9, 23, 0.94);
    text-align: center;
  }

  .rotate-hint img {
    display: block;
    width: min(240px, 62vw);
    margin: 0 auto 18px;
    filter: brightness(0) invert(1);
  }

  .rotate-hint span {
    display: block;
    max-width: 320px;
    border: 3px solid var(--cyan);
    background: rgba(3, 19, 48, 0.96);
    color: var(--white);
    padding: 24px 20px;
    font: 700 24px/1.25 "Courier New", Courier, monospace;
    text-shadow: 2px 2px 0 #00122c;
    box-shadow: 0 0 28px rgba(99, 216, 255, 0.35);
  }
}

body.is-playing.is-finished .touch-controls {
  display: none !important;
}
