/* English-practice chatbot: launch card + in-app overlay.
   Page-scoped (linked from eXam/practice_landing.html via {% block extra_head %});
   plain CSS so no Tailwind rebuild is needed. The overlay shell mirrors
   20-simulate.css; the launch card reuses 06-landing.css's .practice-card (the
   cyan accent comes from --accent-rgb set inline in the template). */

/* ── Launch card (a <button> wearing .practice-card; reset button defaults) ──────── */
.chat-launch {
  width: 100%;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

/* Compact inline launcher for the subject-tutor button in /know + /learn headers
   (sits beside the handout / simulate buttons). Overrides the full-width card. */
.chat-launch-inline {
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.625rem;
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1;
  border-radius: 0.375rem;
  border: 1px solid rgba(167, 139, 250, 0.30);
  background: rgba(167, 139, 250, 0.10);
  color: #ddd6fe;
  white-space: nowrap;
  transition: background 0.15s;
}
.chat-launch-inline:hover { background: rgba(167, 139, 250, 0.20); }
.chat-launch-inline svg { width: 0.9rem; height: 0.9rem; flex-shrink: 0; }
@media (prefers-reduced-motion: reduce) { .chat-launch-inline { transition: none; } }

/* ── Overlay shell (clone of .sim-overlay) ──────────────────────────────────────── */
.chat-overlay {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.chat-overlay[hidden] { display: none; }
.chat-overlay-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 23, 0.74);
  backdrop-filter: blur(2px);
}

.chat-modal {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: min(640px, 96vw);
  height: min(80vh, 720px);
  background: #0b1120;
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 1rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.chat-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 0.5rem 0.6rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  flex-shrink: 0;
}
.chat-modal-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: #e2e8f0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.5rem;
  color: #94a3b8;
  background: transparent;
  border: 0;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}
.chat-modal-close svg { width: 1.1rem; height: 1.1rem; }
.chat-modal-close:hover { background: rgba(255, 255, 255, 0.08); color: #e2e8f0; }

/* ── Body: message log (scrolls) + starter chips + input bar (pinned) ───────────── */
.chat-modal-body {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1 1 auto;
}

.chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1rem;
}

.chat-bubble {
  max-width: 85%;
  padding: 0.5rem 0.75rem;
  border-radius: 0.9rem;
  font-size: 0.9rem;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.chat-user {
  align-self: flex-end;
  background: rgba(34, 211, 238, 0.16);
  color: #e0f2fe;
  border-bottom-right-radius: 0.25rem;
}
.chat-assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.06);
  color: #e2e8f0;
  border-bottom-left-radius: 0.25rem;
}
.chat-typing { color: #94a3b8; animation: chat-pulse 1.2s ease-in-out infinite; }
@keyframes chat-pulse { 0%, 100% { opacity: 0.45; } 50% { opacity: 1; } }

/* Starter chips — only shown until the first user message (hidden via [hidden] in JS). */
.chat-starters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0 1rem 0.75rem;
  flex-shrink: 0;
}
.chat-starters[hidden] { display: none; }
.chat-chip {
  font: inherit;
  font-size: 0.78rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid rgba(34, 211, 238, 0.30);
  background: rgba(34, 211, 238, 0.08);
  color: #a5f3fc;
  cursor: pointer;
  transition: background 0.15s;
}
.chat-chip:hover { background: rgba(34, 211, 238, 0.16); }

.chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  flex-shrink: 0;
}
.chat-input {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.55rem 0.8rem;
  border-radius: 0.6rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: #0f172a;
  color: #e2e8f0;
}
.chat-input:focus { outline: none; border-color: rgba(34, 211, 238, 0.55); }
.chat-input:disabled { opacity: 0.6; }
.chat-send {
  flex-shrink: 0;
  font: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  padding: 0.55rem 1.1rem;
  border-radius: 0.6rem;
  border: 0;
  background: rgba(34, 211, 238, 0.9);
  color: #042f3a;
  cursor: pointer;
  transition: background 0.15s;
}
.chat-send:hover { background: #67e8f9; }
.chat-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Mobile: full-screen; use dvh so the input bar stays above the keyboard. ─────── */
@media (max-width: 640px) {
  .chat-overlay { padding: 0; }
  .chat-modal {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border: 0;
    border-radius: 0;
    /* Safe area: header clears the notch, input bar clears the home indicator. */
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-modal-close, .chat-chip, .chat-send { transition: none; }
  .chat-typing { animation: none; opacity: 0.7; }
}

/* ── Interlinear pinyin + English on the Chinese-Buddy reply (hover / tap) ──────────
   The reply is server-rendered, fully-escaped .lx-* markup (web/zh_annotate.py) — the SAME
   component the Chinese-news reader uses. Default = clean hanzi (pinyin row reserved but
   hidden); hover, or tap (.is-open, set by chatbot.js), reveals the ruby pinyin in place +
   a faint English gloss above the word. Self-contained copy of 24-reading.css's .lx-* block,
   scoped to .chat-annotated so it can't leak. */
.chat-annotated { line-height: 2.1; }
.chat-annotated .lx-zh { cursor: default; }
.chat-annotated .lx-w {
  position: relative; display: inline-block; text-align: center; vertical-align: bottom;
  padding: 0.1rem 0.04em 0; border-radius: 0.3rem;
}
/* On hover/tap, a SUBTLE 1px ring (no fill) around the word — ties its pinyin + English to this 汉字. */
.chat-annotated .lx-w:hover, .chat-annotated .lx-w.is-open { box-shadow: 0 0 0 1px rgba(148, 163, 184, 0.4); }
.chat-annotated .lx-w ruby { font-size: 1.3rem; line-height: 1.5; color: #eef2f7; }
.chat-annotated .lx-w rt { font-size: 0.46em; font-weight: 600; line-height: 1; padding-bottom: 0.3em; visibility: hidden; }
.chat-annotated .lx-w-x { color: #e2e8f0; font-size: 1.3rem; }   /* punctuation / latin run */
.chat-annotated .lx-w-en {   /* SOLID deep-blue box; a long gloss wraps within a bounded width (grows upward) → never cut off */
  position: absolute; left: 50%; bottom: 100%; transform: translateX(-50%);
  visibility: hidden; pointer-events: none; margin-bottom: 0.3rem;
  width: max-content; max-width: min(15rem, 80vw); white-space: normal; overflow-wrap: break-word;
  text-align: center; line-height: 1.2; font-size: 0.64rem; color: #c4d0e6;
  background: #172546; border: 1px solid rgba(96, 165, 250, 0.4);
  border-radius: 0.3rem; padding: 0.1rem 0.4rem; box-shadow: 0 1px 5px rgba(0, 0, 0, 0.6);
}
/* …and ALL words on keyboard focus of the sentence (.lx-zh has tabindex=0), mirroring
   the canonical 25-annotate-zh.css so keyboard users get pinyin/gloss in chat replies too. */
.chat-annotated .lx-w:hover rt, .chat-annotated .lx-w.is-open rt, .chat-annotated .lx-zh:focus rt { visibility: visible; }
.chat-annotated .lx-w:hover .lx-w-en, .chat-annotated .lx-w.is-open .lx-w-en, .chat-annotated .lx-zh:focus .lx-w-en { visibility: visible; }
/* Copy yields ONLY the 汉字 — pinyin (rt) + English gloss never enter the selection. */
.chat-annotated .lx-w rt, .chat-annotated .lx-w-en { -webkit-user-select: none; user-select: none; }
.chat-annotated .lx-t1 { color: #f87171; } .chat-annotated .lx-t2 { color: #fbbf24; }
.chat-annotated .lx-t3 { color: #34d399; } .chat-annotated .lx-t4 { color: #60a5fa; }
.chat-annotated .lx-t0 { color: #94a3b8; }

/* ════════════════════════════════════════════════════════════════════════════════
   DOCKED VARIANT (.chat-overlay--docked) — the content-attached tutor (/learn, /know).
   A right-side panel that does NOT overlap or blur the content, plus a minimise → floating
   symbol (.chat-fab). All class-gated so the practice-page centered modal is unaffected.
   ════════════════════════════════════════════════════════════════════════════════ */
:root { --lx-chat-w: 420px; }   /* single source: panel width, content reflow reserve, feedback-button clearance */

/* Header action group — minimise + close sit together on the right. */
.chat-modal-actions { display: flex; align-items: center; gap: 0.125rem; flex-shrink: 0; }
.chat-modal-min {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.5rem;
  color: #94a3b8;
  background: transparent;
  border: 0;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.chat-modal-min svg { width: 1.1rem; height: 1.1rem; }
.chat-modal-min:hover { background: rgba(255, 255, 255, 0.08); color: #e2e8f0; }

/* ── Desktop (≥1024px): dock as a right strip; reflow the lesson so nothing is covered. ── */
@media (min-width: 1024px) {
  /* Single class on purpose: .chat-overlay[hidden]{display:none} (higher specificity) still
     wins the hide; this beats base .chat-overlay (flex, inset:0) by source order. */
  .chat-overlay--docked {
    inset: var(--lx-chat-top, 3.5rem) 0 0 auto;
    width: var(--lx-chat-w, 420px);
    display: block;
    padding: 0;
  }
  .chat-overlay--docked .chat-overlay-backdrop { display: none; }   /* no dim, no blur */
  .chat-overlay--docked .chat-modal {
    width: 100%;
    height: 100%;
    border: 0;
    border-left: 1px solid rgba(255, 255, 255, 0.10);
    border-radius: 0;
    box-shadow: -12px 0 40px rgba(0, 0, 0, 0.45);
  }
  /* Reserve the panel's space by padding the whole content column (.z-content holds header +
     main + footer). The page shifts left as a unit and #site-main stays centered in the
     remaining space, so the fixed panel never overlaps the lesson. Padding the flex COLUMN
     works cleanly; a margin on the flex-item #site-main would make it shrink-to-fit its
     content (a narrow column with a big empty gutter) instead of filling the space. */
  html.chat-docked-open .z-content {
    padding-right: var(--lx-chat-w, 420px);
    transition: padding-right 0.2s ease;
  }
  /* Keep the global feedback button/dialog (23-feedback.css, fixed bottom-right) clear of the
     open panel: shift them to its LEFT so they never cover the chat's Send button. */
  html.chat-docked-open .feedback-launcher,
  html.chat-docked-open .feedback-dialog {
    right: calc(1rem + var(--lx-chat-w, 420px));
  }
}

/* ── Below 1024px: no side-by-side room → full-width sheet (still NO blur); minimise to read. ── */
@media (max-width: 1023.98px) {
  .chat-overlay--docked { display: block; padding: 0; }            /* base inset:0 stays → full */
  .chat-overlay--docked .chat-overlay-backdrop { display: none; }
  .chat-overlay--docked .chat-modal {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border: 0;
    border-radius: 0;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  /* Full-width sheet covers the whole viewport → hide the global feedback button while open. */
  html.chat-docked-open .feedback-launcher { display: none; }
}

/* ── Minimised floating symbol: mirrors .feedback-launcher (23-feedback.css) but a graduation-cap
   icon in CYAN, stacked ABOVE the violet feedback button so they never overlap or get confused. ── */
.chat-fab {
  position: fixed;
  right: calc(1rem + env(safe-area-inset-right, 0px));
  bottom: calc(4.25rem + env(safe-area-inset-bottom, 0px));   /* above the feedback launcher + clears the home indicator */
  z-index: 60;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 3rem;
  height: 3rem;
  border-radius: 999px;
  border: 1px solid rgba(34, 211, 238, 0.40);
  background: rgba(15, 23, 42, 0.78);
  color: #a5f3fc;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(2, 6, 23, 0.45);
  backdrop-filter: blur(6px);
  opacity: 0.85;
  transition: opacity 0.18s, transform 0.18s, background 0.18s, color 0.18s, box-shadow 0.18s;
}
.chat-fab[hidden] { display: none; }
.chat-fab svg { width: 1.4rem; height: 1.4rem; }
.chat-fab:hover,
.chat-fab:focus-visible {
  opacity: 1;
  transform: translateY(-2px);
  background: rgba(34, 211, 238, 0.92);
  color: #042f3a;
  box-shadow: 0 12px 30px rgba(34, 211, 238, 0.35);
  outline: none;
}
@media (max-width: 640px) {
  .chat-fab {
    width: 2.85rem; height: 2.85rem;
    right: calc(0.85rem + env(safe-area-inset-right, 0px));
    bottom: calc(3.95rem + env(safe-area-inset-bottom, 0px));
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-modal-min, .chat-fab { transition: none; }
  .chat-fab:hover, .chat-fab:focus-visible { transform: none; }
  html.chat-docked-open .z-content { transition: none; }
}
