:root {
  color-scheme: light;
  --paper: #fffdf8;
  --ink: #151515;
  --muted: #66625a;
  --line: #151515;
  --stripe-width: clamp(28px, 6vw, 72px);
  --race-finish-left: calc(100vw - var(--stripe-width) - 1.75em);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--paper);
}

body {
  min-height: 100vh;
  margin: 0;
  position: relative;
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  isolation: isolate;
  letter-spacing: 0;
  background: var(--paper);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  background-image: url("images/red_gingham_pattern_tile.png");
  background-size: 168px 170px;
  opacity: 0.1;
  filter: saturate(0.62) brightness(1.22);
  pointer-events: none;
}

button,
output {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.page-shell {
  min-height: 100vh;
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: center;
  padding: clamp(18px, 6vw, 54px);
}

.home-shell {
  align-items: center;
  overflow: hidden;
}

.home-shell::before,
.home-shell::after {
  content: "";
  width: var(--stripe-width);
  position: absolute;
  inset-block: 0;
  z-index: 0;
  background: rgba(255, 255, 255, 0.92);
  pointer-events: none;
}

.home-shell::before {
  left: 0;
}

.home-shell::after {
  right: 0;
}

.race {
  width: 100vw;
  position: absolute;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.racer {
  position: absolute;
  top: calc(var(--lane-y) - 0.5em);
  left: var(--stripe-width);
  display: inline-flex;
  align-items: center;
  gap: 0.18em;
  font-size: clamp(2.4rem, 7.6vw, 5.5rem);
  line-height: 1;
  filter: drop-shadow(0 5px 0 rgba(21, 21, 21, 0.12));
  transform: translate3d(0, 0, 0);
}

.racer.is-racing {
  animation: pig-race var(--duration) steps(2, end) forwards;
}

.pig-emoji {
  display: inline-block;
  transform: scaleX(-1);
}

.winner-flag {
  opacity: 0;
  transform: rotate(8deg);
}

.racer.is-racing .winner-flag {
  animation: flag-pop var(--duration) linear forwards;
}

.enter-button {
  width: min(70vw, 220px);
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  text-transform: none;
}

.watch {
  width: min(100%, 620px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(18px, 4vw, 32px);
}

.timer-shell {
  align-items: center;
}

.hero-image {
  width: auto;
  max-width: min(100%, 520px);
  max-height: clamp(260px, 44vh, 420px);
  height: auto;
  display: block;
}

.time-display {
  width: 100%;
  display: block;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  font-size: clamp(3.4rem, 16vw, 6.4rem);
  font-weight: 800;
  line-height: 1;
  text-align: center;
}

.visually-hidden {
  width: 1px;
  height: 1px;
  position: absolute;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.controls {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.control {
  min-width: 0;
  min-height: 58px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  transition:
    background 140ms ease,
    color 140ms ease;
}

.control:hover:not(:disabled),
.control:focus-visible {
  background: #fff;
  color: var(--ink);
}

.control:disabled {
  background: #fff;
  border-color: rgba(21, 21, 21, 0.28);
  color: rgba(21, 21, 21, 0.32);
  cursor: not-allowed;
}

.control.primary {
  background: #fff;
  color: var(--ink);
}

.control.primary.is-running {
  background: #fff;
  color: var(--ink);
}

.menu-button {
  width: 48px;
  height: 48px;
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 10;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 10px;
  border: 2px solid var(--line);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
}

.menu-button span {
  width: 100%;
  height: 2px;
  display: block;
  background: var(--ink);
}

.back-button {
  place-items: center;
  padding: 0;
  font-size: 2.4rem;
  font-weight: 800;
  line-height: 1;
}

.back-button span {
  width: auto;
  height: auto;
  background: transparent;
}

.save-status {
  min-height: 1.25rem;
  margin: -12px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
  text-align: center;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(255, 253, 248, 0.74);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  width: min(100%, 380px);
  border: 2px solid var(--line);
  background: #fff;
  display: grid;
  gap: 14px;
  padding: 22px;
}

.modal h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 900;
}

.modal-time {
  margin: 0;
  font-size: 2.4rem;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  text-align: center;
}

.field-label {
  font-size: 0.82rem;
  font-weight: 850;
}

#player-name {
  min-height: 48px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 750;
}

.modal-error {
  min-height: 1.2rem;
  margin: -4px 0 0;
  color: #9c1d1d;
  font-size: 0.86rem;
  font-weight: 800;
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.leaderboard-page {
  align-items: center;
}

.leaderboard-view {
  width: min(100%, 520px);
  display: grid;
  gap: 18px;
}

.leaderboard-view h1 {
  margin: 0;
  font-size: clamp(2.4rem, 12vw, 4.6rem);
  font-weight: 900;
  line-height: 1;
  text-align: center;
}

.leaderboard-timer-link {
  width: min(100%, 220px);
  justify-self: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.leaderboard-list {
  display: grid;
  gap: 10px;
  margin: 16px 0 0;
  padding: 0;
  list-style: none;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 10px;
  font-variant-numeric: tabular-nums;
}

.leaderboard-rank,
.leaderboard-time {
  font-weight: 850;
}

.leaderboard-name {
  overflow: hidden;
  color: var(--muted);
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-status {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.laps {
  width: 100%;
  max-height: 180px;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.laps li {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  align-items: baseline;
  gap: 12px;
  min-height: 34px;
  font-variant-numeric: tabular-nums;
}

.lap-number {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.lap-total {
  font-size: 1.02rem;
  font-weight: 850;
}

.lap-split {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

@media (max-width: 640px) {
  .timer-shell {
    padding-top: calc(82px + env(safe-area-inset-top));
  }
}

@media (max-width: 390px) {
  .page-shell {
    padding: 18px;
  }

  .timer-shell {
    padding-top: calc(82px + env(safe-area-inset-top));
  }

  .time-display {
    font-size: clamp(3rem, 17vw, 4.4rem);
  }

  .controls {
    gap: 8px;
  }
}

@keyframes pig-race {
  0% {
    left: var(--stripe-width);
    transform: translate3d(0, 0, 0) rotate(-3deg);
  }

  7% {
    left: calc(var(--stripe-width) + 4vw);
    transform: translate3d(0, -5px, 0) rotate(4deg);
  }

  12% {
    left: calc(var(--stripe-width) + 4vw);
    transform: translate3d(0, 1px, 0) rotate(-2deg);
  }

  21% {
    left: calc(var(--stripe-width) + 18vw);
    transform: translate3d(0, -4px, 0) rotate(5deg);
  }

  27% {
    left: calc(var(--stripe-width) + 20vw);
    transform: translate3d(0, 2px, 0) rotate(-4deg);
  }

  36% {
    left: calc(var(--stripe-width) + 34vw);
    transform: translate3d(0, -6px, 0) rotate(4deg);
  }

  43% {
    left: calc(var(--stripe-width) + 35vw);
    transform: translate3d(0, 3px, 0) rotate(-3deg);
  }

  54% {
    left: calc(var(--stripe-width) + 52vw);
    transform: translate3d(0, -4px, 0) rotate(5deg);
  }

  61% {
    left: calc(var(--stripe-width) + 52vw);
    transform: translate3d(0, 2px, 0) rotate(-4deg);
  }

  73% {
    left: calc(var(--stripe-width) + 68vw);
    transform: translate3d(0, -5px, 0) rotate(3deg);
  }

  82% {
    left: calc(var(--stripe-width) + 74vw);
    transform: translate3d(0, 3px, 0) rotate(-3deg);
  }

  100% {
    left: var(--race-finish-left);
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
}

@keyframes flag-pop {
  0%,
  84% {
    opacity: 0;
  }

  85%,
  100% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .racer,
  .winner-flag {
    animation: none;
  }

  .racer {
    transform: translateX(var(--resting-x));
  }

  .winner-flag {
    opacity: 1;
  }
}
