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

:root {
  --accent: #e8894a;
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --border: #2a2a2a;
  --text: #f0f0f0;
  --muted: #666;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1rem;
  background: var(--bg);
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

/* ── Top bar ─────────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: var(--board-max, min(88vw, 60svh, 420px));
}

.moves-wrap {
  display: flex;
  flex-direction: column;
  line-height: 1;
  gap: 2px;
}

.moves-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.moves-value {
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--text);
}

.size-toggle {
  display: flex;
  gap: 3px;
  background: var(--surface);
  border-radius: 8px;
  padding: 3px;
  border: 1px solid var(--border);
}

.size-btn {
  padding: 0.3rem 0.75rem;
  border: none;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  background: transparent;
  color: var(--muted);
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.size-btn.active {
  background: var(--text);
  color: var(--bg);
}

/* ── Board ──────────────────────────────────────────────────────────────── */
.board-wrap {
  position: relative;
  width: min(88vw, 60svh, 420px);
  aspect-ratio: 1;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid var(--border);
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--n, 3), 1fr);
  gap: 2px;
  width: 100%;
  height: 100%;
  background: var(--bg);
}

.tile {
  width: 100%;
  height: 100%;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
  padding: 0;
  display: block;
  transition: filter 0.08s, transform 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.tile:active:not(.blank) {
  filter: brightness(1.3);
  transform: scale(0.95);
}

.tile.blank {
  background: var(--bg) !important;
  cursor: default;
}

/* ── Win overlay ─────────────────────────────────────────────────────────── */
.win-overlay {
  position: absolute;
  inset: 0;
  background: rgba(13, 13, 13, 0.93);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.win-overlay.open { display: flex; }

.win-icon { font-size: 2.5rem; }

.win-text {
  font-size: 2rem;
  font-weight: 900;
  color: #ffd700;
  letter-spacing: 0.05em;
}

.win-sub {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

.win-btn {
  padding: 0.75rem 2.5rem;
  border: none;
  border-radius: 50px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  min-height: 48px;
  -webkit-tap-highlight-color: transparent;
}

.win-btn:active { opacity: 0.8; }

/* ── Bottom controls ─────────────────────────────────────────────────────── */
.controls {
  display: flex;
  gap: 0.6rem;
  width: 100%;
  max-width: min(88vw, 60svh, 420px);
}

.btn {
  flex: 1;
  padding: 0.8rem 1rem;
  border: none;
  border-radius: 50px;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  touch-action: manipulation;
  min-height: 50px;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.btn:active { opacity: 0.75; transform: scale(0.97); }

.btn.primary {
  background: var(--accent);
  color: #fff;
}

.btn.ghost {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

/* ── Preview overlay ─────────────────────────────────────────────────────── */
/*
  Plain fixed div — no <dialog>, no backdrop-filter.
  Visibility is toggled via the .open class in JS.
  The close button uses both ontouchstart AND onclick in HTML
  so it fires on iOS Safari without any propagation tricks.
*/
.preview-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 999;
  background: rgba(0, 0, 0, 0.95);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1.5rem;
}

.preview-overlay.open { display: flex; }

.preview-img {
  width: 100%;
  max-width: min(88vw, 72svh);
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 12px;
  border: 2px solid var(--border);
  display: block;
  /* pointer-events off so touches go straight to the button below */
  pointer-events: none;
}

.preview-close {
  width: 100%;
  max-width: min(88vw, 72svh);
  padding: 1rem;
  border: none;
  border-radius: 50px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  cursor: pointer;
  touch-action: manipulation;
  min-height: 54px;
  -webkit-tap-highlight-color: transparent;
}

.preview-close:active { opacity: 0.8; }
