/* ===================================================================
   盤と駒（仕様書 3章）。js/board/board.js が組み立てた要素を描く。
   マスは container-type: size にして、駒の大きさをマスの大きさ（cqw / cqh / cqmin）から決める。
     駒の直径 s = 0.62 × マスの短い辺、1枚の厚み t = 0.065 × マスの高さ
   =================================================================== */
.ul-board {
  --label: 28px;
  display: grid;
  grid-template-columns: var(--label) minmax(0, 1fr);
  grid-template-rows: var(--label) auto;
  width: 100%;
  background: var(--surface);
  user-select: none;
  -webkit-user-select: none;
}
.ul-board.no-coords { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto; }
.ul-corner { background: var(--aqua); }
.ul-files {
  display: grid; grid-template-columns: repeat(var(--size), 1fr);
  background: var(--aqua); color: #fff; font-weight: 700; font-size: 13px;
}
.ul-files span, .ul-ranks span { display: flex; align-items: center; justify-content: center; }
.ul-ranks {
  display: grid; grid-template-rows: repeat(var(--size), 1fr);
  color: var(--text-sub); font-weight: 700; font-size: 13px; background: var(--surface);
}
.ul-grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
}

.cell {
  position: relative;
  container-type: size;
  aspect-ratio: var(--cell-ratio, 1);
  background: var(--surface);
  cursor: pointer;
}
.cell.checker { background: var(--checker); }
.cell.is-inc { background: var(--aqua-increase); }
.cell.is-dec { background: var(--red-decrease); }
.cell.is-selected { background: var(--aqua-pale); box-shadow: inset 0 0 0 2px var(--slate); }
@media (max-width: 767px) { .cell.is-selected { box-shadow: inset 0 0 0 1.5px var(--slate); } }
.ul-board[data-variant="static"] .cell { cursor: default; }

/* 移動先の候補: マスの中央に点（駒のあるマスでは駒の上に、白い縁をつけて重ねる） */
.cell.is-target::after {
  content: ''; position: absolute; z-index: 60; pointer-events: none;
  width: clamp(14px, 16cqmin, 22px); height: clamp(14px, 16cqmin, 22px); border-radius: 50%;
  background: var(--aqua);
  left: 0; right: 0; top: 0; bottom: 0; margin: auto;
}
.cell.is-target.occupied::after { box-shadow: 0 0 0 3px var(--surface); }

/* 積み重ねの枚数（2枚以上） */
.stack-count {
  position: absolute; right: 6%; bottom: 4%; z-index: 55;
  font-size: clamp(10px, 11cqmin, 13px); font-weight: 700; color: var(--text); line-height: 1;
  pointer-events: none;
}

/* ───────── 駒（厚みのあるコイン） ───────── */
.piece {
  --s: 62cqmin;
  --t: min(6.5cqh, calc((92cqh - var(--s)) / var(--n)));
  position: absolute;
  left: calc(50cqw - var(--s) / 2);
  top: calc((100cqh - (var(--s) + var(--t) * var(--n))) / 2 + var(--t) * (var(--n) - 1 - var(--i)));
  width: var(--s);
  height: calc(var(--s) + var(--t));
  z-index: calc(var(--i) * 2 + 2); /* 間の奇数は、排除されて抜けていく駒に使う */
  pointer-events: none;
}
.piece::before, .piece::after {
  content: ''; position: absolute; left: 0; width: 100%; box-sizing: border-box;
}
.piece::before { /* 側面 */
  top: calc(var(--s) / 2);
  height: calc(var(--s) / 2 + var(--t));
  border: 1px solid var(--edge);
  border-top: 0;
  border-radius: 0 0 calc(var(--s) / 2) calc(var(--s) / 2);
  background: var(--side);
}
.piece::after { /* 上面 */
  top: 0; height: var(--s); border-radius: 50%;
  border: 1px solid var(--edge);
  background: var(--top);
}
.piece.white { --top: var(--piece-w-top); --side: var(--piece-w-side); --edge: var(--piece-w-edge); }
.piece.black { --top: var(--piece-b-top); --side: var(--piece-b-side); --edge: var(--piece-b-edge); }

/* ───────── 駒の動き（js/board/board.js の animatedPiecesHtml がつける印） ─────────
   --motion は1枚分の時間、--stagger は複数枚を1枚ずつずらす間隔。
   離れる駒・排除される駒は前の局面の位置（--i・--n）に描いた影で、動き終わったら消えたままにする。 */
.ul-board { --motion: 0.26s; --stagger: 0.07s; }
.piece.is-arriving {
  animation: piece-arrive var(--motion) ease-out both;
  animation-delay: calc(var(--k, 0) * var(--stagger));
}
@keyframes piece-arrive {
  from { transform: translateY(-14cqh); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.piece.is-leaving {
  z-index: calc(var(--i) * 2 + 2);
  animation: piece-leave var(--motion) ease-in both;
  animation-delay: calc(var(--k, 0) * var(--stagger));
}
@keyframes piece-leave {
  from { transform: none; opacity: 1; }
  to { transform: translateY(-14cqh); opacity: 0; }
}
.piece.is-eliminated {
  z-index: calc(var(--i) * 2 + 1);
  animation: piece-eliminate calc(var(--motion) * 1.3) ease-in both;
}
@keyframes piece-eliminate {
  from { transform: none; opacity: 1; }
  to { transform: translateX(38cqw); opacity: 0; }
}
/* 残った駒: 枚数が変わって高さがずれた分だけ、前の位置（--pi・--pn）から滑らせる。
   排除で上の駒が下がるのは、抜けていく駒が横へずれてから */
.piece.is-shift {
  --pt: min(6.5cqh, calc((92cqh - var(--s)) / var(--pn)));
  --from: calc((100cqh - (var(--s) + var(--pt) * var(--pn))) / 2 + var(--pt) * (var(--pn) - 1 - var(--pi)));
  animation: piece-shift var(--motion) ease-in-out both;
}
.cell:has(.is-eliminated) .piece.is-shift { animation-delay: calc(var(--motion) * 0.6); }
@keyframes piece-shift {
  from { top: var(--from); }
}
@media (prefers-reduced-motion: reduce) {
  .piece.is-leaving, .piece.is-eliminated { display: none; }
}

/* 盤の外の小さな積み重ね（対局者のアイコン・文中の図） */
.coin-box {
  position: relative; display: inline-block; vertical-align: middle; flex: none;
  width: 28px; height: 30px; container-type: size;
}
.coin-box .piece { --s: 86cqmin; --t: min(10cqh, calc((96cqh - var(--s)) / var(--n))); }
.coin-box.lg { width: 40px; height: 42px; }
.coin-box.sm { width: 22px; height: 24px; }

/* ───────── 矢印（AIの候補手・正解の手順） ───────── */
.ul-arrows { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 70; overflow: visible; }
.ul-arrow polyline { fill: none; stroke-linejoin: round; stroke-linecap: round; }
.ul-arrow .ul-ring { fill: none; stroke-width: 6; }
.ul-arrow.first { opacity: 0.95; color: var(--aqua); }
.ul-arrow.second { opacity: 0.85; color: var(--slate); }
.ul-arrow.third { opacity: 0.85; color: #8C99A6; }
.ul-arrow.first polyline { stroke-width: 10; }
.ul-arrow.second polyline { stroke-width: 7; }
.ul-arrow.third polyline { stroke-width: 6; }
.ul-arrow polyline, .ul-arrow .ul-ring { stroke: currentColor; }
.ul-head path { fill: currentColor; }
.ul-head.first { color: var(--aqua); }
.ul-head.second { color: var(--slate); }
.ul-head.third { color: #8C99A6; }
.ul-rank circle { fill: currentColor; stroke: #fff; stroke-width: 3; }
.ul-rank text { fill: #fff; font-size: 17px; font-weight: 700; text-anchor: middle; font-family: 'Open Sans', sans-serif; }

/* 解析のプレビュー・盤面編集 */
.ul-board[data-variant="preview"] .ul-grid { outline: 3px dashed var(--aqua); outline-offset: 2px; }
.ul-board[data-variant="edit"] .ul-grid { outline: 3px dashed var(--slate); outline-offset: 2px; }

@media (max-width: 767px) {
  .ul-board { --label: 22px; }
  .ul-files, .ul-ranks { font-size: 12px; }
}

/* ───────── 凡例 ───────── */
.board-legend { display: flex; flex-wrap: wrap; gap: 8px 18px; font-size: 12.5px; color: var(--text-sub); }
.board-legend span { display: inline-flex; align-items: center; gap: 6px; }
.board-legend i { width: 16px; height: 16px; display: inline-block; flex: none; }
.board-legend .lg-selected { background: var(--aqua-pale); box-shadow: inset 0 0 0 2px var(--slate); }
.board-legend .lg-target { border-radius: 50%; background: var(--aqua); width: 12px; height: 12px; }
.board-legend .lg-inc { background: var(--aqua-increase); }
.board-legend .lg-dec { background: var(--red-decrease); }

/* 盤の中の重なり（駒・枚数・矢印の z-index）を盤の中だけで閉じる。ヘッダーより手前に出ないように */
.ul-board, .coin-box { isolation: isolate; }
