/* WareQuery web styles — Linear-minimal three-pane shell.
 *
 * Section index:
 *   1. Reset & base
 *   2. Layout (app-grid, topbar, panes)
 *   3. Sidebar
 *   4. Context panel
 *   5. Welcome card
 *   6. Message pair        (added in Task 4)
 *   7. Composer            (added in Task 5)
 *   8. Modals              (added in Task 6)
 *   9. Cmd+K palette       (added in Task 9)
 *  10. Toast               (added in Task 11)
 *  11. Responsive          (added in Task 12)
 */

/* ---------- 1. Reset & base ---------- */

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

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  background: transparent;
  border: 0;
  padding: 0;
}

button:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

a { color: var(--color-accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code, kbd { font-family: var(--font-mono); font-size: 0.92em; }

kbd {
  display: inline-block;
  padding: 1px 5px;
  border: 1px solid var(--color-border);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  background: var(--color-bg-subtle);
  color: var(--color-text-muted);
  font-size: 11px;
  line-height: 1;
}

.inline { display: inline; }

.btn-ghost {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-md);
  transition: background var(--duration-fast) var(--easing);
}
.btn-ghost:hover { background: var(--color-bg-muted); color: var(--color-text); }

.btn-icon {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  border-radius: var(--radius-md);
  transition: background var(--duration-fast) var(--easing);
}
.btn-icon:hover { background: var(--color-bg-muted); color: var(--color-text); }

/* ---------- 2. Layout ---------- */

.app-shell {
  display: flex;
  flex-direction: column;
  /* Lock the shell to exactly one viewport tall and hide overflow at the
   * shell level. The internal panes (.sidebar, .context-panel, .message-list)
   * each have their own overflow-y: auto and scroll independently, which
   * keeps the left and right trays fixed while the chat scrolls. Without
   * this, a tall message list would push the grid row past 100vh and the
   * document scroller (with the trays attached) would scroll instead. */
  height: 100vh;
  overflow: hidden;
}

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: var(--space-2); }
.brand { font-weight: 600; font-size: var(--text-md); }
.brand-sep { color: var(--color-text-subtle); }
.brand-context { color: var(--color-text-muted); font-size: var(--text-sm); }

.theme-icon-light, .theme-icon-dark { display: inline-block; }
:root[data-theme="light"] .theme-icon-dark { display: none; }
:root[data-theme="dark"] .theme-icon-light { display: none; }

.app-grid {
  display: grid;
  grid-template-columns: var(--sidebar-w) 5px 1fr 5px var(--context-w);
  flex: 1;
  min-height: 0;
}

/* Resize handles between panes. 5px thin column with a wider hover hit
 * area via ::before, draggable via JS in app.js. Persists widths to
 * localStorage. */
.resize-handle {
  position: relative;
  background: transparent;
  cursor: col-resize;
  z-index: 5;
  transition: background var(--duration-fast) var(--easing);
}
.resize-handle:hover,
.resize-handle:focus-visible,
.resize-handle.is-dragging {
  background: var(--color-border-focus);
  outline: none;
}
.resize-handle::before {
  content: "";
  position: absolute;
  inset: 0 -3px;
}
body.is-resizing-pane,
body.is-resizing-pane * { cursor: col-resize !important; user-select: none; }

.main-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--color-bg);
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6) var(--space-6) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

/* ---------- 3. Sidebar ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--color-border);
  background: var(--color-bg-subtle);
  padding: var(--space-3) var(--space-2);
  overflow-y: auto;
}

.sidebar-new { margin-bottom: var(--space-3); }
.btn-newthread {
  width: 100%;
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  letter-spacing: 0.4px;
  text-align: left;
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text);
  transition: background var(--duration-fast) var(--easing);
}
.btn-newthread:hover { background: var(--color-bg-muted); }

.sidebar-section { margin-bottom: var(--space-4); }
.sidebar-section-label {
  font-size: 10px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--color-text-subtle);
  font-weight: 600;
  padding: var(--space-1) var(--space-2);
}

.thread-list, .recent-list { list-style: none; margin: 0; padding: 0; }

.thread-item, .recent-item {
  padding: var(--space-2) var(--space-2);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  display: flex;
  flex-direction: column;
  gap: 2px;
  cursor: pointer;
  transition: background var(--duration-fast) var(--easing);
}
.thread-item:hover, .recent-item:hover { background: var(--color-bg-muted); }
.thread-item.is-active {
  background: var(--color-accent-bg);
  color: var(--color-accent-text);
  font-weight: 500;
}
.thread-title { display: block; }
.thread-meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--color-text-subtle);
}

.recent-link {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.recent-link:hover { color: var(--color-text); text-decoration: none; }

.sidebar-footer {
  margin-top: auto;
  padding: var(--space-3) var(--space-2);
  border-top: 1px solid var(--color-border);
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
}
.kbd-hint { display: inline-flex; align-items: center; gap: var(--space-1); }

/* D additions: thread search, thread list, hover affordances. */

.thread-search {
  width: 100%;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  color: var(--color-text);
  margin: 0 0 var(--space-3);
  transition: border-color var(--duration-fast) var(--easing);
}
.thread-search:focus-visible {
  outline: none;
  border-color: var(--color-border-focus);
}

.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.thread-item {
  position: relative;
  display: flex;
  align-items: center;
  padding: var(--space-2) var(--space-2);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  cursor: default;
  transition: background var(--duration-fast) var(--easing);
}
.thread-item:hover { background: var(--color-bg-muted); }
.thread-item.is-active {
  background: var(--color-accent-bg);
  color: var(--color-accent-text);
  font-weight: 500;
}

.thread-link {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  color: inherit;
  text-decoration: none;
  padding-right: var(--space-2);
}
.thread-link:hover { text-decoration: none; color: inherit; }

.thread-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: var(--text-sm);
}
.thread-meta {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--color-text-subtle);
}
.thread-item.is-active .thread-meta { color: var(--color-accent-text); opacity: 0.8; }

.thread-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity var(--duration-fast) var(--easing);
}
.thread-item:hover .thread-actions { opacity: 1; }

.thread-edit-btn,
.thread-delete-btn {
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--color-text-muted);
  border-radius: var(--radius-sm);
  background: transparent;
}
.thread-edit-btn:hover,
.thread-delete-btn:hover {
  background: var(--color-bg-emphasis);
  color: var(--color-text);
}
.thread-delete-btn:hover { color: var(--color-danger); }

.thread-rename-form,
.thread-delete-confirm { display: none; width: 100%; gap: var(--space-1); align-items: center; }

.thread-item.is-renaming .thread-rename-form {
  display: flex;
}
.thread-item.is-renaming .thread-link,
.thread-item.is-renaming .thread-actions { display: none; }

.thread-rename-input {
  flex: 1;
  font-size: var(--text-sm);
  padding: 2px var(--space-2);
  border: 1px solid var(--color-border-focus);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
}

.thread-rename-save,
.thread-rename-cancel,
.thread-delete-yes,
.thread-delete-no {
  font-size: var(--text-xs);
  padding: 2px var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
}

.thread-item.is-confirming-delete .thread-delete-confirm { display: flex; align-items: center; gap: var(--space-1); font-size: var(--text-xs); }
.thread-item.is-confirming-delete .thread-link,
.thread-item.is-confirming-delete .thread-actions { display: none; }

.thread-list-empty {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  font-style: italic;
  padding: var(--space-3) var(--space-2);
  margin: 0;
}
.thread-list-no-matches[hidden] { display: none; }

.thread-rename-error {
  margin: var(--space-1) 0 0;
  font-size: 10px;
  color: var(--color-danger);
}

/* ---------- 4. Context panel ---------- */

.context-panel {
  border-left: 1px solid var(--color-border);
  background: var(--color-bg-subtle);
  padding: var(--space-4);
  overflow-y: auto;
}

.context-section { margin-bottom: var(--space-5); }
.context-section-label {
  font-size: 10px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--color-text-subtle);
  font-weight: 600;
  margin-bottom: var(--space-2);
}
.context-section-body {
  font-size: var(--text-sm);
  color: var(--color-text);
  line-height: var(--leading-snug);
}
.context-list { list-style: none; margin: 0; padding: 0; font-size: var(--text-sm); }
.context-list li { padding: 2px 0; color: var(--color-text-muted); }
.context-list code {
  background: var(--color-bg-muted);
  padding: 1px 4px;
  border-radius: var(--radius-sm);
}

.context-empty {
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
  font-style: italic;
  padding: var(--space-3) 0;
  line-height: var(--leading-snug);
}

/* ---------- 5. Welcome card ---------- */

.welcome-card {
  padding: var(--space-12) var(--space-4);
  text-align: left;
  max-width: var(--content-max);
}
.welcome-eyebrow {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--color-text-subtle);
  margin-bottom: var(--space-2);
}
.welcome-title {
  font-size: var(--text-2xl);
  font-weight: 600;
  margin: 0 0 var(--space-3);
  line-height: var(--leading-tight);
}
.welcome-body {
  font-size: var(--text-md);
  color: var(--color-text-muted);
  margin: 0;
}
.welcome-body em {
  font-style: normal;
  font-family: var(--font-mono);
  font-size: 0.92em;
  background: var(--color-bg-muted);
  padding: 1px 6px;
  border-radius: var(--radius-sm);
}

.soft-banner {
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  max-width: var(--content-max);
}

/* Auth card (login page) */

.auth-shell {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: var(--space-6);
  background: var(--color-bg-subtle);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  box-shadow: var(--shadow-md);
}

.auth-eyebrow {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--color-text-subtle);
  margin-bottom: var(--space-2);
}
.auth-title {
  font-size: var(--text-xl);
  font-weight: 600;
  margin: 0 0 var(--space-2);
}
.auth-subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-5);
  line-height: var(--leading-snug);
}

.auth-error {
  background: rgba(239, 68, 68, 0.08);
  color: var(--color-danger);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  margin-bottom: var(--space-4);
}

.auth-form { display: flex; flex-direction: column; gap: var(--space-3); }
.auth-field { display: flex; flex-direction: column; gap: var(--space-1); }
.auth-label {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--color-text-muted);
  font-weight: 600;
}
.auth-input {
  font-family: var(--font-sans);
  font-size: var(--text-md);
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  color: var(--color-text);
  transition: border-color var(--duration-fast) var(--easing),
              box-shadow var(--duration-fast) var(--easing);
}
.auth-input:focus-visible {
  outline: none;
  border-color: var(--color-border-focus);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}
.auth-submit { width: 100%; margin-top: var(--space-2); }

/* ---------- 6. Message pair ---------- */

.turn-pair {
  max-width: var(--content-max);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.turn { padding: 0; }

.turn-meta {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  margin-bottom: var(--space-1);
}
.turn-meta .speaker {
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-size: 10px;
}
.turn-meta .marker {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--color-text-subtle);
}

.turn-user .turn-body {
  background: var(--color-bg-muted);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-md);
  line-height: var(--leading-snug);
  display: inline-block;
  max-width: 100%;
}

.turn-bot .turn-body {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  font-size: var(--text-md);
  line-height: var(--leading-relaxed);
  position: relative;
}
.turn-bot .turn-answer {
  font-variant-numeric: tabular-nums;
}

.copy-btn {
  font-size: var(--text-xs);
  padding: 2px var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background var(--duration-fast) var(--easing);
}
.copy-btn:hover { background: var(--color-bg-muted); color: var(--color-text); }
.copy-btn.is-copied { background: var(--color-success); color: var(--color-text-inverse); border-color: transparent; }
.copy-btn-inline {
  position: absolute;
  top: var(--space-2);
  right: var(--space-2);
  opacity: 0;
  transition: opacity var(--duration-fast) var(--easing);
}
.turn-bot .turn-body:hover .copy-btn-inline { opacity: 1; }

.turn-meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  margin-top: var(--space-2);
}

.meta-pill {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.3px;
  padding: 2px var(--space-2);
  background: var(--color-bg-muted);
  color: var(--color-text-muted);
  border-radius: var(--radius-sm);
}
.meta-pill-plain {
  font-family: var(--font-sans);
  background: transparent;
  padding-left: 0;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}
.action-btn {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  letter-spacing: 0.3px;
  padding: var(--space-1) var(--space-3);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text);
  transition: background var(--duration-fast) var(--easing);
}
.action-btn:hover { background: var(--color-bg-muted); border-color: var(--color-border-strong); }
.action-btn-loading .action-btn-busy { display: none; }
.action-btn-loading[disabled] .action-btn-label { display: none; }
.action-btn-loading[disabled] .action-btn-busy { display: inline; }

/* Inline result table */
.inline-result-table {
  margin-top: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-size: var(--text-sm);
}
.inline-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  background: var(--color-bg-subtle);
  border-bottom: 1px solid var(--color-border);
}
.inline-result-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--color-text-subtle);
  font-weight: 600;
}
.inline-result-table-wrap { overflow-x: auto; }
.trace-table {
  border-collapse: collapse;
  width: 100%;
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}
.trace-table th, .trace-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  vertical-align: top;
}
.trace-table th {
  background: var(--color-bg-subtle);
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--color-text-muted);
}
.trace-table td { color: var(--color-text); }
.trace-table tbody tr:last-child td { border-bottom: 0; }

.inline-result-more {
  padding: var(--space-2) var(--space-3);
  margin: 0;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: var(--color-bg-subtle);
}
.link-btn {
  background: none;
  color: var(--color-accent);
  text-decoration: underline;
  font: inherit;
}
.link-btn:hover { color: var(--color-accent-text); }

.chart-block {
  height: 280px;
  margin-top: var(--space-4);
  padding: var(--space-3);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

/* ---------- 7. Composer ---------- */

.composer {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg);
  position: sticky;
  bottom: 0;
}

.composer textarea {
  width: 100%;
  resize: none;
  font-family: var(--font-sans);
  font-size: var(--text-md);
  line-height: var(--leading-snug);
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  min-height: 56px;
  max-height: 180px;       /* 6 lines-ish; overflow scrolls internally */
  overflow-y: auto;
  transition: border-color var(--duration-fast) var(--easing),
              box-shadow var(--duration-fast) var(--easing);
}
.composer textarea:focus-visible {
  outline: none;
  border-color: var(--color-border-focus);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}
.composer-hint {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.btn-send {
  font-family: var(--font-sans);
  font-weight: 500;
  font-size: var(--text-sm);
  padding: var(--space-2) var(--space-5);
  background: var(--color-text);
  color: var(--color-text-inverse);
  border: 1px solid var(--color-text);
  border-radius: var(--radius-md);
  transition: opacity var(--duration-fast) var(--easing);
}
.btn-send:hover { opacity: 0.9; }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* Inline spinner state for buttons that show loading after a form
 * submit. The login button uses this so users see immediate feedback
 * during the (slow) bcrypt + Postgres round trip on first auth. */
.btn-with-spinner {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
}
.btn-with-spinner .btn-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  display: none;
  animation: spinner-rotate 0.7s linear infinite;
  flex-shrink: 0;
}
.btn-with-spinner.is-loading .btn-spinner { display: inline-block; }

/* Thinking placeholder spinner shown in the agent's pending-response turn
 * while the LLM round-trip is in flight. Pairs with the .thinking class
 * applied by app.js makeTurn() when extraClass === "thinking". */
.turn-body.thinking {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-subtle);
}
.thinking-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: spinner-rotate 0.7s linear infinite;
  flex-shrink: 0;
}
.thinking-label {
  display: inline-block;
}
.thinking-label::after {
  content: "";
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: thinking-dots 1.2s steps(4, end) infinite;
}
@keyframes thinking-dots {
  0%   { content: ""; }
  25%  { content: "."; }
  50%  { content: ".."; }
  75%  { content: "..."; }
  100% { content: ""; }
}
.btn-with-spinner.is-loading .btn-label { opacity: 0.7; }
.btn-with-spinner.is-loading {
  cursor: progress;
  opacity: 0.85;
}
@keyframes spinner-rotate {
  to { transform: rotate(360deg); }
}

/* ---------- 8. Modals ---------- */

dialog.modal {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-bg);
  color: var(--color-text);
  padding: 0;
  margin: auto;
  width: min(720px, 90vw);
  max-height: 85vh;
  overflow: hidden;
  box-shadow: var(--shadow-modal);
}
dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--color-border);
}
.modal-title {
  margin: 0;
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--color-text);
}
.modal-close {
  font-size: var(--text-lg);
  color: var(--color-text-muted);
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
}
.modal-close:hover { background: var(--color-bg-muted); color: var(--color-text); }

.modal-body {
  padding: var(--space-5);
  overflow-y: auto;
  max-height: calc(85vh - 56px);
}

.modal-section { margin-bottom: var(--space-5); }
.modal-section:last-child { margin-bottom: 0; }
.modal-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--color-text-subtle);
  font-weight: 600;
  margin-bottom: var(--space-2);
}

.sql-block {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  line-height: var(--leading-snug);
  background: var(--color-bg-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  white-space: pre;
  overflow-x: auto;
  color: var(--color-text);
}
.sql-block .sql-comment { color: var(--color-text-subtle); }
.sql-block .sql-keyword { color: var(--color-accent-text); font-weight: 500; }

/* ---------- 9. Cmd+K palette ---------- */

dialog.cmdk {
  width: min(480px, 90vw);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-bg);
  color: var(--color-text);
  padding: 0;
  margin-top: 12vh;
  margin-left: auto;
  margin-right: auto;
  box-shadow: var(--shadow-modal);
  overflow: hidden;
}
dialog.cmdk::backdrop {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.cmdk-input-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  border-bottom: 1px solid var(--color-border);
}
.cmdk-input {
  flex: 1;
  font-family: var(--font-sans);
  font-size: var(--text-md);
  background: transparent;
  border: 0;
  color: var(--color-text);
  outline: none;
}
.cmdk-esc {
  font-size: 10px;
  color: var(--color-text-subtle);
}

.cmdk-list { list-style: none; margin: 0; padding: var(--space-1); max-height: 320px; overflow-y: auto; }
.cmdk-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  cursor: pointer;
  color: var(--color-text);
}
.cmdk-item[hidden] { display: none; }
.cmdk-item.is-disabled { color: var(--color-text-subtle); cursor: not-allowed; opacity: 0.5; }
.cmdk-item.is-active,
.cmdk-item:hover:not(.is-disabled) { background: var(--color-bg-muted); }
.cmdk-item-hint { font-family: var(--font-mono); font-size: 10px; color: var(--color-text-subtle); }

/* ---------- 10. Toast ---------- */

#toast-root {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
  z-index: 100;
}

.toast {
  background: var(--color-text);
  color: var(--color-text-inverse);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-md);
  pointer-events: auto;
  animation: toast-in 180ms var(--easing) forwards,
             toast-out 200ms var(--easing) 2300ms forwards;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(4px); }
}

/* ---------- 11. Responsive ---------- */

@media (max-width: 1024px) {
  .app-grid {
    grid-template-columns: var(--sidebar-w) 5px 1fr;
  }
  .context-panel { display: none; }
  .resize-handle-context { display: none; }
}

@media (max-width: 768px) {
  .app-grid {
    grid-template-columns: 1fr;
  }
  .resize-handle { display: none; }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    bottom: 0;
    left: 0;
    width: 80vw;
    max-width: 320px;
    z-index: 25;
    transform: translateX(-100%);
    transition: transform var(--duration-normal) var(--easing);
    box-shadow: var(--shadow-lg);
  }
  body[data-sidebar-open="1"] .sidebar { transform: translateX(0); }
  body[data-sidebar-open="1"]::before {
    content: "";
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 20;
  }
  .message-list { padding: var(--space-3); }
  .composer { padding: var(--space-3); }
  .welcome-card { padding: var(--space-6) var(--space-3); }
  .welcome-title { font-size: var(--text-xl); }
}

@media (min-width: 769px) {
  .sidebar-toggle { display: none; }
}

/* ---------- User menu (topbar dropdown) ---------- */

.user-menu {
  position: relative;
  display: inline-block;
}
.user-menu-trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
  list-style: none;
  font-size: var(--text-sm);
  color: var(--color-text);
  transition: background var(--duration-fast) var(--easing);
}
.user-menu-trigger::-webkit-details-marker { display: none; }
.user-menu-trigger:hover { background: var(--color-bg-muted); }

.user-menu-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-accent-bg);
  color: var(--color-accent-text);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
}
.user-menu-name { font-weight: 500; }
.user-menu-caret { color: var(--color-text-subtle); font-size: 10px; }

.user-menu-panel {
  position: absolute;
  top: calc(100% + var(--space-1));
  right: 0;
  min-width: 220px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--space-1);
  z-index: 40;
}

.user-menu-header {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border);
  margin-bottom: var(--space-1);
}
.user-menu-display-name { font-weight: 600; font-size: var(--text-sm); }
.user-menu-email { font-size: var(--text-xs); color: var(--color-text-muted); }

.user-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-text);
  border-radius: var(--radius-sm);
  text-decoration: none;
  background: transparent;
  border: 0;
  cursor: pointer;
}
.user-menu-item:hover { background: var(--color-bg-muted); text-decoration: none; }
.user-menu-form { margin: 0; }
.user-menu-signout { color: var(--color-danger); }

/* ---------- Admin pages ---------- */

.admin-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.admin-main {
  max-width: 1100px;
  width: 100%;
  margin: var(--space-6) auto;
  padding: 0 var(--space-6);
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-6);
}
.admin-title {
  font-size: var(--text-2xl);
  font-weight: 600;
  margin: 0;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.admin-table th, .admin-table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}
.admin-table th {
  background: var(--color-bg-subtle);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--color-text-muted);
  font-weight: 600;
}
.admin-table tbody tr:last-child td { border-bottom: 0; }
.admin-table tbody tr.is-disabled-row td { color: var(--color-text-subtle); }

.inline-form { display: inline; margin: 0; }
.role-select {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  padding: 2px var(--space-2);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
}
