/* ============================================================
   万国编年 · 样式
   配色：米白纸底 + 棕褐墨线
   ============================================================ */

:root {
  --paper:      #fbf8f1;
  --paper-2:    #f4efe4;
  --paper-3:    #ebe4d5;
  --ink:        #352d20;
  --ink-2:      #6b5c46;
  --ink-3:      #9c8b72;
  --line:       #d8cdb8;
  --accent:     #a8543a;
  --accent-2:   #b8763f;
  --brown:      #8a7356;

  --topbar-h:   54px;
  --layer-h:   38px;
  --tl-h:       196px;

  --shadow-s:   0 1px 2px rgba(90, 72, 48, .10);
  --shadow-m:   0 2px 10px rgba(90, 72, 48, .13);
  --shadow-l:   0 6px 26px rgba(90, 72, 48, .18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans SC", sans-serif;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

button, select { font-family: inherit; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--paper);
}

/* 移动端地址栏收放会让 100% 与可见高度不等；支持 dvh 的浏览器优先用 dvh */
@supports (height: 100dvh) {
  html, body, #app { height: 100dvh; }
}

/* ---------------- 顶栏 ---------------- */

#topbar {
  flex: none;
  position: relative;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  background: linear-gradient(180deg, #fdfbf6 0%, #f6f1e6 100%);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-s);
  z-index: 1200;
}

#back-home {
  display: inline-flex;
  align-items: center;
  flex: none;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--ink-2);
  text-decoration: none;
  white-space: nowrap;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 3px;
}
#back-home:hover { color: var(--accent); border-color: var(--accent); background: var(--paper-3); }

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 3px;
  color: var(--ink);
}
.brand .sub {
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: 1px;
}

#year-badge {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 12px;
  background: var(--paper-3);
  border: 1px solid var(--line);
  border-radius: 3px;
  font-variant-numeric: tabular-nums;
}
#year-num {
  font-size: 17px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: .5px;
  min-width: 42px;
  text-align: center;
}
#year-era { font-size: 10.5px; color: var(--ink-2); }

.topbar-spacer { flex: 1; }

#view-tabs {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 3px;
  overflow: hidden;
  background: var(--paper);
}
#view-tabs button {
  border: 0;
  background: transparent;
  padding: 6px 16px;
  font-size: 12.5px;
  color: var(--ink-2);
  cursor: pointer;
  letter-spacing: 1px;
  transition: background .15s, color .15s;
}
#view-tabs button + button { border-left: 1px solid var(--line); }
#view-tabs button:hover { background: var(--paper-2); }
#view-tabs button.is-active {
  background: var(--brown);
  color: #fdfbf6;
}

#basemap-pick select {
  appearance: none;
  padding: 6px 26px 6px 12px;
  font-size: 12.5px;
  color: var(--ink-2);
  background: var(--paper) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6'><path d='M0 0l5 6 5-6z' fill='%236b5c46'/></svg>") no-repeat right 10px center;
  border: 1px solid var(--line);
  border-radius: 3px;
  cursor: pointer;
}
#basemap-pick select:focus { outline: 2px solid rgba(168, 84, 58, .28); outline-offset: 1px; }

/* ---------------- 图层弹层 ---------------- */

#layer-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--paper);
  font-size: 12.5px;
  color: var(--ink-2);
  cursor: pointer;
  letter-spacing: 1px;
  transition: background .15s, color .15s, border-color .15s;
}
#layer-btn:hover { background: var(--paper-2); }
#layer-btn .caret { font-size: 9px; opacity: .7; transition: transform .16s; }
#layer-btn[aria-expanded="true"] {
  background: #fff;
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}
#layer-btn[aria-expanded="true"] .caret { transform: rotate(180deg); }

#layer-pop {
  position: absolute;
  top: calc(var(--topbar-h) - 6px);
  right: 16px;
  width: 334px;
  padding: 12px 14px 13px;
  background: rgba(253, 251, 246, .985);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow-l);
  backdrop-filter: blur(8px);
  z-index: 1300;
  transition: opacity .14s, transform .14s;
}
#layer-pop.is-hidden {
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
  visibility: hidden;
}

.lp-group + .lp-group {
  margin-top: 9px;
  padding-top: 9px;
  border-top: 1px dashed var(--line);
}
.lp-head {
  font-size: 10.5px;
  font-weight: 700;
  color: var(--ink-3);
  letter-spacing: 2px;
  margin-bottom: 7px;
}
.lp-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.lp-empty { font-size: 11.5px; color: var(--ink-3); }

/* ---------------- 图层 chip ---------------- */

.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px 3px 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  font-size: 11.5px;
  color: var(--ink-2);
  cursor: pointer;
  white-space: nowrap;
  transition: background .14s, border-color .14s, color .14s;
  user-select: none;
}
.chip:hover { background: var(--paper-2); }
.chip .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: currentColor;
  opacity: .35;
  transition: opacity .14s;
}
.chip.is-on {
  background: #fff;
  border-color: var(--brown);
  color: var(--ink);
  box-shadow: var(--shadow-s);
}
.chip.is-on .dot { opacity: 1; }
.chip .num { font-size: 10px; color: var(--ink-3); font-variant-numeric: tabular-nums; }

/* ---------------- 全局搜索 ---------------- */

#search-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--paper);
  font-size: 12.5px;
  color: var(--ink-2);
  cursor: pointer;
  letter-spacing: 1px;
  transition: background .15s, color .15s, border-color .15s;
}
#search-btn svg { flex: none; opacity: .78; }
#search-btn:hover { background: var(--paper-2); }
#search-btn[aria-expanded="true"] {
  background: #fff;
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}
#search-btn[aria-expanded="true"] svg { opacity: 1; }

/* fixed 而不是 absolute：#search-pop 挂在 header 外面按视口定位，
   手机键盘弹出时不会跟着布局抖动 */
#search-pop {
  position: fixed;
  top: calc(var(--topbar-h) - 5px);
  left: 50%;
  margin-left: -240px;
  width: 480px;
  display: flex;
  flex-direction: column;
  max-height: min(462px, calc(100vh - var(--topbar-h) - 22px));
  max-height: min(462px, calc(100dvh - var(--topbar-h) - 22px));
  background: rgba(253, 251, 246, .99);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow-l);
  backdrop-filter: blur(10px);
  z-index: 1500;
  overflow: hidden;
  transition: opacity .14s, transform .14s;
}
#search-pop.is-hidden {
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
  visibility: hidden;
}

.sp-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.sp-ico { flex: none; color: var(--ink-3); }

#search-input {
  flex: 1;
  min-width: 0;
  padding: 2px 0;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 13.5px;
  color: var(--ink);
}
#search-input:focus { outline: none; }
#search-input::placeholder { color: var(--ink-3); }

#search-clear {
  flex: none;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--paper-3);
  color: var(--ink-2);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}
#search-clear:hover { background: var(--line); }
#search-clear[hidden] { display: none; }

#search-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px;
}

.sr-ul { margin: 0; padding: 0; list-style: none; }

.sr-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 4px;
  cursor: pointer;
  line-height: 1.35;
}
.sr-item.is-sel { background: var(--paper-3); }
.sr-item.is-sel .sr-t { color: var(--accent); }

.sr-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; opacity: .8; }
.sr-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 6px;
  overflow: hidden;
}
.sr-t {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sr-t mark {
  padding: 0 1px;
  border-radius: 2px;
  background: rgba(168, 84, 58, .16);
  color: var(--accent);
}
.sr-en {
  flex: 0 1 auto;
  font-size: 11px;
  color: var(--ink-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sr-layer { flex: none; font-size: 10.5px; color: var(--ink-3); letter-spacing: .5px; }
.sr-span {
  flex: none;
  min-width: 64px;
  text-align: right;
  font-size: 10.5px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}

.sr-empty {
  padding: 26px 16px 28px;
  text-align: center;
  font-size: 12px;
  line-height: 1.95;
  color: var(--ink-2);
}
.sr-empty .sr-hint { font-size: 11px; color: var(--ink-3); }

#search-foot {
  flex: none;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 12px 7px;
  border-top: 1px solid var(--line);
  background: var(--paper);
  font-size: 10.5px;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}
#search-foot:empty { display: none; }
#search-foot .sf-key { opacity: .85; }

/* ---------------- 主区 ---------------- */

#stage {
  flex: 1;
  position: relative;
  min-height: 0;
}

.view {
  position: absolute;
  inset: 0;
  display: none;
}
.view.is-active { display: flex; }

/* ---------------- 地图视角 ---------------- */

#view-map { flex-direction: column; }

#map {
  flex: 1;
  min-height: 0;
  background: #f7f3ea;
  outline: none;
}

/* Leaflet 控件改造 */
.leaflet-control-zoom { border: 0 !important; box-shadow: var(--shadow-m) !important; }
.leaflet-control-zoom a {
  width: 30px !important;
  height: 30px !important;
  line-height: 30px !important;
  background: var(--paper) !important;
  color: var(--ink-2) !important;
  border-color: var(--line) !important;
  font-size: 16px !important;
}
.leaflet-control-zoom a:hover { background: var(--paper-2) !important; color: var(--accent) !important; }
.leaflet-control-attribution {
  background: rgba(251, 248, 241, .82) !important;
  color: var(--ink-3) !important;
  font-size: 10px !important;
  padding: 2px 6px !important;
}
.leaflet-control-attribution a { color: var(--brown) !important; }
.leaflet-control-scale-line {
  background: rgba(251, 248, 241, .86) !important;
  border-color: var(--ink-3) !important;
  color: var(--ink-2) !important;
  font-size: 10px !important;
}

/* 点实体图标：一张 canvas 批量画，压在矢量图层之上、地图控件之下。
   尺寸由 icons.js 按地图尺寸 + DPR 设置；pointer-events:none 保证点击穿透 */
#icon-layer {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 620;
  pointer-events: none;
}

#year-corner {
  position: absolute;
  right: 14px;
  bottom: 44px;
  padding: 4px 9px;
  background: rgba(251, 248, 241, .88);
  border: 1px solid var(--line);
  border-radius: 3px;
  font-size: 10.5px;
  color: var(--ink-2);
  pointer-events: none;
  z-index: 800;
  font-variant-numeric: tabular-nums;
}
#year-corner:empty { display: none; }

#attribution-box {
  position: absolute;
  left: 14px;
  bottom: 12px;
  font-size: 10px;
  color: var(--ink-3);
  pointer-events: none;
  z-index: 800;
  line-height: 1.5;
}

/* 悬停提示 */
.leaflet-tooltip.polity-tip {
  background: rgba(53, 45, 32, .93);
  border: 0;
  border-radius: 3px;
  color: #fbf8f1;
  font-size: 11.5px;
  padding: 4px 8px;
  box-shadow: var(--shadow-m);
}
.leaflet-tooltip.polity-tip::before { display: none; }

/* 点/文化圈 tooltip：标题加粗、正文小字 */
.leaflet-tooltip.point-tip,
.leaflet-tooltip.culture-tip {
  background: rgba(53, 45, 32, .95);
  border: 0;
  border-radius: 3px;
  color: #fbf8f1;
  font-size: 12px;
  padding: 5px 9px;
  max-width: 240px;
  line-height: 1.5;
  box-shadow: var(--shadow-m);
}
.leaflet-tooltip.point-tip::before,
.leaflet-tooltip.culture-tip::before { display: none; }
.tip-en { opacity: .62; font-size: 10.5px; margin-left: 5px; font-style: italic; }
.tip-sum { opacity: .8; font-size: 11px; }
.tip-gap { opacity: .55; font-size: 10px; margin-left: 6px; }

/* 文化圈名字：轻描淡写在色块中间，不抢视线 */
.cul-label {
  pointer-events: none;
  background: none;
  border: 0;
}
.cul-label span {
  display: inline-block;
  transform: translate(-50%, -50%);
  font-size: 12px;
  letter-spacing: 3px;
  font-weight: 600;
  opacity: .8;
  white-space: nowrap;
  text-shadow: 0 0 3px rgba(250, 246, 238, 1), 0 0 7px rgba(250, 246, 238, 1), 0 0 12px rgba(250, 246, 238, .8);
}

/* ---------------- 底部泳道 ---------------- */

#timeline-wrap {
  flex: none;
  height: var(--tl-h);
  position: relative;
  background: linear-gradient(180deg, #f8f4ea 0%, #f2ece0 100%);
  border-top: 1px solid var(--line);
  user-select: none;
}
#app[data-view="timeline"] #timeline-wrap { display: none; }

#tl-head {
  height: 22px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  font-size: 10.5px;
  color: var(--ink-3);
}
#tl-range-label { font-variant-numeric: tabular-nums; color: var(--ink-2); }
.tl-tip { margin-left: 10px; opacity: .75; }

/* 顶部一条 5px 的拖拽带：向上拖把泳道拉高，勾了很多图层时方块不至于被压扁 */
#tl-resizer {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 5px;
  cursor: ns-resize;
  z-index: 15;
}
#tl-resizer::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 1px;
  width: 52px;
  height: 3px;
  transform: translateX(-50%);
  border-radius: 2px;
  background: var(--line);
}
#tl-resizer:hover::after, #tl-resizer.is-active::after { background: var(--accent); }

#tl-size-btn {
  margin-left: auto;
  height: 16px;
  padding: 0 7px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: 9.5px;
  cursor: pointer;
}
#tl-size-btn:hover { color: var(--accent); border-color: var(--accent); }

/* ---------------- 播放控制 ---------------- */

.pc {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: none;
}
.pc button {
  height: 18px;
  min-width: 22px;
  padding: 0 4px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: rgba(251, 248, 241, .92);
  color: var(--ink-2);
  font: inherit;
  font-size: 9.5px;
  line-height: 1;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: background .15s, border-color .15s, color .15s;
}
.pc button:hover { border-color: var(--accent); color: var(--accent); }
.pc button[data-act="play"] { font-size: 10px; letter-spacing: -.5px; }
.pc button[data-act="play"].is-on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fdfbf6;
}
.pc button[data-act="speed"].is-custom { color: var(--accent); border-color: var(--accent); }

.play-sub {
  font-size: 10.5px;
  color: var(--accent);
  margin-left: 6px;
  max-width: 42%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity .25s;
}
.play-sub.is-on { opacity: 1; }
.play-sub:empty { display: none; }

/* 播放中：游标匀速滑过去，看起来像在走，而不是一格格闪 */
#app.is-playing #tl-cursor,
#app.is-playing #gantt-cursor { transition: left .18s linear; }
/* 游标会一路扫过去，提示文字挡视线，播放时让位 */
#app.is-playing .tl-tip { opacity: 0; transition: opacity .2s; }
#app.is-playing #year-badge {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(168, 84, 58, .12);
}
#app.is-playing #year-num { color: var(--accent); }

#tl-panes {
  position: relative;
  margin: 0 14px;
  height: calc(var(--tl-h) - 22px - 26px);
  overflow: hidden;
}
#tl-panes:active { cursor: grabbing; }

.pane {
  position: relative;
  display: flex;
  border-bottom: 1px dashed rgba(216, 205, 184, .7);
}
.pane:last-child { border-bottom: 0; }

.pane-label {
  flex: none;
  width: 82px;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  overflow: hidden;
  font-size: 10.5px;
  color: var(--ink-2);
  letter-spacing: .5px;
  cursor: pointer;
  padding-right: 4px;
}
.pane-label:hover { color: var(--accent); }
.pane-label .dot { width: 6px; height: 6px; border-radius: 50%; flex: none; }
/* 图层名可省略，条数不可省略：空间不够时截断名字，保住角标 */
.pane-label .nm { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pane-label .n { color: var(--ink-3); font-size: 9.5px; flex: none; margin-left: auto; font-variant-numeric: tabular-nums; }
/* 有条目因高度/重要性没画出来时，角标变成「已画/总数」并染色，避免误以为全画了 */
.pane-label .n.is-part { color: var(--accent); opacity: .9; }

.pane-track { position: relative; flex: 1; }

/* 历史时期背景带：与泳道共用同一条时间轴，当前年份所在段高亮 */
.pane-epoch { border-bottom-style: solid; }
.grow-epoch { margin-bottom: 6px; }
.epoch-seg {
  position: absolute;
  top: 1px;
  bottom: 1px;
  border-left: 1px solid rgba(138, 122, 92, .55);
  background: rgba(138, 122, 92, .10);
  cursor: pointer;
  overflow: hidden;
  transition: background .15s;
}
.epoch-seg:first-child { border-left: 0; }
.epoch-seg:hover { background: rgba(138, 122, 92, .24); z-index: 4; }
.epoch-seg.is-now { background: rgba(168, 84, 58, .20); border-left-color: var(--accent); }
.epoch-seg span {
  position: absolute;
  left: 3px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 9px;
  line-height: 1;
  white-space: nowrap;
  color: var(--ink-2);
  pointer-events: none;
  text-shadow: 0 0 2px #fbf8f1;
}
.epoch-seg.is-now span { color: var(--accent); font-weight: 600; }

/* 事件类图层的刻度线，不占轨道高度 */
.tickline {
  position: absolute;
  top: 6px;
  bottom: 6px;
  width: 1px;
  background: var(--ink-3);
  cursor: pointer;
}
.tickline:hover { width: 2px; background: var(--accent); }

.blk {
  position: absolute;
  height: 11px;
  border-radius: 1.5px;
  border: 1px solid rgba(90, 72, 48, .30);
  cursor: pointer;
  overflow: hidden;
  transition: filter .12s;
}
.blk:hover { filter: brightness(1.14) saturate(1.1); z-index: 5; }

.blk-label {
  position: absolute;
  left: 3px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 9px;
  line-height: 1;
  font-weight: 500;
  color: #2b2418;
  white-space: nowrap;
  pointer-events: none;
  text-shadow: 0 0 2px #fbf8f1, 0 0 2px #fbf8f1, 0 0 3px #fbf8f1, 0 0 4px #fbf8f1;
}

#tl-axis {
  position: relative;
  height: 26px;
  margin: 0 14px 0 76px;
  border-top: 1px solid var(--line);
}
.tick {
  position: absolute;
  top: 0;
  height: 100%;
  border-left: 1px solid rgba(216, 205, 184, .9);
}
.tick span {
  position: absolute;
  left: 3px;
  top: 5px;
  font-size: 9.5px;
  color: var(--ink-3);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

#tl-cursor {
  position: absolute;
  top: 22px;
  bottom: 26px;
  width: 0;
  border-left: 1.5px solid var(--accent);
  pointer-events: none;
  z-index: 20;
}
#tl-cursor::before {
  content: '';
  position: absolute;
  top: -5px;
  left: -3.5px;
  border: 4px solid transparent;
  border-top-color: var(--accent);
}
.cursor-year {
  position: absolute;
  left: 50%;
  top: -22px;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fdfbf6;
  font-size: 10px;
  line-height: 1;
  padding: 3px 6px;
  border-radius: 2px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  letter-spacing: .3px;
  pointer-events: auto;
  cursor: ew-resize;
  user-select: none;
}

/* 游标本体是 1px 线（pointer-events:none），把手单独开可点击区 */
.cursor-grip {
  position: absolute;
  top: -9px;
  left: -8px;
  width: 16px;
  height: 16px;
  border: 2px solid var(--paper);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 4px rgba(90, 72, 48, .35);
  pointer-events: auto;
  cursor: ew-resize;
  z-index: 3;
}
.cursor-grip:hover { transform: scale(1.2); }
#app.is-scrubbing .cursor-grip { transform: scale(1.2); }

#tl-axis, #gantt-axis { cursor: ew-resize; }

/* ---------------- 时间轴视角（甘特） ---------------- */

#view-timeline { flex-direction: row; }

#gantt-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#gantt-head {
  flex: none;
  height: 26px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  font-size: 10.5px;
  color: var(--ink-3);
  border-bottom: 1px solid var(--line);
}
#gantt-body {
  flex: 1;
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 14px;
}
#gantt-axis {
  flex: none;
  height: 28px;
  position: relative;
  margin: 0 14px 0 90px;   /* 与 .grow 的 76px 标签栏对齐（14 + 76） */
  border-top: 1px solid var(--line);
}
#gantt-cursor {
  position: absolute;
  top: 26px;
  bottom: 28px;
  left: 0;
  width: 0;
  border-left: 1.5px solid var(--accent);
  pointer-events: none;
  z-index: 20;
}

.grow {
  position: relative;
  margin-bottom: 10px;
  padding-left: 92px;
}
/* 内容装得下时垂直居中（单层聚焦通常只有两三行，贴顶会显得页面没做完） */
#gantt-body.is-short {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.grow-label {
  position: absolute;
  left: 0;
  top: 0;
  width: 88px;
  white-space: nowrap;
  overflow: hidden;
  font-size: 11px;
  color: var(--ink-2);
  display: flex;
  align-items: center;
  gap: 4px;
  height: 18px;
}
.grow-label .dot { width: 7px; height: 7px; border-radius: 50%; }
.grow-label .n { font-size: 9.5px; color: var(--ink-3); }
.grow-track {
  position: relative;
  min-height: 18px;
}
.gbar-tick {
  position: absolute;
  top: 0;
  width: 1px;
  background: rgba(140, 50, 38, .45);
  cursor: pointer;
}
.gbar-tick:hover { width: 2px; background: rgba(140, 50, 38, .95); }

.gbar {
  position: absolute;
  height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(90, 72, 48, .32);
  cursor: pointer;
  overflow: hidden;
  transition: filter .12s;
}
.gbar:hover { filter: brightness(1.14); z-index: 5; }
.gbar-label {
  position: absolute;
  left: 4px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 9.5px;
  line-height: 1;
  color: #2b2418;
  white-space: nowrap;
  pointer-events: none;
  text-shadow: 0 0 2px #fbf8f1, 0 0 2px #fbf8f1, 0 0 3px #fbf8f1;
}

#gantt-side {
  flex: none;
  width: 210px;
  border-left: 1px solid var(--line);
  background: var(--paper-2);
  padding: 12px;
  overflow-y: auto;
}
#gantt-side h3 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 500;
  color: var(--ink-3);
  letter-spacing: 2px;
}
#gantt-layer-list {
  list-style: none;
  margin: 0 0 14px;
  padding: 0;
}
#gantt-layer-list li {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 6px;
  border-radius: 3px;
  font-size: 12px;
  color: var(--ink-2);
  cursor: pointer;
}
#gantt-layer-list li:hover { background: var(--paper-3); }
#gantt-layer-list li.is-on { color: var(--ink); background: #fff; box-shadow: var(--shadow-s); }
#gantt-layer-list li .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
#gantt-layer-list li .n { margin-left: auto; font-size: 10px; color: var(--ink-3); font-variant-numeric: tabular-nums; }

#gantt-stat {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-size: 10.5px;
  color: var(--ink-3);
  line-height: 1.7;
}

/* 单层聚焦：状态条 + 退出按钮。放在甘特头部右侧 —— 手机端侧栏是隐藏的 */
#focus-bar {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 1px 5px 1px 6px;
  border: 1px solid var(--accent-2);
  border-radius: 3px;
  background: rgba(184, 118, 63, .08);
  color: var(--accent-2);
  font-size: 10.5px;
  line-height: 1.5;
  white-space: nowrap;
}
#focus-bar[hidden] { display: none; }
#focus-bar .dot { flex: none; width: 6px; height: 6px; border-radius: 50%; }
#focus-bar .fb-nm { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; }
#focus-bar button {
  flex: none;
  padding: 0 5px;
  border: 1px solid currentColor;
  border-radius: 2px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
#focus-bar button:hover { background: var(--accent-2); color: var(--paper); }

/* ---------------- 载入 ---------------- */

#loading {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--paper);
  transition: opacity .45s ease, visibility .45s;
}
#loading.is-done { opacity: 0; visibility: hidden; pointer-events: none; }
.loading-inner { text-align: center; width: 220px; }
.loading-title {
  font-size: 15px;
  letter-spacing: 8px;
  color: var(--ink-2);
  margin-bottom: 16px;
  padding-left: 8px;
}
.loading-bar {
  height: 2px;
  background: var(--paper-3);
  border-radius: 1px;
  overflow: hidden;
}
.loading-bar i {
  display: block;
  height: 100%;
  width: 30%;
  background: var(--accent);
  animation: slide 1.15s ease-in-out infinite;
}
@keyframes slide {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}
.loading-step {
  margin-top: 12px;
  font-size: 11px;
  color: var(--ink-3);
  letter-spacing: .5px;
}

/* ---------------- 响应式 ---------------- */

@media (max-width: 900px) {
  :root { --topbar-h: 48px; --tl-h: 168px; }
  .brand .sub { display: none; }
  #year-num { font-size: 15px; }
  #view-tabs button { padding: 5px 11px; font-size: 12px; }
  #gantt-side { width: 168px; }
  #attribution-box { display: none; }
  .tl-tip { display: none; }
  #tl-size-btn { margin-left: 10px; }
}

/* ============================================================
   实体抽屉
   ============================================================ */

#drawer {
  position: fixed;
  top: var(--topbar-h);
  right: 0;
  bottom: 0;
  width: 420px;
  z-index: 1250;
  display: flex;
  background: linear-gradient(180deg, #fdfbf6 0%, #f8f4ea 100%);
  border-left: 1px solid var(--line);
  box-shadow: -10px 0 30px rgba(90, 72, 48, .13);
  transform: translateX(calc(100% + 12px));
  transition: transform .24s cubic-bezier(.22, .7, .3, 1);
}
#drawer.is-open { transform: translateX(0); }

/* 抽屉打开时地图与泳道让位，Leaflet 需要 invalidateSize 才会重算尺寸 */
#stage, #timeline-wrap { transition: margin-right .24s cubic-bezier(.22, .7, .3, 1); }
#app.drawer-open #stage,
#app.drawer-open #timeline-wrap { margin-right: 420px; }

#drawer-inner {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 0;
  padding: 16px 18px 0;
}

#drawer-head {
  flex: none;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
#drawer-titles { flex: 1; min-width: 0; }
#drawer-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 1.5px;
  color: var(--ink);
  line-height: 1.3;
}
#drawer-sub {
  margin-top: 3px;
  font-size: 11.5px;
  color: var(--ink-3);
  font-style: italic;
  letter-spacing: .3px;
}
#drawer-close {
  flex: none;
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--paper);
  color: var(--ink-2);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: background .14s, color .14s, border-color .14s;
}
#drawer-close:hover { background: var(--paper-3); color: var(--accent); border-color: var(--brown); }

#drawer-meta {
  flex: none;
  padding: 10px 0;
  border-bottom: 1px dashed var(--line);
}
#drawer-meta:empty { display: none; }

.dm-row {
  display: flex;
  gap: 8px;
  font-size: 11.5px;
  line-height: 1.9;
  color: var(--ink-2);
}
.dm-row + .dm-row { margin-top: 1px; }
.dm-k {
  flex: none;
  width: 40px;
  color: var(--ink-3);
  letter-spacing: 1px;
}
.dm-v { flex: 1; min-width: 0; }
/* 示意位置：与地图上的点状环呼应，提示这不是史实城址坐标 */
.dm-approx {
  display: inline-block;
  padding: 0 6px;
  border: 1px dashed var(--accent-2);
  border-radius: 3px;
  color: var(--accent-2);
  font-size: 11px;
  letter-spacing: .5px;
}
.dm-v .tag {
  display: inline-block;
  margin: 0 4px 3px 0;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-2);
  font-size: 10.5px;
  color: var(--ink-2);
}
.dm-span { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 600; }

#drawer-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 2px 20px;
  overscroll-behavior: contain;
}
#drawer-body::-webkit-scrollbar { width: 7px; }
#drawer-body::-webkit-scrollbar-thumb { background: rgba(138, 115, 86, .28); border-radius: 4px; }
#drawer-body::-webkit-scrollbar-track { background: transparent; }

#drawer-foot {
  flex: none;
  padding: 8px 0 10px;
  border-top: 1px solid var(--line);
  font-size: 10.5px;
  color: var(--ink-3);
  letter-spacing: .3px;
}
#drawer-foot:empty { display: none; }

/* 空状态 */
.drawer-empty {
  padding: 26px 4px;
  text-align: center;
  color: var(--ink-3);
  font-size: 12px;
  line-height: 2;
}
.drawer-empty .big { display: block; font-size: 26px; opacity: .25; margin-bottom: 6px; }

/* ---------------- Markdown 正文 ---------------- */

.md {
  font-size: 13px;
  line-height: 1.8;
  color: var(--ink);
  word-break: break-word;
}
.md > *:first-child { margin-top: 0; }
.md p { margin: 0 0 11px; }
.md h1, .md h2, .md h3, .md h4 {
  margin: 18px 0 9px;
  font-weight: 600;
  line-height: 1.45;
  color: var(--ink);
  letter-spacing: .5px;
}
.md h1 { font-size: 16px; }
.md h2 {
  font-size: 13.5px;
  padding-left: 8px;
  border-left: 3px solid var(--accent-2);
}
.md h3 { font-size: 13px; color: var(--ink-2); }
.md h4 { font-size: 12.5px; color: var(--ink-2); }
.md strong { font-weight: 600; color: #241d13; }
.md em { font-style: italic; color: var(--ink-2); }
.md ul, .md ol { margin: 0 0 11px; padding-left: 19px; }
.md li { margin: 0 0 5px; }
.md li::marker { color: var(--ink-3); }
.md blockquote {
  margin: 12px 0;
  padding: 7px 12px;
  background: var(--paper-2);
  border-left: 3px solid var(--line);
  color: var(--ink-2);
  font-size: 12.5px;
}
.md blockquote > *:last-child { margin-bottom: 0; }
.md a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px dotted rgba(168, 84, 58, .5);
}
.md a:hover { border-bottom-style: solid; }
.md code {
  padding: 1px 4px;
  background: var(--paper-3);
  border-radius: 2px;
  font-size: 11.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.md pre {
  margin: 12px 0;
  padding: 10px 12px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 3px;
  overflow-x: auto;
  font-size: 11.5px;
  line-height: 1.6;
}
.md pre code { padding: 0; background: none; }
.md hr { margin: 16px 0; border: 0; border-top: 1px dashed var(--line); }
.md table {
  width: 100%;
  margin: 12px 0;
  border-collapse: collapse;
  font-size: 12px;
}
.md th, .md td {
  padding: 5px 8px;
  border: 1px solid var(--line);
  text-align: left;
}
.md th { background: var(--paper-2); font-weight: 600; color: var(--ink-2); }
.md img { max-width: 100%; border-radius: 3px; }

/* ============================================================
   手机端（≤720px）
   --topbar-h 是抽屉 / 编辑器 / 图层弹层共同的定位基准，改这里即可联动。
   顶栏用伪元素强制折成两行，避免所有子项被 flex 压成竖排。
   ============================================================ */
@media (max-width: 720px) {
  :root { --topbar-h: 84px; --tl-h: 140px; }

  #app { overscroll-behavior: none; }

  #topbar {
    flex-wrap: wrap;
    align-content: center;
    /* 360px 是第一行的临界宽度：博客+品牌+年份+新建+图层 共 301px，
       加上 gap 与内边距刚好卡在 361 —— 差 1px 就会把「图层」挤到下一行，
       再连锁把底图挤到第三行，顶栏溢出到地图上 */
    gap: 6px 6px;
    padding: 5px 8px;
  }
  /* 强制换行的零高占位：order 小于它的在第一行，大于的在第二行 */
  #topbar::after { content: ''; order: 20; flex: 0 0 100%; height: 0; }
  #back-home     { order: 1; }
  .brand         { order: 2; }
  #year-badge    { order: 3; }
  /* 给 spacer 一个最小宽度：≤390px 时把搜索按钮顶到第二行，第一行不那么挤 */
  .topbar-spacer { order: 4; flex: 1 1 10px; }
  #new-btn       { order: 5; }
  #layer-btn     { order: 6; }
  /* 搜索放第二行首位：第一行已经有 博客/品牌/年份/新建/图层 五个控件，
     再加一个会把「图层」挤下去（390px 下总宽 427 > 390） */
  #search-btn    { order: 8; }
  #view-tabs     { order: 9; }
  #basemap-pick  { order: 10; }
  #edit-exit     { order: 11; }

  .brand h1 { font-size: 15px; letter-spacing: 1px; white-space: nowrap; }
  .brand .sub { display: none; }
  #year-badge { padding: 2px 8px; gap: 4px; white-space: nowrap; }
  #year-num { font-size: 15px; min-width: 38px; }
  #year-era { font-size: 10px; }
  #back-home { padding: 5px 9px; font-size: 12px; }
  #new-btn { width: 34px; height: 34px; font-size: 18px; }

  /* 手机只留图标：带「搜索」两个字会宽 34px，第二行还要塞视图切换与底图 */
  #search-btn { padding: 7px 9px; min-height: 34px; }
  #search-btn .s-txt { display: none; }
  #search-btn svg { width: 15px; height: 15px; opacity: .85; }

  #layer-btn { padding: 7px 11px; font-size: 12.5px; white-space: nowrap; min-height: 34px; }
  #edit-exit { height: 34px; padding: 0 8px; font-size: 11px; white-space: nowrap; }
  #view-tabs button { padding: 8px 14px; font-size: 12.5px; min-height: 34px; }
  #basemap-pick select { padding: 7px 24px 7px 9px; font-size: 12px; min-height: 34px; }

  /* 图层弹层：左右贴边（JS 在窄屏不再写内联 right），超高可滚 */
  #layer-pop {
    left: 8px;
    right: 8px;
    top: calc(var(--topbar-h) - 4px);
    width: auto;
    max-height: calc(100vh - var(--topbar-h) - 20px);
    max-height: calc(100dvh - var(--topbar-h) - 20px);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 11px 12px;
  }
  .lp-grid { gap: 7px; }
  .chip { padding: 6px 11px 6px 9px; font-size: 12px; }

  /* 搜索弹层：左右贴边，底部留一截让用户看得见「点外面能关」；
     英文名在 390px 下会把中文名挤没 */
  #search-pop {
    left: 8px;
    right: 8px;
    width: auto;
    margin-left: 0;
    max-height: calc(100vh - var(--topbar-h) - 96px);
    max-height: calc(100dvh - var(--topbar-h) - 96px);
  }
  #search-input { font-size: 16px; }  /* < 16px 时 iOS 会自动放大页面 */
  .sp-head { padding: 8px 11px; }
  .sr-item { padding: 8px; gap: 7px; }
  .sr-t { font-size: 13.5px; }
  .sr-en { display: none; }
  .sr-span { min-width: 0; }
  #search-foot .sf-key { display: none; }

  /* 抽屉：全宽，头部按钮放大 */
  #drawer { width: 100%; }
  #app.drawer-open #stage, #app.drawer-open #timeline-wrap { margin-right: 0; }
  #drawer-head { flex-wrap: wrap; gap: 6px; }
  #drawer-close { width: 34px; height: 34px; font-size: 20px; }

  /* 时间轴视角：侧栏与顶栏图层弹层重复，窄屏让位给甘特本身 */
  #gantt-side { display: none; }

  /* 甘特头部空间有限：时段文案（含「按住 Ctrl 滚轮」，触屏无意义）让位给聚焦条；
     游标上的年份标签会压住聚焦条，而顶部徽标已经有年份，直接去掉 */
  #gantt-range { display: none; }
  #gantt-cursor .cursor-year { display: none; }
  #gantt-head { padding: 0 8px; gap: 8px; }
  #focus-bar .fb-nm { max-width: 46vw; }

  /* 只放大控件，不动 #tl-panes / #tl-axis / .grow 的水平留白——游标位置按它们算 */
  #tl-head { padding: 0 8px; gap: 8px; }
  #tl-size-btn { height: 24px; padding: 0 9px; font-size: 10.5px; }
  .pc button { height: 24px; min-width: 28px; font-size: 11px; }
  /* 把手放大后不能压住年份标签：下移，并让标签叠在上层 */
  .cursor-grip { width: 26px; height: 26px; left: -13px; top: -4px; }
  .cursor-year { padding: 5px 8px; font-size: 11px; z-index: 4; }

  /* touch-action 只加时间轴相关元素：加到 #app / 地图容器会让 Leaflet 双指缩放失效 */
  #tl-axis, #gantt-axis, .cursor-grip, .cursor-year, #tl-resizer, #tl-panes { touch-action: none; }
  #gantt-body { touch-action: pan-y; }
}

/* 悬停提示（canvas 无图层级 tooltip，自己做一个） */
#hover-tip {
  position: fixed;
  z-index: 1400;
  pointer-events: none;
  padding: 4px 9px;
  background: rgba(53, 45, 32, .94);
  color: #fbf8f1;
  font-size: 12px;
  line-height: 1.5;
  border-radius: 3px;
  white-space: nowrap;
  opacity: 0;
  transform: translate(14px, -50%);
  transition: opacity .12s;
  box-shadow: var(--shadow-m);
}
#hover-tip.is-on { opacity: 1; }

/* 抽屉里的「跳到 X 年」提示 */
.dm-jump {
  margin-top: 8px;
  padding: 6px 10px;
  background: rgba(168, 84, 58, .08);
  border: 1px dashed rgba(168, 84, 58, .34);
  border-radius: 3px;
  font-size: 11.5px;
  color: var(--accent);
  cursor: pointer;
  transition: background .14s, border-color .14s;
}
.dm-jump:hover {
  background: rgba(168, 84, 58, .15);
  border-color: rgba(168, 84, 58, .6);
}
