:root {
  --bg-0: #f6f7fb;
  --bg-1: #ffffff;
  --panel-border: #e4e7f2;
  --text: #1b1f2e;
  --text-dim: #767c94;
  --accent: #4f6df5;
  --accent-bg: #eaeeff;
  --equation-border: #d9d3f9;
  --equation-border-highlight: #8b6cf0;
  --shadow: 0 1px 2px rgba(30, 34, 54, 0.04), 0 6px 20px rgba(30, 34, 54, 0.06);
  --radius: 14px;
  --font-ui: "Inter", system-ui, sans-serif;
  --font-brand: "Space Grotesk", var(--font-ui);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100dvh;
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--font-ui);
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 20px 0 22px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--panel-border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-family: var(--font-brand);
}

.brand-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  font-size: 25px;
  line-height: 1;
  font-style: italic;
  color: var(--accent);
  transform: translateY(1px);
}

.brand-name {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
}

.topbar-hint {
  font-size: 12.5px;
  color: var(--text-dim);
}

.github-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  padding: 7px;
  border-radius: 8px;
  color: var(--text-dim);
  transition: color 0.15s ease, background 0.15s ease;
}

.github-link:hover {
  color: var(--text);
  background: var(--accent-bg);
}

.github-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.github-link svg {
  display: block;
}

.layout {
  display: grid;
  grid-template-columns: var(--explore-width, 320px) 6px 1fr 6px var(--curriculum-width, 380px);
  flex: 1;
  min-height: 0;
  position: relative;
}

@media (min-width: 1101px) {
  .layout {
    transition: grid-template-columns 0.22s ease;
  }

  /* The explore pane floats above the graph. */
  .layout {
    grid-template-columns: 1fr 6px var(--curriculum-width, 380px);
  }

  .layout.curriculum-collapsed {
    grid-template-columns: 1fr 0px 0px;
  }

  .panel-explore {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 4;
    width: var(--explore-width, 320px);
    box-shadow: var(--shadow);
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    overflow-x: hidden;
  }

  .layout:not(.explore-collapsed) .panel-explore {
    transform: translateX(0);
  }

  #explore-resizer {
    position: absolute;
    top: 0;
    bottom: 0;
    left: var(--explore-width, 320px);
    width: 6px;
    z-index: 5;
    transition: opacity 0.22s ease;
  }

  .layout.explore-collapsed #explore-resizer {
    opacity: 0;
    pointer-events: none;
  }

  .layout.curriculum-collapsed .panel-curriculum {
    opacity: 0;
    pointer-events: none;
  }

  .panel-curriculum {
    transition: opacity 0.16s ease;
    overflow-x: hidden;
  }

  .layout.curriculum-collapsed #curriculum-resizer {
    pointer-events: none;
  }
}

.graph {
  position: relative;
  min-width: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 1px 1px, #e6e9f5 1px, transparent 1px) 0 0 / 28px 28px,
    var(--bg-0);
}

@property --node-emphasis {
  syntax: "<number>";
  inherits: false;
  initial-value: 1;
}

.equation-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* --node-scale is set on the layer each frame to the view's current zoom (see _syncOverlays), so
   these HTML overlays shrink and grow with the canvas underneath them instead of staying a fixed
   pixel size — otherwise a zoomed-out graph draws full-size cards piled on top of each other. */
.q-hit {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(var(--nx, 0px), var(--ny, 0px)) translate(-50%, -50%) scale(var(--node-scale, 1));
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
  transition: opacity 0.25s ease;
}

.eq-card {
  position: absolute;
  left: 0;
  top: 0;
  /* Without an explicit width, an absolutely-positioned box's shrink-to-fit width is bounded by
     (containing block width - left), so the card's own title would rewrap as `left` changes while
     dragging it toward an edge. max-content ties the width to the content instead. */
  width: max-content;
  transform: translate(var(--nx, 0px), var(--ny, 0px)) translate(-50%, -50%)
    scale(calc(var(--node-scale, 1) * var(--node-emphasis, 1)));
  background: var(--bg-1);
  border: 1.5px solid var(--equation-border);
  border-radius: 11px;
  padding: 8px 13px;
  box-shadow: var(--shadow);
  white-space: nowrap;
  font-size: 17px;
  color: var(--text);
  cursor: grab;
  pointer-events: auto;
  touch-action: none;
  opacity: 1;
  transition: border-color 0.25s ease, box-shadow 0.25s ease, --node-emphasis 0.15s ease, opacity 0.25s ease;
}

.q-hit.dragging,
.eq-card.dragging {
  cursor: grabbing;
  transition: none;
}

.cluster-box-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cluster-box {
  position: absolute;
  border: 1.5px dashed var(--cluster-color, var(--accent));
  background: var(--cluster-bg, transparent);
  border-radius: 26px;
  transition: left 0.1s linear, top 0.1s linear, width 0.1s linear, height 0.1s linear,
    background 0.18s ease, border-style 0.18s ease;
  /* Stays click-through while expanded: the box covers the canvas, and hit-testing can't be handed
     back once it lands, so making this `auto` turned every gap between cards into a dead zone where
     wheel-zoom and drag-to-pan silently did nothing. The label is the drag handle instead (see
     _setupClusterDrag); a collapsed box has no cards left to shadow, so it takes pointers as a tile. */
  pointer-events: none;
}

.cluster-box.dragging {
  cursor: grabbing;
  transition: none;
}

.cluster-box-label {
  position: absolute;
  top: -12px;
  left: 18px;
  padding: 3px 11px;
  border-radius: 999px;
  font-family: var(--font-brand);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fff;
  background: var(--cluster-color, var(--accent));
  white-space: nowrap;
  pointer-events: auto;
  cursor: grab;
  /* Same reason as .eq-card/.q-hit: without this a touch drag is claimed by the browser as a page
     scroll, which fires pointercancel and kills the cluster drag before it ever starts. */
  touch-action: none;
  transition: opacity 0.18s ease;
}

.cluster-box-label:active {
  cursor: grabbing;
}

/* Collapsed: swap the outline+cards presentation for one solid, clickable tile. The label is
   redundant with (and would overlap) the tile's own text, so it's hidden rather than shown twice. */
.cluster-box.collapsed {
  background: var(--cluster-bg-collapsed, var(--cluster-bg));
  border-style: solid;
  pointer-events: auto;
  cursor: grab;
  touch-action: none; /* the whole tile is the drag handle when collapsed - see .cluster-box-label */
}

/* The label doubles as the only drag handle for an expanded cluster, and at its desktop size it is
   a ~20px-tall target - too small to grab reliably with a finger. Coarse pointers get a roomier
   one; the box's own padding ring means this can grow without covering the cards inside. */
@media (pointer: coarse) {
  .cluster-box-label {
    top: -15px;
    padding: 6px 14px;
    font-size: 12px;
  }
}

.cluster-box.collapsed .cluster-box-label {
  opacity: 0;
  pointer-events: none;
}

.cluster-box-summary {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 10px;
  font-family: var(--font-brand);
  font-weight: 700;
  font-size: 14px;
  line-height: 1.3;
  color: var(--cluster-color, var(--accent));
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.cluster-box.collapsed .cluster-box-summary {
  opacity: 1;
  pointer-events: auto;
  cursor: pointer;
}

.eq-card.highlight {
  border-color: var(--equation-border-highlight);
  box-shadow: 0 0 0 3px rgba(139, 108, 240, 0.15), var(--shadow);
  --node-emphasis: 1.08;
}

.eq-card.dim {
  opacity: 0.3;
}

/* A chapter filter hides non-matching nodes outright (as opposed to .dim, which just fades
   them for a single quantity/equation selection while keeping the rest of the graph visible). */
.q-hit.hidden-node,
.eq-card.hidden-node {
  opacity: 0;
  pointer-events: none;
}

/* Zoomed out past CLUSTER_COLLAPSE_ZOOM (see _updateClusterCollapse), individual cards/quantities
   would overlap into unreadable mush - hide them in favor of each cluster's collapsed tile. */
.equation-layer.clusters-collapsed .eq-card,
.equation-layer.clusters-collapsed .q-hit {
  opacity: 0;
  pointer-events: none;
}

.eq-card-label {
  font-family: var(--font-ui);
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 3px;
  white-space: normal;
  max-width: min(230px, calc(100vw - 120px));
}

.panel-resizer {
  position: relative;
  cursor: col-resize;
  background: transparent;
}

.panel-resizer::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
  transform: translateX(-50%);
  background: var(--panel-border);
  transition: background 0.15s ease, width 0.15s ease;
}

.panel-resizer:hover::after,
.panel-resizer.dragging::after {
  width: 3px;
  background: var(--accent, #4f6df5);
}

.panel {
  background: var(--bg-1);
  padding: 22px 20px;
  overflow-y: auto;
  min-width: 0;
}

.panel-body {
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.curriculum-hint {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
}

.curriculum-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.curriculum-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-0);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 9px 12px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.curriculum-row:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.curriculum-row.active {
  border-color: var(--equation-border-highlight);
  background: var(--accent-bg);
}

.curriculum-section {
  font-family: var(--font-brand);
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

.curriculum-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.curriculum-katex {
  font-size: 16px;
}

.curriculum-name {
  font-size: 11.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.curriculum-group-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 18px 0 0;
}

.curriculum-item:first-child .curriculum-group-row {
  margin-top: 0;
}

.curriculum-group-label {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  margin: 0;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease, color 0.15s ease;
}

.curriculum-group-label::after {
  content: "";
}

.curriculum-group-label:hover::after {
  content: "add +";
  font-weight: 500;
  text-transform: none;
  letter-spacing: normal;
  opacity: 0.7;
}

.curriculum-group-label:hover {
  color: var(--accent);
  background: var(--accent-bg);
}

.curriculum-group-label.active {
  color: #fff;
  background: var(--accent);
}

.curriculum-group-label.active:hover::after {
  content: "remove ✕";
}

.curriculum-chevron {
  flex: none;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.curriculum-chevron svg {
  display: block;
  pointer-events: none;
}

.curriculum-chevron:hover {
  background: var(--accent-bg);
  color: var(--accent);
}

.curriculum-chevron:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.curriculum-chevron.expanded {
  transform: rotate(180deg);
}

.curriculum-sublist {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.panel-block h2 {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.curriculum-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.curriculum-header-row h2 {
  margin: 0;
}

.panel-collapse-btn {
  flex: none;
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0 0 4px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transform: rotate(-90deg);
}

.panel-collapse-btn svg {
  display: block;
  pointer-events: none;
}

.panel-collapse-btn:hover {
  background: var(--accent-bg);
  color: var(--accent);
}

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

.curriculum-reopen-tab {
  display: none;
}

@media (min-width: 1101px) {
  .curriculum-reopen-tab {
    display: flex;
    align-items: center;
    gap: 6px;
    position: absolute;
    top: 50%;
    right: 0;
    z-index: 2;
    transform: translateY(-50%);
    padding: 10px 8px;
    border: 1px solid var(--panel-border);
    border-right: none;
    border-radius: 10px 0 0 10px;
    background: var(--bg-1);
    color: var(--text-dim);
    box-shadow: var(--shadow);
    cursor: pointer;
    writing-mode: vertical-rl;
  }

  .curriculum-reopen-tab svg {
    transform: rotate(90deg);
  }

  .curriculum-reopen-tab:hover {
    color: var(--accent);
    border-color: var(--accent);
  }

  .curriculum-reopen-tab span {
    font-size: 11.5px;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }
}

.panel-block.hint p {
  margin: 10px 0 0;
  color: var(--text-dim);
  font-size: 13.5px;
  line-height: 1.6;
}

.detail-header {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 6px;
}

.detail-header h2 {
  margin: 0;
  font-size: 20px;
  text-transform: none;
  letter-spacing: normal;
  color: var(--text);
}

.detail-symbol {
  font-family: var(--font-brand);
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
}

.detail > h2 {
  margin: 0 0 10px;
  font-size: 20px;
}

.detail-katex-big {
  font-size: 22px;
  padding: 14px 0;
  margin-bottom: 6px;
}

.detail-description {
  margin: 0 0 14px;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text-dim);
}

.detail-facts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0 0 20px;
  padding: 12px 14px;
  background: var(--bg-0);
  border-radius: 10px;
}

.detail-facts dt {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.detail-facts dd {
  margin: 2px 0 0;
  font-size: 14px;
  font-weight: 600;
}

.detail-fact-value {
  grid-column: 1 / -1;
}

.help-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-left: 2px;
  border-radius: 50%;
  background: var(--panel-border);
  color: var(--text-dim);
  font-size: 10px;
  font-weight: 700;
  text-decoration: none;
  vertical-align: middle;
}

.help-link:hover {
  background: var(--accent);
  color: #fff;
}

.learn-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 20px;
}

.learn-link {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 5px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.learn-link:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.source-link {
  display: inline-block;
  margin-bottom: 20px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
}

.source-link:hover {
  text-decoration: underline;
}

.detail h3 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.related-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.related-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-0);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 11px 14px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.related-row:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.related-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.related-katex {
  font-size: 16px;
}

.related-subtitle {
  font-size: 11.5px;
  color: var(--text-dim);
}

.related-symbol {
  font-family: var(--font-brand);
  font-weight: 700;
  color: var(--accent);
  font-size: 17px;
  width: 24px;
}

.related-name {
  flex: 1;
  font-size: 15px;
}

.related-role {
  font-size: 11.5px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.related-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.related-unit {
  font-size: 11.5px;
  color: var(--text-dim);
}

.empty-note {
  color: var(--text-dim);
  font-size: 13px;
}

.sheet-handle,
.sheet-scrim,
.fab-chapters {
  display: none;
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .panel-resizer {
    display: none;
  }

  .topbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 3;
    height: 52px;
    padding: 0 12px 0 16px;
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(8px);
    border-bottom-color: transparent;
  }

  .graph {
    grid-area: 1 / 1;
  }

  .panel-explore,
  .panel-curriculum {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 5;
    height: 88dvh;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 32px rgba(30, 34, 54, 0.16);
    padding: 0 18px calc(20px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    border-top: none;
  }

  .layout[data-sheet="detail"] .panel-explore,
  .layout[data-sheet="chapters"] .panel-curriculum {
    transform: translateY(var(--sheet-peek, 58%));
  }

  .layout.sheet-full[data-sheet="detail"] .panel-explore,
  .layout.sheet-full[data-sheet="chapters"] .panel-curriculum {
    transform: translateY(0);
  }

  .panel-explore.dragging,
  .panel-curriculum.dragging {
    transition: none;
    transform: translateY(var(--sheet-y));
  }

  .sheet-handle {
    display: block;
    position: sticky;
    top: 0;
    width: 100%;
    height: 22px;
    background: inherit;
    touch-action: none;
    cursor: grab;
  }

  .sheet-handle span {
    display: block;
    width: 36px;
    height: 4px;
    margin: 9px auto 0;
    border-radius: 999px;
    background: var(--panel-border);
  }

  .sheet-scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 4;
    background: rgba(20, 23, 38, 0.28);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.28s ease;
  }

  .sheet-scrim.visible {
    opacity: 1;
    pointer-events: auto;
  }

  .fab-chapters {
    display: block;
    position: fixed;
    right: 16px;
    bottom: calc(18px + env(safe-area-inset-bottom));
    z-index: 3;
    min-height: 44px;
    padding: 0 20px;
    border: none;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-family: var(--font-ui);
    font-weight: 600;
    font-size: 14px;
    box-shadow: var(--shadow);
    cursor: pointer;
  }
}
