:root {
  --bg: #f3efe6;
  --text: #2f2418;
  --muted: #6b5b4e;
  --primary: #8b6914;
  --primary-soft: #f5edde;
  --card: rgba(255, 252, 245, 0.96);
  --border: #d5c9b5;
  --graph-bg: linear-gradient(145deg, #1c1710 0%, #2f281c 48%, #3d3424 100%);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "PingFang SC", "Noto Sans SC", "Hiragino Sans GB", sans-serif;
  color: var(--text);
  background: var(--bg);
}

img { max-width: 100%; height: auto; }
button, input { font: inherit; }
a { color: inherit; }

/* —— Landing —— */
.landing-body {
  min-height: 100vh;
  background: linear-gradient(175deg, #0a0f1e 0%, #111d38 60%, #0a1226 100%);
  color: #fff;
}
#stars { position: fixed; inset: 0; pointer-events: none; }
.landing {
  position: relative; z-index: 1; max-width: 920px; margin: 0 auto;
  padding: calc(48px + var(--safe-top)) 20px calc(80px + var(--safe-bottom));
  text-align: center;
}
.landing .eyebrow { color: #f0c060; letter-spacing: .12em; }
.landing h1 { font-size: clamp(2rem, 7vw, 3.4rem); line-height: 1.2; margin: 8px 0; }
.landing h1 span {
  background: linear-gradient(135deg, #6cb4ff, #f0c060);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.landing .sub { color: rgba(255,255,255,.55); line-height: 1.7; }
.features {
  display: grid; grid-template-columns: repeat(auto-fit,minmax(220px,1fr));
  gap: 16px; margin: 48px 0; text-align: left;
}
.features article {
  background: rgba(255,255,255,.05); border: 1px solid rgba(255,255,255,.1);
  border-radius: 18px; padding: 20px;
}
.pipeline { margin-top: 40px; }
.pipeline-row { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin: 16px 0; }
.pipeline input {
  padding: 12px 16px; border-radius: 999px; border: 1px solid rgba(255,255,255,.2);
  background: rgba(255,255,255,.08); color: #fff; width: min(280px, 100%);
}
.stats { display: flex; gap: 28px 40px; justify-content: center; margin-top: 48px; flex-wrap: wrap; }
.stats strong { display: block; font-size: clamp(1.6rem, 5vw, 2rem); color: #f0c060; }
.stats span { color: rgba(255,255,255,.45); font-size: .85rem; }
footer { margin-top: 56px; color: rgba(255,255,255,.25); font-size: .8rem; }

.btn {
  border: none; border-radius: 999px; padding: 12px 24px; cursor: pointer; font-weight: 600;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn.primary { background: linear-gradient(135deg, #1a73e8, #4094f0); color: #fff; }

/* —— Explore / constellation —— */
.index-body {
  background:
    radial-gradient(1200px 400px at 10% -10%, rgba(201,162,39,.12), transparent 60%),
    radial-gradient(900px 360px at 90% 0%, rgba(47,111,237,.08), transparent 55%),
    var(--bg);
  min-height: 100vh;
  padding-bottom: var(--safe-bottom);
}
.topbar {
  display: flex; justify-content: space-between; gap: 16px;
  padding: calc(16px + var(--safe-top)) 20px 12px; align-items: flex-start; flex-wrap: wrap;
}
.topbar .eyebrow { margin: 6px 0 0; color: var(--primary); font-size: .8rem; letter-spacing: .08em; }
.topbar h1 { margin: 4px 0 0; font-size: clamp(1.35rem, 4vw, 1.9rem); }
.topbar-actions { display: flex; flex-direction: column; gap: 10px; align-items: stretch; min-width: min(100%, 320px); }
.search { display: flex; gap: 8px; width: 100%; }
.search input {
  flex: 1; min-width: 0; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: #fff;
}
.search button, .toolbar button {
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: #fff; cursor: pointer; white-space: nowrap;
}
.link-home {
  color: var(--primary); text-decoration: none; font-size: .9rem; display: inline-block;
}
.toolbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 20px 12px; gap: 12px; flex-wrap: wrap;
}
.tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.tabs .active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.range {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  color: var(--muted); font-size: .9rem;
}
.range input {
  width: 88px; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: #fff;
}
.presets {
  display: flex; gap: 8px; flex-wrap: wrap; width: 100%;
}
.presets button {
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: #fff; cursor: pointer; color: var(--muted); font-size: .82rem;
}
.presets button:active { background: var(--primary-soft); color: var(--primary); }

.stage {
  margin: 0 16px 12px; border-radius: 20px; overflow: hidden;
  border: 1px solid var(--border); background: #1c1710; position: relative;
}
.explore-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(140px, 200px);
  min-height: min(62vh, 560px);
}
.graph-main { position: relative; min-width: 0; min-height: inherit; }
.pane { min-height: min(62vh, 560px); position: relative; }
.pane.hidden { display: none !important; }
#timeline-canvas {
  width: 100%; height: 100%; min-height: min(62vh, 560px); display: block; background: var(--graph-bg);
}
#china-map { width: 100%; height: min(62vh, 560px); }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: rgba(15,23,42,.94); color: #fff; padding: 10px 12px;
  border-radius: 12px; font-size: 12px; max-width: min(280px, 78vw);
  box-shadow: 0 12px 28px rgba(0,0,0,.28);
}
.tooltip.hidden { display: none; }
.tooltip-row { display: flex; gap: 10px; align-items: center; }
.tooltip-row strong { font-size: 13px; }
.province-panel {
  border-left: 1px solid rgba(255,255,255,.08);
  background: rgba(0,0,0,.28); color: #fff; padding: 14px;
  font-size: 12px; overflow: auto;
}
.province-panel h3 { margin: 0 0 10px; font-size: .85rem; color: rgba(255,255,255,.8); }
.province-panel .prov-row {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; margin-bottom: 10px;
}
.province-panel .bar {
  grid-column: 1 / -1;
  height: 6px; background: #6cb4ff; border-radius: 999px; margin-top: 2px;
}
.muted-panel { color: rgba(255,255,255,.45); margin: 0; }
.chips {
  display: flex; gap: 8px; flex-wrap: wrap; padding: 4px 16px 20px;
}
.chips button {
  border: 1px solid var(--border); background: #fff; border-radius: 999px;
  padding: 6px 12px 6px 6px; cursor: pointer; display: inline-flex; align-items: center; gap: 8px;
}

/* avatar */
.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: inline-flex;
  align-items: center; justify-content: center; font-weight: 700; color: #fff;
  flex-shrink: 0; letter-spacing: 0; box-shadow: inset 0 0 0 2px rgba(255,255,255,.25);
  font-family: "Noto Serif SC", "Songti SC", serif;
}
.avatar-sm { width: 28px; height: 28px; font-size: .75rem; }
.avatar-md { width: 44px; height: 44px; font-size: 1rem; }
.avatar-lg { width: 72px; height: 72px; font-size: 1.6rem; }
.avatar-poet { background: linear-gradient(135deg, #3b82f6, #1d4ed8); }
.avatar-writer { background: linear-gradient(135deg, #8b5cf6, #6d28d9); }
.avatar-politician { background: linear-gradient(135deg, #b45309, #92400e); }
.avatar-thinker { background: linear-gradient(135deg, #0d9488, #0f766e); }
.avatar-scientist { background: linear-gradient(135deg, #059669, #047857); }
.avatar-military { background: linear-gradient(135deg, #dc2626, #991b1b); }
.avatar-artist { background: linear-gradient(135deg, #db2777, #9d174d); }
.avatar-other { background: linear-gradient(135deg, #64748b, #475569); }

.people-rail {
  display: none;
  gap: 10px;
  padding: 0 16px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
}
.people-rail .rail-card {
  min-width: 92px; text-decoration: none; text-align: center;
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 10px 8px; color: var(--text); scroll-snap-align: start;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.people-rail .name { display: block; font-size: .82rem; font-weight: 600; }
.people-rail .years { display: block; font-size: .68rem; color: var(--muted); }

.people-section { padding: 4px 16px 8px; }
.people-section-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px;
}
.people-section-head h2 { margin: 0; font-size: 1.05rem; }
.people-section-head p { margin: 0; color: var(--muted); font-size: .82rem; }
.people-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}
.person-card {
  display: flex; gap: 10px; align-items: center;
  text-decoration: none; color: var(--text);
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 10px 12px;
}
.person-card-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.person-card-body strong { font-size: .95rem; }
.person-card-body span { font-size: .75rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person-card-body .years { color: var(--primary); }

/* —— Person page —— */
.person-body { background: var(--bg); min-height: 100vh; padding-bottom: calc(16px + var(--safe-bottom)); }
.person-header {
  padding: calc(16px + var(--safe-top)) 20px 12px;
  display: flex; gap: 14px; align-items: flex-start; flex-wrap: wrap;
}
.person-header-main { display: flex; gap: 14px; align-items: center; flex: 1; min-width: 0; }
.person-header h1 { margin: 0 0 4px; font-size: clamp(1.4rem, 5vw, 2rem); }
.person-header p { margin: 0; color: var(--muted); }
.person-header blockquote { color: var(--primary); margin: 8px 0 0; }
.back { color: var(--primary); text-decoration: none; white-space: nowrap; }
.person-grid {
  display: grid; grid-template-columns: minmax(240px, 320px) minmax(0, 1fr) minmax(220px, 280px);
  gap: 16px; padding: 0 16px 16px; min-height: 420px;
}
.timeline-panel, .map-panel, .chat-panel, .markdown-panel {
  background: var(--card); border: 1px solid var(--border); border-radius: 18px; padding: 16px;
}
.timeline-panel .desc { color: var(--muted); font-size: .92rem; line-height: 1.55; }
.timeline-list { list-style: none; padding: 0; margin: 0; max-height: 460px; overflow: auto; }
.timeline-list li { padding: 10px 0; border-bottom: 1px solid rgba(0,0,0,.06); cursor: pointer; }
.timeline-list li.active { background: var(--primary-soft); border-radius: 10px; padding-left: 8px; }
#journey-map { width: 100%; height: min(48vh, 480px); min-height: 280px; border-radius: 12px; }
.chat-list { min-height: 220px; max-height: 360px; overflow: auto; font-size: 14px; }
.chat-list .msg { margin-bottom: 10px; }
.chat-list .user { color: var(--muted); }
.chat-list .bot { color: var(--text); }
#chat-form { display: flex; gap: 8px; margin-top: 8px; }
#chat-form input { flex: 1; min-width: 0; padding: 10px; border-radius: 10px; border: 1px solid var(--border); }
.markdown-panel { margin: 0 16px 16px; }
.markdown-panel pre { white-space: pre-wrap; font-family: inherit; line-height: 1.6; margin: 0; }

@media (max-width: 960px) {
  .person-grid { grid-template-columns: 1fr; }
  .explore-layout { grid-template-columns: 1fr; }
  .province-panel {
    border-left: none; border-top: 1px solid rgba(255,255,255,.08);
    max-height: 150px;
  }
  .pane, .graph-main, #timeline-canvas, #china-map { min-height: 360px; height: 360px; }
  .people-rail { display: flex; }
  .people-list { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
  .landing { padding: calc(36px + var(--safe-top)) 16px 64px; }
  .features { grid-template-columns: 1fr; margin: 32px 0; }
  .pipeline-row { flex-direction: column; align-items: stretch; }
  .pipeline input, .pipeline .btn { width: 100%; }
  .topbar { padding: calc(14px + var(--safe-top)) 14px 8px; }
  .topbar-actions { min-width: 100%; }
  .toolbar { padding: 0 14px 10px; }
  .stage { margin: 0 10px 10px; border-radius: 16px; }
  .pane, .graph-main, #timeline-canvas, #china-map { min-height: 360px; height: 360px; }
  .chips { padding: 0 12px 16px; }
  .people-rail { padding: 0 12px 12px; }
  .people-section { padding: 0 12px 8px; }
  .people-list { grid-template-columns: 1fr; }
  .person-header { padding: calc(14px + var(--safe-top)) 14px 10px; }
  .person-header-main { width: 100%; }
  .person-grid, .markdown-panel { padding-left: 10px; padding-right: 10px; }
  #journey-map { height: 300px; min-height: 260px; }
  .range input { width: 72px; }
  .avatar-lg { width: 56px; height: 56px; font-size: 1.25rem; }
}
