:root {
  --bg: #0f1a14;
  --surface: #16251c;
  --surface-2: #1e3226;
  --border: #2a4535;
  --text: #e8f5ec;
  --text-dim: #8fb39a;
  --accent: #3dd68c;
  --accent-dim: #2a9d63;
  --user-bg: #1a3d2a;
  --bot-bg: #16251c;
  --danger: #f07178;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --radius: 16px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  background-image:
    radial-gradient(ellipse at 20% 0%, rgba(61, 214, 140, 0.08) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(42, 157, 99, 0.06) 0%, transparent 50%);
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: rgba(15, 26, 20, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: var(--bg);
  font-weight: 700;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 20px rgba(61, 214, 140, 0.25);
}

.header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.header p {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 2px;
}

.icon-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 40px;
  height: 40px;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s;
}

.icon-btn:hover {
  color: var(--accent);
  border-color: var(--accent-dim);
}

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 88%;
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  align-self: flex-end;
  background: var(--user-bg);
  border: 1px solid var(--border);
  border-bottom-right-radius: 4px;
}

.message.bot {
  align-self: flex-start;
  background: var(--bot-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message.system {
  align-self: center;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  padding: 4px 8px;
  border: none;
}

.message.error {
  align-self: center;
  background: rgba(240, 113, 120, 0.12);
  border: 1px solid rgba(240, 113, 120, 0.35);
  color: var(--danger);
  font-size: 0.88rem;
}

.message.thinking {
  align-self: flex-start;
  color: var(--text-dim);
  font-style: italic;
  background: transparent;
  border: none;
  padding: 4px 0;
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

.suggestions {
  display: flex;
  gap: 8px;
  padding: 0 16px 10px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}

.suggestions::-webkit-scrollbar { display: none; }

.suggestions button {
  flex-shrink: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.suggestions button:hover {
  color: var(--accent);
  border-color: var(--accent-dim);
}

.input-bar {
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: rgba(15, 26, 20, 0.95);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#chat-form {
  display: flex;
  gap: 10px;
  align-items: center;
}

#message-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 14px 18px;
  border-radius: 24px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s;
}

#message-input:focus {
  border-color: var(--accent-dim);
}

#message-input::placeholder {
  color: var(--text-dim);
  opacity: 0.7;
}

#send-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: var(--bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.15s, opacity 0.15s;
}

#send-btn:hover { transform: scale(1.05); }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.graph-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #0a1410;
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.2s ease;
  padding-top: max(24px, env(safe-area-inset-top, 0px));
  padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
}

.graph-overlay.hidden {
  display: none;
}

.graph-overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: rgba(255,255,255,0.04);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}

.graph-overlay-header .avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2ecc71, #27ae60);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1rem; color: #fff;
  box-shadow: 0 0 12px rgba(46,204,113,0.4);
}

.graph-overlay-header h1 {
  font-size: 1.1rem; font-weight: 600; color: #f0f0f0;
}

.graph-overlay-header .subtitle {
  font-size: 0.75rem; color: #6db87a; margin-top: 1px;
}

.back-btn {
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  color: #c0d8c0;
  border-radius: 10px;
  padding: 8px 16px;
  font-size: 0.85rem;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s;
  display: flex; align-items: center; gap: 6px;
  font-family: var(--font);
}
.back-btn:hover { background: rgba(255,255,255,0.12); color: #fff; }

#graph-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#graph {
  width: 100%;
  height: 100%;
}

#graph-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #0a1410;
  z-index: 10;
  gap: 16px;
}

#graph-loading.hidden {
  display: none;
}

.spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(46,204,113,0.2);
  border-top-color: #2ecc71;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
#graph-loading p { color: #6db87a; font-size: 0.9rem; }

#graph-stats {
  position: absolute;
  top: 20px; left: 20px;
  background: rgba(10, 20, 14, 0.85);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 10px 16px;
  display: flex; gap: 20px;
}

#graph-stats .stat { text-align: center; }
#graph-stats .stat-num { font-size: 1.4rem; font-weight: 700; color: #2ecc71; }
#graph-stats .stat-label { font-size: 0.65rem; color: #6db87a; text-transform: uppercase; letter-spacing: 0.05em; }

#graph-legend {
  position: absolute;
  bottom: 20px;
  left: 20px;
  background: rgba(10, 20, 14, 0.85);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 12px 16px;
}

#graph-legend h3 { font-size: 0.75rem; color: #8ab89a; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.05em; }

.legend-item {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.8rem; color: #c0d8c0; margin-bottom: 5px;
}

.legend-dot {
  width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0;
}

#graph-info {
  position: absolute;
  top: 20px; right: 20px;
  background: rgba(10, 20, 14, 0.9);
  border: 1px solid rgba(46,204,113,0.25);
  border-radius: 14px;
  padding: 16px 20px;
  min-width: 220px;
  max-width: 300px;
  display: none;
}

#graph-info.show {
  display: block;
}

#graph-info h3 { font-size: 0.95rem; font-weight: 600; color: #2ecc71; margin-bottom: 6px; word-break: break-word; }
#graph-info p { font-size: 0.8rem; color: #8ab89a; line-height: 1.5; }

.cronologia-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 18px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.cronologia-card:hover {
  border-color: var(--accent-dim);
  background: var(--surface-2);
}
.cronologia-preview {
  font-size: 0.95rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cronologia-meta {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 4px;
}
.cronologia-del {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--danger);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s;
}
.cronologia-del:hover {
  background: rgba(240, 113, 120, 0.15);
  border-color: var(--danger);
}

@media (min-width: 768px) {
  body {
    max-width: 720px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
  .graph-overlay {
    max-width: 100vw;
  }
}
