:root {
  --bg: #F3EDE2;
  --panel: #FAF6EF;
  --text: #2C2A28;
  --muted: #6E6A64;
  --line: #E0D6C8;
  --accent: #8B3A3A;
  --accent2: #8B3A3A;
  --water: #A8C0C4;
  --city-stroke: #3D4F56;
  --ink: #2C2A28;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "PingFang SC", "Noto Serif SC", "Songti SC", "Noto Sans SC", "Segoe UI", serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  height: 100%;
  overflow: hidden;
}
.top {
  grid-column: 2;
  grid-row: 1;
  display: flex; flex-wrap: wrap; gap: 12px;
  align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-shrink: 0;
  z-index: 2;
}
.top h1 { margin: 0; font-size: 1.2rem; font-weight: 650; letter-spacing: 0.06em; }
.sub { margin: 4px 0 0; color: var(--muted); font-size: 0.82rem; }
.layers { display: flex; gap: 6px; }
.layers button {
  border: 1px solid var(--line); background: transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 999px; cursor: pointer; font: inherit;
}
.layers button.active {
  background: var(--accent); color: #F3EDE2; border-color: transparent; font-weight: 600;
}
.layout {
  display: contents;
}
.sheet-handle { display: none; }


.panel {
  grid-column: 1;
  grid-row: 1 / -1;
  padding: 14px 16px 20px; overflow: auto;
  border-right: 1px solid var(--line); background: var(--panel);
  min-height: 0;
}
.map-wrap {
  position: relative;
  min-height: 0;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
}
@media (min-width: 861px) {
  .map-wrap {
    grid-column: 2;
    grid-row: 2;
  }
}

@media (max-width: 860px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100dvh;
    overflow: hidden;
  }
  .top {
    grid-column: 1;
    grid-row: 1;
    padding: 8px 12px;
    gap: 8px;
  }
  .top h1 { font-size: 1rem; }
  .sub { display: none; }
  .layers { width: 100%; justify-content: stretch; }
  .layers button {
    flex: 1;
    text-align: center;
    padding: 7px 8px;
    font-size: 0.82rem;
  }
  .map-wrap {
    grid-column: 1 !important;
    grid-row: 2 !important;
    width: 100%;
    min-height: 0;
    height: 100%;
  }
  .map-hud {
    bottom: calc(var(--sheet-peek, 140px) + 12px);
    max-width: min(280px, calc(100% - 24px));
  }
  .panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    grid-column: auto;
    grid-row: auto;
    z-index: 20;
    width: 100%;
    max-height: min(78dvh, 640px);
    border-right: none;
    border-bottom: none;
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 28px rgba(44, 42, 40, 0.14);
    padding: 0 14px 16px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    transform: translateY(calc(100% - var(--sheet-peek, 140px)));
    transition: transform 0.28s ease;
    touch-action: pan-y;
  }
  .panel.sheet-open {
    transform: translateY(0);
  }
  .sheet-handle {
    display: flex;
    position: sticky;
    top: 0;
    z-index: 8;
    width: 100%;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 0;
    padding: 10px 8px 8px;
    border: 0;
    background: var(--panel);
    color: var(--muted);
    font: inherit;
    font-size: 0.78rem;
    cursor: pointer;
  }
  .sheet-grip {
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background: #C4B8A8;
    display: inline-block;
  }
  .panel-head {
    max-height: min(42vh, 320px);
  }
  .panel-head:has(#cityPlayDock:not(.hidden)) {
    max-height: min(40vh, 300px);
  }
}
#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.hidden { display: none !important; }
label { display: block; color: var(--muted); font-size: 0.8rem; margin-bottom: 6px; }
.body, .hint, .meta { color: var(--muted); font-size: 0.9rem; line-height: 1.55; }
.list, .events { list-style: none; padding: 0; margin: 8px 0 0; }
.list li, .events li {
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  margin-bottom: 8px; cursor: pointer; background: #FFFaf3;
}
.list li:hover, .list li.active, .events li:hover, .events li.active {
  border-color: var(--accent); box-shadow: 0 0 0 1px rgba(139,58,58,.18);
}
.events li {
  padding: 7px 10px;
  margin-bottom: 6px;
  font-size: 0.82rem;
  line-height: 1.35;
  color: var(--ink);
}
.events .year { color: var(--accent2); font-size: 0.78rem; }
.events strong { display: inline; margin: 0; color: var(--ink); font-weight: 650; }
#citySelect {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: #FFFaf3; color: var(--text); font: inherit; margin-bottom: 10px;
}
.credit { margin-top: 18px; font-size: 0.72rem; color: var(--muted); opacity: 0.85; }
.map-hud {
  position: absolute; left: 12px; bottom: 72px; z-index: 2;
  max-width: min(300px, calc(100% - 24px));
  display: flex; flex-direction: column; gap: 6px;
  pointer-events: none;
}
.map-info {
  display: none;
  text-align: left;
  border: 1px solid var(--line);
  background: rgba(250,246,239,.96);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  color: var(--ink);
  cursor: pointer;
  pointer-events: auto;
  box-shadow: 0 1px 4px rgba(44,42,40,.06);
}
.map-info.visible { display: block; }
.map-info .mi-kicker {
  margin: 0 0 2px; font-size: 0.68rem; letter-spacing: 0.06em; color: var(--accent);
}
.map-info .mi-title {
  margin: 0; font-size: 0.88rem; font-weight: 650; line-height: 1.35; color: var(--ink);
}
.map-info .mi-sub {
  margin: 3px 0 0; font-size: 0.75rem; line-height: 1.4; color: var(--muted);
}
.legend {
  position: static;
  background: rgba(250,246,239,.94); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; font-size: 0.75rem; color: var(--muted);
  pointer-events: none;
}
.legend .row { display: flex; align-items: center; gap: 8px; margin: 3px 0; }
.legend i { width: 12px; height: 12px; border-radius: 2px; display: inline-block; flex-shrink: 0; }
.maplibregl-ctrl-logo { display: none !important; }

/* Jiangnan: map zoom + scrollbars */
.maplibregl-ctrl-group {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  overflow: visible !important;
}
.maplibregl-ctrl-group:not(:empty) {
  box-shadow: none !important;
}
.maplibregl-ctrl-group button {
  width: 32px !important;
  height: 32px !important;
  margin: 0 0 6px !important;
  background: var(--bg) !important;
  border: 1px solid var(--line) !important;
  border-radius: 999px !important;
  box-shadow: 0 1px 3px rgba(44, 42, 40, 0.08) !important;
}
.maplibregl-ctrl-group button + button {
  border-top: 1px solid var(--line) !important;
}
.maplibregl-ctrl-group button:hover {
  background: var(--panel) !important;
  border-color: var(--accent) !important;
}
.maplibregl-ctrl-group button:focus:focus-visible {
  box-shadow: 0 0 0 2px rgba(139, 58, 58, 0.25) !important;
}
.maplibregl-ctrl-group button .maplibregl-ctrl-icon {
  background-size: 18px 18px !important;
  opacity: 0.85;
  /* ink-tint the default SVG via CSS filter toward charcoal */
  filter: invert(14%) sepia(8%) saturate(500%) hue-rotate(350deg) brightness(90%);
}
.maplibregl-ctrl-group button:hover .maplibregl-ctrl-icon {
  opacity: 1;
  filter: invert(20%) sepia(40%) saturate(1200%) hue-rotate(330deg) brightness(85%);
}
.maplibregl-ctrl-attrib {
  background: rgba(250, 246, 239, 0.9) !important;
  color: var(--muted) !important;
  border-radius: 8px !important;
  padding: 2px 6px !important;
}

* {
  scrollbar-width: thin;
  scrollbar-color: #C4B8A8 transparent;
}
*::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: #C4B8A8;
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
*::-webkit-scrollbar-thumb:hover {
  background: var(--ink);
  border: 2px solid transparent;
  background-clip: padding-box;
}
.panel::-webkit-scrollbar-thumb {
  background: #C4B8A8;
  border-radius: 999px;
}

.panel-head {
  position: sticky;
  top: 0;
  z-index: 6;
  margin: 0 0 12px;
  padding: 0 0 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  max-height: min(52vh, 420px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  box-shadow: 0 6px 10px -8px rgba(44, 42, 40, 0.35);
}
.panel-head:has(#cityPlayDock:not(.hidden)) {
  max-height: min(48vh, 380px);
}
.story {
  margin: 0;
  padding: 10px 12px;
  background: #FFFaf3;
  border: 1px solid var(--line);
  border-radius: 12px;
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
}
.story-kicker { margin: 0 0 4px; font-size: 0.72rem; letter-spacing: 0.08em; color: var(--accent); }
.story-title {
  margin: 0 0 6px; font-size: 1.05rem; font-weight: 650;
  color: var(--ink); letter-spacing: 0.04em; line-height: 1.35;
}
.story-year { margin: 0 0 8px; font-size: 0.8rem; color: var(--accent); min-height: 1.1em; }
.story-body { margin: 0; font-size: 0.88rem; line-height: 1.55; color: var(--muted); }
.city-play-dock {
  position: static;
  margin: 0;
  padding: 0;
  background: transparent;
  border-bottom: none;
  flex: 0 0 auto;
}
.play-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.play-progress {
  font-size: 0.78rem;
  color: var(--muted);
  min-width: 4.5em;
}
.play-btn, .era-nav-btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--accent);
  border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 0.8rem; cursor: pointer;
}
.play-btn:hover, .play-btn.playing, .era-nav-btn:hover, .era-nav-btn.playing {
  background: var(--accent); color: #F3EDE2; border-color: transparent;
}
.era-nav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }

.timeline {
  list-style: none; margin: 10px 0 0; padding: 0 0 0 14px;
  border-left: 2px solid var(--line);
}
.timeline li {
  position: relative;
  margin: 0 0 10px;
  padding: 8px 10px 8px 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  background: transparent;
}
.timeline li::before {
  content: "";
  position: absolute;
  left: -19px; top: 14px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #C8BBA8;
  border: 2px solid var(--panel);
}
.timeline li.active {
  background: #FFFaf3;
  border-color: var(--accent);
}
.timeline li.active::before { background: var(--accent); }
.timeline .t-label { display: block; font-weight: 650; color: var(--ink); font-size: 0.9rem; }
.timeline .t-year { display: block; margin-top: 2px; font-size: 0.75rem; color: var(--accent); }
.era-progress {
  margin-top: 10px; height: 4px; background: var(--line); border-radius: 999px; overflow: hidden;
}
.era-progress-bar {
  height: 100%; width: 0%; background: var(--accent); transition: width .25s ease;
}

.city-chips {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin: 8px 0 10px;
}
.city-chip {
  text-align: left;
  border: 1px solid var(--line);
  background: #FFFaf3;
  border-radius: 10px;
  padding: 7px 10px;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.city-chip:hover, .city-chip.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(139,58,58,.18);
}
.city-chip.special-island { color: var(--accent); }
.city-chip.special-exit { color: var(--accent); }
.map-label {
  background: rgba(250,246,239,.92);
  border: 1px solid #E0D6C8;
  color: #2C2A28;
  font-size: 12px;
  line-height: 1.2;
  padding: 2px 6px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 1px 2px rgba(44,42,40,.08);
}
.map-label.mig {
  background: rgba(139,58,58,.92);
  color: #F3EDE2;
  border-color: transparent;
  font-weight: 650;
}
.map-label.branch-label {
  font-size: 11px;
  opacity: 0.92;
}

.follow-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.78rem; color: var(--muted); cursor: pointer; user-select: none;
}
.follow-toggle input { accent-color: var(--accent); }

.map-label.arc-label {
  font-size: 0.72rem;
  font-weight: 600;
  background: rgba(255, 248, 240, 0.92);
  border: 1px solid var(--accent, #8B3A3A);
  color: var(--accent, #8B3A3A);
  padding: 2px 7px;
  box-shadow: 0 1px 3px rgba(44, 42, 40, 0.12);
}

.time-axis {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 8px;
  z-index: 3;
  pointer-events: none;
}
.time-axis-inner {
  position: relative;
  height: 56px;
  padding: 4px 8px 16px;
  background: rgba(250, 246, 239, 0.94);
  border: 1px solid var(--line);
  border-radius: 10px;
  pointer-events: auto;
}
.time-axis-dynasties {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 6px;
  height: 12px;
  display: block;
}
.time-axis-dynasty {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 3px;
  opacity: 0.85;
  overflow: hidden;
  box-sizing: border-box;
  border-right: 1px solid rgba(250, 246, 239, 0.65);
}
.time-axis-dynasty span {
  display: block;
  font-size: 0.58rem;
  line-height: 12px;
  text-align: center;
  color: #3A342C;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
  padding: 0 1px;
  pointer-events: none;
}
.time-axis-rail {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 24px;
  height: 2px;
  background: #D8CDBE;
  border-radius: 999px;
}
.time-axis-ranges {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 20px;
  height: 10px;
}
.time-axis-range {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 4px;
  background: rgba(139, 58, 58, 0.22);
  border: 1px solid rgba(139, 58, 58, 0.35);
  cursor: pointer;
  box-sizing: border-box;
}
.time-axis-range:hover { background: rgba(139, 58, 58, 0.35); }
.time-axis-range.active {
  background: rgba(139, 58, 58, 0.62);
  border-color: var(--accent);
  height: 12px;
  top: -1px;
  z-index: 1;
}
.time-axis-ticks {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 0;
  bottom: 0;
  pointer-events: none;
}
.time-axis-tick {
  position: absolute;
  top: 22px;
  width: 0;
  transform: translateX(-50%);
}
.time-axis-tick::before {
  content: "";
  display: block;
  width: 1px;
  height: 6px;
  margin: 0 auto;
  background: #B7A996;
}
.time-axis-tick span {
  display: block;
  margin-top: 2px;
  font-size: 0.65rem;
  color: var(--muted);
  white-space: nowrap;
  transform: translateX(-50%);
  margin-left: 50%;
}
.time-axis-dots {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 18px;
  height: 16px;
}
.time-axis-dot {
  position: absolute;
  top: 50%;
  width: 10px;
  height: 10px;
  margin-left: -5px;
  margin-top: -5px;
  border-radius: 50%;
  background: #C8BBA8;
  border: 2px solid #FFFaf3;
  cursor: pointer;
  box-shadow: 0 0 0 1px rgba(44, 42, 40, 0.08);
}
.time-axis-dot:hover { background: var(--accent); }
.time-axis-dot.active {
  background: var(--accent);
  width: 12px;
  height: 12px;
  margin-left: -6px;
  margin-top: -6px;
  z-index: 2;
}
.time-axis-needle {
  position: absolute;
  top: 14px;
  width: 2px;
  height: 22px;
  margin-left: -1px;
  background: var(--accent);
  border-radius: 1px;
  box-shadow: 0 0 0 2px rgba(243, 237, 226, 0.85);
  transition: left 0.35s ease;
  pointer-events: none;
  z-index: 3;
}
.time-axis-needle::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -3px;
  width: 7px;
  height: 7px;
  margin-left: -3.5px;
  border-radius: 50%;
  background: var(--accent);
}

.map-label.sub-label {
  font-size: 10px;
  padding: 1px 5px;
  opacity: 0.9;
  background: rgba(255, 250, 243, 0.9);
}
.map-label.sub-label.dim {
  opacity: 0.35;
}
.island-mark {
  position: relative;
  width: 28px;
  height: 28px;
  pointer-events: none;
}
.island-mark .island-ring {
  position: absolute;
  inset: 0;
  border: 2px dashed var(--accent, #8B3A3A);
  border-radius: 50%;
  background: rgba(139, 58, 58, 0.08);
}
.island-mark .island-star {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent, #8B3A3A);
  font-size: 14px;
  line-height: 1;
  font-weight: 700;
}
.island-mark.soft .island-ring {
  border-style: dotted;
  border-color: #6E6A64;
  background: rgba(110, 106, 100, 0.06);
}
.island-mark.soft .island-star {
  color: #6E6A64;
  font-size: 11px;
}
.island-caption {
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translate(-50%, 2px);
  white-space: nowrap;
  font-size: 10px;
  font-weight: 650;
  color: var(--accent, #8B3A3A);
  background: rgba(250, 246, 239, 0.95);
  border: 1px solid var(--accent, #8B3A3A);
  border-radius: 6px;
  padding: 1px 5px;
  pointer-events: none;
}
.island-mark.soft .island-caption {
  color: var(--muted, #6E6A64);
  border-color: #C4B6A4;
}
.story-extra {
  margin: 8px 0 0;
  padding-top: 8px;
  border-top: 1px dashed var(--line);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--muted);
}
.story-extra strong {
  color: var(--ink);
  font-weight: 650;
}
.sub-list {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}
.sub-list li {
  margin: 0 0 6px;
  padding: 6px 8px;
  border-radius: 8px;
  background: #FFFaf3;
  border: 1px solid var(--line);
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--muted);
}
.sub-list li b {
  color: var(--ink);
  font-weight: 650;
}

.map-label.island-label {
  font-size: 10px;
  font-weight: 650;
  color: var(--accent, #8B3A3A);
  border: 1px dashed var(--accent, #8B3A3A);
  background: rgba(250, 246, 239, 0.95);
  padding: 1px 5px;
}
.map-label.island-label.emph {
  border-width: 1.5px;
}

.strata-wrap {
  margin-top: 8px;
}
.strata-label {
  font-size: 0.68rem;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 4px;
}
.strata-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.strata-chip {
  border: 1px solid var(--line);
  background: #FFFaf3;
  border-radius: 999px;
  padding: 3px 8px 3px 6px;
  font: inherit;
  font-size: 0.72rem;
  color: var(--ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  line-height: 1.2;
  max-width: 100%;
}
.strata-chip i {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
  border: 1px solid rgba(44,42,40,.12);
}
.strata-chip:hover {
  border-color: var(--accent);
}
.strata-chip.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(139,58,58,.22);
  background: #FFF6EC;
  font-weight: 650;
}
.strata-chip.dim {
  opacity: 0.45;
}

/* Local historical-prefecture debug overlay. Hidden unless ?overlay=jiangshan. */
.jiangshan-debug {
  pointer-events: auto;
  background: rgba(250, 246, 239, 0.96);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.45;
  max-height: 42vh;
  overflow: auto;
}
.jiangshan-debug[hidden],
.jiangshan-legend[hidden],
.jiangshan-years[hidden] { display: none !important; }
.jiangshan-status { margin: 6px 0 0; color: var(--ink); }
.jiangshan-license { margin: 6px 0 0; font-size: 0.68rem; }
.jiangshan-years { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.jiangshan-years button[aria-pressed="true"] {
  background: var(--accent);
  color: #F3EDE2;
  border-color: transparent;
}
.map-label.jiangshan-label {
  font-size: 11px;
  font-weight: 650;
  background: rgba(255, 250, 243, 0.94);
  pointer-events: none;
}
