:root {
  --bg: #172f2a;
  --panel: #0e1d1a;
  --panel-edge: #0b1715;
  --shadow: #08100f;
  --cream: #fff3bf;
  --text: #f8f0d2;
  --muted: #e8dcb9;
  --mint: #9bf2cc;
  --mint-shadow: #347d63;
  --gold: #f1cf62;
  --gold-shadow: #8b5e22;
  --error: #ffb4a8;
  --ink: #20140b;
  color: var(--text);
  background: var(--bg);
  font-family: Avenir Next, Trebuchet MS, Verdana, sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100dvh;
  margin: 0;
  display: grid;
  place-items: center;
  overflow: auto;
  background:
    radial-gradient(circle at 18% 12%, #255a4e 0 14rem, transparent 34rem),
    radial-gradient(circle at 85% 88%, #0b1f1c 0 20rem, transparent 38rem),
    var(--bg);
}

.shell {
  width: min(100vw, 760px);
  max-width: 100vw;
  min-height: 100dvh;
  padding: max(14px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: grid;
  grid-template-areas:
    "masthead"
    "display"
    "status"
    "controls"
    "keys";
  grid-template-rows: auto auto auto auto auto;
  gap: clamp(10px, 2.4vw, 18px);
  align-content: center;
  justify-items: stretch;
  overflow-x: clip;
}

.shell:fullscreen {
  width: 100vw;
  height: 100vh;
  min-height: 0;
  max-width: none;
  background: var(--bg);
}

.shell > *,
.masthead > * {
  min-width: 0;
}

.masthead {
  grid-area: masthead;
  display: grid;
  justify-items: center;
  gap: clamp(4px, .8vmin, 9px);
}

.github-link {
  display: inline-flex;
  align-items: center;
  gap: .42em;
  color: var(--cream);
  font-size: clamp(15px, 3vw, 24px);
  font-weight: 500;
  line-height: 1;
  text-decoration: underline;
  text-decoration-thickness: .11em;
  text-underline-offset: .22em;
  opacity: .86;
  transition: opacity 120ms ease, transform 120ms ease;
}

.github-link:hover,
.github-link:focus-visible {
  opacity: 1;
  transform: translateY(-1px);
}

.github-link svg {
  width: 1.18em;
  height: 1.18em;
  display: block;
  fill: currentColor;
  filter: drop-shadow(0 .12em 0 #00000040);
}

h1 {
  width: 100%;
  max-width: 100%;
  margin: 0;
  color: var(--cream);
  font-size: clamp(24px, 7.4vw, 46px);
  line-height: .86;
  letter-spacing: -.065em;
  text-align: center;
  text-shadow: 0 .08em 0 #00000040;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.display-panel {
  grid-area: display;
  width: min(100%, 62dvh);
  min-height: 0;
  aspect-ratio: 3 / 2;
  padding: clamp(6px, 1.2vmin, 14px);
  justify-self: center;
  display: grid;
  place-items: center;
  border: clamp(2px, .55vmin, 6px) solid var(--panel-edge);
  border-radius: clamp(14px, 3vmin, 32px);
  background: var(--panel);
  box-shadow:
    0 clamp(8px, 2vmin, 22px) 0 var(--shadow),
    0 clamp(16px, 5vmin, 52px) #00000066,
    inset 0 1px 0 #ffffff18;
}

#screen {
  width: 100%;
  max-width: 100%;
  height: auto;
  max-height: 100%;
  aspect-ratio: 3 / 2;
  display: block;
  object-fit: contain;
  image-rendering: pixelated;
  border-radius: clamp(8px, 1.6vmin, 18px);
  background: #f8f8f8;
}

.status {
  grid-area: status;
  min-height: 1.25em;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  color: var(--mint);
  font-size: clamp(12px, 3.2vw, 18px);
  font-weight: 400;
  line-height: 1.2;
  text-align: center;
  font-variant-numeric: tabular-nums;
  white-space: normal;
  overflow-wrap: anywhere;
}

.status.error {
  padding: .7em .9em;
  border: 2px solid var(--error);
  border-radius: 10px;
  color: var(--cream);
  background: #5a211d;
  box-shadow: 0 5px 0 #210b09;
  font-size: clamp(14px, 2.4vw, 18px);
  font-weight: 650;
  line-height: 1.35;
}

.controls {
  grid-area: controls;
  width: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, .9fr) minmax(0, 1.1fr);
  gap: clamp(10px, 2.5vw, 18px);
  align-items: center;
}

.dpad,
.face {
  min-width: 0;
  display: grid;
  gap: clamp(6px, 1.7vw, 12px);
}

.dpad {
  justify-self: center;
  grid-template-columns: repeat(3, clamp(34px, 10vw, 54px));
  grid-template-rows: repeat(2, clamp(34px, 10vw, 54px));
}

.dpad [data-key='up'] { grid-column: 2; }
.dpad [data-key='left'] { grid-column: 1; grid-row: 2; }
.dpad [data-key='right'] { grid-column: 3; grid-row: 2; }
.dpad [data-key='down'] { grid-column: 2; grid-row: 2; }

.face {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

button,
.upload-save {
  min-width: 0;
  min-height: clamp(36px, 9.5vw, 54px);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  border: 0;
  border-radius: 999px;
  color: var(--ink);
  background: var(--gold);
  font: inherit;
  font-size: clamp(12px, 3.6vw, 17px);
  font-weight: 950;
  box-shadow:
    0 clamp(5px, 1.1vmin, 10px) 0 var(--gold-shadow),
    0 clamp(9px, 2vmin, 18px) clamp(8px, 2vmin, 24px) #00000055;
  cursor: pointer;
  touch-action: none;
}

button:active,
button.pressed,
.upload-save:active {
  transform: translateY(clamp(4px, .9vmin, 8px));
  box-shadow:
    0 clamp(1px, .35vmin, 3px) 0 var(--gold-shadow),
    0 clamp(4px, 1vmin, 10px) clamp(6px, 1.5vmin, 18px) #00000055;
}

button.active {
  color: #062018;
  background: var(--mint);
  box-shadow:
    0 clamp(5px, 1.1vmin, 10px) 0 var(--mint-shadow),
    0 0 0 clamp(2px, .45vmin, 5px) var(--cream),
    0 clamp(9px, 2vmin, 18px) clamp(8px, 2vmin, 24px) #00000055;
}

.speed-control,
.resolution-control,
.render-control,
.utility-controls {
  grid-column: 1 / -1;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  display: grid;
  min-width: 0;
  gap: clamp(8px, 1.8vw, 14px);
}

.speed-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  color: var(--cream);
  font-size: clamp(14px, 4vw, 18px);
  font-weight: 900;
}

#speed-value {
  color: var(--mint);
  font-variant-numeric: tabular-nums;
}

.resolution-control,
.render-control {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: clamp(10px, 2vw, 18px);
  color: var(--cream);
  font-size: clamp(13px, 3.6vw, 17px);
  font-weight: 900;
}

.render-control output {
  color: var(--mint);
  font-variant-numeric: tabular-nums;
}

#render-scale,
#visual-mode {
  width: 100%;
  min-width: 0;
  min-height: clamp(38px, 9vw, 50px);
  padding: 0 clamp(10px, 2vw, 16px);
  border: 2px solid var(--mint-shadow);
  border-radius: 999px;
  color: var(--cream);
  background: #142d27;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

#render-scale:focus-visible,
#visual-mode:focus-visible,
.render-control input:focus-visible {
  outline: 3px solid var(--mint);
  outline-offset: 2px;
}

.speed-buttons,
.render-control input[type="range"] {
  width: 100%;
  min-width: 0;
  accent-color: var(--mint);
  cursor: pointer;
}

.utility-controls {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.speed-buttons {
  display: grid;
  gap: clamp(7px, 1.7vw, 14px);
}

#fullscreen {
  grid-column: 1 / -1;
}

.upload-save {
  display: grid;
  place-items: center;
  background: var(--mint);
  box-shadow:
    0 clamp(5px, 1.1vmin, 10px) 0 var(--mint-shadow),
    0 clamp(9px, 2vmin, 18px) clamp(8px, 2vmin, 24px) #00000055;
}

.upload-save:active {
  box-shadow:
    0 clamp(1px, .35vmin, 3px) 0 var(--mint-shadow),
    0 clamp(4px, 1vmin, 10px) clamp(6px, 1.5vmin, 18px) #00000055;
}

#upload-save {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.keys {
  grid-area: keys;
  margin: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  color: var(--muted);
  font-size: clamp(12px, 3.5vw, 15px);
  line-height: 1.2;
  text-align: center;
}

@media (max-width: 480px) {
  .shell {
    gap: 9px;
    align-content: start;
  }

  .display-panel {
    width: min(100%, 48dvh);
  }

  .controls {
    gap: 8px;
  }

  .speed-control,
  .resolution-control,
  .utility-controls,
  .speed-buttons {
    gap: 7px;
  }
}

@media (min-width: 560px) {
  .utility-controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  #fullscreen {
    grid-column: auto;
  }
}

@media (min-width: 900px) and (min-aspect-ratio: 6 / 5) {
  body {
    overflow: hidden;
  }

  .shell {
    width: min(100vw, 1440px);
    height: 100dvh;
    min-height: 0;
    padding: clamp(10px, 2.1vmin, 24px);
    grid-template-areas:
      "masthead masthead"
      "display controls"
      "status controls"
      "keys keys";
    grid-template-columns: minmax(0, 1fr) clamp(300px, 32vw, 430px);
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    gap: clamp(8px, 1.6vmin, 18px) clamp(14px, 2.4vmin, 28px);
    align-content: stretch;
  }

  h1 {
    font-size: clamp(28px, 6.2vmin, 68px);
  }

  .github-link {
    font-size: clamp(15px, 2.4vmin, 24px);
  }

  .display-panel {
    width: 100%;
    height: auto;
    max-height: 100%;
    align-self: center;
  }

  #screen {
    width: 100%;
    height: auto;
  }

  .controls {
    align-self: center;
    grid-template-columns: minmax(112px, .9fr) minmax(0, 1.1fr);
    gap: clamp(8px, 1.4vmin, 16px);
  }

  .dpad {
    grid-template-columns: repeat(3, clamp(38px, 6.5vmin, 62px));
    grid-template-rows: repeat(2, clamp(38px, 6.5vmin, 62px));
  }

  button,
  .upload-save {
    min-height: clamp(36px, 6.4vmin, 58px);
    font-size: clamp(12px, 1.85vmin, 17px);
  }

  .speed-heading,
  .resolution-control {
    font-size: clamp(12px, 1.85vmin, 16px);
  }

  .speed-buttons,
  .utility-controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #fullscreen {
    grid-column: 1 / -1;
  }

  .status {
    font-size: clamp(11px, 1.7vmin, 16px);
  }

  .keys {
    font-size: clamp(10px, 1.45vmin, 14px);
  }
}

@media (max-width: 380px) {
  .shell {
    gap: 10px;
  }

  .github-link {
    font-size: 16px;
  }

  h1 {
    font-size: clamp(25px, 8.4vw, 34px);
  }

  .controls {
    grid-template-columns: minmax(98px, .8fr) minmax(0, 1.2fr);
    gap: 10px 8px;
  }

  .dpad {
    grid-template-columns: repeat(3, clamp(32px, 10.5vw, 40px));
    grid-template-rows: repeat(2, clamp(32px, 10.5vw, 40px));
    gap: 6px;
  }

  button,
  .upload-save {
    min-height: 38px;
    font-size: 12px;
  }

  .speed-heading,
  .resolution-control,
  .status,
  .keys {
    font-size: 12px;
  }
}
