/* ==== Reset & Base ==== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #21262d;
  --border: #30363d;
  --accent: #7c3aed;
  --accent2: #a855f7;
  --glow: rgba(124, 58, 237, 0.35);
  --text: #e6edf3;
  --text-muted: #8b949e;
  --cell-bg: #1e2530;
  --cell-open: #131820;
  --cell-hover: #2a3140;
  --mine-bg: #450a0a;
  --flag-color: #f59e0b;
  --num-1: #60a5fa; --num-2: #34d399; --num-3: #f87171;
  --num-4: #a78bfa; --num-5: #fb923c; --num-6: #22d3ee;
  --num-7: #e879f9; --num-8: #94a3b8;
  --radius: 12px; --cell-radius: 6px;
  /* セルサイズ: 画面幅に応じて clamp で自動調整 */
  --cell-size: clamp(28px, 8vw, 36px);
  --cell-font: clamp(0.65rem, 2vw, 0.85rem);
}

body {
  font-family: 'Outfit', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-x: hidden;
  position: relative;
  /* スマホでのバウンス・ズーム無効化 */
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* Background animated blobs */
.bg-effect {
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(ellipse 60% 50% at 20% 20%, rgba(124,58,237,0.12) 0%, transparent 60%),
    radial-gradient(ellipse 45% 55% at 80% 80%, rgba(168,85,247,0.08) 0%, transparent 60%);
}

/* ==== Container ==== */
.container {
  position: relative; z-index: 1;
  display: flex; flex-direction: column; align-items: center;
  gap: 24px;
  padding: 32px 20px 40px;
  max-width: 700px; width: 100%;
}

/* ==== Header ==== */
.header { text-align: center; }
.title {
  font-size: clamp(2rem, 5vw, 2.8rem);
  font-weight: 900;
  letter-spacing: -0.5px;
  line-height: 1.1;
}
.title span {
  background: linear-gradient(135deg, #a855f7, #60a5fa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.subtitle {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 0.95rem;
  font-weight: 300;
}

/* ==== Control Panel ==== */
.control-panel {
  display: flex; flex-direction: column; align-items: center;
  gap: 16px; width: 100%;
}

/* Difficulty buttons */
.difficulty-group {
  display: flex; gap: 8px; flex-wrap: wrap; justify-content: center;
}
.diff-btn {
  border: 1.5px solid var(--border);
  background: var(--surface2);
  color: var(--text-muted);
  font-family: 'Outfit', sans-serif;
  font-size: 0.9rem; font-weight: 500;
  padding: 10px 20px;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.2s ease;
  /* タップ領域を確保 */
  min-height: 44px;
  touch-action: manipulation;
}
.diff-btn:hover {
  border-color: var(--accent2);
  color: var(--text);
  background: var(--surface);
}
.diff-btn.active {
  border-color: var(--accent2);
  background: linear-gradient(135deg, #4c0f8f, #7c3aed);
  color: #fff;
  box-shadow: 0 0 14px var(--glow);
}

/* Stats row */
.stats-group {
  display: flex; align-items: center; gap: 16px;
}
.stat-box {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 18px;
  display: flex; align-items: center; gap: 8px;
  min-width: 90px; justify-content: center;
}
.stat-icon { font-size: 1.1rem; }
.stat-value {
  font-size: 1.35rem; font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.05em;
  color: var(--accent2);
}

.reset-btn {
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 50%;
  width: 52px; height: 52px;
  font-size: 1.6rem;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex; align-items: center; justify-content: center;
  touch-action: manipulation;
}
.reset-btn:hover {
  border-color: var(--accent2);
  box-shadow: 0 0 12px var(--glow);
  transform: scale(1.1);
}

/* 旗モードボタン */
.flag-mode-btn {
  display: flex; align-items: center; gap: 8px;
  border: 1.5px solid var(--border);
  background: var(--surface2);
  color: var(--text-muted);
  font-family: 'Outfit', sans-serif;
  font-size: 0.9rem; font-weight: 600;
  padding: 10px 24px;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 44px;
  touch-action: manipulation;
}
.flag-mode-btn:hover {
  border-color: var(--flag-color);
  color: var(--text);
}
.flag-mode-btn.active {
  border-color: var(--flag-color);
  background: rgba(245, 158, 11, 0.15);
  color: var(--flag-color);
  box-shadow: 0 0 14px rgba(245, 158, 11, 0.3);
}
.flag-mode-icon { font-size: 1.1rem; }


/* ==== Board ==== */
.board-wrapper {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 12px;
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.03),
    0 20px 60px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.06);
  /* スマホでタッチスクロール */
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 96vw;
  width: fit-content;
}

.board {
  display: grid;
  gap: 3px;
  /* セルサイズのmin-contentで固まらせない */
  width: max-content;
}

/* ==== Cell ==== */
.cell {
  width: var(--cell-size); height: var(--cell-size);
  background: var(--cell-bg);
  border: 1.5px solid rgba(255,255,255,0.07);
  border-radius: var(--cell-radius);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--cell-font); font-weight: 700;
  cursor: pointer;
  user-select: none;
  /* タッチ時に長押しメニューを出さない */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 0.12s, transform 0.1s, box-shadow 0.12s;
  position: relative;
  overflow: hidden;
}
.cell::before {
  content: '';
  position: absolute; inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.07) 0%, transparent 50%);
  border-radius: var(--cell-radius);
  pointer-events: none;
}
.cell:hover:not(.open):not(.mine-revealed) {
  background: var(--cell-hover);
  transform: scale(1.06);
  box-shadow: 0 0 8px rgba(124,58,237,0.25);
  z-index: 1;
}
.cell:active:not(.open):not(.mine-revealed) {
  transform: scale(0.94);
}

/* Open cell */
.cell.open {
  background: var(--cell-open);
  border-color: rgba(255,255,255,0.04);
  cursor: default;
}
.cell.open::before { display: none; }

/* Number colors */
.cell[data-num="1"] { color: var(--num-1); }
.cell[data-num="2"] { color: var(--num-2); }
.cell[data-num="3"] { color: var(--num-3); }
.cell[data-num="4"] { color: var(--num-4); }
.cell[data-num="5"] { color: var(--num-5); }
.cell[data-num="6"] { color: var(--num-6); }
.cell[data-num="7"] { color: var(--num-7); }
.cell[data-num="8"] { color: var(--num-8); }

/* Flag */
.cell.flagged {
  background: rgba(245, 158, 11, 0.1);
  border-color: var(--flag-color);
}
.cell.flagged::after {
  content: '🚩';
  font-size: 1rem;
}
.cell.flagged::before { display: none; }

/* Mine revealed */
.cell.mine-revealed {
  background: var(--mine-bg);
  border-color: #ef4444;
  animation: shake 0.4s ease;
}
.cell.mine-revealed.exploded {
  background: linear-gradient(135deg, #7f1d1d, #450a0a);
  box-shadow: 0 0 16px rgba(239,68,68,0.5);
}

/* Safe cell on lose */
.cell.safe-reveal {
  background: var(--cell-open);
  opacity: 0.7;
}

/* Open animation */
@keyframes popIn {
  0% { transform: scale(0.75); opacity: 0; }
  60% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}
.cell.pop { animation: popIn 0.18s ease forwards; }

@keyframes shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-4px) rotate(-2deg); }
  40% { transform: translateX(4px) rotate(2deg); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

/* ==== Overlay ==== */
.overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(6px);
  z-index: 100;
  align-items: center; justify-content: center;
}
.overlay.show { display: flex; animation: fadeIn 0.3s ease; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 24px;
  padding: 48px 52px;
  text-align: center;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.04), 0 40px 80px rgba(0,0,0,0.6);
  animation: slideUp 0.35s cubic-bezier(0.34,1.56,0.64,1);
}

@keyframes slideUp {
  from { transform: translateY(40px) scale(0.95); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

.overlay-emoji { font-size: 4rem; margin-bottom: 12px; line-height: 1; }
.overlay-title { font-size: 2rem; font-weight: 900; margin-bottom: 8px; }
.overlay-msg { color: var(--text-muted); font-size: 1rem; margin-bottom: 28px; }

.play-again-btn {
  background: linear-gradient(135deg, #7c3aed, #a855f7);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 12px 32px;
  font-family: 'Outfit', sans-serif;
  font-size: 1rem; font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 0 20px var(--glow);
}
.play-again-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 0 30px rgba(124,58,237,0.55);
}

/* ==== Responsive ==== */

/* タブレット */
@media (max-width: 768px) {
  :root {
    --cell-size: clamp(26px, 7vw, 34px);
    --cell-font: clamp(0.62rem, 1.8vw, 0.8rem);
  }
  .container { gap: 16px; padding: 20px 12px 32px; }
  .stats-group { gap: 10px; }
  .stat-box { padding: 7px 12px; min-width: 76px; }
  .stat-value { font-size: 1.15rem; }
}

/* スマホ縦 */
@media (max-width: 480px) {
  :root {
    --cell-size: clamp(24px, 9.5vw, 32px);
    --cell-font: clamp(0.58rem, 2.2vw, 0.75rem);
  }
  .container { gap: 12px; padding: 16px 8px 24px; }
  .title { font-size: 1.6rem; }
  .subtitle { font-size: 0.82rem; }
  .diff-btn { font-size: 0.82rem; padding: 9px 14px; }
  .stats-group { gap: 8px; }
  .stat-box { padding: 6px 10px; min-width: 68px; }
  .stat-value { font-size: 1rem; }
  .reset-btn { width: 44px; height: 44px; font-size: 1.3rem; }
  .board-wrapper { padding: 8px; border-radius: 12px; }
  .board { gap: 2px; }
  .overlay-card { padding: 28px 20px; margin: 16px; }
  .overlay-title { font-size: 1.6rem; }
  .play-again-btn { padding: 12px 28px; }
}

/* スマホ超小型 (iPhone SE等) */
@media (max-width: 360px) {
  :root {
    --cell-size: clamp(22px, 8.5vw, 28px);
    --cell-font: clamp(0.54rem, 2vw, 0.7rem);
  }
}
