:root {
  --bg: #0a0d12;
  --bg2: #11161f;
  --panel: #161c28;
  --panel2: #1a2030;
  --line: #232b3a;
  --line2: #2c364a;
  --text: #e7ecf3;
  --muted: #8a93a4;
  --accent: #7cf06b;
  --accent2: #ffd23f;
  --bot: #1a2233;
  --danger: #ff7676;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --composer-h: 76px;
  --sidebar-w: 280px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  overscroll-behavior-y: none;
  -webkit-text-size-adjust: 100%;
}
body {
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(124,240,107,.08), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(255,210,63,.07), transparent 60%),
    var(--bg);
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
  height: 100vh;
}

/* ---------- sidebar ---------- */
.sidebar {
  background: var(--bg2);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  min-width: 0;
  padding-top: var(--safe-top);
}
.side-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 8px;
  border-bottom: 1px solid var(--line);
  gap: 8px;
}
.side-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.side-logo {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, #2a3447, #0f1521);
  display: grid; place-items: center; font-size: 18px;
  box-shadow: inset 0 0 0 1px var(--line);
}
.side-name {
  font-weight: 600; font-size: 15px;
  letter-spacing: .2px;
}
.icon-btn {
  background: transparent; color: var(--muted);
  border: 1px solid var(--line); border-radius: 9px;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  transition: color .15s, border-color .15s;
}
.icon-btn:hover, .icon-btn:active { color: var(--text); border-color: var(--line2); }
.icon-btn svg { width: 18px; height: 18px; }
.only-mobile { display: none; }

.new-chat {
  display: flex; align-items: center; gap: 8px;
  margin: 10px 12px; padding: 10px 12px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  cursor: pointer; font: inherit; font-size: 14px;
  transition: border-color .15s, background .15s;
  -webkit-tap-highlight-color: transparent;
}
.new-chat:hover { border-color: var(--accent); background: var(--panel2); }
.new-chat svg { width: 16px; height: 16px; flex-shrink: 0; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
  display: flex; flex-direction: column; gap: 2px;
}
.conv-item {
  position: relative;
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  color: var(--text); font-size: 13.5px;
  border: 1px solid transparent;
  -webkit-tap-highlight-color: transparent;
}
.conv-item:hover { background: var(--panel); }
.conv-item.active {
  background: var(--panel);
  border-color: var(--line2);
}
.conv-item .title {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .actions {
  display: none; gap: 4px;
}
.conv-item:hover .actions,
.conv-item.active .actions { display: flex; }
.conv-item .actions button {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; padding: 4px; border-radius: 6px;
  display: grid; place-items: center;
  -webkit-tap-highlight-color: transparent;
}
.conv-item .actions button:hover { color: var(--text); background: var(--bg2); }
.conv-item .actions svg { width: 14px; height: 14px; }
.conv-item .title-input {
  flex: 1; min-width: 0;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line2); border-radius: 6px;
  padding: 3px 6px; font: inherit; font-size: 13.5px;
  outline: none;
}
.conv-empty {
  color: var(--muted); font-size: 12.5px;
  text-align: center; padding: 16px 12px;
}

.side-foot {
  padding: 10px 14px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  color: var(--muted); font-size: 11px;
}

/* ---------- main area ---------- */
.main {
  display: flex; flex-direction: column;
  min-width: 0;
  height: 100%;
  position: relative;
}

.top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(10,13,18,.75);
  position: sticky; top: 0; z-index: 5;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.brand-text { min-width: 0; }
.logo {
  width: 38px; height: 38px; border-radius: 11px; flex: 0 0 38px;
  background: linear-gradient(135deg, #2a3447, #0f1521);
  display: grid; place-items: center; font-size: 20px;
  box-shadow: 0 4px 18px rgba(0,0,0,.4), inset 0 0 0 1px var(--line);
}
h1 {
  margin: 0; font-size: 16px; letter-spacing: .2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 60vw;
}
.sub {
  color: var(--muted); font-size: 11.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.chat {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px calc(var(--composer-h) + 36px + var(--safe-bottom));
  max-width: 880px; width: 100%; margin: 0 auto;
  scroll-behavior: smooth;
}

.msg {
  display: flex; gap: 10px; margin: 14px 0;
  align-items: flex-end;
  animation: msgIn .25s ease-out both;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg .avatar {
  width: 32px; height: 32px; flex: 0 0 32px;
  border-radius: 50%; display: grid; place-items: center;
  font-size: 17px; background: #1a2231; border: 1px solid var(--line);
  user-select: none;
  overflow: hidden;
}
.msg .avatar img {
  width: 100%; height: 100%; object-fit: cover;
  display: block;
}
.msg .bubble {
  max-width: min(78%, 720px);
  background: var(--bot); padding: 10px 14px;
  border-radius: 16px; border: 1px solid var(--line);
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
  box-shadow: 0 2px 12px rgba(0,0,0,.2);
  font-size: 15.5px; line-height: 1.5;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .bubble {
  background: linear-gradient(135deg, #2b78d6, #1f4f8f);
  border-color: #2b78d6;
  border-bottom-right-radius: 6px;
}
.msg.bot .bubble {
  border-bottom-left-radius: 6px;
  position: relative;
}
.tts-btn {
  display: inline-flex; align-items: center; gap: 4px;
  margin-top: 6px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 10px 4px 8px;
  font: inherit; font-size: 11.5px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s, border-color .15s, background .15s;
}
.tts-btn svg { width: 12px; height: 12px; }
.tts-btn:hover { color: var(--text); border-color: var(--line2); }
.tts-btn.playing {
  color: #0a0d12; background: var(--accent); border-color: var(--accent);
}
.tts-btn.loading {
  color: var(--text);
}
.tts-btn.loading .ico { animation: spin 1s linear infinite; }
.msg.user .avatar { background: #1c3050; border-color: #2b78d6; }
.msg.bot .avatar { background: #1a2a1d; border-color: #355c2f; padding: 0; }

.tool-card {
  margin: 4px 0 8px;
  background: #131a26;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--muted);
  display: flex; flex-direction: column; gap: 4px;
  max-width: min(78%, 720px);
  animation: msgIn .25s ease-out both;
}
.tool-card .tool-head { display: flex; align-items: center; gap: 8px; }
.tool-card .tool-head .spin {
  width: 12px; height: 12px;
  border: 2px solid var(--line2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
.tool-card.done .tool-head .spin {
  border: none; width: 12px; height: 12px;
  background: var(--accent);
  border-radius: 50%;
  animation: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.tool-card .tool-label { color: var(--text); font-weight: 500; }
.tool-card .tool-results a {
  color: var(--accent); text-decoration: none;
  display: block; padding: 2px 0;
  font-size: 12.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 100%;
}
.tool-card .tool-results a:hover { text-decoration: underline; }
.tool-row { display: flex; flex-direction: column; align-items: flex-start; padding-left: 42px; }

.img-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 6px;
  margin-top: 6px;
  width: 100%;
}
.img-grid a {
  display: block;
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  overflow: hidden;
  background: #0c1119;
  border: 1px solid var(--line);
  text-decoration: none;
  animation: imgIn .35s ease-out both;
}
@keyframes imgIn {
  from { opacity: 0; transform: scale(.96); }
  to   { opacity: 1; transform: scale(1); }
}
.img-grid a img {
  width: 100%; height: 100%; object-fit: cover;
  display: block;
  transition: transform .25s;
}
.img-grid a:hover img { transform: scale(1.05); }
.img-grid a .src {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: linear-gradient(transparent, rgba(0,0,0,.75));
  color: #fff; font-size: 10px;
  padding: 12px 6px 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  opacity: 0; transition: opacity .15s;
}
.img-grid a:hover .src { opacity: 1; }
@media (max-width: 640px) {
  .img-grid { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 5px; }
  .img-grid a .src { opacity: 1; }
}

/* Streaming caret + per-token fade-in */
.bubble .tok {
  display: inline;
  opacity: 0;
  animation: tokIn .18s ease-out forwards;
}
@keyframes tokIn {
  from { opacity: 0; filter: blur(2px); transform: translateY(1px); }
  to   { opacity: 1; filter: blur(0);   transform: translateY(0); }
}
.bubble.streaming::after {
  content: '';
  display: inline-block;
  width: 8px; height: 1.05em;
  vertical-align: -2px;
  margin-left: 2px;
  background: var(--accent);
  border-radius: 1px;
  animation: caret 1s steps(2) infinite;
}
@keyframes caret { 50% { opacity: 0; } }
.bubble.typing::before {
  content: '';
  display: inline-block;
  width: 38px; height: 14px;
  background:
    radial-gradient(circle at 6px 7px, var(--muted) 3px, transparent 3.5px),
    radial-gradient(circle at 19px 7px, var(--muted) 3px, transparent 3.5px),
    radial-gradient(circle at 32px 7px, var(--muted) 3px, transparent 3.5px);
  animation: dots3 1.1s infinite ease-in-out;
  vertical-align: middle;
}
@keyframes dots3 {
  0%, 100% { opacity: .35; }
  50%      { opacity: 1; }
}
.msg.error .bubble { background: #3a1f24; border-color: #6b3036; color: #ffd1d1; }

.composer {
  position: sticky; bottom: 0;
  background: linear-gradient(180deg, transparent, var(--bg) 35%);
  padding: 10px 12px calc(12px + var(--safe-bottom));
  display: flex; justify-content: center;
  z-index: 4;
  flex-shrink: 0;
}
.composer .wrap {
  position: relative;
  width: 100%;
  max-width: 820px;
  display: flex; align-items: flex-end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 6px;
  transition: border-color .15s;
}
.composer .wrap:focus-within { border-color: var(--line2); }
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  background: transparent; color: var(--text);
  border: none; outline: none;
  padding: 10px 8px 10px 12px;
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
}
.composer textarea::placeholder { color: var(--muted); }
.composer button.send {
  width: 40px; height: 40px;
  flex: 0 0 40px;
  border-radius: 12px;
  background: var(--accent); color: #0a0d12;
  border: none;
  cursor: pointer; font-size: 17px;
  margin: 3px;
  display: grid; place-items: center;
  -webkit-tap-highlight-color: transparent;
  transition: transform .1s, opacity .15s;
}
.composer button.send:active { transform: scale(.94); }
.composer button.send:disabled { opacity: .45; cursor: not-allowed; }
.composer button.send svg { width: 18px; height: 18px; }

.welcome {
  margin: 24px auto; text-align: center; color: var(--muted);
  max-width: 560px; padding: 0 8px;
  animation: msgIn .35s ease-out both;
}
.welcome h2 { color: var(--text); margin: 10px 0 6px; font-size: 22px; }
.welcome p  { margin: 6px 0 0; }
.chips {
  display: flex; flex-wrap: wrap; gap: 8px;
  justify-content: center; margin-top: 18px;
}
.chip {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: 9px 14px; cursor: pointer;
  color: var(--text); font-size: 13.5px;
  -webkit-tap-highlight-color: transparent;
  transition: border-color .15s, transform .1s;
}
.chip:hover { border-color: var(--accent); }
.chip:active { transform: scale(.97); }

.backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  opacity: 0; pointer-events: none;
  transition: opacity .2s;
  z-index: 10;
}
.backdrop.open { opacity: 1; pointer-events: auto; }

/* ---------- mobile ---------- */
@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: min(86vw, 320px);
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 20;
    box-shadow: 12px 0 40px rgba(0,0,0,.4);
  }
  .sidebar.open { transform: translateX(0); }
  .only-mobile { display: grid; }
  #new-chat-top { display: none; }
  h1 { font-size: 15px; max-width: 55vw; }
  .sub { font-size: 11px; }
  .logo { width: 36px; height: 36px; flex-basis: 36px; font-size: 19px; }
  .chat { padding: 12px 10px calc(var(--composer-h) + 28px + var(--safe-bottom)); }
  .msg { margin: 10px 0; gap: 8px; }
  .msg .bubble { max-width: 84%; font-size: 15px; padding: 9px 12px; }
  .msg .avatar { width: 30px; height: 30px; flex-basis: 30px; font-size: 16px; }
  .welcome h2 { font-size: 19px; }
  .composer { padding: 8px 10px calc(10px + var(--safe-bottom)); }
}
@media (min-width: 801px) {
  .backdrop { display: none; }
}
@media (min-width: 801px) and (max-width: 1024px) {
  :root { --sidebar-w: 240px; }
  .chat { padding-left: 22px; padding-right: 22px; }
  .msg .bubble { max-width: 80%; }
}

@media (prefers-reduced-motion: reduce) {
  .msg, .welcome, .tool-card, .img-grid a { animation: none !important; }
  .bubble .tok { animation: none !important; opacity: 1 !important; filter: none !important; transform: none !important; }
  .bubble.streaming::after { animation: none; }
}
