:root {
  color-scheme: light;
  --paper: #edf3fc;
  --ink: #18284b;
  --muted: #415375;
  --rule: #7789a7;
  --field: #202e56;
  --field-text: #f4f7fe;
  --coordinate: #c3d3ee;
  --cover: #a7c6ec;
  --cover-hover: #ccdef6;
  --cover-ink: #3d5f8e;
  --open: #f5f8ff;
  --open-edge: #6b7f9c;
  --mine: #d9e1ef;
  --burst: #ffc295;
  --burst-ink: #7d281b;
  --win-ink: #215947;
  --action: #253f82;
  --action-hover: #192f69;
  --focus: #ffe074;
  --clue-1: #234a91;
  --clue-2: #215947;
  --clue-3: #8a301f;
  --clue-4: #573881;
  --clue-5: #814715;
  --clue-6: #17575b;
  --clue-7: #65254d;
  --clue-8: #18284b;
  --sans: "Trebuchet MS", "Segoe UI", sans-serif;
  --serif: Georgia, "Bitstream Charter", serif;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--paper);
  overscroll-behavior: none;
}

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

body {
  min-height: 100svh;
  margin: 0;
  display: grid;
  align-items: center;
}

::selection { color: var(--field-text); background: var(--action); }

h1, h2, p, dl, dd { margin: 0; }

.game {
  width: min(100%, 70rem);
  margin-inline: auto;
  padding: 2.75rem 2rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 33.5rem);
  grid-template-areas: "intro field" "guide field";
  grid-template-rows: auto auto;
  align-items: start;
  column-gap: clamp(2rem, 6vw, 5.25rem);
  row-gap: 2.75rem;
}

.intro {
  grid-area: intro;
  align-self: end;
}

h1 {
  font-family: var(--serif);
  font-size: clamp(3rem, 5.25vw, 4.25rem);
  line-height: 1;
  letter-spacing: -.035em;
  font-weight: 700;
}

.goal {
  max-width: 28ch;
  margin-block-start: 1.125rem;
  font-size: clamp(1.125rem, 2.1vw, 1.5rem);
  line-height: 1.35;
  text-wrap: balance;
}

.round-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  justify-content: space-between;
  gap: .875rem 1rem;
  border-block: 1px solid var(--rule);
  margin-block-start: 2rem;
  padding-block: .75rem;
}

.game-status { grid-column: 1 / -1; }

.game-status strong {
  display: flex;
  align-items: center;
  gap: .5rem;
  line-height: 1.35;
}

.game-status strong::before {
  content: "";
  inline-size: .55rem;
  block-size: .55rem;
  border: 2px solid currentColor;
  border-radius: 50%;
  flex: none;
}

.game-status[data-phase="playing"] strong::before,
.game-status[data-phase="lost"] strong::before { background: currentColor; }
.game-status[data-phase="won"] strong { color: var(--win-ink); }
.game-status[data-phase="won"] strong::before {
  inline-size: .58rem;
  block-size: .38rem;
  border-width: 0 0 2px 2px;
  border-radius: 0;
  transform: rotate(-45deg) translateY(-1px);
}
.game-status[data-phase="lost"] strong { color: var(--burst-ink); }
.game-status[data-phase="lost"] strong::before { border-radius: 1px; transform: rotate(45deg); }

.game-status p { margin-top: .2rem; color: var(--muted); font-size: .875rem; line-height: 1.4; }

.difficulty {
  position: relative;
  display: grid;
  gap: .25rem;
  min-width: 0;
  max-width: 13rem;
  color: var(--muted);
  font-size: .875rem;
  font-weight: 700;
}

.difficulty::after {
  content: "";
  position: absolute;
  pointer-events: none;
  inset: auto .875rem 1.125rem auto;
  inline-size: .5rem;
  block-size: .5rem;
  color: var(--action);
  border: solid currentColor;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.difficulty select, .tap-modes button {
  appearance: none;
  min-height: 2.75rem;
  padding: .625rem .875rem;
  border: 2px solid var(--action);
  border-radius: 5px;
  color: var(--action);
  background: var(--paper);
  font: 700 .9375rem/1.2 var(--sans);
  cursor: pointer;
}

.difficulty select { width: 100%; padding-right: 2.125rem; }
.difficulty select:hover, .tap-modes button:hover { background: var(--cover-hover); }
.difficulty select:active, .tap-modes button:active { color: var(--field-text); background: var(--action-hover); }
.difficulty:has(select:active)::after { color: var(--field-text); }
.difficulty option { color: var(--ink); background: var(--paper); }

.new-game {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .625rem;
  min-height: 2.75rem;
  padding: .625rem .875rem;
  border: 2px solid var(--action);
  border-radius: 5px;
  color: var(--field-text);
  background: var(--action);
  font: 700 .9375rem/1.2 var(--sans);
  white-space: nowrap;
  cursor: pointer;
}

.tap-modes button[aria-pressed="true"] { color: var(--field-text); background: var(--action); }
.new-game:hover, .tap-modes button[aria-pressed="true"]:hover { background: var(--action-hover); }
.new-game:active, .tap-modes button[aria-pressed="true"]:active { background: var(--field); transform: translateY(1px); }
.new-game:focus-visible, .difficulty select:focus-visible, .tap-modes button:focus-visible {
  outline: 3px solid var(--action);
  outline-offset: 3px;
}

.tap-controls { display: none; max-width: 31rem; margin-top: .75rem; }
.tap-modes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .625rem; }
.tap-controls p { margin-top: .5rem; color: var(--muted); font-size: .9375rem; line-height: 1.45; text-wrap: balance; }
.game[data-touch] .tap-controls { display: block; }

.field-scroll, .round-controls :is(button, select), .tap-modes button {
  touch-action: none;
  -webkit-touch-callout: none;
  user-select: none;
}

.restart-mark {
  width: .8rem;
  height: .8rem;
  border: 1.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  position: relative;
}

.restart-mark::after {
  content: "";
  position: absolute;
  inset: -2px -1px auto auto;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
}

.field {
  grid-area: field;
  background: var(--field);
  color: var(--field-text);
  border-radius: 12px;
  padding: 1.375rem 1.5rem 1.5rem;
  box-shadow: 0 14px 32px rgb(24 40 75 / 18%);
  --track-gap: 3px;
  --coordinate-track: 1.125rem;
}

.field-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .75rem;
  border-bottom: 1px solid var(--rule);
  padding-bottom: .875rem;
  margin-bottom: .5rem;
}

.field-header h2 { font-size: 1.1875rem; letter-spacing: -.01em; }

.field-size {
  display: flex;
  align-items: center;
  gap: .625rem;
  font-size: .875rem;
  font-variant-numeric: tabular-nums;
  color: var(--coordinate);
  white-space: nowrap;
}

.field-size span + span { border-left: 1px solid var(--rule); padding-left: .625rem; }

.readouts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border-bottom: 1px solid var(--rule);
  padding-block: .25rem .625rem;
  margin-bottom: .625rem;
  font-variant-numeric: tabular-nums;
}

.readouts > div { padding-inline: .75rem; }
.readouts > div:first-child { padding-inline-start: 0; }
.readouts > div + div { border-left: 1px solid var(--rule); padding-left: .75rem; }
.readouts dt { color: var(--coordinate); font-size: .875rem; }
.readouts dd { margin-top: .125rem; font-size: 1.0625rem; font-weight: 700; white-space: nowrap; }

.field-scroll {
  padding: 2px;
  overflow-x: auto;
  overscroll-behavior: none;
  scrollbar-color: var(--coordinate) var(--field);
  scroll-padding-inline-start: calc(var(--coordinate-track) + var(--track-gap));
}
.columns, .board { min-inline-size: calc(var(--coordinate-track) + var(--columns, 9) * (32px + var(--track-gap))); }
.columns, .board-row {
  display: grid;
  grid-template-columns: var(--coordinate-track) repeat(var(--columns, 9), minmax(32px, 1fr));
  gap: var(--track-gap);
}

.columns { margin-block: 0 .375rem; }
.columns::before {
  content: "";
  grid-area: 1 / 1;
  background: var(--field);
}
.columns::before, .row-coordinate { position: sticky; inset-inline-start: 0; z-index: 2; }
.columns .coordinate:first-child { grid-column: 2; }
.board { display: grid; gap: var(--track-gap); }

.coordinate {
  display: grid;
  place-items: center;
  color: var(--coordinate);
  background: var(--field);
  font-size: .75rem;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.row-coordinate { justify-items: start; }
.cell-slot { min-width: 0; }

.cell, .sample {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border: 1px solid var(--open-edge);
  border-radius: 4px;
  background: var(--open);
  font: 750 clamp(1.0625rem, 1.8vw, 1.375rem)/1 var(--sans);
  font-variant-numeric: tabular-nums;
}

.cell {
  appearance: none;
  position: relative;
  isolation: isolate;
  width: 100%;
  padding: 0;
  color: var(--ink);
  user-select: none;
}

.cell:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; z-index: 1; }
.cell[data-state="covered"] { background: var(--cover); border-color: var(--cover-ink); color: var(--cover-ink); }
.cell[data-state="covered"]::before {
  content: "";
  position: absolute;
  inset: 1px;
  z-index: -1;
  border-radius: 3px;
  background: url('../assets/minesweeper-tiles.png') 100% 50% / 300% 300% no-repeat;
  filter: grayscale(1);
  opacity: .14;
}

.cell[data-state="covered"] .cell-mark {
  width: .29em;
  height: .29em;
  border-radius: 50%;
  background: currentColor;
}

.cell[data-state="covered"][aria-disabled="false"] { cursor: pointer; }
.cell[data-state="covered"][aria-disabled="false"]:hover { background: var(--cover-hover); }
.cell[data-state="covered"][aria-disabled="false"]:active { border-width: 2px; }

.cell:is([data-state="flagged"], [data-state="correct-flag"], [data-state="wrong-flag"], [data-state="won-flag"]) {
  color: var(--ink);
  background: var(--cover);
  border-color: var(--cover-ink);
}

.cell[data-state="flagged"] { cursor: default; }
.cell[data-state="correct-flag"], .cell[data-state="won-flag"] { color: var(--win-ink); background: var(--cover-hover); }
.cell[data-state="wrong-flag"] { color: var(--burst-ink); background: var(--mine); }

.cell:is([data-state="flagged"], [data-state="correct-flag"], [data-state="wrong-flag"], [data-state="won-flag"]) .cell-mark {
  position: relative;
  width: 1.1em;
  height: .95em;
}

.cell:is([data-state="flagged"], [data-state="correct-flag"], [data-state="wrong-flag"], [data-state="won-flag"]) .cell-mark::before {
  content: "";
  position: absolute;
  inset: 0 25% 0 0;
  background: currentColor;
  clip-path: polygon(21% 0, 100% 25%, 34% 49%, 34% 86%, 63% 86%, 63% 100%, 0 100%, 0 86%, 21% 86%);
}

.cell[data-state="flagged"] .cell-mark::before { inset-inline: 15% 10%; }

.cell[data-state="correct-flag"] .cell-mark::after {
  content: "";
  position: absolute;
  inset: auto 0 .12em auto;
  width: .41em;
  height: .25em;
  border: solid currentColor;
  border-width: 0 0 2px 2px;
  transform: rotate(-45deg);
}

.cell[data-state="wrong-flag"] .cell-mark::after {
  content: "";
  position: absolute;
  inset: auto 0 0 auto;
  width: .43em;
  height: .43em;
  background:
    linear-gradient(45deg, transparent 40%, currentColor 41% 59%, transparent 60%),
    linear-gradient(-45deg, transparent 40%, currentColor 41% 59%, transparent 60%);
}

.cell[data-state="won-flag"] .cell-mark::after {
  content: "";
  position: absolute;
  inset: auto 0 .14em auto;
  width: .31em;
  height: .31em;
  border: 2px solid currentColor;
  border-radius: 50%;
}

.cell[data-state="revealed"] { animation: reveal 150ms ease-out; }
.game:not([data-preset="beginner"]) .cell[data-clue] { font-size: clamp(1.0625rem, 1.56vw, 1.25rem); }
@keyframes reveal { from { background-color: var(--cover-hover); } }

[data-clue="1"] { color: var(--clue-1); }
[data-clue="2"] { color: var(--clue-2); }
[data-clue="3"] { color: var(--clue-3); }
[data-clue="4"] { color: var(--clue-4); }
[data-clue="5"] { color: var(--clue-5); }
[data-clue="6"] { color: var(--clue-6); }
[data-clue="7"] { color: var(--clue-7); }
[data-clue="8"] { color: var(--clue-8); }

.cell[data-state="mined"] { background: var(--mine); }
.cell[data-state="triggered"] { background: var(--burst); color: var(--burst-ink); border: 3px double currentColor; }

.cell:is([data-state="mined"], [data-state="triggered"]) .cell-mark {
  width: .69em;
  height: .69em;
  position: relative;
  isolation: isolate;
  border-radius: 50%;
  background: currentColor;
}

.cell:is([data-state="mined"], [data-state="triggered"]) .cell-mark::before,
.cell:is([data-state="mined"], [data-state="triggered"]) .cell-mark::after {
  content: "";
  position: absolute;
  inset: -.2em;
  background: linear-gradient(currentColor, currentColor) center / 2px 100% no-repeat,
    linear-gradient(currentColor, currentColor) center / 100% 2px no-repeat;
}

.cell:is([data-state="mined"], [data-state="triggered"]) .cell-mark::after { transform: rotate(45deg); }
.cell[data-state="triggered"] .cell-mark { border-radius: 2px; transform: rotate(45deg); }

.guide { grid-area: guide; }
.guide h2 { font-size: 1.125rem; }
.guide p, .clue-key dd { color: var(--muted); font-size: .9375rem; line-height: 1.45; }
.guide p { margin-block: .5rem 1.125rem; max-width: 42ch; }
.clue-key, .clue-key div { display: grid; }
.clue-key { gap: .625rem; }
.clue-key div { grid-template-columns: 1.875rem minmax(0, 1fr); gap: .625rem; align-items: center; }
.sample { width: 1.875rem; font-size: 1rem; }

@media (min-width: 961px) {
  .game[data-preset="intermediate"] {
    grid-template-columns: minmax(0, 1fr) minmax(0, 39rem);
    column-gap: clamp(1.5rem, 4vw, 3.5rem);
  }

  .game[data-preset="expert"] {
    width: min(100%, 77rem);
    grid-template-columns: minmax(0, 1fr) minmax(0, 28rem);
    grid-template-areas: "intro guide" "field field";
    row-gap: 1.5rem;
    padding-block: 1.75rem;
  }

  .game[data-preset="expert"] .intro { align-self: start; }
  .game[data-preset="expert"] .guide { align-self: end; }
  .game[data-preset="expert"] .round-controls { max-width: 31rem; margin-top: 1rem; }
}

@media (max-width: 960px) {
  body { align-items: start; }
  .game {
    width: min(100%, 38rem);
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "intro" "field" "guide";
    grid-template-rows: auto;
    gap: 1.35rem;
    padding: 1.5rem max(12px, 3vw) 2rem;
  }
  .goal { margin-top: .5rem; max-width: none; text-wrap: wrap; }
  .round-controls { margin-top: 1rem; }
}

@media (any-pointer: coarse), (max-width: 960px) {
  .tap-controls { display: block; }
}

@media (max-width: 450px) {
  h1 { font-size: clamp(2.5rem, 11.6vw, 3rem); }
  .game { gap: 1rem; padding-top: 1rem; }
  .goal { font-size: 1rem; }
  .round-controls { gap: .5rem 1rem; margin-top: .75rem; padding-block: .5rem; }
  .game-status { display: flex; flex-wrap: wrap; align-items: baseline; gap: .2rem .65rem; }
  .game-status p { margin-top: 0; }
  .tap-controls { margin-top: .5rem; }
  .field {
    padding: .625rem 7px 7px;
    border-radius: 8px;
    --coordinate-track: 12px;
    --track-gap: 2px;
  }
  .field-header { margin-inline: 4px; padding-bottom: .5rem; margin-bottom: .375rem; }
  .field-header h2 { font-size: 1rem; }
  .readouts { margin-inline: 4px; margin-bottom: .375rem; padding-bottom: .5rem; }
  .coordinate { font-size: .6875rem; }
  .row-coordinate { justify-items: center; }
  .columns { margin-bottom: .25rem; }
}

@media (prefers-reduced-motion: reduce) {
  .cell[data-state="revealed"] { animation: none; }
}

@media (forced-colors: active) {
  .cell[data-state="revealed"] { border-style: dotted; }
  .cell[data-state="triggered"] { border-style: double; }
  .cell[data-state="covered"] .cell-mark,
  .cell:is([data-state="mined"], [data-state="triggered"]) .cell-mark { forced-color-adjust: none; background: ButtonText; }
  .cell:is([data-state="flagged"], [data-state="correct-flag"], [data-state="wrong-flag"], [data-state="won-flag"]) .cell-mark {
    forced-color-adjust: none;
    color: ButtonText;
  }
  .cell:focus-visible, .new-game:focus-visible, .difficulty select:focus-visible,
  .tap-modes button:focus-visible { outline-color: Highlight; }
  .tap-modes button[aria-pressed="true"] {
    color: HighlightText;
    background: Highlight;
    border-color: Highlight;
    forced-color-adjust: none;
  }
}
