:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #17202a;
  --muted: #667085;
  --line: #d8dee8;
  --accent: #167b8f;
  --accent-strong: #0f6070;
  --rag: #6156c9;
  --tool: #bd6520;
  --error: #b42318;
  --soft: #eef5f6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(22, 123, 143, 0.09), transparent 280px),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

.shell {
  width: min(1240px, calc(100vw - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 28px 0;
  display: grid;
  align-items: start;
}

.workspace {
  min-height: calc(100vh - 56px);
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: visible;
  box-shadow: 0 24px 80px rgba(23, 32, 42, 0.12);
}

.sidebar {
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 26px;
  background: var(--soft);
  border-right: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--accent-strong);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  max-width: 13ch;
  font-size: 2rem;
  line-height: 1.06;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 1.35rem;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 8px;
  font-size: 1rem;
}

.status-grid {
  display: grid;
  gap: 10px;
}

.status-grid div {
  padding: 14px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.status-grid span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.8rem;
}

.status-grid strong {
  display: block;
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.status-grid p {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.38;
}

.composer button,
.database-controls button,
.local-doc-actions button,
.local-doc-item button,
.row-actions button,
.dialog-actions button,
.file-button,
.icon-button,
.tab {
  min-height: 42px;
  border-radius: 8px;
  cursor: pointer;
}

.composer button:hover,
.database-controls button:hover,
.local-doc-actions button:hover,
.local-doc-item button:hover,
.row-actions button:hover,
.dialog-actions button:hover,
.file-button:hover,
.icon-button:hover,
.tab:hover {
  filter: brightness(0.97);
}

.main-panel {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #fbfcfd;
}

.tabs {
  padding: 14px 18px;
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

.tab {
  padding: 0 16px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #ffffff;
}

.tab.active {
  color: #ffffff;
  border-color: var(--accent);
  background: var(--accent);
}

.view {
  min-height: 0;
  display: none;
}

.view.active {
  display: grid;
}

#chat-view {
  grid-template-rows: minmax(0, 1fr) auto;
}

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

.message {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 12px;
  align-items: start;
}

.message.user {
  grid-template-columns: minmax(0, 1fr) 38px;
}

.message.user .avatar {
  grid-column: 2;
  background: #1f2a37;
}

.message.user .bubble {
  grid-column: 1;
  grid-row: 1;
  justify-self: end;
  background: #edf1f6;
}

.avatar {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #ffffff;
  background: var(--accent);
  font-size: 0.76rem;
  font-weight: 800;
}

.bubble {
  width: fit-content;
  max-width: min(760px, 100%);
  padding: 13px 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.meta {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  max-width: 100%;
  padding: 4px 8px;
  border-radius: 999px;
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 800;
  overflow-wrap: anywhere;
}

.chip.rag {
  background: var(--rag);
}

.chip.tool {
  background: var(--tool);
}

.chip.latency,
.chip.source,
.chip.reason {
  color: var(--ink);
  background: #e9edf3;
}

.message.error .bubble {
  border-color: rgba(180, 35, 24, 0.35);
  color: var(--error);
}

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

.composer input,
.panel-header select {
  min-width: 0;
  height: 48px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  outline: none;
}

.composer input:focus,
.panel-header select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(22, 123, 143, 0.16);
}

.composer button {
  border: 0;
  color: #ffffff;
  background: var(--accent);
}

.composer button:disabled {
  cursor: progress;
  opacity: 0.68;
}

.database-view,
.sources-view {
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
}

.panel-header {
  padding: 24px 28px 18px;
  display: flex;
  gap: 16px;
  align-items: end;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

.database-controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.panel-header select,
.database-controls {
  width: min(520px, 100%);
}

.database-controls button {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #ffffff;
}

.active-db {
  margin: 8px 0 0;
  max-width: 560px;
  color: var(--muted);
  font-size: 0.84rem;
  overflow-wrap: anywhere;
}

.table-wrap {
  min-width: 0;
  min-height: 0;
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}

th,
td {
  max-width: 340px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.row-actions {
  min-width: 132px;
  white-space: nowrap;
}

.row-actions button {
  min-height: 34px;
  margin-right: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #ffffff;
}

.row-dialog {
  width: min(680px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  box-shadow: 0 24px 80px rgba(23, 32, 42, 0.24);
}

.row-dialog::backdrop {
  background: rgba(23, 32, 42, 0.32);
}

.dialog-header {
  padding: 18px 20px;
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
}

.icon-button {
  width: 38px;
  min-height: 38px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #ffffff;
}

.row-fields {
  max-height: 500px;
  padding: 18px 20px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.row-fields label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.row-fields input {
  min-width: 0;
  height: 42px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  outline: none;
}

.row-fields input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(22, 123, 143, 0.16);
}

.dialog-actions {
  padding: 14px 20px;
  display: flex;
  justify-content: end;
  gap: 10px;
  border-top: 1px solid var(--line);
}

.dialog-actions button {
  min-width: 92px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #ffffff;
}

#save-row-button {
  border-color: var(--accent);
  color: #ffffff;
  background: var(--accent);
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: #344054;
  background: #f1f4f8;
  font-size: 0.78rem;
  text-transform: uppercase;
}

.source-grid {
  min-height: 0;
  padding: 24px 28px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.source-card {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.source-card p {
  margin-bottom: 12px;
  color: var(--muted);
  line-height: 1.45;
}

.local-docs-card {
  grid-column: 1 / -1;
}

.local-doc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.local-doc-actions button,
.file-button,
.local-doc-item button {
  min-height: 38px;
  padding: 0 12px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  cursor: pointer;
}

#local-doc-upload {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.local-doc-list {
  margin-top: 12px;
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.local-doc-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

code {
  display: block;
  padding: 9px 10px;
  border-radius: 8px;
  color: #213547;
  background: #eef2f6;
  overflow-wrap: anywhere;
}

@media (max-width: 900px) {
  .shell {
    width: 100%;
    min-height: 100vh;
    padding: 0;
  }

  .workspace {
    height: auto;
    min-height: 100vh;
    grid-template-columns: 1fr;
    border: 0;
    border-radius: 0;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  h1 {
    max-width: none;
    font-size: 1.7rem;
  }

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

  .main-panel {
    min-height: 720px;
  }
}

@media (max-width: 640px) {
  .sidebar,
  .messages,
  .source-grid {
    padding: 18px;
  }

  .status-grid,
  .composer,
  .source-grid {
    grid-template-columns: 1fr;
  }

  .tabs,
  .panel-header {
    flex-direction: column;
    align-items: stretch;
  }

  .database-controls {
    justify-content: stretch;
  }

  .database-controls,
  .panel-header select {
    width: 100%;
  }

  .row-fields {
    grid-template-columns: 1fr;
  }

  .tab,
  .composer button {
    width: 100%;
  }
}
