:root {
  --sky-top: #5ec8ff;
  --sky-bottom: #d6f4ff;
  --ink: #2b1a10;
  --card: #fffaf0;
  --accent: #ff9f1c;
  --accent-dark: #d97706;
  --green: #3ddc84;
  --font: 'Baloo 2', ui-rounded, 'SF Pro Rounded', system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(var(--sky-top), var(--sky-bottom));
  transition: background 0.6s;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

#game, #game canvas { position: fixed; inset: 0; display: block; }

.hidden { display: none !important; }

/* ---------------- HUD */
#hud {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 16px;
  right: 16px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  z-index: 5;
}
.hud-group { display: flex; gap: 8px; flex-wrap: wrap; }
.pill {
  background: rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  padding: 4px 14px;
  font-weight: 800;
  font-size: 20px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15);
  font-family: var(--font);
  color: var(--ink);
}
.pill.icon { border: 0; cursor: pointer; pointer-events: auto; padding: 4px 10px; }

/* Frame-rate counter (ui.js frameTime): a small pill under the top-right buttons with the current FPS and,
   under it, up to three rows "32 fps  ×2" of how far the frame rate dropped this level.
   Fixed width, absolutely placed rows that only show when there is a drop, and layout/paint containment, so
   updating them never re-lays out the rest of the HUD. */
.pill.fps, #hud-drops .fps-row {
  box-sizing: border-box;
  width: 7.5em;
  font-size: 12px;
  padding: 2px 8px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  contain: layout paint style;
  opacity: 0.8;
}
/* right: clear of a landscape iPhone's notch / rounded corner (the HUD itself sits 16px from the edge) */
.pill.fps { position: absolute; top: calc(100% + 6px); right: max(0px, calc(env(safe-area-inset-right) - 16px)); text-align: center; }
#hud-drops {
  position: absolute;
  top: calc(100% + 32px);   /* under the FPS pill */
  right: max(0px, calc(env(safe-area-inset-right) - 16px));
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
}
#hud-drops .fps-row { display: flex; justify-content: space-between; }
#hud-drops .fps-row.hidden { display: none; }
.pill.fps.slow, #hud-drops .fps-row.slow { color: #c0392b; }
#hud-drops .fps-session { box-sizing: border-box; width: 7.5em; padding: 0 6px; font-size: 10px; text-align: center;
  white-space: nowrap; overflow: hidden; opacity: 0.7; font-variant-numeric: tabular-nums; }
/* phones: a little smaller, so the pill and three rows stay well above the touch buttons */
@media (pointer: coarse) {
  .pill.fps, #hud-drops .fps-row { font-size: 11px; padding: 1px 7px; }
  #hud-drops { top: calc(100% + 27px); gap: 2px; font-size: 11px; }
}

/* Sprint stamina bar */
.pill.stamina { display: flex; align-items: center; gap: 8px; }
.stamina .bar {
  width: 120px;
  height: 14px;
  border-radius: 999px;
  background: rgba(43, 26, 16, 0.15);
  overflow: hidden;
}
.stamina .fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd23f, #ff9f1c);
  transition: width 0.12s linear, background 0.2s;
}
.stamina.active .fill { background: linear-gradient(90deg, #fff27a, #ffb300); box-shadow: 0 0 8px #ffcc00; }
.stamina.active .bolt { display: inline-block; animation: zap 0.3s ease-in-out infinite alternate; }
.stamina.low .fill { background: linear-gradient(90deg, #ff6b6b, #e53935); }
.stamina.empty { animation: tired 0.8s ease-in-out infinite; }
.stamina.empty .bolt { filter: grayscale(1); opacity: 0.5; }
.stamina.refill { animation: refill 0.35s ease-out; }
@keyframes zap { to { transform: scale(1.25) rotate(-8deg); } }
@keyframes tired { 50% { background: rgba(255, 205, 205, 0.9); } }
@keyframes refill { 40% { transform: scale(1.12); box-shadow: 0 0 14px #ffd23f; } }

/* ---------------- overlay screens */
#overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
  background: rgba(20, 60, 30, 0.25);
}
#overlay.show { display: flex; }
.card {
  background: var(--card);
  border-radius: 28px;
  padding: 28px 32px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.12), 0 20px 40px rgba(0, 0, 0, 0.2);
  animation: pop-in 0.35s cubic-bezier(.2, 1.6, .4, 1);
  user-select: text;
}
@keyframes pop-in { from { transform: scale(0.7); opacity: 0; } }

.logo {
  font-size: 64px;
  font-weight: 800;
  line-height: 0.9;
  color: var(--accent);
  text-shadow: 0 4px 0 var(--accent-dark);
  display: flex;
  flex-direction: column;
}
.logo span { color: var(--green); text-shadow: 0 4px 0 #1f9d55; }
.tag { font-size: 18px; margin: 12px 0 18px; }
h2 { font-size: 34px; margin: 0 0 12px; }
.msg { font-size: 20px; margin: 4px 0 12px; }
.controls { font-size: 15px; opacity: 0.8; margin: 18px 0 0; line-height: 1.8; }
.credit { font-size: 11px; opacity: 0.55; margin: 12px 0 0; }
.credit a { color: inherit; }
kbd {
  display: inline-block;
  background: #fff;
  border: 2px solid #e2d6c2;
  border-bottom-width: 4px;
  border-radius: 8px;
  padding: 0 7px;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

.buttons { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }
.buttons.col { flex-direction: column; align-items: stretch; }
.btn {
  font-family: var(--font);
  font-size: 22px;
  font-weight: 800;
  border: 0;
  border-radius: 18px;
  padding: 10px 26px;
  cursor: pointer;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 5px 0 #e2d6c2;
  transition: transform 0.08s;
}
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 5px 0 var(--accent-dark); }
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #e2d6c2; }
.btn.primary:active { box-shadow: 0 2px 0 var(--accent-dark); }
.btn:focus-visible { outline: 3px solid #4cc9f0; outline-offset: 3px; }
.btn:disabled { opacity: 0.6; cursor: wait; }
/* Clear focus ring when navigating menus with a controller. */
body.using-pad .btn:focus,
body.using-pad .name-form input:focus {
  outline: 4px solid #4cc9f0;
  outline-offset: 3px;
  transform: scale(1.06);
}
.pad-controls { margin-top: 6px; }

.stats { display: grid; gap: 8px; margin: 8px 0 18px; }
.stats div {
  display: flex;
  justify-content: space-between;
  background: #fff;
  border-radius: 14px;
  padding: 8px 16px;
  font-size: 20px;
}
.big-score { font-size: 44px; font-weight: 800; margin: 0 0 12px; }

.name-form { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.name-form input {
  font-family: var(--font);
  font-size: 20px;
  padding: 8px 14px;
  border-radius: 14px;
  border: 3px solid #e2d6c2;
  width: 200px;
  min-width: 0;
}
.name-form input:focus { outline: none; border-color: var(--accent); }
.form-msg { min-height: 1.4em; margin: 6px 0; font-size: 16px; }

.board { list-style: none; counter-reset: rank; padding: 0; margin: 0 0 16px; text-align: left; }
.board li {
  counter-increment: rank;
  display: flex;
  gap: 12px;
  align-items: center;
  background: #fff;
  border-radius: 12px;
  padding: 6px 14px;
  margin-bottom: 6px;
  font-size: 19px;
}
.board li::before { content: counter(rank); font-weight: 800; width: 1.6em; color: var(--accent-dark); }
.board li span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board li.me { background: #fff1c2; outline: 3px solid var(--accent); }
.board li.empty { justify-content: center; }
.board li.empty::before { content: none; }

/* ---------------- toast + popups */
#toast {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(43, 26, 16, 0.85);
  color: #fff;
  padding: 8px 20px;
  border-radius: 999px;
  font-size: 22px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  z-index: 6;
  white-space: nowrap;
}
#toast.show { animation: toast 2s ease forwards; }
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, -10px); }
  12%, 80% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -10px); }
}

.pop {
  position: fixed;
  transform: translate(-50%, -50%);
  font-size: 24px;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 2px 0 var(--accent-dark), 0 0 6px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  z-index: 6;
  animation: pop-up 0.9s ease-out forwards;
}
@keyframes pop-up { to { transform: translate(-50%, -150%); opacity: 0; } }

/* ---------------- on-screen controller (js/padview.js, a three.js HUD) */
#padview {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}
#padview.off, #padview.nopad { display: none; }
/* Drop-shadow + mask on a full-screen layer is costly to composite on phones. */
.low-end .pad-canvas { filter: none; }
.pad-canvas {
  --half: calc(clamp(170px, 21vw, 300px) + 4px);   /* matches .pad-half width + its inset */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.9;
  filter: drop-shadow(0 6px 8px rgba(20, 40, 20, 0.28));
  /* Each half fades out toward the middle of the screen instead of ending in a hard cut. */
  -webkit-mask-image:
    linear-gradient(to right, #000 calc(var(--half) * 0.55), transparent var(--half)),
    linear-gradient(to left, #000 calc(var(--half) * 0.55), transparent var(--half));
  mask-image:
    linear-gradient(to right, #000 calc(var(--half) * 0.55), transparent var(--half)),
    linear-gradient(to left, #000 calc(var(--half) * 0.55), transparent var(--half));
}
.pad-half {
  position: absolute;
  bottom: max(4px, env(safe-area-inset-bottom));
  width: clamp(170px, 21vw, 300px);
  aspect-ratio: 1 / 1.08;   /* headroom above the triggers for their glow */
  pointer-events: auto;
  touch-action: none;
  cursor: pointer;
}
.pad-left { left: max(4px, env(safe-area-inset-left)); }
.pad-right { right: max(4px, env(safe-area-inset-right)); }

@media (max-width: 560px) {
  .pill { font-size: 16px; padding: 3px 10px; }
  .stamina .bar { width: 64px; height: 10px; }
  .logo { font-size: 48px; }
  .card { padding: 22px 18px; }
}

/* ---------------- Phones in landscape (short screens) */
.home-hint {
  font-size: 15px;
  background: #fff3d6;
  border-radius: 14px;
  padding: 8px 12px;
  margin: 14px 0 0;
}
#overlay { overflow-y: auto; }
@media (max-height: 500px) {
  #overlay {
    align-items: safe center;
    padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
             max(8px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  }
  .card { padding: 14px 22px; max-width: 560px; border-radius: 22px; }
  .logo { font-size: 38px; flex-direction: row; justify-content: center; gap: 10px; }
  .tag { font-size: 15px; margin: 6px 0 10px; line-height: 1.35; }
  .btn { font-size: 18px; padding: 7px 20px; }
  .controls { font-size: 13px; margin-top: 10px; line-height: 1.5; }
  .home-hint { font-size: 13px; margin-top: 10px; padding: 6px 10px; }
  .credit { margin-top: 6px; }
  h2 { font-size: 26px; margin-bottom: 6px; }
}
/* On phones the on-screen controller is the controls, so skip the PS5 button list. */
@media (pointer: coarse) { .pad-controls { display: none; } }

/* ---------------- touch buttons for phones (js/touchpad.js) */
#touchpad {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  /* Lifted off the bottom edge and pulled in from the sides, to where thumbs naturally rest
     when holding a phone sideways. */
  padding: 0
    calc(max(12px, env(safe-area-inset-right)) + 3vw)
    calc(max(10px, env(safe-area-inset-bottom)) + 11vh)
    calc(max(12px, env(safe-area-inset-left)) + 3vw);
  pointer-events: none;
  z-index: 6;
}
#touchpad.pad-on, #hud.hidden ~ #touchpad { display: none; }
.tp-group { display: flex; gap: 18px; align-items: flex-end; pointer-events: auto; }
.tp-btn {
  --size: clamp(66px, 18vh, 96px);
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.9);
  background: rgba(43, 26, 16, 0.32);
  color: #fff;
  font: 800 calc(var(--size) * 0.42) / 1 var(--font);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
  box-sizing: border-box;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
  transition: transform 0.06s, background 0.06s;
}
/* Invisible switch over each button: tapping it gives the iPhone vibration tick (see touchpad.js). */
.tp-tick {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  border-radius: 50%;
  opacity: 0.01;
}
.tp-btn.on { transform: scale(0.92); background: rgba(255, 159, 28, 0.75); }
.tp-jump { --size: clamp(82px, 23vh, 118px); background: rgba(255, 159, 28, 0.45); }
/* Sprint sits up and to the left of Jump, on the thumb's natural arc. */
.tp-run { margin-bottom: calc(clamp(82px, 23vh, 118px) * 0.7); }
.low-end .tp-btn { -webkit-backdrop-filter: none; backdrop-filter: none; }

/* ---------------- phones held upright: the game is landscape-only */
#rotate { display: none; }
@media (pointer: coarse) and (orientation: portrait) {
  #rotate {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: linear-gradient(var(--sky-top), var(--sky-bottom));
    text-align: center;
    padding: 24px;
  }
  #rotate div { font-size: 64px; animation: tilt 1.6s ease-in-out infinite; }
  #rotate p { font-size: 22px; font-weight: 800; margin: 0; }
}
@keyframes tilt { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }

/* Right thumb slides between ⚡ and ⤒ (js/touchpad.js): latched Sprint glows, the button under
   the thumb is pressed in. */
.tp-btn.on:not(.thumb) { transform: none; box-shadow: 0 0 0 4px rgba(255, 210, 63, 0.55), 0 4px 0 rgba(0, 0, 0, 0.18); }
.tp-btn.thumb { transform: scale(0.92); background: rgba(255, 159, 28, 0.8); }
/* Joystick for the left thumb (js/touchpad.js). The invisible zone covers the lower-left of the
   screen: a thumb landing anywhere in it brings the stick to the thumb. */
.tp-group.tp-right { margin-left: auto; }
.tp-stick-zone {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 42vw;
  height: 70vh;
  pointer-events: auto;
  touch-action: none;
  -webkit-touch-callout: none;
}
.tp-stick {
  --stick: clamp(130px, 38vh, 176px);
  position: fixed;
  left: calc(max(12px, env(safe-area-inset-left)) + 3vw);
  bottom: calc(max(10px, env(safe-area-inset-bottom)) + 9vh);
  width: var(--stick);
  height: var(--stick);
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  background: radial-gradient(circle, rgba(43, 26, 16, 0.12) 0 40%, rgba(43, 26, 16, 0.3) 100%);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
  pointer-events: none;
  transition: opacity 0.15s;
}
/* Floating: placed by top/left from JS while the thumb is down. */
.tp-stick.active { bottom: auto; }
/* Little ◀ ▶ marks on the rim, lit in the direction being pushed. */
.tp-stick::before, .tp-stick::after {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.75);
  font-size: calc(var(--stick) * 0.13);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
}
.tp-stick::before { content: '◀'; left: 8%; }
.tp-stick::after { content: '▶'; right: 8%; }
.tp-stick.left::before, .tp-stick.right::after { color: #ffd23f; }
.tp-knob {
  position: absolute;
  left: 30%;
  top: 30%;
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.6); /* see-through: the monkey may be right behind it */
  border: 2px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
}
.tp-stick:not(.active) .tp-knob { transition: transform 0.12s ease-out; }
.tp-stick.left .tp-knob, .tp-stick.right .tp-knob { background: rgba(255, 210, 63, 0.6); }

/* ---------------- iPhone Safari: swipe once to hide the browser bars (see js/fullview.js) */
html.ios-browser, html.ios-browser body { height: auto; overflow: visible; }
html.ios-browser body { min-height: 1000vh; } /* lots of room so a swipe never hits the end; the game is position: fixed */
html.ios-catch, html.ios-catch body { touch-action: pan-y; }
#overlay { overscroll-behavior: contain; } /* a menu scrolled to its end must not scroll the page */
#swipe-full { display: none; }
html.ios-catch #swipe-full { display: block; position: fixed; inset: 0; z-index: 60; }
/* Swipe mode: the surface is page content as tall as the page, so the finger drags content that
   really scrolls (a fixed layer let the page scroll without Safari minimizing its bars). */
html.ios-browser.ios-catch #swipe-full {
  position: absolute; top: 0; left: 0; right: 0; bottom: auto;
  height: 1000vh;
  touch-action: pan-y;
}
.sf-tint {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 60, 30, 0.45);
  pointer-events: none; /* touches land on the scrolling surface underneath */
}
.sf-card {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 14px 28px; border-radius: 22px;
  background: var(--card); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}
.sf-card p { margin: 0; font-size: 22px; font-weight: 800; }
.sf-hand { font-size: 44px; animation: sf-swipe 1.3s ease-in-out infinite; }
@keyframes sf-swipe { 0% { transform: translateY(18px); opacity: 0; } 30% { opacity: 1; }
  80% { transform: translateY(-18px); opacity: 1; } 100% { transform: translateY(-18px); opacity: 0; } }
@media (orientation: portrait) { html.ios-catch #swipe-full { display: none; } }
.sf-debug:empty { display: none; }
.sf-debug { font-size: 12px; opacity: 0.6; font-variant-numeric: tabular-nums; }
.sf-card .sf-btn { pointer-events: auto; touch-action: manipulation; font-size: 22px; }

/* ---------------- loading screen (index.html #boot): until the title (or an error) is up */
#boot {
  position: fixed;
  inset: 0;
  z-index: 40;   /* under #rotate (50), over everything else */
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(var(--sky-top), var(--sky-bottom));
  transition: opacity 0.5s;
}
#boot.done { opacity: 0; pointer-events: none; }
.boot-box { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.boot-logo { font-size: 64px; animation: boot-bob 1.2s ease-in-out infinite; }
.boot-bar { width: min(260px, 60vw); height: 14px; border-radius: 7px; background: rgba(255, 255, 255, 0.55); overflow: hidden; }
.boot-fill { width: 0; height: 100%; border-radius: 7px; background: #ffb21e; transition: width 0.3s; }
#boot.indet .boot-fill { width: 35%; animation: boot-sweep 1.3s ease-in-out infinite; }
.boot-text { font-weight: 800; font-size: 18px; color: var(--ink); }
@keyframes boot-bob { 50% { transform: translateY(-8px); } }
@keyframes boot-sweep { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
