:root {
  color-scheme: dark;
  --bg: #0d0d0d;
  --sidebar: #171717;
  --surface: #212121;
  --surface-2: #2f2f2f;
  --surface-3: #3a3a3a;
  --border: #2a2a2a;
  --hover: rgba(255,255,255,0.06);
  --hover-strong: rgba(255,255,255,0.1);
  --text: #ececec;
  --text-2: #b4b4b4;
  --text-3: #8e8ea0;
  --accent: #ffffff;
  --danger: #ef4444;
  --success: #10b981;
  --warn: #f59e0b;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; color: inherit; }
textarea { font-family: inherit; }

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

/* Sidebar */
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-top {
  padding: 12px 12px 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
  font-weight: 600;
  font-size: 14px;
}
.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, #8b5cf6, #06b6d4);
  display: grid;
  place-items: center;
  font-size: 12px;
  color: #fff;
  font-weight: 700;
}
.brand-name { color: var(--text); }
.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--text-2);
  flex: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.mobile-only { display: none; }

.sidebar-actions { padding: 8px 8px 4px; }
.sidebar-spacer { flex: 1 1 auto; min-height: 0; }
.side-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
}
.side-item:hover { background: var(--hover); }
.side-item.active { background: var(--hover-strong); }
.side-item .icon { width: 16px; height: 16px; flex: none; color: var(--text-2); }

.side-section {
  padding: 14px 14px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-3);
  font-weight: 600;
}
.persona-list {
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
}
.persona-list::-webkit-scrollbar { width: 6px; }
.persona-list::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 3px; }
.persona-skeleton {
  padding: 8px 10px;
  color: var(--text-3);
  font-size: 13px;
}
.persona-item {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text-2);
  font-size: 13.5px;
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.persona-item:hover { background: var(--hover); color: var(--text); }
.persona-item.active { background: var(--hover-strong); color: var(--text); font-weight: 500; }
.persona-item .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-3);
  flex: none;
}
.persona-item.active .dot { background: var(--success); }

.sidebar-footer {
  padding: 10px 8px 14px;
  border-top: 1px solid var(--border);
}
.user-card {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  text-align: left;
}
.user-card:hover { background: var(--hover); }
.user-card.placeholder { color: var(--text-3); }
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #f59e0b, #ef4444);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 12px;
  color: #fff;
  flex: none;
  text-transform: uppercase;
}
.user-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: var(--text);
}
.user-card.placeholder .avatar { background: var(--surface-2); color: var(--text-3); }
.user-card.placeholder .user-name { color: var(--text-3); }
.login-cta {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 13.5px;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 10px;
}
.login-cta:hover { background: var(--surface-3); }
.login-cta svg { color: var(--text); }

/* Main */
.main {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
  min-width: 0;
}
.topbar {
  height: 52px;
  padding: 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.topbar .title {
  font-size: 14px;
  color: var(--text-2);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar .status {
  font-size: 12px;
  color: var(--text-3);
  flex: none;
}
.status.ok { color: var(--success); }
.status.warn { color: var(--warn); }
.status.err { color: var(--danger); }

.canvas {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.scroll {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.scroll::-webkit-scrollbar { width: 8px; }
.scroll::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 4px; }
.scroll::-webkit-scrollbar-thumb:hover { background: #3a3a3a; }

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 16px 24px;
  gap: 28px;
}
.welcome {
  font-size: 28px;
  font-weight: 500;
  color: var(--text);
  margin: 0;
  text-align: center;
  letter-spacing: -0.01em;
}
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  width: min(760px, 100%);
}
.action-chip {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-2);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: transparent;
}
.action-chip:hover {
  background: var(--hover);
  color: var(--text);
  border-color: rgba(255,255,255,0.16);
}
.action-chip svg { width: 14px; height: 14px; }

/* Thread */
.thread {
  width: min(760px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.bubble {
  border-radius: 16px;
  line-height: 1.65;
  font-size: 14.5px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.bubble.user {
  background: var(--surface-2);
  padding: 12px 16px;
  margin-left: auto;
  max-width: 80%;
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  color: var(--text);
  padding: 4px 2px;
}
.bubble.assistant.error { color: #fca5a5; }
.bubble.assistant.thinking::after {
  content: "▍";
  animation: blink 1s step-end infinite;
  margin-left: 2px;
  color: var(--text-3);
}
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer-slot { width: 100%; display: flex; justify-content: center; }
.composer-bottom {
  padding: 0 16px 16px;
  background: linear-gradient(to bottom, transparent, var(--bg) 30%);
}
.composer {
  width: min(760px, 100%);
  background: var(--surface-2);
  border-radius: 24px;
  padding: 6px 6px 6px 8px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 4px;
  align-items: end;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.04);
  transition: box-shadow 0.15s ease;
}
.composer:focus-within { box-shadow: 0 0 0 1px rgba(255,255,255,0.12); }
.composer .composer-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  align-self: end;
  margin-bottom: 1px;
}
.composer textarea {
  border: 0;
  outline: 0;
  background: transparent;
  resize: none;
  padding: 9px 4px;
  font-size: 14.5px;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text);
}
.composer textarea::placeholder { color: var(--text-3); }
.composer textarea::-webkit-scrollbar { width: 6px; }
.composer textarea::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
.send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #000;
  display: grid;
  place-items: center;
  align-self: end;
  margin: 0 2px 2px 0;
  transition: opacity 0.15s ease, background 0.15s ease;
}
.send-btn:hover:not(:disabled) { background: var(--text-2); }
.send-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.send-btn svg { width: 16px; height: 16px; }
.send-btn.thinking {
  background: var(--surface-3);
  color: var(--text);
}
.send-btn.thinking svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 24px 48px rgba(0,0,0,0.5);
}
.auth-brand {
  width: 48px;
  height: 48px;
  margin: 0 0 16px;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 12px 28px rgba(0,0,0,0.35);
}
.auth-brand img {
  width: 100%;
  height: 100%;
  display: block;
}
.modal h3 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}
.modal p {
  margin: 0 0 20px;
  color: var(--text-2);
  line-height: 1.6;
  font-size: 13.5px;
}
.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.btn {
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 500;
  border: 1px solid var(--border);
  color: var(--text);
  background: transparent;
}
.btn:hover { background: var(--hover); }
.btn.primary {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
}
.btn.primary:hover { background: var(--text-2); }

/* Mobile */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 280px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 32px rgba(0,0,0,0.5); }
  .icon-btn.mobile-only { display: grid; }
  .welcome { font-size: 22px; }
}
