:root {
    --cell-size: 60px;
    --board-color: #f7e6c4;
    --line-color: #8a5a36;
    --piece-size: 54px;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #f0f0f0;
    font-family: "Microsoft JhengHei", sans-serif;
    margin: 0;
    padding: 20px 12px 24px;
    box-sizing: border-box;
    min-height: 100svh;
    overflow-x: hidden;
    user-select: none;
}

#status {
    font-size: 24px;
    margin-bottom: 8px;
    font-weight: bold;
    text-align: center;
}

#room-info {
    min-height: 24px;
    margin-bottom: 14px;
    color: #5c3a21;
    font-weight: 700;
    text-align: center;
}

.board-container {
    padding: calc(var(--cell-size) / 2);
    background-color: var(--board-color);
    border: 4px double #8a5a36;
    box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
    position: relative;
    box-sizing: content-box;
    flex: 0 0 auto;
}

.board-grid {
    display: grid;
    grid-template-columns: repeat(8, var(--cell-size));
    grid-template-rows: repeat(9, var(--cell-size));
    border: 1px solid var(--line-color);
    position: relative;
}

.grid-cell {
    border: 1px solid var(--line-color);
    box-sizing: border-box;
    position: relative;
}

.grid-cell.in-river {
    border-left: none !important;
    border-right: none !important;
}

.grid-cell:nth-child(8n+1) { border-left: none; }
.grid-cell:nth-child(8n) { border-right: none; }
.grid-cell:nth-child(-n+8) { border-top: none; }
.grid-cell:nth-child(n+65) { border-bottom: none; }

.river {
    position: absolute;
    top: calc(var(--cell-size) * 4 + var(--cell-size) / 2);
    left: 0;
    width: 100%;
    height: var(--cell-size);
    display: flex;
    justify-content: space-around;
    align-items: center;
    font-size: 28px;
    font-weight: bold;
    color: #5c3a21;
    pointer-events: none;
    z-index: 1;
}

.grid-cell.palace-top-left::after,
.grid-cell.palace-top-right::after,
.grid-cell.palace-bottom-left::after,
.grid-cell.palace-bottom-right::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--line-color);
    clip-path: polygon(0 0, 10% 0, 100% 90%, 100% 100%, 90% 100%, 0 10%, 0 0);
    z-index: 0;
}

.grid-cell.palace-top-left::before,
.grid-cell.palace-top-right::before,
.grid-cell.palace-bottom-left::before,
.grid-cell.palace-bottom-right::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--line-color);
    clip-path: polygon(100% 0, 100% 10%, 10% 100%, 0 100%, 0 90%, 90% 0, 100% 0);
    z-index: 0;
}

.grid-cell:nth-child(4).palace-top-left::after { display: block; }
.grid-cell:nth-child(5).palace-top-right::before { display: block; }
.grid-cell:nth-child(12).palace-bottom-left::before { display: block; }
.grid-cell:nth-child(13).palace-bottom-right::after { display: block; }
.grid-cell:nth-child(60).palace-top-left::after { display: block; }
.grid-cell:nth-child(61).palace-top-right::before { display: block; }
.grid-cell:nth-child(68).palace-bottom-left::before { display: block; }
.grid-cell:nth-child(69).palace-bottom-right::after { display: block; }

.grid-cell::after,
.grid-cell::before {
    display: none;
}

#piece-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
}

.piece {
    width: var(--piece-size);
    height: var(--piece-size);
    border-radius: 50%;
    background-color: #f7e6c4;
    border: 2px solid #5c3a21;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 28px;
    font-weight: bold;
    position: absolute;
    transform: translate(-50%, -50%);
    transition: all 0.2s ease;
    pointer-events: auto;
    cursor: pointer;
}

.piece.red {
    color: #cc0000;
    border-color: #cc0000;
}

.piece.black {
    color: #000;
    border-color: #000;
}

.piece.selected {
    transform: translate(-50%, -50%) scale(1.1);
    box-shadow: 0 0 10px #ffea00;
    border-color: #ffea00;
}

.hint-dot {
    width: 16px;
    height: 16px;
    background-color: rgba(0, 150, 60, 0.68);
    border-radius: 50%;
    position: absolute;
    transform: translate(-50%, -50%);
    pointer-events: auto;
    cursor: pointer;
    z-index: 3;
}

#match-overlay,
#home-overlay {
    position: fixed;
    inset: 0;
    z-index: 999;
    display: flex;
    justify-content: center;
    align-items: center;
}

#match-overlay {
    background: rgba(0, 0, 0, 0.8);
    display: none;
}

#home-overlay {
    z-index: 1000;
    background: rgba(247, 230, 196, 0.96);
    padding: 24px;
    box-sizing: border-box;
}

.match-box,
.home-panel {
    background: #fffaf1;
    border: 2px solid #8a5a36;
    border-radius: 8px;
    box-shadow: 0 14px 36px rgba(0, 0, 0, 0.18);
    color: #333;
    text-align: center;
}

.match-box {
    padding: 30px;
    font-size: 24px;
    font-weight: bold;
}

.home-panel {
    width: min(420px, 100%);
    padding: 28px;
    box-sizing: border-box;
    display: grid;
    gap: 14px;
}

.home-panel h1 {
    margin: 0 0 4px;
    color: #5c3a21;
    font-size: 30px;
    letter-spacing: 0;
}

.mode-button,
.room-actions button {
    min-height: 44px;
    border: 0;
    border-radius: 6px;
    background: #5c3a21;
    color: white;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
}

.mode-button:hover,
.room-actions button:hover {
    background: #7a4d2d;
}

.join-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
}

#room-code-input {
    min-width: 0;
    min-height: 44px;
    border: 1px solid #b98d62;
    border-radius: 6px;
    box-sizing: border-box;
    padding: 0 12px;
    font-size: 16px;
    text-transform: uppercase;
}

.room-actions {
    position: sticky;
    top: 8px;
    z-index: 20;
    display: flex;
    gap: 8px;
    justify-content: center;
    align-self: flex-end;
    margin-bottom: 12px;
}

.room-actions button {
    min-height: 36px;
    padding: 0 12px;
    font-size: 14px;
}

.spinner {
    margin: 20px auto 0;
    width: 40px;
    height: 40px;
    border: 5px solid #f3f3f3;
    border-top: 5px solid #cc0000;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@media (max-width: 680px) {
    :root {
        --cell-size: min(calc((100svw - 32px) / 9), calc((100svh - 178px) / 10), 42px);
        --piece-size: calc(var(--cell-size) * 0.86);
    }

    body {
        justify-content: flex-start;
        padding: 10px 8px 14px;
    }

    #status {
        font-size: 18px;
        line-height: 1.35;
        margin: 2px 0 4px;
        max-width: 100%;
        word-break: break-word;
    }

    #room-info {
        min-height: 20px;
        margin-bottom: 8px;
        font-size: 14px;
        line-height: 1.35;
        max-width: 100%;
        word-break: break-word;
    }

    .board-container {
        border-width: 3px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.22);
    }

    .river {
        font-size: 20px;
    }

    .piece {
        border-width: 1.5px;
        font-size: calc(var(--cell-size) * 0.48);
    }

    .room-actions {
        position: static;
        width: 100%;
        margin-bottom: 8px;
        justify-content: center;
    }

    .room-actions button {
        min-height: 34px;
        padding: 0 12px;
        font-size: 14px;
    }

    #home-overlay {
        align-items: flex-start;
        padding: max(18px, env(safe-area-inset-top)) 14px 18px;
        overflow-y: auto;
    }

    .home-panel {
        padding: 20px;
        gap: 12px;
    }

    .home-panel h1 {
        font-size: 28px;
    }

    .join-row {
        grid-template-columns: 1fr;
    }

    .match-box {
        width: min(320px, 100%);
        padding: 24px 18px;
        box-sizing: border-box;
        font-size: 20px;
    }
}

@media (max-width: 380px), (max-height: 640px) {
    :root {
        --cell-size: min(calc((100svw - 28px) / 9), calc((100svh - 158px) / 10), 38px);
    }

    .river {
        font-size: 18px;
    }

    .hint-dot {
        width: 12px;
        height: 12px;
    }
}
