:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #efebe3;
  --text: #1f1d1a;
  --muted: #6b665d;
  --line: #ddd7cc;
  --grid: rgba(0, 0, 0, 0.06);
  --accent: #1f5f8b;
  --link-out: #1f5f8b;
  --link-in: #b0592b;
  --shadow: 0 6px 24px rgba(30, 25, 15, 0.14);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: Georgia, "Times New Roman", serif;
  color-scheme: light;
}
/* Dark: follows the computer's setting ("Auto"), unless the theme button chose Light or Dark
   (data-theme on <html>, saved in the browser). The two dark blocks must stay identical. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a;
    --surface: #1e2125;
    --surface-2: #272b30;
    --text: #ece9e3;
    --muted: #a19b90;
    --line: #34383e;
    --grid: rgba(255, 255, 255, 0.06);
    --accent: #7fb6de;
    --link-out: #7fb6de;
    --link-in: #e59a6c;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171a;
  --surface: #1e2125;
  --surface-2: #272b30;
  --text: #ece9e3;
  --muted: #a19b90;
  --line: #34383e;
  --grid: rgba(255, 255, 255, 0.06);
  --accent: #7fb6de;
  --link-out: #7fb6de;
  --link-in: #e59a6c;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- header ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface);
}
.brand h1 { font: 700 20px/1.2 var(--serif); margin: 0; letter-spacing: 0.2px; }
.topbar .end { display: flex; align-items: center; gap: 10px; }
.theme-btn {
  border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 7px 12px; cursor: pointer;
  white-space: nowrap; font-size: 13px;
}
.theme-btn:hover { border-color: var(--accent); }
.where { margin: 2px 0 0; color: var(--muted); font-size: 13px; min-height: 1.2em; }
.search { position: relative; width: min(380px, 45vw); }
.search input {
  width: 100%; padding: 8px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg);
}
.results {
  position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px;
  list-style: none; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 60vh; overflow: auto;
}
.results li { padding: 7px 10px; border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.results li span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.results li[aria-selected="true"], .results li:hover { background: var(--surface-2); }
.results li.none { color: var(--muted); cursor: default; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; padding: 8px 16px;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.toolbar button, .toolbar select {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 5px 10px; cursor: pointer; min-height: 32px;
}
.toolbar button:hover, .toolbar select:hover { background: var(--surface-2); }
#zoomIn, #zoomOut { width: 34px; font-weight: 700; padding: 0; }
.seg { gap: 0; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 8px 0 0 8px; }
.seg button:last-child { border-radius: 0 8px 8px 0; border-left: 0; }
.seg button[aria-checked="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.chips .chip { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; font-size: 13px; padding: 3px 10px; }
.chip .swatch, .tl-lane-label .swatch { width: 9px; height: 9px; border-radius: 50%; background: var(--lane); display: inline-block; flex: none; }
.chip[aria-pressed="false"] { opacity: 0.45; text-decoration: line-through; }

/* ---------- stage ---------- */
.stage { flex: 1; min-height: 0; position: relative; display: flex; }
#timeline { flex: 1; min-width: 0; }
.panel {
  width: 380px; flex: none; border-left: 1px solid var(--line); background: var(--surface);
  overflow: auto; padding: 18px 20px 28px; position: relative;
}
.panel .close {
  position: absolute; top: 8px; right: 10px; border: 0; background: transparent; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted);
}
.panel h2 { font: 700 22px/1.25 var(--serif); margin: 6px 30px 4px 0; }
.panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 20px 0 6px; }
.panel .when { color: var(--muted); margin: 0 0 12px; }
.panel .era { border-bottom: 3px solid var(--period); }
.panel .summary { font-size: 15px; }
.panel .hero { display: block; max-width: 100%; max-height: 260px; margin: 4px auto 8px; border-radius: 8px; }
.panel .place { color: var(--muted); }
.panel .muted { color: var(--muted); }
.tag { display: inline-block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; padding: 2px 8px; border-radius: 999px; background: var(--lane); color: #fff; margin-right: 6px; }
.tag.plain { background: var(--surface-2); color: var(--muted); }
.links { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.linkto {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 8px 10px; cursor: pointer; display: block;
}
.linkto:hover { border-color: var(--accent); }
.linkto .rel { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent); }
.linkto .date { color: var(--muted); font-size: 12px; }
.linkto .note { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.more { margin-top: 20px; }

.foot {
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 6px 16px; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--line); background: var(--surface);
}
.error { padding: 24px; color: #b00020; }

/* ---------- timeline ---------- */
.tl { height: 100%; display: flex; flex-direction: column; user-select: none; -webkit-user-select: none; touch-action: none; position: relative; }
.tl.is-dragging { cursor: grabbing; }
.tl-periods { position: relative; height: 30px; flex: none; overflow: hidden; background: var(--surface-2); }
.tl-period {
  position: absolute; top: 0; bottom: 0; border: 0; padding: 0 6px; margin: 0; cursor: pointer;
  background: var(--period); color: #fff; font-size: 12px; font-weight: 600; overflow: hidden; white-space: nowrap;
  box-shadow: inset -1px 0 0 rgba(255, 255, 255, 0.5);
}
.tl-period span { position: sticky; left: 6px; display: block; overflow: hidden; text-overflow: ellipsis; }
.tl-period.is-narrow span { visibility: hidden; }
.tl-period:hover { filter: brightness(1.1); }
.tl-axis { position: relative; height: 22px; flex: none; border-bottom: 1px solid var(--line); font-size: 11px; color: var(--muted); overflow: hidden; }
.tl-axis span { position: absolute; top: 4px; transform: translateX(-50%); white-space: nowrap; }
.tl-body { position: relative; flex: 1; min-height: 0; overflow: hidden; cursor: grab; outline: none; }
.tl-body:focus-visible { box-shadow: inset 0 0 0 2px var(--accent); }
.tl-grid, .tl-links { position: absolute; inset: 0; pointer-events: none; }
.tl-links { width: 100%; height: 100%; z-index: 3; overflow: visible; }
.tl-links path { fill: none; stroke-width: 2; }
.tl-links path.out { stroke: var(--link-out); }
.tl-links path.in { stroke: var(--link-in); stroke-dasharray: 5 4; }
.tl-links circle.out { fill: var(--link-out); }
.tl-links circle.in { fill: var(--link-in); }
.tl-grid i { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--grid); }
.tl-lanes { position: relative; height: 100%; }
.tl-lane { position: relative; border-bottom: 1px solid var(--line); overflow: hidden; }
.tl-lane:nth-child(even) { background: rgba(127, 127, 127, 0.035); }
/* Lane names: a filled strip down the left edge in the lane's colour, reading upwards, like the era bands. */
.tl-lane-label {
  position: absolute; top: 0; bottom: 0; left: 0; width: 22px; z-index: 4;
  background: var(--lane); color: #fff; box-shadow: inset 0 -1px 0 rgba(255, 255, 255, 0.5);
  writing-mode: vertical-rl; transform: rotate(180deg);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; white-space: nowrap;
}
.tl-lane-label span { overflow: hidden; text-overflow: ellipsis; max-height: calc(100% - 6px); }
.tl-lane-overflow { position: absolute; left: 22px; right: 0; bottom: 0; height: 12px; border-top: 1px dotted var(--line); }

.tl-ev {
  position: absolute; height: 22px; display: flex; align-items: center; gap: 5px;
  border: 0; background: none; padding: 0; margin: 0 0 0 -5px; cursor: pointer; white-space: nowrap; z-index: 1;
}
.tl-ev .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--lane); flex: none; box-shadow: 0 0 0 2px var(--bg); }
.tl-ev .lbl { font-size: 13px; padding: 1px 5px; border-radius: 4px; background: color-mix(in srgb, var(--bg) 85%, transparent); }
.tl-ev.imp-4 .lbl, .tl-ev.imp-5 .lbl { font-weight: 600; }
.tl-ev.imp-1 .lbl, .tl-ev.imp-2 .lbl { color: var(--muted); }
.tl-ev .bar { display: none; }
.tl-ev.is-bar { margin-left: 0; }
.tl-ev.is-bar .dot { display: none; }
.tl-ev.is-bar .bar {
  display: block; width: var(--w); height: 18px; border-radius: 5px; flex: none;
  background: color-mix(in srgb, var(--lane) 30%, var(--bg)); border: 1.5px solid var(--lane);
}
/* Label on the left of the dot/bar (used near the right-hand edge): reverse the order and anchor on the right. */
.tl-ev.label-left { flex-direction: row-reverse; transform: translateX(-100%); margin-left: 0; }
.tl-ev.is-bar.label-inside .lbl { position: absolute; left: calc(6px + var(--shift, 0px)); background: none; }
.tl-ev:hover .lbl, .tl-ev:focus-visible .lbl { background: var(--surface); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.18); }
.tl-ev:hover, .tl-ev:focus-visible { z-index: 4; outline: none; }
.tl-ev.is-selected { z-index: 5; }
.tl-ev.is-selected .lbl { background: var(--text); color: var(--bg); }
.tl-ev.is-selected .dot { box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--lane); }
.tl-ev.is-selected .bar { background: var(--lane); }

.tl-tick {
  position: absolute; width: 6px; height: 6px; border-radius: 50%; border: 0; padding: 0; background: var(--lane); opacity: 0.55; cursor: pointer;
}
.tl-tick:hover, .tl-tick.is-selected { opacity: 1; transform: scale(1.6); }

.tl-minimap { position: relative; height: 18px; flex: none; border-top: 1px solid var(--line); cursor: pointer; background: var(--surface-2); }
.tl-mm-bands div { position: absolute; top: 4px; bottom: 4px; opacity: 0.75; }
.tl-mm-window {
  position: absolute; top: 1px; bottom: 1px; border: 2px solid var(--text); border-radius: 3px; min-width: 4px; pointer-events: none;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.0);
}

/* ---------- phones ---------- */
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 8px 16px; }
  .brand h1 { font-size: 17px; }
  .search { width: 100%; }
  .topbar .end { width: 100%; }
  .topbar .end .search { flex: 1; width: auto; min-width: 0; }
  .toolbar { padding: 6px 16px; gap: 6px; }
  .toolbar .group.pick { width: 100%; flex-wrap: nowrap; }
  .toolbar .group.pick select { flex: 1; min-width: 0; }
  .chips { flex-wrap: nowrap; overflow-x: auto; width: 100%; padding-bottom: 2px; }
  .chips .chip { white-space: nowrap; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; max-height: 62%; z-index: 20;
    border-left: 0; border-top: 1px solid var(--line); border-radius: 14px 14px 0 0; box-shadow: var(--shadow);
  }
  .foot span:first-child { display: none; }
}
