#keni-widget {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.5rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#keni-bubble {
  background: #141414;
  border: 1px solid #a3e635;
  border-radius: 12px 12px 0 12px;
  padding: 0.625rem 0.875rem;
  font-size: 0.8rem;
  line-height: 1.4;
  color: #fff;
  max-width: 190px;
  text-align: right;
  opacity: 0;
  transform: translateY(6px) scale(0.95);
  transform-origin: bottom right;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

#keni-bubble.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

#keni-open-chat {
  display: block;
  margin-top: 0.35rem;
  color: #a3e635;
  font-size: 0.75rem;
  font-weight: 500;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-align: right;
  width: 100%;
}

#keni-open-chat:hover {
  text-decoration: underline;
}

#keni-img {
  width: 110px;
  height: 110px;
  object-fit: contain;
  display: block;
}

.keni-float  { animation: keni-float  3s   ease-in-out infinite; }
.keni-bounce { animation: keni-bounce 0.5s ease        forwards; }
.keni-shake  { animation: keni-shake  0.5s ease        forwards; }
.keni-fadein { animation: keni-fadein 1.5s ease        forwards; }

@keyframes keni-float {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-8px); }
}

@keyframes keni-bounce {
  0%   { transform: translateY(0)   scale(1); }
  30%  { transform: translateY(-12px) scale(1.06); }
  60%  { transform: translateY(-5px)  scale(1.02); }
  80%  { transform: translateY(-8px)  scale(1.04); }
  100% { transform: translateY(0)   scale(1); }
}

@keyframes keni-shake {
  0%, 100% { transform: rotate(0deg)  scale(1); }
  15%  { transform: rotate(-8deg) scale(1.05); }
  30%  { transform: rotate(8deg)  scale(1.05); }
  45%  { transform: rotate(-6deg); }
  60%  { transform: rotate(6deg); }
  75%  { transform: rotate(-3deg); }
  90%  { transform: rotate(3deg); }
}

@keyframes keni-fadein {
  from { opacity: 0.3; }
  to   { opacity: 1; }
}

/* ── Chat widget ─────────────────────────────────────────────────────────── */

#keni-chat {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  right: 0;
  width: 300px;
  max-height: 420px;
  background: #141414;
  border: 1px solid #a3e635;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

#keni-chat[hidden] { display: none; }

#keni-chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 0.875rem;
  background: #1a1a1a;
  border-bottom: 1px solid #2a2a2a;
  font-size: 0.78rem;
  font-weight: 600;
  color: #a3e635;
}

#keni-close-chat {
  background: none;
  border: none;
  color: #aaa;
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 4px;
  line-height: 1;
  border-radius: 4px;
}

#keni-close-chat:hover { color: #fff; }

#keni-close-chat:focus-visible {
  outline: 2px solid #a3e635;
  outline-offset: 2px;
}

#keni-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}

.keni-msg {
  max-width: 85%;
  padding: 0.45rem 0.7rem;
  border-radius: 10px;
  font-size: 0.78rem;
  line-height: 1.45;
  word-break: break-word;
}

.keni-msg-user {
  align-self: flex-end;
  background: #a3e635;
  color: #000;
  border-bottom-right-radius: 2px;
}

.keni-msg-assistant {
  align-self: flex-start;
  background: #222;
  color: #e5e5e5;
  border-bottom-left-radius: 2px;
}

.keni-typing { opacity: 0.5; font-style: italic; }

/* El feedback vive DENTRO del recuadro del mensaje (esquina inferior derecha),
   como un par de íconos casi invisibles — no emojis — que solo se insinúan al
   pasar el mouse por el mensaje. */
.keni-msg-assistant { position: relative; padding-bottom: 1.05rem; }

.keni-feedback {
  position: absolute;
  right: 0.45rem;
  bottom: 0.3rem;
  display: flex;
  gap: 0.15rem;
}

.keni-feedback-btn {
  background: none;
  border: none;
  padding: 0.15rem;
  min-width: 20px;
  min-height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #e5e5e5;
  cursor: pointer;
  opacity: 0.16;
  transition: opacity 0.15s ease;
}

.keni-feedback-btn svg { width: 10px; height: 10px; display: block; }

.keni-feedback-down svg { transform: scaleY(-1); }

/* Se insinúan al pasar el mouse por el mensaje completo, no solo por el ícono */
.keni-msg-assistant:hover .keni-feedback-btn:not(:disabled),
.keni-feedback-btn:hover:not(:disabled),
.keni-feedback-btn:focus-visible:not(:disabled) {
  opacity: 0.7;
}

.keni-feedback-btn:focus-visible {
  outline: 2px solid #a3e635;
  outline-offset: 1px;
  opacity: 0.9;
}

.keni-feedback-btn:disabled { cursor: default; opacity: 0.1; }

#keni-form {
  display: flex;
  border-top: 1px solid #2a2a2a;
  padding: 0.5rem;
  gap: 0.4rem;
}

#keni-input {
  flex: 1;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 8px;
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  color: #fff;
  outline: none;
}

#keni-input:focus { border-color: #a3e635; }

#keni-input:focus-visible {
  border-color: #a3e635;
  outline: 2px solid #a3e635;
  outline-offset: 1px;
}

#keni-form button[type="submit"] {
  background: #a3e635;
  border: none;
  border-radius: 8px;
  color: #000;
  font-weight: 700;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
}

#keni-form button[type="submit"]:hover { background: #b5f042; }

#keni-form button[type="submit"]:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

#keni-open-chat:focus-visible {
  outline: 2px solid #a3e635;
  outline-offset: 2px;
  border-radius: 2px;
}

/* ── Auth panel (acceso familiar vía OTP) ────────────────────────────────── */

#keni-auth-panel {
  padding: 0.75rem;
  background: #141414;
  border-top: 1px solid #2a2a2a;
}

.keni-auth-desc {
  font-size: 0.75rem;
  color: #aaa;
  margin: 0 0 0.6rem;
}

.keni-auth-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.5rem;
}

.keni-auth-field label {
  font-size: 0.72rem;
  color: #ccc;
}

/* Especificidad alta (#id + class + element) para ganar a Tailwind/reset global */
#keni-auth-panel .keni-auth-field input {
  background: #1e1e1e;
  border: 1px solid #444;
  border-radius: 8px;
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  color: #f0f0f0;
  outline: none;
}

#keni-auth-panel .keni-auth-field input:focus { border-color: #a3e635; }

#keni-auth-panel .keni-auth-field input:focus-visible {
  border-color: #a3e635;
  outline: 2px solid #a3e635;
  outline-offset: 1px;
}

#keni-auth-panel .keni-auth-field input[aria-invalid="true"] { border-color: #f87171; }

.keni-auth-error {
  font-size: 0.7rem;
  color: #f87171;
  margin: 0;
}

.keni-auth-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.keni-auth-submit {
  background: #a3e635;
  border: none;
  border-radius: 8px;
  color: #000;
  font-weight: 700;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  font-size: 0.78rem;
}

.keni-auth-submit:hover { background: #b5f042; }

.keni-auth-submit:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.keni-auth-link {
  background: none;
  border: none;
  color: #777;
  font-size: 0.72rem;
  cursor: pointer;
  padding: 0;
}

.keni-auth-link:hover { color: #ccc; }

.keni-auth-link:focus-visible {
  outline: 2px solid #a3e635;
  outline-offset: 2px;
  border-radius: 2px;
}

/* Botón de acceso familiar (pie del chat) — desktop: discreto */
.keni-family-link {
  display: block;
  width: 100%;
  background: none;
  border: none;
  border-top: 1px solid #1e1e1e;
  color: #3d3d3d;
  font-size: 0.6rem;
  letter-spacing: 0.01em;
  padding: 0.28rem 0.875rem;
  cursor: pointer;
  text-align: left;
  transition: color 0.2s;
}

.keni-family-link:hover { color: #888; }

.keni-family-link:focus-visible {
  outline: 2px solid #a3e635;
  outline-offset: -2px;
  color: #a3e635;
}

/* ── Responsive ──────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  #keni-widget { bottom: 1rem; right: 1rem; }
  #keni-img    { width: 64px; height: 64px; }
  #keni-bubble { max-width: 170px; font-size: 0.75rem; }

  /* Bottom sheet: fijo al viewport, full-width, esquinas redondeadas arriba */
  #keni-chat {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    max-height: 80vh;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.7);
    background-color: #1a1a1a;
    color: #e5e5e5;
    z-index: 10000;
  }

  /* Pill handle — indicador visual de bottom sheet */
  #keni-chat::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: #444;
    border-radius: 2px;
    margin: 10px auto 4px;
    flex-shrink: 0;
  }

  /* Forzar fondo y texto en todos los sub-elementos del chat en mobile */
  #keni-chat-header {
    background-color: #242424;
    color: #a3e635;
  }

  #keni-messages {
    background-color: #1a1a1a;
  }

  .keni-msg-assistant {
    background-color: #2a2a2a;
    color: #e5e5e5;
  }

  .keni-msg-user {
    background-color: #a3e635;
    color: #000;
  }

  #keni-form {
    background-color: #1a1a1a;
  }

  #keni-input {
    background-color: #2a2a2a;
    color: #e5e5e5;
    border-color: #444;
    -webkit-text-fill-color: #e5e5e5;
  }

  #keni-input::placeholder {
    color: #888;
    -webkit-text-fill-color: #888;
  }

  /* En mobile el botón familiar es más visible */
  .keni-family-link {
    color: #666;
    font-size: 0.7rem;
    padding: 0.4rem 0.875rem;
    border-top-color: #2a2a2a;
  }

  .keni-family-link:hover { color: #a3e635; }

  /* Auth inputs en mobile también con override -webkit para iOS */
  #keni-auth-panel .keni-auth-field input {
    background-color: #2a2a2a;
    -webkit-text-fill-color: #f0f0f0;
    border-color: #555;
  }
}
