:root {
  color-scheme: dark;
  --fg: #f7fbff;
  --panel: rgba(0, 0, 0, 0.88);
  --muted: #8e8e8e;
  --green: #58f878;
  --red: #ff3848;
  --cyan: #80fff8;
  --yellow: #ffe76b;
  --pink: #ff72d2;
  --app-w: 100vw;
  --app-h: 100vh;
  --game-w: min(100vw, 133.333vh);
  --game-h: min(100vh, 75vw);
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  width: var(--app-w);
  height: var(--app-h);
  overflow: hidden;
  background: #000;
  color: var(--fg);
  font-family: "Courier New", monospace;
  overscroll-behavior: none;
  touch-action: none;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

input {
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

#shell {
  position: fixed;
  left: 0;
  top: 0;
  width: var(--app-w);
  height: var(--app-h);
  display: grid;
  place-items: center;
  background: #000;
}

#game {
  width: var(--game-w);
  height: var(--game-h);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  touch-action: none;
}

.panel {
  position: fixed;
  left: 50%;
  top: 50%;
  width: min(340px, calc(var(--app-w) - 32px));
  transform: translate(-50%, -50%);
  padding: 18px;
  border: 2px solid var(--fg);
  background: var(--panel);
  text-align: center;
  z-index: 5;
}

.panel[hidden] {
  display: none;
}

.panel h1 {
  margin: 0 0 14px;
  color: var(--fg);
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 0;
}

#joinForm {
  display: grid;
  gap: 10px;
}

.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

#nickname {
  min-width: 0;
  padding: 10px 8px;
  border: 1px solid var(--fg);
  border-radius: 0;
  background: #000;
  color: var(--fg);
  text-transform: uppercase;
}

#join button,
#startButton {
  padding: 10px 12px;
  border: 1px solid var(--fg);
  border-radius: 0;
  background: #000;
  color: var(--fg);
}

#join button:active,
#join button[aria-checked="true"],
#startButton:active {
  background: var(--fg);
  color: #000;
}

.rooms {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

#join .room-button {
  min-width: 0;
  padding: 7px 8px;
  color: var(--fg);
  font-size: 12px;
  overflow-wrap: anywhere;
}

#status {
  min-height: 18px;
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.best-list {
  display: grid;
  gap: 3px;
  margin-top: 12px;
  color: var(--fg);
  font-size: 12px;
  text-align: left;
}

.best-row {
  display: grid;
  grid-template-columns: 22px 1fr auto;
  gap: 8px;
  min-width: 0;
}

.best-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#hud {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: max(8px, env(safe-area-inset-left));
  display: flex;
  gap: 12px;
  padding: 5px 7px;
  background: rgba(0, 0, 0, 0.72);
  color: var(--fg);
  font-size: 14px;
  line-height: 1;
  pointer-events: auto;
  cursor: pointer;
  touch-action: manipulation;
  z-index: 3;
}

#hud:focus-visible,
#startButton:focus-visible,
.move-stick:focus-visible {
  outline: 1px solid var(--fg);
  outline-offset: 2px;
}

#score::before {
  content: "S ";
}

#players::before {
  content: "P ";
}

#slot::before {
  content: "R ";
}

#latency::before {
  content: "L ";
}

#debugStats {
  position: fixed;
  top: calc(max(8px, env(safe-area-inset-top)) + 26px);
  left: max(8px, env(safe-area-inset-left));
  max-width: min(540px, calc(var(--app-w) - 16px));
  padding: 5px 7px;
  background: rgba(0, 0, 0, 0.72);
  color: var(--fg);
  font-size: 11px;
  line-height: 1.25;
  pointer-events: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  text-shadow: 1px 1px 0 #000;
  z-index: 4;
}

#debugStats[data-expanded="false"] {
  display: none;
}

#leaderboard {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  width: min(180px, calc(var(--app-w) - 172px));
  max-width: 180px;
  display: grid;
  gap: 3px;
  padding: 6px 7px;
  background: rgba(0, 0, 0, 0.68);
  color: var(--fg);
  font-size: 11px;
  line-height: 1.15;
  pointer-events: none;
  z-index: 2;
}

#leaderboard:empty {
  display: none;
}

#startButton {
  position: fixed;
  right: max(8px, env(safe-area-inset-right));
  bottom: calc(max(14px, env(safe-area-inset-bottom)) + 118px);
  min-width: 76px;
  z-index: 3;
  touch-action: manipulation;
}

#startButton[hidden] {
  display: none;
}

#touchControls {
  --stick-size: clamp(118px, 30vw, 148px);
  --stick-puck: clamp(42px, 11vw, 56px);
  --stick-x: 0px;
  --stick-y: 0px;
  --stick-drift-x: 0px;
  --stick-drift-y: 0px;
  position: fixed;
  inset: auto 0 max(14px, env(safe-area-inset-bottom)) 0;
  height: var(--stick-size);
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 0 max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  pointer-events: none;
  touch-action: none;
  z-index: 3;
}

#join:not([hidden]) ~ #touchControls {
  display: none;
}

.pad {
  position: relative;
  width: var(--stick-size);
  height: var(--stick-size);
  pointer-events: auto;
  touch-action: none;
}

.move-stick {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--stick-size);
  height: var(--stick-size);
  border: 2px solid rgb(247 251 255 / 48%);
  border-radius: 50%;
  background:
    radial-gradient(circle at center, rgb(88 248 120 / 15%) 0 28%, transparent 30%),
    radial-gradient(circle at center, transparent 0 52%, rgb(247 251 255 / 18%) 53% 55%, transparent 56%),
    rgb(4 8 5 / 72%);
  box-shadow:
    inset 0 0 0 1px rgb(128 255 248 / 14%),
    0 0 0 1px rgb(0 0 0 / 86%);
  pointer-events: auto;
  touch-action: none;
  outline: 0;
  transform: translate(var(--stick-drift-x), var(--stick-drift-y));
  transition: transform 90ms ease-out, opacity 90ms ease-out;
}

.move-stick::before,
.move-stick::after {
  content: "";
  position: absolute;
  background: rgb(247 251 255 / 34%);
}

.move-stick::before {
  top: 15%;
  bottom: 15%;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
}

.move-stick::after {
  left: 15%;
  right: 15%;
  top: 50%;
  height: 2px;
  transform: translateY(-50%);
}

.move-stick.is-active {
  transition: none;
  background:
    radial-gradient(circle at center, rgb(255 56 72 / 18%) 0 28%, transparent 30%),
    radial-gradient(circle at center, transparent 0 52%, rgb(255 56 72 / 34%) 53% 55%, transparent 56%),
    rgb(8 8 6 / 82%);
}

.stick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--stick-puck);
  height: var(--stick-puck);
  border: 2px solid rgb(255 231 107 / 82%);
  border-radius: 50%;
  background:
    radial-gradient(circle at 36% 30%, rgb(255 255 206 / 86%) 0 12%, transparent 13%),
    radial-gradient(circle at center, #ffe76b 0 58%, #a68b0e 100%);
  box-shadow:
    inset 0 -6px 0 rgb(0 0 0 / 22%),
    0 0 0 4px rgb(255 231 107 / 16%),
    0 2px 0 rgb(0 0 0 / 82%);
  transform: translate(calc(-50% + var(--stick-x)), calc(-50% + var(--stick-y)));
  pointer-events: none;
}

@media (max-width: 560px) {
  #leaderboard {
    top: calc(max(8px, env(safe-area-inset-top)) + 27px);
    right: max(8px, env(safe-area-inset-right));
    width: min(154px, calc(var(--app-w) - 16px));
    font-size: 10px;
  }
}

@media (max-height: 430px) and (pointer: coarse) {
  #touchControls {
    --stick-size: clamp(92px, 20vw, 118px);
    --stick-puck: clamp(36px, 8vw, 48px);
    inset: auto 0 max(8px, env(safe-area-inset-bottom)) 0;
  }

  #startButton {
    bottom: calc(max(8px, env(safe-area-inset-bottom)) + 92px);
  }
}
