:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-strong: #2563eb;
  --success: #22c55e;
  --danger: #ef4444;
  --flag: #f87171;
  --cell-hidden-top: #3d4d68;
  --cell-hidden-bottom: #2f3d56;
  --cell-open: #111a2c;
  --frame: #172135;
  --fade: 36px;
  --radius: 12px;
  --shadow: 0 12px 32px -14px rgba(2, 6, 23, 0.8), 0 2px 6px rgba(2, 6, 23, 0.35);
  --dur: 180ms;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Segoe UI Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(900px 480px at 50% -220px, rgba(59, 130, 246, 0.16), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.45;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  width: 1em;
  height: 1em;
  flex: none;
}

button {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Header and footer ---------- */

.app-header {
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 36px;
  height: 36px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: linear-gradient(140deg, #60a5fa, var(--accent) 55%, var(--accent-strong));
  color: var(--bg);
  font-size: 24px;
  box-shadow: 0 6px 18px -6px rgba(59, 130, 246, 0.8);
}

h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  gap: 8px;
  flex: none;
}

.app-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  padding: 4px 16px 16px;
  color: var(--muted);
  font-size: 13px;
}

.app-footer a {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 4px;
  color: #93c5fd;
  text-decoration: none;
  border-radius: 6px;
}

.app-footer a:hover {
  text-decoration: underline;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition:
    background-color var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    color var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}

.btn .icon {
  font-size: 18px;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  border-color: transparent;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 20px -10px rgba(59, 130, 246, 0.9);
}

.btn-square {
  width: 44px;
  padding: 0;
  font-size: 14px;
  letter-spacing: 0.05em;
}

:is(.btn, .seg, .status):focus-visible,
.app-footer a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (hover: hover) {
  .btn:hover {
    background: var(--surface-2);
    border-color: #475569;
  }

  .btn-primary:hover {
    background: var(--accent-strong);
    border-color: transparent;
  }
}

/* ---------- Layout ---------- */

.layout {
  flex: 1;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px 20px;
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "hud"
    "controls"
    "board"
    "info";
  align-content: start;
}

.hud {
  grid-area: hud;
}

.controls {
  grid-area: controls;
}

.board-area {
  grid-area: board;
}

.info {
  grid-area: info;
}

.card {
  background: var(--surface);
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- HUD ---------- */

.hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px 12px;
  padding: 12px 14px 10px;
}

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

.stat-end {
  justify-content: flex-end;
  text-align: right;
}

.stat-icon {
  font-size: 22px;
  color: var(--muted);
}

.stat-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.stat-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font: 700 24px/1.1 var(--mono);
  font-variant-numeric: tabular-nums;
}

.status {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #334155, var(--surface) 75%);
  color: #93c5fd;
  font-size: 28px;
  cursor: pointer;
  transition:
    color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}

.status:active {
  transform: scale(0.94);
}

.status[data-status="playing"] {
  color: var(--accent);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.16);
}

.status[data-status="won"] {
  color: var(--success);
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.18);
}

.status[data-status="lost"] {
  color: var(--danger);
  box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.18);
}

.message {
  grid-column: 1 / -1;
  min-height: 1.45em;
  margin: 0;
  font-size: 13.5px;
  text-align: center;
  color: var(--muted);
}

.message[data-tone="won"] {
  color: #86efac;
}

.message[data-tone="lost"] {
  color: #fca5a5;
}

/* ---------- Controls ---------- */

.controls {
  display: grid;
  gap: 8px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid rgba(148, 163, 184, 0.12);
  border-radius: var(--radius);
}

.seg {
  min-height: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
  line-height: 1.15;
  cursor: pointer;
  transition:
    background-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

.seg > span {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.seg small {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11px;
  font-weight: 500;
  opacity: 0.85;
  font-variant-numeric: tabular-nums;
}

.seg[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 6px 16px -8px rgba(59, 130, 246, 0.9);
}

.mode-toggle {
  width: 100%;
}

.mode-toggle[aria-pressed="true"] {
  border-color: rgba(248, 113, 113, 0.55);
  background: rgba(248, 113, 113, 0.12);
  color: #fecaca;
}

.mode-toggle[aria-pressed="true"] .icon {
  color: var(--flag);
}

@media (hover: hover) {
  .seg[aria-pressed="false"]:hover {
    background: rgba(148, 163, 184, 0.1);
    color: var(--text);
  }
}

/* ---------- Board ---------- */

.board-area {
  position: relative;
  align-self: start;
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/*
 * Edge fades with a chevron over a board wider than its frame (the frame is
 * then exactly as wide as this area). Phones only show overlay scrollbars, so
 * these tell the player that the board scrolls, and in which direction.
 */
.board-area::before,
.board-area::after {
  content: "";
  position: absolute;
  top: 1px;
  bottom: 1px;
  z-index: 1;
  width: var(--fade);
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--dur) var(--ease);
}

.board-area::before {
  left: 1px;
  border-radius: calc(var(--radius) - 1px) 0 0 calc(var(--radius) - 1px);
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M15 5l-7 7 7 7' fill='none' stroke='%2393c5fd' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") left 2px center / 18px 18px no-repeat,
    linear-gradient(to right, var(--frame) 25%, rgba(23, 33, 53, 0));
}

.board-area::after {
  right: 1px;
  border-radius: 0 calc(var(--radius) - 1px) calc(var(--radius) - 1px) 0;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 5l7 7-7 7' fill='none' stroke='%2393c5fd' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") right 2px center / 18px 18px no-repeat,
    linear-gradient(to left, var(--frame) 25%, rgba(23, 33, 53, 0));
}

.board-area.has-more-start::before,
.board-area.has-more-end::after {
  opacity: 1;
}

.board-card {
  max-width: 100%;
  overflow: auto;
  padding: 8px;
  border: 1px solid rgba(148, 163, 184, 0.14);
  border-radius: var(--radius);
  background: var(--frame);
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: box-shadow 250ms var(--ease);
}

.board-card.is-scrollable {
  touch-action: pan-x pan-y;
  /* Keyboard focus scrolls cells clear of the edge fades. */
  scroll-padding-inline: var(--fade);
}

.board-card[data-status="won"] {
  box-shadow: var(--shadow), 0 0 0 2px rgba(34, 197, 94, 0.55), 0 0 42px -10px rgba(34, 197, 94, 0.5);
}

.board-card[data-status="lost"] {
  box-shadow: var(--shadow), 0 0 0 2px rgba(239, 68, 68, 0.5), 0 0 42px -10px rgba(239, 68, 68, 0.45);
}

.board {
  --cell: 32px;
  display: grid;
  grid-template-columns: repeat(var(--cols, 9), var(--cell));
  grid-auto-rows: var(--cell);
  gap: 2px;
  width: max-content;
}

.board-row {
  display: contents;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: max(3px, calc(var(--cell) * 0.18));
  background: linear-gradient(180deg, var(--cell-hidden-top), var(--cell-hidden-bottom));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.09), inset 0 -2px 0 rgba(2, 6, 23, 0.3);
  color: var(--text);
  font-family: var(--font);
  font-size: calc(var(--cell) * 0.54);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition:
    filter var(--dur) var(--ease),
    transform 150ms var(--ease);
}

.cell .icon {
  width: 70%;
  height: 70%;
  pointer-events: none;
}

.cell:focus-visible {
  position: relative;
  z-index: 2;
  outline: 2px solid #93c5fd;
  outline-offset: 1px;
}

.cell[data-kind="hidden"]:active {
  transform: scale(0.92);
}

.cell[data-kind="open"],
.cell[data-kind="mine"],
.cell[data-kind="wrong-flag"] {
  background: var(--cell-open);
  box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.07);
  cursor: default;
}

.cell[data-kind="flag"] {
  color: var(--flag);
}

.cell[data-kind="flag"] .icon {
  animation: flag-in 200ms var(--ease);
}

.cell[data-kind="mine"] {
  color: #cbd5e1;
}

.cell[data-kind="wrong-flag"] {
  color: var(--flag);
}

.cell[data-kind="exploded"] {
  background: radial-gradient(circle at 50% 45%, #fb7185, #b91c1c 80%);
  color: #1c0a0d;
  animation: boom 250ms var(--ease);
}

.cell[data-n="1"] { color: #60a5fa; }
.cell[data-n="2"] { color: #4ade80; }
.cell[data-n="3"] { color: #f87171; }
.cell[data-n="4"] { color: #c084fc; }
.cell[data-n="5"] { color: #fb923c; }
.cell[data-n="6"] { color: #22d3ee; }
.cell[data-n="7"] { color: #f472b6; }
.cell[data-n="8"] { color: #e2e8f0; }

.cell.is-new {
  animation: cell-open 220ms var(--ease) both;
  animation-delay: var(--delay, 0ms);
}

.board-card[data-mode="flag"] .cell[data-kind="hidden"] {
  cursor: crosshair;
}

@media (hover: hover) {
  .cell:is([data-kind="hidden"], [data-kind="flag"]):hover {
    filter: brightness(1.18);
  }
}

@keyframes cell-open {
  from {
    opacity: 0;
    transform: scale(0.72);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes flag-in {
  from {
    transform: translateY(-30%) scale(0.6);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

@keyframes boom {
  0% { transform: scale(0.6); }
  60% { transform: scale(1.15); }
  100% { transform: none; }
}

/* ---------- Info cards ---------- */

.info {
  display: grid;
  gap: 12px;
}

.panel-card {
  padding: 14px 16px;
}

h2 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

.best-list {
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.best-list li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 14.5px;
  transition: background-color var(--dur) var(--ease);
}

.best-list li.is-current {
  background: rgba(59, 130, 246, 0.14);
  color: #bfdbfe;
}

.best-time {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.help-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 18px;
  font-size: 14px;
  color: var(--muted);
}

/* ---------- Narrow phones: keep the header on one line ---------- */

@media (max-width: 419px) {
  .brand {
    gap: 8px;
  }

  .brand-mark {
    width: 32px;
    height: 32px;
    font-size: 21px;
  }

  h1 {
    font-size: 18px;
  }

  .btn-primary {
    padding: 0 14px;
  }

  .btn-primary .icon {
    display: none;
  }
}

/* ---------- Smallest phones (320-359 px): drop the logo so the title stays whole ---------- */

@media (max-width: 359px) {
  .app-header {
    gap: 8px;
  }

  .brand-mark {
    display: none;
  }

  h1 {
    font-size: 17px;
  }

  .btn-primary {
    padding: 0 12px;
  }
}

/* ---------- Wide screens: panel left, board right ---------- */

@media (min-width: 900px) {
  .layout {
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      "hud board"
      "controls board"
      "info board";
    gap: 16px 28px;
    align-items: start;
    padding-top: 4px;
  }

  .board-card {
    padding: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
