:root {
  color-scheme: light;
  --bg: #f6f3ec;
  --panel: #fffdf8;
  --ink: #2a2723;
  --muted: #8a8378;
  --line: #e4ddd0;
  --chip: #ece6da;
  --accent: #2f6f8f;
  --font: -apple-system, "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); }
body {
  -webkit-text-size-adjust: 100%;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
button { font: inherit; color: inherit; cursor: pointer; }

/* ───── 顶部 ───── */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 10px 8px;
  border-bottom: 1px solid var(--line);
}
.top h1 { margin: 0; font-size: 22px; letter-spacing: 1px; }
.sub { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.rotate-hint { display: none; margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; }
@media (orientation: portrait) and (pointer: coarse) { .rotate-hint { display: block; } }

/* ───── 时间轴舞台 ───── */
#stage { display: flex; flex-direction: column; }
.readout {
  display: flex; align-items: baseline; gap: 10px;
  padding-block: 8px 4px; min-height: 52px;
}
.ryear { font-size: 38px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.rregimes { flex: 1; min-width: 0; font-size: 14px; color: #5a5348; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  align-self: center; border: 0; background: var(--chip); border-radius: 10px;
  width: 38px; height: 38px; font-size: 20px; line-height: 1;
}

.canvas-wrap { position: relative; height: min(var(--content-h, 58vh), 64vh); min-height: 240px; }
.readout, .chips { flex: none; }
#tl {
  position: absolute; inset: 0;
  display: block; width: 100%; height: 100%;
  border-radius: 14px; background: #efeadf;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.zoom-ctrl { align-self: center; display: flex; gap: 6px; }
.zoom-ctrl button {
  width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 9px;
  background: rgba(255, 253, 248, .92); font-size: 17px; line-height: 1;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.zoom-ctrl button:last-child { font-size: 13px; }

.chips {
  display: flex; gap: 8px; overflow-x: auto; padding-block: 10px;
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; border-radius: 999px; padding: 7px 14px;
  background: var(--chip); font-size: 15px; white-space: nowrap;
}
.chip.active { background: var(--accent); color: #fff; }

/* ───── 列表 ───── */
#listSec { border-top: 1px solid var(--line); }
.tabs { display: flex; align-items: center; gap: 8px; padding-block: 10px; }
.tab { border: 0; border-radius: 999px; padding: 7px 18px; background: var(--chip); font-size: 15px; }
.tab.active { background: var(--ink); color: #fff; }
.list-title { margin-left: auto; color: var(--muted); font-size: 14px; }
.list { padding-block: 0 24px; display: grid; gap: 10px; }
@media (min-width: 760px) { .list { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); } }
.card {
  text-align: left; border: 0; width: 100%;
  background: var(--panel); border-radius: 14px; padding: 12px 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.card .row1 { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.tag { color: #fff; border-radius: 7px; padding: 2px 8px; font-size: 12px; }
.card .name { font-size: 19px; font-weight: 600; margin: 6px 0 2px; }
.card .name small { font-size: 14px; font-weight: 400; color: var(--muted); margin-left: 6px; }
.card .txt {
  margin: 0; font-size: 14px; color: #5a5348; line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.empty { color: var(--muted); padding: 20px 4px; }

/* ───── 横屏 / 全屏：时间轴占满屏幕 ───── */
@media (orientation: landscape) and (max-height: 540px) {
  body:not(.detail-open) { overflow: hidden; }
  .top, #listSec { display: none; }
  #stage {
    position: fixed; inset: 0; background: var(--bg);
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .readout { min-height: 0; padding-block: 4px 2px; }
  .ryear { font-size: 24px; }
  .canvas-wrap { flex: 1; min-height: 0; height: auto; }
  .chips { padding-block: 6px; }
  .chip { padding: 4px 12px; font-size: 14px; }
  #fsBtn { display: none; }
}
body.fs { overflow: hidden; }
body.fs .top, body.fs #listSec { display: none; }
body.fs #stage {
  position: fixed; inset: 0; background: var(--bg);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
body.fs .canvas-wrap { flex: 1; min-height: 0; height: auto; }

/* ───── 朝代详情 ───── */
.detail { position: fixed; inset: 0; z-index: 50; }
.detail[hidden] { display: none; }
.detail-scrim { position: absolute; inset: 0; background: rgba(30, 25, 20, .35); }
.sheet {
  position: absolute; left: 50%; top: 4vh; bottom: 0; transform: translateX(-50%);
  width: min(760px, 100%); background: var(--bg);
  border-radius: 18px 18px 0 0; overflow: hidden;
  display: flex; flex-direction: column;
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translate(-50%, 30px); opacity: 0; } }
@media (orientation: landscape) and (max-height: 540px) {
  .sheet { top: 0; border-radius: 0; width: 100%; }
}
.sheet-head {
  background: var(--c); color: #fff;
  padding-block: calc(10px + env(safe-area-inset-top, 0px)) 16px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.sheet-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.nav-btn, .close-btn {
  border: 0; background: rgba(255,255,255,.2); color: #fff;
  border-radius: 999px; padding: 6px 12px; font-size: 14px;
}
.nav-btn[disabled] { visibility: hidden; }
.close-btn { width: 36px; height: 36px; padding: 0; font-size: 16px; }
.sheet-head h2 { margin: 12px 0 4px; font-size: 32px; letter-spacing: 2px; }
.meta { margin: 0; font-size: 15px; opacity: .92; }
.sheet-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding-block: 16px calc(32px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 12px; font-size: 15px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.desc { font-size: 15px; line-height: 1.7; margin: 0 0 12px; }
.locate-btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 8px 14px; font-size: 14px; margin-bottom: 18px;
}
.sheet-body h3 { font-size: 17px; margin: 0 0 10px; }
.strip { display: flex; height: 30px; border-radius: 8px; overflow: hidden; gap: 1px; margin-bottom: 14px; background: var(--line); }
.strip button {
  border: 0; padding: 0; min-width: 3px; background: var(--c); color: #fff;
  font-size: 11px; overflow: hidden; white-space: nowrap; text-overflow: clip;
}
.strip button:nth-child(even) { filter: brightness(.86); }
.rulers { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.rulers li {
  display: grid; grid-template-columns: 30px 1fr auto; align-items: center; gap: 10px;
  background: var(--panel); border-radius: 12px; padding: 10px 14px;
  border-left: 4px solid var(--c);
  transition: background .3s;
}
.rulers li.hl { background: #fff1c9; }
.rulers .idx { color: var(--muted); font-variant-numeric: tabular-nums; }
.rulers .t { font-size: 17px; font-weight: 600; }
.rulers .p { font-size: 13px; color: var(--muted); margin-top: 2px; }
.rulers .yr { text-align: right; font-size: 14px; font-variant-numeric: tabular-nums; }
.rulers .yr small { display: block; color: var(--muted); font-size: 12px; }
.no-rulers { color: var(--muted); font-size: 14px; }

/* 并立政权组的甘特图 */
.back-btn { margin-top: 10px; }
.rulers li.gantt {
  grid-template-columns: 4.5em 1fr auto; cursor: pointer;
  border-left-color: var(--m);
}
.g-name { font-size: 16px; font-weight: 600; }
.g-track { position: relative; height: 14px; background: var(--chip); border-radius: 7px; }
.g-track span { position: absolute; top: 0; bottom: 0; background: var(--m); border-radius: 7px; }
.rulers li.g-axis { background: none; border: 0; padding-block: 0; grid-template-columns: 4.5em 1fr auto; }
.g-ticks { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.rulers li.gantt .yr, .g-axis > div:last-child { min-width: 6.5em; }
.rulers li.g-axis { border-left: 4px solid transparent; }

/* 窄屏竖屏：年份和按钮一行，当前政权放到第二行 */
@media (max-width: 600px) {
  .readout { flex-wrap: wrap; row-gap: 2px; }
  .ryear { margin-right: auto; }
  .rregimes { order: 3; flex-basis: 100%; white-space: normal; }
}
.g-name small { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }

/* ───── 搜索 ───── */
.top-right { display: flex; align-items: center; gap: 10px; }
.search-btn { border: 0; background: var(--chip); border-radius: 999px; padding: 7px 16px; font-size: 15px; }
.search {
  position: fixed; inset: 0; z-index: 40; background: var(--bg); overflow-y: auto;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.search-box { display: flex; gap: 10px; position: sticky; top: 0; background: var(--bg); padding-block: 4px 8px; }
.search-box input {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
}
.search-box input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.q-close { border: 0; background: none; color: var(--accent); font-size: 16px; }
.q-hint { margin: 4px 2px 10px; color: var(--muted); font-size: 13px; }
.search .list { padding-inline: 0; }
mark { background: #fbe3a0; color: inherit; border-radius: 3px; }
.search-btn { white-space: nowrap; flex: none; }
.top > div:first-child { min-width: 0; }
@media (max-width: 600px) { .top .rotate-hint { display: none; } }

/* 大事记 */
#dEventsSec { margin-bottom: 18px; }
.events { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.events li {
  display: grid; grid-template-columns: 4.6em 1fr; gap: 10px; align-items: baseline;
  background: var(--panel); border-radius: 12px; padding: 10px 14px;
  border-left: 4px solid var(--k); transition: background .3s;
}
.events li.hl { background: #fff1c9; }
.ev-y { font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; color: #5a5348; }
.ev-t { font-size: 16px; font-weight: 600; }
.ev-cat { margin-left: 8px; font-size: 11px; font-weight: 400; color: var(--k); }
.ev-d { font-size: 14px; line-height: 1.55; color: #5a5348; margin-top: 2px; }
#dOffices h3 { margin: 22px 0 8px; }
.office-note { margin: 0 0 10px; font-size: 13px; color: var(--muted); }

/* 边疆开关 */
.zoom-ctrl .fr-btn { width: auto; padding: 0 10px; font-size: 14px; }
.zoom-ctrl .fr-btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* 名人、文物、详情页快速跳转 */
#dPeopleSec, #dRelicsSec { margin-top: 22px; }
#dPeople li, #dRelics li { grid-template-columns: 5.2em 1fr; }
.ev-loc { font-size: 12px; color: var(--muted); margin-top: 4px; }
.jump { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.jump:empty { display: none; }
.jump button { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.tabs { flex-wrap: wrap; row-gap: 8px; }
.tab { padding: 7px 14px; }
.sheet-body h3 { scroll-margin-top: 8px; }
