:root {
  --bg: #0b0918;
  --bg-2: #171233;
  --edge: #2a2250;
  --accent: #2ec4b6;
  --gold: #ffd166;
  --coral: #ff5d73;
  --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);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 35%, var(--bg-2) 0%, var(--bg) 70%) fixed;
  background-color: var(--bg);
  color: #fff;
  font-family: system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#loading {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 30;
}
#loading img {
  display: block;
  width: min(96px, 24vmin);
  height: auto;
}
html:not(.loading) #loading { display: none; }

.loading #frame, .loading #toolbar, .loading #touch { visibility: hidden; }
#frame {
  position: relative;
  line-height: 0;
  border-radius: 6px;
  box-shadow:
    0 0 0 2px var(--edge),
    0 0 0 6px #120e27,
    0 24px 60px rgba(0, 0, 0, .65),
    0 0 120px rgba(46, 196, 182, .12);
}

#game {
  display: block;
  width: 400px;
  height: 224px;
  border-radius: 4px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  outline: none;
}
/* drawn at least as sharp as the display (SCREEN_K, boot.js fit): the browser just smooths the last bit of scaling */
#game.smooth { image-rendering: auto; }

/* ---------- toolbar (pause / fullscreen) ---------- */
#toolbar {
  position: fixed;
  top: calc(12px + var(--safe-t));
  right: calc(12px + var(--safe-r));
  display: flex;
  gap: 8px;
  z-index: 10;
}
.tool {
  width: 40px;
  height: 40px;
  padding: 9px;
  border: 2px solid var(--edge);
  border-radius: 10px;
  background: rgba(18, 14, 39, .85);
  color: var(--gold);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s ease, border-color .12s ease;
}
.tool:hover { transform: translateY(-1px); border-color: var(--accent); }
.tool:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tool svg { width: 100%; height: 100%; display: block; }
.tool path { fill: currentColor; }
.tool path.line { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

#fs .icon-exit { display: none; }
#fs.on .icon-enter { display: none; }
#fs.on .icon-exit { display: block; }

/* ---------- touch controls ---------- */
.touch-only { display: none; }
html.touch .touch-only { display: block; }
html.touch #frame { box-shadow: 0 0 0 2px var(--edge), 0 12px 40px rgba(0, 0, 0, .6); border-radius: 0; }
html.touch #game { border-radius: 0; }

/* landscape phone: the game fills the screen, controls float over its lower corners */
html.touch #toolbar { flex-direction: column; }
html.touch .tool { width: 38px; height: 38px; padding: 8px; background: rgba(18, 14, 39, .6); }

/* --ui scales the whole overlay to the phone (boot.js fit): about 1 on a typical phone held sideways */
#touch {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  transition: opacity .2s ease;
}
#dpad, #btns, #autorun {
  position: absolute;
  pointer-events: auto;
  touch-action: none;
}

/* the stick: a round base with a knob that follows the thumb. The touch area is bigger than the base,
   so a thumb that wanders a little off it still steers */
#dpad {
  left: calc(4px + var(--safe-l));
  bottom: calc(4px + var(--safe-b));
  width: 180px;
  height: 180px;
  transform: scale(var(--ui, 1));
  transform-origin: left bottom;
}
#dpad .base {
  position: absolute;
  left: 22px;
  top: 22px;
  width: 136px;
  height: 136px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(18, 14, 39, .2) 0%, rgba(18, 14, 39, .45) 70%);
  border: 2px solid rgba(255, 255, 255, .22);
}
#dpad .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 58px;
  margin: -29px 0 0 -29px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .22);
  border: 2px solid rgba(255, 255, 255, .45);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
  transform: translate(var(--kx, 0px), var(--ky, 0px));
}
#dpad.held .knob { background: rgba(255, 209, 102, .35); border-color: rgba(255, 209, 102, .85); }
#dpad .chev {
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid transparent;
  opacity: .55;
  transition: opacity .06s ease;
}
#dpad .chev.u { left: 50%; top: 8px; margin-left: -8px; border-bottom: 11px solid #fff; border-top-width: 0; }
#dpad .chev.d { left: 50%; bottom: 8px; margin-left: -8px; border-top: 11px solid #fff; border-bottom-width: 0; }
#dpad .chev.l { top: 50%; left: 8px; margin-top: -8px; border-right: 11px solid #fff; border-left-width: 0; }
#dpad .chev.r { top: 50%; right: 8px; margin-top: -8px; border-left: 11px solid #fff; border-right-width: 0; }
#dpad.up .chev.u, #dpad.down .chev.d, #dpad.left .chev.l, #dpad.right .chev.r { opacity: 1; border-top-color: transparent; }
#dpad.up .chev.u { border-bottom-color: var(--gold); }
#dpad.down .chev.d { border-top-color: var(--gold); border-bottom-color: transparent; }
#dpad.left .chev.l { border-right-color: var(--gold); }
#dpad.right .chev.r { border-left-color: var(--gold); }

/* auto-run chip, just above the stick (it changes how the stick moves him) */
#autorun {
  left: calc(26px + var(--safe-l));
  bottom: calc(4px + 162px * var(--ui, 1) + var(--safe-b));
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px 5px 8px;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, .22);
  background: rgba(18, 14, 39, .5);
  color: rgba(255, 255, 255, .6);
  font: 800 10px/1 system-ui, sans-serif;
  letter-spacing: .06em;
  cursor: pointer;
}
#autorun .dot { width: 8px; height: 8px; border-radius: 50%; background: rgba(255, 255, 255, .3); }
#autorun.on { color: #fff; border-color: rgba(46, 196, 182, .7); }
#autorun.on .dot { background: var(--accent); box-shadow: 0 0 6px var(--accent); }

/* the face buttons in a controller's diamond, the same on every controller: jump at the bottom (biggest, where the
   thumb rests), run / fire on the left (one thumb can lie across both), spin attack on top, roll on the right */
#btns {
  right: calc(8px + var(--safe-r));
  bottom: calc(6px + var(--safe-b));
  width: 210px;
  height: 200px;
  transform: scale(var(--ui, 1));
  transform-origin: right bottom;
}
#btns .btn {
  position: absolute;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, .92);
  border: 2px solid rgba(255, 255, 255, .28);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .3);
  transition: transform .06s ease, background-color .06s ease;
}
#btns .btn i { font: 800 12px/1 system-ui, sans-serif; font-style: normal; letter-spacing: .05em; }
#bB i { font-size: 10px; }
#bA { width: 74px; height: 74px; left: 68px; top: 122px; background: rgba(255, 93, 115, .45); }
#bB { width: 62px; height: 62px; left: 10px; top: 69px; background: rgba(46, 196, 182, .38); }
#bS { width: 62px; height: 62px; left: 74px; top: 8px; background: rgba(255, 209, 102, .3); }
#bR { width: 62px; height: 62px; left: 138px; top: 69px; background: rgba(160, 130, 255, .32); }
#btns .btn.on { transform: scale(.92); border-color: #fff; }
#bA.on { background: rgba(255, 93, 115, .8); }
#bB.on { background: rgba(46, 196, 182, .75); }
#bS.on { background: rgba(255, 209, 102, .7); }
#bR.on { background: rgba(160, 130, 255, .7); }

/* faint while resting so the game shows through; a press lights up what's under the thumb */
#dpad .base, #btns .btn, #autorun { opacity: .3; transition: opacity .08s ease, transform .06s ease, background-color .06s ease; }
#dpad.held .base, #btns .btn.on { opacity: .6; }

/* a controller plugged in or paired: the touch controls step aside until it's gone again */
html.pad #touch, html.pad #toolbar .touch-only { display: none; }

/* menus are tapped directly: the play controls step out of the way */
html.menus #touch, html.menus .play-only { opacity: 0; visibility: hidden; }
html.touch:not(.playing) .play-only { display: none; }

/* ---------- turn the phone sideways ---------- */
#rotate {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 24px;
  text-align: center;
  background: radial-gradient(ellipse at 50% 40%, var(--bg-2) 0%, var(--bg) 75%);
  color: #fff;
  font-family: Fredoka, system-ui, sans-serif;
}
html.touch.portrait #rotate { display: flex; }
html.touch.portrait #touch, html.touch.portrait #toolbar { visibility: hidden; }
#rotate .big { margin: 18px 0 0; font-size: 22px; font-weight: 700; color: var(--gold); }
#rotate .small { margin: 0; font-size: 14px; color: #b8b2d8; max-width: 260px; }
#rotate .phone {
  width: 54px;
  height: 92px;
  border: 4px solid #fff;
  border-radius: 10px;
  position: relative;
  animation: tilt 2.4s ease-in-out infinite;
}
#rotate .phone span {
  position: absolute;
  left: 50%;
  bottom: 5px;
  width: 10px;
  height: 4px;
  margin-left: -5px;
  border-radius: 2px;
  background: #fff;
}
@keyframes tilt {
  0%, 25% { transform: rotate(0deg); }
  55%, 85% { transform: rotate(-90deg); border-color: var(--accent); }
  100% { transform: rotate(0deg); }
}
@media (prefers-reduced-motion: reduce) { #rotate .phone { animation: none; transform: rotate(-90deg); } }

/* portrait phone: the game waits behind the turn-sideways screen (#rotate) */
html.touch.portrait #stage { align-items: flex-start; padding-top: calc(64px + var(--safe-t)); }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  top: calc(16px + var(--safe-t));
  transform: translate(-50%, -8px);
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(18, 14, 39, .92);
  border: 2px solid var(--edge);
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 20;
  max-width: calc(100vw - 32px);
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* edge to edge (fullscreen, or a phone on its side): no frame, nothing round the picture but a dark blue */
html.edge, html.edge body { background: #000; }
html.loading, html.loading body,
html.edge #frame { box-shadow: none; border-radius: 0; }
html.edge #game { border-radius: 0; }
html.edge:not(.touch) #toolbar { opacity: 0; transition: opacity .3s ease; }
html.edge:not(.touch) #toolbar:hover { opacity: 1; }
