:root {
  --bg: #1a1a2e;
  --wall: #0f3460;
  --path: #16213e;
  --accent: #e94560;
  --accent-2: #ffd166;
  --green: #2ecc71;
  --text: #e6e9f0;
  --muted: #9aa3c0;
  --panel: #20203a;
  --panel-2: #2a2a4a;
  --line: #33335a;
  --header-space: 62px;
  --ad-height: 60px;
  --ad-gap: 20px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

/* 顶部标题栏 */
.app-header {
  flex: 0 0 auto;
  text-align: center;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: linear-gradient(180deg, #232347, var(--bg));
  border-bottom: 1px solid var(--line);
}
.app-header h1 { font-size: 20px; color: #fff; letter-spacing: 1px; }
.app-header h2 { font-size: 14px; font-weight: 400; color: var(--muted); margin-top: 2px; }

/* 主区域：PC 上与文章区同宽，迷宫与侧栏成组居中 */
.app-main {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  gap: 16px;
  width: 100%;
  max-width: 1100px;
  margin-left: auto;
  margin-right: auto;
  padding: 16px;
  height: calc(100vh - var(--header-space) - var(--ad-height) - var(--ad-gap));
  height: calc(100dvh - var(--header-space) - var(--ad-height) - var(--ad-gap));
  min-height: 520px;
  overflow: hidden;
}

.maze-stage {
  flex: 2 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  gap: 10px;
}

.canvas-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
  overflow: hidden;
}

#mazeCanvas {
  background: var(--path);
  border: 2px solid var(--wall);
  border-radius: 6px;
  box-shadow: 0 0 24px rgba(15, 52, 96, 0.6);
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
}

.status-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
  font-size: 14px;
  color: var(--muted);
}
.status-item strong { color: var(--accent-2); font-variant-numeric: tabular-nums; }

.hint { font-size: 14px; color: var(--muted); text-align: center; }

/* 方向键(默认隐藏，移动端显示) */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(2, 48px);
  gap: 6px;
}
.dpad-btn {
  font-size: 18px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.dpad-btn:active { background: var(--accent); }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }

/* 控制面板 */
.control-panel {
  flex: 1 1 0;
  max-width: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
}
.panel-block {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}
.panel-title { font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

.ctrl-btn {
  flex: 1 1 auto;
  min-width: 80px;
  font-size: 14px;
  padding: 9px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.ctrl-btn:hover { border-color: var(--accent); }
.ctrl-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.ctrl-btn.primary:hover { background: #ff5b78; }
.diff-btn.is-active { background: var(--wall); border-color: var(--accent-2); color: #fff; }
.sound-toggle.is-on { background: var(--green); border-color: var(--green); color: #071b10; }

.custom-row { display: flex; align-items: flex-end; gap: 8px; flex-wrap: wrap; }
.custom-field { font-size: 14px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; flex: 1 1 60px; }
.custom-field input {
  width: 100%;
  font-size: 16px;
  padding: 7px 8px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.custom-row .ctrl-btn { flex: 0 0 auto; min-width: 64px; }

.best-time { font-size: 14px; color: var(--muted); }
.best-time strong { color: var(--accent-2); }

/* 广告位 */
.ad-slot { display: none; }
.ad-slot:not(:empty) {
  flex: 0 0 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60px;
  max-height: 60px;
  margin: 10px 16px;
  color: var(--muted);
  font-size: 14px;
  border-radius: 8px;
}

/* 文章内容区 */
.article-content {
  flex: 0 0 auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}
.article-content[hidden] { display: none; }
.article-content.desktop-content {
  display: block;
  max-width: 1100px;
  width: calc(100% - 32px);
  margin: 14px auto 14px;
  background: rgba(32, 32, 58, 0.62);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.article-content.desktop-content[hidden] { display: block; }
#panelRecord.desktop-content { display: none; }
.article-content h2 { font-size: 18px; color: #fff; margin-bottom: 10px; }
.article-content h3 { font-size: 15px; color: var(--accent-2); margin: 14px 0 6px; }
.article-content p { font-size: 14px; line-height: 1.7; color: var(--text); margin-bottom: 8px; }
.article-content ul { font-size: 14px; line-height: 1.7; padding-left: 20px; margin-bottom: 8px; }
.article-content li { margin-bottom: 4px; }
.faq dt { font-size: 14px; color: var(--accent-2); margin-top: 8px; }
.faq dd { font-size: 14px; color: var(--muted); margin: 2px 0 0; }

.record-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  margin-bottom: 8px;
}
.record-table th, .record-table td {
  border: 1px solid var(--line);
  padding: 8px;
  text-align: center;
}
.record-table th { background: var(--panel-2); color: var(--muted); }
.record-table td { color: var(--text); }
.empty-row { color: var(--muted); }

/* 页脚 */
.app-footer {
  flex: 0 0 auto;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  padding: 8px 12px;
  border-top: 1px solid var(--line);
}
.app-footer a { color: var(--muted); }

/* 底部 TabBar */
.tab-bar {
  display: none;
  order: 999;
  flex: 0 0 auto;
  flex-shrink: 0;
  height: 50px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.tab-btn {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
}
.tab-btn .tab-icon { font-size: 18px; line-height: 1; }
.tab-btn .tab-label { font-size: 12px; }
.tab-btn.is-active { color: var(--accent); }

/* ===== 移动端 ===== */
@media (max-width: 760px) {
  :root {
    --header-space: 56px;
    --tab-height: 56px;
  }

  .app-header { padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .app-header h1 { font-size: 17px; }
  .app-header h2 { font-size: 13px; }

  /* 底部留出 tab-bar 空间，让 flex 子元素自然填满剩余区域 */
  .app-container {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding-bottom: calc(var(--tab-height) + env(safe-area-inset-bottom));
  }
  /* app-main 弹性填充剩余空间；广告槽出现时自动收缩，无广告时占满 */
  .app-main {
    flex: 1 1 0;
    flex-direction: column;
    max-width: none;
    margin-left: 0;
    margin-right: 0;
    padding: 10px 12px;
    gap: 10px;
    min-height: 0;
    overflow: hidden;
  }
  .maze-stage { flex: 1 1 0; gap: 7px; min-height: 0; }
  .canvas-wrap { flex: 1 1 0; min-height: 220px; }
  .status-bar { gap: 12px; font-size: 13px; }
  .hint { font-size: 13px; line-height: 1.35; }
  .control-panel {
    flex: 0 0 auto;
    max-width: none;
    max-height: 35%;
    gap: 10px;
  }

  .dpad { display: none; }

  /* 广告槽有内容时自然占位，无内容时 display:none 不占空间 */
  .ad-slot:not(:empty) {
    margin: 0 12px 6px;
  }

  .app-footer { display: none; }
  .tab-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    height: calc(var(--tab-height) + env(safe-area-inset-bottom));
    min-height: calc(var(--tab-height) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tab-btn {
    min-height: var(--tab-height);
  }
  /* 容器 padding-bottom 已隔开 tab-bar，文章内部只需常规底部留白 */
  .article-content {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    padding: 14px 14px 20px;
  }
  .article-content.desktop-content {
    display: block;
    width: auto;
    max-width: none;
    margin: 10px 12px 10px;
    background: rgba(32, 32, 58, 0.36);
    border: 1px solid rgba(51, 51, 90, 0.72);
    border-radius: 12px;
  }
  .article-content.desktop-content[hidden],
  #panelRecord.desktop-content[hidden] {
    display: none;
  }
  #panelRecord.desktop-content {
    display: block;
  }

  /* 切到文章页时收缩主区域 */
  .app-container.view-article .app-main {
    flex: 0 0 0;
    height: 0;
    min-height: 0;
    overflow: hidden;
    padding: 0;
  }
  .app-container.view-article .ad-slot:not(:empty) {
    margin: 8px 12px;
  }
  .app-container.view-article .article-content.desktop-content {
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .app-container.view-article .app-footer { display: none; }

  .maze-stage .dpad,
  .maze-stage .hint {
    display: none;
  }
}
