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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --panel: #0f3460;
  --accent: #e94560;
  --text: #eaeaea;
  --cell-unrevealed: #2d4a6e;
  --cell-hover: #3a5a80;
  --cell-revealed: #1a2a3e;
  --border: #4a7fa5;
  --flag: #ff6b6b;
  --number-colors: #64b5f6, #81c784, #e57373, #9575cd, #ff8a65, #4dd0e1, #f06292, #90a4ae;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overscroll-behavior: none;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px;
  position: relative;
}

header {
  text-align: center;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 16px;
  letter-spacing: 2px;
}

#controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#stats {
  display: flex;
  align-items: center;
  gap: 20px;
  background: var(--panel);
  padding: 10px 24px;
  border-radius: 8px;
  border: 2px inset rgba(0,0,0,0.4);
  font-family: 'Courier New', monospace;
  font-size: 1.2rem;
  font-weight: bold;
  min-width: 280px;
  justify-content: space-between;
}

#reset-btn {
  font-size: 1.5rem;
  background: var(--cell-unrevealed);
  border: 2px outset rgba(255,255,255,0.2);
  border-radius: 4px;
  cursor: pointer;
  padding: 4px 10px;
  transition: transform 0.1s;
}

#reset-btn:hover { background: var(--cell-hover); }
#reset-btn:active { border-style: inset; transform: scale(0.95); }

#difficulty {
  display: flex;
  gap: 8px;
}

.diff-btn {
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 6px;
  padding: 6px 14px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.15s;
}

.diff-btn:hover { background: var(--panel); }
.diff-btn.active { background: var(--accent); border-color: var(--accent); color: white; }

#board-wrap {
  max-width: 100vw;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

#board {
  display: grid;
  gap: 2px;
  background: var(--panel);
  padding: 8px;
  border-radius: 8px;
  border: 3px outset rgba(255,255,255,0.15);
  user-select: none;
  touch-action: pan-x pan-y;
}

.cell {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: bold;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.1s;
  border: 2px outset rgba(255,255,255,0.2);
  background: var(--cell-unrevealed);
}

.cell:hover:not(.revealed):not(.exploded) { background: var(--cell-hover); }
.cell:active:not(.revealed):not(.exploded) { border-style: inset; }

.cell.revealed {
  background: var(--cell-revealed);
  border: 1px solid rgba(255,255,255,0.05);
  cursor: default;
}

.cell.flagged::after { content: '🚩'; font-size: 1rem; }
.cell.question::after { content: '❓'; font-size: 1rem; }

.cell.exploded {
  background: #8b0000;
  border-color: var(--accent);
}

.cell.mine-revealed::after { content: '💣'; font-size: 1rem; }
.cell.mine-wrong { background: #4a0000; }
.cell.mine-wrong::after { content: '❌'; font-size: 1rem; }

.cell[data-n="1"] { color: #64b5f6; }
.cell[data-n="2"] { color: #81c784; }
.cell[data-n="3"] { color: #e57373; }
.cell[data-n="4"] { color: #9575cd; }
.cell[data-n="5"] { color: #ff8a65; }
.cell[data-n="6"] { color: #4dd0e1; }
.cell[data-n="7"] { color: #f06292; }
.cell[data-n="8"] { color: #90a4ae; }

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  backdrop-filter: blur(4px);
}

#overlay.hidden { display: none; }

#overlay-content {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 40px 60px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: pop 0.3s ease;
}

@keyframes pop {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

#overlay-emoji { font-size: 4rem; }
#overlay-message { font-size: 1.5rem; font-weight: bold; }

#overlay-btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 32px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.1s;
  margin-top: 8px;
}

#overlay-btn:hover { opacity: 0.9; transform: scale(1.05); }

#lb-btn {
  background: var(--surface);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.15s;
}
#lb-btn:hover { background: var(--panel); }

#score-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
}
#score-form.hidden { display: none; }
#score-time-label { font-size: 0.95rem; color: #aaa; }
#score-name {
  background: var(--cell-revealed);
  border: 2px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 1rem;
  padding: 8px 14px;
  text-align: center;
  width: 200px;
  outline: none;
}
#score-name:focus { border-color: var(--accent); }
#score-form button[type="submit"] {
  background: #2a7a4a;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 8px 24px;
  font-size: 0.95rem;
  font-weight: bold;
  cursor: pointer;
  transition: opacity 0.15s;
}
#score-form button[type="submit"]:hover { opacity: 0.85; }

#lb-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  backdrop-filter: blur(4px);
}
#lb-modal.hidden { display: none; }

#lb-content {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 32px 40px;
  min-width: 320px;
  position: relative;
  animation: pop 0.25s ease;
}
#lb-content h2 { text-align: center; margin-bottom: 20px; font-size: 1.4rem; }

#lb-close {
  position: absolute;
  top: 12px; right: 16px;
  background: none;
  border: none;
  color: #aaa;
  font-size: 1.2rem;
  cursor: pointer;
}
#lb-close:hover { color: var(--text); }

#lb-tabs {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 20px;
}
.lb-tab {
  background: var(--cell-unrevealed);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 0.82rem;
  transition: all 0.15s;
}
.lb-tab:hover { background: var(--cell-hover); }
.lb-tab.active { background: var(--accent); border-color: var(--accent); color: white; }

#lb-table { width: 100%; border-collapse: collapse; }
#lb-table th, #lb-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid rgba(255,255,255,0.07);
  font-size: 0.9rem;
}
#lb-table th { color: #aaa; font-weight: 600; font-size: 0.8rem; text-transform: uppercase; }
#lb-table tr:first-child td { color: #ffd700; font-weight: bold; }
#lb-table tr:nth-child(2) td { color: #c0c0c0; }
#lb-table tr:nth-child(3) td { color: #cd7f32; }
#lb-empty { text-align: center; color: #888; padding: 20px 0; }
#lb-empty.hidden { display: none; }

#legend {
  font-size: 0.78rem;
  color: #7a9ab8;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.legend-desktop, .legend-mobile {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  justify-content: center;
}

.legend-mobile { display: none; }

@media (hover: none) and (pointer: coarse) {
  .legend-desktop { display: none; }
  .legend-mobile { display: flex; }
}

@media (max-width: 600px) {
  .cell { width: 30px; height: 30px; font-size: 0.8rem; }
  h1 { font-size: 1.5rem; }
  #lb-content { padding: 24px 20px; min-width: 280px; }
  #stats { min-width: unset; padding: 8px 16px; font-size: 1rem; }
  #difficulty { flex-wrap: wrap; justify-content: center; }
  .legend-desktop { display: none; }
  .legend-mobile { display: flex; }
  #legend { gap: 6px; }
  .legend-mobile { gap: 12px; flex-direction: column; align-items: center; }
}
