:root {
  color-scheme: dark;
  --bg: #080b18;
  --panel: #111934;
  --panel-light: #1c2b50;
  --ink: #f5f7ff;
  --muted: #9eb0d3;
  --yellow: #ffd447;
  --cyan: #42e8e0;
  --pink: #ff5da2;
  --green: #69f58b;
  --red: #ff5964;
  --line: #36528a;
  --shadow: #03050c;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 0;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  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));
  overflow-x: hidden;
  color: var(--ink);
  background:
    linear-gradient(rgba(8, 11, 24, .82), rgba(8, 11, 24, .96)),
    repeating-linear-gradient(90deg, transparent 0 31px, rgba(66, 232, 224, .07) 31px 32px),
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(66, 232, 224, .07) 31px 32px),
    radial-gradient(circle at 50% 0, #263c70, var(--bg) 62%);
  font-family: "Courier New", Courier, monospace;
  overscroll-behavior: none;
}

button,
a {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

button {
  color: inherit;
}

.game-shell {
  width: min(100%, 760px);
  margin: 0 auto;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  margin-bottom: 9px;
}

.topbar h1 {
  margin: 0;
  color: var(--yellow);
  font-size: clamp(1.4rem, 5vw, 2.25rem);
  line-height: 1;
  letter-spacing: -.08em;
  text-align: center;
  text-shadow: 3px 3px 0 #000;
  white-space: nowrap;
}

.topbar h1 span {
  color: var(--cyan);
}

.back-link {
  width: fit-content;
  color: var(--muted);
  font-size: clamp(.65rem, 2vw, .78rem);
  font-weight: 700;
  text-decoration: none;
  text-transform: uppercase;
}

.back-link:hover,
.back-link:focus-visible {
  color: var(--yellow);
}

.top-actions {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
}

.icon-button {
  width: 40px;
  min-height: 40px;
  padding: 0;
  border: 3px solid #050711;
  border-radius: 4px;
  background: var(--panel-light);
  box-shadow: 3px 3px 0 #000;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 900;
}

.icon-button[aria-pressed="true"] {
  color: #11182a;
  background: var(--red);
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 8px;
}

.stat {
  min-width: 0;
  padding: 7px 9px;
  border: 2px solid var(--line);
  background: rgba(17, 25, 52, .94);
  box-shadow: 3px 3px 0 var(--shadow);
}

.stat span,
.status-label {
  display: block;
  color: var(--muted);
  font-size: clamp(.55rem, 1.8vw, .68rem);
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.stat strong {
  display: block;
  overflow: hidden;
  color: var(--yellow);
  font-size: clamp(.9rem, 3vw, 1.2rem);
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.play-column {
  width: min(100%, 570px, calc((100vh - 250px) * .8));
  margin: 0 auto;
}

@supports (height: 100dvh) {
  .play-column {
    width: min(100%, 570px, calc((100dvh - 250px) * .8));
  }
}

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 5;
  overflow: hidden;
  border: 4px solid #050711;
  background: #070b18;
  box-shadow:
    0 0 0 3px var(--line),
    7px 7px 0 #02030a;
  isolation: isolate;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  user-select: none;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(4, 7, 17, .78);
}

.overlay.visible {
  display: grid;
}

.overlay-card {
  width: min(100%, 410px);
  padding: 24px 20px;
  border: 4px solid #050711;
  background: rgba(17, 25, 52, .97);
  box-shadow:
    0 0 0 3px var(--cyan),
    8px 8px 0 #000;
  text-align: center;
}

.overlay-kicker {
  margin: 0 0 7px;
  color: var(--cyan);
  font-size: .68rem;
  font-weight: 900;
  letter-spacing: .15em;
  text-transform: uppercase;
}

.overlay-card h2 {
  margin: 0;
  color: var(--yellow);
  font-size: clamp(1.55rem, 7vw, 2.55rem);
  line-height: 1;
  text-shadow: 3px 3px 0 #000;
}

.overlay-card p:not(.overlay-kicker) {
  margin: 14px auto 19px;
  color: #d0daf0;
  font-size: clamp(.76rem, 2.4vw, .92rem);
  line-height: 1.55;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.primary-button,
.secondary-button,
.launch-button {
  min-height: 44px;
  padding: 10px 16px;
  border: 3px solid #050711;
  border-radius: 3px;
  box-shadow: 4px 4px 0 #000;
  cursor: pointer;
  font-size: .76rem;
  font-weight: 900;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.primary-button,
.launch-button {
  color: #07101c;
  background: var(--yellow);
}

.secondary-button {
  background: var(--panel-light);
}

.hidden {
  display: none !important;
}

.toast {
  position: absolute;
  z-index: 3;
  top: 14%;
  left: 50%;
  min-width: 170px;
  padding: 9px 13px;
  border: 2px solid #000;
  color: #07101c;
  background: var(--cyan);
  box-shadow: 4px 4px 0 #000;
  font-size: .8rem;
  font-weight: 900;
  text-align: center;
  text-transform: uppercase;
  opacity: 0;
  transform: translate(-50%, -10px);
  pointer-events: none;
}

.toast.visible {
  animation: toast-in 1.35s steps(4, end);
}

.status-row {
  display: flex;
  min-height: 57px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
  padding: 8px 10px;
  border: 2px solid var(--line);
  background: rgba(17, 25, 52, .9);
}

.effects {
  margin-top: 3px;
  color: var(--green);
  font-size: clamp(.65rem, 2vw, .78rem);
  font-weight: 700;
}

.launch-button {
  flex: 0 0 auto;
}

.controls-hint {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: .66rem;
  line-height: 1.6;
  text-align: center;
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 4px;
  border: 1px solid #53699a;
  border-bottom-width: 2px;
  background: var(--panel-light);
  color: var(--ink);
  font-family: inherit;
  text-align: center;
}

button:hover {
  filter: brightness(1.13);
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--pink);
  outline-offset: 3px;
}

button:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 #000;
}

@keyframes toast-in {
  0% { opacity: 0; transform: translate(-50%, -10px); }
  12%, 72% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, 8px); }
}

@media (max-width: 560px) {
  .topbar {
    grid-template-columns: 1fr auto;
  }

  .topbar h1 {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .back-link {
    grid-column: 1;
    grid-row: 2;
  }

  .top-actions {
    grid-column: 2;
    grid-row: 2;
  }

  .stat {
    padding: 5px 6px;
  }

  .play-column {
    width: min(100%, 570px);
  }

  .status-row {
    min-height: 52px;
    padding: 6px 8px;
  }

  .launch-button {
    min-height: 42px;
    padding: 8px 10px;
    font-size: .65rem;
  }

  .controls-hint {
    display: none;
  }
}

@media (max-width: 920px) and (max-height: 500px) and (orientation: landscape) {
  body {
    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));
  }

  .game-shell {
    display: grid;
    width: min(100%, 860px);
    grid-template-columns: minmax(265px, 1fr) minmax(310px, 1.08fr);
    grid-template-rows: auto 1fr;
    gap: 7px 14px;
  }

  .topbar {
    grid-column: 2;
    grid-row: 1;
    margin: 0;
  }

  .topbar h1 {
    grid-column: 1;
    grid-row: 1;
    font-size: 1.55rem;
    text-align: left;
  }

  .back-link {
    display: none;
  }

  .top-actions {
    grid-column: 2;
    grid-row: 1;
  }

  .icon-button {
    width: 36px;
    min-height: 36px;
  }

  .hud {
    grid-column: 2;
    grid-row: 2;
    align-self: start;
    margin: 0;
  }

  .play-column {
    display: grid;
    width: auto;
    grid-column: 1;
    grid-row: 1 / 3;
    grid-template-columns: auto;
    align-content: center;
  }

  .stage {
    width: min(100%, calc((100dvh - 12px) * .8));
    max-height: calc(100dvh - 12px);
    justify-self: center;
  }

  .status-row {
    position: fixed;
    right: max(8px, env(safe-area-inset-right));
    bottom: max(7px, env(safe-area-inset-bottom));
    left: calc(50% + 18px);
    min-height: 49px;
    margin: 0;
  }

  .controls-hint {
    position: fixed;
    right: max(8px, env(safe-area-inset-right));
    bottom: 64px;
    left: calc(50% + 18px);
    margin: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
