:root {
  --accent: #2563eb;
  --bg: #f7f7f8; --panel: #ffffff; --side: #f1f2f4; --text: #16181d; --muted: #6b7280;
  --line: #e5e7eb; --bubble: #eef2ff; --code: #f3f4f6; --danger: #b91c1c;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #151821; --side: #12141b; --text: #e7e9ee; --muted: #9aa1ad;
    --line: #262a35; --bubble: #1d2333; --code: #1b1f2a; --danger: #f87171; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: Inter, system-ui, -apple-system, Segoe UI, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12px; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; }
input:focus, textarea:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); border-color: var(--accent); }
label, .label { display: block; font-size: 13px; font-weight: 500; margin: 12px 0 6px; }
label input, label select, label textarea { margin-top: 6px; }

.btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 10px; padding: 9px 14px; font-weight: 500; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; color: var(--muted); border-radius: 8px; width: 28px; height: 28px; font-size: 16px; }
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); }
.pill { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); padding: 3px 8px; border-radius: 999px; }
.msg { min-height: 18px; font-size: 13px; color: var(--muted); }
.msg.err { color: var(--danger); }

.boot { position: fixed; inset: 0; display: grid; place-items: center; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.brand-mark { width: 48px; height: 48px; border-radius: 14px; background: var(--accent) center/cover no-repeat; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 20px; }
.brand-mark.sm { width: 34px; height: 34px; border-radius: 10px; font-size: 15px; }
.brand-mark.has-logo { background-color: transparent; background-size: contain; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); }
.login-card h1 { margin: 14px 0 2px; font-size: 22px; }
.login-card form { margin-top: 10px; }
.login-card .btn { width: 100%; margin-top: 12px; }
.fine { font-size: 12px; color: var(--muted); margin: 16px 0 0; }

.app { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 16px; border-bottom: 1px solid var(--line); }
.brand-row { display: flex; gap: 10px; align-items: center; }
.brand-name { font-weight: 700; }
.brand-tag { font-size: 12px; color: var(--muted); }
.org-switch { margin-top: 12px; padding: 7px 10px; font-size: 13px; }
.side-section { padding: 12px 10px 4px; display: flex; flex-direction: column; min-height: 0; }
.side-section.grow { flex: 1; }
.side-label { display: flex; justify-content: space-between; align-items: center; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 0 6px 6px; }
.space-list, .thread-list { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-item { display: flex; align-items: center; gap: 8px; border: 0; background: transparent; color: var(--text); text-align: left; padding: 8px 10px; border-radius: 9px; width: 100%; font-size: 14px; }
.nav-item:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.nav-item.active { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--text); font-weight: 600; }
.nav-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .del { opacity: 0; font-size: 13px; }
.nav-item:hover .del { opacity: .6; }
.empty-note { color: var(--muted); font-size: 13px; padding: 6px 10px; }
.side-foot { border-top: 1px solid var(--line); padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.me { display: flex; gap: 10px; align-items: center; min-width: 0; }
.me-name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 12px; font-weight: 700; flex: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.topbar { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--panel); }
.space-title { flex: 1; font-weight: 600; display: flex; align-items: center; gap: 8px; min-width: 0; }
.menu { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.msg-row { max-width: 820px; margin: 0 auto 18px; display: flex; gap: 12px; }
.msg-row .who { width: 30px; flex: none; }
.msg-row .body { flex: 1; min-width: 0; line-height: 1.6; }
.msg-row .name { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 2px; }
.msg-row.user .content { background: var(--bubble); border-radius: var(--radius); padding: 10px 14px; display: inline-block; max-width: 100%; white-space: pre-wrap; word-wrap: break-word; }
.content p { margin: 0 0 10px; } .content p:last-child { margin-bottom: 0; }
.content pre { background: var(--code); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.content code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.content pre code { background: none; padding: 0; }
.content table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.content a { color: var(--accent); }
.thinking { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.dots span { display: inline-block; width: 5px; height: 5px; margin-right: 3px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.dots span:nth-child(2) { animation-delay: .2s; } .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }
.error-line { color: var(--danger); font-size: 13px; }
.welcome { max-width: 620px; margin: 10vh auto 0; text-align: center; }
.welcome h2 { font-size: 26px; margin: 16px 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 20px; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 8px 14px; font-size: 13px; color: var(--text); }
.chip:hover { border-color: var(--accent); }

.composer { max-width: 820px; width: calc(100% - 32px); margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px; box-shadow: var(--shadow); }
.composer textarea { border: 0; resize: none; max-height: 220px; padding: 8px; background: transparent; }
.composer textarea:focus { outline: none; }
.send { width: 40px; height: 40px; padding: 0; border-radius: 12px; font-size: 18px; flex: none; }
.composer-note { text-align: center; padding: 8px 16px 14px; }

dialog { border: 1px solid var(--line); border-radius: 16px; padding: 22px; width: min(460px, calc(100% - 32px)); background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h2 { margin: 0 0 6px; font-size: 18px; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.member-picker, .members-list { max-height: 220px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.member-picker label, .members-list .m { display: flex; align-items: center; gap: 10px; margin: 0; padding: 8px 12px; border-bottom: 1px solid var(--line); font-weight: 400; }
.member-picker label:last-child, .members-list .m:last-child { border-bottom: 0; }
.member-picker input { width: auto; margin: 0; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 86%; max-width: 320px; z-index: 20; transform: translateX(-100%); transition: transform .2s; }
  .app.nav-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .menu { display: inline-block; }
  .topbar { padding: 10px 12px; }
  .messages { padding: 16px 12px; }
}
