/* iQ AI — clean, crisp, light/dark. Tokens first; everything else reads them. */
:root {
  --bg: #ffffff;
  --bg-soft: #f6f6f8;
  --surface: #ffffff;
  --surface-2: #f2f2f5;
  --line: #e6e6eb;
  --line-strong: #d4d4dc;
  --text: #0e0e12;
  --text-2: #55555f;
  --text-3: #8c8c96;
  --accent: #6d4aff;
  --accent-2: #1fb6e0;
  --accent-ink: #ffffff;
  --user-bg: #f0eefe;
  --user-text: #1b1340;
  --code-bg: #f6f6f9;
  --danger: #d6344b;
  --shadow: 0 1px 2px rgba(16, 16, 24, .04), 0 8px 28px rgba(16, 16, 24, .07);
  --glow: rgba(109, 74, 255, .16);
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI Variable", "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0b0d; --bg-soft: #111114; --surface: #141418; --surface-2: #1b1b20; --line: #24242b; --line-strong: #34343d;
    --text: #f2f2f5; --text-2: #a9a9b3; --text-3: #71717b; --accent: #8b72ff; --accent-2: #3cc8ee; --accent-ink: #0b0b0d;
    --user-bg: #1e1a33; --user-text: #ece8ff; --code-bg: #121216; --danger: #ff6b80;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35); --glow: rgba(139, 114, 255, .22);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0b0d; --bg-soft: #111114; --surface: #141418; --surface-2: #1b1b20; --line: #24242b; --line-strong: #34343d;
  --text: #f2f2f5; --text-2: #a9a9b3; --text-3: #71717b; --accent: #8b72ff; --accent-2: #3cc8ee; --accent-ink: #0b0b0d;
  --user-bg: #1e1a33; --user-text: #ece8ff; --code-bg: #121216; --danger: #ff6b80;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35); --glow: rgba(139, 114, 255, .22);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 15.5px/1.6 var(--font);
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-rendering: optimizeLegibility;
  overflow: hidden;
}
.theming, .theming * { transition: background-color .3s ease, color .3s ease, border-color .3s ease !important; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- The mark: a ring that spins while generating ---- */
.mark { position: relative; display: grid; place-items: center; flex: none; }
.mark-ring {
  position: absolute; inset: 0; border-radius: 50%;
  background: conic-gradient(from 0deg, var(--accent), var(--accent-2), var(--accent));
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 2.5px), #000 calc(100% - 2px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 2.5px), #000 calc(100% - 2px));
}
.mark-core { font-weight: 750; letter-spacing: -.02em; color: var(--text); }
.mark-sm { width: 26px; height: 26px; } .mark-sm .mark-core { font-size: 10.5px; }
.mark-md { width: 52px; height: 52px; } .mark-md .mark-core { font-size: 18px; }
.mark-lg { width: 84px; height: 84px; } .mark-lg .mark-core { font-size: 29px; }
.mark-lg .mark-ring { animation: spin 7s linear infinite; }
.mark.busy .mark-ring { animation: spin 1.1s linear infinite; }
.mark.busy { animation: breathe 1.6s ease-in-out infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes breathe { 50% { transform: scale(.9); } }

/* ---- Splash ---- */
.splash {
  position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 24px 16px;
  background: var(--bg); transition: opacity .55s ease, transform .55s cubic-bezier(.2, .7, .2, 1), filter .55s ease;
}
.splash.leaving { opacity: 0; transform: scale(1.04); filter: blur(6px); pointer-events: none; }
.splash-glow {
  position: absolute; width: min(620px, 120vw); aspect-ratio: 1; border-radius: 50%;
  background: radial-gradient(closest-side, var(--glow), transparent 70%); animation: drift 9s ease-in-out infinite; pointer-events: none;
}
@keyframes drift { 50% { transform: translate(3%, -4%) scale(1.08); } }
.splash-inner { position: relative; width: min(340px, 100%); display: flex; flex-direction: column; align-items: center; text-align: center; animation: rise .8s cubic-bezier(.2, .7, .2, 1) both; }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }
.splash-title { margin: 22px 0 4px; font-size: 34px; font-weight: 750; letter-spacing: -.035em; }
.splash-title span { background: linear-gradient(100deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.splash-sub { margin: 0 0 26px; color: var(--text-2); font-size: 14.5px; }
.unlock {
  width: 100%; display: flex; gap: 6px; padding: 5px; border: 1px solid var(--line-strong); border-radius: 14px;
  background: var(--surface); box-shadow: var(--shadow); transition: border-color .2s, box-shadow .2s;
}
.unlock:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--glow), var(--shadow); }
.unlock input { flex: 1; min-width: 0; border: 0; background: none; color: var(--text); font: inherit; font-size: 16px; padding: 8px 10px; outline: none; }
.unlock button {
  width: 40px; height: 40px; border: 0; border-radius: 10px; display: grid; place-items: center;
  background: var(--text); color: var(--bg); transition: transform .15s, opacity .2s;
}
.unlock button:active { transform: scale(.94); }
.unlock button:disabled { opacity: .5; }
.unlock.shake { animation: shake .42s cubic-bezier(.36, .07, .19, .97); border-color: var(--danger); }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }
.splash-error { min-height: 22px; margin: 12px 0 0; color: var(--danger); font-size: 13.5px; }
.splash-theme { position: absolute; top: max(14px, env(safe-area-inset-top)); right: 16px; }

/* ---- App shell ---- */
.app { height: 100dvh; display: flex; flex-direction: column; animation: appear .6s ease both; }
@keyframes appear { from { opacity: 0; } }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px; border-bottom: 1px solid transparent; transition: border-color .2s;
}
.topbar.scrolled { border-bottom-color: var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -.01em; }
.top-actions { display: flex; gap: 4px; }
.icon-btn, .theme-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid transparent; background: none; color: var(--text-2);
  display: grid; place-items: center; transition: background .15s, color .15s;
}
.icon-btn:hover, .theme-btn:hover { background: var(--surface-2); color: var(--text); }

.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; scroll-behavior: auto; }
.thread { width: min(760px, 100%); margin: 0 auto; padding: 12px 16px 36px; }

/* Empty state */
.empty { min-height: 58vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; animation: rise .6s cubic-bezier(.2, .7, .2, 1) both; }
.empty h2 { margin: 18px 0 2px; font-size: 26px; font-weight: 700; letter-spacing: -.03em; }
.empty p { margin: 0 0 24px; color: var(--text-2); }
.chips { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; width: min(560px, 100%); }
.chip {
  text-align: left; padding: 12px 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); font-size: 14px; line-height: 1.35; transition: border-color .15s, transform .15s, box-shadow .15s;
}
.chip small { display: block; color: var(--text-3); font-size: 12.5px; margin-top: 2px; }
.chip:hover { border-color: var(--line-strong); box-shadow: var(--shadow); transform: translateY(-1px); }
@media (max-width: 520px) { .chips { grid-template-columns: 1fr; } .chip:nth-child(n+3) { display: none; } }

/* Messages */
.msg { margin: 22px 0; animation: msgIn .35s cubic-bezier(.2, .7, .2, 1) both; }
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  max-width: min(85%, 600px); padding: 10px 16px; border-radius: 20px 20px 6px 20px; background: var(--user-bg); color: var(--user-text);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.ai { display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 14px; }
.msg.ai .mark { margin-top: 2px; }
.body { min-width: 0; }

/* Thinking indicator */
.thinking { display: flex; align-items: center; gap: 10px; height: 28px; color: var(--text-2); font-size: 14.5px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: wave 1.2s ease-in-out infinite; }
.dots i:nth-child(2) { animation-delay: .15s; background: color-mix(in srgb, var(--accent) 50%, var(--accent-2)); }
.dots i:nth-child(3) { animation-delay: .3s; background: var(--accent-2); }
@keyframes wave { 0%, 60%, 100% { transform: translateY(0); opacity: .45; } 30% { transform: translateY(-5px); opacity: 1; } }
.shimmer {
  background: linear-gradient(90deg, var(--text-3) 0%, var(--text) 45%, var(--text-3) 90%); background-size: 220% 100%;
  -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 1.8s linear infinite;
}
@keyframes shimmer { from { background-position: 120% 0; } to { background-position: -120% 0; } }

/* Streaming text: each new word fades up from a soft blur */
.w { animation: word .42s cubic-bezier(.2, .7, .2, 1) both; }
@keyframes word { from { opacity: 0; filter: blur(3px); transform: translateY(.18em); } }
.streaming .md > :last-child::after, .streaming .md:empty::after {
  content: ""; display: inline-block; width: .5em; height: .5em; margin-left: .3em; border-radius: 50%; vertical-align: .05em;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { transform: scale(.55); opacity: .5; } }

/* Markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .85em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.3em 0 .5em; line-height: 1.3; letter-spacing: -.015em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.07em; } .md h4 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .85em; padding-left: 1.4em; } .md li { margin: .25em 0; } .md li::marker { color: var(--text-3); }
.md strong { font-weight: 650; }
.md a { color: var(--accent); text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent); text-underline-offset: 3px; }
.md blockquote { margin: 0 0 .85em; padding: .1em 0 .1em 1em; border-left: 3px solid var(--line-strong); color: var(--text-2); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.4em 0; }
.md :not(pre) > code { font: .88em var(--mono); background: var(--surface-2); border: 1px solid var(--line); padding: .1em .38em; border-radius: 6px; }
.md .code { margin: 0 0 .95em; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--code-bg); }
.md .code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--line); color: var(--text-3); font-size: 12.5px; }
.md .code-head button { border: 0; background: none; color: var(--text-2); font-size: 12.5px; padding: 3px 8px; border-radius: 7px; }
.md .code-head button:hover { background: var(--surface-2); color: var(--text); }
.md pre { margin: 0; padding: 12px 14px; overflow-x: auto; font: 13.5px/1.55 var(--mono); }
.md .table { overflow-x: auto; margin: 0 0 .95em; border: 1px solid var(--line); border-radius: 12px; }
.md table { border-collapse: collapse; width: 100%; font-size: .94em; }
.md th, .md td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); font-weight: 600; } .md tr:last-child td { border-bottom: 0; }

/* Notices and actions */
.notice { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-soft); color: var(--text-2); font-size: 14.5px; }
.notice.error { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); color: var(--danger); }
.notice code { font: .9em var(--mono); color: var(--text); }
.actions { display: flex; gap: 2px; margin-top: 8px; margin-left: -6px; opacity: 0; transition: opacity .2s; }
.msg.ai:hover .actions, .msg.ai:last-child .actions, .actions:focus-within { opacity: 1; }
.actions button { display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; color: var(--text-3); font-size: 12.5px; padding: 5px 8px; border-radius: 8px; }
.actions button:hover { background: var(--surface-2); color: var(--text); }
@media (hover: none) { .actions { opacity: 1; } }

/* Composer */
.composer-wrap { position: relative; width: min(760px, 100%); margin: 0 auto; padding: 0 16px max(10px, env(safe-area-inset-bottom)); }
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 8px 8px 16px; border: 1px solid var(--line-strong); border-radius: 22px;
  background: var(--surface); box-shadow: var(--shadow); transition: border-color .2s, box-shadow .2s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--line-strong)); box-shadow: 0 0 0 4px var(--glow), var(--shadow); }
.composer textarea {
  flex: 1; min-width: 0; max-height: 200px; resize: none; border: 0; outline: none; background: none; color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.5; padding: 7px 0;
}
.composer textarea::placeholder { color: var(--text-3); }
.send {
  width: 38px; height: 38px; flex: none; border: 0; border-radius: 50%; display: grid; place-items: center;
  background: var(--text); color: var(--bg); transition: transform .15s, opacity .2s, background .2s;
}
.send:disabled { opacity: .25; cursor: default; }
.send:not(:disabled):active { transform: scale(.92); }
.send .i-stop { display: none; }
.busy .send .i-send { display: none; } .busy .send .i-stop { display: block; }
.busy .send { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; opacity: 1; }
.fineprint { margin: 8px 0 0; text-align: center; color: var(--text-3); font-size: 12px; }
.to-bottom {
  position: absolute; left: 50%; top: -48px; transform: translateX(-50%); width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface); color: var(--text-2); box-shadow: var(--shadow); display: grid; place-items: center;
  animation: appear .2s ease both;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
