/* Layout: full-screen canvas. Time axis down the right edge, parallel dynasty lanes run right-to-left, detail drawer slides in from the left. */
:root {
  --bg: #eceee9;
  --surface: #f9faf7;
  --raised: #ffffff;
  --ink: #1a2320;
  --muted: #5b6862;
  --line: #d2d8d1;
  --grid: rgba(26, 35, 32, 0.07);
  --accent: #1d4f86;
  --accent-ink: #ffffff;
  --focus: #1d4f86;
  --shadow: 0 10px 40px rgba(20, 30, 26, 0.16);

  --d-umayyad: #8e2f3a;
  --d-zubayr: #a8731b;
  --d-abbasid: #2b313b;
  --d-andalus: #23786a;
  --d-fatimid: #3d7a2c;

  --c-battle: #b23a2b;
  --c-city: #2c68a3;
  --c-reform: #765aa3;
  --c-calamity: #9c6c12;
  --c-scholar: #3c5a74;

  --f-display: "Reem Kufi", "Noto Kufi Arabic", "Segoe UI", Tahoma, sans-serif;
  --f-body: "Noto Naskh Arabic", "Amiri", "Traditional Arabic", serif;
  --f-ui: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111614; --surface: #171d1a; --raised: #1e2522; --ink: #e5ebe6; --muted: #98a59e; --line: #2c3631;
    --grid: rgba(229, 235, 230, 0.06); --accent: #7fb0e6; --accent-ink: #0d1a28; --focus: #9cc3ef; --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    --d-umayyad: #c75b67; --d-zubayr: #d39b3c; --d-abbasid: #9aa6b8; --d-andalus: #4fb3a1; --d-fatimid: #72b45c;
    --c-battle: #e0705f; --c-city: #6ea6dc; --c-reform: #a991d4; --c-calamity: #d3a24a; --c-scholar: #8eb0cc;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111614; --surface: #171d1a; --raised: #1e2522; --ink: #e5ebe6; --muted: #98a59e; --line: #2c3631;
  --grid: rgba(229, 235, 230, 0.06); --accent: #7fb0e6; --accent-ink: #0d1a28; --focus: #9cc3ef; --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  --d-umayyad: #c75b67; --d-zubayr: #d39b3c; --d-abbasid: #9aa6b8; --d-andalus: #4fb3a1; --d-fatimid: #72b45c;
  --c-battle: #e0705f; --c-city: #6ea6dc; --c-reform: #a991d4; --c-calamity: #d3a24a; --c-scholar: #8eb0cc;
  color-scheme: dark;
}

html, body { height: 100%; }
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--f-ui); font-size: 14px; overflow: hidden;
}
.app { height: 100%; display: grid; grid-template-rows: auto 1fr auto; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* top bar */
.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding-block: 10px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--surface); border-bottom: 1px solid var(--line); position: relative; z-index: 20;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand h1 { font-family: var(--f-display); font-weight: 600; font-size: 22px; margin: 0; letter-spacing: 0; white-space: nowrap; }
.brand span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.search { position: relative; flex: 1 1 220px; max-width: 360px; min-width: 0; }
.search input {
  width: 100%; box-sizing: border-box; padding: 8px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--raised);
}
.results {
  position: absolute; inset-inline: 0; top: calc(100% + 4px); background: var(--raised); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: var(--shadow); max-height: 320px; overflow: auto; z-index: 30; padding: 4px;
}
.results button {
  display: flex; justify-content: space-between; gap: 12px; width: 100%; text-align: start; padding: 8px 10px;
  border: 0; background: none; border-radius: 6px; cursor: pointer;
}
.results button:hover, .results button.on { background: var(--bg); }
.results small { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--raised); cursor: pointer; font-size: 13px;
}
.chip[aria-pressed="false"] { opacity: 0.45; }
.chip i { width: 8px; height: 8px; border-radius: 50%; background: var(--d-abbasid); display: inline-block; }
.chip[data-filter="scholar"] i { background: var(--c-scholar); }
.tools { display: flex; gap: 6px; margin-inline-start: auto; }
.btn {
  padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--raised); cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.icon { width: 36px; padding: 7px 0; text-align: center; font-weight: 600; }

/* stage */
.stage { position: relative; overflow: hidden; touch-action: none; cursor: grab; user-select: none; background: var(--bg); }
.stage.dragging { cursor: grabbing; }
.layer { position: absolute; inset: 0; }
.grid-line { position: absolute; left: 0; right: 0; height: 0; border-top: 1px solid var(--grid); }
.grid-line.major { border-top-color: var(--line); }
.band { position: absolute; border-radius: 10px; background: color-mix(in srgb, var(--c) 8%, transparent); border: 1px solid color-mix(in srgb, var(--c) 22%, transparent); }

.axis {
  position: absolute; top: 0; bottom: 0; right: 0; width: 84px; background: var(--surface);
  border-left: 1px solid var(--line); z-index: 6; padding-right: env(safe-area-inset-right, 0px);
}
.tick { position: absolute; right: 10px; transform: translateY(-50%); text-align: right; line-height: 1.15; font-variant-numeric: tabular-nums; }
.tick b { display: block; font-size: 13px; font-weight: 600; }
.tick small { display: block; font-size: 10.5px; color: var(--muted); }
.axis-cap { position: absolute; top: 0; right: 0; width: 84px; height: 52px; display: grid; place-content: center; text-align: center; font-size: 11px; color: var(--muted); background: var(--surface); border-bottom: 1px solid var(--line); z-index: 2; line-height: 1.3; }

.heads { position: absolute; top: 0; left: 0; right: 84px; height: 52px; background: color-mix(in srgb, var(--surface) 92%, transparent); border-bottom: 1px solid var(--line); z-index: 5; overflow: hidden; }
.head { position: absolute; top: 0; height: 52px; display: flex; flex-direction: column; justify-content: center; padding-inline: 10px; box-sizing: border-box; border-inline-start: 1px solid var(--line); }
.head strong { font-family: var(--f-display); font-weight: 600; font-size: 15px; color: var(--c, var(--ink)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.head small { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; }

.item { position: absolute; border: 0; padding: 0; margin: 0; background: none; cursor: pointer; text-align: right; color: var(--ink); }
.ruler {
  background: color-mix(in srgb, var(--c) 16%, var(--raised)); border-right: 4px solid var(--c);
  border-radius: 4px; overflow: hidden; box-sizing: border-box; padding: 0 8px; display: flex; flex-direction: column; justify-content: center;
}
.ruler:nth-child(even) { background: color-mix(in srgb, var(--c) 26%, var(--raised)); }
.ruler .n { font-family: var(--f-body); font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.3; }
.ruler .y { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ruler .cap { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ruler.thin .n, .ruler.thin .y, .ruler.thin .cap { display: none; }
.ruler.short .y, .ruler.short .cap { display: none; }
.item.review::after { content: ""; position: absolute; top: 4px; left: 4px; width: 6px; height: 6px; border-radius: 50%; background: var(--c-calamity); }

.event { display: flex; align-items: center; gap: 6px; height: 24px; transform: translateY(-50%); white-space: nowrap; }
.event .dot { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; background: color-mix(in srgb, var(--c) 18%, var(--raised)); border: 1.5px solid var(--c); flex: none; }
.event .t { font-size: 12.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; background: color-mix(in srgb, var(--bg) 80%, transparent); padding-inline: 4px; border-radius: 4px; }
.event .t em { font-style: normal; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 11px; margin-inline-start: 4px; }

.scholar { display: flex; gap: 6px; }
.event.crowd .t, .scholar.crowd .lbl { display: none; }
.event.crowd:hover .t, .event.crowd:focus-visible .t { display: block; position: relative; z-index: 4; }
.scholar .life { width: 4px; border-radius: 2px; background: var(--c-scholar); flex: none; min-height: 10px; }
.scholar .life.approx { background: repeating-linear-gradient(to bottom, var(--c-scholar) 0 6px, transparent 6px 9px); }
.scholar .lbl { font-size: 12.5px; line-height: 1.35; white-space: nowrap; overflow: hidden; }
.scholar .lbl b { font-family: var(--f-body); font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; }
.scholar .lbl small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; }

.item.sel { outline: 2px solid var(--focus); outline-offset: 2px; z-index: 3; }
.hint { position: absolute; bottom: 12px; left: max(12px, env(safe-area-inset-left, 0px)); z-index: 7; background: var(--raised); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; color: var(--muted); font-size: 12px; pointer-events: none; }

/* footer */
.foot {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; justify-content: space-between;
  padding-block: 8px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--surface); border-top: 1px solid var(--line); font-size: 12.5px; color: var(--muted);
}
.foot .acts { display: flex; gap: 8px; }

/* drawer */
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(420px, 100vw); z-index: 40; background: var(--raised);
  box-shadow: var(--shadow); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  transform: translateX(-104%); transition: transform 0.22s ease; padding-left: env(safe-area-inset-left, 0px);
}
.drawer.open { transform: none; }
.drawer header { display: flex; align-items: flex-start; gap: 12px; padding: 18px 20px 12px; border-bottom: 1px solid var(--line); border-top: 5px solid var(--c, var(--accent)); }
.drawer header > div { flex: 1; min-width: 0; }
.kicker { font-size: 12px; color: var(--c, var(--muted)); font-weight: 600; letter-spacing: 0; }
.drawer h2 { font-family: var(--f-display); font-weight: 600; font-size: 26px; margin: 4px 0 2px; text-wrap: balance; line-height: 1.25; }
.drawer .full { font-family: var(--f-body); color: var(--muted); font-size: 15px; }
.drawer .body { overflow: auto; padding: 16px 20px 24px; display: flex; flex-direction: column; gap: 18px; font-family: var(--f-body); font-size: 16px; line-height: 1.8; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-family: var(--f-ui); font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-variant-numeric: tabular-nums; min-width: 0; }
.drawer h3 { font-family: var(--f-ui); font-size: 12.5px; color: var(--muted); font-weight: 600; margin: 0 0 6px; }
.drawer ul { margin: 0; padding-inline-start: 20px; }
.note { background: var(--bg); border-radius: 8px; padding: 10px 12px; font-size: 14.5px; line-height: 1.7; }
.status { display: inline-flex; align-items: center; gap: 6px; font-family: var(--f-ui); font-size: 12px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--c-calamity); }
.peers { display: flex; flex-wrap: wrap; gap: 6px; font-family: var(--f-ui); }
.peers button { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 3px 8px; cursor: pointer; font-size: 13px; }
.peers button i { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--c); margin-inline-end: 5px; }
.srcs { font-size: 14px; }
.srcs li span { color: var(--muted); }
.close { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); padding: 0 4px; line-height: 1; }
.drawer footer { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; gap: 8px; }

/* modal */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(10, 16, 14, 0.45); display: grid; place-items: center; padding: 16px; }
.sheet {
  background: var(--raised); border-radius: 12px; box-shadow: var(--shadow); width: min(640px, 100%); max-height: calc(100% - 32px);
  display: flex; flex-direction: column; overflow: hidden;
}
.sheet > header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.sheet > header h2 { font-family: var(--f-display); font-weight: 600; font-size: 22px; margin: 0; }
.sheet .content { overflow: auto; padding: 18px 20px 22px; }
.prose { font-family: var(--f-body); font-size: 16px; line-height: 1.9; max-width: 65ch; }
.prose h3 { font-family: var(--f-display); font-weight: 600; font-size: 18px; margin: 22px 0 6px; }
.prose h3:first-child { margin-top: 0; }
.prose ol, .prose ul { padding-inline-start: 22px; margin: 6px 0; }
.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.form input, .form select, .form textarea { padding: 9px 11px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-size: 14.5px; }
.form textarea { min-height: 110px; resize: vertical; font-family: var(--f-body); line-height: 1.7; }
.form .hp { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }
.form .row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.msg { border-radius: 8px; padding: 10px 12px; font-size: 14px; background: var(--bg); }
.msg.ok { border-inline-start: 4px solid var(--d-fatimid); }
.msg.warn { border-inline-start: 4px solid var(--c-calamity); }

@media (max-width: 720px) {
  .brand span { display: none; }
  .bar { gap: 8px 10px; }
  .chips { flex-wrap: nowrap; overflow-x: auto; flex: 1 1 100%; order: 3; scrollbar-width: none; }
  .chip { flex: none; }
  .tools { order: 1; }
  .tools .btn.text { display: none; }
  .drawer { top: auto; height: 78%; width: 100%; border-right: 0; border-radius: 14px 14px 0 0; transform: translateY(104%); padding-bottom: env(safe-area-inset-bottom, 0px); }
  .drawer.open { transform: none; }
  .hint { display: none; }
}
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }
