/**
 * 中国象棋 - 样式表
 * 古典中国风配色，全设备响应式布局
 * 支持：手机竖屏/横屏、平板、PC大屏
 */

/* ======== CSS 变量 ======== */
:root {
    /* 配色 */
    --bg-dark: #2C1810;
    --bg-mid: #4A2C1A;
    --bg-light: #3D2B1F;
    --gold: #F0D5A0;
    --gold-dim: #A08060;
    --brown: #6B4226;
    --brown-light: #8B5A2B;
    --brown-dark: #3A2518;
    --red-primary: #8B2500;
    --red-hover: #A03000;
    --red-active: #B83C00;
    --text-primary: #F5E6D3;
    --text-secondary: #D4C4A8;
    --overlay-bg: rgba(0, 0, 0, 0.75);

    /* 尺寸 */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-xl: 20px;

    /* 间距 */
    --gap-xs: 4px;
    --gap-sm: 8px;
    --gap-md: 12px;
    --gap-lg: 20px;
    --gap-xl: 30px;

    /* 字体 */
    --font-serif: "KaiTi", "STKaiti", "FangSong", "SimSun", serif;
    --font-sans: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif;
    --font-mono: "Courier New", "Source Code Pro", monospace;

    /* 安全区域 */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);

    /* 动画 */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;
    --transition-slow: 0.4s ease;
}

/* ======== 基础重置 ======== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    /* 防止 iOS 弹性滚动 */
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
}

body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: var(--font-sans);
    background: linear-gradient(135deg, var(--bg-dark) 0%, var(--bg-mid) 50%, var(--bg-light) 100%);
    background-attachment: fixed;
    color: var(--text-primary);
    /* 防止移动端拖拽刷新 */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    /* 安全区域内边距 */
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
}

/* 防止选中文字和长按菜单 */
body, button, input {
    -webkit-user-select: none;
    user-select: none;
}

/* 输入框允许选中 */
input[type="text"] {
    -webkit-user-select: text;
    user-select: text;
}

/* ======== 通用 ======== */
.screen {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 0;
    left: 0;
}

.center-content {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ======== 模式选择页 ======== */
.menu-container {
    text-align: center;
    padding: var(--gap-xl);
    max-width: 440px;
    width: 92%;
}

.game-title {
    font-size: clamp(2em, 8vw, 3em);
    font-weight: bold;
    color: var(--gold);
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 40px rgba(240, 213, 160, 0.2);
    letter-spacing: 0.3em;
    margin-bottom: 5px;
    font-family: var(--font-serif);
    line-height: 1.3;
}

.menu-subtitle {
    font-size: clamp(0.8em, 3vw, 1em);
    color: var(--gold-dim);
    margin-bottom: clamp(20px, 5vh, 40px);
    letter-spacing: 0.15em;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: var(--gap-md);
}

.section-title {
    font-size: clamp(1.3em, 5vw, 1.6em);
    color: var(--gold);
    margin-bottom: clamp(16px, 3vh, 25px);
    font-family: var(--font-serif);
}

/* ======== 按钮 ======== */
.menu-btn {
    border: none;
    cursor: pointer;
    border-radius: var(--radius-lg);
    padding: 16px 24px;
    font-size: 1em;
    font-family: var(--font-sans);
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    position: relative;
    overflow: hidden;
    /* 移动端触摸目标至少 44px */
    min-height: 44px;
}

.menu-btn:active {
    transform: scale(0.97);
}

/* hover 仅在非触屏设备生效 */
@media (hover: hover) {
    .menu-btn:hover {
        transform: translateY(-1px);
    }
    .menu-btn:active {
        transform: scale(0.97) translateY(0);
    }
}

.primary-btn {
    background: linear-gradient(135deg, var(--red-primary), #A03000);
    color: #FFF;
    box-shadow: 0 4px 15px rgba(139, 37, 0, 0.4);
}

@media (hover: hover) {
    .primary-btn:hover {
        background: linear-gradient(135deg, var(--red-hover), var(--red-active));
        box-shadow: 0 6px 20px rgba(139, 37, 0, 0.5);
    }
}

.secondary-btn {
    background: linear-gradient(135deg, var(--brown-dark), #4D3225);
    color: var(--gold);
    border: 1px solid var(--brown);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

@media (hover: hover) {
    .secondary-btn:hover {
        background: linear-gradient(135deg, #4D3225, #5D3E2E);
        border-color: var(--brown-light);
    }
}

.small-btn {
    padding: 10px 20px;
    font-size: 0.9em;
    width: auto;
    border-radius: var(--radius-sm);
    min-height: 40px;
}

.small-margin {
    margin: 6px;
    padding: 12px 28px;
}

.btn-icon {
    font-size: 1.4em;
}

.btn-text {
    font-size: 1.1em;
    font-weight: bold;
}

.btn-desc {
    font-size: 0.75em;
    opacity: 0.7;
    position: absolute;
    bottom: 4px;
    right: 12px;
}

.back-btn {
    background: none;
    border: none;
    color: var(--gold-dim);
    cursor: pointer;
    font-size: 1em;
    font-family: var(--font-sans);
    padding: 15px;
    margin-top: var(--gap-lg);
    transition: color var(--transition-fast);
    min-height: 44px;
}

@media (hover: hover) {
    .back-btn:hover {
        color: var(--gold);
    }
}

/* ======== 联网模式 ======== */
.online-options {
    display: flex;
    flex-direction: column;
    gap: var(--gap-lg);
    align-items: center;
}

.divider-text {
    color: var(--brown);
    font-size: 0.9em;
    position: relative;
    width: 100%;
    text-align: center;
}

.divider-text::before,
.divider-text::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 35%;
    height: 1px;
    background: var(--brown);
}

.divider-text::before { left: 0; }
.divider-text::after { right: 0; }

.join-room-form {
    display: flex;
    gap: var(--gap-sm);
    width: 100%;
    align-items: center;
}

.room-input {
    flex: 1;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--brown);
    border-radius: var(--radius-sm);
    color: var(--gold);
    font-size: clamp(1em, 4vw, 1.1em);
    font-family: var(--font-sans);
    letter-spacing: 0.2em;
    text-align: center;
    text-transform: uppercase;
    outline: none;
    transition: border-color var(--transition-fast);
    min-height: 44px;
}

.room-input:focus {
    border-color: #C08050;
}

.room-input::placeholder {
    color: var(--brown);
    letter-spacing: 0.1em;
    text-transform: none;
}

/* ======== 等待房间 ======== */
.waiting-info {
    display: flex;
    flex-direction: column;
    gap: var(--gap-lg);
    align-items: center;
}

.label {
    font-size: 0.85em;
    color: var(--gold-dim);
    margin-bottom: 6px;
}

.room-code-display {
    text-align: center;
}

.room-code {
    font-size: clamp(2em, 8vw, 2.5em);
    font-weight: bold;
    color: var(--gold);
    letter-spacing: 0.3em;
    font-family: var(--font-mono);
}

.link-share {
    width: 100%;
}

.link-row {
    display: flex;
    gap: var(--gap-sm);
}

.link-input {
    font-size: clamp(0.7em, 3vw, 0.8em);
    letter-spacing: 0;
    text-align: left;
    text-transform: none;
    min-width: 0;
}

.waiting-spinner {
    display: flex;
    align-items: center;
    gap: var(--gap-md);
    color: var(--gold-dim);
    margin-top: 10px;
}

.spinner {
    width: 20px;
    height: 20px;
    border: 2px solid var(--brown);
    border-top-color: var(--gold);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ======== 游戏界面 ======== */
.game-layout {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: var(--gap-sm);
    gap: var(--gap-xs);
    overflow: hidden;
}

.game-header {
    width: 100%;
    text-align: center;
    padding: var(--gap-xs) 0;
    flex-shrink: 0;
}

.status-text {
    font-size: clamp(0.9em, 3.5vw, 1.1em);
    color: var(--gold);
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.board-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 1;
    min-height: 0;
    /* 棋盘容器占满可用空间 */
    flex: 1 1 auto;
}

/* 侧边栏布局（横屏/宽屏） */
.game-sidebar {
    display: none;
    flex-direction: column;
    gap: var(--gap-sm);
    flex: 1 1 auto;
    min-width: 140px;
    max-width: 240px;
    justify-content: center;
    padding: var(--gap-xs);
}

/* 底部布局（竖屏） */
.game-bottom {
    display: flex;
    flex-direction: column;
    gap: var(--gap-xs);
    width: 100%;
    flex-shrink: 0;
}

#chessBoard {
    cursor: pointer;
    border-radius: var(--radius-sm);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
    /* 防止触摸拖拽 */
    touch-action: none;
    -webkit-touch-callout: none;
}

.game-controls {
    display: flex;
    gap: var(--gap-sm);
    flex-wrap: wrap;
    justify-content: center;
    padding: var(--gap-xs) 0;
    flex-shrink: 0;
}

.ctrl-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--brown);
    color: var(--gold);
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: clamp(0.8em, 3vw, 0.9em);
    font-family: var(--font-sans);
    transition: all var(--transition-fast);
    min-height: 40px;
    white-space: nowrap;
}

@media (hover: hover) {
    .ctrl-btn:hover {
        background: rgba(255, 255, 255, 0.15);
        border-color: var(--brown-light);
    }
}

.ctrl-btn:active {
    transform: scale(0.95);
}

/* ======== 聊天区域 ======== */
.chat-area {
    width: 100%;
    max-width: 560px;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    /* 聊天区固定高度，不抢占棋盘空间 */
    max-height: 130px;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--gap-sm);
    background: rgba(0, 0, 0, 0.2);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    min-height: 40px;
    max-height: 90px;
    /* iOS 平滑滚动 */
    -webkit-overflow-scrolling: touch;
}

.chat-message {
    padding: 3px var(--gap-sm);
    font-size: clamp(0.75em, 3vw, 0.85em);
    color: var(--text-secondary);
    word-break: break-all;
}

.chat-sender {
    color: #C08050;
    font-weight: bold;
}

.chat-input-row {
    display: flex;
    gap: 6px;
}

.chat-input {
    flex: 1;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--brown);
    border-radius: var(--radius-sm);
    color: var(--gold);
    font-size: clamp(0.8em, 3vw, 0.85em);
    font-family: var(--font-sans);
    outline: none;
    transition: border-color var(--transition-fast);
    min-height: 40px;
}

.chat-input:focus {
    border-color: #C08050;
}

.chat-send-btn {
    padding: 8px 16px;
    background: var(--brown);
    border: none;
    border-radius: var(--radius-sm);
    color: var(--gold);
    cursor: pointer;
    font-size: clamp(0.8em, 3vw, 0.85em);
    font-family: var(--font-sans);
    transition: background var(--transition-fast);
    min-height: 40px;
    white-space: nowrap;
}

@media (hover: hover) {
    .chat-send-btn:hover {
        background: var(--brown-light);
    }
}

/* ======== 遮罩层 ======== */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--overlay-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeIn var(--transition-normal);
    padding: var(--gap-lg);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.overlay-content {
    text-align: center;
    padding: clamp(24px, 5vw, 40px);
    background: linear-gradient(135deg, var(--brown-dark), #4D3225);
    border-radius: var(--radius-xl);
    border: 2px solid var(--brown);
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    animation: scaleIn var(--transition-normal);
    min-width: 260px;
    max-width: 360px;
    width: 100%;
}

@keyframes scaleIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.winner-text {
    font-size: clamp(1.6em, 7vw, 2.2em);
    font-weight: bold;
    margin-bottom: var(--gap-sm);
    font-family: var(--font-serif);
}

.reason-text {
    font-size: clamp(0.85em, 3.5vw, 1em);
    color: var(--gold-dim);
    margin-bottom: clamp(16px, 3vh, 25px);
}

.overlay-buttons {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: var(--gap-sm);
}

.dialog {
    min-width: 240px;
}

.dialog-text {
    font-size: clamp(1.1em, 4.5vw, 1.3em);
    color: var(--gold);
    margin-bottom: var(--gap-lg);
}

/* ======== Toast ======== */
.toast {
    position: fixed;
    top: calc(var(--safe-top) + 16px);
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    background: rgba(60, 35, 20, 0.95);
    color: var(--gold);
    padding: 12px 24px;
    border-radius: var(--radius-md);
    border: 1px solid var(--brown);
    font-size: clamp(0.85em, 3.5vw, 0.95em);
    z-index: 200;
    transition: transform var(--transition-normal);
    pointer-events: none;
    white-space: nowrap;
    max-width: 90vw;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
}

/* ======== 响应式：超小屏（< 360px） ======== */
@media (max-width: 359px) {
    .game-title {
        letter-spacing: 0.1em;
    }
    .menu-btn {
        padding: 12px 14px;
        gap: 6px;
    }
    .btn-desc {
        display: none;
    }
    .ctrl-btn {
        padding: 6px 8px;
        font-size: 0.75em;
    }
    .overlay-content {
        padding: 20px;
    }
}

/* ======== 响应式：小屏手机（360 - 414px） ======== */
@media (min-width: 360px) and (max-width: 414px) {
    .menu-container {
        padding: var(--gap-lg);
    }
}

/* ======== 响应式：矮屏幕（< 640px 高度） ======== */
@media (max-height: 639px) {
    .game-title {
        font-size: clamp(1.6em, 6vh, 2em);
    }
    .menu-subtitle {
        margin-bottom: 16px;
    }
    .game-header {
        padding: 2px 0;
    }
    .status-text {
        font-size: clamp(0.8em, 2.5vh, 0.95em);
    }
    .ctrl-btn {
        padding: 5px 10px;
        font-size: 0.8em;
        min-height: 34px;
    }
    .game-controls {
        gap: 4px;
    }
    .chat-area {
        max-height: 80px;
    }
    .chat-messages {
        max-height: 50px;
    }
}

/* ======== 响应式：横屏手机 ======== */
@media (max-height: 500px) and (orientation: landscape) {
    .game-layout {
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: stretch;
        padding: var(--gap-xs);
    }

    .game-header {
        width: auto;
        padding: 0 var(--gap-sm);
        writing-mode: vertical-lr;
        text-orientation: mixed;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .status-text {
        font-size: 0.85em;
    }

    .board-container {
        flex: 0 0 auto;
    }

    /* 侧边栏：控制+聊天 */
    .game-sidebar {
        display: flex;
        flex-direction: column;
        gap: var(--gap-xs);
        flex: 1 1 auto;
        min-width: 160px;
        max-width: 280px;
        justify-content: center;
    }

    .game-controls {
        flex-direction: column;
        flex-wrap: nowrap;
        align-items: stretch;
    }

    .ctrl-btn {
        width: 100%;
    }

    .chat-area {
        max-width: none;
        max-height: none;
        flex: 1 1 auto;
    }

    .chat-messages {
        max-height: none;
        flex: 1 1 auto;
    }

    /* 菜单页面横屏优化 */
    .menu-container {
        padding: var(--gap-md);
        max-width: 360px;
    }

    .game-title {
        font-size: 1.8em;
        margin-bottom: 2px;
    }

    .menu-subtitle {
        margin-bottom: 16px;
    }

    .menu-buttons {
        flex-direction: row;
        gap: var(--gap-sm);
    }

    .menu-btn {
        flex: 1;
    }

    .btn-desc {
        display: none;
    }

    .online-options {
        gap: var(--gap-sm);
    }

    .waiting-info {
        gap: var(--gap-sm);
    }

    .room-code {
        font-size: 1.6em;
    }
}

/* ======== 响应式：平板竖屏（768px+） ======== */
@media (min-width: 768px) {
    .menu-container {
        max-width: 500px;
        padding: 40px;
    }

    .menu-btn {
        padding: 18px 28px;
        border-radius: var(--radius-xl);
    }

    .game-header {
        padding: var(--gap-sm) 0;
    }

    .ctrl-btn {
        padding: 10px 20px;
        font-size: 1em;
    }

    .chat-area {
        max-height: 160px;
    }

    .chat-messages {
        max-height: 110px;
    }
}

/* ======== 响应式：平板横屏/小桌面（1024px+） ======== */
@media (min-width: 1024px) {
    .menu-container {
        max-width: 520px;
    }

    /* PC 游戏界面可以并排 */
    .game-layout.pc-wide {
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: center;
        gap: var(--gap-lg);
        padding: var(--gap-lg);
    }

    .game-layout.pc-wide .game-header {
        writing-mode: horizontal-tb;
        width: auto;
        padding: 0;
    }

    .game-layout.pc-wide .board-container {
        flex: 0 0 auto;
    }

    .game-layout.pc-wide .game-sidebar {
        display: flex;
        flex-direction: column;
        gap: var(--gap-md);
        min-width: 200px;
        max-width: 260px;
    }

    .game-layout.pc-wide .game-controls {
        flex-direction: column;
        flex-wrap: nowrap;
        align-items: stretch;
    }

    .game-layout.pc-wide .ctrl-btn {
        width: 100%;
    }

    .game-layout.pc-wide .chat-area {
        max-width: none;
        max-height: 200px;
        flex: 1 1 auto;
    }

    .game-layout.pc-wide .chat-messages {
        max-height: 140px;
    }
}

/* ======== 响应式：大桌面（1440px+） ======== */
@media (min-width: 1440px) {
    .menu-container {
        max-width: 560px;
        padding: 50px;
    }

    .game-title {
        font-size: 3em;
    }
}

/* ======== 打印 / 无障碍 ======== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ======== 隐藏辅助类 ======== */
.hidden {
    display: none !important;
}

/* 联网模式专属控件 */
.online-only {
    /* 由 JS 控制 display */
}
