/* ── Reset & tokens ─────────────────────────────────────────────── */
:root {
  --bg:            #ffffff;
  --surface:       #f7f7f8;
  --surface-2:     #ececec;
  --border:        #e5e5e5;
  --text:          #0d0d0d;
  --text-muted:    #6b6b6b;
  --accent:        #111111;
  --accent-hover:  #333333;
  --user-bubble:   #111111;
  --user-text:     #ffffff;
  --bot-bubble:    #f4f4f5;
  --bot-text:      #0d0d0d;
  --radius-msg:    18px;
  --sidebar-w:     240px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
}

/* ── Layout ─────────────────────────────────────────────────────── */
.chat-layout {
  display: flex;
  height: 100dvh;
}

/* ── Sidebar ─────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 16px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.logo {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.3px;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  transition: background 0.15s;
  white-space: nowrap;
}
.new-chat-btn:hover { background: var(--surface-2); }

.sidebar-hint {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ── Conversation list ───────────────────────────────────────────── */
.conv-list {
  flex: 1;
  overflow-y: auto;
  margin: 4px 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 0;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 8px;
  padding: 0 6px;
  cursor: pointer;
  min-height: 32px;
  transition: background 0.12s;
}

.conv-item:hover {
  background: var(--surface);
}

.conv-item.active {
  background: var(--surface-2);
}

.conv-title {
  flex: 1;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 6px 2px;
}

.conv-delete {
  flex-shrink: 0;
  opacity: 0;
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, opacity 0.12s;
}

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

.conv-delete:hover {
  background: var(--surface-2);
  color: var(--text);
}

.conv-loading {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  padding: 24px;
}

/* ── Model selector ─────────────────────────────────────────────── */
.model-selector {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.model-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.model-select {
  width: 100%;
  padding: 7px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%236b6b6b' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
}
.model-select:hover { border-color: #aaa; }
.model-select:focus { border-color: var(--accent); }

.model-select optgroup {
  font-weight: 600;
  font-size: 11px;
  color: var(--text-muted);
}
.model-select option {
  font-weight: 400;
  font-size: 12px;
  color: var(--text);
  padding: 4px 0;
}

/* ── Chat main ───────────────────────────────────────────────────── */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Message list ────────────────────────────────────────────────── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 32px 0 16px;
  scroll-behavior: smooth;
}

/* ── Welcome screen ──────────────────────────────────────────────── */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 60px 24px 24px;
  gap: 12px;
}

.welcome-icon { font-size: 40px; line-height: 1; }

.welcome h2 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.4px;
}

.welcome p {
  color: var(--text-muted);
  font-size: 14px;
  max-width: 400px;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

.suggestion-chip {
  padding: 8px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}
.suggestion-chip:hover {
  background: var(--surface);
  border-color: var(--accent);
}

/* ── Individual message rows ─────────────────────────────────────── */
.msg-row {
  display: flex;
  padding: 6px 24px;
  max-width: 780px;
  margin: 0 auto;
  width: 100%;
}

.msg-row.user  { justify-content: flex-end; }
.msg-row.bot   { justify-content: flex-start; flex-direction: column; align-items: flex-start; }

.bubble {
  max-width: 72%;
  padding: 12px 16px;
  border-radius: var(--radius-msg);
  line-height: 1.6;
  font-size: 14px;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg-row.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.msg-row.bot .bubble {
  background: var(--bot-bubble);
  color: var(--bot-text);
  border-bottom-left-radius: 4px;
}

/* ── Markdown content in bot bubbles ─────────────────────────────── */
.bubble.markdown-body {
  white-space: normal;
}
.bubble.markdown-body p { margin: 0 0 8px; }
.bubble.markdown-body p:last-child { margin-bottom: 0; }
.bubble.markdown-body ul,
.bubble.markdown-body ol {
  margin: 4px 0 8px 18px;
  padding: 0;
}
.bubble.markdown-body li { margin-bottom: 2px; }
.bubble.markdown-body h1,
.bubble.markdown-body h2,
.bubble.markdown-body h3 {
  font-size: 14px;
  font-weight: 700;
  margin: 10px 0 4px;
}
.bubble.markdown-body h1 { font-size: 16px; }
.bubble.markdown-body h2 { font-size: 15px; }
.bubble.markdown-body code {
  background: rgba(0,0,0,0.06);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12.5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
.bubble.markdown-body pre {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 6px 0;
  font-size: 12px;
}
.bubble.markdown-body pre code {
  background: none;
  padding: 0;
  color: inherit;
}
.bubble.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13px;
}
.bubble.markdown-body th,
.bubble.markdown-body td {
  border: 1px solid var(--border);
  padding: 5px 8px;
  text-align: left;
}
.bubble.markdown-body th {
  background: var(--surface);
  font-weight: 600;
}
.bubble.markdown-body strong { font-weight: 700; }
.bubble.markdown-body em { font-style: italic; }
.bubble.markdown-body blockquote {
  border-left: 3px solid var(--border);
  margin: 6px 0;
  padding: 4px 12px;
  color: var(--text-muted);
}
.bubble.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}

/* ── Message action bar (feedback + copy + regenerate) ──────────── */
.msg-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: 6px;
  padding: 0 2px;
}

.msg-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.msg-action-btn:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}
.msg-action-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.msg-action-btn.active {
  color: var(--accent);
  opacity: 1;
}
.feedback-btn.active[data-direction="up"] { color: #27ae60; }
.feedback-btn.active[data-direction="down"] { color: #c0392b; }

/* Thinking dots */
.thinking {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 14px 18px;
}
.thinking span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: 0.2s; }
.thinking span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%            { transform: translateY(-6px); opacity: 1; }
}

/* ── Source description + SQL panel (below bot bubble) ───────────── */
.msg-source {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
  padding: 0 2px;
  font-style: italic;
}

/* ── Token usage line ────────────────────────────────────────────── */
.msg-usage {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text);
  margin-top: 6px;
  padding: 4px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-weight: 500;
}

.msg-usage::before {
  content: "⚡";
  font-size: 11px;
}

/* ── Follow-up suggestion chips (below bot response) ─────────────── */
.follow-ups {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.follow-up-chip {
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-muted);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  text-align: left;
}

.follow-up-chip:hover {
  background: var(--surface);
  border-color: var(--accent);
  color: var(--text);
}

.sql-details {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  font-size: 12px;
  width: 100%;
}

.sql-details summary {
  padding: 7px 12px;
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
  font-style: normal;
  color: var(--text-muted);
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
.sql-details summary::-webkit-details-marker { display: none; }
.sql-details summary::before {
  content: "▶";
  font-size: 9px;
  transition: transform 0.2s;
  flex-shrink: 0;
}
.sql-details[open] summary::before { transform: rotate(90deg); }

/* Push action buttons to the right */
.sql-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}

.sql-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.sql-action-btn:hover {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--text-muted);
}
.sql-action-link { cursor: pointer; }

.sql-pre {
  margin: 0;
  padding: 12px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
  background: #fafafa;
  color: #333;
  max-height: 300px;
  overflow-y: auto;
}

/* ── Composer ────────────────────────────────────────────────────── */
.composer-wrap {
  padding: 12px 24px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  transition: border-color 0.15s;
}
.composer:focus-within { border-color: #aaa; }

.composer-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text);
}
.composer-input::placeholder { color: var(--text-muted); }

.send-btn {
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.composer-hint {
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
  margin-top: 8px;
}

/* ── Scrollbar polish ────────────────────────────────────────────── */
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 3px; }

/* ── Mobile model selector (shown inside composer on narrow screens) */
.mobile-model-wrap {
  display: none;
  padding: 0 24px 4px;
}
.mobile-model-wrap .model-select {
  width: auto;
  max-width: 200px;
}

/* ── Mobile top nav (hidden on desktop) ──────────────────────────── */
.mobile-nav {
  display: none;
}

/* ── Responsive: hide sidebar, show mobile nav on narrow screens ─── */
@media (max-width: 600px) {
  .chat-layout { flex-direction: column; }
  .sidebar { display: none; }
  .mobile-model-wrap { display: block; }
  .mobile-nav {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
    overflow-x: auto;
  }
  .mobile-nav .nav-item {
    flex: 0 0 auto;
    padding: 6px 12px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--bg);
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
  }
  .mobile-nav .nav-item.active {
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
  }
  /* Let the active main fill remaining height in column layout */
  .chat-main, .schedules-main { min-height: 0; flex: 1; }
}

/* ── Sidebar nav (chat / schedules tabs) ───────────────────────────── */
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.12s;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--surface-2); font-weight: 600; }

/* ── Schedules view ─────────────────────────────────────────────── */
.schedules-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.schedules-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--border);
}
.schedules-header h1 {
  font-size: 20px;
  font-weight: 600;
}
.schedules-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.schedules-empty {
  color: var(--text-muted);
  text-align: center;
  padding: 40px 0;
}
.schedules-empty.error { color: #c0392b; }

.schedule-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.schedule-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.schedule-card-header h3 {
  font-size: 15px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.schedule-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 3px;
}
.schedule-considerations {
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  padding: 8px 10px;
  border-radius: 8px;
  font-style: italic;
}
.schedule-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  transition: background 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.danger:hover { background: #fdecea; color: #c0392b; }

.badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-weight: 500;
}
.badge.muted { background: var(--surface); }

.schedule-last-run {
  display: flex;
  gap: 10px;
  font-size: 12px;
  color: var(--text-muted);
  align-items: center;
  flex-wrap: wrap;
}
.last-run-status {
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 500;
}
.status-ok { background: #e8f5e9; color: #2e7d32; }
.status-error { background: #fdecea; color: #c0392b; }
.status-none, .status-null { background: var(--surface-2); color: var(--text-muted); }
.last-run-tokens {
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
}
.schedule-summary {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  font-size: 13px;
}
.schedule-summary summary {
  cursor: pointer;
  color: var(--text-muted);
  user-select: none;
}
.schedule-summary[open] summary { color: var(--text); }
.schedule-summary .markdown-body {
  margin-top: 8px;
  padding: 12px;
  background: var(--surface);
  border-radius: 8px;
}
.schedule-error {
  font-size: 12px;
  color: #c0392b;
  padding: 6px 10px;
  background: #fdecea;
  border-radius: 6px;
}

/* ── Buttons ───────────────────────────────────────────────────── */
.primary-btn {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.12s;
}
.primary-btn:hover { background: var(--accent-hover); }
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
}
.ghost-btn:hover { background: var(--surface-2); }

/* ── Modal ─────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 50;
  align-items: center;
  justify-content: center;
}
.modal {
  background: var(--bg);
  width: min(560px, 92vw);
  max-height: 90vh;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 17px; font-weight: 600; }
.modal-close {
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 4px;
}
.modal-close:hover { color: var(--text); }
.modal-body {
  padding: 18px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-footer {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
  padding-top: 14px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.form-row > span {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.form-row input[type="text"],
.form-row input[type="number"],
.form-row textarea,
.form-row select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
}
.form-row textarea { resize: vertical; }
.form-row small {
  font-size: 11px;
  color: var(--text-muted);
}
.form-row-inline {
  flex-direction: row;
  gap: 24px;
  align-items: flex-start;
}
.form-row-inline > label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}
.form-row-inline span {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.time-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}
.time-inputs input { width: 60px; }
.dow-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.dow-checks label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 12px;
  background: var(--surface);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.form-row-toggle {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
.form-row-toggle span {
  text-transform: none;
  letter-spacing: 0;
  font-weight: 500;
  color: var(--text);
  font-size: 14px;
}
