/* ===== AI Chat Widget ===== */

#ai_btn {
  position: fixed;
  bottom: 22px;
  right: 10px;
  width: 24px;
  z-index: 9999;
}
#ai_btn_img {
  width: 32px;
  cursor: pointer;
  opacity: 0.4;
  transition: opacity .2s;
  display: block;
}

#ai_overlay {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(255,255,255,0.5);
  z-index: 10499;
  backdrop-filter: blur(2px);
}

#ai_pop {
  display: none;
  position: fixed;
  bottom: 66px;
  right: 10px;
  width: 360px;
  height: 500px;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  z-index: 10500;
  text-align: left;
  box-shadow: 0 12px 48px rgba(0,0,0,0.25);
  flex-direction: column;
}

/* Header */
.ai-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  background: #1060C0;
  color: #fff;
  flex-shrink: 0;
}
.ai-header-icon {
  width: 30px; height: 30px;
  background: rgba(255,255,255,.18);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
}
.ai-header-title {
  font-size: 13px;
  font-weight: 700;
  font-family: Arial, sans-serif;
}
.ai-header-sub {
  font-size: 11px;
  opacity: .75;
  font-family: Arial, sans-serif;
}
.ai-header-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}
#ai_dot {
  width: 7px; height: 7px;
  background: #4ade80;
  border-radius: 50%;
}
#ai_stxt {
  font-size: 11px;
  opacity: .85;
  font-family: Arial, sans-serif;
}
.ai-hbtn {
  width: 24px; height: 24px;
  border-radius: 50%;
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  font-size: 13px;
}
.ai-hbtn:hover { background: rgba(255,255,255,.3); }

/* Messages area */
#ai_msgs {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
  height: 380px;
  font-family: Arial, sans-serif;
}

/* Welcome screen */
#ai_wlc {
  text-align: center;
  padding: 20px 8px;
  color: #94a3b8;
}
.ai-wlc-icon { font-size: 32px; margin-bottom: 8px; }
.ai-wlc-title { font-size: 14px; color: #475569; font-weight: 700; margin-bottom: 5px; font-family: Arial, sans-serif; }
.ai-wlc-sub  { font-size: 12px; line-height: 1.5; font-family: Arial, sans-serif; }

/* Input area */
.ai-input-wrap {
  border-top: 1px solid #e2e8f0;
  padding: 9px 11px;
  background: #fff;
  flex-shrink: 0;
}
.ai-input-row {
  display: flex;
  align-items: center;
  gap: 7px;
  background: #f8fafc;
  border: 1.5px solid #e2e8f0;
  border-radius: 18px;
  padding: 4px 4px 4px 12px;
}
#ai_ta {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 13px;
  font-family: Arial, sans-serif;
  resize: none;
  outline: none;
  max-height: 80px;
  line-height: 1.4;
  padding: 3px 0;
  color: #1e293b;
}
#ai_sbtn {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: none;
  background: #1060C0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  transition: background .2s;
}
#ai_sbtn:hover { background: #0A4FA0; }

/* Message rows */
.ai-row-user {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  flex-direction: row-reverse;
}
.ai-row-bot {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
.ai-avatar-user {
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #1060C0;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  flex-shrink: 0;
}
.ai-avatar-bot {
  width: 24px; height: 24px;
  border-radius: 50%;
  background: #d0e4f7;
  color: #0A4FA0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  flex-shrink: 0;
  margin-bottom: 2px;
}
.ai-bubble-user {
  max-width: 240px;
  padding: 8px 12px;
  border-radius: 14px;
  border-bottom-right-radius: 3px;
  background: #1060C0;
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
  font-family: Arial, sans-serif;
}
.ai-bubble-bot {
  max-width: 90%;
  padding: 8px 12px;
  border-radius: 14px;
  border-bottom-left-radius: 3px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  color: #1e293b;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  font-family: Arial, sans-serif;
}
.ai-time {
  font-size: 10px;
  color: #94a3b8;
  margin-top: 2px;
  font-family: Arial, sans-serif;
}
.ai-time-r { text-align: right; }

/* Source chips */
.ai-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.ai-chip {
  background: #e8f0fb;
  border: 1px solid #a8c8ef;
  border-radius: 9px;
  padding: 2px 8px;
  font-size: 10px;
  color: #003B80;
  font-family: Arial, sans-serif;
  text-decoration: none;
  display: inline-block;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ai-chip-pct { opacity: .6; }
a.ai-chip { cursor: pointer; }
a.ai-chip:hover {
  background: #d0e4f7;
  border-color: #1060C0;
}

/* Error */
.ai-error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 9px;
  padding: 7px 11px;
  font-size: 12px;
  color: #dc2626;
  margin-left: 30px;
  font-family: Arial, sans-serif;
}

/* Expanded state */
#ai_pop.ai-expanded {
  width: min(1260px, 96vw);
  height: 90vh;
  top: 5vh;
  bottom: auto;
  right: 50%;
  transform: translateX(50%);
  border-radius: 12px;
}
#ai_pop.ai-expanded #ai_msgs {
  height: auto;
}
#ai_pop.ai-expanded .ai-bubble-user {
  font-size: 15px;
  max-width: 55%;
}
#ai_pop.ai-expanded .ai-bubble-bot {
  font-size: 15px;
  max-width: 90%;
}
#ai_pop.ai-expanded #ai_ta { font-size: 15px; }
#ai_pop.ai-expanded .ai-header-title { font-size: 15px; }
#ai_pop.ai-expanded .ai-header-sub   { font-size: 13px; }
#ai_pop.ai-expanded .ai-wlc-title    { font-size: 16px; }
#ai_pop.ai-expanded .ai-wlc-sub      { font-size: 14px; }
#ai_pop.ai-expanded .ai-chip         { font-size: 12px; padding: 3px 10px; }
#ai_pop.ai-expanded .ai-time         { font-size: 11px; }
#ai_pop.ai-expanded #ai_sbtn         { width: 36px; height: 36px; }
#ai_pop.ai-expanded .ai-hbtn         { width: 28px; height: 28px; font-size: 15px; }
#ai_pop.ai-expanded .ai-input-wrap   { padding: 11px 14px; }

/* Typing indicator */
.ai-typ-row { display: flex; align-items: flex-end; gap: 6px; }
.ai-typ-dots {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  border-bottom-left-radius: 3px;
  padding: 10px 13px;
  display: flex;
  gap: 4px;
  align-items: center;
}
.aitd {
  width: 7px; height: 7px;
  background: #94a3b8;
  border-radius: 50%;
  animation: aitdb 1.2s infinite;
}
.aitd:nth-child(2) { animation-delay: .2s; }
.aitd:nth-child(3) { animation-delay: .4s; }
@keyframes aitdb {
  0%, 60%, 100% { transform: translateY(0); }
  30%            { transform: translateY(-5px); }
}

/* Иконка-ссылка рядом с названием товара */
.ai-prod-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0;
  font-size: 11px;
  font-weight: 700;
  line-height: 16px;
  color: #1a3a6b;
  background: none;
  border-radius: 0;
  text-decoration: none;
  vertical-align: middle;
  margin-left: 4px;
  white-space: nowrap;
  opacity: 1;
  transition: background .15s, transform .1s, opacity .15s;
  flex-shrink: 0;
  box-shadow: none;
}
.ai-prod-link:hover {
  background: none;
  text-decoration: none;
  transform: scale(1.05);
  color: #1060C0;
  opacity: 1;
}

/* Обёртка строки бота — занимает всё доступное место */
.ai-row-bot .ai-wrap {
  flex: 1;
  min-width: 0;
}

/* Мобильная версия — всегда полный экран, без JS переключений */
@media (max-width: 600px) {
  #ai_pop,
  #ai_pop.ai-expanded {
    top: 0 !important; left: 0 !important;
    right: 0 !important; bottom: 0 !important;
    width: 100% !important; height: 100% !important;
    max-height: 100% !important;
    border-radius: 0 !important;
    transform: none !important;
    z-index: 2147483647 !important;
  }
  #ai_pop #ai_msgs,
  #ai_pop.ai-expanded #ai_msgs { height: auto !important; max-height: none !important; }
  #ai_btn { right: 8px; }
}

/* iPhone 16 Pro/Max и другие высокие экраны: Safari снизу перекрывает bottom:22px.
   С viewport width=360: iPhone X даёт ~779px, iPhone 16 Pro Max ~800px. */
@media (max-width: 600px) and (min-height: 780px) {
  #ai_btn {
    bottom: 80px !important;
  }
  #ai_pop {
    bottom: 128px !important;
  }
}
