:root {
  color-scheme: light;
  --bg: #f5f2ed;
  --panel: #ffffff;
  --panel-soft: #f7f8f6;
  --text: #1f2523;
  --muted: #6f746f;
  --line: #ded8cf;
  --primary: #1f6f5b;
  --primary-strong: #164f42;
  --accent: #9b5a3c;
  --accent-soft: #f4e5d9;
  --danger: #b42318;
  --shadow: 0 14px 32px rgba(38, 35, 28, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
}

button, textarea, input { font: inherit; }

button {
  border: 0;
  border-radius: 6px;
  color: #fff;
  background: var(--primary);
  cursor: pointer;
}

button:hover { background: var(--primary-strong); }
button:disabled { cursor: not-allowed; opacity: 0.55; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  width: min(1280px, calc(100vw - 32px));
  min-height: calc(100vh - 32px);
  margin: 16px auto;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  min-width: 0;
}

.side-panel {
  display: grid;
  grid-template-rows: auto auto auto;
  align-content: start;
  gap: 12px;
  min-width: 0;
  min-height: 0;
}

.topbar, .chat-panel, .upload-panel, .folder-panel, .log-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
}

.topbar h1, .section-title h2 { margin: 0; letter-spacing: 0; }
.topbar h1 { font-size: 24px; line-height: 1.2; }
.topbar p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

.top-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.top-actions a {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 11px;
  color: var(--text);
  background: #fff;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}

.top-actions a:hover {
  border-color: #b9c9c1;
  background: #eef5f1;
}

.status {
  flex: 0 0 auto;
  border: 1px solid #b9d5ca;
  border-radius: 999px;
  padding: 5px 10px;
  color: var(--primary-strong);
  background: #e9f5f0;
  font-size: 13px;
  font-weight: 700;
}

.status.pending {
  color: #6b4a0f;
  border-color: #e2c36d;
  background: #fff5ce;
}

.status.error {
  color: var(--danger);
  border-color: #f0b4ae;
  background: #fff1ef;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
  overflow: hidden;
}

.quick-start {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  padding: 14px 16px;
  background: #fffcf8;
}

.quick-start div:first-child {
  display: grid;
  gap: 3px;
}

.quick-start strong {
  font-size: 14px;
}

.quick-start span {
  color: var(--muted);
  font-size: 13px;
}

.quick-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.quick-buttons button {
  min-height: 34px;
  padding: 0 12px;
  color: var(--primary-strong);
  border: 1px solid #b9d5ca;
  background: #e9f5f0;
  font-size: 13px;
  font-weight: 700;
}

.quick-buttons button:hover {
  background: #dceee7;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 18px;
  overflow: auto;
}

.message {
  width: min(78%, 720px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--panel-soft);
}

.message.user {
  align-self: flex-end;
  color: #fff;
  border-color: var(--primary);
  background: var(--primary);
}

.message.assistant { align-self: flex-start; }
.message.error { color: var(--danger); border-color: #f0b4ae; background: #fff1ef; }
.message.pending {
  color: #4f3b12;
  border-color: #ead17b;
  background: #fff8dc;
}

.message.pending p::after {
  display: inline-block;
  width: 1.5em;
  content: "";
  animation: pendingDots 1.2s steps(4, end) infinite;
}

@keyframes pendingDots {
  0% { content: ""; }
  25% { content: "."; }
  50% { content: ".."; }
  75%, 100% { content: "..."; }
}

.message-meta {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.message.user .message-meta { color: rgba(255, 255, 255, 0.78); }
.message p { margin: 0; line-height: 1.55; white-space: pre-wrap; word-break: break-word; }

.source-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.source-chips[hidden] { display: none; }

.source-chips span {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid #cfd8e3;
  border-radius: 999px;
  padding: 3px 8px;
  color: #435163;
  background: #fff;
  font-size: 12px;
  font-weight: 700;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding: 12px;
  background: #fbfcfd;
}

.composer textarea {
  width: 100%;
  min-height: 72px;
  max-height: 180px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--text);
  background: #fff;
}

.composer button { min-height: 44px; align-self: end; }

.upload-panel, .folder-panel, .log-panel { padding: 14px; }
.log-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  max-height: 420px;
  overflow: hidden;
}

.log-panel.collapsed {
  grid-template-rows: auto;
  max-height: none;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title h2 { font-size: 15px; }

.section-title span, .section-title button {
  min-width: 34px;
  border-radius: 999px;
  padding: 4px 9px;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
}

.section-title span { color: var(--accent); background: #f5eadf; }

.section-title button {
  color: var(--primary-strong);
  border: 1px solid #b9d5ca;
  background: #e9f5f0;
}

.log-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.dropzone {
  position: relative;
  border: 1px dashed #a7b2c0;
  border-radius: 8px;
  background: #fbfcfd;
}

.dropzone.dragging { border-color: var(--primary); background: #e9f5f0; }

.dropzone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.dropzone label {
  display: grid;
  gap: 4px;
  min-height: 102px;
  place-items: center;
  padding: 18px;
  text-align: center;
  color: var(--muted);
}

.dropzone strong { color: var(--text); }

.folder-form input {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--text);
  background: #fff;
}

.folder-form {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 0.9fr) 58px;
  gap: 8px;
  margin-top: 12px;
}

.folder-form button {
  min-height: 38px;
}

.file-list, .folder-list, .log-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.file-list, .folder-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.file-list li, .folder-list li, .log-list li {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.file-list li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 9px 10px;
  font-size: 13px;
}

.file-list li.empty-file {
  display: block;
  color: var(--muted);
}

.knowledge-files {
  margin-bottom: 10px;
}

.file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size, .folder-path, .folder-meta, .log-time { color: var(--muted); font-size: 12px; }

.folder-list li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: start;
  padding: 9px 10px;
}

.folder-list li.missing {
  border-color: #f0b4ae;
  background: #fff8f7;
}

.folder-list li.empty-folder {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.folder-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.folder-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 700;
}

.folder-path {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.folder-remove {
  align-self: center;
  min-height: 30px;
  padding: 0 8px;
  color: var(--danger);
  border: 1px solid #f0b4ae;
  background: #fff1ef;
  font-size: 12px;
}

.folder-remove:hover {
  background: #ffe3df;
}

.scope-dialog {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  color: var(--text);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.scope-dialog::backdrop {
  background: rgba(24, 32, 42, 0.38);
}

.scope-dialog form {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.scope-dialog h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.scope-dialog p {
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scope-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.scope-actions button,
.dialog-cancel {
  min-height: 42px;
  padding: 0 12px;
}

.dialog-cancel {
  color: var(--primary-strong);
  border: 1px solid #b9d5ca;
  background: #e9f5f0;
}

.dialog-cancel:hover {
  background: #d7edf3;
}

.log-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  overflow: auto;
}

.log-panel.collapsed .log-list {
  display: none;
}

.log-list li { padding: 10px; }

.log-action {
  display: block;
  margin-bottom: 4px;
  font-size: 13px;
  font-weight: 700;
  word-break: break-word;
}

.log-detail {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    width: min(100vw - 20px, 680px);
    min-height: auto;
    margin: 10px auto;
  }

  .chat-panel { min-height: 62vh; }
  .side-panel { grid-template-rows: auto auto auto; }
  .log-panel { max-height: 360px; }
  .message { width: 100%; }
}

@media (max-width: 520px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .top-actions {
    justify-content: flex-start;
  }

  .quick-start {
    align-items: stretch;
    flex-direction: column;
  }

  .composer { grid-template-columns: 1fr; }
  .composer button { width: 100%; }
  .folder-form, .scope-actions { grid-template-columns: 1fr; }
}
