:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #15181d; --muted: #667085; --line: #e1e4ea;
  --accent: #2563eb; --accent-text: #ffffff; --user: #e8efff; --err: #b42318; --code: #eef0f4; --side: #eef0f4;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #171a21; --text: #e8eaee; --muted: #98a2b3; --line: #272c36;
    --accent: #5b8cff; --accent-text: #0b1020; --user: #1c2a4a; --err: #f97066; --code: #0c0e13; --side: #13161c;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--err); min-height: 1.5em; margin: 0; }

.card { max-width: 380px; margin: 12vh auto 0; padding: 24px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.card h1 { margin: 0 0 4px; }
label { display: block; margin: 12px 0; font-size: 14px; color: var(--muted); }
input, textarea { display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button { padding: 10px 16px; font: inherit; color: var(--accent-text); background: var(--accent); border: 0; border-radius: 10px; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
button.ghost { color: var(--text); background: transparent; border: 1px solid var(--line); padding: 6px 12px; font-size: 14px; }
button.small { padding: 6px 12px; font-size: 14px; }
button.icon { padding: 4px 10px; font-size: 18px; line-height: 1; }

/* Estructura */
#app { display: grid; grid-template-columns: 270px 1fr; height: 100dvh; }
#sidebar { display: flex; flex-direction: column; background: var(--side); border-right: 1px solid var(--line); min-height: 0; }
.side-head, .side-foot { display: flex; align-items: center; gap: 8px; padding: 14px; }
.side-head strong { flex: 1; font-size: 18px; }
.side-foot { border-top: 1px solid var(--line); justify-content: space-between; }
#conv-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.conv { display: flex; align-items: center; width: 100%; gap: 6px; margin: 2px 0; padding: 8px 10px; text-align: left; color: var(--text); background: transparent; border-radius: 8px; font-size: 14px; }
.conv:hover { background: var(--line); }
.conv.active { background: var(--user); }
.conv .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .del { padding: 0 6px; color: var(--muted); background: transparent; font-size: 14px; border-radius: 6px; }
.conv .del:hover { color: var(--err); }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
#scrim { display: none; }

#main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#main header { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#menu { display: none; }
#title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#log { flex: 1; overflow-y: auto; padding: 18px 16px 8px; }
#log > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { margin-top: 18vh; text-align: center; color: var(--muted); }
.empty strong { display: block; margin-bottom: 6px; font-size: 22px; color: var(--text); }

.msg { margin-bottom: 14px; padding: 10px 14px; overflow-wrap: anywhere; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.msg.user { max-width: 85%; margin-left: auto; background: var(--user); white-space: pre-wrap; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg p { margin: 0 0 .75em; white-space: pre-wrap; }
.msg h3, .msg h4, .msg h5, .msg h6 { margin: 1em 0 .4em; line-height: 1.3; }
.msg ul, .msg ol { margin: 0 0 .75em; padding-left: 1.4em; }
.msg blockquote { margin: 0 0 .75em; padding: 2px 12px; color: var(--muted); border-left: 3px solid var(--line); }
.msg a { color: var(--accent); }
.msg code { padding: 1px 5px; font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--code); border-radius: 5px; }
.msg pre { margin: 0 0 .75em; padding: 12px; overflow-x: auto; background: var(--code); border: 1px solid var(--line); border-radius: 10px; }
.msg pre code { padding: 0; background: none; font-size: 13px; white-space: pre; }
.tool { margin: 0 auto 8px; font-size: 13px; color: var(--muted); font-family: ui-monospace, monospace; overflow-wrap: anywhere; }
.tool.bad { color: var(--err); }

#chat-form { display: flex; gap: 10px; align-items: flex-end; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
#chat-form textarea { resize: none; margin: 0; max-height: 160px; }

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; z-index: 20; inset: 0 auto 0 0; width: min(300px, 86vw); transform: translateX(-100%); transition: transform .2s; }
  body.menu-open #sidebar { transform: none; }
  body.menu-open #scrim { display: block; position: fixed; z-index: 10; inset: 0; background: rgba(0, 0, 0, .45); }
  #menu { display: inline-block; }
  .msg.user { max-width: 92%; }
  .card { margin-top: 6vh; }
}

/* Fila de conversación: un div con dos botones (abrir y quitar) */
.conv .open { flex: 1; min-width: 0; padding: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; color: var(--text); background: transparent; font-size: 14px; }

/* Ventanilla de publicación */
#pub-btn { margin-left: auto; }
#pub-panel { padding: 12px 16px; border-bottom: 1px solid var(--line); max-height: 45vh; overflow-y: auto; }
.pub-card { margin: 10px 0; padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.pub-card pre { margin: 8px 0; padding: 8px; max-height: 120px; overflow: auto; white-space: pre-wrap; background: var(--code); border-radius: 8px; font-size: 13px; }
.pub-card button { margin-top: 8px; }
