/* === Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f8f9fa;
  --fg: #1a1a2e;
  --accent: #2563eb;
  --accent-light: #dbeafe;
  --win: #6b7280;
  --lose: #dc2626;
  --democracy: #2563eb;
  --autocracy: #d97706;
  --border: #e5e7eb;
  --card-bg: #ffffff;
  --section-alt: #f1f5f9;
  --radius: 8px;
}

html { scroll-behavior: smooth; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.6;
  font-size: 16px;
}

.container { max-width: 1100px; margin: 0 auto; padding: 0 24px; }

/* === Hero === */
.hero {
  background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
  color: #fff;
  padding: 60px 0 40px;
  text-align: center;
}
.hero h1 { font-size: 2rem; font-weight: 700; margin-bottom: 8px; }
.hero .authors { font-size: 1.1rem; opacity: 0.9; margin-bottom: 4px; }
.hero .subtitle { font-size: 0.95rem; opacity: 0.75; margin-bottom: 24px; }

.nav-pills { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.nav-pills a {
  color: #fff;
  text-decoration: none;
  padding: 6px 16px;
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 20px;
  font-size: 0.85rem;
  transition: background 0.2s;
}
.nav-pills a:hover { background: rgba(255,255,255,0.15); }

/* === Sections === */
.section { padding: 48px 0; }
.section.alt { background: var(--section-alt); }
.section h2 { font-size: 1.5rem; margin-bottom: 8px; }
.section-desc { color: #6b7280; margin-bottom: 20px; max-width: 800px; }

/* === Cards === */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-top: 16px; }
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}
.card.highlight { border-color: var(--accent); background: var(--accent-light); }
.card-number { font-size: 1.8rem; font-weight: 700; color: var(--accent); }
.card.highlight .card-number { color: var(--accent); }
.card-label { font-weight: 600; margin: 4px 0; }
.card-detail { font-size: 0.85rem; color: #6b7280; }

/* === Controls === */
.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.controls label { font-weight: 600; font-size: 0.9rem; }

.btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card-bg);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.15s;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

select, input[type="text"] {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
  background: #fff;
}
input[type="text"] { width: 180px; }

/* === Plot === */
.plot-container { width: 100%; min-height: 420px; }

/* === Datasheet === */
.ds-summary {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 16px;
  display: none;
}
.ds-summary h3 { font-size: 1.1rem; margin-bottom: 6px; }
.ds-summary .ds-meta { display: flex; gap: 24px; flex-wrap: wrap; font-size: 0.9rem; color: #6b7280; }
.ds-meta span { display: inline-flex; align-items: center; gap: 4px; }
.ds-meta .tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 600;
}
.tag-visible { background: #fef2f2; color: #dc2626; }
.tag-subtle { background: #f0f9ff; color: #2563eb; }
.tag-won { background: #f3f4f6; color: #6b7280; }
.tag-lost { background: #fef2f2; color: #dc2626; }

#ds-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  display: none;
}
#ds-table th, #ds-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
#ds-table th { background: var(--card-bg); font-weight: 600; white-space: nowrap; }
#ds-table td:nth-child(2) { max-width: 500px; }
#ds-table tr:hover td { background: var(--accent-light); }
.type-visible { color: #dc2626; font-weight: 600; }
.type-subtle { color: #2563eb; }

/* === Table === */
.table-wrap { overflow-x: auto; }
#election-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
#election-table th, #election-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
#election-table th {
  background: var(--card-bg);
  font-weight: 600;
  cursor: pointer;
  position: sticky;
  top: 0;
  user-select: none;
  white-space: nowrap;
}
#election-table th:hover { color: var(--accent); }
#election-table tr:hover td { background: var(--accent-light); }

.outcome-won { color: var(--win); }
.outcome-lost { color: var(--lose); font-weight: 600; }

.table-count { font-size: 0.85rem; color: #6b7280; margin-top: 8px; }

/* === Footer === */
footer {
  background: #1a1a2e;
  color: #cbd5e1;
  text-align: center;
  padding: 32px 0;
  font-size: 0.9rem;
}
footer .small { font-size: 0.8rem; opacity: 0.7; margin-top: 4px; }

/* === Codebook Table === */
.codebook-table-wrap { overflow-x: auto; }
.codebook-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.codebook-table th, .codebook-table td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.codebook-table th {
  background: var(--card-bg);
  font-weight: 600;
  white-space: nowrap;
}
.codebook-table .target-label {
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  width: 120px;
}
.codebook-table .col-subtle { background: #f8fafc; }
.codebook-table .col-visible { background: #fef7f7; }
.codebook-table thead .col-subtle { background: #f0f9ff; color: #2563eb; }
.codebook-table thead .col-visible { background: #fef2f2; color: #dc2626; }
.codebook-table tr:hover td { background: var(--accent-light); }

/* === Index Construction Cards === */
.index-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 16px;
}
.index-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
}
.index-card h3 {
  font-size: 0.95rem;
  margin-bottom: 8px;
  color: var(--accent);
}
.index-card ul { list-style: none; padding: 0; }
.index-card li {
  font-size: 0.85rem;
  padding: 4px 0;
  border-bottom: 1px solid #f1f5f9;
}
.index-card li:last-child { border-bottom: none; }
.index-card li strong { font-family: 'SFMono-Regular', Consolas, monospace; font-size: 0.8rem; color: #6b7280; }
.reverse-tag {
  display: inline-block;
  background: #fef3c7;
  color: #92400e;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 3px;
  margin-left: 4px;
  font-weight: 600;
}

/* === Pillar Cards === */
.pillar-card { text-align: center; }
.pillar-card h3 { margin-bottom: 4px; }
.pillar-card .vdem-code {
  font-family: 'SFMono-Regular', Consolas, monospace;
  font-size: 0.8rem;
  color: #6b7280;
  margin-bottom: 8px;
}
.pillar-card p:last-child { font-size: 0.85rem; color: #6b7280; }

/* === Formula Box === */
.formula-box {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 16px 24px;
  margin-top: 16px;
}
.formula-box p { margin: 4px 0; font-size: 0.9rem; }
.formula-box code {
  font-family: 'SFMono-Regular', Consolas, monospace;
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 0.85rem;
}

/* === Data Sources Table === */
.sources-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.sources-table th, .sources-table td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.sources-table th {
  background: var(--card-bg);
  font-weight: 600;
  white-space: nowrap;
}
.sources-table tr:hover td { background: var(--accent-light); }

/* === Key Concepts === */
.concept-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 20px;
  margin-top: 16px;
}
.concept-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}
.concept-icon {
  font-size: 2rem;
  margin-bottom: 8px;
}
.concept-card h3 {
  font-size: 1.15rem;
  color: var(--accent);
  margin-bottom: 8px;
}
.concept-card p {
  font-size: 0.9rem;
  color: #374151;
  margin-bottom: 8px;
  line-height: 1.6;
}
.concept-detail {
  color: #6b7280 !important;
  font-size: 0.85rem !important;
}
.concept-link a {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
}
.concept-link a:hover { text-decoration: underline; }
.concept-callout {
  background: var(--accent-light);
  border: 1px solid #93c5fd;
  border-radius: var(--radius);
  padding: 20px 24px;
  margin-top: 20px;
}
.concept-callout h4 {
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 6px;
}
.concept-callout p {
  font-size: 0.9rem;
  color: #374151;
  line-height: 1.6;
}

/* === Election Detail Panel === */
.election-detail {
  background: var(--card-bg);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  margin-top: 16px;
  overflow: hidden;
}
.detail-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--accent-light);
  border-bottom: 1px solid var(--border);
}
.detail-header h3 { font-size: 1.1rem; margin: 0; }
.detail-type {
  font-size: 0.8rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  color: #6b7280;
}
.detail-outcome {
  font-size: 0.8rem;
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 4px;
}
.detail-outcome.outcome-lost { background: #fef2f2; color: #dc2626; }
.detail-outcome.outcome-won { background: #f3f4f6; color: #6b7280; }
.detail-close {
  margin-left: auto;
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: #6b7280;
  padding: 0 4px;
}
.detail-close:hover { color: #1a1a2e; }
.detail-body {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 20px;
}
.detail-candidate {
  flex: 1;
  text-align: center;
  padding: 12px;
}
.detail-incumbent { border-right: 1px solid var(--border); }
.candidate-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  color: #9ca3af;
  font-weight: 600;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}
.candidate-name { font-size: 1.05rem; font-weight: 700; color: var(--fg); }
.candidate-party { font-size: 0.85rem; color: #6b7280; margin: 2px 0; }
.candidate-pct { font-size: 1.4rem; font-weight: 700; color: var(--accent); margin-top: 4px; }
.detail-vs {
  font-size: 0.85rem;
  color: #9ca3af;
  font-weight: 600;
  flex-shrink: 0;
}
.detail-footer {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 20px;
  background: #fafbfc;
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
  color: #6b7280;
}
.detail-notes { font-style: italic; }
.detail-source {
  margin-left: auto;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
.detail-source:hover { text-decoration: underline; }

/* Clickable rows */
.clickable-row { cursor: pointer; }
.clickable-row:hover td { background: var(--accent-light) !important; }
.detail-indicator {
  color: var(--accent);
  font-size: 0.8rem;
  opacity: 0.6;
}

/* === Responsive === */
@media (max-width: 640px) {
  .hero h1 { font-size: 1.5rem; }
  .cards { grid-template-columns: repeat(2, 1fr); }
  .controls { flex-direction: column; align-items: flex-start; }
  .index-cards { grid-template-columns: 1fr; }
}
