:root {
  --bg: #faf8ef;
  --text: #776e65;
  --text-strong: #5d544b;
  --muted: #a39889;
  --board: #bbada0;
  --cell: rgba(238, 228, 218, 0.38);
  --panel: #ffffff;
  --border: #e6dfd4;
  --btn: #8f7a66;
  --btn-hover: #7c6857;
  --btn-text: #f9f6f2;
  --secondary: #ede6db;
  --secondary-text: #5d544b;
  --danger: #c2410c;
  --shadow: 0 10px 30px rgba(80, 60, 40, 0.18);
  --overlay: rgba(250, 248, 239, 0.78);
  --win: rgba(237, 194, 46, 0.55);
  --gap: clamp(7px, 2.4vw, 14px);
  --move-ms: 110ms;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #1c1917;
  --text: #d6cfc7;
  --text-strong: #f3eee8;
  --muted: #9b9086;
  --board: #3a332e;
  --cell: rgba(255, 255, 255, 0.06);
  --panel: #27221f;
  --border: #3d3631;
  --btn: #9a8370;
  --btn-hover: #ad9682;
  --btn-text: #1c1917;
  --secondary: #332c28;
  --secondary-text: #e7e0d8;
  --danger: #fb923c;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  --overlay: rgba(28, 25, 23, 0.8);
  --win: rgba(237, 194, 46, 0.45);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1917;
    --text: #d6cfc7;
    --text-strong: #f3eee8;
    --muted: #9b9086;
    --board: #3a332e;
    --cell: rgba(255, 255, 255, 0.06);
    --panel: #27221f;
    --border: #3d3631;
    --btn: #9a8370;
    --btn-hover: #ad9682;
    --btn-text: #1c1917;
    --secondary: #332c28;
    --secondary-text: #e7e0d8;
    --danger: #fb923c;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    --overlay: rgba(28, 25, 23, 0.8);
    --win: rgba(237, 194, 46, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.25s, color 0.25s;
}

.app {
  max-width: 500px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}

.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;
}
.hidden { display: none !important; }

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo { margin: 0; font-size: clamp(2.6rem, 12vw, 3.6rem); font-weight: 800; color: var(--text-strong); letter-spacing: -0.02em; }
.scores { display: flex; gap: 8px; }
.score-box {
  position: relative;
  background: var(--board);
  border-radius: 8px;
  padding: 6px 14px 8px;
  min-width: 78px;
  text-align: center;
  color: #fff;
}
.score-box .label { display: block; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: #eee4da; }
.score-box strong { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.score-add {
  position: absolute; left: 50%; top: 26px;
  transform: translateX(-50%);
  font-weight: 800; color: #fff;
  opacity: 0; pointer-events: none; white-space: nowrap;
}
.score-add.show { animation: float-up 0.7s ease-out; }
.score-box.bump strong { display: inline-block; animation: bump 0.22s ease-out; }
@keyframes bump { 40% { transform: scale(1.12); } }
@keyframes float-up {
  0% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -26px); }
}

/* Mode bar */
.modes { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 14px; }
.segmented { display: inline-flex; background: var(--secondary); border-radius: 10px; padding: 3px; }
.segmented button {
  position: relative;
  border: 0; background: transparent; color: var(--secondary-text);
  font: inherit; font-weight: 700; font-size: 0.95rem;
  padding: 8px 16px; border-radius: 8px; cursor: pointer; min-height: 38px;
}
.segmented button[aria-selected="true"] { background: var(--panel); color: var(--text-strong); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
.dot { position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: #f65e3b; }
.size-pick select, .settings select {
  font: inherit; font-weight: 700; color: var(--secondary-text);
  background: var(--secondary); border: 0; border-radius: 10px;
  padding: 9px 12px; min-height: 44px; cursor: pointer;
}
.subtitle { margin: 12px 0 14px; font-size: 0.98rem; line-height: 1.4; min-height: 1.4em; }
.subtitle b { color: var(--text-strong); }

/* Board */
.board-wrap { position: relative; }
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: var(--board);
  border-radius: 12px;
  padding: var(--gap);
  container-type: inline-size;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  transition: background-color 0.25s;
}
.board:active { cursor: grabbing; }
.board:focus-visible { outline: 3px solid #edc22e; outline-offset: 3px; }
.grid { display: grid; grid-template-columns: repeat(var(--size, 4), 1fr); gap: var(--gap); height: 100%; }
.grid .cell { background: var(--cell); border-radius: 8px; }
.tiles { position: absolute; inset: var(--gap); contain: layout style; }

.board.nudge-left  { animation: nudge-x 0.16s; --nx: -6px; }
.board.nudge-right { animation: nudge-x 0.16s; --nx: 6px; }
.board.nudge-up    { animation: nudge-y 0.16s; --ny: -6px; }
.board.nudge-down  { animation: nudge-y 0.16s; --ny: 6px; }
@keyframes nudge-x { 50% { transform: translateX(var(--nx)); } }
@keyframes nudge-y { 50% { transform: translateY(var(--ny)); } }

/* Hint (first visit) */
.hint {
  position: absolute; inset: 0; display: flex; align-items: flex-end; justify-content: center;
  padding: 14px; pointer-events: none; animation: fade 0.3s ease;
}
.hint-card {
  pointer-events: none; /* swipes go through the card to the board */
  background: var(--panel); color: var(--text); border-radius: 12px; box-shadow: var(--shadow);
  padding: 16px; max-width: 360px; text-align: center;
}
.hint-card button { pointer-events: auto; }
.hint-card p { margin: 10px 0 14px; line-height: 1.45; font-size: 0.95rem; }
.hint-card b { color: var(--text-strong); }
.hint-demo {
  position: relative; display: grid; grid-template-columns: repeat(3, 44px); gap: 6px;
  justify-content: center; padding: 6px; background: var(--board); border-radius: 8px; width: max-content; margin: 0 auto;
}
.demo-cell { width: 44px; height: 44px; border-radius: 6px; background: var(--cell); }
.demo-tile {
  position: absolute; top: 6px; left: 6px; width: 44px; height: 44px; border-radius: 6px;
  display: grid; place-items: center; font-weight: 800; font-size: 1.2rem;
  background: #eee4da; color: #776e65;
}
.demo-tile.b { animation: demo-slide 2.6s ease-in-out infinite; }
.demo-tile.a { animation: demo-hide 2.6s infinite; }
.demo-tile.c { background: #ede0c8; opacity: 0; animation: demo-pop 2.6s infinite; }
@keyframes demo-slide {
  0%, 30% { transform: translateX(100px); opacity: 1; }
  48% { transform: translateX(0); opacity: 1; }
  50%, 100% { transform: translateX(0); opacity: 0; }
}
@keyframes demo-hide { 0%, 48% { opacity: 1; } 50%, 100% { opacity: 0; } }
@keyframes demo-pop {
  0%, 48% { opacity: 0; transform: scale(1); }
  58% { opacity: 1; transform: scale(1.18); }
  66%, 92% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; }
}

/* End / win overlay */
.overlay {
  position: absolute; inset: 0; border-radius: 12px;
  background: var(--overlay);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 20px;
  animation: fade 0.35s ease;
  -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
}
.overlay.win { background: var(--win); }
.overlay h2 { margin: 0; font-size: clamp(1.9rem, 9vw, 2.8rem); font-weight: 800; color: var(--text-strong); }
.overlay.win h2 { color: #fff; text-shadow: 0 2px 10px rgba(120, 80, 0, 0.35); }
.overlay p { margin: 8px 0 18px; font-size: 1.05rem; line-height: 1.45; color: var(--text-strong); }
.overlay-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; max-width: 340px; }
.overlay > * { animation: rise 0.4s cubic-bezier(0.2, 0.8, 0.3, 1) backwards; }
.overlay > p { animation-delay: 0.06s; }
.overlay > .overlay-actions { animation-delay: 0.12s; }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 10px;
  font: inherit; font-weight: 700; font-size: 0.98rem;
  padding: 10px 16px; min-height: 44px; cursor: pointer;
  transition: background-color 0.15s, transform 0.08s, opacity 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn:focus-visible, .segmented button:focus-visible, select:focus-visible { outline: 3px solid #edc22e; outline-offset: 2px; }
.btn.primary { background: var(--btn); color: var(--btn-text); }
.btn.primary:hover { background: var(--btn-hover); }
.btn.secondary { background: var(--secondary); color: var(--secondary-text); }
.btn.secondary:hover { filter: brightness(0.96); }
.btn.danger { background: transparent; color: var(--danger); border: 1.5px solid currentColor; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.icon-only { width: 44px; padding: 0; }
.count {
  min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: var(--btn); color: var(--btn-text); font-size: 0.75rem; line-height: 20px;
}

.actions { display: flex; gap: 8px; margin-top: 14px; }
.actions #btn-new { flex: 1; }
.help { margin-top: 16px; text-align: center; font-size: 0.85rem; color: var(--muted); }
kbd {
  font: inherit; font-size: 0.8rem; font-weight: 700;
  padding: 1px 6px; border-radius: 4px; background: var(--secondary); color: var(--secondary-text);
}
@media (hover: none) { .help { display: none; } }

/* Dialogs */
dialog.sheet {
  border: 0; border-radius: 16px; padding: 0;
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 40px);
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
}
dialog.sheet[open] { animation: sheet-in 0.22s ease-out; }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
@keyframes sheet-in { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px 8px; position: sticky; top: 0; background: var(--panel);
}
.sheet-head h2 { margin: 0; font-size: 1.25rem; color: var(--text-strong); }
.close {
  border: 0; background: var(--secondary); color: var(--secondary-text);
  width: 36px; height: 36px; border-radius: 50%; font-size: 1.4rem; line-height: 1; cursor: pointer;
}
#stats-body, .settings { padding: 8px 18px 20px; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--secondary); border-radius: 10px; padding: 10px 6px; text-align: center; }
.stat strong { display: block; font-size: 1.35rem; color: var(--text-strong); font-variant-numeric: tabular-nums; }
.stat span { font-size: 0.75rem; color: var(--muted); }
.section-title { margin: 18px 0 8px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.ach-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.ach { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: 10px; background: var(--secondary); }
.ach .badge {
  flex: none; width: 36px; height: 36px; border-radius: 8px;
  display: grid; place-items: center; font-weight: 800; font-size: 0.8rem;
  background: #edc22e; color: #fff;
}
.ach.locked { opacity: 0.5; }
.ach.locked .badge { background: var(--board); }
.ach b { display: block; color: var(--text-strong); font-size: 0.95rem; }
.ach small { color: var(--muted); }

.settings { display: grid; gap: 10px; }
.settings .row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0; font-weight: 600; color: var(--text-strong); cursor: pointer;
}
.switch {
  appearance: none; -webkit-appearance: none;
  width: 48px; height: 28px; border-radius: 14px; margin: 0;
  background: var(--border); position: relative; cursor: pointer; transition: background-color 0.2s;
}
.switch::before {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3); transition: transform 0.2s;
}
.switch:checked { background: #8f7a66; }
.switch:checked::before { transform: translateX(20px); }
.switch:focus-visible { outline: 3px solid #edc22e; outline-offset: 2px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--text-strong); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-weight: 700; font-size: 0.95rem;
  box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; z-index: 10; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Info section below the game (SEO content, collapsible) ---------- */
.info { margin-top: 36px; line-height: 1.6; font-size: 0.98rem; }
.info > p { margin: 0 0 12px; }
.info a, .site-footer a { color: var(--text-strong); text-decoration: underline; text-underline-offset: 2px; text-decoration-thickness: 1px; }
.info b { color: var(--text-strong); }
.info details { border-top: 1px solid var(--border); }
.info details:last-of-type { border-bottom: 1px solid var(--border); }
.info summary {
  list-style: none; cursor: pointer; padding: 14px 4px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.info summary::-webkit-details-marker { display: none; }
.info summary h2 { margin: 0; font-size: 1.05rem; color: var(--text-strong); }
.info summary::after {
  content: ""; flex: none; width: 9px; height: 9px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(45deg) translate(-2px, -2px); transition: transform 0.2s;
}
.info details[open] summary::after { transform: rotate(-135deg) translate(-2px, -2px); }
.info summary:focus-visible { outline: 3px solid #edc22e; outline-offset: 2px; border-radius: 6px; }
.details-body { padding: 0 4px 16px; }
.details-body h3 { margin: 16px 0 4px; font-size: 1rem; color: var(--text-strong); }
.details-body p { margin: 0 0 10px; }
.details-body ol, .details-body ul { margin: 0 0 10px; padding-left: 1.3em; }
.details-body li { margin-bottom: 6px; }

.site-footer { margin-top: 28px; padding-top: 18px; font-size: 0.88rem; color: var(--muted); text-align: center; }
.site-footer nav { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 16px; margin-bottom: 10px; }
.site-footer nav a, .site-footer nav span { color: var(--text); }
.site-footer nav span { font-weight: 700; color: var(--text-strong); }

/* Strategy article */
.article-page .logo { text-decoration: none; font-size: 2.4rem; }
.article-page .top a.btn { text-decoration: none; color: var(--btn-text); }
.article { margin-top: 20px; }
.article h1 { font-size: clamp(1.7rem, 7vw, 2.2rem); line-height: 1.2; color: var(--text-strong); margin: 0 0 14px; }
.article h2 { font-size: 1.2rem; color: var(--text-strong); margin: 28px 0 8px; }
.article p, .article li { margin: 0 0 12px; }
.article .lead { font-size: 1.08rem; }
.article ul { padding-left: 1.3em; }
.article .cta { margin-top: 28px; text-align: center; }
.article .cta a.btn { color: var(--btn-text); text-decoration: none; }
.logo small { font-size: 0.4em; font-weight: 700; color: var(--muted); letter-spacing: 0; vertical-align: middle; }

/* 404 */
.not-found { text-align: center; padding-top: 15vh; }
.not-found h1 { color: var(--text-strong); font-size: 1.5rem; }
.not-found a.btn { color: var(--btn-text); text-decoration: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
