:root {
    /* Default Theme */
    --bg-color: #f0f2f5;
    --text-primary: #2d3436;
    --card-bg: white;
    --msg-bg: #fafafa;
    --border-color: #eee;
    --input-border: #ddd;

    /* Board colors (constant across themes usually, unless specified) */
    --board-bg: #e6b36a;
    --board-line: #5c3a18;
    --piece-red: #d63031;
    --piece-black: #2d3436;
    --piece-bg: #fdf5e6;
    --highlight-color: rgba(9, 132, 227, 0.4);
    --valid-move-color: rgba(0, 184, 148, 0.6);
    --shadow-soft: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-deep: 0 10px 20px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] {
    --bg-color: #1e1e1e;
    --text-primary: #ecf0f1;
    --card-bg: #2d2d2d;
    --msg-bg: #383838;
    --border-color: #444;
    --input-border: #555;
    --shadow-soft: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-deep: 0 10px 20px rgba(0, 0, 0, 0.5);
}

[data-theme="bamboo"] {
    --bg-color: #e8f5e9;
    --text-primary: #1b5e20;
    --card-bg: #f1f8e9;
    --msg-bg: #dcedc8;
    --border-color: #c5e1a5;
    --input-border: #aed581;
    --shadow-soft: 0 4px 6px rgba(51, 105, 30, 0.1);
}

.theme-selector {
    position: absolute;
    top: 20px;
    right: 20px;
}

#theme-select {
    padding: 8px;
    border-radius: 6px;
    border: 1px solid var(--input-border);
    background: var(--card-bg);
    color: var(--text-primary);
    cursor: pointer;
    font-family: inherit;
}


* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Noto Serif SC', serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.app-container {
    width: 100%;
    max-width: 1600px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

header {
    text-align: center;
}

header h1 {
    font-family: 'Ma Shan Zheng', cursive;
    font-size: 3rem;
    color: var(--piece-black);
    margin-bottom: 5px;
}

header p {
    font-size: 1rem;
    color: #636e72;
    letter-spacing: 2px;
}

/* Lobby Screen */
#lobby-screen {
    width: 100%;
    max-width: 1000px;
    background: var(--card-bg);
    padding: 30px;
    border-radius: 12px;
    box-shadow: var(--shadow-deep);
    display: none;
    /* Hidden by default or controlled via JS class */
}

#lobby-screen.active {
    display: block;
}

.hidden {
    display: none !important;
}

.lobby-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 20px;
}

.player-input {
    display: flex;
    gap: 10px;
    align-items: center;
}

.create-room-section {
    margin-bottom: 30px;
    display: flex;
    gap: 10px;
}

.room-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}

.lobby-chat-card {
    background: var(--msg-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 15px;
    margin-top: 20px;
}

.lobby-chat-card h3 {
    margin-bottom: 10px;
    font-size: 1rem;
}

.lobby-chat-card .chat-messages {
    height: 150px;
    margin: 0 0 10px 0;
}

.spectator-count {
    margin-top: 10px;
    padding: 8px;
    background: var(--msg-bg);
    border-radius: 6px;
    font-size: 0.85rem;
    color: #888;
    text-align: center;
}

.room-card {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    box-shadow: var(--shadow-soft);
}

.room-name {
    font-size: 1.2rem;
    font-weight: bold;
    margin-bottom: 15px;
    color: var(--text-primary);
}

.table-visual {
    width: 150px;
    height: 100px;
    background: var(--board-bg);
    border: 4px solid var(--board-line);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
    position: relative;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
}

.table-visual::before {
    content: '';
    position: absolute;
    width: 80%;
    height: 1px;
    background: var(--board-line);
}

.table-visual::after {
    content: '';
    position: absolute;
    height: 80%;
    width: 1px;
    background: var(--board-line);
}

.seat-container {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-top: 10px;
}

.seat-btn {
    padding: 8px 16px;
    border-radius: 20px;
    border: 1px solid var(--valid-move-color);
    background: transparent;
    color: var(--valid-move-color);
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.9rem;
}

.seat-btn:hover:not(:disabled) {
    background: var(--valid-move-color);
    color: white;
}

.seat-btn.occupied {
    background: var(--border-color);
    border-color: #ccc;
    color: #888;
    cursor: not-allowed;
}

.spectate-btn {
    padding: 6px 12px;
    border-radius: 15px;
    border: 1px solid #888;
    background: transparent;
    color: #888;
    cursor: pointer;
    font-size: 0.8rem;
    margin-top: 10px;
}

.spectate-btn:hover {
    background: #888;
    color: white;
}

/* Player Labels on Board */
.board-wrapper {
    position: relative;
    flex-shrink: 0;
}

.player-label {
    position: absolute;
    padding: 6px 12px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.85rem;
    color: var(--text-primary);
    z-index: 10;
    box-shadow: var(--shadow-soft);
}

.player-label.top-left {
    top: -45px;
    left: 0;
}

.player-label.bottom-right {
    bottom: -45px;
    right: 0;
}

/* Layout: Chat - Board - Controls */
.game-area {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 20px;
    width: 100%;
}

.chat-sidebar {
    width: 300px;
    flex-shrink: 0;
}

.board-wrapper {
    flex-shrink: 0;
}

.game-controls {
    width: 300px;
    flex-shrink: 0;
}

/* Board rotation for Black player */
.board-container.rotated {
    transform: rotate(180deg);
}

.board-container.rotated .piece {
    transform: rotate(180deg);
}

.board-container.rotated .piece.selected {
    transform: rotate(180deg) scale(1.15);
}

.board-container.rotated .river {
    transform: rotate(180deg);
}

/* Chat Styles */
.chat-card {
    background: var(--card-bg);
    padding: 20px;
    border-radius: 12px;
    box-shadow: var(--shadow-soft);
    height: 600px;
    /* Match close to board height + header */
    display: flex;
    flex-direction: column;
}

.chat-messages {
    flex-grow: 1;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    margin: 10px 0;
    padding: 10px;
    border-radius: 8px;
    background: var(--msg-bg);
}

.chat-msg {
    margin-bottom: 8px;
    font-size: 0.9rem;
    word-wrap: break-word;
}

.chat-msg .time {
    color: #999;
    font-size: 0.75rem;
    margin-left: 8px;
    float: right;
}

.chat-msg.system {
    color: var(--text-secondary);
    font-style: italic;
    text-align: center;
}

.chat-emoji {
    width: 24px;
    height: 24px;
    vertical-align: middle;
    margin: 0 2px;
}

.chat-msg .sender {
    font-weight: 600;
}

.chat-input-area {
    display: flex;
    gap: 5px;
}

#chat-input {
    flex-grow: 1;
    padding: 8px;
    border: 1px solid var(--input-border);
    border-radius: 4px;
    outline: none;
    background: var(--card-bg);
    /* Add background for dark mode input */
    color: var(--text-primary);
}

#chat-input:focus {
    border-color: var(--piece-black);
}

.btn-send {
    padding: 8px 15px;
    background-color: var(--piece-black);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.btn-send:hover {
    background-color: #454d50;
}

/* Board Styling */
.board-container {
    background: #deb887;
    padding: 18px;
    border-radius: 10px;
    box-shadow: var(--shadow-deep);
    border: 6px solid #8b4513;
    position: relative;
    user-select: none;
}

.board {
    width: 540px;
    height: 600px;
    position: relative;
    background-color: #eecfa1;
}

/* ... (Remaining board styles same as before) ... */
.grid-layer {
    position: absolute;
    top: 30px;
    left: 30px;
    width: 480px;
    height: 540px;
    display: flex;
    flex-direction: column;
}

.grid-row {
    display: flex;
    height: 60px;
}

.cell {
    width: 60px;
    height: 60px;
    border: 1px solid var(--board-line);
    border-top: none;
    border-left: none;
}

.grid-layer {
    display: none;
}

.board-lines {
    background: none;
}

.board-lines line,
.board-lines path {
    stroke: var(--board-line);
    stroke-width: 2.5;
    fill: none;
}

.river {
    position: absolute;
    top: 270px;
    left: 30px;
    width: 480px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 120px;
    z-index: 3;
}

.river-text {
    font-family: 'STXingkai', '华文行楷', 'KaiTi', '楷体', 'Ma Shan Zheng', cursive;
    font-size: 2.8rem;
    color: var(--board-line);
    letter-spacing: 0.3em;
}

/* 红方视角：汉界在右边，旋转180度 */
.river-text.right {
    transform: rotate(180deg);
}

/* 黑方视角（棋盘旋转时）：交换位置并调整旋转 */
.board-container.rotated .river {
    flex-direction: row-reverse;
}

.board-container.rotated .river-text {
    transform: rotate(180deg);
}

.board-container.rotated .river-text.right {
    transform: rotate(0deg);
}

/* Pieces */
.pieces-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.piece {
    width: 52px;
    height: 52px;
    position: absolute;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 30px;
    font-weight: bold;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3), inset 0 -4px 4px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    pointer-events: auto;
    transition: transform 0.2s ease, box-shadow 0.2s ease, left 0.25s ease-out, top 0.25s ease-out, opacity 0.15s ease;
    z-index: 10;
    margin-left: -26px;
    margin-top: -26px;
    background: radial-gradient(circle at 30% 30%, #fffdf5, #e0d0b0);
    border: 3px solid #bfa76f;
}

.piece::before {
    content: '';
    position: absolute;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: 1.5px dashed rgba(0, 0, 0, 0.2);
}

.piece.red {
    color: var(--piece-red);
    border-color: #c0392b;
}

.piece.black {
    color: var(--piece-black);
    border-color: #2d3436;
}

.piece.selected {
    transform: scale(1.15);
    box-shadow: 0 0 0 4px rgba(255, 234, 167, 0.8), 0 8px 15px rgba(0, 0, 0, 0.3);
    z-index: 20;
    background: #fff;
}

/* Logic moves highlight */
.highlight-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.marker-dot {
    position: absolute;
    width: 16px;
    height: 16px;
    background-color: var(--valid-move-color);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: auto;
    cursor: pointer;
    z-index: 5;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

.marker-dot:hover {
    transform: translate(-50%, -50%) scale(1.3);
    background-color: #00b894;
}

/* Capture Hint - pulsing red glow on capturable enemy pieces */
.piece.capture-target {
    box-shadow: 0 0 0 4px rgba(255, 71, 87, 0.6), 0 0 15px rgba(231, 76, 60, 0.4);
    z-index: 15;
    animation: pulse-red 1.5s infinite;
}

@keyframes pulse-red {
    0% {
        box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.5);
    }

    50% {
        box-shadow: 0 0 0 6px rgba(255, 71, 87, 0.2);
    }

    100% {
        box-shadow: 0 0 0 2px rgba(255, 71, 87, 0.5);
    }
}

/* Last Move Trace - yellow markers on start and end positions */
.last-move-marker {
    position: absolute;
    width: 52px;
    height: 52px;
    border: 2px dashed rgba(45, 52, 54, 0.5);
    background-color: rgba(255, 234, 167, 0.4);
    border-radius: 8px;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 2;
}

/* Controls */
.game-controls {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.status-card,
.history-card {
    background: var(--card-bg);
    padding: 20px;
    border-radius: 12px;
    box-shadow: var(--shadow-soft);
}

.turn-indicator {
    font-size: 1.5rem;
    font-weight: bold;
    margin-top: 10px;
    padding: 10px;
    border-radius: 8px;
    text-align: center;
}

.red-turn {
    background-color: rgba(214, 48, 49, 0.1);
    color: var(--piece-red);
}

.black-turn {
    background-color: rgba(45, 52, 54, 0.1);
    color: var(--piece-black);
}

/* Dark theme: better contrast for black turn indicator */
[data-theme="dark"] .black-turn {
    background-color: rgba(200, 200, 200, 0.15);
    color: #e0e0e0;
}

.message {
    margin-top: 10px;
    color: #e74c3c;
    font-weight: bold;
    text-align: center;
    min-height: 1.5em;
}

.history-card {
    flex-grow: 1;
    max-height: 300px;
    display: flex;
    flex-direction: column;
}

#move-history {
    list-style: none;
    overflow-y: auto;
    margin-top: 10px;
    flex-grow: 1;
    font-family: monospace;
    font-size: 0.9rem;
}

#move-history li {
    padding: 5px 0;
    border-bottom: 1px solid var(--border-color);
}

.btn {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
}

.btn.primary {
    background-color: var(--piece-black);
    color: white;
}

.btn.secondary {
    background-color: #dfe6e9;
    color: #2d3436;
    margin-top: 10px;
}

.btn.secondary:hover {
    background-color: #b2bec3;
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal-overlay.hidden {
    display: none;
}

.modal {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    min-width: 300px;
}

.modal h3 {
    margin-bottom: 20px;
    color: var(--piece-black);
}

.modal p {
    margin-bottom: 25px;
    color: #636e72;
}

.modal-actions {
    display: flex;
    gap: 15px;
}

footer {
    color: #b2bec3;
    font-size: 0.8rem;
    margin-top: 20px;
}

/* SVG Line Drawing fix */
svg.board-lines {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
}

/* Responsive adjustments */
@media (max-width: 1100px) {
    .game-area {
        flex-wrap: wrap;
        justify-content: center;
    }

    .chat-sidebar {
        order: 3;
        width: 100%;
        max-width: 500px;
    }

    .chat-card {
        height: 300px;
    }

    .game-controls {
        order: 2;
        width: 100%;
        max-width: 500px;
    }
}

/* Mobile - scale down board to fit screen */
@media (max-width: 600px) {
    .app-container {
        padding: 10px;
    }

    header h1 {
        font-size: 2rem;
    }

    .board-wrapper {
        /* Fixed scale for mobile: 390px screen / 588px board = ~0.63 */
        transform: scale(0.63);
        transform-origin: top center;
        /* Adjust margin to compensate for scaled size: 620px * (1-0.63) = ~229px */
        margin-bottom: -230px;
    }

    .player-label {
        font-size: 0.7rem;
        padding: 4px 8px;
    }

    .player-label.top-left {
        top: -35px;
    }

    .player-label.bottom-right {
        bottom: -35px;
    }

    .chat-sidebar,
    .game-controls {
        width: 95%;
        max-width: none;
    }

    .chat-card {
        height: 250px;
    }

    .history-card {
        max-height: 200px;
    }

    .btn {
        padding: 10px;
        font-size: 0.9rem;
    }

    /* Lobby adjustments */
    #lobby-screen {
        padding: 15px;
    }

    .lobby-header {
        flex-direction: column;
        gap: 15px;
    }

    .room-list {
        grid-template-columns: 1fr;
    }

    .create-room-section {
        flex-direction: column;
    }

    .create-room-section input,
    .create-room-section button {
        width: 100%;
    }

    .theme-selector {
        position: static;
        display: flex;
        gap: 10px;
        margin-bottom: 10px;
    }
}