:root {
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  background: #111827;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow: hidden;
}

.game-shell {
  --shell-padding: clamp(4px, 2vmin, 16px);
  --touch-row-height: 88px;
  --touch-row-gap: 12px;

  display: grid;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  place-items: center;
  padding: var(--shell-padding);
}

.pwa-actions {
  position: fixed;
  z-index: 300;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: grid;
  max-width: min(360px, calc(100vw - 24px));
  gap: 8px;
}

.pwa-install-button,
.pwa-update button {
  min-height: 42px;
  padding: 9px 14px;
  border: 2px solid rgb(255 255 255 / 75%);
  border-radius: 9px;
  background: #ffe27a;
  color: #10243a;
  font: 800 13px/1 system-ui, sans-serif;
  cursor: pointer;
}

.pwa-update {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border: 1px solid rgb(255 255 255 / 45%);
  border-radius: 10px;
  background: rgb(17 24 39 / 94%);
  color: #fff;
  font: 700 13px/1.35 system-ui, sans-serif;
  box-shadow: 0 8px 24px rgb(0 0 0 / 35%);
}

.pwa-actions [hidden] {
  display: none;
}

.game-layout {
  position: relative;
  display: grid;
  max-width: 100%;
  max-height: 100%;
  justify-items: center;
}

.performance-diagnostics {
  position: absolute;
  z-index: 195;
  top: max(52px, calc(env(safe-area-inset-top) + 52px));
  right: max(10px, env(safe-area-inset-right));
  padding: 6px 8px;
  border: 1px solid rgb(255 255 255 / 45%);
  border-radius: 6px;
  background: rgb(12 20 30 / 82%);
  color: #fff;
  font: 600 12px/1.3 ui-monospace, monospace;
  white-space: pre;
  pointer-events: none;
}

.fullscreen-button {
  position: absolute;
  z-index: 200;
  top: 10px;
  right: 10px;
  display: flex;
  min-height: 38px;
  align-items: center;
  gap: 7px;
  padding: 8px 11px;
  border: 2px solid rgb(255 255 255 / 68%);
  border-radius: 10px;
  background: rgb(17 24 39 / 78%);
  color: #fff;
  font: 700 12px/1 system-ui, sans-serif;
  touch-action: manipulation;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.fullscreen-button[hidden] {
  display: none;
}

.fullscreen-button:focus-visible {
  outline: 3px solid #fff0a8;
  outline-offset: 2px;
}

.fullscreen-button:active {
  transform: scale(0.97);
}

.game-layout:fullscreen {
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  place-content: center;
  padding:
    max(var(--shell-padding), env(safe-area-inset-top))
    max(var(--shell-padding), env(safe-area-inset-right))
    max(var(--shell-padding), env(safe-area-inset-bottom))
    max(var(--shell-padding), env(safe-area-inset-left));
  background: #111827;
}

.game-layout:fullscreen #game {
  width: min(100%, 960px);
}

.game-layout:fullscreen .fullscreen-button {
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  gap: 5px;
  padding: 6px 9px;
  font-size: 11px;
}

.game-layout:fullscreen .fullscreen-button [data-fullscreen-label] {
  display: none;
}

.game-layout:fullscreen .fullscreen-button::after {
  content: 'SALIR';
}

#game {
  width: min(
    calc(100vw - var(--shell-padding) - var(--shell-padding)),
    960px
  );
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 2px solid #dbeafe;
  border-radius: 12px;
  box-shadow: 0 18px 50px rgb(0 0 0 / 35%);
}

#game canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

.touch-controls {
  display: none;
  width: 100%;
  height: var(--touch-row-height);
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 8px 4px 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.touch-enabled .game-layout {
  gap: var(--touch-row-gap);
}

.touch-enabled .touch-controls {
  display: flex;
}

.touch-controls__movement {
  display: flex;
  gap: 16px;
}

.touch-controls__actions {
  display: flex;
  gap: 10px;
}

.touch-controls button {
  display: grid;
  width: 72px;
  height: 72px;
  margin: 0;
  padding: 0;
  place-items: center;
  border: 3px solid rgb(255 255 255 / 72%);
  border-radius: 50%;
  outline: none;
  background: rgb(23 50 77 / 78%);
  color: #fff;
  font: 700 30px/1 system-ui, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-enabled .touch-controls button.is-pressed {
  background: rgb(47 111 145 / 95%);
  transform: scale(0.95);
}

.touch-controls button.is-hidden {
  visibility: hidden;
  pointer-events: none;
}

.touch-controls .touch-controls__action {
  width: 92px;
  border-radius: 28px;
  font-size: 16px;
}

@media (orientation: portrait) {
  .game-shell.touch-enabled {
    --shell-padding: 2px;
    --touch-row-gap: 6px;
  }

  .touch-enabled .touch-controls {
    padding-right: max(6px, env(safe-area-inset-right));
    padding-left: max(6px, env(safe-area-inset-left));
  }
}

@media (orientation: landscape) {
  #game {
    width: auto;
    height: calc(100dvh - var(--shell-padding) - var(--shell-padding));
    max-height: 540px;
  }

  .touch-enabled #game {
    width: min(
      calc(
        100vw - env(safe-area-inset-left) - env(safe-area-inset-right)
      ),
      177.7778dvh,
      960px
    );
    height: auto;
  }

  .touch-enabled #game canvas {
    width: 100% !important;
    height: 100% !important;
  }

  .game-shell.touch-enabled {
    --shell-padding: 0px;
  }

  .touch-enabled .game-layout {
    width: 100%;
    height: 100%;
    gap: 0;
    place-items: center;
  }

  .touch-enabled .touch-controls {
    position: absolute;
    z-index: 190;
    bottom: max(4px, env(safe-area-inset-bottom));
    left: 0;
    height: auto;
    padding: 0 max(8px, env(safe-area-inset-right)) 0
      max(8px, env(safe-area-inset-left));
    pointer-events: none;
  }

  .touch-enabled .touch-controls button {
    width: 60px;
    height: 60px;
    border-width: 2px;
    background: rgb(23 50 77 / 68%);
    pointer-events: auto;
  }

  .touch-enabled .touch-controls__movement {
    flex-direction: row;
    gap: 8px;
  }

  .touch-enabled .touch-controls__movement button {
    width: 80px;
    height: 80px;
  }

  .touch-enabled .touch-controls .touch-controls__action {
    width: 92px;
    height: 82px;
    font-size: 14px;
  }

  .touch-enabled .touch-controls__actions {
    gap: 8px;
  }

  .game-layout:fullscreen #game {
    width: auto;
    height: min(
      calc(
        100dvh - max(var(--shell-padding), env(safe-area-inset-top)) -
          max(var(--shell-padding), env(safe-area-inset-bottom))
      ),
      540px
    );
  }

  .touch-enabled .game-layout:fullscreen #game {
    width: min(
      calc(
        100vw - env(safe-area-inset-left) - env(safe-area-inset-right)
      ),
      177.7778dvh,
      960px
    );
    height: auto;
  }
}
