:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1d2129;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1668dc;
  --accent-soft: #e8f0ff;
  --danger: #d4380d;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; }

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(160deg, #f7f9ff, #eef2f8);
}
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px;
  box-shadow: 0 18px 50px rgba(20, 30, 60, .08);
}
.login-card h1 { margin: 0 0 6px; font-size: 24px; }
.login-card input {
  width: 100%;
  margin-top: 18px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button { width: 100%; margin-top: 14px; }

.primary {
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 10px;
  padding: 10px 16px;
}
.primary:hover { filter: brightness(1.06); }
.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 11px;
  color: var(--text);
}
.ghost:hover { background: #f8fafc; }
.small { padding: 5px 9px; font-size: 12px; }
.danger { color: var(--danger); border-color: #ffd6cc; background: #fff7f5; }
.icon-btn {
  background: transparent;
  border: 0;
  font-size: 20px;
  line-height: 1;
  padding: 6px 8px;
  border-radius: 8px;
  color: var(--text);
}
.icon-btn:hover { background: #eef1f5; }

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top" "side main";
}
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}
.topbar-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-title strong {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions { margin-left: auto; }
.status { color: var(--muted); font-size: 12px; }
.status.running { color: #0f7b3f; }
.status.stopped { color: var(--danger); }

.sidebar {
  grid-area: side;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: auto;
  padding: 14px;
  gap: 10px;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.projects { display: flex; flex-direction: column; gap: 4px; }
.project-item {
  text-align: left;
  border: 0;
  background: transparent;
  padding: 9px 10px;
  border-radius: 9px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.project-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}
.project-preview {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.project-time { font-size: 11px; color: #9ca3af; white-space: nowrap; }
.search {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: white;
}
.archive summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 2px;
}
.archive .project-item { padding: 7px 9px; }
.project-item:hover { background: #f3f6fa; }
.project-item.active { background: var(--accent-soft); color: var(--accent); }
.project-item .dot { font-size: 10px; align-self: center; color: #9ca3af; }
.project-item .dot.on { color: #12a150; }
.new-project { display: flex; gap: 6px; }
.new-project input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
}
.new-project button { padding: 8px 12px; }

.file-panel {
  border-top: 1px solid var(--line);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}
.file-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tree {
  font-size: 13px;
  overflow: auto;
  min-height: 0;
  padding-bottom: 20px;
}
.tree details { margin: 2px 0; }
.tree summary {
  cursor: pointer;
  padding: 3px 4px;
  border-radius: 6px;
  list-style: none;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary:hover { background: #f3f6fa; }
.tree .file-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 4px 3px 18px;
  border-radius: 6px;
}
.tree .file-row:hover { background: #f3f6fa; }
.tree .file-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: transparent;
  border: 0;
  text-align: left;
  padding: 0;
  color: var(--text);
}
.tree .file-name:hover { color: var(--accent); }
.tree .row-del { opacity: 0; border: 0; background: transparent; color: var(--danger); }
.tree .file-row:hover .row-del { opacity: 1; }

.main {
  grid-area: main;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.welcome { padding: 48px; }
.welcome h2 { margin: 0 0 6px; }
.chat {
  flex: 1;
  overflow: auto;
  padding: 18px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.bubble {
  max-width: min(760px, 88%);
  padding: 10px 13px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
}
.bubble.user {
  align-self: flex-end;
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}
.bubble.assistant, .bubble.final {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.bubble.final { border-color: #cfe0ff; box-shadow: 0 2px 10px rgba(22, 104, 220, .06); }
.bubble.markdown { white-space: normal; }
.bubble.markdown p { margin: 0 0 .65em; }
.bubble.markdown p:last-child { margin-bottom: 0; }
.bubble.markdown h1,
.bubble.markdown h2,
.bubble.markdown h3,
.bubble.markdown h4 { margin: .7em 0 .4em; line-height: 1.3; }
.bubble.markdown h1 { font-size: 1.25em; }
.bubble.markdown h2 { font-size: 1.15em; }
.bubble.markdown h3 { font-size: 1.05em; }
.bubble.markdown ul,
.bubble.markdown ol { margin: .4em 0 .65em; padding-left: 1.4em; }
.bubble.markdown li { margin: .15em 0; }
.bubble.markdown blockquote {
  margin: .5em 0;
  padding: .2em .8em;
  border-left: 3px solid #d0d7de;
  color: #57606a;
}
.bubble.markdown code {
  background: #f1f3f5;
  padding: 1px 4px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .92em;
}
.bubble.markdown pre {
  background: #f6f8fa;
  padding: 10px 12px;
  border-radius: 9px;
  overflow: auto;
  margin: .5em 0 .7em;
}
.bubble.markdown pre code { background: transparent; padding: 0; }
.bubble.markdown table {
  border-collapse: collapse;
  width: 100%;
  margin: .5em 0 .7em;
  font-size: .92em;
  display: block;
  overflow: auto;
}
.bubble.markdown th,
.bubble.markdown td {
  border: 1px solid #e5e7eb;
  padding: 5px 8px;
  text-align: left;
  white-space: nowrap;
}
.bubble.markdown th { background: #f8fafc; }
.bubble.markdown a { color: var(--accent); }
.bubble.markdown hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.bubble.streaming::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.think, .tool {
  align-self: flex-start;
  max-width: min(760px, 92%);
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: #fafbfc;
  padding: 6px 10px;
  color: #4b5563;
}
.think summary, .tool summary { cursor: pointer; font-size: 12px; }
.think pre, .tool pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  margin: 8px 0 2px;
  max-height: 320px;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.turn-sep { align-self: center; color: #adb5bd; font-size: 11px; }
.file-cards {
  align-self: flex-start;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: min(760px, 92%);
}
.file-card {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  padding: 8px;
  min-width: 160px;
  max-width: 260px;
}
.file-card img {
  width: 100%;
  max-height: 220px;
  object-fit: cover;
  border-radius: 7px;
  display: block;
  margin-bottom: 6px;
  background: #f1f3f5;
}
.file-card .file-meta { font-size: 12px; color: var(--muted); }
.file-card a { color: var(--accent); text-decoration: none; font-weight: 500; }

.composer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 10px 14px 12px;
}
.pending { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.pending:empty { display: none; }
.chip {
  background: #f1f4f8;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 12px;
  display: inline-flex;
  gap: 6px;
  align-items: center;
}
.chip button { border: 0; background: transparent; color: var(--muted); }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 160px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send { height: 42px; }
.composer-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 7px;
  color: var(--muted);
  font-size: 12px;
}
.composer-status {
  min-height: 1.1em;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}
.composer-meta select {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 3px 6px;
  margin-left: 4px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .45);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 18px;
}
.modal-card {
  width: min(880px, 100%);
  max-height: 88vh;
  background: var(--panel);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.modal-body { padding: 14px; overflow: auto; }
.modal-body img { max-width: 100%; }
.modal-body pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
}

.small-text { font-size: 12px; line-height: 1.5; }
.drawer {
  position: fixed;
  top: 56px;
  right: 0;
  bottom: 0;
  width: min(420px, 94vw);
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 35px rgba(15, 23, 42, .12);
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  overflow: auto;
}
.drawer.settings { width: min(380px, 94vw); }
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.drawer .file-actions { margin-bottom: 2px; }
.drawer .tree { flex: 1; overflow: auto; }
.settings-body { display: flex; flex-direction: column; gap: 18px; }
.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.setting-group:last-child { border-bottom: 0; }
.setting-group label { font-weight: 600; }
.setting-group select {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}
.setting-group button { align-self: flex-start; }
.backdrop {
  position: fixed;
  top: 56px;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(15, 23, 42, .38);
  z-index: 35;
}

.mobile-only { display: none; }

@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas: "top" "main";
  }
  .sidebar {
    position: fixed;
    top: 56px;
    bottom: 0;
    left: 0;
    width: min(84vw, 320px);
    transform: translateX(-102%);
    transition: transform .18s ease;
    z-index: 30;
    box-shadow: 10px 0 30px rgba(15, 23, 42, .12);
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-only { display: inline-block; }
  .chat { padding: 12px 10px 6px; }
  .bubble { max-width: 94%; }
  .welcome { padding: 24px; }
  .composer { padding: 8px 10px 10px; }
  .drawer, .drawer.settings { top: 56px; width: 100%; }
  .backdrop { top: 56px; }
}
