:root {
  color-scheme: light;
  --paper: #e7ebdc;
  --stage: #f4f5eb;
  --ink: #233b30;
  --muted: #4d6051;
  --edge: #586e58;
  --grid-line: #53684f;
  --covered: #c6d4b7;
  --covered-hover: #dbe4cc;
  --open: #f7f5e8;
  --clay: #f4ba9e;
  --clay-hover: #f8cdb5;
  --clay-pressed: #eaa987;
  --rust: #80361f;
  --wrong: #f6dace;
  --impact: #e9a18b;
  --neutral-focus: #853513;
  --white-ink: #f7f8ec;
  --count-blue: #245377;
  --count-green: #315b34;
  --count-red: #923d29;
  --count-plum: #6a3e67;
  --count-brown: #76441f;
  --count-teal: #21615e;
  --count-gray: #50514b;
  --font-body: "Trebuchet MS", "Liberation Sans", sans-serif;
  --font-display: "Bitstream Charter", Georgia, "Liberation Serif", serif;
  --font-data: "Courier New", "Liberation Mono", monospace;
  --radius: 6px;
  --space: 12px;
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--paper);
  overscroll-behavior: none;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; overscroll-behavior: none; }
h1, h2, p, dl, dd { margin: 0; }
button, input { font: inherit; }
button { color: inherit; }
::selection { color: var(--white-ink); background: var(--ink); }

.game-shell {
  --gutter: clamp(20px, 5.2vw, 104px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 20px;
  min-height: 100svh;
  padding: 24px var(--gutter) 26px;
}

.masthead {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding-block: 3px 15px;
  border-bottom: 1px solid var(--edge);
}

.wordmark { display: flex; align-items: center; gap: 16px; }
.masthead h1 {
  font: 700 clamp(2.5rem, 4.4vw, 5rem)/1 var(--font-display);
  letter-spacing: -.037em;
}
.masthead p { color: var(--muted); font-size: .9375rem; }

.field-mark {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  flex: 0 0 auto;
  gap: 2px;
  width: clamp(32px, 3.4vw, 56px);
  aspect-ratio: 1;
  padding: 2px;
  background: var(--ink);
}
.field-mark i { display: grid; place-items: center; background: var(--covered); }
.field-mark i:nth-child(2) { background: var(--clay); }
.field-mark i:nth-child(3) { background: var(--stage); }
.field-mark i:nth-child(2)::after, .field-mark i:nth-child(4)::after {
  content: "";
  width: 29%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--ink);
}

.workspace {
  display: grid;
  grid-template-columns: clamp(212px, 22vw, 272px) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  gap: 22px clamp(20px, 3vw, 44px);
  min-width: 0;
  min-height: 0;
}

.control-rail { grid-column: 1; grid-row: 1; }
.presets { display: grid; gap: 7px; min-inline-size: 0; margin: 0; padding: 0; border: 0; }
.presets legend, .help h2 { margin-bottom: 10px; padding: 0; font-size: .9375rem; font-weight: 700; }
.preset { position: relative; display: block; cursor: pointer; }
.preset input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: inherit;
  touch-action: none;
}
.preset-face {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 9px 12px;
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  background: var(--stage);
  font-size: .9375rem;
  font-weight: 700;
  pointer-events: none;
}
.preset-face::before {
  content: "";
  width: 12px;
  aspect-ratio: 1;
  border: 1px solid currentColor;
  border-radius: 50%;
}
.preset-face small {
  color: var(--muted);
  font: 700 .75rem/1 var(--font-data);
  white-space: nowrap;
}
.preset input:checked + .preset-face { border-color: var(--ink); background: var(--ink); color: var(--white-ink); }
@media (hover: hover) {
  .preset:hover .preset-face { background: var(--covered-hover); }
  .preset:hover input:checked + .preset-face { background: var(--count-green); }
}
.preset input:active + .preset-face { border-color: var(--ink); background: var(--covered); }
.preset input:checked:active + .preset-face { background: var(--ink); }
.preset input:checked + .preset-face small { color: var(--white-ink); }
.preset input:checked + .preset-face::before { background: currentColor; box-shadow: inset 0 0 0 2px var(--ink); }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 13px; }
.actions button {
  appearance: none;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--ink);
  font-size: .875rem;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  touch-action: none;
}
.restart { flex: 1 1 100%; border-radius: var(--radius); background: var(--clay); }
@media (hover: hover) {
  .restart:hover { background: var(--clay-hover); }
}
.restart:active { background: var(--clay-pressed); }
.restart-icon {
  position: relative;
  display: block;
  width: 13px;
  height: 13px;
  border: 1.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  transform: rotate(-34deg);
}
.restart-icon::after {
  content: "";
  position: absolute;
  top: -2px;
  right: -2px;
  width: 5px;
  height: 5px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
}
.modes { display: grid; grid-template-columns: repeat(2, 1fr); width: 100%; }
.modes button { background: var(--stage); }
.modes button:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.modes button:last-child { border-radius: 0 var(--radius) var(--radius) 0; border-left: 0; }
.modes button[aria-pressed="true"] { color: var(--white-ink); background: var(--ink); }
@media (hover: hover) {
  .modes button:hover { background: var(--covered-hover); }
  .modes button[aria-pressed="true"]:hover { background: var(--count-green); }
}
.modes button:active { background: var(--covered); }
.modes button[aria-pressed="true"]:active { background: var(--ink); }
.touch-help { margin-top: 8px; color: var(--muted); font-size: .8125rem; line-height: 1.4; }

button:focus-visible, .preset input:focus-visible + .preset-face {
  position: relative;
  z-index: 1;
  outline: 2px solid var(--neutral-focus);
  outline-offset: 2px;
}

.help { grid-column: 1; grid-row: 2; align-self: start; padding-top: 15px; border-top: 1px solid var(--edge); }
.help h2 { margin-bottom: 6px; }
.help p { color: var(--muted); font-size: .875rem; line-height: 1.45; }
.help dl { display: grid; gap: 8px; margin-top: 16px; font-size: .8125rem; }
.help dl div { display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 8px; }
.help dt { font-weight: 700; }
.help dd { color: var(--muted); }

.stage {
  grid-column: 2;
  grid-row: 1 / 3;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  background: var(--stage);
}
.stage-header {
  display: grid;
  grid-template-columns: minmax(min-content, 1fr) minmax(224px, 48%);
  align-items: center;
  padding: 12px 18px;
  border-bottom: 1px solid var(--edge);
}
.game-state {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 1rem;
  font-weight: 700;
}
.game-state::before {
  content: "";
  flex: 0 0 10px;
  height: 10px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
}
.game-state[data-phase="playing"]::before { background: currentColor; }
.game-state[data-phase="won"]::before { border-radius: 1px; background: currentColor; }
.game-state[data-phase="lost"] { color: var(--rust); }
.game-state[data-phase="lost"]::before { border-radius: 1px; background: currentColor; transform: rotate(45deg); }

.readings { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.readings > div { display: grid; gap: 2px; padding-inline: 12px 2px; border-left: 1px solid var(--edge); }
.readings dt { color: var(--muted); font-size: .75rem; font-weight: 700; white-space: nowrap; }
.readings dd { font: 700 1.375rem/1.2 var(--font-data); font-variant-numeric: tabular-nums; white-space: nowrap; }

.play-space { min-width: 0; min-height: 0; }
.board-scroll {
  display: block;
  max-width: 100%;
  max-height: max(232px, 100svh - 232px);
  padding: var(--space);
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-color: var(--edge) var(--stage);
  touch-action: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.board {
  --min-cell: 26px;
  --cell: max(var(--min-cell), min(46px, (100vw - 52px) / var(--columns)));
  width: max-content;
  margin-inline: auto;
  border: 1px solid var(--grid-line);
  background: var(--grid-line);
}
.board-row { display: grid; grid-template-columns: repeat(var(--columns), var(--cell)); }
.cell {
  appearance: none;
  display: grid;
  place-items: center;
  width: var(--cell);
  height: var(--cell);
  padding: 0;
  border: 0;
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
  border-radius: 0;
  background: var(--covered);
  font: 700 calc(var(--cell) * .53)/1 var(--font-data);
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  position: relative;
  cursor: pointer;
}
.board-row:last-child .cell { border-bottom-width: 0; }
.cell:last-child { border-right-width: 0; }
.cell:focus-visible { outline-offset: -3px; }
@media (hover: hover) {
  .board:is([data-phase="ready"], [data-phase="playing"]) .cell:is([data-view="covered"], [data-view="flagged"]):hover {
    background: var(--covered-hover);
  }
}
.board:is([data-phase="ready"], [data-phase="playing"]) .cell:is([data-view="covered"], [data-view="flagged"]):active {
  background: var(--clay);
}
.board:is([data-phase="won"], [data-phase="lost"]) .cell { cursor: default; }
.cell[data-view="covered"] .cell-mark {
  width: max(3px, 10%);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--grid-line);
}
.cell[data-view="revealed"], .cell[data-view="mine"] { background: var(--open); cursor: default; }
.cell[data-count="1"] { color: var(--count-blue); }
.cell[data-count="2"] { color: var(--count-green); }
.cell[data-count="3"] { color: var(--count-red); }
.cell[data-count="4"] { color: var(--count-plum); }
.cell[data-count="5"] { color: var(--count-brown); }
.cell[data-count="6"] { color: var(--count-teal); }
.cell[data-count="7"] { color: var(--count-gray); }

.cell:is([data-view="flagged"], [data-view="wrong"]) .cell-mark {
  position: relative;
  width: 53%;
  aspect-ratio: 1;
  color: var(--rust);
}
.cell:is([data-view="flagged"], [data-view="wrong"]) .cell-mark::before {
  content: "";
  position: absolute;
  inset: 3% 4% 7% 10%;
  background: currentColor;
  clip-path: polygon(0 0, 100% 0, 76% 28%, 100% 56%, 17% 56%, 17% 100%, 0 100%);
}
.cell[data-view="wrong"] { background: var(--wrong); cursor: default; }
.cell[data-view="wrong"] .cell-mark::after {
  content: "";
  position: absolute;
  inset: 43% -12% auto;
  height: 16%;
  background: var(--ink);
  outline: 1px solid var(--wrong);
  transform: rotate(42deg);
}
.cell[data-view="triggered"] { background: var(--impact); cursor: default; }
.cell:is([data-view="mine"], [data-view="triggered"]) .cell-mark {
  width: 53%;
  aspect-ratio: 1;
  background: url("../assets/mine-space.png") center / contain no-repeat;
}
.cell[data-view="triggered"]::after {
  content: "";
  position: absolute;
  inset: 12%;
  border: clamp(1px, var(--cell) * .045, 5px) solid var(--rust);
  transform: rotate(45deg) scale(.82);
  pointer-events: none;
}
@media (min-width: 1024px) and (min-height: 720px) {
  .game-shell { height: 100dvh; }
  .play-space { container-type: size; }
  .board-scroll { height: 100%; max-height: none; padding: 16px; }
  .board {
    --cell: max(var(--min-cell), min((100cqw - 34px) / var(--columns), (100cqh - 34px) / var(--rows)));
  }
}

@media (max-width: 720px) {
  .game-shell { --gutter: 12px; grid-template-rows: auto 1fr; gap: 12px; padding-block: 12px 16px; }
  .masthead { align-items: start; flex-direction: column; gap: 5px; padding-block: 0 8px; }
  .wordmark { gap: 9px; }
  .masthead h1 { font-size: clamp(2.125rem, 9vw, 3rem); }
  .masthead p { margin-left: 41px; font-size: .8125rem; }
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto auto; align-content: start; gap: 12px; }
  .control-rail, .stage, .help { grid-column: 1; grid-row: auto; }
  .presets { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 5px; }
  .presets legend { margin-bottom: 6px; font-size: .8125rem; }
  .preset-face { grid-template-columns: auto minmax(0, 1fr); gap: 2px 5px; padding: 5px 6px; font-size: .8125rem; }
  .preset-face::before { width: 8px; }
  .preset-face small { grid-column: 2; font-size: .6875rem; }
  .actions { flex-wrap: nowrap; gap: 7px; margin-top: 7px; }
  .restart { flex: 1 1 45%; }
  .modes { width: 55%; }
  .actions button { min-height: 40px; padding-inline: 7px; font-size: .8125rem; }
  .stage-header { grid-template-columns: minmax(65px, 1fr) minmax(216px, 72%); padding: 8px; }
  .game-state { gap: 6px; font-size: .8125rem; }
  .game-state::before { flex-basis: 8px; height: 8px; }
  .readings > div { padding-left: 7px; }
  .readings dt { font-size: .6875rem; }
  .readings dd { font-size: 1.125rem; }
  .board-scroll { max-height: max(232px, 100svh - 300px); padding: 8px; }
  .board { --cell: max(var(--min-cell), min(40px, (100vw - 45px) / var(--columns))); }
  .help { padding-top: 8px; }
  .help h2 { font-size: .8125rem; }
  .help p, .help dl { font-size: .8125rem; }
  .help dl { margin-top: 9px; gap: 5px; }
}

@media (pointer: coarse) {
  .board { --min-cell: 32px; }
}
