/* ============================================================
   Narvio AI Teacher Widget - Universal Component
   Version: 1.0.0
   Usage: Include this CSS + ai-teacher-widget.js on any page
   Config: window.NARVIO_API_BASE = 'https://drama.narvio.cn'
   ============================================================ */

:root {
  --at-primary: #ff6b35;
  --at-primary-dark: #e55a2b;
  --at-primary-light: #ff8c5a;
  --at-bg: #ffffff;
  --at-bg-elevated: #f7f8fa;
  --at-bg-input: #f0f1f3;
  --at-text: #1a1a2e;
  --at-text-secondary: #6b7280;
  --at-border: #e5e7eb;
  --at-shadow: 0 8px 32px rgba(0,0,0,0.12);
  --at-shadow-sm: 0 2px 8px rgba(0,0,0,0.08);
  --at-radius: 16px;
  --at-radius-sm: 8px;
  --at-transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --at-z-fab: 9990;
  --at-z-panel: 9995;
  --at-z-overlay: 9989;
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --at-bg: #1a1a2e;
    --at-bg-elevated: #252540;
    --at-bg-input: #2d2d4a;
    --at-text: #f0f0f5;
    --at-text-secondary: #9ca3af;
    --at-border: #374151;
  }
}

/* ---- Floating Action Button ---- */
.at-fab {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: var(--at-z-fab);
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--at-primary), var(--at-primary-dark));
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  box-shadow: 0 4px 16px rgba(255,107,53,0.4);
  transition: transform var(--at-transition), box-shadow var(--at-transition);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

.at-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(255,107,53,0.5);
}

.at-fab:active {
  transform: scale(0.95);
}

.at-fab.at-open {
  transform: rotate(45deg) scale(1);
}

.at-fab .at-fab-icon {
  line-height: 1;
  transition: transform var(--at-transition);
}

/* Pulse animation */
@keyframes at-pulse {
  0% { box-shadow: 0 4px 16px rgba(255,107,53,0.4), 0 0 0 0 rgba(255,107,53,0.3); }
  70% { box-shadow: 0 4px 16px rgba(255,107,53,0.4), 0 0 0 12px rgba(255,107,53,0); }
  100% { box-shadow: 0 4px 16px rgba(255,107,53,0.4), 0 0 0 0 rgba(255,107,53,0); }
}

.at-fab.at-pulse {
  animation: at-pulse 2.5s infinite;
}

/* ---- Badge (unread count) ---- */
.at-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* ---- Chat Panel ---- */
.at-panel {
  position: fixed;
  right: 20px;
  bottom: 86px;
  z-index: var(--at-z-panel);
  width: 380px;
  height: 520px;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - 120px);
  background: var(--at-bg);
  border: 1px solid var(--at-border);
  border-radius: var(--at-radius);
  box-shadow: var(--at-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  pointer-events: none;
  transition: opacity var(--at-transition), transform var(--at-transition);
}

.at-panel.at-active {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ---- Panel Header ---- */
.at-header {
  padding: 14px 16px;
  background: linear-gradient(135deg, var(--at-primary), var(--at-primary-dark));
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.at-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.at-header-info {
  flex: 1;
  min-width: 0;
}

.at-header-title {
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  line-height: 1.3;
}

.at-header-status {
  color: rgba(255,255,255,0.7);
  font-size: 11px;
  margin: 0;
  line-height: 1.3;
}

.at-header-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.at-header-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.15);
  color: rgba(255,255,255,0.8);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: background 0.2s;
  padding: 0;
}

.at-header-btn:hover {
  background: rgba(255,255,255,0.25);
  color: #fff;
}

/* ---- Messages Area ---- */
.at-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  scroll-behavior: smooth;
}

.at-messages::-webkit-scrollbar {
  width: 4px;
}

.at-messages::-webkit-scrollbar-thumb {
  background: var(--at-border);
  border-radius: 2px;
}

/* ---- Message Bubble ---- */
.at-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 13px;
  line-height: 1.6;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: at-fadeIn 0.3s ease;
}

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

.at-msg.at-user {
  background: linear-gradient(135deg, var(--at-primary), var(--at-primary-dark));
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.at-msg.at-bot {
  background: var(--at-bg-elevated);
  color: var(--at-text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.at-msg.at-typing {
  color: var(--at-text-secondary);
  font-style: italic;
}

.at-msg-time {
  font-size: 10px;
  color: var(--at-text-secondary);
  margin-top: 4px;
  opacity: 0.6;
}

.at-msg.at-user .at-msg-time {
  color: rgba(255,255,255,0.6);
  text-align: right;
}

/* ---- Welcome Message ---- */
.at-welcome {
  text-align: center;
  padding: 20px 16px 8px;
}

.at-welcome-icon {
  font-size: 40px;
  margin-bottom: 8px;
}

.at-welcome-text {
  font-size: 13px;
  color: var(--at-text-secondary);
  line-height: 1.5;
}

/* ---- Quick Actions ---- */
.at-quick {
  padding: 8px 12px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  border-top: 1px solid var(--at-border);
  flex-shrink: 0;
}

.at-quick-btn {
  padding: 6px 12px;
  border-radius: 16px;
  background: var(--at-bg-elevated);
  border: 1px solid var(--at-border);
  color: var(--at-text-secondary);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.at-quick-btn:hover {
  background: var(--at-primary);
  color: #fff;
  border-color: var(--at-primary);
}

/* ---- Input Area ---- */
.at-input-area {
  padding: 10px 12px;
  border-top: 1px solid var(--at-border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
  background: var(--at-bg);
}

.at-input {
  flex: 1;
  padding: 10px 14px;
  border-radius: 20px;
  background: var(--at-bg-input);
  border: 1px solid var(--at-border);
  color: var(--at-text);
  font-size: 13px;
  outline: none;
  resize: none;
  min-height: 40px;
  max-height: 100px;
  line-height: 1.4;
  transition: border-color 0.2s;
  font-family: inherit;
}

.at-input:focus {
  border-color: var(--at-primary);
}

.at-input::placeholder {
  color: var(--at-text-secondary);
}

.at-voice-btn,
.at-send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.2s;
  flex-shrink: 0;
  padding: 0;
}

.at-voice-btn {
  background: var(--at-bg-elevated);
  color: var(--at-text-secondary);
  border: 1px solid var(--at-border);
}

.at-voice-btn:hover {
  color: var(--at-primary);
  border-color: var(--at-primary);
}

.at-voice-btn.at-recording {
  background: #ef4444;
  color: #fff;
  border-color: #ef4444;
  animation: at-pulse 1s infinite;
}

.at-send-btn {
  background: linear-gradient(135deg, var(--at-primary), var(--at-primary-dark));
  color: #fff;
}

.at-send-btn:hover {
  transform: scale(1.05);
}

.at-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ---- Loading Indicator ---- */
.at-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: var(--at-text-secondary);
  font-size: 13px;
  gap: 8px;
}

.at-loading-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--at-primary);
  animation: at-dotBounce 1.4s infinite ease-in-out;
}

.at-loading-dots span:nth-child(1) { animation-delay: 0s; }
.at-loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.at-loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes at-dotBounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ---- Mobile Responsive ---- */
@media (max-width: 480px) {
  .at-panel {
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
    border: none;
  }

  .at-fab {
    right: 16px;
    bottom: 16px;
  }
}

/* ---- Overlay (mobile) ---- */
.at-overlay {
  display: none;
}

@media (max-width: 480px) {
  .at-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: var(--at-z-overlay);
    background: rgba(0,0,0,0.3);
    opacity: 0;
    transition: opacity var(--at-transition);
    pointer-events: none;
  }

  .at-overlay.at-active {
    opacity: 1;
    pointer-events: auto;
  }
}

/* ---- Clear history confirm ---- */
.at-confirm {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.at-confirm.at-active {
  opacity: 1;
  pointer-events: auto;
}

.at-confirm-box {
  background: var(--at-bg);
  border-radius: var(--at-radius-sm);
  padding: 20px;
  max-width: 280px;
  text-align: center;
  box-shadow: var(--at-shadow);
}

.at-confirm-box p {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--at-text);
}

.at-confirm-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.at-confirm-actions button {
  padding: 8px 20px;
  border-radius: 8px;
  border: none;
  font-size: 13px;
  cursor: pointer;
  font-weight: 500;
}

.at-confirm-cancel {
  background: var(--at-bg-elevated);
  color: var(--at-text);
}

.at-confirm-ok {
  background: #ef4444;
  color: #fff;
}
