:root {
  --die-size: 5.5rem;
  --dot-size: 0.95rem;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  font-family: var(--font-display);
  color: var(--ink);
  background: var(--paper);
  font-variant-numeric: tabular-nums;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 0;
}

.app {
  width: 100%;
  max-width: 64rem;
  margin: 0 auto;
  padding: 1rem 1.25rem 3rem;
  flex: 1;
}

.screen {
  text-align: center;
  padding: 2.25rem 1.75rem;
  background: var(--panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  position: relative;
  min-height: 34rem;
}

.hidden {
  display: none !important;
}

h1 {
  font-family: var(--font-display);
  font-size: clamp(2rem, 3vw + 1rem, 2.75rem);
  font-weight: 800;
  color: var(--ink-strong);
  margin: 0 0 0.5rem;
  letter-spacing: -0.015em;
}

.tagline {
  font-family: var(--font-display);
  color: var(--ink-soft);
  margin: 0 0 2rem;
  font-size: 1.125rem;
  font-weight: 400;
}

/* -- Setup screen -- */

.setup-group {
  border: none;
  margin: 0 auto 1.5rem;
  padding: 0;
  max-width: 28rem;
}

.setup-group legend {
  font-family: var(--font-display);
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--ink-soft);
  margin-bottom: 0.6rem;
}

.segmented {
  display: inline-flex;
  gap: 0.25rem;
  padding: 0.3rem;
  background: var(--panel-muted);
  border-radius: var(--radius-md);
  border: 2px solid var(--edge);
}

.segmented input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.segmented label {
  font-family: var(--font-display);
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease;
  user-select: none;
}

.segmented input:checked + label {
  background: var(--accent);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}

.segmented input:focus-visible + label {
  outline: 3px solid var(--coral);
  outline-offset: 3px;
}

/* -- Gameplay screen -- */

.gameplay-content {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.game-header {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.header-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  flex-wrap: wrap;
}

.player-name {
  font-family: var(--font-display);
  font-size: clamp(1.25rem, 1.5vw + 0.75rem, 1.75rem);
  font-weight: 800;
  color: var(--ink-strong);
}

.turn-tracker {
  font-family: var(--font-display);
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  background: var(--panel-muted);
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--edge);
}

.score-strip {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.score-pill {
  font-family: var(--font-display);
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-sm);
  background: var(--panel-muted);
  border: 2px solid var(--edge);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  display: inline-flex;
  gap: 0.5rem;
  align-items: baseline;
}

.score-pill-score {
  font-weight: 800;
  color: var(--ink);
}

.score-pill.active {
  background: var(--mint-soft);
  border-color: var(--mint-dark);
  color: var(--mint-dark);
}

.score-pill.active .score-pill-score {
  color: var(--mint-dark);
}

.current-score-line {
  font-family: var(--font-display);
  font-size: 1.125rem;
  color: var(--ink-soft);
  margin: 0.25rem 0 1rem;
}

.current-score-line #current-score {
  font-weight: 800;
  color: var(--ink-strong);
  font-size: 1.5rem;
  margin-left: 0.25rem;
}

.dice-arena {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  align-items: center;
  min-height: calc(var(--die-size) + 1rem);
  margin: 0.25rem 0 1rem;
  padding: 1rem;
  background: var(--panel-muted);
  border: 2px dashed var(--edge-strong);
  border-radius: var(--radius-lg);
}

.die {
  width: var(--die-size);
  height: var(--die-size);
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 0.55rem;
  gap: 0.2rem;
  cursor: pointer;
  transition:
    transform 140ms ease,
    border-color 140ms ease,
    box-shadow 140ms ease;
  font: inherit;
}

.die:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.die:focus-visible {
  outline: 3px solid var(--coral);
  outline-offset: 3px;
}

.die.active-train {
  border-color: var(--coral);
  box-shadow:
    0 0 0 4px var(--coral-soft),
    var(--shadow-lg);
  transform: translateY(-3px);
  animation: pulse 1.8s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    box-shadow:
      0 0 0 4px var(--coral-soft),
      var(--shadow-lg);
  }
  50% {
    box-shadow:
      0 0 0 8px var(--coral-soft),
      var(--shadow-lg);
  }
}

.die.rolling {
  animation: shake 360ms ease-in-out;
}

@keyframes shake {
  0% {
    transform: rotate(0deg) translateY(0);
  }
  20% {
    transform: rotate(-8deg) translateY(-4px);
  }
  40% {
    transform: rotate(6deg) translateY(-2px);
  }
  60% {
    transform: rotate(-4deg) translateY(-3px);
  }
  80% {
    transform: rotate(3deg) translateY(-1px);
  }
  100% {
    transform: rotate(0deg) translateY(0);
  }
}

.dot {
  width: var(--dot-size);
  height: var(--dot-size);
  background: var(--ink);
  border-radius: 50%;
  align-self: center;
  justify-self: center;
}

.dot-1 { grid-column: 1; grid-row: 1; }
.dot-2 { grid-column: 2; grid-row: 1; }
.dot-3 { grid-column: 3; grid-row: 1; }
.dot-4 { grid-column: 1; grid-row: 2; }
.dot-5 { grid-column: 2; grid-row: 2; }
.dot-6 { grid-column: 3; grid-row: 2; }
.dot-7 { grid-column: 1; grid-row: 3; }
.dot-8 { grid-column: 2; grid-row: 3; }
.dot-9 { grid-column: 3; grid-row: 3; }

.readout {
  font-family: var(--font-display);
  font-size: 1.125rem;
  font-weight: 500;
  color: var(--ink);
  min-height: 3.25rem;
  margin: 0 auto 1.25rem;
  max-width: 36rem;
  padding: 0.9rem 1.25rem;
  background: var(--pink-soft);
  border: 2px solid var(--pink);
  border-radius: var(--radius-md);
  transition: background-color 160ms ease, color 160ms ease, border-color 160ms ease;
}

.readout.scored {
  background: var(--mint-soft);
  border-color: var(--mint-dark);
  color: var(--mint-dark);
  font-weight: 700;
}

/* -- Buttons -- */

.primary {
  font: inherit;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.9rem 2rem;
  border-radius: var(--radius-md);
  border: 2px solid var(--ink);
  background: var(--accent);
  color: var(--ink);
  cursor: pointer;
  transition:
    background-color 120ms ease,
    transform 80ms ease,
    opacity 120ms ease;
}

.primary:hover:not(:disabled) {
  background: var(--accent-dark);
}

.primary:active:not(:disabled) {
  transform: scale(0.97);
}

.primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.primary:focus-visible {
  outline: 3px solid var(--coral);
  outline-offset: 3px;
}

.secondary {
  font: inherit;
  font-family: var(--font-display);
  font-size: 1rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.9rem 2rem;
  border-radius: var(--radius-md);
  border: 2px solid var(--ink);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  transition:
    background-color 120ms ease,
    transform 80ms ease;
}

.secondary:hover {
  background: var(--accent-soft);
}

.secondary:active {
  transform: scale(0.97);
}

.secondary:focus-visible {
  outline: 3px solid var(--coral);
  outline-offset: 3px;
}

/* -- Pass overlay -- */

.pass-overlay {
  position: absolute;
  inset: 0;
  background: var(--pink);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  z-index: 10;
}

.pass-card {
  max-width: 28rem;
  text-align: center;
}

.pass-card h2 {
  font-family: var(--font-display);
  font-size: clamp(1.5rem, 2vw + 1rem, 2.25rem);
  font-weight: 800;
  color: var(--ink-strong);
  margin: 0 0 1rem;
}

.pass-card p {
  font-family: var(--font-display);
  font-size: 1.25rem;
  color: var(--ink);
  margin: 0 0 1.75rem;
}

/* -- Summary screen -- */

.leaderboard {
  width: 100%;
  max-width: 36rem;
  margin: 1rem auto 2rem;
  border-collapse: collapse;
  font-size: 1.125rem;
  font-family: var(--font-display);
}

.leaderboard th,
.leaderboard td {
  padding: 0.875rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--edge);
}

.leaderboard th {
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
  background: var(--panel-muted);
}

.leaderboard tbody tr:last-child td {
  border-bottom: none;
}

.leaderboard tbody tr.winner {
  background: var(--mint-soft);
}

.leaderboard tbody tr.winner td {
  font-weight: 800;
  color: var(--mint-dark);
}

.leaderboard td:last-child {
  text-align: right;
  font-weight: 800;
}

.summary-actions {
  display: flex;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
}

/* -- Responsive -- */

@media (max-width: 640px) {
  :root {
    --die-size: 4.25rem;
    --dot-size: 0.75rem;
  }

  body {
    padding: 0;
  }

  .app {
    padding: 0.5rem 0.75rem 2rem;
  }

  .screen {
    padding: 1.5rem 1rem;
    min-height: auto;
  }

  .header-row {
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
  }

  .dice-arena {
    padding: 0.75rem;
    gap: 0.5rem;
  }

  .summary-actions {
    flex-direction: column;
    align-items: stretch;
  }
}

/* -- Motion preferences -- */

@media (prefers-reduced-motion: reduce) {
  .die,
  .die:hover,
  .primary,
  .primary:active,
  .secondary,
  .secondary:active,
  .readout {
    transition: none;
  }

  .die.active-train,
  .die.rolling {
    animation: none;
  }

  .die:hover:not(:disabled),
  .die.active-train {
    transform: none;
  }
}
