/*
 * 扫雷样式 —— 零依赖，无第三方字体/图标/图片。
 * 地雷和旗子全部用 CSS 图形 + Unicode 字符绘制，版权 100% 自有。
 * 配色刻意避开 Windows 扫雷的灰色浮雕美术，走干净现代的中性色板。
 */

.ms {
  /* 所有可调项集中在这里，方便攻略站按主题覆写 */
  --ms-tile: 30px;
  --ms-gap: 2px;
  --ms-radius: 4px;
  --ms-bg: #eef1f6;
  --ms-panel: #ffffff;
  --ms-hidden: #b9c3d4;
  --ms-hidden-hi: #cfd7e4;
  --ms-open: #e6eaf1;
  --ms-line: #9aa7bd;
  --ms-text: #26303f;

  display: inline-block;
  max-width: 100%;
  padding: 12px;
  border-radius: 10px;
  background: var(--ms-bg);
  color: var(--ms-text);
  /* 只用系统字体栈，不引任何 web font */
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  /* 移动端双击和弦时不要触发浏览器的双击缩放 */
  touch-action: manipulation;
}

/* ---------- 顶部状态栏 ---------- */
.ms-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  padding: 6px 8px;
  border-radius: 8px;
  background: var(--ms-panel);
}

.ms-counter {
  min-width: 3.4em;
  padding: 4px 8px;
  border-radius: 6px;
  background: #26303f;
  color: #7ef2c4;
  /* 等宽字体保证数字跳动时宽度不抖 */
  font: 600 18px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.ms-reset {
  width: 36px;
  height: 36px;
  border: 1px solid var(--ms-line);
  border-radius: 8px;
  background: var(--ms-hidden-hi);
  color: var(--ms-text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: background .12s ease, transform .08s ease;
}
.ms-reset:hover { background: var(--ms-hidden); }
.ms-reset:active { transform: scale(.94); }

/* ---------- 棋盘 ---------- */
/*
 * expert 是 30 列宽盘，窄屏放不下。让滚动发生在这个内层容器上，
 * 页面整体布局就不会被撑破（overflow 交给它，不外溢到 body）。
 */
.ms-scroll {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

.ms-grid {
  display: grid;
  /* --ms-cols 由 JS 按难度写入，CSS 不必知道具体难度 */
  grid-template-columns: repeat(var(--ms-cols, 9), var(--ms-tile));
  gap: var(--ms-gap);
  /* 防止 grid 被 flex/scroll 容器压缩，保证格子始终是正方形 */
  width: max-content;
}

.ms-cell {
  width: var(--ms-tile);
  height: var(--ms-tile);
  margin: 0;
  padding: 0;
  border: none;
  border-radius: var(--ms-radius);
  background: var(--ms-hidden);
  color: transparent;
  font: 700 15px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-align: center;
  cursor: pointer;
  position: relative;
  /* 顶部高光模拟微凸起，比拟物浮雕更克制 */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .55);
  transition: background .1s ease;
}

.ms-cell:hover:not(.is-open) { background: var(--ms-hidden-hi); }

/* 键盘焦点必须清晰可见 —— 这是键盘可玩性的前提 */
.ms-cell:focus-visible {
  outline: 3px solid #2f6bff;
  outline-offset: -3px;
  z-index: 1;
}

.ms-cell.is-open {
  background: var(--ms-open);
  box-shadow: none;
  cursor: default;
}

/*
 * 经典数字配色（1蓝 2绿 3红 4深蓝 5棕 6青 7黑 8灰）。
 * 这是几十年沿用的功能性色码 —— 玩家靠颜色瞬间读盘，属于玩法而非美术抄袭。
 * 注意 .n0 故意不设颜色：0 格不显示数字。
 */
.ms-cell.n1 { color: #1976d2; }
.ms-cell.n2 { color: #2e7d32; }
.ms-cell.n3 { color: #d32f2f; }
.ms-cell.n4 { color: #1a237e; }
.ms-cell.n5 { color: #8d4e2a; }
.ms-cell.n6 { color: #00838f; }
.ms-cell.n7 { color: #1c1c1c; }
.ms-cell.n8 { color: #6b7280; }

/*
 * 可见数字用伪元素渲染（避免文本节点影响布局）。
 * 必须排除 .is-mine：雷格的 ::after 要留给地雷圆形，两者不能抢同一个伪元素。
 * 0 格没有 data-n 属性，天然不渲染。
 */
.ms-cell.is-open:not(.is-mine)::after {
  content: attr(data-n);
}

/* ---------- 旗子：纯 CSS 三角旗 + 旗杆 ---------- */
.ms-cell.is-flag::before {
  content: "";
  position: absolute;
  left: 34%;
  top: 22%;
  width: 0;
  height: 0;
  /* 用 border 技巧画三角形，不依赖任何图片资源 */
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 9px solid #e53935;
}
.ms-cell.is-flag::after {
  content: "";
  position: absolute;
  left: 33%;
  top: 22%;
  width: 2px;
  height: 56%;
  border-radius: 1px;
  background: #37474f;
}

/* ---------- 地雷：纯 CSS 圆形 + 高光 ---------- */
.ms-cell.is-mine::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 52%;
  height: 52%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 32%, #6b7280 0 18%, #1f2530 60%);
}

/* 踩中的那颗雷用红底突出，和"局终揭示的其他雷"区分开 */
.ms-cell.is-open.is-mine { background: #ff8a80; }

/* 局终揭示的未踩中的雷：底色保持隐藏态，暗示"你没点到这里" */
.ms-cell.is-revealed-mine { background: var(--ms-hidden-hi); }

/*
 * 插错的旗：局终时旗下无雷。画一道红叉盖在旗上，
 * 让玩家复盘时一眼看到自己的判断失误发生在哪。
 */
.ms-cell.is-wrong::before {
  content: "";
  position: absolute;
  inset: 18%;
  border: none;
  background:
    linear-gradient(45deg, transparent 44%, #c62828 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, #c62828 44% 56%, transparent 56%);
}
.ms-cell.is-wrong::after { display: none; }

/* ---------- 状态播报 ---------- */
.ms-status {
  margin-top: 8px;
  min-height: 1.2em;
  font-size: 14px;
  font-weight: 600;
  text-align: center;
}

/* ---------- 概率叠加层（/solver/）---------- */
/*
 * 概率数字画在**未翻开**的格子上。设计约束有三条：
 *
 * 1. 不能动 .ms-cell 的既有伪元素。::before 是旗子、::after 是数字/地雷，
 *    两个都被占了。所以概率百分比走 ::before 的**覆写**，且只在
 *    .has-prob 上生效 —— 未翻开且未插旗的格子本来就没用到 ::before。
 *
 * 2. 底色用内联 style 的 --p-bg 传（JS 按概率算 HSL），不用几十个 class。
 *    概率是连续量，class 只能表达分档；分档会让 0.34 和 0.66 看起来一样，
 *    而它们恰恰是"该点哪个"的分界。
 *
 * 3. 确定安全/确定是雷单独给 class，不靠色阶两端。
 *    p=0 和 p=0.03 在色阶上几乎同色，但一个是"点它"、一个是"别点"——
 *    这是全页最重要的区别，必须是断崖式的视觉差而非渐变。
 */
.ms-cell.has-prob {
  background: var(--p-bg, var(--ms-hidden));
  color: transparent;
}

.ms-cell.has-prob::before {
  content: attr(data-p);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 概率文字比棋盘数字小一号：它是叠加信息，不该盖过棋盘本身的读数节奏。
     clamp 让 expert 的 23px 格子上两位数仍然放得下。 */
  font: 700 clamp(8px, calc(var(--ms-tile) * .36), 11px)/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--p-fg, #26303f);
  /* 归零旗子伪元素的几何：.has-prob 与 .is-flag 互斥（插旗格不算未知），
     但把这几条写全，防止将来有人放开互斥时出现半个旗杆。 */
  border: none;
  background: none;
  transform: none;
}

/* 确定安全 —— 绿。这一格点下去 100% 不会炸。 */
.ms-cell.p-safe {
  --p-bg: #1e8e3e;
  --p-fg: #ffffff;
  box-shadow: inset 0 0 0 2px #0f5d27;
}
/* 确定是雷 —— 红。这一格点下去 100% 会炸。 */
.ms-cell.p-mine {
  --p-bg: #c5221f;
  --p-fg: #ffffff;
  box-shadow: inset 0 0 0 2px #8c1512;
}

/* 概率显示开着的时候，未翻开格子的 hover 高亮会盖掉色阶 —— 关掉它。 */
.ms.probs-on .ms-cell.has-prob:hover { background: var(--p-bg, var(--ms-hidden)); }

/* ---------- 概率工具条 ---------- */
/*
 * 🔴 主题变量必须在这里重新声明一份。
 *    上面那套 --ms-* 定义在 .ms 上，而 .ms-solver 是 #ms-root 的**兄弟节点**
 *    （工具条在棋盘外面，不该被棋盘的 padding/背景框住）。CSS 变量按 DOM
 *    继承，兄弟节点继承不到 —— var(--ms-panel) 会解析成空值，于是
 *    `background: var(--ms-panel)` 等于没写背景，按钮变透明。
 *    这一条是实测出来的：按下态白字 + 透明底 = 按钮上的字直接消失。
 *    所以不写 var(...) 的 fallback（fallback 只是把 bug 藏起来），
 *    而是让这个组件自带一份完整的变量。
 */
.ms-solver {
  --ms-radius: 4px;
  --ms-panel: #ffffff;
  --ms-hidden: #b9c3d4;
  --ms-hidden-hi: #cfd7e4;
  --ms-line: #9aa7bd;
  --ms-text: #26303f;

  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  justify-content: center;
}

.ms-prob-btn {
  border: 1px solid var(--ms-line);
  border-radius: var(--ms-radius);
  background: var(--ms-panel);
  color: var(--ms-text);
  font: 600 14px/1.2 inherit;
  padding: 9px 14px;
  cursor: pointer;
  transition: background .1s ease;
}
.ms-prob-btn:hover { background: var(--ms-hidden-hi); }
.ms-prob-btn[aria-pressed="true"] {
  background: #26303f;
  border-color: #26303f;
  color: #fff;
}
/* 局终禁用。禁用态必须肉眼可辨，否则玩家会以为按钮坏了。 */
.ms-prob-btn:disabled {
  opacity: .45;
  cursor: not-allowed;
}
.ms-prob-btn:disabled:hover { background: var(--ms-panel); }

/* 求解器的说明/提示行。和 .ms-status 分开：那条是游戏胜负播报，
   这条是概率工具的输出，两者会同时有话要说。 */
.ms-solver-note {
  flex-basis: 100%;
  min-height: 1.2em;
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
  color: #4a5567;
}
.ms-solver-note strong { color: var(--ms-text); }

/* 图例。只在概率显示打开时出现 —— 关着的时候它是纯噪音。 */
.ms-legend {
  display: none;
  flex-basis: 100%;
  flex-wrap: wrap;
  gap: 6px 14px;
  justify-content: center;
  font-size: 12px;
  color: #4a5567;
}
.ms.probs-on ~ .ms-solver .ms-legend,
.ms-solver.is-on .ms-legend { display: flex; }

.ms-legend span { display: inline-flex; align-items: center; gap: 5px; }
.ms-legend i {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  display: inline-block;
}

/* ---------- 响应式 ---------- */
/* 窄屏缩小格子，让 beginner/intermediate 尽量整盘可见；expert 仍靠横向滚动 */
@media (max-width: 640px) {
  .ms { --ms-tile: 26px; padding: 8px; }
}
@media (max-width: 380px) {
  .ms { --ms-tile: 23px; }
}

/* 尊重系统的减少动效偏好 */
@media (prefers-reduced-motion: reduce) {
  .ms-cell, .ms-reset { transition: none; }
}
