/* =========================================================
   Chat page. Colors follow the rest of the site.
   ========================================================= */
*,
*::before,
*::after { margin: 0; padding: 0; box-sizing: border-box; }

[hidden] { display: none !important; }

:root {
  --bg: #0f0a1f;
  --bg-raise: #161033;
  --line: rgba(196, 181, 253, 0.16);
  --text: #efeafd;
  --muted: #a99fd0;
  --accent: #7c3aed;
  --accent-soft: #a78bfa;
  --accent-ink: #c4b5fd;
  --user-bg: linear-gradient(135deg, #7c3aed, #5b21b6);
  --user-ink: #fff;
  --bot-bg: rgba(255, 255, 255, 0.05);
  --code-bg: rgba(0, 0, 0, 0.35);
  --dock-space: 7rem;
  --font: "Archivo", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #f5f3ff;
  --bg-raise: #ffffff;
  --line: rgba(76, 29, 149, 0.18);
  --text: #1b1530;
  --muted: #5a5080;
  --accent: #6d28d9;
  --accent-soft: #6d28d9;
  --accent-ink: #5b21b6;
  --bot-bg: #ffffff;
  --code-bg: rgba(76, 29, 149, 0.08);
  color-scheme: light;
}

html { background: var(--bg); -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.55;
  min-height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ===== drifting background glow ===== */
.glow { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.glow i {
  position: absolute;
  width: 46vmax; height: 46vmax;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.28;
  background: var(--accent);
  animation: drift 22s ease-in-out infinite alternate;
}
.glow i:nth-child(1) { top: -18vmax; left: -12vmax; }
.glow i:nth-child(2) { bottom: -22vmax; right: -14vmax; background: #4c1d95; animation-duration: 28s; animation-delay: -6s; }
.glow i:nth-child(3) { top: 30%; left: 55%; width: 26vmax; height: 26vmax; background: #a78bfa; opacity: 0.14; animation-duration: 34s; animation-delay: -12s; }
html[data-theme="light"] .glow i { opacity: 0.16; }

@keyframes drift {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to   { transform: translate3d(6vmax, 5vmax, 0) scale(1.15); }
}

/* ===== layout ===== */
.chat {
  position: relative;
  z-index: 1;
  width: min(820px, 100%);
  height: 100dvh;
  margin: 0 auto;
  padding: 1rem 1rem calc(var(--dock-space) + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  animation: enter 0.6s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

@keyframes enter {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}

.chat__head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.25rem 0.25rem 0.9rem;
}

.chat__title { display: flex; align-items: center; gap: 0.65rem; }
.chat__title h1 { font-size: 1.05rem; font-weight: 700; letter-spacing: 0.01em; }

.pulse {
  position: relative;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: #34d399;
}
.pulse::after {
  content: "";
  position: absolute; inset: 0;
  border-radius: 50%;
  background: #34d399;
  animation: ping 2s cubic-bezier(0, 0, 0.2, 1) infinite;
}
@keyframes ping { to { transform: scale(2.8); opacity: 0; } }

.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.2s, background 0.2s, transform 0.2s;
}
.icon-btn:hover { color: var(--text); background: var(--bot-bg); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .jump svg, .send svg {
  width: 20px; height: 20px;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ===== message log ===== */
.log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.5rem 0.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.empty {
  margin: auto;
  text-align: center;
  max-width: 34rem;
  animation: enter 0.7s 0.1s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
.empty h2 { font-size: clamp(1.8rem, 6vw, 2.6rem); font-weight: 900; letter-spacing: -0.02em; }
.empty p { margin-top: 0.4rem; color: var(--muted); }

.chips { margin-top: 1.4rem; display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: center; }
.chip {
  font: inherit; font-size: 0.9rem;
  color: var(--accent-ink);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.2s, border-color 0.2s;
}
.chip:hover { background: var(--bot-bg); border-color: var(--accent-soft); transform: translateY(-2px); }
.chip:active { transform: scale(0.97); }

.msg {
  display: flex;
  max-width: 100%;
  animation: pop 0.38s cubic-bezier(0.2, 0.9, 0.3, 1.15) both;
  transform-origin: bottom left;
}
.msg--user { justify-content: flex-end; transform-origin: bottom right; }

@keyframes pop {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}

.bubble {
  max-width: min(86%, 640px);
  padding: 0.7rem 1rem;
  border-radius: 20px;
  overflow-wrap: anywhere;
  font-size: 0.98rem;
}
.msg--user .bubble {
  background: var(--user-bg);
  color: var(--user-ink);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
  box-shadow: 0 10px 24px -14px rgba(124, 58, 237, 0.8);
}
.msg--bot .bubble {
  background: var(--bot-bg);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.msg--error .bubble { border-color: rgba(255, 69, 58, 0.55); color: #ff9b94; }
html[data-theme="light"] .msg--error .bubble { color: #b42318; }

/* rendered markdown */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0.55em 0; }
.bubble ul, .bubble ol { margin: 0.55em 0; padding-left: 1.3em; }
.bubble li { margin: 0.2em 0; }
.bubble a { color: var(--accent-ink); text-underline-offset: 3px; }
.bubble h3, .bubble h4 { margin: 0.8em 0 0.3em; font-size: 1.02em; }
.bubble code {
  font-family: var(--mono); font-size: 0.86em;
  background: var(--code-bg);
  padding: 0.12em 0.4em; border-radius: 6px;
}
.bubble pre {
  position: relative;
  margin: 0.7em 0;
  padding: 0.9rem 1rem;
  background: var(--code-bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow-x: auto;
  font-size: 0.85rem;
}
.bubble pre code { background: none; padding: 0; font-size: inherit; }
.copy {
  position: absolute; top: 0.4rem; right: 0.4rem;
  font: inherit; font-size: 0.72rem;
  color: var(--muted);
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}
.bubble pre:hover .copy, .copy:focus-visible { opacity: 1; }

/* blinking caret while text streams in */
.msg.is-live .bubble > :last-child::after {
  content: "";
  display: inline-block;
  width: 7px; height: 1.05em;
  margin-left: 3px;
  vertical-align: -0.15em;
  border-radius: 2px;
  background: var(--accent-soft);
  animation: blink 0.9s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* waiting dots */
.dots { display: inline-flex; gap: 5px; padding: 0.35rem 0.1rem; }
.dots i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent-soft);
  animation: bounce 1.1s ease-in-out infinite;
}
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 70%, 100% { transform: translateY(0); opacity: 0.45; }
  35% { transform: translateY(-6px); opacity: 1; }
}

/* ===== scroll-to-latest ===== */
.jump {
  position: absolute;
  right: 1.4rem;
  bottom: calc(var(--dock-space) + 5.2rem + env(safe-area-inset-bottom, 0px));
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-raise);
  color: var(--text);
  cursor: pointer;
  z-index: 2;
  box-shadow: 0 8px 20px -8px rgba(0, 0, 0, 0.6);
  animation: pop 0.25s both;
}

/* ===== composer ===== */
.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.5rem 0.5rem 0.5rem 1.1rem;
  background: var(--bg-raise);
  border: 1px solid var(--line);
  border-radius: 26px;
  box-shadow: 0 18px 40px -26px rgba(0, 0, 0, 0.8);
  transition: border-color 0.25s, box-shadow 0.25s;
}
.composer:focus-within {
  border-color: var(--accent-soft);
  box-shadow: 0 0 0 4px rgba(124, 58, 237, 0.18), 0 18px 40px -26px rgba(0, 0, 0, 0.8);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  max-height: 160px;
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.45;
  padding: 0.5rem 0;
}
.composer textarea::placeholder { color: var(--muted); }

.send {
  position: relative;
  flex: none;
  width: 42px; height: 42px;
  border: 0;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
  transition: transform 0.2s, opacity 0.2s, background 0.2s;
}
.send:hover { transform: scale(1.06); }
.send:active { transform: scale(0.92); }
.send:disabled { opacity: 0.35; cursor: default; transform: none; }
.send svg { position: absolute; transition: transform 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2), opacity 0.2s; }
.send__stop { opacity: 0; transform: scale(0.4) rotate(-90deg); fill: currentColor; }
.send.is-busy .send__go { opacity: 0; transform: translateY(-14px) scale(0.5); }
.send.is-busy .send__stop { opacity: 1; transform: none; }

.note {
  min-height: 1.2rem;
  margin-top: 0.5rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
}

:focus-visible { outline: 2px solid var(--accent-soft); outline-offset: 2px; }

@media (max-width: 520px) {
  .chat { padding-left: 0.7rem; padding-right: 0.7rem; }
  .bubble { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}