:root {
  color-scheme: light;
  --bg: #f7f4e9;
  --bg-dot: #e6e0cf;
  --surface: #fffdf4;
  --surface-soft: #f1ecdf;
  --tile-hidden: #e8e0f5;
  --tile-hidden-hover: #ded4f1;
  --tile-hidden-edge: #b6a3d2;
  --border: #d5d1c5;
  --border-strong: #a39d90;
  --ink: #34313e;
  --muted: #67616e;
  --soft: #736d7a;
  --lavender: #ded4f1;
  --lavender-edge: #b6a3d2;
  --lavender-deep: #6f5a9a;
  --mint: #c5e7d8;
  --mint-edge: #8ab7a3;
  --coral: #ffb7a5;
  --coral-edge: #d28f7f;
  --focus: #b11f4b;
  --commit-1: #dbf3e2;
  --commit-2: #b8e6c7;
  --commit-3: #8bd4a4;
  --commit-4: #62bc84;
  --panel: rgba(255, 253, 244, 0.94);
  --shadow: 0 16px 40px rgba(52, 49, 62, 0.12);
  --num-1: #1f6feb;
  --num-2: #247a4d;
  --num-3: #b11f4b;
  --num-4: #6f5a9a;
  --num-5: #9a4b1f;
  --num-6: #1b7c83;
  --num-7: #34313e;
  --num-8: #67616e;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #25232f;
  --bg-dot: #312e3c;
  --surface: #302d3c;
  --surface-soft: #3a3647;
  --tile-hidden: #49415c;
  --tile-hidden-hover: #5c4f7a;
  --tile-hidden-edge: #7d6fa0;
  --border: #514b62;
  --border-strong: #7d7591;
  --ink: #f4efe2;
  --muted: #c9c1d6;
  --soft: #b9b1c6;
  --lavender: #5c4f7a;
  --lavender-edge: #9c89c4;
  --lavender-deep: #cbbcf0;
  --mint: #3f6a5a;
  --mint-edge: #8fcfb4;
  --coral: #8a5448;
  --coral-edge: #ffb7a5;
  --focus: #fd8ea1;
  --commit-1: #2b4a3a;
  --commit-2: #366049;
  --commit-3: #467a5d;
  --commit-4: #569672;
  --panel: rgba(48, 45, 60, 0.95);
  --shadow: 0 16px 40px rgba(10, 8, 16, 0.42);
  --num-1: #79c0ff;
  --num-2: #7ee787;
  --num-3: #ffa198;
  --num-4: #d2a8ff;
  --num-5: #ffa657;
  --num-6: #56d4dd;
  --num-7: #f4efe2;
  --num-8: #c9c1d6;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--bg) radial-gradient(var(--bg-dot) 1px, transparent 1.4px) 0 0 / 22px 22px;
  font-family: "Mona Sans", "Segoe UI", -apple-system, BlinkMacSystemFont, Aptos, sans-serif;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
button {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button:hover:not(:disabled) { background: var(--lavender); }
button:disabled { opacity: 0.5; cursor: default; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.stage:focus-visible { outline: 3px solid var(--lavender-deep); outline-offset: -3px; }
kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 5px;
  border: 1.5px solid var(--ink);
  border-bottom-width: 3px;
  border-radius: 6px;
  background: var(--surface);
  font: 700 11px ui-monospace, Consolas, monospace;
  color: var(--ink);
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
.eyebrow, .label {
  font: 700 10px/1.2 ui-monospace, Consolas, "Courier New", monospace;
  letter-spacing: 1.4px;
  color: var(--soft);
  margin: 0;
}

.app { max-width: 1120px; margin: 0 auto; padding: 0 24px 20px; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 64px;
  border-bottom: 2px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 750;
  font-size: 18px;
  letter-spacing: -0.4px;
}
.brand svg { width: 28px; height: 28px; }
.brand-sub { font-weight: 400; color: var(--muted); }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.chip { padding: 6px 11px; font-size: 12px; font-weight: 650; border-width: 1.5px; background: var(--surface); }
.chip[aria-pressed="false"]:is(#music, #sound) { color: var(--muted); border-style: dashed; }
.chip.round { width: 32px; padding: 6px 0; }

.heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin: 18px 0 14px;
}
h1 {
  margin: 6px 0 0;
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1.05;
  letter-spacing: -1.6px;
  font-weight: 800;
}
h1 span { color: var(--lavender-deep); }
.edition {
  margin-left: 10px;
  padding: 3px 7px;
  vertical-align: middle;
  font: 700 11px ui-monospace, monospace;
  letter-spacing: 0;
  color: var(--ink);
  background: var(--mint);
  border: 1.5px solid var(--ink);
  border-radius: 6px;
}
.controls-hint {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--muted);
}
.controls-hint span { margin-right: 5px; }

.game-shell {
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 18px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.game-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 12px 18px;
  border-bottom: 2px solid var(--ink);
  background: var(--surface-soft);
  flex-wrap: wrap;
}
.repo { display: flex; align-items: center; gap: 10px; min-width: 0; }
.repo strong {
  display: block;
  font-size: 15px;
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.branch-icon { font-size: 26px; color: var(--lavender-deep); }
.branch-tag {
  font: 11px ui-monospace, monospace;
  padding: 3px 7px;
  border-radius: 6px;
  border: 1.5px solid var(--mint-edge);
  background: var(--mint);
  color: var(--ink);
  white-space: nowrap;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.difficulty-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.diff-btn {
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 650;
  border-width: 1.5px;
  border-radius: 8px;
  background: var(--surface);
}
.diff-btn[aria-pressed="true"] {
  background: var(--lavender);
  border-width: 2px;
  box-shadow: 0 2px 0 var(--ink);
}

.scoreboard { display: flex; gap: 22px; }
.scoreboard strong {
  display: block;
  margin-top: 3px;
  font: 700 20px ui-monospace, Consolas, monospace;
  letter-spacing: 1px;
}
#bugs-left { color: var(--focus); }

.action-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px;
  border-bottom: 2px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}
.mona-badge {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border-width: 1.5px;
  border-radius: 12px;
  background: var(--surface-soft);
  text-align: left;
}
.mona-badge img {
  image-rendering: pixelated;
  display: block;
}
.mona-badge strong {
  display: block;
  font-size: 13px;
  margin-top: 2px;
}
.mona-badge[data-mood="celebrating"] { background: var(--mint); }
.mona-badge[data-mood="startled"] { background: var(--coral); }

.action-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.mode-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 700;
  border-width: 2px;
  background: var(--mint);
}
.mode-btn[aria-pressed="true"] {
  background: var(--coral);
}
.mode-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid var(--ink);
  background: var(--commit-4);
}
.mode-btn[aria-pressed="true"] .mode-dot {
  background: var(--focus);
}
.action-chip {
  padding: 7px 11px;
  font-size: 12px;
  font-weight: 650;
  border-width: 1.5px;
}
.pause-btn {
  min-width: 36px;
  padding: 7px 10px;
}

.stage {
  position: relative;
  min-height: 430px;
  padding: 20px 16px;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.board {
  --cols: 9;
  --rows: 9;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: 4px;
  width: min(100%, calc(var(--cols) * 44px));
  padding: 10px;
  background: var(--surface-soft);
  border: 2.5px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 6px 0 var(--border-strong);
  touch-action: manipulation;
}
.cell {
  aspect-ratio: 1 / 1;
  min-width: 0;
  border: 1.5px solid var(--ink);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font: 800 clamp(11px, 2.2vw, 17px)/1 ui-monospace, Consolas, monospace;
  position: relative;
  cursor: pointer;
  transition: transform 0.06s ease, background-color 0.08s ease;
}
.cell[data-state="hidden"] {
  background: var(--tile-hidden);
  box-shadow: inset 0 -2px 0 var(--tile-hidden-edge), inset 0 2px 0 rgba(255, 255, 255, 0.45);
}
.cell[data-state="hidden"]:hover {
  background: var(--tile-hidden-hover);
}
.cell[data-state="flagged"] {
  background: var(--coral);
  box-shadow: inset 0 -2px 0 var(--coral-edge);
}
.cell[data-state="revealed"] {
  background: var(--surface);
  border-color: var(--border-strong);
}
.cell[data-state="revealed"][data-adjacent="0"][data-commit="1"] { background: var(--commit-1); }
.cell[data-state="revealed"][data-adjacent="0"][data-commit="2"] { background: var(--commit-2); }
.cell[data-state="revealed"][data-adjacent="0"][data-commit="3"] { background: var(--commit-3); }
.cell[data-state="revealed"][data-adjacent="0"][data-commit="4"] { background: var(--commit-4); }

.cell[data-state="exploded"] {
  background: var(--coral);
  border-color: var(--focus);
  border-width: 2px;
}
.cell[data-state="bug"] {
  background: var(--surface-soft);
}
.cell[data-state="wrong-flag"] {
  background: var(--lavender);
  text-decoration: line-through;
}

.cell[data-cursor="true"] {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
  z-index: 1;
}

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

.cell-icon {
  width: 66%;
  height: 66%;
  display: block;
  pointer-events: none;
}

.message {
  position: absolute;
  inset: 0;
  margin: auto;
  z-index: 3;
  width: min(370px, calc(100% - 28px));
  height: fit-content;
  padding: 22px;
  text-align: left;
  background: var(--panel);
  border: 2px solid var(--ink);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.message h2 {
  margin: 8px 0 8px;
  font-size: 25px;
  line-height: 1.12;
  letter-spacing: -0.8px;
}
.message p:not(.eyebrow) {
  margin: 0 0 16px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted);
}
.primary {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 11px 16px;
  font-weight: 700;
  font-size: 14px;
  background: var(--lavender);
}
.primary:hover:not(:disabled) { background: var(--mint); }

.progress-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-top: 2px solid var(--border);
  font-size: 13px;
  color: var(--muted);
  flex-wrap: wrap;
}
.progress-row b { color: var(--ink); }
.live-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 8px;
  border-radius: 50%;
  background: var(--commit-4);
  border: 1.5px solid var(--ink);
  vertical-align: -1px;
}
.progress-track {
  flex: 1;
  min-width: 80px;
  height: 10px;
  border: 1.5px solid var(--ink);
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface-soft);
}
.progress-track i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--commit-4);
}
#percentage {
  font: 700 12px ui-monospace, monospace;
  color: var(--ink);
  min-width: 36px;
  text-align: right;
}
.best-time-pill {
  font: 600 12px ui-monospace, monospace;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1.5px solid var(--border-strong);
  background: var(--surface-soft);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  align-items: center;
  margin: 14px 2px;
  font-size: 12px;
  color: var(--muted);
}
.legend-title {
  font: 700 10px ui-monospace, monospace;
  letter-spacing: 1.4px;
  color: var(--soft);
}
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 1.5px solid var(--ink);
  border-radius: 3px;
}
.swatch.unrevealed { background: var(--tile-hidden); }
.swatch.c1 { background: var(--commit-1); }
.swatch.c2 { background: var(--commit-2); }
.swatch.c3 { background: var(--commit-3); }
.swatch.c4 { background: var(--commit-4); }
.swatch.num { background: var(--surface); }
.swatch.flag { background: var(--coral); }
.swatch.bug { background: var(--coral-edge); border-radius: 50%; }

footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 12px;
  border-top: 2px solid var(--border);
  font: 10px ui-monospace, monospace;
  letter-spacing: 1px;
  color: var(--soft);
}
footer .arcade-link,
footer .source-link {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  color: var(--lavender-deep);
  font: 650 12px/1.5 "Mona Sans", "Segoe UI", -apple-system, BlinkMacSystemFont, Aptos, sans-serif;
  letter-spacing: 0;
  text-underline-offset: 3px;
}
footer .arcade-link:hover,
footer .source-link:hover { color: var(--ink); }

dialog {
  width: min(560px, calc(100% - 24px));
  max-height: min(480px, calc(100% - 24px));
  margin: 12px auto auto;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 24px;
  color: var(--ink);
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(37, 35, 47, 0.45); }
dialog h2 { margin: 6px 0 10px; font-size: 26px; letter-spacing: -0.8px; }
dialog h3 { margin: 16px 0 6px; font-size: 15px; }
dialog p, dialog li { font-size: 14px; line-height: 1.55; color: var(--muted); }
dialog b { color: var(--ink); }
.dialog-close {
  position: sticky;
  float: right;
  margin: -10px -10px 0 0;
  top: 0;
  width: 32px;
  height: 32px;
  font-size: 18px;
  line-height: 1;
  border-width: 1.5px;
}
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.help-grid > div { padding: 2px 14px; border: 1.5px solid var(--border); border-radius: 12px; }
.help-list { padding-left: 0; list-style: none; }
.help-list li { margin: 6px 0; }
#got-it { position: sticky; bottom: 0; box-shadow: 0 -10px 0 var(--surface); }
.help-list .swatch { margin-right: 6px; vertical-align: -1px; }

@media (max-width: 760px) {
  .app { padding: 0 10px 14px; }
  .topbar { min-height: 54px; }
  .brand { font-size: 16px; }
  .brand-sub { display: none; }
  .brand > span { display: none; }
  .controls-hint { display: none; }
  .heading { margin: 12px 0 10px; }
  .game-toolbar { padding: 10px 12px; gap: 10px; }
  .difficulty-group { width: 100%; justify-content: space-between; }
  .diff-btn { flex: 1; text-align: center; padding: 6px 6px; font-size: 11px; }
  .scoreboard { gap: 12px; width: 100%; justify-content: space-between; }
  .scoreboard strong { font-size: 16px; }
  .action-bar { padding: 8px 12px; gap: 8px; }
  .mona-badge { width: 100%; justify-content: flex-start; }
  .action-controls { width: 100%; justify-content: space-between; }
  .mode-btn, .action-chip { padding: 6px 8px; font-size: 11px; }
  .stage { min-height: 340px; padding: 12px 8px; }
  .board { gap: 2px; padding: 6px; border-radius: 10px; }
  .cell { border-radius: 4px; font-size: clamp(10px, 3.2vw, 14px); }
  .progress-row { padding: 8px 10px; font-size: 12px; gap: 8px; }
  .help-grid { grid-template-columns: 1fr; }
}

@media (max-width: 360px) {
  .chip { padding: 5px 8px; font-size: 11px; }
  .repo strong { font-size: 14px; }
  .branch-tag { max-width: 110px; }
  .diff-btn { font-size: 10px; padding: 5px 4px; }
  .mode-btn, .action-chip { padding: 5px 6px; font-size: 10px; }
  .board { gap: 1.5px; padding: 4px; }
  .message { padding: 16px; }
  .message h2 { font-size: 21px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
