:root {
  --bg: #16181c;
  --bg-hi: #23262d;
  --btn: #34373f;
  --btn-hi: #4a4e58;
  --btn-lo: #1a1c21;
  --btn-pressed: #24262c;
  --label: #9a9ea8;
  --text: #e6e7ea;
  --muted: #a3a7b0;
  --accent: #5fd3a0;
  --menu-bg: #202329;
  --menu-btn: #30343b;
  --menu-btn-hi: #3b4048;
  --danger: #ff8a80;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  color: var(--text);
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-synthesis: none;
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(120% 70% at 50% 0%, var(--bg-hi) 0, transparent 70%),
    var(--bg);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.shell {
  position: fixed;
  inset: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.shell:fullscreen {
  background:
    radial-gradient(120% 70% at 50% 0%, var(--bg-hi) 0, transparent 70%),
    var(--bg);
}

/* Left-handed play mirrors the layout: landscape grid columns follow
   `direction`, portrait positions swap right for left below. The D-pad's
   cross and the A/B cluster keep their inner order. */
.shell.left-hand {
  direction: rtl;
}

.dpad,
.face {
  direction: ltr;
}

/* ---------- screen ---------- */

.screen-area {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: start center;
}

.picture {
  position: relative;
}

#screen {
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 2;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}

.status {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  padding: 12px;
  overflow: auto;
  color: var(--text);
  background: #0a0b0dee;
  font-size: 14px;
  line-height: 1.35;
  text-align: center;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  -webkit-user-select: text;
  user-select: text;
}

.status.visible {
  display: grid;
}

.status.error {
  place-items: start;
  color: var(--danger);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  text-align: left;
}

/* ---------- controls (shared look) ---------- */

.shell button {
  margin: 0;
  padding: 0;
  border: 0;
  font: inherit;
  color: inherit;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  cursor: pointer;
}

.menu-open {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #ffffff0d;
  color: var(--muted);
  touch-action: manipulation !important;
}

.menu-open svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
}

.menu-open:active {
  background: #ffffff22;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--d));
  grid-template-rows: repeat(3, var(--d));
  touch-action: none;
  filter: drop-shadow(0 4px 6px #00000088);
}

/* One gradient across the whole pad, so the cross reads as a single piece. */
.dpad button,
.dpad-center {
  background: linear-gradient(180deg, var(--btn-hi), var(--btn) 40%, var(--btn-lo)) 0 0 / calc(var(--d) * 3) calc(var(--d) * 3);
}

.dpad [data-key='up'] { grid-column: 2; grid-row: 1; border-radius: 8px 8px 0 0; }
.dpad [data-key='left'] { grid-column: 1; grid-row: 2; border-radius: 8px 0 0 8px; background-position-y: calc(var(--d) * -1); }
.dpad-center { grid-column: 2; grid-row: 2; background-position-y: calc(var(--d) * -1); }
.dpad [data-key='right'] { grid-column: 3; grid-row: 2; border-radius: 0 8px 8px 0; background-position-y: calc(var(--d) * -1); }
.dpad [data-key='down'] { grid-column: 2; grid-row: 3; border-radius: 0 0 8px 8px; background-position-y: calc(var(--d) * -2); }

.dpad button {
  position: relative;
  display: grid;
  place-items: center;
}

.dpad-center::after {
  content: "";
  display: block;
  width: 46%;
  height: 46%;
  margin: 27%;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 40%, var(--btn-lo), var(--btn));
}

.dpad i {
  width: 0;
  height: 0;
  border: calc(var(--d) * .13) solid transparent;
  opacity: .55;
}

.dpad [data-key='up'] i { border-bottom-color: var(--label); margin-top: calc(var(--d) * -.13); }
.dpad [data-key='down'] i { border-top-color: var(--label); margin-top: calc(var(--d) * .13); }
.dpad [data-key='left'] i { border-right-color: var(--label); margin-left: calc(var(--d) * -.13); }
.dpad [data-key='right'] i { border-left-color: var(--label); margin-left: calc(var(--d) * .13); }

.dpad button.pressed {
  background: var(--btn-pressed);
  box-shadow: inset 0 3px 6px #000000aa;
}

.face button {
  width: var(--f);
  height: var(--f);
  border-radius: 50%;
  color: var(--label);
  font-size: calc(var(--f) * .34);
  font-weight: 700;
  background: radial-gradient(circle at 50% 35%, var(--btn-hi), var(--btn) 55%, var(--btn-lo));
  box-shadow:
    0 4px 0 #0c0d10,
    0 8px 14px #00000099,
    inset 0 1px 0 #ffffff1a;
}

.face button.pressed {
  transform: translateY(3px);
  background: radial-gradient(circle at 50% 40%, var(--btn), var(--btn-pressed));
  box-shadow:
    0 1px 0 #0c0d10,
    0 3px 6px #00000099,
    inset 0 2px 5px #00000088;
}

.pill {
  display: grid;
  justify-items: center;
  gap: 8px;
  touch-action: none;
}

.pill button {
  position: relative;
  width: var(--pill-w);
  height: var(--pill-h);
  border-radius: 999px;
  background: linear-gradient(180deg, #4a4e58, #2c2f36);
  box-shadow: 0 2px 0 #0c0d10, 0 4px 8px #00000088;
}

/* Larger invisible hit area around the small pill. */
.pill button::before {
  content: "";
  position: absolute;
  inset: -10px -8px;
}

.pill button.pressed {
  background: #24262c;
  box-shadow: inset 0 2px 4px #000000aa;
}

.pill span {
  color: var(--label);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .12em;
  pointer-events: none;
}

.run-toggle {
  width: var(--pill-w);
  height: max(24px, var(--pill-h));
  border-radius: 999px;
  color: var(--label);
  background: #24262c;
  box-shadow: inset 0 0 0 1.5px #4a4e58;
  font-size: 11px !important;
  font-weight: 800 !important;
  letter-spacing: .12em;
  touch-action: manipulation !important;
}

.run-toggle[aria-pressed='true'] {
  color: #0b1d15;
  background: var(--accent);
  box-shadow: 0 0 12px #5fd3a066;
}

/* ---------- portrait: one-thumb layout ----------
   Picture full width at the top. All controls sit in one cluster anchored to
   the bottom corner of the holding hand, placed by how often they are used:
   D-pad and A/B in the thumb's natural arc (A highest-frequency action at
   the arc's outer edge, B a short roll below it), START / SELECT / RUN in a
   row at the top of the reach, the menu gear at the far corner (rarely
   needed, hard to hit by accident). Every size scales with --u so the
   cluster fits short phones; positions use inline-end so the left-hand
   setting mirrors them. */

@media (orientation: portrait), (max-aspect-ratio: 1249 / 1000) {
  .shell {
    --rm: max(10px, var(--safe-r));
    --bm: calc(max(14px, var(--safe-b)) + 6px);
    /* Cluster is 380u wide and 330u tall; 1u is at most 1px. */
    --u: min(1px,
      calc((100vw - 20px) / 380),
      calc((100dvh - 100vw / 1.5 - var(--safe-t) - var(--bm) - 12px) / 330));
    --d: calc(58 * var(--u));
    --f: calc(76 * var(--u));
    --pill-w: calc(64 * var(--u));
    --pill-h: max(22px, calc(26 * var(--u)));
    padding-top: var(--safe-t);
  }

  .shell.left-hand {
    --rm: max(10px, var(--safe-l));
  }

  /* The picture sits just above the control cluster (a 20px gap over the
     START / SELECT / RUN row and the gear), not at the top of the screen,
     so the eyes travel less between game and thumb. Any spare height goes
     above it. Full width unless the space left is too short. */
  .screen-area {
    position: absolute;
    top: var(--safe-t);
    left: 0;
    right: 0;
    bottom: calc(var(--bm) + max(316 * var(--u), 274 * var(--u) + 40px) + 20px);
    container-type: size;
    place-items: end center;
  }

  .picture {
    width: min(100cqw, 150cqh);
  }

  .dpad,
  .face-a,
  .face-b,
  .pill,
  .run-toggle,
  .menu-open {
    position: absolute;
  }

  .face {
    display: contents;
  }

  /* --e: distance from the holding hand's screen edge; --b: from the bottom. */
  .dpad { --e: 150; --b: 34; }
  .face-a { --e: 32; --b: 152; }
  .face-b { --e: 46; --b: 52; }
  .pill-start { --e: 48; --b: 262; }
  .pill-select { --e: 140; --b: 262; }
  .run-toggle { --e: 232; --b: 280; }
  .menu-open { --e: 330; --b: 274; }

  .dpad,
  .face-a,
  .face-b,
  .pill,
  .run-toggle,
  .menu-open {
    right: calc(var(--rm) + var(--e) * var(--u));
    bottom: calc(var(--bm) + var(--b) * var(--u));
  }

  .shell.left-hand :is(.dpad, .face-a, .face-b, .pill, .run-toggle, .menu-open) {
    right: auto;
    left: calc(var(--rm) + var(--e) * var(--u));
  }
}

/* ---------- landscape: two-handed, controls beside the game ---------- */

@media (orientation: landscape) and (min-aspect-ratio: 5 / 4) {
  .shell {
    --d: clamp(36px, 11dvh, 72px);
    --f: calc(var(--d) * 1.22);
    --pill-w: calc(var(--d) * 1.05);
    --pill-h: max(16px, calc(var(--d) * .36));
    padding: max(6px, var(--safe-t)) max(8px, var(--safe-r)) max(6px, var(--safe-b)) max(8px, var(--safe-l));
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "select screen start"
      "dpad screen face"
      "run screen .";
    column-gap: clamp(8px, 1.6vw, 28px);
    row-gap: clamp(4px, 1.5dvh, 14px);
    align-items: center;
  }

  .screen-area {
    grid-area: screen;
    align-self: stretch;
    height: 100%;
    container-type: size;
    place-items: center;
  }

  .picture {
    width: min(100cqw, 150cqh);
  }

  .dpad { grid-area: dpad; justify-self: center; }

  .face {
    grid-area: face;
    justify-self: center;
    position: relative;
    width: calc(var(--f) * 2.25);
    height: calc(var(--f) * 1.75);
  }

  .face button { position: absolute; }
  .face-a { right: 0; top: 0; }
  .face-b { left: 0; bottom: calc(var(--f) * .04); }

  .pill button { transform: rotate(-18deg); }
  .pill-select { grid-area: select; justify-self: center; align-self: start; padding-top: 6px; }
  .pill-start { grid-area: start; justify-self: start; align-self: start; padding-top: 6px; }

  /* The gear shares START's cell, at its far edge. */
  .menu-open { grid-area: start; justify-self: end; align-self: start; }

  .run-toggle { grid-area: run; justify-self: center; align-self: end; }
}

/* Very short landscape screens (phones with browser bars). */
@media (orientation: landscape) and (min-aspect-ratio: 5 / 4) and (max-height: 340px) {
  .shell {
    --d: clamp(34px, 12dvh, 44px);
  }

  .menu-open {
    width: 34px;
    height: 34px;
  }

  .pill span {
    display: none;
  }
}

/* ---------- menu ---------- */

.menu {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  overflow: auto;
  border: 1px solid #ffffff14;
  border-radius: 16px;
  color: var(--text);
  background: var(--menu-bg);
  box-shadow: 0 20px 60px #000000cc;
  overscroll-behavior: contain;
}

.menu::backdrop {
  background: #000000a0;
}

.menu-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 14px 10px 18px;
  background: var(--menu-bg);
}

.menu-head h2 {
  margin: 0;
  font-size: 18px;
}

.menu-paused {
  padding: 2px 8px;
  border-radius: 999px;
  color: #0b1d15;
  background: var(--accent);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}

.menu-close {
  margin-left: auto;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  color: var(--muted);
  background: #ffffff0d;
  font: inherit;
  font-size: 16px;
  cursor: pointer;
}

.menu-section {
  display: grid;
  gap: 10px;
  padding: 10px 18px;
}

.menu-section + .menu-section {
  border-top: 1px solid #ffffff0d;
}

.menu-row {
  display: flex;
  justify-content: space-between;
  font-weight: 600;
}

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

.speed-slider {
  width: 100%;
  height: 32px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
  touch-action: pan-y;
}

.speed-ticks {
  display: flex;
  justify-content: space-between;
  margin-top: -6px;
  padding: 0 2px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.menu-grid > :only-child {
  grid-column: 1 / -1;
}

.menu button:not(.menu-close) {
  min-height: 44px;
  padding: 8px 10px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  color: var(--text);
  background: var(--menu-btn);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.menu button:not(.menu-close):active {
  background: var(--menu-btn-hi);
}

.menu button.active {
  color: #0b1d15;
  background: var(--accent);
}

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


.menu-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}




