/* Jorge Copilot — frontend del piloto. Mobile-first (B14): el publico son
   mecanicos desde el celular, a veces con manos sucias. Controles >=44px,
   input fijo abajo que el teclado no tape. */

:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1c2330;
  --border: #2a3340;
  --text: #e6edf3;
  --muted: #9aa7b4;
  --accent: #e0903a;     /* naranja taller */
  --accent-2: #f2b66d;
  --green: #5fb96a;
  --red: #e5534b;
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px; line-height: 1.5;
}
button { font-family: inherit; font-size: 16px; cursor: pointer; }
.hidden { display: none !important; }

/* --- layout app --- */
#app { display: flex; flex-direction: column; height: 100dvh; max-width: 720px; margin: 0 auto; position: relative; }

header.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.topbar .brand { font-weight: 700; display: flex; align-items: center; gap: 8px; flex: 1; }
.topbar-btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 9px; min-height: 40px; padding: 0 12px; font-size: 14px; font-weight: 600; white-space: nowrap; }
/* "Nueva consulta": acción primaria — color de acento para que sea inconfundible */
.topbar-btn.new-btn { background: var(--accent); color: #1a1207; border: none; font-weight: 700; }
/* "Mis chats": acceso al historial (reloj + texto para que se note dónde está) */
.topbar-btn.history-btn { background: var(--panel-2); }
/* En móvil chico el historial queda solo con el ícono de reloj para que quepa "Nueva consulta" */
@media (max-width: 360px) { .history-btn .tb-label { display: none; } }
.chat-list { display: flex; flex-direction: column; gap: 6px; max-height: 52vh; overflow-y: auto; margin: 8px 0; }
.chat-item { display: block; width: 100%; text-align: left; background: var(--panel-2);
  border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 11px 12px;
  min-height: 44px; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* "Cerrar sesión" del menú de chats: separado de la lista para no confundirlo con un chat */
.logout-row { margin-top: 12px; }
.topbar .brand small { color: var(--muted); font-weight: 400; }
.balance-pill {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; white-space: nowrap;
}
.balance-pill b { color: var(--accent-2); }
.saldo-wrap { display: flex; align-items: center; gap: 6px; margin-left: auto; }
/* La palabra "saldo" solo en pantallas anchas; en móvil basta el monto + el "?" */
.bp-label { display: none; }
@media (min-width: 431px) { .bp-label { display: inline; } }
.saldo-info { width: 22px; height: 22px; min-width: 22px; border-radius: 50%; background: var(--panel-2);
  border: 1px solid var(--border); color: var(--muted); font-size: 12px; font-weight: 700; line-height: 1;
  padding: 0; cursor: pointer; }

/* --- chat --- */
#chat { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px;
  overflow-anchor: auto; -webkit-overflow-scrolling: touch; }
.msg { max-width: 88%; padding: 10px 13px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #1a1207; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant .cites { margin-top: 8px; font-size: 12.5px; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 6px; }
.msg.assistant .cites a, .msg.assistant .cites span { color: var(--accent-2); }
/* Markdown renderizado en la respuesta del bot (negritas, viñetas, títulos, código) */
.msg.assistant strong { font-weight: 700; }
.msg.assistant em { font-style: italic; }
.msg.assistant code { background: var(--panel-2, rgba(255,255,255,.08)); border-radius: 5px;
  padding: 1px 5px; font-size: .92em; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.msg.assistant ul, .msg.assistant ol { margin: 4px 0; padding-left: 20px; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant .md-h { font-weight: 700; margin: 8px 0 2px; }
.msg.assistant .md-sp { height: 8px; }
.status { align-self: flex-start; color: var(--muted); font-size: 14px; font-style: italic; padding: 2px 6px;
  display: flex; flex-direction: row; align-items: center; gap: 8px; }
/* etiqueta con shimmer cálido sutil (se siente "procesando" aunque el texto no cambie) */
.status-label {
  white-space: nowrap;
  background-image: linear-gradient(100deg, var(--muted) 0%, var(--muted) 40%, var(--accent-2) 50%, var(--muted) 60%, var(--muted) 100%);
  background-size: 220% 100%; background-repeat: no-repeat;
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: status-shimmer 2.4s linear infinite; will-change: background-position;
}
/* tres puntos naranja con rebote escalonado (la "ola" tipo escribiendo) */
.typing-dots { display: inline-flex; align-items: center; gap: 4px; padding-bottom: 1px; flex: 0 0 auto; }
.typing-dots .dot {
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
  transform: translateY(0); opacity: .4; animation: typing-bounce 1.25s infinite ease-in-out both;
  will-change: transform, opacity;
}
.typing-dots .dot:nth-child(1) { animation-delay: 0s; }
.typing-dots .dot:nth-child(2) { animation-delay: .16s; }
.typing-dots .dot:nth-child(3) { animation-delay: .32s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-4px); opacity: 1; }
}
@keyframes status-shimmer { from { background-position: 140% 0; } to { background-position: -40% 0; } }
@media (prefers-reduced-motion: reduce) {
  .status-label { animation: none; background: none; -webkit-background-clip: border-box; background-clip: border-box; color: var(--muted); }
  .typing-dots .dot { animation: none; transform: none; opacity: .7; }
}
.safety-warn { color: var(--accent-2); font-size: 12.5px; margin-top: 6px; }

/* feedback 👍/👎 */
.fb { display: flex; gap: 8px; margin-top: 8px; }
.fb button { background: transparent; border: 1px solid var(--border); border-radius: 8px;
  min-width: 44px; min-height: 36px; color: var(--muted); }
.fb button.active { border-color: var(--accent); color: var(--accent-2); }
.fb-note { width: 100%; margin-top: 6px; }

/* --- loop de desenlace (captura del fix que resolvió) --- */
.outcome { margin-top: 8px; }
.outcome-trigger { background: transparent; border: 1px dashed var(--border); border-radius: 8px;
  color: var(--accent-2); font-size: 13px; padding: 6px 10px; min-height: 36px; cursor: pointer; }
.outcome-form { margin-top: 8px; display: flex; flex-direction: column; gap: 8px;
  border-top: 1px dashed var(--border); padding-top: 8px; }
.outcome-q { font-size: 13px; color: var(--muted); }
.outcome .rating button.active { border-color: var(--accent); color: var(--accent-2); }
.outcome-send { align-self: flex-start; background: var(--accent); color: #fff; border: none;
  border-radius: 8px; padding: 8px 16px; min-height: 40px; cursor: pointer; }
.outcome-thanks { font-size: 13px; color: var(--accent-2); }

/* --- widgets de la guia (D7) --- */
.widget { align-self: flex-start; max-width: 88%; display: flex; flex-direction: column; gap: 8px;
  margin-top: -4px; }
.widget .w-opciones { display: flex; flex-wrap: wrap; gap: 8px; }
.widget button.opt, .widget .form-submit {
  background: var(--panel-2); border: 1px solid var(--accent); color: var(--accent-2);
  border-radius: 10px; padding: 10px 14px; min-height: 44px; }
.widget .form-submit { background: var(--accent); color: #1a1207; border: none; font-weight: 600; }
.widget .card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.widget .card .cap { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.widget label.row { display: flex; align-items: center; gap: 10px; padding: 8px 0; min-height: 44px; }
.widget label.row input[type=checkbox] { width: 22px; height: 22px; }
.widget .field { margin-bottom: 8px; }
.widget .field label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 3px; }
.widget input[type=text] {
  width: 100%; padding: 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); min-height: 44px; }
.widget .med-row { display: flex; gap: 8px; align-items: flex-end; }
.widget .med-row .m-input { flex: 3; }
.widget .med-row .m-exp { flex: 2; color: var(--green); font-size: 13px; padding-bottom: 12px; }

/* --- input fijo abajo --- */
.composer {
  display: flex; gap: 8px; padding: 10px; background: var(--panel);
  border-top: 1px solid var(--border); position: sticky; bottom: 0;
}
.composer textarea {
  flex: 1; resize: none; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); padding: 11px 12px; font-size: 16px;
  max-height: 40dvh; min-height: 46px; overflow-y: auto; }
.composer button.send {
  background: var(--accent); color: #1a1207; border: none; border-radius: 12px;
  min-width: 56px; min-height: 46px; font-weight: 700; }
.composer button:disabled { opacity: .5; }

/* --- pantallas (login / consent / onboarding) --- */
.screen { flex: 1; overflow-y: auto; padding: 22px 18px; display: flex; flex-direction: column; gap: 16px; }
.screen h1 { font-size: 22px; margin: 0; }
.screen h2 { font-size: 18px; margin: 6px 0; }
.screen p { color: var(--muted); margin: 4px 0; }
.card-box { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.input-lg { width: 100%; padding: 13px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 17px; min-height: 50px; }
.input-lg.invalid { border-color: var(--red); box-shadow: 0 0 0 2px rgba(229,83,75,.25); }
.btn-primary { background: var(--accent); color: #1a1207; border: none; border-radius: 10px;
  padding: 14px; font-weight: 700; width: 100%; min-height: 50px; }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 13px; width: 100%; min-height: 50px; }
.starters { display: flex; flex-direction: column; gap: 10px; }
.starters button { text-align: left; background: var(--panel-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 12px; padding: 14px; min-height: 56px; font-size: 16px; }
.starters button.active { border-color: var(--accent); color: var(--accent-2); background: #1f1a10; }
.screen .field { margin-bottom: 11px; }
.screen .field > label { display: block; font-size: 13.5px; color: var(--muted); margin-bottom: 4px; }
.consent-row { display: flex; gap: 10px; align-items: flex-start; margin-top: 8px; }
.consent-row input { width: 22px; height: 22px; margin-top: 2px; }
.disclaimer { background: #2a2110; border: 1px solid #5a4520; border-radius: 10px; padding: 12px; font-size: 14px; }
.muted-note { font-size: 12.5px; color: var(--muted); }
.error { color: var(--red); font-size: 14px; }
a { color: var(--accent-2); }

/* --- recharge / survey panel --- */
.panel-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex;
  align-items: flex-end; justify-content: center; z-index: 20; }
.panel-overlay .sheet { background: var(--panel); border: 1px solid var(--border);
  border-radius: 18px 18px 0 0; padding: 20px; width: 100%; max-width: 720px;
  display: flex; flex-direction: column; gap: 12px; max-height: 90dvh; overflow-y: auto; }
.sheet h2 { margin: 0; }
.rating { display: flex; gap: 8px; flex-wrap: wrap; }
.rating button { flex: 1; min-width: 56px; min-height: 46px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); }
.rating button.sel { border-color: var(--accent); color: var(--accent-2); }

/* footer legal persistente */
footer.legal { padding: 8px 14px; background: var(--panel); border-top: 1px solid var(--border);
  font-size: 11.5px; color: var(--muted); text-align: center; }

/* --- UX móvil: maximizar espacio con el teclado abierto --- */
/* Campo de un widget enfocado: ocultar el composer libre (más espacio para escribir) */
#app.widget-focused #composer { display: none !important; }
/* Teclado abierto o campo de widget: header al mínimo (libera espacio arriba) */
#app.kbd header.topbar,
#app.widget-focused header.topbar { padding: 5px 12px; }
#app.kbd .topbar .brand small,
#app.widget-focused .topbar .brand small { display: none; }
#app.kbd .saldo-wrap,
#app.widget-focused .saldo-wrap { display: none; }

/* Botón flotante "ir al fondo" (aparece solo si subiste a leer) */
.scroll-to-bottom {
  position: absolute; bottom: 76px; right: 14px; width: 42px; height: 42px;
  border-radius: 50%; background: var(--accent); color: #1a1207; border: none;
  font-size: 20px; display: none; align-items: center; justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.45); z-index: 10; }
.scroll-to-bottom.visible { display: flex; }
.scroll-to-bottom:active { transform: scale(.94); }
