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

:root {
  --bg: #0f172a;
  --bg-2: #111827;
  --panel: #1e293b;
  --panel-2: rgba(30, 41, 59, 0.92);
  --line: rgba(59, 130, 246, 0.45);
  --line-soft: #334155;
  --ink: #e5e7eb;
  --muted: #94a3b8;
  --label: #93c5fd;
  --blue: #0066ff;
  --blue-hover: #1a73e8;
  --blue-bright: #3b82f6;
  --gold: #fbbf24;
  --green: #22c55e;
  --red: #ef4444;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  --radius: 16px;
  --touch: 48px;
  --cell: 36px;
  --gap: 3px;
}

html,
body {
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  background-color: var(--bg);
  color-scheme: dark;
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: Manrope, "Segoe UI", sans-serif;
  color: var(--ink);
  background-color: var(--bg);
  background-image: linear-gradient(180deg, #0f172a 0%, #111827 100%);
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: 10px 10px 20px;
}

.topbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 3px solid var(--blue);
}

.brand {
  display: flex;
  justify-content: center;
  max-width: 100%;
}

.logo {
  display: block;
  height: 72px;
  width: auto;
  max-width: min(46vw, 120px);
  object-fit: contain;
  pointer-events: none;
}

.head-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 100%;
}

.intro {
  margin: 14px 0 16px;
  color: #cbd5e1;
  font-size: 0.9rem;
  text-align: center;
}

.game {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.card,
.board-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--ink);
}

.card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
}

.levels,
.modes {
  display: grid;
  gap: 8px;
}

.levels {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.level,
.mode {
  min-height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line-soft);
  background: rgba(15, 23, 42, 0.55);
  color: #e2e8f0;
  font: 700 0.82rem/1.2 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  padding: 8px 10px;
}

.level.is-active,
.mode.is-active {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

#flagMode.is-active {
  background: #b45309;
  border-color: var(--gold);
  color: #fff;
}

.label {
  display: block;
  margin-bottom: 8px;
  color: var(--label);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.stat {
  min-width: 0;
  padding: 12px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
}

.stat .label {
  margin-bottom: 4px;
  min-height: 2.2em;
  line-height: 1.2;
}

.stat strong {
  display: block;
  font-size: 1.35rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#mines.is-over {
  color: #fca5a5;
}

.help {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.help-rules p {
  margin: 0;
}

.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  background: linear-gradient(180deg, #10233f 0%, #0b1220 42%);
  border: 2px solid var(--blue);
  box-shadow: 0 12px 28px rgba(0, 102, 255, 0.16);
  overflow: hidden;
  transition: border-color 0.35s ease, box-shadow 0.35s ease;
}

.board-wrap.is-flag {
  box-shadow: 0 0 0 2px rgba(251, 191, 36, 0.55), 0 12px 28px rgba(180, 83, 9, 0.22);
}

.board-wrap.is-won {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.35), 0 16px 36px rgba(34, 197, 94, 0.22);
}

.board-wrap.is-lost {
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.38), 0 16px 36px rgba(239, 68, 68, 0.2);
  animation: shake 0.42s ease;
}

.board {
  --gap: 3px;
  display: grid;
  gap: var(--gap);
  width: max-content;
  max-width: 100%;
  max-height: 55vh;
  max-height: 55dvh;
  margin: 0 auto;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: linear-gradient(180deg, #3b4c63 0%, #243044 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 2px 0 #0b1220;
  color: #f8fafc;
  font: 800 calc(var(--cell) * 0.5) / 1 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cell:not(.is-open):not(.is-mine):not(.is-boom):active {
  transform: translateY(1px);
  filter: brightness(1.08);
}

.cell.is-open {
  background: #0b1220;
  box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.16);
  cursor: pointer;
}

.cell.is-flag {
  background: linear-gradient(180deg, #4b3b16 0%, #29200d 100%);
  box-shadow:
    inset 0 0 0 1px rgba(251, 191, 36, 0.45),
    0 2px 0 #0b1220;
}

.cell.is-mine {
  background: #7f1d1d;
  box-shadow: inset 0 0 0 1px rgba(254, 202, 202, 0.35);
  animation: minePop 0.35s ease both;
}

.cell.is-boom {
  background: var(--red);
  color: #fff;
  box-shadow: 0 0 0 2px #fecaca, 0 0 18px rgba(239, 68, 68, 0.7);
  animation: boomPulse 0.7s ease both;
}

.cell.is-wrong {
  background: #1e293b;
  color: #fca5a5;
  box-shadow: inset 0 0 0 2px #ef4444;
  font-size: calc(var(--cell) * 0.46);
}

.cell.n1 { color: #60a5fa; }
.cell.n2 { color: #4ade80; }
.cell.n3 { color: #f87171; }
.cell.n4 { color: #c084fc; }
.cell.n5 { color: #fb923c; }
.cell.n6 { color: #22d3ee; }
.cell.n7 { color: #e2e8f0; }
.cell.n8 { color: #94a3b8; }

.board.is-flag .cell:not(.is-open) {
  cursor: crosshair;
}

.board-wrap.is-won .cell.is-open {
  animation: winPop 0.48s ease both;
  animation-delay: calc(var(--i) * 6ms);
}

.celebrate {
  position: absolute;
  inset: 0;
  z-index: 2;
  overflow: hidden;
  pointer-events: none;
}

.bit {
  position: absolute;
  top: 8%;
  width: 8px;
  height: 12px;
  border-radius: 2px;
  animation: fall 1.15s ease-in forwards;
}

.overlay {
  position: absolute;
  inset: 12px;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  border-radius: 12px;
  background: rgba(8, 14, 28, 0.9);
  color: #f4f7ff;
  text-align: center;
  animation: fadeIn 0.25s ease;
}

.overlay.hidden {
  display: none;
}

.overlay.is-result {
  background: linear-gradient(180deg, transparent 0%, transparent 42%, rgba(8, 14, 28, 0.94) 100%);
  align-items: flex-end;
  pointer-events: none;
}

.overlay.is-result .overlay-inner {
  pointer-events: auto;
  padding: 12px;
  border-radius: 14px;
  background: rgba(8, 14, 28, 0.94);
  border: 1px solid rgba(59, 130, 246, 0.45);
}

.overlay-inner {
  width: min(100%, 320px);
  overflow-wrap: anywhere;
}

.eyebrow {
  margin-bottom: 8px;
  color: #7eb6ff;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.overlay h2 {
  margin-bottom: 8px;
  font-size: 1.7rem;
}

.overlay p {
  margin-bottom: 16px;
  color: #c5d4ea;
  font-size: 0.9rem;
}

.overlay.is-result h2 {
  font-size: 1.35rem;
}

.overlay.is-result p {
  margin-bottom: 12px;
}

.btn {
  min-height: var(--touch);
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  font: 700 0.85rem/1 Manrope, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--blue);
  color: #fff;
}

.btn-primary:hover {
  background: var(--blue-hover);
}

.btn-ghost {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
}

.btn-restart {
  width: 100%;
  background: var(--blue);
  color: #fff;
  box-shadow: 0 8px 18px rgba(0, 102, 255, 0.28);
  letter-spacing: 0.02em;
}

.foot,
.live {
  margin-top: 14px;
  text-align: center;
  color: #94a3b8;
  font-size: 0.75rem;
}

.live {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.btn:focus-visible,
.level:focus-visible,
.mode:focus-visible,
.cell:focus-visible {
  outline: 3px solid #7eb6ff;
  outline-offset: 2px;
  z-index: 1;
}

.board-wrap { order: 1; }
.side-left { order: 2; }
.side-right { order: 3; }

.help-rules {
  display: none;
}

@media (min-width: 769px) {
  body {
    background-image: linear-gradient(180deg, #0f172a 0%, #111827 55%, #0b1220 100%);
  }

  .app {
    padding: 16px 14px 24px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
  }

  .brand {
    grid-column: 2;
    justify-self: center;
  }

  .head-actions {
    grid-column: 3;
    justify-self: end;
    justify-content: flex-end;
  }

  .logo {
    height: 84px;
    max-width: 132px;
  }

  .intro {
    text-align: left;
  }

  .game {
    display: grid;
    grid-template-columns: minmax(200px, 240px) max-content minmax(210px, 250px);
    gap: 16px;
    justify-content: center;
    align-items: stretch;
  }

  .stats {
    grid-template-columns: 1fr;
  }

  .stat .label {
    min-height: 0;
  }

  .stat strong {
    font-size: 1.6rem;
  }

  .board-wrap,
  .side-left,
  .side-right {
    order: unset;
    width: auto;
  }

  .side-right {
    justify-content: space-between;
  }

  .help-rules {
    display: block;
  }

  .btn-restart {
    margin-top: auto;
  }

  .board-wrap {
    width: max-content;
    max-width: 100%;
    max-height: 55vh;
    max-height: 55dvh;
    justify-self: center;
    padding: 16px;
  }

  .board {
    --gap: 4px;
    max-height: 55vh;
    max-height: 55dvh;
  }

  .overlay {
    inset: 16px;
  }
}

@media (max-width: 768px) {
  html,
  body {
    height: 100dvh;
    overflow: hidden;
    background-color: #0f172a;
    color: #e5e7eb;
  }

  .app {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding:
      max(4px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .intro,
  .foot,
  .help-rules {
    display: none;
  }

  .topbar {
    flex-shrink: 0;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding-bottom: 4px;
    border-bottom-width: 2px;
  }

  .logo {
    height: 46px;
    max-width: 72px;
  }

  .head-actions {
    flex-wrap: nowrap;
    justify-content: flex-end;
  }

  .head-actions .btn {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  .game {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    gap: 6px;
    overflow: hidden;
    justify-content: flex-start;
  }

  .side-left,
  .card,
  .stat,
  .btn-ghost,
  .level,
  .mode {
    background-color: rgba(30, 41, 59, 0.92);
    color: #e5e7eb;
    border-color: rgba(59, 130, 246, 0.55);
  }

  .level.is-active,
  #revealMode.is-active {
    background: #0066ff;
    border-color: #0066ff;
    color: #fff;
  }

  #flagMode.is-active {
    background: #b45309;
    border-color: #fbbf24;
    color: #fff;
  }

  .label,
  .stat strong,
  .stat .label {
    color: #e2e8f0;
  }

  .side-left {
    order: 1;
    flex: 0 0 auto;
    gap: 6px;
    padding: 6px;
  }

  .levels,
  .modes {
    gap: 4px;
  }

  .modes {
    grid-template-columns: 1fr 1fr;
  }

  .level,
  .mode {
    min-height: 32px;
    padding: 0 4px;
    font-size: 0.72rem;
  }

  .stats {
    gap: 4px;
  }

  .stat {
    padding: 4px 6px;
  }

  .stat .label {
    min-height: 0;
    margin-bottom: 2px;
    font-size: 0.56rem;
    letter-spacing: 0.04em;
  }

  .stat strong {
    font-size: 0.95rem;
  }

  #mines {
    font-size: 0.86rem;
  }

  .board-wrap {
    order: 2;
    flex: 1 1 auto;
    min-height: 0;
    max-height: 55vh;
    max-height: 55dvh;
    width: 100%;
    margin: 0;
    padding: 8px;
    background-color: #0b1220;
    border-color: #3b82f6;
  }

  .board {
    --gap: 3px;
    max-width: 100%;
    max-height: 55vh;
    max-height: 55dvh;
  }

  .overlay {
    inset: 8px;
    padding: 10px;
  }

  .overlay h2 {
    font-size: 1.25rem;
    margin-bottom: 6px;
  }

  .overlay p {
    margin-bottom: 12px;
    font-size: 0.82rem;
  }

  .overlay.is-result h2 {
    font-size: 1.15rem;
  }

  .side-right {
    order: 3;
    align-self: stretch;
    width: 100%;
    gap: 8px;
    margin: 0;
    margin-top: auto;
    padding: 0;
    flex: 0 0 auto;
    background: transparent;
    border: 0;
    box-shadow: none;
  }

  #restartBtn,
  #startBtn {
    width: 100%;
    min-height: 48px;
    margin: 0;
    padding: 12px 16px;
    font-size: 1rem;
  }

  #restartBtn {
    background: #0066ff;
    color: #fff;
    border-color: #0066ff;
  }
}

@media (max-width: 380px) {
  .head-actions .btn {
    padding: 6px 7px;
    font-size: 0.68rem;
  }

  .mode {
    font-size: 0.64rem;
  }
}

@media (max-width: 768px) and (max-height: 680px) {
  .logo {
    height: 40px;
  }

  .stat strong,
  #mines {
    font-size: 0.74rem;
  }

  .level,
  .mode,
  .head-actions .btn {
    min-height: 30px;
  }

  .overlay h2 {
    font-size: 1.1rem;
  }
}

@keyframes winPop {
  0% { transform: scale(0.86); filter: brightness(1.5); }
  55% { transform: scale(1.06); }
  100% { transform: scale(1); filter: brightness(1); }
}

@keyframes minePop {
  0% { transform: scale(0.55); }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes boomPulse {
  0% { transform: scale(0.7); }
  40% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(2px); }
}

@keyframes fall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(52vh) rotate(240deg); opacity: 0; }
}

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

@media (prefers-reduced-motion: reduce) {
  .cell,
  .board-wrap,
  .board-wrap.is-won .cell.is-open,
  .cell.is-mine,
  .cell.is-boom,
  .overlay,
  .bit {
    animation: none;
    transition: none;
  }
}
