/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #f0f2f5;
  --surface:     #ffffff;
  --sidebar-bg:  #1a1a2e;
  --sidebar-txt: #e0e0e0;
  --primary:     #2563eb;
  --primary-dk:  #1d4ed8;
  --user-bg:     #2563eb;
  --user-txt:    #ffffff;
  --agent-bg:    #ffffff;
  --agent-txt:   #1a1a1a;
  --border:      #e5e7eb;
  --muted:       #6b7280;
  --radius:      14px;
  --input-h:     56px;
  --header-h:    60px;
  --sidebar-w:   230px;
  --error:       #ef4444;
  --font:        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

html, body { height: 100%; overflow: hidden; font-family: var(--font); background: var(--bg); }
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

/* ── App wrapper ──────────────────────────────────────────────────────────── */
.app-wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ── Top bar ──────────────────────────────────────────────────────────────── */
.top-bar {
  width: 100%;
  flex-shrink: 0;
  background: #0d1b2e;
  line-height: 0;
}

.top-logo {
  display: block;
  width: 100%;
  height: clamp(60px, 17vw, 210px);
  object-fit: contain;
  object-position: center center;
}

/* ── Layout ───────────────────────────────────────────────────────────────── */
.layout {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ── Sidebar ──────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}


.sidebar-nav {
  flex: 1;
  padding: 12px;
}

.nav-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  color: var(--sidebar-txt);
  font-size: 13.5px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}
.nav-btn:hover, .nav-btn.active {
  background: rgba(255,255,255,0.15);
}


.sidebar-footer {
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: rgba(255,255,255,0.5);
  border-top: 1px solid rgba(255,255,255,0.08);
}

.dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot.connected    { background: #22c55e; }
.dot.disconnected { background: #ef4444; }
.dot.connecting   { background: #f59e0b; animation: pulse 1s infinite; }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:0.4; } }

/* ── Chat ─────────────────────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.chat-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.agent-name {
  font-size: 15px;
  font-weight: 600;
  color: #111;
}

/* ── Mensajes ─────────────────────────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 72%;
}
.msg.agent { align-self: flex-start; }
.msg.user  { align-self: flex-end; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
}

.msg.agent .msg-bubble {
  background: var(--agent-bg);
  color: var(--agent-txt);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.msg.user .msg-bubble {
  background: var(--user-bg);
  color: var(--user-txt);
  border-bottom-right-radius: 4px;
}

/* Markdown dentro de burbujas */
.msg-bubble p          { margin: 0 0 6px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble strong     { font-weight: 600; }
.msg-bubble ul, .msg-bubble ol { padding-left: 18px; margin: 4px 0; }
.msg-bubble li         { margin: 2px 0; }
.msg-bubble code       { background: rgba(0,0,0,0.07); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.msg-bubble pre code   { display: block; padding: 8px; overflow-x: auto; }
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { margin: 6px 0 4px; font-size: 14px; }

/* Prompt del agente (pregunta) */
.msg.prompt .msg-bubble {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #1e40af;
}

/* Error */
.msg.error .msg-bubble {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

/* Timestamp */
.msg-time {
  font-size: 11px;
  color: var(--muted);
  margin-top: 3px;
  padding: 0 4px;
}
.msg.user .msg-time { text-align: right; }

/* Typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
  background: var(--agent-bg);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
  width: fit-content;
}
.typing-indicator span {
  width: 7px; height: 7px;
  background: var(--muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40%           { transform: translateY(-6px); }
}

/* File card */
.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 10px;
  padding: 10px 14px;
  margin-top: 6px;
  text-decoration: none;
  color: #15803d;
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.15s;
}
.file-card:hover { background: #dcfce7; }
.file-card svg   { flex-shrink: 0; }

/* ── Barra de opciones (botones de menú) ──────────────────────────────────── */
.options-bar {
  padding: 10px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.opt-btn {
  padding: 8px 16px;
  border: 1.5px solid var(--primary);
  border-radius: 20px;
  background: transparent;
  color: var(--primary);
  font-size: 13.5px;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
.opt-btn:hover {
  background: var(--primary);
  color: #fff;
}
.opt-btn.selected {
  background: var(--primary);
  color: #fff;
  opacity: 0.7;
  cursor: default;
}

/* ── Input bar ────────────────────────────────────────────────────────────── */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

#input {
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  resize: none;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 14px;
  font-family: var(--font);
  outline: none;
  background: var(--bg);
  color: #111;
  transition: border-color 0.15s;
  overflow-y: auto;
}
#input:focus        { border-color: var(--primary); }
#input:disabled     { background: #f9fafb; color: var(--muted); cursor: not-allowed; }
#input::placeholder { color: #9ca3af; }

#send-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}
#send-btn:hover:not(:disabled)  { background: var(--primary-dk); }
#send-btn:active:not(:disabled) { transform: scale(0.94); }
#send-btn:disabled { background: #d1d5db; cursor: not-allowed; }

/* Botón ↵ para aceptar valor por defecto en prompts de agente */
#enter-btn {
  height: 40px;
  padding: 0 12px;
  border-radius: 20px;
  border: 1.5px solid var(--primary);
  background: transparent;
  color: var(--primary);
  font-size: 15px;
  font-family: var(--font);
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  align-items: center;
  gap: 4px;
}
#enter-btn:hover { background: var(--primary); color: #fff; }
.enter-hint {
  font-size: 12px;
  font-weight: 400;
  opacity: 0.85;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  vertical-align: middle;
}

/* Botón de micrófono */
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: transparent;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.icon-btn:hover {
  background: var(--bg);
  color: #374151;
  border-color: #9ca3af;
}
.icon-btn.active {
  background: #fee2e2;
  border-color: var(--error);
  color: var(--error);
}
.icon-btn.active:hover {
  background: #fecaca;
}

/* Botón de llamada de voz (Realtime) cuando está activo */
.icon-btn.calling {
  background: var(--error, #e5534b);
  border-color: var(--error, #e5534b);
  color: #fff;
  animation: rt-pulse 1.4s ease-in-out infinite;
}
@keyframes rt-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(229, 83, 75, .55); }
  50%      { box-shadow: 0 0 0 9px rgba(229, 83, 75, 0); }
}

/* Mensaje de acción inline (↵ aceptar default) */
.msg-action {
  align-self: center;
  font-size: 11.5px;
  color: var(--muted);
  padding: 2px 10px;
  background: rgba(0,0,0,0.04);
  border-radius: 10px;
  margin: 2px 0;
  max-width: 60%;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Drag & drop overlay ─────────────────────────────────────────────────── */
.chat-area.drag-over::after {
  content: "Suelta el archivo aquí";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(37, 99, 235, 0.08);
  border: 2px dashed var(--primary);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  color: var(--primary);
  pointer-events: none;
  z-index: 10;
}
.chat-area { position: relative; }

/* ── PDF Preview inline ───────────────────────────────────────────────────── */
.pdf-preview-wrapper { max-width: 92%; width: 680px; }

.pdf-preview-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.10);
}

.pdf-preview-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 500;
  color: #374151;
}

.pdf-preview-titulo {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pdf-preview-open {
  color: var(--primary);
  display: flex;
  align-items: center;
  padding: 3px;
  border-radius: 4px;
  transition: background 0.15s;
  flex-shrink: 0;
}
.pdf-preview-open:hover { background: #eff6ff; }

.pdf-preview-iframe {
  display: block;
  width: 100%;
  height: 520px;
  border: none;
  background: #f1f5f9;
}

.pdf-preview-actions {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  background: #f8fafc;
  border-top: 1px solid var(--border);
}

.pdf-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 18px;
  border-radius: 8px;
  border: none;
  font-size: 13.5px;
  font-family: var(--font);
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.pdf-action-approve {
  background: #16a34a;
  color: #fff;
}
.pdf-action-approve:hover:not(:disabled) { background: #15803d; }

.pdf-action-cancel {
  background: transparent;
  color: #dc2626;
  border: 1.5px solid #fca5a5;
}
.pdf-action-cancel:hover:not(:disabled) { background: #fef2f2; }

.pdf-action-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.pdf-action-btn.selected { opacity: 0.65; }

/* ── Scrollbar sutil ──────────────────────────────────────────────────────── */
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 2px; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .sidebar { display: none; }
  .msg { max-width: 90%; }
  .top-logo { height: clamp(70px, 20vw, 130px); }
}

/* ── iOS / iPad fixes ─────────────────────────────────────────────────────── */
@supports (-webkit-touch-callout: none) {
  html, body { height: -webkit-fill-available; }
  .app-wrapper { min-height: -webkit-fill-available; }
  #input { font-size: 16px; } /* evita zoom automático al enfocar en iOS */
}
