* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: "Noto Sans SC", "KaiTi", "STKaiti", "SimSun", sans-serif;
    background: #1a1a1a;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: 16px;
    color: #ffffff;
}

/* Top navigation (shared across all pages) */
.top-nav {
    width: 100%;
    max-width: 650px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    margin-bottom: 12px;
    background: #222;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
}

.nav-brand {
    font-size: clamp(15px, 3vw, 18px);
    font-weight: 600;
    letter-spacing: 2px;
    color: #f9dc4a;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.nav-link {
    color: #b0b0b0;
    text-decoration: none;
    font-size: clamp(13px, 2.5vw, 14px);
    padding: 4px 8px;
    border-radius: 3px;
    transition: all 0.15s ease;
}

.nav-link:hover {
    color: #fff;
    background: #333;
}

.nav-link.active {
    color: #fff;
    background: #005a9c;
}

/* Sound on/off toggle injected by static/js/modules/sound.js */
.sound-toggle {
    background: transparent;
    border: none;
    cursor: pointer;
    line-height: 1;
    font-size: 16px;
    padding: 2px 6px;
}

.sound-toggle.muted {
    opacity: 0.55;
    filter: grayscale(1);
}

.sound-toggle:focus-visible {
    outline: 2px solid #f9dc4a;
    outline-offset: 1px;
}

/* Logged-in user info in nav bar */
.nav-user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 8px;
    border-left: 1px solid #444;
}

.nav-username {
    font-size: clamp(12px, 2.5vw, 13px);
    color: #f9dc4a;
    white-space: nowrap;
}

.nav-logout-btn {
    font-family: inherit;
    font-size: clamp(11px, 2.2vw, 12px);
    padding: 3px 8px;
    background: transparent;
    color: #b0b0b0;
    border: 1px solid #555;
    border-radius: 3px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.nav-logout-btn:hover {
    color: #fff;
    border-color: #dc143c;
    background: rgba(220, 20, 60, 0.15);
}

.container {
    text-align: center;
    max-width: 650px;
    width: 100%;
}

h1 {
    color: #ffffff;
    font-size: clamp(20px, 5vw, 28px);
    font-weight: 600;
    margin-bottom: clamp(10px, 2vw, 16px);
    letter-spacing: clamp(2px, 0.5vw, 4px);
}

.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: clamp(6px, 1.5vw, 10px);
    padding: clamp(6px, 1.5vw, 10px) clamp(10px, 2.5vw, 14px);
    background: #2a2a2a;
    border-radius: 4px;
    border: 1px solid #3a3a3a;
}

.turn-indicator {
    color: #b0b0b0;
    font-size: clamp(14px, 3vw, 16px);
}

.turn-red {
    color: #dc143c;
    font-weight: 600;
}

.turn-black {
    color: #333333;
    font-weight: 600;
    background: #e0e0e0;
    padding: 2px 8px;
    border-radius: 2px;
}

/* Action buttons */
.action-btn {
    padding: clamp(5px, 1.2vw, 7px) clamp(10px, 2.5vw, 14px);
    font-size: clamp(12px, 2.5vw, 14px);
    font-family: inherit;
    background: #333;
    color: #ccc;
    border: 1px solid #555;
    border-radius: 2px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.action-btn:hover {
    background: #444;
    color: #fff;
    border-color: #777;
}

.action-btn:focus {
    outline: none;
    box-shadow: 0 0 0 3px #f9dc4a;
}

.action-btn.active {
    background: #005a9c;
    color: #fff;
    border-color: #005a9c;
}

.action-btn.danger {
    background: #8b0000;
    color: #e0e0e0;
    border-color: #8b0000;
}

.action-btn.danger:hover {
    background: #a50000;
    border-color: #a50000;
}

.action-btn.small {
    padding: 3px 10px;
    font-size: 12px;
}

.action-btn.success {
    background: #2e7d32;
    color: #fff;
    border-color: #2e7d32;
}

.action-btn.success:hover {
    background: #388e3c;
}

/* Draw banner */
.draw-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: clamp(6px, 1.5vw, 10px);
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2.5vw, 14px);
    background: #3a3000;
    border: 1px solid #8a7a00;
    border-radius: 4px;
    font-size: clamp(13px, 2.5vw, 15px);
    color: #f9dc4a;
}

/* Board */
.board-wrapper {
    display: flex;
    justify-content: center;
}

.board-container {
    position: relative;
    width: 630px;
    max-width: 95vw;
    aspect-ratio: 630 / 700;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    overflow: hidden;
}

.board-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.pieces-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.piece {
    position: absolute;
    width: 8.8%;
    height: auto;
    aspect-ratio: 1 / 1;
    object-fit: contain;
    pointer-events: auto;
    cursor: pointer;
    transition: transform 0.15s ease;
    transform: translate(-50%, -50%);
}

.piece:hover {
    transform: translate(-50%, -50%) scale(1.08);
}

.piece.selected {
    outline: clamp(2px, 0.5vw, 3px) solid #f9dc4a;
    outline-offset: 2px;
}

.piece.capture-highlight {
    filter: brightness(1.2) drop-shadow(0 0 4px rgba(255, 69, 0, 0.6));
}

.piece.move-to {
    animation: moveToPulse 0.5s ease-out;
    filter: drop-shadow(0 0 6px rgba(76, 175, 80, 0.8));
}

@keyframes moveToPulse {
    0% {
        filter: drop-shadow(0 0 12px rgba(76, 175, 80, 1));
    }
    100% {
        filter: drop-shadow(0 0 6px rgba(76, 175, 80, 0.8));
    }
}

.move-from-marker {
    position: absolute;
    width: 50px;
    height: 50px;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background-color: rgba(255, 193, 7, 0.4);
    border: 2px dashed rgba(255, 193, 7, 0.8);
    pointer-events: none;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.5);
    }
    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

.click-areas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.click-area {
    position: absolute;
    width: 10.3%;
    height: auto;
    aspect-ratio: 1 / 1;
    cursor: pointer;
    background: transparent;
    transform: translate(-50%, -50%);
    border-radius: 50%;
}

.click-area:hover {
    background: rgba(255, 255, 255, 0.1);
}

.click-area.valid-move {
    background: rgba(76, 175, 80, 0.3);
}

.click-area.valid-move:hover {
    background: rgba(76, 175, 80, 0.4);
}

.click-area.valid-capture {
    background: rgba(244, 67, 54, 0.3);
}

.click-area.valid-capture:hover {
    background: rgba(244, 67, 54, 0.4);
}

/* Toolbar */
.toolbar {
    display: flex;
    justify-content: center;
    gap: clamp(4px, 1vw, 8px);
    margin-top: clamp(8px, 2vw, 12px);
    flex-wrap: wrap;
}

/* Adjust panel */
.adjust-panel {
    margin-top: clamp(8px, 2vw, 12px);
    padding: clamp(8px, 2vw, 12px);
    background: #2a2a2a;
    border: 1px solid #005a9c;
    border-radius: 4px;
}

.adjust-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: clamp(13px, 2.5vw, 15px);
}

.adjust-hint {
    font-size: clamp(11px, 2vw, 13px);
    color: #888;
}

.adjust-pieces {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.adjust-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.adjust-group-label {
    font-size: clamp(12px, 2vw, 14px);
    color: #999;
    min-width: 32px;
}

.adjust-pieces-row {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

.adjust-piece {
    width: clamp(28px, 6vw, 38px);
    height: clamp(28px, 6vw, 38px);
    object-fit: contain;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 2px;
    transition: border-color 0.15s ease;
}

.adjust-piece:hover {
    border-color: #777;
}

.adjust-piece.selected {
    border-color: #f9dc4a;
    background: rgba(249, 220, 74, 0.15);
}

/* History */
.history-section {
    margin-top: clamp(8px, 2vw, 12px);
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    overflow: hidden;
}

.history-header {
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2.5vw, 14px);
    cursor: pointer;
    font-size: clamp(13px, 2.5vw, 15px);
    color: #999;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.history-title {
    flex-shrink: 0;
}

/* Inline navigation controls inside the history header */
.history-nav {
    display: flex;
    align-items: center;
    gap: 4px;
}

.hist-nav-btn {
    background: #333;
    border: 1px solid #444;
    color: #fff;
    padding: 2px 8px;
    border-radius: 3px;
    cursor: pointer;
    font-size: clamp(12px, 2.2vw, 14px);
    line-height: 1.4;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.hist-nav-btn:hover:not(:disabled) {
    background: #444;
    border-color: #555;
}

.hist-nav-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.hist-position {
    font-size: clamp(11px, 2vw, 13px);
    color: #888;
    min-width: 48px;
    text-align: center;
    user-select: none;
}

.history-header:hover {
    color: #ccc;
    background: #333;
}

.history-list {
    max-height: 180px;
    overflow-y: auto;
    border-top: 1px solid #3a3a3a;
    padding: 4px 0;
}

.history-row {
    display: flex;
    padding: 3px 10px;
    font-size: clamp(12px, 2.5vw, 14px);
    gap: 12px;
}

.history-row:nth-child(odd) {
    background: rgba(255, 255, 255, 0.02);
}

.history-num {
    color: #666;
    min-width: 28px;
}

.history-red {
    color: #dc143c;
    min-width: 80px;
}

.history-black {
    color: #aaa;
}

/* Clickable move cells inside the history list */
.history-move {
    cursor: pointer;
    padding: 1px 4px;
    border-radius: 3px;
    transition: background 0.15s ease;
    flex: 1;
}

.history-move:hover {
    background: rgba(255, 255, 255, 0.08);
}

/* Highlight the move corresponding to the currently-viewed board state */
.history-move.history-current {
    background: rgba(0, 90, 156, 0.45);
    color: #fff;
}

.history-empty {
    padding: 10px;
    color: #666;
    font-size: 13px;
}

/* Subtle visual cue on the board while reviewing a past position */
.board-container.reviewing {
    box-shadow: 0 0 0 2px rgba(0, 90, 156, 0.6), 0 0 12px rgba(0, 90, 156, 0.3);
}

/* Message */
.message {
    margin-top: clamp(8px, 2vw, 12px);
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2.5vw, 14px);
    font-size: clamp(13px, 2.5vw, 15px);
    color: #b0b0b0;
    min-height: clamp(28px, 7vw, 36px);
}

.message.error {
    color: #ef5350;
}

.message.success {
    color: #66bb6a;
}

.message.warning {
    color: #f9dc4a;
}

/* ===== Online: Auth (login / register) ===== */
.auth-container {
    max-width: 380px;
    margin-top: 24px;
}

.auth-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    background: #222;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    overflow: hidden;
}

.auth-tab {
    flex: 1;
    padding: 10px;
    background: transparent;
    color: #b0b0b0;
    border: none;
    font-family: inherit;
    font-size: clamp(13px, 2.5vw, 15px);
    cursor: pointer;
    transition: all 0.15s ease;
}

.auth-tab:hover {
    background: #2c2c2c;
    color: #fff;
}

.auth-tab.active {
    background: #005a9c;
    color: #fff;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    padding: 16px;
}

.auth-input {
    width: 100%;
    padding: 10px 12px;
    font-family: inherit;
    font-size: clamp(13px, 2.5vw, 15px);
    background: #1f1f1f;
    color: #fff;
    border: 1px solid #444;
    border-radius: 4px;
    outline: none;
    transition: border-color 0.15s ease;
}

.auth-input:focus {
    border-color: #005a9c;
    box-shadow: 0 0 0 2px rgba(0, 90, 156, 0.25);
}

.auth-submit {
    margin-top: 4px;
    padding: 10px;
    font-size: clamp(13px, 2.5vw, 15px);
    background: #005a9c;
    color: #fff;
    border-color: #005a9c;
}

.auth-submit:hover {
    background: #0070b8;
    border-color: #0070b8;
}

.auth-footer {
    margin-top: 14px;
    font-size: 13px;
}

.auth-subtitle {
    color: #b0b0b0;
    font-size: clamp(13px, 2.5vw, 15px);
    margin-bottom: 16px;
    margin-top: -8px;
}

/* ===== Online: Lobby ===== */
.user-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    margin-bottom: 12px;
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    font-size: clamp(13px, 2.5vw, 15px);
    color: #ccc;
}

.lobby-section {
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    padding: 14px 16px;
    margin-bottom: 12px;
    text-align: left;
}

.lobby-section h2 {
    font-size: clamp(15px, 3vw, 17px);
    font-weight: 600;
    margin-bottom: 8px;
    color: #f9dc4a;
}

.lobby-hint {
    font-size: clamp(12px, 2vw, 13px);
    color: #888;
    margin-bottom: 10px;
}

.join-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.join-row .auth-input {
    flex: 1;
    min-width: 120px;
}

.lobby-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    flex-wrap: wrap;
}

/* ===== Online: Room info / pending ===== */
.room-info {
    padding: 8px 12px;
    margin-bottom: 10px;
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    font-size: clamp(12px, 2.5vw, 14px);
    color: #ccc;
    text-align: center;
}

.pending-dot {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    font-size: 12px;
    color: #f9dc4a;
    background: rgba(249, 220, 74, 0.12);
    border: 1px solid rgba(249, 220, 74, 0.4);
    border-radius: 12px;
    animation: pending-pulse 1.2s ease-in-out infinite;
}

.pending-dot::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #f9dc4a;
    animation: pending-blink 1s ease-in-out infinite;
}

@keyframes pending-pulse {
    0%, 100% { opacity: 0.85; }
    50% { opacity: 1; }
}

@keyframes pending-blink {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

/* ===== AI Battle specific ===== */
.difficulty-selector {
    display: flex;
    align-items: center;
    gap: 6px;
}

.difficulty-label {
    font-size: clamp(12px, 2.5vw, 14px);
    color: #999;
    white-space: nowrap;
}

.difficulty-select {
    padding: 4px 8px;
    font-size: clamp(12px, 2.5vw, 14px);
    font-family: inherit;
    background: #1f1f1f;
    color: #fff;
    border: 1px solid #444;
    border-radius: 4px;
    outline: none;
    cursor: pointer;
    transition: border-color 0.15s ease;
}

.difficulty-select:focus {
    border-color: #f9dc4a;
    box-shadow: 0 0 0 2px rgba(249, 220, 74, 0.25);
}

.ai-thinking {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: clamp(6px, 1.5vw, 10px);
    padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2.5vw, 14px);
    background: rgba(0, 90, 156, 0.15);
    border: 1px solid rgba(0, 90, 156, 0.4);
    border-radius: 4px;
    font-size: clamp(13px, 2.5vw, 15px);
    color: #64b5f6;
}

.thinking-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #64b5f6;
    animation: thinking-pulse 1.2s ease-in-out infinite;
}

@keyframes thinking-pulse {
    0%, 100% {
        transform: scale(1);
        opacity: 0.5;
    }
    50% {
        transform: scale(1.4);
        opacity: 1;
    }
}

.last-move-info {
    display: flex;
    justify-content: center;
    gap: clamp(16px, 4vw, 24px);
    margin-bottom: clamp(6px, 1.5vw, 10px);
    padding: clamp(4px, 1vw, 6px) clamp(10px, 2.5vw, 14px);
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 4px;
    font-size: clamp(12px, 2.5vw, 14px);
}

.last-move-player {
    color: #dc143c;
}

.last-move-ai {
    color: #aaa;
}

.last-move-player span,
.last-move-ai span {
    font-weight: 500;
}

/* ===== Last move highlight on click areas ===== */
.click-area.last-move-from {
    background: rgba(255, 193, 7, 0.25);
    border: 2px dashed rgba(255, 193, 7, 0.6);
}

.click-area.last-move-to {
    background: rgba(76, 175, 80, 0.25);
    border: 2px solid rgba(76, 175, 80, 0.6);
}

/* ===== Board anti-shake (no animation during render) ===== */
.pieces-layer.no-anim .piece {
    transition: none !important;
}

.pieces-layer.no-anim .move-from-marker {
    animation: none !important;
}

/* ===== Scrollbar styling ===== */
.history-list::-webkit-scrollbar {
    width: 6px;
}

.history-list::-webkit-scrollbar-track {
    background: #1f1f1f;
    border-radius: 3px;
}

.history-list::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 3px;
}

.history-list::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* ===== Responsive adjustments ===== */

/* --- Tablet / small desktop (≤ 768px) --- */
@media (max-width: 768px) {
    body {
        padding: 10px;
    }

    .top-nav {
        padding: 8px 10px;
    }

    .nav-links {
        gap: 8px;
    }

    .nav-user-info {
        gap: 6px;
        padding-left: 6px;
    }

    .game-info {
        padding: 8px 10px;
    }

    .action-btn {
        padding: 6px 10px;
        font-size: 13px;
    }

    .toolbar {
        gap: 6px;
    }

    .toolbar-divider {
        height: 18px;
        margin: 0 2px;
    }

    .history-section,
    .analyze-panel,
    .review-panel,
    .replay-panel,
    .chat-panel,
    .adjust-panel {
        margin-top: 10px;
    }

    .review-list,
    .replay-list,
    .chat-messages {
        max-height: 220px;
    }
}

/* --- Mobile phones (≤ 480px) --- */
@media (max-width: 480px) {
    body {
        padding: 8px;
        padding-top: env(safe-area-inset-top, 8px);
        padding-bottom: env(safe-area-inset-bottom, 8px);
    }

    .top-nav {
        flex-direction: column;
        gap: 8px;
        padding: 10px;
    }

    .nav-links {
        flex-wrap: wrap;
        justify-content: center;
        width: 100%;
        gap: 6px;
    }

    .nav-link {
        padding: 6px 10px;
        font-size: 13px;
    }

    .nav-user-info {
        padding-left: 0;
        border-left: none;
        padding-top: 8px;
        border-top: 1px solid #444;
        width: 100%;
        justify-content: center;
    }

    h1 {
        font-size: 20px;
        margin-bottom: 10px;
        letter-spacing: 2px;
    }

    .game-info {
        flex-direction: column;
        gap: 6px;
        align-items: flex-start;
        padding: 8px 10px;
    }

    .turn-indicator {
        width: 100%;
        font-size: 15px;
    }

    .board-container {
        max-width: 98vw;
        border-radius: 4px;
    }

    .toolbar {
        justify-content: flex-start;
        overflow-x: auto;
        padding-bottom: 6px;
        -webkit-overflow-scrolling: touch;
        gap: 4px;
    }

    .action-btn {
        flex-shrink: 0;
        padding: 7px 10px;
        font-size: 12px;
        min-height: 36px;
        min-width: 44px;
    }

    .action-btn.small {
        padding: 5px 8px;
        min-height: 32px;
    }

    .last-move-info {
        flex-direction: column;
        gap: 4px;
        text-align: center;
        padding: 6px 10px;
    }

    .difficulty-selector {
        flex-wrap: wrap;
        justify-content: center;
    }

    .difficulty-select {
        padding: 6px 8px;
        min-height: 36px;
    }

    .draw-banner {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        padding: 8px 10px;
    }

    .history-header {
        padding: 8px 10px;
        flex-wrap: wrap;
    }

    .history-list {
        max-height: 140px;
    }

    .history-row {
        padding: 4px 8px;
        gap: 8px;
    }

    .history-red {
        min-width: 60px;
    }

    .hist-nav-btn {
        padding: 4px 10px;
        min-height: 32px;
        min-width: 36px;
    }

    .message {
        padding: 6px 10px;
        min-height: 32px;
        font-size: 13px;
    }

    /* Adjust panel mobile */
    .adjust-panel {
        padding: 10px;
    }

    .adjust-hint {
        font-size: 12px;
    }

    .adjust-piece {
        width: 36px;
        height: 36px;
    }

    /* Analyze / review / replay / chat panels mobile */
    .analyze-header,
    .review-header,
    .replay-header,
    .chat-header {
        padding: 8px 10px;
        font-size: 14px;
    }

    .analyze-content,
    .review-content,
    .replay-content,
    .chat-content {
        padding: 10px;
    }

    .review-list {
        max-height: 200px;
    }

    .review-row {
        flex-wrap: wrap;
        gap: 4px 8px;
        padding: 6px 0;
    }

    .review-comment {
        width: 100%;
        margin-left: 32px;
    }

    .replay-controls {
        gap: 6px;
        margin-bottom: 10px;
    }

    .replay-btn {
        padding: 8px 12px;
        font-size: 14px;
        min-height: 36px;
        min-width: 36px;
    }

    .replay-list {
        max-height: 180px;
    }

    .replay-row {
        padding: 6px 8px;
    }

    .chat-messages {
        max-height: 180px;
        margin-bottom: 10px;
    }

    .chat-text {
        max-width: 80%;
    }

    .chat-input-area {
        gap: 4px;
    }

    .chat-input {
        padding: 8px 10px;
        min-height: 36px;
    }

    .chat-send-btn {
        padding: 8px 14px;
        min-height: 36px;
    }

    /* Auth pages mobile */
    .auth-container {
        margin-top: 16px;
        max-width: 100%;
    }

    .auth-form {
        padding: 14px;
    }

    .auth-input {
        padding: 10px 12px;
        min-height: 40px;
    }

    .auth-submit {
        padding: 10px;
        min-height: 40px;
    }

    /* Lobby mobile */
    .user-bar {
        flex-wrap: wrap;
        padding: 8px 10px;
        font-size: 13px;
    }

    .lobby-section {
        padding: 12px;
    }

    .lobby-section h2 {
        font-size: 15px;
        margin-bottom: 6px;
    }

    .join-row {
        flex-direction: column;
    }

    .join-row .auth-input {
        min-width: 100%;
    }

    .lobby-actions {
        flex-direction: column;
    }

    .lobby-actions .action-btn {
        width: 100%;
        min-height: 40px;
    }

    /* Room info mobile */
    .room-info {
        padding: 6px 10px;
        font-size: 12px;
        word-break: break-all;
    }

    /* Click areas bigger on mobile */
    .click-area {
        width: 11%;
    }

    .piece {
        width: 9.2%;
    }
}

/* --- Very small screens (≤ 360px) --- */
@media (max-width: 360px) {
    body {
        padding: 6px;
    }

    .top-nav {
        padding: 8px;
        gap: 6px;
    }

    .nav-link {
        padding: 5px 8px;
        font-size: 12px;
    }

    .nav-brand {
        font-size: 14px;
        letter-spacing: 1px;
    }

    .action-btn {
        padding: 6px 8px;
        font-size: 11px;
        min-height: 34px;
    }

    h1 {
        font-size: 18px;
    }

    .turn-indicator {
        font-size: 14px;
    }

    .review-quality {
        font-size: 11px;
        padding: 1px 4px;
    }
}

/* --- Landscape mode on mobile --- */
@media (max-height: 500px) and (orientation: landscape) {
    body {
        padding: 8px;
    }

    .top-nav {
        padding: 6px 10px;
        margin-bottom: 6px;
    }

    .nav-brand {
        font-size: 14px;
    }

    .nav-link {
        font-size: 12px;
        padding: 3px 6px;
    }

    h1 {
        display: none;
    }

    .game-info {
        padding: 4px 8px;
        margin-bottom: 4px;
    }

    .turn-indicator {
        font-size: 13px;
    }

    .toolbar {
        margin-top: 6px;
    }

    .action-btn {
        padding: 4px 8px;
        font-size: 11px;
        min-height: 28px;
    }

    .board-container {
        max-height: calc(100vh - 120px);
        max-width: calc(100vh - 120px) * 0.9;
    }

    .message {
        margin-top: 4px;
        padding: 4px 8px;
        min-height: 24px;
        font-size: 12px;
    }

    .history-section,
    .analyze-panel,
    .review-panel,
    .replay-panel,
    .chat-panel,
    .adjust-panel {
        margin-top: 6px;
    }

    .history-list,
    .review-list,
    .replay-list,
    .chat-messages {
        max-height: 100px;
    }
}

/* --- Touch device optimizations --- */
@media (hover: none) and (pointer: coarse) {
    .piece:hover {
        transform: translate(-50%, -50%);
    }

    .action-btn:hover {
        background: #333;
        color: #ccc;
        border-color: #555;
    }

    .action-btn:active {
        background: #444;
        color: #fff;
        border-color: #777;
    }

    .nav-link:hover {
        color: #b0b0b0;
        background: transparent;
    }

    .nav-link:active {
        color: #fff;
        background: #333;
    }

    .close-btn:hover {
        color: #888;
    }

    .close-btn:active {
        color: #fff;
    }

    .adjust-piece:hover {
        border-color: transparent;
    }

    .adjust-piece:active {
        border-color: #f9dc4a;
    }

    .replay-row:hover {
        background: transparent;
    }

    .replay-row:active {
        background: #333;
    }

    .click-area:hover {
        background: transparent;
    }

    .click-area.valid-move:hover {
        background: rgba(76, 175, 80, 0.3);
    }

    .click-area.valid-capture:hover {
        background: rgba(244, 67, 54, 0.3);
    }

    .history-header:hover {
        color: #999;
        background: transparent;
    }

    .history-header:active {
        color: #ccc;
        background: #333;
    }

    .chat-send-btn:hover {
        background: #005a9c;
    }

    .chat-send-btn:active {
        background: #0077cc;
    }

    .auth-tab:hover {
        background: transparent;
        color: #b0b0b0;
    }

    .auth-tab:active {
        background: #2c2c2c;
        color: #fff;
    }

    .nav-logout-btn:hover {
        color: #b0b0b0;
        border-color: #555;
        background: transparent;
    }

    .nav-logout-btn:active {
        color: #fff;
        border-color: #dc143c;
        background: rgba(220, 20, 60, 0.15);
    }

    .replay-btn:hover {
        background: #333;
        border-color: #444;
    }

    .replay-btn:active {
        background: #444;
        border-color: #555;
    }
}

/* --- Focus styles for accessibility --- */
.action-btn:focus-visible,
.auth-input:focus-visible,
.difficulty-select:focus-visible,
.nav-link:focus-visible {
    outline: 2px solid #f9dc4a;
    outline-offset: 2px;
}

/* ===== Disabled state ===== */
.action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* ===== Toolbar divider ===== */
.toolbar-divider {
    width: 1px;
    height: 20px;
    background: #444;
    margin: 0 4px;
}

/* ===== AI assistance opt-in switch (admins / granted users) ===== */
.ai-assist-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    font-size: clamp(11px, 2.2vw, 12px);
    color: #f9dc4a;
    background: #2a2a2a;
    border: 1px solid #4a4a4a;
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
}

.ai-assist-toggle:hover {
    border-color: #f9dc4a;
}

.ai-assist-toggle input {
    accent-color: #005a9c;
    cursor: pointer;
}

/* ===== Analyze panel ===== */
.analyze-panel, .review-panel, .replay-panel, .chat-panel {
    margin-top: clamp(8px, 2vw, 12px);
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    overflow: hidden;
    text-align: left;
}

.analyze-header, .review-header, .replay-header, .chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: #333;
    font-size: clamp(13px, 2.5vw, 15px);
    font-weight: 600;
}

.close-btn {
    background: transparent;
    border: none;
    color: #888;
    font-size: 20px;
    cursor: pointer;
    line-height: 1;
    padding: 0 4px;
    transition: color 0.15s ease;
}

.close-btn:hover {
    color: #fff;
}

.analyze-content, .review-content, .replay-content, .chat-content {
    padding: clamp(8px, 2vw, 12px);
}

.analyze-loading, .review-loading {
    text-align: center;
    color: #888;
    padding: 16px;
}

.analyze-recommendation, .analyze-evaluation {
    margin-bottom: 8px;
    font-size: clamp(12px, 2.2vw, 14px);
}

.analyze-label {
    color: #888;
    margin-right: 4px;
}

.analyze-desc {
    color: #f9dc4a;
}

.analyze-score {
    color: #4cc9f0;
}

/* ===== Review panel ===== */
.review-list {
    max-height: 300px;
    overflow-y: auto;
}

.review-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid #333;
    font-size: clamp(12px, 2.2vw, 14px);
}

.review-row:last-child {
    border-bottom: none;
}

.review-num {
    color: #666;
    min-width: 32px;
}

.review-color {
    color: #f9dc4a;
    min-width: 24px;
}

.review-desc {
    flex: 1;
    color: #ccc;
}

.review-quality {
    min-width: 48px;
    text-align: center;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: clamp(11px, 2vw, 12px);
}

.review-excellent {
    background: rgba(0, 128, 0, 0.2);
    color: #4ade80;
}

.review-good {
    background: rgba(0, 90, 156, 0.2);
    color: #4cc9f0;
}

.review-miss {
    background: rgba(255, 165, 0, 0.2);
    color: #fb923c;
}

.review-bad {
    background: rgba(220, 20, 60, 0.2);
    color: #f87171;
}

.review-comment {
    color: #888;
    font-size: clamp(11px, 2vw, 12px);
}

/* ===== IO panel (import / export) ===== */
.io-panel {
    margin-top: clamp(8px, 2vw, 12px);
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    overflow: hidden;
    text-align: left;
}

.io-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: #333;
    font-size: clamp(13px, 2.5vw, 15px);
    font-weight: 600;
}

.io-content {
    padding: clamp(8px, 2vw, 12px);
}

.io-mode-tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}

.io-tab {
    flex: 1;
    padding: 6px 12px;
    background: #333;
    color: #aaa;
    border: 1px solid #444;
    border-radius: 3px;
    cursor: pointer;
    font-family: inherit;
    font-size: clamp(12px, 2.2vw, 14px);
    transition: all 0.15s ease;
}

.io-tab:hover {
    background: #444;
    color: #fff;
}

.io-tab.active {
    background: #005a9c;
    color: #fff;
    border-color: #005a9c;
}

.io-format-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.io-format-label {
    color: #888;
    font-size: clamp(12px, 2.2vw, 13px);
    flex-shrink: 0;
}

.io-format-chips {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

.io-chip {
    padding: 4px 10px;
    background: #2a2a2a;
    color: #ccc;
    border: 1px solid #555;
    border-radius: 12px;
    cursor: pointer;
    font-family: inherit;
    font-size: clamp(11px, 2vw, 12px);
    transition: all 0.15s ease;
    user-select: none;
}

.io-chip:hover {
    background: #3a3a3a;
    color: #fff;
    border-color: #777;
}

.io-chip.active {
    background: #f9dc4a;
    color: #1a1a1a;
    border-color: #f9dc4a;
    font-weight: 600;
}

.io-textarea {
    width: 100%;
    min-height: 110px;
    max-height: 240px;
    padding: 8px 10px;
    background: #1f1f1f;
    color: #ddd;
    border: 1px solid #444;
    border-radius: 4px;
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: clamp(11px, 2vw, 13px);
    line-height: 1.5;
    resize: vertical;
    word-break: break-all;
}

.io-textarea:focus {
    outline: none;
    border-color: #005a9c;
    box-shadow: 0 0 0 2px rgba(0, 90, 156, 0.25);
}

.io-textarea[readonly] {
    background: #181818;
    color: #aaa;
    cursor: default;
}

.io-hint {
    margin-top: 6px;
    color: #888;
    font-size: clamp(11px, 2vw, 12px);
    min-height: 1em;
    line-height: 1.4;
}

.io-hint.io-error {
    color: #f87171;
}

.io-hint.io-success {
    color: #4ade80;
}

.io-actions {
    display: flex;
    gap: 6px;
    margin-top: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

/* ===== Replay panel ===== */
.replay-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
}

.replay-btn {
    background: #333;
    border: 1px solid #444;
    color: #fff;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.15s ease;
}

.replay-btn:hover {
    background: #444;
    border-color: #555;
}

.replay-position {
    font-size: clamp(13px, 2.5vw, 15px);
    color: #888;
    min-width: 60px;
    text-align: center;
}

.replay-list {
    max-height: 250px;
    overflow-y: auto;
}

.replay-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.15s ease;
    font-size: clamp(12px, 2.2vw, 14px);
}

.replay-row:hover {
    background: #333;
}

.replay-row.replay-current {
    background: rgba(0, 90, 156, 0.3);
}

.replay-num {
    color: #666;
    min-width: 32px;
}

.replay-color {
    color: #f9dc4a;
    min-width: 24px;
}

.replay-desc {
    color: #ccc;
}

.replay-empty, .review-empty {
    text-align: center;
    color: #666;
    padding: 16px;
}

/* ===== Chat panel ===== */
.chat-messages {
    max-height: 200px;
    overflow-y: auto;
    margin-bottom: 12px;
}

.chat-message {
    display: flex;
    gap: 6px;
    padding: 6px 0;
    font-size: clamp(12px, 2.2vw, 14px);
}

.chat-me {
    justify-content: flex-end;
}

.chat-me .chat-name {
    color: #4cc9f0;
}

.chat-me .chat-text {
    background: rgba(0, 90, 156, 0.3);
    color: #fff;
}

.chat-other {
    justify-content: flex-start;
}

.chat-other .chat-name {
    color: #f9dc4a;
}

.chat-other .chat-text {
    background: #333;
    color: #ccc;
}

.chat-name {
    min-width: 60px;
    font-weight: 500;
}

.chat-text {
    padding: 4px 8px;
    border-radius: 4px;
    max-width: 70%;
    word-break: break-word;
}

/* ===== Leaderboard & Admin tables ===== */
.leaderboard-hint {
    color: #888;
    font-size: clamp(12px, 2.2vw, 14px);
    margin-bottom: 12px;
}

.data-table-wrapper {
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: clamp(12px, 2.2vw, 14px);
    min-width: 480px;
}

.data-table th {
    background: #222;
    color: #f9dc4a;
    font-weight: 600;
    text-align: left;
    padding: 8px 10px;
    border-bottom: 1px solid #3a3a3a;
    white-space: nowrap;
}

.data-table td {
    padding: 7px 10px;
    border-bottom: 1px solid #333;
    color: #ddd;
}

.data-table tbody tr:hover {
    background: #323232;
}

.data-table tbody tr:last-child td {
    border-bottom: none;
}

.data-empty {
    text-align: center;
    color: #666;
    padding: 18px 10px !important;
}

.rank-cell {
    font-weight: 600;
    text-align: center;
    min-width: 40px;
}

.player-cell {
    font-weight: 500;
    color: #f9dc4a;
}

.win-cell {
    color: #66bb6a;
    font-weight: 600;
}

.loss-cell {
    color: #ef5350;
}

.time-cell {
    color: #888;
    font-size: clamp(11px, 2vw, 13px);
    white-space: nowrap;
}

/* ===== Admin: stats cards & badges ===== */
.stats-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px;
    margin-bottom: 16px;
}

.stat-card {
    background: #2a2a2a;
    border: 1px solid #3a3a3a;
    border-radius: 6px;
    padding: 14px 10px;
    text-align: center;
}

.stat-value {
    font-size: clamp(20px, 4vw, 26px);
    font-weight: 700;
    color: #f9dc4a;
}

.stat-label {
    margin-top: 4px;
    font-size: clamp(12px, 2vw, 13px);
    color: #888;
}

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: clamp(11px, 1.8vw, 12px);
    border: 1px solid #444;
}

.badge-admin {
    color: #f9dc4a;
    border-color: #8a7a00;
    background: rgba(249, 220, 74, 0.12);
}

.badge-user {
    color: #aaa;
    background: #333;
}

.admin-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
}

@media (max-width: 560px) {
    .data-table {
        min-width: 560px;
    }
}

.chat-input-area {
    display: flex;
    gap: 6px;
}

.chat-input {
    flex: 1;
    background: #333;
    border: 1px solid #444;
    color: #fff;
    padding: 6px 10px;
    border-radius: 4px;
    font-family: inherit;
    font-size: clamp(12px, 2.2vw, 14px);
    outline: none;
    transition: border-color 0.15s ease;
}

.chat-input:focus {
    border-color: #005a9c;
}

.chat-send-btn {
    background: #005a9c;
    border: none;
    color: #fff;
    padding: 6px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: clamp(12px, 2.2vw, 14px);
    transition: background 0.15s ease;
}

.chat-send-btn:hover {
    background: #0077cc;
}

/* ===== Deduce panel (推演浮动窗口) ===== */
.deduce-panel {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1000;
    width: min(720px, 95vw);
    max-height: 90vh;
    background: #1f1f1f;
    border: 1px solid #3a3a3a;
    border-radius: 8px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
    overflow: hidden;
    flex-direction: column;
}

.deduce-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    background: #2a2a2a;
    border-bottom: 1px solid #3a3a3a;
}

.deduce-title {
    font-size: clamp(14px, 2.6vw, 16px);
    font-weight: 600;
    color: #f9dc4a;
}

.deduce-hint {
    flex: 1;
    font-size: clamp(11px, 2vw, 12px);
    color: #888;
}

.deduce-body {
    display: flex;
    gap: 12px;
    padding: 12px;
    overflow: auto;
}

.deduce-board-wrapper {
    flex-shrink: 0;
}

.deduce-board-container {
    position: relative;
    width: clamp(280px, 42vw, 390px);
    aspect-ratio: 9 / 10;
}

.deduce-board-container .board-image {
    width: 100%;
    height: 100%;
    display: block;
    border-radius: 4px;
}

.deduce-board-container .pieces-layer,
.deduce-board-container .click-areas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.deduce-board-container .move-from-marker {
    width: 28px;
    height: 28px;
}

.deduce-sidebar {
    flex: 1;
    min-width: 180px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.deduce-turn {
    font-size: clamp(13px, 2.4vw, 15px);
    color: #ccc;
    padding: 8px 10px;
    background: #2a2a2a;
    border-radius: 4px;
}

.deduce-controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.deduce-history-header {
    font-size: clamp(12px, 2.2vw, 14px);
    color: #888;
    padding: 4px 2px;
    border-bottom: 1px solid #333;
}

.deduce-history-list {
    flex: 1;
    max-height: 260px;
    overflow-y: auto;
    font-size: clamp(12px, 2.2vw, 14px);
}

@media (max-width: 600px) {
    .deduce-panel {
        width: 97vw;
        max-height: 95vh;
        border-radius: 6px;
    }

    .deduce-header {
        padding: 8px 12px;
        gap: 8px;
    }

    .deduce-body {
        flex-direction: column;
        padding: 10px;
        gap: 10px;
    }

    .deduce-board-container {
        width: 100%;
        max-width: 320px;
        margin: 0 auto;
    }

    .deduce-sidebar {
        min-width: 0;
    }

    .deduce-controls .action-btn {
        flex: 1;
        min-width: 80px;
    }

    .deduce-history-list {
        max-height: 140px;
    }
}

@media (max-width: 480px) {
    .deduce-panel {
        width: 100vw;
        height: 100vh;
        max-height: 100vh;
        top: 0;
        left: 0;
        transform: none;
        border-radius: 0;
    }

    .deduce-header {
        padding: 10px 12px;
    }

    .deduce-title {
        font-size: 15px;
    }

    .close-btn {
        font-size: 22px;
        padding: 4px 8px;
        min-height: 36px;
        min-width: 36px;
    }

    .deduce-body {
        flex-direction: column;
        padding: 8px;
        gap: 8px;
    }

    .deduce-board-container {
        max-width: 100%;
    }

    .deduce-turn {
        font-size: 13px;
        padding: 6px 8px;
    }

    .deduce-controls {
        gap: 6px;
    }

    .deduce-controls .action-btn {
        padding: 6px 10px;
        font-size: 12px;
        min-height: 36px;
    }

    .deduce-history-list {
        max-height: 120px;
    }
}

@media (max-height: 500px) and (orientation: landscape) {
    .deduce-panel {
        width: 95vw;
        max-height: 95vh;
    }

    .deduce-body {
        flex-direction: row;
        padding: 8px;
        gap: 8px;
    }

    .deduce-board-container {
        width: 50vw;
        max-width: 300px;
    }

    .deduce-sidebar {
        min-width: 160px;
    }

    .deduce-history-list {
        max-height: 120px;
    }
}
