:root {
  color-scheme: dark;
  --ink: #f8fbff;
  --night: #10162b;
  --panel: rgba(16, 22, 43, 0.92);
  --cyan: #5eead4;
  --gold: #ffd166;
  --outline: #0a1022;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  overflow-x: hidden;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background:
    radial-gradient(circle at 50% 20%, #26365f 0, transparent 42%),
    linear-gradient(155deg, #0a1022, #182546 55%, #10162b);
  color: var(--ink);
  font-family: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }

.game-shell {
  width: min(100%, 1100px);
  display: grid;
  justify-items: center;
  gap: 12px;
}

.stage {
  position: relative;
  width: min(100%, calc((100dvh - 92px) * 16 / 9));
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: clamp(3px, 0.6vw, 7px) solid #27395f;
  border-radius: 14px;
  background: #8be1ef;
  box-shadow: 0 18px 55px rgba(0, 0, 0, 0.45), inset 0 0 0 2px #5b79a9;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  outline: 0;
}

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

.hud {
  position: absolute;
  z-index: 4;
  inset: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr)) auto;
  gap: clamp(3px, 1vw, 12px);
  align-items: start;
  padding: clamp(7px, 1.4vw, 14px);
  pointer-events: none;
  background: linear-gradient(rgba(7, 16, 39, 0.66), transparent);
  text-shadow: 2px 2px 0 var(--outline);
}

.hud-item {
  display: flex;
  flex-direction: column;
  min-width: 0;
  font-size: clamp(8px, 1.35vw, 14px);
  letter-spacing: 0.06em;
}

.hud-item span { color: #d7f5ff; }
.hud-item strong { color: #fff8c7; font-size: 1.08em; white-space: nowrap; }

.icon-button {
  pointer-events: auto;
  min-height: 34px;
  padding: 6px 10px;
  color: var(--ink);
  border: 2px solid rgba(255, 255, 255, 0.7);
  border-radius: 8px;
  background: rgba(14, 25, 52, 0.82);
  cursor: pointer;
  font-size: clamp(8px, 1.1vw, 12px);
}

.overlay {
  position: absolute;
  z-index: 10;
  inset: 0;
  display: none;
  place-items: center;
  padding: clamp(12px, 4vw, 38px);
  background: rgba(7, 13, 30, 0.72);
  backdrop-filter: blur(3px);
}

.overlay.is-visible { display: grid; }

.panel {
  width: min(560px, 94%);
  max-height: 95%;
  overflow: auto;
  padding: clamp(18px, 4vw, 36px);
  border: 3px solid #7be0d1;
  border-radius: 16px;
  background: var(--panel);
  text-align: center;
  box-shadow: 0 12px 0 #07101f, 0 20px 50px rgba(0, 0, 0, 0.35);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--cyan);
  font-size: clamp(9px, 1.5vw, 13px);
  letter-spacing: 0.12em;
}

.panel h2 {
  margin: 0.15em 0;
  color: var(--gold);
  font-size: clamp(26px, 5vw, 50px);
  text-shadow: 3px 4px 0 #8b4668;
}

.panel > p:not(.eyebrow) {
  margin: 10px auto 14px;
  max-width: 42ch;
  color: #e7f8ff;
  font-size: clamp(11px, 1.6vw, 15px);
  line-height: 1.7;
}

.control-hints {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px 14px;
  margin: 12px 0 20px;
  padding: 0;
  list-style: none;
  color: #c9d8ec;
  font-size: clamp(9px, 1.25vw, 12px);
  line-height: 1.8;
}

kbd {
  display: inline-block;
  margin: 0 1px;
  padding: 1px 5px;
  border: 1px solid #7284a3;
  border-bottom-width: 3px;
  border-radius: 4px;
  background: #202e50;
  color: #fff;
  font: inherit;
}

.primary-button {
  min-width: 150px;
  padding: 11px 24px;
  border: 0;
  border-radius: 9px;
  background: linear-gradient(#ffe58e, #ffb845);
  color: #211733;
  box-shadow: 0 5px 0 #a84c4c;
  cursor: pointer;
  font-weight: 900;
}

.primary-button:hover { filter: brightness(1.1); transform: translateY(-1px); }
.primary-button:active { transform: translateY(4px); box-shadow: 0 1px 0 #a84c4c; }

.toast {
  position: absolute;
  z-index: 7;
  top: 20%;
  left: 50%;
  translate: -50% 0;
  padding: 8px 14px;
  border: 2px solid #fff3a8;
  border-radius: 8px;
  background: rgba(24, 31, 62, 0.9);
  color: #fff3a8;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 180ms, transform 180ms;
  pointer-events: none;
  font-weight: 800;
  text-align: center;
}

.toast.is-visible { opacity: 1; transform: translateY(0); }

.touch-controls {
  display: none;
  width: min(100%, 760px);
  justify-content: space-between;
  gap: 20px;
  user-select: none;
  touch-action: none;
}

.touch-group { display: flex; gap: 10px; align-items: end; }

.touch-button {
  width: 62px;
  height: 58px;
  border: 2px solid #6d86b1;
  border-radius: 16px;
  background: rgba(31, 48, 84, 0.92);
  color: #fff;
  box-shadow: 0 5px 0 #0a1022;
  font-weight: 900;
}

.touch-button.is-held { translate: 0 4px; box-shadow: 0 1px 0 #0a1022; background: #3d6288; }
.touch-button.touch-small { width: 52px; height: 48px; }
.touch-button.touch-jump { width: 72px; height: 66px; background: #a85272; border-color: #e99abc; }

.desktop-help { margin: 0; color: #99accb; font-size: 11px; text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (hover: none), (pointer: coarse) {
  body { place-items: start center; }
  .touch-controls { display: flex; }
  .desktop-help { display: none; }
  .stage { width: min(100%, calc((100dvh - 112px) * 16 / 9)); }
}

@media (max-width: 620px) {
  body { padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left)); }
  .stage { border-width: 3px; border-radius: 9px; }
  .hud { grid-template-columns: repeat(5, 1fr); }
  .icon-button { position: absolute; right: 7px; top: 42px; min-height: 27px; padding: 3px 6px; }
  .control-hints { grid-template-columns: 1fr; }
  .touch-button { width: 54px; height: 52px; }
  .touch-button.touch-jump { width: 62px; height: 60px; }
}

@media (max-height: 500px) and (orientation: landscape) and (hover: none) {
  body { display: block; padding: 0; overflow: hidden; }
  .game-shell, .stage { width: 100vw; height: 100dvh; max-width: none; border: 0; border-radius: 0; }
  .touch-controls { position: fixed; z-index: 20; inset: auto 10px max(8px, env(safe-area-inset-bottom)); width: auto; pointer-events: none; }
  .touch-button { pointer-events: auto; opacity: 0.72; }
}

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