:root {
  --bg-color: #F5F7F3;
  --surface-color: #FFFFFF;
  --surface-hover: #D9D6E4;
  --primary-color: #3b82f6;
  --accent-color: #10b981;
  --danger-color: #A61127;
  --text-color: #0A1137;
  --text-muted: #1B1820;
  --cell-bg: #ACA6BB;
  --cell-revealed: #F5F7F3;
  --border-radius: 12px;
  --font-family: 'Inter', system-ui, -apple-system, sans-serif;
  /* Cell number colors — dark shades for light mode (WCAG AA on #F5F7F3) */
  --num-1: #1d4ed8;
  --num-2: #047857;
  --num-3: #b91c1c;
  --num-4: #6d28d9;
  --num-5: #92400e;
  --num-6: #155e75;
  --num-7: #9333ea;
  --num-8: #be185d;
}

body.dark-mode {
  --bg-color: #0A1137;
  --surface-color: #1B1820;
  --surface-hover: #2A2338;
  --text-color: #F5F7F3;
  --text-muted: #ACA6BB;
  --cell-bg: #2A2338;
  --cell-revealed: #0A1137;
  /* Cell number colors — bright shades for dark mode (WCAG AA on #0A1137) */
  --num-1: #3b82f6;
  --num-2: #10b981;
  --num-3: #ef4444;
  --num-4: #a78bfa;
  --num-5: #f59e0b;
  --num-6: #06b6d4;
  --num-7: #a855f7;
  --num-8: #ec4899;
}

body.dark-mode { background-image: radial-gradient(circle at top right, #1B1820 0%, #0A1137 100%); }
body.dark-mode .game-container { border-color: rgba(255, 255, 255, 0.08); }
body.dark-mode select { border-color: rgba(255, 255, 255, 0.1); }
body.dark-mode .icon-btn { border-color: rgba(255, 255, 255, 0.1); }
body.dark-mode .status-bar { border-color: rgba(255, 255, 255, 0.05); box-shadow: inset 0 2px 4px rgba(0,0,0,0.2); }
body.dark-mode .board { border-color: rgba(255, 255, 255, 0.05); }
body.dark-mode .modal { background: rgba(10, 17, 55, 0.85); }
body.dark-mode .modal-content { box-shadow: 0 25px 50px -12px rgba(0,0,0,0.6), 0 0 0 1px rgba(255,255,255,0.1); }
body.dark-mode .primary-btn.secondary { background: linear-gradient(135deg, #2A2338, #1B1820); }

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

body {
  font-family: var(--font-family);
  background-color: var(--bg-color);
  background-image: radial-gradient(circle at top right, #E8E5EF 0%, #F5F7F3 100%);
  transition: background-color 0.3s, color 0.3s;
  color: var(--text-color);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.game-container {
  background-color: var(--surface-color);
  padding: 24px;
  border-radius: 20px;
  box-shadow: 0 25px 50px -12px rgb(0 0 0 / 0.5);
  max-width: 100%;
  border: 1px solid rgba(0, 0, 0, 0.08);
  transition: background-color 0.3s, border-color 0.3s;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 15px;
}

h1 {
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.025em;
  color: var(--text-color);
}

.controls {
  display: flex;
  gap: 12px;
  align-items: center;
}

select {
  background: var(--bg-color);
  color: var(--text-color);
  border: 1px solid rgba(0,0,0,0.1);
  padding: 10px 14px;
  transition: background-color 0.3s, color 0.3s, border-color 0.3s;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  outline: none;
  cursor: pointer;
  appearance: none;
}

select:focus { border-color: var(--primary-color); }

.icon-btn {
  background: var(--bg-color);
  color: var(--text-color);
  border: 1px solid rgba(0,0,0,0.1);
  font-size: 1.5rem;
  transition: background-color 0.3s, color 0.3s, border-color 0.3s;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.icon-btn:hover { background: var(--surface-hover); transform: scale(1.1) rotate(5deg); }
.icon-btn:active { transform: scale(0.95); }

.text-btn {
  width: auto;
  padding: 0 16px;
  font-size: 0.95rem;
  font-weight: 700;
  gap: 6px;
}
.text-btn:hover { transform: translateY(-2px); }

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-color);
  padding: 14px 20px;
  border-radius: 12px;
  margin-bottom: 24px;
  border: 1px solid rgba(0,0,0,0.08);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.08);
  transition: background-color 0.3s, border-color 0.3s;
}

.lcd {
  font-family: 'Courier New', monospace;
  font-size: 2rem;
  font-weight: 800;
  color: #ff5555;
  background: #000;
  padding: 4px 12px;
  border-radius: 6px;
  min-width: 80px;
  text-align: center;
  text-shadow: 0 0 10px rgba(255, 85, 85, 0.6);
  border: 2px solid #1A0508;
}

.mode-toggle { display: none; }

@media (min-width: 651px) {
  .mode-toggle {
    display: flex;
    background: var(--surface-hover);
    border-radius: 8px;
    padding: 3px;
    border: 1px solid rgba(0,0,0,0.1);
  }
  .mode-toggle button {
    background: transparent;
    color: var(--text-muted);
    border: none;
    padding: 8px 16px;
    font-size: 0.95rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 700;
    transition: all 0.2s;
  }
  .mode-toggle button.active {
    background: var(--surface-color);
    color: var(--text-color);
    box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  }
}

body.dark-mode .mode-toggle { border-color: rgba(255,255,255,0.1); }

.mode-toggle-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

@media (min-width: 651px) {
  .mode-toggle-wrapper {
    flex-direction: column;
    gap: 4px;
  }
}

.mode-toggle-label {
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text-muted);
  white-space: nowrap;
  letter-spacing: 0.03em;
  opacity: 0.7;
  text-transform: lowercase;
}

.board-container {
  overflow: auto;
  border-radius: var(--border-radius);
  scrollbar-width: thin;
  scrollbar-color: var(--surface-hover) var(--bg-color);
  padding-bottom: 4px;
}

.board-container::-webkit-scrollbar { height: 10px; width: 10px; }
.board-container::-webkit-scrollbar-track { background: var(--bg-color); border-radius: 5px; }
.board-container::-webkit-scrollbar-thumb { background: var(--surface-hover); border-radius: 5px; }

.board {
  display: grid;
  gap: 3px;
  background-color: var(--bg-color);
  padding: 6px;
  border-radius: var(--border-radius);
  margin: 0 auto;
  width: max-content;
  border: 1px solid rgba(0,0,0,0.08);
}

.cell {
  width: 36px;
  height: 36px;
  background-color: var(--cell-bg);
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: 800;
  font-size: 1.25rem;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s, color 0.3s;
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.1), inset 0 -3px 0 rgba(0,0,0,0.25);
  position: relative;
}

.cell::after {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 50%);
  border-radius: inherit;
  pointer-events: none;
}

.cell:hover:not(.revealed) { background-color: #2A2338; }

.cell:active:not(.revealed) {
  box-shadow: inset 0 2px 5px rgba(0,0,0,0.4);
  transform: scale(0.95);
}

.cell.revealed {
  background-color: var(--cell-revealed);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
  cursor: default;
}
.cell.revealed::after { display: none; }

.cell.mine { background-color: var(--danger-color) !important; font-size: 1.5rem; }
.cell.flag { font-size: 1.3rem; }

.cell[data-val="1"] { color: var(--num-1); text-shadow: 0 0 8px rgba(29,78,216,0.4); }
.cell[data-val="2"] { color: var(--num-2); text-shadow: 0 0 8px rgba(4,120,87,0.4); }
.cell[data-val="3"] { color: var(--num-3); text-shadow: 0 0 8px rgba(185,28,28,0.4); }
.cell[data-val="4"] { color: var(--num-4); text-shadow: 0 0 8px rgba(109,40,217,0.4); }
.cell[data-val="5"] { color: var(--num-5); }
.cell[data-val="6"] { color: var(--num-6); }
.cell[data-val="7"] { color: var(--num-7); }
.cell[data-val="8"] { color: var(--num-8); }

@keyframes pop {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}
.cell.revealed[data-val] { animation: pop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1); }

@keyframes explode {
  0% { transform: scale(1); background-color: var(--danger-color); }
  50% { transform: scale(1.5); background-color: #fff; box-shadow: 0 0 30px #fff, 0 0 60px var(--danger-color); z-index: 20; color: transparent; }
  100% { transform: scale(1.1); background-color: var(--danger-color); box-shadow: inset 0 0 10px rgba(0,0,0,0.5), 0 0 20px var(--danger-color); z-index: 10; }
}

.cell.mine-exploded {
  animation: explode 0.6s cubic-bezier(0.2, 0.8, 0.2, 1) forwards !important;
}

@keyframes boardShake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-6px) rotate(-1deg); }
  20%, 40%, 60%, 80% { transform: translateX(6px) rotate(1deg); }
}

.board.shaking {
  animation: boardShake 0.4s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes winPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); background-color: var(--accent-color); color: #fff; box-shadow: 0 0 15px var(--accent-color); z-index: 5; }
  100% { transform: scale(1); }
}

.cell.win-pulse {
  animation: winPulse 0.6s ease-out forwards;
}

/* Modal styles */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(245, 247, 243, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.modal.hidden { opacity: 0; pointer-events: none; }

.modal-content {
  background: var(--surface-color);
  padding: 40px;
  border-radius: 24px;
  text-align: center;
  transform: scale(1);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.15), 0 0 0 1px rgba(0,0,0,0.08);
  transition: background-color 0.3s;
}

.modal.hidden .modal-content { transform: scale(0.8) translateY(20px); }

.modal h2 { font-size: 3rem; margin-bottom: 8px; font-weight: 800; letter-spacing: -0.025em; }
.modal p { font-size: 1.25rem; margin-bottom: 32px; color: var(--text-muted); }
.modal.win h2 { color: #047857; text-shadow: 0 0 20px rgba(4,120,87,0.3); }
.modal.loss h2 { color: var(--danger-color); text-shadow: 0 0 20px rgba(239,68,68,0.3); }
body.dark-mode .modal.win h2 { color: #10b981; text-shadow: 0 0 20px rgba(16,185,129,0.3); }
body.dark-mode .modal.loss h2 { color: #f87171; text-shadow: 0 0 20px rgba(248,113,113,0.3); }

.primary-btn {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  border: none;
  padding: 14px 32px;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 10px 15px -3px rgba(59, 130, 246, 0.4), 0 4px 6px -2px rgba(59, 130, 246, 0.2);
}

.primary-btn:hover { transform: translateY(-3px); box-shadow: 0 15px 20px -3px rgba(59, 130, 246, 0.5), 0 4px 6px -2px rgba(59, 130, 246, 0.2); }
.primary-btn:active { transform: translateY(0); }

.primary-btn.secondary {
  background: linear-gradient(135deg, #D9D6E4, #FFFFFF);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
}
.primary-btn.secondary:hover { box-shadow: 0 15px 20px -3px rgba(0, 0, 0, 0.4); }

/* Settings dropdown */
.settings-wrapper {
  position: relative;
}

.settings-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--surface-color);
  border: 1px solid rgba(0,0,0,0.1);
  border-radius: 12px;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.15);
  padding: 6px;
  min-width: 160px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transform-origin: top right;
  transition: opacity 0.15s, transform 0.15s;
}

.settings-menu.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.95) translateY(-4px);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-color);
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}

.menu-item:hover { background: var(--surface-hover); }

.menu-icon { font-size: 1.1rem; display: flex; align-items: center; width: 18px; }

body.dark-mode .settings-menu { border-color: rgba(255,255,255,0.1); }

/* Footer */
.game-footer {
  margin-top: 24px;
  padding-bottom: 12px;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  width: 100%;
}

.footer-name { font-weight: 700; color: var(--text-muted); }
.footer-sep { opacity: 0.4; }
.footer-slogan { font-style: italic; }

.footer-link {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 600;
  transition: opacity 0.2s;
}
.footer-link:hover { opacity: 0.75; text-decoration: underline; }

/* Link styles */
a, a:visited, a:focus, a:hover, a:active {
  color: inherit;
}

a:visited.link {
  text-decoration: underline dashed;
  text-decoration-thickness: 50%;
}

a:focus.link, a:hover.link, a:active.link {
  text-decoration: underline wavy;
  text-underline-offset: 2px;
}

.link {
  text-decoration: underline dotted;
  text-underline-offset: 4px;
  text-decoration-thickness: 2.75px;
}

/* Restore footer-link color overridden by a { color: inherit } */
.footer-link { color: var(--primary-color); }

/* Mobile optimization */
@media (max-width: 650px) {
  .game-container { padding: 16px; border-radius: 0; min-height: 100vh; display: flex; flex-direction: column; background: var(--bg-color); border: none; box-shadow: none; }
  body { padding: 0; }
  .status-bar {
    position: sticky;
    top: 0;
    z-index: 50;
    box-shadow: 0 10px 15px -3px rgba(0,0,0,0.5);
    border-radius: 0;
    margin-left: -16px;
    margin-right: -16px;
    padding-left: 16px;
    padding-right: 16px;
  }
  
  .mode-toggle-label { display: none; }
  .mobile-only.mode-toggle {
    display: flex;
    background: var(--surface-hover);
    border-radius: 8px;
    padding: 3px;
    border: 1px solid rgba(255,255,255,0.1);
  }
  .mode-toggle button {
    background: transparent;
    color: var(--text-muted);
    border: none;
    padding: 8px 16px;
    font-size: 0.95rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 700;
    transition: all 0.2s;
  }
  .mode-toggle button.active {
    background: var(--surface-color);
    color: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  }
  .cell { width: 40px; height: 40px; font-size: 1.4rem; /* Larger hit area for touch */ }
  .lcd { font-size: 1.7rem; min-width: 70px; padding: 4px 8px; }
  .board-container { flex-grow: 1; display: flex; align-items: center; margin: 0 -16px; padding: 0 16px 16px 16px; }
  .board { margin: auto; }
  .tutorial-modal-content { padding: 32px 24px 24px; }
  .tutorial-modal-content h2.tutorial-step-title { font-size: 1.35rem; }
  .tutorial-nav { flex-direction: column; }
  .tutorial-nav .primary-btn { width: 100%; }
}

/* --- Tutorial modal --- */
.tutorial-modal-content {
  position: relative;
  max-width: 420px;
  width: 90vw;
  padding: 40px 40px 32px;
}
.tutorial-close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
  transition: background 0.15s, color 0.15s;
}
.tutorial-close-btn:hover {
  background: var(--surface-hover);
  color: var(--text-color);
}
.tutorial-step-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}
.tutorial-modal-content h2.tutorial-step-title {
  font-size: 1.6rem;
  margin-bottom: 12px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text-color);
}
.tutorial-step-body {
  font-size: 1rem;
  line-height: 1.65;
  color: var(--text-muted);
  margin-bottom: 24px;
}
.tutorial-dots {
  display: flex;
  justify-content: center;
  gap: 7px;
  margin-bottom: 24px;
}
.tutorial-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--surface-hover);
  transition: background 0.2s, transform 0.2s;
}
.tutorial-dot.active {
  background: var(--primary-color);
  transform: scale(1.25);
}
.tutorial-nav {
  display: flex;
  gap: 12px;
  justify-content: center;
}
.tutorial-nav .primary-btn {
  padding: 12px 24px;
  font-size: 1rem;
}
.tutorial-nav .primary-btn.secondary {
  background: #e0e0e0;
  color: #333;
  box-shadow: none;
}
body.dark-mode .tutorial-nav .primary-btn.secondary {
  background: #2a2a2a;
  color: #ccc;
  box-shadow: none;
}
