:root {
  --bg: #0f1115;
  --panel: rgba(20, 23, 30, 0.92);
  --panel-solid: #14171e;
  --text: #e9ecf1;
  --text-dim: #aab3c0;
  --accent: #ffba08;
  --border: rgba(255, 255, 255, 0.10);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

#app { position: fixed; inset: 0; }

#map { position: absolute; inset: 0; background: #0b1d2a; }

/* ---- Title bar ---------------------------------------------------------- */
#titlebar {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 5;
  background: var(--panel);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
  max-width: 60%;
}
#titlebar h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.3px;
  font-weight: 700;
}
#titlebar .subtitle {
  margin: 4px 0 0;
  font-size: 12.5px;
  color: var(--text-dim);
}

/* ---- Info panel --------------------------------------------------------- */
#infopanel {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 5;
  width: 340px;
  max-width: calc(100% - 32px);
  max-height: calc(100% - 180px);
  overflow-y: auto;
  background: var(--panel);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.info-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.info-period {
  font-size: 12px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 600;
}
#info-title {
  margin: 2px 0 8px;
  font-size: 19px;
  line-height: 1.25;
}
.info-summary {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text);
}

.badge {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
  border: 1px solid transparent;
}
.badge[data-rank="0"] { background: rgba(208,0,0,0.18);  color: #ff6b6b; border-color: rgba(208,0,0,0.5); }
.badge[data-rank="1"] { background: rgba(232,131,30,0.18); color: #ffb15c; border-color: rgba(232,131,30,0.5); }
.badge[data-rank="2"] { background: rgba(255,186,8,0.16);  color: #ffd166; border-color: rgba(255,186,8,0.45); }
.badge[data-rank="3"] { background: rgba(106,170,100,0.18); color: #9ae6b4; border-color: rgba(106,170,100,0.5); }
.badge[data-rank="4"] { background: rgba(43,147,72,0.20);  color: #74e39b; border-color: rgba(43,147,72,0.55); }

.info-others { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.info-others h3, .info-sources summary {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
}
.info-others h3 { margin: 0 0 6px; }
.info-others ul { margin: 0; padding: 0; list-style: none; }
.info-others li {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; margin: 4px 0;
}
.info-others .dot {
  width: 12px; height: 12px; border-radius: 3px; flex: none;
  border: 1px solid rgba(255,255,255,0.25);
}

.info-sources { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.info-sources summary { cursor: pointer; user-select: none; margin-bottom: 6px; }
.info-sources .caveat {
  font-size: 12px; line-height: 1.5; color: var(--text-dim);
  background: rgba(255,186,8,0.07);
  border-left: 2px solid var(--accent);
  padding: 8px 10px; border-radius: 4px; margin: 8px 0;
}
.info-sources ul { margin: 6px 0 0; padding-left: 18px; }
.info-sources li { font-size: 11.5px; line-height: 1.5; color: var(--text-dim); margin: 4px 0; }

/* ---- Legend ------------------------------------------------------------- */
#legend {
  position: absolute;
  left: 16px;
  bottom: 110px;
  z-index: 5;
  background: var(--panel);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  font-size: 12px;
}
.legend-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; color: var(--text-dim); }
.swatch { width: 16px; height: 12px; border-radius: 3px; flex: none; }
.swatch-current { background: #7b2cbf; border: 1px solid #b277e8; }
.swatch-ghost { background: transparent; border: 1.5px dashed #cbb7e0; }
.swatch-today { background: transparent; border: 1.5px dotted #6fd3ff; }
.swatch-other { background: #2b9348; border: 1px solid #6ee79a; }
.legend-toggle {
  position: absolute; top: 6px; right: 6px;
  width: 20px; height: 20px; line-height: 16px;
  background: transparent; color: var(--text-dim);
  border: 1px solid var(--border); border-radius: 6px;
  cursor: pointer; font-size: 14px; padding: 0;
}
#legend.collapsed .legend-row { display: none; }
#legend.collapsed { padding: 6px; }

/* ---- Controls ----------------------------------------------------------- */
#controls {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 16px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--panel);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 18px;
  box-shadow: var(--shadow);
}
.control-buttons { display: flex; gap: 8px; flex: none; }
.ctrl {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
.ctrl:hover { background: rgba(255,255,255,0.10); }
.ctrl:active { transform: scale(0.94); }
.ctrl-play { background: var(--accent); color: #1a1205; border-color: var(--accent); font-weight: 700; }
.ctrl-play:hover { background: #ffc83d; }

.slider-wrap { flex: 1; min-width: 0; }
#era-slider {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
.timeline-ticks {
  position: relative;
  height: 18px;
  margin-top: 2px;
}
.tick {
  position: absolute;
  transform: translateX(-50%);
  top: 0;
  width: 1px;
  height: 6px;
  background: rgba(255,255,255,0.25);
}
.tick.active { background: var(--accent); height: 10px; width: 2px; }
.tick-label {
  position: absolute;
  top: 7px;
  transform: translateX(-50%);
  font-size: 9px;
  color: var(--text-dim);
  white-space: nowrap;
}
.timeline-current {
  margin-top: 2px;
  font-size: 12.5px;
  color: var(--text);
  text-align: center;
}
.timeline-current #tl-year { color: var(--accent); font-weight: 700; }

.speed-wrap { flex: none; display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.speed-wrap select {
  background: var(--panel-solid);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12px;
}

/* ---- MapLibre tweaks ---------------------------------------------------- */
.maplibregl-ctrl-attrib { font-size: 10px; }
.map-era-label {
  font-weight: 700;
  font-size: 13px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.9), 0 0 2px rgba(0,0,0,0.9);
  pointer-events: none;
  white-space: nowrap;
}

/* ---- Click popups ------------------------------------------------------- */
.maplibregl-popup.era-popup .maplibregl-popup-content {
  background: var(--panel-solid);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 28px 12px 14px;
  box-shadow: var(--shadow);
}
.maplibregl-popup.era-popup .maplibregl-popup-close-button {
  color: var(--text-dim); font-size: 16px; padding: 2px 6px;
}
.maplibregl-popup.era-popup .maplibregl-popup-close-button:hover { color: var(--text); background: transparent; }
/* hide the default tip (hard to theme cleanly across anchors) */
.maplibregl-popup.era-popup .maplibregl-popup-tip { display: none; }

.pp-period { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--accent); font-weight: 600; }
.pp-title { font-size: 15px; font-weight: 700; margin: 2px 0 6px; line-height: 1.2; }
.pp-badge {
  display: inline-block; font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .4px; padding: 2px 7px; border-radius: 999px;
  background: rgba(255,186,8,.16); color: #ffd166;
}
.pp-badge[data-rank="0"]{ background: rgba(208,0,0,.18);  color:#ff6b6b; }
.pp-badge[data-rank="1"]{ background: rgba(232,131,30,.18); color:#ffb15c; }
.pp-badge[data-rank="2"]{ background: rgba(255,186,8,.16);  color:#ffd166; }
.pp-badge[data-rank="3"]{ background: rgba(106,170,100,.18); color:#9ae6b4; }
.pp-badge[data-rank="4"]{ background: rgba(43,147,72,.2);   color:#74e39b; }
.pp-badge-other { background: rgba(43,147,72,.2); color:#74e39b; }
.pp-sum { font-size: 12.5px; line-height: 1.5; margin: 8px 0 0; color: var(--text); }
.pp-cap { font-size: 11.5px; color: var(--text-dim); margin-top: 6px; }

/* ---- "X years ago" ------------------------------------------------------ */
.info-ago {
  font-size: 12px;
  color: var(--text-dim);
  margin: -2px 0 4px;
}
.info-ago::before { content: "🕰 "; }
.tl-ago { color: var(--text-dim); font-size: 11.5px; }
.tl-ago::before { content: "· "; }

/* ---- Era image ---------------------------------------------------------- */
.info-figure { margin: 4px 0 12px; }
.info-figure img {
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  display: block;
  background: #0e2536;
}
.info-figure figcaption {
  font-size: 10.5px;
  line-height: 1.4;
  color: var(--text-dim);
  margin-top: 5px;
}
.info-figure figcaption a { color: #6fd3ff; text-decoration: none; }
.info-figure figcaption a:hover { text-decoration: underline; }

/* ---- Clickable sources -------------------------------------------------- */
#info-sources-list { list-style: none; padding-left: 0; }
#info-sources-list li { margin: 5px 0; }
.source-link {
  display: block;
  width: 100%;
  text-align: left;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 11.5px;
  line-height: 1.4;
  padding: 6px 8px 6px 22px;
  cursor: pointer;
  position: relative;
  transition: background 0.12s;
}
.source-link:hover { background: rgba(111,211,255,0.12); border-color: rgba(111,211,255,0.4); }
.source-link::before {
  content: "❝";
  position: absolute;
  left: 7px; top: 5px;
  color: var(--accent);
  font-size: 11px;
}

/* ---- Source popup (modal) ----------------------------------------------- */
.source-popup {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(5, 10, 16, 0.6);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.source-popup[hidden] { display: none; }
.sp-card {
  position: relative;
  max-width: 440px;
  width: 100%;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 22px;
  box-shadow: var(--shadow);
}
.sp-close {
  position: absolute; top: 8px; right: 10px;
  width: 28px; height: 28px;
  background: transparent; color: var(--text-dim);
  border: none; border-radius: 6px;
  font-size: 22px; line-height: 1; cursor: pointer;
}
.sp-close:hover { color: var(--text); background: rgba(255,255,255,0.06); }
.sp-label {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.6px;
  color: var(--accent); font-weight: 700; margin-bottom: 6px;
}
.sp-cite { font-size: 15px; font-weight: 600; line-height: 1.35; margin-bottom: 10px; padding-right: 20px; }
.sp-summary { font-size: 13px; line-height: 1.6; color: var(--text-dim); margin: 0 0 16px; }
.sp-link {
  display: inline-block;
  background: var(--accent); color: #1a1205;
  font-weight: 700; font-size: 12.5px;
  padding: 8px 14px; border-radius: 8px;
  text-decoration: none;
}
.sp-link:hover { background: #ffc83d; }
.sp-link[hidden] { display: none; }

/* ---- Responsive --------------------------------------------------------- */
@media (max-width: 720px) {
  #titlebar { max-width: calc(100% - 32px); }
  #infopanel {
    top: auto;
    bottom: 92px;
    right: 16px;
    left: 16px;
    width: auto;
    max-height: 42vh;
  }
  #legend { display: none; }
  #controls { flex-wrap: wrap; gap: 10px; }
  .speed-wrap { order: 3; }
  .slider-wrap { order: 2; flex-basis: 100%; }
}
