/* In-page Game Boy (js/emulator/EmulatorPanel.js). Colors come from the
   variables below; a page can override them on .gbemu. */
.gbemu {
  --gbemu-text: #fff;
  --gbemu-muted: rgba(255, 255, 255, .7);
  --gbemu-panel: #444;
  --gbemu-line: rgba(255, 255, 255, .15);
  --gbemu-hover: rgba(255, 255, 255, .14);
  --gbemu-pad: #5a5a5a;
  --gbemu-ab: #e8e8e8;
  --gbemu-ab-text: #333;
  --gbemu-fullscreen-bg: #222;
  --gbemu-error: #ff8a80;
  color: var(--gbemu-text);
  text-align: center;
  text-shadow: none;
}

.gbemu[hidden],
.gbemu [hidden] {
  display: none !important;
}

.gbemu-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 34px;
  margin-bottom: 8px;
}

.gbemu-title {
  min-width: 0;
  overflow: hidden;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.gbemu-view {
  display: flex;
  gap: 4px;
  margin-right: -6px;
}

.gbemu-icon {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--gbemu-text);
  opacity: .9;
  cursor: pointer;
}

.gbemu-icon:hover,
.gbemu-icon:focus-visible {
  background: var(--gbemu-hover);
  opacity: 1;
}

.gbemu-icon[aria-pressed="false"] {
  opacity: .65;
}

.gbemu-screen {
  position: relative;
  width: 100%;
  margin: 0 auto;
  aspect-ratio: var(--gbemu-ar, 10 / 9);
  overflow: hidden;
  border-radius: 6px;
  background: #000;
}

.gbemu-screen canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.gbemu-screen.empty canvas {
  visibility: hidden;
}

.gbemu-loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  border: 2px dashed var(--gbemu-line);
  border-radius: 6px;
  background: var(--gbemu-panel);
}

.gbemu-loader-title {
  margin: 0;
  font-weight: 700;
  font-size: 1.1rem;
}

.gbemu-hint {
  max-width: 320px;
  margin: 0;
  color: var(--gbemu-muted);
  font-size: .85rem;
}

/* With kept games listed, the loader grows with the list */
.gbemu-screen.empty.has-library {
  aspect-ratio: auto;
}

.gbemu-screen.empty.has-library canvas {
  display: none;
}

.gbemu-screen.empty.has-library .gbemu-loader {
  position: relative;
  inset: auto;
}

.gbemu-library {
  width: 100%;
  max-width: 420px;
  text-align: left;
}

.gbemu-library-head {
  padding: 0 6px 6px;
  border-bottom: 1px solid var(--gbemu-line);
  color: var(--gbemu-muted);
  font-size: .8rem;
}

.gbemu-library-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.gbemu-game {
  display: flex;
  align-items: center;
  gap: 2px;
  border-bottom: 1px solid var(--gbemu-line);
}

.gbemu-game-play {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  gap: 2px;
  margin: 3px 0;
  padding: 6px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--gbemu-text);
  text-align: left;
  cursor: pointer;
}

.gbemu-game-play:hover,
.gbemu-game-play:focus-visible {
  background: var(--gbemu-hover);
}

.gbemu-game-title {
  font-weight: 700;
}

.gbemu-game-meta {
  max-width: 100%;
  overflow: hidden;
  color: var(--gbemu-muted);
  font-size: .8rem;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.gbemu-toolbar {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.gbemu-toolbar button {
  padding-right: .5rem;
  padding-left: .5rem;
  white-space: nowrap;
}

.gbemu-keys {
  margin: 10px 0 0;
  color: var(--gbemu-muted);
  font-size: .8rem;
}

.gbemu-message {
  margin: 10px 0 0;
  font-size: .9rem;
}

.gbemu-message[data-type="error"] {
  color: var(--gbemu-error);
  font-weight: 700;
}

/* Touch controls, on touch screens only */
.gbemu-touch {
  display: none;
}

@media (pointer: coarse) {
  .gbemu-keys {
    display: none;
  }

  .gbemu-touch {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-areas: "dpad ab" "meta meta";
    align-items: center;
    gap: 14px 8px;
    margin: 16px auto 0;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .gbemu-screen.empty + .gbemu-touch {
    display: none;
  }
}

.gbemu-dpad {
  display: grid;
  grid-area: dpad;
  grid-template-columns: repeat(3, clamp(36px, 12vw, 46px));
  grid-template-rows: repeat(3, clamp(36px, 12vw, 46px));
}

.gbemu-dpad span {
  background: var(--gbemu-pad);
}

.gbemu-dpad .up { grid-column: 2; grid-row: 1; border-radius: 8px 8px 0 0; }
.gbemu-dpad .left { grid-column: 1; grid-row: 2; border-radius: 8px 0 0 8px; }
.gbemu-dpad .center { grid-column: 2; grid-row: 2; }
.gbemu-dpad .right { grid-column: 3; grid-row: 2; border-radius: 0 8px 8px 0; }
.gbemu-dpad .down { grid-column: 2; grid-row: 3; border-radius: 0 0 8px 8px; }

.gbemu-meta {
  display: flex;
  grid-area: meta;
  justify-content: center;
  gap: 16px;
}

.gbemu-meta span {
  min-width: 64px;
  padding: 6px 12px;
  border-radius: 12px;
  background: var(--gbemu-pad);
  color: var(--gbemu-text);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

.gbemu-ab {
  display: flex;
  grid-area: ab;
  align-items: flex-start;
  justify-content: flex-end;
  gap: 14px;
}

.gbemu-ab span {
  display: flex;
  align-items: center;
  justify-content: center;
  width: clamp(46px, 15vw, 58px);
  height: clamp(46px, 15vw, 58px);
  border-radius: 50%;
  background: var(--gbemu-ab);
  color: var(--gbemu-ab-text);
  font-size: 18px;
  font-weight: 700;
}

.gbemu-ab span[data-key="B"] {
  margin-top: 30px;
}

.gbemu-touch .pressed {
  filter: brightness(1.3);
}

/* Fullscreen: only the game, filling the screen (the Fullscreen API where there is one, the window otherwise) */
body.gbemu-fullscreen {
  overflow: hidden;
}

body.gbemu-fullscreen .gbemu {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  max-width: none;
  margin: 0;
  padding: 8px 14px 12px;
  background: var(--gbemu-fullscreen-bg);
}

body.gbemu-fullscreen .gbemu-bar {
  justify-content: flex-end;
  margin: 0;
}

body.gbemu-fullscreen .gbemu-title,
body.gbemu-fullscreen .gbemu-toolbar,
body.gbemu-fullscreen .gbemu-keys,
body.gbemu-fullscreen .gbemu-message {
  display: none;
}

body.gbemu-fullscreen .gbemu-screen {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin-top: 8px;
  aspect-ratio: auto;
  border-radius: 0;
  background: transparent;
}

body.gbemu-fullscreen .gbemu-screen canvas {
  object-fit: contain;
}

/* Screens wider than the picture, without the touch controls: the buttons in a
   column at the top right, beside the picture, which stays centred */
@media (min-aspect-ratio: 5/4) {
  @media not all and (pointer: coarse) {
    body.gbemu-fullscreen .gbemu-bar {
      position: absolute;
      top: 8px;
      right: 10px;
      z-index: 2;
    }

    body.gbemu-fullscreen .gbemu-view {
      flex-direction: column;
    }

    body.gbemu-fullscreen .gbemu-screen {
      margin: 0 38px;
      width: auto;
    }
  }
}

@media (pointer: coarse) {
  body.gbemu-fullscreen .gbemu-touch {
    flex: none;
    width: 100%;
    margin-top: 12px;
  }
}

/* Phone upright: the controls in the middle of the space under the picture */
@media (pointer: coarse) and (orientation: portrait) {
  body.gbemu-fullscreen .gbemu-screen {
    flex: 0 1 auto;
    aspect-ratio: var(--gbemu-ar, 10 / 9);
  }

  body.gbemu-fullscreen .gbemu-touch {
    flex: 1 0 auto;
    align-content: center;
    margin-top: 0;
    padding: 12px 0;
  }
}

/* Phone on its side: d-pad left of the picture, A/B and Select/Start right of it */
@media (pointer: coarse) and (orientation: landscape) {
  body.gbemu-fullscreen .gbemu {
    --gbemu-side: clamp(150px, 22vw, 200px);
  }

  body.gbemu-fullscreen .gbemu-bar {
    position: absolute;
    top: 8px;
    right: 14px;
    z-index: 2;
  }

  body.gbemu-fullscreen .gbemu-screen {
    position: absolute;
    inset: 8px calc(var(--gbemu-side) + 22px) 12px;
    width: auto;
    margin: 0;
  }

  body.gbemu-fullscreen .gbemu-touch {
    position: absolute;
    inset: 8px 14px 12px;
    width: auto;
    max-width: none;
    margin: 0;
    grid-template-columns: var(--gbemu-side) 1fr var(--gbemu-side);
    grid-template-areas: "dpad . ab" "dpad . meta";
    align-content: center;
    pointer-events: none;
  }

  body.gbemu-fullscreen .gbemu-touch > * {
    pointer-events: auto;
  }

  body.gbemu-fullscreen .gbemu-meta {
    justify-content: flex-end;
    gap: 12px;
  }

  body.gbemu-fullscreen .gbemu-meta span {
    min-width: 0;
    padding: 6px 10px;
  }
}
