/* =========================================================
   ROOTER · Inteligencia Territorial — Design tokens
   Concepto: sala de mando nocturna sobre un mapa topográfico
   de fiordos y canales del sur de Chile. El único acento vivo
   es el "pulso" de dato en vivo; todo lo demás es silencioso.
   ========================================================= */

:root {
  /* --- color: terreno nocturno --- */
  --bg-deep:        #0A121C;
  --bg-panel:       #101B29;
  --bg-panel-raised:#14243570;
  --bg-elevated:    #16283A;
  --border-hair:    #223447;
  --border-hair-soft: #1A2C3D;

  --text-primary:   #E7EEF3;
  --text-muted:     #8CA3B3;
  --text-faint:     #5B7285;

  /* --- acento único: vivo (dato automático) --- */
  --live:           #46D5C4;
  --live-dim:       #46D5C433;

  /* --- acento secundario: manual (carga histórica) --- */
  --manual:         #D4A73C;
  --manual-dim:     #D4A73C2E;

  /* --- acento terciario: hipótesis / atención --- */
  --amber:          #E7A362;

  /* --- estado --- */
  --danger:         #E2685E;

  /* --- tipografía --- */
  --font-display: 'Space Grotesk', 'Inter', sans-serif;
  --font-body:    'Inter', sans-serif;
  --font-mono:    'JetBrains Mono', ui-monospace, monospace;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --shadow-panel: 0 8px 30px rgba(0,0,0,0.35);
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg-deep);
  background-image:
    radial-gradient(ellipse 900px 500px at 15% -10%, #14283a55, transparent 60%),
    radial-gradient(ellipse 700px 500px at 100% 20%, #0f1f2c66, transparent 55%);
  color: var(--text-primary);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

button, textarea, input { font-family: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--live); outline-offset: 2px; border-radius: 4px; }

.app { display: flex; flex-direction: column; height: 100vh; min-height: 560px; }

/* ============ TOPBAR ============ */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border-hair);
  background: linear-gradient(180deg, #0D1826, #0A121C);
  flex-shrink: 0;
}
.topbar__brand { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.brand-mark { color: var(--live); display: inline-flex; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.06em;
}
.brand-subtitle {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-faint);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.topbar__territories {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
}
.topbar__territories::-webkit-scrollbar { display: none; }
.territory-chip {
  flex-shrink: 0;
  border: 1px solid var(--border-hair);
  background: var(--bg-panel);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  font-family: var(--font-mono);
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
  white-space: nowrap;
}
.territory-chip:hover { color: var(--text-primary); border-color: var(--border-hair-soft); }
.territory-chip.is-active {
  color: var(--bg-deep);
  background: var(--live);
  border-color: var(--live);
  font-weight: 600;
}

.topbar__status { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.status-pill {
  display: flex; align-items: center; gap: 7px;
  border: 1px solid var(--border-hair);
  border-radius: 999px;
  padding: 6px 12px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.status-dot--demo { background: var(--manual); }
.status-dot--live { background: var(--live); box-shadow: 0 0 0 0 var(--live-dim); animation: pulse 2s infinite; }
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 var(--live-dim); }
  70%  { box-shadow: 0 0 0 6px transparent; }
  100% { box-shadow: 0 0 0 0 transparent; }
}
.icon-btn {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-panel);
  border: 1px solid var(--border-hair);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.icon-btn:hover { color: var(--text-primary); border-color: var(--live); }

/* ============ BODY LAYOUT ============ */
.app__body { flex: 1; display: flex; min-height: 0; }

/* ============ CONVERSATION RAIL ============ */
.conversation-rail {
  width: 380px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-hair);
  background: var(--bg-panel);
  min-height: 0;
}
.conversation-rail__scroll {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.msg--user { align-items: flex-end; }
.msg--assistant { align-items: flex-start; }

.msg__bubble {
  padding: 10px 13px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  line-height: 1.5;
  max-width: 92%;
}
.msg--user .msg__bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border-hair-soft);
  border-bottom-right-radius: 3px;
}
.msg--assistant .msg__bubble {
  background: transparent;
  border: 1px solid var(--border-hair);
  border-bottom-left-radius: 3px;
  color: var(--text-primary);
}
.msg--assistant.is-thinking .msg__bubble {
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 12px;
}

.msg__meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-faint);
  padding: 0 3px;
}

.thinking-dots { display: inline-flex; gap: 3px; }
.thinking-dots span {
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--live);
  animation: blink 1.1s infinite ease-in-out;
}
.thinking-dots span:nth-child(2) { animation-delay: .18s; }
.thinking-dots span:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%,80%,100%{opacity:.2} 40%{opacity:1} }

/* sugerencias */
.prompt-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 16px;
  border-top: 1px solid var(--border-hair-soft);
}
.suggestion-chip {
  border: 1px dashed var(--border-hair);
  background: transparent;
  color: var(--text-muted);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 11px;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}
.suggestion-chip:hover { color: var(--live); border-color: var(--live); border-style: solid; }

/* input */
.prompt-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border-hair);
}
.prompt-input {
  flex: 1;
  resize: none;
  background: var(--bg-deep);
  border: 1px solid var(--border-hair);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 10px 12px;
  font-size: 13.5px;
  max-height: 120px;
  line-height: 1.4;
}
.prompt-input:focus { border-color: var(--live); }
.prompt-input::placeholder { color: var(--text-faint); }
.prompt-send {
  width: 38px; height: 38px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  border: 1px solid var(--live);
  background: var(--live-dim);
  color: var(--live);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background .15s;
}
.prompt-send:hover { background: var(--live); color: var(--bg-deep); }
.prompt-send:disabled { opacity: .4; cursor: not-allowed; }

/* ============ CANVAS ============ */
.canvas {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 28px;
  display: flex;
  flex-direction: column;
}
.canvas__empty {
  margin: auto;
  text-align: center;
  color: var(--text-faint);
  max-width: 360px;
}
.empty-topo { color: var(--border-hair); margin-bottom: 14px; }
.canvas__empty h2 {
  font-family: var(--font-display);
  font-size: 18px;
  color: var(--text-muted);
  margin: 0 0 8px;
  font-weight: 600;
}
.canvas__empty p { font-size: 13px; line-height: 1.6; margin: 0; }

.canvas__content {
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 1080px;
  width: 100%;
  margin: 0 auto;
}

/* ---- tarjeta de visualización (contenedor genérico) ---- */
.viz-card {
  background: var(--bg-panel);
  border: 1px solid var(--border-hair);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-panel);
  overflow: hidden;
  animation: rise .35s ease both;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.viz-card__header {
  padding: 18px 20px 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  border-bottom: 1px solid var(--border-hair-soft);
}
.viz-card__title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 4px;
}
.viz-card__desc { font-size: 12.5px; color: var(--text-muted); margin: 0; max-width: 56ch; }
.viz-card__type-tag {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  border: 1px solid var(--border-hair);
  border-radius: 999px;
  padding: 3px 9px;
  flex-shrink: 0;
  white-space: nowrap;
}
.viz-card__body { padding: 18px 20px; }

/* --- signature element: cinta de procedencia --- */
.provenance-ribbon {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  padding: 10px 20px;
  background: var(--bg-deep);
  border-top: 1px solid var(--border-hair-soft);
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-faint);
}
.provenance-ribbon::before {
  content: '';
  flex-shrink: 0;
  width: 40px; height: 1px;
  background-image: repeating-linear-gradient(90deg, var(--border-hair) 0 4px, transparent 4px 7px);
}
.provenance-item { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.provenance-item strong { color: var(--text-muted); font-weight: 500; }
.load-marker { display: inline-flex; align-items: center; gap: 5px; }
.load-marker--auto .load-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--live); box-shadow: 0 0 0 0 var(--live-dim); animation: pulse 2s infinite; }
.load-marker--manual .load-dot { width: 6px; height: 6px; background: var(--manual); transform: rotate(45deg); }

/* ---- KPI ---- */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }
.kpi-tile {
  border: 1px solid var(--border-hair-soft);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  background: var(--bg-elevated);
}
.kpi-tile__label { font-size: 11px; color: var(--text-muted); margin-bottom: 8px; }
.kpi-tile__value { font-family: var(--font-display); font-size: 26px; font-weight: 700; color: var(--live); }
.kpi-tile__delta { font-size: 11px; margin-top: 6px; font-family: var(--font-mono); }
.kpi-tile__delta.up { color: var(--live); }
.kpi-tile__delta.down { color: var(--danger); }

/* ---- tabla / ranking ---- */
.viz-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.viz-table th {
  text-align: left;
  font-family: var(--font-mono);
  font-size: 10.5px;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 6px 10px;
  border-bottom: 1px solid var(--border-hair);
}
.viz-table td { padding: 9px 10px; border-bottom: 1px solid var(--border-hair-soft); }
.viz-table tr:last-child td { border-bottom: none; }
.rank-index {
  font-family: var(--font-mono);
  color: var(--text-faint);
  width: 28px;
  display: inline-block;
}
.rank-bar-track { background: var(--bg-deep); border-radius: 4px; height: 6px; overflow: hidden; margin-top: 5px; }
.rank-bar-fill { height: 100%; background: var(--live); border-radius: 4px; }

/* ---- mapa territorial esquemático ---- */
.territory-map { display: flex; flex-direction: column; gap: 10px; }
.territory-row {
  display: grid;
  grid-template-columns: 140px 1fr 70px;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-hair-soft);
  background: var(--bg-elevated);
}
.territory-row__name { font-size: 12.5px; font-weight: 500; }
.territory-row__track {
  position: relative;
  height: 10px;
  background: var(--bg-deep);
  border-radius: 5px;
  overflow: hidden;
}
.territory-row__fill { height: 100%; background: linear-gradient(90deg, var(--live-dim), var(--live)); border-radius: 5px; }
.territory-row__value { font-family: var(--font-mono); font-size: 12px; text-align: right; color: var(--text-muted); }

/* ---- comparación entre territorios ---- */
.compare-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.compare-col { border: 1px solid var(--border-hair-soft); border-radius: var(--radius-md); padding: 14px; background: var(--bg-elevated); }
.compare-col__title { font-family: var(--font-display); font-weight: 600; font-size: 13.5px; margin-bottom: 10px; color: var(--live); }
.compare-metric { display: flex; justify-content: space-between; font-size: 12.5px; padding: 6px 0; border-bottom: 1px solid var(--border-hair-soft); }
.compare-metric:last-child { border-bottom: none; }
.compare-metric__val { font-family: var(--font-mono); color: var(--text-primary); }

/* ---- chart canvas wrapper ---- */
.chart-wrap { position: relative; height: 280px; }

/* ---- badges de interpretación (observado/hipótesis/etc) ---- */
.interp-badge {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 2px 7px;
  border-radius: 4px;
  margin-right: 6px;
  border: 1px solid currentColor;
}
.interp-badge--dato { color: var(--text-muted); }
.interp-badge--relacion { color: var(--live); }
.interp-badge--interpretacion { color: #9FB8CE; }
.interp-badge--hipotesis { color: var(--amber); }
.interp-badge--recomendacion { color: var(--manual); }

.error-banner {
  border: 1px solid var(--danger);
  background: #E2685E14;
  color: var(--danger);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  font-size: 13px;
}

/* ============ RESPONSIVE ============ */
@media (max-width: 900px) {
  .app__body { flex-direction: column; }
  .conversation-rail { width: 100%; height: 46%; border-right: none; border-bottom: 1px solid var(--border-hair); }
  .canvas { flex: 1; }
  .compare-grid { grid-template-columns: 1fr; }
  .topbar__territories { order: 3; width: 100%; }
  .topbar { flex-wrap: wrap; }
}
@media (max-width: 520px) {
  .territory-row { grid-template-columns: 90px 1fr 56px; }
  .kpi-grid { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
}
