html,
body,
#game {
  width: 100%;
  height: 100%;
  margin: 0;
}

html {
  --webnes-controls-height: clamp(180px, 44dvh, 380px);
  --webnes-game-height: calc(100dvh - var(--webnes-controls-height));
  background: #000;
  color-scheme: dark;
}

body {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: #000;
  cursor: none;
  user-select: none;
}

#game {
  position: fixed;
  inset: 0;
}

html.touch-controls-on #game {
  inset: 0 0 auto;
  height: var(--webnes-game-height);
}

.add-rom {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 20;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.26);
  border-radius: 999px;
  background: rgba(28, 28, 30, 0.62);
  box-shadow:
    0 16px 40px rgba(0, 0, 0, 0.38),
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
  -webkit-backdrop-filter: blur(18px) saturate(180%);
  backdrop-filter: blur(18px) saturate(180%);
  cursor: pointer;
  opacity: 0.42;
  outline: none;
  transition:
    opacity 160ms ease,
    transform 160ms ease,
    background 160ms ease,
    border-color 160ms ease;
}

.add-rom::before,
.add-rom::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 2px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.96);
  transform: translate(-50%, -50%);
}

.add-rom::after {
  transform: translate(-50%, -50%) rotate(90deg);
}

.add-rom:hover,
.add-rom:focus-visible {
  opacity: 1;
  border-color: rgba(255, 255, 255, 0.42);
  background: rgba(44, 44, 46, 0.74);
}

.add-rom:active {
  transform: scale(0.94);
}

.rom-picker {
  position: fixed;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.touch-controls {
  position: fixed;
  inset: auto 0 0;
  z-index: 14;
  display: none;
  height: var(--webnes-controls-height);
  padding:
    22px max(18px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom))
    max(18px, env(safe-area-inset-left));
  box-sizing: border-box;
  background: #030303;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  grid-template-columns: minmax(128px, 1fr) minmax(118px, 0.7fr) minmax(132px, 1fr);
  grid-template-rows: 38px 1fr;
  gap: 12px;
  touch-action: none;
}

html.touch-controls-on .touch-controls {
  display: grid;
}

.touch-shoulders {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.touch-dpad,
.touch-face,
.touch-center {
  align-self: center;
  justify-self: center;
}

.touch-dpad {
  position: relative;
  width: min(34dvh, 36vw, 184px);
  min-width: 130px;
  aspect-ratio: 1;
  border-radius: 50%;
  background:
    radial-gradient(circle at center, rgba(255, 255, 255, 0.22) 0 8%, transparent 8.5%),
    rgba(255, 255, 255, 0.035);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05);
  opacity: 0.72;
}

.touch-dpad.is-down {
  opacity: 1;
  background:
    radial-gradient(circle at center, rgba(255, 255, 255, 0.32) 0 9%, transparent 9.5%),
    rgba(255, 255, 255, 0.06);
}

.dpad-line {
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.36);
  transform: translate(-50%, -50%);
}

.dpad-line-x {
  width: 72%;
  height: 21%;
}

.dpad-line-y {
  width: 21%;
  height: 72%;
}

.dpad-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 16%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: #050505;
  border: 1px solid rgba(255, 255, 255, 0.26);
  transform: translate(-50%, -50%);
}

.touch-face {
  position: relative;
  width: min(34dvh, 36vw, 190px);
  min-width: 138px;
  aspect-ratio: 1;
}

.touch-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-width: 96px;
}

.pad-button {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.045);
  color: rgba(255, 255, 255, 0.84);
  font: 700 13px/1 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  letter-spacing: 0;
  cursor: none;
  touch-action: none;
  user-select: none;
}

.pad-button.is-down,
.pad-button:active {
  background: rgba(255, 255, 255, 0.86);
  color: #050505;
  transform: scale(0.96);
}

.shoulder {
  height: 38px;
  border-radius: 6px;
}

.system {
  width: min(92px, 26vw);
  min-height: 28px;
  border-radius: 999px;
  font-size: 10px;
}

.menu-key {
  min-height: 34px;
  border-color: rgba(255, 255, 255, 0.34);
  color: rgba(255, 255, 255, 0.96);
}

.face {
  position: absolute;
  width: 42%;
  aspect-ratio: 1;
  border-radius: 50%;
  font-size: clamp(15px, 4.3dvh, 22px);
}

.face-y {
  left: 0;
  top: 29%;
}

.face-x {
  left: 29%;
  top: 0;
}

.face-b {
  left: 29%;
  bottom: 0;
}

.face-a {
  right: 0;
  top: 29%;
}

html.touch-lefty .touch-dpad {
  grid-column: 3;
}

html.touch-lefty .touch-face {
  grid-column: 1;
  grid-row: 2;
}

html.touch-lefty .touch-center {
  grid-column: 2;
  grid-row: 2;
}

.minui-menu {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0.92);
  color: #f4f4ee;
}

.minui-menu[hidden] {
  display: none;
}

.menu-screen {
  width: min(520px, 100%);
  max-height: min(680px, calc(100dvh - 36px));
  display: grid;
  grid-template-rows: auto 1fr;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  background: #050505;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.58);
  overflow: hidden;
}

.menu-head {
  padding: 18px 18px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.16);
}

.menu-title {
  overflow: hidden;
  color: #fff;
  font: 800 20px/1.15 -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", sans-serif;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.menu-subtitle {
  overflow: hidden;
  margin-top: 5px;
  color: rgba(244, 244, 238, 0.62);
  font: 600 12px/1.2 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.menu-list {
  overflow: auto;
  padding: 10px;
}

.menu-row {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  min-height: 44px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 0 10px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: rgba(244, 244, 238, 0.84);
  font: 750 15px/1.1 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  letter-spacing: 0;
  text-align: left;
}

.menu-row + .menu-row {
  margin-top: 2px;
}

.menu-row.is-selected {
  background: #f4f4ee;
  color: #050505;
}

.menu-row:disabled {
  color: rgba(244, 244, 238, 0.36);
}

.menu-row.is-selected:disabled {
  background: rgba(244, 244, 238, 0.16);
  color: rgba(244, 244, 238, 0.46);
}

.menu-row-label,
.menu-row-value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.menu-row-value {
  color: currentColor;
  opacity: 0.68;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 21;
  max-width: min(520px, calc(100vw - 32px));
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(28, 28, 30, 0.72);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.44);
  color: rgba(255, 255, 255, 0.96);
  font: 500 14px/1.25 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition:
    opacity 180ms ease,
    transform 180ms ease;
  -webkit-backdrop-filter: blur(18px) saturate(180%);
  backdrop-filter: blur(18px) saturate(180%);
}

html.touch-controls-on .toast {
  bottom: calc(var(--webnes-controls-height) + 12px);
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#game > *,
.ejs_parent,
.ejs_game,
.ejs_canvas_parent,
.ejs_canvas {
  width: 100% !important;
  height: 100% !important;
  background: #000 !important;
  border-radius: 0 !important;
}

.ejs_canvas {
  width: 100vw;
  height: 100dvh;
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

html.touch-controls-on #game > *,
html.touch-controls-on .ejs_parent,
html.touch-controls-on .ejs_game,
html.touch-controls-on .ejs_canvas_parent,
html.touch-controls-on .ejs_canvas {
  height: var(--webnes-game-height) !important;
}

.ejs_menu_bar,
.ejs_context_menu,
.ejs_list_selector,
.ejs_virtualGamepad_parent,
.ejs_virtualGamepad_open,
.ejs_ad_iframe,
.ejs_ad_close {
  display: none !important;
  pointer-events: none !important;
  visibility: hidden !important;
}

.ejs_start_button,
.ejs_loading_text {
  opacity: 0 !important;
  pointer-events: none !important;
}

@media (max-width: 520px) {
  html {
    --webnes-controls-height: clamp(260px, 49dvh, 460px);
  }

  .add-rom {
    width: 42px;
    height: 42px;
  }

  .touch-controls {
    grid-template-columns: minmax(112px, 1fr) minmax(84px, 0.7fr) minmax(118px, 1fr);
    gap: 8px;
    padding-top: 16px;
  }

  .touch-center {
    gap: 8px;
  }

  .system {
    width: min(82px, 24vw);
  }
}
