/* ── Reset & Base ──────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  /* dark theme (default) */
  --bg: #0d1117;
  --sidebar: #161b22;
  --surface: #1c2333;
  --accent: #f0883e;
  --accent-hover: #ff9d54;
  --text: #e6edf3;
  --muted: #8b949e;
  --border: #30363d;
  --msg-user: #1c2333;
  --msg-ai: #0d1117;
  --code-bg: #0a0e14;
  --table-stripe: rgba(255,255,255,0.02);
}

:root.light {
  --bg: #ffffff;
  --sidebar: #f6f8fa;
  --surface: #f0f4f8;
  --accent: #e36209;
  --accent-hover: #c75000;
  --text: #1f2328;
  --muted: #656d76;
  --border: #d0d7de;
  --msg-user: #f0f4f8;
  --msg-ai: #ffffff;
  --code-bg: #f6f8fa;
  --table-stripe: rgba(0,0,0,0.02);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
}
#app {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* ── Sidebar ──────────────────────────────── */
.sidebar {
  width: 260px;
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-header {
  padding: 20px;
  font-size: 18px;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.sidebar-header .logo {
  width: 28px; height: 28px;
  background: linear-gradient(135deg, var(--accent), #e94560);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 900; color: #fff;
}
.task-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 0;
}
.task-item {
  padding: 12px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--muted);
  transition: all .15s;
  border-left: 3px solid transparent;
}
.task-item:hover { background: rgba(240,136,62,.08); color: var(--text); }
.task-item.active { background: rgba(240,136,62,.12); color: var(--accent); border-left-color: var(--accent); }
.task-item .task-icon { font-size: 14px; width: 20px; text-align: center; }
.task-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.task-meta { font-size: 11px; color: var(--muted); }
.task-delete { opacity: 0; font-size: 14px; cursor: pointer; padding: 2px 6px; border-radius: 4px; }
.task-item:hover .task-delete { opacity: .5; }
.task-delete:hover { opacity: 1 !important; background: rgba(233,69,96,.3); }

.sidebar-footer { padding: 16px 20px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.btn {
  display: block;
  width: 100%;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
  transition: all .15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: rgba(255,255,255,.05); color: var(--text); }
.btn-sm { padding: 6px 12px; font-size: 12px; width: auto; }

.theme-toggle {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 0; color: var(--muted); font-size: 13px;
  cursor: pointer; user-select: none;
}
.theme-toggle:hover { color: var(--text); }
.theme-toggle .switch {
  width: 36px; height: 20px; background: var(--border); border-radius: 10px;
  position: relative; transition: background .2s;
}
.theme-toggle .switch::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--text); transition: transform .2s;
}
:root.light .theme-toggle .switch { background: var(--accent); }
:root.light .theme-toggle .switch::after { transform: translateX(16px); }

/* ── Main Chat Area ────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}
.chat-header {
  padding: 16px 24px;
  font-size: 15px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  display: flex; align-items: center; justify-content: space-between;
}
.chat-title { display: flex; align-items: center; gap: 8px; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.msg { display: flex; gap: 12px; max-width: 85%; animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; }
.msg-avatar {
  width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.msg.user .msg-avatar { background: var(--accent); color: #fff; }
.msg.assistant .msg-avatar { background: var(--surface); color: var(--accent); }
.msg-content {
  padding: 12px 16px;
  border-radius: 12px;
  line-height: 1.6;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .msg-content { background: var(--msg-user); border-bottom-right-radius: 4px; }
.msg.assistant .msg-content { background: var(--msg-ai); border-bottom-left-radius: 4px; border: 1px solid var(--border); }

.msg.assistant .msg-content code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: "SF Mono", "Fira Code", monospace;
}
.msg.assistant .msg-content pre {
  background: var(--code-bg);
  padding: 14px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 10px 0;
  border: 1px solid var(--border);
}
.msg.assistant .msg-content pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}
.msg.assistant .msg-content h1 { font-size: 20px; margin: 12px 0 8px; color: var(--text); }
.msg.assistant .msg-content h2 { font-size: 17px; margin: 10px 0 6px; color: var(--text); }
.msg.assistant .msg-content h3 { font-size: 15px; margin: 8px 0 4px; color: var(--text); }
.msg.assistant .msg-content ul,
.msg.assistant .msg-content ol {
  padding-left: 20px;
  margin: 6px 0;
}
.msg.assistant .msg-content li { margin: 2px 0; }
.msg.assistant .msg-content p { margin: 4px 0; line-height: 1.7; }
.msg.assistant .msg-content a { color: var(--accent); }
.msg.assistant .msg-content strong { color: var(--text); }
.msg.assistant .msg-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.msg.assistant .msg-content th,
.msg.assistant .msg-content td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.msg.assistant .msg-content th {
  background: var(--surface);
  font-weight: 600;
}
.msg.assistant .msg-content tr:nth-child(even) td {
  background: var(--table-stripe);
}
.msg.assistant .msg-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}
.msg.assistant .msg-content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--muted);
  margin: 8px 0;
}

.msg.error .msg-content {
  background: rgba(233,69,96,.12);
  border: 1px solid rgba(233,69,96,.25);
}

.thinking { color: var(--muted); font-style: italic; padding: 8px 0; font-size: 13px; }

/* ── Input ─────────────────────────────────── */
.chat-input-area { padding: 16px 24px; border-top: 1px solid var(--border); background: var(--bg); }
.chat-input-wrap {
  display: flex;
  gap: 8px;
  background: var(--msg-user);
  border-radius: 12px;
  padding: 4px;
  border: 1px solid var(--border);
  align-items: stretch;
}
.chat-input-wrap:focus-within { border-color: var(--accent); }
.chat-input-wrap textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 14px;
  resize: none;
  padding: 8px 12px;
  outline: none;
  font-family: inherit;
  max-height: 120px;
}
.chat-actions {
  display: flex;
  gap: 4px;
  align-items: stretch;
  flex-shrink: 0;
}
.chat-actions button {
  border: none;
  color: #fff;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all .15s;
}
#chat-send {
  background: var(--accent);
}
#chat-send:hover { background: var(--accent-hover); }
#chat-send:disabled { opacity: .5; cursor: not-allowed; }
#chat-send .btn-hint {
  font-size: 11px;
  opacity: .7;
}
#chat-stop {
  background: #e94560;
  font-size: 16px;
  padding: 8px 16px;
}
#chat-stop:hover { background: #d63650; }

.chat-input-hint {
  color: var(--muted);
  font-size: 11px;
  padding: 6px 4px 0;
  text-align: right;
  user-select: none;
}

/* ── Login ─────────────────────────────────── */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100vw;
  background: var(--bg);
}
.login-card {
  background: var(--sidebar);
  padding: 40px;
  border-radius: 16px;
  width: 400px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  gap: 20px;
  border: 1px solid var(--border);
}
.login-card h1 { text-align: center; font-size: 24px; display: flex; align-items: center; justify-content: center; gap: 8px; color: var(--text); }
.login-card .login-logo {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--accent), #e94560);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 900; color: #fff;
}
.login-card input {
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-error { color: #f85149; font-size: 13px; text-align: center; }

/* ── Empty state ───────────────────────────── */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 12px;
  font-size: 14px;
}
.empty-state .empty-icon {
  width: 64px; height: 64px;
  background: linear-gradient(135deg, var(--accent), #e94560);
  border-radius: 16px;
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; font-weight: 900; color: #fff;
  opacity: .6;
}

/* ── Scrollbar ─────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
