:root {
  color-scheme: dark;
  --arcade-green: #58f878;
  --arcade-white: #f7fbff;
  --arcade-red: #ff3848;
  --panel: #061009;
  --app-width: 100vw;
  --app-height: 100svh;
}

* {
  box-sizing: border-box;
}

html,
body {
  position: fixed;
  inset: 0;
  width: var(--app-width);
  height: var(--app-height);
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: #000;
}

body {
  display: grid;
  place-items: center;
  min-height: var(--app-height);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#cabinet {
  --screen-reserve: 126px;
  position: relative;
  display: grid;
  grid-template-rows: auto auto auto;
  place-items: center;
  align-content: center;
  gap: 10px;
  width: min(var(--app-width), 760px);
  height: var(--app-height);
  max-height: var(--app-height);
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) calc(env(safe-area-inset-bottom) + 10px) env(safe-area-inset-left);
  background: #000;
}

#cabinet[data-game-ui="playing"] {
  --screen-reserve: 44px;
}

#screen {
  display: block;
  width: min(100%, calc((var(--app-height) - var(--screen-reserve)) * 0.875));
  height: auto;
  max-height: calc(var(--app-height) - var(--screen-reserve));
  aspect-ratio: 224 / 256;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  outline: 0;
  touch-action: none;
}

.peer-mini {
  display: none;
}

@media (pointer: fine), (hover: hover), (min-width: 720px) {
  #cabinet[data-game-ui="playing"] .peer-mini {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 8px);
    width: min(16vw, 128px);
    height: auto;
    aspect-ratio: 112 / 128;
    border: 1px solid rgb(88 248 120 / 72%);
    background: #000;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    box-shadow: 0 0 0 1px rgb(0 0 0 / 84%), inset 0 0 0 1px rgb(88 248 120 / 18%);
    pointer-events: none;
    z-index: 2;
  }

  #cabinet[data-mini-left="true"] #peerMiniLeft {
    display: block;
    left: calc(env(safe-area-inset-left) + 8px);
  }

  #cabinet[data-mini-right="true"] #peerMiniRight {
    display: block;
    right: calc(env(safe-area-inset-right) + 8px);
  }
}

.network-panel {
  display: grid;
  gap: 6px;
  width: min(100%, 560px);
  color: var(--arcade-white);
  font: 700 12px / 1.25 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0;
}

.network-panel[hidden] {
  display: none !important;
}

#joinForm {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(72px, auto);
  align-items: center;
  gap: 6px;
}

.room-picker {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  min-width: 0;
}

.room-button,
#joinForm > button,
#netHud {
  min-width: 0;
  min-height: 28px;
  border: 1px solid color-mix(in srgb, var(--arcade-green) 62%, #fff 12%);
  border-radius: 4px;
  background: #020502;
  color: var(--arcade-green);
  font: inherit;
  letter-spacing: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-button[aria-checked="true"],
#joinForm > button:active,
#netHud:active {
  background: var(--arcade-green);
  color: #001905;
}

#status {
  justify-self: end;
  white-space: nowrap;
}

#netHud {
  width: 100%;
  color: var(--arcade-white);
  text-align: left;
  padding: 0 7px;
}

#debugStats {
  display: none;
  max-height: 102px;
  margin: 0;
  overflow: auto;
  border: 1px solid rgb(88 248 120 / 42%);
  border-radius: 4px;
  background: #020502;
  color: var(--arcade-green);
  padding: 6px;
  font: 600 11px / 1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  user-select: text;
  -webkit-user-select: text;
}

#debugStats[data-expanded="true"] {
  display: block;
}

.player-panel {
  display: grid;
  grid-template-columns: auto minmax(88px, 10ch) minmax(136px, auto);
  align-items: center;
  gap: 8px;
  width: min(100%, 560px);
  min-height: 38px;
  color: var(--arcade-green);
  font: 700 14px / 1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0;
}

.player-panel label,
.player-panel output {
  white-space: nowrap;
}

.player-panel input {
  width: 100%;
  min-width: 0;
  height: 32px;
  border: 1px solid color-mix(in srgb, var(--arcade-green) 72%, #fff 12%);
  border-radius: 4px;
  background: #020502;
  color: var(--arcade-white);
  font: inherit;
  letter-spacing: 0;
  padding: 0 7px;
  text-transform: uppercase;
  outline: 0;
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

.player-panel input:focus {
  border-color: var(--arcade-white);
  box-shadow: 0 0 0 2px rgb(88 248 120 / 24%);
}

.player-panel output {
  justify-self: end;
  color: var(--arcade-white);
}

#touchControls {
  --touch-size: clamp(76px, 20vw, 94px);
  --stick-size: clamp(110px, 29vw, 132px);
  --stick-puck: clamp(42px, 11vw, 54px);
  --stick-x: 0px;
  --stick-drift-x: 0px;
  --stick-drift-y: 0px;
  display: none;
  grid-template-columns: var(--stick-size) 1fr var(--touch-size);
  align-items: center;
  gap: 0;
  width: min(100%, 560px);
  pointer-events: auto;
  touch-action: none;
}

#touchControls .move-stick {
  position: relative;
  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(255 56 72 / 18%) 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(88 248 120 / 16%),
    0 0 0 1px rgb(0 0 0 / 86%);
  justify-self: start;
  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;
}

#touchControls .move-stick::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 15%;
  right: 15%;
  height: 2px;
  border-radius: 999px;
  background: rgb(247 251 255 / 34%);
  transform: translateY(-50%);
}

#touchControls .move-stick:focus-visible {
  box-shadow:
    inset 0 0 0 1px rgb(255 56 72 / 20%),
    0 0 0 2px rgb(247 251 255 / 70%);
}

#touchControls .move-stick.is-active {
  transition: none;
  background:
    radial-gradient(circle at center, rgb(255 56 72 / 22%) 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%);
}

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

#touchControls button {
  width: var(--touch-size);
  height: var(--touch-size);
  min-height: 0;
  aspect-ratio: 1;
  border: 2px solid rgb(255 154 164 / 86%);
  border-radius: 50%;
  background:
    radial-gradient(circle at 36% 30%, rgb(255 214 218 / 90%) 0 11%, transparent 12%),
    radial-gradient(circle at center, #ff3848 0 56%, #9e0714 100%);
  color: transparent;
  font: 0 / 1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0;
  pointer-events: auto;
  touch-action: none;
  box-shadow:
    inset 0 -8px 0 rgb(0 0 0 / 18%),
    0 0 0 4px rgb(255 56 72 / 14%),
    0 3px 0 rgb(0 0 0 / 90%);
}

#touchControls .fire-button {
  grid-column: 3;
  justify-self: end;
}

#touchControls button:active,
#touchControls button.is-down {
  background:
    radial-gradient(circle at 38% 32%, rgb(255 190 197 / 82%) 0 8%, transparent 9%),
    radial-gradient(circle at center, #d81727 0 58%, #76050d 100%);
  transform: translateY(2px);
  box-shadow:
    inset 0 -4px 0 rgb(0 0 0 / 20%),
    0 0 0 5px rgb(255 56 72 / 18%),
    0 1px 0 rgb(0 0 0 / 90%);
}

@media (pointer: coarse) {
  #cabinet {
    --screen-reserve: 194px;
    min-height: var(--app-height);
    align-content: center;
  }

  #cabinet[data-game-ui="playing"] {
    --screen-reserve: 160px;
  }

  #screen {
    width: min(100%, calc((var(--app-height) - var(--screen-reserve)) * 0.875));
    max-height: calc(var(--app-height) - var(--screen-reserve));
  }

  #touchControls {
    display: grid;
  }
}

@media (pointer: coarse) and (orientation: landscape) {
  #cabinet {
    gap: 6px;
  }

  #cabinet[data-game-ui="playing"] {
    --screen-reserve: 0px;
    width: var(--app-width);
    height: var(--app-height);
    display: block;
    align-content: initial;
  }

  #cabinet[data-game-ui="playing"] .network-panel {
    display: none !important;
  }

  #cabinet[data-game-ui="playing"] #screen {
    position: absolute;
    top: env(safe-area-inset-top);
    left: 50%;
    width: auto;
    height: calc(var(--app-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    max-width: 100%;
    max-height: calc(var(--app-height) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 1;
  }

  #cabinet[data-game-ui="playing"] .peer-mini {
    position: absolute;
    top: env(safe-area-inset-top);
    width: min(22vw, 128px);
    height: auto;
    aspect-ratio: 112 / 128;
    border: 1px solid rgb(88 248 120 / 72%);
    background: #000;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    box-shadow: 0 0 0 1px rgb(0 0 0 / 84%), inset 0 0 0 1px rgb(88 248 120 / 18%);
    pointer-events: none;
    z-index: 2;
  }

  #cabinet[data-mini-left="true"] #peerMiniLeft {
    display: block;
    left: env(safe-area-inset-left);
  }

  #cabinet[data-mini-right="true"] #peerMiniRight {
    display: block;
    right: env(safe-area-inset-right);
  }

  #cabinet[data-game-ui="playing"] .player-panel {
    display: none;
  }

  #cabinet[data-game-ui="playing"] #touchControls {
    position: absolute;
    left: env(safe-area-inset-left);
    right: env(safe-area-inset-right);
    bottom: env(safe-area-inset-bottom);
    width: auto;
    height: var(--stick-size);
    z-index: 2;
  }
}

@media (max-width: 430px) {
  .network-panel {
    font-size: 11px;
  }

  #joinForm {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  #status {
    grid-column: 1 / -1;
    justify-self: center;
  }

  .player-panel {
    grid-template-columns: auto minmax(78px, 1fr);
    gap: 7px;
    font-size: 13px;
  }

  .player-panel output {
    grid-column: 1 / -1;
    justify-self: center;
  }
}

@media (max-height: 520px) and (pointer: coarse) {
  .network-panel {
    gap: 4px;
  }

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

  #touchControls {
    --touch-size: clamp(68px, 18vw, 86px);
    --stick-size: clamp(108px, 26vw, 132px);
    --stick-puck: clamp(42px, 10vw, 54px);
  }

  .player-panel {
    min-height: 32px;
  }

  .player-panel input {
    height: 28px;
  }

  #touchControls button {
    min-height: 0;
  }
}
