/**
 * MOTHI Chat — Front-end widget styles (v2 — polished)
 * WCAG 2.1 AA compliant, fully responsive, modern design.
 */

/* =====================================================================
   TOKENS
===================================================================== */
#mothi-chat-root {
  --mothi-primary:       #006400;
  --mothi-primary-light: #1a7a1a;
  --mothi-primary-dark:  #004d00;
  --mothi-secondary:     #FFD700;
  --mothi-radius:        16px;
  --mothi-font:          'Segoe UI', system-ui, -apple-system, sans-serif;

  /* Panel */
  --mothi-panel-bg:      #f8faf8;
  --mothi-panel-fg:      #1a1a1a;
  --mothi-panel-shadow:  0 20px 60px rgba(0,0,0,0.22), 0 4px 16px rgba(0,0,0,0.12);

  /* Bubbles */
  --mothi-bot-bg:        #ffffff;
  --mothi-bot-fg:        #1a1a1a;
  --mothi-bot-border:    #e4ede4;
  --mothi-user-bg:       linear-gradient(135deg, var(--mothi-primary-light), var(--mothi-primary-dark));
  --mothi-user-fg:       #ffffff;

  /* Input */
  --mothi-input-bg:      #ffffff;
  --mothi-border:        #dde8dd;

  /* Misc */
  --mothi-z:             99999;

  font-family: var(--mothi-font);
}

/* Dark mode */
#mothi-chat-root.mothi-dark {
  --mothi-panel-bg:    #1a1f1a;
  --mothi-panel-fg:    #eef2ee;
  --mothi-bot-bg:      #252d25;
  --mothi-bot-fg:      #eef2ee;
  --mothi-bot-border:  #3a4a3a;
  --mothi-input-bg:    #252d25;
  --mothi-border:      #3a4a3a;
}

/* =====================================================================
   POSITIONING
===================================================================== */
#mothi-chat-root {
  position: fixed;
  z-index: var(--mothi-z);
  bottom: 28px;
  right: 28px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  pointer-events: none; /* children re-enable */
}

#mothi-chat-root.mothi-position-bottom-left {
  right: auto;
  left: 28px;
  align-items: flex-start;
}

/* =====================================================================
   TOGGLE BUTTON
===================================================================== */
#mothi-chat-toggle {
  pointer-events: all;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--mothi-primary-light), var(--mothi-primary-dark));
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 24px rgba(0,100,0,0.45), 0 2px 8px rgba(0,0,0,0.2);
  transition: transform 0.25s cubic-bezier(.34,1.56,.64,1), box-shadow 0.2s ease;
  position: relative;
  flex-shrink: 0;
}

#mothi-chat-toggle::before {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--mothi-secondary);
  opacity: 0;
  transition: opacity 0.2s;
}

#mothi-chat-toggle:hover {
  transform: scale(1.1) translateY(-2px);
  box-shadow: 0 10px 32px rgba(0,100,0,0.5), 0 4px 12px rgba(0,0,0,0.2);
}

#mothi-chat-toggle:hover::before {
  opacity: 0.7;
}

#mothi-chat-toggle:focus-visible {
  outline: 3px solid var(--mothi-secondary);
  outline-offset: 3px;
}

#mothi-chat-toggle:active {
  transform: scale(0.95);
}

/* Pulse ring animation */
#mothi-chat-toggle::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: rgba(0,100,0,0.3);
  animation: mothiPulse 2.5s ease-out infinite;
}

@keyframes mothiPulse {
  0%   { transform: scale(1);   opacity: 0.6; }
  70%  { transform: scale(1.5); opacity: 0;   }
  100% { transform: scale(1.5); opacity: 0;   }
}

/* Stop pulse when open */
#mothi-chat-root.mothi-open #mothi-chat-toggle::after {
  animation: none;
}

.mothi-badge-dot {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 13px;
  height: 13px;
  background: var(--mothi-secondary);
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

/* =====================================================================
   CHAT PANEL
===================================================================== */
#mothi-chat-panel {
  pointer-events: all;
  width: 390px;
  max-width: calc(100vw - 56px);
  height: 600px;
  max-height: calc(100vh - 120px);
  border-radius: var(--mothi-radius);
  background: var(--mothi-panel-bg);
  color: var(--mothi-panel-fg);
  box-shadow: var(--mothi-panel-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  margin-bottom: 14px;
  border: 1px solid rgba(0,100,0,0.12);
  transform-origin: bottom right;
  animation: mothiPanelIn 0.3s cubic-bezier(.34,1.4,.64,1);
}

#mothi-chat-root.mothi-position-bottom-left #mothi-chat-panel {
  transform-origin: bottom left;
}

@keyframes mothiPanelIn {
  from { opacity: 0; transform: scale(0.88) translateY(20px); }
  to   { opacity: 1; transform: scale(1)    translateY(0);    }
}

#mothi-chat-panel[hidden] {
  display: none;
}

/* =====================================================================
   HEADER
===================================================================== */
.mothi-header {
  background: linear-gradient(135deg, var(--mothi-primary-light) 0%, var(--mothi-primary-dark) 100%);
  color: #fff;
  padding: 14px 16px 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

/* Decorative diagonal stripe */
.mothi-header::after {
  content: '';
  position: absolute;
  top: -30px;
  right: -20px;
  width: 100px;
  height: 100px;
  background: rgba(255,215,0,0.12);
  border-radius: 50%;
  pointer-events: none;
}

.mothi-header::before {
  content: '';
  position: absolute;
  bottom: -40px;
  right: 40px;
  width: 80px;
  height: 80px;
  background: rgba(255,255,255,0.06);
  border-radius: 50%;
  pointer-events: none;
}

.mothi-header-left {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
  position: relative;
  z-index: 1;
}

.mothi-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.18);
  backdrop-filter: blur(4px);
  border: 2px solid rgba(255,215,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.mothi-title {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.1px;
}

.mothi-subtitle {
  font-size: 11px;
  opacity: 0.8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

/* Online dot */
.mothi-subtitle::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #4ade80;
  margin-right: 5px;
  vertical-align: middle;
  box-shadow: 0 0 6px #4ade80;
}

.mothi-header-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}

.mothi-icon-btn {
  background: rgba(255,255,255,0.12);
  border: none;
  color: rgba(255,255,255,0.9);
  cursor: pointer;
  padding: 7px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  backdrop-filter: blur(2px);
}

.mothi-icon-btn:hover,
.mothi-icon-btn:focus-visible {
  background: rgba(255,255,255,0.25);
  color: #fff;
  outline: 2px solid rgba(255,215,0,0.6);
  outline-offset: 1px;
}

/* =====================================================================
   MESSAGES AREA
===================================================================== */
.mothi-messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;

  /* Subtle dot-grid background */
  background-image: radial-gradient(circle, rgba(0,100,0,0.06) 1px, transparent 1px);
  background-size: 22px 22px;
  background-color: var(--mothi-panel-bg);
}

.mothi-messages::-webkit-scrollbar { width: 4px; }
.mothi-messages::-webkit-scrollbar-track { background: transparent; }
.mothi-messages::-webkit-scrollbar-thumb {
  background: rgba(0,100,0,0.2);
  border-radius: 2px;
}
.mothi-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(0,100,0,0.35);
}

/* =====================================================================
   MESSAGE BUBBLES
===================================================================== */
.mothi-msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: mothiMsgIn 0.22s ease;
}

@keyframes mothiMsgIn {
  from { opacity: 0; transform: translateY(8px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0)   scale(1);    }
}

.mothi-msg-bot  { align-self: flex-start; }
.mothi-msg-user { align-self: flex-end;   }

/* Role label */
.mothi-msg-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  margin-bottom: 4px;
  opacity: 0.55;
  padding: 0 4px;
}

.mothi-msg-bot  .mothi-msg-label { color: var(--mothi-primary); }
.mothi-msg-user .mothi-msg-label { color: #888; text-align: right; }

/* Bubble */
.mothi-bubble {
  padding: 11px 15px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
  position: relative;
}

.mothi-msg-bot .mothi-bubble {
  background: var(--mothi-bot-bg);
  color: var(--mothi-bot-fg);
  border: 1px solid var(--mothi-bot-border);
  border-radius: 4px 18px 18px 18px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.07);
}

.mothi-msg-user .mothi-bubble {
  background: linear-gradient(135deg, #1a7a1a, #004d00);
  color: var(--mothi-user-fg);
  border-radius: 18px 4px 18px 18px;
  box-shadow: 0 3px 12px rgba(0,100,0,0.35);
}

/* Timestamp */
.mothi-msg-time {
  font-size: 10px;
  opacity: 0.45;
  margin-top: 4px;
  padding: 0 4px;
}

.mothi-msg-user .mothi-msg-time { text-align: right; }

/* Sources */
.mothi-sources {
  margin-top: 8px;
  padding: 7px 10px;
  background: rgba(0,100,0,0.06);
  border-left: 3px solid var(--mothi-primary);
  border-radius: 0 6px 6px 0;
  font-size: 11px;
  color: var(--mothi-primary);
}

.mothi-sources-label {
  font-weight: 700;
  margin-bottom: 3px;
  opacity: 0.7;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.mothi-sources a {
  color: var(--mothi-primary);
  text-decoration: none;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.mothi-sources a:hover { text-decoration: underline; }
.mothi-sources a::before { content: '↗ '; }

/* =====================================================================
   TYPING INDICATOR
===================================================================== */
.mothi-typing-wrap {
  display: flex;
  flex-direction: column;
  align-self: flex-start;
  max-width: 85%;
  animation: mothiMsgIn 0.22s ease;
}

.mothi-typing {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 16px;
  background: var(--mothi-bot-bg);
  border: 1px solid var(--mothi-bot-border);
  border-radius: 4px 18px 18px 18px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.07);
  width: fit-content;
}

.mothi-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--mothi-primary);
  opacity: 0.5;
  animation: mothiBounce 1.3s ease-in-out infinite;
}

.mothi-dot:nth-child(2) { animation-delay: 0.18s; }
.mothi-dot:nth-child(3) { animation-delay: 0.36s; }

@keyframes mothiBounce {
  0%, 60%, 100% { transform: translateY(0);    opacity: 0.5; }
  30%            { transform: translateY(-7px); opacity: 1;   }
}

/* =====================================================================
   INPUT AREA
===================================================================== */
.mothi-input-area {
  border-top: 1px solid var(--mothi-border);
  padding: 12px 14px 10px;
  background: var(--mothi-input-bg);
  flex-shrink: 0;
  position: relative;
}

/* Subtle top shadow */
.mothi-input-area::before {
  content: '';
  position: absolute;
  top: -12px;
  left: 0;
  right: 0;
  height: 12px;
  background: linear-gradient(to top, rgba(0,0,0,0.04), transparent);
  pointer-events: none;
}

#mothi-chat-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--mothi-panel-bg);
  border: 1.5px solid var(--mothi-border);
  border-radius: 24px;
  padding: 6px 6px 6px 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

#mothi-chat-form:focus-within {
  border-color: var(--mothi-primary);
  box-shadow: 0 0 0 3px rgba(0,100,0,0.12);
}

#mothi-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  padding: 6px 0;
  font-size: 14px;
  color: var(--mothi-panel-fg);
  resize: none;
  min-height: 24px;
  max-height: 110px;
  overflow-y: auto;
  line-height: 1.5;
  font-family: var(--mothi-font);
}

#mothi-input::placeholder {
  color: #aaa;
}

#mothi-input::-webkit-scrollbar { width: 3px; }
#mothi-input::-webkit-scrollbar-thumb { background: rgba(0,100,0,0.2); border-radius: 2px; }

#mothi-send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--mothi-primary-light), var(--mothi-primary-dark));
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 3px 10px rgba(0,100,0,0.4);
}

#mothi-send-btn:hover:not(:disabled) {
  transform: scale(1.08);
  box-shadow: 0 5px 16px rgba(0,100,0,0.5);
}

#mothi-send-btn:focus-visible {
  outline: 3px solid var(--mothi-secondary);
  outline-offset: 2px;
}

#mothi-send-btn:active:not(:disabled) {
  transform: scale(0.93);
}

#mothi-send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

.mothi-powered-by {
  font-size: 10px;
  text-align: center;
  color: #aaa;
  margin: 7px 0 0;
  letter-spacing: 0.2px;
}

/* Screen-reader only */
.screen-reader-text {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  overflow: hidden !important;
  clip: rect(1px,1px,1px,1px) !important;
  white-space: nowrap !important;
}

/* =====================================================================
   WELCOME CARD (first message)
===================================================================== */
.mothi-welcome-card .mothi-bubble {
  background: linear-gradient(135deg, #f0faf0, #e8f5e8);
  border-color: rgba(0,100,0,0.2);
  border-left: 3px solid var(--mothi-secondary);
}

/* =====================================================================
   DATE DIVIDER
===================================================================== */
.mothi-date-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 11px;
  color: #aaa;
  margin: 4px 0;
}

.mothi-date-divider::before,
.mothi-date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--mothi-border);
}

/* =====================================================================
   RESPONSIVE — MOBILE
===================================================================== */
@media (max-width: 480px) {
  #mothi-chat-root {
    bottom: 0;
    right: 0;
    left: 0;
    align-items: stretch;
    pointer-events: none;
  }

  #mothi-chat-panel {
    width: 100%;
    max-width: 100%;
    height: 92dvh;
    max-height: 92dvh;
    border-radius: 20px 20px 0 0;
    margin-bottom: 0;
    pointer-events: all;
  }

  #mothi-chat-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    pointer-events: all;
  }

  #mothi-chat-root.mothi-position-bottom-left #mothi-chat-toggle {
    right: auto;
    left: 20px;
  }
}

/* =====================================================================
   HIGH CONTRAST
===================================================================== */
@media (forced-colors: active) {
  #mothi-chat-toggle,
  #mothi-send-btn { forced-color-adjust: none; }
}

/* =====================================================================
   PRINT
===================================================================== */
@media print {
  #mothi-chat-root { display: none !important; }
}
