:root {
  --bg: #fbf4df;
  --card: #fffaf0;
  --ink: #332414;
  --muted: #77654f;
  --accent: #ef7f45;
  --accent-dark: #b94e22;
  --green: #5fa36a;
  --border: #ead9b9;
  --shadow: 0 18px 45px rgba(72, 45, 12, 0.14);
  --pillbug-image: url("assets/pillbug-cartoon-transparent.png");
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at top left, rgba(239, 127, 69, 0.22), transparent 32rem),
    radial-gradient(circle at bottom right, rgba(95, 163, 106, 0.18), transparent 30rem),
    var(--bg);
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }

.app-shell {
  width: min(1120px, calc(100% - 28px));
  margin: 0 auto;
  padding: 28px 0 48px;
}

.hero {
  display: flex;
  justify-content: space-between;
  gap: 32px;
  align-items: center;
  padding: 26px;
}

.hero-copy { flex: 1 1 620px; }

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent-dark);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

h1, h2, h3, p { margin-top: 0; }
h2 { margin-bottom: 12px; font-size: clamp(1.5rem, 3vw, 2rem); }
h3 { margin-bottom: 10px; }
.tagline { color: var(--muted); font-size: 1.08rem; margin: 16px 0 0; max-width: 52ch; }

.game-title {
  margin: 0 0 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: .14em;
  font-size: clamp(2.7rem, 8vw, 5.4rem);
  line-height: .95;
}

.title-word,
.title-hyphen {
  display: inline-flex;
  align-items: flex-end;
  gap: .08em;
}

.title-hyphen { padding: 0 .04em .04em; }

.pillbug-o {
  display: inline-block;
  width: .68em;
  height: .68em;
  margin: 0 .03em;
  transform: translateY(.035em);
  background: center / contain no-repeat var(--pillbug-image);
}

.hero-art {
  flex: 0 0 clamp(200px, 24vw, 290px);
  margin: 0;
  padding: 18px;
  border: 2px solid var(--border);
  border-radius: 28px;
  background: rgba(255,255,255,.72);
  box-shadow: var(--shadow);
  transform: rotate(-2deg);
}

.hero-art img {
  display: block;
  width: 100%;
  height: auto;
  filter: drop-shadow(0 10px 12px rgba(0,0,0,.08));
}

.card {
  background: rgba(255, 250, 240, .9);
  border: 2px solid var(--border);
  border-radius: 28px;
  box-shadow: var(--shadow);
  padding: 24px;
  backdrop-filter: blur(10px);
}

.hidden { display: none !important; }

.setup-panel { max-width: 620px; margin: 0 auto 24px; }
.player-count-row { display: flex; gap: 12px; align-items: center; margin: 18px 0; }
.player-count-row select { padding: 10px 14px; border-radius: 14px; border: 2px solid var(--border); background: white; }
.player-inputs { display: grid; gap: 10px; margin-bottom: 18px; }
.player-inputs input { width: 100%; padding: 12px 14px; border-radius: 14px; border: 2px solid var(--border); background: white; }

.primary-btn, .secondary-btn, .small-btn {
  border: 0;
  border-radius: 999px;
  font-weight: 800;
  box-shadow: 0 8px 18px rgba(72, 45, 12, 0.16);
}
.primary-btn { background: var(--accent); color: white; padding: 13px 22px; }
.secondary-btn { background: var(--ink); color: white; padding: 13px 22px; }
.small-btn { background: white; color: var(--ink); border: 2px solid var(--border); padding: 8px 12px; box-shadow: none; }

.game-grid { display: grid; grid-template-columns: 330px 1fr; gap: 22px; align-items: start; margin-bottom: 24px; }
.card-title-row, .turn-header { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.scoreboard { display: grid; gap: 10px; }
.player-score {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 18px;
  background: white;
}
.player-score.active { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(239,127,69,.15); }
.player-score .name { font-weight: 850; }
.player-score .score { font-size: 1.4rem; font-weight: 900; }
.notice { margin-top: 16px; padding: 12px; border-radius: 16px; background: #fff0d6; color: #76440f; font-weight: 700; }

.play-card { min-height: 460px; }
.roll-pill { white-space: nowrap; padding: 10px 14px; border-radius: 999px; background: white; border: 2px solid var(--border); font-weight: 900; }
.status-message { margin: 14px 0 20px; color: var(--muted); font-weight: 650; min-height: 28px; }
.dice-area { display: flex; flex-wrap: wrap; align-items: center; align-content: center; justify-content: center; gap: 20px; padding: 24px 0; min-height: 180px; }
.die {
  width: clamp(84px, 17vw, 132px);
  aspect-ratio: 1;
  flex: 0 0 auto;
  border: 4px solid var(--ink);
  border-radius: 24px;
  background: white;
  box-shadow: 0 12px 0 rgba(0,0,0,.08), 0 22px 34px rgba(72,45,12,.16);
  display: grid;
  grid-template-areas:
    "a b c"
    "d e f"
    "g h i";
  /* minmax(0,1fr) tracks can't grow past the box, so aspect-ratio (square) is
     honored even on narrow phones — otherwise auto rows force a tall rectangle. */
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  padding: clamp(10px, 3vw, 16px);
  transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.die:hover { transform: translateY(-3px); }
.die.kept { background: #eaf7df; box-shadow: 0 0 0 6px rgba(95,163,106,.18), 0 12px 0 rgba(0,0,0,.08); }
.pip {
  width: 60%;
  aspect-ratio: 1;
  max-width: 22px;
  align-self: center;
  justify-self: center;
  background: center / contain no-repeat var(--pillbug-image);
}
.pip.a { grid-area: a; } .pip.b { grid-area: b; } .pip.c { grid-area: c; }
.pip.d { grid-area: d; } .pip.e { grid-area: e; } .pip.f { grid-area: f; }
.pip.g { grid-area: g; } .pip.h { grid-area: h; } .pip.i { grid-area: i; }
.controls { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; }
.score-preview { margin-top: 20px; padding: 16px; border-radius: 18px; background: white; border: 2px solid var(--border); text-align: center; font-weight: 800; }
.score-preview strong { color: var(--accent-dark); font-size: 1.4rem; }

.rules-card { margin-top: 24px; }
.rules-card ul { padding-left: 22px; color: var(--muted); line-height: 1.6; }
.rules-table { display: grid; gap: 8px; }
.rules-table div { display: grid; grid-template-columns: 1fr auto; gap: 12px; padding: 10px 12px; background: white; border-radius: 14px; border: 1px solid var(--border); }
.rules-table span { font-weight: 900; color: var(--accent-dark); }
.winner-panel { margin: 0 auto 24px; max-width: 700px; text-align: center; }
.winner-panel h2 { font-size: clamp(2rem, 6vw, 4rem); animation: winnerBounce 1.1s ease-in-out infinite; }

.celebration-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(51, 36, 20, 0.28);
  backdrop-filter: blur(3px);
  z-index: 9999;
}

.celebration-card {
  position: relative;
  overflow: hidden;
  width: min(760px, calc(100vw - 32px));
  min-height: 420px;
  border-radius: 32px;
  border: 3px solid rgba(255,255,255,.5);
  background:
    radial-gradient(circle at top, rgba(255,255,255,.65), transparent 42%),
    linear-gradient(145deg, #fff7dc, #ffe0bf 55%, #ffd0a4);
  box-shadow: 0 26px 80px rgba(0,0,0,.24);
  color: var(--ink);
  text-align: center;
  padding: 34px 28px 28px;
}

.celebration-card.winner-mode {
  background:
    radial-gradient(circle at top, rgba(255,255,255,.7), transparent 42%),
    linear-gradient(145deg, #fff4d2, #ffd58c 55%, #ffb46d);
}

.confetti-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.celebration-kicker {
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: .18em;
  font-weight: 900;
  font-size: .78rem;
  color: var(--accent-dark);
}

.celebration-title {
  font-size: clamp(2.1rem, 5vw, 4rem);
  margin: 0;
  line-height: 1;
}

.celebration-subtitle {
  color: var(--muted);
  font-size: 1.1rem;
  margin: 12px 0 22px;
}

.celebration-stage {
  position: relative;
  min-height: 210px;
  display: grid;
  place-items: center;
}

.bug-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: clamp(16px, 3vw, 28px);
}

.celebration-bug,
.winner-bug {
  background: center / contain no-repeat var(--pillbug-image);
  filter: drop-shadow(0 10px 18px rgba(0,0,0,.14));
}

.celebration-bug {
  width: clamp(92px, 18vw, 130px);
  aspect-ratio: 1;
  animation: bugSpin 1s linear infinite;
}

.burst-phase .celebration-bug {
  animation: bugExplode 880ms ease-out forwards;
}
.burst-phase .celebration-bug:nth-child(1) { --tx: -120px; --ty: -36px; }
.burst-phase .celebration-bug:nth-child(2) { --tx: 0px; --ty: -118px; }
.burst-phase .celebration-bug:nth-child(3) { --tx: 120px; --ty: -36px; }

.winner-stage {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
}

.winner-bug {
  width: min(210px, 48vw);
  aspect-ratio: 1;
  animation: winnerSpin 1.6s ease-in-out infinite;
}

.winner-crown {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(2.2rem, 5vw, 3.4rem);
  animation: crownBob .85s ease-in-out infinite alternate;
}

.sparkles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.sparkle {
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,.95), rgba(255,255,255,0));
  animation: sparklePulse 1.2s ease-in-out infinite;
}

.sparkle:nth-child(1) { top: 28px; left: 24%; }
.sparkle:nth-child(2) { top: 58px; right: 24%; animation-delay: .2s; }
.sparkle:nth-child(3) { bottom: 54px; left: 20%; animation-delay: .4s; }
.sparkle:nth-child(4) { bottom: 24px; right: 20%; animation-delay: .6s; }

.confetti {
  position: absolute;
  width: var(--size, 12px);
  height: calc(var(--size, 12px) * 1.8);
  left: 50%;
  top: 50%;
  opacity: 0;
  border-radius: 4px;
  background: var(--color, #ef7f45);
}

.confetti.burst {
  animation: confettiBurst 900ms ease-out forwards;
}

.confetti.rain {
  left: var(--left, 50%);
  top: -10%;
  opacity: 1;
  animation: confettiRain 2600ms linear forwards;
  animation-delay: var(--delay, 0ms);
}

@keyframes bugSpin {
  from { transform: rotate(0deg) scale(1); }
  50% { transform: rotate(180deg) scale(1.08); }
  to { transform: rotate(360deg) scale(1); }
}

@keyframes bugExplode {
  0% { transform: translate(0,0) rotate(0deg) scale(1); opacity: 1; }
  100% { transform: translate(var(--tx), var(--ty)) rotate(720deg) scale(1.8); opacity: 0; }
}

@keyframes confettiBurst {
  0% { opacity: 1; transform: translate(-50%, -50%) rotate(0deg) scale(.5); }
  100% { opacity: 1; transform: translate(calc(-50% + var(--tx)), calc(-50% + var(--ty))) rotate(var(--rot)) scale(1.15); }
}

@keyframes confettiRain {
  0% { transform: translate3d(0, -10vh, 0) rotate(0deg); }
  100% { transform: translate3d(var(--drift, 0px), 118vh, 0) rotate(var(--rot)); }
}

@keyframes winnerSpin {
  0% { transform: rotate(-8deg) scale(1); }
  50% { transform: rotate(8deg) scale(1.08); }
  100% { transform: rotate(-8deg) scale(1); }
}

@keyframes crownBob {
  from { transform: translateX(-50%) translateY(0); }
  to { transform: translateX(-50%) translateY(-8px); }
}

@keyframes sparklePulse {
  0%, 100% { transform: scale(.5); opacity: .4; }
  50% { transform: scale(1.3); opacity: 1; }
}

@keyframes winnerBounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@media (max-width: 820px) {
  .hero {
    align-items: flex-start;
    flex-direction: column;
  }

  .hero-art {
    width: min(100%, 240px);
    align-self: flex-end;
  }

  .game-grid { grid-template-columns: 1fr; }
  .card-title-row, .turn-header { align-items: flex-start; }
  .celebration-card { min-height: 380px; padding-inline: 18px; }
  .winner-crown { top: 12px; }
}
