:root {
  --bg: #ffffff;
  --bg-sidebar: #f7f7f8;
  --bg-msg-user: #f7f7f8;
  --bg-msg-assistant: #ffffff;
  --border: #e5e5e5;
  --text: #1a1a1a;
  --text-muted: #6b6b6b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --bg-sidebar: #111;
    --bg-msg-user: #262626;
    --bg-msg-assistant: #1a1a1a;
    --border: #333;
    --text: #e5e5e5;
    --text-muted: #999;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  font-size: 14px; line-height: 1.5;
}

#app { display: flex; height: 100vh; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 290px; min-width: 290px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.sidebar-header { padding: 12px; }
.sidebar-toolbar { padding: 0 12px 8px; display: flex; gap: 4px; }
.sidebar-footer {
  padding: 8px 12px; border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-muted);
}

.btn {
  border: 1px solid transparent; background: transparent;
  color: inherit; padding: 6px 10px; border-radius: 6px;
  cursor: pointer; font-size: 13px; font-family: inherit;
  transition: background 0.15s;
}
.btn:hover { background: rgba(0,0,0,0.06); }
@media (prefers-color-scheme: dark) {
  .btn:hover { background: rgba(255,255,255,0.08); }
}
.btn-primary {
  width: 100%; background: var(--accent); color: white;
  padding: 10px; font-weight: 500;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { color: var(--text-muted); }

.tree-container { flex: 1; overflow-y: auto; padding: 0 8px 8px; }

.tree-item {
  padding: 5px 8px; border-radius: 6px; cursor: pointer;
  user-select: none; display: flex; align-items: center;
  gap: 6px; font-size: 13px; position: relative;
}
.tree-item:hover { background: rgba(0,0,0,0.06); }
.tree-item.active { background: rgba(37,99,235,0.12); color: var(--accent); }
@media (prefers-color-scheme: dark) {
  .tree-item:hover { background: rgba(255,255,255,0.08); }
}
.tree-folder > .tree-item { font-weight: 500; }
.tree-children { padding-left: 16px; }

.tree-toggle {
  width: 16px; text-align: center; font-size: 10px;
  color: var(--text-muted); transition: transform 0.15s; flex-shrink: 0;
}
.tree-toggle.expanded { transform: rotate(90deg); }

.tree-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.color-dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
}
.tree-actions {
  display: flex; gap: 2px; opacity: 0; transition: opacity 0.15s;
}
.tree-item:hover .tree-actions { opacity: 1; }
.tree-actions button {
  background: none; border: none; cursor: pointer;
  padding: 2px 5px; color: var(--text-muted);
  font-size: 13px; border-radius: 3px;
}
.tree-actions button:hover { background: rgba(0,0,0,0.1); color: var(--text); }

.pinned-section { margin-bottom: 8px; }
.pinned-header {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 8px; font-size: 12px; font-weight: 600;
  color: var(--text-muted); cursor: pointer;
}

/* ---------- Main area ---------- */
#main { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }
.welcome {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center; padding: 40px; color: var(--text-muted);
}
.welcome h1 { color: var(--text); margin-bottom: 8px; }
.welcome .hint { font-size: 12px; margin-top: 16px; }

#conversation-view { display: flex; flex-direction: column; height: 100%; }

#conv-header {
  padding: 16px 24px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg); z-index: 10;
}
.conv-header-top { display: flex; align-items: center; gap: 12px; }
#conv-title {
  margin: 0; font-size: 18px; flex: 1;
  overflow: hidden; text-overflow: ellipsis;
}
.conv-header-actions { display: flex; gap: 4px; align-items: center; }
#conv-color {
  width: 28px; height: 28px; border: 1px solid var(--border);
  border-radius: 6px; cursor: pointer; padding: 0; background: none;
}
.conv-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.messages {
  flex: 1; padding: 16px 24px 40px;
  max-width: 900px; margin: 0 auto; width: 100%;
}

.message {
  margin-bottom: 16px; padding: 12px 16px; border-radius: 8px;
  position: relative; border: 1px solid var(--border);
}
.message.user { background: var(--bg-msg-user); }
.message.assistant { background: var(--bg-msg-assistant); }
.message.system { opacity: 0.7; font-style: italic; }

.msg-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin-bottom: 8px;
  font-size: 12px; color: var(--text-muted);
}
.msg-role { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.msg-role .role-icon { font-size: 14px; }
.msg-time { font-size: 11px; }

.msg-body { line-height: 1.6; word-wrap: break-word; overflow-wrap: break-word; }
.msg-body pre {
  background: rgba(0,0,0,0.05); padding: 8px 12px;
  border-radius: 6px; overflow-x: auto; font-size: 12px;
}
@media (prefers-color-scheme: dark) {
  .msg-body pre { background: rgba(255,255,255,0.08); }
}
.msg-body code {
  background: rgba(0,0,0,0.06); padding: 1px 4px;
  border-radius: 3px; font-size: 0.9em;
}
@media (prefers-color-scheme: dark) {
  .msg-body code { background: rgba(255,255,255,0.1); }
}

.msg-actions {
  position: absolute; top: 8px; right: 8px;
  display: flex; gap: 2px; opacity: 0; transition: opacity 0.15s;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px;
}
.message:hover .msg-actions { opacity: 1; }
.message.editing .msg-actions { display: none; }
.msg-actions button {
  background: none; border: none; cursor: pointer;
  padding: 3px 6px; color: var(--text-muted);
  font-size: 12px; border-radius: 3px;
}
.msg-actions button:hover { background: rgba(0,0,0,0.1); color: var(--text); }

.msg-edit-area {
  width: 100%; min-height: 100px; padding: 8px;
  border: 1px solid var(--border); border-radius: 6px;
  font-family: inherit; font-size: inherit;
  resize: vertical; background: var(--bg); color: var(--text);
}
.msg-edit-buttons { display: flex; gap: 6px; margin-top: 8px; }
.msg-edit-buttons .btn-primary { width: auto; padding: 6px 14px; }

/* ---------- Modales ---------- */
.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 {
  background: var(--bg); padding: 20px; border-radius: 10px;
  min-width: 320px; box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.modal h3 { margin: 0 0 12px; }
.modal input {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text);
  font-family: inherit; font-size: 14px; margin-bottom: 12px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.modal-actions .btn-primary { width: auto; padding: 6px 16px; }

/* ---------- Razonamiento interno (DeepSeek) ---------- */
.msg-thinking {
  margin-top: 12px;
  padding: 8px 12px;
  background: rgba(0,0,0,0.03);
  border-left: 3px solid var(--text-muted);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-muted);
}
@media (prefers-color-scheme: dark) {
  .msg-thinking { background: rgba(255,255,255,0.04); }
}
.msg-thinking summary {
  cursor: pointer;
  user-select: none;
  font-weight: 500;
  list-style: none;
}
.msg-thinking summary::-webkit-details-marker { display: none; }
.msg-thinking summary::before {
  content: '▸ ';
  display: inline-block;
  transition: transform 0.15s;
}
.msg-thinking[open] summary::before { content: '▾ '; }
.thinking-content {
  margin-top: 8px;
  white-space: pre-wrap;
  font-size: 12px;
  line-height: 1.5;
}

/* ---------- Chips de herramientas ---------- */
.msg-tools {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.tool-chip {
  display: inline-block;
  padding: 2px 8px;
  font-size: 11px;
  background: rgba(37,99,235,0.1);
  color: var(--accent);
  border-radius: 4px;
  border: 1px solid rgba(37,99,235,0.2);
}
