:root {
  --bg-deep: #050505;
  --bg-carbon: #111111;
  --bg-graphite: #1a1a1a;
  --text-primary: #f5f5f5;
  --text-secondary: #b8b8b8;
  --accent: #4d7cfe;
  --accent-mint: #6ef7d9;
  --border: rgba(245, 245, 245, 0.08);
  /* Tonos semánticos (texto/estado sobre superficies oscuras). Los usa
     shared/app.css (badges, stat-deltas) y el chrome nuevo (banner de score,
     bordes de hero). Coinciden con los colores ya usados en app.js: ámbar de
     waste (#f2b84b) y rojo de TERMINATED (#e05252). */
  --good: #34d399;
  --warn: #f2b84b;
  --bad: #e05252;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "SF Pro Display", "Segoe UI", sans-serif;
}

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

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

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 20px;
  letter-spacing: 0.01em;
}

.brand-icon {
  width: 30px;
  height: 30px;
}

.brand-word {
  font-weight: 700;
  color: var(--text-primary);
}

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

.btn {
  background: var(--bg-graphite);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease;
}

.btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-ghost {
  background: transparent;
  padding: 2px 8px;
}

/* Botones de ícono del topbar (hamburguesa, actualizar, encuadrar, cuenta) —
   mismo tamaño de toque (36px), un solo lenguaje visual para los cuatro. */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  background: var(--bg-graphite);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
}

.icon-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* La cuenta se distingue del resto (redonda, como un avatar) aunque hoy no
   haga nada — ya queda listo el lugar para cuando exista sesión real. */
.account-btn {
  border-radius: 999px;
  margin-left: 6px;
}

/* Estado/conteo de recursos — vive flotando sobre el lienzo, no en el
   topbar, con el acento mint para que resalte sobre el fondo del grafo. */
.stage-status {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 20;
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(17, 17, 17, 0.7);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(110, 247, 217, 0.35);
  color: var(--accent-mint);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  pointer-events: none;
  white-space: nowrap;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  /* Capa estática de fondo (detrás del starfield animado): una veta de luz
     azulada muy tenue arriba (brand kit: "soft gradients, subtle light
     diffusion"), una retícula punteada fina que evoca un plano técnico
     ("blueprint" literal) y una viñeta suave que hunde los bordes hacia el
     negro — todo por debajo del umbral de "excessive gradients". */
  background:
    radial-gradient(ellipse 70% 55% at 50% 100%, rgba(0, 0, 0, 0.4), transparent 60%),
    radial-gradient(ellipse 60% 42% at 50% 22%, rgba(77, 124, 254, 0.05), transparent 70%),
    radial-gradient(circle at 1px 1px, rgba(245, 245, 245, 0.035) 1px, transparent 1.5px) 0 0 / 28px 28px,
    var(--bg-deep);
}

#starfield {
  position: absolute;
  inset: 0;
}

#cy {
  position: absolute;
  inset: 0;
  background: transparent;
}

/* Capa de íconos dibujada a mano sobre un <canvas> propio, en vez de
   `background-image` de Cytoscape (ver app.js, drawIconLayer()). Cytoscape
   cachea el bitmap de cada nodo (borde + background-image) en un atlas de
   textura compartido para rendir rápido; a zoom alto combinado con
   devicePixelRatio > 1 ese atlas puede leer/generar el tile equivocado — el
   ícono aparece desplazado o directamente desaparece, mientras que borde,
   anillo punteado de waste, label y edges (que no pasan por ese cache)
   siguen perfectamente alineados. Ese es el bug real detrás de "los íconos
   se descuadran": no es una propiedad de estilo nuestra, es una limitación
   del renderer de canvas de Cytoscape. La salida es no depender de ese
   cache para los íconos: se dibujan en un canvas propio, sincronizado al
   loop de render de Cytoscape (evento 'render'), con posición/tamaño leídos
   en vivo de cada nodo — así siempre coincide con el círculo real,
   cualquiera sea el zoom o el DPR de la pantalla. */
#icon-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* Estado de carga: un momento de marca en vez del texto plano "cargando…"
   que había antes — reemplaza a #status para ese primer instante (el texto
   del topbar sigue existiendo para el conteo de recursos post-carga). Se
   desvanece con la misma curva "cinematic" (cubic-bezier tipo ease-out) que
   el resto de las transiciones del Blueprint. */
.loading-veil {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  background: var(--bg-deep);
  transition: opacity 0.7s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.7s;
}

.loading-veil-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loading-mark {
  position: relative;
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-glyph {
  width: 30px;
  height: 30px;
  opacity: 0.9;
  animation: loading-breathe 2.6s ease-in-out infinite;
}

.loading-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(77, 124, 254, 0.14);
  border-top-color: var(--accent);
  animation: loading-spin 1.7s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}

.loading-caption {
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-secondary);
  animation: loading-breathe 2.6s ease-in-out infinite;
}

@keyframes loading-spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes loading-breathe {
  0%,
  100% {
    opacity: 0.45;
  }
  50% {
    opacity: 1;
  }
}

.panel {
  position: absolute;
  top: 16px;
  right: 16px;
  bottom: 16px;
  width: 340px;
  background: var(--bg-carbon);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.25s ease, opacity 0.25s ease;
  /* Encima del pill de estado (.stage-status, z 20) — si no, el contador
     flotante se dibuja sobre el título del panel. */
  z-index: 25;
}

.panel-hidden {
  transform: translateX(16px);
  opacity: 0;
  pointer-events: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.panel-body {
  padding: 16px;
  overflow-y: auto;
  font-size: 13px;
  color: var(--text-secondary);
}

.panel-body dt {
  color: var(--text-secondary);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 12px;
}

.panel-body dt:first-child {
  margin-top: 0;
}

.panel-body dd {
  margin: 4px 0 0;
  color: var(--text-primary);
  word-break: break-all;
}

.panel-body pre {
  background: var(--bg-graphite);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-size: 12px;
  color: var(--text-primary);
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  border: 1px solid var(--border);
  color: var(--accent-mint);
}

.badge-waste {
  color: #f2b84b;
  border-color: rgba(242, 184, 75, 0.35);
  text-transform: uppercase;
  font-size: 10px;
  margin-right: 6px;
}

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

.waste-list li {
  color: var(--text-primary);
  line-height: 1.4;
}

/* --------------------------------------------------- Chat nativo (Concierge) */
/* Panel acoplado a la derecha del lienzo — misma familia visual que .panel
   (glassmorphism sobre el fondo oscuro), pero de altura completa y persistente
   mientras esté abierto. No es un FAB flotante: el chat vive donde vive el mapa. */
.chat-panel {
  position: absolute;
  top: 16px;
  right: 16px;
  bottom: 16px;
  width: 380px;
  max-width: calc(100% - 32px);
  background: rgba(17, 17, 17, 0.82);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Por encima del panel de detalle (z 25) y del pill de estado (z 20). */
  z-index: 26;
  transition: transform 0.32s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.32s cubic-bezier(0.16, 1, 0.3, 1);
}

.chat-hidden {
  transform: translateX(24px);
  opacity: 0;
  pointer-events: none;
}

/* Con el chat abierto, el panel de detalle y el pill de estado se corren a la
   izquierda para no quedar tapados por el chat. */
body.chat-open .panel:not(.panel-hidden) {
  right: 412px;
}
body.chat-open .stage-status {
  right: 412px;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-title {
  font-weight: 600;
  font-size: 14px;
}

.chat-close-btn {
  width: 30px;
  height: 30px;
  font-size: 15px;
  line-height: 1;
}

/* Barra de filtro activo — aparece cuando un comando del chat filtra el mapa. */
.chat-filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(110, 247, 217, 0.06);
  flex-shrink: 0;
}

.chat-filter-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--accent-mint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-filter-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-mint);
  flex-shrink: 0;
}

.chat-filter-clear {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: 7px;
  padding: 4px 9px;
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease;
}

.chat-filter-clear:hover {
  border-color: var(--accent-mint);
  color: var(--accent-mint);
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Estado vacío / hint con sugerencias */
.chat-empty {
  margin: auto 0;
  text-align: center;
  padding: 12px 4px;
}

.chat-empty-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.chat-empty-sub {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 16px;
}

.chat-suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-chip {
  text-align: left;
  background: var(--bg-graphite);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12.5px;
  font-family: inherit;
  line-height: 1.35;
  cursor: pointer;
  transition: border-color 0.2s ease, color 0.2s ease, background-color 0.2s ease;
}

.chat-chip:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

/* Burbujas */
.chat-msg {
  display: flex;
}

.chat-msg-user {
  justify-content: flex-end;
}

.chat-msg-assistant {
  justify-content: flex-start;
}

.chat-bubble {
  max-width: 85%;
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-msg-user .chat-bubble {
  background: rgba(77, 124, 254, 0.16);
  border: 1px solid rgba(77, 124, 254, 0.35);
  color: var(--text-primary);
  border-bottom-right-radius: 4px;
}

.chat-msg-assistant .chat-bubble {
  background: var(--bg-graphite);
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.chat-bubble.chat-error {
  color: var(--bad);
  border-color: rgba(224, 82, 82, 0.4);
}

/* Indicador "escribiendo…" */
.chat-typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.chat-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  animation: chat-bounce 1.2s ease-in-out infinite;
}

.chat-dot:nth-child(2) {
  animation-delay: 0.15s;
}
.chat-dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes chat-bounce {
  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* Composer */
.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  resize: none;
  background: var(--bg-graphite);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  line-height: 1.4;
  padding: 10px 12px;
  max-height: 120px;
  transition: border-color 0.2s ease;
}

.chat-input:focus {
  outline: none;
  border-color: var(--accent);
}

.chat-input::placeholder {
  color: var(--text-secondary);
}

.chat-send {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: opacity 0.2s ease, transform 0.15s ease;
}

.chat-send svg {
  width: 18px;
  height: 18px;
}

.chat-send:hover {
  transform: translateY(-1px);
}

.chat-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

@media (max-width: 720px) {
  .chat-panel {
    width: calc(100% - 32px);
  }
  body.chat-open .panel:not(.panel-hidden),
  body.chat-open .stage-status {
    right: 16px;
  }
}
