:root {
  --meadow: #3f7d3a;
  --meadow-edge: #2b5a28;
  --cell: #c9e7a9;
  --cell-2: #bfe09b;
  --H: min(calc(100vw - 32px), 520px);
  /* Board width: phone width first, then the height that is left. */
  /* Board width (meadow only); the count column (--lab) sits beside it inside the 16px gutters. */
  --lab: 26px;
  --W: min(calc(100vw - 32px - var(--lab)), 440px, calc(100dvh - 300px - env(safe-area-inset-top) - env(safe-area-inset-bottom)));
  --pad: 6px;
  --gap: 3px;
}
body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

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

/* Header */
.top { width: var(--H); display: flex; align-items: center; gap: 8px; }
.brand { display: flex; flex-direction: column; margin-right: auto; line-height: 1.05; min-width: 0; }
.brand > a { font-weight: 800; font-size: 22px; letter-spacing: -0.03em; color: inherit; text-decoration: none; white-space: nowrap; }
.brand span a { font-size: 13px; font-weight: 600; color: var(--muted); }

.scores { margin-top: auto; width: var(--H); display: grid; grid-template-columns: 1fr auto 1fr; align-items: end; }
.score label { display: block; font-size: 12px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.score output { display: block; font-size: 36px; font-weight: 800; line-height: 1; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.score.best { text-align: right; }
.score.best output { font-size: 26px; color: var(--muted); }
.mid { padding: 0 6px 4px; text-align: center; }
.mode { font-size: 14px; font-weight: 700; color: var(--muted); }

/* Board: count labels outside the meadow, lined up with its cells */
.board-wrap { position: relative; width: calc(var(--W) + var(--lab)); flex: none; }
.field-grid {
  display: grid; grid-template-columns: var(--lab) 1fr; grid-template-rows: var(--lab) auto;
  touch-action: manipulation;
}
.col-counts { display: grid; grid-template-columns: repeat(var(--n), 1fr); gap: var(--gap); padding: 0 var(--pad); align-items: end; }
.row-counts { display: grid; grid-template-rows: repeat(var(--n), 1fr); gap: var(--gap); padding: var(--pad) 0; }
.count {
  display: grid; place-items: center; font-weight: 800; font-variant-numeric: tabular-nums;
  font-size: clamp(15px, calc(var(--W) / var(--n) * 0.42), 22px); color: var(--ink);
  transition: color 0.2s, transform 0.25s cubic-bezier(0.3, 1.5, 0.5, 1);
}
.col-counts .count { padding-bottom: 4px; }
.row-counts .count { padding-right: 6px; }
.count.ok { color: #2c8b45; }
.count.over { color: #c9342a; transform: scale(1.15); }

.field {
  display: grid; grid-template-columns: repeat(var(--n), 1fr); gap: var(--gap); padding: var(--pad);
  background: var(--meadow); border-radius: 16px;
  box-shadow: 0 6px 0 var(--meadow-edge), 0 18px 30px rgba(31, 29, 36, 0.18);
}
.cell {
  position: relative; aspect-ratio: 1; border-radius: 8px; background: var(--cell); cursor: pointer;
  transition: background 0.2s;
}
.cell.alt { background: var(--cell-2); }
.cell.grass { cursor: default; background: #d7efbf; }
.cell > svg, .cell > .a { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.cell .a svg { display: block; width: 100%; height: 100%; overflow: visible; }
.cell .a { padding: 6%; }
.cell .a.pop svg { animation: pop-in 0.38s cubic-bezier(0.3, 1.55, 0.5, 1) both; }
.cell .fence.pop { animation: fade-in 0.2s ease-out both; }
.cell.clash { background: #ffc2b8; }
.cell.clash .a svg { filter: drop-shadow(0 0 4px rgba(201, 52, 42, 0.9)); }
.cell.hinted { box-shadow: inset 0 0 0 3px #ffbe2e; }
.cell.flash { animation: flash 0.7s ease-out; }
.field.solved .cell .a svg { animation: hop 0.6s cubic-bezier(0.3, 1.5, 0.5, 1) both; animation-delay: calc(var(--d, 0) * 40ms); }
@keyframes pop-in { 0% { transform: scale(0.2); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes fade-in { 0% { opacity: 0; transform: scale(0.8); } 100% { opacity: 1; transform: scale(1); } }
@keyframes flash { 0% { box-shadow: inset 0 0 0 3px #ffbe2e, 0 0 0 0 rgba(255, 190, 46, 0.9); } 100% { box-shadow: inset 0 0 0 3px #ffbe2e, 0 0 0 12px rgba(255, 190, 46, 0); } }
@keyframes hop { 0% { transform: translateY(0); } 40% { transform: translateY(-18%) scale(1.06); } 100% { transform: translateY(0); } }

/* Below the board */
.below { width: calc(var(--W) + var(--lab)); margin-bottom: auto; }
.rules { margin: 4px 0 10px; color: var(--muted); font-size: 14px; line-height: 1.4; }
.below-actions { display: flex; align-items: center; gap: 12px; }
.tap-hint { flex: 1; font-size: 13px; font-weight: 600; color: var(--muted); }

/* New dialog */
.sizes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.sizes .btn { flex-direction: column; gap: 2px; padding: 10px 6px; }
.sizes .btn span { font-size: 13px; font-weight: 600; color: var(--muted); }
.sizes .btn.current { background: var(--ink); color: var(--paper); }
.sizes .btn.current span { color: var(--paper); opacity: 0.75; }
.daily-box { margin: 16px 0 12px; padding-top: 14px; border-top: 2px solid var(--paper-2); }
.daily-box .hint { margin-bottom: 0; }

/* Cheer */
.cheer { position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; pointer-events: none; }
.cheer-inner { display: flex; flex-direction: column; align-items: center; gap: 10px; width: 80%; }
.cheer.show .cheer-inner { animation: cheer var(--cheer-ms, 2000ms) ease-out both; }
@keyframes cheer {
  0% { transform: scale(0.5) rotate(-6deg); opacity: 0; }
  10% { transform: scale(1.06) rotate(1deg); opacity: 1; }
  16% { transform: scale(1) rotate(0); }
  84% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.96) translateY(-14px); opacity: 0; }
}
.cheer-title {
  font-weight: 800; font-size: clamp(30px, 9vw, 46px); letter-spacing: -0.03em; line-height: 1;
  color: #fff; text-align: center;
  text-shadow: 0 3px 0 var(--ink), 0 0 18px rgba(0, 0, 0, 0.45);
}
.cheer-card {
  margin: 0; background: #fff; padding: 8px 8px 10px; border-radius: 12px; transform: rotate(-2.5deg);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.4); max-width: 100%;
}
.cheer-card img { display: block; width: 100%; max-height: calc(var(--W) * 0.75); object-fit: cover; border-radius: 6px; }
.cheer-card figcaption { margin-top: 8px; text-align: center; font-weight: 700; font-size: 17px; color: var(--ink); }

#confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 60; }

.over-photo { margin: 14px auto 0; background: #fff; padding: 8px 8px 10px; border-radius: 12px; transform: rotate(-2deg); max-width: 78%; box-shadow: 0 10px 24px rgba(0, 0, 0, 0.2); }
.over-photo img { display: block; width: 100%; max-height: 36dvh; object-fit: cover; border-radius: 6px; }
.over-photo figcaption { margin-top: 8px; font-weight: 700; }
.over-scores { display: grid; grid-template-columns: 1fr 1fr; margin: 18px 0 6px; }
.over-scores label { display: block; font-size: 12px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.over-scores b { font-size: 36px; font-weight: 800; }

@media (prefers-reduced-motion: reduce) {
  .cell .a svg, .cell .fence, .count, .cheer.show .cheer-inner { animation-duration: 0.01ms !important; transition: none !important; }
}
