:root {
  --bg: #0b0f16;
  --bg-panel: rgba(22, 27, 34, .92);
  --bg-input: #1c2129;
  --border: #2d333b;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --accent: #fbbf24;
  --accent-dark: #d97706;
  --accent-soft: rgba(251, 191, 36, .12);
  --user-bubble: linear-gradient(135deg, #1f4b99 0%, #2563eb 100%);
  --bot-bubble: #191f28;
  --radius: 16px;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  background:
    radial-gradient(1200px 500px at 50% -10%, rgba(37, 99, 235, .14), transparent 60%),
    radial-gradient(900px 400px at 85% 110%, rgba(251, 191, 36, .07), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex;
  justify-content: center;
}
.app {
  width: 100%;
  max-width: 860px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: .55;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand-icon {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  font-size: 19px;
  background: var(--accent-soft);
  border: 1px solid rgba(251, 191, 36, .35);
  border-radius: 10px;
}
.brand-text { display: flex; flex-direction: column; }
.brand-status {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-dim);
  display: flex; align-items: center; gap: 5px;
}
.brand-status::before {
  content: "";
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #3fb950;
  box-shadow: 0 0 6px rgba(63, 185, 80, .8);
}
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.lang-switch {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: var(--bg-input);
}
.lang-switch button {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
}
.lang-switch button.active {
  background: var(--accent);
  color: #1a1a1a;
}
.model-select {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color .15s;
}
.model-select:hover { border-color: var(--accent); }
.icon-btn {
  background: var(--bg-input);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 15px;
  cursor: pointer;
  transition: all .15s;
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- conversation drawer ---------- */
.drawer-mask {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .55);
  opacity: 0; pointer-events: none;
  transition: opacity .2s;
  z-index: 40;
}
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0;
  width: min(320px, 86vw);
  background: var(--bg-panel);
  backdrop-filter: blur(14px);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform .22s ease;
  z-index: 41;
  display: flex; flex-direction: column;
}
body.drawer-open .drawer { transform: translateX(0); }
body.drawer-open .drawer-mask { opacity: 1; pointer-events: auto; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 14.5px;
}
.drawer-new {
  margin: 12px 16px 4px;
  padding: 9px 12px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px dashed var(--accent-dark);
  border-radius: 10px;
  font-size: 13.5px; font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.drawer-new:hover { background: rgba(251, 191, 36, .2); }
.drawer-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.conv-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.conv-item:hover { background: var(--bg-input); }
.conv-item.active { background: var(--bg-input); border-color: var(--accent-dark); }
.conv-title {
  flex: 1; min-width: 0;
  font-size: 13.5px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-time { font-size: 11px; color: var(--text-dim); flex-shrink: 0; }
.conv-del {
  background: none; border: none; color: var(--text-dim);
  font-size: 13px; cursor: pointer; padding: 2px 4px;
  border-radius: 6px; flex-shrink: 0;
}
.conv-del:hover { color: #f87171; }
.drawer-empty {
  padding: 24px 12px; text-align: center;
  color: var(--text-dim); font-size: 13px;
}

/* ---------- chat area ---------- */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}
.chat-area::-webkit-scrollbar { width: 8px; }
.chat-area::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
.chat-area::-webkit-scrollbar-thumb:hover { background: #3d444d; }

.welcome {
  margin: auto;
  text-align: center;
  max-width: 560px;
  padding: 30px 10px;
  animation: fadeUp .5s ease both;
}
.welcome-icon {
  font-size: 44px;
  width: 84px; height: 84px;
  margin: 0 auto 16px;
  display: grid; place-items: center;
  background: var(--accent-soft);
  border: 1px solid rgba(251, 191, 36, .3);
  border-radius: 26px;
}
.welcome h2 { font-size: 21px; margin-bottom: 10px; }
.welcome p { color: var(--text-dim); font-size: 14px; line-height: 1.65; }
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 20px;
}
.suggestions button {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 9px 15px;
  font-size: 13px;
  cursor: pointer;
  transition: all .18s;
}
.suggestions button:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: translateY(-1px);
}

/* ---------- message rows ---------- */
.row {
  display: flex;
  gap: 10px;
  animation: fadeUp .28s ease both;
}
.row.user { justify-content: flex-end; }
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
.avatar {
  flex: 0 0 32px;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  font-size: 17px;
  background: var(--accent-soft);
  border: 1px solid rgba(251, 191, 36, .3);
  border-radius: 50%;
  margin-top: 2px;
}
.msg {
  max-width: 78%;
  padding: 12px 15px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.65;
  word-break: break-word;
}
.msg.user {
  background: var(--user-bubble);
  border-bottom-right-radius: 5px;
  box-shadow: 0 2px 10px rgba(37, 99, 235, .25);
  white-space: pre-wrap;
}
.msg.bot {
  background: var(--bot-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}
.msg.error { border-color: #f85149; color: #f8a19b; }

/* markdown inside bot bubbles */
.msg.bot p { margin: 0 0 8px; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot ul, .msg.bot ol { margin: 4px 0 8px; padding-left: 20px; }
.msg.bot li { margin: 3px 0; }
.msg.bot .md-h {
  font-weight: 700;
  font-size: 15px;
  margin: 10px 0 6px;
  color: var(--accent);
}
.msg.bot code {
  background: rgba(110, 118, 129, .25);
  padding: 1px 6px;
  border-radius: 5px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg.bot a { color: #58a6ff; text-decoration: none; }
.msg.bot a:hover { text-decoration: underline; }
.msg.bot strong { color: #fff; }

.model-tag {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 9px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 11px;
  color: var(--text-dim);
}
.model-tag::before { content: "🤖"; font-size: 11px; }

/* typing dots */
.dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.dots i {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s infinite;
}
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce {
  0%, 60%, 100% { transform: none; opacity: .45; }
  30% { transform: translateY(-5px); opacity: 1; }
}
/* streaming caret */
.msg.bot.typing .md-body::after {
  content: "▋";
  color: var(--accent);
  animation: blink 1s infinite;
  margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- input ---------- */
.input-bar {
  display: flex;
  gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  backdrop-filter: blur(8px);
}
.input-bar textarea {
  flex: 1;
  resize: none;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  padding: 12px 14px;
  font-size: 14.5px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 140px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.input-bar textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(251, 191, 36, .12);
}
.send-btn {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: #1a1a1a;
  border: none;
  border-radius: 12px;
  width: 46px;
  font-size: 18px;
  cursor: pointer;
  transition: transform .12s, filter .15s;
}
.send-btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.send-btn:active { transform: none; }
.send-btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }

@media (max-width: 600px) {
  .brand-text { display: none; }
  .msg { max-width: 88%; }
  .avatar { flex-basis: 28px; width: 28px; height: 28px; font-size: 15px; }
  .topbar { padding: 10px 12px; }
  .controls { gap: 6px; }
  .lang-switch button { padding: 6px 8px; font-size: 12px; }
  .model-select { max-width: 96px; padding: 6px 6px; font-size: 12px; }
  .icon-btn { width: 30px; height: 30px; font-size: 13px; }
}

/* ---------- embed mode (inside widget iframe) ---------- */
body.embed .brand-text { display: none; }
body.embed .topbar { padding: 8px 10px; }
body.embed .welcome { padding: 16px 8px; }
body.embed .welcome-icon { width: 64px; height: 64px; font-size: 32px; border-radius: 20px; }
body.embed .welcome h2 { font-size: 16px; }
body.embed .msg { max-width: 88%; }
