/* ===========================================================
   Chatbot window (bottom right) — uses the space.css design tokens
   =========================================================== */

/* keep the footer links clear of the launcher */
html.has-chatbot footer.site { padding-bottom: 84px; }

/* ---- launcher: robot chatbot icon ---- */
.cb-launcher {
  position: fixed; z-index: 900;
  right: max(20px, env(safe-area-inset-right));
  bottom: max(20px, env(safe-area-inset-bottom));
  display: inline-flex; align-items: center;
  height: 56px; min-width: 56px;
  padding: 0;
  color: var(--on-accent);
  background: var(--accent);
  border: 0; border-radius: 999px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28), 0 0 0 1px var(--glow);
  cursor: pointer;
  font: 500 0.8rem var(--mono);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
  animation: cbRing 2.6s ease-out 1.2s 3;
}
.cb-launcher[hidden] { display: none; }
.cb-launcher:hover { background: var(--accent-dim); transform: translateY(-2px); }
.cb-launcher:focus-visible, .cb-icon-btn:focus-visible, .cb-send:focus-visible, .cb-chip:focus-visible,
.cb-src:focus-visible, .cb-log:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.cb-ico { width: 56px; height: 56px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.cb-launch-label {
  max-width: 0; overflow: hidden; white-space: nowrap; opacity: 0;
  transition: max-width 0.25s ease, opacity 0.2s ease, padding 0.25s ease;
}
.cb-launcher:hover .cb-launch-label, .cb-launcher:focus-visible .cb-launch-label { max-width: 140px; opacity: 1; padding-right: 20px; }
@keyframes cbRing {
  0%   { box-shadow: 0 8px 28px rgba(0,0,0,0.28), 0 0 0 0 rgba(45,226,201,0.55); }
  70%  { box-shadow: 0 8px 28px rgba(0,0,0,0.28), 0 0 0 16px rgba(45,226,201,0); }
  100% { box-shadow: 0 8px 28px rgba(0,0,0,0.28), 0 0 0 0 rgba(45,226,201,0); }
}

/* ---- window ---- */
.cb-win {
  position: fixed; z-index: 900;
  right: max(20px, env(safe-area-inset-right));
  bottom: max(20px, env(safe-area-inset-bottom));
  width: min(392px, calc(100vw - 32px));
  height: min(600px, calc(100dvh - 40px));
  display: flex; flex-direction: column;
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.42), 0 0 0 1px var(--glow);
  overflow: hidden;
  animation: cbPop 0.2s cubic-bezier(0.2, 0.9, 0.3, 1.1);
}
.cb-win[hidden] { display: none; }
@keyframes cbPop { from { opacity: 0; transform: translateY(14px) scale(0.97); } to { opacity: 1; transform: none; } }

.cb-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 12px 12px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.cb-av {
  flex: none; width: 30px; height: 30px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--on-accent); background: var(--accent);
}
.cb-av svg { width: 19px; height: 19px; }
.cb-av-lg { width: 38px; height: 38px; }
.cb-av-lg svg { width: 24px; height: 24px; }
.cb-ttl { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.3; }
.cb-ttl strong { font: 700 0.9rem var(--mono); letter-spacing: -0.01em; }
.cb-ttl span { font: 400 0.7rem var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cb-icon-btn {
  flex: none; width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--muted); background: transparent;
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
}
.cb-icon-btn:hover { color: var(--accent); border-color: var(--line); background: var(--bg-soft); }

/* ---- messages ---- */
.cb-log {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 14px; display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.cb-msg { display: flex; gap: 8px; align-items: flex-start; max-width: 100%; }
.cb-user { justify-content: flex-end; }
.cb-bubble {
  max-width: 86%; min-width: 0;
  padding: 9px 13px; border-radius: 4px 14px 14px 14px;
  background: var(--surface); border: 1px solid var(--line);
  font: 300 0.88rem/1.65 var(--sans); color: var(--text-soft);
  overflow-wrap: anywhere;
}
.cb-user .cb-bubble {
  border-radius: 14px 4px 14px 14px;
  background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 400;
}
.cb-bubble p { margin: 0 0 8px; } .cb-bubble p:last-child { margin-bottom: 0; }
.cb-bubble ul, .cb-bubble ol { margin: 0 0 8px 1.15em; padding: 0; }
.cb-bubble li { margin: 3px 0; padding-left: 3px; }
.cb-bubble strong { color: var(--text); font-weight: 600; }
.cb-bubble code { font: 0.82em var(--mono); background: var(--bg); border: 1px solid var(--line); padding: 1px 5px; border-radius: 4px; color: var(--accent); }
.cb-cite {
  font: 600 0.62rem var(--mono); vertical-align: super; margin: 0 1px; padding: 0 3px; border-radius: 3px;
  color: var(--accent); background: var(--glow); text-decoration: none;
}
.cb-note { font-size: 0.82rem; color: var(--muted); }
.cb-detail { font: 400 0.68rem/1.4 var(--mono); color: var(--faint); word-break: break-word; }
.cb-sources { margin-top: 10px; padding-top: 9px; border-top: 1px dashed var(--line); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.cb-sources-label { font: 500 0.6rem var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--faint); }
.cb-src {
  display: inline-block; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font: 400 0.7rem var(--mono); color: var(--text-soft); text-decoration: none;
  border: 1px solid var(--line); border-radius: 999px; padding: 3px 9px; background: var(--bg);
}
.cb-src:hover { color: var(--accent); border-color: var(--accent); }
.cb-src b { color: var(--accent); font-weight: 600; margin-right: 6px; }
.cb-passages { list-style: none; margin: 8px 0 0 !important; display: grid; gap: 8px; }
.cb-passages li { margin: 0; padding: 0; }
.cb-passages li > span { display: block; margin-top: 3px; font-size: 0.76rem; color: var(--muted); line-height: 1.5; }

.cb-chips { display: flex; flex-direction: column; align-items: flex-start; gap: 7px; padding-left: 38px; }
.cb-chips-label { font: 500 0.62rem var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--faint); }
.cb-chip {
  text-align: left; max-width: 100%;
  font: 400 0.8rem/1.45 var(--sans); color: var(--text);
  padding: 8px 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
}
.cb-chip:hover { border-color: var(--accent); color: var(--accent); transform: translateX(2px); }

.cb-typing { display: inline-flex; gap: 4px; padding: 6px 2px; }
.cb-typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); opacity: 0.35; animation: cbDot 1.2s infinite ease-in-out; }
.cb-typing i:nth-child(2) { animation-delay: 0.15s; } .cb-typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes cbDot { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

/* ---- input ---- */
.cb-form {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px 6px; border-top: 1px solid var(--line); background: var(--bg-soft);
}
.cb-form textarea {
  flex: 1; min-height: 40px; max-height: 120px; resize: none;
  padding: 9px 12px; font: 400 0.9rem/1.5 var(--sans); color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; outline: 0;
}
.cb-form textarea:focus { border-color: var(--accent); }
.cb-form textarea::placeholder { color: var(--muted); }
.cb-send {
  flex: none; width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--on-accent); background: var(--accent); border: 0; border-radius: 10px; cursor: pointer;
  transition: background 0.15s ease;
}
.cb-send:hover { background: var(--accent-dim); }
.cb-send.is-stop { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.cb-foot { padding: 4px 14px 10px; font: 400 0.64rem/1.4 var(--mono); color: var(--faint); text-align: center; }

/* ---- phones: full screen ---- */
@media (max-width: 620px) {
  .cb-launcher { height: 52px; min-width: 52px; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); }
  .cb-ico { width: 52px; height: 52px; }
  .cb-launcher:hover .cb-launch-label { max-width: 0; opacity: 0; padding: 0; }
  .cb-win { inset: 0; width: 100%; height: 100dvh; border-radius: 0; border: 0; }
  .cb-form textarea { font-size: 16px; } /* no iOS zoom on focus */
  .cb-bubble { max-width: 90%; }
}

@media (prefers-reduced-motion: reduce) {
  .cb-launcher, .cb-win { animation: none; }
  .cb-typing i { animation: none; opacity: 0.7; }
  .cb-log { scroll-behavior: auto; }
}
@media print { .cb-launcher, .cb-win { display: none !important; } }
