:root {
  --bg: #f5f1e7;
  --bg-2: #efe9da;
  --card: #ffffff;
  --ink: #5b544c;
  --ink-soft: #8d857a;
  --accent: #9b82d6;
  --accent-soft: #efeaff;
  --chip-bg: #fbeae6;
  --chip-ink: #c07f73;
  --heart: #ec6a83;
  --heart-empty: #e4ddcf;
  --shadow: 0 12px 30px rgba(95, 80, 60, 0.1);
  --shadow-sm: 0 4px 12px rgba(95, 80, 60, 0.1);
  --cell-radius: 13px;
  --gap: 8px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

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

body {
  font-family: "Nunito", "Quicksand", -apple-system, BlinkMacSystemFont, "Segoe UI",
    system-ui, sans-serif;
  background: radial-gradient(1200px 800px at 50% -10%, #fbf8f1, var(--bg)) fixed;
  color: var(--ink);
  min-height: 100vh;
  /* display:block (not flex) so the <cuppa-game-header> nav — which renders as
     display:contents — does not become a flex sibling beside the board. The app
     is centered with margin:auto instead. Header clearance via padding-top. */
  display: block;
  padding-top: 52px;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 18px 16px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

/* ---------- header ---------- */
.app-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 1.5rem;
  letter-spacing: -0.02em;
  color: #6f6457;
}
.brand-paw {
  width: 24px;
  height: 24px;
  color: var(--accent);
  transform: rotate(-12deg);
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.icon-btn,
.pill-btn {
  border: none;
  cursor: pointer;
  background: #fff;
  color: var(--accent);
  box-shadow: var(--shadow-sm);
  font-weight: 800;
  transition: transform 0.12s ease, background 0.15s ease;
}
.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.1rem;
}
.pill-btn {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 22px;
  font-size: 0.95rem;
}
.icon-btn:hover,
.pill-btn:hover {
  transform: translateY(-1px);
}
.icon-btn:active,
.pill-btn:active {
  transform: scale(0.94);
}
.pill-btn.active {
  background: var(--accent);
  color: #fff;
}

/* ---------- status row ---------- */
.status-row {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}
.progress-pill {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fff;
  border-radius: 18px;
  padding: 5px 12px 5px 8px;
  box-shadow: var(--shadow-sm);
  font-weight: 800;
  color: #6f6457;
}
.pill-cat {
  width: 24px;
  height: 24px;
}
.level-title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  text-align: center;
  white-space: nowrap;
}
.hearts {
  justify-self: end;
  display: inline-flex;
  gap: 3px;
}
.hearts svg {
  width: 24px;
  height: 24px;
  color: var(--heart);
  transition: transform 0.2s ease, color 0.2s ease;
}
.hearts svg.empty {
  color: var(--heart-empty);
}
.hearts svg.lost {
  animation: heart-break 0.5s ease;
}
@keyframes heart-break {
  0% {
    transform: scale(1.4);
  }
  40% {
    transform: scale(0.8) rotate(-12deg);
  }
  100% {
    transform: scale(1);
  }
}

/* ---------- rules ---------- */
.rules {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  width: 100%;
}
.rule-chip {
  background: var(--chip-bg);
  color: var(--chip-ink);
  font-size: 0.8rem;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 12px;
  text-align: center;
  flex: 1 1 auto;
  max-width: 175px;
}

/* ---------- board ---------- */
.board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
}
.board {
  --n: 4;
  width: min(92vw, 460px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: var(--gap);
  padding: 14px;
  background: var(--card);
  border-radius: 24px;
  box-shadow: var(--shadow);
}
/* 9x9/10x10: wider board, tighter gaps + radii so cells stay tappable on
   320px phones (10x10 at 320px ≈ 26px cells vs 22px without this) */
.board.dense {
  width: min(96vw, 500px);
  gap: 3px;
  padding: 8px;
  --cell-radius: 8px;
}
.cell {
  position: relative;
  border-radius: var(--cell-radius);
  background: var(--cell-color, #ddd);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, filter 0.15s ease;
  overflow: hidden;
}
.cell:hover {
  filter: brightness(1.06);
}
.cell:active {
  transform: scale(0.95);
}
.cell .cat {
  width: 90%;
  height: 90%;
  opacity: 0;
  transform: scale(0.3);
  transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.15s ease;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.18));
}
.cell .x-mark {
  position: absolute;
  width: 46%;
  height: 46%;
  color: rgba(255, 255, 255, 0.92);
  opacity: 0;
  transform: scale(0.4);
  transition: transform 0.14s ease, opacity 0.14s ease;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.15));
}
.cell.cat .cat {
  opacity: 1;
  transform: scale(1);
}
.cell.cat::before {
  content: "";
  position: absolute;
  width: 86%;
  height: 86%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
}
.cell.mark .x-mark {
  opacity: 1;
  transform: scale(1);
}
.cell.wrong {
  animation: shake 0.4s ease;
}
.cell.wrong::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(231, 76, 92, 0.55);
  border-radius: var(--cell-radius);
  animation: fade-out 0.5s ease forwards;
}
@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-5px);
  }
  40% {
    transform: translateX(5px);
  }
  60% {
    transform: translateX(-4px);
  }
  80% {
    transform: translateX(3px);
  }
}
@keyframes fade-out {
  to {
    opacity: 0;
  }
}
.cell.hint {
  animation: hint-pulse 0.7s ease infinite;
  z-index: 2;
}
@keyframes hint-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(155, 130, 214, 0);
    filter: brightness(1.1);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(155, 130, 214, 0.45);
    filter: brightness(1.2);
  }
}
.cell.pop .cat {
  animation: pop 0.32s ease;
}
@keyframes pop {
  0% {
    transform: scale(0.3);
  }
  60% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}

/* ---------- controls ---------- */
.controls {
  display: flex;
  gap: 18px;
  align-items: flex-start;
}
.ctrl-btn {
  border: none;
  cursor: pointer;
  background: #fff;
  box-shadow: var(--shadow-sm);
  color: var(--accent);
  width: 58px;
  height: 58px;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  transition: transform 0.12s ease, background 0.15s ease, color 0.15s ease;
}
.ctrl-btn svg {
  width: 24px;
  height: 24px;
}
.ctrl-label {
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.ctrl-btn:hover {
  transform: translateY(-2px);
}
.ctrl-btn:active {
  transform: scale(0.92);
}
.ctrl-btn:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}
.ctrl-btn.active {
  background: var(--accent);
  color: #fff;
}
.ctrl-btn.active .ctrl-label {
  color: rgba(255, 255, 255, 0.9);
}

/* ---------- subbar ---------- */
.subbar {
  display: flex;
  gap: 14px;
  align-items: baseline;
  font-weight: 800;
  color: var(--ink-soft);
}
.timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.05rem;
}
.best-time {
  font-size: 0.85rem;
  color: var(--accent);
}
.controls-hint {
  margin: -4px 0 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  text-align: center;
}
.controls-hint b {
  color: var(--accent);
}

/* ---------- modals ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(70, 60, 50, 0.32);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: overlay-in 0.2s ease;
}
.modal-overlay[hidden] {
  display: none;
}
@keyframes overlay-in {
  from {
    opacity: 0;
  }
}
.modal {
  background: #fff;
  border-radius: 26px;
  padding: 28px 26px;
  width: min(90vw, 360px);
  text-align: center;
  box-shadow: 0 24px 60px rgba(70, 50, 30, 0.25);
  animation: modal-in 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes modal-in {
  from {
    transform: scale(0.85) translateY(10px);
    opacity: 0;
  }
}
.modal h2 {
  margin: 8px 0 6px;
  font-size: 1.7rem;
  color: #6f6457;
}
.modal p {
  margin: 0 0 16px;
  color: var(--ink-soft);
  font-weight: 600;
}
.modal-cat {
  width: 84px;
  height: 84px;
  animation: bob 2s ease-in-out infinite;
}
@keyframes bob {
  0%,
  100% {
    transform: translateY(0) rotate(-3deg);
  }
  50% {
    transform: translateY(-7px) rotate(3deg);
  }
}
.modal-emoji {
  font-size: 4rem;
  line-height: 1;
}
.modal-stats {
  display: flex;
  justify-content: center;
  gap: 22px;
  margin-bottom: 18px;
}
.modal-stats .stat {
  display: flex;
  flex-direction: column;
}
.modal-stats .stat b {
  font-size: 1.3rem;
  color: var(--accent);
}
.modal-stats .stat span {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  font-weight: 800;
}
.modal-row {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.primary-btn,
.ghost-btn {
  border: none;
  cursor: pointer;
  border-radius: 16px;
  padding: 13px 22px;
  font-weight: 800;
  font-size: 1rem;
  font-family: inherit;
  transition: transform 0.12s ease, filter 0.15s ease;
}
.primary-btn {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 18px rgba(155, 130, 214, 0.4);
}
.ghost-btn {
  background: var(--bg-2);
  color: var(--ink);
}
.primary-btn:hover,
.ghost-btn:hover {
  filter: brightness(1.05);
  transform: translateY(-1px);
}
.primary-btn:active,
.ghost-btn:active {
  transform: scale(0.96);
}
.modal-help {
  text-align: left;
}
.modal-help h2 {
  text-align: center;
}
.help-list {
  list-style: none;
  padding: 0;
  margin: 0 0 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.help-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--ink);
  font-size: 0.92rem;
  line-height: 1.3;
}
.help-ic {
  width: 26px;
  height: 26px;
  flex: 0 0 26px;
  color: var(--accent);
}
.modal-help .primary-btn {
  width: 100%;
}

/* ---------- confetti ---------- */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 60;
}

/* ---------- header icon svg ---------- */
.icon-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}

/* ---------- level select ---------- */
.levelselect-panel {
  background: #fff;
  border-radius: 24px;
  width: min(93vw, 480px);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  padding: 20px 14px 8px 20px;
  box-shadow: 0 24px 60px rgba(70, 50, 30, 0.25);
  animation: modal-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.levelselect-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 6px;
}
.levelselect-head h2 {
  margin: 0;
  font-size: 1.5rem;
  color: #6f6457;
}
.levelselect-sub {
  margin: 4px 6px 14px 0;
  font-size: 0.82rem;
  color: var(--ink-soft);
  font-weight: 600;
}
.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  overflow-y: auto;
  padding: 4px 8px 18px 0;
}
.level-tile {
  position: relative;
  aspect-ratio: 1;
  border: 2px solid var(--bg-2);
  background: #fff;
  border-radius: 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-family: inherit;
  transition: transform 0.1s ease, box-shadow 0.15s ease;
}
.level-tile:hover {
  transform: translateY(-2px);
}
.level-tile:active {
  transform: scale(0.94);
}
.lt-num {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--band, var(--accent));
  line-height: 1;
}
.lt-sub {
  font-size: 0.58rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}
.level-tile.completed {
  background: color-mix(in srgb, var(--band) 15%, #fff);
  border-color: color-mix(in srgb, var(--band) 38%, #fff);
}
.level-tile.completed .lt-sub {
  color: color-mix(in srgb, var(--band) 65%, #6f6457);
}
.level-tile.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.lt-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 13px;
  height: 13px;
  color: var(--band, var(--accent));
}

@media (max-width: 420px) {
  /* Keep the complete action set visible at 320–414px without shrinking
     touch targets below 44px. */
  .app-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
    gap: 10px;
  }
  .brand { justify-self: center; }
  .header-actions {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 380px) {
  /* every horizontal px matters for 9x9/10x10 boards on small phones */
  .app {
    padding-left: 10px;
    padding-right: 10px;
  }
  .rule-chip {
    font-size: 0.72rem;
    padding: 7px 9px;
  }
  .ctrl-btn {
    width: 52px;
    height: 52px;
  }
  .controls {
    gap: 12px;
  }
  .level-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}
