:root {
  --bg: #f7f7fb;
  --panel: #ffffff;
  --sidebar: #f0f0f7;
  --text: #1b1b25;
  --muted: #6b6b80;
  --border: #e3e3ee;
  --accent: #6d5efc;
  --accent-soft: #ecebff;
  --user-bubble: #6d5efc;
  --user-text: #ffffff;
  --code-bg: #0f1117;
  --danger: #e5484d;
  --ok: #30a46c;
  --shadow: 0 8px 30px rgba(20, 20, 50, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e0e14;
    --panel: #16161f;
    --sidebar: #12121a;
    --text: #ececf3;
    --muted: #9696aa;
    --border: #262634;
    --accent: #8b7fff;
    --accent-soft: #23213d;
    --user-bubble: #5b4ee8;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  --bg: #0e0e14;
  --panel: #16161f;
  --sidebar: #12121a;
  --text: #ececf3;
  --muted: #9696aa;
  --border: #262634;
  --accent: #8b7fff;
  --accent-soft: #23213d;
  --user-bubble: #5b4ee8;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

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

button { font: inherit; cursor: pointer; }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- Sidebar ---------- */

.sidebar {
  width: 270px;
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 12px;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 8px; }
.brand-name { font-weight: 700; font-size: 17px; }
.brand-sub { font-size: 12px; color: var(--muted); }

.logo {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #b26bff);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}
.logo.big { width: 56px; height: 56px; font-size: 26px; border-radius: 16px; margin: 0 auto 14px; }

.btn-new {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  transition: filter .15s;
}
.btn-new:hover { filter: brightness(1.08); }
.btn-new span { font-size: 18px; line-height: 1; }

.history-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 6px 8px 0; }

.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }

.history-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}
.history-item:hover { background: var(--accent-soft); }
.history-item.active { background: var(--accent-soft); font-weight: 600; }
.history-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-item .del {
  opacity: 0; border: none; background: none; color: var(--muted);
  padding: 2px 6px; border-radius: 6px;
}
.history-item:hover .del { opacity: 1; }
.history-item .del:hover { color: var(--danger); }

.sidebar-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-top: 8px; border-top: 1px solid var(--border); }

.btn-ghost { border: 1px solid var(--border); background: transparent; color: var(--text); padding: 6px 10px; border-radius: 8px; font-size: 13px; }
.btn-ghost:hover { background: var(--accent-soft); }

.status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.ok .dot { background: var(--ok); }
.status.err .dot { background: var(--danger); }

/* ---------- Main ---------- */

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.btn-icon { border: none; background: none; color: var(--text); font-size: 20px; padding: 4px 8px; border-radius: 8px; }
.menu { display: none; }

.modes { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.modes::-webkit-scrollbar { display: none; }

.mode {
  display: flex; align-items: center; gap: 6px;
  white-space: nowrap;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  transition: all .15s;
}
.mode:hover { border-color: var(--accent); }
.mode.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

/* ---------- Chat ---------- */

.chat { flex: 1; overflow-y: auto; padding: 24px 20px; }

.welcome { max-width: 720px; margin: 8vh auto 0; text-align: center; }
.welcome h1 { margin: 0 0 8px; font-size: 28px; }
.welcome p { margin: 0 0 28px; color: var(--muted); }

.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }

.suggestion {
  text-align: left;
  padding: 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
  transition: all .15s;
}
.suggestion:hover { border-color: var(--accent); box-shadow: var(--shadow); transform: translateY(-1px); }

.messages { max-width: 800px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }

.msg { display: flex; gap: 12px; }
.msg.user { justify-content: flex-end; }

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  padding: 11px 16px;
  border-radius: 16px 16px 4px 16px;
  max-width: 80%;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.55;
}

.msg.assistant .logo { width: 30px; height: 30px; font-size: 14px; border-radius: 9px; margin-top: 2px; }
.msg.assistant .body { flex: 1; min-width: 0; }

.content { line-height: 1.65; word-wrap: break-word; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content h1, .content h2, .content h3 { margin: 1.2em 0 .5em; line-height: 1.3; }
.content h1 { font-size: 1.35em; }
.content h2 { font-size: 1.2em; }
.content h3 { font-size: 1.05em; }
.content p { margin: .6em 0; }
.content ul, .content ol { padding-left: 1.4em; margin: .5em 0; }
.content li { margin: .25em 0; }
.content a { color: var(--accent); }
.content blockquote { margin: .8em 0; padding: .2em 1em; border-left: 3px solid var(--accent); color: var(--muted); }
.content table { border-collapse: collapse; width: 100%; margin: .8em 0; font-size: 14px; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.content th { background: var(--accent-soft); }
.content :not(pre) > code {
  font-family: 'JetBrains Mono', monospace;
  font-size: .88em;
  background: var(--accent-soft);
  padding: .15em .4em;
  border-radius: 5px;
}

.code-block { position: relative; margin: .8em 0; border-radius: 10px; overflow: hidden; background: var(--code-bg); }
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px;
  background: #1a1d27;
  color: #9aa0b4;
  font-size: 12px;
  font-family: 'JetBrains Mono', monospace;
}
.code-copy { border: none; background: none; color: #9aa0b4; font-size: 12px; padding: 2px 6px; border-radius: 5px; }
.code-copy:hover { color: #fff; background: rgba(255,255,255,.08); }
.code-block pre { margin: 0; padding: 14px; overflow-x: auto; }
.code-block code { font-family: 'JetBrains Mono', monospace; font-size: 13px; line-height: 1.55; background: none !important; padding: 0 !important; }

.thinking {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  color: var(--muted);
}
.thinking summary { cursor: pointer; padding: 8px 12px; user-select: none; }
.thinking .thinking-text { padding: 0 12px 10px; white-space: pre-wrap; line-height: 1.5; max-height: 260px; overflow-y: auto; }

.meta { display: flex; align-items: center; gap: 10px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.meta button { border: none; background: none; color: var(--muted); font-size: 12px; padding: 3px 6px; border-radius: 6px; }
.meta button:hover { background: var(--accent-soft); color: var(--text); }

.notice { font-size: 14px; color: var(--muted); font-style: italic; }
.error-text { color: var(--danger); font-size: 14px; }

.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- Composer ---------- */

.composer-wrap { padding: 12px 20px 16px; background: var(--bg); }

.composer {
  max-width: 800px;
  margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }

.composer textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  padding: 8px 0;
  max-height: 220px;
}

.btn-send, .btn-stop {
  width: 40px; height: 40px;
  border: none;
  border-radius: 12px;
  color: #fff;
  flex-shrink: 0;
  font-size: 15px;
}
.btn-send { background: var(--accent); }
.btn-send:disabled { opacity: .4; cursor: default; }
.btn-stop { background: var(--text); color: var(--panel); }

.hint { text-align: center; font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ---------- Usuario ---------- */

.user-card {
  display: flex; align-items: center; gap: 10px;
  padding: 10px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
}
.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid; place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-usage { font-size: 12px; color: var(--muted); }
.btn-icon.small { font-size: 18px; color: var(--muted); }
.btn-icon.small:hover { color: var(--danger); background: var(--accent-soft); }

.history-empty { font-size: 13px; color: var(--muted); padding: 8px 10px; }

/* ---------- Pantalla de acceso ---------- */

.auth {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background:
    radial-gradient(circle at 20% 10%, var(--accent-soft), transparent 45%),
    radial-gradient(circle at 85% 90%, var(--accent-soft), transparent 40%),
    var(--bg);
  overflow-y: auto;
}
.auth[hidden] { display: none; }

.auth-card {
  width: min(400px, 100%);
  padding: 32px 28px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow);
  text-align: center;
}
.auth-card h1 { margin: 0; font-size: 26px; }
.auth-sub { margin: 4px 0 22px; color: var(--muted); font-size: 14px; }

.auth-tabs {
  display: grid; grid-template-columns: 1fr 1fr;
  padding: 4px;
  margin-bottom: 20px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.auth-tab {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 500;
}
.auth-tab.active { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 4px rgba(0,0,0,.08); }

.auth-form { display: flex; flex-direction: column; gap: 14px; text-align: left; }
.auth-form[hidden] { display: none; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
.label-hint { font-weight: 400; color: var(--muted); }
.auth-form input {
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-weight: 400;
}
.auth-form input:focus { outline: none; border-color: var(--accent); }
.auth-form .btn-new:disabled { opacity: .6; cursor: default; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; }
.auth-closed { color: var(--muted); font-size: 14px; margin: 0; }

.backdrop { display: none; }

/* ---------- Móvil ---------- */

@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .backdrop.show { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.4); }
  .menu { display: block; }
  .topbar { padding: 10px 16px; }
  .chat { padding: 18px 16px; }
  .composer-wrap { padding: 10px 16px 14px; }
  .msg.user .bubble { max-width: 90%; }
  .msg.assistant .logo { display: none; }
  .welcome { margin-top: 4vh; }
  .welcome h1 { font-size: 24px; }
}
