:root {
  --bg: #faf8ef;
  --board-bg: #bbada0;
  --cell-bg: #cdc1b4;
  --text-dark: #776e65;
  --text-light: #f9f6f2;
  --accent: #edc22e;
  --accent-dark: #8f7a66;
  --card-bg: #ffffff;
  --shadow: 0 2px 8px rgba(119, 110, 101, 0.15);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text-dark);
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100vh; }

.app-container {
  max-width: 1140px;
  margin: 0 auto;
  padding: 16px 16px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 顶部标题 */
.app-header {
  position: relative;
  text-align: center;
}
.site-title {
  font-size: 28px;
  font-weight: 800;
  color: var(--text-dark);
  letter-spacing: -0.5px;
}
.site-subtitle {
  font-size: 15px;
  color: var(--accent-dark);
  margin-top: 4px;
}
.sound-toggle {
  position: absolute;
  top: 50%;
  right: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transform: translateY(-50%);
  border: 1px solid #e6d8bf;
  border-radius: 999px;
  padding: 8px 12px;
  background: #fff8e8;
  color: var(--accent-dark);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  transition: background 0.15s ease, color 0.15s ease, transform 0.08s ease;
}
.sound-toggle:hover { background: #fff1ca; }
.sound-toggle:active { transform: translateY(-50%) scale(0.96); }
.sound-toggle[aria-pressed="true"] {
  background: var(--accent);
  border-color: #d7ae21;
  color: var(--text-dark);
}
.sound-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(143, 122, 102, 0.14);
}

/* 主体布局 */
.app-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 28px;
  align-items: flex-start;
}

/* 棋盘区 */
.board-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.score-bar {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 460px;
  justify-content: center;
}
.score-box {
  background: var(--accent-dark);
  color: var(--text-light);
  border-radius: 8px;
  padding: 8px 18px;
  text-align: center;
  min-width: 100px;
  flex: 1;
}
.score-label {
  display: block;
  font-size: 14px;
  text-transform: uppercase;
  opacity: 0.85;
}
.score-value {
  display: block;
  font-size: 24px;
  font-weight: 800;
  line-height: 1.2;
}

/* Canvas 棋盘 */
.canvas-wrap {
  position: relative;
  width: 100%;
  max-width: 460px;
  aspect-ratio: 1 / 1;
  margin: 0 auto;
}
#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--board-bg);
  border-radius: 12px;
  touch-action: none;
}

.game-overlay {
  position: absolute;
  inset: 0;
  background: rgba(238, 228, 218, 0.78);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 5;
}
.game-overlay[hidden] { display: none; }
.overlay-title { font-size: 30px; font-weight: 800; color: var(--text-dark); }
.overlay-text { font-size: 16px; color: var(--accent-dark); }

.game-status {
  font-size: 14px;
  color: var(--accent-dark);
  text-align: center;
  min-height: 20px;
}

/* 按钮 */
.action-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  width: 100%;
  max-width: 460px;
}
.btn {
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-light);
  background: var(--accent-dark);
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
  box-shadow: var(--shadow);
}
.btn:hover { background: #9d8775; }
.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--accent); color: var(--text-dark); }
.btn-primary:hover { background: #f0cb45; }
.btn.active,
.btn[aria-pressed="true"] {
  background: #f67c5f;
  color: var(--text-light);
}
.btn-small { font-size: 14px; padding: 8px 14px; }

/* 广告位 */
.ad-placeholder {
  grid-column: 1 / -1;
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 14px;
}

/* 控制 / 信息面板 */
.panel-area {
  grid-column: 2;
  grid-row: 1;
  flex: 0 0 300px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.panel-h2 {
  font-size: 18px;
  font-weight: 800;
  color: var(--text-dark);
  margin-bottom: 10px;
}
.panel-tip { font-size: 14px; color: var(--accent-dark); margin-bottom: 10px; }
.info-card {
  background: var(--card-bg);
  border-radius: 10px;
  padding: 14px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.info-title { font-size: 16px; font-weight: 800; margin-bottom: 8px; color: var(--text-dark); }
.info-row {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  padding: 6px 0;
  border-bottom: 1px solid #f0ece4;
}
.info-row:last-child { border-bottom: none; }
.info-row span:last-child { font-weight: 700; color: var(--text-dark); }
.daily-desc { font-size: 14px; color: var(--accent-dark); margin-bottom: 8px; }

/* 历史列表 */
.history-list { list-style: none; display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.history-item {
  display: flex;
  justify-content: space-between;
  background: var(--card-bg);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  box-shadow: var(--shadow);
}
.history-item .h-score { font-weight: 800; color: var(--text-dark); }
.history-item .h-max { color: #f67c5f; font-weight: 700; }
.history-item .h-time { color: var(--accent-dark); }
.history-empty { font-size: 14px; color: var(--accent-dark); padding: 10px 0; }

/* 教程 / FAQ */
.tutorial-list { list-style: none; display: flex; flex-direction: column; gap: 10px; }
.tutorial-list li { font-size: 14px; line-height: 1.6; }
.faq-list-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.faq-item {
  background: var(--card-bg);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.faq-list dt { font-size: 15px; font-weight: 800; color: var(--text-dark); margin-top: 12px; }
.faq-list dt:first-child { margin-top: 0; }
.faq-item dt { margin-top: 0; }
.faq-list dd { font-size: 14px; line-height: 1.6; margin-top: 4px; color: #6b6258; }

/* TabBar（仅移动端） */
.tab-bar { display: none; }

/* 文章内容 */
.article-content {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow);
  line-height: 1.75;
}
.article-content h2 {
  font-size: 18px;
  font-weight: 800;
  color: var(--text-dark);
  margin: 18px 0 8px;
}
.article-content h2:first-child { margin-top: 0; }
.article-content p { font-size: 15px; color: #6b6258; margin-bottom: 8px; }
.article-content ul { padding-left: 20px; margin-bottom: 8px; }
.article-content li { font-size: 15px; color: #6b6258; margin-bottom: 6px; }
.article-content dt { font-size: 15px; font-weight: 700; color: var(--text-dark); margin-top: 10px; }
.article-content dd { font-size: 15px; color: #6b6258; margin-left: 0; }
.article-section {
  border-top: 1px solid #f1ebdf;
  padding-top: 16px;
  margin-top: 16px;
}
.article-section:first-child {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}
.faq-item {
  background: #fbf7ef;
  border: 1px solid #f1ebdf;
  border-radius: 10px;
  padding: 14px;
}
.faq-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 8px;
}
.faq-list dt { margin-top: 0; }

/* 页脚 */
.app-footer {
  text-align: center;
  padding: 20px 0;
  font-size: 14px;
  color: var(--accent-dark);
}
.footer-links { margin-top: 6px; display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }
.footer-links a { color: var(--accent-dark); text-decoration: none; }
.footer-links a:hover { text-decoration: underline; }

/* ============ 移动端布局 (<768px) ============ */
@media (max-width: 767px) {
  body { overflow: hidden; }
  .app-container {
    height: 100vh;
    height: 100dvh;
    max-width: 100%;
    padding: calc(env(safe-area-inset-top) + 8px) 0 0;
    gap: 6px;
    overflow: hidden;
  }

  .app-header {
    flex: 0 0 auto;
    padding: 0 76px 0 10px;
  }
  .site-title { font-size: 17px; }
  .site-subtitle { display: none; }
  .sound-toggle {
    right: 10px;
    padding: 5px 8px;
    font-size: 11px;
  }
  .sound-icon {
    width: 15px;
    height: 15px;
  }

  .app-main {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    justify-content: center;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    padding: 0 10px 6px;
  }

  .board-area {
    flex: 1 1 auto;
    justify-content: center;
    gap: clamp(8px, 1.4dvh, 14px);
    align-items: center;
    width: 100%;
    min-height: 0;
    padding: 0 0 8px;
  }
  .canvas-wrap {
    width: min(90vw, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 250px));
    min-width: 240px;
    max-width: 380px;
    margin: 0 auto;
  }
  .score-bar { max-width: min(90vw, 380px); gap: 8px; }
  .score-box { padding: 5px 12px; }
  .score-label { font-size: 11px; }
  .score-value { font-size: 18px; }
  .game-status { font-size: 12px; min-height: 16px; }
  .action-bar {
    max-width: min(90vw, 380px);
    gap: 5px;
    flex-wrap: wrap;
  }
  .btn { font-size: 12px; padding: 7px 10px; }

  .ad-placeholder {
    grid-column: auto;
    flex: 0 0 auto;
    min-height: 48px;
    margin-bottom: 4px;
  }

  /* 面板区默认隐藏，靠 Tab 切换 */
  .panel-area {
    grid-column: auto;
    grid-row: auto;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: none;
  }

  /* 非游戏 Tab：隐藏棋盘和广告位 */
  .app-container:not([data-active-tab="game"]) .board-area,
  .app-container:not([data-active-tab="game"]) .ad-placeholder {
    display: none;
  }

  /* 游戏 Tab：隐藏面板区 */
  .app-container[data-active-tab="game"] .panel-area { display: none; }

  /* 历史 Tab：显示面板区 */
  .app-container[data-active-tab="history"] .panel-area { display: block; }

  /* 教程 / 关于 Tab：隐藏面板区，显示文章对应区域 */
  .app-container[data-active-tab="tutorial"] .panel-area,
  .app-container[data-active-tab="about"] .panel-area { display: none; }

  /* 教程/关于 tab 时让 main 彻底收缩，空间全给 article-content */
  .app-container[data-active-tab="tutorial"] .app-main,
  .app-container[data-active-tab="about"] .app-main {
    flex: 0 0 0;
    min-height: 0;
    overflow: hidden;
    padding: 0;
    gap: 0;
  }

  .app-container[data-active-tab="tutorial"] .article-content,
  .app-container[data-active-tab="about"] .article-content {
    display: block;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 0;
    box-shadow: none;
    padding: 16px;
  }

  /* 教程 Tab 只显示「怎么玩」，FAQ 区域跟随「关于」Tab */
  .app-container[data-active-tab="tutorial"] .article-faq { display: none; }

  /* 移动端默认隐藏文章区和页脚 */
  .article-content { display: none; }
  .app-footer { display: none; }

  /* 底部 TabBar */
  .tab-bar {
    order: 999;
    flex: 0 0 auto;
    display: flex;
    justify-content: space-around;
    background: var(--card-bg);
    border-top: 2px solid #e8e2d8;
    padding: 0 0 env(safe-area-inset-bottom);
    width: 100%;
  }
  .tab-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 8px 0;
    background: none;
    border: none;
    cursor: pointer;
    color: #b0a89e;
    font-family: inherit;
    position: relative;
    transition: color 0.15s ease;
  }
  .tab-btn.active { color: #f67c5f; }
  .tab-icon {
    width: 22px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .tab-icon svg { width: 22px; height: 22px; fill: currentColor; }
  .tab-text { font-size: 11px; font-weight: 500; letter-spacing: 0.3px; }
  .tab-btn.active .tab-text { font-weight: 700; }
}

@media (max-width: 900px) {
  .article-grid,
  .faq-list {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 767px) and (max-height: 700px) {
  .board-area { gap: 6px; }
  .canvas-wrap {
    width: min(88vw, calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 205px));
    min-width: 200px;
  }
  .action-bar { gap: 4px; }
  .btn { padding: 6px 9px; }
}

/* 桌面端：面板始终显示游戏面板，其他面板通过 Tab 切换 */
@media (min-width: 768px) {
  .panel-content[hidden] { display: none; }
}
