:root {
  --bg: #fbf6ef;
  --ink: #2b2330;
  --muted: #8b8294;
  --card: #fffdf9;
  --line: #e6dccf;
  --accent: #ef7d45;
  --bad: #e5484d;
  --good: #3aa676;
  --cat-eye: #fbf6ef;
  --cat-nose: #f4a3b5;
  --edge: #2b2330;
  --r0: #ffd9c7; --r1: #cfe6ff; --r2: #d8f0c4; --r3: #f8e6a0; --r4: #e6d5f8;
  --r5: #ffc9dc; --r6: #c4eeea; --r7: #e8dcc9; --r8: #d3d9f6;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17141c; --ink: #f1ebf5; --muted: #9a90a5; --card: #221e2a; --line: #353040;
    --accent: #ff9a63; --bad: #ff6b70; --good: #5fcf9b; --cat-eye: #17141c; --cat-nose: #f4a3b5;
    --edge: #cfc6da;
    --r0: #7a4a3a; --r1: #35577a; --r2: #4d6b3a; --r3: #766a2c; --r4: #5d4880;
    --r5: #80425c; --r6: #2f6b68; --r7: #655a48; --r8: #4a5088;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.4 ui-rounded, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; -webkit-user-select: none; user-select: none; }
button:disabled { cursor: not-allowed; opacity: .4; }
#app {
  max-width: 600px; margin: 0 auto; min-height: 100%;
  padding: max(10px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px;
}
.top { display: flex; align-items: center; justify-content: space-between; }
h1 { margin: 0; font-size: 1.35rem; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.logo { width: 28px; height: 28px; color: var(--ink); }
.top-actions { display: flex; gap: 4px; }
.icon-btn { background: none; border: 0; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; color: var(--ink); }
.icon-btn:hover { background: var(--line); }
.icon-btn svg { width: 22px; height: 22px; }

.modes { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.modes::-webkit-scrollbar { display: none; }
.mode { flex: 0 0 auto; border: 1.5px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 14px; font-weight: 600; font-size: .92rem; }
.mode small { color: var(--muted); font-weight: 500; margin-left: 4px; }
.mode[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.mode[aria-pressed="true"] small { color: inherit; opacity: .7; }
.mode .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--good); margin-left: 6px; vertical-align: middle; }

.info { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--card); border: 1.5px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: .88rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.chip-btn { margin-left: auto; }
.chip-btn:hover { background: var(--line); }
.streak { border-color: var(--accent); color: var(--accent); animation: pop .35s; }

.board-wrap { display: grid; place-items: center; }
.board {
  --n: 7;
  --size: min(100vw - 24px, 100dvh - 290px, 560px);
  width: max(var(--size), 240px); aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr)); grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  border: 3px solid var(--edge); border-radius: 12px; overflow: hidden; background: var(--edge);
  gap: 0;
}
.cell {
  position: relative; display: grid; place-items: center; padding: 0; margin: 0; min-width: 0; min-height: 0; overflow: visible;
  background: var(--c); border: 0; border-radius: 0;
  box-shadow: inset 0 0 0 .5px rgba(0,0,0,.18);
  border-style: solid; border-color: var(--edge); border-width: 0;
  transition: filter .15s;
  outline-offset: -3px;
}
.cell.bt { border-top-width: 2.5px; } .cell.bl { border-left-width: 2.5px; }
.cell.br { border-right-width: 2.5px; } .cell.bb { border-bottom-width: 2.5px; }
.cell:hover:not(:disabled) { filter: brightness(1.06); }
.cell:focus-visible { outline: 3px solid var(--accent); z-index: 2; }
.cell:disabled { cursor: default; opacity: 1; }
.cell .cat { position: absolute; inset: 13%; width: 74%; height: 74%; color: var(--ink); pointer-events: none; }
.cell .x { font-size: calc(var(--size) / var(--n) * .55); line-height: 1; color: var(--ink); opacity: .38; pointer-events: none; font-weight: 600; }
.cell.cat-on .cat { animation: pop .3s ease-out; }
.cell.bad { background: color-mix(in srgb, var(--bad) 55%, var(--c)); animation: shake .4s; }
.cell.bad .cat { color: #fff; }
.cell.focus { filter: brightness(1.12) saturate(1.15); box-shadow: inset 0 0 0 3px var(--accent); }
.cell.target { animation: pulse 1s ease-in-out infinite; }
.cell.solved { cursor: default; }
.board.won .cell .cat { animation: bounce 1.2s ease-in-out infinite; animation-delay: calc(var(--i, 0) * 70ms); }

.spark { position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); pointer-events: none; z-index: 3; animation: fly .6s ease-out forwards; }

.msg { min-height: 1.5em; margin: 0; text-align: center; font-weight: 600; color: var(--muted); font-size: .95rem; }
.msg.good { color: var(--good); } .msg.bad { color: var(--bad); } .msg.hint { color: var(--ink); }

.tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tool { background: var(--card); border: 1.5px solid var(--line); border-radius: 14px; padding: 8px 2px; font-size: .78rem; font-weight: 600; display: flex; flex-direction: column; align-items: center; gap: 0; line-height: 1.2; }
.tool span { font-size: 1.2rem; line-height: 1.1; }
.tool:hover:not(:disabled) { background: var(--line); }
.tool.retire { grid-column: 1 / -1; background: var(--bad); color: #fff; border-color: var(--bad); font-size: .95rem; padding: 12px; flex-direction: row; justify-content: center; animation: pop .25s; }
.tool.retire[hidden] { display: none; }
body.no-helps [data-help="1"] { display: none; }
body.no-helps .tools { grid-template-columns: repeat(3, 1fr); }

.dlg { border: 0; border-radius: 22px; padding: 22px; background: var(--card); color: var(--ink); width: min(92vw, 380px); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.dlg::backdrop { background: rgba(20, 14, 28, .5); }
.dlg h2 { margin: 0 0 4px; font-size: 1.35rem; }
.dlg .sub { margin: 0 0 14px; color: var(--muted); }
.dlg .close-row { display: flex; gap: 8px; margin-top: 16px; }
.btn { flex: 1; border: 0; border-radius: 14px; padding: 12px; font-weight: 700; background: var(--accent); color: #fff; }
.btn.ghost { background: var(--line); color: var(--ink); }
.win-cat { width: 72px; height: 72px; display: block; margin: 0 auto 6px; animation: bounce 1s ease-in-out infinite; }
.win { text-align: center; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 10px 0; }
.badge { background: var(--bg); border: 1.5px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: .85rem; font-weight: 600; }
.badge.gold { border-color: var(--accent); color: var(--accent); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.stat { background: var(--bg); border-radius: 14px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.4rem; }
.stat small { color: var(--muted); }
.stat-table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: .92rem; }
.stat-table td, .stat-table th { padding: 5px 4px; text-align: left; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.stat-table th { color: var(--muted); font-weight: 600; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; font-weight: 600; }
.toggle small { display: block; color: var(--muted); font-weight: 500; }
.toggle input { appearance: none; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; flex: none; cursor: pointer; transition: background .15s; }
.toggle input::after { content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked { background: var(--good); }
.toggle input:checked::after { transform: translateX(18px); }

#confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 50; }
.conf { position: absolute; top: -20px; width: 9px; height: 13px; border-radius: 2px; animation: fall 2.4s ease-in forwards; }

@keyframes pop { 0% { transform: scale(.4); } 60% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-4px); } 40% { transform: translateX(4px); } 60% { transform: translateX(-3px); } 80% { transform: translateX(3px); } }
@keyframes pulse { 0%,100% { box-shadow: inset 0 0 0 3px var(--accent); } 50% { box-shadow: inset 0 0 0 6px var(--accent); } }
@keyframes bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-6%); } }
@keyframes fly { to { transform: translate(var(--dx), var(--dy)) scale(.3); opacity: 0; } }
@keyframes fall { to { transform: translateY(105vh) rotate(var(--rot)); opacity: .9; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
