:root {
  --bg: #fffaf8;
  --ink: #3d302c;
  --muted: #8a7e75;
  --line: #3b3330;
  --card: #fffdf9;
  --accent: #e07a5f;
  --accent-ink: #fff;
  --bad: #d62839;
  --ok: #3a9d5d;
  --hold: 450ms;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: inherit; }
button { font: inherit; color: inherit; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); max-width: 760px; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 20px; text-decoration: none; }
.brand-cat { width: 30px; height: 30px; }
.top nav { display: flex; gap: 14px; font-size: 15px; }
.top nav a { text-decoration: none; color: var(--muted); }
.top nav a.on { color: var(--ink); font-weight: 700; }

main { max-width: 760px; margin: 0 auto; padding: 0 16px 40px; }

/* ---------- level list ---------- */
.hero { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 16px; flex-wrap: wrap; }
.hero h1 { margin: 0; font-size: 24px; }
.hero p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.btn { border: 0; border-radius: 12px; padding: 10px 16px; background: var(--card); box-shadow: 0 1px 0 rgba(0,0,0,.08), inset 0 0 0 1.5px #e6dccf;
  cursor: pointer; font-weight: 650; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; -webkit-tap-highlight-color: transparent; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: none; }
.btn:not(:disabled):active { transform: translateY(1px); }

.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border: 0; border-radius: 999px; padding: 5px 12px; background: #efe6da; cursor: pointer; font-size: 14px; }
.chip.on { background: var(--ink); color: var(--bg); }

.levels { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.lv { position: relative; aspect-ratio: 1; border-radius: 12px; background: var(--card); box-shadow: inset 0 0 0 1.5px #e6dccf;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-decoration: none; }
.lv b { font-size: 18px; }
.lv small { font-size: 11px; color: var(--muted); }
.lv.done { background: #e4f3e8; box-shadow: inset 0 0 0 1.5px #a9d8b6; }
.lv.started::after { content: ""; position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.lv.done::after { content: "✓"; position: absolute; top: 2px; right: 6px; font-size: 12px; color: var(--ok); font-weight: 800; }

/* ---------- game ---------- */
.gamebar { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 10px; gap: 8px; }
.gamebar h1 { margin: 0; font-size: 20px; }
.gamebar .meta { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.lvnav { display: flex; gap: 6px; }
.lvnav .btn { padding: 8px 12px; }

.board-wrap { display: flex; justify-content: center; }
.board { --n: 8; display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr)); grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  width: min(100%, 580px, calc(100svh - 240px)); min-width: 280px; aspect-ratio: 1; gap: clamp(3px, 1.3vw, 6px); padding: clamp(10px, 3.5vw, 16px);
  background: #fff; border-radius: 27px; box-shadow: 0 0 0 1px #eaded8, 0 12px 28px rgba(78,64,55,.1);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.cell { position: relative; min-width: 0; min-height: 0; overflow: hidden; border: 2px solid #fff; border-radius: 23%;
  box-shadow: 0 1px 2px rgba(87,67,58,.16); transition: filter .15s; display: flex; align-items: center; justify-content: center; cursor: pointer; -webkit-tap-highlight-color: transparent; }
/* white X with rounded ends and a soft shadow, like the original game */
.cell .x { width: 42%; height: 42%; pointer-events: none; overflow: visible; filter: drop-shadow(0 2px 3px rgba(92,61,44,.18)); }
.cell .x path { stroke: #fff; stroke-width: 3.3; stroke-linecap: round; }
.cell svg.c { width: 78%; height: 78%; pointer-events: none; animation: pop .22s ease-out; }
.cell.bad svg.c { filter: drop-shadow(0 0 0 var(--bad)); }
.cell.bad::before { content: ""; position: absolute; inset: 0; border-radius: inherit; background: rgba(214,40,57,.22); border: 3px solid var(--bad); }
.cell.cursor::before { content: ""; position: absolute; inset: 0; border-radius: inherit; border: 2px dashed var(--ink); }
.cell.shake { animation: shake .3s; }

/* hold-to-place ring */
.cell .ring { position: absolute; inset: 8%; pointer-events: none; }
.cell .ring circle { fill: rgba(255,255,255,.35); stroke: var(--ink); stroke-width: 6; stroke-dasharray: 289; stroke-dashoffset: 289;
  transform: rotate(-90deg); transform-origin: 50% 50%; animation: ring var(--hold) linear forwards; }
.cell .ring.remove circle { stroke: var(--bad); }

.board.won .cell { animation: wave .6s ease-out both; }
.board.won { box-shadow: 0 0 0 3px var(--ok), 0 12px 28px rgba(58,157,93,.25); }
.cell.bad svg.c { animation: pop .22s ease-out, shake .3s .22s; }
.status { text-align: center; margin: 12px 0 0; font-weight: 700; font-size: 15px; color: var(--muted); }
.status.wrong { color: var(--bad); }
.status.solved { color: var(--ok); font-size: 17px; }
.status a { color: inherit; }

@keyframes pop { from { transform: scale(.4); } 70% { transform: scale(1.12); } to { transform: scale(1); } }
@keyframes ring { to { stroke-dashoffset: 0; } }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@keyframes wave { 50% { filter: brightness(1.15); transform: scale(.94); } }

.tools { display: flex; justify-content: center; gap: 8px; margin: 14px 0 6px; flex-wrap: wrap; }
.tip { text-align: center; color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.tip kbd { font: inherit; background: #efe6da; border-radius: 4px; padding: 0 4px; }

.settings { margin: 14px auto 0; max-width: 560px; font-size: 14px; color: var(--muted); }
.settings summary { cursor: pointer; text-align: center; }
.settings .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid #ece3d7; color: var(--ink); }
.settings select { font: inherit; padding: 4px 6px; border-radius: 8px; border: 1px solid #ddd2c4; background: var(--card); }

.win { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(45,38,34,.35); z-index: 10; padding: 20px; }
.win .card { background: var(--card); border-radius: 20px; padding: 24px; text-align: center; max-width: 320px; width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,.25); animation: pop .3s ease-out; }
.win .card svg { width: 72px; height: 72px; }
.win h2 { margin: 6px 0 4px; }
.win p { margin: 0 0 16px; color: var(--muted); }
.win .row { display: flex; gap: 8px; justify-content: center; }

#toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; transition: .2s; pointer-events: none; z-index: 20; }
#toast.on { opacity: 1; transform: translate(-50%, 0); }

.how { max-width: 560px; }
.how h1 { font-size: 24px; }
.how li { margin: 6px 0; }
@media (hover: hover) { .cell:hover { filter: brightness(1.04); } }
.share-btn { margin-top: 10px; }
.win .row { flex-wrap: wrap; }

/* mode tabs, styled like a segmented control */
.modes { display: flex; gap: 4px; padding: 4px; margin: 4px 0 14px; background: #fff; border-radius: 999px; box-shadow: 0 0 0 1px #eaded8; }
.mode { flex: 1; border: 0; background: none; border-radius: 999px; padding: 9px 4px; font-size: 13px; font-weight: 800;
  text-transform: uppercase; letter-spacing: .02em; color: #8b746d; cursor: pointer; }
.mode.on { background: var(--ink); color: #fff; box-shadow: 0 4px 10px rgba(61,48,44,.18); }

/* locked starter cat */
.cell.fixed { box-shadow: 0 0 0 2px var(--ink), 0 1px 2px rgba(87,67,58,.16); }
.cell.fixed::after { content: ""; position: absolute; right: 6%; bottom: 6%; width: 24%; height: 24%; border-radius: 50%; background: #fff
  url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='5' y='11' width='14' height='10' rx='2' fill='%233d302c'/%3E%3Cpath d='M8 11V8a4 4 0 0 1 8 0v3' stroke='%233d302c' stroke-width='2.5' fill='none'/%3E%3C/svg%3E") center / 70% no-repeat; }

/* hints */
.hintbox { max-width: 580px; margin: 12px auto 0; background: #fff; border-radius: 18px; padding: 14px 16px;
  box-shadow: 0 0 0 1px #eaded8, 0 8px 20px rgba(78,64,55,.08); animation: pop .2s ease-out; }
.hintbox p { margin: 0 0 12px; line-height: 1.45; }
.hintbox .row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.cell.h-why { outline: 3px solid var(--ink); outline-offset: -3px; }
.cell.h-x::after, .cell.h-bad::after { content: ""; position: absolute; inset: 22%; animation: pulse 1.2s ease-in-out infinite;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M4 4L16 16M16 4L4 16' stroke='%23d62839' stroke-width='3.2' stroke-linecap='round'/%3E%3C/svg%3E") center / contain no-repeat; }
.cell.h-bad::after { inset: 8%; opacity: .8; }
.cell.h-cat { outline: 4px solid var(--ok); outline-offset: -4px; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }
