:root {
  color-scheme: light;
  --ground: #e6eeeb;
  --ink: #142f2c;
  --muted-ink: #3d5b57;
  --rule: #a3b8b1;
  --field: #173d39;
  --field-edge: #0e2e2b;
  --field-label: #c2d9d3;
  --tile-face: #b9d6cd;
  --tile-light: #d8ece5;
  --tile-edge: #688f84;
  --tile-mark: #4c6e65;
  --revealed-face: #64857e;
  --clue-ink: #f3fff9;
  --amber: #f0c675;
  --amber-hover: #f6d794;
  --amber-pressed: #dfb663;
  --focus: #895814;
  --fault: #793a32;
  --radius-control: 6px;
  --radius-frame: 12px;
  --radius-tile: 4px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --font-ui: system-ui, sans-serif;
  --font-data: ui-monospace, Consolas, monospace;
  --font-title-size: clamp(2rem, 5vw, 3.25rem);
  --font-label-size: 0.8125rem;
  --font-body-size: 0.9375rem;
  --font-reading-size: 1.625rem;
  --ease-out: cubic-bezier(0.2, 0.7, 0.25, 1);
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 100%;
  scrollbar-color: var(--muted-ink) var(--ground);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

*, *::before, *::after { box-sizing: border-box; }
body, h1, h2, p, dl, dd { margin: 0; }

::selection { color: var(--ink); background: var(--amber); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }

a { color: var(--muted-ink); text-underline-offset: 0.22em; }
a:hover { color: var(--ink); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.game {
  width: min(100% - 2 * var(--space-6), 1000px);
  margin-inline: auto;
  padding-block: 40px var(--space-5);
}

.masthead, .identity, .round-controls, .round-heading, .footer {
  display: flex;
  align-items: center;
}

.masthead {
  justify-content: space-between;
  gap: var(--space-5);
  padding-block-end: var(--space-5);
  border-block-end: 1px solid var(--rule);
}

.identity { gap: var(--space-4); }

.game-mark {
  position: relative;
  width: 32px;
  height: 32px;
  flex: none;
  border: 1px solid var(--muted-ink);
  border-radius: var(--radius-tile);
}

.game-mark::before, .game-mark::after {
  position: absolute;
  inset: 0;
  margin: auto;
  content: "";
}

.game-mark::before {
  width: 30px;
  height: 30px;
  background:
    linear-gradient(var(--muted-ink), var(--muted-ink)) center / 1px 100% no-repeat,
    linear-gradient(var(--muted-ink), var(--muted-ink)) center / 100% 1px no-repeat;
}

.game-mark::after {
  width: 9px;
  height: 9px;
  background: var(--field);
  transform: rotate(45deg);
}

h1 {
  font-size: var(--font-title-size);
  font-weight: 760;
  line-height: 1.02;
  letter-spacing: -0.04em;
}

.identity p {
  margin-block-start: var(--space-2);
  color: var(--muted-ink);
  font-size: var(--font-body-size);
  line-height: 1.45;
}

.round-controls { gap: var(--space-2); }
.difficulty { position: relative; }

.difficulty-label {
  position: absolute;
  inset-block-start: var(--space-1);
  inset-inline-start: calc(var(--space-3) + 1px);
  color: var(--muted-ink);
  font: 580 var(--font-label-size)/1 var(--font-ui);
  pointer-events: none;
}

.difficulty::after {
  position: absolute;
  inset-block-start: calc(50% - 5px);
  inset-inline-end: var(--space-4);
  width: 7px;
  height: 7px;
  border: solid currentColor;
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
  pointer-events: none;
  content: "";
}

.difficulty select, .new-game {
  appearance: none;
  min-height: 44px;
  border: 1px solid var(--muted-ink);
  border-radius: var(--radius-control);
  color: var(--ink);
  font: 650 var(--font-label-size)/1.25 var(--font-ui);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.difficulty select {
  display: block;
  width: 148px;
  padding: 20px var(--space-6) var(--space-1) var(--space-3);
  background: var(--ground);
  cursor: pointer;
}

.new-game {
  padding: var(--space-2) var(--space-4);
  background: var(--amber);
  font-weight: 720;
  white-space: nowrap;
}

.new-game:disabled { opacity: 1; }
.new-game:enabled { cursor: pointer; }

@media (hover: hover) {
  .difficulty select:hover { background: var(--tile-light); border-color: var(--ink); }
  .new-game:enabled:hover { background: var(--amber-hover); border-color: var(--ink); }
}

.difficulty select:active { background: var(--tile-face); }
.new-game:enabled:active { background: var(--amber-pressed); }
.difficulty select:active, .new-game:enabled:active {
  border-color: var(--ink);
  box-shadow: inset 0 2px var(--ink);
}

.workspace {
  display: grid;
  grid-template: "round field" auto "guide field" 1fr / minmax(min-content, 1fr) minmax(0, 566px);
  align-items: start;
  gap: var(--space-6) clamp(2rem, 7vw, 5.75rem);
  margin-block-start: var(--space-5);
}

.round { grid-area: round; }
.round-heading { justify-content: space-between; gap: var(--space-2); }

h2 {
  font-size: 1.125rem;
  letter-spacing: -0.018em;
  line-height: 1.25;
  font-weight: 720;
}

.status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--muted-ink);
  font: 680 var(--font-label-size)/1.25 var(--font-ui);
}

.status::before {
  width: 7px;
  height: 7px;
  flex: none;
  background: currentColor;
  border-radius: 50%;
  content: "";
}

.status:is([data-status="playing"], [data-status="won"]) { color: var(--field); }
.status[data-status="lost"] { color: var(--fault); }

.status[data-status="playing"]::before {
  border: 1px solid currentColor;
  background: none;
}

.status[data-status="won"]::before {
  height: 11px;
  border: solid currentColor;
  border-width: 0 2px 2px 0;
  border-radius: 0;
  background: none;
  transform: rotate(45deg);
}

.status[data-status="lost"]::before {
  border-radius: 0;
  transform: rotate(45deg);
}

.readings {
  display: grid;
  grid-template-columns: repeat(3, minmax(min-content, 1fr));
  margin-block-start: var(--space-3);
  border-block: 1px solid var(--rule);
}

.readings > div { padding-block: var(--space-3) var(--space-2); }

.readings > div + div {
  padding-inline-start: var(--space-3);
  border-inline-start: 1px solid var(--rule);
}

.readings dt {
  color: var(--muted-ink);
  font: 580 var(--font-label-size)/1.25 var(--font-ui);
}

.readings dd {
  padding-block: var(--space-1);
  color: var(--ink);
  font: 650 var(--font-reading-size)/1.1 var(--font-data);
  font-variant-numeric: tabular-nums;
}

.readings dd:has(> #elapsed-time) {
  /* Reserve five digits, the smaller seconds unit, and its gap before the clock starts. */
  min-inline-size: calc(6ch + 2px);
  white-space: nowrap;
}

.unit { margin-inline-start: 2px; font-size: 0.625em; }

.best-time { overflow-wrap: anywhere; }
.best-time-empty { color: var(--muted-ink); }

.tap-modes:not([hidden]) {
  display: flex;
  width: fit-content;
  margin: var(--space-2) 0 0;
  padding: 0;
  gap: var(--space-2);
  border: 0;
}

.tap-modes label {
  position: relative;
  cursor: pointer;
}

.tap-modes label, .tap-modes input { touch-action: none; }

.tap-modes input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: inherit;
  -webkit-tap-highlight-color: transparent;
}

.tap-modes span {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-width: 84px;
  min-height: 44px;
  padding-inline: var(--space-2);
  border: 1px solid var(--muted-ink);
  border-radius: var(--radius-control);
  background: var(--ground);
  color: var(--ink);
  font: 650 var(--font-label-size)/1.25 var(--font-ui);
}

.tap-modes span::before {
  width: 6px;
  height: 6px;
  border: 1px solid currentColor;
  transform: rotate(45deg);
  content: "";
}

.tap-modes input:checked + span {
  border-color: var(--field-edge);
  background: var(--field);
  color: var(--tile-light);
}
.tap-modes input:checked + span::before { background: currentColor; }
.tap-modes input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 3px; }

@media (hover: hover) {
  .tap-modes input:hover:not(:checked) + span { background: var(--tile-light); border-color: var(--ink); }
  .tap-modes input:hover:checked + span { background: var(--field-edge); }
}

.tap-modes input:active:not(:checked) + span {
  background: var(--tile-face);
  box-shadow: inset 0 2px var(--ink);
}
.tap-modes input:active:checked + span {
  background: var(--field-edge);
  box-shadow: inset 0 2px var(--tile-light);
}

.field {
  /* Leave room around the 1rem clue even at the densest preset. */
  --tile-min-size: 1.5rem;
  --tile-gap: 4px;
  --index-size: 20px;
  grid-area: field;
  min-width: 0;
  padding: var(--space-3) var(--space-4) var(--space-4) var(--space-3);
  border: 1px solid var(--field-edge);
  border-radius: var(--radius-frame);
  background: var(--field);
  color: var(--field-label);
  box-shadow: 0 9px 24px rgb(14 46 43 / 15%);
}

.field-scroll {
  --cell-focus-width: 2px;
  /* Give the outer focus stroke room at the last column and row. */
  margin-inline-end: calc(-1 * var(--cell-focus-width));
  margin-block-end: calc(-1 * var(--cell-focus-width));
  padding-inline-end: var(--cell-focus-width);
  padding-block-end: var(--cell-focus-width);
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-color: var(--tile-edge) var(--field);
  touch-action: none;
}

.plot {
  display: grid;
  grid-template: "corner columns" var(--index-size) "rows board" auto / var(--index-size) minmax(0, 1fr);
  gap: var(--tile-gap);
  min-width: calc(var(--index-size) + var(--columns) * (var(--tile-min-size) + var(--tile-gap)));
  user-select: none;
}

.plot-corner {
  grid-area: corner;
  place-self: center;
  width: 6px;
  height: 6px;
  background: var(--amber);
  transform: rotate(45deg);
}

.column-index, .row-index, .board-row { display: grid; gap: var(--tile-gap); }
.column-index, .board-row { grid-template-columns: repeat(var(--columns), minmax(0, 1fr)); }

.column-index, .row-index {
  place-items: center;
  font: 600 var(--font-label-size)/1 var(--font-data);
  font-variant-numeric: tabular-nums;
}

.column-index { grid-area: columns; }
.row-index { grid-area: rows; grid-template-rows: repeat(var(--rows), minmax(0, 1fr)); }
.board { grid-area: board; display: grid; gap: var(--tile-gap); }

.tile {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--tile-edge);
  border-radius: var(--radius-tile);
  color: var(--ink);
  font: 750 var(--font-label-size)/1 var(--font-data);
  font-variant-numeric: tabular-nums;
}

.tile:is([data-state="covered"], [data-state="flagged"], [data-state="incorrect-flag"]) {
  background: var(--tile-face);
  box-shadow: inset 0 1px var(--tile-light), inset 0 -1px var(--tile-edge);
}

.tile::before, .tile::after {
  grid-area: 1 / 1;
  pointer-events: none;
}

.tile[data-state="covered"]::before {
  width: 6px;
  height: 6px;
  border: 1px solid var(--tile-mark);
  transform: rotate(45deg);
  content: "";
}

.tile:is([data-state="flagged"], [data-state="incorrect-flag"])::before {
  width: 16px;
  height: 18px;
  background: var(--focus);
  clip-path: polygon(0 0, 100% 24%, 18% 51%, 18% 100%, 0 100%);
  content: "";
}

.tile[data-state="incorrect-flag"]::after {
  width: 12px;
  height: 12px;
  margin: 10px 0 0 12px;
  border-radius: var(--radius-tile);
  background:
    linear-gradient(var(--clue-ink), var(--clue-ink)) center / 8px 2px no-repeat,
    linear-gradient(var(--clue-ink), var(--clue-ink)) center / 2px 8px no-repeat,
    var(--fault);
  transform: rotate(45deg);
  content: "";
}

.tile > button {
  appearance: none;
  grid-area: 1 / 1;
  place-self: stretch;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: inherit;
  background: none;
  color: inherit;
  font: inherit;
  font-variant-numeric: inherit;
  -webkit-tap-highlight-color: transparent;
}

.tile:is([data-state="covered"], [data-state="flagged"]) > button[aria-disabled="false"] { cursor: pointer; }
.board .tile:is([data-state="covered"], [data-state="flagged"]) { transition: background-color 120ms var(--ease-out); }

@media (hover: hover) {
  .tile:is([data-state="covered"], [data-state="flagged"]):has(> button[aria-disabled="false"]:hover) {
    background: var(--tile-light);
  }
}

.tile:is([data-state="covered"], [data-state="flagged"]):has(> button[aria-disabled="false"]:active) {
  box-shadow: inset 0 2px var(--tile-mark);
}
.tile > button:focus-visible {
  z-index: 1;
  outline: var(--cell-focus-width) solid var(--amber);
  outline-offset: 0;
  scroll-margin: var(--cell-focus-width);
}

.tile[data-state="mine"],
.tile[data-state="revealed"] {
  border-color: var(--field-edge);
  background: var(--revealed-face) url("assets/board-tiles.png") 0 0 / 400% 300% no-repeat;
  background-blend-mode: multiply;
  color: var(--clue-ink);
}

.board .tile[data-state="revealed"] { font-size: 1rem; }

.tile[data-state="triggered-mine"] {
  border-color: var(--amber);
  background: var(--fault);
  color: var(--clue-ink);
}

.tile:is([data-state="mine"], [data-state="triggered-mine"])::before,
.tile:is([data-state="mine"], [data-state="triggered-mine"])::after {
  width: 18px;
  height: 18px;
  content: "";
}

.tile:is([data-state="mine"], [data-state="triggered-mine"])::before {
  background:
    linear-gradient(currentColor, currentColor) center / 100% 2px no-repeat,
    linear-gradient(currentColor, currentColor) center / 2px 100% no-repeat;
  transform: rotate(45deg);
}

.tile:is([data-state="mine"], [data-state="triggered-mine"])::after {
  background:
    radial-gradient(circle closest-side, currentColor 95%, transparent) center / 12px 12px no-repeat,
    linear-gradient(currentColor, currentColor) center / 100% 2px no-repeat,
    linear-gradient(currentColor, currentColor) center / 2px 100% no-repeat;
}

.guide { grid-area: guide; color: var(--muted-ink); }
.guide h2 { color: var(--ink); }

.guide p {
  font-size: var(--font-body-size);
  line-height: 1.5;
}

.guide > p { margin-block-start: var(--space-2); }
.clue-key {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  margin-block-start: var(--space-4);
  padding-block-start: var(--space-4);
  border-block-start: 1px solid var(--rule);
}

.clue-key .tile { width: 38px; flex: none; }
.clue-key strong { color: var(--ink); }

.footer {
  justify-content: space-between;
  gap: var(--space-3);
  margin-block-start: var(--space-4);
  color: var(--muted-ink);
  font-size: var(--font-label-size);
}

.footer-divider { margin-inline: var(--space-1); }

.credits {
  max-width: 64ch;
  margin-block-start: var(--space-4);
  padding-block-start: var(--space-4);
  border-block-start: 1px solid var(--rule);
  font-size: var(--font-body-size);
  line-height: 1.55;
}

.credits:not(:target) { display: none; }
.credits p { margin-block: var(--space-2); }

@media (max-width: 820px) {
  .game {
    width: min(100% - 2 * var(--space-4), 566px);
    padding-block: var(--space-5) var(--space-4);
  }

  .masthead { align-items: stretch; flex-direction: column; gap: var(--space-3); padding-block-end: var(--space-3); }
  .identity { gap: var(--space-3); }
  .game-mark { width: 26px; height: 26px; }
  .game-mark::before { width: 24px; height: 24px; }
  .identity p { margin-block-start: var(--space-1); }
  .round-controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .difficulty select { width: 100%; }

  .workspace {
    grid-template: "round" auto "field" auto "guide" auto / minmax(0, 1fr);
    gap: var(--space-3);
    margin-block-start: var(--space-3);
  }

  .readings { margin-block-start: var(--space-2); }
  .readings > div { padding-block: 6px 0; }
  .readings dd { font-size: 1.25rem; }
  .guide > p { margin-block-start: var(--space-1); }
  .clue-key { margin-block-start: var(--space-2); padding-block-start: var(--space-2); }
  .clue-key .tile { width: 32px; }
}

@media (max-width: 480px) {
  .game { width: calc(100% - 2 * var(--space-3)); padding-block-start: var(--space-3); }
  .field { --tile-gap: 2px; --index-size: 14px; padding: 6px; }
  .column-index, .row-index { font-size: 0.6875rem; }
}

@media (pointer: coarse) {
  .field { --tile-min-size: max(32px, 1.5rem); }
  .board-row { grid-template-columns: repeat(var(--columns), minmax(32px, 1fr)); }
  .board .tile { min-width: 32px; min-height: 32px; }
  .board .tile > button { margin: -1px; }
}

@media (any-pointer: coarse) {
  /* Keep Safari from magnifying the page when the native picker gains focus. */
  .difficulty select { font-size: 1rem; line-height: 1; }
}

@media (max-width: 480px) and (max-height: 700px) {
  .game { padding-block: var(--space-2) var(--space-1); }
  .masthead, .workspace { gap: var(--space-2); }
  .masthead { padding-block-end: var(--space-2); }
  .difficulty select, .new-game { min-height: 38px; }
  .difficulty select { padding-block: var(--space-4) 2px; }
  .workspace { margin-block-start: var(--space-2); }
  .identity p { display: none; }
  .round { display: grid; grid-template-columns: 1fr auto; }
  .round-heading { grid-column: 2; grid-row: 2; justify-content: flex-end; margin-block-start: var(--space-2); }
  .round-heading h2, .guide h2 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .readings { grid-column: 1 / -1; margin-block-start: 0; }
  .tap-modes { grid-column: 1; grid-row: 2; }
  .tap-modes span { min-height: 38px; }
  .guide p { font-size: var(--font-label-size); }
  .guide > p { margin-block-start: 0; }
  .clue-key { margin-block-start: var(--space-1); padding-block-start: var(--space-1); }
  .clue-key .tile { width: 28px; }
  .footer { margin-block-start: var(--space-1); line-height: 1.15; }
}

@media (prefers-reduced-motion: reduce) {
  .board .tile:is([data-state="covered"], [data-state="flagged"]) { transition: none; }
}
