:root {
  --bg: #f9f8f5;
  --surface: #ffffff;
  --surface-2: #f1efe8;
  --border: #e4e1d8;
  --text: #29261f;
  --muted: #76716a;
  --accent: #c2410c;
  --accent-hover: #a8380a;
  --user-bubble: #efece4;
  --code-bg: #f4f2ec;
  --danger: #b42318;
  --ok: #2f7d4f;
  --shadow: 0 1px 2px rgba(41, 38, 31, .04), 0 4px 16px rgba(41, 38, 31, .06);
  --sidebar-bg: #f2f0ea;
  --hover: #e9e6de;
  --active: #e3dfd5;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1b;
    --surface: #2a2925;
    --surface-2: #302e2a;
    --border: #3c3a35;
    --text: #ece9e1;
    --muted: #a39e94;
    --accent: #e0703f;
    --accent-hover: #ea855a;
    --user-bubble: #34322d;
    --code-bg: #262521;
    --danger: #f2877d;
    --ok: #6cc08f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .2), 0 4px 16px rgba(0, 0, 0, .25);
    --sidebar-bg: #1a1917;
    --hover: #2a2925;
    --active: #33312c;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  overflow: hidden;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* App shell: sidebar + main column */
.app { display: flex; height: 100vh; height: 100dvh; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.scroller { flex: 1; overflow-y: auto; }

/* Sidebar */
.sidebar {
  --sidebar-w: 272px;
  flex: none; width: var(--sidebar-w); display: flex; flex-direction: column;
  background: var(--sidebar-bg); border-right: 1px solid var(--border);
  transition: margin-left .2s ease;
}
.app.sidebar-closed .sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px 16px; }
.new-chat {
  display: flex; align-items: center; gap: 8px; margin: 6px 12px 10px;
  padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-size: 14px; font-weight: 500; text-align: left;
}
.new-chat svg { color: var(--accent); }
.new-chat:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.history-label { padding: 8px 18px 4px; font-size: 12px; font-weight: 600; color: var(--muted); }
.history-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.history-group { padding: 12px 10px 4px; font-size: 11.5px; color: var(--muted); }
.history-item {
  display: flex; align-items: center; gap: 4px; border-radius: 8px;
  padding: 0 4px 0 0;
}
.history-item:hover { background: var(--hover); }
.history-item.active { background: var(--active); }
.history-open {
  flex: 1; min-width: 0; background: none; border: 0; text-align: left;
  padding: 7px 10px; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-del {
  flex: none; width: 26px; height: 26px; border-radius: 6px; border: 0; background: none;
  color: var(--muted); display: grid; place-items: center; opacity: 0;
}
.history-item:hover .history-del, .history-item.active .history-del, .history-del:focus-visible { opacity: 1; }
.history-del:hover { color: var(--danger); background: var(--surface-2); }
.history-empty { padding: 10px 12px; color: var(--muted); font-size: 13px; }
.sidebar-foot { padding: 10px 16px 14px; border-top: 1px solid var(--border); }
.text-btn { background: none; border: 0; padding: 0; color: var(--muted); font-size: 12.5px; }
.text-btn:hover { color: var(--danger); }
.text-btn.plain:hover { color: var(--text); }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-name { font-weight: 700; letter-spacing: .08em; }
.brand-tag { font-size: 10.5px; font-weight: 500; color: var(--muted); letter-spacing: .02em; margin-top: 2px; }
.user-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.user-avatar {
  flex: none; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 18%, var(--surface)); color: var(--accent);
  font-weight: 600; font-size: 13px; text-transform: uppercase;
}
.user-name { flex: 1; min-width: 0; font-size: 13.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ghost.small { padding: 4px 10px; font-size: 12.5px; }
.foot-links { display: flex; justify-content: space-between; }

/* Disclaimer dialog */
.disclaimer {
  width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow: auto;
  padding: 0; border: 1px solid var(--border); border-radius: 18px;
  background: var(--surface); color: var(--text); box-shadow: 0 24px 64px rgba(0, 0, 0, .25);
}
.disclaimer::backdrop { background: rgba(20, 18, 14, .55); backdrop-filter: blur(3px); }
.disclaimer[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.disc-head { padding: 26px 28px 8px; }
.disc-badge {
  display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11.5px; font-weight: 600;
  letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent);
}
.disc-head h2 { margin: 12px 0 8px; font-family: var(--serif); font-weight: 400; font-size: 28px; }
.disc-head p { margin: 0; color: var(--text); }
.disc-section { margin: 16px 28px 0; padding: 16px 18px; border-radius: 12px; background: var(--surface-2); }
.disc-section h3 { margin: 0 0 4px; font-size: 13.5px; font-weight: 600; }
.disc-desc { margin: 0 0 12px; color: var(--muted); font-size: 13.5px; }
.disc-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.disc-stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.disc-stat .n { display: block; font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.disc-stat .l { display: block; font-size: 12px; color: var(--muted); text-transform: capitalize; }
.disc-loading { grid-column: 1 / -1; color: var(--muted); font-size: 13px; }
.disc-depts { margin: 12px 0 0; font-size: 12.5px; color: var(--muted); }
.disc-notes { margin: 16px 28px 0; padding-left: 18px; font-size: 13.5px; color: var(--muted); }
.disc-notes li { margin: 4px 0; }
.disc-actions { padding: 20px 28px 24px; display: flex; justify-content: flex-end; }
.disc-btn {
  border: 0; border-radius: 10px; padding: 11px 18px; background: var(--accent); color: #fff; font-weight: 600;
}
.disc-btn:hover { background: var(--accent-hover); }
@media (max-width: 520px) {
  .disc-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .disc-head { padding: 22px 20px 6px; }
  .disc-section, .disc-notes { margin-left: 20px; margin-right: 20px; }
  .disc-actions { padding: 18px 20px 20px; }
  .disc-btn { width: 100%; }
}
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 19; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; min-height: 54px;
}
.topbar-left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.chat-title { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.app:not(.sidebar-closed) #openSidebarBtn { display: none; }
.sidebar-toggle { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; }
.sidebar-toggle:hover { background: var(--surface-2); color: var(--text); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -.01em; }
.logo {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff;
}
.ghost {
  background: transparent; border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px; font-size: 13.5px; color: var(--muted);
}
.ghost:hover { background: var(--surface-2); color: var(--text); }

/* Schema panel */
.schema-panel {
  position: absolute; top: 54px; right: 16px; z-index: 10;
  width: min(340px, calc(100vw - 32px)); max-height: 70vh; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 14px 16px;
}
.schema-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.icon-btn { background: none; border: 0; font-size: 22px; line-height: 1; color: var(--muted); }
.schema-list .tbl { padding: 8px 0; border-top: 1px solid var(--border); }
.schema-list .tbl:first-child { border-top: 0; }
.schema-list .tbl-name { font-family: var(--mono); font-size: 13px; font-weight: 600; }
.schema-list .tbl-cols { color: var(--muted); font-size: 12.5px; font-family: var(--mono); }

/* Chat area */
.chat { flex: 1; width: 100%; max-width: 780px; margin: 0 auto; padding: 8px 16px 24px; }

.welcome { text-align: center; padding: 12vh 0 24px; }
.welcome h1 { font-family: var(--serif); font-weight: 400; font-size: clamp(26px, 5vw, 36px); margin: 0 0 6px; letter-spacing: -.01em; }
.welcome p { color: var(--muted); margin: 0 0 28px; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; text-align: left; }
.suggestions button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; font-size: 14px; color: var(--text);
  transition: border-color .15s, transform .15s;
}
.suggestions button:hover { border-color: var(--accent); transform: translateY(-1px); }

.messages { display: flex; flex-direction: column; gap: 22px; }

.msg-user {
  align-self: flex-end; max-width: 85%;
  background: var(--user-bubble); padding: 10px 16px; border-radius: 18px 18px 4px 18px;
  white-space: pre-wrap; word-wrap: break-word;
}

.msg-assistant { display: flex; gap: 12px; align-items: flex-start; }
.avatar {
  flex: none; width: 28px; height: 28px; border-radius: 8px; margin-top: 2px;
  display: grid; place-items: center; background: var(--accent); color: #fff;
}
.assistant-body { flex: 1; min-width: 0; }

/* Answer text */
.answer { font-family: var(--serif); font-size: 16.5px; line-height: 1.65; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer p { margin: 0 0 .8em; }
.answer ul, .answer ol { padding-left: 1.3em; margin: 0 0 .8em; }
.answer li { margin: .2em 0; }
.answer code { font-family: var(--mono); font-size: .85em; background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.answer table { border-collapse: collapse; font-family: var(--sans); font-size: 14px; margin: .5em 0 1em; }
.answer th, .answer td { border: 1px solid var(--border); padding: 4px 10px; }
.answer.streaming > :last-child::after {
  content: ""; display: inline-block; width: 8px; height: 16px; margin-left: 2px; vertical-align: -2px;
  background: var(--accent); border-radius: 2px; animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Thinking indicator */
.thinking { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; padding: 4px 0; }
.dots { display: inline-flex; gap: 4px; }
.dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: bounce 1.2s infinite ease-in-out; }
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

/* Query card */
.query-card {
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  margin-bottom: 14px; overflow: hidden;
}
.query-head {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 14px; background: none; border: 0; text-align: left; font-size: 14px;
}
.query-head:hover { background: var(--surface-2); }
.status-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite; }
.status-dot.ok { background: var(--ok); animation: none; }
.status-dot.err { background: var(--danger); animation: none; }
@keyframes pulse { 50% { opacity: .35; } }
.query-title { font-weight: 500; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.query-meta { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.chev { color: var(--muted); transition: transform .15s; }
.query-card.open .chev { transform: rotate(90deg); }
.query-body { display: none; border-top: 1px solid var(--border); }
.query-card.open .query-body { display: block; }
.sql {
  margin: 0; padding: 12px 14px; background: var(--code-bg);
  font-family: var(--mono); font-size: 12.5px; line-height: 1.5;
  white-space: pre-wrap; word-break: break-word; color: var(--text);
}
.query-error { color: var(--danger); padding: 10px 14px; font-size: 13.5px; border-top: 1px solid var(--border); }

/* Results table */
.table-scroll { max-height: 340px; overflow: auto; }
table.result { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
table.result th {
  position: sticky; top: 0; background: var(--surface-2); text-align: left;
  font-weight: 600; padding: 7px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.result td { padding: 6px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.result td.num { text-align: right; }
table.result tr:last-child td { border-bottom: 0; }
.result-foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; font-size: 12.5px; color: var(--muted); border-top: 1px solid var(--border);
}
.foot-actions { display: flex; gap: 14px; }
.sql:not([hidden]) + .result-wrap { border-top: 1px solid var(--border); }
.link-btn { background: none; border: 0; color: var(--accent); font-size: 12.5px; padding: 0; }
.link-btn:hover { text-decoration: underline; }

.fatal { color: var(--danger); font-size: 14px; }

/* Charts */
.chart-card {
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  margin-bottom: 14px; overflow: hidden;
}
.chart-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
}
.chart-title { font-weight: 600; font-size: 14px; }
.chart-types { display: flex; flex-wrap: wrap; gap: 2px; background: var(--surface-2); border-radius: 8px; padding: 2px; }
.chart-types:empty { display: none; }
.chart-types button {
  border: 0; background: none; border-radius: 6px; padding: 3px 9px; font-size: 12px; color: var(--muted);
}
.chart-types button:hover { color: var(--text); }
.chart-types button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, .08); }
.chart-box { padding: 14px 14px 8px; }
.chart-canvas { position: relative; width: 100%; }
.chart-foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 14px; font-size: 12.5px; color: var(--muted); border-top: 1px solid var(--border);
}
.followups:empty { display: none; }
.followups { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 8px; }
.viz-offer {
  display: inline-flex; align-items: center; gap: 8px;
  border: 1px solid color-mix(in srgb, var(--accent) 40%, var(--border)); border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 7%, var(--surface)); color: var(--accent);
  padding: 7px 14px; font-size: 13.5px; font-weight: 500;
  transition: background .15s, transform .15s;
}
.viz-offer:hover { background: color-mix(in srgb, var(--accent) 14%, var(--surface)); transform: translateY(-1px); }

/* Composer */
.composer-wrap { flex: none; padding: 8px 16px 12px; background: var(--bg); }
.composer {
  max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 10px 10px 10px 16px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: inherit; font-size: 15.5px; max-height: 200px; padding: 6px 0;
}
#sendBtn {
  flex: none; width: 36px; height: 36px; border-radius: 10px; border: 0;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
#sendBtn:hover:not(:disabled) { background: var(--accent-hover); }
#sendBtn:disabled { opacity: .35; cursor: default; }
.hint { text-align: center; color: var(--muted); font-size: 12px; margin: 8px 0 0; }

/* Narrow screens: sidebar becomes an overlay drawer */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 20;
    box-shadow: var(--shadow); margin-left: 0; transform: translateX(0); transition: transform .2s ease;
  }
  .app.sidebar-closed .sidebar { margin-left: 0; transform: translateX(-100%); }
}

@media (max-width: 600px) {
  .topbar { padding: 10px 12px; }
  .msg-user { max-width: 92%; }
  .avatar { display: none; }
  .answer { font-size: 16px; }
}
