:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #eef0f4;
  --border: #dcdfe6;
  --text: #1c2230;
  --muted: #667085;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --user-bubble: #e4edff;
  --code-bg: #f1f3f7;
  color-scheme: light dark;
}
/* Tema oscuro: por preferencia del sistema, salvo que el usuario haya elegido uno con el selector. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b;
    --panel: #1a1e26;
    --panel-2: #232833;
    --border: #2f3542;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --accent: #4c8dff;
    --accent-text: #0b1220;
    --danger: #ff7b72;
    --user-bubble: #22304d;
    --code-bg: #0f1218;
    color-scheme: dark;
  }
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] {
  --bg: #12151b;
  --panel: #1a1e26;
  --panel-2: #232833;
  --border: #2f3542;
  --text: #e6e9ef;
  --muted: #98a2b3;
  --accent: #4c8dff;
  --accent-text: #0b1220;
  --danger: #ff7b72;
  --user-bubble: #22304d;
  --code-bg: #0f1218;
  color-scheme: dark;
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }
:root[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
#app { height: 100%; }
.loading { text-align: center; padding-top: 30vh; color: var(--muted); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
a, .link { color: var(--accent); background: none; border: 0; padding: 0; text-decoration: none; cursor: pointer; }
.link:hover { text-decoration: underline; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
}
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); }

/* ---------- Acceso ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card .sub { margin: 0 0 20px; color: var(--muted); }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input {
  width: 100%; padding: 10px 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
.field input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field input.code { font-size: 24px; letter-spacing: 8px; text-align: center; }
.auth-card .btn.primary { width: 100%; padding: 10px; margin-top: 4px; }
.auth-links { display: flex; justify-content: space-between; gap: 12px; margin-top: 16px; font-size: 14px; flex-wrap: wrap; }
.error { color: var(--danger); margin: 0 0 12px; font-size: 14px; }
.notice { color: var(--muted); margin: 0 0 12px; font-size: 14px; }

/* ---------- Chat ---------- */
.layout { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar header { padding: 14px; border-bottom: 1px solid var(--border); }
.sidebar header strong { display: block; margin-bottom: 10px; }
.sidebar header .btn { width: 100%; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv {
  display: flex; align-items: center; gap: 4px;
  border-radius: 8px; padding: 2px 4px 2px 10px;
}
.conv:hover, .conv.active { background: var(--panel-2); }
.conv .title {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 7px 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv .icon { background: none; border: 0; color: var(--muted); padding: 4px 6px; border-radius: 6px; visibility: hidden; }
.conv:hover .icon, .conv.active .icon { visibility: visible; }
.conv .icon:hover { color: var(--danger); background: var(--panel); }
.sidebar footer {
  padding: 12px 14px; border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px;
}
.sidebar footer span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: none; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--panel); align-items: center; gap: 10px; }
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.messages-inner { max-width: 860px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.empty { text-align: center; color: var(--muted); margin-top: 14vh; }
.empty h2 { color: var(--text); margin-bottom: 6px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }

.msg.user { align-self: flex-end; max-width: 85%; background: var(--user-bubble); border-radius: 14px 14px 4px 14px; padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { align-self: stretch; min-width: 0; }
.msg-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.status { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.status::before {
  content: ""; width: 10px; height: 10px; border-radius: 50%;
  border: 2px solid var(--muted); border-top-color: transparent; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3 { line-height: 1.3; margin: 1.2em 0 0.5em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.05em; }
.md code { font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.md pre { position: relative; background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; }
.md pre code { background: none; padding: 0; white-space: pre; }
.md pre .copy { position: absolute; top: 6px; right: 6px; opacity: 0.85; }
.md blockquote { margin: 0.8em 0; padding: 2px 14px; border-left: 3px solid var(--border); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.md th { background: var(--panel-2); }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 12px 16px 14px; }
.composer-inner { max-width: 860px; margin: 0 auto; }
.composer textarea {
  width: 100%; resize: none; min-height: 52px; max-height: 240px;
  padding: 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg);
}
.composer-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.composer-row .hint { color: var(--muted); font-size: 12px; }
.branch { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.branch select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 7px 8px; max-width: 200px; }
.pending { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.thumb { position: relative; }
.thumb img { height: 64px; max-width: 120px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.thumb button { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 1px solid var(--border); background: var(--panel); font-size: 11px; line-height: 1; padding: 0; }
.shots { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.shots img { max-height: 160px; max-width: 240px; border-radius: 8px; border: 1px solid var(--border); display: block; }
.conv .title small { display: block; color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; }
.branch select:disabled { opacity: 0.75; cursor: not-allowed; }
.composer-row .right { flex-wrap: wrap; justify-content: flex-end; display: flex; gap: 8px; margin-left: auto; }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 20% 0 0; z-index: 10; transform: translateX(-100%); transition: transform 0.2s; }
  .layout.menu-open .sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.4); }
  .topbar { display: flex; }
  .msg.user { max-width: 95%; }
}

/* ---------- Navegación y mapa ---------- */
.nav { display: flex; gap: 6px; margin-bottom: 10px; }
.nav .btn { flex: 1; }
.nav .nav-on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.layout.map-open .main { display: none; }
.map { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.map[hidden] { display: none; }
.map-loading { text-align: center; color: var(--muted); margin-top: 20vh; padding: 0 16px; }
.map-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--panel); }
.map-search-wrap { position: relative; flex: 1 1 260px; max-width: 420px; }
.map-search { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.map-results { position: absolute; z-index: 5; top: calc(100% + 4px); left: 0; right: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18); max-height: 60vh; overflow: auto; }
.map-results button, .map-list button { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; background: none; border: 0; padding: 7px 10px; }
.map-results button:hover, .map-list button:hover { background: var(--panel-2); }
.map-results small { margin-left: auto; color: var(--muted); }
.map-results p { margin: 0; padding: 10px; color: var(--muted); }
.map-results i, .map-list i, .map-legend i { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.map-check { display: flex; align-items: center; gap: 5px; font-size: 13px; white-space: nowrap; }
.map-check[hidden] { display: none; }
.map-toolbar select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; }
.map-body { flex: 1; display: grid; grid-template-columns: 1fr 340px; min-height: 0; }
.map-stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
.map-canvas { position: absolute; inset: 0; }
.map-hint { position: absolute; left: 12px; top: 10px; right: 12px; font-size: 12.5px; color: var(--muted); pointer-events: none; }
.map-legend { position: absolute; left: 12px; bottom: 10px; display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); pointer-events: none; }
.map-legend span { display: flex; align-items: center; gap: 5px; }
.map-panel { border-left: 1px solid var(--border); background: var(--panel); padding: 16px; overflow-y: auto; font-size: 14px; }
.map-panel h2 { margin: 6px 0 2px; font-size: 18px; line-height: 1.3; }
.map-path { margin: 0 0 10px; color: var(--muted); font: 12px ui-monospace, monospace; overflow-wrap: anywhere; }
.map-muted { color: var(--muted); }
.map-badge { display: inline-block; color: #fff; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.map-actions, .map-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; }
.map-panel details { border-top: 1px solid var(--border); padding: 8px 0; }
.map-panel summary { cursor: pointer; font-weight: 600; }
.map-list { margin-top: 6px; }
.map-list button { padding: 4px 2px; font-size: 13.5px; }
.refs { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; font-size: 12.5px; color: var(--muted); }
.refs a { border: 1px solid var(--border); border-radius: 999px; padding: 2px 9px; background: var(--panel); }
.refs a:hover { background: var(--panel-2); text-decoration: none; }
@media (max-width: 900px) {
  .map-body { grid-template-columns: 1fr; grid-template-rows: 55% 45%; }
  .map-panel { border-left: 0; border-top: 1px solid var(--border); }
}

/* ---------- Marca y selector de tema ---------- */
/* El logotipo ocupa el ancho de la columna y «SECOND BRAIN» se reparte debajo, a ese mismo ancho. */
.brand { display: flex; flex-direction: column; align-items: stretch; gap: 8px; width: 100%; margin: 4px 0 18px; }
.brand img { width: 100%; height: auto; display: block; }
.brand .logo-dark { display: none; }
.brand span { display: flex; justify-content: space-between; padding: 0 2%; font-size: 12px; font-weight: 700; color: var(--muted); line-height: 1; }
.brand span i { font-style: normal; }
.brand span i.gap { width: 0.6em; }
.sidebar header { padding: 18px 20px 20px; }
.conv-list { padding-top: 14px; }
.auth-card .brand { width: 230px; margin: 0 auto 24px; }
.topbar .brand { width: 104px; margin: 0; gap: 4px; }
.topbar .brand span { font-size: 6.5px; }
.theme-toggle { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 3px 8px; font-size: 14px; line-height: 1.2; }
.theme-toggle:hover { background: var(--panel-2); }
.sidebar footer .who { flex: 1; min-width: 0; }

.lang-toggle { font-size: 12px; font-weight: 700; padding: 5px 8px; }
.auth-tools { display: flex; justify-content: flex-end; gap: 6px; margin: -10px -10px 6px 0; }

/* ---------- Accesos MCP ---------- */
.admin-page { overflow-y: auto; min-width: 0; height: 100%; }
.admin-page[hidden] { display: none; }
.admin-inner { max-width: 860px; margin: 0 auto; padding: 28px 20px 60px; }
.admin-inner h1 { margin: 0 0 8px; font-size: 22px; }
.admin-inner h2 { margin: 28px 0 10px; font-size: 16px; }
.mcp-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px; margin: 18px 0 8px; padding: 16px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; }
.mcp-form label > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.mcp-form input[type="text"], .mcp-form select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); min-width: 220px; font: inherit; color: var(--text); }
.mcp-new { margin: 14px 0; padding: 16px; border: 1px solid var(--accent); border-radius: 12px; background: var(--panel); }
.mcp-new h3 { margin: 0 0 6px; }
.mcp-new pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; font: 12.5px/1.5 ui-monospace, monospace; }
.mcp-list table { width: 100%; border-collapse: collapse; font-size: 14px; }
.mcp-list th, .mcp-list td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.mcp-list th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
.mcp-list tr.revoked td { color: var(--muted); text-decoration: line-through; }
.mcp-list tr.revoked td:last-child { text-decoration: none; }
.mcp-new h4 { margin: 14px 0 6px; font-size: 13.5px; }
