:root {
  --bg: #f5f6fa;
  --panel: #ffffff;
  --panel-alt: #fafbfc;
  --border: #e4e7ee;
  --text: #171a23;
  --text-dim: #5b6178;
  --text-faint: #9298ab;
  --accent: #4f46e5;
  --accent-soft: #eef0fe;
  --accent-strong: #3730a3;
  --sidebar-bg: #12131c;
  --sidebar-text: #c9cbe0;
  --sidebar-active: #1f2033;
  --green: #12875a;
  --green-bg: #e5f7ee;
  --amber: #b45309;
  --amber-bg: #fef3e0;
  --blue: #1d5fd6;
  --blue-bg: #e8f0fe;
  --gray: #6b7280;
  --gray-bg: #eef0f3;
  --red: #c0273c;
  --red-bg: #fdeced;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 22, 40, 0.04), 0 8px 24px -8px rgba(20, 22, 40, 0.10);
  --shadow-lg: 0 20px 60px -20px rgba(15, 16, 30, 0.35);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: 'JetBrains Mono', monospace; }

.app-shell { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 248px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 20px; }
.brand-mark { font-size: 26px; }
.brand-title { font-weight: 700; font-size: 15px; color: #fff; letter-spacing: -0.01em; }
.brand-sub { font-size: 11px; color: #8b8fae; margin-top: 1px; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav-section-title {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.06em;
  color: #61647f; padding: 16px 12px 6px;
  font-weight: 600;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 9px; cursor: pointer;
  font-size: 13.5px; font-weight: 500; color: var(--sidebar-text);
  transition: background 0.12s ease, color 0.12s ease;
}
.nav-item:hover { background: #1a1c2a; color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; box-shadow: inset 2px 0 0 var(--accent); }
.nav-item .ic { width: 18px; text-align: center; font-size: 14px; opacity: 0.9; }
.nav-item .count { margin-left: auto; font-size: 11px; color: #7a7ea0; font-family: 'JetBrains Mono', monospace; }

.sidebar-footer { padding: 12px 10px 4px; border-top: 1px solid #23243a; margin-top: 8px; }
.paper-ref { font-size: 10.5px; color: #6c7093; line-height: 1.5; }
.paper-ref a { color: #9a9dfb; }

/* ---------- Top bar ---------- */
.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 64px; flex-shrink: 0;
  display: flex; align-items: center; gap: 16px;
  padding: 0 28px; background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.search-wrap { position: relative; flex: 1; max-width: 520px; }
.search-icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--text-faint); font-size: 15px; }
#global-search {
  width: 100%; padding: 9px 14px 9px 36px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--panel-alt);
  font-size: 13.5px; color: var(--text); outline: none;
  transition: border-color 0.12s;
}
#global-search:focus { border-color: var(--accent); background: #fff; }
.search-results {
  position: absolute; top: 44px; left: 0; right: 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow-lg); max-height: 360px; overflow-y: auto; z-index: 40;
}
.search-results .sr-item { padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.search-results .sr-item:last-child { border-bottom: none; }
.search-results .sr-item:hover { background: var(--accent-soft); }
.sr-name { font-weight: 600; font-size: 13px; }
.sr-type { font-size: 11px; color: var(--text-faint); }
.sr-empty { padding: 14px; font-size: 13px; color: var(--text-faint); }

.topbar-actions { display: flex; gap: 10px; }
.btn {
  border: 1px solid var(--border); background: var(--panel);
  padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 600;
  cursor: pointer; color: var(--text); transition: all 0.12s;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-danger { color: var(--red); }
.btn-danger:hover { border-color: var(--red); }

/* ---------- Content ---------- */
.content { flex: 1; padding: 28px; max-width: 1320px; width: 100%; margin: 0 auto; }
.view-header { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; gap: 12px; }
.view-title { font-size: 22px; font-weight: 800; letter-spacing: -0.02em; }
.view-subtitle { font-size: 13px; color: var(--text-dim); margin-top: 4px; max-width: 640px; line-height: 1.5; }

/* ---------- Grids / Cards ---------- */
.grid { display: grid; gap: 16px; }
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.grid-cards { grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
.grid-2 { grid-template-columns: 1fr 1fr; }

.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.stat-card { display: flex; flex-direction: column; gap: 6px; }
.stat-label { font-size: 11.5px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
.stat-value { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }
.stat-icon { font-size: 18px; opacity: 0.7; }

.entity-card {
  cursor: pointer; display: flex; flex-direction: column; gap: 10px;
  transition: transform 0.1s, box-shadow 0.1s, border-color 0.1s;
}
.entity-card:hover { transform: translateY(-2px); box-shadow: 0 12px 28px -10px rgba(20,22,40,0.18); border-color: #d3d7e6; }
.entity-card-top { display: flex; align-items: flex-start; gap: 10px; }
.entity-emoji { font-size: 22px; width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; background: var(--panel-alt); border-radius: 9px; flex-shrink: 0; }
.entity-name { font-weight: 700; font-size: 14.5px; line-height: 1.3; }
.entity-type { font-size: 11px; color: var(--text-faint); margin-top: 2px; }
.entity-summary { font-size: 12.5px; color: var(--text-dim); line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.entity-card-foot { display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 10.5px; font-weight: 700; padding: 3px 8px; border-radius: 999px;
  text-transform: uppercase; letter-spacing: 0.03em; white-space: nowrap;
}
.badge-current { background: var(--green-bg); color: var(--green); }
.badge-proposed { background: var(--amber-bg); color: var(--amber); }
.badge-future { background: var(--blue-bg); color: var(--blue); }
.badge-historical { background: var(--gray-bg); color: var(--gray); }
.badge-deprecated { background: var(--red-bg); color: var(--red); }
.badge-neutral { background: var(--gray-bg); color: var(--gray); }
.badge-outline { background: transparent; border: 1px solid var(--border); color: var(--text-dim); }

.pill { display: inline-block; padding: 3px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-strong); font-size: 11px; font-weight: 600; }

/* ---------- Section / Table ---------- */
.section { margin-bottom: 26px; }
.section-title { font-size: 13.5px; font-weight: 700; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.section-title .muted-count { font-weight: 500; color: var(--text-faint); font-size: 12px; }

table.data-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.data-table th {
  text-align: left; padding: 9px 12px; color: var(--text-faint); font-weight: 600;
  border-bottom: 1px solid var(--border); font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--panel-alt);
}
table.data-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr.clickable { cursor: pointer; }
table.data-table tr.clickable:hover { background: var(--accent-soft); }
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }

/* ---------- Filters ---------- */
.filter-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; align-items: center; }
.filter-bar select, .filter-bar input[type=text] {
  padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border);
  font-size: 12.5px; background: var(--panel); color: var(--text);
}
.filter-chip {
  padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  font-size: 12px; font-weight: 600; cursor: pointer; background: var(--panel); color: var(--text-dim);
}
.filter-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Bars ---------- */
.bar-row { display: grid; grid-template-columns: 160px 1fr 40px; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 12.5px; }
.bar-track { height: 8px; background: var(--panel-alt); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), #7c72f0); border-radius: 4px; }
.bar-label { color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-value { text-align: right; color: var(--text-faint); font-family: 'JetBrains Mono', monospace; font-size: 11.5px; }

/* ---------- Scenario cards ---------- */
.scenario-card { position: relative; overflow: hidden; }
.scenario-cat-existential_risk { border-left: 4px solid var(--red); }
.scenario-cat-positive_outcome { border-left: 4px solid var(--green); }
.scenario-cat-geopolitical { border-left: 4px solid var(--amber); }
.scenario-cat-socioeconomic { border-left: 4px solid var(--blue); }
.indicator-list { margin: 8px 0 0; padding-left: 18px; font-size: 12px; color: var(--text-dim); line-height: 1.6; }

/* ---------- Drawer ---------- */
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(10, 11, 20, 0.4);
  display: flex; justify-content: flex-end; z-index: 100;
  backdrop-filter: blur(1px);
}
.drawer-overlay.hidden, .modal-overlay.hidden { display: none; }
.drawer {
  width: min(560px, 96vw); background: var(--bg); height: 100%; overflow-y: auto;
  box-shadow: var(--shadow-lg); padding: 0;
  animation: slideIn 0.16s ease-out;
}
@keyframes slideIn { from { transform: translateX(24px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.drawer-header {
  position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 18px 24px; display: flex; justify-content: space-between; align-items: flex-start; z-index: 5;
}
.drawer-body { padding: 20px 24px 60px; }
.drawer-close { cursor: pointer; font-size: 20px; color: var(--text-faint); background: none; border: none; line-height: 1; padding: 4px; }
.drawer-close:hover { color: var(--text); }

.kv-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; font-size: 12.5px; margin-top: 12px; }
.kv-grid .k { color: var(--text-faint); font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; }
.kv-grid .v { font-weight: 600; margin-top: 2px; }

.rel-list { display: flex; flex-direction: column; gap: 6px; }
.rel-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; font-size: 12.5px; cursor: pointer; }
.rel-item:hover { border-color: var(--accent); }
.rel-type { color: var(--text-faint); font-size: 11px; }
.rel-arrow { color: var(--text-faint); }

.commentary-item { padding: 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--panel); margin-bottom: 8px; }
.commentary-head { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--text-faint); margin-bottom: 6px; }
.commentary-body { font-size: 13px; line-height: 1.5; }

.timeline-list { border-left: 2px solid var(--border); padding-left: 18px; margin-left: 6px; }
.timeline-item { position: relative; padding-bottom: 18px; }
.timeline-item::before { content: ''; position: absolute; left: -23px; top: 3px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); }
.timeline-item.predicted::before, .timeline-item.proposed::before, .timeline-item.planned::before { background: var(--amber); }
.timeline-date { font-size: 11px; color: var(--text-faint); font-family: 'JetBrains Mono', monospace; }
.timeline-title { font-weight: 700; font-size: 13.5px; margin-top: 2px; }
.timeline-desc { font-size: 12.5px; color: var(--text-dim); margin-top: 3px; line-height: 1.5; }

/* ---------- Modal (new entity form) ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(10,11,20,0.45); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal { width: min(560px, 92vw); max-height: 88vh; overflow-y: auto; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 24px; }
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 12px; font-weight: 700; margin-bottom: 5px; color: var(--text-dim); }
.form-row input, .form-row select, .form-row textarea {
  width: 100%; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border); font-size: 13px; font-family: inherit; background: var(--panel-alt);
}
.form-row textarea { min-height: 70px; resize: vertical; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- Empty / loading ---------- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-faint); }
.empty-state .em-icon { font-size: 32px; margin-bottom: 10px; }
.loading { padding: 40px; text-align: center; color: var(--text-faint); font-size: 13px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #171a23; color: #fff; padding: 10px 18px; border-radius: 9px; font-size: 13px;
  box-shadow: var(--shadow-lg); z-index: 300;
}
.toast.hidden { display: none; }

/* ---------- Jurisdiction flag rows ---------- */
.flag-row { display: flex; align-items: center; gap: 10px; }
.flag-emoji { font-size: 20px; }

/* ---------- Misc ---------- */
.text-faint { color: var(--text-faint); }
.text-dim { color: var(--text-dim); }
.mb-0 { margin-bottom: 0; }
hr.sep { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

@media (max-width: 900px) {
  .sidebar { display: none; }
  .content { padding: 16px; }
  .kv-grid { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
}
