:root {
  color-scheme: light;
  --ink: #1a1a1a;
  --body: #4a4a4a;
  --muted: #8a8a8a;
  --line: #e8e8e8;
  --surface: #ffffff;
  --surface-soft: #fbf7f4;
  --brand-tint: #f5e8e0;
  --bg: #fbf7f4;
  --nav: #ffffff;
  --accent: #e75127;
  --accent-strong: #c83f1b;
  --danger: #b42318;
  --warning: #9a6700;
  --success: #166534;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Nunito, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--body);
  background: var(--bg);
  font-size: 16px;
  line-height: 1.6;
}

a { color: inherit; }

.app-body {
  display: grid;
  grid-template-columns: 228px minmax(0, 1fr);
  min-height: 100vh;
}

.auth-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.sidebar {
  background: var(--nav);
  color: var(--ink);
  padding: 24px 20px;
  border-right: 1px solid var(--line);
}

.brand {
  color: var(--ink);
  display: block;
  font-weight: 800;
  font-size: 24px;
  margin-bottom: 32px;
  text-decoration: none;
  font-family: "Bricolage Grotesque", ui-sans-serif, system-ui, sans-serif;
}

nav {
  display: grid;
  gap: 6px;
}

nav a {
  color: var(--body);
  display: block;
  padding: 10px 12px;
  border-radius: 6px;
  text-decoration: none;
  border: 1px solid transparent;
}

nav a:hover {
  background: var(--brand-tint);
  border-color: #efd4c7;
  color: var(--ink);
}

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

.topbar {
  min-width: 0;
  min-height: 72px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.tenant-current {
  display: grid;
  gap: 2px;
}

.tenant-name {
  font-weight: 750;
  font-size: 17px;
  color: var(--ink);
  font-family: "Bricolage Grotesque", ui-sans-serif, system-ui, sans-serif;
}

.tenant-slug,
.filename,
.page-heading p,
small {
  color: var(--muted);
}

.topbar-actions,
.tenant-switcher,
.document-actions {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.main {
  min-width: 0;
  padding: 28px;
  max-width: 1280px;
  width: 100%;
}

.chat-main {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.auth-main {
  width: min(420px, 100%);
}

.page-heading {
  min-width: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

h1, h2, h3, p { margin-top: 0; }
h1, h2, h3 {
  color: var(--ink);
  font-family: "Bricolage Grotesque", ui-sans-serif, system-ui, sans-serif;
}
h1 { font-size: 30px; line-height: 1.2; margin-bottom: 6px; font-weight: 800; }
h2 { font-size: 19px; margin-bottom: 14px; font-weight: 800; }
h3 { font-size: 15px; margin-bottom: 0; font-weight: 700; }

.panel,
.message,
.chunk,
.entity-panel,
.table-shell {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel,
.message,
.chunk,
.entity-panel {
  padding: 18px;
}

.narrow { max-width: 420px; }
.stack { display: grid; gap: 16px; }
.auth-copy { color: var(--muted); }

.qr-panel {
  display: grid;
  place-items: center;
  padding: 18px;
  margin: 16px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

.qr-panel img {
  width: min(240px, 100%);
  height: auto;
  border: 8px solid white;
}

.manual-secret {
  display: grid;
  gap: 6px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 13px;
}

.manual-secret code {
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
}

.otp-input {
  font-size: 22px;
  letter-spacing: 0;
  text-align: center;
}

input,
select,
textarea,
button,
.button {
  font: inherit;
  border-radius: 6px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  padding: 10px 12px;
  background: white;
  color: var(--ink);
}

button,
.button {
  border: 0;
  background: var(--ink);
  color: white;
  padding: 10px 14px;
  text-decoration: none;
  cursor: pointer;
  min-height: 40px;
}

button:hover,
.button:hover {
  background: #2a2a2a;
}

button.danger {
  background: var(--danger);
}

.ghost-button,
.secondary-button {
  background: transparent;
  color: var(--ink);
  border: 1.5px solid #d0d0d0;
}

.ghost-button:hover,
.secondary-button:hover {
  background: var(--brand-tint);
  border-color: var(--accent);
}

.chat-shell {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 18px;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.conversation {
  min-height: 0;
  overflow-y: auto;
  padding-right: 2px;
  display: grid;
  gap: 14px;
  align-content: start;
}

.query-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.query-form textarea {
  min-height: 60px;
  max-height: 160px;
  resize: vertical;
}

@media (min-width: 861px) {
  .app-body {
    height: 100vh;
    overflow: hidden;
  }

  .sidebar,
  .main {
    min-height: 0;
    overflow-y: auto;
  }

  .workspace {
    min-height: 0;
    height: 100vh;
  }

  .chat-main {
    overflow: hidden;
  }
}

.htmx-indicator { display: none; color: var(--muted); }
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator { display: inline; }

.question {
  color: var(--muted);
  font-weight: 650;
}

.answer {
  line-height: 1.55;
}

.answer p {
  margin: 0 0 10px;
}

.answer ul {
  margin: 0 0 12px 18px;
  padding: 0;
}

.answer li + li {
  margin-top: 6px;
}

.citations {
  display: grid;
  gap: 10px;
  padding-left: 20px;
}

.citations blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--line);
  color: var(--muted);
}

.upload-form {
  width: min(520px, 100%);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

.notice {
  background: var(--brand-tint);
  color: var(--ink);
  border: 1px solid #efd4c7;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 16px;
}

.danger-notice {
  background: #fff1f0;
  color: var(--danger);
  border-color: #ffc9c2;
}

.table-shell {
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: white;
}

th,
td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  font-size: 13px;
  color: var(--muted);
  font-weight: 700;
  background: var(--surface-soft);
}

td a {
  display: block;
  font-weight: 650;
  text-decoration: none;
}

.filename {
  display: block;
  font-size: 13px;
  margin-top: 4px;
}

.badge,
.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--brand-tint);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.badge-indexed { background: #e8f5ee; color: var(--success); }
.badge-failed { background: #fff1f0; color: var(--danger); }
.badge-pending,
.badge-parsing,
.badge-chunking,
.badge-embedding,
.badge-indexing {
  background: #fff8e1;
  color: var(--warning);
}

.empty-cell {
  color: var(--muted);
  text-align: center;
}

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 18px;
  align-items: start;
}

.source-list {
  display: grid;
  gap: 14px;
}

.chunk header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  color: var(--muted);
}

.chunk-context {
  color: var(--muted);
  line-height: 1.45;
}

pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0;
  line-height: 1.45;
  font-size: 13px;
}

.entity-panel {
  position: sticky;
  top: 88px;
}

.entity-panel ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 10px;
}

.entity-panel li {
  display: grid;
  gap: 3px;
}

.entity-panel span {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

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

@media (max-width: 860px) {
  .app-body { grid-template-columns: 1fr; }
  .sidebar {
    display: flex;
    gap: 18px;
    align-items: center;
    padding: 16px 20px;
  }
  .brand { margin: 0; }
  nav { display: flex; gap: 8px; }
  .topbar,
  .page-heading,
  .topbar-actions,
  .document-actions {
    align-items: stretch;
    flex-direction: column;
    width: 100%;
  }
  .tenant-switcher {
    width: 100%;
  }
  .tenant-switcher select {
    min-width: 0;
  }
  .tenant-switcher button {
    flex: 0 0 auto;
  }
  .main { padding: 20px; }
  .chat-shell {
    flex: none;
    min-height: auto;
    grid-template-rows: auto auto auto;
  }
  .conversation {
    overflow: visible;
  }
  .conversation:empty {
    min-height: 16px;
  }
  .query-form,
  .upload-form,
  .detail-grid {
    grid-template-columns: 1fr;
  }
  .entity-panel {
    position: static;
  }
}
