:root {
  --primary: #1da1f2;
  --primary2: #0d8ddb;
  --bg: #f3f6fb;
  --card: #ffffff;
  --card2: #f8fbff;
  --text: #0f172a;
  --muted: #5f6b7a;
  --line: #dbe3ec;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius: 16px;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: "Plus Jakarta Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

[data-theme="dark"],
body.modo-oscuro {
  --bg: #171717;
  --card: #1f1f1f;
  --card2: #232323;
  --text: rgba(255, 255, 255, 0.92);
  --muted: rgba(255, 255, 255, 0.62);
  --line: rgba(255, 255, 255, 0.12);
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
}

/* Background: paper ripple + soft edge vignette (no neon, no fatigue) */
.page-bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(900px 420px at 12% 12%, rgba(29, 161, 242, 0.12), transparent 60%),
    radial-gradient(700px 360px at 88% 18%, rgba(13, 141, 219, 0.10), transparent 55%),
    radial-gradient(900px 520px at 50% 100%, rgba(29, 161, 242, 0.06), transparent 60%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.03), transparent 40%),
    var(--bg);
}

[data-theme="dark"] .page-bg,
body.modo-oscuro .page-bg {
  background:
    radial-gradient(900px 420px at 12% 12%, rgba(29, 161, 242, 0.13), transparent 60%),
    radial-gradient(700px 360px at 88% 18%, rgba(13, 141, 219, 0.10), transparent 55%),
    radial-gradient(900px 520px at 50% 100%, rgba(29, 161, 242, 0.07), transparent 60%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent 45%),
    var(--bg);
}

/* Header like Home (same order), but a new skin */
.main-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.nav-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 18px;
  gap: 12px;
}

.logo-section {
  display: flex;
  align-items: center;
  height: 46px;
  min-width: 190px;
}

.logo {
  width: 200px;
  height: 54px;
  object-fit: contain;
  display: block;
}

[data-theme="dark"] .logo,
body.modo-oscuro .logo {
  filter: brightness(0) invert(1);
}

.nav-center {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--muted);
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
  font-weight: 700;
  font-size: 13px;
}

.nav-link i {
  font-size: 14px;
}

.nav-link:hover {
  background: color-mix(in srgb, var(--primary) 86%, var(--primary2));
  border-color: transparent;
  color: #fff;
}

.nav-link.active {
  background: var(--primary);
  border-color: transparent;
  color: #fff;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.presence-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--card2) 75%, transparent);
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  white-space: nowrap;
}

.presence-badge::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.9);
  box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.18);
}

.presence-badge.online::before {
  background: #22c55e;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
}

.presence-badge.away::before {
  background: #f59e0b;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.18);
}

.presence-badge.offline::before {
  background: rgba(148, 163, 184, 0.9);
}

.pill {
  border-radius: 999px;
  border: 1px solid var(--line);
  padding: 9px 12px;
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
  background: color-mix(in srgb, var(--card2) 75%, transparent);
  color: var(--text);
}

.pill-soft {
  color: var(--muted);
}

.pill-accent {
  border-color: color-mix(in srgb, var(--primary) 50%, var(--line));
  background: linear-gradient(140deg, color-mix(in srgb, var(--primary) 24%, var(--card)), var(--card2));
  color: color-mix(in srgb, var(--primary2) 70%, var(--text));
}

.profile-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.profile-pic {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 2px solid color-mix(in srgb, var(--primary) 45%, var(--line));
  object-fit: cover;
  background: var(--card2);
}

/* Workspace */
.workspace {
  width: min(1240px, calc(100% - 22px));
  margin: 14px auto 18px;
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 14px;
}

.sidebar,
.chat-panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.sidebar {
  padding: 14px;
  max-height: calc(100vh - 104px);
  position: sticky;
  top: 86px;
  overflow: auto;
}

.side-top {
  display: grid;
  gap: 12px;
  margin-bottom: 12px;
}

.side-title h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: -0.3px;
}

.side-title p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.new-chat-btn {
  width: 100%;
  border: 1px solid color-mix(in srgb, var(--primary) 40%, var(--line));
  border-radius: 14px;
  padding: 12px 14px;
  background: linear-gradient(140deg, var(--primary), var(--primary2));
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}

.search-wrap input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card2);
  color: var(--text);
  padding: 11px 12px;
  font-size: 13px;
}

.search-wrap input:focus {
  outline: 0;
  border-color: color-mix(in srgb, var(--primary) 70%, var(--line));
  box-shadow: 0 0 0 4px rgba(29, 161, 242, 0.12);
}

.conversations-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.model-row {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card2);
  display: grid;
  gap: 8px;
}

.model-label {
  font-size: 12px;
  font-weight: 900;
  color: var(--muted);
}

.model-select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  padding: 10px 10px;
  font-size: 13px;
  font-weight: 800;
}

.model-select:focus {
  outline: 0;
  border-color: color-mix(in srgb, var(--primary) 70%, var(--line));
  box-shadow: 0 0 0 4px rgba(29, 161, 242, 0.12);
}

.model-hint {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.35;
}

.conversation-item {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 11px 12px;
  cursor: pointer;
  background: var(--card2);
  transition: 0.2s ease;
}

.conversation-item:hover {
  border-color: color-mix(in srgb, var(--primary) 45%, var(--line));
  transform: translateY(-1px);
}

.conversation-item.active {
  border-color: color-mix(in srgb, var(--primary) 65%, var(--line));
  background: color-mix(in srgb, var(--primary) 10%, var(--card2));
}

.conversation-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.conversation-title {
  font-size: 13px;
  font-weight: 900;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-time {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.conversation-meta {
  margin-top: 7px;
  font-size: 11px;
  color: var(--muted);
}

/* Chat panel */
.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  min-height: calc(100vh - 104px);
  overflow: hidden;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--card2) 60%, transparent);
}

.chat-header-info h2 {
  margin: 0;
  font-size: 16px;
}

.chat-header-info p {
  margin: 5px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.chat-header-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 9px 12px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 900;
}

.chat-header-btn:hover {
  border-color: #ef4444;
  color: #ef4444;
}

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

.message-row {
  display: flex;
  align-items: flex-start;
  position: relative;
}

.message-row.user {
  justify-content: flex-end;
}

.message-bubble {
  max-width: min(780px, 88%);
  border-radius: 18px;
  padding: 12px 13px;
  font-size: 14px;
  line-height: 1.52;
  border: 1px solid var(--line);
  background: var(--card2);
  color: var(--text);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.06);
  word-break: break-word;
}

.message-row.user .message-bubble {
  border-color: color-mix(in srgb, var(--primary) 60%, var(--line));
  background: color-mix(in srgb, var(--primary) 12%, var(--card2));
}

.message-actions {
  position: absolute;
  top: -8px;
  right: 10px;
  display: none;
}

.message-row.user:hover .message-actions {
  display: block;
}

.message-edit-btn {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--muted);
  border-radius: 999px;
  width: 26px;
  height: 26px;
  font-size: 12px;
  cursor: pointer;
}

.message-edit-btn:hover {
  color: var(--primary);
  border-color: color-mix(in srgb, var(--primary) 60%, var(--line));
}

.message-time {
  margin-top: 7px;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.message-bubble code {
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--primary) 28%, var(--line));
  border-radius: 8px;
  padding: 1px 6px;
  font-family: var(--mono);
  font-size: 12px;
}

.message-bubble pre {
  margin: 0;
  background: transparent;
  color: inherit;
  border-radius: 0;
  padding: 0;
  overflow: visible;
  font-family: var(--mono);
  font-size: 12px;
}

.codeblock {
  margin-top: 10px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.10);
  background: #1e1e1e; /* VS Code dark editor */
  color: #d4d4d4;
  overflow: hidden;
}

[data-theme="dark"] .codeblock,
body.modo-oscuro .codeblock {
  background: #1e1e1e;
}

.codeblock-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: #252526; /* VS Code title/tab strip */
}

.code-lang {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
  color: #d4d4d4;
  letter-spacing: 0.2px;
  text-transform: lowercase;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.code-lang.muted {
  opacity: 0.75;
}

.code-lang::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 3px;
  background: #3c3c3c;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.10);
}

.code-copy-btn {
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.18);
  color: rgba(212, 212, 212, 0.92);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
}

.code-copy-btn:hover {
  border-color: rgba(29, 161, 242, 0.45);
  background: rgba(29, 161, 242, 0.10);
}

.codeblock pre {
  padding: 10px 0;
  overflow: auto;
  max-height: 320px; /* keep messages compact; scroll inside like an editor */
  overscroll-behavior: contain;
}

.codeblock pre code {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
  white-space: pre;
  display: block;
  counter-reset: q-lineno;
}

.codeblock pre code .code-line {
  display: block;
  padding: 0 12px 0 56px; /* gutter space for line numbers */
  position: relative;
  white-space: pre;
}

.codeblock pre code .code-line::before {
  counter-increment: q-lineno;
  content: counter(q-lineno);
  position: absolute;
  left: 12px;
  width: 34px;
  text-align: right;
  color: rgba(212, 212, 212, 0.42);
  font-variant-numeric: tabular-nums;
}

.codeblock pre code .code-line:hover {
  background: rgba(255, 255, 255, 0.03);
}

@media (max-width: 520px) {
  .codeblock pre {
    max-height: 260px;
  }
}

.message-bubble a {
  color: var(--primary2);
}

/* Composer */
.composer {
  border-top: 1px solid var(--line);
  padding: 12px;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: color-mix(in srgb, var(--card2) 60%, transparent);
}

.composer-input-wrap {
  flex: 1;
}

#messageInput {
  width: 100%;
  min-height: 56px;
  max-height: 180px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 12px;
  font-size: 14px;
  background: var(--card2);
  color: var(--text);
}

#messageInput:focus {
  outline: 0;
  border-color: color-mix(in srgb, var(--primary) 70%, var(--line));
  box-shadow: 0 0 0 4px rgba(29, 161, 242, 0.12);
}

.char-counter {
  margin-top: 6px;
  text-align: right;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.send-btn {
  border: 0;
  border-radius: 14px;
  background: var(--primary);
  color: #fff;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 18px;
  font-weight: 900;
}

.send-btn:hover {
  background: var(--primary2);
}

.stop-btn {
  background: #ef4444;
}

.stop-btn:hover {
  background: #dc2626;
}

.hidden {
  display: none !important;
}

.send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.typing-indicator {
  padding: 0 16px 14px;
  font-size: 12px;
  color: var(--muted);
  display: none;
}

.typing-indicator.show {
  display: block;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(10px);
}

.modal {
  width: min(720px, 100%);
  border-radius: 18px;
  border: 1px solid var(--line);
  background: var(--card);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--card2);
}

.modal-head h3 {
  margin: 0;
  font-size: 16px;
}

.icon-x {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.modal-body {
  padding: 14px 16px 2px;
}

.modal-lead {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.plan-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.plan-card {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  background: var(--card2);
}

.plan-card.featured {
  border-color: color-mix(in srgb, var(--primary) 45%, var(--line));
  background: color-mix(in srgb, var(--primary) 8%, var(--card2));
}

.plan-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.plan-ul {
  margin: 10px 0 0;
  padding: 0 0 0 18px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.modal-note {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px dashed color-mix(in srgb, var(--primary) 30%, var(--line));
  color: var(--muted);
  font-size: 12px;
  background: color-mix(in srgb, var(--primary) 5%, var(--card2));
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  background: var(--card2);
}

.btn {
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
  border: 1px solid var(--line);
}

.btn.ghost {
  background: transparent;
  color: var(--muted);
}

.btn.primary {
  background: linear-gradient(140deg, var(--primary), var(--primary2));
  border-color: color-mix(in srgb, var(--primary) 45%, var(--line));
  color: #fff;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Scrollbars */
.sidebar::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar,
.message-bubble pre::-webkit-scrollbar {
  width: 10px;
}

.sidebar::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb,
.message-bubble pre::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--primary) 15%, var(--line));
  border: 3px solid var(--card);
  border-radius: 999px;
}

.sidebar::-webkit-scrollbar-thumb:hover,
.chat-messages::-webkit-scrollbar-thumb:hover,
.message-bubble pre::-webkit-scrollbar-thumb:hover {
  background: color-mix(in srgb, var(--primary) 28%, var(--line));
}

@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    max-height: unset;
  }

  .chat-panel {
    min-height: 68vh;
  }

  .logo-section {
    min-width: unset;
  }
}

@media (max-width: 680px) {
  .nav-center {
    display: none;
  }
}

@media (max-width: 520px) {
  .nav-actions .presence-badge {
    display: none;
  }
}

/* Mejorar estilos para markdown visual */
.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4 {
  margin-top: 16px;
  margin-bottom: 8px;
  font-weight: 700;
  color: var(--primary);
}

.message-bubble h1 {
  font-size: 1.4em;
  border-bottom: 2px solid var(--primary);
  padding-bottom: 8px;
}

.message-bubble h2 {
  font-size: 1.2em;
  margin-top: 12px;
}

.message-bubble h3 {
  font-size: 1.05em;
}

.message-bubble ul,
.message-bubble ol {
  margin: 8px 0;
  padding-left: 24px;
  line-height: 1.7;
}

.message-bubble li {
  margin: 4px 0;
  color: var(--text);
}

.message-bubble ul li::marker {
  color: var(--primary);
  font-weight: 600;
}

.message-bubble ol li::marker {
  color: var(--primary);
  font-weight: 600;
}

.message-bubble strong {
  color: var(--primary);
  font-weight: 700;
}

.message-bubble em {
  color: var(--muted);
  font-style: italic;
}

.message-bubble blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, var(--card2));
  border-radius: 4px;
  font-style: italic;
  color: var(--text);
}

.message-bubble hr {
  margin: 12px 0;
  border: none;
  border-top: 1px solid var(--line);
}

/* Estilos para tablas markdown */
.message-bubble table {
  margin: 8px 0;
  border-collapse: collapse;
  width: 100%;
  font-size: 0.95em;
}

.message-bubble table th,
.message-bubble table td {
  border: 1px solid var(--line);
  padding: 8px;
  text-align: left;
}

.message-bubble table th {
  background: color-mix(in srgb, var(--primary) 15%, var(--card2));
  font-weight: 700;
  color: var(--primary);
}

.message-bubble table tr:nth-child(even) {
  background: color-mix(in srgb, var(--primary) 5%, var(--card2));
}

/* Estilos para gráficos Plotly */
.message-bubble > div[id^="plotly-chart-"] {
  width: 100%;
  height: 400px;
  margin: 12px 0;
  border-radius: 8px;
  overflow: hidden;
  background: var(--card2);
  border: 1px solid var(--line);
}

/* Responsive para gráficos en mobile */
@media (max-width: 600px) {
  .message-bubble > div[id^="plotly-chart-"] {
    height: 300px;
  }
}
