/* ── Reset & base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --ink:       #1a1a18;
  --paper:     #f5f2ea;
  --rule:      #c8c0ae;
  --accent:    #8b1a1a;
  --muted:     #6b6355;
  --th-bg:     #e8e3d8;
  --font-serif: Georgia, "Times New Roman", serif;
  --font-mono:  "Courier New", Courier, monospace;
}

html { font-size: 16px; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  line-height: 1.5;
  padding: 0 0 4rem;
}

/* ── Header ────────────────────────────────────────────────────────── */
header {
  border-bottom: 2px solid var(--ink);
  padding: 1.5rem 1.25rem 1rem;
  margin-bottom: 1.5rem;
}

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

header p.subtitle {
  font-size: 0.875rem;
  color: var(--muted);
  margin-top: 0.25rem;
  font-style: italic;
}

/* ── Sticky controls bar ───────────────────────────────────────────── */
#controls {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--paper);
  border-bottom: 1px solid var(--rule);
  padding: 0.4rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.stat-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 2rem;
  padding-top: 0.3rem;
}

.stat-group:nth-child(2) {
  border-top: 1px solid var(--rule);
  padding-top: 0.5rem;
  margin-top: 0.2rem;
}

.stat-group-label {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink);
  white-space: nowrap;
  min-width: auto;
}

.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  flex: 1;
  padding-bottom: 2px; /* room for focus outline */
}

.stat-btn {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
  flex-shrink: 0;
  padding: 0.15rem 0.45rem;
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--rule);
  cursor: pointer;
  line-height: 1.5;
}

.stat-btn:hover { background: var(--rule); }

.stat-btn:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.stat-btn.active {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

/* ── Main content ──────────────────────────────────────────────────── */
main {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 1.25rem;
}

/* ── Section labels ────────────────────────────────────────────────── */
.chart-section,
#table-section {
  margin-top: 2.5rem;
}

.chart-section h2,
#table-section h2 {
  font-size: 1rem;
  font-weight: bold;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--rule);
  padding-bottom: 0.35rem;
}

#chart-title {
  font-size: 1.15rem;
  color: var(--ink);
  border-bottom: 2px solid var(--accent);
  padding-bottom: 0.5rem;
  margin-bottom: 1.2rem;
  margin-bottom: 1rem;
}

/* ── Chart containers ──────────────────────────────────────────────── */
.chart-wrap {
  position: relative;
  width: 100%;
}

#chart-area-wrap { height: 400px; }

/* ── Summary table ─────────────────────────────────────────────────── */

.table-scroll {
  overflow-x: auto;
}

#summary-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

#summary-table th {
  background: var(--th-bg);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: left;
  padding: 0.45rem 0.6rem;
  border-top: 2px solid var(--ink);
  border-bottom: 1px solid var(--rule);
  white-space: normal;
  cursor: pointer;
  user-select: none;
}

#summary-table th:hover { background: var(--rule); }

#summary-table th.sort-asc::after  { content: " ▲"; font-size: 0.65rem; }
#summary-table th.sort-desc::after { content: " ▼"; font-size: 0.65rem; }

#summary-table td {
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--rule);
  vertical-align: top;
}

#summary-table td.num {
  font-family: var(--font-mono);
  text-align: right;
  white-space: nowrap;
}

#summary-table th:first-child,
#summary-table td:first-child { white-space: nowrap; }

#summary-table th:nth-child(6) { width: 4.5rem; }

#summary-table tbody tr { cursor: pointer; }
#summary-table tbody tr:hover td { background: #ede9df; }
#summary-table tbody tr.selected td { background: #ddeaf5; }

/* ── Column header info icons + tooltips ───────────────────────────── */
.th-info {
  display: inline-block;
  position: relative;
  font-size: 0.65rem;
  opacity: 0.45;
  cursor: help;
  vertical-align: middle;
  margin-left: 0.2em;
  font-style: normal;
  text-transform: none;
  letter-spacing: 0;
  line-height: 1;
}

.th-info:hover { opacity: 0.9; }

.th-info::after {
  content: attr(data-tooltip);
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  width: 200px;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-serif);
  font-size: 0.72rem;
  font-weight: normal;
  text-transform: none;
  letter-spacing: 0;
  line-height: 1.4;
  padding: 0.4rem 0.55rem;
  white-space: normal;
  text-align: left;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 200;
}

#summary-table th:hover .th-info::after { opacity: 1; }

/* ── Chart legend ──────────────────────────────────────────────────── */
.chart-legend {
  display: flex;
  gap: 2rem;
  justify-content: center;
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.8rem;
  padding: 0 1.25rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.legend-bar {
  width: 12px;
  height: 12px;
  background: var(--ink);
  opacity: 0.5;
}

.legend-line {
  width: 12px;
  height: 2px;
  background: rgba(20, 20, 20, 0.75);
}

/* ── Chart tooltip ─────────────────────────────────────────────────── */
#chart-tooltip {
  position: fixed;
  z-index: 300;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-serif);
  font-size: 0.75rem;
  line-height: 1.55;
  padding: 0.4rem 0.6rem;
  white-space: nowrap;
}

/* horizontal arrow pointing toward the line; side chosen by JS */
#chart-tooltip.tt-left::after,
#chart-tooltip.tt-right::after {
  content: '';
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
}

#chart-tooltip.tt-left::after {
  right: -7px;
  border-left: 7px solid var(--ink);
}

#chart-tooltip.tt-right::after {
  left: -7px;
  border-right: 7px solid var(--ink);
}

#chart-tooltip.tt-locked {
  border-top: 2px solid var(--accent);
}

#chart-hover-tooltip,
#chart-hover-tooltip-2 {
  position: fixed;
  z-index: 301;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  line-height: 1.4;
  padding: 0.2rem 0.4rem;
  white-space: nowrap;
  text-align: center;
}

#chart-leader-line {
  position: fixed;
  z-index: 300;
  pointer-events: none;
  width: 1px;
  opacity: 0;
  background: rgba(26, 26, 24, 0.35);
  transition: opacity 0.1s;
}

#chart-hover-tooltip::after,
#chart-hover-tooltip-2::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid var(--ink);
}

.tt-year {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-weight: bold;
  margin-bottom: 0.15rem;
}

.tt-row { color: rgba(245,242,234,0.85); }

/* ── Loading / error states ────────────────────────────────────────── */
#status {
  padding: 1rem 1.25rem;
  font-style: italic;
  color: var(--muted);
  display: none;
}

#status.visible { display: block; }
#status.error   { color: var(--accent); font-style: normal; }

/* ── Footer ────────────────────────────────────────────────────────── */
footer {
  max-width: 900px;
  margin: 3rem auto 0;
  padding: 1rem 1.25rem 0;
  border-top: 1px solid var(--rule);
  font-size: 0.75rem;
  color: var(--muted);
  font-style: italic;
}

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  header h1 { font-size: 1.35rem; }
  #chart-area-wrap { height: 280px; }
}
