:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --ink: #141414;
  --ink-soft: #6b6862;
  --line: #dcd8d0;
  --accent: #1f8a70;
  --accent-soft: #d6ede6;
  --token-white: #fbfaf7;
  --radius: 14px;
  --n: 5;
  /* One grid unit: fits n cells plus a clue column in both width and height. */
  --unit: min(
    calc((100vw - 32px - env(safe-area-inset-left) - env(safe-area-inset-right)) / (var(--n) + 1)),
    calc((100dvh - 300px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) / (var(--n) + 1)),
    64px
  );
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

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

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

/* Header */
.top { width: 100%; display: flex; align-items: center; justify-content: space-between; }
.wordmark { margin: 0; font-size: 1.25rem; font-weight: 700; letter-spacing: -0.02em; display: flex; align-items: center; gap: 4px; }
.dot { width: 14px; height: 14px; border-radius: 50%; display: inline-block; }
.dot-black { background: var(--ink); }
.dot-white { background: var(--token-white); border: 2px solid var(--ink); margin-right: 6px; }
.best { margin: 0; font-size: 0.9rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

/* Size picker */
.sizes {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 3px;
  gap: 2px;
}
.size {
  border: 0;
  background: transparent;
  border-radius: 9px;
  padding: 8px 0;
  font-size: 0.95rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  cursor: pointer;
}
.size[aria-pressed="true"] { background: var(--ink); color: var(--bg); }

/* Stats */
.stats { width: 100%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft); }
.value { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.par { font-size: 0.85rem; font-weight: 500; color: var(--ink-soft); }

/* Board */
.board-wrap { flex: 1; display: flex; align-items: center; justify-content: center; width: 100%; }
.board {
  display: grid;
  grid-template-columns: repeat(calc(var(--n) + 1), var(--unit));
  grid-auto-rows: var(--unit);
}

.clue {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-weight: 700;
  font-size: calc(var(--unit) * 0.42);
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.clue .num {
  min-width: calc(var(--unit) * 0.7);
  height: calc(var(--unit) * 0.7);
  display: grid;
  place-items: center;
  border-radius: 999px;
  transition: background-color 150ms, color 150ms;
}
.clue .mark {
  position: absolute;
  font-size: calc(var(--unit) * 0.3);
  font-weight: 800;
  line-height: 1;
  color: var(--ink-soft);
}
.clue-col .mark { bottom: 0; }
.clue-row .mark { right: calc(var(--unit) * 0.02); }
.clue[data-status="ok"] .num { background: var(--accent); color: #fff; }
.clue[data-status="ok"] .mark { display: none; } /* the filled pill is the non-color cue */

.cell {
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  display: grid;
  place-items: center;
  cursor: pointer;
  perspective: 300px;
  position: relative;
  border-radius: 50%;
}
.cell::before {
  /* Faint grid lines between tokens. */
  content: "";
  position: absolute;
  inset: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  pointer-events: none;
}
.cell.last-col::before { border-right: 0; }
.cell.last-row::before { border-bottom: 0; }
.cell:focus { outline: none; }
.cell:focus-visible .token { outline: 3px solid var(--accent); outline-offset: 3px; border-radius: 50%; }

.token {
  width: 80%;
  height: 80%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 180ms ease-out;
}
.board.no-anim .token { transition: none; }
.cell[data-black="1"] .token { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.face-white { background: var(--token-white); box-shadow: inset 0 0 0 2px var(--ink), 0 1px 2px rgba(0,0,0,0.12); }
.face-black { background: var(--ink); transform: rotateY(180deg); box-shadow: 0 1px 2px rgba(0,0,0,0.25); }

.cell.hint .token::after {
  content: "";
  position: absolute;
  inset: -14%;
  border-radius: 50%;
  border: 3px solid var(--accent);
  animation: pulse 1s ease-in-out infinite;
  pointer-events: none;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(1.08); }
}

.board.solved .cell { cursor: default; }
.board.celebrate .token { animation: ripple 520ms ease-out both; animation-delay: var(--d, 0ms); }
@keyframes ripple {
  0% { scale: 1; }
  40% { scale: 1.18; }
  100% { scale: 1; }
}

/* Controls */
.controls { width: 100%; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 10px 4px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn:active:not(:disabled) { transform: scale(0.96); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn:focus-visible, .size:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* Win overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 20, 0.35);
  display: grid;
  place-items: center;
  padding: 24px;
  animation: fade 200ms ease-out;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.card {
  width: min(100%, 360px);
  background: var(--surface);
  border-radius: 22px;
  padding: 24px 20px 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.2);
  animation: rise 260ms cubic-bezier(.2,.9,.3,1.2);
}
.card h2 { margin: 0 0 6px; font-size: 1.6rem; letter-spacing: -0.02em; }
.badge { display: inline-block; margin: 4px 0 8px; padding: 4px 12px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 0.85rem; }
.badge[hidden] { display: none; }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); margin: 14px 0 8px; }
.summary dt { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft); }
.summary dd { margin: 2px 0 0; font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.verdict { margin: 4px 0 18px; color: var(--ink-soft); font-size: 0.95rem; }
.card-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card-actions .btn { padding: 14px 8px; font-size: 0.95rem; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(16px) scale(0.97); } }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .token, .clue .num { transition: none !important; }
  .cell.hint .token::after, .board.celebrate .token, .overlay, .card { animation: none !important; }
}
