/*
 * 配色は Windows の古典的なマインスイーパーに寄せる。
 * 銀色の盤に立体のベベル、開いたマスは平らに沈み、数字は定番の色
 * （1=青・2=緑・3=赤…）。残数は黒地に赤いデジタル数字で出す。
 */

:root {
  /* マスの大きさ。16列が狭い画面にも収まるよう、画面幅にも上限にも従う。 */
  --cell-size: clamp(1.15rem, 5.2vmin, 1.8rem);
  --gap: 0.5rem;
  /* Windows の銀色と、立体感を作る光と影。 */
  --silver: #c0c0c0;
  --silver-light: #d4d4d4;
  --highlight: #ffffff;
  --shadow: #808080;
  --title-from: #000080;
  --title-to: #1084d0;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

/* デスクトップの緑青色。その上に銀色の「窓」を置く。 */
html {
  background: #008080;
}

body {
  margin: 1rem auto;
  width: fit-content;
  max-width: calc(100vw - 1rem);
  padding: var(--gap);
  font-family: Tahoma, "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  color: #000;
  background: var(--silver);
  border: 2px solid;
  border-color: var(--highlight) var(--shadow) var(--shadow) var(--highlight);
  box-shadow: 1px 1px 0 #000;
}

header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
}

/* 窓のタイトルバー。 */
header h1 {
  flex: 1 1 100%;
  margin: 0;
  padding: 0.25rem 0.5rem;
  font-size: 0.95rem;
  letter-spacing: 0.06em;
  color: #fff;
  background: linear-gradient(90deg, var(--title-from), var(--title-to));
}

/* 順位表。点の高い順に並び、遊んでいる間ずっと出したままにする。 */
.players {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.player {
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
  padding: 0.1rem 0.5rem;
  font-size: 0.8rem;
  background: #fff;
  border: 1px solid var(--shadow);
  border-left: 4px solid var(--player-color, var(--shadow));
}

/* 自分の席だけ、ひと目で分かるように沈ませる。 */
.player--you {
  background: #ffffe1;
  font-weight: 700;
}

.player__rank {
  min-width: 1.1em;
  color: var(--player-color, #000);
}

/* 首位だけ王冠。人数が少なくても順位表らしく見える。 */
.player--top .player__rank::after {
  content: "👑";
  font-size: 0.85em;
}

.player__score {
  font-family: "Courier New", monospace;
  font-weight: 700;
}

/* 内訳（開けたマス・旗・見つけた地雷）。狭い画面では控えめに。 */
.player__detail {
  font-size: 0.7rem;
  color: #404040;
  white-space: nowrap;
}

.controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
}

/* 残りの地雷数。黒地に赤の、あのデジタル数字。 */
.mine-count {
  padding: 0.1rem 0.4rem;
  font-family: "Courier New", monospace;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: #f00;
  background: #000;
  border: 2px solid;
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
}

button {
  padding: 0.3rem 0.85rem;
  border: 2px solid;
  border-color: var(--highlight) var(--shadow) var(--shadow) var(--highlight);
  border-radius: 0;
  background: var(--silver);
  color: #000;
  font: inherit;
  cursor: pointer;
}

button:hover {
  background: var(--silver-light);
}

/* 押している間は沈む。 */
button:active {
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
}

/* --- 盤面 --- */

/*
 * 盤面は「マスの並び」ちょうどの幅にする。ウィンドウ幅に依らず同じ形になるので、
 * カーソルの位置を割合で送れば、画面の広さが違う相手にも同じ場所に見える。
 */
.board {
  position: relative;
  width: fit-content;
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 16), var(--cell-size));
  gap: 0;
  background: var(--silver);
  /* 盤面の枠は深く沈める。 */
  border: 3px solid;
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: grid;
  place-items: center;
  font-size: calc(var(--cell-size) * 0.62);
  font-weight: 700;
  line-height: 1;
  font-family: "Courier New", "Hiragino Sans", monospace;
  background: var(--silver);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

/* 隠れたマス：盛り上がったベベル。 */
.cell--hidden,
.cell--flagged {
  border: 2px solid;
  border-color: var(--highlight) var(--shadow) var(--shadow) var(--highlight);
  cursor: pointer;
}

.cell--hidden:hover,
.cell--flagged:hover {
  background: var(--silver-light);
}

.cell--hidden:active {
  border: 1px solid var(--shadow);
  border-right: none;
  border-bottom: none;
}

/* 開いたマス：平らに沈み、細い線で区切られる。 */
.cell--revealed {
  border-top: 1px solid var(--shadow);
  border-left: 1px solid var(--shadow);
}

.cell--revealed[data-count]:not([data-count="0"]) {
  cursor: pointer;
}

.cell--flagged {
  font-size: calc(var(--cell-size) * 0.52);
}

/* 数字の色。Windows の定番の配色そのまま。 */
.cell--n1 {
  color: #0000ff;
}
.cell--n2 {
  color: #008000;
}
.cell--n3 {
  color: #ff0000;
}
.cell--n4 {
  color: #000080;
}
.cell--n5 {
  color: #800000;
}
.cell--n6 {
  color: #008080;
}
.cell--n7 {
  color: #000000;
}
.cell--n8 {
  color: #808080;
}

/* 終局後の表示。 */
.cell--mine {
  border-top: 1px solid var(--shadow);
  border-left: 1px solid var(--shadow);
  font-size: calc(var(--cell-size) * 0.55);
}

/* 踏んだマスだけ赤くなる（あの見た目）。 */
.cell--exploded {
  background: #ff0000;
  border-top: 1px solid var(--shadow);
  border-left: 1px solid var(--shadow);
  font-size: calc(var(--cell-size) * 0.55);
  animation: exploded-shake 0.4s ease-out;
}

.cell--wrong-flag {
  border-top: 1px solid var(--shadow);
  border-left: 1px solid var(--shadow);
  font-size: calc(var(--cell-size) * 0.52);
  position: relative;
}

/*
 * 旗は絵文字でなく形で描く。立てた人の色に塗り分けるため
 * （絵文字は色を指図できない）。大きさはマスに追従させる。
 */
.flag {
  position: relative;
  display: block;
  width: 82%;
  height: 82%;
  /* 竿の足元。旗が地に立って見える。 */
  border-bottom: 2px solid #202020;
}

/* 竿。 */
.flag::before {
  content: "";
  position: absolute;
  left: 48%;
  top: 2%;
  bottom: 0;
  width: 10%;
  min-width: 1px;
  background: #202020;
}

/* 旗そのもの。左向きの三角形を、立てた人の色で塗る。 */
.flag::after {
  content: "";
  position: absolute;
  left: 2%;
  top: 2%;
  width: 48%;
  height: 44%;
  background: var(--player-color, #d40000);
  clip-path: polygon(100% 0, 100% 100%, 0 50%);
}

.cell--wrong-flag::after {
  content: "✕";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #ff0000;
  font-size: calc(var(--cell-size) * 0.75);
}

@keyframes exploded-shake {
  25% {
    translate: -2px 0;
  }
  50% {
    translate: 2px 0;
  }
  75% {
    translate: -1px 0;
  }
}

/* --- 爆発 --- */

/*
 * 踏んだ瞬間の演出。盤面とは別の層に重ねるので、盤面を描き直しても消えない。
 * 出したものは終わったらすべて捨てる（残しても操作の邪魔になるだけ）。
 */
.blast {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 15;
}

/* 盤面ぜんたいが、踏んだところを中心に赤くまたたく。 */
.blast__wave {
  position: absolute;
  inset: 0;
  background: radial-gradient(
    circle at var(--x) var(--y),
    rgb(255 245 200 / 0.95),
    rgb(255 90 0 / 0.5) 35%,
    rgb(255 0 0 / 0) 70%
  );
  animation: blast-wave 0.6s ease-out forwards;
}

/* 踏んだマスの中心。破片も火球もここを起点に飛ぶ。 */
.blast__spot {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: 0;
  height: 0;
}

.blast__fire {
  position: absolute;
  width: calc(var(--cell-size) * 3.2);
  height: calc(var(--cell-size) * 3.2);
  border-radius: 50%;
  background: radial-gradient(
    circle,
    #fff 0%,
    #ffe07a 22%,
    #ff8c00 48%,
    rgb(255 60 0 / 0.7) 68%,
    rgb(255 0 0 / 0) 78%
  );
  animation: blast-fire 0.55s ease-out forwards;
}

.blast__ring {
  position: absolute;
  width: calc(var(--cell-size) * 2);
  height: calc(var(--cell-size) * 2);
  border: calc(var(--cell-size) * 0.16) solid rgb(255 225 130 / 0.9);
  border-radius: 50%;
  animation: blast-ring 0.7s cubic-bezier(0.15, 0.7, 0.3, 1) forwards;
}

/* 2枚目の輪は少し遅れて広がる。1枚だけより奥行きが出る。 */
.blast__ring--late {
  animation-delay: 0.12s;
  border-color: rgb(255 120 40 / 0.75);
}

.blast__shard {
  position: absolute;
  width: calc(var(--cell-size) * 0.3);
  height: calc(var(--cell-size) * 0.3);
  background: var(--shard-color, #303030);
  animation: blast-shard 0.8s cubic-bezier(0.1, 0.6, 0.3, 1) forwards;
}

@keyframes blast-wave {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@keyframes blast-fire {
  from {
    translate: -50% -50%;
    scale: 0.2;
    opacity: 1;
  }
  60% {
    opacity: 0.9;
  }
  to {
    translate: -50% -50%;
    scale: 1.15;
    opacity: 0;
  }
}

@keyframes blast-ring {
  from {
    translate: -50% -50%;
    scale: 0.15;
    opacity: 1;
  }
  to {
    translate: -50% -50%;
    scale: 2.8;
    opacity: 0;
  }
}

/* 破片は飛ぶ向きと回り方を1つずつ変える（JS が --dx/--dy/--spin を渡す）。 */
@keyframes blast-shard {
  from {
    translate: -50% -50%;
    rotate: 0deg;
    opacity: 1;
  }
  70% {
    opacity: 1;
  }
  to {
    translate: calc(-50% + var(--dx)) calc(-50% + var(--dy));
    rotate: var(--spin, 180deg);
    opacity: 0;
  }
}

/* 盤面ごと揺れる。踏んだ手応えは、マス1つの揺れでは出ない。 */
.board--blast {
  animation: board-shake 0.5s ease-out;
}

@keyframes board-shake {
  10% {
    translate: -4px 2px;
    rotate: -0.4deg;
  }
  25% {
    translate: 5px -3px;
    rotate: 0.5deg;
  }
  45% {
    translate: -4px -2px;
    rotate: -0.3deg;
  }
  65% {
    translate: 3px 2px;
    rotate: 0.2deg;
  }
  85% {
    translate: -2px 0;
    rotate: 0deg;
  }
}

/* 最後に動かされたマス。その人の色で一瞬光って、誰の手か分かる。 */
.cell--flash {
  animation: cell-flash 0.7s ease-out;
}

@keyframes cell-flash {
  from {
    outline: 3px solid var(--flash-color, #f4a300);
    outline-offset: -2px;
  }
  to {
    outline: 3px solid transparent;
    outline-offset: -2px;
  }
}

/* ヒントで勧められたマス。安全は青、地雷は赤（数字の色と同じ約束）。 */
.cell--hint {
  outline: 3px solid #0000ff;
  outline-offset: -2px;
  animation: hint-pulse 1.2s ease-in-out infinite;
  z-index: 1;
}

.cell--hint-mine {
  outline-color: #ff0000;
}

@keyframes hint-pulse {
  50% {
    outline-color: transparent;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell--hint,
  .cell--flash,
  .cell--exploded,
  .board--blast {
    animation: none;
  }

  /* 動きを控える設定でも、踏んだことは伝わってほしい。光だけ短く残す。 */
  .blast__fire,
  .blast__ring,
  .blast__shard {
    display: none;
  }

  .blast__wave {
    animation-duration: 0.3s;
  }
}

/* 操作への短い知らせ。Windows のツールチップの色で、手の少し上に出す。 */
.notice {
  position: absolute;
  left: 0;
  top: 0;
  translate: -50% -100%;
  margin-top: -1.6rem;
  padding: 0.25rem 0.6rem;
  font-size: 0.85rem;
  white-space: nowrap;
  background: #ffffe1;
  color: #000;
  border: 1px solid #000;
  box-shadow: 2px 2px 0 rgb(0 0 0 / 0.35);
  pointer-events: none;
  z-index: 20;
}

/* --- 他のプレイヤーのカーソル --- */

.cursors {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cursor {
  position: absolute;
  translate: 0.4rem 0.4rem;
  padding: 0.05rem 0.4rem;
  font-size: 0.7rem;
  white-space: nowrap;
  background: var(--player-color, #808080);
  color: #fff;
  border: 1px solid #000;
  box-shadow: 1px 1px 0 rgb(0 0 0 / 0.4);
  transition: left 60ms linear, top 60ms linear;
  z-index: 10;
}

/* --- 参加ダイアログと結果表示 --- */

dialog {
  border: 2px solid;
  border-color: var(--highlight) var(--shadow) var(--shadow) var(--highlight);
  border-radius: 0;
  padding: 0;
  background: var(--silver);
  color: #000;
  box-shadow: 2px 2px 0 #000;
}

/* dialog を知らないブラウザでも、open の有無どおりに出し入れする。 */
dialog:not([open]) {
  display: none;
}

dialog[open] {
  display: block;
  width: fit-content;
  margin: 1rem auto;
}

dialog::backdrop {
  background: rgb(0 64 64 / 0.5);
}

dialog form {
  padding: 0 1.25rem 1.25rem;
}

/* ダイアログにも青いタイトルバー。 */
dialog h1 {
  margin: 0 -1.25rem 1rem;
  padding: 0.3rem 0.6rem;
  font-size: 1rem;
  color: #fff;
  background: linear-gradient(90deg, var(--title-from), var(--title-to));
}

dialog label {
  display: block;
  margin: 1rem 0;
  font-size: 0.85rem;
}

dialog input {
  display: block;
  margin-top: 0.35rem;
  padding: 0.35rem 0.6rem;
  width: 14rem;
  border: 2px solid;
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
  border-radius: 0;
  background: #fff;
  color: #000;
  font: inherit;
}

.connection {
  margin: 1rem auto;
  max-width: 32rem;
  padding: 0.75rem 1rem;
  background: var(--silver);
  border: 2px solid;
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
  text-align: center;
  line-height: 1.6;
}

.result {
  margin: 0.75rem 0 0.25rem;
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
}

/* ?debug のときだけ出る通信の記録。 */
.debug {
  margin: 0.75rem 0 0;
  padding: 0.5rem;
  max-height: 12rem;
  overflow-y: auto;
  font-family: "Courier New", monospace;
  font-size: 0.7rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  color: #0f0;
  background: #000;
  border: 2px solid;
  border-color: var(--shadow) var(--highlight) var(--highlight) var(--shadow);
}
