:root {
  --bg: #f2f4f7;
  --panel: #ffffff;
  --panel-2: #f6f8fb;
  --panel-3: #eceff3;
  --border: #e0e4ea;
  --text: #1b2230;
  --muted: #66707f;
  --accent: #0088d1;        /* azul del logotipo */
  --accent-2: #0a6fb0;
  --accent-text: #ffffff;
  --brand-red: #e10713;     /* rojo del logotipo */
  --brand-gold: #bc8533;    /* dorado del logotipo */
  --user-bubble: #e1f0fb;
  --warn-bg: #fff4d6;
  --warn-text: #7a5200;
  --error-bg: #ffe3e3;
  --error-text: #8f1d1d;
  --danger: #c62828;
  --code-bg: #eef1f5;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
  --radius: 14px;
  /* Curvas de movimiento (siempre ease-out en entradas; drawer tipo iOS) */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --panel: #1a1f27;
    --panel-2: #20262f;
    --panel-3: #262d37;
    --border: #2c343f;
    --text: #e7eaef;
    --muted: #97a1af;
    --accent: #2ea3ec;
    --accent-2: #1d8ad0;
    --accent-text: #0a1220;
    --user-bubble: #1f3650;
    --warn-bg: #3a2f10;
    --warn-text: #ffd66b;
    --error-bg: #3c1a1a;
    --error-text: #ff9c9c;
    --danger: #e5484d;
    --code-bg: #262c36;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}

/* ---------- base y plataforma móvil */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html {
  -webkit-tap-highlight-color: transparent; /* sin destello gris al tocar */
  -webkit-text-size-adjust: 100%;           /* sin inflado de texto en horizontal */
  overscroll-behavior: none;                /* la app tiene sus propios contenedores de scroll */
}
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}
button { font: inherit; }
button, a, [role="button"], .chip, .session {
  touch-action: manipulation;   /* sin retardo de 300 ms al tocar */
  user-select: none;            /* los controles no se seleccionan con pulsación larga */
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
input, textarea, select { font-size: 16px; } /* < 16px hace zoom en iOS al enfocar */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pad { padding: 12px; }

#app { display: grid; grid-template-columns: 290px 1fr; height: 100vh; height: 100dvh; }

/* ---------- sidebar */
#sidebar {
  display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border);
  min-height: 0;
  border-top: 4px solid var(--brand-red);
  padding-top: var(--sat);
}
.sb-head { padding: 16px 16px 12px; border-bottom: 1px solid var(--border); position: relative; }
.sb-close { display: none; position: absolute; top: 10px; right: 10px; }
.brand { display: block; text-decoration: none; color: inherit; margin-bottom: 14px; text-align: center; }
.brand img { width: 100%; max-width: 210px; height: auto; display: block; margin: 0 auto 4px; }
.brand-sub { display: block; color: var(--muted); font-size: 12px; letter-spacing: .06em; text-transform: uppercase; }
.sessions { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 8px; }
.session {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 10px; border-radius: 8px; cursor: pointer;
  color: var(--text);
  transition: background 120ms ease, transform 160ms var(--ease-out);
}
.session:active { transform: scale(0.98); background: var(--panel-3); }
.session.active { background: var(--user-bubble); }
.session .title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; }
.session .del { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 18px; padding: 4px 8px; border-radius: 6px; opacity: 0; transition: opacity 120ms ease; }
.session:focus-within .del, .session.active .del { opacity: 1; }
@media (hover: none) { .session .del { opacity: 1; } }
.sb-foot { padding: 12px 16px calc(12px + var(--sab)); border-top: 1px solid var(--border); font-size: 13px; }
.status { margin-bottom: 8px; }
.status .warn { color: var(--warn-text); }
.status .error { color: var(--error-text); }
.spin::before { content: "⟳ "; display: inline-block; animation: spin 1.2s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.logout { display: block; margin: 8px auto 0; color: var(--muted); }
.backdrop { position: fixed; inset: 0; z-index: 19; background: rgba(10, 16, 28, .45); opacity: 0; transition: opacity 200ms ease; }
.backdrop.show { opacity: 1; }

/* ---------- buttons */
.btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 8px 14px; border-radius: 10px; cursor: pointer;
  transition: transform 160ms var(--ease-out), background 120ms ease, border-color 120ms ease;
}
.btn:active { transform: scale(0.97); background: var(--panel-3); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:active { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn.stop { background: var(--danger); border-color: var(--danger); color: #fff; }
.link { border: 0; background: transparent; color: var(--accent); cursor: pointer; padding: 0; font-size: 13px; }

/* ---------- main */
#main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.topbar { display: none; align-items: center; gap: 10px; padding: calc(8px + var(--sat)) 12px 8px; border-bottom: 1px solid var(--border); background: var(--panel); font-weight: 600; border-top: 4px solid var(--brand-red); }
.topbar-logo { height: 30px; width: auto; }
.topbar-title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.banner { background: var(--warn-bg); color: var(--warn-text); padding: 10px 20px; font-size: 14px; border-bottom: 1px solid var(--border); }
.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 24px 0 12px; -webkit-overflow-scrolling: touch; }
.messages > * { max-width: 920px; margin-left: auto; margin-right: auto; padding: 0 20px; }

.welcome { padding-top: 28px; text-align: center; }
.welcome-logo { width: min(320px, 70%); height: auto; margin: 0 auto 8px; display: block; }
.welcome h1 { font-size: 28px; line-height: 1.15; letter-spacing: -0.02em; margin: 6px 0 8px; }
.welcome p { color: var(--muted); max-width: 640px; margin: 0 auto; }
.welcome .hint { margin-top: 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; justify-content: center; }
.chip {
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 8px 14px; border-radius: 999px; cursor: pointer; text-align: left; font-size: 14px;
  box-shadow: var(--shadow);
  transition: transform 160ms var(--ease-out), border-color 120ms ease, color 120ms ease, background 120ms ease;
}
.chip:active { transform: scale(0.97); background: var(--panel-3); }

.msg { display: flex; gap: 12px; margin-bottom: 22px; }
.msg.discarded { opacity: .55; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 16px 16px 4px 16px;
  max-width: 80%; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble .imgs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; }
.msg.user .bubble img { max-width: 220px; max-height: 170px; border-radius: 8px; cursor: zoom-in; display: block; }
.msg.assistant .avatar {
  flex: 0 0 36px; width: 36px; height: 36px; border-radius: 10px; overflow: hidden;
  background: #fff; border: 1px solid var(--border); margin-top: 2px;
  display: inline-flex; align-items: center; justify-content: center;
}
.msg.assistant .avatar img { width: 100%; height: 100%; object-fit: contain; }
.msg.assistant .body { flex: 1; min-width: 0; }
.status-line { color: var(--muted); font-size: 13px; padding: 4px 0; }
.status-line:not(:empty)::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 8px; animation: pulse 1s ease-in-out infinite; vertical-align: middle; }
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }

.steps { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.step { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; font-size: 13px; }
.step summary { display: flex; gap: 8px; align-items: center; padding: 7px 10px; cursor: pointer; list-style: none; }
.step summary::-webkit-details-marker { display: none; }
.step .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step .summary { color: var(--muted); white-space: nowrap; }
.step.error .summary { color: var(--error-text); }
.step pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--border); background: var(--code-bg); font-size: 12px; overflow-x: auto; border-radius: 0 0 8px 8px; }

.thinking { margin-bottom: 8px; font-size: 13px; color: var(--muted); }
.thinking summary { cursor: pointer; }
.thinking-text { white-space: pre-wrap; padding: 6px 0 0 12px; border-left: 2px solid var(--border); margin-top: 4px; }

.content { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px; box-shadow: var(--shadow); overflow-x: auto; border-left: 3px solid var(--accent); overflow-wrap: anywhere; }
.content:empty { display: none; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content p { margin: 0 0 10px; }
.content h1, .content h2, .content h3 { font-size: 16px; margin: 14px 0 6px; }
.content ul, .content ol { padding-left: 22px; margin: 0 0 10px; }
.content table { border-collapse: collapse; width: 100%; margin: 8px 0 12px; font-size: 14px; }
/* Tablas anchas: contenedor desplazable con sombra en el borde que indica que hay más contenido */
.table-wrap { position: relative; overflow-x: auto; margin: 8px 0 12px; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.table-wrap table { margin: 0; }
.table-wrap::before, .table-wrap::after {
  content: ""; position: sticky; top: 0; bottom: 0; width: 18px; pointer-events: none; flex: none;
  display: none;
}
.table-wrap.scroll-right { box-shadow: inset -18px 0 14px -14px rgba(0, 0, 0, .28); }
.table-wrap.scroll-left { box-shadow: inset 18px 0 14px -14px rgba(0, 0, 0, .28); }
.table-wrap.scroll-left.scroll-right { box-shadow: inset 18px 0 14px -14px rgba(0, 0, 0, .28), inset -18px 0 14px -14px rgba(0, 0, 0, .28); }
.content th, .content td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
.content th { background: var(--panel-2); }
.content tr:nth-child(even) td { background: color-mix(in srgb, var(--panel-2) 60%, transparent); }
.content code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.content pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.content pre code { background: transparent; padding: 0; }
.content a { color: var(--accent); }
.content blockquote { margin: 0 0 10px; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }

.notice { margin-top: 8px; padding: 8px 12px; border-radius: 8px; background: var(--warn-bg); color: var(--warn-text); font-size: 14px; }
.notice.error { background: var(--error-bg); color: var(--error-text); }
.meta { display: flex; gap: 12px; align-items: center; margin-top: 6px; font-size: 12px; color: var(--muted); }

/* ---------- composer (barra de escritura tipo chat) */
.composer {
  display: flex; flex-direction: column; gap: 8px;
  max-width: 920px; width: 100%; margin: 0 auto; padding: 8px 20px calc(18px + var(--sab));
}
.composer-row {
  display: flex; gap: 6px; align-items: flex-end;
  background: var(--panel); border: 1px solid var(--border); border-radius: 26px;
  padding: 5px 5px 5px 6px; box-shadow: var(--shadow);
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
.composer-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.composer .attach {
  flex: 0 0 40px; width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent;
  color: var(--muted); cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  transition: transform 160ms var(--ease-out), background 120ms ease, color 120ms ease;
}
.composer .attach:active { transform: scale(0.94); background: var(--panel-3); color: var(--text); }
.composer textarea {
  flex: 1; resize: none; font: inherit; font-size: 15px; line-height: 1.4; color: var(--text);
  background: transparent; border: 0; padding: 10px 6px; max-height: 200px; outline: none; min-width: 0;
}
.composer .send {
  flex: 0 0 40px; width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: transform 160ms var(--ease-out), background 120ms ease;
}
.composer .send:active { transform: scale(0.94); background: var(--accent-2); }
.composer .send.stop { background: var(--danger); color: #fff; }
.composer .send .send-text { display: none; }
.composer .send .stop-icon { display: none; }
.composer .send.stop .send-icon { display: none; }
.composer .send.stop .stop-icon { display: block; }
.composer .send .send-icon { margin-left: 2px; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; }
.attachment { position: relative; width: 72px; height: 72px; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); background: var(--panel); }
.attachment img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attachment .rm {
  position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.65); color: #fff; cursor: pointer; font-size: 13px; line-height: 22px; padding: 0;
}
body.dragging #main { outline: 3px dashed var(--accent); outline-offset: -8px; }
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; border-radius: 8px; }

/* ---------- login */
.login-body { display: flex; align-items: center; justify-content: center; padding: 20px; min-height: 100svh; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border);
  border-top: 4px solid var(--brand-red); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 28px 28px 24px; text-align: center;
}
.login-logo { width: 220px; max-width: 100%; height: auto; margin: 0 auto 8px; display: block; }
.login-card h1 { font-size: 20px; margin: 4px 0 4px; letter-spacing: -0.01em; }
.login-card p { margin: 0 0 16px; }
.login-card input {
  width: 100%; font: inherit; font-size: 16px; padding: 12px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel); color: var(--text); margin-bottom: 10px; outline: none;
  transition: border-color 120ms ease;
}
.login-card input:focus { border-color: var(--accent); }

/* ---------- hover solo donde hay ratón (en táctil se quedaría pegado) */
@media (hover: hover) and (pointer: fine) {
  .btn:hover { background: var(--panel-2); }
  .btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
  .session:hover { background: var(--panel-2); }
  .session:hover .del { opacity: 1; }
  .session .del:hover { color: var(--error-text); background: var(--error-bg); }
  .chip:hover { border-color: var(--accent); color: var(--accent); }
}

/* ---------- móvil */
@media (max-width: 820px) {
  #app { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: min(85vw, 340px); z-index: 20;
    transform: translateX(-100%); transition: transform 400ms var(--ease-drawer);
    box-shadow: 0 0 40px rgba(0,0,0,.35); padding-left: var(--sal);
  }
  #sidebar.open { transform: none; }
  .sb-close { display: inline-flex; }
  .brand img { max-width: 170px; }
  .topbar { display: flex; }
  .messages { padding: 12px 0 8px; }
  .messages > * { padding: 0 12px; }
  /* Inicio compacto: cabe en una pantalla; las sugerencias se deslizan de lado */
  .welcome { padding-top: 10px; }
  .welcome-logo { width: 42%; max-width: 160px; margin-bottom: 4px; }
  .welcome h1 { font-size: 24px; margin: 4px 0 6px; }
  .welcome p { font-size: 14px; line-height: 1.45; }
  .chips {
    display: flex; flex-wrap: nowrap; justify-content: flex-start; gap: 8px;
    overflow-x: auto; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch;
    margin: 16px -12px 0; padding: 2px 12px 10px; scrollbar-width: none; overscroll-behavior-x: contain;
    scroll-padding-inline: 12px;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: 0 0 76%; scroll-snap-align: start; border-radius: 14px; padding: 12px 14px; line-height: 1.35; }
  .welcome .hint { margin-top: 12px; }
  .msg { gap: 8px; margin-bottom: 16px; }
  .msg.assistant .avatar { display: none; }
  .msg.user .bubble { max-width: 90%; }
  .msg.user .bubble img { max-width: 160px; max-height: 130px; }
  .content { padding: 12px 12px; border-radius: 12px; }
  /* Las tablas no se comprimen: conservan su anchura natural y se desplazan con el dedo */
  .content table { font-size: 13px; width: max-content; min-width: 100%; }
  .table-wrap { margin-left: -12px; margin-right: -12px; padding: 0 12px; }
  .content th, .content td { padding: 6px 8px; max-width: 55vw; }
  .content td:first-child, .content th:first-child { white-space: nowrap; }
  .composer { padding: 6px 10px calc(10px + var(--sab)); gap: 6px; }
  .composer textarea { font-size: 16px; }
}

/* ---------- menos movimiento */
@media (prefers-reduced-motion: reduce) {
  #sidebar { transition: opacity 150ms ease; }
  .btn, .chip, .session { transition: background 120ms ease, border-color 120ms ease; }
  .btn:active, .chip:active, .session:active { transform: none; }
  .status-line:not(:empty)::before { animation: none; opacity: 1; }
}
