/* ============================================================
 * study.yayafan.cn — 浅色·教科书风格
 * 设计目标: 像算法导论 / LeetCode 那样长时间读不刺眼,
 * 蓝绿强调色 + 衬线正文 + 等宽代码 + 克制阴影.
 * ============================================================ */

:root {
  --bg:           #fbfaf6;  /* 暖白,纸张感 */
  --bg-2:         #f4f1e8;  /* 代码区底色 */
  --bg-card:      #ffffff;  /* 卡片 */
  --ink:          #1a1a17;  /* 主文字 */
  --ink-mute:     #5b574d;  /* 次要文字 */
  --ink-faint:    #8a8475;  /* 辅助文字 */
  --accent:       #1d6f5b;  /* 墨蓝绿 — 主强调 */
  --accent-soft:  #e7f3ee;  /* 高亮背景 */
  --accent-line:  #b6dccd;  /* 高亮边框 */
  --warn:         #b16207;  /* 警告/比较 */
  --danger:       #b91c1c;  /* 错误/交换 */
  --rule:         #e3dfd4;  /* 分隔线 */
  --rule-strong:  #c8c2b1;
  --code-bg:      #f4f1e8;
  --code-line:    #d6d1c0;  /* 当前代码行高亮底色 */

  --font-serif:   "Source Serif Pro", "Source Han Serif SC", "Noto Serif CJK SC",
                  Georgia, "Times New Roman", serif;
  --font-sans:    "Inter", "Helvetica Neue", -apple-system, BlinkMacSystemFont,
                  "PingFang SC", "Microsoft YaHei", sans-serif;
  --font-mono:    "JetBrains Mono", "SF Mono", "Cascadia Code",
                  "Fira Code", Consolas, "Liberation Mono", monospace;

  --radius:       4px;
  --shadow-1:     0 1px 0 rgba(0,0,0,.04), 0 2px 6px rgba(0,0,0,.03);
  --shadow-2:     0 1px 0 rgba(0,0,0,.06), 0 4px 12px rgba(0,0,0,.05);

  --header-h:     64px;
}

/* ----- Reset ----- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.7;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }
button { font-family: inherit; cursor: pointer; }

/* ----- Topbar ----- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 32px;
  height: var(--header-h);
  padding: 0 32px;
  background: rgba(251, 250, 246, 0.92);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand {
  display: inline-flex;
  align-items: baseline;
  gap: 12px;
  color: var(--ink);
  font-family: var(--font-sans);
}
.brand:hover { text-decoration: none; }
.brand-mark {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 12px;
  color: var(--ink-faint);
  letter-spacing: 0.04em;
}

.nav {
  display: flex;
  gap: 4px;
  flex: 1;
}
.nav-link {
  padding: 8px 14px;
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--ink-mute);
  border-radius: var(--radius);
  transition: color .15s, background-color .15s;
}
.nav-link:hover {
  color: var(--ink);
  background: rgba(0,0,0,.03);
  text-decoration: none;
}
.nav-link.is-active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

.lang-switch {
  display: inline-flex;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  overflow: hidden;
}
.lang-btn {
  padding: 6px 12px;
  font-family: var(--font-mono);
  font-size: 12px;
  background: transparent;
  border: none;
  color: var(--ink-mute);
  border-right: 1px solid var(--rule);
}
.lang-btn:last-child { border-right: none; }
.lang-btn:hover { background: rgba(0,0,0,.04); color: var(--ink); }
.lang-btn.is-active {
  background: var(--accent);
  color: #fff;
}

/* ----- Main ----- */
.main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px 32px 64px;
}
.noscript {
  background: #fff7e6;
  border: 1px solid #f1c97e;
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 24px;
  color: var(--ink);
  font-family: var(--font-sans);
}

/* ----- Footer ----- */
.footer {
  border-top: 1px solid var(--rule);
  padding: 24px 32px;
  text-align: center;
  color: var(--ink-mute);
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.6;
}
.footer p { margin: 4px 0; }
.footer .copy { color: var(--ink-faint); font-size: 12px; }
.footer .beian {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.footer .beian .dot { color: var(--ink-faint); }
.footer .beian-icon { height: 14px; width: auto; vertical-align: -2px; }
.footer .beian a { color: var(--ink-mute); }
.footer .beian a:hover { color: var(--accent); text-decoration: underline; }

/* ============================================================
 * Tab: Networking (网络 tab)
 * sidebar 锚点 + 单节详情,跟算法 tab 一致的两栏布局
 * ============================================================ */
.networking-layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 32px;
  align-items: start;
}
.net-sidebar {
  position: sticky;
  top: calc(var(--header-h) + 16px);
  border-right: 1px solid var(--rule);
  padding-right: 12px;
  max-height: calc(100vh - var(--header-h) - 32px);
  overflow-y: auto;
}
.net-sidebar h3 {
  margin: 12px 0 6px;
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.net-sidebar h3:first-of-type { margin-top: 0; }
.net-sidebar ul { list-style: none; padding: 0; margin: 0 0 12px; }
.net-sidebar li { margin: 0; }
.net-sidebar a {
  display: block;
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--ink-mute);
  padding: 4px 8px;
  border-radius: 3px;
  text-decoration: none;
  border-left: 2px solid transparent;
}
.net-sidebar a:hover {
  background: var(--accent-soft);
  color: var(--accent);
}
.net-sidebar a.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  border-left-color: var(--accent);
}

.net-detail > .net-section { padding: 0; }
.net-section > header {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 14px;
}
.net-cat {
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
  background: var(--bg-2);
  padding: 2px 8px;
  border-radius: 3px;
}
.net-section h2 {
  font-family: var(--font-serif);
  font-size: 22px;
  margin: 0;
  color: var(--ink);
}
.net-anim {
  margin: 12px 0 16px;
  background: var(--bg-card);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 12px;
}
.net-anim .viz-canvas-wrap {
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 3px;
  overflow: hidden;
}
.net-anim .controls {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.net-body {
  font-family: var(--font-serif);
  font-size: 15px;
  line-height: 1.75;
  color: var(--ink);
}
.net-body p { margin: 6px 0; }
.net-body ul, .net-body ol { margin: 6px 0 6px 20px; }
.net-body li { margin: 3px 0; }
.net-body pre {
  background: var(--code-bg);
  border: 1px solid var(--rule);
  border-radius: 3px;
  padding: 10px 14px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  margin: 10px 0;
}
.net-body code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: rgba(0,0,0,0.05);
  padding: 1px 4px;
  border-radius: 2px;
}
.net-body h4 {
  font-family: var(--font-sans);
  font-size: 14px;
  margin: 14px 0 6px;
  color: var(--accent);
}
.net-body strong { color: var(--accent); }
.net-body table.kv {
  border-collapse: collapse;
  width: 100%;
  margin: 10px 0;
  font-family: var(--font-sans);
  font-size: 13px;
}
.net-body table.kv th,
.net-body table.kv td {
  border: 1px solid var(--rule);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
}
.net-body table.kv th { background: var(--bg-2); color: var(--ink); }

/* ============================================================
 * Tab: Algorithms (算法 tab)
 * ============================================================ */

.algorithms-page {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 32px;
  align-items: start;
}
.algo-sidebar {
  position: sticky;
  top: calc(var(--header-h) + 16px);
  border-right: 1px solid var(--rule);
  padding-right: 16px;
}
.algo-sidebar h3 {
  margin: 0 0 12px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.algo-sidebar ul { list-style: none; padding: 0; margin: 0 0 16px; }
.algo-sidebar li { margin: 0; }
.algo-sidebar a {
  display: block;
  padding: 6px 8px;
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--ink-mute);
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  margin-left: -2px;
}
.algo-sidebar a:hover {
  color: var(--ink);
  background: rgba(0,0,0,.03);
  text-decoration: none;
}
.algo-sidebar a.is-active {
  color: var(--accent);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  font-weight: 600;
}

.algo-detail {
  min-width: 0;
}
.algo-header {
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--rule);
}
.algo-header h1 {
  margin: 0 0 8px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.algo-meta {
  display: flex;
  gap: 18px;
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--ink-mute);
}
.algo-meta .pill {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.algo-meta .pill.difficulty-easy    { background: #e6f4ea; color: #137333; }
.algo-meta .pill.difficulty-medium  { background: #fef3c7; color: #92400e; }
.algo-meta .pill.difficulty-hard    { background: #fee2e2; color: #991b1b; }
.algo-meta .complexity { font-family: var(--font-mono); font-size: 12px; }

.algo-principle {
  font-family: var(--font-serif);
  font-size: 15px;
  line-height: 1.75;
  color: var(--ink);
  background: var(--bg-2);
  padding: 14px 18px;
  border-radius: 4px;
  margin: 12px 0 16px;
}
.algo-principle p { margin: 6px 0; }
.algo-principle ul, .algo-principle ol { margin: 6px 0 6px 20px; }
.algo-principle li { margin: 3px 0; }
.algo-principle code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: rgba(0,0,0,0.05);
  padding: 1px 4px;
  border-radius: 2px;
}
.algo-principle strong { color: var(--accent); }

.algo-stage {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-items: stretch;
}
.viz-panel, .code-panel {
  background: var(--bg-card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  overflow: hidden;
  min-height: 360px;
}
.viz-canvas-wrap {
  position: relative;
  width: 100%;
  height: 360px;
  background: var(--bg-2);
  display: flex;
  align-items: center;
  justify-content: center;
}
.viz-canvas-wrap canvas {
  display: block;
  width: 100%;
  height: 100%;
}
.viz-empty {
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--ink-faint);
}

.code-panel {
  display: flex;
  flex-direction: column;
}
.code-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--rule);
  font-family: var(--font-sans);
  font-size: 12px;
  color: var(--ink-mute);
  background: var(--bg-2);
}
.code-head .lang-tag {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--accent);
  margin-right: auto;
}
.code-body {
  flex: 1;
  overflow: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  background: var(--bg-2);
}
.code-body pre {
  margin: 0;
  padding: 12px 0 12px 0;
  white-space: pre;
  tab-size: 4;
}
.code-line {
  display: block;
  padding: 0 12px 0 56px;
  position: relative;
  color: var(--ink);
}
.code-line .ln {
  position: absolute;
  left: 0;
  width: 44px;
  text-align: right;
  color: var(--ink-faint);
  user-select: none;
  font-variant-numeric: tabular-nums;
}
.code-line.is-active {
  background: var(--code-line);
}
.code-line.is-active::before {
  content: "▶";
  position: absolute;
  left: 36px;
  color: var(--accent);
  font-size: 10px;
}
.code-line .tk-kw  { color: #1d6f5b; font-weight: 600; }
.code-line .tk-str { color: #b16207; }
.code-line .tk-num { color: #6f42c1; }
.code-line .tk-cmt { color: var(--ink-faint); font-style: italic; }
.code-line .tk-fn  { color: #0a4f7a; }
.code-line .tk-ty  { color: #0a4f7a; }

.controls {
  margin-top: 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  font-family: var(--font-sans);
  font-size: 13px;
}
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  background: var(--bg-card);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  transition: all .15s;
}
.btn:hover { background: var(--accent-soft); border-color: var(--accent-line); }
.btn.is-active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn .icon { font-size: 11px; }

.speed-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-mute);
}
.speed-control input[type=range] {
  width: 140px;
  accent-color: var(--accent);
}
.speed-control .speed-label {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink);
  min-width: 60px;
  text-align: right;
}

.algo-step-note {
  margin-top: 12px;
  padding: 10px 14px;
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink);
  min-height: 1.5em;
}

/* ============================================================
 * Placeholder tabs (数据结构 / OS / 网络 — Phase G 之前)
 * ============================================================ */

.placeholder-page {
  max-width: 720px;
  margin: 0 auto;
}
.placeholder-page h1 {
  margin: 0 0 16px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.placeholder-page .lead {
  font-size: 17px;
  line-height: 1.7;
  color: var(--ink-mute);
  margin-bottom: 24px;
}
.placeholder-page h2 {
  margin: 32px 0 12px;
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
}
.placeholder-page ul {
  padding-left: 20px;
  line-height: 1.8;
}
.placeholder-page li {
  margin-bottom: 4px;
}
.placeholder-page code {
  font-family: var(--font-mono);
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 13px;
}

/* ============================================================
 * Responsive
 * ============================================================ */
@media (max-width: 960px) {
  .topbar { padding: 0 16px; gap: 16px; }
  .brand-sub { display: none; }
  .nav { gap: 2px; }
  .nav-link { padding: 6px 10px; font-size: 13px; }
  .lang-btn { padding: 5px 8px; font-size: 11px; }
  .main { padding: 16px 16px 48px; }
  .algorithms-page { grid-template-columns: 1fr; gap: 16px; }
  .networking-layout { grid-template-columns: 1fr; gap: 16px; }
  .net-sidebar { position: static; max-height: none; border-right: none; border-bottom: 1px solid var(--rule); padding-right: 0; padding-bottom: 12px; }
  .algo-sidebar {
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--rule);
    padding: 0 0 12px;
  }
  .algo-stage { grid-template-columns: 1fr; }
  .algo-header h1 { font-size: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
  html { scroll-behavior: auto; }
}