:root {
  --bg: #0d0618;
  --bg-2: #150a26;
  --panel: rgba(34, 18, 60, 0.55);
  --panel-solid: #1d1033;
  --border: rgba(196, 160, 255, 0.14);
  --border-strong: rgba(196, 160, 255, 0.28);
  --text: #efe8ff;
  --muted: #a998c9;
  --faint: #6f5f8f;
  --violet: #8b5cf6;
  --violet-2: #a855f7;
  --lilac: #c4b5fd;
  --orchid: #e879f9;
  --grad: linear-gradient(135deg, #c084fc 0%, #8b5cf6 50%, #6d28d9 100%);
  --radius: 18px;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --serif: "Instrument Serif", Georgia, "Times New Roman", serif;
  --mono: "JetBrains Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.65 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
::selection { background: rgba(168, 85, 247, 0.45); }

/* ---------- Background aurora ---------- */
.aurora { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.aurora span {
  position: absolute; border-radius: 50%; filter: blur(90px); opacity: 0.55;
  animation: drift 26s ease-in-out infinite alternate;
}
.aurora span:nth-child(1) { width: 55vmax; height: 55vmax; left: -15vmax; top: -20vmax; background: #5b21b6; }
.aurora span:nth-child(2) { width: 45vmax; height: 45vmax; right: -15vmax; top: 10vh; background: #86198f; opacity: 0.35; animation-duration: 32s; }
.aurora span:nth-child(3) { width: 40vmax; height: 40vmax; left: 25vw; bottom: -25vmax; background: #4c1d95; animation-duration: 38s; }
@keyframes drift { to { transform: translate(6vw, 4vh) scale(1.12); } }
@media (prefers-reduced-motion: reduce) { .aurora span { animation: none; } }

/* ---------- Brand mark (4-point star) ---------- */
.mark {
  width: 22px; height: 22px; background: var(--grad);
  clip-path: polygon(50% 0, 61% 39%, 100% 50%, 61% 61%, 50% 100%, 39% 61%, 0 50%, 39% 39%);
  filter: drop-shadow(0 0 10px rgba(192, 132, 252, 0.6));
}
.mark.big { width: 44px; height: 44px; margin: 0 auto 8px; }
.mark.huge { width: 64px; height: 64px; margin: 0 auto 26px; animation: pulse 4s ease-in-out infinite; }
@keyframes pulse { 50% { transform: rotate(45deg) scale(0.9); filter: drop-shadow(0 0 22px rgba(232, 121, 249, 0.8)); } }

/* ---------- Login ---------- */
.login { position: relative; z-index: 1; height: 100%; display: grid; place-items: center; padding: 16px; }
.login-form { width: min(300px, 100%); }
.login-form input {
  width: 100%; padding: 12px 16px; border-radius: 14px; text-align: center; letter-spacing: 0.2em;
  background: rgba(10, 4, 20, 0.55); border: 1px solid var(--border-strong); color: var(--text); font: inherit; outline: none;
  backdrop-filter: blur(20px); transition: border-color 0.2s, box-shadow 0.2s;
}
.login-form input:focus { border-color: var(--violet-2); box-shadow: 0 0 0 4px rgba(168, 85, 247, 0.22); }
.login-form.wrong input { border-color: #f472b6; }
.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* ---------- App shell ---------- */
.app { position: relative; z-index: 1; height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 20px 12px;
  border-bottom: 1px solid var(--border); background: rgba(13, 6, 24, 0.5); backdrop-filter: blur(16px);
}
.brand { display: flex; align-items: center; gap: 10px; font: 400 24px/1 var(--serif); letter-spacing: 0.01em; }
.actions { display: flex; gap: 6px; }
.btn-ghost {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 10px;
  background: transparent; border: 1px solid transparent; color: var(--muted); font-size: 13.5px;
}
.btn-ghost:hover { color: var(--text); background: rgba(139, 92, 246, 0.14); border-color: var(--border); }
.btn-ghost svg, .send svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.scroller { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.scroller::-webkit-scrollbar { width: 10px; }
.scroller::-webkit-scrollbar-thumb { background: rgba(139, 92, 246, 0.3); border-radius: 10px; border: 3px solid transparent; background-clip: padding-box; }

/* ---------- Hero ---------- */
.hero { max-width: 760px; margin: 0 auto; padding: 22vh 20px 40px; text-align: center; animation: rise 0.6s ease-out; }
.hero h1 {
  font: 400 clamp(38px, 7vw, 68px)/1.05 var(--serif); margin: 0; letter-spacing: -0.01em;
}
.hero h1 em {
  font-style: italic; background: linear-gradient(90deg, #d8b4fe, #e879f9, #a78bfa, #d8b4fe);
  background-size: 200% auto; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 6s linear infinite;
}
@keyframes shimmer { to { background-position: 200% center; } }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }

/* ---------- Messages ---------- */
.messages { max-width: 800px; margin: 0 auto; padding: 28px 20px 24px; display: flex; flex-direction: column; gap: 26px; }
.msg { animation: rise 0.25s ease-out; min-width: 0; }
.msg.user {
  align-self: flex-end; max-width: 82%; padding: 11px 16px; border-radius: 18px 18px 4px 18px;
  background: linear-gradient(135deg, rgba(139, 92, 246, 0.9), rgba(109, 40, 217, 0.9));
  box-shadow: 0 8px 28px rgba(109, 40, 217, 0.35); white-space: pre-wrap; word-wrap: break-word;
}
.msg.model { display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 14px; }
.msg.model > .mark { margin-top: 4px; }
.msg.model.streaming > .mark { animation: pulse 1.4s ease-in-out infinite; }
.msg.error .body { color: #f9a8d4; }
.meta { display: flex; gap: 8px; align-items: center; margin-top: 10px; color: var(--faint); font-size: 12px; }
.meta button { background: none; border: 0; color: var(--faint); padding: 2px 6px; border-radius: 6px; }
.meta button:hover { color: var(--lilac); background: rgba(139, 92, 246, 0.14); }

.typing { display: inline-flex; gap: 5px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--lilac); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.15s; } .typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }

/* ---------- Markdown ---------- */
.body { overflow-wrap: anywhere; }
.body > :first-child { margin-top: 0; }
.body > :last-child { margin-bottom: 0; }
.body p, .body ul, .body ol, .body blockquote, .body table, .body pre { margin: 0 0 14px; }
.body h1, .body h2, .body h3, .body h4 { font-weight: 600; line-height: 1.3; margin: 22px 0 10px; }
.body h1 { font-size: 1.5em; } .body h2 { font-size: 1.3em; } .body h3 { font-size: 1.12em; }
.body h1, .body h2 { font-family: var(--serif); font-weight: 400; font-size: 1.75em; color: #f3e8ff; }
.body ul, .body ol { padding-left: 1.4em; }
.body li { margin: 4px 0; }
.body li::marker { color: var(--violet-2); }
.body a { color: #d8b4fe; text-decoration: underline; text-decoration-color: rgba(216, 180, 254, 0.4); text-underline-offset: 3px; }
.body a:hover { text-decoration-color: currentColor; }
.body strong { color: #fff; }
.body hr { border: 0; height: 1px; background: var(--border-strong); margin: 22px 0; }
.body blockquote { border-left: 3px solid var(--violet); padding: 4px 0 4px 16px; color: var(--muted); background: rgba(139, 92, 246, 0.07); border-radius: 0 10px 10px 0; }
.body :not(pre) > code {
  font: 0.88em var(--mono); background: rgba(139, 92, 246, 0.18); color: #e9d5ff;
  padding: 0.15em 0.4em; border-radius: 6px; border: 1px solid rgba(196, 160, 255, 0.12);
}
.body .table-wrap { overflow-x: auto; margin: 0 0 14px; border: 1px solid var(--border); border-radius: 12px; }
.body table { border-collapse: collapse; width: 100%; font-size: 14px; margin: 0; }
.body th, .body td { padding: 9px 13px; text-align: left; border-bottom: 1px solid var(--border); }
.body th { background: rgba(139, 92, 246, 0.16); font-weight: 600; color: #f3e8ff; }
.body tr:last-child td { border-bottom: 0; }
.body tr:nth-child(even) td { background: rgba(139, 92, 246, 0.04); }
.body input[type="checkbox"] { accent-color: var(--violet); }
.body img { max-width: 100%; border-radius: 10px; }
.body .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

.codeblock { margin: 0 0 14px; border-radius: 14px; overflow: hidden; border: 1px solid var(--border); background: #120a20; }
.codeblock-head {
  display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 14px;
  background: rgba(139, 92, 246, 0.12); color: var(--muted); font: 12px var(--mono);
}
.codeblock-head button { background: none; border: 0; color: var(--muted); padding: 3px 8px; border-radius: 6px; font-size: 12px; }
.codeblock-head button:hover { color: var(--text); background: rgba(139, 92, 246, 0.2); }
.codeblock pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.codeblock pre code.hljs { background: none; padding: 0; font: 13.5px/1.6 var(--mono); }

/* ---------- Sources ---------- */
.sources { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 6px; }
.sources a {
  display: inline-flex; align-items: center; gap: 6px; max-width: 240px; padding: 4px 10px; border-radius: 999px;
  background: rgba(139, 92, 246, 0.12); border: 1px solid var(--border); color: var(--lilac);
  font-size: 12px; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sources a:hover { border-color: var(--border-strong); color: var(--text); }
.sources a b { color: var(--faint); font-weight: 500; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 8px 20px calc(10px + env(safe-area-inset-bottom, 0px)); }
.composer {
  max-width: 800px; margin: 0 auto; display: flex; align-items: flex-end; gap: 10px; padding: 8px 8px 8px 18px;
  background: rgba(29, 16, 51, 0.8); border: 1px solid var(--border-strong); border-radius: 22px;
  backdrop-filter: blur(20px); box-shadow: 0 16px 50px rgba(0, 0, 0, 0.4); transition: 0.2s;
}
.composer:focus-within { border-color: rgba(192, 132, 252, 0.6); box-shadow: 0 16px 50px rgba(0, 0, 0, 0.4), 0 0 0 4px rgba(139, 92, 246, 0.18); }
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none; background: transparent; color: var(--text);
  font: inherit; line-height: 1.5; padding: 8px 0; max-height: 220px;
}
.composer textarea::placeholder { color: var(--faint); }
.send {
  flex: none; width: 40px; height: 40px; border-radius: 14px; border: 0; display: grid; place-items: center;
  background: var(--grad); box-shadow: 0 6px 18px rgba(139, 92, 246, 0.45); transition: 0.15s;
}
.send:hover { filter: brightness(1.12); }
.send:disabled { opacity: 0.4; cursor: default; filter: none; }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.send.stop .i-send { display: none; }
.send.stop .i-stop { display: block; }
.fineprint { text-align: center; color: var(--faint); font-size: 11.5px; margin: 8px 0 0; }

@media (max-width: 560px) {
  .btn-ghost span { display: none; }
  .topbar { padding-left: 16px; padding-right: 12px; }
  .messages { padding: 20px 16px; }
  .composer-wrap { padding-left: 12px; padding-right: 12px; }
  .msg.user { max-width: 90%; }
  .msg.model { grid-template-columns: minmax(0, 1fr); }
  .msg.model > .mark { display: none; }
  .hero { padding-top: 8vh; }
}
