:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --bubble-user-bg: #2563eb;
  --bubble-user-text: #ffffff;
  --bubble-bot-bg: #f0f1f5;
  --bubble-bot-text: #1a1a2e;
  --accent: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14151a;
    --panel: #1c1d24;
    --text: #e8e8ec;
    --text-muted: #9497a3;
    --border: #2c2d36;
    --bubble-user-bg: #3b82f6;
    --bubble-user-text: #ffffff;
    --bubble-bot-bg: #26272f;
    --bubble-bot-text: #e8e8ec;
    --accent: #3b82f6;
  }
}

:root[data-theme="dark"] {
  --bg: #14151a;
  --panel: #1c1d24;
  --text: #e8e8ec;
  --text-muted: #9497a3;
  --border: #2c2d36;
  --bubble-user-bg: #3b82f6;
  --bubble-user-text: #ffffff;
  --bubble-bot-bg: #26272f;
  --bubble-bot-text: #e8e8ec;
  --accent: #3b82f6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  height: 100vh;
  height: 100dvh; /* se ajusta cuando aparece el teclado en mobile, a diferencia de vh */
  overflow: hidden;
}

/* ===================== screens (login / agents / chat) ===================== */

.screen {
  height: 100%;
  width: 100%;
}

.screen[hidden] {
  display: none !important;
}

/* ===================== login ===================== */

#screen-login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}

.login-card h1 {
  font-size: 17px;
  margin: 2px 0 14px;
}

.login-card input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 14.5px;
  outline: none;
}

.login-card input:focus {
  border-color: var(--accent);
}

.login-card button[type="submit"] {
  width: 100%;
  padding: 11px;
  margin-top: 6px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.login-card button[type="submit"]:hover {
  opacity: 0.9;
}

.error-text {
  color: #ef4444;
  font-size: 13px;
  margin: 2px 0 0;
  text-align: center;
}

/* ===================== menu de agentes ===================== */

#screen-agents {
  display: flex;
  flex-direction: column;
  max-width: 780px;
  margin: 0 auto;
  padding: 28px 24px;
  overflow-y: auto;
}

.simple-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 22px;
}

.simple-header h1 {
  font-size: 20px;
  margin: 0;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13.5px;
  cursor: pointer;
}

.link-btn:hover {
  text-decoration: underline;
}

.agents-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.agent-card {
  background: var(--panel);
  color: var(--text); /* los <button> no heredan color por defecto del navegador */
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-family: inherit;
  transition: border-color 0.15s, transform 0.1s;
}

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

.agent-card:active {
  transform: scale(0.98);
}

.agent-card h3 {
  margin: 0;
  font-size: 15px;
}

.agent-card p {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.empty-hint {
  color: var(--text-muted);
  font-size: 13.5px;
}

/* ===================== chat + sidebar ===================== */

#screen-chat {
  position: relative;
  display: flex;
  height: 100%;
  overflow: hidden;
}

.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 84vw;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  z-index: 20;
}

.sidebar.open {
  transform: translateX(0);
}

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

.new-conv-btn {
  margin: 12px 16px;
  padding: 10px;
  border-radius: 10px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--accent);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.new-conv-btn:hover {
  background: var(--bg);
}

.conversations-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.conversation-item {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  gap: 4px;
  gap: 2px;
}

.conversation-item:hover {
  background: var(--bg);
}

.conversation-item.active {
  background: var(--bg);
  border-color: var(--border);
}

.conversation-item .conv-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.conversation-item .conv-name {
  font-weight: 600;
  font-size: 13.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item .conv-date {
  font-size: 11.5px;
  color: var(--text-muted);
}

.conv-rename-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
  width: 24px;
  height: 24px;
  border-radius: 6px;
}

.conv-rename-btn:hover {
  color: var(--text);
  background: var(--border);
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 15;
}

.sidebar-backdrop[hidden] {
  display: none;
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--bg);
}

.chat-shell {
  flex: 1;
  min-width: 0;
  height: 100%;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.docs-btn {
  margin-left: auto;
}

.docs-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 12px;
  width: min(320px, calc(100vw - 24px));
  max-height: 320px;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  z-index: 20;
  padding: 10px;
}

.docs-dropdown-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 2px 6px 8px;
}

.docs-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.docs-empty {
  font-size: 13px;
  color: var(--text-muted);
  padding: 6px;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
  font-size: 13px;
}

.doc-item:hover {
  background: var(--bg);
}

.doc-item .doc-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-status {
  flex-shrink: 0;
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
}

.doc-status.attached {
  color: #16a34a;
  border-color: #16a34a;
}

.doc-status.pending {
  color: #d97706;
  border-color: #d97706;
}

.doc-status.stored {
  color: var(--accent);
  border-color: var(--accent);
}

.doc-download {
  flex-shrink: 0;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 14px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.doc-download:hover {
  color: var(--text);
  background: var(--border);
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bubble-bot-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.avatar-lg {
  width: 64px;
  height: 64px;
  font-size: 32px;
}

.header-text h1 {
  font-size: 15px;
  margin: 0;
  font-weight: 600;
}

.status {
  font-size: 12px;
  color: var(--text-muted);
}

.status::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22c55e;
  margin-right: 5px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14.5px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: pop-in 0.15s ease-out;
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msg.user {
  align-self: flex-end;
  background: var(--bubble-user-bg);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 4px;
}

.msg.bot {
  align-self: flex-start;
  background: var(--bubble-bot-bg);
  color: var(--bubble-bot-text);
  border-bottom-left-radius: 4px;
  white-space: normal; /* el HTML renderizado ya maneja sus propios saltos */
}

/* Markdown renderizado (marked.js) dentro de un mensaje del asistente */
.msg.bot > :first-child {
  margin-top: 0;
}
.msg.bot > :last-child {
  margin-bottom: 0;
}
.msg.bot p {
  margin: 0 0 8px;
}
.msg.bot ul,
.msg.bot ol {
  margin: 0 0 8px;
  padding-left: 22px;
}
.msg.bot li {
  margin: 2px 0;
}
.msg.bot a {
  color: var(--accent);
}
.msg.bot strong {
  font-weight: 700;
}
.msg.bot code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: rgba(127, 127, 127, 0.18);
  padding: 1px 5px;
  border-radius: 5px;
}
.msg.bot pre {
  background: rgba(127, 127, 127, 0.18);
  padding: 10px 12px;
  border-radius: 10px;
  overflow-x: auto;
  margin: 0 0 8px;
}
.msg.bot pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}
.msg.bot blockquote {
  margin: 0 0 8px;
  padding-left: 10px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.msg.bot h1,
.msg.bot h2,
.msg.bot h3 {
  margin: 10px 0 6px;
  font-size: 1.05em;
}

.msg.typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 14px;
}

.msg.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  opacity: 0.6;
  animation: bounce 1.2s infinite;
}

.msg.typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.msg.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.chat-input-bar {
  display: flex;
  gap: 10px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input-bar input {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 999px;
  padding: 11px 16px;
  font-size: 14.5px;
  outline: none;
}

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

.chat-input-bar button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s;
}

.chat-input-bar button:hover {
  opacity: 0.9;
}

.chat-input-bar button:disabled {
  opacity: 0.5;
  cursor: default;
}

/* El boton de adjuntar usa .icon-btn (transparente, sin el circulo azul
   del boton de enviar) - .chat-input-bar .icon-btn tiene mas
   especificidad que .chat-input-bar button, asi que gana esta regla. */
.chat-input-bar .icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font-size: 17px;
}

.chat-input-bar .icon-btn:hover {
  background: var(--bg);
  opacity: 1;
}

/* Estado activo del boton de dictado por voz - color + pulso suave para
   que quede claro que esta escuchando (a diferencia del boton de
   adjuntar, que no tiene un estado "en curso" que mostrar). */
#mic-btn.recording {
  color: #e5484d;
  background: rgba(229, 72, 77, 0.12);
  animation: mic-pulse 1.4s ease-in-out infinite;
}

@keyframes mic-pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(229, 72, 77, 0.35);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(229, 72, 77, 0);
  }
}
