/* =========================================================
   SCARLET — Asistente (botón flotante + panel de chat)
   Visible en todas las vistas del alumno. Estética de la app móvil.
   ========================================================= */

.stw {
  --stw-bg: #F7F2EA;
  --stw-surface: #FFFFFF;
  --stw-ink: #2A221C;
  --stw-ink-2: #5E554D;
  --stw-ink-3: #8A8077;
  --stw-line: rgba(42, 34, 28, .09);
  --stw-orange: #E8770F;
  --stw-btn: #BD5807;
  --stw-btn-hover: #A44C05;
  --stw-orange-soft: #FCE9D6;
  --stw-orange-text: #A44C05;
  --stw-lilac: #B9A3D6;
  --stw-avatar: linear-gradient(135deg, #F4EEFA 0%, #FBEDEE 100%);
  --stw-err: #B03E33;
  --stw-err-soft: #FBEAE7;
  --stw-shadow: 0 30px 70px -24px rgba(60, 40, 20, .38), 0 6px 18px rgba(60, 40, 20, .08);
  --stw-shadow-sm: 0 2px 10px rgba(60, 40, 20, .06);
  --stw-fab-shadow: 0 14px 30px -10px rgba(189, 88, 7, .65);
  --stw-ease: cubic-bezier(.22, 1, .36, 1);
  font-family: "Outfit", system-ui, sans-serif;
}

[data-theme="dark"] .stw {
  --stw-bg: #1F1A17;
  --stw-surface: #2B2420;
  --stw-ink: #F4EDE4;
  --stw-ink-2: #C3B8AD;
  --stw-ink-3: #958A80;
  --stw-line: rgba(244, 237, 228, .09);
  --stw-orange: #F08A2C;
  --stw-orange-soft: rgba(240, 138, 44, .16);
  --stw-orange-text: #F59A47;
  --stw-lilac: #A58FC4;
  --stw-avatar: linear-gradient(135deg, #2D2533 0%, #33262A 100%);
  --stw-err: #F0938A;
  --stw-err-soft: rgba(240, 147, 138, .12);
  --stw-shadow: 0 30px 70px -24px rgba(0, 0, 0, .7), 0 6px 18px rgba(0, 0, 0, .3);
  --stw-shadow-sm: 0 2px 10px rgba(0, 0, 0, .25);
}

.stw [hidden] { display: none !important; }
.stw svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.stw button { font: inherit; }
.stw :focus-visible { outline: 3px solid var(--stw-orange); outline-offset: 2px; }

/* El widget se incluye tanto en páginas del alumno (que ya traen su
   propia utilidad .visually-hidden vía layout.css) como en el landing
   público (que no la define) -- se declara aquí, scoped a .stw, para
   que el widget sea autocontenido y no dependa de que la página
   anfitriona la tenga. Sin esto, el <label> del input ("Escribe tu
   pregunta...") y las etiquetas "Tú:"/"Asistente:" se renderizan como
   texto visible y rompen el layout de la barra de escritura. */
.stw .visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Botón flotante ---------- */
.stw-fab {
  position: fixed; right: max(20px, env(safe-area-inset-right)); bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 1150;
  display: grid; place-items: center; width: 60px; height: 60px;
  border: 0; border-radius: 50%; cursor: pointer;
  background: var(--stw-btn); color: #fff; box-shadow: var(--stw-fab-shadow);
  transition: transform .25s var(--stw-ease), background-color .2s, opacity .2s;
}
.stw-fab:hover { background: var(--stw-btn-hover); transform: translateY(-2px); }
.stw-fab:active { transform: scale(.95); }
.stw-fab:focus-visible { outline-offset: 4px; border-radius: 50%; }
.stw-fab__ico { grid-area: 1 / 1; width: 26px; height: 26px; transition: transform .3s var(--stw-ease), opacity .2s; }
.stw-fab__ico--open { fill: currentColor !important; stroke: none !important; }
.stw-fab__ico--close { opacity: 0; transform: rotate(-90deg) scale(.6); }
.stw.is-open .stw-fab__ico--open { opacity: 0; transform: rotate(90deg) scale(.6); }
.stw.is-open .stw-fab__ico--close { opacity: 1; transform: none; }

.stw-fab__tip {
  position: absolute; right: calc(100% + 12px); top: 50%;
  padding: 7px 12px; border-radius: 10px; white-space: nowrap;
  background: var(--stw-ink); color: var(--stw-bg); font-size: 13.5px; font-weight: 600;
  opacity: 0; transform: translate(6px, -50%); pointer-events: none;
  transition: opacity .2s, transform .2s var(--stw-ease);
}
.stw-fab:hover .stw-fab__tip, .stw-fab:focus-visible .stw-fab__tip { opacity: 1; transform: translate(0, -50%); }
.stw.is-open .stw-fab__tip { display: none; }

/* Punto de aviso cuando llega una respuesta con el panel cerrado */
.stw-fab.has-unread::after {
  content: ""; position: absolute; top: 4px; right: 4px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--stw-lilac); box-shadow: 0 0 0 3px var(--stw-btn);
}

/* Pausado durante el tour de bienvenida */
.stw.is-paused .stw-fab { opacity: 0; pointer-events: none; }

/* ---------- Panel ---------- */
.stw-backdrop {
  position: fixed; inset: 0; z-index: 1140;
  background: rgba(20, 15, 10, .38);
  animation: stw-fade .25s ease both;
}
.stw-panel {
  position: fixed; z-index: 1145;
  right: max(20px, env(safe-area-inset-right)); bottom: calc(max(20px, env(safe-area-inset-bottom)) + 76px);
  width: min(400px, calc(100vw - 40px)); height: min(640px, calc(100vh - 140px));
  display: flex; flex-direction: column; overflow: hidden;
  border-radius: 28px; background: var(--stw-bg); color: var(--stw-ink);
  box-shadow: var(--stw-shadow); border: 1px solid var(--stw-line);
  transform-origin: bottom right;
  animation: stw-pop .3s var(--stw-ease) both;
}
@keyframes stw-pop { from { opacity: 0; transform: translateY(12px) scale(.96); } }
@keyframes stw-fade { from { opacity: 0; } }

.stw-handle { display: none; }

.stw-head {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 16px 14px 18px; background: var(--stw-surface);
  border-bottom: 1px solid var(--stw-line);
}
.stw-avatar {
  display: grid; place-items: center; width: 44px; height: 44px; flex: none;
  border-radius: 14px; background: var(--stw-avatar); color: var(--stw-orange);
}
.stw-avatar svg { width: 22px; height: 22px; fill: currentColor; stroke: none; }
.stw-head__text { flex: 1; min-width: 0; }
.stw-head h2 { margin: 0; font-size: 18px; font-weight: 700; line-height: 1.2; letter-spacing: -.01em; color: var(--stw-ink); }
.stw-head p { margin: 2px 0 0; font-size: 13.5px; color: var(--stw-ink-2); }

.stw-icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 50%; cursor: pointer;
  background: transparent; color: var(--stw-ink-3);
  transition: background-color .2s, color .2s;
}
.stw-icon-btn svg { width: 18px; height: 18px; }
.stw-icon-btn:hover { background: var(--stw-bg); color: var(--stw-ink); }
.stw-icon-btn--close { background: var(--stw-bg); color: var(--stw-ink); }
.stw-icon-btn--close svg { width: 20px; height: 20px; }

/* ---------- Conversación ---------- */
.stw-body {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 18px 16px 8px; scroll-behavior: smooth;
}
.stw-stamp {
  margin: 0 0 14px; text-align: center;
  font-size: 12px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; color: var(--stw-ink-3);
}
.stw-log, .stw-intro { display: flex; flex-direction: column; gap: 12px; }
.stw-intro { margin-bottom: 12px; }
.stw-msg { display: flex; flex-direction: column; max-width: 86%; animation: stw-in .3s var(--stw-ease) both; }
@keyframes stw-in { from { opacity: 0; transform: translateY(6px); } }
.stw-msg--assistant { align-self: flex-start; }
.stw-msg--user { align-self: flex-end; align-items: flex-end; }
.stw-bubble {
  margin: 0; padding: 12px 16px; border-radius: 22px;
  font-size: 15.5px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
}
.stw-msg--assistant .stw-bubble { background: var(--stw-surface); color: var(--stw-ink); border-bottom-left-radius: 8px; box-shadow: var(--stw-shadow-sm); }
.stw-msg--user .stw-bubble { background: var(--stw-btn); color: #fff; border-bottom-right-radius: 8px; }

/* Acciones (mazos sugeridos) dentro de la burbuja del asistente */
.stw-actions { display: grid; gap: 8px; margin-top: 8px; }
.stw-action {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-radius: 16px;
  background: var(--stw-orange-soft); color: var(--stw-orange-text);
  text-decoration: none; font-weight: 700; font-size: 14.5px; line-height: 1.25;
  transition: filter .2s, transform .2s var(--stw-ease);
}
.stw-action:hover { filter: brightness(.97); transform: translateX(2px); color: var(--stw-orange-text); text-decoration: none; }
.stw-action__text { flex: 1; min-width: 0; display: grid; gap: 2px; }
.stw-action small { font-weight: 500; font-size: 12.5px; opacity: .85; }
.stw-action svg { width: 18px; height: 18px; flex: none; }

/* Escribiendo… */
.stw-typing { display: inline-flex; gap: 5px; padding: 16px 18px; }
.stw-typing i { width: 8px; height: 8px; border-radius: 50%; background: var(--stw-lilac); animation: stw-dot 1.2s ease-in-out infinite; }
.stw-typing i:nth-child(2) { animation-delay: .15s; }
.stw-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes stw-dot { 0%, 60%, 100% { opacity: .35; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-4px); } }

.stw-error {
  margin: 0 16px 8px; padding: 10px 14px; border-radius: 14px;
  background: var(--stw-err-soft); color: var(--stw-err); font-size: 14px;
}

/* ---------- Sugerencias ---------- */
.stw-chips {
  display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x proximity;
  padding: 8px 16px 10px; scrollbar-width: none;
  -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent);
          mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent);
}
.stw-chips::-webkit-scrollbar { display: none; }
.stw-chip {
  flex: none; scroll-snap-align: start;
  min-height: 40px; padding: 8px 16px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--stw-line); background: var(--stw-surface); color: var(--stw-ink);
  font-size: 14.5px; font-weight: 600; white-space: nowrap;
  transition: border-color .2s, background-color .2s;
}
.stw-chip:hover { border-color: var(--stw-orange); }
.stw-chip:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Entrada ---------- */
.stw-form {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 6px 16px max(16px, env(safe-area-inset-bottom));
}
.stw-input {
  flex: 1; min-height: 50px; max-height: 140px; resize: none;
  padding: 13px 18px; border-radius: 26px;
  border: 1.5px solid var(--stw-line); background: var(--stw-surface); color: var(--stw-ink);
  font: inherit; font-size: 16px; line-height: 1.4;
  transition: border-color .2s, box-shadow .2s;
}
.stw-input::placeholder { color: var(--stw-ink-3); }
.stw-input:focus { outline: none; border-color: var(--stw-orange); box-shadow: 0 0 0 4px color-mix(in srgb, var(--stw-orange) 18%, transparent); }
.stw-send {
  display: grid; place-items: center; width: 50px; height: 50px; flex: none;
  border: 0; border-radius: 50%; cursor: pointer;
  background: var(--stw-btn); color: #fff;
  transition: background-color .2s, transform .2s var(--stw-ease), opacity .2s;
}
.stw-send svg { width: 22px; height: 22px; stroke-width: 2.4; }
.stw-send:hover { background: var(--stw-btn-hover); }
.stw-send:active { transform: scale(.94); }
.stw-send:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Móvil: hoja inferior ---------- */
@media (max-width: 640px) {
  .stw-fab { right: 16px; bottom: max(16px, env(safe-area-inset-bottom)); width: 56px; height: 56px; }
  .stw-fab__tip { display: none; }
  .stw.is-open .stw-fab { opacity: 0; pointer-events: none; }
  .stw-panel {
    left: 0; right: 0; bottom: 0; width: 100%;
    height: min(88vh, 88dvh); border-radius: 28px 28px 0 0; border-bottom: 0;
    transform-origin: bottom center;
    animation: stw-sheet .35s var(--stw-ease) both;
  }
  @keyframes stw-sheet { from { transform: translateY(100%); } }
  .stw-handle { display: block; width: 44px; height: 5px; margin: 10px auto 0; border-radius: 99px; background: var(--stw-line); background: color-mix(in srgb, var(--stw-ink) 18%, transparent); position: absolute; left: 50%; translate: -50% 0; top: 0; }
  .stw-head { padding-top: 24px; }
}
@media (min-width: 641px) { .stw-backdrop { display: none; } }

@media (prefers-reduced-motion: reduce) {
  .stw *, .stw *::before, .stw *::after { animation: none !important; transition: none !important; }
}
