/* SPACbot — light/dark themes via CSS variables */
:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #212121;
  --sidebar-bg: #171717;
  --surface: #262626;
  --border: #3a3a3a;
  --text: #ececec;
  --muted: #b4b4b4;
  --accent: #ececec;
  --accent-hover: #ffffff;
  --accent-on: #171717;
  --user-bg: #333333;
  --user-fg: #ececec;
  --err: #ef4444;
  --ok: #22c55e;
  --btn-hover: #333333;
  --sidebar-hover: #2a2a2a;
  --sidebar-active: #2f2f2f;
  --code-bg: rgba(0, 0, 0, 0.35);
  --pre-bg: rgba(0, 0, 0, 0.4);
  --th-bg: rgba(255, 255, 255, 0.06);
  --zebra: rgba(255, 255, 255, 0.02);
  --row-hover: rgba(255, 255, 255, 0.06);
  --strong: #f5f5f5;
  --em: #d4d4d4;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  --overlay: rgba(0, 0, 0, 0.55);
  --focus-ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--border);
  --primary-btn-bg: #ececec;
  --primary-btn-fg: #171717;
  --primary-btn-hover: #ffffff;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #ffffff;
  --sidebar-bg: #f7f7f8;
  --surface: #ffffff;
  --border: #e3e3e3;
  --text: #0d0d0d;
  --muted: #6e6e80;
  --accent: #0d0d0d;
  --accent-hover: #262626;
  --accent-on: #ffffff;
  --user-bg: #ececec;
  --user-fg: #0d0d0d;
  --err: #dc2626;
  --ok: #16a34a;
  --btn-hover: #ececec;
  --sidebar-hover: #ececec;
  --sidebar-active: #e3e3e3;
  --code-bg: rgba(0, 0, 0, 0.06);
  --pre-bg: rgba(0, 0, 0, 0.04);
  --th-bg: rgba(0, 0, 0, 0.04);
  --zebra: rgba(0, 0, 0, 0.02);
  --row-hover: rgba(0, 0, 0, 0.04);
  --strong: #0d0d0d;
  --em: #3f3f46;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  --overlay: rgba(0, 0, 0, 0.35);
  --focus-ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--border);
  --primary-btn-bg: #0d0d0d;
  --primary-btn-fg: #ffffff;
  --primary-btn-hover: #262626;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

button, a {
  font: inherit;
}

button:focus-visible,
a:focus-visible,
textarea:focus-visible,
input:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 12px;
  z-index: 40;
  transition: transform 0.2s ease, width 0.2s ease;
}

.sidebar-brand h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.sidebar-powered-by {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.sidebar-powered-by span {
  font-size: 10px;
  color: var(--muted);
  line-height: 1;
}

.sidebar-brand-logo-link {
  display: inline-flex;
  align-items: center;
  line-height: 0;
  border-radius: 4px;
}

.sidebar-brand-logo-link:hover {
  opacity: 0.85;
}

.sidebar-brand-logo {
  height: 14px;
  width: auto;
  max-width: 84px;
  display: block;
  object-fit: contain;
  object-position: left center;
}

.usage-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.usage-card h2 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.usage-stats {
  display: flex;
  gap: 16px;
}

.usage-stat strong {
  display: block;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.usage-stat span {
  font-size: 11px;
  color: var(--muted);
}

.usage-progress {
  margin-top: 10px;
}

.usage-progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
}

.usage-progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.usage-progress-fill {
  height: 100%;
  background: var(--primary-btn-bg);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.usage-card.loading,
.usage-card.error,
.usage-card.logged-out {
  color: var(--muted);
  font-size: 12px;
}

/* Sidebar token usage indicator (sits above the user profile) */
.sidebar-usage[hidden] {
  display: none !important;
}

.sidebar-usage-btn {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.sidebar-usage-btn:hover {
  background: var(--sidebar-hover);
}

.sidebar-usage-metric {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.sidebar-usage-metric-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.sidebar-usage-metric-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.sidebar-usage-metric-value {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.sidebar-usage-track {
  display: block;
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.sidebar-usage-track[hidden] {
  display: none !important;
}

.sidebar-usage-fill {
  display: block;
  height: 100%;
  width: 0;
  min-width: 0;
  background: var(--primary-btn-bg);
  border-radius: 999px;
  transition: width 0.2s ease;
}

.sidebar-usage-fill.exceeded {
  background: var(--err);
}

.btn-new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
  font-weight: 600;
  cursor: pointer;
}

.btn-new-chat:hover:not([disabled]) {
  background: var(--primary-btn-hover);
}

.btn-new-chat[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.sidebar-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar-section h2 {
  margin: 0 0 6px;
  padding: 0 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.chat-history {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.chat-history li { margin: 2px 0; }

.chat-history button {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}

.chat-history button:hover {
  background: var(--sidebar-hover);
}

.chat-history button.active {
  background: var(--sidebar-active);
  font-weight: 500;
}

.chat-history .empty-history {
  padding: 8px 10px;
  font-size: 12px;
  color: var(--muted);
}

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

.sidebar-footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
  position: relative;
}

.user-menu-wrap {
  position: relative;
}

.user-menu-trigger {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.user-menu-trigger:hover {
  background: var(--sidebar-hover);
}

.user-avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
  font-size: 12px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.02em;
}

.user-avatar.guest {
  background: var(--border);
  color: var(--muted);
}

.user-menu-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.user-menu-text .user-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-menu-text .user-sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-menu-chevron {
  flex-shrink: 0;
  color: var(--muted);
}

.user-menu-popover {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(100% + 6px);
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  z-index: 50;
}

.user-menu-popover[hidden] {
  display: none !important;
}

.user-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  text-decoration: none;
  cursor: pointer;
  text-align: left;
}

.user-menu-item:hover {
  background: var(--sidebar-hover);
}

.user-menu-item svg {
  flex-shrink: 0;
  color: var(--muted);
}

.user-menu-divider {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}

.user-menu-item[hidden],
.user-menu-divider[hidden] {
  display: none !important;
}

.user-menu-popover.is-guest {
  display: none !important;
}

.user-menu-trigger.is-guest .user-menu-chevron {
  display: none;
}

.profile-panel-backdrop,
.usage-panel-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--overlay);
}

.profile-panel-backdrop[hidden],
.usage-panel-backdrop[hidden] {
  display: none !important;
}

.profile-panel,
.usage-panel {
  width: min(100%, 400px);
  height: 360px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.usage-panel-header,
.profile-panel-header {
  position: relative;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 56px 0;
  min-height: 64px;
}

.usage-panel-header h2,
.profile-panel-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}

.panel-close-x {
  position: absolute;
  top: 18px;
  right: 18px;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.panel-close-x:hover {
  background: var(--btn-hover);
  color: var(--text);
}

.usage-panel-body,
.profile-panel-body {
  flex: 1;
  min-height: 0;
  padding: 20px 28px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  overflow-y: auto;
}

.usage-panel-desc {
  margin: 0 0 20px;
  width: 100%;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.5;
  text-align: center;
}

.usage-metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: 100%;
}

.usage-metric-card {
  padding: 20px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  text-align: center;
}

.usage-metric-value {
  display: block;
  font-size: 28px;
  font-weight: 700;
  line-height: 1.1;
  color: var(--text);
  letter-spacing: -0.02em;
}

.usage-metric-label {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

.usage-panel-message {
  margin: 0;
  width: 100%;
  padding: 24px 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

.usage-progress-fill.exceeded {
  background: var(--err);
}

.usage-quota-banner {
  margin: 0 0 8px;
  padding: 10px 12px;
  border: 1px solid var(--err);
  border-radius: 10px;
  background: color-mix(in srgb, var(--err) 12%, var(--surface));
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
}

.usage-quota-banner a {
  color: inherit;
  font-weight: 600;
}

.usage-quota-banner[hidden] {
  display: none !important;
}

.usage-panel .usage-progress {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  width: 100%;
}

.profile-panel-body {
  gap: 4px;
}

.profile-avatar {
  width: 72px;
  height: 72px;
  margin: 0 0 16px;
  border-radius: 50%;
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
  font-size: 24px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.profile-name {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}

.profile-username {
  margin: 6px 0 0;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

#usage-panel-card {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.panel-close {
  display: none;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}

.sidebar-link:hover {
  background: var(--sidebar-hover);
}

/* Main area */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  padding: 2px;
  gap: 2px;
}

.theme-toggle button {
  width: 32px;
  height: 28px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.theme-toggle button.active {
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
}

.auth-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.btn-login {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
}

.btn-login[hidden] {
  display: none !important;
}

.btn-login:hover {
  background: var(--btn-hover);
}

.login-dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.55);
}

.login-dialog-backdrop[hidden] {
  display: none !important;
}

.login-dialog {
  width: min(100%, 400px);
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-dialog h2 {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.login-dialog p {
  margin: 0 0 20px;
  font-size: 14px;
  color: var(--muted);
  line-height: 1.5;
}

.login-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.login-dialog-cancel,
.login-dialog-login {
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.login-dialog-cancel {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
}

.login-dialog-cancel:hover {
  background: var(--btn-hover);
}

.login-dialog-login {
  border: none;
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
}

.login-dialog-login:hover {
  background: var(--primary-btn-hover);
}

.chat-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.scroller {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.chat-inner {
  width: 100%;
  max-width: 768px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 520px;
  padding: 24px 16px;
}

.empty-state h2 {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 8px;
  margin-top: 16px;
}

.suggestion-card {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 13px;
}

.suggestion-card:hover {
  background: var(--btn-hover);
}

/* Messages */
.msg-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-row.user {
  align-items: flex-end;
}

.msg-row.assistant {
  align-items: flex-start;
}

.msg-meta {
  font-size: 11px;
  color: var(--muted);
  padding: 0 4px;
}

.bubble.user {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  background: var(--user-bg);
  color: var(--user-fg);
  white-space: pre-wrap;
  word-wrap: break-word;
}

.assistant-content {
  max-width: 100%;
  color: var(--text);
}

.assistant-content .md { white-space: normal; }
.assistant-content .md > *:first-child { margin-top: 0; }
.assistant-content .md > *:last-child { margin-bottom: 0; }
.assistant-content .md p { margin: 0 0 10px; }
.assistant-content .md h1,
.assistant-content .md h2,
.assistant-content .md h3,
.assistant-content .md h4 {
  margin: 14px 0 8px;
  font-weight: 600;
  line-height: 1.3;
}
.assistant-content .md h1 { font-size: 20px; }
.assistant-content .md h2 { font-size: 17px; }
.assistant-content .md h3 { font-size: 15px; }
.assistant-content .md h4 { font-size: 14px; }
.assistant-content .md ul,
.assistant-content .md ol { margin: 8px 0; padding-left: 22px; }
.assistant-content .md li { margin: 4px 0; }
.assistant-content .md li > p { margin: 0; }
.assistant-content .md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 4px;
}
.assistant-content .md pre {
  margin: 10px 0;
  padding: 12px 14px;
  background: var(--pre-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}
.assistant-content .md pre code {
  background: none;
  padding: 0;
  font-size: 12.5px;
  white-space: pre;
}
.assistant-content .md a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; }
.assistant-content .md a:hover { color: var(--strong); }
.assistant-content .md hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}
.assistant-content .md strong { color: var(--strong); }
.assistant-content .md em { color: var(--em); }
.assistant-content .md blockquote {
  margin: 10px 0;
  padding: 10px 14px;
  border-left: 3px solid var(--border);
  border-radius: 0 8px 8px 0;
  color: var(--muted);
  background: var(--surface);
}

.table-wrap {
  position: relative;
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.table-brand-header {
  display: flex;
  align-items: center;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--th-bg);
}

.table-scroll {
  overflow: auto;
  max-height: 420px;
  border-radius: 10px;
}

.assistant-content .md table {
  border-collapse: separate;
  border-spacing: 0;
  margin: 0;
  font-size: 13px;
  width: 100%;
  min-width: max-content;
}

.assistant-content .md th,
.assistant-content .md td {
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}

.assistant-content .md th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--th-bg);
  backdrop-filter: blur(6px);
  font-weight: 600;
}

.assistant-content .md thead::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--surface);
  z-index: -1;
}

.assistant-content .md tbody tr:nth-child(even) td { background: var(--zebra); }
.assistant-content .md tbody tr:hover td { background: var(--row-hover); }
.assistant-content .md tbody tr:last-child td { border-bottom: none; }
.assistant-content .md td.num,
.assistant-content .md th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.table-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
  flex-wrap: wrap;
}

.table-tools-brand {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  text-decoration: none;
  color: var(--muted);
}

.table-tools-brand:hover {
  color: var(--text);
}

.table-tools-brand img {
  height: 12px;
  width: auto;
  max-width: 72px;
  opacity: 0.85;
  display: block;
}

.table-tools-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.table-tools button {
  padding: 3px 8px;
  font-size: 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.clarification-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
  margin: 12px 0 0;
  list-style: none;
  padding: 0;
}

.clarification-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 13px;
}

.clarification-card:hover {
  background: var(--btn-hover);
  border-color: var(--text);
}

.clarification-card .icon {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  color: var(--text);
}

.clarification-card .label {
  flex: 1;
  min-width: 0;
}

.clarification-card .chevron {
  flex-shrink: 0;
  color: var(--muted);
}

.info-box {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 12px 0;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 13px;
  color: var(--muted);
}

.info-box strong,
.info-box .highlight {
  color: var(--text);
}

.chart-block {
  position: relative;
  margin: 12px 0;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.chart-brand {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  text-decoration: none;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.chart-brand:hover { color: var(--text); }

.chart-brand img {
  height: 12px;
  width: auto;
  max-width: 72px;
  opacity: 0.85;
  display: block;
}

.chart-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.chart-title {
  font-weight: 600;
  font-size: 13px;
  flex: 1;
  min-width: 0;
}

.chart-actions { display: flex; gap: 4px; flex-shrink: 0; }

.chart-actions button {
  padding: 4px 8px;
  font-size: 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.chart-actions button:hover:not([disabled]) { color: var(--text); }

.chart-container {
  width: 100%;
  height: 320px;
}

.chart-block.fullscreen {
  position: fixed;
  inset: 24px;
  z-index: 60;
  margin: 0;
  overflow: auto;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
}

.chart-block.fullscreen .chart-container { height: calc(100vh - 130px); }

.chart-backdrop {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 59;
}

.chart-error {
  font-size: 12px;
  color: var(--err);
  padding: 6px 0 0;
}

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}

.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing 1.2s ease-in-out infinite;
}

.typing i:nth-child(2) { animation-delay: 0.18s; }
.typing i:nth-child(3) { animation-delay: 0.36s; }

@keyframes typing {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.msg-error {
  margin-top: 8px;
  color: var(--err);
  font-size: 13px;
}

.stopped-note {
  margin-top: 8px;
  font-size: 12px;
  font-style: italic;
  color: var(--muted);
}

.msg-actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 0.12s ease;
}

.msg-row:hover .msg-actions,
.msg-actions:focus-within { opacity: 1; }

.msg-actions button {
  padding: 4px 10px;
  font-size: 11px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.msg-actions button:hover:not([disabled]) {
  color: var(--text);
  background: var(--btn-hover);
}

@media (hover: none) {
  .msg-actions { opacity: 1; }
}

/* Composer */
.composer-wrap {
  flex-shrink: 0;
  padding: 0 16px 12px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.composer-inner {
  max-width: 768px;
  margin: 0 auto;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.composer.locked textarea {
  cursor: not-allowed;
}

.composer.locked .composer-send {
  opacity: 0.7;
}

.composer-input-wrap {
  flex: 1;
  min-width: 0;
}

.composer textarea {
  width: 100%;
  min-height: 24px;
  max-height: 200px;
  padding: 6px 4px;
  border: none;
  background: transparent;
  color: var(--text);
  resize: none;
  font: inherit;
  line-height: 1.5;
}

.composer textarea::placeholder { color: var(--muted); }
.composer textarea:focus { outline: none; box-shadow: none; }

.composer-send {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--primary-btn-bg);
  color: var(--primary-btn-fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.composer-send:hover:not([disabled]) { background: var(--primary-btn-hover); }
.composer-send[disabled] { opacity: 0.45; cursor: not-allowed; }

.composer-send .spinner {
  width: 16px;
  height: 16px;
  border: 2px solid color-mix(in srgb, var(--primary-btn-fg) 35%, transparent);
  border-top-color: var(--primary-btn-fg);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

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

.pre-wrap { position: relative; }

.pre-wrap > button.copy-code {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 2px 8px;
  font-size: 11px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  opacity: 0;
  transition: opacity 0.12s ease;
  cursor: pointer;
}

.pre-wrap:hover > button.copy-code { opacity: 1; }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 35;
}

/* Tablet: collapsible sidebar */
@media (max-width: 1024px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop.visible {
    display: block;
  }

  .sidebar-toggle {
    display: inline-flex;
  }
}

@media (max-width: 640px) {
  .scroller { padding: 16px 12px 12px; }
  .composer-wrap { padding: 0 10px 10px; }
  .bubble.user { max-width: 92%; }
  .chart-block.fullscreen { inset: 8px; }
  .table-scroll { max-height: 320px; }
  .topbar { padding: 8px 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* Optional shape switch under a finished answer. Never a precondition, so it
   reads as a quiet aside rather than a prompt. */
.format-switch {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.format-switch-label {
  font-size: 12px;
  color: var(--muted);
}

.format-switch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.format-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.format-btn:hover:not(:disabled) {
  background: var(--btn-hover);
  border-color: var(--text);
}

.format-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.format-badge {
  display: inline-block;
  margin-bottom: 6px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

@media print {
  .table-watermark,
  .table-tools-brand,
  .chart-brand {
    opacity: 0.7 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

.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;
}
