:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --text: #1f1f1e;
  --muted: #6b6a66;
  --border: #e3e2de;
  --accent: #c96442;
  --accent-text: #ffffff;
  --user-bg: #efeee9;
  --code-bg: #f3f2ee;
  --error-bg: #fdecea;
  --error-text: #9b2c1f;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1b;
    --panel: #262624;
    --text: #ecebe6;
    --muted: #a3a29c;
    --border: #3a3937;
    --accent: #d97757;
    --user-bg: #33322f;
    --code-bg: #1f1f1d;
    --error-bg: #3b1f1a;
    --error-text: #f4a593;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; cursor: pointer; }

/* ---------- layout chat ---------- */
.app { display: flex; flex-direction: column; height: 100dvh; }
header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel);
}
header h1 { font-size: 16px; font-weight: 600; margin: 0; flex: 1; }
header .sub { color: var(--muted); font-weight: 400; font-size: 13px; margin-left: 6px; }
.ghost {
  background: none; border: 1px solid var(--border); color: var(--text);
  padding: 6px 12px; border-radius: 8px; font-size: 13px;
}
.ghost:hover { background: var(--user-bg); }

#messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; }
.thread { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }

.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end; background: var(--user-bg);
  padding: 10px 14px; border-radius: var(--radius) var(--radius) 4px var(--radius);
  max-width: 85%; white-space: pre-wrap;
}
.msg.assistant { align-self: stretch; }
.msg.error {
  background: var(--error-bg); color: var(--error-text);
  padding: 10px 14px; border-radius: var(--radius);
}
.msg .meta { color: var(--muted); font-size: 12px; margin-top: 4px; }

.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 .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, "JetBrains Mono", Menlo, Consolas, monospace;
  background: var(--code-bg); padding: 1px 5px; border-radius: 5px;
}
.md pre {
  position: relative; background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; overflow-x: auto;
}
.md pre code { background: none; padding: 0; display: block; }
.md pre code.hljs { background: none; padding: 0; }
.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 blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md a { color: var(--accent); }
.copy {
  position: absolute; top: 6px; right: 6px; font-size: 12px;
  background: var(--panel); border: 1px solid var(--border); color: var(--muted);
  border-radius: 6px; padding: 2px 8px; opacity: 0; transition: opacity .15s;
}
.md pre:hover .copy, .copy:focus { opacity: 1; }

.typing { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.dots { display: inline-flex; gap: 4px; }
.dots span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: blink 1.2s infinite ease-in-out;
}
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.empty { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty h2 { color: var(--text); font-weight: 600; font-size: 22px; margin-bottom: 6px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.suggestions button {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 7px 14px; font-size: 13px;
}
.suggestions button:hover { border-color: var(--accent); }

/* ---------- base de datos ---------- */
.db-badge {
  font-size: 12px; color: var(--muted); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px; white-space: nowrap;
}
.db-picker {
  max-width: 820px; margin: 0 auto 8px; display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap; font-size: 13px; color: var(--muted);
}
.db-picker select {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px;
}
.db-picker select.missing { border-color: var(--accent); }

/* ---------- composer ---------- */
.composer { padding: 8px 16px 16px; }
.composer form {
  max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 6px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 8px 8px 8px 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
}
.composer form:focus-within, .composer form.dragover { border-color: var(--accent); }
.composer .row { display: flex; align-items: flex-end; gap: 8px; }
.attach {
  width: 34px; height: 38px; flex: none; border: 0; background: none; color: var(--muted);
  display: grid; place-items: center; border-radius: 10px;
}
.attach:hover { color: var(--text); background: var(--user-bg); }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 4px; }
.thumb { position: relative; }
.thumb img {
  width: 64px; height: 64px; object-fit: cover; border-radius: 10px;
  border: 1px solid var(--border); display: block;
}
.thumb button {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  border: 0; background: var(--text); color: var(--bg); font-size: 12px; line-height: 20px; padding: 0;
}
.msg-images { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg-images img {
  max-width: 220px; max-height: 160px; border-radius: 10px; border: 1px solid var(--border);
  display: block; cursor: zoom-in;
}
textarea {
  flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; max-height: 220px; padding: 6px 0;
}
.send {
  width: 38px; height: 38px; flex: none; border-radius: 12px; border: 0;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send:disabled { opacity: .4; cursor: default; }
.hint { max-width: 820px; margin: 6px auto 0; color: var(--muted); font-size: 12px; text-align: center; }

/* ---------- login ---------- */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px 24px;
}
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login p { color: var(--muted); margin: 0 0 20px; font-size: 14px; }
.login input {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font: inherit; margin-bottom: 12px;
}
.login input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login button {
  width: 100%; padding: 10px; border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.login .err { color: var(--error-text); font-size: 13px; min-height: 1.2em; margin: 4px 0 0; }

@media (max-width: 600px) {
  header .sub { display: none; }
  #messages { padding: 16px 12px 8px; }
  .composer { padding: 6px 10px 10px; }
  .msg.user { max-width: 92%; }
  .hint { display: none; }
}
