/* ═══════════════════════════════════════════
   web-ia — estilos (tema claro/oscuro/sistema)
   ═══════════════════════════════════════════ */
:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", SFMono-Regular, ui-monospace, Menlo, Consolas, "Liberation Mono", monospace;
  --radius: 12px;
  --radius-sm: 8px;
  --trans: 0.18s ease;
}

/* ─────── Tema claro ─────── */
:root, :root[data-theme="light"] {
  --bg: #f7f7f8;
  --bg-sidebar: #f0f0f2;
  --bg-chat: #f7f7f8;
  --card: #ffffff;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --text: #18181b;
  --text-2: #52525b;
  --text-3: #8a8a94;
  --accent: #7c3aed;
  --accent-2: #6d28d9;
  --accent-soft: rgba(124, 58, 237, 0.1);
  --user-bubble: #7c3aed;
  --user-text: #ffffff;
  --code-bg: #f4f4f5;
  --code-block-bg: #fafafa;
  --hover: rgba(0, 0, 0, 0.05);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
  --ok: #16a34a;
  --err: #dc2626;
  --warn: #d97706;
}

/* ─────── Tema oscuro ─────── */
:root[data-theme="dark"] {
  --bg: #0d0d10;
  --bg-sidebar: #111114;
  --bg-chat: #0d0d10;
  --card: #17171b;
  --border: #26262c;
  --border-strong: #34343c;
  --text: #ededf0;
  --text-2: #a1a1aa;
  --text-3: #71717a;
  --accent: #8b5cf6;
  --accent-2: #a78bfa;
  --accent-soft: rgba(139, 92, 246, 0.14);
  --user-bubble: #6d28d9;
  --user-text: #f5f3ff;
  --code-bg: #1c1c22;
  --code-block-bg: #141418;
  --hover: rgba(255, 255, 255, 0.06);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
  --ok: #22c55e;
  --err: #ef4444;
  --warn: #f59e0b;
}

/* ─────── Base ─────── */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg-chat);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button { font-family: inherit; cursor: pointer; background: none; border: none; color: inherit; }
input, textarea { font-family: inherit; color: inherit; }
.hidden { display: none !important; }

/* ═══════════ LOGIN ═══════════ */
.login-screen {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), transparent 60%), var(--bg-chat);
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--card);
  border: 1px solid var(--border); border-radius: 16px;
  padding: 40px 32px; text-align: center; box-shadow: var(--shadow-lg);
  animation: pop 0.3s ease;
}
@keyframes pop { from { opacity: 0; transform: scale(0.96) translateY(8px); } to { opacity: 1; transform: none; } }
.login-logo { font-size: 44px; margin-bottom: 8px; }
.login-card h1 { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.login-sub { color: var(--text-2); font-size: 13px; margin-bottom: 24px; }
.login-card input {
  width: 100%; padding: 12px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--bg);
  font-size: 15px; margin-bottom: 12px; outline: none;
  transition: border-color var(--trans);
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.login-error { color: var(--err); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ═══════════ BOTONES ═══════════ */
.btn-primary {
  background: var(--accent); color: #fff; font-weight: 600;
  border-radius: var(--radius-sm); padding: 11px 18px; font-size: 14px;
  transition: background var(--trans), transform var(--trans);
}
.btn-primary:hover { background: var(--accent-2); }
.btn-primary:active { transform: scale(0.98); }
.btn-block { width: 100%; }

/* ═══════════ APP LAYOUT ═══════════ */
.app { display: flex; height: 100dvh; }

/* ─────── Sidebar ─────── */
.sidebar {
  width: 268px; flex-shrink: 0; background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
  transition: width var(--trans);
}
.sidebar-head { display: flex; align-items: center; gap: 6px; padding: 12px; }
.btn-new {
  flex: 1; display: flex; align-items: center; gap: 8px;
  background: var(--card); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 10px 12px;
  font-size: 13.5px; font-weight: 600; color: var(--text);
  transition: background var(--trans), border-color var(--trans);
  white-space: nowrap; overflow: hidden;
}
.btn-new:hover { background: var(--hover); border-color: var(--accent); }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  color: var(--text-2); transition: background var(--trans), color var(--trans);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

.sidebar-search {
  display: flex; align-items: center; gap: 8px; margin: 0 12px 8px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 10px; color: var(--text-3);
}
.sidebar-search:focus-within { border-color: var(--accent); }
.sidebar-search input {
  border: none; outline: none; background: none; width: 100%;
  font-size: 13px; color: var(--text);
}
.sidebar-search input::placeholder { color: var(--text-3); }

.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 8px; min-height: 0; }
.conv-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: var(--radius-sm);
  color: var(--text-2); font-size: 13.5px; cursor: pointer;
  transition: background var(--trans);
  position: relative; overflow: hidden;
}
.conv-item:hover { background: var(--hover); }
.conv-item.active { background: var(--accent-soft); color: var(--text); }
.conv-item.active::before {
  content: ""; position: absolute; left: 0; top: 8px; bottom: 8px; width: 3px;
  background: var(--accent); border-radius: 3px;
}
.conv-item .conv-icon { flex-shrink: 0; opacity: 0.8; }
.conv-item .conv-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-time { font-size: 11px; color: var(--text-3); flex-shrink: 0; }
.conv-item .conv-menu-btn { opacity: 0; width: 26px; height: 26px; flex-shrink: 0; color: var(--text-3); }
.conv-item:hover .conv-menu-btn { opacity: 1; }
.conv-item.active .conv-menu-btn { opacity: 1; }

.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-top: 1px solid var(--border);
}
.user-chip { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent-soft); display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.user-info { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.user-info strong { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge-online { font-size: 11px; color: var(--ok); display: flex; align-items: center; gap: 4px; }
.badge-online i { width: 6px; height: 6px; border-radius: 50%; background: var(--ok); display: inline-block; animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* modo compacto (barra contraída) */
.app.sidebar-collapsed .sidebar { width: 60px; }
.app.sidebar-collapsed .btn-new span,
.app.sidebar-collapsed .sidebar-search,
.app.sidebar-collapsed .conv-item .conv-title,
.app.sidebar-collapsed .conv-item .conv-time,
.app.sidebar-collapsed .user-info,
.app.sidebar-collapsed #btn-collapse { display: none; }
.app.sidebar-collapsed .sidebar-head { justify-content: center; padding: 12px 8px; }
.app.sidebar-collapsed .btn-new { padding: 10px; justify-content: center; }
.app.sidebar-collapsed .sidebar-foot { justify-content: center; padding: 10px 6px; }
.app.sidebar-collapsed .conv-item { justify-content: center; padding: 9px 0; }
.app.sidebar-collapsed .conv-item.active::before { left: 2px; right: auto; }

/* ─────── Área principal ─────── */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.mobile-bar {
  display: none; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--bg-chat);
  border-bottom: 1px solid var(--border);
}
.mobile-title { flex: 1; font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-window { flex: 1; overflow-y: auto; min-height: 0; scroll-behavior: smooth; }

/* ─────── Empty state ─────── */
.empty-state {
  height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center;
  padding: 24px; gap: 6px; animation: fadein 0.4s ease;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.empty-logo { font-size: 52px; margin-bottom: 10px; animation: float 4s ease-in-out infinite; }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
.empty-state h2 { font-size: 22px; font-weight: 700; }
.empty-state p { color: var(--text-2); font-size: 14.5px; max-width: 440px; margin-bottom: 18px; }
.empty-suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; max-width: 720px; width: 100%; }
.suggestion {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; font-size: 13.5px;
  color: var(--text-2); text-align: left; transition: border-color var(--trans), box-shadow var(--trans), transform var(--trans);
  animation: pop 0.35s ease both;
}
.suggestion:nth-child(2) { animation-delay: 0.05s; }
.suggestion:nth-child(3) { animation-delay: 0.1s; }
.suggestion:nth-child(4) { animation-delay: 0.15s; }
.suggestion:hover { border-color: var(--accent); box-shadow: var(--shadow); transform: translateY(-1px); color: var(--text); }

/* ─────── Mensajes ─────── */
.messages { max-width: 860px; margin: 0 auto; padding: 20px 20px 30px; display: flex; flex-direction: column; gap: 22px; }

.msg { display: flex; gap: 14px; animation: fadein 0.25s ease; }
.msg-avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
  background: var(--accent-soft); margin-top: 2px;
}
.msg-body { flex: 1; min-width: 0; }
.msg-role { font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 4px; display: flex; align-items: center; gap: 8px; }
.msg-time { font-weight: 400; color: var(--text-3); font-size: 11.5px; }

/* burbuja del usuario */
.msg.user { flex-direction: row-reverse; }
.msg.user .msg-role { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); color: var(--user-text);
  border-radius: 16px 16px 4px 16px; padding: 10px 14px;
  font-size: 14.5px; line-height: 1.55; white-space: pre-wrap; word-break: break-word;
  max-width: 82%; box-shadow: var(--shadow);
}
.msg.user .msg-avatar { background: rgba(255,255,255,0.12); }

/* contenido del asistente */
.msg.ai .content { font-size: 14.5px; line-height: 1.7; word-break: break-word; }
.msg.ai .content > * + * { margin-top: 0.75em; }
.msg.ai .content h1, .msg.ai .content h2, .msg.ai .content h3, .msg.ai .content h4 {
  font-weight: 650; line-height: 1.3; margin-top: 1.2em;
}
.msg.ai .content h1 { font-size: 1.35em; }
.msg.ai .content h2 { font-size: 1.2em; }
.msg.ai .content h3 { font-size: 1.08em; }
.msg.ai .content ul, .msg.ai .content ol { padding-left: 1.4em; }
.msg.ai .content li { margin: 0.3em 0; }
.msg.ai .content a { color: var(--accent-2); text-decoration: none; border-bottom: 1px solid var(--accent-soft); }
.msg.ai .content a:hover { border-bottom-color: var(--accent); }
.msg.ai .content strong { font-weight: 650; }
.msg.ai .content code {
  font-family: var(--mono); font-size: 0.88em; background: var(--code-bg);
  padding: 2px 6px; border-radius: 5px; color: var(--text);
}
.msg.ai .content pre {
  background: var(--code-block-bg); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px 16px; overflow-x: auto;
  font-size: 13px; line-height: 1.55;
}
.msg.ai .content pre code { background: none; padding: 0; font-size: 13px; }
.msg.ai .content blockquote {
  border-left: 3px solid var(--accent); padding: 2px 14px;
  color: var(--text-2); background: var(--accent-soft); border-radius: 0 8px 8px 0;
}
.msg.ai .content hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }
.msg.ai .content img { max-width: 100%; border-radius: var(--radius-sm); }

/* tablas markdown */
.msg.ai .content table {
  border-collapse: collapse; width: 100%; margin: 0.6em 0; font-size: 13.5px;
  overflow: hidden; border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.msg.ai .content th {
  background: var(--code-bg); font-weight: 650; text-align: left;
  padding: 8px 12px; border-bottom: 1px solid var(--border-strong);
}
.msg.ai .content td { padding: 7px 12px; border-bottom: 1px solid var(--border); }
.msg.ai .content tr:last-child td { border-bottom: none; }
.msg.ai .content tbody tr:hover td { background: var(--hover); }

/* ─────── indicador escribiendo ─────── */
.typing { display: inline-flex; gap: 5px; padding: 6px 0; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--text-3);
  animation: bounce 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-5px); opacity: 1; } }

.tool-status {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--text-3); margin-top: 8px;
  background: var(--code-bg); border-radius: 20px; padding: 5px 12px;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.spinner {
  width: 13px; height: 13px; border-radius: 50%; flex-shrink: 0;
  border: 2px solid var(--text-3); border-top-color: transparent;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─────── archivos generados por la IA ─────── */
.file-card {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-top: 12px;
  transition: border-color var(--trans), box-shadow var(--trans);
}
.file-card:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.file-card .f-icon { font-size: 26px; flex-shrink: 0; }
.file-card .f-info { flex: 1; min-width: 0; }
.file-card .f-name { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-card .f-meta { font-size: 11.5px; color: var(--text-3); }
.file-card .f-dl {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent); color: #fff; font-size: 13px; font-weight: 600;
  padding: 8px 14px; border-radius: var(--radius-sm); text-decoration: none;
  transition: background var(--trans); flex-shrink: 0;
}
.file-card .f-dl:hover { background: var(--accent-2); }
.file-card .f-drive { color: var(--accent-2); font-size: 12.5px; text-decoration: none; border-bottom: 1px dashed; }

/* ─────── compositor ─────── */
.composer-wrap {
  border-top: 1px solid var(--border); padding: 10px 16px 8px;
  background: var(--bg-chat);
}
.composer {
  max-width: 860px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--card); border: 1px solid var(--border-strong);
  border-radius: 16px; padding: 8px 8px 8px 12px;
  box-shadow: var(--shadow); transition: border-color var(--trans), box-shadow var(--trans);
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
.composer textarea {
  flex: 1; border: none; outline: none; background: none; resize: none;
  font-size: 14.5px; line-height: 1.5; max-height: 180px; padding: 8px 4px;
  min-height: 26px;
}
.composer textarea::placeholder { color: var(--text-3); }
.btn-send {
  width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff;
  transition: background var(--trans), transform var(--trans), opacity var(--trans);
}
.btn-send:hover { background: var(--accent-2); }
.btn-send:active { transform: scale(0.92); }
.btn-send:disabled { background: var(--border-strong); color: var(--text-3); cursor: default; }
.composer-hint { text-align: center; font-size: 11px; color: var(--text-3); margin-top: 6px; }

/* ─────── preview de adjuntos ─────── */
.preview-bar {
  max-width: 860px; margin: 0 auto 8px;
  display: flex; flex-wrap: wrap; gap: 8px;
}
.preview-chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--card); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 6px 8px 6px 10px;
  font-size: 12.5px; animation: pop 0.2s ease;
}
.preview-chip .p-icon { font-size: 18px; }
.preview-chip .p-name { max-width: 180px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-chip .p-size { color: var(--text-3); font-size: 11px; }
.preview-chip .p-x { width: 22px; height: 22px; border-radius: 5px; color: var(--text-3); display: flex; align-items: center; justify-content: center; }
.preview-chip .p-x:hover { background: var(--err); color: #fff; }
.preview-chip.p-img { padding: 6px; align-items: flex-start; }
.preview-chip.p-img img { width: 52px; height: 52px; object-fit: cover; border-radius: 6px; }

/* ─────── menú contextual ─────── */
.menu-pop {
  position: fixed; z-index: 200; min-width: 170px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 5px;
  animation: pop 0.12s ease;
}
.menu-item {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 8px 10px; font-size: 13.5px; border-radius: 7px;
  color: var(--text-2); text-align: left;
}
.menu-item:hover { background: var(--hover); color: var(--text); }
.menu-item.danger { color: var(--err); }
.menu-item.danger:hover { background: rgba(220, 38, 38, 0.1); }

/* ─────── toasts ─────── */
#toast-wrap { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 300; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 20px; padding: 9px 18px; font-size: 13.5px;
  box-shadow: var(--shadow-lg); animation: toastin 0.25s ease;
  display: flex; align-items: center; gap: 8px; max-width: 90vw;
}
.toast.err { border-color: var(--err); color: var(--err); }
.toast.ok { border-color: var(--ok); color: var(--ok); }
@keyframes toastin { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

/* ─────── modo icono del tema ─────── */
.icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }

/* ─────── scrollbars ─────── */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }
::-webkit-scrollbar-track { background: transparent; }

/* ─────── RESPONSIVE ─────── */
@media (max-width: 860px) {
  .messages { padding: 14px 12px 24px; }
  .mobile-bar { display: flex; }
  .sidebar {
    position: fixed; z-index: 90; top: 0; bottom: 0; left: 0;
    width: min(300px, 84vw); box-shadow: var(--shadow-lg);
    transform: translateX(-105%); transition: transform 0.25s ease;
  }
  .app.sidebar-open .sidebar { transform: none; }
  .app.sidebar-collapsed .sidebar { width: min(300px, 84vw); }
  .app.sidebar-collapsed .btn-new span,
  .app.sidebar-collapsed .sidebar-search,
  .app.sidebar-collapsed .conv-item .conv-title,
  .app.sidebar-collapsed .conv-item .conv-time,
  .app.sidebar-collapsed .user-info { display: block; }
  .app.sidebar-collapsed .sidebar-head { justify-content: flex-start; padding: 12px; }
  .app.sidebar-collapsed .btn-new { padding: 10px 12px; justify-content: flex-start; }
  .app.sidebar-collapsed .conv-item { justify-content: flex-start; padding: 9px 10px; }
  .app.sidebar-collapsed .conv-item.active::before { left: 0; }
  .sidebar-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 85; animation: fadein 0.2s ease; }
  .msg.user .bubble { max-width: 90%; }
  .empty-suggestions { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .msg { gap: 10px; }
  .msg-avatar { width: 26px; height: 26px; font-size: 13px; }
  .composer-wrap { padding: 8px 8px 6px; }
  .empty-state h2 { font-size: 19px; }
  .file-card { flex-wrap: wrap; }
  .file-card .f-dl { width: 100%; justify-content: center; }
}
