/* ============================================================
   時空の地図帳 — スタイル
   カラーは dataviz 検証済みパレット（light/dark 両モード）
   ============================================================ */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --land: #e8e6de;
  --landline: #d2d0c5;
  --era-band: rgba(11, 11, 11, 0.035);
  --wash: rgba(11, 11, 11, 0.05);
  --cat-p: #2a78d6;  /* 国家・政治 */
  --cat-i: #e79016;  /* 宗教・思想（黄橙。戦争の赤と区別） */
  --cat-h: #1baf7a;  /* 人類・技術 */
  --river: #6ea3cd;  /* 河川 */
  --cat-j: #8a56c9;  /* 指導者 */
  --cat-t: #b352c9;  /* 思想家（紫寄り。戦争の赤と区別） */
  --cat-s: #0d94b5;  /* 科学者 */
  --cat-a: #b08d28;  /* 文化人 */
  --cat-w: #b8342e;  /* 戦争 */
  --cat-g: #8a5a44;  /* 美術（セピア茶） */
  --polity-line: rgba(11, 11, 11, 0.28);
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  --docu-sea: #d9e5eb;   /* 物語モード: 歴史地図帳風の淡い海 */
  --docu-land: #f2ecdc;  /* 物語モード: 羊皮紙調の陸 */
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --land: #2a2a27;
    --landline: #3a3a36;
    --era-band: rgba(255, 255, 255, 0.045);
    --wash: rgba(255, 255, 255, 0.07);
    --cat-p: #3987e5;
    --cat-i: #d3830f;
    --cat-h: #199e70;
    --river: #4a7fa5;
    --cat-j: #9a6fdd;
    --cat-t: #c46bd6;
    --cat-s: #2ba8c8;
    --cat-a: #c19f3a;
    --cat-w: #d0524c;
    --cat-g: #a97a63;
    --polity-line: rgba(255, 255, 255, 0.22);
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    --docu-sea: #101820;
    --docu-land: #2b2820;
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: var(--page);
  color: var(--ink);
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

/* ---------- ヘッダー ---------- */
#topbar {
  padding: 8px 14px 6px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px 14px;
  min-width: 0;
  overflow: hidden;   /* グリッド列がnowrapコンテンツにつられて広がるのを防ぐ */
}
.titlerow { display: flex; align-items: baseline; gap: 10px; white-space: nowrap; }
.titlerow h1 { font-size: 17px; font-weight: 700; }
.titlerow h1 .brandicon {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  margin-right: 7px;
  vertical-align: -4px;
}
.subtitle { font-size: 11.5px; color: var(--muted); }

/* コントロールは1行の横スクロール（flexではなく古典的な nowrap ブロック方式。
   タッチスクロールの確実さを最優先） */
#controls {
  flex: 1;
  min-width: 0;
  display: block;
  white-space: nowrap;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#controls::-webkit-scrollbar { display: none; }
#controls { cursor: grab; }
#controls.dragScrolling { cursor: grabbing; }
#controls.dragScrolling > * { pointer-events: none; }
.btngroup { display: inline-flex; gap: 4px; vertical-align: middle; margin-right: 8px; }
#controls > button { vertical-align: middle; margin-right: 8px; }

button {
  font: inherit;
  color: var(--ink-2);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
  min-height: 30px;
}
button:hover { background: var(--wash); }
button.on {
  color: var(--ink);
  border-color: var(--baseline);
  background: var(--wash);
  font-weight: 600;
}
@media (pointer: coarse) {
  button { min-height: 40px; padding: 7px 14px; font-size: 13.5px; }
}

/* カテゴリ凡例チップ（フィルタ兼用） */
.chip .cdot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: -1px;
}
.chip[data-cat="p"] .cdot { background: var(--cat-p); }
.chip[data-cat="i"] .cdot { background: var(--cat-i); }
.chip[data-cat="h"] .cdot { background: var(--cat-h); }
.chip[data-cat="w"] .cdot { background: var(--cat-w); }
.chip[data-cat="g"] .cdot { background: var(--cat-g); }
.chip[data-cat="j"] .cdot { background: var(--cat-j); }
.chip[data-cat="t"] .cdot { background: var(--cat-t); }
.chip[data-cat="s"] .cdot { background: var(--cat-s); }
.chip[data-cat="a"] .cdot { background: var(--cat-a); }
.chip.off { opacity: 0.4; }
.chip.off .cdot { background: var(--muted) !important; }

#listBtn { display: none; }

/* 地域フィルタのメニュー（ヘッダーのボタン下に開く） */
#regionMenu {
  position: fixed;
  z-index: 70;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 190px;
}
#regionMenu[hidden] { display: none; }
#regionMenu button {
  border: none;
  border-radius: 8px;
  text-align: left;
  min-height: 32px;
  font-size: 12.5px;
  padding: 5px 10px;
}
#regionMenu button.on { background: var(--wash); color: var(--ink); font-weight: 600; }
#regionMenu .rall { border-bottom: 1px solid var(--border); border-radius: 8px 8px 0 0; }
#regionMenu .rchip::before { content: '○ '; color: var(--muted); }
#regionMenu .rchip.on::before { content: '● '; color: var(--cat-p); }

/* ---------- メイン：地図 ＋ サイドパネル ---------- */
main {
  display: grid;
  grid-template-columns: 1fr var(--side-w, 340px);
  min-height: 0;
}

#mapwrap {
  position: relative;
  background: var(--surface);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;   /* ピンチ／ドラッグを自前で処理する */
  cursor: grab;
}
#mapwrap.dragging { cursor: grabbing; }
#map { width: 100%; height: 100%; display: block; }

#mapBtns {
  position: absolute;
  right: 10px;
  top: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 5;
}
#mapBtns button {
  width: 34px;
  min-height: 34px;
  padding: 0;
  border-radius: 8px;
  background: var(--surface);
  font-size: 15px;
  line-height: 1;
}

/* 言語切替（左上）。i18n.js が生成する */
#langBtn {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 6;
  min-height: 30px;
  padding: 4px 10px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  font-size: 11.5px;
  font-weight: 600;
}
@media (pointer: coarse) {
  #mapBtns button { width: 42px; min-height: 42px; }
}
/* 🗾など切替ボタンのオン状態をはっきり示す（青枠＋青地） */
#mapBtns button.on {
  background: color-mix(in srgb, var(--cat-p) 22%, var(--surface));
  border: 2px solid var(--cat-p);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--cat-p) 35%, transparent);
}

/* 🗾の三段階: 詳細モードには小さな「＋」を添える */
#mapBtns button { position: relative; }
#adminBtn.full::after {
  content: '＋';
  position: absolute;
  top: 0;
  right: 1px;
  font-size: 10px;
  font-weight: 700;
  color: var(--cat-p);
}

/* （陸地・勢力図・国名ラベルはCanvas描画に移行済み） */
#mapYear {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 11.5px;
  color: var(--ink-2);
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  padding: 3px 9px;
  border-radius: 6px;
  pointer-events: none;
  z-index: 5;
}

/* 地図カード（政体・河川の解説） */
#mapCard {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 34px;
  width: min(520px, calc(100% - 20px));
  max-height: 56%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 7;
}
#mapCard[hidden] { display: none; }
.mc-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 6px;
  flex: none;
}
.mc-title { font-size: 14.5px; font-weight: 700; line-height: 1.4; }
.mc-sub { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
#mcClose { border: none; color: var(--ink-2); min-height: 32px; min-width: 32px; padding: 2px 10px; flex: none; font-size: 15px; }
@media (pointer: coarse) {
  #mcClose { min-height: 44px; min-width: 44px; font-size: 17px; }
}
.mc-body {
  overflow-y: auto;
  padding: 0 12px 12px;
  min-height: 0;
  font-size: 13px;
  line-height: 1.85;
  color: var(--ink-2);
  white-space: pre-line;
}

/* プレイヘッド（波線）の年。常時表示し、再生中は大きくする */
#playYear {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(13px, 2.4vw, 19px);
  font-weight: 700;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  padding: 2px 12px;
  border-radius: 10px;
  pointer-events: none;
  white-space: nowrap;
  z-index: 6;
}
#playYear { cursor: pointer; pointer-events: auto; }
#playYear input {
  font: inherit;
  width: 8em;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
}
body.playing #playYear {
  font-size: clamp(20px, 4vw, 34px);
  font-weight: 800;
  padding: 2px 16px;
}

/* （地図のマーカー・勢力図・ラベルはCanvas描画に移行済み） */

/* ---------- サイドパネル ---------- */
#side {
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}
#sheetHandle { display: none; }

/* 閲覧履歴の戻る／進むバー */
#navBar {
  flex: none;
  display: flex;
  gap: 6px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
}
#navBar button { flex: 1; font-size: 12px; padding: 3px 0; }
#navBar button:disabled { opacity: .35; }
/* 横スワイプ(戻る/進む)を検出できるよう、縦パンのみブラウザに任せる */
#side, #mapCard { touch-action: pan-y; }
/* ボタン連打がダブルタップ拡大と誤認されてページが拡大されるのを防ぐ
   （manipulation = パンとピンチ拡大は許可、ダブルタップ拡大のみ無効） */
body { touch-action: manipulation; }

/* パネル幅の調整ハンドル（左端）。タッチ・マウス両対応 */
#sideResizer {
  position: absolute;
  left: -5px;
  top: 0;
  bottom: 0;
  width: 10px;
  cursor: col-resize;
  touch-action: none;
  z-index: 30;
}
#sideResizer:hover { background: color-mix(in srgb, var(--cat-p) 20%, transparent); }

/* 詳細と出来事リストの高さ調整ハンドル */
#panelSplit {
  flex: none;
  height: 12px;
  cursor: row-resize;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--border);
}
#panelSplit::before {
  content: '';
  width: 38px;
  height: 3px;
  border-radius: 2px;
  background: var(--border);
}
@media (max-width: 820px) {
  #sideResizer { display: none; }
}

/* 物語のナレーションカードをパネルに組み込んだとき（デスクトップ） */
#docuCard.docked {
  position: relative;
  left: auto;
  bottom: auto;
  transform: none;
  width: auto;
  max-height: 56%;
  flex: none;
  margin: 0;
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  box-shadow: none;
  backdrop-filter: none;
  z-index: auto;
}

/* 地図カードをパネルに組み込んだとき（デスクトップ） */
#mapCard.docked {
  position: relative;
  left: auto;
  bottom: auto;
  transform: none;
  width: auto;
  max-width: none;
  max-height: 44%;
  flex: none;
  margin: 0;
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  box-shadow: none;
  z-index: auto;
}

#detail {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  overflow-y: auto;
  flex: none;
  max-height: 46%;
}
#detail .bigicon { font-size: 26px; line-height: 1; }
#detail h2 { font-size: 15.5px; margin: 6px 0 4px; }
#detail .meta { font-size: 12px; color: var(--ink-2); display: flex; flex-wrap: wrap; gap: 4px 10px; margin-bottom: 8px; }
#detail .meta .catname { font-weight: 600; }
#detail .meta .cdot {
  display: inline-block; width: 9px; height: 9px; border-radius: 50%;
  margin-right: 4px; vertical-align: -0.5px;
}
#detail p { font-size: 13px; line-height: 1.75; color: var(--ink-2); }
#detail p strong { color: var(--ink); }
#detail .hint { font-size: 12.5px; line-height: 1.8; color: var(--ink-2); }
#detail .hint b { color: var(--ink); }

/* ストーリーモード */
.storycard {
  display: flex;
  align-items: center;
  gap: 10px;
  width: calc(100% - 8px);
  margin: 6px 4px;
  text-align: left;
  border-radius: 12px;
  padding: 10px 12px;
}
.storycard .sicon { font-size: 22px; flex: none; }
.storycard .sbody { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.storycard .sdesc { font-size: 11.5px; color: var(--muted); white-space: normal; }
.storycard .scount { flex: none; font-size: 11px; color: var(--muted); }
.storyhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}
.storyhead .storyprog { color: var(--muted); font-size: 11.5px; margin-left: 8px; }
.storyhead button { border: none; min-height: 28px; padding: 2px 8px; color: var(--muted); }
.storynote {
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--ink-2);
  border-left: 3px solid var(--baseline);
  padding: 2px 0 2px 10px;
  margin-bottom: 10px;
}
.storybar { display: flex; gap: 8px; margin-top: 12px; }
.storybar button { flex: 1; }
.storybar button:disabled { opacity: 0.35; cursor: default; }

#listHead {
  padding: 8px 16px 6px;
  font-size: 11.5px;
  color: var(--muted);
  flex: none;
}
#searchBox {
  display: block;
  width: 100%;
  font: inherit;
  font-size: 12.5px;
  color: var(--ink);
  background: var(--wash);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  margin-bottom: 6px;
  outline: none;
}
#searchBox:focus { border-color: var(--baseline); }
.noresult { padding: 14px 16px; font-size: 12.5px; color: var(--muted); }
.bandtext { white-space: pre-line; }

/* 「もっと詳しく」（詳説解説の折りたたみ） */
.ddbox {
  margin: 10px 0 4px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--wash);
}
.ddbox summary {
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  padding: 8px 12px;
  user-select: none;
}
.ddbox p {
  padding: 0 12px 10px;
  font-size: 13px;
  line-height: 1.85;
  color: var(--ink-2);
  white-space: pre-line;   /* 長文詳説の段落（\n）を活かす */
}

/* 詳細パネルの「⤢ 大きく」ボタン */
#detail { position: relative; }
.expandbtn {
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 11.5px;
  min-height: 26px;
  padding: 3px 9px;
  color: var(--muted);
}

/* 大きく表示（読み物リーダー） */
#readerOverlay {
  position: fixed;
  inset: 0;
  z-index: 95;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
#readerOverlay[hidden] { display: none; }
#readerBox {
  position: relative;
  background: var(--page);
  border-radius: 16px;
  box-shadow: var(--shadow);
  width: min(760px, 100%);
  max-height: 94vh;
  display: flex;
  flex-direction: column;
}
#readerClose {
  position: absolute;
  top: 8px;
  right: 10px;
  border: none;
  font-size: 17px;
  color: var(--muted);
  z-index: 2;
}
#readerBody {
  overflow-y: auto;
  padding: 22px 26px 30px;
  min-height: 0;
}
#readerBody h2 { font-size: 20px; margin: 8px 0 6px; }
#readerBody .meta { font-size: 13px; color: var(--ink-2); display: flex; flex-wrap: wrap; gap: 4px 12px; margin-bottom: 12px; }
#readerBody .meta .cdot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 4px; }
.rd-icon { font-size: 34px; line-height: 1; }
.rd-portrait { float: right; margin: 0 0 10px 16px; text-align: center; }
.rd-portrait img { width: 140px; border-radius: 12px; border: 1px solid var(--border); display: block; }
.rd-portrait a { font-size: 10px; color: var(--muted); text-decoration: none; }
.rd-lead { font-size: 14.5px; line-height: 1.85; color: var(--ink); font-weight: 500; margin-bottom: 12px; }
.rd-dd {
  font-size: 14px;
  line-height: 1.95;
  color: var(--ink-2);
  white-space: pre-line;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
@media (max-width: 820px) {
  #readerOverlay { padding: 8px; }
  #readerBody { padding: 18px 16px 26px; }
  .rd-portrait img { width: 104px; }
}

/* 人物の肖像（Wikipediaサムネイル） */
.portraitbox { float: right; margin: 0 0 8px 12px; text-align: center; max-width: 96px; }
.portraitbox img { width: 92px; border-radius: 10px; display: block; border: 1px solid var(--border); }
.portraitbox a { font-size: 9.5px; color: var(--muted); text-decoration: none; }

/* つながりチップ（きっかけ／その後の流れ） */
.relwrap { margin-top: 12px; }
.rellabel {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  margin: 10px 0 5px;
}
.relchip {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  text-align: left;
  border-radius: 10px;
  padding: 7px 10px;
  margin-bottom: 5px;
  font-size: 12.5px;
}
.relchip .re { flex: none; }
.relchip .rt { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
.relchip .ry { flex: none; font-size: 11px; color: var(--muted); }
.relchip[data-c="p"] { border-left: 3px solid var(--cat-p); }
.relchip[data-c="i"] { border-left: 3px solid var(--cat-i); }
.relchip[data-c="h"] { border-left: 3px solid var(--cat-h); }
.relchip[data-c="j"] { border-left: 3px solid var(--cat-j); }
.relchip[data-c="t"] { border-left: 3px solid var(--cat-t); }
.relchip[data-c="s"] { border-left: 3px solid var(--cat-s); }
.relchip[data-c="a"] { border-left: 3px solid var(--cat-a); }
#evList { overflow-y: auto; flex: 1; padding-bottom: 8px; }
.evrow {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  border: none;
  border-radius: 0;
  padding: 7px 16px;
  min-height: 34px;
  font-size: 12.5px;
  color: var(--ink);
}
.evrow:hover { background: var(--wash); }
.evrow.active { background: var(--wash); font-weight: 600; }
.evrow .cdot { flex: none; width: 9px; height: 9px; border-radius: 50%; }
.evrow[data-c="p"] .cdot { background: var(--cat-p); }
.evrow[data-c="i"] .cdot { background: var(--cat-i); }
.evrow[data-c="h"] .cdot { background: var(--cat-h); }
.evrow[data-c="j"] .cdot { background: var(--cat-j); }
.evrow[data-c="t"] .cdot { background: var(--cat-t); }
.evrow[data-c="s"] .cdot { background: var(--cat-s); }
.evrow[data-c="a"] .cdot { background: var(--cat-a); }
.evrow .yr { flex: none; color: var(--muted); font-size: 11.5px; min-width: 4.6em; }
.evrow .ttl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- タイムライン ---------- */
#timelinewrap {
  position: relative;
  border-top: 1px solid var(--border);
  background: var(--page);
  display: flex;
  flex-direction: column;
}
/* 王朝・文明の帯年表（📜ボタンで開閉。高さはJSが行数に合わせて設定） */
#bands {
  display: none;
  width: 100%;
  height: 0;
  flex: none;
  border-bottom: 1px solid var(--border);
}
#timelinewrap.withbands #bands { display: block; }
#timeline {
  width: 100%;
  flex: none;
  height: 190px;
  display: block;
  touch-action: none;
  cursor: grab;
}
#timeline.dragging { cursor: grabbing; }
#zoomBtns {
  position: absolute;
  right: 10px;
  bottom: 30px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 5;
}
/* 時間の再生（左下） */
#playCtrl {
  position: absolute;
  left: 10px;
  bottom: 30px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 5;
}
#playCtrl button {
  min-width: 40px;
  min-height: 40px;
  padding: 0 8px;
  border-radius: 8px;
  background: var(--surface);
  font-size: 15px;
  line-height: 1;
}
#playCtrl #speedBtn { min-height: 28px; font-size: 12px; }
#playCtrl #popupBtn { min-height: 28px; font-size: 13px; }
#playCtrl #popupBtn:not(.on) { opacity: 0.4; }
#playBtn.on {
  background: var(--cat-p);
  color: #fff;
  border-color: var(--cat-p);
}
/* 再生速度メニュー（速度ボタンの上に開く） */
#speedMenu {
  position: absolute;
  left: 0;
  bottom: calc(100% + 6px);
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  box-shadow: var(--shadow);
  z-index: 50;
}
#speedMenu[hidden] { display: none; }
#speedMenu button {
  border: none;
  border-radius: 6px;
  min-height: 30px;
  padding: 4px 12px;
  font-size: 12px;
  text-align: right;
}
#speedMenu button.on { background: var(--wash); font-weight: 600; color: var(--ink); }
@media (pointer: coarse) {
  #playCtrl button { min-width: 46px; min-height: 46px; font-size: 17px; }
  #playCtrl #speedBtn { min-height: 32px; font-size: 12.5px; }
  #speedMenu button { min-height: 38px; font-size: 13px; }
}
#zoomBtns button {
  width: 34px;
  min-height: 34px;
  padding: 0;
  border-radius: 8px;
  background: var(--surface);
  font-size: 16px;
  line-height: 1;
}
@media (pointer: coarse) {
  #zoomBtns button { width: 42px; min-height: 42px; }
}
#rangeLabel {
  position: absolute;
  top: 6px;
  right: 12px;
  font-size: 11.5px;
  color: var(--muted);
  background: color-mix(in srgb, var(--page) 75%, transparent);
  padding: 2px 8px;
  border-radius: 6px;
  pointer-events: none;
}

/* ---------- ツールチップ ---------- */
#tooltip {
  position: fixed;
  z-index: 60;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 6px 10px;
  font-size: 12px;
  color: var(--ink);
  pointer-events: none;
  display: none;
  max-width: 240px;
}
#tooltip .tyear { color: var(--muted); font-size: 11px; }

/* ---------- 相関図（樹形図）ビュー ---------- */
#treeOverlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: var(--page);
  display: flex;
  flex-direction: column;
}
#treeOverlay[hidden] { display: none; }
#treeHead {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
#treeTitle { font-weight: 700; font-size: 15px; flex: 1; }
#treeBack { border: none; color: var(--muted); }
#treeClose { border: none; font-size: 16px; color: var(--muted); }
#treeList { flex: 1; overflow-y: auto; padding: 12px 14px 40px; max-width: 640px; margin: 0 auto; width: 100%; }
#treeCanvas { flex: 1; display: block; touch-action: none; cursor: grab; }

/* 相関図のノード詳細カード（図の上に重なる。閉じれば図に戻る） */
#treeDetail {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 10px;
  width: min(560px, calc(100% - 20px));
  max-height: 52%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 5;
}
#treeDetail[hidden] { display: none; }
.td-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 6px;
  flex: none;
}
.td-title { font-size: 14px; line-height: 1.4; }
.td-year { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
#tdClose { border: none; color: var(--muted); min-height: 28px; padding: 2px 8px; }
.td-body { overflow-y: auto; padding: 0 12px; min-height: 0; }
.td-body > p { font-size: 12.5px; line-height: 1.75; color: var(--ink-2); }
.td-portrait { float: right; margin: 0 0 6px 10px; }
.td-portrait img { width: 72px; border-radius: 8px; border: 1px solid var(--border); display: block; }
.td-rels { margin-top: 8px; }
.td-rel {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 12px;
  color: var(--ink-2);
  min-height: 30px;
}
.td-rel:hover { background: var(--wash); }
.td-rel b { color: var(--ink); }
.td-actions { flex: none; padding: 8px 12px 10px; }
.td-actions button { width: 100%; }

/* ---------- ゲームモード「ヒストリーカード」 ---------- */
#gameOverlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--page);
  display: flex;
  flex-direction: column;
}
/* ID セレクタは UA の [hidden]{display:none} より強いので明示的に打ち消す */
#gameOverlay[hidden] { display: none; }
#gameHead {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
#gameTitle { font-weight: 700; font-size: 15px; }
#collBadge, #shardBadge { font-size: 12.5px; color: var(--ink-2); }
#gameTabs { display: flex; gap: 4px; margin-left: auto; }
#gameClose { border: none; font-size: 16px; color: var(--muted); }
#gameBody { flex: 1; overflow-y: auto; padding: 12px 14px 40px; }

.ghint { font-size: 12.5px; color: var(--ink-2); line-height: 1.7; margin-bottom: 12px; }
.ghint b { color: var(--ink); }

/* カード */
.gcard {
  display: inline-flex;
  flex-direction: column;
  border: 2px solid;
  border-radius: 10px;
  background: var(--surface);
  overflow: hidden;
  cursor: pointer;
  vertical-align: top;
  user-select: none;
}
.gcard.rar-n { border-color: #a9714b; }
.gcard.rar-r { border-color: #8a94a3; }
.gcard.rar-sr { border-color: #d4a017; box-shadow: 0 0 8px rgba(212, 160, 23, 0.35); }
.gcard.mini { width: 106px; min-height: 128px; font-size: 11px; }
.gcard.mid { width: 132px; min-height: 150px; font-size: 12px; }
.gcard.big { width: 200px; min-height: 210px; font-size: 13px; margin: 0 auto; display: flex; }
.gc-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  padding: 2px 7px;
  font-size: 0.9em;
  min-height: 17px;
}
.gc-p { background: var(--cat-p); }
.gc-i { background: var(--cat-i); }
.gc-h { background: var(--cat-h); }
.gc-j { background: var(--cat-j); }
.gc-t { background: var(--cat-t); }
.gc-s { background: var(--cat-s); }
.gc-a { background: var(--cat-a); }
.gc-emoji { text-align: center; font-size: 2.6em; padding: 8px 0 2px; }
.gcard.big .gc-emoji { font-size: 3.4em; }
.gc-name {
  text-align: center;
  font-weight: 600;
  padding: 0 6px;
  line-height: 1.35;
  flex: 1;
}
.gc-meta { text-align: center; color: var(--muted); font-size: 0.85em; padding: 3px 4px 6px; }
.gcard.unowned { opacity: 0.55; }
.gcard.unowned .gc-emoji { filter: grayscale(1); }

/* 図鑑 */
.zchapter { margin-bottom: 10px; }
.zchapter summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 13.5px;
  padding: 7px 4px;
  border-bottom: 1px solid var(--border);
}
.zcount { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 6px; }
.zgrid { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 0; }

/* モーダル */
.gmodal {
  position: fixed;
  inset: 0;
  z-index: 120;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.gmodal-box {
  background: var(--page);
  border-radius: 14px;
  padding: 18px;
  max-width: 420px;
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.gdesc { font-size: 13px; line-height: 1.75; color: var(--ink-2); margin: 12px 0; }
.gmodal-btns { display: flex; gap: 8px; }
.gmodal-btns button { flex: 1; }

/* クイズ */
.qhome { display: flex; flex-direction: column; gap: 10px; max-width: 460px; margin: 0 auto; }
.qbig { padding: 14px; font-size: 14.5px; border-radius: 12px; }
.qbig.sr { border-color: #d4a017; }
.qstats { text-align: center; color: var(--muted); font-size: 12px; }
.qcard { max-width: 560px; margin: 0 auto; }
.qsr { color: #d4a017; font-weight: 700; margin-bottom: 6px; }
.qq { font-weight: 700; font-size: 15px; margin-bottom: 10px; }
.qdesc {
  border-left: 3px solid var(--baseline);
  padding: 4px 0 4px 12px;
  font-size: 13.5px;
  line-height: 1.8;
  color: var(--ink-2);
  margin-bottom: 6px;
}
.qhintline { font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.qchoices { display: flex; flex-direction: column; gap: 8px; }
.qc { text-align: left; padding: 11px 14px; border-radius: 10px; font-size: 13.5px; white-space: normal; }
.qresult { font-size: 20px; font-weight: 700; text-align: center; margin: 14px 0 10px; }
.qresult.ok { color: var(--cat-h); }
.qresult.ng { color: var(--cat-i); }
.qanswer { text-align: center; font-weight: 600; margin-bottom: 8px; }
.qgot { text-align: center; font-size: 14px; margin-top: 10px; }
.qnav { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }
.qnav button { padding: 10px 18px; }
.qnav .primary, #bStart.primary { background: var(--cat-p); color: #fff; border-color: var(--cat-p); font-weight: 700; }

/* 旅えらび */
.jsec { font-size: 13px; color: var(--muted); margin: 16px 0 8px; font-weight: 600; }
.jrow {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto 8px;
  padding: 11px 14px;
  border-radius: 12px;
  text-align: left;
}
.jrow .jicon { font-size: 24px; flex: none; }
.jrow .jbody { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.jrow .jdesc, .jrow .jmeta { font-size: 11.5px; color: var(--muted); white-space: normal; }
.jrow .jstate { flex: none; font-size: 12px; color: var(--muted); }

/* 旅パネル（サイドに出る） */
.jhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
}
.jhead .jprog { color: var(--muted); font-size: 11.5px; margin-left: 8px; }
.jhead button { border: none; min-height: 28px; padding: 2px 8px; color: var(--muted); }
.jbar { height: 4px; background: var(--wash); border-radius: 2px; margin: 8px 0 12px; overflow: hidden; }
.jbar-in { height: 100%; background: var(--cat-i); transition: width 0.4s; }
.jq { font-weight: 700; font-size: 14.5px; margin-bottom: 8px; }
.jwhere {
  background: var(--wash);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.7;
  margin-bottom: 10px;
}
.jdone { font-size: 13.5px; line-height: 1.9; text-align: center; margin: 10px 0; }
.jperfect { color: #d4a017; font-weight: 700; }
.qc.picked { background: var(--wash); border-color: var(--cat-i); }

/* バトル（関所） */
.bscore { font-size: 12.5px; color: var(--ink-2); margin-bottom: 10px; display: block; }
.breveal {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  margin: 14px 0;
}
.bside { text-align: center; }
.bvsmark { font-size: 30px; }
.bcalc { font-size: 12.5px; margin-top: 6px; color: var(--ink-2); }
.bcalc b { color: var(--ink); font-size: 14px; }
.bplus { color: var(--cat-h); font-weight: 600; }

@media (max-width: 820px) {
  .gcard.mini { width: 96px; min-height: 120px; }
  #gameTabs { margin-left: 0; width: 100%; order: 5; }
}

/* ---------- モバイル（縦積み＋ボトムシート） ---------- */
@media (max-width: 820px) {
  .subtitle { display: none; }
  .titlerow h1 { font-size: 15px; }
  #topbar { padding: 6px 10px; flex-wrap: nowrap; }
  main { grid-template-columns: 1fr; }
  #timeline { height: 158px; }
  #listBtn { display: inline-block; }

  #side {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 40;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: var(--shadow);
    max-height: 62vh;
    transform: translateY(105%);
    transition: transform 0.28s ease;
  }
  #side.open { transform: translateY(0); }

  #sheetHandle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    padding: 12px 0 8px;   /* 下スワイプで閉じるハンドル。触りやすく広めに */
    flex: none;
    touch-action: none;
    cursor: grab;
  }
  #sheetHandle .bar {
    width: 52px; height: 5px;
    border-radius: 3px;
    background: var(--baseline);
  }
  #detail { max-height: 34vh; }
  #evList { max-height: 24vh; }
}

/* ---------- 歴史ドキュメンタリー（物語モード） ---------- */
#docuOverlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: var(--page);
  display: flex;
  flex-direction: column;
}
#docuOverlay[hidden] { display: none; }
#docuHead {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
#docuTitle { font-weight: 700; font-size: 15px; flex: 1; }
#docuClose { border: none; font-size: 16px; color: var(--muted); }
#docuList {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

#docuCard {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 12px;
  width: min(560px, calc(100% - 16px));
  max-height: 62%;
  display: flex;
  flex-direction: column;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 6;
}
#docuCard[hidden] { display: none; }
.dc-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px 0 14px;
  flex: none;
}
.dc-ch { font-size: 10.5px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.dc-title { font-size: 15px; font-weight: 700; flex: 1; }
#dcExit { border: none; color: var(--ink-2); min-height: 32px; min-width: 32px; padding: 2px 8px; flex: none; }
.dc-body {
  overflow-y: auto;
  padding: 6px 14px 4px;
  min-height: 0;
  font-size: 13px;
  line-height: 1.95;
  color: var(--ink-2);
}
.dc-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 10px;
  flex: none;
}
.dc-foot .spacer { flex: 1; text-align: center; font-size: 11.5px; color: var(--muted); }
.dc-foot button { border-radius: 10px; }
#dcNext {
  font-weight: 700;
  background: color-mix(in srgb, var(--cat-p) 16%, var(--surface));
  border-color: var(--cat-p);
}
@media (pointer: coarse) {
  #dcExit { min-height: 40px; min-width: 40px; }
  .dc-body { font-size: 13.5px; }
}

/* 物語カードの図版 */
.dc-img { margin: 2px 0 8px; }
.dc-img img {
  width: 100%;
  height: clamp(90px, 16vh, 150px);
  object-fit: cover;
  border-radius: 10px;
  display: block;
  background: var(--wash);
}
.dc-img figcaption { text-align: right; margin-top: 2px; }
.dc-img figcaption a { font-size: 10px; color: var(--muted); text-decoration: none; }

/* 図版のライトボックス */
#docuLightbox {
  position: fixed;
  inset: 0;
  z-index: 97;
  background: rgba(0, 0, 0, 0.86);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  cursor: zoom-out;
}
#docuLightbox[hidden] { display: none; }
#docuLightbox img {
  max-width: 100%;
  max-height: 84vh;
  object-fit: contain;
  border-radius: 8px;
}
#docuLightbox .lb-cap { color: #ddd; font-size: 12.5px; }
#docuLightbox .lb-cap a { color: #9cc4e8; }
.dc-img img { cursor: zoom-in; }
.portraitbox img, .rd-portrait img { cursor: zoom-in; }

/* iOS Safariは16px未満の入力欄にフォーカスすると自動でページを拡大してしまうため、
   スマホでは入力欄の文字を16pxにして自動拡大自体を起こさせない
   （個別の font-size 指定より後勝ちさせる必要があるためファイル末尾に置く） */
@media (max-width: 820px) {
  #searchBox, #playYear input { font-size: 16px; }
}

/* 右パネルの開閉ボタン（横レイアウトのみ。地図右端の縦長ハンドル） */
#sideToggle {
  position: absolute;
  right: 0;
  top: auto;
  bottom: 96px;   /* 地図右上のボタン列（🌡地理など）と重ならないよう下側に置く */
  transform: none;
  z-index: 6;
  width: 24px;
  min-height: 72px;
  padding: 0;
  border-radius: 12px 0 0 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-right: none;
  font-size: 13px;
  color: var(--muted);
  box-shadow: var(--shadow);
}
#sideToggle:hover { color: var(--ink); }
@media (max-width: 820px) {
  #sideToggle { display: none; }
}
body.side-collapsed main { grid-template-columns: 1fr 0; }
body.side-collapsed #side { display: none; }

/* 雨温図カード */
.hytherCv { width: 100%; display: block; margin-top: 4px; }
.hynote { font-size: 12px; color: var(--muted); padding: 6px 0 2px; }
.hynote button { min-height: 28px; font-size: 12px; padding: 3px 10px; }

/* 政体カードの領土推移グラフ */
.areaspark { margin: 2px 0 8px; }
.areaspark canvas { width: 100%; display: block; }
.as-cap { font-size: 10.5px; color: var(--muted); text-align: right; }

/* 戦争カードの前後比較ボタン */
.warmapbtn {
  display: block;
  margin: 8px 0 2px;
  border-color: var(--cat-w);
  color: var(--cat-w);
  font-weight: 600;
}
.warmapbtn:hover { background: color-mix(in srgb, var(--cat-w) 12%, transparent); }

/* タイムラインの開閉 */
#tlToggle,
#bandsToggle {
  position: absolute;
  right: 8px;
  top: -13px;
  z-index: 25;
  min-height: 24px;
  padding: 1px 12px;
  font-size: 11px;
  border-radius: 10px;
  background: var(--surface);
}
#bandsToggle { right: 118px; }   /* タイムラインボタンの左に並べる */
body.tl-collapsed #timelinewrap > *:not(#tlToggle) { display: none; }
body.tl-collapsed #timelinewrap { min-height: 16px; height: 16px; }
body.tl-collapsed #tlToggle { top: -11px; }

/* 物語カードの「📖詳細」チップと、解説カード内の図版 */
.dc-evs {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding: 2px 14px 6px;
}
.dc-ev {
  font-size: 11px;
  min-height: 24px;
  padding: 2px 9px;
  border-radius: 10px;
  background: var(--surface);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.mc-fig img {
  float: right;
  max-width: 45%;
  border-radius: 6px;
  margin: 0 0 8px 10px;
  cursor: zoom-in;
}

/* 物語一覧のカテゴリ折りたたみ */
.doccat { margin-bottom: 6px; }
.doccat summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 14.5px;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--surface);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}
.doccat summary::-webkit-details-marker { display: none; }
.doccat summary::after { content: '▸'; margin-left: auto; opacity: 0.55; }
.doccat[open] summary::after { content: '▾'; }
.doccat-n {
  font-size: 11.5px;
  font-weight: 600;
  opacity: 0.6;
}
.doccat .storycard { margin-left: 8px; }

/* 物語カードはヘッダーを掴んで移動できる */
#docuCard .dc-head { cursor: grab; touch-action: none; }
#docuCard .dc-head:active { cursor: grabbing; }

/* 物語本文の自動リンク(作品・人物名をタップで解説カード) */
.dc-link {
  display: inline;
  padding: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  min-height: 0;
  border-bottom: 1px dashed color-mix(in srgb, currentColor 55%, transparent);
  border-radius: 0;
  cursor: pointer;
}
.dc-link:active { opacity: 0.6; }

/* カードの絵柄を実画像に(big/mid)。取得できるまでは絵文字のまま */
.gc-emoji.gc-img { padding: 6px 8px 2px; }
.gc-emoji.gc-img img {
  width: 100%;
  height: 96px;
  object-fit: cover;
  border-radius: 8px;
  display: block;
}
.gcard.big .gc-emoji.gc-img img { height: 150px; }
.gcard.mid .gc-emoji.gc-img img { height: 84px; }
