/* ===========================================================
   四把尺子理论 v27 · 共享样式
   参考 v26/build_main_html.py 主题系统 + 工具面板样式
   适配 v27 全部 HTML（main.html / index.html / 0-5.html / ai_discussion）
   =========================================================== */

/* === 基础变量（默认深色主题） === */
:root {
  --bg: #0f172a;
  --panel-bg: #1e293b;
  --panel-border: #334155;
  --text: #e2e8f0;
  --text-secondary: #94a3b8;
  --link: #7c3aed;
  --code-bg: #eef2ff;
  --button-bg: transparent;
  --font-scale: 1;
  --table-stripe: rgba(255,255,255,0.03);
  --note-bg: rgba(255,255,255,0.05);
}

/* === 浅色主题 === */
html[data-theme="light"] {
  --bg: #f8fafc;
  --panel-bg: #ffffff;
  --panel-border: #dbe3ee;
  --text: #1e293b;
  --text-secondary: #475569;
  --link: #7c3aed;
  --code-bg: #eef2ff;
  --button-bg: #f1f5f9;
  --table-stripe: rgba(0,0,0,0.02);
  --note-bg: rgba(0,0,0,0.03);
}

/* === 深色主题 === */
html[data-theme="dark"] {
  --bg: #0f172a;
  --panel-bg: #1e293b;
  --panel-border: #334155;
  --text: #e2e8f0;
  --text-secondary: #94a3b8;
  --link: #7c3aed;
  --code-bg: rgba(124,58,237,0.15);
  --button-bg: transparent;
  --table-stripe: rgba(255,255,255,0.03);
  --note-bg: rgba(255,255,255,0.05);
}

/* === 暗黑主题 === */
html[data-theme="black"] {
  --bg: #000000;
  --panel-bg: #0a0a0a;
  --panel-border: #1a1a1a;
  --text: #e0e0e0;
  --text-secondary: #888888;
  --link: #a855f7;
  --code-bg: #1a1a1a;
  --button-bg: #0a0a0a;
  --table-stripe: rgba(255,255,255,0.02);
  --note-bg: rgba(255,255,255,0.03);
}

/* === 护眼主题 === */
html[data-theme="sepia"] {
  --bg: #f5e6c8;
  --panel-bg: #fbeed0;
  --panel-border: #d4b896;
  --text: #2d1b0e;
  --text-secondary: #5a4634;
  --link: #b45309;
  --code-bg: #ede0c0;
  --button-bg: #ede0c0;
  --table-stripe: rgba(0,0,0,0.03);
  --note-bg: rgba(0,0,0,0.05);
}

/* === 森林主题 === */
html[data-theme="forest"] {
  --bg: #1a2e1a;
  --panel-bg: #243524;
  --panel-border: #3a523a;
  --text: #d4e4d4;
  --text-secondary: #9eb89e;
  --link: #4ade80;
  --code-bg: rgba(74,222,128,0.12);
  --button-bg: transparent;
  --table-stripe: rgba(74,222,128,0.04);
  --note-bg: rgba(74,222,128,0.08);
}

/* === 石板主题 === */
html[data-theme="slate"] {
  --bg: #1e293b;
  --panel-bg: #2c3e50;
  --panel-border: #475569;
  --text: #cbd5e1;
  --text-secondary: #94a3b8;
  --link: #38bdf8;
  --code-bg: rgba(56,189,248,0.12);
  --button-bg: transparent;
  --table-stripe: rgba(56,189,248,0.04);
  --note-bg: rgba(56,189,248,0.08);
}

/* === 紫罗兰主题 === */
html[data-theme="purple"] {
  --bg: #2e1065;
  --panel-bg: #3b1d8a;
  --panel-border: #5b21b6;
  --text: #e9d5ff;
  --text-secondary: #c4b5fd;
  --link: #f0abfc;
  --code-bg: rgba(240,171,252,0.12);
  --button-bg: transparent;
  --table-stripe: rgba(240,171,252,0.04);
  --note-bg: rgba(240,171,252,0.08);
}

/* === 跟随系统 === */
@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] {
    --bg: #0f172a;
    --panel-bg: #1e293b;
    --panel-border: #334155;
    --text: #e2e8f0;
    --text-secondary: #94a3b8;
    --link: #7c3aed;
    --code-bg: rgba(124,58,237,0.15);
    --button-bg: transparent;
    --table-stripe: rgba(255,255,255,0.03);
    --note-bg: rgba(255,255,255,0.05);
  }
}
@media (prefers-color-scheme: light) {
  html[data-theme="auto"] {
    --bg: #f8fafc;
    --panel-bg: #ffffff;
    --panel-border: #dbe3ee;
    --text: #1e293b;
    --text-secondary: #475569;
    --link: #7c3aed;
    --code-bg: #eef2ff;
    --button-bg: #f1f5f9;
    --table-stripe: rgba(0,0,0,0.02);
    --note-bg: rgba(0,0,0,0.03);
  }
}

/* === 全局 === */
* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  line-height: 1.7;
  font-size: 16px;
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* === 主容器 === */
main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}
header { margin-bottom: 24px; }
footer {
  margin-top: 30px;
  color: var(--text-secondary);
  font-size: 14px;
  opacity: 0.75;
  text-align: center;
  padding: 20px 0;
  border-top: 1px solid var(--panel-border);
}

/* === 头部 === */
.article-header {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  color: #ffffff;
  padding: 50px 40px;
  border-radius: 16px;
  margin-bottom: 30px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.15);
}
.article-header h1 {
  font-size: 2em;
  font-weight: 700;
  margin: 0 0 16px 0;
  letter-spacing: 1px;
  line-height: 1.4;
}
.article-header .meta {
  font-size: 0.95em;
  opacity: 0.88;
  line-height: 2;
}
.article-header .meta p { margin: 4px 0; }
.article-header .meta a { color: #93c5fd; text-decoration: none; }
.article-header .meta a:hover { text-decoration: underline; }
.article-header .article-subtitle {
  font-size: 1em;
  opacity: 0.78;
  margin: 0;
  font-weight: 400;
}

/* === 顶部导航条 === */
.article-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  margin-bottom: 24px;
  font-size: 14px;
}
.article-nav a {
  padding: 4px 10px;
  border-radius: 6px;
  color: var(--text);
  background: var(--button-bg);
  border: 1px solid var(--panel-border);
}
.article-nav a:hover { background: var(--link); color: #fff; border-color: var(--link); text-decoration: none; }

/* === 布局容器（带 TOC 侧边栏） === */
.layout-container {
  display: flex;
  gap: 20px;
  align-items: start;
}

/* === TOC 侧边栏 === */
.sidebar {
  position: sticky;
  top: 20px;
  flex: 0 0 280px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 16px;
  font-size: 14px;
}
.sidebar-head {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--panel-border);
}
.sidebar-title {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  color: var(--text);
}
.sidebar-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.toc-global-toggle {
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}
.toc-global-toggle:hover { border-color: var(--link); color: var(--link); }
.toc-tree { list-style: none; padding: 0; margin: 0; }
.toc-children {
  margin-left: 12px;
  padding-left: 8px;
  border-left: 1px dashed var(--panel-border);
}
.toc-children.collapsed { display: none; }
.toc-h1 > .toc-item-row > a { font-weight: 700; font-size: 14px; }
.toc-h2 > .toc-item-row > a { font-weight: 600; font-size: 13px; }
.toc-h3 > .toc-item-row > a { font-size: 12px; color: var(--text-secondary); opacity: 0.9; }
.toc-h4 > .toc-item-row > a { font-size: 12px; color: var(--text-secondary); opacity: 0.75; padding-left: 4px; }
.toc-item-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 0;
}
.toc-item-row a {
  color: var(--text);
  text-decoration: none;
  flex: 1;
  padding: 2px 4px;
  border-radius: 4px;
}
.toc-item-row a:hover { background: var(--link); color: #fff; }
.toc-current > .toc-item-row > a {
  background: var(--link);
  color: #fff;
  font-weight: 600;
}
.toc-toggle {
  border: none;
  background: transparent;
  color: var(--text);
  width: 18px; height: 18px;
  cursor: pointer;
  font-family: monospace;
  border-radius: 3px;
  font-size: 12px;
}
.toc-toggle:hover { background: var(--link); color: #fff; }
.toc-spacer { width: 18px; display: inline-block; }

/* === 主内容区 === */
.article-content { flex: 1; min-width: 0; }

.content {
  background: var(--panel-bg);
  padding: 40px 50px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: calc(18px * var(--font-scale));
  line-height: 1.9;
}

/* === 标题 === */
.content h1, .content h2, .content h3, .content h4, .content h5, .content h6 {
  color: var(--text);
  margin: 1.4em 0 0.6em 0;
  font-weight: 700;
  line-height: 1.4;
  scroll-margin-top: 24px;
}
.content h1 { font-size: 1.9em; border-bottom: 3px solid var(--link); padding-bottom: 12px; }
.content h2 { font-size: 1.5em; border-left: 4px solid var(--link); padding-left: 14px; margin-top: 2em; }
.content h3 { font-size: 1.25em; color: var(--text); }
.content h4 { font-size: 1.1em; color: var(--text-secondary); }

/* === 段落与列表 === */
.content p { margin: 12px 0; }
.content ul, .content ol { padding-left: 24px; margin: 12px 0; }
.content li { margin: 4px 0; }

/* === 行内代码 === */
.content code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 6px;
  font-family: "Consolas", "Monaco", monospace;
  font-size: 0.92em;
  color: var(--text);
}

/* === 代码块 === */
.content pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 16px 18px;
  border-radius: 10px;
  overflow-x: auto;
  margin: 16px 0;
  font-size: 0.92em;
  line-height: 1.6;
}
.content pre code {
  background: transparent;
  padding: 0;
  color: inherit;
  font-size: 1em;
}

/* === 引用块 === */
.content blockquote {
  margin: 14px 0;
  padding: 12px 16px;
  border-left: 3px solid var(--link);
  background: var(--note-bg);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.96em;
}
html[data-theme="light"] .content blockquote { background: #f3f4f6; }
html[data-theme="sepia"] .content blockquote { background: #f5e6c8; color: #2d1b0e; }

/* === 分隔线 === */
.content hr {
  border: 0;
  border-top: 1px solid var(--panel-border);
  margin: 24px 0;
}

/* === 强调 === */
.content strong { font-weight: 700; color: var(--text); }
.content em { font-style: italic; }

/* === 表格 === */
.content table {
  border-collapse: collapse;
  width: 100%;
  min-width: 620px;
  margin: 12px 0;
}
.content th, .content td {
  border: 1px solid var(--panel-border);
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
}
.content thead th {
  background: #2c3e50;
  color: #ffffff;
  font-weight: 700;
}
.content tbody tr:nth-child(even) { background: var(--table-stripe); }
.content .table-wrap { overflow-x: auto; margin: 18px 0; }

/* === 强调引用（callout）=== */
.content blockquote strong { color: var(--link); }

/* === 阅读设置面板 === */
.font-zoom {
  position: fixed;
  right: 18px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel-bg);
  box-shadow: 0 10px 24px rgba(15, 23, 42, .12);
  font-size: 13px;
  min-width: 200px;
}
.font-zoom .tool-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.font-zoom-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.font-zoom button {
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 12px;
}
.font-zoom button:hover { border-color: var(--link); color: var(--link); }
.font-zoom select {
  width: 100%;
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12px;
}

/* === TTS 语音阅读面板 === */
.tts-panel {
  position: fixed;
  right: 18px;
  bottom: 20px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel-bg);
  box-shadow: 0 10px 24px rgba(15, 23, 42, .12);
  font-size: 13px;
  min-width: 240px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
}
.tts-panel .tool-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.tts-panel-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}
.tts-panel button {
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 12px;
}
.tts-panel button:hover { border-color: var(--link); color: var(--link); }
.tts-panel select {
  width: 100%;
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12px;
}
.tts-panel .tool-group-note {
  font-size: 12px;
  color: var(--text-secondary);
  padding: 4px 6px;
  border-radius: 4px;
  background: var(--note-bg);
  margin-top: 4px;
  min-height: 1.4em;
  line-height: 1.5;
}

/* === 通用工具组 === */
.tool-group { margin-bottom: 10px; }
.tool-group-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  opacity: 0.7;
}
.tool-button-row { display: flex; gap: 6px; margin-top: 6px; }
.tool-button-row button { flex: 1; }

/* === 面板切换 === */
.tool-panel-body { display: block; }
/* 任何带 data-panel-id 的 aside 折叠时都隐藏 .tool-panel-body */
[data-panel-id].collapsed .tool-panel-body { display: none; }
/* 折叠后只剩标题，缩小面板宽度，让"展开"按钮可见 */
[data-panel-id].collapsed {
  min-width: 0;
  width: auto;
}
[data-panel-id].collapsed .tool-panel-head { margin-bottom: 0; }
[data-panel-toggle] {
  cursor: pointer;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
}
[data-panel-toggle]:hover { border-color: var(--link); color: var(--link); }

/* === 移动端响应 === */
@media (max-width: 1024px) {
  .layout-container { flex-direction: column; }
  .sidebar { position: static; flex: 0 0 auto; max-height: none; }
  .font-zoom { right: 10px; top: auto; bottom: 20px; transform: none; }
  .tts-panel { right: 10px; bottom: 10px; min-width: 200px; }
  .content { padding: 24px 20px; }
  .article-header { padding: 30px 20px; }
  .article-header h1 { font-size: 1.5em; }
}

/* === 索引页专用：卡片网格 === */
.index-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin: 24px 0;
}
.index-card {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 20px 24px;
  transition: transform 0.2s, box-shadow 0.2s;
}
.index-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
  border-color: var(--link);
}
.index-card h3 {
  margin: 0 0 8px 0;
  font-size: 1.15em;
  color: var(--text);
}
.index-card h3 a { color: inherit; text-decoration: none; }
.index-card h3 a:hover { color: var(--link); }
.index-card .meta {
  font-size: 0.85em;
  color: var(--text-secondary);
  margin-bottom: 10px;
}
.index-card p { margin: 0; color: var(--text); line-height: 1.7; }

/* === 版本徽章 === */
.version-badge {
  display: inline-block;
  padding: 2px 10px;
  font-size: 0.78em;
  font-weight: 700;
  border-radius: 10px;
  background: var(--link);
  color: #fff;
  margin-right: 6px;
  vertical-align: middle;
}
.version-badge.new { background: #10b981; }
.version-badge.legacy { background: #6b7280; }


/* === 总览图区（hero-figure） === */
.hero-figure {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 20px;
  margin: 0 0 24px 0;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0,0,0,0.06);
}
.hero-figure object,
.hero-figure img {
  display: block;
  width: 100%;
  max-width: 900px;
  height: auto;
  margin: 0 auto;
  border-radius: 8px;
}
.hero-toggle {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 16px;
  font-size: 13px;
}
.hero-toggle-btn {
  border: 1px solid var(--panel-border);
  background: var(--button-bg);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
}
.hero-toggle-btn:hover { border-color: var(--link); color: var(--link); }
.hero-toggle-btn.is-active {
  background: var(--link);
  color: #ffffff;
  border-color: var(--link);
}

/* === 卡片图标 === */
.card-icon-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(124, 58, 237, 0.12), rgba(124, 58, 237, 0.04));
  color: var(--link);
  margin-bottom: 12px;
}
.card-icon {
  width: 28px;
  height: 28px;
  display: block;
}
