:root {
  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --ink-1: #0b0b0b;
  --ink-2: #52514e;
  --ink-muted: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --ocean: #d9e7f4;
  --land: #f0ede4;
  --land-stroke: #c9c5b8;
  --japan: #ef9f96;
  --japan-stroke: #b5443a;
  --accent: #2a78d6;
  --c-politics: #2a78d6;
  --c-war: #e34948;
  --c-diplomacy: #1baf7a;
  --c-culture: #4a3aa7;
  --c-society: #eda100;
  --header-h: 52px;
  --timeline-h: 118px;
  --sidebar-w: 340px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;
  color: var(--ink-1);
  background: var(--page);
}

button { font-family: inherit; }

/* ---------- ヘッダー ---------- */
header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--hairline);
}

header h1 {
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
}
header h1 .sub { font-size: 12px; font-weight: 500; color: var(--ink-2); margin-left: 8px; }

.mode-toggle {
  display: flex;
  border: 1px solid var(--hairline);
  border-radius: 8px;
  overflow: hidden;
  margin-left: auto;
  flex: none;
}
.mode-toggle button {
  border: none;
  background: var(--surface-1);
  color: var(--ink-2);
  font-size: 13px;
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.mode-toggle button.active {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.list-toggle {
  display: none;
  border: 1px solid var(--hairline);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--ink-2);
  font-size: 13px;
  padding: 7px 10px;
  cursor: pointer;
  white-space: nowrap;
}

/* ---------- レイアウト ---------- */
main {
  position: relative;
  height: calc(100% - var(--header-h) - var(--timeline-h));
  display: flex;
}

#map-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  background: var(--page);
}

#map { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#map.dragging { cursor: grabbing; }
#map-wrap.fading #map-content { opacity: 0; }
#map-content { transition: opacity 0.22s ease; }

/* 地図の描画スタイル */
.sphere { fill: var(--ocean); stroke: var(--border); stroke-width: 1; }
.graticule { fill: none; stroke: rgba(11, 11, 11, 0.07); stroke-width: 0.5; }
.country { fill: var(--land); stroke: var(--land-stroke); stroke-width: 0.6; }
.country.japan { fill: var(--japan); stroke: var(--japan-stroke); stroke-width: 0.9; }

.arc {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-dasharray: 7 7;
  animation: dashflow 0.9s linear infinite;
  opacity: 0.9;
}
@keyframes dashflow { to { stroke-dashoffset: -14; } }

.marker { cursor: pointer; }
.marker circle {
  stroke: #fff;
  stroke-width: 2;
  paint-order: stroke;
}
.marker .ring {
  fill: none;
  stroke-width: 1;
  stroke: rgba(11, 11, 11, 0.35);
}
.marker.selected circle.dot { stroke: var(--ink-1); stroke-width: 2.5; }

.dyn-label, .jp-label {
  font-size: 11px;
  font-weight: 700;
  fill: var(--ink-2);
  paint-order: stroke;
  stroke: rgba(255, 255, 255, 0.85);
  stroke-width: 3;
  text-anchor: middle;
  pointer-events: none;
}
.dyn-label .region { font-weight: 500; fill: var(--ink-muted); }
.jp-label { fill: #8c2f26; }

/* ---------- 地図上のコントロール ---------- */
.map-controls {
  position: absolute;
  right: 12px;
  top: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 5;
}
.map-controls button {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  border: 1px solid var(--hairline);
  background: var(--surface-1);
  color: var(--ink-2);
  font-size: 17px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
.map-controls button:hover { background: #f1f0ea; }

/* 凡例（分類フィルター） */
.legend {
  position: absolute;
  left: 12px;
  top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  z-index: 5;
  max-width: 70%;
}
.legend button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--ink-2);
  background: var(--surface-1);
  border: 1px solid var(--hairline);
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.legend button .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
}
.legend button.off { opacity: 0.4; }
.legend button.off .dot { background: var(--ink-muted) !important; }

/* ポップアップ */
#popup {
  position: absolute;
  z-index: 8;
  max-width: 280px;
  background: var(--surface-1);
  border: 1px solid var(--hairline);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  padding: 10px 12px;
  font-size: 13px;
  pointer-events: auto;
  display: none;
}
#popup.show { display: block; }
#popup .p-year { font-weight: 700; font-size: 12px; color: var(--ink-2); }
#popup .p-year .chip {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  border-radius: 4px;
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: 1px;
}
#popup .p-title { font-weight: 700; margin: 3px 0 2px; line-height: 1.45; }
#popup .p-place { color: var(--ink-muted); font-size: 12px; }
#popup .p-close {
  position: absolute;
  right: 6px;
  top: 4px;
  border: none;
  background: none;
  font-size: 15px;
  color: var(--ink-muted);
  cursor: pointer;
  padding: 2px 5px;
}

/* ---------- サイドパネル ---------- */
#sidebar {
  width: var(--sidebar-w);
  flex: none;
  background: var(--surface-1);
  border-left: 1px solid var(--hairline);
  display: flex;
  flex-direction: column;
}
#sidebar .panel-head {
  padding: 10px 14px 8px;
  border-bottom: 1px solid var(--hairline);
}
#sidebar .panel-head h2 { font-size: 14px; }
#sidebar .panel-head .hint { font-size: 11px; color: var(--ink-muted); margin-top: 2px; }

#event-list { flex: 1; overflow-y: auto; padding: 6px 0; }

.ev-item {
  display: flex;
  gap: 8px;
  padding: 7px 12px 7px 9px;
  border-left: 4px solid transparent;
  cursor: pointer;
  font-size: 12.5px;
  line-height: 1.45;
}
.ev-item:hover { background: #f3f2ec; }
.ev-item.in-window { background: #eef4fc; }
.ev-item.selected { background: #dcebfb; }
.ev-item .ev-year {
  flex: none;
  width: 86px;
  font-weight: 700;
  color: var(--ink-2);
  font-size: 11.5px;
  padding-top: 1px;
}
.ev-item .ev-body { flex: 1; min-width: 0; }
.ev-item .ev-title { color: var(--ink-1); }
.ev-item .ev-side {
  display: inline-block;
  flex: none;
  font-size: 10px;
  font-weight: 700;
  border-radius: 4px;
  padding: 0 5px;
  margin-right: 5px;
  vertical-align: 1px;
}
.ev-side.japan { background: #fbe3e0; color: #a03428; }
.ev-side.world { background: #e2ecf9; color: #1c5cab; }

/* ---------- タイムライン ---------- */
#timeline {
  height: var(--timeline-h);
  background: var(--surface-1);
  border-top: 1px solid var(--hairline);
  padding: 8px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chapter-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chapter-tabs::-webkit-scrollbar { display: none; }
.chapter-tabs button {
  border: 1px solid var(--hairline);
  background: var(--surface-1);
  color: var(--ink-2);
  border-radius: 8px;
  font-size: 12.5px;
  padding: 5px 12px;
  cursor: pointer;
  white-space: nowrap;
  flex: none;
}
.chapter-tabs button.active {
  background: var(--ink-1);
  border-color: var(--ink-1);
  color: #fff;
  font-weight: 700;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 14px;
}
.year-display {
  flex: none;
  min-width: 168px;
  text-align: center;
}
.year-display .y-num { font-size: 21px; font-weight: 800; letter-spacing: 0.5px; }
.year-display .y-era { font-size: 12px; color: var(--ink-2); font-weight: 600; }

#year-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: var(--hairline);
  outline: none;
}
#year-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}
#year-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}
.slider-range { font-size: 11px; color: var(--ink-muted); flex: none; }

/* ---------- スマホ・タブレット ---------- */
@media (max-width: 820px) {
  :root { --timeline-h: 128px; --header-h: 48px; }
  header { padding: 0 10px; gap: 8px; }
  header h1 { font-size: 14.5px; }
  header h1 .sub { display: none; }
  .mode-toggle button { padding: 7px 12px; font-size: 15px; }
  .mode-toggle button .txt { display: none; }
  .list-toggle { display: block; }

  #sidebar {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: min(85vw, 360px);
    z-index: 20;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.18);
  }
  #sidebar.open { transform: translateX(0); }

  .legend { max-width: 55%; }
  .legend button { font-size: 11px; padding: 4px 8px; }
  .year-display { min-width: 128px; }
  .year-display .y-num { font-size: 17px; }
  .slider-range { display: none; }
}
