/* ============================================================
   Xike Yang — Personal Academic Homepage  (V4)
   AI twin — a bottom-right launcher and the chat panel it opens.

   Not a modal. The visitor can keep reading and scrolling while it is
   open, so the panel stays small and never covers the page. It is
   stacked below the photo lightbox (z-index 1000) and the confetti
   layer (1500), so opening a photo or a celebration still owns the
   screen.

   All behaviour lives in js/ai-twin.js; this file is only the shell.
   ============================================================ */

/* ---------- 1. Tokens ---------- */
:root {
  --ai-twin-gap: 1.25rem;
  --ai-twin-size: 3.25rem;          /* launcher diameter (52px) */
  --ai-twin-width: 21rem;           /* panel width (336px) */
  /* Space from the bottom edge of the viewport to the bottom of the panel:
     the launcher, its gap, and a little air between the two. */
  --ai-twin-stack: calc(var(--ai-twin-size) + var(--ai-twin-gap) + 0.75rem);
}

/* ---------- 2. Launcher (bottom-right, always visible) ---------- */
.ai-twin-launcher {
  position: fixed;
  right: var(--ai-twin-gap);
  bottom: var(--ai-twin-gap);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--ai-twin-size);
  height: var(--ai-twin-size);
  padding: 0;
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(23, 38, 61, 0.26);
  transition: transform 0.25s var(--ease), box-shadow 0.25s var(--ease),
    background 0.25s var(--ease);
}

.ai-twin-launcher svg {
  width: 1.3rem;
  height: 1.3rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ai-twin-launcher:hover {
  transform: translateY(-2px);
  background: var(--accent);
  box-shadow: 0 10px 26px rgba(23, 38, 61, 0.32);
}

.ai-twin-launcher:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(43, 91, 139, 0.35), 0 6px 18px rgba(23, 38, 61, 0.26);
}

/* Once open, the launcher reads as the "close" affordance for the panel. */
.ai-twin-launcher[aria-expanded="true"] {
  background: var(--muted);
}

/* ---------- 3. Panel ---------- */
.ai-twin-panel {
  position: fixed;
  right: var(--ai-twin-gap);
  bottom: calc(var(--ai-twin-stack) + var(--ai-twin-gap));
  z-index: 900;
  display: flex;
  flex-direction: column;
  width: var(--ai-twin-width);
  max-width: calc(100vw - var(--ai-twin-gap) * 2);
  max-height: min(28rem, calc(100vh - var(--ai-twin-stack) - var(--ai-twin-gap)));
  max-height: min(28rem, calc(100svh - var(--ai-twin-stack) - var(--ai-twin-gap)));
  overflow: hidden;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 14px 40px rgba(23, 38, 61, 0.16);

  /* Closed state. visibility (not display) so the open/close can animate,
     and so a closed panel is out of the tab order and the a11y tree. */
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px) scale(0.98);
  transform-origin: bottom right;
  transition: opacity 0.22s var(--ease), transform 0.22s var(--ease),
    visibility 0.22s var(--ease);
}

.ai-twin-panel.is-open {
  opacity: 1;
  visibility: visible;
  transform: none;
}

/* ---------- 4. Head ---------- */
.ai-twin-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex: 0 0 auto;
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.ai-twin-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 1.9rem;
  height: 1.9rem;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 500;
  color: #fff;
  background: var(--accent);
  border-radius: 50%;
}

.ai-twin-title {
  display: grid;
  flex: 1 1 auto;
  min-width: 0;
  line-height: 1.3;
}

.ai-twin-title strong {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--primary);
}

.ai-twin-title small {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.03em;
  color: var(--muted);
}

.ai-twin-close {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 1.75rem;
  height: 1.75rem;
  font: inherit;
  font-size: 1.15rem;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.ai-twin-close:hover {
  color: var(--primary);
  background: var(--background);
}

/* ---------- 5. Message log (the only part that scrolls) ---------- */
.ai-twin-log {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  flex: 1 1 auto;
  min-height: 0;
  padding: 0.85rem 0.75rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--background);
}

.ai-twin-msg {
  max-width: 88%;
  padding: 0.5rem 0.7rem;
  font-size: 0.86rem;
  line-height: 1.5;
  border-radius: var(--radius-sm);
  /* Answers are written with real newlines - keep them. */
  white-space: pre-wrap;
  /* A long URL the visitor pastes must wrap, not widen the panel. */
  overflow-wrap: anywhere;
}

.ai-twin-msg.is-bot {
  align-self: flex-start;
  color: var(--foreground);
  background: var(--card);
  border: 1px solid var(--border);
}

.ai-twin-msg.is-me {
  align-self: flex-end;
  color: #fff;
  background: var(--accent);
}

.ai-twin-msg a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Typing indicator: three dots, replaced by the real answer. */
.ai-twin-typing {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.ai-twin-typing span {
  width: 0.3rem;
  height: 0.3rem;
  background: var(--muted);
  border-radius: 50%;
  animation: ai-twin-blink 1.2s var(--ease) infinite;
}

.ai-twin-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-twin-typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes ai-twin-blink {
  0%, 60%, 100% { opacity: 0.3; }
  30% { opacity: 1; }
}

/* ---------- 6. Quick picks (the "extract it for me" row) ---------- */
.ai-twin-quick {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  flex: 0 0 auto;
  padding: 0.6rem 0.7rem 0.15rem;
  border-top: 1px solid var(--border);
}

.ai-twin-chip {
  padding: 0.25rem 0.55rem;
  font: inherit;
  font-size: 0.74rem;
  line-height: 1.4;
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: border-color 0.2s var(--ease), background 0.2s var(--ease);
}

.ai-twin-chip:hover {
  border-color: var(--accent);
  background: var(--background);
}

/* ---------- 7. Input ---------- */
.ai-twin-form {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex: 0 0 auto;
  padding: 0.6rem 0.7rem 0.7rem;
}

.ai-twin-form input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.5rem 0.65rem;
  font: inherit;
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--foreground);
  background: var(--background);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.ai-twin-form input::placeholder {
  color: #9aa2b0;
}

.ai-twin-form input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(43, 91, 139, 0.12);
}

.ai-twin-send {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  color: #fff;
  background: var(--primary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s var(--ease);
}

.ai-twin-send svg {
  width: 1.05rem;
  height: 1.05rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ai-twin-send:disabled,
.ai-twin-chip:disabled {
  opacity: .5;
  cursor: wait;
}

.ai-twin-send:hover {
  background: var(--accent);
}

/* ---------- 8. Small screens: become a sheet, not a bubble ---------- */
@media (max-width: 480px) {
  :root {
    --ai-twin-gap: 0.9rem;
    --ai-twin-size: 3rem;
  }

  .ai-twin-panel {
    left: var(--ai-twin-gap);
    right: var(--ai-twin-gap);
    width: auto;
    max-width: none;
    /* Taller than the desktop panel - it is the only thing on screen. */
    max-height: min(72svh, calc(100svh - var(--ai-twin-stack) - var(--ai-twin-gap)));
  }

  .ai-twin-msg {
    max-width: 94%;
  }
}

/* ---------- 9. Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .ai-twin-launcher,
  .ai-twin-launcher:hover,
  .ai-twin-panel {
    transition: none;
    transform: none;
  }

  .ai-twin-typing span {
    animation: none;
    opacity: 0.55;
  }
}
