:root {
  --bg: #f7f8f7;
  --surface: #ffffff;
  --surface-2: #eaf1ec;
  --border: #dde4de;
  --text: #17181a;
  --text-dim: #5c615d;
  --accent: #007953;
  --accent-strong: #00664b;
  --accent-soft: rgba(0, 121, 83, 0.1);
  --magenta: #a41a76;
  --magenta-soft: rgba(164, 26, 118, 0.1);
  --warn: #9a6b0a;
  --danger: #b5382b;
  --danger-soft: rgba(181, 56, 43, 0.1);
  --radius: 6px;
  --sans: "Montserrat", ui-sans-serif, "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", "JetBrains Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1210;
    --surface: #121c18;
    --surface-2: #17231e;
    --border: #23342b;
    --text: #eef3ef;
    --text-dim: #9bb0a3;
    --accent: #1fae82;
    --accent-strong: #4bd6a8;
    --accent-soft: rgba(31, 174, 130, 0.16);
    --magenta: #e0499d;
    --magenta-soft: rgba(224, 73, 157, 0.16);
    --warn: #e3b341;
    --danger: #e2685c;
    --danger-soft: rgba(226, 104, 92, 0.12);
  }
}

:root[data-theme="dark"] {
  --bg: #0c1210;
  --surface: #121c18;
  --surface-2: #17231e;
  --border: #23342b;
  --text: #eef3ef;
  --text-dim: #9bb0a3;
  --accent: #1fae82;
  --accent-strong: #4bd6a8;
  --accent-soft: rgba(31, 174, 130, 0.16);
  --magenta: #e0499d;
  --magenta-soft: rgba(224, 73, 157, 0.16);
  --warn: #e3b341;
  --danger: #e2685c;
  --danger-soft: rgba(226, 104, 92, 0.12);
}

:root[data-theme="light"] {
  --bg: #f7f8f7;
  --surface: #ffffff;
  --surface-2: #eaf1ec;
  --border: #dde4de;
  --text: #17181a;
  --text-dim: #5c615d;
  --accent: #007953;
  --accent-strong: #00664b;
  --accent-soft: rgba(0, 121, 83, 0.1);
  --magenta: #a41a76;
  --magenta-soft: rgba(164, 26, 118, 0.1);
  --warn: #9a6b0a;
  --danger: #b5382b;
  --danger-soft: rgba(181, 56, 43, 0.1);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 14px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 12px;
  border-bottom: 1px solid var(--border);
}

.brand-mark {
  position: relative;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
}

.brand-mark span {
  position: absolute;
  width: 19px;
  height: 19px;
  border-radius: 50% 50% 50% 4px;
}

.brand-mark .petal-magenta {
  background: var(--magenta);
  top: 0;
  left: 3px;
  transform: rotate(-45deg);
}

.brand-mark .petal-green {
  background: var(--accent);
  bottom: 0;
  right: 3px;
  transform: rotate(135deg);
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
  min-width: 0;
}

.brand-text strong {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-text span {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
}

.new-thread-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.new-thread-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.new-thread-btn .plus {
  color: var(--accent);
  font-weight: 700;
  font-size: 15px;
}

.thread-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0 -4px;
  padding: 0 4px;
}

.thread-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid transparent;
}

.thread-item:hover {
  background: var(--surface-2);
  color: var(--text);
}

.thread-item.active {
  background: var(--accent-soft);
  color: var(--text);
  border-color: var(--accent);
}

.thread-item .thread-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-item .thread-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
  flex-shrink: 0;
}

.thread-item:hover .thread-delete {
  opacity: 1;
}

.thread-item .thread-delete:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 6px 2px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  flex-shrink: 0;
}

.status-dot.ok { background: var(--accent); }
.status-dot.err { background: var(--danger); }

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 6px 2px;
  font-size: 12px;
}

.sidebar-user-avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-user-email {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-dim);
}

.sidebar-user-logout {
  color: var(--text-dim);
  text-decoration: none;
  font-weight: 600;
  flex-shrink: 0;
}

.sidebar-user-logout:hover {
  color: var(--accent);
}

/* ---------- Chat column ---------- */

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
}

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

.chat-header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.chat-header p {
  margin: 3px 0 0;
  font-size: 12.5px;
  color: var(--text-dim);
}

.model-badge[hidden] {
  display: none;
}

.model-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.model-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px 12px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.empty-state {
  max-width: 620px;
  margin: 8vh auto 0;
  text-align: center;
}

.empty-state h2 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-wrap: balance;
  margin: 0 0 8px;
}

.empty-state p {
  color: var(--text-dim);
  font-size: 14px;
  margin: 0 0 22px;
}

.examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.example-chip {
  font-family: inherit;
  font-size: 13px;
  padding: 9px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.example-chip:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---------- Messages ---------- */

.msg {
  display: flex;
  gap: 12px;
  max-width: 840px;
}

.msg-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-user .msg-bubble {
  background: var(--accent);
  color: #ffffff;
  padding: 10px 14px;
  border-radius: 12px 12px 2px 12px;
  font-size: 14.5px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.msg-assistant {
  align-self: flex-start;
  width: 100%;
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
}

.msg-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-text {
  font-size: 14.5px;
  line-height: 1.6;
  white-space: pre-wrap;
}

.msg-text.pending {
  color: var(--text-dim);
}

.msg-text.pending::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 4px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 1; }
}

.sql-block {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  overflow: hidden;
}

.sql-block summary {
  padding: 8px 12px;
  font-size: 12.5px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

.sql-block[open] summary {
  border-bottom: 1px solid var(--border);
}

.sql-block pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
}

.sql-block code {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--accent-strong);
  white-space: pre;
}

.chart-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fcfcfb;
  padding: 6px;
}

.chart-img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 4px;
}

.result-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.result-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}

.download-btn {
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.download-btn:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.table-scroll {
  overflow-x: auto;
  max-height: 360px;
  overflow-y: auto;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 12.5px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
}

tbody td {
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: var(--surface-2);
}

.msg-error {
  border: 1px solid var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 13px;
  font-family: var(--mono);
  white-space: pre-wrap;
}

/* ---------- Composer ---------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 28px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 11px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.5;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.composer button[type="submit"] {
  width: 38px;
  height: 38px;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: #ffffff;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
}

.composer button[type="submit"]:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- Login ---------- */

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px 32px 30px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.brand-mark-lg {
  width: 48px;
  height: 48px;
  margin-bottom: 14px;
}

.brand-mark-lg span {
  width: 30px;
  height: 30px;
}

.login-card h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.login-subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-dim);
}

.login-copy {
  margin: 18px 0 22px;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text-dim);
  text-wrap: balance;
}

.login-error {
  width: 100%;
  border: 1px solid var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 12.5px;
  line-height: 1.5;
  margin-bottom: 18px;
}

.google-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 11px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.google-btn:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.login-footnote {
  margin: 18px 0 0;
  font-size: 11.5px;
  color: var(--text-dim);
}

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

@media (prefers-reduced-motion: reduce) {
  .status-dot, .msg-text.pending::after {
    animation: none;
  }
}
