:root {
  --bg: #fdf6ec;
  --bg-2: #f6ead8;
  --paper: #fffdf8;
  --ink: #3d3a35;
  --ink-soft: #8a8173;
  --teal: #2a9d8f;
  --teal-deep: #1f7a6f;
  --teal-soft: #d6efeb;
  --orange: #f4a261;
  --line: #e4d6c1;
  --line-bold: #8a7a63;
  --sel: #ffe2b8;
  --peer: #f8eedf;
  --same: #e3f4f1;
  --bad: #e76f51;
  --bad-soft: #fde3dc;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; overflow: hidden; }

body {
  font-family: 'Nunito', system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 18%, rgba(244,162,97,0.14) 0 2px, transparent 3px) 0 0 / 34px 34px,
    linear-gradient(180deg, var(--bg), var(--bg-2));
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#app {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
}

/* ── Header ──────────────────────────────────────────────────────────── */
header { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 8px; }
.mascot { width: 44px; height: 44px; animation: tilt 4s ease-in-out infinite; }
@keyframes tilt { 50% { transform: rotate(-8deg); } }
h1 {
  font-family: 'Baloo 2', 'Nunito', sans-serif;
  font-weight: 800;
  font-size: 30px;
  margin: 0;
  color: var(--teal-deep);
  letter-spacing: 0.01em;
}
.head-btns { display: flex; gap: 8px; }
button { font-family: inherit; color: inherit; }
.round {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--paper);
  font-size: 16px;
  cursor: pointer;
  padding: 0;
}
.round:active { transform: scale(0.94); }

/* ── Controls ────────────────────────────────────────────────────────── */
.controls { display: flex; gap: 8px; }
.seg {
  flex: 1;
  display: flex;
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 3px;
  gap: 2px;
}
.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 9px;
  padding: 6px 4px;
  font-weight: 800;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
}
.seg button.on { background: var(--teal); color: #fff; }

/* Pushes the board, cats and tools together at the bottom, near thumbs */
.status { display: flex; align-items: center; gap: 12px; font-size: 14px; color: var(--ink-soft); margin-bottom: auto; }
.stat b { color: var(--ink); font-variant-numeric: tabular-nums; }
.new {
  margin-left: auto;
  border: 0;
  background: var(--orange);
  color: #fff;
  font-weight: 800;
  font-size: 13px;
  padding: 7px 12px;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: 0 2px 0 #d9823f;
}
.new:active { transform: translateY(2px); box-shadow: none; }

/* ── Board ───────────────────────────────────────────────────────────── */
main { flex: 0 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }
#board {
  --n: 9;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  /* Fallback only — game.js measures the real free space and sets the width */
  width: min(100%, calc(100dvh - 320px));
  aspect-ratio: 1;
  background: var(--paper);
  border: 3px solid var(--line-bold);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 6px 18px -8px rgba(90, 70, 40, 0.35);
}
.cell {
  position: relative;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.cell.bx { border-right: 2.5px solid var(--line-bold); }
.cell.by { border-bottom: 2.5px solid var(--line-bold); }
.cell.lastc { border-right: 0; }
.cell.lastr { border-bottom: 0; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.bad { background: var(--bad-soft); }
.cell.bad.sel { background: #fbcfc2; }

.cell > svg.v { width: 84%; height: 84%; }
.cell.given > svg.v { filter: drop-shadow(0 1px 0 rgba(0,0,0,0.12)); }
.cell:not(.given) > svg.v { opacity: 0.95; }
.cell.bad > svg.v { animation: shake 0.3s ease; }
.cell.pop > svg.v { animation: pop 0.25s cubic-bezier(.3,1.8,.5,1); }
.cell.given::after {
  content: "";
  position: absolute;
  left: 3px; top: 3px;
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--line-bold);
  opacity: 0.35;
}
@keyframes pop { from { transform: scale(0.4); } }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

.num {
  position: absolute;
  right: 2px; bottom: 1px;
  font-weight: 800;
  font-size: clamp(8px, 2.2vw, 12px);
  line-height: 1;
  color: var(--ink);
  background: rgba(255, 253, 248, 0.85);
  border-radius: 4px;
  padding: 1px 2px;
}
.cell.bad .num { color: var(--bad); }

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(var(--nc), minmax(0, 1fr));
  grid-template-rows: repeat(var(--nr), minmax(0, 1fr));
}
.notes svg { width: 100%; height: 100%; opacity: 0.8; }
.notes .slot { display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; position: relative; }
.notes .slot b { font-size: clamp(7px, 1.8vw, 10px); color: var(--ink-soft); }

/* Win ripple */
.cell.win { animation: winwave 0.6s ease both; }
@keyframes winwave { 40% { background: var(--teal-soft); transform: scale(1.08); } }

/* ── Palette & tools ─────────────────────────────────────────────────── */
#palette {
  display: grid;
  /* minmax(0, …) stops Safari from letting the cat SVGs widen the columns */
  grid-template-columns: repeat(var(--pc, 9), minmax(0, 1fr));
  gap: 5px;
}
.pick {
  min-width: 0;
  position: relative;
  aspect-ratio: 1;
  max-height: 64px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  padding: 3px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 0 var(--line);
}
.pick svg { width: 100%; height: 100%; }
.pick:active { transform: translateY(2px); box-shadow: none; }
.pick.done { opacity: 0.35; }
.pick.hl { border-color: var(--teal); background: var(--teal-soft); }
.pick .left {
  position: absolute;
  top: -6px; right: -6px;
  min-width: 18px; height: 18px;
  border-radius: 9px;
  background: var(--teal);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--paper);
}
.pick .pnum { position: absolute; left: 3px; bottom: 1px; font-size: 11px; font-weight: 800; color: var(--ink-soft); }
.pick.done .left { display: none; }

.tools { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tool {
  border: 2px solid var(--line);
  background: var(--paper);
  border-radius: 12px;
  padding: 6px 2px;
  font-weight: 800;
  font-size: 12px;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  cursor: pointer;
}
.tool span { font-size: 18px; line-height: 1.1; color: var(--ink); }
.tool em { font-style: normal; font-size: 10px; text-transform: uppercase; }
.tool.on { border-color: var(--teal); background: var(--teal-soft); color: var(--teal-deep); }
.tool:active { transform: scale(0.96); }
.tool:disabled { opacity: 0.4; }

/* ── Modal ───────────────────────────────────────────────────────────── */
#modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(61, 58, 53, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.sheet {
  position: relative;
  width: min(440px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--paper);
  border-radius: 22px;
  border: 3px solid var(--line);
  padding: 22px 18px 18px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(61, 58, 53, 0.35);
  animation: arrive 0.3s cubic-bezier(.3,1.5,.5,1);
}
@keyframes arrive { from { transform: scale(0.85); opacity: 0; } }
.sheet h2 { font-family: 'Baloo 2', sans-serif; font-weight: 800; color: var(--teal-deep); margin: 0 0 4px; font-size: 28px; }
.sheet p { color: var(--ink-soft); margin: 0 0 14px; line-height: 1.45; }
.close {
  position: absolute;
  top: 10px; right: 10px;
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 0;
  background: var(--bg-2);
  cursor: pointer;
}
.big {
  border: 0;
  background: var(--teal);
  color: #fff;
  font-weight: 800;
  font-size: 17px;
  padding: 12px 26px;
  border-radius: 999px;
  margin-top: 8px;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--teal-deep);
}
.big:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--teal-deep); }

.steps { list-style: none; padding: 0; margin: 0 0 6px; text-align: left; display: flex; flex-direction: column; gap: 8px; }
.steps li { display: flex; gap: 12px; align-items: center; background: var(--bg); border-radius: 14px; padding: 9px 12px; font-size: 15px; line-height: 1.35; }
.steps .ico { flex: 0 0 auto; width: 46px; display: flex; justify-content: center; font-size: 26px; }
.steps .ico svg { width: 42px; height: 42px; }
.steps b { color: var(--teal-deep); }
.mini-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 1px; }
.mini-row svg { width: 21px !important; height: 21px !important; }

.cast { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 4px 0 12px; }
.cast div { background: var(--bg); border-radius: 12px; padding: 6px 2px; font-size: 12px; font-weight: 800; }
.cast svg { width: 48px; height: 48px; display: block; margin: 0 auto 2px; }

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  padding: 12px 4px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
}
.toggle small { display: block; font-weight: 500; color: var(--ink-soft); }
.switch {
  flex: 0 0 auto;
  width: 50px; height: 30px;
  border-radius: 15px;
  border: 0;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.switch.on { background: var(--teal); }
.switch.on::after { transform: translateX(20px); }

.results { display: flex; justify-content: center; gap: 10px; margin: 8px 0 6px; flex-wrap: wrap; }
.results div { background: var(--bg); border-radius: 12px; padding: 8px 14px; min-width: 84px; }
.results b { display: block; font-size: 20px; color: var(--ink); }
.results span { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-soft); }
.hero svg { width: 120px; height: 120px; animation: pop 0.5s cubic-bezier(.3,1.8,.5,1); }

/* Confetti paws */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.paw {
  position: absolute;
  top: -40px;
  font-size: 24px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }

/* Buy Me a Coffee link at the bottom of the help, settings and win sheets */
.support {
  display: block;
  width: fit-content;
  margin: 14px auto 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-soft);
  text-decoration: none;
}
.support:hover { color: var(--teal-deep); text-decoration: underline; }

/* YouTube link sits first and a touch louder than the coffee link */
.links { display: flex; justify-content: center; align-items: center; gap: 16px; flex-wrap: wrap; margin-top: 14px; }
.links .support { margin: 0; }
.yt { font-size: 13px; font-weight: 700; color: #d62828; text-decoration: none; }
.yt:hover { text-decoration: underline; }

/* Back to the landing page + version label */
a.round.home-x {
  display: flex; align-items: center; justify-content: center;
  text-decoration: none;
  color: var(--ink);
  font-size: 15px;
  font-weight: 800;
}
.ver { margin: 10px 0 0; font-size: 11px; color: var(--ink-soft); opacity: 0.7; }
.ver:empty { display: none; }
