:root {
  color-scheme: dark;
  --bg: #16151a;
  --panel: #201f26;
  --raised: #2b2a33;
  --ink: #ece9e2;
  --muted: #a39f97;
  --line: #3a3842;
  --red: #e0344f;
  --radius: 0.9rem;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  font-size: 1rem;
  line-height: 1.5;
}

a {
  color: inherit;
  text-decoration-color: var(--muted);
  text-underline-offset: 0.15em;
}

a:hover {
  text-decoration-color: var(--red);
}

code,
kbd {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.app {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  max-width: 76rem;
  margin: 0 auto;
  padding: max(1rem, env(safe-area-inset-top))
    max(1rem, env(safe-area-inset-right))
    max(1.5rem, env(safe-area-inset-bottom))
    max(1rem, env(safe-area-inset-left));
}

h1,
h2 {
  margin: 0;
  line-height: 1.15;
  text-wrap: balance;
}

h1 {
  font-size: clamp(1.5rem, 1.2rem + 1.2vw, 2rem);
  font-weight: 650;
  letter-spacing: -0.02em;
}

h2 {
  font-size: 1rem;
  font-weight: 600;
}

.masthead p {
  margin: 0.4rem 0 0;
  max-width: 65ch;
  color: var(--muted);
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 20rem;
  gap: 1.5rem;
  align-items: start;
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
}

.screen {
  position: relative;
  display: grid;
  place-items: center;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 256 / 240;
  width: min(100%, calc((100svh - 14rem) * 16 / 15));
  min-width: min(100%, 16rem);
  margin: 0 auto;
}

.screen canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
  outline: none;
}

.screen:fullscreen {
  border-radius: 0;
  width: 100%;
  height: 100%;
  aspect-ratio: auto;
}

.screen-empty {
  position: absolute;
  margin: 0;
  color: var(--muted);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

button,
.button {
  appearance: none;
  font: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 0.6rem;
  padding: 0.45rem 0.85rem;
  min-height: 2.5rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:hover:not(:disabled),
.button:hover {
  border-color: var(--muted);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button:focus-visible,
.button:focus-within,
input:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--red);
  outline-offset: 2px;
}

.toolbar .button {
  margin-left: auto;
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  font-weight: 600;
}

.status {
  margin: 0;
  min-height: 1.5em;
  color: var(--muted);
}

.error {
  color: #ff8a9a;
}

.library {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  max-height: calc(100vh - 2rem);
  position: sticky;
  top: 1rem;
  width: 20rem;
}

.library-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.library-head input {
  font: inherit;
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.3rem 0.55rem;
  width: 9rem;
  min-width: 0;
}

.library-note {
  margin: 0;
  color: var(--muted);
}

.games {
  list-style: none;
  margin: 0 -0.4rem;
  padding: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  min-height: 0;
}

.games li {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.game {
  flex: 1;
  justify-content: flex-start;
  background: transparent;
  border-color: transparent;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9rem;
  min-height: 2.25rem;
  padding: 0.3rem 0.4rem;
  text-align: left;
  overflow-wrap: anywhere;
}

.game[aria-current="true"] {
  background: var(--raised);
  border-color: var(--red);
}

.source {
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0.3rem 0.4rem;
}

.controls {
  border-top: 1px solid var(--line);
  padding-top: 0.75rem;
}

.controls table {
  border-collapse: collapse;
  margin-top: 0.6rem;
  width: 100%;
}

.controls th,
.controls td {
  text-align: left;
  padding: 0.2rem 0.6rem 0.2rem 0;
  border-bottom: 1px solid var(--line);
  font-weight: 400;
}

.controls thead th {
  color: var(--muted);
  font-size: 0.9rem;
}

.controls kbd {
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 0.35rem;
  padding: 0.05rem 0.4rem;
}

.controls p {
  margin: 0.6rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.pad {
  display: none;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 0.25rem;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

@media (pointer: coarse) {
  .pad {
    display: grid;
  }
}

.pad button {
  touch-action: none;
  padding: 0;
}

.pad button.held {
  background: var(--red);
  border-color: var(--red);
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 3.25rem);
  grid-template-rows: repeat(3, 3.25rem);
}

.dpad button {
  border-radius: 0.5rem;
  font-size: 1.1rem;
}

.dpad .up {
  grid-area: 1 / 2;
}

.dpad .left {
  grid-area: 2 / 1;
}

.dpad .right {
  grid-area: 2 / 3;
}

.dpad .down {
  grid-area: 3 / 2;
}

.meta {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
}

.meta button {
  font-size: 0.8rem;
  padding: 0 0.7rem;
  border-radius: 999px;
}

.face {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.face .round {
  width: 4rem;
  height: 4rem;
  border-radius: 50%;
  font-weight: 700;
  font-size: 1.1rem;
}

.face .round:last-child {
  transform: translateY(-1.25rem);
}

.drop-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgb(22 21 26 / 0.85);
  border: 3px dashed var(--red);
  pointer-events: none;
  z-index: 10;
}

.drop-overlay[hidden] {
  display: none;
}

.drop-overlay p {
  font-size: 1.5rem;
  font-weight: 600;
}

@media (max-width: 52rem) {
  .layout {
    grid-template-columns: 1fr;
  }

  .library {
    position: static;
    max-height: none;
  }

  .toolbar .button {
    margin-left: 0;
  }
}

@media (max-width: 30rem) {
  .dpad {
    grid-template-columns: repeat(3, 2.75rem);
    grid-template-rows: repeat(3, 2.75rem);
  }

  .face .round {
    width: 3.4rem;
    height: 3.4rem;
  }
}
