:root {
  --bg: #f5efe3;
  --panel: #fffdf8;
  --card: #fbf6ec;
  --line: #e4dac6;
  --grid: rgba(90, 70, 40, 0.09);
  --band: rgba(140, 110, 60, 0.06);
  --text: #2a241c;
  --muted: #7a6d5a;
  --accent: #9a6a12;
  --sel: #1f1a13;
  --shadow: 0 10px 30px rgba(60, 40, 10, 0.18);
  --serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16130f;
    --panel: #1f1b16;
    --card: #26211b;
    --line: #3a3229;
    --grid: rgba(240, 220, 180, 0.07);
    --band: rgba(240, 220, 180, 0.035);
    --text: #f1e9da;
    --muted: #a99b85;
    --accent: #e0b04e;
    --sel: #fff6e6;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

/* [hidden] must beat every display rule below (the whistle/facepunch trap). */
[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--sans);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* ------------------------------------------------------------------ header */
.top {
  flex: none;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
  position: relative;
  z-index: 20;
}
.bar { display: flex; align-items: center; gap: 6px; padding: 8px 10px 4px; }
.brand {
  font: 600 21px/1 var(--serif);
  margin: 0 auto 0 2px;
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: 0.01em;
}
.mark {
  width: 22px; height: 22px; border-radius: 6px;
  background:
    radial-gradient(circle at 25% 50%, #ea580c 0 2.6px, transparent 3px),
    radial-gradient(circle at 50% 50%, #dc2626 0 2.6px, transparent 3px),
    radial-gradient(circle at 75% 50%, #1d4ed8 0 2.6px, transparent 3px),
    linear-gradient(transparent 10px, var(--accent) 10px 12px, transparent 12px),
    #2a241c;
}
.seg {
  display: flex;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px;
}
.seg button {
  border: 0; background: none; border-radius: 999px;
  padding: 6px 11px; font-size: 13px; font-weight: 600; color: var(--muted);
  min-height: 34px;
}
.seg button.on { background: var(--text); color: var(--bg); }
.icon {
  width: 40px; height: 40px; flex: none;
  border: 0; background: none; border-radius: 10px;
  display: grid; place-items: center;
}
.icon:hover { background: var(--card); }
.icon svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

.presets {
  display: flex; gap: 6px; overflow-x: auto; padding: 4px 10px 9px;
  scrollbar-width: none;
}
.presets::-webkit-scrollbar { display: none; }
.presets button {
  flex: none; border: 1px solid var(--line); background: var(--card);
  border-radius: 999px; padding: 6px 12px; font-size: 13px; min-height: 34px;
  white-space: nowrap;
}
.presets button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.searchrow { padding: 2px 10px 8px; position: relative; }
.searchrow input {
  width: 100%; font: inherit; font-size: 16px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card); color: var(--text);
}
.results {
  position: absolute; left: 10px; right: 10px; top: calc(100% - 4px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); max-height: 60vh; overflow-y: auto; z-index: 30;
}
.results:empty { display: none; }
.results button {
  display: flex; gap: 10px; align-items: baseline; width: 100%; text-align: left;
  border: 0; background: none; padding: 10px 12px; border-bottom: 1px solid var(--line);
}
.results button:last-child { border-bottom: 0; }
.results button:hover, .results button.hl { background: var(--card); }
.results .sw { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--c); transform: translateY(-1px); }
.results .rt { font-weight: 600; }
.results .rd { color: var(--muted); font-size: 13px; margin-left: auto; white-space: nowrap; }
.results .none { padding: 12px; color: var(--muted); }

/* ------------------------------------------------------------------ layout */
.layout { flex: 1; display: flex; min-height: 0; }
.tl { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }

.axis {
  flex: none; height: 46px; position: relative; overflow: hidden;
  border-bottom: 1px solid var(--line); background: var(--bg);
  font-size: 11.5px; color: var(--muted); user-select: none;
}
.axis .era {
  position: absolute; top: 0; height: 20px; line-height: 20px;
  font: italic 12px var(--serif); color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: clip; padding: 0 6px;
  border-left: 1px solid var(--line);
}
.axis .era.alt { background: var(--band); }
.axis .tick {
  position: absolute; top: 22px; height: 24px; border-left: 1px solid var(--line);
  padding: 4px 0 0 5px; white-space: nowrap; font-variant-numeric: tabular-nums;
}
.axis .tick.zero { border-left: 2px solid var(--accent); color: var(--accent); font-weight: 600; }

.stage {
  flex: 1; min-height: 0; position: relative;
  overflow-x: hidden; overflow-y: auto;
  touch-action: none;
  overscroll-behavior: contain;
  cursor: grab;
  user-select: none; -webkit-user-select: none;
}
.stage.dragging { cursor: grabbing; }
.plot { position: relative; min-height: 100%; }

.band { position: absolute; top: 0; bottom: 0; background: var(--band); pointer-events: none; }
.gl { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--grid); pointer-events: none; }
.gl.zero { background: var(--accent); opacity: 0.45; width: 2px; }

.lanehead {
  position: absolute; left: 0; right: 0; height: 24px;
  display: flex; align-items: center; gap: 7px; padding: 0 10px;
  font-size: 11.5px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--muted); pointer-events: none;
}
.lanehead::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.lanehead i { width: 10px; height: 10px; border-radius: 3px; background: var(--c); }

/* ------------------------------------------------------------------ items */
.ev {
  position: absolute; height: 24px; display: flex; align-items: center;
  cursor: pointer; outline: none;
}
.ev .mk {
  position: absolute; left: 0; top: 2px; bottom: 2px; width: 100%;
  border-radius: 5px;
  background: color-mix(in srgb, var(--c) 26%, transparent);
  border-left: 3px solid var(--c);
}
.ev.fuzz .mk {
  border-left: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--c) 22%, transparent) 30%);
}
.ev.death .mk::after {
  content: '\2020'; position: absolute; right: -1px; top: 50%; transform: translate(50%, -50%);
  width: 16px; height: 16px; border-radius: 50%; background: var(--c); color: #fff;
  font: 700 11px/16px var(--serif); text-align: center;
}
.ev.pt .mk {
  width: 11px; height: 11px; top: 6.5px; left: 0; border: 2px solid var(--panel);
  border-radius: 50%; background: var(--c); box-shadow: 0 0 0 1px var(--c);
}
.ev .lb {
  position: absolute; white-space: nowrap; font-size: 12.5px; font-weight: 500; line-height: 24px;
  color: var(--text);
}
.ev.in .lb { left: 8px; right: 6px; overflow: hidden; text-overflow: ellipsis; }
.ev.out .lb { left: calc(100% + 5px); }
.ev.pt .lb { left: 16px; }
.ev:hover .mk, .ev:focus-visible .mk { background: color-mix(in srgb, var(--c) 34%, transparent); }
.ev.pt:hover .mk { transform: scale(1.15); }
.ev.sel .mk { background: var(--c); box-shadow: 0 0 0 2px var(--sel); }
.ev.sel.pt .mk { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--sel); }
.ev.sel .lb { font-weight: 700; }
.ev.sel.in .lb { color: #fff; }
.ev.dim { opacity: 0.28; }
.ev.hit .mk { box-shadow: 0 0 0 2px var(--accent); }

.plot.anim .ev, .plot.anim .lanehead {
  transition: left 0.6s cubic-bezier(.3,.7,.2,1), width 0.6s cubic-bezier(.3,.7,.2,1), top 0.6s cubic-bezier(.3,.7,.2,1);
}
@media (prefers-reduced-motion: reduce) {
  .plot.anim .ev, .plot.anim .lanehead { transition: none; }
}

.lanehead em { display: none; font-style: italic; font-weight: 400; text-transform: none; letter-spacing: 0; }
.lanehead.none em { display: inline; }
.lanehead.none { opacity: 0.75; }

/* ------------------------------------------------------------------ controls */
.zoom {
  position: absolute; right: 12px; bottom: 58px; display: flex; flex-direction: column; gap: 6px; z-index: 5;
}
.zoom button {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); font-size: 22px; line-height: 1; box-shadow: var(--shadow);
}
.hint {
  position: absolute; left: 50%; bottom: 60px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); font-size: 13px; padding: 7px 12px; border-radius: 999px;
  pointer-events: none; opacity: 0; transition: opacity .4s; white-space: nowrap; z-index: 5;
}
.hint.show { opacity: 0.92; }
.mini {
  flex: none; display: block; width: 100%; height: 46px;
  border-top: 1px solid var(--line); background: var(--panel);
  touch-action: none; cursor: pointer;
  margin-bottom: env(safe-area-inset-bottom);
}

/* ------------------------------------------------------------------ detail */
.detail {
  background: var(--panel); display: flex; flex-direction: column; min-height: 0;
}
.d-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 6px 0 20px; flex: none; position: relative; }
.d-body { flex: 1; overflow-y: auto; padding: 0 20px 16px; overscroll-behavior: contain; }
.d-lane {
  display: flex; align-items: center; gap: 7px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted);
}
.d-lane i { width: 10px; height: 10px; border-radius: 3px; background: var(--c); }
.d-body h2 { font: 600 24px/1.2 var(--serif); margin: 2px 0 10px; }
.dates { display: grid; gap: 4px; margin: 0 0 12px; }
.dates div {
  display: flex; gap: 10px; align-items: baseline; padding: 6px 10px; border-radius: 9px;
  background: var(--card); font-variant-numeric: tabular-nums;
}
.dates div.cur { box-shadow: inset 3px 0 0 var(--accent); }
.dates b { font-size: 11.5px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); min-width: 86px; }
.badge {
  display: inline-block; font-size: 11.5px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;
  border: 1px solid var(--accent); color: var(--accent); border-radius: 999px; padding: 1px 8px; margin: 0 0 10px;
}
.d-body p { margin: 0 0 12px; }
.d-body h3 {
  font-size: 12px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--accent);
  margin: 18px 0 4px;
}
.d-body .refs { color: var(--muted); font-size: 14px; }
.d-foot {
  flex: none; display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line);
}
.d-foot button {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px; min-height: 44px;
  padding: 0 12px; font-size: 14px; font-weight: 600;
}
.d-foot button:disabled { opacity: 0.35; cursor: default; }

/* Phone: the detail is a bottom sheet over the lower half of the timeline. */
@media (max-width: 899px) {
  .detail {
    position: fixed; left: 0; right: 0; bottom: 0; height: 58vh; z-index: 40;
    border-radius: 18px 18px 0 0; box-shadow: var(--shadow); border-top: 1px solid var(--line);
    animation: up .25s cubic-bezier(.3,.7,.2,1);
  }
  .d-head::before {
    content: ''; position: absolute; left: 50%; top: 7px; width: 40px; height: 4px; margin-left: -20px;
    border-radius: 2px; background: var(--line);
  }
  .seg button { padding: 6px 9px; }
}
@keyframes up { from { transform: translateY(40%); opacity: 0; } }
/* Desktop: a side panel, and the timeline narrows beside it. */
@media (min-width: 900px) {
  .detail { width: 420px; flex: none; border-left: 1px solid var(--line); }
}
@media (max-width: 380px) {
  .brand { font-size: 18px; }
  .seg button { padding: 6px 7px; font-size: 12.5px; }
  .icon { width: 36px; }
}

/* ------------------------------------------------------------------ sheets */
.sheet {
  position: fixed; inset: 0; z-index: 50; background: rgba(20, 14, 6, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-card {
  background: var(--panel); width: 100%; max-width: 560px; max-height: 85vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 8px 18px calc(18px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-card { border-radius: 18px; }
}
.sheet-title { display: flex; justify-content: space-between; align-items: center; font: 600 20px var(--serif); margin: 4px 0 6px; }
#laneList label {
  display: flex; align-items: center; gap: 12px; min-height: 48px; border-bottom: 1px solid var(--line);
  cursor: pointer;
}
#laneList input { width: 20px; height: 20px; accent-color: var(--accent); }
#laneList i { width: 12px; height: 12px; border-radius: 3px; background: var(--c); }
#laneList span { flex: 1; }
#laneList small { color: var(--muted); }
.wide {
  width: 100%; margin-top: 14px; min-height: 46px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); font-weight: 600;
}
.prose p { margin: 0 0 12px; }
.prose h3 { font-size: 12px; letter-spacing: 0.07em; text-transform: uppercase; color: var(--accent); margin: 18px 0 6px; }
.prose .ver { color: var(--muted); font-size: 13px; margin-top: 18px; }
.ev.bare .lb { display: none; }
.ev.bare.in .mk, .ev.bare.out .mk { opacity: 0.75; }
@media (max-width: 430px) {
  .bar { gap: 2px; padding-left: 8px; padding-right: 4px; }
  .brand { font-size: 19px; gap: 6px; }
  .seg button { padding: 6px 8px; font-size: 12.5px; }
  .icon { width: 36px; }
}
/* Label on the left of its mark, near the right edge (see `flip` in draw()). */
.ev.flip.pt .lb { left: auto; right: 15px; }
.ev.flip.out .lb { left: auto; right: calc(100% + 5px); }
