/* Fredoka (SIL Open Font License, see fonts/OFL-Fredoka.txt), stored with the game
   so it loads without a font service and works offline. Weights 400 to 700. */
@font-face {
  font-family: 'Fredoka';
  src: url('../fonts/fredoka.woff') format('woff');
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #f8f3ec;
  --card: #ffffff;
  --ink: #4a3a3f;
  --ink-soft: #8a7a7c;
  --line: #ece3da;
  --dark: #3d2f2f;
  --accent: #ef5e8a;
  --heart: #ff6b8b;

  /* region colours */
  --r0: #6cb6e0;
  --r1: #a9d98e;
  --r2: #62c4bd;
  --r3: #ebcb50;
  --r4: #e597b4;
  --r5: #9d85de;
  --r6: #f3a66b;
  --r7: #c7a27c;

  --font: 'Fredoka', 'Nunito', ui-rounded, 'Segoe UI', system-ui, sans-serif;
  --board: min(calc(100vw - 64px), calc(100dvh - 340px), 640px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

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

.app {
  max-width: 720px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* ---------- header ---------- */
.top {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}
.top h1 {
  margin: 0;
  font-size: clamp(26px, 6vw, 36px);
  font-weight: 700;
  color: #5e4a52;
}
.pill {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 14px 5px 6px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 2px 6px rgba(80, 50, 40, .06);
  font-size: 20px;
  font-weight: 600;
  color: var(--ink-soft);
}
.pill b { color: #5aa548; }
.counter-cat { width: 36px; height: 36px; display: block; }
.round-btn {
  justify-self: end;
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--card);
  display: grid; place-items: center;
  box-shadow: 0 2px 6px rgba(80, 50, 40, .06);
}
.round-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--ink); stroke-width: 1.8; }

/* ---------- difficulty ---------- */
.difficulty { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.difficulty .label {
  font-size: 12px; letter-spacing: .14em; text-transform: uppercase;
  font-weight: 600; color: var(--ink-soft);
}
.tabs {
  display: flex; gap: 4px; padding: 5px;
  background: var(--card); border: 1px solid var(--line); border-radius: 999px;
}
.tabs button {
  border: 0; background: none; border-radius: 999px;
  padding: 9px 20px; font-weight: 600; font-size: 16px;
  color: var(--ink-soft); text-transform: uppercase; letter-spacing: .03em;
}
.tabs button.active { background: var(--dark); color: #fff; }

/* ---------- hearts ---------- */
.hearts { display: flex; gap: 6px; min-height: 28px; }
.hearts[hidden] { display: none; }
.heart svg { width: 28px; height: 28px; fill: var(--heart); display: block; }
.heart.lost svg { fill: #e5dcd5; }
.heart.break { animation: heartBreak .6s ease; }

/* ---------- board ---------- */
.board-card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 14px;
  box-shadow: 0 6px 20px rgba(80, 50, 40, .07);
}
.board {
  --gap: calc(var(--board) / var(--n) * 0.09);
  width: var(--board);
  height: var(--board);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: var(--gap);
}

.tile {
  position: relative;
  border: 0;
  padding: 0;
  border-radius: 22%;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .07), 0 0 0 2px rgba(255, 255, 255, .9);
  transition: transform .12s ease, filter .12s ease;
}
.tile:active { transform: scale(.94); }
@media (hover: hover) { .tile:hover { filter: brightness(1.05); } }

/* Subtle textures so colours can be told apart without relying on hue alone */
.tile::before {
  content: '';
  position: absolute; inset: 0;
  border-radius: inherit;
  opacity: .22;
  pointer-events: none;
}
.r0 { background: var(--r0); }
.r1 { background: var(--r1); }
.r2 { background: var(--r2); }
.r3 { background: var(--r3); }
.r4 { background: var(--r4); }
.r5 { background: var(--r5); }
.r6 { background: var(--r6); }
.r7 { background: var(--r7); }
.r0::before { background: radial-gradient(circle, #fff 18%, transparent 20%) 0 0 / 33% 33%; }
.r1::before { background: repeating-linear-gradient(45deg, #fff 0 3px, transparent 3px 11px); }
.r3::before { background: repeating-linear-gradient(0deg, #fff 0 3px, transparent 3px 11px); }
.r4::before { background: repeating-linear-gradient(-45deg, #fff 0 3px, transparent 3px 11px); }
.r5::before { background: radial-gradient(circle, #fff 26%, transparent 28%) 0 0 / 50% 50%; }
.r6::before { background: repeating-linear-gradient(90deg, #fff 0 3px, transparent 3px 11px); }
.r7::before {
  background:
    repeating-linear-gradient(0deg, #fff 0 2px, transparent 2px 12px),
    repeating-linear-gradient(90deg, #fff 0 2px, transparent 2px 12px);
}

.tile-inner {
  position: absolute; inset: 7%;
  display: grid; place-items: center;
  border-radius: 18%;
  pointer-events: none;
}
.tile-inner svg { width: 100%; height: 100%; display: block; }
.tile.has-cat .tile-inner { background: rgba(255, 255, 255, .55); }
.tile.has-paw .tile-inner { inset: 24%; color: rgba(70, 45, 40, .38); }

.ghost {
  position: absolute; inset: 7%;
  opacity: .6;
  pointer-events: none;
  animation: ghost .65s ease forwards;
}
.ghost svg { width: 100%; height: 100%; }

.tile.pop .tile-inner { animation: pop .45s cubic-bezier(.3, 1.6, .5, 1); }
.tile.shake { animation: shake .45s ease; }
.tile.warn { animation: warn .9s ease; }
.tile.sparkle { animation: sparkle 1.2s ease; }
.tile.dance .tile-inner { animation: dance .9s ease; }

.toast {
  position: absolute;
  left: 50%; bottom: 18px;
  transform: translate(-50%, 10px);
  background: var(--dark); color: #fff;
  padding: 10px 18px; border-radius: 999px;
  font-size: 16px; font-weight: 500;
  white-space: nowrap; max-width: 92%;
  overflow: hidden; text-overflow: ellipsis;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- mode toggle ---------- */
.mode {
  display: flex; gap: 6px; padding: 5px;
  background: var(--card); border: 1px solid var(--line); border-radius: 999px;
}
.mode-btn {
  display: flex; align-items: center; gap: 8px;
  border: 0; background: none; border-radius: 999px;
  padding: 8px 26px 8px 12px; font-size: 20px; font-weight: 600;
  color: var(--ink-soft);
}
.mode-btn.active { background: var(--dark); color: #fff; }
.mode-icon { width: 34px; height: 34px; display: block; }
.paw-icon {
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cellipse cx='50' cy='64' rx='19' ry='16'/%3E%3Ccircle cx='27' cy='43' r='8'/%3E%3Ccircle cx='41' cy='30' r='8.5'/%3E%3Ccircle cx='59' cy='30' r='8.5'/%3E%3Ccircle cx='73' cy='43' r='8'/%3E%3C/svg%3E") center / 80% no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cellipse cx='50' cy='64' rx='19' ry='16'/%3E%3Ccircle cx='27' cy='43' r='8'/%3E%3Ccircle cx='41' cy='30' r='8.5'/%3E%3Ccircle cx='59' cy='30' r='8.5'/%3E%3Ccircle cx='73' cy='43' r='8'/%3E%3C/svg%3E") center / 80% no-repeat;
}

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; gap: 8px; width: 100%; max-width: 520px;
  justify-content: space-between;
}
.tool {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px;
  border: 1px solid var(--line); border-radius: 18px;
  background: var(--card);
  font-size: 14px; font-weight: 500; color: var(--ink-soft);
  box-shadow: 0 2px 6px rgba(80, 50, 40, .05);
}
.tool:active { transform: scale(.95); }
.ti { font-size: 24px; line-height: 1.1; }

/* ---------- dialogs ---------- */
dialog.sheet {
  border: 0;
  border-radius: 28px;
  padding: 22px 22px 18px;
  width: min(460px, calc(100vw - 32px));
  max-height: 90dvh;
  overflow: auto;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 20px 60px rgba(60, 40, 40, .25);
}
dialog.sheet::backdrop { background: rgba(60, 40, 40, .35); backdrop-filter: blur(3px); }
dialog[open] { animation: sheetIn .3s cubic-bezier(.3, 1.4, .5, 1); }
dialog.center { text-align: center; overflow: visible; }
dialog h2 { margin: 0 0 6px; font-size: 26px; color: #5e4a52; }
.lead { margin: 0 0 14px; color: var(--ink-soft); font-size: 17px; line-height: 1.4; }

.big-btn {
  display: block; width: 100%;
  margin-top: 14px;
  border: 0; border-radius: 999px;
  padding: 14px; font-size: 20px; font-weight: 600;
  background: var(--accent); color: #fff;
  box-shadow: 0 4px 0 #c94470;
}
.big-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #c94470; }
.big-btn:focus { outline: none; }
.big-btn:focus-visible { outline: 3px solid #ffc2d4; outline-offset: 3px; }
form[method="dialog"] { margin: 0; }

/* help */
.example {
  width: 200px; height: 200px; margin: 0 auto 14px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px;
}
.example .tile { cursor: default; }
.rules { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.rules li {
  border: 1px solid var(--line); border-radius: 16px; padding: 10px 14px;
  font-size: 15px; color: var(--ink-soft); line-height: 1.35;
}
.rules b { display: block; font-size: 18px; color: var(--ink); }
.rule-tag { display: block; font-size: 12px; font-weight: 700; color: var(--accent); }
.tip { font-size: 15px; color: var(--ink-soft); margin: 12px 0 0; }

/* collection */
.collection { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (min-width: 520px) { .collection { grid-template-columns: repeat(4, 1fr); } }
.coll-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  border: 2px solid var(--line); border-radius: 18px; background: #fffaf5;
  padding: 8px 4px 6px;
}
.coll-item.selected { border-color: var(--accent); background: #fff0f4; }
.coll-item.locked { cursor: default; opacity: .8; }
.coll-cat { width: 64px; height: 64px; }
.coll-cat svg { width: 100%; height: 100%; }
.coll-name { font-size: 15px; font-weight: 600; }

/* settings */
.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer;
}
.switch-row b { display: block; font-size: 18px; }
.switch-row small { color: var(--ink-soft); font-size: 14px; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch-row i {
  flex: none; position: relative; width: 54px; height: 32px; border-radius: 999px;
  background: #e4dad1; transition: background .2s;
}
.switch-row i::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 26px; height: 26px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2); transition: transform .2s;
}
.switch-row input:checked + i { background: #5aa548; }
.switch-row input:checked + i::after { transform: translateX(22px); }
.switch-row input:focus-visible + i { outline: 3px solid var(--accent); outline-offset: 2px; }

/* win / lose */
.win-cat { width: 120px; height: 120px; margin: -70px auto 4px; animation: dance 1.2s ease infinite; position: relative; z-index: 1; }
.win-cat svg { width: 100%; height: 100%; filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .15)); }
.win-cat.sleepy { animation: none; transform: rotate(-8deg); }
#winDialog > *:not(canvas), #loseDialog > * { position: relative; z-index: 1; }
.unlock {
  display: flex; align-items: center; gap: 12px; text-align: left;
  background: #fff6e0; border-radius: 18px; padding: 10px 14px; margin-bottom: 4px;
}
.unlock[hidden] { display: none; }
.unlock p { margin: 0; font-size: 16px; }
.unlock-cat { width: 64px; height: 64px; flex: none; animation: pop .6s cubic-bezier(.3, 1.6, .5, 1) .6s both; }

#confetti {
  position: fixed; inset: 0; width: 100vw; height: 100vh;
  pointer-events: none; display: none; z-index: 0;
}

/* ---------- animations ---------- */
@keyframes pop {
  0% { transform: scale(.3); }
  70% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px) rotate(-3deg); }
  40% { transform: translateX(7px) rotate(3deg); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(4px); }
}
@keyframes ghost {
  0% { opacity: .7; transform: scale(.9); }
  60% { opacity: .5; }
  100% { opacity: 0; transform: scale(.7) translateY(-10%); }
}
@keyframes warn {
  0%, 100% { box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .07), 0 0 0 2px #fff; }
  25%, 75% { box-shadow: 0 0 0 4px #ff5a7a, 0 0 14px #ff5a7a; }
}
@keyframes sparkle {
  0% { box-shadow: 0 0 0 2px #fff; }
  30% { box-shadow: 0 0 0 5px #ffd84d, 0 0 26px #ffd84d; }
  100% { box-shadow: 0 0 0 2px #fff; }
}
@keyframes dance {
  0%, 100% { transform: translateY(0) rotate(0); }
  25% { transform: translateY(-10%) rotate(-8deg); }
  50% { transform: translateY(0) rotate(0); }
  75% { transform: translateY(-6%) rotate(8deg); }
}
@keyframes heartBreak {
  0% { transform: scale(1); }
  40% { transform: scale(1.4) rotate(-10deg); }
  100% { transform: scale(1); }
}
@keyframes sheetIn {
  from { transform: translateY(30px) scale(.95); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* Small phones */
@media (max-width: 400px) {
  .tabs button { padding: 8px 14px; font-size: 14px; }
  .mode-btn { font-size: 18px; padding: 6px 20px 6px 10px; }
  .tool { font-size: 12px; }
  .ti { font-size: 20px; }
  .board-card { padding: 10px; }
}

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