/* Ask Caledapt — the floating website assistant.
 *
 * WHY A NEW FILE AND NOT AN EDIT TO site.css
 * ------------------------------------------
 * Critique section 29: "RAG launcher should be lightweight before open" and
 * section 3: mobile must become a bottom SHEET, not a squeezed desktop widget.
 * Both need enough rules that folding them into site.css would make that file
 * unreadable, and the widget must be removable in one delete.
 *
 * Everything here uses the EXISTING design tokens (--ink-900, --caledapt-cyan,
 * --space-N, --font-*). No new palette, no purple default-AI gradient, no glow.
 * Those tokens are the visual contract from design-system.css.
 */

/* ── launcher ───────────────────────────────────────────────────────────── */

/* The launcher is a CIRCLE, not a pill. It rendered 64x56px because the
   horizontal padding reserved room for the "Ask Caledapt" label, so the box was
   never square and border-radius:999px produced a stadium shape. A circle needs
   a square box with the radius applied to all four corners.

   The label stays a hover/focus affordance on pointer devices, but it is
   absolutely positioned rather than laid out in flow, so revealing it can never
   change the button's geometry. On touch devices the circle is all that is
   rendered - the panel opens on tap, which is the interaction the label was
   advertising anyway. */
.rag-launcher {
  position: fixed;
  /* 20-28px from safe viewport edges, per the spec. env() keeps it clear of
     the iOS home indicator without hard-coding a model-specific inset. */
  right: max(22px, env(safe-area-inset-right, 0px));
  bottom: max(22px, env(safe-area-inset-bottom, 0px));
  z-index: 2147483000; /* above the sticky nav and the Academy sheet */

  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;  /* square: width == height, so the radius makes a true circle */
  height: 56px; /* 56 also clears the 44px WCAG touch target */
  min-width: 56px;
  min-height: 56px;
  padding: 0;  /* no horizontal padding - padding is what made it a stadium */
  position: fixed; /* re-asserted: a body-level "> *" rule can reset this */

  background: var(--ink-900);
  color: var(--text-primary);
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  font: 500 var(--t-sm) / 1 var(--font-body);
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .45);
  transition: border-color .18s ease, transform .18s ease, background-color .18s ease;
}

.rag-launcher:hover {
  border-color: var(--caledapt-cyan);
  background: var(--ink-850);
}

.rag-launcher:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 3px;
}

.rag-launcher:active { transform: translateY(1px); }

.rag-launcher__mark {
  width: 26px;
  height: 26px;
  flex: 0 0 auto;
  display: block;
}

/* The label is hidden until an INTENTIONAL hover/focus, per the spec. It is
   never hidden with display:none alone, because that would remove it from the
   accessibility tree on keyboard focus. */
.rag-launcher__label {
  position: absolute;
  right: calc(100% + var(--space-2));
  top: 50%;
  transform: translateY(-50%);
  padding: 0 var(--space-3);
  border-radius: 999px;
  background: var(--ink-900);
  border: 1px solid var(--line-strong);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
}

.rag-launcher:hover .rag-launcher__label,
.rag-launcher:focus-visible .rag-launcher__label {
  opacity: 1;
}

/* Touch devices never get the hover reveal, so show the label there instead of
   spending the width on an icon nobody can read. */
@media (hover: none) {
  /* Touch renders the circle only - the panel opens on tap. */
  .rag-launcher__label { display: none; }
}

/* ── panel ──────────────────────────────────────────────────────────────── */

.rag-panel {
  position: fixed;
  right: max(22px, env(safe-area-inset-right, 0px));
  bottom: calc(max(22px, env(safe-area-inset-bottom, 0px)) + 68px);
  z-index: 2147483001;

  /* 380-430px spec band */
  width: min(408px, calc(100vw - 32px));
  /* 70-78vh spec band */
  max-height: min(74vh, 620px);

  display: none;
  flex-direction: column;
  background: var(--ink-900);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg, 18px);
  box-shadow: 0 18px 54px rgba(0, 0, 0, .58);
  overflow: hidden;
}

.rag-panel[data-open="true"] { display: flex; }

.rag-panel__head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--line-soft);
  background: var(--ink-850);
}

.rag-panel__title {
  margin: 0;
  font: 600 var(--t-base) / 1.2 var(--font-body);
  color: var(--text-primary);
}

.rag-panel__sub {
  margin: 2px 0 0;
  font: 400 var(--t-xs) / 1.35 var(--font-body);
  color: var(--text-dim);
}

.rag-panel__close {
  margin-left: auto;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--line-soft);
  border-radius: 9px;
  cursor: pointer;
  font: 400 18px / 1 var(--font-body);
}

.rag-panel__close:hover { color: var(--text-primary); border-color: var(--line-strong); }
.rag-panel__close:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 2px;
}

.rag-panel__log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.rag-msg {
  max-width: 88%;
  padding: var(--space-2) var(--space-3);
  border-radius: 13px;
  font: 400 var(--t-sm) / 1.6 var(--font-body);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.rag-msg--bot {
  align-self: flex-start;
  background: var(--ink-800);
  color: var(--text-primary);
  border: 1px solid var(--line-soft);
  border-top-left-radius: 5px;
}

.rag-msg--user {
  align-self: flex-end;
  background: var(--caledapt-cyan);
  color: var(--text-on-cyan);
  border-top-right-radius: 5px;
}

.rag-msg--err {
  align-self: flex-start;
  background: transparent;
  color: var(--sun-gold);
  border: 1px solid rgba(222, 185, 141, .4);
}

.rag-msg a { color: var(--caledapt-cyan-bright); }

.rag-sources {
  align-self: flex-start;
  max-width: 92%;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.rag-sources a {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text-dim);
  font: 400 var(--t-xs) / 1.4 var(--font-body);
  text-decoration: none;
}

.rag-sources a:hover { color: var(--caledapt-cyan); border-color: var(--line-strong); }
.rag-sources a:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 2px;
}

.rag-offers {
  align-self: flex-start;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: calc(var(--space-1) * -1);
}

/* The opening suggestion chips. They used to share .rag-offers with the
 * answer's offer links, so a query for ".rag-offers button" matched both the
 * chips and a destination-less offer. They look the same, but they are a
 * different thing: a chip asks a question, an offer link navigates. */
.rag-suggestions {
  align-self: flex-start;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: calc(var(--space-1) * -1);
}

.rag-suggestions button {
  display: inline-block;
  padding: 8px 13px;
  min-height: 40px;
  background: transparent;
  color: var(--caledapt-cyan);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  font: 500 var(--t-xs) / 1.3 var(--font-body);
  cursor: pointer;
  text-align: left;
}

.rag-suggestions button:hover { background: rgba(25, 232, 222, .1); }
.rag-suggestions button:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 2px;
}

/* The offers are real <a> elements now, so they carry the same affordance
 * as the buttons they replaced. The shared rule covers both so a link and
 * a button can never drift apart visually. */
.rag-offers a,
.rag-offers button {
  display: inline-block;
  padding: 8px 13px;
  min-height: 40px;
  background: transparent;
  color: var(--caledapt-cyan);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  font: 500 var(--t-xs) / 1.3 var(--font-body);
  cursor: pointer;
  text-align: left;
  text-decoration: none;
}

.rag-offers a:hover,
.rag-offers button:hover { background: rgba(25, 232, 222, .1); }

.rag-offers a:focus-visible,
.rag-offers button:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 2px;
}

/* ── composer ───────────────────────────────────────────────────────────── */

.rag-form {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3);
  border-top: 1px solid var(--line-soft);
  background: var(--ink-850);
}

.rag-input {
  flex: 1 1 auto;
  min-width: 0; /* without this the flex item refuses to shrink and overflows */
  min-height: 42px;
  padding: 10px var(--space-3);
  background: var(--ink-900);
  color: var(--text-primary);
  border: 1px solid var(--line);
  border-radius: 11px;
  font: 400 var(--t-sm) / 1.4 var(--font-body);
}

.rag-input::placeholder { color: var(--text-dim); }

.rag-input:focus-visible {
  outline: 2px solid var(--caledapt-cyan-bright);
  outline-offset: 1px;
  border-color: var(--caledapt-cyan);
}

.rag-send {
  flex: 0 0 auto;
  min-width: 74px;
  min-height: 42px;
  padding: 0 var(--space-3);
  background: var(--caledapt-cyan);
  color: var(--text-on-cyan);
  border: 0;
  border-radius: 11px;
  font: 600 var(--t-xs) / 1 var(--font-body);
  cursor: pointer;
}

.rag-send:disabled { opacity: .45; cursor: not-allowed; }
.rag-send:focus-visible { outline: 2px solid var(--caledapt-cyan-bright); outline-offset: 2px; }

.rag-note {
  margin: 0;
  padding: 0 var(--space-3) var(--space-3);
  background: var(--ink-850);
  font: 400 11px / 1.45 var(--font-body);
  color: var(--text-dim);
}

.rag-note a { color: var(--text-muted); }

/* ── status / thinking ──────────────────────────────────────────────────── */

.rag-status {
  align-self: flex-start;
  font: 400 var(--t-xs) / 1.4 var(--font-body);
  color: var(--text-dim);
}

/* ── mobile: a bottom SHEET, not a squeezed widget ───────────────────────── */

@media (max-width: 600px) {
  .rag-panel {
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    /* Full-height conversational surface. The critique explicitly forbids
       compressing the 408px desktop panel into 320px. */
    max-height: 88vh;
    border-radius: 20px 20px 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .rag-launcher { min-height: 52px; }
}

/* ── reduced motion ─────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .rag-launcher,
  .rag-launcher__label {
    transition: none;
  }
  .rag-launcher:active { transform: none; }
}

/* ── print ──────────────────────────────────────────────────────────────── */

@media print {
  .rag-launcher,
  .rag-panel { display: none !important; }
}

/* ── high contrast / forced colours ─────────────────────────────────────── */

@media (forced-colors: active) {
  .rag-launcher,
  .rag-panel { border: 1px solid CanvasText; }
  .rag-msg--bot,
  .rag-msg--user { border: 1px solid CanvasText; }
}

/* ══ typing indicator ══════════════════════════════════════════════════════
   A person does not answer instantly. The previous single status line was
   swapped out the moment the response arrived, so a fast reply flickered and a
   slow one sat on a static string that read as a stalled request.

   These dots animate on the THEME accent, are reduced to a static marker under
   prefers-reduced-motion, and are hidden from assistive tech: the visually
   hidden "Caledapt is typing" text in the same live region is what gets
   announced, once, instead of three animated dots being read aloud.
   ─────────────────────────────────────────────────────────────────────────── */
.rag-typing {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 1.6em;
}

.rag-dots {
  display: inline-flex;
  gap: 0.28rem;
  align-items: center;
}

.rag-dot__dot {
  display: block;
  width: 0.38rem;
  height: 0.38rem;
  border-radius: 50%;
  background: var(--caledapt-cyan, currentColor);
  opacity: 0.35;
  animation: rag-dot-pulse 1.15s ease-in-out infinite;
}

.rag-dot:nth-child(2) .rag-dot__dot { animation-delay: 0.16s; }
.rag-dot:nth-child(3) .rag-dot__dot { animation-delay: 0.32s; }

@keyframes rag-dot-pulse {
  0%, 80%, 100% { opacity: 0.28; transform: translateY(0) scale(0.9); }
  40%           { opacity: 1;   transform: translateY(-2px) scale(1); }
}

/* Screen-reader-only. Visually hidden but NOT display:none, which would remove
   it from the accessibility tree entirely. */
.rag-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .rag-dot__dot {
    animation: none;
    opacity: 0.85;
    transform: none;
  }
}
