* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: #f5f0eb;
  color: #3a3a3a;
  font-family: 'SF Mono', 'Fira Code', monospace;
  overflow: hidden;
  height: 100vh;
}

#container {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
}

#container canvas {
  display: block;
}

/* ── Chat ── */

#chat {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  width: 420px;
  max-width: calc(100% - 32px);
}

#chat-input {
  width: 100%;
  padding: 12px 16px;
  font-family: inherit;
  font-size: 14px;
  color: #2a2a2a;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#chat-input:focus {
  border-color: rgba(0, 0, 0, 0.15);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.06);
}

#chat-input::placeholder {
  color: #b0a89e;
}

#chat-input.loading {
  border-color: rgba(80, 120, 200, 0.4);
  box-shadow: 0 0 12px rgba(80, 120, 200, 0.1);
}

#chat-input.error {
  border-color: rgba(200, 80, 80, 0.4);
}

#chat-response {
  margin-top: 8px;
  padding: 10px 14px;
  font-size: 12px;
  line-height: 1.5;
  color: #3a3a3a;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  max-height: 140px;
  overflow-y: auto;
  white-space: pre-wrap;
  transition: opacity 0.3s;
}

#chat-response.visible { opacity: 1; }
#chat-response.hidden { opacity: 0; pointer-events: none; }

/* ── Nav sidebar ── */

#nav-toggle {
  position: fixed;
  top: 16px; left: 16px;
  z-index: 22;
  width: 32px; height: 32px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 8px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: #6a6a6a;
  transition: background 0.2s, color 0.2s;
}

#nav-toggle:hover { background: rgba(255, 255, 255, 0.95); color: #2a2a2a; }
#nav-toggle.open { background: rgba(255, 255, 255, 0.95); color: #1a1a1a; }

#nav-panel {
  position: fixed;
  top: 56px; left: 16px;
  width: 180px;
  max-height: calc(100vh - 120px);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  padding: 12px;
  overflow-y: auto;
  z-index: 21;
  font-size: 12px;
  line-height: 1.5;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-8px);
  transition: opacity 0.2s, transform 0.2s;
}

#nav-panel.open {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}

.nav-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #aaa;
  margin-top: 10px;
  margin-bottom: 4px;
}

.nav-section-label:first-child { margin-top: 0; }

.nav-stat {
  color: #888;
  font-size: 11px;
  margin-bottom: 2px;
}

.nav-item {
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  color: #4a4a4a;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-item:hover { background: rgba(0, 0, 0, 0.05); color: #1a1a1a; }

.nav-link {
  display: block;
  padding: 5px 8px;
  border-radius: 6px;
  color: #4a4a4a;
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-link:hover { background: rgba(0, 0, 0, 0.05); color: #1a1a1a; }

.nav-warp-icon { color: #6a5a20; }
.nav-crystal-icon { color: #8a7a5a; }

.nav-empty {
  color: #bbb;
  font-size: 11px;
  font-style: italic;
  margin-top: 8px;
  line-height: 1.6;
}

/* ── Detail panel ── */

#detail {
  position: fixed;
  top: 50%;
  right: 24px;
  transform: translateY(-50%);
  width: 280px;
  max-height: 70vh;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  padding: 16px;
  overflow-y: auto;
  z-index: 20;
  transition: opacity 0.2s;
  font-size: 12px;
  line-height: 1.5;
}

#detail.hidden {
  opacity: 0;
  pointer-events: none;
}

#detail .detail-title {
  font-size: 15px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 6px;
}

#detail .detail-cluster {
  font-size: 11px;
  color: #8a8a8a;
  margin-bottom: 10px;
}

#detail .detail-abstract {
  font-size: 11px;
  line-height: 1.5;
  color: #5a5a5a;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.detail-source {
  font-size: 10px; color: #8a8a8a; margin-left: 6px;
  text-decoration: none;
}

.detail-source:hover { color: #4a6a9a; text-decoration: underline; }

#detail .detail-section {
  margin-bottom: 8px;
}

#detail .detail-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #aaa;
  margin-bottom: 4px;
}

#detail .tag-chip {
  display: inline-block;
  padding: 2px 8px;
  margin: 2px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.05);
  color: #555;
  cursor: pointer;
  transition: background 0.15s;
}

#detail .tag-chip:hover {
  background: rgba(80, 120, 200, 0.15);
  color: #2a5aaa;
}

#detail .neighbor-link {
  display: block;
  padding: 3px 0;
  color: #4a6a9a;
  cursor: pointer;
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
  transition: color 0.15s;
}

#detail .neighbor-link:hover {
  color: #2a4a8a;
}

#detail .neighbor-link .strength {
  float: right;
  color: #bbb;
  font-size: 11px;
}

#detail .detail-close {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  color: #aaa;
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}

/* ── Node labels ── */

.node-label {
  position: absolute;
  pointer-events: none;
  font-size: 11px;
  color: #4a4a4a;
  white-space: nowrap;
  transform: translate(-50%, -100%);
  padding-bottom: 4px;
  text-shadow: 0 0 4px #f5f0eb, 0 0 4px #f5f0eb;
}

/* ── Loom / Warps ── */

.warp-label {
  position: absolute;
  pointer-events: none;
  font-size: 13px;
  font-weight: 600;
  color: #6a5a20;
  white-space: nowrap;
  transform: translateX(-50%);
  text-shadow: 0 0 6px #f5f0eb, 0 0 6px #f5f0eb, 0 0 12px rgba(180, 150, 60, 0.3);
  letter-spacing: 0.5px;
}

.warp-input {
  width: 120px;
  padding: 3px 8px;
  font-family: inherit;
  font-size: 11px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  outline: none;
  margin-right: 4px;
}

.warp-input:focus {
  border-color: rgba(120, 100, 40, 0.4);
}

.warp-btn {
  background: rgba(160, 140, 40, 0.1);
  color: #7a6a20;
}

.warp-btn:hover {
  background: rgba(160, 140, 40, 0.2);
}

.warp-name {
  display: inline-block;
  padding: 2px 8px;
  background: rgba(160, 140, 40, 0.12);
  color: #6a5a20;
  border-radius: 4px;
  font-weight: 600;
  font-size: 12px;
  margin-right: 6px;
}

.weave-btn {
  background: rgba(160, 140, 40, 0.1);
  color: #7a6a20;
  margin-top: 4px;
}

.weave-btn:hover {
  background: rgba(160, 140, 40, 0.22);
  color: #5a4a10;
}

/* ── Resonance ── */

.resonance-bridge {
  color: #9a7a2a !important;
  border-bottom-color: rgba(180, 140, 40, 0.15) !important;
}

.resonance-bridge:hover {
  color: #7a5a10 !important;
}

/* ── Crystal controls ── */

.crystal-btn {
  display: inline-block;
  padding: 4px 12px;
  font-family: inherit;
  font-size: 11px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.shatter-btn {
  background: rgba(200, 80, 60, 0.08);
  color: #a05040;
}

.shatter-btn:hover {
  background: rgba(200, 80, 60, 0.18);
  color: #803828;
}

/* ── Responsive ── */

@media (max-width: 600px) {
  #chat { width: calc(100% - 24px); bottom: 16px; }
  #detail { width: 240px; right: 12px; }
}

/* ── Sphere warp ── */

.sphere-defs { position: absolute; width: 0; height: 0; }

#sphere-backdrop {
  position: fixed; inset: 0;
  background: rgba(245, 240, 235, 0.6);
  backdrop-filter: blur(6px);
  z-index: 30;
  transition: opacity 0.35s;
}

#sphere-backdrop.hidden { opacity: 0; pointer-events: none; }

#sphere-warp {
  position: fixed;
  top: 2vh; bottom: 2vh; left: 3vw; right: 3vw;
  z-index: 31;
  display: grid;
  grid-template-columns: 170px 1fr 170px;
  clip-path: url(#sphere-clip);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(12px);
  transition: transform 0.45s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s;
}

#sphere-warp.hidden {
  opacity: 0; pointer-events: none;
  transform: scale(0.01);
}

.sphere-side {
  padding: 28px 14px 28px 20px;
  overflow-y: auto;
  font-size: 12px; line-height: 1.6;
}

#sphere-right { padding: 28px 20px 28px 14px; }

#sphere-center {
  padding: 32px 28px;
  overflow-y: auto;
  border-left: 1px solid rgba(0, 0, 0, 0.06);
  border-right: 1px solid rgba(0, 0, 0, 0.06);
}

.sphere-panel-title {
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 1.2px; color: #aaa;
  margin-bottom: 16px;
}

.sphere-title {
  font-size: 22px; font-weight: 600;
  color: #1a1a1a; margin-bottom: 8px; line-height: 1.3;
}

.sphere-cluster { font-size: 11px; color: #8a8a8a; margin-bottom: 16px; }

.sphere-weight {
  height: 3px; background: rgba(0, 0, 0, 0.06);
  border-radius: 2px; margin-bottom: 20px; overflow: hidden;
}

.sphere-weight-fill {
  height: 100%; background: rgba(120, 100, 40, 0.4); border-radius: 2px;
}

.sphere-dim { margin-bottom: 14px; }

.sphere-dim-label {
  font-size: 10px; text-transform: uppercase;
  letter-spacing: 1px; color: #aaa; margin-bottom: 6px;
}

.sphere-dim-tags { display: flex; flex-wrap: wrap; gap: 4px; }

.sphere-tag {
  display: inline-block; padding: 3px 10px;
  border-radius: 4px; background: rgba(0, 0, 0, 0.04);
  color: #555; cursor: pointer; font-size: 12px;
  transition: background 0.15s, color 0.15s;
}

.sphere-tag:hover { background: rgba(80, 120, 200, 0.12); color: #2a5aaa; }

.sphere-action {
  padding: 8px 12px; margin-bottom: 4px;
  border-radius: 6px; cursor: pointer; color: #4a4a4a;
  transition: background 0.15s, color 0.15s;
}

.sphere-action:hover { background: rgba(0, 0, 0, 0.05); color: #1a1a1a; }
.sphere-danger { color: #a05040; }
.sphere-danger:hover { background: rgba(200, 80, 60, 0.1); color: #803828; }
.sphere-collected { color: #7a6a20; pointer-events: none; }

.sphere-warp-input {
  padding: 8px 12px; display: flex; gap: 6px;
}

.sphere-warp-input.hidden { display: none; }

.sphere-warp-input input {
  flex: 1; padding: 4px 8px;
  font-family: inherit; font-size: 11px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 4px; outline: none;
}

.sphere-warp-input button {
  padding: 4px 10px; font-family: inherit; font-size: 11px;
  border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 4px;
  background: rgba(160, 140, 40, 0.1); color: #7a6a20; cursor: pointer;
}

.sphere-warp-badge {
  padding: 8px 12px; color: #6a5a20;
  font-weight: 600; font-size: 12px;
}

.sphere-neighbor {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 4px;
  cursor: pointer; transition: background 0.15s;
}

.sphere-neighbor:hover { background: rgba(0, 0, 0, 0.05); }

.sphere-neighbor-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}

.sphere-neighbor-name {
  color: #4a6a9a; font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.sphere-empty { color: #bbb; font-size: 11px; font-style: italic; }

.sphere-abstract {
  font-size: 13px; line-height: 1.7; color: #3a3a3a;
  margin-bottom: 18px; padding-bottom: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  white-space: pre-wrap;
}

.sphere-source {
  font-size: 10px; margin-bottom: 14px;
}

.sphere-source a {
  color: #8a8a8a; text-decoration: none;
  transition: color 0.15s;
}

.sphere-source a:hover { color: #4a6a9a; text-decoration: underline; }

.sphere-ai-context { margin-bottom: 14px; min-height: 20px; }

.sphere-ai-loading {
  font-size: 11px; color: #bbb; font-style: italic;
}

.sphere-ai-text {
  font-size: 12px; line-height: 1.6; color: #5a5a5a;
  padding: 8px 10px; border-radius: 6px;
  background: rgba(120, 100, 40, 0.05);
  border-left: 2px solid rgba(120, 100, 40, 0.2);
}

@media (max-width: 900px) {
  #sphere-warp { grid-template-columns: 120px 1fr 120px; left: 1vw; right: 1vw; }
}

@media (max-width: 600px) {
  #sphere-warp { grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; }
  .sphere-side { padding: 16px; }
  #sphere-center { border: none; border-top: 1px solid rgba(0,0,0,0.06); border-bottom: 1px solid rgba(0,0,0,0.06); }
}

/* ── Narrative paths ── */

.path-item {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 8px; margin: 3px 0; font-size: 11px; border-radius: 0 4px 4px 0;
  background: hsl(calc(var(--ph, 0.5) * 360deg), 30%, 94%);
  border-left: 3px solid hsl(calc(var(--ph, 0.5) * 360deg), 55%, 55%);
}

.path-strand {
  display: inline-block; width: 18px; height: 18px;
  text-align: center; line-height: 18px; border-radius: 50%;
  color: white; font-weight: 700; font-size: 10px; text-transform: lowercase;
  background: hsl(calc(var(--ph, 0.5) * 360deg), 55%, 50%);
}

.path-label { flex: 1; color: #3a3a3a; font-weight: 500; }
.path-count { color: #999; font-size: 10px; }

