/*
 * @stratia-layer L30 — Chat-web (frontend ciudadano) · estilos
 * @plane experience · @tenancy per-tenant · @source new · @migration reescribe
 * @status in-progress · @owner frontend
 * @arch-notes Paleta y tipografía tomadas del lobby STRATIA (mismo look & feel). Tema oscuro fijo.
 *   Fuentes servidas localmente (assets/fonts/*.woff2) → CSP-safe, sin CDN. Sin imágenes remotas.
 */

/* ── Fuentes locales (Fira Sans para títulos/marca; Arial para el cuerpo) ── */
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 400; font-display: swap; src: url("./assets/fonts/FiraSans-400.woff2") format("woff2"); }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 600; font-display: swap; src: url("./assets/fonts/FiraSans-600.woff2") format("woff2"); }
@font-face { font-family: "Fira Sans"; font-style: normal; font-weight: 700; font-display: swap; src: url("./assets/fonts/FiraSans-700.woff2") format("woff2"); }

:root {
  --paper: #0E1116;
  --surface: #161B22;
  --surface-2: #1C222B;
  --surface-3: #232A34;
  --ink: #E7ECF3;
  --ink-2: #B4BDCA;
  --ink-3: #7B8595;
  --line: #262D38;
  --line-2: #333C49;
  --orange: #F7AE45;
  --coral: #F0637A;
  --teal: #2FA98C;
  --shadow: 0 1px 2px rgba(0,0,0,.5), 0 26px 54px -28px rgba(0,0,0,.75);
  --head: "Fira Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  --sans: Arial, "Helvetica Neue", Helvetica, sans-serif;
  --maxw: 820px;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: var(--maxw);
  margin: 0 auto;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  background: var(--paper);
}

/* ── Header ── */
.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--surface), var(--paper));
}
.brandmark { height: 30px; width: auto; display: block; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
#bot-name {
  font-family: var(--head);
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-sub { font-size: 11px; color: var(--ink-3); letter-spacing: .06em; text-transform: uppercase; }
.header-spacer { flex: 1; }

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--ink-3); flex: 0 0 auto;
}
.status-dot[data-state="ready"] { background: var(--teal); box-shadow: 0 0 0 3px rgba(47,169,140,.18); }
.status-dot[data-state="warming"] { background: var(--orange); box-shadow: 0 0 0 3px rgba(247,174,69,.18); animation: pulse 1.4s ease-in-out infinite; }
.status-dot[data-state="demo"] { background: var(--coral); box-shadow: 0 0 0 3px rgba(240,99,122,.18); }
.status-dot[data-state="offline"] { background: var(--ink-3); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .4; } }

.icon-btn {
  appearance: none;
  border: 1px solid var(--line-2);
  background: var(--surface-2);
  color: var(--ink-2);
  font-family: var(--sans);
  font-size: 13px;
  padding: 7px 12px;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.icon-btn:hover { background: var(--surface-3); color: var(--ink); border-color: var(--line-2); }
.icon-btn:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; }

/* ── Banner "calentando" / demo ── */
.warming {
  margin: 0;
  padding: 10px 18px;
  font-size: 13px;
  color: #221206;
  background: var(--orange);
  border-bottom: 1px solid var(--line);
}
.warming[hidden] { display: none; }

/* ── Lista de mensajes ── */
.messages-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

/* Botón flotante "ir al final" (aparece si el usuario subió durante el stream). */
.to-bottom {
  position: absolute;
  right: 16px;
  bottom: 12px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line-2);
  background: var(--surface-3);
  color: var(--ink);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s, opacity .15s;
}
.to-bottom:hover { background: var(--surface-2); }
.to-bottom:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; }
.to-bottom[hidden] { display: none; }
.msg { display: flex; }
.msg-user { justify-content: flex-end; }
.msg-assistant { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.msg-user .bubble {
  background: linear-gradient(180deg, var(--surface-3), var(--surface-2));
  border-color: var(--line-2);
  border-bottom-right-radius: 5px;
}
.msg-assistant .bubble { border-bottom-left-radius: 5px; }
.bubble-error { border-color: var(--coral); }

.bubble-text { white-space: pre-wrap; word-wrap: break-word; }

/* Markdown renderizado dentro de la burbuja (bloques a nodos, no innerHTML). */
.bubble-text > :first-child { margin-top: 0; }
.bubble-text > :last-child { margin-bottom: 0; }
.bubble-text p { margin: 0 0 8px; white-space: normal; }
.bubble-text h3, .bubble-text h4, .bubble-text h5 {
  font-family: var(--head); margin: 12px 0 6px; line-height: 1.3;
}
.bubble-text h3 { font-size: 17px; }
.bubble-text h4 { font-size: 15px; }
.bubble-text h5 { font-size: 14px; color: var(--ink-2); }
.bubble-text ul, .bubble-text ol { margin: 4px 0 8px; padding-left: 20px; }
.bubble-text li { margin: 2px 0; }
.bubble-text a { color: var(--orange); text-decoration: underline; }
.bubble-text code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px; background: var(--surface-3); border: 1px solid var(--line-2);
  border-radius: 5px; padding: 1px 5px;
}
.bubble-text pre {
  margin: 8px 0; padding: 10px 12px; overflow-x: auto;
  background: var(--surface-3); border: 1px solid var(--line-2); border-radius: 9px;
}
.bubble-text pre code { background: none; border: none; padding: 0; font-size: 12.5px; }
.bubble-text blockquote {
  margin: 8px 0; padding: 2px 0 2px 12px; border-left: 3px solid var(--line-2); color: var(--ink-2);
}

/* Cursor de tecleo mientras la burbuja está en streaming. */
.bubble.streaming .bubble-text::after {
  content: "▍";
  display: inline;
  margin-left: 1px;
  color: var(--orange);
  animation: caret 1s step-end infinite;
}
@keyframes caret { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.stopped-note { color: var(--ink-3); font-size: 12px; font-style: italic; }

.bubble-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; }

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  color: var(--ink-2);
  white-space: nowrap;
}
.badge-scope.scope-white { color: var(--teal); border-color: rgba(47,169,140,.5); background: rgba(47,169,140,.1); }
.badge-scope.scope-grey { color: var(--orange); border-color: rgba(247,174,69,.5); background: rgba(247,174,69,.1); }
.badge-scope.scope-black { color: var(--coral); border-color: rgba(240,99,122,.5); background: rgba(240,99,122,.12); }
.badge-demo { color: var(--coral); border-color: rgba(240,99,122,.5); }
.badge-intent { color: var(--ink-3); font-family: var(--head); font-weight: 600; text-transform: none; }

/* ── Fuentes citadas ── */
.sources { margin-top: 10px; border-top: 1px dashed var(--line-2); padding-top: 8px; }
.sources summary { cursor: pointer; font-size: 12px; color: var(--ink-3); font-weight: 600; }
.sources summary:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; border-radius: 4px; }
.sources-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.source { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.source-name { color: var(--ink); text-decoration: none; }
a.source-name:hover { color: var(--orange); text-decoration: underline; }
.source-cat { color: var(--ink-3); font-size: 11px; }
.source-sim { margin-left: auto; color: var(--teal); font-variant-numeric: tabular-nums; font-size: 11px; }

/* ── Indicador de "escribiendo" ── */
.bubble.typing { display: inline-flex; gap: 5px; padding: 14px; }
.typing .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); animation: bounce 1.2s infinite ease-in-out; }
.typing .dot:nth-child(2) { animation-delay: .15s; }
.typing .dot:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* ── Chips de sugerencia (config-driven, chat_chips by-intent) ── */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 18px 0;
  max-width: var(--maxw);
  margin: 0 auto;
  width: 100%;
}
.chips[hidden] { display: none; }
.chip {
  padding: 5px 12px;
  font-size: 12px;
  font-family: var(--sans);
  font-weight: 600;
  line-height: 1.3;
  border-radius: 99px;
  border: 1px solid var(--line-2);
  background: var(--surface-2);
  color: var(--ink-2);
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { background: var(--teal); color: #fff; border-color: var(--teal); }
.chip:focus-visible { outline: none; border-color: var(--orange); box-shadow: 0 0 0 3px rgba(247,174,69,.15); }

/* ── Composer ── */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: linear-gradient(0deg, var(--surface), var(--paper));
}
#input {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 200px;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line-2);
  background: var(--surface-2);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.4;
}
#input::placeholder { color: var(--ink-3); }
#input:focus { outline: none; border-color: var(--orange); box-shadow: 0 0 0 3px rgba(247,174,69,.15); }
#input:disabled { opacity: .6; }

.send-btn {
  appearance: none;
  border: none;
  cursor: pointer;
  height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  font-family: var(--head);
  font-weight: 700;
  font-size: 14px;
  color: #221206;
  background: linear-gradient(180deg, var(--orange), #E0A24A);
  transition: filter .15s, opacity .15s;
}
.send-btn:hover { filter: brightness(1.06); }
.send-btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.send-btn:disabled { opacity: .5; cursor: not-allowed; }
.send-btn[hidden] { display: none; }

/* Botón Detener (reemplaza a Enviar mientras se transmite la respuesta). */
.stop-btn {
  appearance: none;
  cursor: pointer;
  height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  font-family: var(--head);
  font-weight: 700;
  font-size: 14px;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--coral);
  transition: background .15s;
}
.stop-btn:hover { background: var(--surface-3); }
.stop-btn:focus-visible { outline: 2px solid var(--coral); outline-offset: 2px; }
.stop-btn[hidden] { display: none; }

.footnote { text-align: center; font-size: 11px; color: var(--ink-3); padding: 0 18px 10px; }
.footnote a { color: var(--ink-3); }
.build-version { display: inline-block; margin-left: 6px; opacity: .7; font-variant-numeric: tabular-nums; letter-spacing: .2px; }
.build-version:empty { display: none; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .app { border-left: none; border-right: none; }
  .bubble { max-width: 90%; }
  .brand-sub { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .status-dot, .typing .dot, .messages { animation: none !important; scroll-behavior: auto; }
  .bubble.streaming .bubble-text::after { animation: none; }
}

/* ── Cockpit de Fine-Tuning (#ft, superficie admin) ── */
.ftx {
  display: block;
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 18px calc(24px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  overflow-y: auto;
  background: var(--paper);
}
.ftx[hidden] { display: none; }
.ftx-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.ftx-title { font-family: var(--head); font-size: 20px; font-weight: 700; margin: 0; color: var(--ink); }
.ftx-sub { margin: 0; color: var(--ink-3); font-size: 13px; }
.ftx-back { margin-left: auto; color: var(--orange); text-decoration: none; font-size: 13px; font-weight: 600; }
.ftx-back:hover { text-decoration: underline; }
.ftx-status { margin: 12px 0; padding: 9px 12px; border-radius: 10px; font-size: 13px; background: var(--surface-2); border: 1px solid var(--line-2); color: var(--ink-2); }
.ftx-status[data-kind="warn"] { border-color: var(--orange); color: var(--ink); }
.ftx-status[data-kind="err"] { border-color: var(--coral); color: var(--ink); }
.ftx-panel { margin: 14px 0; padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.ftx-h2 { font-family: var(--head); font-size: 15px; font-weight: 600; margin: 0 0 10px; color: var(--ink); }
.ftx-row { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; }
.ftx-label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-3); }
.ftx-select { min-width: 320px; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--surface-2); color: var(--ink); font-family: var(--sans); font-size: 14px; }
.ftx-btn { padding: 10px 18px; border-radius: 10px; border: 0; background: var(--teal); color: #fff; font-family: var(--head); font-weight: 600; font-size: 14px; cursor: pointer; }
.ftx-btn:disabled { opacity: .45; cursor: not-allowed; }
.ftx-muted { color: var(--ink-3); font-size: 13px; margin: 0; }
.ftx-kv { display: flex; flex-wrap: wrap; gap: 8px 22px; margin-bottom: 12px; }
.ftx-kv-row { display: flex; gap: 6px; align-items: baseline; }
.ftx-kv-k { color: var(--ink-3); font-size: 12px; }
.ftx-kv-v { color: var(--ink); font-size: 13px; }
.ftx-steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
.ftx-step { display: flex; align-items: center; gap: 10px; padding: 5px 8px; border-radius: 8px; font-size: 13px; border-left: 3px solid var(--line-2); }
.ftx-step-n { display: inline-flex; align-items: center; justify-content: center; width: 22px; height: 22px; border-radius: 50%; background: var(--surface-3); color: var(--ink-3); font-size: 11px; flex: none; }
.ftx-step-name { color: var(--ink-2); }
.ftx-step-done { border-left-color: var(--teal); }
.ftx-step-done .ftx-step-n { background: var(--teal); color: #fff; }
.ftx-step-run { border-left-color: var(--orange); background: rgba(247,174,69,.08); }
.ftx-step-run .ftx-step-n { background: var(--orange); color: #241a05; }
.ftx-step-run .ftx-step-name { color: var(--ink); font-weight: 600; }
.ftx-step-bad { border-left-color: var(--coral); background: rgba(240,99,122,.08); }
.ftx-step-bad .ftx-step-n { background: var(--coral); color: #fff; }
.ftx-log { margin: 0; max-height: 320px; overflow-y: auto; padding: 12px; border-radius: 10px; background: #0a0d12; border: 1px solid var(--line); color: var(--ink-2); font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
