/* ========== 基础变量与重置 ========== */
:root {
  --primary: #306998;
  --primary-light: #4b8bbe;
  --secondary: #ffd43b;
  --accent: #ffe873;
  --bg: #f8f9fa;
  --card-bg: #ffffff;
  --text: #212529;
  --text-muted: #6c757d;
  --border: #dee2e6;
  --code-bg: #f4f4f4;
  --shadow: 0 4px 12px rgba(0,0,0,0.08);
  --radius: 12px;
  --sidebar-width: 280px;
  --header-height: 64px;
  --transition: 0.3s ease;
}

[data-theme="dark"] {
  --bg: #0d1117;
  --card-bg: #161b22;
  --text: #c9d1d9;
  --text-muted: #8b949e;
  --border: #30363d;
  --code-bg: #1e2530;
  --shadow: 0 4px 12px rgba(0,0,0,0.3);
}

* { margin:0; padding:0; box-sizing:border-box; }
html { scroll-behavior: smooth; }
body {
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  transition: background var(--transition), color var(--transition);
}

a { text-decoration: none; color: var(--primary); }
a.logo { color: inherit; }
ul { list-style: none; }

/* 首页章节链接 */
.chapter-link {
  display: inline-block;
  color: inherit;
  transition: color var(--transition);
}
.chapter-link:hover {
  color: var(--primary);
}
.chapter-link:hover .badge {
  filter: brightness(1.1);
}

/* ========== 头部 ========== */
.header {
  position: fixed; top:0; left:0; right:0;
  height: var(--header-height);
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px; z-index: 1000;
  box-shadow: var(--shadow);
}
.logo {
  display: flex; align-items: center; gap: 10px;
  font-size: 1.25rem; font-weight: 700; color: var(--text);
}
.logo-icon {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: 1.1rem;
}
.header-actions {
  display: flex; align-items: center; gap: 16px;
}
.search-box {
  position: relative;
}
.search-box input {
  width: 220px; padding: 8px 12px 8px 36px;
  border: 1px solid var(--border); border-radius: 20px;
  background: var(--bg); color: var(--text);
  font-size: 0.9rem; outline: none;
  transition: width var(--transition), border-color var(--transition);
}
.search-box input:focus { width: 280px; border-color: var(--primary); }
.search-box svg {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px; stroke: var(--text-muted);
}
.theme-toggle, .menu-toggle {
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--bg);
  color: var(--text); cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  transition: all var(--transition);
}
.theme-toggle:hover, .menu-toggle:hover { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ========== 侧边栏 ========== */
.sidebar {
  position: fixed; top: var(--header-height); left: 0; bottom: 0;
  width: var(--sidebar-width); background: var(--card-bg);
  border-right: 1px solid var(--border); overflow-y: auto;
  padding: 20px 0; z-index: 900; transition: transform var(--transition);
}
.sidebar-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  z-index: 850;
}
.sidebar-section { margin-bottom: 8px; }
.sidebar-title {
  padding: 8px 24px; font-size: 0.75rem; text-transform: uppercase;
  letter-spacing: 1px; color: var(--text-muted); font-weight: 600;
}
.sidebar-link {
  display: block; padding: 10px 24px; color: var(--text);
  font-size: 0.95rem; border-left: 3px solid transparent;
  transition: all var(--transition); cursor: pointer;
}
.sidebar-link:hover, .sidebar-link.active {
  background: rgba(48,105,152,0.08); color: var(--primary);
  border-left-color: var(--primary);
}
.sidebar-link .chapter-num {
  display: inline-block; width: 24px; font-weight: 700; color: var(--primary-light);
}

/* ========== 主内容区 ========== */
.main {
  margin-left: var(--sidebar-width); padding-top: var(--header-height);
  min-height: 100vh; transition: margin var(--transition);
}
.content { max-width: 900px; margin: 0 auto; padding: 40px 24px; }

/* ========== 排版 ========== */
h1 { font-size: 2.4rem; font-weight: 800; margin-bottom: 16px; line-height: 1.2; }
h2 { font-size: 1.8rem; font-weight: 700; margin: 40px 0 16px; padding-bottom: 8px; border-bottom: 2px solid var(--border); }
h3 { font-size: 1.3rem; font-weight: 600; margin: 28px 0 12px; }
h4 { font-size: 1.1rem; font-weight: 600; margin: 20px 0 10px; }
p { margin-bottom: 16px; font-size: 1.05rem; }
.lead { font-size: 1.2rem; color: var(--text-muted); }

/* ========== 卡片与布局组件 ========== */
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; }
.card {
  background: var(--card-bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px;
  box-shadow: var(--shadow); transition: transform var(--transition), box-shadow var(--transition);
}
.card:hover { transform: translateY(-4px); box-shadow: 0 8px 24px rgba(0,0,0,0.12); }
.card-icon {
  width: 48px; height: 48px; border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 1.5rem; margin-bottom: 16px;
}
.card h3 { margin: 0 0 8px; font-size: 1.15rem; }
.card p { margin: 0; font-size: 0.95rem; color: var(--text-muted); }

.badge {
  display: inline-block; padding: 4px 10px; border-radius: 20px;
  font-size: 0.75rem; font-weight: 600; background: var(--primary);
  color: #fff; margin-right: 6px; margin-bottom: 6px;
}

/* ========== 代码块 ========== */
pre {
  background: var(--code-bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 20px;
  overflow-x: auto; font-family: "Fira Code", "Consolas", monospace;
  font-size: 0.95rem; line-height: 1.6; margin: 16px 0; position: relative;
}
code {
  font-family: "Fira Code", "Consolas", monospace;
  background: var(--code-bg); padding: 2px 6px; border-radius: 4px;
  font-size: 0.9em; color: var(--primary);
}
pre code { background: none; padding: 0; color: inherit; }
.code-header {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--border); padding: 8px 16px; border-radius: var(--radius) var(--radius) 0 0;
  font-size: 0.85rem; color: var(--text-muted); font-weight: 600;
}
.code-header + pre { border-radius: 0 0 var(--radius) var(--radius); margin-top: 0; }
.copy-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 0.8rem; display: flex; align-items: center; gap: 4px;
  transition: color var(--transition);
}
.copy-btn:hover { color: var(--primary); }

/* ========== 运行代码区域 ========== */
.run-box { border: 2px solid var(--primary); border-radius: var(--radius); overflow: hidden; margin: 20px 0; }
.run-box-header {
  background: var(--primary); color: #fff; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between;
}
.run-box-header span { font-weight: 600; font-size: 0.95rem; }
.run-btn {
  background: var(--secondary); color: #333; border: none;
  padding: 6px 16px; border-radius: 6px; font-weight: 600;
  cursor: pointer; transition: filter var(--transition); font-size: 0.9rem;
}
.run-btn:hover { filter: brightness(1.1); }
.run-box textarea {
  width: 100%; min-height: 120px; background: var(--code-bg); color: var(--text);
  border: none; padding: 16px 20px; font-family: "Fira Code", "Consolas", monospace;
  font-size: 0.95rem; line-height: 1.6; outline: none; resize: vertical;
}
.run-output {
  background: var(--bg); border-top: 1px solid var(--border);
  padding: 16px 20px; font-family: monospace; font-size: 0.95rem;
  min-height: 50px; white-space: pre-wrap; color: var(--text);
}
.run-output:empty::before { content: "点击运行查看输出..."; color: var(--text-muted); }

/* ========== 提示框 ========== */
.tip, .note, .warning {
  border-radius: var(--radius); padding: 16px 20px; margin: 20px 0;
  border-left: 4px solid;
}
.tip   { background: rgba(48,105,152,0.08); border-left-color: var(--primary); }
.note  { background: rgba(255,212,59,0.12); border-left-color: var(--secondary); }
.warning { background: rgba(220,53,69,0.08); border-left-color: #dc3545; }
.tip strong, .note strong, .warning strong { display: block; margin-bottom: 6px; font-weight: 700; }
.tip strong { color: var(--primary); }
.note strong { color: #856404; }
[data-theme="dark"] .note strong { color: var(--secondary); }
.warning strong { color: #dc3545; }

/* ========== 练习区 ========== */
.quiz-option {
  display: block; padding: 12px 16px; border: 2px solid var(--border);
  border-radius: 8px; margin-bottom: 10px; cursor: pointer;
  transition: all var(--transition); background: var(--card-bg);
}
.quiz-option:hover { border-color: var(--primary); background: rgba(48,105,152,0.05); }
.quiz-option.correct { border-color: #28a745; background: rgba(40,167,69,0.08); }
.quiz-option.wrong { border-color: #dc3545; background: rgba(220,53,69,0.08); }
.quiz-feedback { margin-top: 12px; font-weight: 600; }
.quiz-feedback.correct { color: #28a745; }
.quiz-feedback.wrong { color: #dc3545; }

/* ========== 表格 ========== */
table {
  width: 100%; border-collapse: collapse; margin: 20px 0;
  font-size: 0.95rem; background: var(--card-bg);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--primary); color: #fff; font-weight: 600; }
tr:hover { background: rgba(48,105,152,0.04); }

/* ========== 英雄区 ========== */
.hero {
  background: linear-gradient(135deg, var(--primary), #1a4a6e);
  color: #fff; padding: 60px 24px; text-align: center; border-radius: var(--radius); margin-bottom: 40px;
}
.hero h1 { font-size: 3rem; margin-bottom: 12px; }
.hero p { font-size: 1.2rem; opacity: 0.9; max-width: 600px; margin: 0 auto 24px; }
.hero-btn {
  display: inline-block; padding: 12px 32px; background: var(--secondary); color: #333;
  border-radius: 30px; font-weight: 700; font-size: 1.05rem; transition: transform var(--transition), box-shadow var(--transition);
}
.hero-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(255,212,59,0.3); }

/* ========== 进度条 ========== */
.progress-bar { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin: 12px 0; }
.progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width 0.5s ease; }

/* ========== 页脚 ========== */
.footer {
  text-align: center; padding: 40px 24px; color: var(--text-muted);
  border-top: 1px solid var(--border); margin-top: 40px; font-size: 0.9rem;
}

/* ========== 响应式 ========== */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.show { display: block; }
  .main { margin-left: 0; }
  .menu-toggle { display: flex; }
  .search-box input { width: 140px; }
  .search-box input:focus { width: 180px; }
  h1 { font-size: 1.8rem; }
  .hero h1 { font-size: 2rem; }
  .content { padding: 24px 16px; }
}
@media (min-width: 769px) {
  .menu-toggle { display: none; }
}

/* ========== 动画 ========== */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fadeInUp 0.6s ease forwards; }

/* ========== 隐藏 ========== */
.hidden { display: none !important; }
