:root {
  color-scheme: light dark;
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #efefec;
  --side: #efefeb;
  --text: #1c1d1f;
  --muted: #5b5e66;
  --border: #d4d4ce;
  --accent: #0f7a55;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --on-danger: #ffffff;
  --danger-bg: #fdecea;
  --code-bg: #f0f0ed;
  --bubble: #e2f0e9;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141518;
    --surface: #1c1e22;
    --surface-2: #26282d;
    --side: #1a1b1f;
    --text: #ececee;
    --muted: #a3a6ae;
    --border: #3a3d44;
    --accent: #7dd3a8;
    --on-accent: #0c2a1c;
    --danger: #ffb4ab;
    --on-danger: #3a0d09;
    --danger-bg: #3a1d1a;
    --code-bg: #101114;
    --bubble: #25332c;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Cada zona desplaza su propio contenido: la pagina en si nunca se mueve. */
html, body { height: 100%; margin: 0; overflow: hidden; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

input, select, textarea {
  width: 100%;
  min-height: 38px;
  padding: 7px 10px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}

textarea { resize: vertical; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: 13px; font-weight: 600; }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
}

.check input { width: 16px; min-height: 16px; margin: 0; accent-color: var(--accent); }

/* Botones */

button {
  min-height: 38px;
  padding: 0 12px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

button:disabled { opacity: 0.55; cursor: default; }

.btn-quiet { background: var(--surface-2); border-color: var(--border); }
.btn-quiet:hover:not(:disabled) { border-color: var(--muted); }

.btn-primary { color: var(--on-accent); background: var(--accent); padding: 0 18px; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }

.btn-danger { color: var(--on-danger); background: var(--danger); padding: 0 18px; }
.btn-stop { background: var(--surface-2); border-color: var(--muted); padding: 0 18px; }
.btn-wide { width: 100%; }

.btn-link {
  min-height: 0;
  padding: 0;
  color: var(--accent);
  text-decoration: underline;
}

.only-narrow { display: none; }

.link-button {
  display: inline-flex;
  flex: none;
  align-items: center;
  min-height: 34px;
  padding: 0 11px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}

.link-button:hover { border-color: var(--muted); }

/* Acceso */

.auth {
  display: flex;
  align-items: safe center;
  justify-content: center;
  height: 100dvh;
  padding: 20px;
  overflow-y: auto;
}

.auth-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(380px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.brand-title { margin: 0; font-size: 24px; font-weight: 650; }
.auth-intro { margin: -8px 0 4px; color: var(--muted); }
.auth-switch { margin: 0; color: var(--muted); font-size: 13px; text-align: center; }

.form-error { min-height: 0; margin: 0; color: var(--danger); font-size: 13px; overflow-wrap: anywhere; }
.form-error:empty { display: none; }
.form-error.ok { color: var(--accent); }

/* Estructura */

.app {
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr);
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 12px;
  background: var(--side);
  border-right: 1px solid var(--border);
}

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 38px;
  padding: 0 4px;
  margin-bottom: 8px;
}

.brand { font-size: 16px; font-weight: 650; }
.btn-new { margin-bottom: 10px; }

.chat-list {
  flex: 1;
  min-height: 0;
  margin: 0 -4px;
  padding: 0 4px;
  overflow-y: auto;
}

.chat-list-empty { margin: 8px 6px; color: var(--muted); font-size: 13px; }

.chat-item {
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--surface-2); }

.chat-open {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 0 8px;
  overflow: hidden;
  font-weight: 450;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-delete {
  flex: none;
  width: 32px;
  min-height: 32px;
  padding: 0;
  color: var(--muted);
  font-size: 16px;
  font-weight: 400;
  opacity: 0;
}

.chat-item:hover .chat-delete, .chat-item.active .chat-delete, .chat-delete:focus-visible { opacity: 1; }
.chat-delete:hover { color: var(--danger); }

@media (hover: none) { .chat-delete { opacity: 1; } }

.side-foot {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-top: 8px;
  margin-top: 8px;
  border-top: 1px solid var(--border);
}

.side-link {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  min-height: 38px;
  padding: 8px;
  font-weight: 500;
  text-align: left;
}

.side-link:hover { background: var(--surface-2); }
.side-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.usage-line { display: flex; align-items: center; gap: 8px; }
.usage-text { flex: none; color: var(--muted); font-size: 12px; font-weight: 400; }

progress {
  flex: 1;
  min-width: 0;
  height: 5px;
  overflow: hidden;
  appearance: none;
  background: var(--border);
  border: 0;
  border-radius: 3px;
}

progress::-webkit-progress-bar { background: var(--border); }
progress::-webkit-progress-value { background: var(--accent); }
progress::-moz-progress-bar { background: var(--accent); }

.backdrop { display: none; }

/* Conversacion */

.chat { display: flex; flex-direction: column; min-width: 0; height: 100dvh; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--border);
}

.model-select {
  flex: 0 1 auto;
  width: auto;
  min-width: 0;
  max-width: 340px;
  background: var(--surface);
  font-size: 14px;
  font-weight: 600;
}

.agent-toggle { flex: none; margin-left: auto; color: var(--text); white-space: nowrap; }
.agent-toggle:has(input:disabled) { color: var(--muted); cursor: default; }

.log {
  flex: 1;
  overflow-y: auto;
  padding: 24px max(20px, calc((100% - 780px) / 2));
  outline-offset: -2px;
}

.empty { max-width: 500px; margin: 16vh auto 0; text-align: center; }
.empty h2 { margin: 0 0 8px; font-size: 26px; font-weight: 650; }
.empty p { margin: 0; color: var(--muted); }

.msg { margin-bottom: 22px; }
.msg-user { display: flex; justify-content: flex-end; }

.bubble {
  max-width: 85%;
  padding: 9px 14px;
  background: var(--bubble);
  border-radius: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.thinking { color: var(--muted); font-size: 14px; animation: pulse 1.4s ease-in-out infinite; }

@keyframes pulse { 50% { opacity: 0.45; } }

@media (prefers-reduced-motion: reduce) {
  .thinking, .tool-running .tool-status { animation: none; }
  .sidebar { transition: none !important; }
}

.reasoning { margin-bottom: 10px; color: var(--muted); font-size: 13px; }
.reasoning summary { min-height: 26px; cursor: pointer; }
.reasoning-text {
  margin-top: 6px;
  padding-left: 12px;
  border-left: 2px solid var(--border);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note { margin-top: 8px; padding: 7px 11px; background: var(--surface-2); border-radius: 8px; font-size: 13px; }

/* Herramientas del modo agente */

.msg-step { margin-bottom: 10px; }
.msg-step .md:not(:empty) { margin-bottom: 8px; }

.tool {
  margin-top: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}

.tool summary {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 4px 10px;
  cursor: pointer;
}

.tool summary::before { content: "▸"; color: var(--muted); }
.tool[open] summary::before { content: "▾"; }

.tool-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  font-family: var(--mono);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tool-status { flex: none; color: var(--muted); font-size: 12px; font-weight: 600; }
.tool-ok .tool-status { color: var(--accent); }
.tool-error .tool-status, .tool-rejected .tool-status { color: var(--danger); }
.tool-running .tool-status { animation: pulse 1.4s ease-in-out infinite; }

.tool-detail { padding: 8px 10px; border-top: 1px solid var(--border); }
.tool-detail-label { color: var(--muted); font-size: 12px; font-weight: 600; }

.tool pre {
  max-height: 320px;
  margin: 4px 0 0;
  overflow: auto;
  font: 12px/1.5 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
}

.msg-foot button {
  min-height: 30px;
  padding: 0 9px;
  color: var(--muted);
  background: transparent;
  border-color: var(--border);
  font-size: 12px;
}

.msg-foot button:hover { color: var(--text); }

.error-card {
  margin-bottom: 22px;
  padding: 12px 14px;
  color: var(--text);
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  border-radius: 10px;
}

.error-card strong { display: block; }
.error-detail { margin: 4px 0 0; font-size: 13px; overflow-wrap: anywhere; }
.error-card button { margin-top: 10px; }
.error-card button:not(.btn-primary) { background: var(--surface); border-color: var(--border); }

/* Plan */

.btn-upgrade { margin-bottom: 6px; padding: 0 10px; }

.banner {
  padding: 9px 18px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.banner.ok { color: var(--accent); font-weight: 600; }
.banner.bad { color: var(--danger); }

.plan-box {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.plan-box .dialog-note { margin: 0 0 4px; }
.plan-box .dialog-note:empty { display: none; }

/* Texto del modelo */

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md .table-wrap, .md .code { margin: 0 0 12px; }
.md ul, .md ol { padding-left: 24px; }
.md li { margin-bottom: 4px; }
.md li > p { margin: 0 0 4px; }
.md li > ul, .md li > ol { margin: 4px 0 0; }
.md h3, .md h4, .md h5, .md h6 { margin: 20px 0 8px; line-height: 1.3; }
.md h3 { font-size: 20px; }
.md h4 { font-size: 17px; }
.md h5, .md h6 { font-size: 15px; }
.md a { color: var(--accent); }
.md hr { margin: 18px 0; border: 0; border-top: 1px solid var(--border); }
.md blockquote { padding-left: 12px; color: var(--muted); border-left: 3px solid var(--border); }
.md code { padding: 1px 5px; background: var(--code-bg); border-radius: 4px; font: 0.9em var(--mono); }

.code { overflow: hidden; background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; }

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 36px;
  padding: 3px 4px 3px 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}

.code-head button { min-height: 28px; padding: 0 9px; color: var(--muted); background: transparent; font-size: 12px; }
.code-head button:hover { color: var(--text); }
.code pre { margin: 0; padding: 12px; overflow-x: auto; }
.code code { padding: 0; background: none; font-size: 13px; overflow-wrap: normal; white-space: pre; }

.table-wrap { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; }
.md th, .md td { padding: 6px 10px; border: 1px solid var(--border); text-align: left; overflow-wrap: normal; }
.md th { background: var(--surface-2); }

/* Caja de escritura */

.composer { padding: 10px max(20px, calc((100% - 780px) / 2)) 10px; }

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.composer-box:focus-within { border-color: var(--accent); }

.composer-box textarea {
  flex: 1;
  max-height: 40vh;
  padding: 7px 8px;
  background: transparent;
  border: 0;
  outline: none;
  resize: none;
}

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

/* Ventanas */

.dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  padding: 20px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.dialog::backdrop { background: rgb(0 0 0 / 45%); }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.dialog-head h2 { margin: 0; font-size: 18px; font-weight: 650; }
.dialog-note { margin: 0 0 12px; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }

.dialog-section { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.dialog-section summary { min-height: 26px; font-size: 14px; font-weight: 600; cursor: pointer; }
.stack { display: flex; flex-direction: column; align-items: stretch; gap: 12px; margin-top: 12px; }

.projects-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }

.project {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.project-text { display: flex; flex-direction: column; min-width: 0; }
.project-text strong { overflow-wrap: anywhere; }
.project-text span { color: var(--muted); font-size: 12px; }
.project-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.project-actions button { min-height: 34px; }

@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .only-narrow { display: inline-block; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 10;
    width: min(320px, 88vw);
    visibility: hidden;
    transform: translateX(-100%);
    transition: transform 0.18s ease, visibility 0.18s;
  }

  .sidebar.open { visibility: visible; transform: none; }

  .backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 9;
    background: rgb(0 0 0 / 45%);
  }

  .chat-head { gap: 8px; padding: 8px 12px; }
  .model-select { flex: 1; max-width: none; }
  .composer-hint { display: none; }
  .log { padding: 16px; }
  .composer { padding: 8px 12px; }
}


/* ---------- Páginas legales (condiciones y privacidad) ---------- */
body.legal-page { height: 100%; overflow-y: auto; }
.legal {
  max-width: 760px;
  margin: 0 auto;
  padding: 32px 20px 80px;
}
.legal-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.legal-top .brand { font-weight: 700; font-size: 20px; color: var(--accent); text-decoration: none; }
.legal h1 { font-size: 26px; margin: 16px 0 4px; }
.legal h2 { font-size: 18px; margin: 28px 0 8px; }
.legal .updated { color: var(--muted); font-size: 13px; margin: 0 0 8px; }
.legal p, .legal li { color: var(--text); }
.legal a { color: var(--accent); }
.legal ul { padding-left: 20px; }
.legal li { margin: 4px 0; }
.legal .note {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  margin: 16px 0;
  color: var(--muted);
  font-size: 14px;
}
.legal .legal-nav { margin-top: 40px; font-size: 14px; }
.legal .fill { color: var(--danger); font-weight: 600; }

/* Enlace y casilla de aceptación en el registro */
.consent {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 4px 0 2px;
  font-size: 13px;
  color: var(--muted);
}
.consent input { width: auto; min-height: 0; margin-top: 3px; }
.consent a { color: var(--accent); }

.auth-legal { margin-top: 14px; text-align: center; font-size: 13px; color: var(--muted); }
.auth-legal a { color: var(--muted); }

.verify-notice {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 10px 12px 0;
  padding: 10px 14px;
  background: var(--bubble);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
}
.verify-notice button { white-space: nowrap; }
