/* ===== 基础与变量 ===== */
:root {
  --cream: #f5f0e1;
  --board-light: #f0d9b5;
  --board-dark: #b58863;
  --wood: #8b4513;
  --wood-dark: #6f3415;
  --wood-light: #a0633a;
  --text: #3a2c1a;
  --text-soft: #6b5a44;
  --panel-bg: #fffdf7;
  --border: #e3d6bd;
  --header-h: 50px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: var(--cream);
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3, h4 { margin: 0 0 .4em; line-height: 1.3; }
a { color: var(--wood); }

/* ===== 通栏顶部导航 ===== */
.site-header {
  background: linear-gradient(180deg, var(--wood-light), var(--wood));
  color: #fff7ea;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  padding-top: var(--safe-top);
  flex-shrink: 0;
  z-index: 100;
}
.header-inner {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand-link {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: #ffffff;
}
.logo-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  line-height: 1;
}
.logo-icon img {
  display: block;
  border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}
.site-name { font-size: 16px; font-weight: 700; letter-spacing: .02em; color: #ffffff; }
.header-nav { display: flex; gap: 6px; }
.header-nav-btn {
  background: rgba(255,255,255,0.22);
  color: #ffffff;
  border: 1px solid rgba(255,255,255,0.6);
  border-radius: 6px;
  padding: 5px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s;
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
}
.header-nav-btn:hover { background: rgba(255,255,255,0.38); }

/* ===== 游戏核心区 ===== */
.game-screen {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

/* ===== 棋盘+侧边栏布局 ===== */
.game-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: row;
  min-height: 0;
  overflow: hidden;
  gap: 16px;
  padding: 12px 18px;
  justify-content: center;
  align-items: stretch;
}

/* ===== 棋盘区 ===== */
.board-area {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

/* 棋盘内层列容器：宽度由 canvas 的 aspect-ratio 自动决定，玩家条随之对齐 */
.board-col {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

/* 玩家标识条 */
.player-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  margin: 3px 0;
}
.player-avatar { font-size: 20px; line-height: 1; }
.player-label strong { color: var(--wood-dark); }
.player-role { color: var(--text-soft); margin-left: 2px; }
.player-badge {
  margin-left: auto;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
}
.ai-badge { background: var(--wood); color: #fff7ea; }
.you-badge { background: var(--board-light); color: var(--wood-dark); border: 1px solid #c8b89a; }
.player-opponent { order: -1; }
.player-self { order: 1; }

.canvas-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  aspect-ratio: 1 / 1;
  max-width: 820px;
  max-height: 820px;
}
#gameCanvas {
  display: block;
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(80, 50, 20, .28);
  touch-action: manipulation;
}
.ai-thinking {
  position: absolute;
  top: 10px; left: 50%;
  transform: translateX(-50%);
  background: rgba(58, 44, 26, .88);
  color: #fff;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 14px;
  pointer-events: none;
  white-space: nowrap;
}
.ai-thinking[hidden] { display: none; }

.board-status {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  padding: 6px 12px;
  font-size: 15px;
  flex: 0 0 auto;
}
.turn-indicator { font-weight: 700; color: var(--wood-dark); }
.game-status { color: #b03a2e; font-weight: 600; }

/* ===== 侧边栏（控制面板） ===== */
.control-panel {
  flex: 0 0 300px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-y: auto;
  align-self: stretch;
}
.control-group { display: flex; flex-direction: column; gap: 6px; }
.control-group label { font-size: 14px; color: var(--text-soft); }

select {
  font-size: 15px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
}
.btn-group { display: flex; flex-wrap: wrap; gap: 8px; }
.btn {
  font-size: 14px;
  padding: 8px 12px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  color: #fff7ea;
  background: var(--wood);
  transition: background .15s, transform .05s;
  flex: 1 1 auto;
  min-width: 76px;
}
.btn:hover { background: var(--wood-dark); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: #a85a1e; }
.btn-primary:hover { background: #8c4716; }
.btn-secondary { background: #7a6650; }
.btn-secondary:hover { background: #5f4f3d; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.sound-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 8px 0;
  border-top: 1px dashed var(--border);
  border-bottom: 1px dashed var(--border);
}
.switch-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text-soft);
  cursor: pointer;
}
.switch-label input { width: 15px; height: 15px; accent-color: var(--wood); }
.panel-hint { font-size: 13px; color: var(--text-soft); margin: 0; }

/* ===== 广告位（game-screen底部，首屏可见） ===== */
.ad-slot {
  display: none;
}
.ad-slot-bottom {
  display: none;
  flex-shrink: 0;
  min-height: 60px;
  border-top: 1px solid var(--border);
  background: #fafafa;
  color: #aaa;
  font-size: 13px;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.ad-slot-bottom:not(:empty) { display: flex; }

/* ===== 侧边栏分区（棋谱 / 分析） ===== */
.sidebar-section {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}
.sidebar-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
}
.sidebar-section-head h3 {
  font-size: 14px;
  color: var(--wood-dark);
  margin: 0;
}

/* 回放控制栏 */
.replay-bar {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}
.replay-bar .btn { flex: 0 0 auto; min-width: 0; font-size: 12px; padding: 5px 8px; }
.replay-btn {
  font-size: 16px;
  width: 32px;
  height: 30px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  cursor: pointer;
  color: var(--wood-dark);
}
.replay-btn:disabled { opacity: .4; cursor: not-allowed; }
.replay-progress { font-size: 12px; color: var(--text-soft); }

/* 走法列表 — 双列流水线风格 */
.move-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 190px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(139, 69, 19, .2) transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}
.move-list::-webkit-scrollbar { width: 4px; }
.move-list::-webkit-scrollbar-thumb { background: rgba(139, 69, 19, .2); border-radius: 99px; }
.move-list:empty { display: none; }

/* 每一行 = 一个回合（白 + 黑） */
.move-list li {
  display: grid;
  grid-template-columns: 28px 1fr 1fr;
  align-items: stretch;
  border-bottom: 1px solid #f0e8d8;
}
.move-list li:last-child { border-bottom: none; }

/* 回合序号 */
.move-list li .move-no {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #b8a898;
  background: #faf6ef;
  border-right: 1px solid #f0e8d8;
  font-variant-numeric: tabular-nums;
  padding: 0 4px;
}

/* 白 / 黑 走法格子 */
.move-list li button {
  width: 100%;
  min-height: 28px;
  padding: 4px 6px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-size: 13px;
  font-family: "Consolas", "Courier New", monospace;
  cursor: pointer;
  transition: background .1s, color .1s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: .02em;
}
/* 白方（第2列）：浅暖白背景 + "白"标签 */
.move-list li button:nth-child(2) {
  background: #fdfaf4;
}
.move-list li button:nth-child(2)::before {
  content: "白";
  display: inline-block;
  font-size: 11px;
  font-family: inherit;
  margin-right: 4px;
  padding: 0 3px;
  border-radius: 3px;
  background: #e8dcc8;
  color: #7a5c30;
  font-weight: 700;
  vertical-align: 1px;
  letter-spacing: 0;
}
/* 黑方（第3列）：略深背景 + "黑"标签 */
.move-list li button:nth-child(3) {
  border-left: 1px solid #f0e8d8;
  background: #f2e8d8;
}
.move-list li button:nth-child(3)::before {
  content: "黑";
  display: inline-block;
  font-size: 11px;
  font-family: inherit;
  margin-right: 4px;
  padding: 0 3px;
  border-radius: 3px;
  background: #3a2c1a;
  color: #f5ede0;
  font-weight: 700;
  vertical-align: 1px;
  letter-spacing: 0;
}
.move-list li .move-empty { display: block; border-left: 1px solid #f0e8d8; }
.move-list li button:hover { background: #eedfc9; color: var(--wood-dark); }
.move-list li button.active {
  background: var(--board-light);
  color: var(--wood-dark);
  font-weight: 700;
}

.empty-hint { color: var(--text-soft); font-size: 13px; text-align: center; padding: 6px; margin: 0; }
.records-tip { font-size: 12px; color: var(--text-soft); margin: 0; }
.history-list { padding-left: 16px; margin: 0; }
.history-list li { font-size: 12px; color: var(--text-soft); margin-bottom: 4px; }

/* 文章区历史对局列表 */
.article-history-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.article-history-item {
  display: grid;
  grid-template-columns: 28px auto 1fr auto;
  grid-template-rows: auto auto;
  align-items: center;
  column-gap: 10px;
  row-gap: 6px;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border);
  font-size: 14px;
  transition: box-shadow .15s, border-color .15s;
}
.article-history-item:hover {
  box-shadow: 0 3px 10px rgba(139, 69, 19, .1);
}
.article-history-item.hist-win  { border-left-color: #4caf50; }
.article-history-item.hist-lose { border-left-color: #f44336; }
.article-history-item.hist-draw { border-left-color: #ff9800; }

.hist-index {
  grid-column: 1; grid-row: 1;
  font-size: 11px;
  color: #b8a898;
  font-variant-numeric: tabular-nums;
  text-align: center;
}
.hist-result {
  grid-column: 2; grid-row: 1;
  font-weight: 700;
  padding: 3px 12px;
  border-radius: 20px;
  font-size: 13px;
  white-space: nowrap;
}
.hist-win  .hist-result { color: #2e7d32; background: #e8f5e9; }
.hist-lose .hist-result { color: #c62828; background: #ffebee; }
.hist-draw .hist-result { color: #e65100; background: #fff3e0; }

.hist-date {
  grid-column: 3; grid-row: 1;
  color: var(--text-soft);
  font-size: 12px;
}
.hist-moves {
  grid-column: 4; grid-row: 1;
  color: var(--text-soft);
  font-size: 12px;
  white-space: nowrap;
  background: rgba(139,69,19,.07);
  padding: 2px 8px;
  border-radius: 10px;
}
.hist-pgn {
  grid-column: 1 / -1; grid-row: 2;
  font-size: 11.5px;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-family: 'Consolas', 'Courier New', monospace;
  background: rgba(0,0,0,.03);
  padding: 4px 8px;
  border-radius: 5px;
  letter-spacing: .03em;
  opacity: .85;
}

/* 分析报告（侧边栏） */
.analysis-report { display: flex; flex-direction: column; gap: 8px; }
.analysis-report[hidden] { display: none; }
.a-section {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--wood);
  border-radius: 6px;
  padding: 8px 10px;
}
.a-section[hidden] { display: none; }
.a-section h3 { font-size: 13px; color: var(--wood-dark); margin-bottom: 2px; }
.a-section p { margin: 0; font-size: 13px; line-height: 1.6; }

/* 文章区赛后分析（覆盖侧边栏小尺寸） */
.article-content .analysis-report {
  gap: 12px;
}
.article-content .a-section {
  border-left-width: 4px;
  border-radius: 10px;
  padding: 14px 18px;
  position: relative;
}
.article-content .a-section:nth-child(1) { border-left-color: #5b9bd5; }
.article-content .a-section:nth-child(2) { border-left-color: #e67e22; }
.article-content .a-section:nth-child(3) { border-left-color: #27ae60; }
.article-content .a-section h3 {
  font-size: 15px;
  color: var(--wood-dark);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.article-content .a-section h3::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  opacity: .6;
  flex-shrink: 0;
}
.article-content .a-section:nth-child(1) h3 { color: #2c5f8a; }
.article-content .a-section:nth-child(2) h3 { color: #a04000; }
.article-content .a-section:nth-child(3) h3 { color: #1e7e45; }
.article-content .a-section p {
  font-size: 14px;
  line-height: 1.75;
  color: var(--text);
  margin: 0;
}

/* ===== 内容区（教程 / FAQ） ===== */
.article-content { background: var(--cream); }
.tab-panel { max-width: 860px; margin: 0 auto; padding: 20px 18px 10px; }
.content-block { margin-bottom: 22px; }
.content-block h2 { font-size: 21px; color: var(--wood-dark); }
.content-block h3 { font-size: 18px; color: var(--wood-dark); margin-top: 4px; }

/* 文章区记录/分析区块标题行 */
.article-section-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 10px;
}
.article-section-head h3 { margin: 0; }
.article-section-hint {
  font-size: 12px;
  color: var(--text-soft);
  background: rgba(139,69,19,.07);
  padding: 2px 8px;
  border-radius: 10px;
}
.content-block p { margin: 0 0 10px; font-size: 15px; }
.step-list, .text-list { padding-left: 22px; margin: 0; }
.step-list li, .text-list li { margin-bottom: 8px; font-size: 15px; }
.piece-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.piece-card {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.piece-card h4 { font-size: 16px; color: var(--wood); margin-bottom: 4px; }
.piece-card p { font-size: 14px; margin: 0; color: var(--text-soft); }
.faq-list dt { font-weight: 700; margin-top: 12px; color: var(--wood-dark); }
.faq-list dd { margin: 4px 0 0; color: var(--text-soft); font-size: 15px; }

/* ===== 页脚 ===== */
.site-footer {
  text-align: center;
  padding: 22px 16px;
  background: var(--wood-dark);
  color: #f0e2cc;
  font-size: 14px;
}
.site-footer a { color: inherit; text-decoration: none; }
.footer-main { margin: 0 0 6px; }
.footer-links { margin: 0; }

/* ===== 移动端底部 TabBar ===== */
.tabbar {
  display: none;
  flex-shrink: 0;
  background: #fff;
  border-top: 1px solid var(--border);
  /* 安全区域在按钮内部用 padding 撑开，不再限死总高度 */
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tab-btn {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: 50px;
  border: none;
  background: none;
  font-size: 11px;
  color: var(--text-soft);
  cursor: pointer;
  font-family: inherit;
  padding: 6px 0 0;
  position: relative;
  transition: color 0.18s;
}
.tab-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  transition: stroke 0.18s;
}
.tab-btn.active {
  color: var(--wood);
  font-weight: 700;
}
.tab-btn.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 20%;
  right: 20%;
  height: 2px;
  background: var(--wood);
  border-radius: 0 0 2px 2px;
}

/* ===== PC端（≥768px）：第一屏游戏区 + 下方文案可滚动 ===== */
@media (min-width: 768px) {
  /*
   * PC 端：body 正常滚动，game-screen 精确锁定 100dvh，
   * header sticky 浮在顶部，game-screen 紧随其后占满视口剩余高度。
   * article 在 game-screen 之后，随页面自然滚动。
   */
  html, body { height: auto; min-height: 100dvh; overflow: visible; }

  body {
    display: flex;
    flex-direction: column;
  }

  .site-header {
    flex: 0 0 var(--header-h);
    position: sticky;
    top: 0;
    z-index: 100;
  }

  /* game-screen = 视口高度减去顶部导航，内部 flex 分配：棋盘区 + 广告 */
  .game-screen {
    flex: none;
    height: calc(100dvh - var(--header-h));
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  /* game-main 填满 game-screen 剩余空间 */
  .game-main {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* 广告位在 PC 端首屏底部可见 */
  .ad-slot-bottom { flex-shrink: 0; }

  .article-content { flex: 0 0 auto; }
  .tab-panel { display: block; }
  .tabbar { display: none; }
}

/* ===== PC端大屏（≥900px）：进一步优化布局 ===== */
@media (min-width: 900px) {
  .control-panel { flex: 0 0 280px; }

  .article-content {
    padding: 0 20px 20px;
  }
  .tab-panel {
    max-width: 1060px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--panel-bg);
    padding: 24px 28px;
    margin: 20px auto;
  }
  .site-footer { margin-top: 0; }
}

/* ===== 移动端（<768px）：站头常驻 + 内容/游戏视图切换 ===== */
@media (max-width: 767px) {
  body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
  }

  .site-header {
    flex: 0 0 auto;
    height: auto;
    min-height: 44px;
    padding: 8px 12px;
    padding-top: calc(8px + var(--safe-top));
  }
  .site-name { font-size: 15px; }
  .logo-icon img { width: 22px; height: 22px; }

  /* 游戏视图：game-screen 可见，article-content 隐藏 */
  .game-screen {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
  }
  .game-main {
    flex-direction: column;
    align-items: center;
    padding: 6px 8px;
    gap: 6px;
    overflow: visible;
    min-height: auto;
    flex: 0 0 auto;
  }
  .header-nav { display: none; }

  .board-area {
    width: 100%;
    max-width: 460px;
    flex: 0 0 auto;
  }
  .board-col {
    width: 100%;
    height: auto;
  }
  .canvas-wrapper {
    flex: 0 0 auto;
    width: 100%;
    aspect-ratio: 1;
    max-width: none;
    max-height: none;
  }
  .player-bar { padding: 4px 8px; font-size: 13px; }
  .player-avatar { font-size: 16px; }
  .player-badge { font-size: 11px; padding: 1px 6px; }

  .control-panel {
    flex: 0 0 auto;
    width: 100%;
    max-width: 460px;
    border-radius: 10px;
    padding: 10px 12px;
    gap: 8px;
    overflow: visible;
    align-self: center;
  }
  /* 移动端精简面板 */
  .panel-hint { display: none; }
  .sound-group { display: none; }
  .btn { padding: 8px 10px; min-width: 70px; font-size: 13px; }

  /* 内容区 */
  .article-content {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: none;
  }

  /* 默认游戏视图 */
  body.view-game .game-screen { display: flex; }
  body.view-game .article-content { display: none; }

  /* 内容视图 */
  body.view-content .game-screen { display: none; }
  body.view-content .article-content { display: block; }

  /* 移动端 TabBar 显示 */
  .tabbar { display: flex; }

  /* 内容区面板默认隐藏，active显示 */
  .tab-panel { display: none; }
  .tab-panel.active { display: block; }

  /* 侧边栏分区在移动端全部展开 */
  .sidebar-section { border-top: 1px solid var(--border); }
  .move-list { max-height: 140px; }
}
