:root {
  --ink: #1e2b28;
  --muted: #69736f;
  --paper: #f5f1e8;
  --card: #fffdf8;
  --line: #ded8cb;
  --forest: #214c43;
  --forest-light: #e5eeea;
  --rust: #a65337;
  --shadow: 0 22px 55px rgba(38, 48, 44, 0.1);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 8%, rgba(166, 83, 55, 0.09), transparent 28rem),
    var(--paper);
  font-family: "DM Sans", sans-serif;
}

button, textarea { font: inherit; }
button { cursor: pointer; }

.page-shell { width: min(1680px, 100%); margin: 0 auto; min-height: 100vh; padding: 0 3vw; }

.fixed-intro {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--paper);
  box-shadow: 0 1px 0 rgba(30, 43, 40, 0.08);
}

.site-header {
  min-height: 84px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(30, 43, 40, 0.12);
}

.brand { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; }
.brand-copy { display: grid; gap: 3px; }
.brand-mark, .avatar {
  display: grid;
  place-items: center;
  background: transparent;
}
.brand-mark { width: 46px; height: 46px; border-radius: 50%; filter: drop-shadow(0 7px 10px rgba(33, 76, 67, 0.18)); }
.provenance-logo { display: block; width: 100%; height: 100%; object-fit: contain; }
.brand strong { font-family: "Libre Caslon Display", serif; font-size: 27px; font-weight: 700; line-height: 1.05; }
.brand small { color: var(--forest); font-size: 12px; line-height: 1.25; }

main { padding: 20px 0 36px; }
.section-label { margin: 0 0 10px; color: var(--rust); font-size: 12px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; }
h2 { font-family: "Libre Caslon Display", serif; font-weight: 400; }

.research-grid { --workspace-height: clamp(760px, calc(100vh - 124px), 1050px); display: grid; grid-template-columns: minmax(250px, 0.31fr) minmax(560px, 1fr) minmax(300px, 0.38fr); gap: 20px; align-items: stretch; }
.sidebar-stack { display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 18px; position: sticky; top: 18px; height: var(--workspace-height); max-height: var(--workspace-height); overflow: hidden; }
.guide-card, .history-card, .assistant-card, .field-card { border: 1px solid var(--line); background: var(--card); box-shadow: var(--shadow); }
.guide-card, .history-card, .field-card { border-radius: 24px; padding: 24px; }
.guide-card { overflow-y: auto; scrollbar-width: thin; }
.guide-card h2, .assistant-heading h2 { margin: 0; font-size: 30px; }
.guide-card > p:not(.section-label) { color: var(--muted); line-height: 1.6; }
.history-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.history-heading h2 { margin: 0; font-family: "Libre Caslon Display", serif; font-size: 21px; font-weight: 400; white-space: nowrap; }
.new-chat-button { flex: 0 0 auto; padding: 9px 12px; border: 0; border-radius: 9px; color: white; background: var(--forest); font-size: 12px; font-weight: 700; }
.new-chat-button:hover { background: #173c34; }
.chat-history { display: grid; gap: 7px; margin-top: 18px; max-height: 220px; overflow-y: auto; }
.history-empty { margin: 0; padding: 14px; border-radius: 10px; color: var(--muted); background: #f4f1e9; font-size: 12px; line-height: 1.5; }
.history-item { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 4px; padding: 5px; border: 1px solid transparent; border-radius: 10px; }
.history-item.active { border-color: #c7d8d2; background: var(--forest-light); }
.history-select { min-width: 0; padding: 7px; border: 0; color: var(--ink); background: transparent; text-align: left; }
.history-select strong, .history-select small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-select strong { font-size: 12px; font-weight: 600; }
.history-select small { margin-top: 2px; color: var(--muted); font-size: 10px; }
.history-action { width: 27px; height: 27px; padding: 0; border: 0; border-radius: 7px; color: var(--muted); background: transparent; font-size: 12px; }
.history-action:hover { color: var(--rust); background: white; }
.guide-card > .field-guide { display: none; }
.field-card { position: sticky; top: 18px; height: var(--workspace-height); max-height: var(--workspace-height); overflow: hidden; }
.field-card h2 { margin: 0; font-family: "Libre Caslon Display", serif; font-size: 27px; font-weight: 400; }
.field-card > p:not(.section-label) { margin: 11px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }
.field-guide { margin-top: 18px; border: 1px solid var(--line); border-radius: 13px; background: #faf8f2; }
.field-card .field-guide { display: block; }
.field-guide summary { padding: 13px 15px; color: var(--forest); cursor: pointer; font-size: 13px; font-weight: 700; }
.field-guide-body { padding: 0 13px 14px; border-top: 1px solid var(--line); }
.field-guide-intro { margin: 13px 0 10px; color: var(--muted); font-size: 12px; }
.field-search { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; color: var(--ink); background: white; outline: none; font: inherit; font-size: 12px; }
.field-search:focus { border-color: var(--forest); box-shadow: 0 0 0 3px rgba(33, 76, 67, 0.08); }
.field-groups { margin-top: 12px; max-height: calc(var(--workspace-height) - 330px); overflow-y: auto; }
.field-group { margin-bottom: 8px; border: 1px solid #e8e2d7; border-radius: 9px; background: white; }
.field-group:last-child { margin-bottom: 0; }
.field-group > summary { padding: 10px 11px; color: var(--rust); font-size: 10px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; }
.field-group[open] > summary { border-bottom: 1px solid #e8e2d7; background: #faf8f2; }
.field-group dl { margin: 0; }
.field-entry { display: grid; grid-template-columns: minmax(100px, 0.42fr) minmax(0, 1fr); gap: 10px; padding: 8px 4px; border-bottom: 1px solid #e8e2d7; font-size: 11px; }
.field-entry dt { color: var(--ink); font-weight: 700; }
.field-entry dd { margin: 0; color: var(--muted); line-height: 1.45; }
.field-entry[hidden], .field-group[hidden] { display: none; }
.no-fields { margin: 12px 0 0; color: var(--muted); font-size: 12px; }
.prompt-list { display: grid; gap: 10px; margin: 20px 0 0; }
.suggestion {
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  background: transparent;
  text-align: left;
  transition: 160ms ease;
}
.suggestion:hover, .suggestion:focus-visible { border-color: var(--forest); background: var(--forest-light); transform: translateX(3px); }
.assistant-card { display: flex; flex-direction: column; height: var(--workspace-height); border-radius: 24px; overflow: hidden; }
.assistant-heading { position: sticky; top: 0; z-index: 5; padding: 27px 30px; display: flex; justify-content: space-between; align-items: center; gap: 20px; border-bottom: 1px solid var(--line); background: var(--card); }
.text-button { border: 0; background: transparent; color: var(--muted); font-size: 12px; }
.text-button:hover { color: var(--rust); }
.clear-chat-button { flex: 0 0 auto; padding: 10px 13px; border: 1px solid #c8b9ad; border-radius: 9px; color: #7c432f; background: #fffaf5; font-size: 12px; font-weight: 700; }
.clear-chat-button:hover { border-color: var(--rust); background: #f8e9e2; }
.clear-chat-button:disabled, .new-chat-button:disabled { cursor: wait; opacity: 0.55; }
.conversation { flex: 1; min-height: 0; max-height: none; padding: 28px 30px; overflow-y: auto; }
.message { display: flex; gap: 13px; margin-bottom: 23px; align-items: flex-start; }
.user-message { justify-content: flex-end; }
.avatar { flex: 0 0 38px; width: 38px; height: 38px; border-radius: 50%; filter: drop-shadow(0 4px 7px rgba(33, 76, 67, 0.16)); }
.message-body { max-width: min(690px, 88%); padding: 14px 17px; border-radius: 4px 16px 16px 16px; background: var(--forest-light); line-height: 1.6; }
.user-message .message-body { background: var(--forest); color: white; border-radius: 16px 4px 16px 16px; }
.message-body p { margin: 0; }
.welcome-action { display: flex; justify-content: center; padding: 4px 0 24px; }
.welcome-chat-button {
  padding: 13px 20px;
  border: 0;
  border-radius: 9px;
  color: white;
  background: var(--forest);
  font-size: 14px;
  font-weight: 700;
}
.welcome-chat-button:hover, .welcome-chat-button:focus-visible { background: #173c34; }
.error-message .message-body { background: #f7e6df; color: #773923; }

.result-toolbar { position: sticky; top: 0; z-index: 4; margin-top: 14px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: rgba(255, 253, 248, 0.97); box-shadow: 0 8px 20px rgba(38, 48, 44, 0.08); }
.result-meta { margin: 0 0 9px; color: var(--muted); font-size: 12px; font-weight: 600; }
.sql-panel { margin-top: 14px; border: 1px solid #cfdad5; border-radius: 10px; background: #f8fbf9; }
.sql-panel summary { padding: 9px 11px; color: var(--forest); cursor: pointer; font-size: 12px; font-weight: 700; }
.sql-panel pre { margin: 0; padding: 12px; overflow-x: auto; border-top: 1px solid #dce5e1; color: #263a34; font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; white-space: pre-wrap; word-break: break-word; }
.pagination { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.pagination button { padding: 8px 12px; border: 1px solid var(--forest); border-radius: 8px; color: var(--forest); background: white; font-size: 12px; font-weight: 600; }
.pagination button:disabled { cursor: default; opacity: 0.4; }
.pagination span { margin-right: auto; color: var(--muted); font-size: 12px; }
.page-size-label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; }
.page-size-label select { padding: 7px 25px 7px 8px; border: 1px solid var(--line); border-radius: 8px; color: var(--ink); background: white; }
.pagination .export-button { color: white; background: var(--forest); }
.table-wrap { max-height: 430px; margin-top: 10px; overflow: auto; border: 1px solid var(--line); border-radius: 12px; background: white; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 11px 12px; border-bottom: 1px solid #ece7dc; text-align: left; vertical-align: top; }
th { position: sticky; top: 0; z-index: 2; color: var(--forest); background: #f4f7f5; font-weight: 700; white-space: nowrap; box-shadow: 0 1px 0 #ece7dc; }
tr:last-child td { border-bottom: 0; }
td { max-width: 360px; }

.composer { display: flex; gap: 12px; padding: 20px 30px 10px; border-top: 1px solid var(--line); }
.composer[hidden] { display: none; }
textarea { flex: 1; resize: none; min-height: 58px; padding: 15px; color: var(--ink); background: white; border: 1px solid var(--line); border-radius: 14px; outline: none; }
textarea:focus { border-color: var(--forest); box-shadow: 0 0 0 3px rgba(33, 76, 67, 0.1); }
.submit-button { min-width: 155px; display: flex; justify-content: center; align-items: center; gap: 12px; padding: 0 18px; border: 0; border-radius: 14px; background: var(--rust); color: white; font-weight: 700; }
.submit-button:hover { background: #8e432d; }
.submit-button:disabled { cursor: wait; opacity: 0.6; }
.disclaimer-card {
  margin-top: 20px;
  padding: 28px 30px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.disclaimer-summary {
  cursor: pointer;
  list-style: none;
}
.disclaimer-summary::-webkit-details-marker { display: none; }
.disclaimer-card h2 {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  color: var(--rust);
  font-family: "DM Sans", sans-serif;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.disclaimer-toggle {
  flex: 0 0 auto;
  color: var(--forest);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
}
.disclaimer-card[open] .when-closed { display: none; }
.disclaimer-card:not([open]) .when-open { display: none; }
.disclaimer-copy { max-width: 1320px; margin-top: 14px; }
.disclaimer-copy p { margin: 0 0 14px; color: var(--muted); font-size: 14px; line-height: 1.7; }
.disclaimer-copy p:last-child { margin-bottom: 0; }
.disclaimer-copy a { color: var(--forest); font-weight: 600; text-underline-offset: 3px; }
.disclaimer-copy a:hover, .disclaimer-copy a:focus-visible { color: var(--rust); }
footer { min-height: 78px; margin-top: 20px; display: flex; justify-content: space-between; align-items: center; border-top: 1px solid rgba(30, 43, 40, 0.12); color: var(--muted); font-size: 14px; }
.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: 1300px) {
  .research-grid { grid-template-columns: minmax(270px, 0.38fr) minmax(0, 1fr); }
  .field-card { position: static; grid-column: 1 / -1; height: auto; max-height: none; }
  .field-card .field-groups { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; max-height: 480px; }
}

@media (max-width: 900px) {
  .research-grid { grid-template-columns: 1fr; }
  .sidebar-stack { position: static; grid-template-rows: auto; height: auto; max-height: none; overflow: visible; }
  .guide-card { overflow: visible; }
  .field-card { grid-column: auto; }
  .field-card .field-groups { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .prompt-list { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 620px) {
  .page-shell { padding: 0 18px; }
  .site-header { min-height: 76px; }
  .brand { gap: 10px; }
  .brand-mark { width: 40px; height: 40px; }
  .brand strong { font-size: 22px; }
  .brand small { font-size: 11px; }
  footer span:last-child { display: none; }
  .prompt-list { grid-template-columns: 1fr; }
  .guide-card, .history-card, .assistant-card, .field-card { border-radius: 18px; }
  .assistant-heading, .conversation { padding-left: 18px; padding-right: 18px; }
  .assistant-heading { align-items: flex-start; gap: 12px; }
  .clear-chat-button { padding: 8px 10px; }
  .field-entry { grid-template-columns: 1fr; gap: 3px; }
  .field-card .field-groups { grid-template-columns: 1fr; }
  .composer { flex-direction: column; padding-left: 18px; padding-right: 18px; }
  .pagination span { width: 100%; margin-right: 0; }
  .page-size-label { margin-left: auto; }
  .submit-button { min-height: 50px; }
  .disclaimer-card { padding: 22px 20px; border-radius: 18px; }
  .disclaimer-card h2 { font-size: 12px; }
  .disclaimer-copy p { font-size: 13px; }
}
