/* ============================
   本格将棋 - style.css
   ============================ */

:root {
    --board-bg: #e3c08d;
    --board-border: #8b4513;
    --line-color: #444;
    --text-color: #2c2c2c;
    --page-bg: #f0e6d2;
    --piece-bg: #f5deb3;
    --piece-border: #a0724a;
    --highlight-move: rgba(100, 180, 255, 0.45);
    --highlight-selected: rgba(80, 200, 80, 0.4);
    --highlight-last: rgba(255, 200, 80, 0.3);
    --promoted-color: #c62828;
    --komadai-bg: #d2b48c;
    --accent: #8b4513;
    --accent-dark: #5d2e0a;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { overscroll-behavior: none; }

body {
    font-family: "Hiragino Mincho ProN", "Yu Mincho", "MS Mincho", serif;
    background-color: var(--page-bg);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    padding: 15px;
}

/* ── ヘッダー ── */
.game-container {
    max-width: 1100px;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--accent);
    padding-bottom: 10px;
}

h1 { font-size: 1.8rem; color: var(--accent-dark); letter-spacing: 0.1em; }
.back-link { text-decoration: none; color: var(--accent); font-weight: bold; font-size: 0.9rem; }
.back-link:hover { color: var(--accent-dark); }

/* ── モード選択 ── */
.mode-selection {
    background-color: var(--komadai-bg);
    padding: 30px;
    border: 3px solid var(--accent);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    width: 340px;
    text-align: center;
}
.mode-selection h2 { font-size: 1.3rem; color: var(--accent-dark); }
.mode-option { display: flex; flex-direction: column; gap: 6px; text-align: left; }
.mode-option label { font-size: 0.9rem; font-weight: bold; color: #555; }
.mode-option select {
    padding: 10px;
    font-size: 1rem;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: #fff;
    font-family: inherit;
}
.btn-start {
    padding: 12px 24px;
    font-size: 1.1rem;
    font-weight: bold;
    font-family: inherit;
    background-color: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-start:hover { background-color: var(--accent-dark); }

/* ── ゲームエリア ── */
.game-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    width: 100%;
}
.main-layout {
    display: flex;
    gap: 16px;
    align-items: flex-start;
    justify-content: center;
}

/* ── 駒台 ── */
.komadai {
    width: 110px;
    min-height: 300px;
    background-color: var(--komadai-bg);
    border: 3px solid var(--accent);
    border-radius: 4px;
    box-shadow: 2px 3px 8px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    padding: 10px 8px;
}
.komadai.gote { align-self: flex-start; }
.komadai.sente { align-self: flex-end; }
.komadai-label {
    text-align: center;
    font-weight: bold;
    font-size: 0.95rem;
    border-bottom: 1px solid var(--accent);
    margin-bottom: 8px;
    padding-bottom: 6px;
    color: var(--accent-dark);
}
.captured-pieces {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: center;
}

/* ── 将棋盤 ── */
.board-wrapper { display: flex; flex-direction: column; align-items: center; }
.col-labels {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    width: 462px; /* 450px board + 12px padding */
    padding: 0 0 4px 0;
}
.col-labels span {
    text-align: center;
    font-size: 0.85rem;
    color: #666;
    font-weight: bold;
}
.board-with-rows { display: flex; align-items: stretch; gap: 4px; }
.row-labels {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 0;
}
.row-labels span {
    font-size: 0.85rem;
    color: #666;
    font-weight: bold;
    display: flex;
    align-items: center;
    height: calc(450px / 9);
}

.board-container {
    background-color: var(--board-border);
    padding: 6px;
    border-radius: 2px;
    box-shadow: 4px 5px 12px rgba(0,0,0,0.25);
}

.board {
    display: grid;
    grid-template-columns: repeat(9, 50px);
    grid-template-rows: repeat(9, 50px);
    background-color: var(--board-bg);
    border: 1.5px solid var(--line-color);
}

/* ── マス目 ── */
.cell {
    border: 0.5px solid var(--line-color);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    cursor: pointer;
    transition: background 0.15s;
}
.cell:hover { background-color: rgba(0,0,0,0.04); }
.cell.highlight { background-color: var(--highlight-move); }
.cell.highlight::after {
    content: "";
    position: absolute;
    width: 12px; height: 12px;
    border-radius: 50%;
    background: rgba(60, 120, 220, 0.5);
    pointer-events: none;
}
.cell.highlight-capture {
    background-color: rgba(220, 60, 60, 0.25);
}
.cell.selected { background-color: var(--highlight-selected); }
.cell.last-move { background-color: var(--highlight-last); }

/* 星印 */
.cell.star::before {
    content: "";
    position: absolute;
    width: 6px; height: 6px;
    border-radius: 50%;
    background: #333;
    z-index: 1;
}

/* ── 駒 ── */
.piece {
    width: 44px;
    height: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    font-weight: bold;
    user-select: none;
    position: relative;
    z-index: 2;
    cursor: pointer;
    line-height: 1;
}
.piece::before {
    content: "";
    position: absolute;
    inset: 0;
    background-color: var(--piece-bg);
    clip-path: polygon(50% 2%, 95% 22%, 95% 98%, 5% 98%, 5% 22%);
    box-shadow: 1px 1px 3px rgba(0,0,0,0.2);
    z-index: -1;
    border: 1px solid var(--piece-border);
}
.piece.gote { transform: rotate(180deg); }
.piece.promoted { color: var(--promoted-color); }

/* ── 持ち駒 ── */
.captured-piece {
    width: 38px;
    height: 42px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.1rem;
    font-weight: bold;
    cursor: pointer;
    position: relative;
    user-select: none;
    transition: transform 0.1s;
}
.captured-piece:hover { transform: scale(1.1); }
.captured-piece::before {
    content: "";
    position: absolute;
    inset: 0;
    background-color: var(--piece-bg);
    clip-path: polygon(50% 2%, 95% 22%, 95% 98%, 5% 98%, 5% 22%);
    z-index: -1;
    border: 1px solid var(--piece-border);
}
.captured-piece.gote { transform: rotate(180deg); }
.captured-piece.gote:hover { transform: rotate(180deg) scale(1.1); }
.captured-piece.selected-hand { outline: 2px solid #2196F3; outline-offset: 2px; border-radius: 4px; }
.count-badge {
    position: absolute;
    bottom: -2px;
    right: -4px;
    background-color: #c62828;
    color: #fff;
    border-radius: 50%;
    width: 18px; height: 18px;
    font-size: 0.65rem;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    z-index: 5;
}
/* 後手の駒のカウントバッジは回転を打ち消す */
.captured-piece.gote .count-badge { transform: rotate(180deg); }

/* ── 情報パネル ── */
.info-panel {
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    justify-content: center;
}
.status-message {
    font-size: 1.3rem;
    font-weight: bold;
    color: var(--accent-dark);
    min-width: 200px;
    text-align: center;
}
.move-counter {
    font-size: 0.95rem;
    color: #666;
    font-weight: bold;
}
.button-row { display: flex; gap: 10px; }
.btn-action {
    padding: 8px 18px;
    font-size: 0.9rem;
    font-weight: bold;
    font-family: inherit;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-resign { background: #c62828; color: #fff; }
.btn-resign:hover { background: #a01010; }
.btn-reset { background: #757575; color: #fff; }
.btn-reset:hover { background: #555; }

/* ── 棋譜パネル ── */
.kifu-panel {
    width: 100%;
    max-width: 700px;
    background: rgba(255,255,255,0.6);
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 12px;
}
.kifu-panel h3 {
    font-size: 1rem;
    margin-bottom: 8px;
    color: var(--accent-dark);
    border-bottom: 1px solid #ccc;
    padding-bottom: 4px;
}
.kifu-list {
    max-height: 160px;
    overflow-y: auto;
    font-size: 0.85rem;
    line-height: 1.8;
    color: #444;
    display: flex;
    flex-wrap: wrap;
    gap: 2px 12px;
}
.kifu-entry { white-space: nowrap; }
.kifu-entry:last-child { font-weight: bold; color: var(--accent-dark); }

/* ── モーダル ── */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    inset: 0;
    background-color: rgba(0,0,0,0.45);
    backdrop-filter: blur(2px);
}
.modal-content {
    background-color: #fefefe;
    margin: 20% auto;
    padding: 30px;
    border: 2px solid var(--accent);
    width: 320px;
    text-align: center;
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.3);
}
.modal-title { font-size: 1.2rem; font-weight: bold; margin-bottom: 20px; color: var(--accent-dark); }
.modal-buttons { display: flex; justify-content: center; gap: 16px; }
.btn-promote {
    padding: 10px 28px;
    font-size: 1rem;
    font-weight: bold;
    font-family: inherit;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
}
.btn-promote:hover { background: var(--accent-dark); }
.btn-no-promote {
    padding: 10px 28px;
    font-size: 1rem;
    font-weight: bold;
    font-family: inherit;
    background: #888;
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
}
.btn-no-promote:hover { background: #666; }

.result-content { padding: 40px 30px; }
.result-text { font-size: 1.5rem; font-weight: bold; color: var(--accent-dark); margin-bottom: 24px; }

/* ── 盤面反転 (オンライン後手プレイヤー視点) ── */
.main-layout.flipped .board { transform: rotate(180deg); }
.main-layout.flipped .col-labels { direction: rtl; }
.main-layout.flipped .row-labels { flex-direction: column-reverse; }
/* 駒台を左右入れ替え: 自分(後手)の駒台を画面右に */
.main-layout.flipped .komadai.gote { order: 1; align-self: flex-end; }
.main-layout.flipped .komadai.sente { order: -1; align-self: flex-start; }
/* 持ち駒: 自分(後手)を正立、相手(先手)を反転 */
.main-layout.flipped .captured-piece { transform: rotate(180deg); }
.main-layout.flipped .captured-piece:hover { transform: rotate(180deg) scale(1.1); }
.main-layout.flipped .captured-piece.gote { transform: rotate(0deg); }
.main-layout.flipped .captured-piece.gote:hover { transform: rotate(0deg) scale(1.1); }
/* バッジは常に画面に対して正立 */
.main-layout.flipped .captured-piece .count-badge { transform: rotate(180deg); }
.main-layout.flipped .captured-piece.gote .count-badge { transform: rotate(0deg); }

/* ── レスポンシブ ── */
@media (max-width: 760px) {
    .main-layout { flex-direction: column; align-items: center; }
    .board { grid-template-columns: repeat(9, 38px); grid-template-rows: repeat(9, 38px); }
    .col-labels { width: 342px; }
    .row-labels span { height: 38px; }
    .piece { width: 34px; height: 37px; font-size: 1.15rem; }
    .komadai { width: 100%; min-height: 50px; flex-direction: row; align-items: center; gap: 4px; }
    .komadai-label { border-bottom: none; margin-bottom: 0; padding-bottom: 0; border-right: 1px solid var(--accent); padding-right: 8px; margin-right: 4px; }
    .captured-pieces { flex-direction: row; flex-wrap: wrap; }
    .captured-piece { width: 32px; height: 36px; font-size: 0.95rem; }
    .cell.highlight::after { width: 8px; height: 8px; }
}
