/* ============================================================
   Kak Sani — Floating Chatbot untuk LearnPress
   Semua selector di-scope di bawah .ks-fw-root agar TIDAK merusak
   style WordPress / LearnPress. Variabel di-namespace --ksfw-*.
   ============================================================ */

.ks-fw-root {
  --ksfw-blue: #3B82F6;
  --ksfw-sky: #DDF3FF;
  --ksfw-green: #22C55E;
  --ksfw-card: #FFFFFF;
  --ksfw-text: #1E293B;
  --ksfw-muted: #64748B;
  --ksfw-border: #E2E8F0;
  --ksfw-danger: #DC2626;
  --ksfw-radius: 18px;
  --ksfw-shadow: 0 18px 45px rgba(30, 41, 59, 0.22);
  --ksfw-font: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;

  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 2147483000; /* sangat tinggi agar di atas elemen LearnPress */
  font-family: var(--ksfw-font);
  color: var(--ksfw-text);
  line-height: 1.5;
}

.ks-fw-root *,
.ks-fw-root *::before,
.ks-fw-root *::after {
  box-sizing: border-box;
}

/* Reset img: cegah tema WP menimpa ukuran/tampilan ikon & avatar
   (mis. max-width:100%, height:auto, border, box-shadow global). */
.ks-fw-root img {
  max-width: none;
  height: auto;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  vertical-align: middle;
  display: block;
}

/* ---------- Floating Action Button (FAB) ---------- */
.ks-fw-root .ks-fab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  padding: 0;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.ks-fw-root .ks-fab-avatar {
  width: 66px;
  height: 66px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--ksfw-card);
  border: 3px solid var(--ksfw-blue);
  box-shadow: var(--ksfw-shadow);
  display: grid;
  place-items: center;
  overflow: hidden;
  transition: transform 0.2s ease;
  animation: ksfw-float 3s ease-in-out infinite;
}

.ks-fw-root .ks-fab-avatar img {
  width: 82%;
  height: 82%;
  object-fit: contain;
  pointer-events: none;
}

.ks-fw-root .ks-fab:hover .ks-fab-avatar,
.ks-fw-root .ks-fab:focus-visible .ks-fab-avatar {
  transform: scale(1.06);
}

.ks-fw-root .ks-fab-bubble {
  position: relative;
  background: var(--ksfw-card);
  color: var(--ksfw-text);
  font-weight: 700;
  font-size: 14px;
  padding: 9px 14px;
  border-radius: 14px;
  box-shadow: var(--ksfw-shadow);
  white-space: nowrap;
  animation: ksfw-pop-in 0.4s ease both;
}

.ks-fw-root .ks-fab-bubble::after {
  content: "";
  position: absolute;
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
  border-left: 8px solid var(--ksfw-card);
}

/* Sembunyikan FAB saat panel terbuka */
.ks-fw-root.ks-fw-open .ks-fab {
  display: none;
}

/* ---------- Panel percakapan (compact) ---------- */
/* Pastikan atribut [hidden] selalu menang atas display:flex panel,
   agar chatbot TERTUTUP saat halaman dimuat (baru terbuka setelah klik FAB).
   Beberapa tema memaksa display pada elemen, jadi dipertegas di sini. */
.ks-fw-root .ks-fw-panel[hidden] {
  display: none !important;
}

.ks-fw-root .ks-fw-panel {
  width: 360px;
  max-width: calc(100vw - 32px);
  height: 520px;
  max-height: calc(100vh - 110px);
  display: flex;
  flex-direction: column;
  background: var(--ksfw-card);
  border: 1px solid var(--ksfw-border);
  border-radius: var(--ksfw-radius);
  box-shadow: var(--ksfw-shadow);
  overflow: hidden;
  animation: ksfw-panel-in 0.22s ease both;
}

.ks-fw-root .ks-fw-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  background: linear-gradient(135deg, var(--ksfw-blue), #2563EB);
  color: #fff;
}

.ks-fw-root .ks-fw-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.ks-fw-root .ks-fw-brand-avatar {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8); /* lebih opaque agar avatar Kak Sani jelas di header */
  display: grid;
  place-items: center;
  overflow: hidden;
}

.ks-fw-root .ks-fw-brand-avatar img {
  width: 84%;
  height: 84%;
  object-fit: contain;
}

.ks-fw-root .ks-fw-brand-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.ks-fw-root .ks-fw-title {
  font-weight: 800;
  font-size: 15px;
}

.ks-fw-root .ks-fw-status {
  font-size: 12px;
  opacity: 0.9;
}

.ks-fw-root .ks-fw-close {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease;
  /* pusatkan glyph × agar tidak melenceng dari lingkaran */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  -webkit-appearance: none;
  appearance: none;
}

.ks-fw-root .ks-fw-close:hover {
  background: rgba(255, 255, 255, 0.32);
}

/* ---------- Body / daftar pesan ---------- */
.ks-fw-root .ks-fw-body {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 12px;
  background: var(--ksfw-sky);
}

.ks-fw-root .ks-date-divider {
  text-align: center;
  font-size: 11px;
  color: var(--ksfw-muted);
  margin-bottom: 8px;
}

.ks-fw-root .ks-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-right: 4px;
  scroll-behavior: smooth;
}

.ks-fw-root .ks-message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
}

.ks-fw-root .ks-message-row.user {
  flex-direction: row-reverse;
}

.ks-fw-root .ks-bot-avatar {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--ksfw-card);
  border: 1px solid var(--ksfw-border);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.ks-fw-root .ks-bot-avatar img {
  width: 84%;
  height: 84%;
  object-fit: contain;
}

.ks-fw-root .ks-message-group {
  display: flex;
  flex-direction: column;
  max-width: 78%;
}

.ks-fw-root .ks-bubble {
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 14px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: normal;
}

.ks-fw-root .ks-bubble.bot {
  background: var(--ksfw-card);
  border: 1px solid var(--ksfw-border);
  border-bottom-left-radius: 4px;
}

.ks-fw-root .ks-bubble.user {
  background: var(--ksfw-blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ks-fw-root .ks-bubble.error {
  background: #FEF2F2;
  border: 1px solid #FCA5A5;
  color: var(--ksfw-danger);
}

.ks-fw-root .ks-message-row.user .ks-message-group {
  align-items: flex-end;
}

.ks-fw-root .ks-message-time {
  font-size: 10px;
  color: var(--ksfw-muted);
  margin-top: 3px;
  padding: 0 4px;
}

/* Typing indicator */
.ks-fw-root .ks-typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.ks-fw-root .ks-typing-text {
  font-size: 13px;
  color: var(--ksfw-muted);
}

.ks-fw-root .ks-typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ksfw-blue);
  animation: ksfw-blink 1.2s infinite ease-in-out both;
}

.ks-fw-root .ks-typing-dot:nth-child(3) { animation-delay: 0.2s; }
.ks-fw-root .ks-typing-dot:nth-child(4) { animation-delay: 0.4s; }

/* ---------- Quick replies (pertanyaan pemantik) ---------- */
.ks-fw-root .ks-quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.ks-fw-root .ks-chip {
  background: var(--ksfw-card);
  border: 1px solid var(--ksfw-blue);
  color: var(--ksfw-blue);
  font-weight: 700;
  font-size: 12.5px;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.ks-fw-root .ks-chip:hover {
  background: var(--ksfw-blue);
  color: #fff;
}

.ks-fw-root .ks-chip:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- Error banner ---------- */
.ks-fw-root .ks-error {
  padding: 8px 12px;
  background: #FEF2F2;
  color: var(--ksfw-danger);
  font-size: 13px;
  border-top: 1px solid #FCA5A5;
}

/* ---------- Input area ---------- */
.ks-fw-root .ks-input-area {
  display: flex;
  align-items: flex-start; /* tombol tetap di atas, tidak turun saat textarea tumbuh */
  gap: 8px;
  padding: 10px 12px;
  background: var(--ksfw-card);
  border-top: 1px solid var(--ksfw-border);
}

.ks-fw-root .ks-input-area textarea {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--ksfw-border);
  border-radius: 12px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  line-height: 22px;      /* konsisten dengan tinggi awal agar 1 baris = 42px */
  color: var(--ksfw-text);
  background: #fff;
  height: 42px;           /* tinggi awal pasti = sama dengan tombol, sebelum autosize JS */
  min-height: 42px;
  max-height: 110px;
  overflow-y: auto;
}

.ks-fw-root .ks-input-area textarea:focus {
  outline: 2px solid var(--ksfw-blue);
  outline-offset: 0;
  border-color: var(--ksfw-blue);
}

.ks-fw-root .ks-send-button {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 12px;
  border: 0;
  background: var(--ksfw-blue);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  transition: background 0.15s ease, transform 0.1s ease;
}

.ks-fw-root .ks-send-button img {
  width: 20px;
  height: 20px;
  max-width: 20px;
  display: block;
  margin: 0;
  pointer-events: none;
}

.ks-fw-root .ks-send-button:hover:not(:disabled) {
  background: #2563EB;
}

.ks-fw-root .ks-send-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.ks-fw-root .ks-click-pop:active {
  transform: scale(0.94);
}

/* ---------- Screen-reader only ---------- */
.ks-fw-root .ks-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Animasi ---------- */
@keyframes ksfw-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes ksfw-pop-in {
  from { opacity: 0; transform: translateY(6px) scale(0.9); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes ksfw-panel-in {
  from { opacity: 0; transform: translateY(16px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes ksfw-blink {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ---------- Mobile ---------- */
@media (max-width: 480px) {
  .ks-fw-root {
    right: 12px;
    bottom: 12px;
  }

  .ks-fw-root .ks-fw-panel {
    width: calc(100vw - 24px);
    height: calc(100vh - 90px);
    max-height: calc(100vh - 90px);
  }

  .ks-fw-root .ks-fab-avatar {
    width: 58px;
    height: 58px;
  }

  .ks-fw-root .ks-fab-bubble {
    font-size: 13px;
    padding: 7px 11px;
  }
}

/* Hormati preferensi reduce-motion */
@media (prefers-reduced-motion: reduce) {
  .ks-fw-root .ks-fab-avatar,
  .ks-fw-root .ks-fab-bubble,
  .ks-fw-root .ks-fw-panel,
  .ks-fw-root .ks-typing-dot {
    animation: none;
  }
}
