:root {
  color-scheme: dark;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --ui-edge: 18px;
  --stick-size: clamp(132px, 36vw, 164px);
  --stick-thumb-size: clamp(56px, 15vw, 70px);
  --action-size: clamp(68px, 18vw, 84px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: #111827;
  color: #f8fafc;
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
}

#game-root {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #111827;
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
}

#hud {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  left: calc(var(--safe-left) + 12px);
  right: calc(var(--safe-right) + 12px);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.hud-item {
  flex: 1;
  min-width: 0;
  padding: 8px 6px;
  border: 1px solid rgb(255 255 255 / 0.14);
  border-radius: 8px;
  background: rgb(15 23 42 / 0.58);
  color: #dbeafe;
  font-size: clamp(0.72rem, 3.2vw, 0.92rem);
  font-weight: 700;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
  backdrop-filter: blur(8px);
}

.hud-item strong {
  color: #ffffff;
}

#touch-controls {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
  touch-action: none;
}

.touch-button {
  display: grid;
  place-items: center;
  border: 1px solid rgb(255 255 255 / 0.24);
  border-radius: 8px;
  background: rgb(15 23 42 / 0.55);
  color: #ffffff;
  box-shadow: 0 10px 24px rgb(0 0 0 / 0.2);
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  backdrop-filter: blur(8px);
}

.touch-button:active,
.touch-button.is-pressed {
  background: rgb(96 165 250 / 0.78);
  border-color: rgb(191 219 254 / 0.76);
  transform: translateY(1px);
}

.analog-stick {
  position: absolute;
  left: calc(var(--safe-left) + var(--ui-edge));
  bottom: calc(var(--safe-bottom) + var(--ui-edge));
  width: var(--stick-size);
  height: var(--stick-size);
  border-radius: 999px;
  border: 1px solid rgb(255 255 255 / 0.2);
  background:
    radial-gradient(circle at center, rgb(255 255 255 / 0.12) 0 2px, transparent 3px),
    rgb(15 23 42 / 0.42);
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.18), 0 10px 24px rgb(0 0 0 / 0.22);
  pointer-events: auto;
  touch-action: none;
  backdrop-filter: blur(8px);
  --stick-x: 0px;
  --stick-y: 0px;
}

.analog-stick::before {
  content: "";
  position: absolute;
  inset: 22%;
  border: 1px solid rgb(255 255 255 / 0.16);
  border-radius: inherit;
}

.analog-stick.is-active {
  background:
    radial-gradient(circle at center, rgb(255 255 255 / 0.18) 0 2px, transparent 3px),
    rgb(30 64 175 / 0.48);
}

.analog-stick-thumb {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--stick-thumb-size);
  height: var(--stick-thumb-size);
  border: 1px solid rgb(255 255 255 / 0.28);
  border-radius: 999px;
  background: rgb(226 232 240 / 0.52);
  box-shadow: 0 8px 18px rgb(0 0 0 / 0.24);
  pointer-events: none;
  transform: translate(calc(-50% + var(--stick-x)), calc(-50% + var(--stick-y)));
}

.action-buttons {
  position: absolute;
  right: calc(var(--safe-right) + var(--ui-edge));
  bottom: calc(var(--safe-bottom) + var(--ui-edge) + 8px);
  display: flex;
  align-items: end;
  justify-content: space-between;
  width: clamp(148px, 39vw, 180px);
  gap: 12px;
  pointer-events: none;
}

.action-button {
  flex: 0 0 var(--action-size);
  width: var(--action-size);
  height: var(--action-size);
  border-radius: 999px;
  font-size: clamp(0.86rem, 4vw, 1.1rem);
  font-weight: 900;
  letter-spacing: 0;
}

.jump-button {
  background: rgb(2 132 199 / 0.64);
  border-color: rgb(186 230 253 / 0.36);
}

.jump-button:active,
.jump-button.is-pressed {
  background: rgb(14 165 233 / 0.84);
}

.attack-button {
  background: rgb(185 28 28 / 0.64);
  border-color: rgb(254 202 202 / 0.36);
}

.attack-button:active,
.attack-button.is-pressed {
  background: rgb(239 68 68 / 0.84);
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: start center;
  padding:
    calc(var(--safe-top) + 18px)
    calc(var(--safe-right) + 18px)
    calc(var(--safe-bottom) + 18px)
    calc(var(--safe-left) + 18px);
  background: rgb(2 6 23 / 0.64);
}

.overlay.hidden {
  display: none;
}

.overlay-panel {
  width: min(320px, 100%);
  margin-top: calc(var(--safe-top) + 64px);
  padding: 22px;
  border: 1px solid rgb(255 255 255 / 0.16);
  border-radius: 8px;
  background: rgb(15 23 42 / 0.86);
  text-align: center;
  backdrop-filter: blur(10px);
}

.overlay-panel h1 {
  margin: 0 0 8px;
  font-size: 2rem;
  line-height: 1.05;
}

.overlay-panel p {
  margin: 0 0 8px;
  color: #cbd5e1;
  font-weight: 700;
}

.high-scores {
  display: grid;
  gap: 4px;
  margin: 14px 0 18px;
  padding-left: 26px;
  color: #e2e8f0;
  text-align: left;
  font-weight: 700;
}

.high-scores:empty {
  display: none;
}

.high-scores li {
  padding-left: 4px;
}

#restart-button {
  width: 100%;
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  background: #38bdf8;
  color: #082f49;
  font-weight: 900;
}

@media (hover: hover) and (pointer: fine) {
  #touch-controls {
    opacity: 0.72;
  }
}
