/* Minesweeper. Every selector starts with .g-minesweeper; colours come from the site variables only. */

.g-minesweeper .ms-modebar { margin: 0 0 10px; min-height: 40px; }
.g-minesweeper .ms-modebar strong { text-align: center; font-size: .98rem; }
.g-minesweeper .ms-note { margin: 12px 0 0; font-size: .9rem; }
.g-minesweeper .ms-building { padding: 60px 0; font-weight: 800; }

/*
 * The board fills the width it is given. Squares are capped at 46px so a small
 * board does not balloon on a desktop, and a tall board is narrowed until it
 * fits the window height, though never below 24px a square. On a phone the
 * screen width is the tighter limit, so the board simply fills it.
 */
.g-minesweeper .ms-wrap {
  --ms-cols: 9;
  --ms-rows: 9;
  container-type: inline-size;
  width: 100%;
  max-width: max(calc(var(--ms-cols) * 24px), min(calc(var(--ms-cols) * 46px), calc((100vh - 200px) * var(--ms-cols) / var(--ms-rows))));
  margin: 16px auto 0;   /* headroom for the "Start here" label */
}

.g-minesweeper .ms-board {
  display: grid;
  grid-template-columns: repeat(var(--ms-cols), minmax(0, 1fr));
  gap: 2px;
  /* digits scale with the squares: first a viewport estimate, then the exact container width where supported */
  font-size: clamp(9px, calc((min(100vw, 620px) - 58px) / var(--ms-cols) * .58), 26px);
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}
.g-minesweeper .ms-board[hidden] { display: none; }
@supports (width: 1cqw) {
  .g-minesweeper .ms-board { font-size: calc(100cqw / var(--ms-cols) * .58); }
  .g-minesweeper .ms-dense .ms-board { font-size: calc(100cqw / var(--ms-cols) * .66); }
}

/* ---------- squares ---------- */
.g-minesweeper .ms-cell {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  border-radius: 16%;
  cursor: pointer;
  color: var(--text);
  background: color-mix(in srgb, var(--accent) 30%, var(--surface-2));
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, var(--accent) 38%, var(--border)),
    inset 0 -.14em 0 color-mix(in srgb, var(--text) 13%, transparent);
}
.g-minesweeper .ms-cell.is-open {
  cursor: default;
  background: var(--bg);
  box-shadow: inset 0 0 0 1px var(--border);
}
.g-minesweeper .ms-over .ms-cell { cursor: default; }
.g-minesweeper .ms-cell:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: -2px;
  border-radius: 16%;
  z-index: 4;
}
@media (hover: hover) {
  .g-minesweeper .ms-wrap:not(.ms-over) .ms-cell:not(.is-open):not(.is-start):hover {
    background: color-mix(in srgb, var(--accent) 44%, var(--surface-2));
  }
}

/* numbers: the digit carries the meaning, the colour is a second cue */
.g-minesweeper .ms-cell.n1 { color: var(--accent); }
.g-minesweeper .ms-cell.n2 { color: var(--warn); }
.g-minesweeper .ms-cell.n3 { color: var(--bad); }
.g-minesweeper .ms-cell.n4 { color: var(--good); }
.g-minesweeper .ms-cell.n5 { color: color-mix(in srgb, var(--bad) 60%, var(--text)); }
.g-minesweeper .ms-cell.n6 { color: color-mix(in srgb, var(--accent) 55%, var(--text)); }
.g-minesweeper .ms-cell.n7 { color: var(--text); }
.g-minesweeper .ms-cell.n8 { color: var(--muted); }

/* flag: a pole on a base with a pennant */
.g-minesweeper .ms-cell.is-flag::before {
  content: '';
  position: absolute;
  left: 26%; width: 36%; top: 18%; bottom: 16%;
  background:
    linear-gradient(var(--text), var(--text)) 50% 0 / 24% 100% no-repeat,
    linear-gradient(var(--text), var(--text)) 0 100% / 100% 14% no-repeat;
}
.g-minesweeper .ms-cell.is-flag::after {
  content: '';
  position: absolute;
  left: 47%; top: 18%; width: 35%; height: 33%;
  background: var(--bad);
  clip-path: polygon(0 0, 100% 50%, 0 100%);
}

/* mine: a round body with eight spikes */
.g-minesweeper .ms-cell.is-mine {
  background: color-mix(in srgb, var(--bad) 14%, var(--surface));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--bad) 40%, var(--border));
}
.g-minesweeper .ms-cell.is-mine::before {
  content: '';
  position: absolute;
  inset: 13%;
  border-radius: 50%;
  background:
    linear-gradient(var(--text), var(--text)) 50% 50% / 11% 100% no-repeat,
    linear-gradient(var(--text), var(--text)) 50% 50% / 100% 11% no-repeat,
    linear-gradient(45deg, transparent 46%, var(--text) 46% 54%, transparent 54%),
    linear-gradient(135deg, transparent 46%, var(--text) 46% 54%, transparent 54%);
}
.g-minesweeper .ms-cell.is-mine::after {
  content: '';
  position: absolute;
  inset: 27%;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 34%, var(--surface) 0 13%, var(--text) 17%);
}
/* the mine that ended the round: stronger fill and a heavy ring */
.g-minesweeper .ms-cell.is-hit {
  background: color-mix(in srgb, var(--bad) 55%, var(--surface));
  box-shadow: inset 0 0 0 .16em var(--bad);
}

/* a flag on a square with no mine: crossed out */
.g-minesweeper .ms-cell.is-wrong {
  background:
    linear-gradient(45deg, transparent 45%, var(--bad) 45% 55%, transparent 55%),
    linear-gradient(135deg, transparent 45%, var(--bad) 45% 55%, transparent 55%),
    color-mix(in srgb, var(--warn) 24%, var(--surface));
  box-shadow: inset 0 0 0 1px var(--bad);
}
.g-minesweeper .ms-cell.is-wrong::before,
.g-minesweeper .ms-cell.is-wrong::after { opacity: .4; }

/* ---------- "Start here" marker ---------- */
.g-minesweeper .ms-cell.is-start {
  z-index: 3;
  background: var(--accent);
  animation: g-minesweeper-pulse 1.5s ease-out infinite;
}
.g-minesweeper .ms-cell.is-start::before {   /* target: ring and dot */
  content: '';
  position: absolute;
  inset: 18%;
  border-radius: 50%;
  border: .13em solid var(--accent-ink);
  background: radial-gradient(circle, var(--accent-ink) 0 30%, transparent 36%);
}
.g-minesweeper .ms-cell.is-start::after {    /* pointer under the label */
  content: '';
  position: absolute;
  left: 50%; bottom: 100%;
  margin: 0 0 1px -6px;
  border: 6px solid transparent;
  border-bottom-width: 0;
  border-top-color: var(--text);
}
.g-minesweeper .ms-tip {
  position: absolute;
  left: 50%; bottom: calc(100% + 7px);
  transform: translateX(-50%);
  padding: 6px 9px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  font: 800 12px/1 var(--font);
  white-space: nowrap;
  pointer-events: none;
}
.g-minesweeper .ms-tip.at-l { left: -3px; transform: none; }
.g-minesweeper .ms-tip.at-r { left: auto; right: -3px; transform: none; }
@keyframes g-minesweeper-pulse {
  from { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 60%, transparent); }
  to { box-shadow: 0 0 0 .55em color-mix(in srgb, var(--accent) 0%, transparent); }
}

/* ---------- wide boards (14+ columns): no gaps, so 16 columns still fit a small phone ---------- */
.g-minesweeper .ms-dense .ms-board {
  gap: 0;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
}
.g-minesweeper .ms-dense .ms-cell {
  border-radius: 0;
  box-shadow: inset -1px -1px 0 color-mix(in srgb, var(--text) 28%, transparent);
}
.g-minesweeper .ms-dense .ms-cell.is-open { box-shadow: inset -1px -1px 0 var(--border); }
.g-minesweeper .ms-dense .ms-cell.is-mine { box-shadow: inset -1px -1px 0 color-mix(in srgb, var(--bad) 40%, var(--border)); }
.g-minesweeper .ms-dense .ms-cell.is-hit { box-shadow: inset 0 0 0 .16em var(--bad); }
.g-minesweeper .ms-dense .ms-cell.is-wrong { box-shadow: inset 0 0 0 1px var(--bad); }
.g-minesweeper .ms-dense .ms-cell.is-start { box-shadow: none; }
.g-minesweeper .ms-dense .ms-cell:focus-visible { border-radius: 0; }
/* on a narrow phone the 15- and 16-column boards borrow most of the card's side padding */
@media (max-width: 400px) {
  .g-minesweeper .ms-wrap.ms-bleed { width: auto; margin-left: -10px; margin-right: -10px; }
}

/* flag mode: a dashed frame as well as the lit button */
.g-minesweeper .ms-flagging .ms-board { outline: 2px dashed var(--warn); outline-offset: 0; }

/* ---------- controls: stay in reach while a tall board scrolls ---------- */
.g-minesweeper .ms-controls {
  position: sticky;
  bottom: 10px;
  z-index: 6;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  width: fit-content;
  max-width: 100%;
  margin: 14px auto 0;
  padding: 6px;
  border-radius: 28px;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  box-shadow: 0 0 0 1px var(--border);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.g-minesweeper .ms-controls .pz-btn[hidden] { display: none; }
.g-minesweeper .ms-flagstate { display: inline-block; min-width: 1.55em; text-align: left; }
@media (max-width: 420px) {
  .g-minesweeper .ms-controls .pz-btn { padding: 8px 13px; font-size: .94rem; }
}
@media (max-width: 360px) {
  .g-minesweeper .ms-flagword { display: none; }
}
.g-minesweeper .ms-flagicon { position: relative; flex: none; width: 1.05em; height: 1.05em; }
.g-minesweeper .ms-flagicon::before {
  content: '';
  position: absolute;
  left: 20%; top: 6%; width: 12%; height: 90%;
  border-radius: 2px;
  background: currentColor;
}
.g-minesweeper .ms-flagicon::after {
  content: '';
  position: absolute;
  left: 32%; top: 6%; width: 60%; height: 52%;
  background: var(--bad);
  clip-path: polygon(0 0, 100% 50%, 0 100%);
}
.g-minesweeper .ms-flagbtn.on .ms-flagicon::after { background: currentColor; }

@media (prefers-reduced-motion: reduce) {
  .g-minesweeper .ms-cell.is-start { animation: none; }
}
