/* ============================================================================
 * Cortex — the living 3D brain (WebGL neuron simulation)
 * Apple Liquid-Glass styling, continuous with brain.html tokens.
 * Light + dark themes, prefers-reduced-motion aware.
 * ==========================================================================*/

.cortex-section {
  max-width: var(--max);
  margin: clamp(40px, 6vh, 72px) auto 0;
  padding: 0 var(--gutter);
}

.cortex-frame {
  position: relative;
  border: 1px solid var(--hairline);
  border-radius: 26px;
  overflow: hidden;
  background:
    radial-gradient(80% 90% at 50% 0%, color-mix(in oklab, var(--accent) 16%, transparent), transparent 70%),
    linear-gradient(180deg, #07070b, #0a0a0c 60%, #06060a);
  box-shadow: 0 40px 120px -70px var(--accent-glow), inset 0 1px 0 rgba(255, 255, 255, .04);
  isolation: isolate;
}

/* In light theme the WebGL stage stays on a deep canvas so neon reads correctly */
:root.light .cortex-frame,
html.light .cortex-frame {
  background:
    radial-gradient(80% 90% at 50% 0%, color-mix(in oklab, var(--accent) 22%, transparent), transparent 70%),
    linear-gradient(180deg, #0c0c12, #0a0a10 60%, #08080e);
  border-color: rgba(0, 0, 0, .12);
}

/* ---- the WebGL stage ---- */
.cortex-stage {
  position: relative;
  width: 100%;
  height: clamp(440px, 64vh, 680px);
  cursor: grab;
  touch-action: none;
}
.cortex-stage:active { cursor: grabbing; }

.cortex-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ---- title card (glass, top-left, non-interactive over canvas) ---- */
.cortex-titlecard {
  position: absolute;
  top: 20px;
  left: 20px;
  right: auto;
  max-width: 380px;
  padding: 18px 20px;
  border: 1px solid var(--hairline-strong);
  border-radius: 18px;
  background: color-mix(in oklab, #0b0b10 62%, transparent);
  -webkit-backdrop-filter: blur(22px) saturate(160%);
  backdrop-filter: blur(22px) saturate(160%);
  box-shadow: 0 24px 60px -40px #000, inset 0 1px 0 rgba(255, 255, 255, .08);
  pointer-events: none;
  z-index: 4;
}
.cortex-kicker {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--accent-2);
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.cortex-kicker i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 10px var(--accent-glow-2);
  animation: cortexPulse 2.4s var(--ease-in-out) infinite;
}
.cortex-title {
  margin: 0 0 8px;
  font-size: clamp(20px, 2.6vw, 28px);
  line-height: 1.08;
  letter-spacing: -.03em;
  color: #f5f5f7;
}
.cortex-title .accent {
  background: linear-gradient(120deg, #7c5cff, #38bdf8);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.cortex-sub {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  color: rgba(245, 245, 247, .66);
}

/* ---- live stat row (bottom-left, glass) ---- */
.cortex-stats {
  position: absolute;
  left: 20px;
  bottom: 20px;
  display: flex;
  gap: 10px;
  z-index: 4;
  pointer-events: none;
  flex-wrap: wrap;
  max-width: calc(100% - 40px);
}
.cortex-stat {
  padding: 9px 13px;
  border: 1px solid var(--hairline-strong);
  border-radius: 13px;
  background: color-mix(in oklab, #0b0b10 56%, transparent);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  backdrop-filter: blur(18px) saturate(160%);
  min-width: 84px;
}
.cortex-stat b {
  display: block;
  font-size: 19px;
  font-weight: 720;
  letter-spacing: -.03em;
  color: #f5f5f7;
  font-variant-numeric: tabular-nums;
}
.cortex-stat span {
  display: block;
  margin-top: 2px;
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: rgba(245, 245, 247, .5);
}

/* ---- region chip rail (bottom, horizontal scroll) ---- */
.cortex-rail {
  position: absolute;
  right: 20px;
  bottom: 20px;
  left: auto;
  display: flex;
  gap: 7px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 46%;
  z-index: 4;
}
.cortex-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border: 1px solid var(--hairline-strong);
  border-radius: 999px;
  background: color-mix(in oklab, #0b0b10 54%, transparent);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  font-size: 11.5px;
  color: rgba(245, 245, 247, .78);
  cursor: pointer;
  transition: transform .2s var(--ease-out), border-color .2s var(--ease-out), background .2s var(--ease-out);
  white-space: nowrap;
}
.cortex-chip:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
  background: color-mix(in oklab, var(--accent) 18%, #0b0b10);
  color: #fff;
}
.cortex-chip i {
  width: 7px; height: 7px; border-radius: 50%;
  background: #38bdf8;
  box-shadow: 0 0 8px rgba(56, 189, 248, .7);
  flex-shrink: 0;
}
.cortex-chip.missing i {
  background: #ff5a6a;
  box-shadow: 0 0 8px rgba(255, 90, 106, .7);
}

/* ---- region HUD (slides in from the right) ---- */
.cortex-hud {
  position: absolute;
  top: 16px;
  right: 16px;
  bottom: 16px;
  width: min(330px, 80%);
  padding: 22px;
  border: 1px solid var(--hairline-strong);
  border-radius: 20px;
  background: color-mix(in oklab, #0a0a10 70%, transparent);
  -webkit-backdrop-filter: blur(28px) saturate(170%);
  backdrop-filter: blur(28px) saturate(170%);
  box-shadow: 0 30px 80px -40px #000, inset 0 1px 0 rgba(255, 255, 255, .08);
  transform: translateX(18px);
  opacity: 0;
  transition: transform .3s var(--ease-out), opacity .3s var(--ease-out);
  z-index: 6;
  overflow-y: auto;
}
.cortex-hud.open { transform: translateX(0); opacity: 1; }
.cortex-hud[hidden] { display: none; }

.cortex-hud-close {
  position: absolute;
  top: 14px; right: 14px;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border: 1px solid var(--hairline-strong);
  border-radius: 999px;
  background: var(--surface-2);
  color: #f5f5f7;
  cursor: pointer;
  transition: border-color .2s var(--ease-out), background .2s var(--ease-out);
}
.cortex-hud-close:hover { border-color: var(--accent); background: var(--accent-soft); }

.cortex-hud-eyebrow {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--accent-2);
  margin: 0 0 6px;
}
.cortex-hud-region {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 680;
  letter-spacing: -.03em;
  color: #f5f5f7;
}
.cortex-hud-agent {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: rgba(245, 245, 247, .45);
  margin: 0 0 14px;
}
.cortex-hud-pill {
  display: inline-block;
  padding: 4px 11px;
  border-radius: 999px;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .08em;
  text-transform: uppercase;
  border: 1px solid var(--hairline-strong);
  margin-bottom: 16px;
}
.cortex-hud-pill.live {
  color: #38bdf8;
  border-color: color-mix(in oklab, #38bdf8 50%, transparent);
  background: color-mix(in oklab, #38bdf8 12%, transparent);
}
.cortex-hud-pill.missing {
  color: #ff5a6a;
  border-color: color-mix(in oklab, #ff5a6a 50%, transparent);
  background: color-mix(in oklab, #ff5a6a 12%, transparent);
}
.cortex-hud-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-bottom: 14px;
}
.cortex-hud-cell {
  padding: 11px;
  border: 1px solid var(--hairline);
  border-radius: 12px;
  background: rgba(255, 255, 255, .03);
}
.cortex-hud-cell span {
  display: block;
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: rgba(245, 245, 247, .42);
  margin-bottom: 4px;
}
.cortex-hud-cell strong {
  display: block;
  font-size: 13.5px;
  line-height: 1.25;
  color: #f5f5f7;
  overflow-wrap: anywhere;
}
.cortex-hud-signal {
  margin: 0 0 6px;
  font-size: 13.5px;
  line-height: 1.5;
  color: rgba(245, 245, 247, .72);
}
.cortex-hud-source {
  font-family: var(--mono);
  font-size: 11px;
  color: rgba(245, 245, 247, .42);
}

/* ---- loader + fallback ---- */
.cortex-loading[hidden] { display: none !important; }
.cortex-fallback[hidden] { display: none !important; }
.cortex-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 3;
  color: rgba(245, 245, 247, .6);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: .1em;
  text-transform: uppercase;
  gap: 14px;
}
.cortex-spinner {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 2px solid rgba(124, 92, 255, .25);
  border-top-color: #38bdf8;
  animation: cortexSpin 1s linear infinite;
}
.cortex-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 30px;
  z-index: 3;
}
.cortex-fallback-inner {
  max-width: 460px;
  color: rgba(245, 245, 247, .7);
}
.cortex-fallback svg { width: 100%; height: auto; opacity: .9; }
.cortex-fallback p {
  margin-top: 16px;
  font-size: 14px;
  line-height: 1.5;
}

/* ---- hint chip ---- */
.cortex-hint {
  position: absolute;
  top: 20px;
  right: 16px;
  z-index: 4;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: rgba(245, 245, 247, .42);
  padding: 6px 11px;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: color-mix(in oklab, #0b0b10 50%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  pointer-events: none;
}
.cortex-hud.open ~ .cortex-hint { opacity: 0; }

@keyframes cortexPulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .4; transform: scale(.8); } }
@keyframes cortexSpin { to { transform: rotate(360deg); } }

@media (max-width: 720px) {
  .cortex-titlecard { max-width: calc(100% - 40px); top: 14px; left: 14px; padding: 14px 16px; }
  .cortex-rail { display: none; }
  .cortex-stats { left: 14px; bottom: 14px; gap: 8px; }
  .cortex-stat { min-width: 70px; padding: 7px 10px; }
  .cortex-hud { width: calc(100% - 28px); top: auto; bottom: 14px; left: 14px; right: 14px; max-height: 56%; }
  .cortex-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .cortex-kicker i { animation: none; }
  .cortex-spinner { animation: none; border-top-color: rgba(124, 92, 255, .5); }
  .cortex-hud { transition: opacity .2s linear; }
}
