/* ============================================================
   中国象棋 - 样式表（响应式重构版）
   ============================================================ */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --board-bg: #f0d9b5;
  --board-line: #5c3317;
  --red: #c0392b;
  --black: #2c3e50;
  --wood: #d4a76a;
  --accent: #e74c3c;
  --green: #27ae60;
  --blue: #2980b9;
  --panel-bg: rgba(255,255,255,0.97);
}

html, body { height: 100%; }
body {
  font-family: 'Microsoft YaHei', 'PingFang SC', 'Helvetica Neue', sans-serif;
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 50%, #1a2530 100%);
  min-height: 100vh;
  color: #333;
  overflow-x: hidden;
}

/* ========== APP 布局：grid 响应式 ========== */
.app {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto 1fr;
  grid-template-areas:
    "header"
    "modebar"
    "main";
  min-height: 100vh;
  padding: 8px;
  gap: 8px;
  max-width: 1200px;
  margin: 0 auto;
}

.app-header {
  grid-area: header;
  text-align: center;
  color: #f1c40f;
  padding: 4px 0;
}
.app-header h1 {
  font-size: clamp(20px, 4.5vw, 32px);
  text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
  letter-spacing: clamp(4px, 1vw, 10px);
}
.app-header p {
  font-size: clamp(11px, 2vw, 13px);
  color: #bdc3c7;
  margin-top: 2px;
}

/* ---------- 模式切换栏 ---------- */
.mode-bar {
  grid-area: modebar;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}
.mode-btn {
  padding: clamp(6px, 1.2vw, 9px) clamp(10px, 2vw, 18px);
  border: none;
  border-radius: 6px;
  background: rgba(255,255,255,0.15);
  color: #ecf0f1;
  cursor: pointer;
  font-size: clamp(12px, 2.3vw, 14px);
  font-weight: bold;
  transition: all 0.2s;
  white-space: nowrap;
}
.mode-btn:hover  { background: rgba(255,255,255,0.3); }
.mode-btn.active { background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(231,76,60,0.4); }

/* ---------- 主区域：桌面端两列，移动端上下排列 ---------- */
.main-area {
  grid-area: main;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 12px;
  align-items: start;
}

/* ---------- 棋盘区 ---------- */
.board-area {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
}
.board-wrapper {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}
#chessCanvas {
  background: var(--board-bg);
  border: clamp(4px, 0.8vw, 10px) solid #8b5a2b;
  border-radius: 4px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5), inset 0 0 20px rgba(139,90,43,0.3);
  cursor: pointer;
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 220px);
  height: auto;
  touch-action: none;      /* 防止手机触屏滑动 */
}
.status-bar {
  width: 100%;
  text-align: center;
  padding: clamp(6px,1.2vw,10px);
  background: var(--panel-bg);
  border-radius: 6px;
  font-weight: bold;
  font-size: clamp(13px, 2.5vw, 16px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.status-red   { color: var(--red); }
.status-black { color: var(--black); }
.check-warn { color: var(--accent); animation: pulse 0.8s infinite; }
@keyframes pulse {
  0%,100% { transform: scale(1); }
  50%     { transform: scale(1.08); }
}

/* ---------- 侧边面板（桌面端：右侧；移动端：棋盘下方，横向滚动tab切换）---------- */
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}
.panel-hidden { display: none !important; }

/* 移动端：面板改为 tab 切换避免过长 */
.panel-tabs-m {
  display: none;
  gap: 4px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.panel-tab-m {
  padding: 6px 12px;
  border: none;
  border-radius: 16px;
  background: rgba(255,255,255,0.15);
  color: #ecf0f1;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
  font-weight: bold;
}
.panel-tab-m.active { background: var(--accent); color: #fff; }

.panel-card {
  background: var(--panel-bg);
  border-radius: 8px;
  padding: clamp(8px, 1.5vw, 12px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.panel-card h3 {
  font-size: clamp(13px, 2.3vw, 15px);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--accent);
  color: var(--accent);
}

/* 控制面板 */
.control-row { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.btn {
  padding: clamp(6px,1vw,8px) clamp(8px,1.5vw,12px);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: clamp(12px, 2.2vw, 13px);
  font-weight: bold;
  transition: all 0.2s;
  flex: 1;
  min-width: 54px;
}
.btn-red    { background: var(--red); color: #fff; }
.btn-blue   { background: var(--blue); color: #fff; }
.btn-green  { background: var(--green); color: #fff; }
.btn-gray   { background: #95a5a6; color: #fff; }
.btn-orange { background: #e67e22; color: #fff; }
.btn:hover  { opacity: 0.85; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* 难度选择 */
.difficulty-row { display: flex; gap: 4px; margin-top: 4px; }
.diff-btn {
  flex: 1;
  padding: clamp(4px,1vw,6px);
  border: 1px solid #ccc;
  background: #fff;
  border-radius: 4px;
  font-size: clamp(11px, 2vw, 12px);
  cursor: pointer;
}
.diff-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- 联机面板 ---------- */
.net-panel .net-info { font-size: 13px; margin-bottom: 8px; line-height: 1.6; }
.net-panel .room-id {
  font-family: 'Courier New', monospace;
  background: #f0f0f0;
  padding: 8px 10px;
  border-radius: 4px;
  font-size: clamp(15px, 3vw, 18px);
  font-weight: bold;
  text-align: center;
  margin: 6px 0;
  letter-spacing: 4px;
  color: var(--blue);
}
.net-panel input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: clamp(13px,2.5vw,14px);
  margin-bottom: 6px;
}
.net-status {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 6px;
}
.net-status.idle     { background: #ecf0f1; color: #7f8c8d; }
.net-status.connecting { background: #fef3cd; color: #856404; }
.net-status.host     { background: #d4edda; color: #155724; }
.net-status.joined   { background: #d1ecf1; color: #0c5460; }
.net-status.error    { background: #f8d7da; color: #721c24; }
.net-status.ble{background:#2196F3;color:#fff;}
.net-status.wifi{background:#4CAF50;color:#fff;}
.net-status.qr{background:#9C27B0;color:#fff;}

.channel-tabs{display:flex;gap:4px;margin-bottom:10px;}
.channel-tab{flex:1;padding:6px 2px;font-size:clamp(11px,2vw,12px);border:1px solid #cbb38a;background:#f5ead0;color:#8b6d3c;border-radius:6px;cursor:pointer;text-align:center;transition:.2s;}
.channel-tab.active{background:#b8860b;color:#fff;border-color:#8b6508;font-weight:bold;}
.channel-pane{display:none;}
.channel-pane.active{display:block;}
.ble-tip{font-size:11px;color:#b8860b;background:#fff8e1;padding:8px;border-radius:4px;line-height:1.6;margin-top:8px;}
.qr-box{display:flex;justify-content:center;margin:10px 0;}
.qr-box canvas,.qr-box img{border:4px solid #fff;box-shadow:0 2px 8px rgba(0,0,0,.2);border-radius:4px;max-width:180px;}

/* ---------- 残局选择 ---------- */
.endgame-search {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: clamp(12px,2.3vw,13px);
  margin-bottom: 6px;
}
.endgame-list {
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  font-size: clamp(12px,2.2vw,13px);
}
.endgame-item {
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  transition: background 0.15s;
}
.endgame-item:hover   { background: #fff3e0; }
.endgame-item.active  { background: var(--accent); color: #fff; }
.endgame-item .num    { color: #999; margin-right: 4px; }
.endgame-item.active .num { color: rgba(255,255,255,0.7); }

/* ---------- 聊天 ---------- */
.chat-panel { display: flex; flex-direction: column; }
.chat-messages {
  height: clamp(130px, 25vh, 220px);
  overflow-y: auto;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 6px;
  margin-bottom: 6px;
  background: #fafafa;
  font-size: clamp(12px,2.2vw,13px);
}
.chat-msg {
  margin-bottom: 5px;
  padding: 5px 8px;
  border-radius: 6px;
  word-wrap: break-word;
  line-height: 1.4;
}
.chat-msg.me      { background: #d4edda; margin-left: 16px; }
.chat-msg.opponent{ background: #d1ecf1; margin-right: 16px; }
.chat-msg.system  { background: #fff3cd; color: #856404; text-align: center; font-style: italic; font-size: 11px; }
.chat-msg .name   { font-weight: bold; margin-right: 4px; }
.chat-input-row   { display: flex; gap: 4px; }
.chat-input-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: clamp(13px,2.3vw,14px);
}

/* ---------- 棋谱记录 ---------- */
.move-log {
  max-height: clamp(120px,20vh,180px);
  min-height: 80px;
  overflow-y: auto;
  font-family: 'Courier New', monospace;
  font-size: clamp(12px,2.2vw,13px);
  background: #fafafa;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 6px;
}
.move-log p { margin: 2px 0; }
.move-log .red   { color: var(--red); }
.move-log .black { color: var(--black); }

/* ---------- 游戏结束弹窗 ---------- */
.modal-mask {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}
.modal-mask.show { display: flex; }
.modal {
  background: #fff;
  padding: clamp(20px,5vw,30px) clamp(24px,6vw,40px);
  border-radius: 12px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: modalIn 0.3s;
  max-width: 90vw;
}
@keyframes modalIn {
  from { transform: scale(0.8); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}
.modal h2 { font-size: clamp(22px,5vw,28px); margin-bottom: 12px; color: var(--accent); }
.modal p  { color: #666; margin-bottom: 20px; font-size: clamp(14px,2.5vw,16px); }

/* ---------- 提示气泡 ---------- */
.toast {
  position: fixed;
  top: 20px; left: 50%;
  transform: translateX(-50%) translateY(-100px);
  background: rgba(0,0,0,0.85);
  color: #fff;
  padding: 10px 24px;
  border-radius: 6px;
  font-size: 14px;
  z-index: 2000;
  transition: transform 0.3s;
  max-width: 90vw;
  text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ========== 平板/小桌面断点 ========== */
@media (max-width: 960px) {
  .main-area {
    grid-template-columns: 1fr;
  }
  .side-panel {
    max-width: 640px;
    margin: 0 auto;
    width: 100%;
  }
  #chessCanvas {
    max-height: calc(100vh - 280px);
  }
}

/* ========== 手机断点：启用面板Tab切换 ========== */
@media (max-width: 640px) {
  .app {
    padding: 6px;
    gap: 6px;
  }
  #chessCanvas {
    border-width: 5px;
    max-height: calc(100vh - 240px);
  }
  /* 移动端侧栏改为单卡片 + tab切换 */
  .side-panel {
    position: relative;
  }
  .panel-tabs-m {
    display: flex;
    justify-content: center;
  }
  .side-panel .panel-card {
    display: none;
  }
  .side-panel .panel-card.active-m {
    display: block;
  }
  .mode-btn {
    padding: 7px 10px;
    font-size: 12px;
  }
}

/* ========== 超小屏（<400px）进一步压缩 ========== */
@media (max-width: 400px) {
  .app { padding: 4px; gap: 4px; }
  #chessCanvas { border-width: 4px; }
  .app-header h1 { letter-spacing: 2px; }
}

/* ========== 横屏手机：棋盘靠左，右侧紧凑面板 ========== */
@media (max-height: 500px) and (orientation: landscape) {
  .app {
    grid-template-columns: 1fr 240px;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      "header header"
      "modebar modebar"
      "main main";
    padding: 4px;
    gap: 6px;
  }
  .main-area {
    grid-template-columns: minmax(0,1fr) 220px;
    gap: 8px;
    height: calc(100vh - 90px);
    overflow: hidden;
  }
  .board-area {
    height: 100%;
    justify-content: center;
  }
  #chessCanvas {
    max-height: calc(100vh - 110px);
    max-width: 100%;
  }
  .side-panel {
    overflow-y: auto;
    max-height: calc(100vh - 110px);
  }
  .panel-tabs-m { display: none; }
  .side-panel .panel-card { display: block; padding: 8px; }
  .chat-messages { height: 100px; }
  .move-log { max-height: 80px; }
  .endgame-list { max-height: 120px; }
}
