:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --sidebar: #f0f0f3;
  --text: #1c1c22;
  --muted: #6b6b76;
  --border: #e2e2e8;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --user-bubble: #4f46e5;
  --user-text: #ffffff;
  --code-bg: #f1f1f5;
  --danger: #dc2626;
  --hover: #e6e6ec;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15151a;
    --panel: #1d1d24;
    --sidebar: #111116;
    --text: #ececf1;
    --muted: #9a9aa6;
    --border: #2d2d36;
    --accent: #6d66f2;
    --user-bubble: #3d38b8;
    --code-bg: #26262f;
    --hover: #24242c;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.app { display: flex; height: 100vh; height: 100dvh; }

/* Sidebar */
.sidebar {
  width: 270px; flex-shrink: 0; background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 10px; padding: 14px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 2px 8px; }
.logo {
  width: 38px; height: 38px; border-radius: 10px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.company { font-weight: 700; font-size: 16px; }
.tag { color: var(--muted); font-size: 12px; }
.section-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; margin-top: 6px; }
.chats { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-item {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: var(--text); text-decoration: none; font-size: 14px;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--panel); box-shadow: 0 0 0 1px var(--border); }
.chat-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .del { visibility: hidden; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 13px; }
.chat-item:hover .del { visibility: visible; }

/* Buttons */
.btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 9px 14px;
  border-radius: 10px; font: inherit; cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; text-align: left; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: none; color: var(--text); font-size: 18px; cursor: pointer; padding: 6px; border-radius: 8px; }
.icon-btn:hover { background: var(--hover); }
.hidden { display: none !important; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--panel); }
#toggleSidebar { display: none; }
.selectors { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; font-size: 13px; color: var(--muted); }
.selectors label { display: flex; align-items: center; gap: 6px; }
select {
  font: inherit; font-size: 13px; color: var(--text); background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 8px; max-width: 230px;
}
.check { cursor: pointer; }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.msg { max-width: 820px; margin: 0 auto 18px; display: flex; gap: 12px; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0; display: grid; place-items: center;
  font-size: 12px; font-weight: 700; background: var(--accent); color: var(--accent-text);
}
.msg.user { justify-content: flex-end; }
.msg.user .avatar { display: none; }
.msg .bubble { min-width: 0; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 80%; white-space: pre-wrap; }
.msg.assistant .bubble { flex: 1; padding-top: 3px; }
.msg.error .bubble { color: var(--danger); }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: var(--code-bg); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; position: relative; }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: 13px; font-family: ui-monospace, Menlo, monospace; }
.bubble pre code { background: none; padding: 0; }
.bubble ul, .bubble ol { padding-left: 22px; margin: 0 0 10px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 14px 0 8px; line-height: 1.3; }
.bubble h1 { font-size: 20px; } .bubble h2 { font-size: 18px; } .bubble h3 { font-size: 16px; }
.bubble table { border-collapse: collapse; margin: 0 0 10px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.bubble a { color: var(--accent); }
.sources { font-size: 12px; color: var(--muted); margin-top: 8px; }
.meta { font-size: 12px; color: var(--muted); margin-top: 6px; display: flex; gap: 10px; }
.meta button { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0; font: inherit; }
.meta button:hover { color: var(--text); }
.cursor::after { content: "▋"; animation: blink 1s steps(2) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }

.welcome { max-width: 640px; margin: 12vh auto 0; text-align: center; }
.welcome .logo { width: 56px; height: 56px; font-size: 20px; margin: 0 auto 14px; border-radius: 14px; }
.welcome h1 { font-size: 24px; margin: 0 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 22px; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.suggestions button { text-align: left; }
.notice { max-width: 640px; margin: 16px auto 0; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); font-size: 14px; text-align: left; }

.composer { display: flex; gap: 8px; align-items: flex-end; max-width: 852px; width: 100%; margin: 0 auto; padding: 0 16px; }
.composer textarea {
  flex: 1; resize: none; font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px; max-height: 200px; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer .btn { height: 46px; border-radius: 12px; }
.footnote { text-align: center; color: var(--muted); font-size: 12px; padding: 8px 16px 12px; }

/* Documents dialog */
dialog {
  width: min(620px, calc(100vw - 32px)); max-height: 85vh; border: 1px solid var(--border); border-radius: 16px;
  background: var(--panel); color: var(--text); padding: 20px;
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.dialog-head h2 { margin: 0; font-size: 18px; }
.muted { color: var(--muted); font-size: 13px; }
.upload {
  display: block; border: 2px dashed var(--border); border-radius: 12px; padding: 22px; text-align: center;
  cursor: pointer; margin: 12px 0 8px; color: var(--muted);
}
.upload.drag, .upload:hover { border-color: var(--accent); color: var(--text); }
.upload input { display: none; }
.doc-list { list-style: none; padding: 0; margin: 12px 0 0; display: flex; flex-direction: column; gap: 6px; }
.doc-list li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; }
.doc-list .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Mobile */
@media (max-width: 760px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgb(0 0 0 / .3); }
  .sidebar.open { transform: none; }
  #toggleSidebar { display: block; }
  .topbar { padding: 8px 12px; }
  select { max-width: 150px; }
  .msg.user .bubble { max-width: 90%; }
}

/* Sign-in screen */
.login { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px; display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-card .logo { width: 52px; height: 52px; margin: 0 auto 4px; font-size: 18px; border-radius: 14px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0 0 6px; }
.login-card input {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-error { color: var(--danger); font-size: 13px; min-height: 18px; }
