:root {
  --bg: #eef0fb;
  --bg-glow-1: rgba(124, 92, 255, 0.28);
  --bg-glow-2: rgba(255, 99, 146, 0.22);
  --surface: #ffffff;
  --surface-2: #eceefa;
  --text: #1b1d2e;
  --muted: #5d6280;
  --border: #d9dcee;
  --primary: #5b4cf0;
  --primary-edge: #3a2db8;
  --primary-text: #ffffff;
  --x: #2563eb;
  --o: #e11d48;
  --win: #f5b400;
  --shadow: 0 8px 24px rgba(30, 34, 80, 0.12);
  --radius: 18px;

  /* 3D board */
  --slab-1: #7c5cff;
  --slab-2: #c04bff;
  --slab-edge: #4a2fc0;
  --tile-top: #ffffff;
  --tile-bottom: #ebe7ff;
  --tile-edge: #b9adf5;
  --tile-pressed: #f3f0ff;
  --win-tile-1: #fff3b0;
  --win-tile-2: #ffd34d;
  --win-edge: #d69a00;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0c1d;
    --bg-glow-1: rgba(124, 92, 255, 0.35);
    --bg-glow-2: rgba(236, 72, 153, 0.22);
    --surface: #1a1c33;
    --surface-2: #24274a;
    --text: #eef0ff;
    --muted: #a3a8cc;
    --border: #2f3360;
    --primary: #8b7dff;
    --primary-edge: #5546d6;
    --primary-text: #0b0c1d;
    --x: #60a5fa;
    --o: #fb7185;
    --win: #fcd34d;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
    --slab-1: #3d2a9e;
    --slab-2: #7a23c9;
    --slab-edge: #1f1560;
    --tile-top: #33366a;
    --tile-bottom: #232548;
    --tile-edge: #12132b;
    --tile-pressed: #1c1e3d;
    --win-tile-1: #6b5310;
    --win-tile-2: #a37a00;
    --win-edge: #4a3600;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b0c1d;
  --bg-glow-1: rgba(124, 92, 255, 0.35);
  --bg-glow-2: rgba(236, 72, 153, 0.22);
  --surface: #1a1c33;
  --surface-2: #24274a;
  --text: #eef0ff;
  --muted: #a3a8cc;
  --border: #2f3360;
  --primary: #8b7dff;
  --primary-edge: #5546d6;
  --primary-text: #0b0c1d;
  --x: #60a5fa;
  --o: #fb7185;
  --win: #fcd34d;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  --slab-1: #3d2a9e;
  --slab-2: #7a23c9;
  --slab-edge: #1f1560;
  --tile-top: #33366a;
  --tile-bottom: #232548;
  --tile-edge: #12132b;
  --tile-pressed: #1c1e3d;
  --win-tile-1: #6b5310;
  --win-tile-2: #a37a00;
  --win-edge: #4a3600;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Devanagari", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

/* Slowly drifting coloured glow behind everything */
body::before {
  content: "";
  position: fixed;
  inset: -20%;
  z-index: -1;
  background:
    radial-gradient(40% 35% at 20% 15%, var(--bg-glow-1), transparent 70%),
    radial-gradient(35% 30% at 85% 80%, var(--bg-glow-2), transparent 70%),
    radial-gradient(30% 30% at 80% 20%, var(--bg-glow-2), transparent 70%);
  animation: drift 18s ease-in-out infinite alternate;
}

[hidden] { display: none !important; }

.app {
  max-width: 480px;
  min-height: 100%;
  margin: 0 auto;
  /* --ad-bottom is the height of the native ad banner floating over the bottom (app only). */
  padding: max(16px, env(safe-area-inset-top)) 16px calc(max(16px, env(safe-area-inset-bottom)) + var(--ad-bottom, 0px));
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: calc(100vh - 32px);
  min-height: calc(100dvh - 32px);
}

#home { justify-content: center; position: relative; }
/* Music on/off in the home screen's top-right corner, mirroring the in-game button. */
.home-music { position: absolute; top: 0; right: 0; z-index: 1; }

/* Brand */
.brand { text-align: center; margin-bottom: 8px; }
.brand-icon { border-radius: 22px; box-shadow: 0 6px 0 var(--slab-edge), var(--shadow); animation: float 4s ease-in-out infinite; }
.brand h1 {
  font-size: 2.8rem;
  margin: 18px 0 4px;
  letter-spacing: -0.02em;
  background: linear-gradient(90deg, var(--x), var(--slab-2), var(--o));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.tagline { margin: 0; color: var(--muted); }

.card {
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow);
}

.label { font-size: 0.85rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.goal { margin: 0; color: var(--muted); font-size: 0.9rem; text-align: center; }
/* Six board sizes: two rows of three keep labels like "11×11" readable on phones. */
#size-picker { grid-auto-flow: row; grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* Buttons */
.btn {
  appearance: none;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 16px;
  min-height: 48px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 0 var(--border);
  transition: transform 0.08s ease, box-shadow 0.08s ease, background 0.2s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--border); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn.primary {
  background: linear-gradient(180deg, color-mix(in srgb, var(--primary) 80%, #fff), var(--primary));
  color: var(--primary-text);
  border-color: transparent;
  box-shadow: 0 4px 0 var(--primary-edge), 0 8px 18px color-mix(in srgb, var(--primary) 40%, transparent);
}
.btn.primary:active { box-shadow: 0 1px 0 var(--primary-edge); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); box-shadow: none; }
.btn.big { min-height: 60px; font-size: 1.1rem; }
.btn small { font-weight: 400; color: var(--muted); }
.btn.pulse { animation: pulse 1.4s ease-in-out infinite; }

:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 4px;
  gap: 4px;
}
.segmented button {
  font: inherit;
  font-weight: 600;
  border: 0;
  border-radius: 9px;
  padding: 10px 4px;
  min-height: 44px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}
.segmented button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 2px 0 var(--border), 0 1px 4px rgba(0, 0, 0, 0.15); }

/* Game top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.topbar h2 { font-size: 1.05rem; margin: 0; text-align: center; flex: 1; }
.icon-btn {
  width: 48px; height: 48px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: grid; place-items: center;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--border);
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--border); }
.music-btn .mute-slash { display: none; }
.music-btn[aria-pressed="false"] { color: var(--muted); }
.music-btn[aria-pressed="false"] .mute-slash { display: inline; }
.music-btn[aria-pressed="true"] svg { animation: float 2.4s ease-in-out infinite; }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Scores */
.scores { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; }
.score {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 4px 0 var(--border);
  transition: border-color 0.2s ease, transform 0.25s ease, box-shadow 0.25s ease;
}
.score .who { font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.score strong { font-size: 1.6rem; }
.score.x strong { color: var(--x); }
.score.o strong { color: var(--o); }
.score.draw { min-width: 72px; }
.score.active { transform: translateY(-3px); }
.score.x.active { border-color: var(--x); box-shadow: 0 4px 0 var(--x), 0 8px 20px color-mix(in srgb, var(--x) 30%, transparent); }
.score.o.active { border-color: var(--o); box-shadow: 0 4px 0 var(--o), 0 8px 20px color-mix(in srgb, var(--o) 30%, transparent); }
.score strong.bump { animation: bump 0.5s cubic-bezier(0.3, 1.8, 0.5, 1); }

.status { text-align: center; font-size: 1.25rem; font-weight: 700; margin: 4px 0 0; min-height: 1.6em; }
.hint, .keys-hint { text-align: center; color: var(--muted); margin: 0; min-height: 1.3em; font-size: 0.9rem; }
.keys-hint { font-size: 0.8rem; }
@media (hover: none) { .keys-hint { display: none; } }

/* ---- 3D board ---- */
.board-stage { perspective: 1100px; padding: 6px 0 18px; }

.board-wrap {
  --n: 3;
  --rx: 14deg;
  --ry: 0deg;
  --depth: calc(27px / var(--n));
  position: relative;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  transform: rotateX(var(--rx)) rotateY(var(--ry));
  transition: transform 0.35s ease-out;
}
/* Big boards (9×9, 11×11) have small cells: tilt less so the far rows stay easy to tap. */
.board-wrap.big { --rx: 6deg; }

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  gap: calc(36px / var(--n));
  aspect-ratio: 1;
  padding: calc(36px / var(--n)) calc(36px / var(--n)) calc(36px / var(--n) + var(--depth));
  border-radius: 26px;
  background:
    linear-gradient(160deg, rgba(255, 255, 255, 0.25), transparent 40%),
    linear-gradient(135deg, var(--slab-1), var(--slab-2));
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    0 14px 0 var(--slab-edge),
    0 28px 40px rgba(40, 20, 120, 0.45);
}
.board.deal { animation: boardIn 0.7s cubic-bezier(0.2, 0.9, 0.3, 1.15) backwards; }

.cell {
  appearance: none;
  border: 0;
  border-radius: calc(54px / var(--n));
  cursor: pointer;
  touch-action: manipulation;
  padding: 18%;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, var(--tile-top), var(--tile-bottom));
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.55),
    0 var(--depth) 0 var(--tile-edge),
    0 calc(var(--depth) + 6px) 12px rgba(20, 10, 70, 0.35);
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.3s ease;
}
.board.deal .cell { animation: tileIn 0.5s cubic-bezier(0.2, 1.4, 0.4, 1) backwards; animation-delay: calc(0.15s + var(--i) * 0.03s); }
.cell:not(:disabled):not(.filled):hover {
  transform: translateY(-3px);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.6),
    0 calc(var(--depth) + 3px) 0 var(--tile-edge),
    0 calc(var(--depth) + 12px) 16px rgba(20, 10, 70, 0.35);
}
.cell:not(:disabled):active,
.cell.filled {
  transform: translateY(calc(var(--depth) * 0.6));
  background: linear-gradient(160deg, var(--tile-pressed), var(--tile-bottom));
  box-shadow:
    inset 0 3px 6px rgba(20, 10, 70, 0.18),
    0 calc(var(--depth) * 0.4) 0 var(--tile-edge),
    0 calc(var(--depth) * 0.4 + 4px) 8px rgba(20, 10, 70, 0.3);
}
.cell:disabled { cursor: default; }
.board.over .cell { cursor: pointer; }
.board.over .cell:not(.win) .mark { opacity: 0.3; filter: grayscale(0.5); }

.cell.win {
  background: linear-gradient(160deg, var(--win-tile-1), var(--win-tile-2));
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.6),
    0 calc(var(--depth) * 0.4) 0 var(--win-edge),
    0 0 24px color-mix(in srgb, var(--win) 70%, transparent);
  animation: winGlow 1.2s ease-in-out infinite alternate;
  animation-delay: calc(var(--k, 0) * 0.08s);
}

/* Marks: glossy gradient strokes with an extruded shadow, popping in with a 3D flip */
.mark {
  width: 100%;
  height: 100%;
  fill: none;
  stroke-width: 14;
  stroke-linecap: round;
  overflow: visible;
  transition: opacity 0.3s ease, filter 0.3s ease;
  animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1) both;
}
.mark.x { stroke: url(#grad-x); filter: drop-shadow(0 4px 0 color-mix(in srgb, var(--x) 55%, #000)) drop-shadow(0 0 6px color-mix(in srgb, var(--x) 50%, transparent)); }
.mark.o { stroke: url(#grad-o); filter: drop-shadow(0 4px 0 color-mix(in srgb, var(--o) 55%, #000)) drop-shadow(0 0 6px color-mix(in srgb, var(--o) 50%, transparent)); }
.mark path, .mark circle { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw 0.3s ease-out 0.05s forwards; }
.mark path:nth-child(2) { animation-delay: 0.2s; }
.mark .shine { stroke: rgba(255, 255, 255, 0.6); stroke-width: 4; }
.cell.win .mark { animation: bounce 0.9s ease-in-out infinite; animation-delay: calc(var(--k, 0) * 0.08s); }

.win-line { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.win-line line { stroke: var(--win); stroke-width: calc(36px / var(--n)); stroke-linecap: round; opacity: 0; }
.win-line.show line {
  opacity: 1;
  stroke-dasharray: 1000;
  stroke-dashoffset: 1000;
  filter: drop-shadow(0 0 8px var(--win)) drop-shadow(0 4px 0 var(--win-edge));
  animation: drawLine 0.5s ease-out 0.1s forwards;
}

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; max-width: 420px; width: 100%; margin: 0 auto; }

/* Confetti */
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 10; }
.confetti i {
  position: absolute;
  top: -12px;
  width: 9px;
  height: 14px;
  border-radius: 2px;
  animation: fall var(--t) cubic-bezier(0.25, 0.6, 0.5, 1) forwards;
  animation-delay: var(--d);
}

/* Home navigation row */
.home-nav { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
.home-nav .btn { padding: 10px 4px; flex-direction: column; gap: 2px; font-size: 0.9rem; }
a.btn { text-decoration: none; }
.topbar-spacer { width: 48px; flex-shrink: 0; }
.muted { color: var(--muted); margin: 0; }
.center { text-align: center; }

/* Stats */
.stat-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat-tile {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 4px 0 var(--border);
  padding: 12px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  text-align: center;
}
.stat-tile strong { font-size: 1.7rem; background: linear-gradient(90deg, var(--x), var(--slab-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.stat-tile span { font-size: 0.8rem; color: var(--muted); }
.stat-legend { margin: -4px 0 0; font-size: 0.8rem; color: var(--muted); }
.stat-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stat-table th, .stat-table td { padding: 8px 4px; text-align: center; border-bottom: 1px solid var(--border); }
.stat-table th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.stat-table th[scope="row"] { color: var(--text); font-size: 0.95rem; text-align: left; }
.stat-table tr:last-child > * { border-bottom: 0; }
.stat-table .wld { font-weight: 600; white-space: nowrap; }
.stat-table .wld .w { color: #16a34a; }
.stat-table .wld .l { color: var(--o); }
.stat-table .streak { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 400; }
.stat-table .none { color: var(--muted); }

/* About */
.about-head { flex-direction: row; align-items: center; gap: 16px; }
.about-head h3 { margin: 0; font-size: 1.5rem; }
.about-head .brand-icon { border-radius: 18px; animation: none; }
.about-actions { grid-template-columns: 1fr; }
.how-list { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.card p { margin: 0; line-height: 1.5; }
.links { gap: 4px; }
.link-row { display: flex; align-items: center; gap: 10px; padding: 10px 4px; color: var(--text); text-decoration: none; border-bottom: 1px solid var(--border); min-height: 44px; }
.link-row:last-child { border-bottom: 0; }

/* Website ad slot (AdSense) */
.web-ad { width: 100%; max-width: 420px; margin: 0 auto; min-height: 50px; }

/* Settings dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog[open] { animation: dialogIn 0.25s cubic-bezier(0.2, 1.3, 0.4, 1); }
dialog::backdrop { background: rgba(10, 10, 30, 0.5); backdrop-filter: blur(2px); }
dialog h2 { margin: 0 0 12px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); min-height: 56px; }
.row select { font: inherit; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); min-height: 40px; }
.shop { display: flex; flex-direction: column; gap: 8px; padding: 14px 0 4px; border-bottom: 1px solid var(--border); }
.shop-links { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px; }
.shop-note { margin: 0; text-align: center; color: var(--muted); font-size: 0.9rem; }
.shop-note:empty { display: none; }
.dialog-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

.switch {
  appearance: none;
  width: 52px; height: 30px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.2s ease;
  flex-shrink: 0;
  margin: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.switch:checked { background: var(--primary); }
.switch:checked::after { transform: translateX(22px); }

@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes drawLine { to { stroke-dashoffset: 0; } }
@keyframes pulse { 50% { transform: scale(1.04); } }
@keyframes float { 50% { transform: translateY(-6px) rotate(-3deg); } }
@keyframes drift { to { transform: translate(4%, -3%) rotate(6deg); } }
@keyframes bump { 40% { transform: scale(1.5); } }
@keyframes pop {
  0% { transform: perspective(300px) scale(0.2) rotateY(-120deg); opacity: 0; }
  60% { opacity: 1; }
  100% { transform: perspective(300px) scale(1) rotateY(0); opacity: 1; }
}
@keyframes bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  40% { transform: translateY(-14%) scale(1.08); }
}
@keyframes winGlow {
  to { box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.6), 0 calc(var(--depth) * 0.4) 0 var(--win-edge), 0 0 38px color-mix(in srgb, var(--win) 95%, transparent); }
}
@keyframes boardIn {
  from { transform: translateY(40px) rotateX(55deg) scale(0.85); opacity: 0; }
}
@keyframes tileIn {
  from { transform: translateY(-30px) scale(0.6); opacity: 0; }
}
@keyframes fall {
  0% { transform: translate3d(0, 0, 0) rotateX(0) rotateZ(0); opacity: 1; }
  100% { transform: translate3d(var(--x), 105vh, 0) rotateX(720deg) rotateZ(var(--r)); opacity: 0.9; }
}
@keyframes dialogIn { from { transform: scale(0.9) translateY(10px); opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-delay: 0s !important; animation-iteration-count: 1 !important; transition: none !important; }
  .board-wrap { transform: rotateX(10deg); }
}
