:root {
  --panel-bg: rgba(14, 22, 38, 0.82);
  --panel-line: rgba(255, 255, 255, 0.10);
  --txt: #eef3ff;
  --txt-dim: rgba(238, 243, 255, 0.6);
  --accent: #ffd54a;
  --accent2: #4ad2ff;
  --good: #7dff9b;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0e1626;
  color: var(--txt);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  /* 'pan-y' allows only vertical scrolling (so the settings panel still scrolls) and
     blocks BOTH pinch-zoom and double-tap-zoom. 'manipulation' still permitted pinch. */
  touch-action: pan-y;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  position: fixed;
  inset: 0;
}

/* only the game canvas swallows touch gestures; overlays stay interactive */
#game { display: block; width: 100%; height: 100%; touch-action: none; }

/* ============================ HUD ============================ */
#hud {
  position: absolute;
  top: calc(10px + var(--safe-t));
  left: calc(14px + var(--safe-l));
  right: calc(14px + var(--safe-r));
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  pointer-events: none;
  z-index: 5;
  text-shadow: 0 1px 3px rgba(0,0,0,0.45);
}
.hud-block { pointer-events: none; }

#hud-left .stat { font-weight: 800; line-height: 0.95; }
.stat-value { font-size: clamp(30px, 6vw, 52px); font-variant-numeric: tabular-nums; }
.stat-unit { font-size: 0.42em; font-weight: 700; opacity: 0.75; margin-left: 2px; }
.stat-sub { font-size: clamp(13px, 2.4vw, 17px); color: var(--txt-dim); font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat-score { font-size: clamp(17px, 3.2vw, 24px); font-weight: 800; color: #ff8fdb; font-variant-numeric: tabular-nums; margin-top: 5px; }
.stat-score .orb-dot { color: #ff4fc8; text-shadow: 0 0 9px rgba(255, 79, 200, 0.95); }

#hud-speed { flex: 0 1 320px; max-width: 42vw; text-align: center; }
.speed-readout { font-weight: 800; font-size: clamp(16px, 3.2vw, 24px); font-variant-numeric: tabular-nums; }
.speed-kmh { display: inline-block; font-size: 0.55em; font-weight: 600; color: var(--txt-dim); margin-left: 8px; }
.speed-bar {
  position: relative;
  height: 9px;
  margin-top: 6px;
  border-radius: 6px;
  background: rgba(255,255,255,0.14);
  overflow: visible;
}
.speed-fill {
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 0%;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  transition: width 0.08s linear;
}
.speed-ticks { position: absolute; inset: 0; }
.speed-ticks .tick { position: absolute; top: -3px; width: 2px; height: 15px; background: rgba(255,255,255,0.5); transform: translateX(-1px); }
.speed-ticks .tick span {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
  font-size: 9px; font-weight: 700; color: var(--txt-dim); white-space: nowrap; letter-spacing: 0.02em;
}
.speed-label { margin-top: 20px; font-size: clamp(11px, 2vw, 13px); font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); }

#hud-right { text-align: right; min-width: 92px; margin-top: 50px; }
.split-title { font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--txt-dim); margin-bottom: 3px; }
#splits { font-size: 13px; font-variant-numeric: tabular-nums; line-height: 1.5; }
#splits .split-row { display: flex; justify-content: flex-end; gap: 8px; }
#splits .split-row b { color: var(--txt-dim); font-weight: 600; }
#splits .split-row.best span { color: var(--good); }

/* ============================ Touch controls ============================ */
.touch-btn {
  position: absolute;
  bottom: calc(22px + var(--safe-b));
  z-index: 6;
  border: none;
  border-radius: 999px;
  font-weight: 800;
  letter-spacing: 0.03em;
  color: #0e1626;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.5);
  transition: transform 0.06s ease, filter 0.06s ease;
  touch-action: none;
  user-select: none;
}
.touch-btn:active { transform: scale(0.94); filter: brightness(1.08); }

#btn-jump {
  right: calc(26px + var(--safe-r));
  width: clamp(92px, 15vw, 130px);
  height: clamp(92px, 15vw, 130px);
  font-size: clamp(15px, 2.4vw, 19px);
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 35% 28%, #ffe89a, var(--accent) 70%, #f0b800);
}
#btn-run {
  left: calc(26px + var(--safe-l));
  width: clamp(88px, 14vw, 124px);
  height: clamp(88px, 14vw, 124px);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: radial-gradient(circle at 35% 28%, #a6ecff, var(--accent2) 70%, #23b6ea);
  box-shadow: 0 10px 26px rgba(20,120,160,0.35), inset 0 2px 0 rgba(255,255,255,0.6), inset 0 -7px 14px rgba(0,0,0,0.12);
}
#btn-run .run-main { font-size: clamp(17px, 2.7vw, 23px); font-weight: 900; letter-spacing: 0.06em; line-height: 1; }
#btn-run .run-sub { font-size: clamp(9px, 1.4vw, 11px); font-weight: 800; letter-spacing: 0.16em; text-transform: uppercase; opacity: 0.72; }

/* JUMP shows only while actually running (hidden on the welcome + at checkpoints) */
body:not(.running) #btn-jump { display: none; }
/* RUN shows only in controlled mode, and never during a checkpoint pause */
body.autorun #btn-run,
body.paused #btn-run { display: none; }

.corner-btn {
  position: absolute;
  top: calc(10px + var(--safe-t));
  z-index: 12;   /* above the welcome overlay (z10) so Settings opens on the first tap */
  width: 44px; height: 44px;
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  background: var(--panel-bg);
  color: var(--txt);
  font-size: 20px;
  line-height: 1;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none; /* shown by JS once HUD layout settles; kept out of the busy top row */
}
/* two corner buttons top-right; splits sit just below them */
#btn-settings { right: calc(14px + var(--safe-r)); }
#btn-reset { right: calc(64px + var(--safe-r)); }
.corner-btn { display: flex; align-items: center; justify-content: center; }

/* ============================ Hint ============================ */
#hint {
  position: absolute; inset: 0;
  z-index: 10;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  background: rgba(8, 12, 22, 0.55);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  transition: opacity 0.4s ease;
  padding: 24px;
}
#hint.gone { opacity: 0; pointer-events: none; }
.hint-title { font-size: clamp(44px, 10vw, 88px); font-weight: 900; letter-spacing: -0.02em; margin-bottom: 2px; color: var(--txt); }
.hint-sub { font-size: clamp(13px, 2.6vw, 18px); font-weight: 800; letter-spacing: 0.34em; text-transform: uppercase; color: var(--txt-dim); margin-bottom: 22px; }
.hint-body { font-size: clamp(16px, 3vw, 22px); line-height: 1.8; color: var(--txt); max-width: 620px; }
.hint-body b { color: var(--accent); }
.hint-tap { margin-top: 26px; font-size: clamp(13px, 2.4vw, 16px); font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--txt-dim); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 0.45; } 50% { opacity: 1; } }

/* ============================ Finish banner ============================ */
#finish-banner {
  position: absolute; top: 22%; left: 50%; transform: translate(-50%, -10px) scale(0.9);
  z-index: 9;
  text-align: center;
  padding: 22px 40px;
  border-radius: 22px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-line);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  opacity: 0; pointer-events: none;
  transition: opacity 0.35s ease, transform 0.35s cubic-bezier(.2,1.4,.4,1);
}
#finish-banner.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
.finish-title { font-size: clamp(26px, 5vw, 40px); font-weight: 900; }
.finish-time { font-size: clamp(34px, 7vw, 60px); font-weight: 900; color: var(--accent); font-variant-numeric: tabular-nums; margin: 4px 0; }
.finish-sub { font-size: 14px; color: var(--txt-dim); }

/* ============================ Checkpoint (every 200 m) ============================ */
#checkpoint {
  position: absolute; inset: 0;
  z-index: 11;
  display: none;
  flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px;
  background: rgba(8, 12, 22, 0.45);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
}
#checkpoint.show { display: flex; }
.cp-dist {
  font-size: clamp(44px, 10vw, 88px); font-weight: 900; letter-spacing: -0.01em;
  color: var(--txt);
}
.cp-sub { font-size: clamp(14px, 2.6vw, 18px); color: var(--txt-dim); margin-top: 4px; font-variant-numeric: tabular-nums; }
#cp-continue {
  margin-top: 28px;
  padding: 16px 44px;
  border: none; border-radius: 999px;
  font-size: clamp(16px, 2.8vw, 21px); font-weight: 800; letter-spacing: 0.03em;
  color: #0e1626;
  background: radial-gradient(circle at 35% 28%, #ffe89a, var(--accent) 70%, #f0b800);
  box-shadow: 0 12px 32px rgba(0,0,0,0.35), inset 0 2px 0 rgba(255,255,255,0.55);
  transition: transform 0.06s ease, filter 0.06s ease;
}
#cp-continue:active { transform: scale(0.96); filter: brightness(1.08); }

/* ============================ Tuning panel ============================ */
#panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  z-index: 20;
  width: min(360px, 88vw);
  background: var(--panel-bg);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-left: 1px solid var(--panel-line);
  box-shadow: -18px 0 50px rgba(0,0,0,0.4);
  transform: translateX(0);
  transition: transform 0.28s cubic-bezier(.4,0,.2,1);
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
  padding-right: var(--safe-r);
}
#panel.hidden { transform: translateX(105%); }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; font-size: 18px; font-weight: 800;
  border-bottom: 1px solid var(--panel-line);
}
#panel-close { background: none; border: none; color: var(--txt); font-size: 20px; width: 36px; height: 36px; border-radius: 10px; }
#panel-close:active { background: rgba(255,255,255,0.1); }
.panel-scroll {
  flex: 1 1 auto;
  min-height: 0;              /* let it shrink so overflow-y actually scrolls in the flex column */
  overflow-y: auto;
  padding: 14px 18px calc(28px + var(--safe-b));
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;        /* allow vertical drag-scroll on touch */
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.22) transparent;
}
.panel-scroll::-webkit-scrollbar { width: 10px; }
.panel-scroll::-webkit-scrollbar-track { background: transparent; }
.panel-scroll::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.18);
  border-radius: 999px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
.panel-scroll::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.34); background-clip: padding-box; }
.panel-scroll::-webkit-scrollbar-thumb:active { background: var(--accent); background-clip: padding-box; }

.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.presets button {
  flex: 1 0 auto;
  padding: 9px 12px; border-radius: 10px;
  border: 1px solid var(--panel-line); background: rgba(255,255,255,0.06);
  color: var(--txt); font-size: 13px; font-weight: 700;
}
.presets button:active { background: rgba(255,255,255,0.16); }
.presets button.active { border-color: var(--accent); color: var(--accent); background: rgba(255,213,74,0.12); }

.slider { margin: 14px 0; }
.slider-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.slider-top .name { font-size: 14px; font-weight: 700; }
.slider-top .val { font-size: 13px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.slider .desc { font-size: 11px; color: var(--txt-dim); margin-top: 5px; line-height: 1.35; }
input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 6px; border-radius: 4px;
  background: rgba(255,255,255,0.16); outline: none;
  touch-action: none;   /* the slider owns the drag; panel still scrolls elsewhere */
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.35);
}

.panel-section { margin: 20px 0 4px; font-size: 12px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: var(--txt-dim); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 11px 0; font-size: 15px; font-weight: 600; border-bottom: 1px solid var(--panel-line); }
.toggle-row input[type=checkbox] { width: 46px; height: 28px; -webkit-appearance: none; appearance: none; background: rgba(255,255,255,0.18); border-radius: 999px; position: relative; transition: background 0.2s; }
.toggle-row input[type=checkbox]::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform 0.2s; }
.toggle-row input[type=checkbox]:checked { background: var(--good); }
.toggle-row input[type=checkbox]:checked::after { transform: translateX(18px); }

.panel-actions { display: flex; gap: 10px; margin-top: 20px; }
.panel-actions button { flex: 1; padding: 12px; border-radius: 12px; border: 1px solid var(--panel-line); background: rgba(255,255,255,0.06); color: var(--txt); font-size: 14px; font-weight: 700; }
.panel-actions button:active { background: rgba(255,255,255,0.16); }
.panel-note { margin-top: 12px; font-size: 11px; line-height: 1.5; color: var(--txt-dim); }

/* ============================ Toast ============================ */
#toast {
  position: absolute; bottom: calc(28px + var(--safe-b)); left: 50%; transform: translateX(-50%) translateY(20px);
  z-index: 30;
  padding: 10px 18px; border-radius: 999px;
  background: var(--panel-bg); border: 1px solid var(--panel-line);
  font-size: 14px; font-weight: 700; color: var(--txt);
  opacity: 0; pointer-events: none; transition: opacity 0.25s, transform 0.25s;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.hidden { }
