/* ── Reset & Base ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0a0f;
  --surface: #16161e;
  --tile: #1e1e2a;
  --tile-hover: #272738;
  --tile-pressed: #12121a;
  --border: #2a2a3a;
  --text: #e8e8f0;
  --text-dim: #6a6a80;
  --accent: #6c5ce7;
  --accent-glow: rgba(108, 92, 231, 0.3);
  --danger: #ff4757;
  --danger-glow: rgba(255, 71, 87, 0.4);
  --success: #2ed573;
  --success-glow: rgba(46, 213, 115, 0.3);
  --flag: #ffa502;

  /* number colors */
  --n1: #5b9df5;
  --n2: #2ed573;
  --n3: #ff6b6b;
  --n4: #a55eea;
  --n5: #ff4757;
  --n6: #1abc9c;
  --n7: #e8e8f0;
  --n8: #6a6a80;

  --grid-gap: 3px;
  --tile-size: 40px;
  --radius: 8px;

  color-scheme: dark;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

/* ── App Layout ──────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  padding: env(safe-area-inset-top, 12px) 12px env(safe-area-inset-bottom, 12px);
  gap: 12px;
}

/* ── Header ──────────────────────────────────────── */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 520px;
  padding: 8px 16px;
  background: var(--surface);
  border-radius: 16px;
  border: 1px solid var(--border);
}

.stat {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 70px;
}

.stat-icon {
  width: 18px;
  height: 18px;
  color: var(--text-dim);
}

.stat span {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 1px;
}

#timer-display { justify-content: flex-end; }

#face-btn {
  background: none;
  border: none;
  font-size: 32px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 12px;
  transition: transform 0.1s, background 0.2s;
  line-height: 1;
}
#face-btn:active { transform: scale(0.85); background: var(--tile); }

/* ── Grid ────────────────────────────────────────── */
#grid-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

#grid {
  display: grid;
  gap: var(--grid-gap);
  padding: 6px;
  background: var(--surface);
  border-radius: 14px;
  border: 1px solid var(--border);
  position: relative;
}

/* ── Tiles ───────────────────────────────────────── */
.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 900;
  font-family: 'Inter', system-ui, sans-serif;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
  -webkit-appearance: none;
  outline: none;
}

/* Unrevealed */
.tile.hidden-tile {
  background: var(--tile);
  box-shadow:
    0 2px 4px rgba(0,0,0,0.3),
    inset 0 1px 0 rgba(255,255,255,0.04);
}

.tile.hidden-tile:active {
  transform: scale(0.9);
  background: var(--tile-pressed);
  box-shadow: 0 0 0 rgba(0,0,0,0);
}

/* Breathing idle animation on unrevealed tiles */
.tile.hidden-tile::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(108,92,231,0.06), transparent);
  opacity: 0;
  animation: breathe 4s ease-in-out infinite;
  pointer-events: none;
}
.tile.hidden-tile:nth-child(3n)::after { animation-delay: -1.3s; }
.tile.hidden-tile:nth-child(5n)::after { animation-delay: -2.7s; }
.tile.hidden-tile:nth-child(7n)::after { animation-delay: -0.5s; }

@keyframes breathe {
  0%, 100% { opacity: 0; }
  50% { opacity: 1; }
}

/* Revealed */
.tile.revealed {
  background: var(--bg);
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.4);
  cursor: default;
  animation: revealPop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes revealPop {
  0% { transform: scale(0.7); opacity: 0.5; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

/* Cascade delay for flood-fill reveals */
.tile.revealed.cascade {
  animation: cascadePop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes cascadePop {
  0% { transform: scale(0.5) rotateX(20deg); opacity: 0; }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

/* Number colors */
.tile[data-num="1"] { color: var(--n1); }
.tile[data-num="2"] { color: var(--n2); }
.tile[data-num="3"] { color: var(--n3); }
.tile[data-num="4"] { color: var(--n4); }
.tile[data-num="5"] { color: var(--n5); }
.tile[data-num="6"] { color: var(--n6); }
.tile[data-num="7"] { color: var(--n7); }
.tile[data-num="8"] { color: var(--n8); }

/* Flag */
.tile.flagged {
  background: var(--tile);
  box-shadow:
    0 2px 4px rgba(0,0,0,0.3),
    0 0 12px rgba(255, 165, 2, 0.15);
}
.tile.flagged::before {
  content: '🚩';
  font-size: 18px;
  animation: flagPlant 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes flagPlant {
  0% { transform: scale(0) rotate(-30deg); }
  60% { transform: scale(1.3) rotate(5deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* Mine */
.tile.mine {
  background: var(--danger);
  animation: mineReveal 0.3s ease;
}
.tile.mine.triggered {
  background: #ff2233;
  box-shadow: 0 0 20px var(--danger-glow);
}
.tile.mine::before {
  content: '💣';
  font-size: 18px;
}

@keyframes mineReveal {
  0% { transform: scale(0.5); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* Wrong flag on game over */
.tile.wrong-flag {
  background: var(--tile);
}
.tile.wrong-flag::before {
  content: '❌';
  font-size: 16px;
}

/* ── Screen shake ────────────────────────────────── */
.shake {
  animation: shake 0.5s ease;
}
@keyframes shake {
  0%, 100% { transform: translate(0); }
  10% { transform: translate(-6px, 3px); }
  20% { transform: translate(5px, -4px); }
  30% { transform: translate(-4px, 2px); }
  40% { transform: translate(3px, -2px); }
  50% { transform: translate(-2px, 4px); }
  60% { transform: translate(4px, -1px); }
  70% { transform: translate(-3px, 3px); }
  80% { transform: translate(2px, -3px); }
  90% { transform: translate(-1px, 1px); }
}

/* ── Difficulty nav ──────────────────────────────── */
#difficulty {
  display: flex;
  gap: 8px;
  padding: 6px;
  background: var(--surface);
  border-radius: 14px;
  border: 1px solid var(--border);
}

.diff-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.diff-btn:active { transform: scale(0.95); }
.diff-btn.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 12px var(--accent-glow);
}

/* ── Overlay ─────────────────────────────────────── */
#overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.3s ease;
}
#overlay.hidden { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

#overlay-content {
  text-align: center;
  animation: slideUp 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes slideUp {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

#overlay-emoji { font-size: 64px; margin-bottom: 12px; }
#overlay-title { font-size: 28px; font-weight: 900; margin-bottom: 4px; }
#overlay-subtitle { font-size: 16px; color: var(--text-dim); margin-bottom: 24px; }

#overlay-btn {
  padding: 14px 40px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 20px var(--accent-glow);
  transition: transform 0.1s;
}
#overlay-btn:active { transform: scale(0.95); }

/* ── FX canvas ───────────────────────────────────── */
#fx-canvas {
  position: fixed;
  inset: 0;
  z-index: 90;
  pointer-events: none;
}

/* ── Responsive ──────────────────────────────────── */
@media (max-width: 400px) {
  :root { --tile-size: 34px; --grid-gap: 2px; }
  .stat span { font-size: 17px; }
  .diff-btn { padding: 7px 14px; font-size: 13px; }
}

@media (min-width: 520px) {
  :root { --tile-size: 38px; }
}
