:root {
  --bg: #ffffff;
  --bg-soft: #f6f7f9;
  --side: #f3f4f7;
  --text: #11151c;
  --muted: #5f6877;
  --line: #e3e6eb;
  --user: #eef1f6;
  --accent: #1b5cff;
  --accent-ink: #ffffff;
  --ok: #0a7a4a;
  --ok-bg: #e6f6ee;
  --err: #b42318;
  --err-bg: #fdecea;
  --code: #0f1420;
  --code-text: #e6e9ef;
  --radius: 14px;
  --font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Devanagari", "Noto Sans Oriya", "Noto Sans Bengali", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220; --bg-soft: #111a2b; --side: #0e1626; --text: #e8ecf3; --muted: #97a2b5;
    --line: #1f2a3f; --user: #18233a; --accent: #5b8cff; --accent-ink: #0b1220;
    --ok: #4fd39a; --ok-bg: #0f2a20; --err: #ff8a80; --err-bg: #2c1414; --code: #060b14; --code-text: #e6e9ef;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15.5px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }

.app { display: grid; grid-template-columns: 268px 1fr; height: 100dvh; }

/* Sidebar */
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding: 12px; gap: 10px; }
.side-top { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -0.01em; color: var(--text); text-decoration: none; padding: 6px 4px; }
.new-chat { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; }
.new-chat:hover { border-color: var(--accent); }
.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.history .group { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; font-weight: 600; }
.history .item { display: flex; align-items: center; gap: 6px; border-radius: 8px; padding: 0 4px 0 10px; }
.history .item:hover, .history .item.active { background: var(--bg-soft); }
.history .item button.open { flex: 1; text-align: left; background: none; border: 0; padding: 8px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.history .item button.del { opacity: 0; background: none; border: 0; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
.history .item:hover button.del, .history .item.active button.del { opacity: 1; }
.history .item button.del:hover { color: var(--err); }
.side-foot { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.side-foot a, .side-foot button { display: block; text-align: left; padding: 8px 10px; border-radius: 8px; background: none; border: 0; color: var(--text); text-decoration: none; }
.side-foot a:hover, .side-foot button:hover { background: var(--bg-soft); }
.side-foot .who { color: var(--muted); font-size: 13px; padding: 0 10px; }

/* Main */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); min-height: 52px; }
.topbar .title { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mode-badge { font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); }
.icon-btn { background: none; border: 0; font-size: 18px; width: 36px; height: 36px; border-radius: 8px; }
.icon-btn:hover { background: var(--bg-soft); }
.only-mobile, .only-embed { display: none; }

.thread { flex: 1; overflow-y: auto; padding: 24px 16px 8px; scroll-behavior: smooth; }
.thread > * { max-width: 780px; margin-left: auto; margin-right: auto; }

.empty { display: flex; flex-direction: column; align-items: center; text-align: center; padding-top: 10vh; gap: 10px; }
.empty h1 { font-size: 28px; letter-spacing: -0.02em; margin: 6px 0 18px; }
.suggestions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: 100%; max-width: 640px; }
.suggestions button { text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 12px 14px; font-size: 14px; }
.suggestions button b { display: block; font-weight: 600; }
.suggestions button span { color: var(--muted); }
.suggestions button:hover { border-color: var(--accent); background: var(--bg-soft); }

.msg { display: flex; gap: 12px; margin-bottom: 22px; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--user); padding: 10px 16px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.bot .avatar { flex: none; width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--line); display: grid; place-items: center; background: var(--bg); }
.msg.bot .avatar img { width: 20px; height: 20px; }
.msg.bot .body { flex: 1; min-width: 0; }
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md ul, .md ol { padding-left: 22px; margin: 0 0 12px; }
.md li { margin: 3px 0; }
.md h1, .md h2, .md h3 { line-height: 1.3; margin: 18px 0 8px; letter-spacing: -0.01em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.05em; }
.md code { font-family: var(--mono); font-size: 0.88em; background: var(--bg-soft); border: 1px solid var(--line); padding: 1px 5px; border-radius: 5px; }
.md pre { position: relative; background: var(--code); color: var(--code-text); padding: 14px 16px; border-radius: 10px; overflow-x: auto; margin: 0 0 12px; }
.md pre code { background: none; border: 0; padding: 0; color: inherit; font-size: 13.5px; }
.md pre .copy-code { position: absolute; top: 6px; right: 6px; font-size: 12px; background: rgba(255,255,255,.08); color: #cfd6e3; border: 0; padding: 3px 8px; border-radius: 6px; }
.md table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md th { background: var(--bg-soft); }
.md blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.md a { word-break: break-all; }

.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; padding: 5px 10px; border-radius: 999px; margin: 0 6px 10px 0; background: var(--ok-bg); color: var(--ok); }
.chip.bad { background: var(--err-bg); color: var(--err); }
.err { background: var(--err-bg); color: var(--err); border-radius: 10px; padding: 10px 12px; font-size: 14px; }

.tools { display: flex; gap: 2px; margin-top: 6px; opacity: .75; }
.tools button { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 4px 8px; border-radius: 6px; }
.tools button:hover { background: var(--bg-soft); color: var(--text); }

.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Composer */
.composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); }
.composer-box { max-width: 780px; margin: 0 auto; border: 1px solid var(--line); background: var(--bg-soft); border-radius: 22px; padding: 8px; }
.composer-box:focus-within, .composer-box.drag { border-color: var(--accent); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.attach { flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0; background: none; color: var(--muted); display: grid; place-items: center; }
.attach:hover { background: var(--bg); color: var(--text); }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; padding: 2px 4px 8px; }
.attachments .thumb { position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--bg); }
.attachments .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attachments .thumb button { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; line-height: 20px; padding: 0; }
.attachments .thumb.loading { display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.msg.user .stack { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; max-width: 85%; }
.msg.user .shots { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.msg.user .shots img { max-width: 220px; max-height: 180px; border-radius: 12px; border: 1px solid var(--line); display: block; cursor: zoom-in; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
textarea { flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; max-height: 220px; padding: 6px 0; }
.send { flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.send:disabled { opacity: .35; cursor: default; }
.send.stop svg { display: none; }
.send.stop::before { content: ""; width: 12px; height: 12px; border-radius: 2px; background: currentColor; }
.fineprint { text-align: center; font-size: 12px; color: var(--muted); margin: 6px 0 0; }

/* Mobile */
.scrim { display: none; }
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 82vw; max-width: 300px; z-index: 20; transform: translateX(-100%); transition: transform .2s ease; }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .only-mobile { display: inline-grid; place-items: center; }
  .suggestions { grid-template-columns: 1fr; }
  .empty h1 { font-size: 22px; }
  .history .item button.del { opacity: 1; }
}

/* Embedded chat bubble mode */
body.embed .app { grid-template-columns: 1fr; }
body.embed .sidebar, body.embed .only-mobile, body.embed .scrim { display: none !important; }
body.embed .only-embed { display: inline-grid; place-items: center; }
body.embed .empty { padding-top: 4vh; }
body.embed .empty h1 { font-size: 20px; }
body.embed .suggestions { grid-template-columns: 1fr; }
body.embed .thread { padding: 16px 12px 4px; }
body.embed .composer { padding: 6px 10px 10px; }

/* Shared form pages (login, admin) */
.page { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
.card { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field input, .field select { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg-soft); color: var(--text); }
.btn { border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 600; padding: 10px 16px; border-radius: 10px; }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
