*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #232340;
  --surface3: #2e2e52;
  --accent: #e94560;
  --accent-dim: #a02f43;
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --win: #2ecc71;
  --lose: #e74c3c;
  --cell-bg: #3a3a5c;
  --cell-revealed: #1a1a30;
  --cell-mine: #8b1c1c;
  --border: rgba(255, 255, 255, 0.08);
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  --radius: 12px;
  --cell-size: 36px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  line-height: 1.4;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 12px 24px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── Header ──────────────────────────────────────────── */
header {
  padding: 8px 0 4px;
}

#header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text);
}

#header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#player-name {
  font-size: 0.75rem;
  color: var(--text-dim);
  background: var(--surface2);
  padding: 4px 10px;
  border-radius: 20px;
  cursor: pointer;
  border: 1px solid var(--border);
  white-space: nowrap;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.2s;
}

#player-name:hover {
  background: var(--surface3);
}

#version-badge {
  font-size: 0.7rem;
  font-weight: 600;
  background: var(--accent);
  color: white;
  padding: 3px 8px;
  border-radius: 20px;
  letter-spacing: 0.5px;
}

/* ── Modal ───────────────────────────────────────────── */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}

.modal.visible {
  display: flex;
}

.modal-content {
  background: var(--surface);
  padding: 32px 24px;
  border-radius: 20px;
  text-align: center;
  width: 88%;
  max-width: 320px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.modal-content h2 {
  font-size: 1.4rem;
  margin-bottom: 8px;
}

.modal-content p {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 20px;
}

.modal-content input {
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  border: 1px solid var(--surface3);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 14px;
  text-align: center;
  outline: none;
  transition: border-color 0.2s;
}

.modal-content input:focus {
  border-color: var(--accent);
}

.modal-content button {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.modal-content button:active {
  background: var(--accent-dim);
  transform: scale(0.98);
}

/* ── Difficulty selector ─────────────────────────────── */
#difficulty-selector {
  display: flex;
  gap: 8px;
}

.diff-btn {
  flex: 1;
  padding: 10px 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.diff-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 700;
}

.diff-btn:active:not(.active) {
  background: var(--surface2);
}

/* ── Game info bar ───────────────────────────────────── */
#game-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
}

#mine-count,
#timer {
  font-size: 1rem;
  font-weight: 700;
  min-width: 72px;
  font-variant-numeric: tabular-nums;
}

#timer {
  text-align: right;
}

#reset-btn {
  font-size: 1.6rem;
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 10px;
  border-radius: 8px;
  transition: transform 0.15s;
  line-height: 1;
}

#reset-btn:active {
  transform: scale(0.85);
}

/* ── Board ───────────────────────────────────────────── */
#board-container {
  display: flex;
  justify-content: center;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
}

#board {
  display: grid;
  gap: 2px;
  user-select: none;
  touch-action: manipulation;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.52);
  font-weight: 800;
  border-radius: 4px;
  cursor: pointer;
  background: var(--cell-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: background 0.1s, transform 0.1s;
  position: relative;
}

.cell::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 3px;
  background: linear-gradient(135deg, rgba(255,255,255,0.08) 0%, transparent 60%);
  pointer-events: none;
}

.cell:active:not(.revealed) {
  transform: scale(0.92);
  background: #4a4a72;
}

.cell.revealed {
  background: var(--cell-revealed);
  border-color: rgba(255, 255, 255, 0.04);
  cursor: default;
  transform: none;
}

.cell.revealed::before {
  display: none;
}

.cell.mine-hit {
  background: var(--lose) !important;
  animation: pulse-red 0.3s ease;
}

.cell.mine-revealed {
  background: var(--cell-mine);
}

.cell.flagged {
  background: var(--cell-bg);
}

.cell.wrong-flag {
  background: #5c1c1c;
}

/* Number colors — classic minesweeper palette adapted for dark bg */
.cell[data-num="1"] { color: #5b9bd5; }
.cell[data-num="2"] { color: #70c27a; }
.cell[data-num="3"] { color: #f47c7c; }
.cell[data-num="4"] { color: #9b6dff; }
.cell[data-num="5"] { color: #ff8c42; }
.cell[data-num="6"] { color: #4ecdc4; }
.cell[data-num="7"] { color: #f7c948; }
.cell[data-num="8"] { color: #aaaaaa; }

@keyframes pulse-red {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

/* ── Controls ────────────────────────────────────────── */
#controls {
  display: flex;
  justify-content: center;
}

#flag-toggle {
  padding: 11px 28px;
  border: 2px solid var(--surface3);
  border-radius: 24px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 180px;
}

#flag-toggle.active {
  background: #8b1c1c;
  border-color: #c0392b;
  color: white;
  font-weight: 700;
}

/* ── Game status ─────────────────────────────────────── */
#game-status {
  text-align: center;
  font-size: 1rem;
  font-weight: 700;
  padding: 10px;
  border-radius: var(--radius);
  min-height: 40px;
  transition: all 0.3s;
}

#game-status.win {
  background: rgba(46, 204, 113, 0.15);
  color: var(--win);
  border: 1px solid rgba(46, 204, 113, 0.3);
}

#game-status.lose {
  background: rgba(231, 76, 60, 0.15);
  color: var(--lose);
  border: 1px solid rgba(231, 76, 60, 0.3);
}

/* ── Leaderboard ─────────────────────────────────────── */
#leaderboard-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#leaderboard-toggle {
  width: 100%;
  padding: 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  text-align: center;
}

#leaderboard-toggle:active {
  background: var(--surface2);
}

.hidden {
  display: none !important;
}

#lb-tabs {
  display: flex;
  gap: 8px;
}

.lb-tab {
  flex: 1;
  padding: 9px 4px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.lb-tab.active {
  background: var(--surface3);
  color: var(--text);
  border-color: var(--accent);
}

#lb-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

#lb-content table {
  width: 100%;
  border-collapse: collapse;
}

#lb-content th {
  background: var(--surface2);
  padding: 10px 8px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-align: center;
}

#lb-content td {
  padding: 10px 8px;
  text-align: center;
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
}

#lb-content tr.you td {
  color: #ffd700;
  font-weight: 700;
}

#lb-content tr.rank-1 td:first-child::before {
  content: '🥇 ';
}

#lb-content tr.rank-2 td:first-child::before {
  content: '🥈 ';
}

#lb-content tr.rank-3 td:first-child::before {
  content: '🥉 ';
}

.lb-empty {
  padding: 28px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* ── Footer ──────────────────────────────────────────── */
footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.75rem;
  padding-top: 4px;
}

/* ── Responsive tweaks ───────────────────────────────── */
@media (max-width: 360px) {
  #app {
    padding: 8px 8px 20px;
  }

  header h1 {
    font-size: 1.2rem;
  }
}

@media (hover: hover) {
  .cell:hover:not(.revealed) {
    background: #4a4a72;
  }
}

/* Added by decoupledlane.com: studio credit */
.dls-note { margin: 4px 0 0; font-size: 0.7rem; text-align: center; opacity: 0.7; }
.dls-credit { margin: 12px 0 0; font-size: 0.8rem; text-align: center; }
.dls-credit a { display: inline-flex; align-items: center; gap: 6px; color: inherit; text-underline-offset: 2px; }
.dls-credit img { width: 20px; height: 20px; border-radius: 4px; }
