:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-subtle: #f4f4f2;
  --surface-hover: #ececea;
  --border: #deded8;
  --border-strong: #c9c9c1;
  --text: #1f1f1f;
  --text-muted: #676761;
  --text-soft: #85857d;
  --primary: #111111;
  --primary-hover: #30302f;
  --accent: #10a37f;
  --accent-soft: #e7f4ef;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.08);
  --radius: 8px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-columns: 312px minmax(0, 1fr);
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: #f2f2ef;
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  height: 100dvh;
  min-height: 0;
  overflow: auto;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.02);
}

.compact-panel {
  padding: 12px;
}

.library-panel {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

h2 {
  font-size: 13px;
  line-height: 1.25;
  margin: 0;
  font-weight: 650;
}

.meta-pill {
  min-height: 24px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 12px;
  white-space: nowrap;
}

.category-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.category-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 999px;
  min-height: 32px;
  padding: 5px 10px;
  font-size: 12px;
  transition: background 180ms ease, color 180ms ease, border-color 180ms ease;
}

.category-tab[aria-selected="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.document-list {
  display: grid;
  flex: 1;
  gap: 8px;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.document-item {
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 9px;
  background: var(--surface-subtle);
}

.document-title {
  font-size: 12px;
  color: var(--text);
  font-weight: 600;
  word-break: break-word;
}

.document-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 5px;
  color: var(--text-soft);
  font-size: 11px;
}

.main {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  background: rgba(247, 247, 245, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 14px 18px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.config-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 170px 140px;
  gap: 12px;
  align-items: end;
}

.field {
  display: grid;
  gap: 5px;
}

.field span {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.field input,
.field select {
  width: 100%;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0 11px;
  transition: border-color 180ms ease, box-shadow 180ms ease;
}

.field input:focus,
.field select:focus,
.composer textarea:focus {
  border-color: var(--border-strong);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  min-height: 0;
  overflow: hidden;
}

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

.message-list {
  overflow: auto;
  min-height: 0;
  padding: 28px 28px 24px;
}

.message {
  display: grid;
  grid-template-columns: 34px minmax(0, 760px);
  justify-content: center;
  gap: 14px;
  padding: 14px 0;
}

.message-avatar {
  width: 30px;
  height: 30px;
  border-radius: var(--radius);
  background: var(--primary);
  align-self: start;
  margin-top: 2px;
}

.message.user .message-avatar {
  background: var(--accent);
}

.message.system .message-avatar {
  background: #d9d9d4;
}

.message-content {
  background: transparent;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 15px;
}

.message.system .message-content {
  color: var(--text-muted);
}

.message-content code {
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 4px;
}

.composer {
  width: min(820px, calc(100% - 42px));
  margin: 0 auto 22px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 10px;
}

.composer textarea {
  width: 100%;
  min-height: 44px;
  max-height: 160px;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  padding: 10px 6px;
}

.composer-actions {
  display: flex;
  align-items: end;
  gap: 8px;
}

.primary-button,
.secondary-button {
  min-height: 40px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  padding: 0 14px;
  font-weight: 650;
  transition: background 180ms ease, border-color 180ms ease, color 180ms ease, transform 180ms ease;
}

.primary-button {
  background: var(--primary);
  color: #ffffff;
}

.primary-button:hover {
  background: var(--primary-hover);
}

.secondary-button {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.secondary-button:hover {
  background: var(--surface-hover);
}

.primary-button:active,
.secondary-button:active,
.category-tab:active {
  transform: translateY(1px);
}

.citation-pane {
  border-left: 1px solid var(--border);
  background: #fbfbfa;
  padding: 18px;
  overflow: auto;
  min-height: 0;
  min-width: 0;
}

.citation-list {
  display: grid;
  gap: 10px;
}

.citation-item {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
}

.citation-topline {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
}

.citation-title {
  font-size: 13px;
  font-weight: 650;
  color: var(--text);
  word-break: break-word;
}

.citation-score {
  color: var(--text-soft);
  font-size: 11px;
  white-space: nowrap;
}

.citation-section {
  color: var(--text-muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.citation-snippet {
  color: var(--text);
  font-size: 13px;
  line-height: 1.55;
}

.empty-state {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  color: var(--text-muted);
  padding: 14px;
  font-size: 13px;
}

.error-text {
  color: var(--danger);
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 270px minmax(0, 1fr);
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .citation-pane {
    display: none;
  }

  .config-grid {
    grid-template-columns: minmax(220px, 1fr) 150px 130px;
  }
}

@media (max-width: 760px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    overflow: auto;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .sidebar {
    height: auto;
    min-height: auto;
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .main {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .workspace {
    min-height: calc(100dvh - 194px);
    overflow: visible;
  }

  .document-list {
    max-height: 220px;
    flex: none;
  }

  .config-grid {
    grid-template-columns: 1fr;
  }

  .message-list {
    padding: 20px 16px;
  }

  .message {
    grid-template-columns: 28px minmax(0, 1fr);
    gap: 10px;
  }

  .message-avatar {
    width: 26px;
    height: 26px;
  }

  .composer {
    width: calc(100% - 24px);
    grid-template-columns: 1fr;
  }

  .composer-actions {
    justify-content: flex-end;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
