/* ============================================================
   iAgent — Support widget
   Sam · 客服 Agent · in-page support panel
   Drops onto any iAgent surface (marketing, login, product).
   Requires tokens.css + base.css to be loaded first.
   ============================================================ */

.sw-root {
  /* host element — invisible itself */
  position: fixed;
  z-index: 9999;
  inset: 0;
  pointer-events: none;
  font-family: var(--font-sans);
}
.sw-root * { box-sizing: border-box; }
.sw-root > * { pointer-events: auto; }

/* ---------- Trigger ---------- */
.sw-trigger {
  position: fixed;
  bottom: 24px; right: 24px;
  width: 56px; height: 56px;
  border-radius: 14px;
  background: var(--slate-950);
  color: #fff;
  border: 0;
  cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 8px 24px -6px rgba(10, 37, 64, 0.30),
              0 2px 6px rgba(10, 37, 64, 0.12);
  transition: transform var(--d-base) var(--ease-out),
              box-shadow var(--d-base) var(--ease-out),
              background var(--d-fast) var(--ease-std);
  font-family: inherit;
}
.sw-trigger:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px -6px rgba(10, 37, 64, 0.36),
              0 4px 8px rgba(10, 37, 64, 0.14);
  background: var(--slate-900);
}
.sw-trigger:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring), 0 8px 24px -6px rgba(10, 37, 64, 0.30);
}

.sw-trigger .sw-mark {
  position: relative;
  width: 36px; height: 36px;
  border-radius: 8px;
  background: #4A5568;
  display: grid; place-items: center;
  font: 700 18px var(--font-sans);
  letter-spacing: -0.04em;
}
.sw-trigger .sw-mark::after {
  /* "?" accent */
  content: "?";
  position: absolute;
  top: 3px; right: 5px;
  font: 700 9px var(--font-sans);
  opacity: 0.88;
}
.sw-trigger .sw-status-dot {
  position: absolute;
  right: 6px; bottom: 6px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--success-500);
  box-shadow: 0 0 0 2px var(--slate-950);
}
.sw-trigger .sw-badge {
  position: absolute;
  top: -4px; right: -4px;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--danger-500);
  color: #fff;
  font: 600 10px / 18px var(--font-mono);
  text-align: center;
  letter-spacing: 0.02em;
  display: none;
  box-shadow: 0 0 0 2px var(--bg-app);
}
.sw-trigger[data-unread="true"] .sw-badge { display: block; }
.sw-trigger[data-open="true"] { transform: scale(0.92); }

/* tooltip-label that appears on hover (desktop only) */
.sw-trigger .sw-tip {
  position: absolute;
  right: calc(100% + 12px);
  top: 50%; transform: translateY(-50%) translateX(8px);
  background: var(--slate-950);
  color: #fff;
  font: 500 12px / 1 var(--font-sans);
  white-space: nowrap;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  opacity: 0; pointer-events: none;
  transition: opacity var(--d-fast) var(--ease-std), transform var(--d-base) var(--ease-out);
}
.sw-trigger:hover .sw-tip { opacity: 1; transform: translateY(-50%) translateX(0); }
.sw-trigger[data-open="true"] .sw-tip { display: none; }

/* ---------- Panel ---------- */
.sw-panel {
  position: fixed;
  bottom: 96px; right: 24px;
  width: 380px;
  max-height: calc(100vh - 120px);
  height: 620px;
  background: var(--bg-canvas);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-pop);
  display: none;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
}
.sw-panel.sw-mounted { display: flex; }

/* ---------- Header ---------- */
.sw-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line-subtle);
  background: var(--bg-canvas);
  flex-shrink: 0;
}
.sw-avatar {
  position: relative;
  width: 36px; height: 36px;
  flex-shrink: 0;
}
.sw-avatar .sw-av-sq {
  width: 100%; height: 100%;
  border-radius: 8px;
  background: #4A5568;
  display: grid; place-items: center;
  font: 700 18px var(--font-sans);
  color: #fff;
  letter-spacing: -0.04em;
  position: relative;
}
.sw-avatar .sw-av-sq::after {
  content: "?";
  position: absolute;
  top: 3px; right: 5px;
  font: 700 9px var(--font-sans);
  color: #fff; opacity: 0.88;
}
.sw-avatar.human .sw-av-sq {
  background: var(--success-600);
}
.sw-avatar.human .sw-av-sq::after { content: ""; }
.sw-avatar .status {
  position: absolute;
  right: -2px; bottom: -2px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--agent-online);
  box-shadow: 0 0 0 2px var(--bg-canvas);
}
.sw-avatar .status.thinking {
  background: var(--brand-400);
  animation: agent-pulse 1.6s var(--ease-std) infinite;
}

.sw-head-meta { flex: 1; min-width: 0; }
.sw-head-meta .sw-name {
  font: 600 13px / 18px var(--font-sans);
  letter-spacing: -0.01em;
  color: var(--text-primary);
}
.sw-head-meta .sw-sub {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-tertiary);
  font-family: var(--font-mono); letter-spacing: 0.02em;
}
.sw-head-meta .sw-sub .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--success-500); flex-shrink: 0;
}
.sw-head-meta .sw-sub.thinking .dot {
  background: var(--brand-400);
  animation: agent-pulse 1.6s var(--ease-std) infinite;
}

.sw-head-actions { display: flex; align-items: center; gap: 6px; }
.sw-iconbtn {
  width: 28px; height: 28px;
  display: grid; place-items: center;
  background: transparent;
  border: 0;
  border-radius: var(--r-sm);
  cursor: pointer;
  color: var(--text-tertiary);
  font-family: inherit;
}
.sw-iconbtn:hover { background: var(--bg-hover); color: var(--text-primary); }
.sw-iconbtn svg { display: block; }

/* ---------- Body / messages ---------- */
.sw-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
  background: var(--bg-app);
}

.sw-msg { max-width: 280px; }
.sw-msg .bubble {
  padding: 8px 12px;
  font: 400 13px / 19px var(--font-sans);
  border-radius: 12px;
}
.sw-msg.user { align-self: flex-end; }
.sw-msg.user .bubble {
  background: var(--brand-500); color: #fff;
  border-bottom-right-radius: 4px;
}
.sw-msg.sam .bubble,
.sw-msg.human .bubble {
  background: var(--bg-canvas);
  color: var(--text-primary);
  border: 1px solid var(--line-subtle);
  border-bottom-left-radius: 4px;
}
.sw-msg.system {
  align-self: center;
  font: 400 11px / 16px var(--font-mono);
  color: var(--text-tertiary);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 6px 0;
  text-align: center;
  max-width: none;
  width: 100%;
}
.sw-msg.system .bubble {
  background: transparent;
  padding: 0;
  border: 0;
  border-radius: 0;
}
.sw-msg .stamp-row {
  display: flex; gap: 6px; align-items: center;
  margin-top: 4px;
  font: 500 10px var(--font-mono);
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
}

/* AI typing indicator */
.sw-typing { align-self: flex-start; padding: 6px 0; }
.sw-typing .dots {
  display: inline-flex; gap: 4px;
  padding: 8px 12px;
  background: var(--bg-canvas);
  border: 1px solid var(--line-subtle);
  border-radius: 12px;
}
.sw-typing .dots i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--slate-400);
  animation: sw-typing 1.2s infinite ease-in-out;
}
.sw-typing .dots i:nth-child(2) { animation-delay: 0.15s; }
.sw-typing .dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes sw-typing {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40%           { opacity: 1;   transform: translateY(-3px); }
}

/* AI-disclosure banner — "tool, not coworker" */
.sw-disclosure {
  align-self: stretch;
  background: color-mix(in oklab, var(--brand-500) 5%, var(--bg-canvas));
  border: 1px solid color-mix(in oklab, var(--brand-500) 20%, var(--line-subtle));
  border-radius: var(--r-md);
  padding: 12px 14px;
  font: 400 12px / 18px var(--font-sans);
  color: var(--text-secondary);
}
.sw-disclosure b {
  color: var(--text-primary);
  font-weight: 600;
}

/* ---------- Suggestions ---------- */
.sw-suggest {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 16px 12px;
  background: var(--bg-app);
}
.sw-suggest button {
  font: 500 12px / 1.2 var(--font-sans);
  color: var(--text-secondary);
  background: var(--bg-canvas);
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  padding: 6px 12px;
  cursor: pointer;
  transition: background var(--d-fast), border-color var(--d-fast), color var(--d-fast);
}
.sw-suggest button:hover {
  background: var(--bg-hover);
  border-color: var(--line-strong);
  color: var(--text-primary);
}

/* ---------- Composer ---------- */
.sw-input {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 12px 16px;
  border-top: 1px solid var(--line-subtle);
  background: var(--bg-canvas);
}
.sw-input textarea {
  flex: 1;
  resize: none;
  border: 0; outline: none;
  background: transparent;
  font: 400 13px / 20px var(--font-sans);
  color: var(--text-primary);
  max-height: 100px;
  min-height: 20px;
  padding: 6px 0;
}
.sw-input textarea::placeholder { color: var(--text-tertiary); }
.sw-input .sw-send {
  background: var(--brand-500); color: #fff;
  border: 0; cursor: pointer;
  width: 32px; height: 32px;
  border-radius: var(--r-sm);
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: background var(--d-fast);
}
.sw-input .sw-send:disabled {
  background: var(--bg-sunken);
  color: var(--text-disabled);
  cursor: not-allowed;
}
.sw-input .sw-send:hover:not(:disabled) { background: var(--brand-600); }
.sw-input .sw-send svg { display: block; }

/* ---------- Footer ---------- */
.sw-foot {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--line-subtle);
  background: var(--bg-sunken);
  font-size: 11px;
  color: var(--text-tertiary);
  font-family: var(--font-mono);
  letter-spacing: 0.02em;
  flex-shrink: 0;
}
.sw-foot .sw-escalate {
  display: inline-flex; align-items: center; gap: 6px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  padding: 4px 10px;
  font: 500 11px var(--font-sans);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background var(--d-fast), color var(--d-fast), border-color var(--d-fast);
}
.sw-foot .sw-escalate:hover {
  background: var(--bg-canvas);
  border-color: var(--line-strong);
  color: var(--text-primary);
}
.sw-foot .sw-escalate.escalated {
  background: color-mix(in oklab, var(--success-500) 12%, transparent);
  border-color: color-mix(in oklab, var(--success-500) 40%, var(--line));
  color: var(--success-700);
  cursor: default;
}
.sw-foot .sw-sla { white-space: nowrap; }

/* ---------- Mobile ---------- */
@media (max-width: 480px) {
  .sw-panel {
    bottom: 88px; right: 12px; left: 12px;
    width: auto; height: auto;
    max-height: calc(100vh - 110px);
  }
  .sw-trigger { right: 16px; bottom: 16px; }
}

/* ---------- Dark mode tweaks ---------- */
[data-theme="dark"] .sw-trigger { background: var(--slate-900); }
[data-theme="dark"] .sw-trigger:hover { background: var(--slate-850); }
[data-theme="dark"] .sw-msg.sam .bubble,
[data-theme="dark"] .sw-msg.human .bubble {
  background: var(--bg-raised);
  border-color: var(--line);
}
