:root {
  --bg1: #fff4d6;
  --bg2: #ffe0ec;
  --ink: #3b2a4d;
  --ink-soft: #6d5a80;
  --card: #ffffff;
  --rust: #c9733a;
  --grey: #7b838b;
  --sun: #ffcf5c;
  --pink: #ff7eb6;
  --mint: #5fd3a5;
  --sky: #59b7ff;
  --bad: #ff3b5c;
  --line: #3b2a4d;
  --radius: 18px;
  --shadow: 0 4px 0 rgba(59, 42, 77, .18);
  font-family: 'Fredoka', 'Comic Sans MS', 'Chalkboard SE', system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

/* Stop iPhone Safari zooming in on a double-tap (pinch-zoom still works).
   touch-action applies to everything inside, so setting it here covers the page. */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body, body * { touch-action: manipulation; }
.board, .board * { touch-action: none; }

body {
  background-color: var(--bg1);
  background-image:
    radial-gradient(circle at 20% 10%, rgba(255,255,255,.7) 0 8%, transparent 9%),
    radial-gradient(circle at 85% 30%, rgba(255,255,255,.5) 0 6%, transparent 7%),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- Header ---------- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.title {
  margin: 0;
  display: flex; align-items: center; gap: 4px;
  font-size: clamp(1.15rem, 5.3vw, 2.2rem);
  min-width: 0;
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  text-shadow: 0 2px 0 rgba(59,42,77,.15);
}
.title span { display: inline-block; }
.logo-pet { width: 1.5em; height: 1.5em; flex: none; filter: drop-shadow(0 2px 0 rgba(0,0,0,.12)); }
.t-rugby { color: var(--rust); }
.t-biggie { color: var(--grey); }
.t-amp { color: var(--pink); transform: rotate(-10deg); }

.wiggle { animation: wiggle 2.4s ease-in-out infinite; }
.wiggle.delay { animation-delay: -1.2s; }
@keyframes wiggle {
  0%, 100% { transform: rotate(-6deg); }
  50% { transform: rotate(6deg) translateY(-2px); }
}

.bar-right { display: flex; gap: 5px; flex: none; }
.chip, .icon-btn {
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-weight: 600;
}
.chip {
  padding: 3px 10px;
  min-width: 0;
  max-width: 34%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: .95rem;
}
.icon-btn { width: 38px; height: 38px; font-size: 1.05rem; display: grid; place-items: center; padding: 0; }
.chip:active, .icon-btn:active, .tool:active, .btn:active, .hero-btn:active, .sizes button:active { transform: translateY(3px); box-shadow: none; }

/* ---------- Size + pet pickers ---------- */
.pickers { display: flex; gap: 8px; align-items: stretch; }
.sizes { flex: 1; display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.sizes button, .hero-btn {
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  line-height: 1.05;
  padding: 4px 0;
}
.sizes button b { display: block; font-size: 1rem; }
.sizes button small { display: none; font-size: .7rem; color: var(--ink-soft); }
.sizes button[aria-checked="true"] { background: var(--sun); }

.hero-btn {
  flex: none;
  width: 76px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: #ffd9a8;
  font-weight: 600;
}
.hero-btn[data-hero="biggie"] { background: #dfe4ea; }
.hero-btn[data-hero="mix"] { background: #ffd1e8; }
.hero-icon { display: flex; height: 26px; }
.hero-icon svg { width: 26px; height: 26px; }
.hero-icon svg + svg { margin-left: -7px; }
.hero-btn small { font-size: .72rem; }

/* ---------- Speech bubble ---------- */
.speech { display: flex; align-items: center; gap: 10px; height: 56px; }
.speaker { width: 44px; height: 44px; flex: none; }
.speaker.bounce { animation: bounce .5s ease; }
@keyframes bounce {
  0% { transform: scale(1); }
  30% { transform: scale(1.25) rotate(-8deg); }
  60% { transform: scale(.95) rotate(5deg); }
  100% { transform: scale(1); }
}
.bubble {
  position: relative;
  margin: 0;
  flex: 1;
  height: 100%;
  display: flex; align-items: center;
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: 16px;
  padding: 2px 10px;
  font-size: .9rem;
  line-height: 1.2;
  font-weight: 500;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.bubble::before {
  content: '';
  position: absolute;
  left: -12px; top: 50%;
  margin-top: -9px;
  border: 9px solid transparent;
  border-right-color: var(--line);
  border-left: 0;
}
.bubble.oops { background: #ffe3e8; }
.bubble.yay { background: #dcffe9; }

/* ---------- Status ---------- */
.status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ---------- Board ---------- */
.board-wrap { position: relative; width: 100%; margin: 0 auto; }
.board {
  --n: 5;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  width: 100%;
  aspect-ratio: 1;
  border: 4px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background: var(--line);
  box-shadow: 0 6px 0 rgba(59,42,77,.22);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-style: solid;
  border-color: var(--line);
  border-width: 0;
  cursor: pointer;
}
.cell::after {
  /* faint grid line between cells of the same colour */
  content: '';
  position: absolute; inset: 0;
  box-shadow: inset 0 0 0 .5px rgba(59,42,77,.18);
  pointer-events: none;
}
.cell svg { position: absolute; inset: 7%; width: 86%; height: 86%; pointer-events: none; }
.cell .paw { inset: 29%; width: 42%; height: 42%; fill: rgba(59,42,77,.55); }
.cell .paw.auto { fill: rgba(59,42,77,.22); }
.cell .pet { animation: pop .28s ease-out; filter: drop-shadow(0 2px 0 rgba(0,0,0,.15)); }
@keyframes pop {
  0% { transform: scale(.2) rotate(-25deg); }
  70% { transform: scale(1.15) rotate(6deg); }
  100% { transform: scale(1); }
}
.cell.bad { background-image: repeating-linear-gradient(45deg, rgba(255,59,92,.35) 0 6px, rgba(255,59,92,.15) 6px 12px); }
.cell.bad .pet { animation: shake .4s ease-in-out infinite; }
@keyframes shake {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-10deg); }
  75% { transform: rotate(10deg); }
}
.cell.stuck::before {
  /* the row/column/colour with no moves left */
  content: '';
  position: absolute; inset: 0;
  background: rgba(255,255,255,.55);
  box-shadow: inset 0 0 0 3px var(--bad);
  animation: stuckPulse .9s ease-in-out infinite alternate;
  pointer-events: none;
}
@keyframes stuckPulse {
  from { opacity: .35; }
  to { opacity: 1; }
}
.board.won .pet { animation: dance .7s ease-in-out infinite alternate; }
.board.won .cell:nth-child(odd) .pet { animation-delay: -.35s; }
@keyframes dance {
  from { transform: translateY(0) rotate(-8deg); }
  to { transform: translateY(-12%) rotate(8deg) scale(1.08); }
}
.board.won .cell { cursor: default; }

.loading {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(255,255,255,.85);
  border-radius: 14px;
  font-size: 1.3rem; font-weight: 600;
}
.hidden { display: none !important; }

/* ---------- Tools ---------- */
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: var(--radius);
  padding: 4px;
  box-shadow: var(--shadow);
  font-size: 1.15rem;
  line-height: 1.15;
  display: flex; flex-direction: column; align-items: center;
}
.tool span { font-size: .82rem; font-weight: 600; }
.tool.big { background: var(--mint); }
.tool:disabled { opacity: .45; cursor: default; }

.toggle {
  display: flex; align-items: center; gap: 10px;
  font-size: .95rem; color: var(--ink-soft);
  cursor: pointer;
}
.toggle input { width: 22px; height: 22px; accent-color: var(--pink); flex: none; }

/* ---------- Dialogs ---------- */
.dlg {
  border: 4px solid var(--line);
  border-radius: 24px;
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 8px 0 rgba(59,42,77,.25);
}
.dlg.wide { width: min(520px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(59,42,77,.45); }
.dlg h2 { margin: 0 0 12px; text-align: center; font-size: 1.6rem; }
.dlg p { line-height: 1.5; }
.rules { padding-left: 1.3em; line-height: 1.6; font-size: 1.05rem; }
.dlg-row { display: flex; justify-content: space-between; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.dlg form[method="dialog"] { margin: 0; text-align: center; }

.btn {
  background: #f1ecf6;
  border: 3px solid var(--line);
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.btn.primary { background: var(--sun); }
.btn.danger { background: #ffd6de; }

.players { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.player-row { display: flex; gap: 8px; }
.player-row .pick {
  flex: 1;
  text-align: left;
  background: #f7f3fb;
  border: 3px solid var(--line);
  border-radius: 14px;
  padding: 8px 12px;
  font-weight: 600;
}
.player-row .pick.on { background: var(--sun); }
.player-row .del { width: 44px; border: 3px solid var(--line); border-radius: 14px; background: #ffd6de; }
.add-player { display: flex; gap: 8px; margin-bottom: 14px; }
.add-player input {
  flex: 1; min-width: 0;
  font: inherit;
  border: 3px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
}

.tabs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-bottom: 10px; }
.tabs button {
  border: 3px solid var(--line);
  background: #f7f3fb;
  border-radius: 999px;
  padding: 4px 12px;
  font-weight: 600;
}
.tabs button.on { background: var(--sun); }
.score-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.score-card { background: #f7f3fb; border-radius: 14px; padding: 8px; text-align: center; }
.score-card b { display: block; font-size: 1.3rem; }
.score-card small { color: var(--ink-soft); }
.dlg h3 { margin: 12px 0 6px; font-size: 1.1rem; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: 38vh; overflow-y: auto; }
.list li {
  display: flex; align-items: center; gap: 8px;
  background: #f7f3fb; border-radius: 12px; padding: 6px 10px;
  font-variant-numeric: tabular-nums;
}
.list li svg { width: 26px; height: 26px; flex: none; }
.list .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .medal { width: 26px; text-align: center; }
.list .meta { color: var(--ink-soft); font-size: .85rem; }
.empty { text-align: center; color: var(--ink-soft); }

.win { text-align: center; }
.win-pets { display: flex; justify-content: center; }
.win-pets svg { width: 90px; height: 90px; animation: dance .6s ease-in-out infinite alternate; }
.win-pets svg + svg { animation-delay: -.3s; }
.win-msg { font-size: 1.15rem; font-weight: 500; margin: 6px 0; }
.stars { font-size: 2.2rem; letter-spacing: 4px; }
.win-stats { color: var(--ink-soft); margin: 4px 0; }
.new-best {
  display: inline-block;
  background: var(--pink); color: #fff;
  border-radius: 999px; padding: 2px 10px; font-weight: 600;
  animation: bounce 1s ease infinite;
}

/* ---------- Confetti ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 10; }
.confetti > * {
  position: absolute; top: -60px;
  animation: fall linear forwards;
}
.confetti svg { width: 100%; height: 100%; }
@keyframes fall {
  to { transform: translateY(110vh) rotate(var(--spin)); }
}

/* Roomier on tall screens (tablets, laptops). */
@media (min-height: 820px) and (min-width: 480px) {
  .app { gap: 12px; padding-top: 16px; }
  .sizes button small { display: block; }
  .sizes button { padding: 6px 0; }
  .hero-btn { width: 90px; }
  .hero-icon, .hero-icon svg { height: 34px; }
  .hero-icon svg { width: 34px; }
  .bubble { font-size: 1rem; }
  .speech { height: 64px; }
  .speaker { width: 54px; height: 54px; }
  .tool { padding: 8px 4px; font-size: 1.4rem; }
  .tool span { font-size: .9rem; }
}

/* Very narrow phones */
@media (max-width: 359px) {
  .bar-right { gap: 4px; }
  .icon-btn { width: 36px; height: 36px; font-size: 1rem; }
  .logo-pet.delay { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
