/* ============================================================
   Knowledge map — page chrome around the Cytoscape canvas.

   The map carries its own palette rather than the site's greys:
   it is a field of small saturated marks on a large empty ground,
   which is a different design problem from a page of text. These
   variables mirror design/tokens.ts so the CSS chrome and the
   canvas never disagree about a colour.
   ============================================================ */

/* nebula — the dark side */
:root[data-theme="dark"] .map-stage,
:root[data-theme="dark"] .legend {
  --kg-canvas: #0A0A0A;
  --kg-surface: #18181B;
  --kg-ink: #EDE9FE;
  --kg-quiet: #A5A0BF;
  --kg-line: #2A2140;
  --kg-trade: #FF2D9B;
  --kg-source: #5B5478;
  --kg-artifact: #FFB224;
  --kg-dsa: #A78BFA;
  --kg-systems: #60A5FA;
  --kg-backend: #F472B6;
  --kg-os: #C084FC;
  --kg-meta: #6B6488;
}

/* paper — the light side */
:root:not([data-theme="dark"]) .map-stage,
:root:not([data-theme="dark"]) .legend {
  --kg-canvas: #FFFFFF;
  --kg-surface: #FFFFFF;
  --kg-ink: #1A1A22;
  --kg-quiet: #6B6880;
  --kg-line: #E2E0EA;
  --kg-trade: #E0218A;
  --kg-source: #B9B6C9;
  --kg-artifact: #B85C00;
  --kg-dsa: #4A7DE8;
  --kg-systems: #8B5CF6;
  --kg-backend: #E0218A;
  --kg-os: #3DD9A0;
  --kg-meta: #22C3E6;
}

.map-main {
  max-width: 1180px;
  margin-inline: auto;
  padding: 8px 24px 56px;
}

.map-head { margin-bottom: 20px; }

/* ---- the stage ---- */

.map-stage {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--gray-400);
  border-radius: 16px;
  background: var(--kg-canvas);
  /* tall enough to be a map rather than a strip, capped so the page still
     scrolls to the note underneath */
  height: min(72vh, 660px);
}

.graph-well {
  width: 100%;
  height: 100%;
}

/* ---- flat / 3D switch ---- */

.view-switch {
  position: absolute;
  z-index: 3;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 2px;
  border: 1px solid var(--kg-line);
  border-radius: 9px;
  padding: 2px;
  background: color-mix(in srgb, var(--kg-surface) 92%, transparent);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.view-btn {
  border-radius: 7px;
  padding: 4px 11px;
  color: var(--kg-quiet);
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  transition: color 0.15s ease-out, background-color 0.15s ease-out;
}

.view-btn:hover { color: var(--kg-ink); }

.view-btn.is-on {
  background: color-mix(in srgb, var(--kg-ink) 12%, transparent);
  color: var(--kg-ink);
}

/* the panel sits under the switch, so it doesn't cover it */
.panel { top: 50px; }

/* ---- 3D well ---- */

.g3d { position: relative; }
.g3d[hidden] { display: none; }
.g3d canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.g3d-labels {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

/* Real DOM rather than canvas text, so type stays crisp at any zoom. */
.g3d-label {
  position: absolute;
  top: 0;
  left: 0;
  color: var(--kg-ink);
  font-size: 11px;
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  /* the halo that keeps a name legible where it crosses a link */
  text-shadow:
     0 0 3px var(--kg-canvas), 0 0 3px var(--kg-canvas),
     0 0 6px var(--kg-canvas);
  transition: opacity 0.18s ease-out;
  will-change: transform, opacity;
}

.g3d-label.is-source { color: var(--kg-quiet); }

@media (prefers-reduced-motion: reduce) {
  .g3d-label { transition: none; }
}

/* ---- controls ---- */

.map-controls {
  position: absolute;
  z-index: 3;
  top: 12px;
  left: 12px;
  max-width: 260px;
}

.ctrl-card {
  border: 1px solid var(--kg-line);
  border-radius: 12px;
  background: color-mix(in srgb, var(--kg-surface) 92%, transparent);
  color: var(--kg-ink);
  font-size: 12px;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.ctrl-card > summary {
  padding: 9px 12px;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
}

.ctrl-card > summary::-webkit-details-marker { display: none; }

.ctrl-card > summary::after {
  content: "▾";
  margin-left: 6px;
  color: var(--kg-quiet);
}

.ctrl-card[open] > summary::after { content: "▴"; }

.ctrl-status {
  display: block;
  margin-top: 2px;
  color: var(--kg-quiet);
  font-size: 11px;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
}

.ctrl-group {
  margin: 0;
  border: 0;
  border-top: 1px solid var(--kg-line);
  padding: 10px 12px;
}

.ctrl-group legend {
  padding: 0;
  color: var(--kg-quiet);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.ctrl-group label {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-top: 7px;
  cursor: pointer;
  line-height: 1.3;
}

.ctrl-group input { margin: 0; accent-color: var(--kg-ink); }

/* the filter chip's own colour, so the key isn't repeated in the legend */
.sw {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  border-radius: 999px;
  background: var(--sw);
}

.ctrl-btn {
  margin-top: 9px;
  border: 1px solid var(--kg-line);
  border-radius: 8px;
  padding: 5px 10px;
  color: var(--kg-ink);
  cursor: pointer;
  font-size: 11px;
}

.ctrl-btn:hover { border-color: var(--kg-quiet); }

/* ---- detail panel ---- */

.panel {
  position: absolute;
  z-index: 4;
  top: 12px;
  right: 12px;
  bottom: 12px;
  width: min(340px, calc(100% - 24px));
  overflow-y: auto;
  border: 1px solid var(--kg-line);
  border-radius: 12px;
  padding: 16px;
  background: color-mix(in srgb, var(--kg-surface) 96%, transparent);
  color: var(--kg-ink);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  animation: panel-in 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

.panel[hidden] { display: none; }

@keyframes panel-in { from { opacity: 0; transform: translateX(16px); } }

.panel-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-kind {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--kg-quiet);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.panel-close {
  border: 1px solid var(--kg-line);
  border-radius: 7px;
  padding: 3px 8px;
  color: var(--kg-quiet);
  cursor: pointer;
  font-size: 11px;
}

.panel-close:hover { color: var(--kg-ink); border-color: var(--kg-quiet); }

.panel-title {
  margin: 10px 0 0;
  font-size: 19px;
  font-weight: 600;
  line-height: 1.25;
}

.panel-title:focus-visible { outline-offset: 4px; }

.depth-badge {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 12px;
  color: var(--kg-quiet);
  font-size: 12px;
  line-height: 1.5;
}

.depth-badge strong { color: var(--kg-ink); font-weight: 600; }
.depth-badge .conn-dot { margin-top: 4px; }

.panel-note {
  margin: 14px 0 0;
  color: var(--kg-ink);
  font-size: 13.5px;
  line-height: 1.62;
}

.panel-link { margin: 12px 0 0; font-size: 13px; }

.panel-link a {
  color: var(--kg-ink);
  text-decoration: underline;
  text-decoration-color: var(--kg-quiet);
  text-underline-offset: 3px;
}

.panel-connections { margin-top: 20px; }

.panel-connections h3 {
  margin: 0 0 10px;
  border-top: 1px solid var(--kg-line);
  padding-top: 14px;
  color: var(--kg-quiet);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.conn-group { margin-bottom: 14px; }

.conn-group h4 {
  margin: 0 0 5px;
  color: var(--kg-quiet);
  font-size: 11px;
  font-weight: 600;
}

.conn-group.is-trade h4 { color: var(--kg-trade); }

.conn-group ul { margin: 0; padding: 0; list-style: none; }
.conn-group li { margin-bottom: 6px; }

.conn {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 8px;
  border-radius: 7px;
  padding: 4px 6px;
  margin-left: -6px;
  color: var(--kg-ink);
  cursor: pointer;
  font-size: 13px;
  text-align: left;
  transition: background-color 0.15s ease-out;
}

.conn:hover { background: color-mix(in srgb, var(--kg-ink) 9%, transparent); }

.conn-dot {
  width: 9px;
  height: 9px;
  flex-shrink: 0;
  border: 1.5px solid currentColor;
  border-radius: 999px;
}

.conn-note {
  margin: 2px 0 0 20px;
  color: var(--kg-quiet);
  font-size: 11.5px;
  line-height: 1.5;
}

/* ---- edge tooltip ---- */

.edge-tip {
  position: absolute;
  z-index: 5;
  display: flex;
  max-width: 300px;
  flex-direction: column;
  gap: 3px;
  border: 1px solid var(--kg-line);
  border-radius: 9px;
  padding: 8px 10px;
  background: var(--kg-surface);
  color: var(--kg-ink);
  font-size: 12px;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 14px));
}

.edge-tip[hidden] { display: none; }
.edge-tip.is-below { transform: translate(-50%, 14px); }

.tip-kind {
  color: var(--kg-quiet);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.tip-pair { font-weight: 600; }
.tip-note { color: var(--kg-quiet); line-height: 1.5; }

/* ---- legend ---- */

.legend {
  position: absolute;
  z-index: 2;
  bottom: 12px;
  left: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  color: var(--kg-quiet);
  font-size: 11px;
  pointer-events: none;
}

.legend .key {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.legend li { display: flex; align-items: center; gap: 5px; }

.legend .dot {
  width: 9px;
  height: 9px;
  border: 1.5px solid var(--sw, var(--kg-quiet));
  border-radius: 999px;
  background: var(--sw, var(--kg-quiet));
}

.legend .dot.hollow { background: transparent; }
.legend .dot.ringed { box-shadow: 0 0 0 1.5px var(--kg-canvas), 0 0 0 3px var(--sw, var(--kg-quiet)); }
.legend .dot.diamond { border-radius: 2px; transform: rotate(45deg); }
.legend .dot.sz-s { width: 6px; height: 6px; }
.legend .dot.sz-l { width: 12px; height: 12px; }

.map-foot {
  max-width: 620px;
  margin: 20px 0 0;
  color: var(--gray-1000);
  font-size: 13px;
  line-height: 1.6;
}

/* ---- narrow screens ---- */

@media (max-width: 720px) {
  .map-main { padding-inline: 16px; }
  .map-stage { height: min(70vh, 520px); }

  /* the panel takes the bottom half rather than a side column */
  .panel {
    top: auto;
    right: 8px;
    bottom: 8px;
    left: 8px;
    width: auto;
    max-height: 58%;
  }

  @keyframes panel-in { from { opacity: 0; transform: translateY(16px); } }

  .legend { display: none; }
  .map-controls { max-width: 200px; }
}

@media (prefers-reduced-motion: reduce) {
  .panel { animation: none; }
}
