/* ===== 在线拼图 · AI 智能版 样式 ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #f5f5f5;
  --board: #ffffff;
  --primary: #4a90e2;
  --primary-dark: #3a78c2;
  --accent: #b58900;
  --highlight: #ffc107;
  --text: #333333;
  --text-soft: #6b6b6b;
  --line: #e3e3e3;
  --radius: 12px;
  --header-bg: #fdf6e3;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* ===== 根容器：Flex 流式 ===== */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ===== 顶部标题栏 ===== */
.app-header {
  flex: 0 0 auto;
  background: var(--header-bg);
  text-align: center;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  border-bottom: 1px solid #ecdfb8;
}

.app-header h1 {
  margin: 0;
  font-size: 19px;
  color: var(--accent);
  letter-spacing: 0.5px;
}

.app-header h2 {
  margin: 4px 0 0;
  font-size: 14px;
  font-weight: 400;
  color: var(--text-soft);
}

/* ===== 主区域 ===== */
.app-main {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  gap: 20px;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
  min-height: 0;
}

/* ===== 拼图舞台 ===== */
.puzzle-stage {
  flex: 2 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.canvas-wrap {
  position: relative;
  width: min(100%, 64vh);
  aspect-ratio: 1 / 1;
  background: var(--board);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  padding: 10px;
}

#puzzleCanvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  touch-action: none;
}

/* 通关遮罩 */
.canvas-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(253, 246, 227, 0.86);
  border-radius: var(--radius);
}

.canvas-overlay[hidden] {
  display: none;
}

.win-card {
  text-align: center;
  background: #fff;
  padding: 24px 28px;
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
}

.win-title {
  margin: 0 0 8px;
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
}

.win-detail {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text-soft);
}

.status-bar {
  display: flex;
  gap: 18px;
  margin-top: 14px;
  flex-wrap: wrap;
  justify-content: center;
}

.status-item {
  font-size: 14px;
  color: var(--text-soft);
}

.status-item strong {
  color: var(--text);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.hint {
  margin: 10px 0 0;
  font-size: 14px;
  color: var(--text-soft);
  text-align: center;
  max-width: 440px;
}

/* ===== 控制面板 ===== */
.control-panel {
  flex: 1 1 0;
  min-width: 240px;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-block {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.panel-title {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--text);
  font-weight: 600;
}

.btn-row,
.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.ctrl-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  padding: 9px 12px;
  font-size: 14px;
  color: var(--text);
  background: #f0f3f7;
  border: 1px solid var(--line);
  border-radius: 8px;
  transition: background 0.2s, color 0.2s, border-color 0.2s, transform 0.1s;
  white-space: nowrap;
}

.ctrl-btn:hover {
  background: #e6ebf2;
}

.ctrl-btn:active {
  transform: scale(0.97);
}

.ctrl-btn.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.ctrl-btn.primary:hover {
  background: var(--primary-dark);
}

.ctrl-btn.diff-btn.is-active,
.preset-btn.is-active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.preset-btn {
  flex: 1 1 0;
  padding: 9px 4px;
  font-size: 14px;
  background: #f0f3f7;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.preset-btn:hover {
  background: #e6ebf2;
}

.upload-field {
  display: block;
  margin-top: 10px;
}

.upload-field .ctrl-btn {
  width: 100%;
  font-size: 14px;
}

.preview-toggle[aria-pressed="true"],
.sound-toggle[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.best-block .best-time {
  margin: 4px 0;
  font-size: 14px;
  color: var(--text-soft);
}

.best-block .best-time strong {
  color: var(--text);
}

/* ===== 广告位：完全由 CSS 控制显隐，JS 不参与 ===== */
.ad-slot {
  display: none;
  min-height: 60px;
  max-height: 60px;
  margin: 10px 16px;
  color: #999;
  font-size: 14px;
  border-radius: 8px;
  border: 1px dashed #ccc;
  background: #fafafa;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* 当广告位有内容时自动显示 */
.ad-slot:not(:empty) {
  display: flex;
  flex: 0 0 60px;
}

/* ===== 文章内容区 ===== */
.article-content {
  flex: 0 0 auto;
  padding: 20px;
  max-width: 1100px;
  width: calc(100% - 32px);
  margin: 0 auto;
  -webkit-overflow-scrolling: touch;
}

.article-content h2 {
  font-size: 20px;
  color: var(--accent);
  margin: 0 0 14px;
}

.article-content h3 {
  font-size: 16px;
  margin: 22px 0 8px;
  color: var(--text);
}

.article-content p {
  font-size: 15px;
  margin: 0 0 12px;
}

.article-content ul {
  padding-left: 20px;
  margin: 0 0 12px;
}

.article-content li {
  font-size: 15px;
  margin-bottom: 6px;
}

.faq dt {
  font-weight: 600;
  margin-top: 12px;
  font-size: 15px;
  color: var(--text);
}

.faq dd {
  margin: 4px 0 0;
  color: var(--text-soft);
  font-size: 14px;
}

.record-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 10px;
  font-size: 14px;
}

.record-table th,
.record-table td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: center;
}

.record-table th {
  background: #faf7ee;
  color: var(--text);
}

.empty-row {
  color: var(--text-soft);
}

/* ===== 页脚 ===== */
.app-footer {
  flex: 0 0 auto;
  text-align: center;
  padding: 14px 16px;
  font-size: 14px;
  color: var(--text-soft);
  background: #fff;
  border-top: 1px solid var(--line);
}

.app-footer a {
  color: inherit;
  text-decoration: none;
}

.app-footer a:hover {
  text-decoration: none;
}

/* ===== 底部 TabBar（移动端） ===== */
.tab-bar {
  display: none;
  flex: 0 0 auto;
  flex-shrink: 0;
  order: 999;
  background: #fff;
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-btn {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0;
  background: none;
  border: none;
  color: var(--text-soft);
  font-size: 14px;
}

.tab-btn .tab-icon {
  font-size: 20px;
  line-height: 1;
}

.tab-btn .tab-label {
  font-size: 14px;
}

.tab-btn.is-active {
  color: var(--primary);
}

/* ===== 响应式：移动端（应用外壳 + Tab 切换） ===== */
@media (max-width: 768px) {
  .app-container {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .app-main {
    flex: 1 1 auto;
    flex-direction: column;
    gap: 12px;
    padding: 12px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
  }

  /* 切换到文章 Tab：折叠主区域，让文章占满剩余高度 */
  .app-main.main-collapse {
    flex: 0 0 0;
    padding: 0;
    overflow: hidden;
  }

  /* 拼图舞台：撑满首屏（app-main 可视高度），控制面板在折叠下方 */
  .puzzle-stage {
    min-height: calc(
      100dvh
      - var(--header-h, 65px)
      - var(--ad-h, 0px)
      - var(--tabbar-h, 56px)
    );
    flex-shrink: 0;
    padding-top: 4px;
  }

  /* 棋盘：以高度为主驱动尺寸，不超出宽度 */
  .canvas-wrap {
    width: min(
      calc(100vw - 24px),
      calc(
        100dvh
        - var(--header-h, 65px)
        - var(--ad-h, 0px)
        - var(--tabbar-h, 56px)
        - 100px
      )
    );
  }

  .control-panel {
    max-width: none;
    min-width: 0;
  }

  /* 文章区默认隐藏，由 Tab 控制显示 */
  .article-content {
    display: none;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    width: 100%;
    max-width: none;
  }

  .article-content.is-active {
    display: block;
  }

  .app-footer {
    display: none;
  }

  .tab-bar {
    display: flex;
  }
}

/* ===== PC 端首屏：确保棋盘+广告占满首屏，文章内容在折叠线以下 ===== */
@media (min-width: 769px) {
  .app-main {
    flex: 1 0 auto;
    min-height: calc(100vh - var(--header-h, 80px) - 90px);
  }

  /* PC 端广告位始终保留高度占位，避免文章内容上移 */
  .ad-slot {
    display: flex;
    flex: 0 0 60px;
    min-height: 60px;
  }

  /* 空广告位不显示虚线框，保持低调占位 */
  .ad-slot:empty {
    border-color: transparent;
    background: transparent;
  }
}

/* ===== 自动还原进行中：停止按钮样式 ===== */
.ctrl-btn.is-solving {
  background: #e74c3c;
  color: #fff;
  border-color: #c0392b;
}

.ctrl-btn.is-solving:hover {
  background: #c0392b;
}
