/* ==========================================================================
   KrishiMitra AI Chat Widget — Production Styles (v2.1)
   ========================================================================== */

#km-widget {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  font-family: 'Plus Jakarta Sans', 'Poppins', 'Noto Sans Devanagari', -apple-system, BlinkMacSystemFont, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

/* ── Floating Launcher Pill / Orb ─────────────────────────────────────────── */
#km-launcher-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

#km-launcher-pill {
  background: #132217;
  color: #fff;
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(12, 38, 23, 0.24);
  border: 1px solid rgba(255, 255, 255, 0.14);
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.2s, transform 0.2s;
}

#km-launcher-pill:hover {
  transform: translateY(-2px);
}

#km-launcher-pill.km-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(8px);
}

#km-launcher {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1b4d2e 0%, #2e7d32 100%);
  border: 2px solid rgba(255, 255, 255, 0.25);
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(27, 77, 46, 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  color: #fff;
  transition: transform 0.22s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.22s;
}

#km-launcher::after {
  content: '';
  position: absolute;
  inset: -5px;
  border-radius: 50%;
  border: 2px solid rgba(46, 125, 50, 0.35);
  animation: km-ring 2.6s ease-out infinite;
  pointer-events: none;
}

#km-launcher.km-active::after {
  display: none;
}

@keyframes km-ring {
  0%   { transform: scale(0.95); opacity: 0.75; }
  75%  { transform: scale(1.24); opacity: 0; }
  100% { transform: scale(1.24); opacity: 0; }
}

#km-launcher:hover {
  transform: scale(1.06);
  box-shadow: 0 14px 32px rgba(27, 77, 46, 0.52);
}

#km-launcher svg {
  width: 26px;
  height: 26px;
}

#km-launcher .km-icon-close { display: none; }
#km-launcher.km-active .km-icon-chat { display: none; }
#km-launcher.km-active .km-icon-close { display: block; }

.km-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  background: #f59e0b;
  color: #132217;
  font-size: 11px;
  font-weight: 800;
  min-width: 21px;
  height: 21px;
  padding: 0 5px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff;
}

/* ── Chat Panel ───────────────────────────────────────────────────────────── */
#km-panel {
  position: absolute;
  bottom: 78px;
  right: 0;
  width: 424px;
  height: min(620px, calc(100vh - 110px));
  background: #ffffff;
  border-radius: 22px;
  box-shadow: 0 24px 60px rgba(12, 38, 23, 0.24), 0 0 0 1px rgba(20, 58, 35, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px) scale(0.98);
  transform-origin: bottom right;
  transition: opacity 0.14s ease, transform 0.16s cubic-bezier(0.22, 1, 0.36, 1), width 0.2s ease, height 0.2s ease;
}

#km-panel.km-open {
  opacity: 1;
  pointer-events: all;
  transform: translateY(0) scale(1);
}

#km-panel.km-expanded {
  width: min(560px, calc(100vw - 48px));
  height: min(760px, calc(100vh - 108px));
}

/* ── Header ───────────────────────────────────────────────────────────────── */
#km-header {
  background: linear-gradient(135deg, #123520 0%, #1f5c37 65%, #2e7d32 100%);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: #fff;
  flex-shrink: 0;
}

.km-head-left {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.km-bot-avatar {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
  flex-shrink: 0;
}

#km-header-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#km-title {
  font-size: 15.5px;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

#km-status-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: rgba(240, 253, 244, 0.88);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.km-status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #fbbf24;
  flex-shrink: 0;
}

.km-status-dot.is-online {
  background: #4ade80;
  box-shadow: 0 0 0 3px rgba(74, 222, 128, 0.24);
}

.km-status-dot.is-offline {
  background: #f87171;
}

.km-head-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Language Switcher */
.km-chat-lang-select {
  background: rgba(0, 0, 0, 0.28);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 11.5px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  max-width: 104px;
}

.km-chat-lang-select option {
  background: #143a23;
  color: #ffffff;
}

.km-icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  border: none;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s;
}

.km-icon-btn:hover {
  background: rgba(255, 255, 255, 0.26);
}

.km-icon-btn svg {
  width: 15px;
  height: 15px;
}

/* ── Messages Area ────────────────────────────────────────────────────────── */
#km-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #f5f8f4;
  scroll-behavior: smooth;
}

#km-messages::-webkit-scrollbar { width: 5px; }
#km-messages::-webkit-scrollbar-thumb { background: #cbd9cc; border-radius: 99px; }

.km-msg-wrap {
  display: flex;
  flex-direction: column;
  max-width: 88%;
  animation: km-fadein 0.14s ease forwards;
}

.km-msg-wrap--user {
  align-self: flex-end;
  align-items: flex-end;
}

.km-msg-wrap--assistant {
  align-self: flex-start;
  align-items: flex-start;
}

@keyframes km-fadein {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.km-msg {
  border-radius: 16px;
  padding: 11px 14px;
  font-size: 13.5px;
  line-height: 1.6;
  word-break: break-word;
}

.km-msg-user {
  background: linear-gradient(135deg, #1b4d2e, #2e7d32);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 12px rgba(27, 77, 46, 0.18);
}

.km-msg-assistant {
  background: #ffffff;
  color: #1b2d1f;
  border-bottom-left-radius: 4px;
  border: 1px solid rgba(20, 58, 35, 0.08);
  box-shadow: 0 2px 8px rgba(12, 38, 23, 0.05);
}

.km-msg-warning {
  border: 1px solid #fecaca;
  border-left: 4px solid #dc2626;
  background: #fff9f9;
}

.km-safety-banner {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 700;
  color: #b91c1c;
  background: #fee2e2;
  padding: 5px 9px;
  border-radius: 8px;
  margin-bottom: 8px;
}

/* Markdown formatting inside bubbles */
.km-md p { margin: 0 0 8px; color: inherit; }
.km-md p:last-child { margin-bottom: 0; }
.km-md ul, .km-md ol {
  margin: 6px 0 8px;
  padding-left: 18px;
  list-style: disc;
}
.km-md ol { list-style: decimal; }
.km-md li { margin-bottom: 4px; }
.km-md strong { font-weight: 700; color: inherit; }
.km-md code {
  background: rgba(20, 58, 35, 0.08);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12.5px;
}

.km-md a {
  color: #1b4d2e;
  font-weight: 600;
  text-decoration: underline;
}

/* Sources collapsible + message actions */
.km-msg-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(20, 58, 35, 0.08);
}

.km-source-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  background: #e7f4ea;
  color: #1b4d2e;
  font-size: 11px;
  font-weight: 700;
  border: none;
  cursor: pointer;
}

.km-source-pill:hover {
  background: #d5ebd9;
}

.km-source-drawer {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: #f4faf5;
  border: 1px solid rgba(20, 58, 35, 0.1);
  font-size: 11.5px;
  color: #364c3b;
  display: none;
}

.km-source-drawer.is-open {
  display: block;
}

.km-source-item {
  padding: 4px 0;
  border-bottom: 1px dashed rgba(20, 58, 35, 0.12);
}

.km-source-item:last-child {
  border-bottom: none;
}

.km-msg-actions {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.km-action-btn {
  border: none;
  background: transparent;
  color: #5d7362;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 7px;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: background 0.15s, color 0.15s;
}

.km-action-btn:hover {
  background: #eaf3ec;
  color: #143a23;
}

.km-action-btn.is-speaking {
  background: #fef3c7;
  color: #b45309;
}

/* Quick Starter & Follow-up Chips */
.km-chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 6px;
}

.km-chip {
  background: #ffffff;
  color: #1b4d2e;
  border: 1px solid rgba(27, 77, 46, 0.22);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: all 0.16s ease;
}

.km-chip:hover {
  background: #1b4d2e;
  color: #fff;
  border-color: #1b4d2e;
}

/* Retry banner */
.km-retry-btn {
  margin-top: 8px;
  padding: 5px 11px;
  border-radius: 8px;
  border: 1px solid #dc2626;
  background: #fff;
  color: #dc2626;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.km-retry-btn:hover {
  background: #fef2f2;
}

/* Typing indicator */
.km-typing-box {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: #fff;
  border-radius: 14px;
  border: 1px solid rgba(20, 58, 35, 0.08);
  align-self: flex-start;
  font-size: 12px;
  color: #526658;
}

.km-dots {
  display: inline-flex;
  gap: 4px;
}

.km-dots span {
  width: 7px;
  height: 7px;
  background: #2e7d32;
  border-radius: 50%;
  animation: km-bounce 1.2s infinite;
}

.km-dots span:nth-child(2) { animation-delay: 0.2s; }
.km-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes km-bounce {
  0%, 80%, 100% { transform: scale(0.65); opacity: 0.45; }
  40%           { transform: scale(1.1); opacity: 1; }
}

/* ── Composer Input Area ──────────────────────────────────────────────────── */
#km-input-area {
  display: flex;
  align-items: flex-end;
  padding: 10px 12px;
  border-top: 1px solid rgba(20, 58, 35, 0.10);
  background: #ffffff;
  gap: 8px;
  flex-shrink: 0;
}

#km-input {
  flex: 1;
  border: 1.5px solid rgba(20, 58, 35, 0.18);
  border-radius: 14px;
  padding: 9px 12px;
  font-size: 13.5px;
  font-family: inherit;
  resize: none;
  outline: none;
  line-height: 1.45;
  color: #132217;
  background: #f7f9f5;
  max-height: 110px;
  overflow-y: auto;
  transition: border-color 0.16s, background 0.16s;
}

#km-input:focus {
  border-color: #2e7d32;
  background: #ffffff;
}

#km-input::placeholder { color: #7e9283; }

#km-mic {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1.5px solid rgba(20, 58, 35, 0.16);
  background: #f7f9f5;
  color: #1b4d2e;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.16s;
}

#km-mic:hover {
  background: #e7f4ea;
  border-color: #2e7d32;
}

#km-mic.is-listening {
  background: #fee2e2;
  border-color: #dc2626;
  color: #dc2626;
  animation: km-pulse-mic 1.1s infinite;
}

@keyframes km-pulse-mic {
  0%, 100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.35); }
  50%       { box-shadow: 0 0 0 6px rgba(220, 38, 38, 0); }
}

#km-send {
  width: 42px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, #1b4d2e, #2e7d32);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.15s, opacity 0.15s;
}

#km-send:hover { transform: scale(1.04); }
#km-send:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

#km-footer {
  text-align: center;
  font-size: 10.5px;
  color: #738779;
  padding: 5px 12px 8px;
  background: #ffffff;
  flex-shrink: 0;
}

/* ── Mobile Responsive ────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  #km-widget {
    bottom: 14px;
    right: 14px;
  }

  #km-launcher-pill {
    display: none;
  }

  #km-panel,
  #km-panel.km-expanded {
    width: calc(100vw - 24px);
    height: min(78vh, 580px);
    bottom: 72px;
    right: 0;
    border-radius: 18px;
  }
}
