:root {
  --bg: #0f172a;
  --bg-grad: radial-gradient(1200px 600px at 20% -10%, #1e293b 0%, #0f172a 55%);
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #818cf8;
  --bubble-user: linear-gradient(135deg, #38bdf8, #818cf8);
  --bubble-assistant: #273449;
  --border: #334155;
  --radius: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-grad);
  color: var(--text);
  min-height: 100vh;
}

.app {
  display: flex;
  flex-direction: row;
  gap: 16px;
  max-width: 1100px;
  margin: 0 auto;
  height: 100vh;
  padding: 16px;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* ---- Sidebar ---- */
.sidebar {
  width: 260px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.new-chat {
  padding: 12px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06121f;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
}
.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.conv-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.conv-item:hover { border-color: var(--accent); }
.conv-item.active { border-color: var(--accent); background: rgba(56, 189, 248, .1); }
.conv-title {
  flex: 1;
  min-width: 0;
  padding: 4px 6px;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.conv-menu-btn {
  flex: 0 0 auto;
  width: 26px; height: 26px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.conv-menu-btn:hover { background: rgba(148,163,184,.15); color: var(--text); }
.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.user-name {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.logout {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
}
.logout:hover { color: #f87171; border-color: #f87171; }

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 20px; color: #0b1220;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-radius: 12px;
}
.topbar h1 { margin: 0; font-size: 18px; }
.subtitle { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #4ade80; box-shadow: 0 0 8px #4ade80; }

/* ---- Chat ---- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 18px 4px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.msg { display: flex; gap: 10px; max-width: 85%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.avatar {
  width: 34px; height: 34px; flex: 0 0 auto;
  display: grid; place-items: center;
  font-weight: 700; border-radius: 50%;
  background: var(--panel-2); color: var(--accent);
  border: 1px solid var(--border);
}
.msg.user .avatar { display: none; }
.bubble {
  padding: 12px 16px;
  border-radius: 16px;
  background: var(--bubble-assistant);
  border: 1px solid var(--border);
  line-height: 1.5;
  font-size: 15px;
  white-space: pre-wrap;
}
.msg.user .bubble { background: var(--bubble-user); color: #06121f; border: none; font-weight: 500; }

/* ---- Fuentes / recursos ---- */
.sources { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.source {
  display: block;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  text-decoration: none;
  color: var(--text);
  transition: border-color .15s, transform .15s;
}
.source:hover { border-color: var(--accent); transform: translateY(-1px); }
.source .t { font-weight: 600; font-size: 14px; color: var(--accent); }
.source .m { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tag {
  display: inline-block;
  font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 999px;
  background: rgba(56, 189, 248, .15); color: var(--accent);
  margin-right: 6px;
}

/* ---- Composer ---- */
.composer {
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.composer form { display: flex; gap: 10px; }
.composer input {
  flex: 1;
  padding: 13px 16px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 15px;
  outline: none;
}
.composer input:focus { border-color: var(--accent); }
#send {
  width: 48px;
  border: none; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06121f; font-size: 18px; cursor: pointer;
  transition: opacity .15s;
}
#send:disabled { opacity: .5; cursor: wait; }
.hint { margin: 8px 4px 0; font-size: 11px; color: var(--muted); }

/* ---- Filtros y sugerencias ---- */
.filters { display: flex; gap: 14px; padding: 0 4px 8px; flex-wrap: wrap; align-items: center; }
.filter-chip {
  flex: 0 0 auto;
  white-space: nowrap;
  background: none;
  border: none;
  padding: 2px 0;
  color: rgba(226,232,240,.55);
  font-size: 12px;
  cursor: pointer;
  transition: color .15s;
}
.filter-chip:hover { color: var(--text); }
.filter-chip.active { color: #fff; font-weight: 600; }

.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.suggestion-chip {
  flex: 0 0 auto;
  white-space: nowrap;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}
.suggestion-chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---- Feedback ---- */
.msg-body { flex: 1; min-width: 0; }
.feedback { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.fb-btn {
  width: 28px; height: 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
  opacity: .6;
}
.fb-btn:hover { opacity: 1; border-color: var(--accent); }
.fb-btn.active { opacity: 1; background: rgba(56,189,248,.15); border-color: var(--accent); }
.fb-msg { font-size: 12px; color: var(--muted); }

/* ---- Menú ⋮ y modal de exportación ---- */
.conv-menu {
  position: fixed;
  z-index: 30;
  min-width: 190px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.conv-menu.hidden { display: none; }
.conv-menu button {
  padding: 9px 12px;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.conv-menu button:hover { background: var(--panel-2); }
.conv-menu button.danger { color: #f87171; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(2,6,23,.72);
  display: grid; place-items: center;
  z-index: 40;
  padding: 16px;
  backdrop-filter: blur(2px);
}
.modal-overlay.hidden { display: none; }
.modal {
  width: min(94%, 540px);
  max-height: 86vh;
  display: flex; flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
  overflow: hidden;
  animation: modalPop .18s ease-out;
}
@keyframes modalPop {
  from { transform: translateY(12px) scale(.97); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-title { display: flex; align-items: center; gap: 12px; }
.modal-icon {
  width: 42px; height: 42px; flex: 0 0 auto;
  display: grid; place-items: center;
  font-size: 20px;
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(56,189,248,.18), rgba(129,140,248,.18));
}
.modal-title h3 { margin: 0; font-size: 17px; }
.modal-sub { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.modal-close {
  width: 32px; height: 32px; flex: 0 0 auto;
  border: none; border-radius: 8px;
  background: transparent; color: var(--muted);
  font-size: 20px; line-height: 1; cursor: pointer;
}
.modal-close:hover { background: var(--panel-2); color: var(--text); }
.modal-count {
  padding: 12px 20px 0;
  font-size: 12px; color: var(--muted);
}
.modal-count b { color: var(--accent); font-weight: 700; }
.export-list {
  flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 20px;
}
.export-item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.export-item:hover { border-color: var(--accent); }
.export-item input[type=checkbox] { margin-top: 3px; width: 17px; height: 17px; accent-color: var(--accent); flex: 0 0 auto; }
.export-item .t { font-size: 14px; font-weight: 600; color: var(--text); line-height: 1.35; }
.export-item .m { font-size: 12px; color: var(--muted); margin-top: 3px; }
.modal-actions {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 14px 20px 8px;
  border-top: 1px solid var(--border);
  margin-top: 8px;
}
.modal-actions-right { display: flex; gap: 10px; }
.btn-ghost, .btn-primary {
  padding: 10px 16px; border-radius: 10px; cursor: pointer; font-size: 14px; border: 1px solid var(--border);
}
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--accent); }
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #06121f; border: none; font-weight: 700; }
.btn-primary:hover { opacity: .92; }
.btn-primary:disabled { opacity: .5; cursor: wait; }
.export-status { min-height: 1.2em; font-size: 13px; margin: 8px 20px 16px; color: #4ade80; }
.export-status.error { color: #f87171; }
.typing span {
  display: inline-block; width: 7px; height: 7px;
  margin-right: 3px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100% { opacity:.2 } 40% { opacity:1 } }

/* ---- Login ---- */
.login-overlay {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg-grad);
  z-index: 10;
}
.login-overlay.hidden { display: none; }
.login-card {
  width: min(92%, 380px);
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  display: flex; flex-direction: column; gap: 12px;
  text-align: center;
}
.logo.big {
  width: 56px; height: 56px; font-size: 26px;
  margin: 0 auto;
  border-radius: 16px;
  display: grid; place-items: center;
  font-weight: 800; color: #0b1220;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.login-card h2 { margin: 0; }
.login-card .sub { margin: -6px 0 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  padding: 12px 14px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text); font-size: 14px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  padding: 12px;
  border: none; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #06121f; font-weight: 700; font-size: 15px; cursor: pointer;
}
.error { color: #f87171; font-size: 13px; margin: 0; min-height: 1em; }
