:root {
  --void: #04060c;
  --panel: rgba(9, 13, 24, 0.86);
  --panel-solid: #0a0f1d;
  --border: rgba(148, 163, 184, 0.16);
  --border-bright: rgba(56, 189, 248, 0.45);
  --text: #e2e8f0;
  --muted: #8492a6;
  --cyan: #38bdf8;
  --amber: #f59e0b;
  --emerald: #10b981;
  --crimson: #f43f5e;
  --violet: #a855f7;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: rgba(56, 189, 248, 0.28) rgba(4, 6, 12, 0.4); }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-track { background: rgba(4, 6, 12, 0.4); }
*::-webkit-scrollbar-thumb { background: rgba(56, 189, 248, 0.3); border-radius: 999px; }
*::-webkit-scrollbar-thumb:hover { background: rgba(56, 189, 248, 0.55); }
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--void);
  color: var(--text);
  color-scheme: dark;
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

#manifold {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  background: radial-gradient(circle at 50% 45%, #0b1328 0%, #04060c 68%, #020307 100%);
}
#manifold:active { cursor: grabbing; }

.vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at center, transparent 58%, rgba(2, 4, 10, 0.72) 100%);
  z-index: 2;
}

/* Top HUD */
.hud-top {
  position: fixed;
  top: 14px;
  left: 16px;
  right: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}
.hud-top > * { pointer-events: auto; }

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(12px);
  cursor: pointer;
  transition: border-color 0.2s;
}
.brand:hover { border-color: var(--border-bright); }
.brand-glyph {
  font-family: var(--mono);
  font-size: 22px;
  color: var(--cyan);
  text-shadow: 0 0 14px rgba(56, 189, 248, 0.65);
}
.brand-title {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.22em;
  color: #f8fafc;
}
.brand-sub {
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--muted);
  margin-top: 2px;
}

.hud-stats {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.stat-pill {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 7px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  backdrop-filter: blur(12px);
  font-family: var(--mono);
}
.stat-label { font-size: 9px; letter-spacing: 0.12em; color: var(--muted); }
.stat-val { font-size: 13px; font-weight: 700; color: #f8fafc; }
.stat-alive .stat-val { color: var(--emerald); text-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }

.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-primary {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid rgba(56, 189, 248, 0.7);
  background: linear-gradient(135deg, rgba(14, 165, 233, 0.25), rgba(168, 85, 247, 0.25));
  color: #f0f9ff;
  cursor: pointer;
  box-shadow: 0 0 22px rgba(56, 189, 248, 0.22);
  transition: all 0.2s;
}
.btn-primary:hover {
  background: linear-gradient(135deg, rgba(14, 165, 233, 0.42), rgba(168, 85, 247, 0.4));
  box-shadow: 0 0 30px rgba(56, 189, 248, 0.4);
  transform: translateY(-1px);
}

.btn-ghost, .btn-ghost-sm {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  padding: 9px 13px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  backdrop-filter: blur(12px);
  transition: border-color 0.2s, color 0.2s;
}
.btn-ghost-sm { padding: 6px 10px; font-size: 10px; }
.btn-ghost:hover, .btn-ghost-sm:hover { border-color: var(--border-bright); color: #fff; }

.btn-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.75);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}
.btn-icon:hover { border-color: var(--border-bright); }

/* Bottom HUDs */
.hud-bottom-left {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 290px;
}
.hud-bottom-right {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 10;
  width: 330px;
  max-width: calc(100vw - 32px);
}

.panel-mini {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(12px);
  padding: 10px 12px;
}
.mini-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  color: var(--muted);
  margin-bottom: 8px;
}
.mini-hint { font-size: 9px; color: var(--cyan); }
.pulse-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--emerald);
  box-shadow: 0 0 8px var(--emerald);
  display: inline-block;
  animation: beat 1.8s infinite ease-in-out;
}
@keyframes beat {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}

.entity-roster { display: flex; flex-direction: column; gap: 6px; }
.entity-chip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 7px;
  border: 1px solid rgba(56, 189, 248, 0.22);
  background: rgba(14, 24, 44, 0.72);
  cursor: pointer;
  font-family: var(--mono);
  font-size: 11px;
  transition: border-color 0.2s, background 0.2s;
}
.entity-chip:hover { border-color: var(--cyan); background: rgba(14, 34, 62, 0.9); }
.entity-chip.fading { border-color: rgba(244, 63, 94, 0.6); }
.entity-chip.dormant { border-color: rgba(148, 163, 184, 0.25); opacity: 0.7; }
.entity-state-tag {
  font-size: 9px;
  padding: 2px 6px;
  border-radius: 4px;
  letter-spacing: 0.08em;
}
.tag-alive { background: rgba(16, 185, 129, 0.18); color: #34d399; }
.tag-fading { background: rgba(244, 63, 94, 0.2); color: #fb7185; }
.tag-dormant { background: rgba(148, 163, 184, 0.16); color: #94a3b8; }

.map-controls { display: flex; gap: 6px; }
.ctrl-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-family: var(--mono);
  font-size: 15px;
  cursor: pointer;
  backdrop-filter: blur(10px);
}
.ctrl-btn:hover { border-color: var(--border-bright); color: #fff; }

.feed-panel { max-height: 175px; display: flex; flex-direction: column; }
.pulse-feed {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 126px;
  padding-right: 4px;
}
.pulse-item {
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.4;
  padding: 5px 7px;
  border-radius: 6px;
  background: rgba(15, 23, 42, 0.55);
  border-left: 2px solid var(--cyan);
  cursor: pointer;
  transition: background 0.15s;
}
.pulse-item:hover { background: rgba(30, 41, 59, 0.78); }
.pulse-item.kind-entity_born { border-left-color: var(--emerald); }
.pulse-item.kind-entity_fading, .pulse-item.kind-shard_mutated { border-left-color: var(--crimson); }
.pulse-item.kind-projection_rendered { border-left-color: var(--amber); }
.pulse-time { color: var(--muted); margin-right: 6px; }

.faction-legend {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9;
  display: flex;
  gap: 14px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--muted);
}
.leg { display: flex; align-items: center; gap: 5px; }
.leg i { width: 8px; height: 8px; border-radius: 2px; background: var(--c); box-shadow: 0 0 8px var(--c); display: inline-block; }

/* Right Inspector Drawer */
.inspector {
  position: fixed;
  top: 72px;
  right: 16px;
  bottom: 204px;
  width: 390px;
  max-width: calc(100vw - 32px);
  z-index: 20;
  background: rgba(7, 11, 21, 0.94);
  border: 1px solid var(--border);
  border-radius: 12px;
  backdrop-filter: blur(18px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.65);
}
.inspector.hidden { display: none; }
.inspector-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.inspector-kicker {
  font-family: var(--mono);
  font-size: 9px;
  letter-spacing: 0.16em;
  color: var(--cyan);
}
.inspector-title {
  margin: 4px 0 0;
  font-family: var(--mono);
  font-size: 18px;
  letter-spacing: 0.08em;
}
.inspector-body {
  padding: 14px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13px;
}
.inspector-body > * {
  flex-shrink: 0;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.badge {
  font-family: var(--mono);
  font-size: 10px;
  padding: 3px 8px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.7);
  color: var(--text);
}
.badge-organ {
  border-color: rgba(16, 185, 129, 0.5);
  background: rgba(16, 185, 129, 0.12);
  color: #6ee7b7;
  cursor: pointer;
}
.badge-drift {
  border-color: rgba(244, 63, 94, 0.6);
  background: rgba(244, 63, 94, 0.14);
  color: #fda4af;
}

.cipher-box {
  position: relative;
  border: 1px solid rgba(56, 189, 248, 0.28);
  border-radius: 8px;
  background: #03050a;
  padding: 12px;
  font-family: var(--mono);
  overflow: hidden;
}
.cipher-header {
  display: flex;
  justify-content: space-between;
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--cyan);
  margin-bottom: 8px;
}
.cipher-lines {
  font-size: 11px;
  line-height: 1.55;
  color: rgba(186, 230, 253, 0.72);
  letter-spacing: 0.08em;
  user-select: all;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.3);
}
.cipher-note {
  margin-top: 8px;
  font-family: var(--sans);
  font-size: 11px;
  color: var(--muted);
  line-height: 1.45;
}

.lens-list { display: flex; flex-direction: column; gap: 8px; }
.lens-card {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.65);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
  transition: all 0.2s;
}
.lens-card:hover { border-color: var(--cyan); background: rgba(15, 32, 58, 0.85); }
.lens-name { font-weight: 600; font-size: 13px; color: #f8fafc; }
.lens-sub { font-family: var(--mono); font-size: 10px; color: var(--muted); margin-top: 3px; }
.btn-enter {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 7px 11px;
  border-radius: 6px;
  border: 1px solid rgba(56, 189, 248, 0.6);
  background: rgba(14, 165, 233, 0.2);
  color: #e0f2fe;
  cursor: pointer;
  white-space: nowrap;
}

.prompt-box {
  display: flex;
  align-items: stretch;
  gap: 8px;
  background: #020409;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
}
.prompt-box code {
  flex: 1;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.5;
  color: #bae6fd;
  word-break: break-word;
}
.prompt-box-lg code { font-size: 12px; }
.btn-copy {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 0 12px;
  border-radius: 6px;
  border: 1px solid rgba(56, 189, 248, 0.5);
  background: rgba(56, 189, 248, 0.15);
  color: #e0f2fe;
  cursor: pointer;
}
.btn-copy:hover { background: rgba(56, 189, 248, 0.3); }

.organ-list { display: flex; flex-direction: column; gap: 6px; }
.organ-row {
  padding: 8px 10px;
  border-radius: 7px;
  border: 1px solid rgba(16, 185, 129, 0.24);
  background: rgba(6, 26, 26, 0.55);
  cursor: pointer;
}
.organ-row:hover { border-color: var(--emerald); }
.organ-title {
  font-family: var(--mono);
  font-size: 11px;
  color: #6ee7b7;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}
.organ-title span:last-child {
  font-size: 10px;
  color: #94a3b8;
  text-align: right;
}
.organ-desc { font-size: 11px; color: var(--muted); margin-top: 3px; line-height: 1.4; }

.ecg-canvas {
  width: 100%;
  height: 54px;
  border-radius: 6px;
  background: #02050b;
  border: 1px solid rgba(16, 185, 129, 0.25);
  display: block;
}

/* Fullscreen Portal Viewer */
.portal-modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: #020408;
  display: flex;
  flex-direction: column;
}
.portal-modal.hidden { display: none; }
.portal-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: rgba(7, 11, 21, 0.96);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.portal-id { display: flex; align-items: center; gap: 8px; font-family: var(--mono); }
.portal-badge { font-size: 12px; font-weight: 700; color: var(--cyan); letter-spacing: 0.08em; }
.portal-coord { font-size: 11px; color: var(--muted); }

.portal-lenses { display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; overflow-x: auto; flex: 1; justify-content: center; max-width: 100%; }
.lens-tab {
  font-family: var(--mono);
  font-size: 10px;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.7);
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all 0.15s;
}
.lens-tab:hover { color: #fff; border-color: var(--border-bright); }
.lens-tab.active {
  color: #fff;
  border-color: var(--cyan);
  background: rgba(14, 165, 233, 0.24);
  box-shadow: 0 0 14px rgba(56, 189, 248, 0.25);
}
.lens-tab.drifted::after { content: " ⚡"; color: var(--crimson); }

.portal-tools { display: flex; align-items: center; gap: 6px; }

.drift-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 7px 14px;
  background: linear-gradient(90deg, rgba(244, 63, 94, 0.22), rgba(168, 85, 247, 0.22));
  border-bottom: 1px solid rgba(244, 63, 94, 0.5);
  font-family: var(--mono);
  font-size: 11px;
  color: #fecdd3;
}
.drift-banner.hidden { display: none; }
.btn-drift-copy {
  font-family: var(--mono);
  font-size: 10px;
  padding: 4px 10px;
  border-radius: 5px;
  border: 1px solid rgba(254, 205, 211, 0.6);
  background: rgba(244, 63, 94, 0.25);
  color: #fff;
  cursor: pointer;
}

.portal-stage {
  position: relative;
  flex: 1;
  width: 100%;
  overflow: hidden;
  background: #020408;
}
.portal-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: #020408;
}
.portal-frame.hidden { display: none; }

.portal-raw-view {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.portal-raw-view.hidden { display: none; }
#portal-raw-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.portal-raw-overlay {
  position: relative;
  z-index: 2;
  max-width: 560px;
  margin: 20px;
  padding: 26px;
  border-radius: 14px;
  background: rgba(4, 7, 15, 0.88);
  border: 1px solid rgba(56, 189, 248, 0.35);
  backdrop-filter: blur(14px);
  text-align: center;
}
.raw-pill {
  display: inline-block;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.16em;
  color: var(--cyan);
  margin-bottom: 10px;
}
.portal-raw-overlay h3 {
  font-family: var(--mono);
  font-size: 28px;
  letter-spacing: 0.16em;
  margin: 0 0 12px;
  color: #f8fafc;
}
.portal-raw-overlay p { color: var(--muted); font-size: 13px; line-height: 1.6; margin: 0 0 16px; }

.portal-glitch-fx {
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 60px rgba(244, 63, 94, 0.25);
  background: repeating-linear-gradient(
    0deg,
    rgba(244, 63, 94, 0.04) 0px,
    rgba(244, 63, 94, 0.04) 1px,
    transparent 1px,
    transparent 4px
  );
  animation: glitchFlicker 4s infinite;
}
.portal-glitch-fx.hidden { display: none; }
@keyframes glitchFlicker {
  0%, 94%, 100% { opacity: 0.55; }
  95% { opacity: 0.95; }
  97% { opacity: 0.25; }
}

.compass-dock {
  position: absolute;
  right: 18px;
  bottom: 72px;
  z-index: 6;
  display: grid;
  grid-template-columns: 32px 44px 32px;
  grid-template-rows: 32px 32px 32px;
  gap: 3px;
  padding: 6px;
  border-radius: 12px;
  background: rgba(6, 10, 20, 0.85);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
}
.compass-dock.hidden { display: none; }
.compass-n { grid-column: 2; grid-row: 1; }
.compass-w { grid-column: 1; grid-row: 2; }
.compass-center {
  grid-column: 2; grid-row: 2;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--mono); font-size: 9px; color: var(--muted);
}
.compass-e { grid-column: 3; grid-row: 2; }
.compass-s { grid-column: 2; grid-row: 3; }
.compass-btn {
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.8);
  color: var(--text);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.compass-btn:hover { border-color: var(--cyan); color: var(--cyan); }

/* Agent Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(2, 4, 10, 0.78);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.hidden { display: none; }
.modal-card {
  width: 640px;
  max-width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--panel-solid);
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 14px;
  padding: 20px 22px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.75);
}
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; }
.modal-head h3 { margin: 4px 0 0; font-size: 19px; }
.modal-lead { font-size: 13px; line-height: 1.6; color: var(--muted); margin: 0 0 14px; }
.modal-lead code { color: var(--cyan); font-family: var(--mono); }
.modal-meta { margin-top: 10px; font-size: 12px; color: var(--muted); }

.mission-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tab-btn {
  font-family: var(--mono);
  font-size: 10px;
  padding: 7px 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.7);
  color: var(--muted);
  cursor: pointer;
}
.tab-btn.active { border-color: var(--cyan); color: #fff; background: rgba(14, 165, 233, 0.22); }

.relay-note { font-size: 12px; color: var(--muted); margin: 0 0 10px; line-height: 1.5; }
.relay-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.relay-grid label, .relay-textarea-label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
}
.relay-grid input, .relay-grid select, textarea {
  background: #03060e;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-family: var(--mono);
  font-size: 12px;
}
.relay-actions-row { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.relay-status { font-family: var(--mono); font-size: 11px; color: var(--cyan); }

.toast {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  padding: 9px 16px;
  border-radius: 8px;
  background: rgba(14, 165, 233, 0.92);
  color: #020617;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
}
.toast.hidden { display: none; }

.hidden { display: none !important; }

@media (max-width: 860px) {
  .hud-stats { display: none; }
  .faction-legend { display: none; }
  .hud-bottom-right { display: none; }
  .inspector { top: 68px; bottom: 16px; width: calc(100vw - 32px); }
  .relay-grid { grid-template-columns: 1fr; }
}
