:root {
  --accent: #7c5cff;
  --accent-2: #22d3ee;
  --bg: #0b0d12;
  --bg-2: #11141b;
  --panel: #151922;
  --panel-2: #1b2030;
  --border: #252b3a;
  --text: #e6e8ef;
  --muted: #8b93a7;
  --danger: #ff5c7a;
  --code-bg: #0d1117;
  --radius: 14px;
  --sidebar-w: 270px;
  font-synthesis: none;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--bg-2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 10px;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 14px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.brand-logo {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); font-size: 17px;
}
.new-chat {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel); font-weight: 600; transition: .15s;
}
.new-chat:hover { border-color: var(--accent); background: var(--panel-2); }
.history-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; padding: 18px 8px 6px; }
.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.history-item {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px;
  color: #c9cede; font-size: 14px; cursor: pointer; border: none; background: none; text-align: left; width: 100%;
}
.history-item:hover { background: var(--panel); }
.history-item.active { background: var(--panel-2); color: #fff; }
.history-item .h-title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.history-item .h-del { opacity: 0; border: none; background: none; color: var(--muted); padding: 0 4px; border-radius: 6px; }
.history-item:hover .h-del, .history-item.active .h-del { opacity: 1; }
.history-item .h-del:hover { color: var(--danger); }
.history-empty { color: var(--muted); font-size: 13px; padding: 8px 10px; }
.sidebar-footer { border-top: 1px solid var(--border); padding: 10px 6px 0; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.link-btn { border: none; background: none; color: var(--muted); font-size: 13px; padding: 4px 0; }
.link-btn:hover { color: var(--danger); }
.model-pill { font-size: 11px; color: var(--muted); border: 1px solid var(--border); padding: 2px 8px; border-radius: 999px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn { border: none; background: none; padding: 6px; border-radius: 8px; display: grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--panel); color: var(--text); }
.close-sidebar, .menu-btn { display: none; }
.backdrop { display: none; }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.topbar {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; gap: 10px; padding: 0 16px;
  border-bottom: 1px solid var(--border); background: rgba(11,13,18,.8); backdrop-filter: blur(8px);
}
.title { flex: 1; font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.mode-select {
  display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 6px 4px 12px;
}
.mode-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 8px var(--accent-2); }
.mode-select.fivem .mode-dot { background: #f5a524; box-shadow: 0 0 8px #f5a524; }
.mode-select select { background: transparent; border: none; color: var(--text); font: inherit; font-size: 13px; outline: none; cursor: pointer; padding-right: 4px; }
.mode-select option { background: var(--panel); }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; scroll-behavior: smooth; }
.msg { max-width: 820px; margin: 0 auto 22px; display: flex; gap: 14px; }
.avatar {
  width: 32px; height: 32px; border-radius: 9px; flex-shrink: 0; display: grid; place-items: center; font-size: 15px; font-weight: 700;
}
.msg.user .avatar { background: var(--panel-2); color: var(--muted); }
.msg.assistant .avatar { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.bubble { flex: 1; min-width: 0; }
.msg.user .bubble .content {
  background: var(--panel); border: 1px solid var(--border); padding: 10px 14px; border-radius: var(--radius);
  white-space: pre-wrap; word-wrap: break-word; display: inline-block; max-width: 100%;
}
.msg.assistant .content { word-wrap: break-word; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content p { margin: 0 0 12px; }
.content a { color: var(--accent-2); }
.content ul, .content ol { padding-left: 22px; margin: 0 0 12px; }
.content h1, .content h2, .content h3 { line-height: 1.3; margin: 18px 0 8px; }
.content h1 { font-size: 1.4em; } .content h2 { font-size: 1.22em; } .content h3 { font-size: 1.08em; }
.content blockquote { border-left: 3px solid var(--accent); margin: 0 0 12px; padding: 2px 14px; color: var(--muted); }
.content table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 10px; }
.content th { background: var(--panel); }
.content :not(pre) > code {
  background: var(--panel-2); border: 1px solid var(--border); padding: 1px 6px; border-radius: 6px;
  font-size: .88em; font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.code-block { margin: 0 0 14px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--code-bg); }
.code-head {
  display: flex; justify-content: space-between; align-items: center; padding: 6px 10px 6px 14px;
  background: #161b26; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--muted);
}
.copy-btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--muted); font-size: 12px;
  padding: 3px 10px; border-radius: 7px; transition: .15s;
}
.copy-btn:hover { color: var(--text); border-color: var(--accent); }
.copy-btn.copied { color: #4ade80; border-color: #4ade80; }
.code-block pre { margin: 0; overflow-x: auto; }
.code-block code.hljs {
  display: block; padding: 14px; background: transparent; font-size: 13.5px; line-height: 1.55;
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: .15s; }
.msg:hover .msg-actions, .msg:last-child .msg-actions { opacity: 1; }
.msg-actions button { border: none; background: none; color: var(--muted); font-size: 12px; padding: 3px 8px; border-radius: 6px; }
.msg-actions button:hover { background: var(--panel); color: var(--text); }

.error-box {
  border: 1px solid rgba(255,92,122,.4); background: rgba(255,92,122,.08); color: #ffb3c1;
  padding: 10px 14px; border-radius: 12px; font-size: 14px; margin-top: 4px;
}
.error-box strong { color: var(--danger); }
.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.streaming .content > :last-child::after { content: "▍"; color: var(--accent); animation: caret 1s steps(1) infinite; margin-left: 2px; }
@keyframes caret { 50% { opacity: 0; } }
.stopped-note { font-size: 12px; color: var(--muted); margin-top: 6px; font-style: italic; }

/* ---------- Welcome ---------- */
.welcome { max-width: 760px; margin: 8vh auto 0; text-align: center; padding: 0 8px; }
.welcome-logo {
  width: 64px; height: 64px; margin: 0 auto 16px; border-radius: 18px; display: grid; place-items: center; font-size: 32px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 10px 40px rgba(124,92,255,.35);
}
.welcome h1 {
  font-size: 34px; margin: 0 0 6px; letter-spacing: -.02em;
  background: linear-gradient(90deg, #fff, #b9b3ff 60%, var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.welcome p { color: var(--muted); margin: 0 0 28px; }
.suggestions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.suggestion {
  border: 1px solid var(--border); background: var(--panel); border-radius: 12px; padding: 12px 14px; transition: .15s; text-align: left;
}
.suggestion:hover { border-color: var(--accent); transform: translateY(-1px); }
.suggestion b { display: block; font-size: 14px; margin-bottom: 2px; }
.suggestion span { color: var(--muted); font-size: 13px; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 8px 16px calc(14px + env(safe-area-inset-bottom)); }
.composer {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 8px 8px 8px 16px;
  transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124,92,255,.15); }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: transparent; color: var(--text);
  font: inherit; max-height: 240px; padding: 7px 0; line-height: 1.5;
}
.composer textarea::placeholder { color: #626a7f; }
.send-btn {
  width: 38px; height: 38px; border-radius: 12px; border: none; flex-shrink: 0; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #9b7dff); color: #fff; transition: .15s;
}
.send-btn:disabled { opacity: .35; cursor: not-allowed; }
.stop-btn {
  display: flex; align-items: center; gap: 6px; height: 38px; padding: 0 14px; border-radius: 12px; flex-shrink: 0;
  border: 1px solid var(--border); background: var(--panel-2); font-size: 13px; font-weight: 600;
}
.stop-btn:hover { border-color: var(--danger); color: var(--danger); }
.composer-hint { max-width: 820px; margin: 6px auto 0; text-align: center; font-size: 12px; color: #5d6478; }
.composer-hint .over { color: var(--danger); }

.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--panel-2); border: 1px solid var(--border); padding: 8px 14px; border-radius: 10px; font-size: 13px; transition: .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #262c3b; border-radius: 10px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Mobile ---------- */
@media (max-width: 800px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .22s ease;
    width: min(85vw, var(--sidebar-w)); box-shadow: 10px 0 40px rgba(0,0,0,.5);
  }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30; }
  .close-sidebar, .menu-btn { display: grid; }
  .topbar { padding: 0 8px; }
  .mode-select select { max-width: 130px; }
  .messages { padding: 16px 10px 8px; }
  .msg { gap: 10px; }
  .avatar { width: 28px; height: 28px; font-size: 13px; }
  .suggestions { grid-template-columns: 1fr; }
  .welcome { margin-top: 4vh; }
  .welcome h1 { font-size: 27px; }
  .composer-wrap { padding: 6px 8px calc(10px + env(safe-area-inset-bottom)); }
  .msg-actions { opacity: 1; }
}
