*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #ffffff;
  --bg-subtle: #f7f7f8;
  --bg-sidebar: #f0f0f1;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --text-sidebar: #374151;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --user-bg: #6366f1;
  --user-text: #ffffff;
  --asst-bg: #f3f4f6;
  --asst-text: #111827;
  --status-ok: #10b981;
  --status-warn: #f59e0b;
  --status-err: #ef4444;
  --status-unknown: #9ca3af;
  --sidebar-w: 240px;
  --header-h: 52px;
  --composer-h: 72px;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f10;
    --bg-subtle: #1a1a1b;
    --bg-sidebar: #161617;
    --border: #2d2d2f;
    --text: #f3f4f6;
    --text-muted: #9ca3af;
    --text-sidebar: #d1d5db;
    --accent: #818cf8;
    --accent-hover: #6366f1;
    --user-bg: #6366f1;
    --user-text: #ffffff;
    --asst-bg: #1f1f21;
    --asst-text: #e5e7eb;
  }
}

html, body { height: 100%; font-family: system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }

#app-shell { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.2s, min-width 0.2s;
}
#sidebar.collapsed { width: 0; min-width: 0; }

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--border);
}
.sidebar-logo { display: flex; align-items: center; color: var(--text-muted); text-decoration: none; }
.sidebar-logo:hover { color: var(--accent); }
.sidebar-title { flex: 1; font-weight: 600; font-size: 14px; color: var(--text-sidebar); }

.sidebar-section-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.conv-list, .memory-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 4px 8px;
}
.conv-list { max-height: 45vh; }
.memory-list { max-height: 20vh; }

.conv-item, .memory-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-sidebar);
  font-size: 13px;
  line-height: 1.3;
}
.conv-item:hover, .memory-item:hover { background: var(--border); }
.conv-item.active { background: var(--accent); color: #fff; }
.conv-item .conv-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .btn-delete { opacity: 0; font-size: 12px; background: none; border: none; cursor: pointer; padding: 2px; color: inherit; }
.conv-item:hover .btn-delete { opacity: 0.7; }
.memory-item .mem-icon { font-size: 14px; flex-shrink: 0; }
.memory-item .mem-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.memory-item .btn-forget { opacity: 0; font-size: 11px; background: none; border: none; cursor: pointer; color: inherit; }
.memory-item:hover .btn-forget { opacity: 0.7; }

/* Main panel */
#main-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--bg); }

#main-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
#header-left { display: flex; align-items: center; gap: 10px; overflow: hidden; }
#header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
#conv-title { font-weight: 500; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }

.status-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 99px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.status-ok   { background: color-mix(in srgb, var(--status-ok) 15%, transparent); color: var(--status-ok); }
.status-warn { background: color-mix(in srgb, var(--status-warn) 15%, transparent); color: var(--status-warn); }
.status-err  { background: color-mix(in srgb, var(--status-err) 15%, transparent); color: var(--status-err); }
.status-unknown { background: var(--bg-subtle); color: var(--text-muted); }

/* Chat area */
#chat-area { flex: 1; overflow-y: auto; padding: 24px 0; }
#messages { max-width: 760px; margin: 0 auto; padding: 0 20px; display: flex; flex-direction: column; gap: 20px; }

.msg { display: flex; flex-direction: column; gap: 4px; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }
.msg-bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .msg-bubble { background: var(--user-bg); color: var(--user-text); border-bottom-right-radius: 2px; }
.msg.assistant .msg-bubble { background: var(--asst-bg); color: var(--asst-text); border-bottom-left-radius: 2px; }

.msg-sources { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.source-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  color: var(--text-muted);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.source-chip img { width: 24px; height: 24px; object-fit: cover; border-radius: 3px; flex-shrink: 0; }

.msg-typing .msg-bubble::after { content: "…"; animation: blink 1s step-start infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Empty state */
#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  color: var(--text-muted);
  gap: 12px;
  padding: 40px 20px;
}
#empty-state svg { opacity: 0.3; }
#empty-state h2 { font-size: 18px; font-weight: 500; color: var(--text); }
#empty-state p { font-size: 14px; max-width: 380px; line-height: 1.5; }

/* Composer */
#composer-wrap { border-top: 1px solid var(--border); background: var(--bg); }
#composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
}
#attach-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px 0;
  font-size: 12px;
  color: var(--text-muted);
}
#attach-preview img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
#attach-preview span#attach-preview-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#question-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg-subtle);
  color: var(--text);
  min-height: 40px;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.5;
}
#question-input:focus { outline: none; border-color: var(--accent); }
#question-input::placeholder { color: var(--text-muted); }

/* Buttons */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ghost { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; cursor: pointer; color: var(--text); font-size: 13px; }
.btn-ghost:hover { background: var(--bg-subtle); }
.btn-text { background: none; border: none; cursor: pointer; color: var(--accent); font-size: 13px; text-decoration: none; }
.btn-icon { background: none; border: none; cursor: pointer; color: var(--text-muted); padding: 4px; border-radius: 4px; display: flex; align-items: center; justify-content: center; }
.btn-icon:hover { background: var(--border); color: var(--text); }
.btn-icon-sm { background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 14px; font-weight: 600; padding: 0 4px; }
.btn-icon-sm:hover { color: var(--accent); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.modal-overlay[hidden] { display: none; }
.modal-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  width: min(420px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-muted); }
.modal-card label input, .modal-card label select, .modal-card label textarea {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  color: var(--text);
  font-family: inherit;
  width: 100%;
}
.modal-card label textarea { resize: vertical; }
.modal-card label input:focus, .modal-card label select:focus, .modal-card label textarea:focus { outline: none; border-color: var(--accent); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Visor de imagen ampliada */
.img-viewer-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  width: min(90vw, 900px);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
}
.img-viewer-close { position: absolute; top: 10px; right: 10px; background: var(--bg-subtle); }
#img-viewer-img { max-width: 100%; max-height: 72vh; object-fit: contain; border-radius: 8px; margin: 0 auto; }
.img-viewer-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 13px; color: var(--text-muted); }
#img-viewer-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Miniaturas del chat: clicables para ampliar */
.source-chip { cursor: pointer; }
.source-chip:hover { border-color: var(--accent); }

/* Memory type icons */
.mem-icon-fact::before { content: "💡"; }
.mem-icon-rule::before { content: "📋"; }
.mem-icon-entity_doc::before { content: "📂"; }
.mem-icon-entity_face::before { content: "👤"; }
.mem-icon-entity_object::before { content: "🔖"; }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Reparto de la GPU: tarjeta para elegir cuando el asistente y el captioning
   de imágenes se pelean por la tarjeta gráfica (ver gpu_broker_service). */
.gpu-choice .msg-bubble {
  border: 1px solid var(--status-warn);
  background: var(--bg-subtle);
}
.gpu-choice p { margin: 0 0 8px; }
.gpu-choice-actions { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.gpu-opt {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 12px;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}
.gpu-opt:hover:not(:disabled) { border-color: var(--accent); }
.gpu-opt:disabled { opacity: 0.5; cursor: default; }
/* La primera es la que casi siempre conviene: pausar cuesta ~40 s frente a los
   ~700 s de responder sin GPU. Se destaca sin esconder las otras dos. */
.gpu-opt:first-child { border-color: var(--accent); }
.gpu-opt-title { font-weight: 600; font-size: 14px; }
.gpu-opt-cost { font-size: 12px; color: var(--text-muted); }
.gpu-waiting { font-size: 13px; color: var(--text-muted); font-style: italic; }

/* Aviso de "hay un trabajo pausado". Discreto pero permanente: si no se toca,
   el trabajo se reanuda solo a los 5 min de silencio, así que esto es un atajo,
   no una alarma. */
#gpu-paused {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--status-warn);
  border-radius: var(--radius);
  background: var(--bg-subtle);
  font-size: 13px;
  color: var(--text-muted);
}
#gpu-paused[hidden] { display: none; }
#gpu-paused-text { flex: 1; min-width: 0; }
#btn-gpu-resume { flex-shrink: 0; font-weight: 600; color: var(--accent); }
#btn-gpu-resume:disabled { opacity: 0.5; cursor: default; }
