:root {
  --bg: #f4efe6;
  --panel: #fffaf2;
  --ink: #3b2f26;
  --muted: #8a7a68;
  --line: #3b2f26;
  --accent: #7fb069;
  --accent-ink: #234d1a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #211c17;
    --panel: #2c261f;
    --ink: #f1e8db;
    --muted: #b3a493;
    --line: #14100c;
    --accent: #7fb069;
    --accent-ink: #eafce0;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

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

header { text-align: center; }
h1 {
  font-size: 1.5rem;
  margin: 6px 0 2px;
  letter-spacing: 0.3px;
}
#solved-flag { font-size: 1rem; display: none; }
.sub { color: var(--muted); font-size: 0.95rem; }
.sub .dot { margin: 0 6px; opacity: 0.5; }

.statbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  margin: 8px 0 10px;
}
.score {
  justify-self: start;
  font-weight: 800;
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent-ink);
}
.score::before { content: "⭐ "; }
.hearts { font-size: 1.25rem; letter-spacing: 2px; justify-self: center; }
.streak {
  justify-self: end;
  font-weight: 800;
  font-size: 1.05rem;
  color: #e2703a;
  min-width: 3ch;
  text-align: right;
}

/* win-overlay score */
.gained {
  font-size: 2.1rem;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  margin: 2px 0 8px;
}
.breakdown {
  display: grid;
  grid-template-columns: auto auto;
  gap: 2px 18px;
  justify-content: center;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 8px;
}
.breakdown span:nth-child(even) { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.delta-note { font-size: 0.78rem; color: var(--muted); margin-bottom: 6px; }

main { flex: 1; display: flex; align-items: center; justify-content: center; }
#board-wrap { position: relative; width: 100%; }

#board {
  --n: 8;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: 0;
  width: min(92vw, 520px);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  background: var(--line);
  border: 3px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell {
  position: relative;
  border: 0;
  padding: 0;
  margin: 0;
  aspect-ratio: 1 / 1;
  font-size: 1.4rem;
  color: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  outline: 1px solid rgba(0, 0, 0, 0.12);
  transition: filter 0.08s;
}
.cell:active { filter: brightness(0.92); }
/* thick region borders */
.cell.bt { box-shadow: inset 0 3px 0 0 var(--line); }
.cell.bb { box-shadow: inset 0 -3px 0 0 var(--line); }
.cell.bl { box-shadow: inset 3px 0 0 0 var(--line); }
.cell.br { box-shadow: inset -3px 0 0 0 var(--line); }
.cell.bt.bb { box-shadow: inset 0 3px 0 0 var(--line), inset 0 -3px 0 0 var(--line); }
.cell.bl.br { box-shadow: inset 3px 0 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bt.bl { box-shadow: inset 0 3px 0 0 var(--line), inset 3px 0 0 0 var(--line); }
.cell.bt.br { box-shadow: inset 0 3px 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bb.bl { box-shadow: inset 0 -3px 0 0 var(--line), inset 3px 0 0 0 var(--line); }
.cell.bb.br { box-shadow: inset 0 -3px 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bt.bb.bl { box-shadow: inset 0 3px 0 0 var(--line), inset 0 -3px 0 0 var(--line), inset 3px 0 0 0 var(--line); }
.cell.bt.bb.br { box-shadow: inset 0 3px 0 0 var(--line), inset 0 -3px 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bt.bl.br { box-shadow: inset 0 3px 0 0 var(--line), inset 3px 0 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bb.bl.br { box-shadow: inset 0 -3px 0 0 var(--line), inset 3px 0 0 0 var(--line), inset -3px 0 0 0 var(--line); }
.cell.bt.bb.bl.br { box-shadow: inset 0 0 0 3px var(--line); }

.cell.mark { color: rgba(0, 0, 0, 0.4); font-size: 1.8rem; line-height: 0; }
.cell.cat img {
  width: 88%;
  height: 88%;
  object-fit: contain;
  display: block;
  pointer-events: none;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
}
.cell.cat.placed { animation: pop 0.14s ease-out; } /* only the just-placed capybara */
.cell.cat.bad img { filter: none; }
.cell.cat.bad::after {
  content: "";
  position: absolute;
  inset: 6%;
  border: 3px solid #d63b3b;
  border-radius: 8px;
  animation: shake 0.2s;
}
@keyframes pop { from { transform: scale(0.6); } to { transform: scale(1); } }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}

#overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 14px;
}
.card {
  background: var(--panel);
  border-radius: 16px;
  padding: 22px 26px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  max-width: 80%;
}
.card h2 { margin: 0 0 6px; }
.card p { color: var(--muted); margin: 0 0 16px; }
.overlay-btns { display: flex; gap: 10px; justify-content: center; }

footer { margin-top: 14px; }
.controls { display: flex; gap: 8px; justify-content: center; margin-bottom: 10px; }
.btn {
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 10px 16px;
  cursor: pointer;
  min-width: 44px;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }

.inline-capy {
  height: 1.4em;
  width: auto;
  vertical-align: -0.35em;
  margin: 0 0.1em;
}
.rules {
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
  line-height: 1.4;
  margin: 6px auto 0;
  max-width: 40ch;
}
