:root {
  --space: #000000;
  --bg: #000000;
  --panel: #141820;
  --border: #2a3344;
  --text: #e8edf4;
  --muted: #8b9bb0;
  --empirical: #6ba3ff;
  --values: #f0a04d;
  --prediction: #4fd4c8;
  --accent: #a78bfa;
  --glass: rgba(12, 16, 24, 0.82);
  --glass-border: rgba(255, 255, 255, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  /* No grey flash on tap, and don't let the panel's scroll chain to the page. */
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#layout {
  display: grid;
  grid-template-columns: 1fr 400px;
  height: 100vh; /* fallback */
  height: 100dvh; /* dvh tracks the visible area, avoiding iOS Safari bar clipping */
}

#viewport { position: relative; min-width: 0; background: var(--space); cursor: grab; }
#viewport:active { cursor: grabbing; }
/* touch-action:none → the globe owns all touch gestures (orbit/pinch/tap), so
   the browser never steals a drag as a page scroll or a pinch as page zoom. */
canvas { display: block; background: var(--space); touch-action: none; }

#overlay { position: absolute; inset: 0; pointer-events: none; z-index: 2; }

.globe-label-layer {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: none;
  z-index: 3;
}

.label {
  position: absolute;
  transform: translate(-50%, -135%);
  padding: 4px 8px 3px;
  border-radius: 5px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  color: var(--text);
  font-size: 11px;
  max-width: 280px;
  pointer-events: none;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
  display: none;
  line-height: 1.35;
}
.label-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.label-region {
  font-size: 9px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

/* Globe labels (CSS2DRenderer): pinned to the sphere, always upright like Google Earth. */
.globe-label {
  pointer-events: none;
  white-space: nowrap;
  transform: translate(-50%, -50%);
  user-select: none;
}

.globe-label-continent {
  font-family: Georgia, "Palatino Linotype", "Times New Roman", serif;
  font-size: 15px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: #f2f6fc;
  text-shadow:
    0 0 3px #000,
    0 0 8px #000,
    0 1px 2px #000,
    0 0 12px rgba(0, 0, 0, 0.85);
}

.globe-label-region {
  font-family: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: #c5d0e0;
  text-shadow:
    0 0 2px #000,
    0 0 6px #000,
    0 1px 2px #000;
}

#header {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  left: max(18px, env(safe-area-inset-left));
  z-index: 2;
  pointer-events: none;
}
#header h1 {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #f5f8fc;
  text-shadow: 0 0 12px rgba(0, 0, 0, 0.9), 0 1px 3px rgba(0, 0, 0, 0.8);
}
#header p { margin: 0; color: var(--muted); font-size: 13px; max-width: 460px; line-height: 1.45; }
#header-links { margin-top: 10px; pointer-events: auto; font-size: 14px; }
#header-links a {
  display: inline-block;
  color: #cfe3ff;
  font-weight: 600;
  text-decoration: none;
  padding: 5px 12px;
  border: 1px solid #2f6df0;
  border-radius: 6px;
  background: rgba(47, 109, 240, 0.15);
}
#header-links a:hover { color: #fff; background: rgba(47, 109, 240, 0.35); }
#header-links .sep { display: none; }

#legend {
  position: absolute;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 2;
  padding: 10px 14px;
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  color: var(--text);
  pointer-events: none;
  max-width: 380px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

#cluster-legend { pointer-events: auto; }

.legend-title {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
}

#cluster-legend {
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: auto;
}
.legend-continent { display: flex; flex-direction: column; gap: 4px; }
.legend-item { display: flex; align-items: center; gap: 7px; line-height: 1.3; }
.legend-item.jumpable {
  cursor: pointer;
  border-radius: 6px;
  margin: 0 -6px;
  padding: 3px 6px;
  transition: background 0.15s ease;
}
.legend-item.jumpable:hover { background: rgba(167, 139, 250, 0.15); }
.legend-item.jumpable:focus-visible {
  outline: 2px solid rgba(167, 139, 250, 0.55);
  outline-offset: 1px;
}
.legend-item.jumpable.active {
  background: rgba(167, 139, 250, 0.22);
  font-weight: 600;
}
.legend-item.sub {
  padding-left: 18px;
  font-size: 11px;
  color: var(--muted);
}
.legend-item.sub.jumpable.active { color: var(--text); }
.legend-item.sub .swatch { width: 9px; height: 9px; }
.swatch { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 auto; }

/* Collapsible legend: the title row is a toggle button. Collapsed, only the
   row shows, so the legend can't cover the globe or swallow taps on posts. */
#legend-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  pointer-events: auto;
}
#legend-toggle .legend-title { margin: 0; }
.legend-caret { color: var(--muted); font-size: 11px; transition: transform 0.2s ease; }
#legend.collapsed .legend-caret { transform: rotate(-90deg); }
#legend-body { margin-top: 8px; }
#legend.collapsed #legend-body { display: none; }

#hint {
  position: absolute;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 2;
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
  text-shadow: 0 0 8px rgba(0, 0, 0, 0.9), 0 1px 2px rgba(0, 0, 0, 0.8);
}

#zoom-control {
  position: absolute;
  right: max(18px, env(safe-area-inset-right));
  bottom: 52px;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  color: var(--muted);
}

#zoom-control .zoom-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-right: 2px;
  user-select: none;
}

#zoom-control button {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--glass-border);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

#zoom-control button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#zoom-slider {
  width: 110px;
  height: 4px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

@media (max-width: 900px) {
  #zoom-control {
    bottom: auto;
    top: max(18px, env(safe-area-inset-top));
    right: max(18px, env(safe-area-inset-right));
  }
  /* Compact on phones: just the +/- buttons (pinch-to-zoom is the primary
     gesture). The full-width label + slider overlapped the header title. */
  #zoom-control .zoom-label,
  #zoom-control #zoom-slider { display: none; }
}

#year-control {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: var(--panel);
  font-size: 12px;
  color: var(--muted);
}

/* Mobile bottom-sheet handle: tap to expand/collapse the details panel so the
   globe isn't permanently sharing the screen with an empty panel. Hidden on
   desktop, where the panel is a fixed side column. */
#panel-handle { display: none; }
.panel-grabber {
  width: 44px; height: 5px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.3); align-self: center;
  transition: background 0.15s ease;
}
#panel-handle:active .panel-grabber,
#layout.panel-open .panel-grabber { background: rgba(255, 255, 255, 0.5); }
.panel-handle-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
#panel-handle-label {
  font-size: 13px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.panel-caret { color: var(--muted); font-size: 12px; transition: transform 0.2s ease; }
#layout.panel-open .panel-caret { transform: rotate(180deg); }

#year-control .zoom-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  user-select: none;
}

#year-readout {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  min-width: 86px;
  text-align: right;
  user-select: none;
}

#year-range { position: relative; flex: 1; min-width: 90px; height: 22px; }

.yr-track, .yr-fill {
  position: absolute;
  top: 50%;
  height: 4px;
  transform: translateY(-50%);
  border-radius: 2px;
}
.yr-track { left: 0; right: 0; background: rgba(255, 255, 255, 0.14); }
.yr-fill { background: var(--accent); }

#year-range input[type="range"] {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 22px;
  margin: 0;
  background: none;
  pointer-events: none;
  -webkit-appearance: none;
  appearance: none;
}
#year-range input[type="range"]::-webkit-slider-runnable-track { background: none; border: none; }
#year-range input[type="range"]::-moz-range-track { background: none; border: none; }
#year-range input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #0c1018;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  pointer-events: auto;
}
#year-range input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #0c1018;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  pointer-events: auto;
}

#panel { border-left: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; overflow: hidden; }
#panel-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 22px 20px; }
#panel h2 { margin: 0 0 6px; font-size: 17px; line-height: 1.35; }
#panel .placeholder { color: var(--muted); font-size: 14px; line-height: 1.55; }

.meta-line { color: var(--muted); font-size: 12.5px; margin: 0 0 16px; }
.meta-line a { color: var(--accent); text-decoration: none; }
.meta-line a:hover { text-decoration: underline; }

.section { margin-bottom: 18px; }
.section h3 {
  margin: 0 0 8px; font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
}

.claims-list { margin: 0; padding-left: 18px; }
.claims-list li { font-size: 13.5px; line-height: 1.5; margin-bottom: 6px; }

.comment-card {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
}
.comment-head { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.comment-head strong { color: var(--text); }
.comment-claim { font-size: 13.5px; line-height: 1.5; margin: 0; }

.stance {
  display: inline-block; margin-left: 8px; padding: 1px 7px; border-radius: 999px;
  font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
}
.stance.disagree { background: rgba(225, 87, 89, 0.16); color: #c0383a; }
.stance.agree { background: rgba(89, 161, 79, 0.16); color: #3d8a5c; }

.badge {
  display: inline-block; padding: 3px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.04em; margin-bottom: 12px;
}
.badge.empirical { background: rgba(37, 99, 235, 0.16); color: var(--empirical); }
.badge.values { background: rgba(180, 83, 9, 0.16); color: var(--values); }
.badge.prediction { background: rgba(13, 148, 136, 0.16); color: var(--prediction); }

.crux-box {
  padding: 14px; border: 1px solid var(--accent); border-radius: 10px;
  background: rgba(167, 139, 250, 0.08);
}
.crux-question { font-size: 15px; line-height: 1.5; margin: 0 0 12px; }
.crux-evidence h4 {
  margin: 10px 0 4px; font-size: 10.5px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.05em;
}
.crux-evidence blockquote {
  margin: 0; padding-left: 12px; border-left: 3px solid var(--border);
  font-size: 13px; line-height: 1.5; color: var(--text);
}

#error {
  position: absolute; inset: 0; display: none; place-items: center;
  padding: 24px; background: rgba(0, 0, 0, 0.92); z-index: 10; text-align: center;
  color: var(--text);
}
#error.visible { display: grid; }

/* Loading overlay: the render loop only starts once the ~1.9 MB graph has
   fetched and parsed, so without this the viewport is just black for a beat.
   Shown by default; hidden once the scene builds (or replaced by #error). */
#loading {
  position: absolute; inset: 0; z-index: 9;
  display: grid; place-items: center; gap: 14px;
  background: var(--space); color: var(--muted);
  transition: opacity 0.3s ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
#loading .spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
#loading .loading-text { font-size: 13px; letter-spacing: 0.02em; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  #loading .spinner { animation: none; border-top-color: var(--accent); }
  #loading { transition: none; }
}

@media (max-width: 900px) {
  /* Collapsed by default: the panel is just its handle + year filter, so the
     globe gets almost the whole screen. Tapping a post (or the handle) opens
     the sheet to its default reading height; dragging the handle resizes it. */
  #layout { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  /* The open height is driven by --sheet-h, which the bottom-sheet drag handler
     sets live as the finger moves; it falls back to the default reading height
     until the user drags. We transition the track height only while snapping to
     a rest position on release (.sheet-snapping), never during the drag, so the
     sheet tracks the finger 1:1. */
  #layout.panel-open { grid-template-rows: 1fr var(--sheet-h, 60dvh); }
  #layout.panel-open.sheet-snapping { transition: grid-template-rows 0.22s ease; }
  #panel { border-left: none; border-top: 1px solid var(--border); }
  #panel-handle {
    display: flex; flex-direction: column; gap: 6px;
    width: 100%; margin: 0; padding: 8px 16px 10px;
    border: none; background: var(--panel); color: var(--text);
    font: inherit; text-align: left; cursor: grab;
    /* The handle owns vertical drags so the browser won't scroll the page. */
    touch-action: none; -webkit-user-select: none; user-select: none;
  }
  #panel-handle:active { cursor: grabbing; }
  #panel-body { display: none; }
  #layout.panel-open #panel-body { display: block; }
  #header h1 { font-size: 18px; }
  #header p { font-size: 12px; max-width: 62vw; }
  #header-links { font-size: 13px; }
  #legend {
    max-width: min(340px, calc(100vw - 24px));
    max-height: 56vh;
    padding: 8px 10px;
    font-size: 11px;
  }
  #hint { max-width: 52vw; text-align: right; font-size: 11px; }
}

/* Touch devices: enlarge tap targets that are otherwise sized for a mouse. */
@media (pointer: coarse) {
  #zoom-control { gap: 10px; }
  #zoom-control button { width: 34px; height: 34px; font-size: 18px; }
  #year-range, #year-range input[type="range"] { height: 30px; }
  #year-range input[type="range"]::-webkit-slider-thumb { width: 22px; height: 22px; }
  #year-range input[type="range"]::-moz-range-thumb { width: 22px; height: 22px; }
  #header-links a { padding: 9px 14px; }
  .legend-item.jumpable { padding: 7px 6px; }
  .legend-item.sub { padding-left: 16px; }
  #legend-toggle { padding: 4px 0; }
  #panel-handle { padding-top: 12px; padding-bottom: 12px; }
}

/* ===== Accessibility additions (appended) ===================================
   Keyboard post finder, focus-visible outlines for every control, and a
   reduced-motion block. Appended at the end to keep the merge surface small. */

/* Post finder: a collapsible, keyboard-navigable list of all posts so keyboard
   and screen-reader users can reach a post's details without clicking the
   canvas. Styled to match the legend; collapsed by default. */
#post-finder {
  position: absolute;
  /* Top-left, clear of the header above and the legend at bottom-left, so its
     collapsed row never overlaps another control's hit area. */
  left: max(18px, env(safe-area-inset-left));
  top: calc(max(18px, env(safe-area-inset-top)) + 104px);
  z-index: 3;
  width: min(280px, 50vw);
  max-width: 280px;
  padding: 10px 12px;
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  background: var(--glass);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  color: var(--text);
  /* The container must not swallow taps meant for posts behind it; only its
     controls are interactive (same pattern as #legend). */
  pointer-events: none;
}
#post-finder-toggle,
#post-finder-search,
#post-finder-list { pointer-events: auto; }
#post-finder-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
#post-finder-toggle .legend-title { margin: 0; }
/* Compact pill label for phones; the media query below swaps it in. */
#post-finder .finder-title-compact { display: none; }
#post-finder.collapsed #post-finder-body { display: none; }
#post-finder-body { margin-top: 8px; }
#post-finder-search {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 8px;
  margin-bottom: 6px;
  border: 1px solid var(--glass-border);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font: inherit;
}
#post-finder-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 40vh;
  overflow-y: auto;
}
#post-finder-list .post-option {
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#post-finder-list .post-option:hover { background: rgba(167, 139, 250, 0.15); }
#post-finder-list .post-option[aria-selected="true"] {
  background: rgba(167, 139, 250, 0.22);
  font-weight: 600;
}
#post-finder-empty { color: var(--muted); padding: 4px 8px; }

@media (max-width: 900px) {
  /* On phones the full finder box crowded the map (it was hidden outright for
     a while for that reason). Collapsed, it is now just a compact search pill
     tucked under the zoom control — clear of the title, legend and hint.
     Expanded, the box becomes a solid overlay pinned near the top, above the
     canvas and the other controls, with the list scrolling internally. */
  #post-finder {
    left: auto;
    right: max(18px, env(safe-area-inset-right));
    top: calc(max(18px, env(safe-area-inset-top)) + 64px);
    width: auto;
    max-width: none;
    padding: 8px 12px;
  }
  #post-finder.collapsed .finder-title-full { display: none; }
  #post-finder.collapsed .legend-caret { display: none; }
  #post-finder.collapsed .finder-title-compact { display: inline; }
  #post-finder:not(.collapsed) {
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    top: max(12px, env(safe-area-inset-top));
    z-index: 6;
    padding: 10px 12px;
    background: var(--panel);
    /* The overlay owns every tap inside it: unlike the desktop pass-through
       box, taps on its padding must not fall through to posts behind it. */
    pointer-events: auto;
  }
  #post-finder:not(.collapsed) #post-finder-list {
    max-height: min(50vh, calc(100dvh - 180px));
  }
}

/* Focus visibility: a consistent, high-contrast outline on every interactive
   control for keyboard users (legend rows already have their own above). */
#legend-toggle:focus-visible,
#post-finder-toggle:focus-visible,
#post-finder-search:focus-visible,
#post-finder-list .post-option:focus-visible,
#zoom-control button:focus-visible,
#zoom-slider:focus-visible,
#yr-min:focus-visible,
#yr-max:focus-visible,
#panel-handle:focus-visible,
#header-links a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}
/* Range thumbs render the outline on the thumb itself in some engines. */
#zoom-slider:focus-visible::-webkit-slider-thumb,
#yr-min:focus-visible::-webkit-slider-thumb,
#yr-max:focus-visible::-webkit-slider-thumb {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Reduced motion: drop CSS transitions/animations that are purely decorative
   (the legend caret rotation, panel snapping, hover fades, finder rows). The
   globe still renders; JS handles the camera-tween/damping opt-out. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
