* {
  box-sizing: border-box;
}

/* Theme colors. The coin itself stays gold in both themes. */
:root,
:root[data-theme="light"] {
  color-scheme: light;
  --background: radial-gradient(circle at top, #fffaf0, #eee2c8);
  --text: #1f2233;
  --accent: #946300;
  --control-border: rgba(31, 34, 51, 0.3);
  --control-hover: rgba(31, 34, 51, 0.08);
  --selected-background: #946300;
  --selected-text: #ffffff;
  --coin-shadow: rgba(80, 55, 0, 0.3);
  --focus-ring: #2f6fed;
  --surface: #ffffff;
  --muted: #5a5f73;
  --track: rgba(31, 34, 51, 0.12);
  --danger: #c62828;
  --danger-text: #ffffff;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --background: radial-gradient(circle at top, #2b2f4a, #14162a);
  --text: #f5f5f5;
  --accent: #ffd54a;
  --control-border: rgba(255, 255, 255, 0.35);
  --control-hover: rgba(255, 255, 255, 0.1);
  --selected-background: #ffd54a;
  --selected-text: #14162a;
  --coin-shadow: rgba(0, 0, 0, 0.45);
  --focus-ring: #8ab4ff;
  --surface: #23263f;
  --muted: #b8bccf;
  --track: rgba(255, 255, 255, 0.15);
  --danger: #ff6b6b;
  --danger-text: #14162a;
}

html {
  /* The page scrolls here, which lets body clip the coin when it grows past the window edges. */
  overflow: auto;
  /* Fast taps on iPhone would trigger Safari's double-tap zoom. This turns that off; pinch-zoom still works. */
  touch-action: manipulation;
}

body {
  margin: 0;
  /* "hidden" is a fallback for older Safari (before iOS 16), which doesn't support "clip". */
  overflow: hidden;
  overflow: clip;
  min-height: 100vh;
  /* Top padding keeps the game clear of the toolbar on short screens. */
  padding: 64px 16px 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--background);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  /* No "Copy / Look Up" pop-up on iPhone when a tap is held. */
  -webkit-touch-callout: none;
}

.game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  text-align: center;
}

h1 {
  margin: 0;
  font-size: 2.5rem;
  letter-spacing: 1px;
}

.score {
  margin: 0;
  /* A little smaller on phones, so "Score: 100,000,000 ×1000" stays on one line. */
  font-size: clamp(1.3rem, 6vw, 1.75rem);
}

#score {
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

/* The goal bonus next to the score, e.g. "×5". */
.multiplier {
  display: inline-block;
  margin-left: 4px;
  padding: 2px 10px;
  font-size: 1rem;
  font-weight: 800;
  vertical-align: middle;
  color: var(--selected-text);
  background: var(--selected-background);
  border-radius: 999px;
}

/* display: inline-block above would otherwise override the hidden attribute. */
.multiplier[hidden] {
  display: none;
}

/* Holds the coin at normal speeds; at very high speeds it grows past this and covers the title. */
.coin-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 280px;
  height: 280px;
}

.coin {
  /* --speed-scale is set by script.js from the click speed; --press-scale is hover/press feedback. */
  --speed-scale: 1;
  --press-scale: 1;
  position: relative;
  width: 120px;
  height: 120px;
  border: 5px solid #c79100;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff3b0, #ffd54a 45%, #e0a800);
  box-shadow: 0 6px 18px var(--coin-shadow), inset 0 -4px 8px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transform: scale(calc(var(--speed-scale) * var(--press-scale)));
  transition: transform 120ms ease-out;
  -webkit-tap-highlight-color: transparent;
}

.coin:hover {
  --press-scale: 1.04;
}

.coin:active {
  --press-scale: 0.94;
}

.coin:focus-visible,
.toolbar-button:focus-visible,
.reset:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
}

.coin:focus-visible {
  outline-offset: 4px;
}

.coin-face {
  font-size: 3rem;
  font-weight: 800;
  color: #a87600;
  text-shadow: 0 2px 0 #fff3b0;
  pointer-events: none;
}

/* "+1" that floats up from the click position */
.floating-plus {
  position: fixed;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  pointer-events: none;
  animation: float-up 700ms ease-out forwards;
}

@keyframes float-up {
  from {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
  to {
    opacity: 0;
    transform: translate(-50%, -150%);
  }
}

.toolbar {
  position: fixed;
  /* Stay above the coin when it grows huge. */
  z-index: 10;
  top: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.language-switcher {
  display: flex;
  gap: 4px;
}

.toolbar-button,
.reset {
  color: var(--text);
  background: transparent;
  border: 1px solid var(--control-border);
  border-radius: 8px;
  cursor: pointer;
}

.toolbar-button:hover,
.reset:hover {
  background: var(--control-hover);
}

.toolbar-button {
  height: 32px;
  padding: 0 12px;
  font-size: 0.9rem;
  font-weight: 600;
}

.language-option[aria-pressed="true"] {
  color: var(--selected-text);
  background: var(--selected-background);
  border-color: var(--selected-background);
}

/* Square toolbar buttons with an icon (sound, theme). */
.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  padding: 0;
}

:root[data-theme="light"] .icon-sun,
:root[data-theme="dark"] .icon-moon,
.sound-toggle[data-sound="on"] .icon-sound-off,
.sound-toggle[data-sound="off"] .icon-sound-on {
  display: none;
}

.reset {
  padding: 8px 20px;
  font-size: 1rem;
}

/* Progress toward the score goals */
.progress {
  display: flex;
  flex-direction: column;
  gap: 8px;
  /* Wide enough for the longest line ("Следующая цель: 5 000 · осталось 4 000") on one line. */
  width: 340px;
  max-width: 100%;
}

.progress-text {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.progress-track {
  display: flex;
  gap: 6px;
}

/* One segment per goal: a bar, with the goal's label under its right end. */
.progress-segment {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  /* Never narrower than its own label, so neighbouring labels can't overlap, however many goals there are. */
  min-width: max-content;
}

.progress-bar {
  height: 10px;
  border-radius: 999px;
  background: var(--track);
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #ffd54a, #e0a800);
  transition: width 200ms ease-out;
}

.progress-marker {
  align-self: flex-end;
  margin-top: 6px;
  font-size: 0.75rem;
  line-height: 1.25;
  color: var(--muted);
  /* "pre" keeps the line break between medal and number, and never wraps "100 тыс." */
  white-space: pre;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.progress-marker.reached {
  color: var(--accent);
  font-weight: 700;
}

/* Goal celebration message */
.milestone-layer {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  /* Above the coin and toolbar, but never blocks clicks on the coin. */
  z-index: 20;
  pointer-events: none;
}

.milestone-card {
  display: grid;
  justify-items: center;
  gap: 4px;
  min-width: 240px;
  max-width: calc(100vw - 32px);
  padding: 16px 28px;
  text-align: center;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  animation: milestone-pop 450ms cubic-bezier(0.2, 1.4, 0.4, 1) both;
}

.milestone-card.is-leaving {
  animation: milestone-leave 300ms ease-in forwards;
}

.milestone-icon {
  font-size: 2.75rem;
  line-height: 1.1;
}

.milestone-title {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--text);
}

.milestone-text {
  font-size: 1rem;
  color: var(--muted);
}

/* display: inline-block below would otherwise override the hidden attribute. */
.milestone-bonus[hidden] {
  display: none;
}

.milestone-bonus {
  display: inline-block;
  margin-top: 6px;
  padding: 2px 12px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--selected-text);
  background: var(--selected-background);
  border-radius: 999px;
}

@keyframes milestone-pop {
  from {
    opacity: 0;
    transform: scale(0.5) rotate(-6deg);
  }
  to {
    opacity: 1;
    transform: scale(1) rotate(0);
  }
}

@keyframes milestone-leave {
  to {
    opacity: 0;
    transform: translateY(-16px) scale(0.95);
  }
}

.confetti {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 12px;
  border-radius: 2px;
  animation: confetti-burst 1200ms ease-out forwards;
}

/* --dx, --dy and --spin are set per piece by script.js. */
@keyframes confetti-burst {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) rotate(0);
  }
  60% {
    opacity: 1;
    transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) rotate(var(--spin));
  }
  100% {
    opacity: 0;
    /* Drift down a little at the end, like it's falling. */
    transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy) + 80px)) rotate(var(--spin));
  }
}

/* "You beat the game!" screen */
.victory {
  width: min(420px, calc(100vw - 32px));
  padding: 32px 28px 28px;
  text-align: center;
  color: var(--text);
  background: var(--surface);
  border: 3px solid var(--accent);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  /* Let the confetti fly outside the box. */
  overflow: visible;
}

.victory[open] {
  animation: milestone-pop 600ms cubic-bezier(0.2, 1.4, 0.4, 1) both;
}

.victory::backdrop,
.confirm-dialog::backdrop,
.rest-dialog::backdrop {
  background: rgba(10, 12, 30, 0.6);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}

.victory-icon {
  font-size: 4.5rem;
  line-height: 1;
  animation: victory-bounce 1400ms ease-in-out infinite;
}

.victory-title {
  margin: 14px 0 8px;
  /* Shrinks on phones so "Вы прошли игру!" stays on one line. */
  font-size: clamp(1.5rem, 7vw, 2rem);
  font-weight: 900;
  line-height: 1.15;
  color: var(--accent);
}

.victory-text {
  margin: 0 0 6px;
  font-size: 1.05rem;
  color: var(--muted);
}

.victory-button {
  margin-top: 22px;
  padding: 12px 28px;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--selected-text);
  background: var(--selected-background);
  border: 0;
  border-radius: 12px;
  cursor: pointer;
  transition: opacity 200ms ease-out;
}

.victory-button:disabled {
  opacity: 0.4;
  cursor: default;
}

.victory-button:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
}

/* 10,000,000 ("You beat it twice!"): a moving rainbow border and a golden glow. */
.victory.is-legendary {
  border-color: transparent;
  background:
    linear-gradient(var(--surface), var(--surface)) padding-box,
    linear-gradient(90deg, #ff7043, #ffd54a, #66bb6a, #42a5f5, #ab47bc, #ff7043) border-box;
  background-size: 100% 100%, 300% 100%;
  box-shadow: 0 0 40px rgba(255, 213, 74, 0.55), 0 20px 60px rgba(0, 0, 0, 0.45);
}

.victory.is-legendary[open] {
  animation:
    milestone-pop 600ms cubic-bezier(0.2, 1.4, 0.4, 1) both,
    legendary-border 3s linear infinite;
}

@keyframes legendary-border {
  to {
    background-position: 0 0, 300% 0;
  }
}

/* 100,000,000: the ending. The whole window becomes a night sky with falling coins. */
.victory.is-ultimate {
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 24px;
  color: #efeaff;
  background: radial-gradient(circle at 50% 35%, #3b2a6b 0%, #1a1440 45%, #07061a 100%);
  border: 0;
  border-radius: 0;
  box-shadow: none;
  /* Keep the falling coins inside the window. */
  overflow: hidden;
}

.victory.is-ultimate[open] {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  animation: rest-fade-in 800ms ease-out both;
}

/* Twinkling stars */
.victory.is-ultimate::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(2px 2px at 8% 12%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 22% 70%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 35% 28%, #fff3b0 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 48% 88%, #fff 50%, transparent 51%),
    radial-gradient(2.5px 2.5px at 62% 16%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 76% 58%, #fff3b0 50%, transparent 51%),
    radial-gradient(2px 2px at 88% 30%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 93% 82%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 14% 46%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 56% 50%, #fff 50%, transparent 51%);
  animation: ultimate-twinkle 2.5s ease-in-out infinite alternate;
}

/* Keep the text above the stars. */
.victory.is-ultimate .victory-kicker,
.victory.is-ultimate .victory-icon,
.victory.is-ultimate .victory-title,
.victory.is-ultimate .victory-text,
.victory.is-ultimate .victory-actions {
  position: relative;
}

.victory-kicker {
  margin: 0 0 8px;
  font-size: 0.95rem;
  font-weight: 800;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: #ffd54a;
}

.victory.is-ultimate .victory-icon {
  font-size: 6rem;
}

.victory.is-ultimate .victory-title {
  font-size: clamp(2rem, 9vw, 3.5rem);
  /* Shining gold letters */
  background: linear-gradient(90deg, #ffb300, #fff3b0, #ffd54a, #ffb300);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.victory.is-ultimate .victory-text {
  max-width: 480px;
  font-size: 1.15rem;
  color: #e3dcff;
}

.victory.is-ultimate .victory-button {
  color: #1a1440;
  background: #ffd54a;
}

/* The ending reveals itself one line at a time. */
.victory.is-ultimate[open] .victory-kicker {
  animation: ultimate-rise 700ms ease-out both;
}

.victory.is-ultimate[open] .victory-icon {
  animation:
    ultimate-rise 800ms 300ms ease-out both,
    victory-bounce 1400ms 1100ms ease-in-out infinite;
}

.victory.is-ultimate[open] .victory-title {
  animation:
    ultimate-rise 800ms 700ms ease-out both,
    ultimate-shine 3s 1500ms linear infinite;
}

.victory.is-ultimate[open] .victory-text {
  animation: ultimate-rise 800ms 1100ms ease-out both;
}

.victory.is-ultimate[open] .victory-actions {
  animation: ultimate-rise 800ms 1500ms ease-out both;
}

/* A small gold coin, drawn like the game's coin (script.js sets each one's size and position). */
.coin-rain {
  position: absolute;
  top: -3rem;
  background: radial-gradient(circle at 35% 30%, #fff3b0, #ffd54a 45%, #e0a800);
  border: 2px solid #c79100;
  border-radius: 50%;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  animation-name: coin-fall;
  animation-timing-function: linear;
  animation-fill-mode: both;
}

@keyframes ultimate-rise {
  from {
    opacity: 0;
    transform: translateY(24px) scale(0.92);
  }
}

@keyframes ultimate-shine {
  to {
    background-position: -200% 0;
  }
}

@keyframes ultimate-twinkle {
  from {
    opacity: 0.35;
  }
  to {
    opacity: 1;
  }
}

@keyframes coin-fall {
  to {
    /* rotateY spins the coin like a flipped coin while it falls. */
    transform: translateY(calc(100vh + 6rem)) rotateY(1080deg);
  }
}

@keyframes victory-bounce {
  0%,
  100% {
    transform: translateY(0) rotate(0);
  }
  50% {
    transform: translateY(-10px) rotate(-8deg);
  }
}

/* "Slow down, speed demon!" screen. It fills the whole window, so clicks land on it instead of the game. */
.rest-dialog {
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 16px;
  color: var(--text);
  background: transparent;
  border: 0;
  overflow: hidden;
}

.rest-dialog[open] {
  display: flex;
  align-items: center;
  justify-content: center;
  animation: rest-fade-in 200ms ease-out both;
}

.rest-card {
  width: min(340px, 100%);
  padding: 24px;
  text-align: center;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.rest-card.is-shaking {
  animation: rest-shake 400ms ease-in-out;
}

.rest-icon {
  font-size: 3.5rem;
  line-height: 1;
}

.rest-title {
  margin: 10px 0 8px;
  font-size: 1.5rem;
  font-weight: 900;
  line-height: 1.15;
  color: var(--accent);
}

.rest-text {
  margin: 0;
  line-height: 1.4;
  color: var(--muted);
}

/* script.js places it at a random spot away from where the clicks were landing. */
.rest-button {
  position: absolute;
  top: 16px;
  left: 16px;
  padding: 12px 20px;
  font-size: 1.05rem;
  font-weight: 700;
  white-space: nowrap;
  color: var(--selected-text);
  background: var(--selected-background);
  border: 0;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.rest-button:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
}

@keyframes rest-fade-in {
  from {
    opacity: 0;
  }
}

@keyframes rest-shake {
  20% {
    transform: translateX(-10px) rotate(-2deg);
  }
  40% {
    transform: translateX(10px) rotate(2deg);
  }
  60% {
    transform: translateX(-6px);
  }
  80% {
    transform: translateX(6px);
  }
}

/* "Reset your progress?" confirmation */
.confirm-dialog {
  width: min(360px, calc(100vw - 32px));
  padding: 24px;
  text-align: center;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--control-border);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

.confirm-dialog[open] {
  animation: dialog-in 180ms ease-out both;
}

.confirm-title {
  margin: 0 0 8px;
  font-size: 1.35rem;
  font-weight: 800;
}

.confirm-text {
  margin: 0 0 20px;
  line-height: 1.4;
  color: var(--muted);
}

.confirm-actions {
  display: flex;
  gap: 12px;
}

.confirm-actions button {
  flex: 1;
  padding: 10px 16px;
  font-size: 1rem;
  font-weight: 700;
  border-radius: 10px;
  cursor: pointer;
}

.confirm-actions button:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
}

.confirm-cancel {
  color: var(--text);
  background: transparent;
  border: 1px solid var(--control-border);
}

.confirm-cancel:hover {
  background: var(--control-hover);
}

/* Red, so the button that erases progress stands out from Cancel. */
.confirm-reset {
  color: var(--danger-text);
  background: var(--danger);
  border: 1px solid var(--danger);
}

.confirm-reset:hover {
  filter: brightness(1.08);
}

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
}

/* For people who turn off animations: just fade the message in and out (script.js skips the confetti). */
@media (prefers-reduced-motion: reduce) {
  .confirm-dialog[open] {
    animation-name: milestone-fade-in;
  }

  .victory[open] {
    animation-name: milestone-fade-in;
  }

  .victory-icon {
    animation: none;
  }

  .victory.is-legendary[open] {
    animation: milestone-fade-in 600ms both;
  }

  .rest-card.is-shaking {
    animation: none;
  }

  .victory.is-ultimate::before,
  .victory.is-ultimate[open] .victory-kicker,
  .victory.is-ultimate[open] .victory-icon,
  .victory.is-ultimate[open] .victory-title,
  .victory.is-ultimate[open] .victory-text,
  .victory.is-ultimate[open] .victory-actions {
    animation: none;
  }

  .milestone-card {
    animation-name: milestone-fade-in;
  }

  .milestone-card.is-leaving {
    animation-name: milestone-fade-out;
  }

  @keyframes milestone-fade-in {
    from {
      opacity: 0;
    }
  }

  @keyframes milestone-fade-out {
    to {
      opacity: 0;
    }
  }
}
