:root {
  --bg: #23233a;
  --panel: #30304a;
  --text: #f2f2f7;
  --muted: #9a9ab0;
  --good: #3ddc84;
  --warn: #ff5a5a;
  --mark: #ffd54a;

  /* Height reserved above the game for the score row and the bar. */
  --hud-height: 118px;

  /* Width of the game column: as wide as fits, never taller than the screen. */
  --stage-w: min(calc(100vw - 16px), calc((100vh - var(--hud-height)) * 0.5625), 560px);
}

@supports (height: 100dvh) {
  :root {
    --stage-w: min(calc(100vw - 16px), calc((100dvh - var(--hud-height)) * 0.5625), 560px);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

#page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Reserved, empty space for ads later. Only shown on wide screens. */
.ad-slot {
  display: none;
  width: 300px;
  flex: none;
}

@media (min-width: 1100px) {
  .ad-slot { display: block; }
}

#game-wrap {
  flex: none;
  width: var(--stage-w);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

#hud {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  width: 100%;
}

.hud-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 24px;
  font-weight: 700;
}

.hud-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

#bar-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

#bar {
  flex: 1;
  display: flex;
  gap: 3px;
  padding: 4px 4px 11px;
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: 8px;
}

.seg {
  position: relative;
  flex: 1;
  height: 14px;
  border-radius: 3px;
   background: #4a4a66;

}

.seg.on { background: var(--good); }
.seg.over.on { background: var(--warn); }

/* Small marker under the segment where Sub8 becomes catchable. */
.seg.mark::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 6px;
  height: 6px;
  background: var(--mark);
  transform: translateX(-50%) rotate(45deg);
}

#bar-wrap.ready .seg.mark::after {
  box-shadow: 0 0 8px var(--mark);
}

#bar-wrap.slowed #bar {
  border-color: var(--warn);
  animation: slowed-pulse 0.5s infinite alternate;
}

@keyframes slowed-pulse {
  from { box-shadow: 0 0 0 rgba(255, 90, 90, 0); }
  to   { box-shadow: 0 0 10px rgba(255, 90, 90, 0.9); }
}

#stage {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 16;
  border: 2px solid #2b2b3a;
  border-radius: 12px;
  overflow: hidden;
  background: #000;
}

#game-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
    justify-content: flex-start;
  overflow-y: auto;
  gap: 12px;
  padding: 20px 16px;
  text-align: center;
    background: rgba(20, 20, 45, 0.6);
}

#overlay.hidden { display: none; }

#overlay h1 {
  margin: 0;
  font-size: clamp(26px, 8vw, 40px);
  letter-spacing: 0.02em;
}

#overlay p {
  margin: 0;
  max-width: 28ch;
  color: var(--muted);
  font-size: clamp(14px, 4vw, 18px);
  line-height: 1.4;
}

#start-btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 14px 40px;
  font-size: 20px;
  font-weight: 700;
  color: #06130b;
  background: var(--good);
  cursor: pointer;
  touch-action: manipulation;
}

#start-btn:disabled { opacity: 0.5; cursor: default; }
#start-btn:active:not(:disabled) { transform: scale(0.97); }
/* Red diamond under the segment where the slowdown starts. */
.seg.slowmark::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -8px;
  width: 6px;
  height: 6px;
  background: var(--warn);
  transform: translateX(-50%) rotate(45deg);
}

#bar-wrap.slowed .seg.slowmark::after {
  box-shadow: 0 0 8px var(--warn);
}
/* Menu is centered when it fits and scrolls from the top when it doesn't. */
#overlay > :first-child { margin-top: auto; }
#overlay > :last-child { margin-bottom: auto; }
   /* Phones: no frame, no margins. The game area fills everything below the score row. */
@media (max-width: 700px) {
  html, body { overflow: hidden; }

  #page {
    display: block;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    padding: 0;
  }

  #game-wrap {
    width: 100%;
    height: 100%;
    gap: 0;
  }

  #hud {
    padding: env(safe-area-inset-top) calc(10px + env(safe-area-inset-right)) 2px calc(10px + env(safe-area-inset-left));
  }

  #bar-wrap {
    padding: 0 calc(10px + env(safe-area-inset-right)) 4px calc(10px + env(safe-area-inset-left));
  }

  #stage {
    flex: 1;
    min-height: 0;
    width: 100%;
    aspect-ratio: auto;
    border: 0;
    border-radius: 0;
  }
}
 
/* ---------- Ad slots: empty means hidden ---------- */
.ad-box {
  align-items: center;
  justify-content: center;
  overflow: hidden;
  text-align: center;
}

/* Phone banner: phones only, only when it has content (or in preview mode). */
#banner-slot {
  display: none;
  flex: none;
  width: 100%;
  min-height: 50px;
  padding-bottom: env(safe-area-inset-bottom);
}

@media (max-width: 700px) {
  #banner-slot:not(:empty),
  body.ads-preview #banner-slot { display: flex; }
}

/* Game-over ad: only on the game-over screen, only when it has content (or in preview mode). */
#gameover-ad {
  display: none;
  width: 300px;
  max-width: 100%;
  min-height: 250px;
}

#overlay.is-over #gameover-ad:not(:empty),
body.ads-preview #overlay.is-over #gameover-ad { display: flex; }

/* Preview mode: dashed boxes with the word Ad, to check the layout. */
body.ads-preview .ad-box { border: 1px dashed var(--muted); color: var(--muted); font-size: 12px; }
body.ads-preview .ad-box::before { content: "Ad"; }

/* Menu link row */
#legal-links { font-size: 12px; color: var(--muted); }
#legal-links a { color: var(--muted); text-decoration: underline; margin: 0 4px; }
/* Ad frames fill their slot with no border. */
.ad-box iframe { display: block; border: 0; max-width: 100%; }
/* Sound on/off button next to the timer. */
#mute-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  padding: 6px 8px;
  margin-left: 4px;
  align-self: center;
  cursor: pointer;
  touch-action: manipulation;
}

#mute-btn[hidden] { display: none; }