/* =====================================================================
   GAME.CSS: layout and look of the game page (game.html)
   ===================================================================== */


/* ---------- The page never scrolls or bounces while playing ---------- */

html,
.game-page {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}


/* ---------- Game screen layout ---------- */

.game {
  /* Size of the button + ring. Fits small phones and caps on big screens. */
  --machine: min(84vw, 360px, 50vh);

  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 560px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding:
    calc(12px + env(safe-area-inset-top))
    calc(16px + env(safe-area-inset-right))
    calc(20px + env(safe-area-inset-bottom))
    calc(16px + env(safe-area-inset-left));
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

@supports (height: 100dvh) {
  .game {
    --machine: min(84vw, 360px, 50dvh);
  }
}

.game__top {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  width: 100%;
}

.level {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.level__label {
  color: var(--text-soft);
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.level__number {
  font-family: var(--font-display);
  font-size: 3rem;
  line-height: 0.95;
  letter-spacing: 0.04em;
}

.level__number.is-bumping {
  animation: level-bump 300ms var(--ease-spring);
}

@keyframes level-bump {
  0% { transform: scale(1.35); color: var(--red); }
  100% { transform: scale(1); }
}


/* ---------- The machine: timer ring + button + burst ---------- */

.machine {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: var(--machine);
  height: var(--machine);
  margin: auto 0 0;
}

.machine .game-button {
  --size: calc(var(--machine) * 0.68);
}

.timer-ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);       /* start at 12 o'clock */
  pointer-events: none;
}

.timer-ring circle {
  fill: none;
}

.timer-ring__outline {
  stroke: var(--line);
  stroke-width: 17;
}

.timer-ring__track {
  stroke: var(--surface-muted);
  stroke-width: 11;
}

/* 565.49 is the length of the circle (2 × π × 90). Keep it if you change nothing else. */
.timer-ring__progress {
  stroke: var(--blue);
  stroke-width: 11;
  stroke-dasharray: 565.49;
  stroke-dashoffset: 0;
}

.timer-ring__progress.is-running {
  animation: ring-empty var(--level-time, 7s) linear forwards;
}

.timer-ring__progress.is-paused {
  animation-play-state: paused;
}

/* The ring empties, and turns red in the last part */
@keyframes ring-empty {
  0% { stroke-dashoffset: 0; stroke: var(--blue); }
  70% { stroke: var(--blue); }
  80% { stroke: var(--red); }
  100% { stroke-dashoffset: 565.49; stroke: var(--red); }
}


/* ---------- Live counter (Easy only) ---------- */

.counter {
  margin-top: 10px;
  font-family: var(--font-display);
  font-size: 2.4rem;
  line-height: 1;
  letter-spacing: 0.04em;
}

.counter.is-bumping {
  animation: counter-bump 160ms var(--ease-spring);
}

@keyframes counter-bump {
  0% { transform: scale(1.3); }
  100% { transform: scale(1); }
}


/* ---------- Instruction: the most readable thing on screen ---------- */

.instruction {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 112px;
  margin: 20px 0 auto;
  padding: 16px 20px;
}

/* The speech-bubble tail pointing up at the button */
.instruction::before {
  content: "";
  position: absolute;
  top: -11px;
  left: 50%;
  width: 18px;
  height: 18px;
  margin-left: -9px;
  border-top: var(--outline-width) solid var(--line);
  border-left: var(--outline-width) solid var(--line);
  background: var(--surface);
  transform: rotate(45deg);
}

.instruction__text {
  color: var(--text);
  font-size: clamp(1.4rem, 5.8vw, 1.95rem);
  font-weight: 900;
  line-height: 1.2;
  text-align: center;
  text-wrap: balance;
}

/* The one-time tip on a player's first game */
.instruction__hint {
  margin-top: 8px;
  padding: 4px 12px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: var(--yellow);
  color: #1B1B1F;
  font-size: 0.9rem;
  font-weight: 800;
  line-height: 1.3;
  text-align: center;
  text-wrap: balance;
}

.instruction__text.is-new {
  animation: instruction-in 180ms var(--ease-out);
}

@keyframes instruction-in {
  0% { opacity: 0; transform: translateY(6px); }
  100% { opacity: 1; transform: none; }
}


/* ---------- Comic bursts ---------- */

.burst {
  position: absolute;
  z-index: 5;
  top: 50%;
  left: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64%;
  aspect-ratio: 1;
  margin: -32% 0 0 -32%;
  opacity: 0;
  pointer-events: none;
}

.burst svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.burst__text {
  position: relative;
  font-family: var(--font-display);
  font-size: calc(var(--machine) * 0.13);
  line-height: 1;
  letter-spacing: 0.04em;
  color: #1B1B1F;
  transform: rotate(-8deg);
}

.burst--fail .burst-shape {
  fill: var(--red);
}

.burst--fail .burst__text {
  color: #FFFFFF;
  text-shadow: 2px 2px 0 #1B1B1F;
}

/* Pass: pops in, then fades away */
.burst--pass.is-visible {
  animation: burst-pass 650ms var(--ease-out) forwards;
}

@keyframes burst-pass {
  0% { opacity: 0; transform: scale(0.3) rotate(-14deg); }
  30% { opacity: 1; transform: scale(1.08) rotate(3deg); }
  45% { transform: scale(1) rotate(0); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: scale(1.04); }
}

/* Fail: pops in and stays */
.burst--fail.is-visible {
  animation: burst-fail 380ms var(--ease-spring) forwards;
}

@keyframes burst-fail {
  0% { opacity: 0; transform: scale(0.3) rotate(12deg); }
  100% { opacity: 1; transform: scale(1.1) rotate(-4deg); }
}

/* The screen shakes on failure */
.game.is-quaking {
  animation: quake 420ms var(--ease-out);
}

@keyframes quake {
  0%, 100% { transform: translate(0, 0); }
  15% { transform: translate(-9px, 4px); }
  30% { transform: translate(8px, -5px); }
  45% { transform: translate(-6px, -3px); }
  60% { transform: translate(5px, 4px); }
  75% { transform: translate(-3px, 2px); }
}


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

.end {
  position: fixed;
  inset: 0;
  z-index: 10;
  overflow-y: auto;
  overscroll-behavior: contain;
  background-color: var(--paper);
  background-image: radial-gradient(var(--paper-dots) 1.2px, transparent 1.7px);
  background-size: 14px 14px;
}

.end__inner {
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-width: 480px;
  margin: 0 auto;
  padding:
    calc(24px + env(safe-area-inset-top))
    calc(16px + env(safe-area-inset-right))
    calc(28px + env(safe-area-inset-bottom))
    calc(16px + env(safe-area-inset-left));
  animation: end-in 260ms var(--ease-out);
}

@keyframes end-in {
  0% { opacity: 0; transform: translateY(12px); }
  100% { opacity: 1; transform: none; }
}

.end__title {
  color: var(--red);
  font-family: var(--font-display);
  font-size: clamp(3rem, 14vw, 4.2rem);
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.04em;
  text-align: center;
  text-shadow: 3px 3px 0 var(--line);
}

.end__title:focus {
  outline: none;
}

.end-card {
  padding: 16px 18px 18px;
}

.end-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

/* NEW BEST! badge: a tilted comic sticker */
.new-best {
  padding: 3px 10px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--yellow);
  color: #1B1B1F;
  box-shadow: var(--shadow-small);
  font-family: var(--font-display);
  font-size: 1.2rem;
  line-height: 1.1;
  letter-spacing: 0.05em;
  transform: rotate(-4deg);
  animation: new-best-in 420ms 250ms var(--ease-spring) both;
}

@keyframes new-best-in {
  0% { opacity: 0; transform: rotate(-4deg) scale(0.4); }
  100% { opacity: 1; transform: rotate(-4deg) scale(1); }
}

.end-card__title {
  color: var(--text-soft);
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.end-card__instruction {
  margin-top: 6px;
  font-size: 1.35rem;
  font-weight: 900;
  line-height: 1.25;
}

.end-card__details {
  margin-top: 10px;
  color: var(--text-soft);
  font-weight: 700;
}

.end-card__details strong {
  color: var(--text);
  font-weight: 900;
}

.end-card__separator {
  margin: 0 6px;
}

.stats {
  margin-top: 8px;
}

.stats__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 2px dashed var(--surface-muted);
}

.stats__row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.stats dt {
  color: var(--text-soft);
  font-weight: 700;
}

.stats dd {
  font-size: 1.15rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.stats__row--big dd {
  color: var(--red);
  font-family: var(--font-display);
  font-size: 2.4rem;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.04em;
}

.end__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 4px;
}

.end__actions .btn {
  min-height: 56px;
}

/* Share is the main action: it takes the whole first row */
.end__share {
  grid-column: 1 / -1;
  font-size: 1.2rem;
}

.end__share svg {
  width: 22px;
  height: 22px;
}


/* ---------- Share sheet ---------- */

.share-sheet__image {
  display: block;
  width: 100%;
  height: auto;
  margin-top: 14px;
  border: var(--outline-width) solid var(--line);
  border-radius: var(--radius-small);
  background: var(--paper);
}

.share-sheet__actions {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

/* For a moment after the end screen appears, its buttons ignore taps
   (so frantic tapping on the big button can't restart by accident) */
.end__actions:not(.is-armed) {
  pointer-events: none;
}


/* ---------- Pause dialog ---------- */

.pause {
  width: min(360px, calc(100% - 32px));
  text-align: center;
}

.pause .modal__inner {
  padding: 24px 20px 20px;
}

/* Dark curtain that hides the question while paused */
.pause::backdrop {
  background: rgba(24, 22, 28, 0.94);
}

.pause__title {
  font-family: var(--font-display);
  font-size: 2.4rem;
  font-weight: 400;
  line-height: 1;
  letter-spacing: 0.04em;
}

.pause__message {
  margin-top: 8px;
  color: var(--text-soft);
  font-size: 1.05rem;
  font-weight: 700;
}

.pause__actions {
  display: grid;
  gap: 12px;
  margin-top: 20px;
}


/* ---------- Short screens (e.g. iPhone SE in landscape-ish sizes) ---------- */

@media (max-height: 600px) {
  .level__number {
    font-size: 2.3rem;
  }

  .instruction {
    min-height: 88px;
    margin-top: 14px;
  }
}


/* ---------- Reduced motion: no shakes, no bursts ---------- */

@media (prefers-reduced-motion: reduce) {
  .burst,
  .game.is-quaking,
  .level__number.is-bumping,
  .counter.is-bumping,
  .instruction__text.is-new,
  .new-best,
  .end__inner {
    animation: none;
  }

  .burst {
    display: none;
  }
}
