/* 将棋盤認識サイト rebuild-v2 — docs/REBUILD_SPEC.md に基づく再実装 */

:root {
  --bg: #fff;
  --text: #1b1b1d;
  --muted: #6b7280;
  --line: #e6e6ea;
  --soft: #f4f5f7;
  --accent: #dc4a37;
  --board: #f0c574;
  --board-line: #8a6a3a;
  --board-border: #5a3d1e;
  --promoted: #c0392b;
  --radius: 12px;
  --shadow: 0 4px 16px rgba(0, 0, 0, .06);
  --serif: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 12px 24px;
}

.logo { display: block; width: 100%; height: auto; }
.howto { display: block; width: 100%; height: auto; margin-top: 4px; }

/* ---------- ボタン共通 ---------- */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1;
}
.btn:active { background: var(--soft); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-cta {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-size: 17px;
  min-height: 50px;
  flex: 1;
}

.actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 12px 0;
}
.actions .btn { flex: 1; }

/* ---------- 状態機械による表示制御 ---------- */
[data-role="card"] { display: none; }
[data-role="pick"] { display: flex; }
[data-role="convert"] { display: none; }
[data-role="overlay"] { display: none; }
[data-role="err"] { display: none; }
[data-role="done"] { display: none; }
[data-role="reset"] { display: none; }

body[data-state="empty"] [data-role="pick"] { display: flex; }

body[data-state="photo"] [data-role="pick"] { display: none; }
body[data-state="photo"] [data-role="card"] { display: block; }
body[data-state="photo"] [data-role="convert"] { display: flex; }

body[data-state="busy"] [data-role="pick"] { display: none; }
body[data-state="busy"] [data-role="card"] { display: block; }
body[data-state="busy"] [data-role="overlay"] { display: flex; }

body[data-state="done"] [data-role="pick"] { display: none; }
body[data-state="done"] [data-role="card"] { display: block; }
body[data-state="done"] [data-role="done"] { display: block; }
body[data-state="done"] [data-role="reset"] { display: flex; }

body[data-state="error"] [data-role="pick"] { display: none; }
body[data-state="error"] [data-role="card"] { display: block; }
body[data-state="error"] [data-role="err"] { display: block; }
body[data-state="error"] [data-role="reset"] { display: flex; }

/* ---------- 結果カード ---------- */
.card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  margin: 12px 0;
}
.card-label {
  font-size: 13px;
  color: var(--muted);
  margin: 4px 0 6px;
}

.photo-wrap { position: relative; }
.photo { display: block; width: 100%; height: auto; border-radius: 8px; }

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, .7);
  border-radius: 8px;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.overlay-text { font-size: 14px; color: var(--text); font-weight: 600; }

.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.result-error .err-msg {
  font-size: 14px;
  color: var(--text);
  text-align: center;
  margin: 12px 0;
}

/* ---------- 盤コンポーネント ---------- */
.board-unit {
  margin: 8px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.board-frame { display: block; }

.file-nums {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  /* rank-nums 列ぶんの余白を右に確保して盤と桁を合わせる */
  padding-right: 16px;
}
.file-nums span {
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.4;
}

.board-row { display: flex; align-items: stretch; }

.board {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 9 / 9.7;
  background: linear-gradient(#f3cd82, #edbe66);
  border: 2px solid var(--board-border);
  position: relative;
}

.rank-nums {
  width: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.rank-nums span {
  font-size: 11px;
  color: var(--muted);
  line-height: 1;
}

.sq {
  border: 0.5px solid var(--board-line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-weight: 700;
  font-size: clamp(13px, 4.2vw, 26px);
  color: #201a12;
  cursor: pointer;
  user-select: none;
  overflow: hidden;
  line-height: 1;
}
.sq.gote { transform: rotate(180deg); }
.sq.promoted { color: var(--promoted); }
.sq.empty { color: transparent; }
.sq.selected { background: rgba(135, 206, 235, .6); }

/* 星（装飾） */
.star {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--board-border);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

/* ---------- 持駒 ---------- */
.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  min-height: 36px;
  padding: 2px 0;
}
.hand--gote { justify-content: flex-start; }
.hand--sente { justify-content: flex-end; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  min-height: 32px;
  padding: 2px 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}
.chip.zero { opacity: .35; }
.chip .koma {
  font-family: var(--serif);
  font-weight: 700;
  font-size: 16px;
  line-height: 1;
}
.hand--gote .chip .koma { transform: rotate(180deg); }
.chip .cnt { font-size: 11px; color: var(--muted); }
.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 編集パレット ---------- */
.palette {
  margin: 8px 0;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft);
}
.palette-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr); /* 14駒がきれいに2行(7+7)。min-height×aspect-ratioの膨張はみ出しも解消 */
  gap: 4px;
  margin-bottom: 4px;
}
.pcell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font-family: var(--serif);
  font-weight: 700;
  font-size: 17px;
  cursor: pointer;
  line-height: 1;
  min-width: 0;
  min-height: 0;
}
.pcell.gote { transform: rotate(180deg); }
.pcell.promoted { color: var(--promoted); }
.pcell.current { outline: 2px solid var(--accent); outline-offset: -2px; }
.pcell.empty { color: var(--muted); }
.palette-close { margin-top: 6px; width: 100%; }

/* ---------- 手番セグメント ---------- */
.segment {
  display: flex;
  margin: 12px 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.seg-btn {
  flex: 1;
  min-height: 44px;
  border: none;
  background: #fff;
  color: var(--muted);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn.active { background: var(--accent); color: #fff; }
.seg-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- 出力 ---------- */
.output-label { font-size: 13px; color: var(--muted); margin: 10px 0 6px; }
.app-links { display: flex; gap: 8px; justify-content: center; }
.linkbtn {
  appearance: none;
  min-height: 44px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}
.linkbtn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.copy-row { display: flex; gap: 8px; align-items: stretch; }
.copy-row .btn { flex: 1; white-space: nowrap; padding-left: 8px; padding-right: 8px; }
.format-select {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

/* ---------- トースト ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 40px;
  transform: translateX(-50%);
  background: rgba(27, 27, 29, .92);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 1000;
  animation: toast-in .2s ease;
}
@keyframes toast-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- フッター ---------- */
.site-foot {
  text-align: center;
  font-size: 14px;
  color: #555;
  margin-top: 24px;
}
.site-foot a { color: #2563eb; }
.faq { display: inline-flex; margin: 4px auto; }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
  .toast { animation: none; }
}

/* ---------- 使い方（手動で開くオーバーレイ） ---------- */
[data-role="help"] { display: none; margin-top: 8px; }
body[data-state="empty"] [data-role="help"] { display: flex; }
.btn-ghost {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  min-height: 40px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.btn-ghost:hover { color: var(--text); }

.howto-modal { position: fixed; inset: 0; z-index: 1100; }
.howto-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.howto-card {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(92vw, 420px);
  max-height: 86vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 16px;
  padding: 18px 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.howto-kuma { display: block; width: 78%; margin: 0 auto 8px; }
.howto-steps { list-style: none; margin: 0 0 14px; padding: 0; }
.howto-steps li {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 4px;
  border-top: 1px solid var(--line);
  font-size: 14px; line-height: 1.55; text-align: left;
}
.howto-steps b { font-size: 15px; }
.step-no {
  flex: none;
  width: 26px; height: 26px; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff;
  font-size: 14px; font-weight: 700;
  margin-top: 2px;
}
.howto-close { width: 100%; }
