/* 錬成将棋 — スタイル（light / dark 両対応。見た目はフレネミー将棋に合わせる） */
:root {
  --bg: #f4efe6;
  --bg-2: #ebe3d4;
  --surface: #fffaf1;
  --border: #d9cdb7;
  --text: #2b2418;
  --text-2: #6d6250;
  --accent: #b5562b;
  --accent-soft: rgba(181, 86, 43, 0.12);
  --danger: #c0392b;
  --board: #e9c37a;
  --board-line: #8a6a33;
  --coord: #6d6250;
  --piece: #f8e7bf;
  --piece-edge: #b08a4a;
  --piece-text: #1f1a12;
  --promoted: #c8261d;
  --sel: rgba(40, 120, 220, 0.38);
  --mv: rgba(40, 120, 220, 0.6);
  --cp: rgba(214, 38, 29, 0.75);
  --at: rgba(230, 126, 0, 0.85);
  --kiki: rgba(120, 80, 200, 0.18);
  --last: rgba(255, 196, 0, 0.42);
  --check: #d6261d;
  --atk: #b3261e;
  --hp: #1f7a3c;
  --mag: #6a3fc0;
  --heal: #0f7f86;
  --p0: #b5562b;
  --p1: #3468aa;
  --shadow: 0 8px 24px rgba(60, 40, 10, 0.12);
  --radius: 14px;
  --font: 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', 'Yu Gothic', system-ui, sans-serif;
  --font-piece: 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --bg-2: #181b26;
    --surface: #1d2130;
    --border: #2f3548;
    --text: #e8e6e1;
    --text-2: #a5a9b8;
    --accent: #f08a54;
    --accent-soft: rgba(240, 138, 84, 0.16);
    --danger: #ff6b5e;
    --board: #b98d4c;
    --board-line: #5a4220;
    --coord: #a5a9b8;
    --piece: #f3dfb2;
    --piece-edge: #8d6b35;
    --sel: rgba(80, 160, 255, 0.45);
    --mv: rgba(90, 170, 255, 0.75);
    --kiki: rgba(170, 130, 255, 0.25);
    --last: rgba(255, 200, 40, 0.38);
    --p0: #f08a54;
    --p1: #6ea0eb;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html { overflow-x: hidden; }
body {
  font-family: var(--font);
  color: var(--text);
  background: linear-gradient(180deg, var(--bg), var(--bg-2));
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  line-height: 1.6;
}
[hidden] { display: none !important; }

button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, transform 0.06s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button.big { width: 100%; padding: 14px; font-size: 1.1rem; margin-top: 18px; }
.muted { color: var(--text-2); }

/* ===== メニュー ===== */
.screen { max-width: 1040px; margin: 0 auto; padding: 12px 16px 32px; }
.menu { max-width: 640px; }
.menu-head { text-align: center; padding: 8px 0 4px; }
.back-link { display: inline-block; color: var(--text-2); font-size: 0.8rem; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.menu-topbar { display: flex; align-items: center; justify-content: flex-start; margin-bottom: 6px; }
.menu-head h1 { margin: 0; font-size: clamp(1.6rem, 6vw, 2.2rem); letter-spacing: 0.04em; }
.subtitle { margin: 0; font-weight: 700; color: var(--accent); font-size: 0.95rem; letter-spacing: 0.08em; }
.lede { color: var(--text-2); margin: 4px 0 12px; font-size: 0.92rem; }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
  margin-top: 14px;
  box-shadow: var(--shadow);
}
.panel h2 { font-size: 0.85rem; color: var(--text-2); margin: 14px 0 6px; font-weight: 600; }
.panel h2:first-child { margin-top: 0; }
#ai-opts > h2:first-child { margin-top: 14px; }
.hint { font-size: 0.8rem; color: var(--text-2); margin: 8px 0 0; }
.seg { display: grid; grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); gap: 6px; }
.seg button { padding: 8px 6px; font-size: 0.92rem; white-space: nowrap; }
.seg button[aria-checked='true'] { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.deck-pick { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; }
.deck-pick button { display: flex; flex-direction: column; align-items: center; padding: 6px 4px; font-size: 0.88rem; white-space: nowrap; line-height: 1.25; }
.deck-pick button small { font-size: 0.68rem; color: var(--text-2); font-weight: 400; }
.deck-pick button[aria-checked='true'] { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }

.table-wrap { overflow-x: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
.deck-table { border-collapse: collapse; font-size: 0.74rem; width: 100%; font-variant-numeric: tabular-nums; }
.deck-table th, .deck-table td { border-bottom: 1px solid var(--border); padding: 3px 4px; text-align: center; white-space: nowrap; }
.deck-table thead th { font-weight: 600; color: var(--text-2); }
.deck-table td { line-height: 1.2; }
.deck-table tbody th small { margin-left: 2px; color: var(--accent); font-size: 0.7rem; }
.deck-table tbody th { text-align: left; font-weight: 600; }
.deck-table td.halved { color: var(--accent); font-weight: 700; }
.deck-table .chosen { background: var(--accent-soft); }

.rules summary { cursor: pointer; font-weight: 700; }
.rules-body { font-size: 0.88rem; }
.rules-body h3 { font-size: 0.92rem; margin: 12px 0 4px; }
.rules-body ul, .rules-body ol { padding-left: 1.2em; margin: 4px 0; }
.rules-body li { margin: 2px 0; }
.menu-foot { margin-top: 18px; text-align: center; font-size: 0.78rem; color: var(--text-2); }

/* ===== 対局画面 ===== */
.game { padding-top: 8px; }
.game-head {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 2px 8px;
  margin-bottom: 6px;
}
.status { display: flex; align-items: center; gap: 6px; justify-content: flex-end; font-weight: 700; min-width: 0; flex-wrap: wrap; }
.status > * { white-space: nowrap; }
.turn { padding: 3px 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); font-size: 0.9rem; }
.turn.p0 { border-color: var(--p0); color: var(--p0); }
.turn.p1 { border-color: var(--p1); color: var(--p1); }
.thinking {
  padding: 3px 10px; border-radius: 999px; background: var(--p1); color: #fff; font-size: 0.85rem;
  animation: blink 1.2s ease-in-out infinite;
}
@keyframes blink { 50% { opacity: 0.55; } }
.check {
  padding: 3px 10px; border-radius: 999px; background: var(--check); color: #fff; font-size: 0.85rem;
  animation: pulse 1s ease-in-out infinite;
}
.turn-info { grid-column: 1 / -1; text-align: center; font-size: 0.85rem; min-height: 1.4em; }
.turn-info:empty { display: none; }
.enh-left { font-size: 1.05rem; color: var(--accent); }
/* 強化の義務（強化を使い切るまで指せない） */
.enh-hint { margin-left: 6px; padding: 1px 8px; border-radius: 999px; background: var(--accent); color: #fff; font-weight: 700; font-size: 0.78rem; white-space: nowrap; }
.enh-hint.ok { background: transparent; color: var(--text-2); font-weight: 400; border: 1px solid var(--border); }
.turn-info.need-enh .enh-left { display: inline-block; min-width: 1.5em; border-radius: 999px; background: var(--accent); color: #fff; animation: pulse 1s ease-in-out infinite; }
.enh-btn.must { box-shadow: 0 0 0 2px var(--accent); animation: enh-glow 1.2s ease-in-out infinite; }
@keyframes enh-glow { 50% { box-shadow: 0 0 0 4px var(--accent-soft), 0 0 10px var(--accent); } }
.detail-foot.need-enh, .detail-hint .need-enh { color: var(--accent); font-weight: 700; }
.board.locked .cell.dest::after { opacity: 0.4; }
.nudge { animation: nudge 0.45s ease-in-out; }
@keyframes nudge { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }
.boost {
  margin-left: 6px; padding: 1px 8px; border-radius: 999px; background: var(--danger); color: #fff; font-weight: 700; font-size: 0.78rem;
  white-space: nowrap;
}
.phase-chip { display: inline-block; padding: 0 7px; border-radius: 999px; font-size: 0.75rem; font-weight: 700; border: 1px solid var(--border); line-height: 1.5; }
.phase-chip.ph1 { background: #e7b416; color: #2b2418; border-color: #e7b416; }
.phase-chip.ph2 { background: var(--danger); color: #fff; border-color: var(--danger); }
@keyframes pulse { 50% { transform: scale(1.08); } }

.game-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
@media (min-width: 900px) {
  .game-body { grid-template-columns: minmax(0, 1fr) 320px; align-items: start; }
}
.play-area { width: 100%; max-width: min(620px, calc((100dvh - 150px) * 1.0)); min-width: min(100%, 300px); margin: 0 auto; }

/* 持ち駒（個体ごと） */
.hand {
  display: flex; align-items: center; gap: 3px; flex-wrap: wrap;
  min-height: 44px; padding: 3px 6px; margin: 4px 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.hand.to-move { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.hand-label { font-size: 0.72rem; color: var(--text-2); margin-right: 2px; white-space: nowrap; }
.hand-empty { font-size: 0.78rem; color: var(--text-2); opacity: 0.7; }
.hand-item {
  width: 34px; height: 40px; padding: 0; border: none; background: none; border-radius: 6px;
  display: grid; place-items: stretch;
  container-type: size;
}
.hand-item.sel { background: var(--sel); }

/* 盤 */
.board-frame {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 16px;
  grid-template-rows: 14px auto;
  gap: 2px;
}
.files { grid-column: 1; grid-row: 1; display: grid; grid-template-columns: repeat(9, 1fr); }
.ranks { grid-column: 2; grid-row: 2; display: grid; grid-template-rows: repeat(9, 1fr); }
.files span, .ranks span { display: grid; place-items: center; font-size: 11px; color: var(--coord); }
.board {
  grid-column: 1; grid-row: 2;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  aspect-ratio: 1 / 1;
  background: var(--board);
  border: 2px solid var(--board-line);
  border-radius: 4px;
  box-shadow: var(--shadow);
  touch-action: manipulation;
  user-select: none;
}
.cell {
  position: relative;
  border-right: 1px solid var(--board-line);
  border-bottom: 1px solid var(--board-line);
  display: grid; place-items: stretch;
  cursor: pointer;
  min-width: 0; min-height: 0;
  container-type: size;
}
.cell.col-last { border-right: none; }
.cell.row-last { border-bottom: none; }
.cell.last { background: var(--last); }
.cell.last-from { background: color-mix(in srgb, var(--last) 45%, transparent); }
.cell.ai-mark, .hand-item.ai-mark { box-shadow: inset 0 0 0 3px var(--mag); animation: pulse-mark 0.6s ease-in-out infinite alternate; }
@keyframes pulse-mark { to { box-shadow: inset 0 0 0 3px var(--mag), 0 0 10px var(--mag); } }
.board.busy .cell { cursor: progress; }
.cell.sel { background: var(--sel); }
.cell.kiki { background: var(--kiki); }
.cell.in-check { box-shadow: inset 0 0 0 3px var(--check); }
.cell.dest::after {
  content: ''; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%; translate: -50% -50%;
  border-radius: 50%; background: var(--mv); pointer-events: none; z-index: 3;
}
.cell.dest.k-drop::after { background: var(--mv); }
.cell.dest.k-capture::after, .cell.dest.k-attack::after {
  width: 92%; height: 92%; background: transparent; border-radius: 4px;
}
.cell.dest.k-capture::after { border: 3px solid var(--cp); }
.cell.dest.k-attack::after { border: 3px dashed var(--at); }
.cell.fl-magic::before, .cell.fl-heal::before {
  content: ''; position: absolute; inset: 0; opacity: 0; pointer-events: none; z-index: 2;
  animation: flash 1.8s ease-out;
}
.cell.fl-magic::before { background: var(--mag); }
.cell.fl-heal::before { background: var(--heal); }
@keyframes flash { 0% { opacity: 0.75; } 100% { opacity: 0; } }

/* 駒＋ステータス（セルの container 単位で大きさを決める） */
.pc { position: relative; width: 100%; height: 100%; pointer-events: none; }
.piece {
  position: absolute; left: 10%; right: 10%; top: 2%; height: 72%;
  display: grid; place-items: center;
  clip-path: polygon(50% 0%, 84% 14%, 100% 100%, 0% 100%, 16% 14%);
  background: linear-gradient(180deg, var(--piece), color-mix(in srgb, var(--piece) 82%, var(--piece-edge)));
  color: var(--piece-text);
  font-family: var(--font-piece);
  font-weight: 700;
  font-size: 40cqh;
  line-height: 1;
  padding-top: 10%;
  filter: drop-shadow(0 1px 0 var(--piece-edge));
}
.piece.down { rotate: 180deg; }
.piece.promoted { color: var(--promoted); }
.pstats {
  position: absolute; left: 0; right: 0; bottom: 0; height: 30%;
  display: flex; justify-content: space-between; align-items: flex-end;
  font-family: var(--font); font-weight: 800; line-height: 1;
  font-size: max(7px, min(29cqh, 27cqw, 17px));
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
}
.pstats span { padding: 0 1px; border-radius: 2px; background: color-mix(in srgb, var(--surface) 78%, transparent); }
.s-atk { color: var(--atk); }
.s-hp { color: var(--hp); }
.s-mag, .s-heal {
  position: absolute; top: 0; font-family: var(--font); font-weight: 800; line-height: 1;
  font-size: max(6px, 21cqh); padding: 0 1px; border-radius: 2px; color: #fff;
}
.s-mag { left: 0; background: var(--mag); }
.s-heal { right: 0; background: var(--heal); }
.owner1 .pstats { border-top: 2px solid var(--p1); }
.owner0 .pstats { border-top: 2px solid transparent; }
.result-title.win { color: var(--accent); }
.result-title.lose { color: var(--p1); }

/* 凡例 */
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 10px; font-size: 0.7rem; color: var(--text-2); margin: 4px 0 0; }
.lg-item { display: inline-flex; align-items: center; gap: 2px; white-space: nowrap; }
.lg { display: inline-block; padding: 0 3px; border-radius: 3px; font-weight: 800; line-height: 1.3; }
.lg-atk { color: var(--atk); }
.lg-hp { color: var(--hp); }
.lg-mag { background: var(--mag); color: #fff; }
.lg-heal { background: var(--heal); color: #fff; }
.lg-dot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
.lg-dot.mv { background: var(--mv); border-radius: 50%; }
.lg-dot.cp { border: 2px solid var(--cp); }
.lg-dot.at { border: 2px dashed var(--at); }

/* 詳細パネル */
.detail {
  margin-top: 8px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  min-height: 64px;
}
.detail-hint { margin: 0; font-size: 0.85rem; color: var(--text-2); }
.detail-head { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; margin-bottom: 6px; }
.detail-head .power { margin-left: auto; font-weight: 700; color: var(--accent); }
.stat-grid { display: grid; gap: 4px; }
.stat-row {
  display: grid; grid-template-columns: 3.2em 5.6em minmax(0, 1fr) auto; align-items: center; gap: 6px;
  font-size: 0.88rem;
}
.stat-row:not(:has(.enh-btn)) { grid-template-columns: 3.2em 5.6em minmax(0, 1fr); }
.stat-name { font-weight: 700; }
.st-atk .stat-name { color: var(--atk); }
.st-hp .stat-name { color: var(--hp); }
.st-mag .stat-name { color: var(--mag); }
.st-heal .stat-name { color: var(--heal); }
.stat-val { font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat-val small { color: var(--text-2); }
.stat-bar { height: 8px; border-radius: 4px; background: var(--bg-2); overflow: hidden; }
.stat-bar i { display: block; height: 100%; background: var(--text-2); }
.st-atk .stat-bar i { background: var(--atk); }
.st-hp .stat-bar i { background: var(--hp); }
.st-mag .stat-bar i { background: var(--mag); }
.st-heal .stat-bar i { background: var(--heal); }
.enh-btn { padding: 4px 10px; min-width: 4.2em; font-size: 0.85rem; border-color: var(--accent); }
.enh-btn:not(:disabled) { background: var(--accent-soft); }
.detail-foot { margin: 6px 0 0; font-size: 0.78rem; color: var(--text-2); }

.controls { display: flex; gap: 8px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
.controls button { flex: 1 1 0; max-width: 140px; white-space: nowrap; }
#flip-btn[aria-pressed='true'] { border-color: var(--accent); background: var(--accent-soft); }

/* サイド */
.side { display: grid; gap: 12px; min-width: 0; }
.side-panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; box-shadow: var(--shadow); min-width: 0;
}
.side-panel h2 { margin: 0 0 6px; font-size: 0.9rem; color: var(--text-2); }
.players { display: grid; gap: 8px; }
@media (min-width: 560px) and (max-width: 899px) { .players { grid-template-columns: 1fr 1fr; } }
.player { border: 1px solid var(--border); border-radius: 10px; padding: 6px 8px; border-left-width: 4px; }
.player.p0 { border-left-color: var(--p0); }
.player.p1 { border-left-color: var(--p1); }
.player.active { background: var(--accent-soft); }
.player-head { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
.player-head .deck { font-size: 0.78rem; color: var(--text-2); }
.player-sub { font-size: 0.8rem; margin: 2px 0 4px; }
.metric-table { width: 100%; border-collapse: collapse; font-size: 0.74rem; font-variant-numeric: tabular-nums; }
.metric-table th, .metric-table td { padding: 1px 3px; text-align: right; border-bottom: 1px dashed var(--border); white-space: nowrap; }
.metric-table th { font-weight: 600; color: var(--text-2); }
.metric-table tr th:first-child { text-align: left; }
.metric-table td.reached { color: var(--accent); font-weight: 800; }
.metric-table tr.lv1 td:nth-child(2), .metric-table tr.lv2 td:nth-child(2) { font-weight: 800; }

.log-list {
  list-style: none; margin: 0; padding: 0;
  max-height: 220px; overflow-y: auto; font-size: 0.8rem;
}
@media (min-width: 900px) { .log-list { max-height: 40vh; } }
.log-list li { padding: 1px 4px; border-bottom: 1px dashed var(--border); overflow-wrap: anywhere; }
.log-list li.ev-turn-start { color: var(--text-2); font-size: 0.74rem; border-bottom: none; padding-top: 4px; }
.log-list li.ev-magic, .log-list li.ev-magic-kill { color: var(--mag); }
.log-list li.ev-heal { color: var(--heal); }
.log-list li.ev-check { color: var(--check); font-weight: 700; }
.log-list li.ev-phase-up { color: var(--accent); font-weight: 700; }
.log-list li.ev-game-over, .log-list li.ev-repetition-warn { font-weight: 700; color: var(--danger); }

/* ダイアログ */
.dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 18px 20px; box-shadow: var(--shadow); min-width: 240px; max-width: calc(100vw - 32px);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.dialog-title { margin: 0 0 8px; font-weight: 700; text-align: center; }
.dialog-sub { margin: 0 0 14px; font-size: 0.85rem; color: var(--text-2); text-align: center; }
.dialog-buttons { display: flex; gap: 10px; justify-content: center; }
.dialog-buttons button { min-width: 96px; }

.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 16px;
  background: rgba(0, 0, 0, 0.45);
  animation: fadein 0.3s ease-out;
}
.overlay-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 22px 24px 16px; text-align: center; min-width: min(320px, 100%); box-shadow: var(--shadow);
}
.result-title { font-size: 1.6rem; font-weight: 900; margin: 0; }
.result-reason { color: var(--text-2); margin: 4px 0 16px; }
@keyframes fadein { from { opacity: 0; } }

/* トースト */
.toasts {
  position: fixed; left: 50%; bottom: 16px; translate: -50% 0; z-index: 60;
  display: grid; gap: 6px; width: min(420px, calc(100vw - 32px)); pointer-events: none;
}
.toast {
  background: var(--text); color: var(--bg); padding: 8px 12px; border-radius: 10px;
  font-size: 0.86rem; font-weight: 600; box-shadow: var(--shadow); text-align: center;
  animation: toast-in 0.25s ease-out;
}
.toast.magic { background: var(--mag); color: #fff; }
.toast.heal { background: var(--heal); color: #fff; }
.toast.phase { background: var(--accent); color: #fff; }
.toast.error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transition: opacity 0.4s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 420px) {
  .screen { padding-left: 10px; padding-right: 10px; }
  .board-frame { grid-template-columns: minmax(0, 1fr) 12px; }
  .ranks span, .files span { font-size: 9px; }
  .hand-item { width: 31px; height: 37px; }
  .stat-row { grid-template-columns: 2.8em 5.2em minmax(0, 1fr) auto; gap: 4px; font-size: 0.82rem; }
  .stat-row:not(:has(.enh-btn)) { grid-template-columns: 2.8em 5.2em minmax(0, 1fr); }
  .controls { gap: 6px; }
  .controls button { padding: 8px 4px; font-size: 0.9rem; }
}
@media (prefers-reduced-motion: reduce) {
  .check, .thinking, .cell.ai-mark, .hand-item.ai-mark, .cell.fl-magic::before, .cell.fl-heal::before, .toast,
  .turn-info.need-enh .enh-left, .enh-btn.must, .nudge { animation: none; }
}
.halved-note { color: var(--accent); font-weight: 700; }
