:root {
  --ac-chat-page: #f8fafc;
  --ac-chat-shell-bg: #ffffff;
  --ac-chat-sidebar-bg: #ffffff;
  --ac-chat-panel-bg: #f8fafc;
  --ac-chat-card: #ffffff;
  --ac-chat-input-bg: #ffffff;
  --ac-chat-border: #dbe4ee;
  --ac-chat-title: #172033;
  --ac-chat-body: #475569;
  --ac-chat-muted: #64748b;
  --ac-chat-placeholder: #7b8794;
  --ac-chat-primary: #2563eb;
  --ac-chat-primary-text: #ffffff;
  --ac-chat-soft-primary: #e8f0ff;
  --ac-chat-danger: #b91c1c;
  --ac-chat-warning-bg: #fff7ed;
  --ac-chat-warning-border: #fed7aa;
  --ac-chat-error-bg: rgba(185, 28, 28, .10);
  --ac-chat-error-border: rgba(185, 28, 28, .24);
  --ac-chat-shadow: 0 14px 34px rgba(15, 23, 42, .10);
  --ac-chat-card-shadow: 0 6px 18px rgba(15, 23, 42, .06);
  --ac-chat-text-sm: 13px;
  --ac-chat-text-base: 15px;
  --ac-chat-text-md: 16px;
  --ac-chat-section-title: 18px;
  --ac-chat-answer-title: 21px;
  --ac-chat-header-title: 22px;
  --ac-site-header-height: 55px;
}

body.dark-mode,
html[data-theme="dark"],
body[data-theme="dark"] {
  --ac-chat-page: #0b1317;
  --ac-chat-shell-bg: #111f25;
  --ac-chat-sidebar-bg: #13242b;
  --ac-chat-panel-bg: #0b1317;
  --ac-chat-card: #172a31;
  --ac-chat-input-bg: #0f1c22;
  --ac-chat-border: #29444e;
  --ac-chat-title: #f8fafc;
  --ac-chat-body: #d7e1e8;
  --ac-chat-muted: #9db0ba;
  --ac-chat-placeholder: #8298a3;
  --ac-chat-primary: #60a5fa;
  --ac-chat-primary-text: #061016;
  --ac-chat-soft-primary: #17345d;
  --ac-chat-danger: #f87171;
  --ac-chat-warning-bg: #2c2114;
  --ac-chat-warning-border: #7c4a18;
  --ac-chat-error-bg: rgba(248, 113, 113, .12);
  --ac-chat-error-border: rgba(248, 113, 113, .28);
  --ac-chat-shadow: 0 14px 34px rgba(0, 0, 0, .26);
  --ac-chat-card-shadow: 0 8px 20px rgba(0, 0, 0, .22);
  background: var(--ac-chat-page);
}

[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
.ac-chat-page {
  min-height: calc(100vh - var(--ac-site-header-height));
  min-height: calc(100dvh - var(--ac-site-header-height));
  background: var(--ac-chat-page);
  color: var(--ac-chat-body);
  padding: 12px 0;
}
.ac-chat-shell {
  height: calc(100vh - var(--ac-site-header-height) - 24px);
  height: calc(100dvh - var(--ac-site-header-height) - 24px);
  min-height: 0;
  display: grid;
  grid-template-columns: 286px minmax(0, 1fr);
  border: 1px solid var(--ac-chat-border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--ac-chat-shell-bg);
  box-shadow: var(--ac-chat-shadow);
}
.ac-chat-sidebar {
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--ac-chat-border);
  background: var(--ac-chat-sidebar-bg);
  padding: 14px;
  overflow: auto;
}
.ac-chat-sidebar-head,
.ac-chat-panel-head,
.history-actions,
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}
.ac-chat-sidebar-head,
.ac-chat-panel-head { justify-content: space-between; }
.ac-chat-sidebar-head h1,
.ac-chat-panel-head h2 {
  margin: 0;
  color: var(--ac-chat-title);
  font-size: var(--ac-chat-header-title);
  line-height: 1.2;
  font-weight: 700;
}
.history-actions { margin: 14px 0 10px; }
.primary-button,
.send-button,
.secondary-button {
  border: 0;
  background: var(--ac-chat-primary);
  color: var(--ac-chat-primary-text);
  border-radius: 10px;
  font-weight: 800;
  padding: 10px 14px;
  text-decoration: none;
  cursor: pointer;
}
.primary-button { flex: 1; text-align: center; }
.secondary-button {
  border: 1px solid var(--ac-chat-border);
  background: var(--ac-chat-card);
  color: var(--ac-chat-primary);
  cursor: pointer;
  font-size: var(--ac-chat-text-sm);
  font-weight: 700;
}
.load-more-conversations {
  width: 100%;
  margin-top: 10px;
}
.ac-icon-button,
.theme-toggle {
  border: 1px solid var(--ac-chat-border);
  border-radius: 10px;
  color: var(--ac-chat-title);
  background: var(--ac-chat-card);
  cursor: pointer;
  font-weight: 800;
}
.ac-icon-button {
  width: 38px;
  height: 38px;
}
.theme-toggle {
  color: var(--ac-chat-primary);
  padding: 9px 10px;
  white-space: nowrap;
}
.ac-icon-button.danger { color: var(--ac-chat-danger); }
.mobile-close,
.mobile-menu { display: none; }
.status-line {
  min-height: 20px;
  color: var(--ac-chat-muted);
  font-size: var(--ac-chat-text-sm);
  margin-bottom: 10px;
}
.conversation-list { display: grid; gap: 9px; }
.conversation-card-wrap {
  position: relative;
}
.conversation-card {
  width: 100%;
  display: flex;
  align-items: center;
  border: 1px solid var(--ac-chat-border);
  border-radius: 10px;
  background: var(--ac-chat-card);
  color: var(--ac-chat-body);
  padding: 10px 8px 10px 12px;
}
.conversation-select {
  min-width: 0;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  padding: 2px 6px 2px 0;
  text-align: left;
}
.conversation-options-button {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ac-chat-muted);
  cursor: pointer;
  font-weight: 900;
  line-height: 1;
}
.conversation-options-button:hover,
.conversation-options-button[aria-expanded="true"] {
  color: var(--ac-chat-primary);
  background: var(--ac-chat-soft-primary);
}
.conversation-menu {
  position: absolute;
  top: calc(100% + 5px);
  right: 0;
  z-index: 5;
  min-width: 190px;
  border: 1px solid var(--ac-chat-border);
  border-radius: 10px;
  background: var(--ac-chat-card);
  box-shadow: var(--ac-chat-card-shadow);
  padding: 6px;
}
.conversation-menu-item {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ac-chat-title);
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  padding: 9px 10px;
  text-align: left;
}
.conversation-menu-item:hover,
.conversation-menu-item:focus {
  background: var(--ac-chat-soft-primary);
  outline: none;
}
.conversation-menu-item.danger { color: var(--ac-chat-danger); }
.conversation-card.active {
  border-color: var(--ac-chat-primary);
  background: var(--ac-chat-soft-primary);
}
.conversation-icon {
  width: 11px;
  height: 11px;
  border-radius: 999px;
  background: var(--ac-chat-primary);
  flex: 0 0 auto;
}
.conversation-main { min-width: 0; display: grid; gap: 3px; }
.conversation-main strong {
  color: var(--ac-chat-title);
  display: block;
  font-size: var(--ac-chat-text-base);
  font-weight: 700;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ac-chat-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--ac-chat-panel-bg);
}
.ac-chat-panel-head {
  min-width: 0;
  border-bottom: 1px solid var(--ac-chat-border);
  background: var(--ac-chat-card);
  padding: 13px 14px;
}
.messages {
  min-height: 0;
  overflow: auto;
  padding: 18px;
}
.ac-chat-shell.is-authenticated .auth-state { display: none !important; }
.ac-chat-shell.needs-auth .messages,
.ac-chat-shell.needs-auth .input-dock { display: none !important; }
.empty-state,
.auth-state,
.service-state {
  min-height: 430px;
  display: grid;
  place-items: center;
  align-content: center;
  text-align: center;
  gap: 10px;
  padding: 24px;
}
.empty-state h2,
.auth-state h2,
.service-state h2 {
  margin: 0;
  color: var(--ac-chat-title);
  font-size: 24px;
  font-weight: 700;
}
.empty-state p,
.auth-state p,
.service-state p {
  max-width: 480px;
  margin: 0;
  color: var(--ac-chat-muted);
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.empty-icon,
.assistant-avatar {
  display: grid;
  place-items: center;
  border-radius: 999px;
  color: var(--ac-chat-primary);
  background: var(--ac-chat-soft-primary);
  font-weight: 900;
}
.empty-icon { width: 70px; height: 70px; font-size: 22px; }
.assistant-avatar { width: 34px; height: 34px; font-size: 12px; }
.suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}
.suggestions button {
  border: 1px solid var(--ac-chat-border);
  border-radius: 999px;
  background: var(--ac-chat-card);
  color: var(--ac-chat-primary);
  padding: 8px 12px;
  cursor: pointer;
  font-size: var(--ac-chat-text-sm);
  font-weight: 700;
}
.user-message-text { white-space: pre-wrap; }
.scan-summary {
  margin-top: 12px;
  border-top: 1px solid color-mix(in srgb, currentColor 24%, transparent);
  padding-top: 10px;
  color: inherit;
}
.scan-summary > strong {
  display: block;
  margin-bottom: 7px;
  font-size: var(--ac-chat-text-sm);
}
.scan-summary dl {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 4px 10px;
  margin: 0;
  font-size: var(--ac-chat-text-sm);
  line-height: 1.45;
}
.scan-summary dt { font-weight: 700; }
.scan-summary dd { margin: 0; overflow-wrap: anywhere; }
.message-row { display: flex; margin: 0 0 13px; }
.message-row.user { justify-content: flex-end; }
.user-bubble {
  max-width: min(620px, 74%);
  padding: 12px 14px;
  border-radius: 16px 16px 5px 16px;
  background: var(--ac-chat-primary);
  color: var(--ac-chat-primary-text);
  white-space: pre-wrap;
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.assistant-card {
  max-width: min(780px, 92%);
  padding: 18px;
  border: 1px solid var(--ac-chat-border);
  border-radius: 12px;
  background: var(--ac-chat-card);
  box-shadow: var(--ac-chat-card-shadow);
}
.assistant-head {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ac-chat-title);
  font-weight: 800;
  margin-bottom: 10px;
}
.assistant-card .content {
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.6;
  white-space: pre-wrap;
}
.markdown-content {
  white-space: normal;
}
.markdown-content p,
.markdown-content ul,
.markdown-content ol,
.markdown-content pre {
  margin: 0 0 12px;
}
.markdown-content > :last-child {
  margin-bottom: 0;
}
.markdown-content ul,
.markdown-content ol {
  padding-left: 24px;
}
.markdown-content li {
  margin: 0 0 8px;
}
.markdown-content li:last-child {
  margin-bottom: 0;
}
.markdown-content strong {
  color: var(--ac-chat-title);
  font-weight: 700;
}
.markdown-content em {
  font-style: italic;
}
.markdown-content code {
  border-radius: 5px;
  background: var(--ac-chat-soft-primary);
  color: var(--ac-chat-title);
  padding: 1px 5px;
  font-size: 0.92em;
}
.markdown-content pre {
  overflow: auto;
  border: 1px solid var(--ac-chat-border);
  border-radius: 8px;
  background: var(--ac-chat-input-bg);
  padding: 10px;
}
.markdown-content pre code {
  background: transparent;
  padding: 0;
}
.typing-dots {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ac-chat-primary);
  font-weight: 700;
}
.chat-loading-spinner {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  border: 2px solid var(--ac-chat-border);
  border-top-color: var(--ac-chat-primary);
  border-radius: 50%;
  animation: ac-chat-spin 0.8s linear infinite;
}
.chat-loading-text { color: var(--ac-chat-body); }
@keyframes ac-chat-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .chat-loading-spinner { animation-duration: 1.8s; }
}
.structured-response { display: grid; gap: 16px; }
.structured-header h3 {
  margin: 0;
  color: var(--ac-chat-title);
  font-size: var(--ac-chat-answer-title);
  line-height: 1.25;
  font-weight: 700;
}
.structured-header p {
  margin: 8px 0 0;
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.6;
}
.structured-header span {
  display: inline-flex;
  margin-bottom: 6px;
  border-radius: 999px;
  background: var(--ac-chat-soft-primary);
  color: var(--ac-chat-primary);
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 900;
}
.diagnostic-code-list {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}
.diagnostic-code-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: baseline;
  gap: 10px;
}
.diagnostic-code-item span { margin: 0; }
.diagnostic-code-item strong {
  color: var(--ac-chat-title);
  font-size: var(--ac-chat-text-base);
  line-height: 1.45;
}
.structured-section h4 {
  margin: 0 0 8px;
  color: var(--ac-chat-title);
  font-size: var(--ac-chat-section-title);
  line-height: 1.3;
  font-weight: 700;
}
.structured-section p {
  margin: 0;
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.6;
}
.section-note {
  color: var(--ac-chat-muted) !important;
  font-size: var(--ac-chat-text-sm) !important;
  line-height: 1.45 !important;
  margin-bottom: 10px !important;
}
.warning-banner {
  border: 1px solid var(--ac-chat-warning-border);
  border-radius: 10px;
  background: var(--ac-chat-warning-bg);
  color: var(--ac-chat-title);
  padding: 10px 12px;
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.cause-list,
.step-list {
  margin: 0;
  padding-left: 22px;
  display: grid;
  gap: 13px;
}
.cause-item,
.step-item { padding-left: 4px; }
.cause-line,
.step-title {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 4px;
  color: var(--ac-chat-title);
  font-size: var(--ac-chat-text-md);
  font-weight: 700;
  line-height: 1.35;
}
.cause-line { flex-wrap: wrap; }
.item-detail {
  margin: 0;
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.probability-badge {
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--ac-chat-soft-primary);
  color: var(--ac-chat-primary);
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
}
.load-older-messages {
  display: block;
  margin: 0 auto 16px;
  border-radius: 8px;
  padding: 8px 12px;
}
.follow-up-list {
  margin: 0;
  padding-left: 22px;
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.follow-up-list li + li { margin-top: 7px; }
.scan-detail-list,
.scan-string-list {
  margin: 0;
  padding-left: 22px;
  color: var(--ac-chat-body);
  font-size: var(--ac-chat-text-base);
  line-height: 1.55;
}
.scan-detail-list {
  display: grid;
  gap: 13px;
}
.scan-detail-list > li,
.scan-string-list > li { padding-left: 4px; }
.scan-string-list li + li { margin-top: 7px; }
.scan-title-line {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 4px;
}
.rank-label,
.scan-code-line {
  color: var(--ac-chat-muted);
  font-size: var(--ac-chat-text-sm);
  font-weight: 600;
}
.rank-label { text-transform: capitalize; }
.scan-code-line { margin: 3px 0 5px; }
.input-dock {
  position: sticky;
  bottom: 0;
  z-index: 2;
  flex-shrink: 0;
  border-top: 1px solid var(--ac-chat-border);
  background: var(--ac-chat-card);
  padding: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}
.input-dock textarea {
  min-height: 46px;
  max-height: 140px;
  resize: vertical;
  border: 1px solid var(--ac-chat-border);
  border-radius: 12px;
  color: var(--ac-chat-body);
  background: var(--ac-chat-input-bg);
  padding: 12px 13px;
}
.input-dock textarea::placeholder { color: var(--ac-chat-placeholder); }
.send-button { width: 50px; height: 46px; padding: 0; }
.diagnostic-card {
  grid-column: 1 / -1;
  border: 1px solid var(--ac-chat-border);
  border-radius: 12px;
  background: var(--ac-chat-card);
  padding: 12px;
}
.diagnostic-card strong { display: block; color: var(--ac-chat-title); }
.diagnostic-card span { color: var(--ac-chat-muted); }
.chat-usage,
.chat-limit-actions {
  grid-column: 1 / -1;
  color: var(--ac-chat-muted);
  font-size: var(--ac-chat-text-sm);
}
.chat-limit-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}
.chat-limit-actions a,
.chat-limit-actions button {
  color: var(--ac-chat-primary);
  font: inherit;
  font-weight: 700;
}
.chat-limit-actions button {
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
}
.error-banner {
  margin: 10px 0;
  padding: 12px;
  border-radius: 10px;
  color: var(--ac-chat-danger);
  background: var(--ac-chat-error-bg);
  border: 1px solid var(--ac-chat-error-border);
}

@media (max-width: 920px) {
  .ac-chat-page { padding-top: 10px; }
  .ac-chat-shell {
    grid-template-columns: 1fr;
    height: calc(100vh - var(--ac-site-header-height) - 22px);
    height: calc(100dvh - var(--ac-site-header-height) - 22px);
    min-height: 0;
    border-radius: 10px;
  }
  .ac-chat-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(88vw, 330px);
    z-index: 60;
    transform: translateX(-105%);
    transition: transform 160ms ease;
    box-shadow: var(--ac-chat-shadow);
  }
  .ac-chat-sidebar.open { transform: translateX(0); }
  .mobile-close,
  .mobile-menu { display: inline-grid; place-items: center; }
  .messages { padding: 14px; }
}

@media (max-width: 560px) {
  :root {
    --ac-chat-text-base: 15px;
    --ac-chat-text-md: 15px;
    --ac-chat-section-title: 16px;
    --ac-chat-answer-title: 20px;
    --ac-chat-header-title: 19px;
  }
  .ac-chat-page {
    min-height: calc(100vh - var(--ac-site-header-height));
    min-height: calc(100dvh - var(--ac-site-header-height));
    padding: 8px 0 0;
  }
  .ac-chat-page .px-4 { padding-left: 10px; padding-right: 10px; }
  .ac-chat-shell {
    height: calc(100vh - var(--ac-site-header-height) - 8px);
    height: calc(100dvh - var(--ac-site-header-height) - 8px);
    min-height: 0;
  }
  .ac-chat-panel-head { padding: 11px; }
  .header-actions { gap: 5px; }
  .theme-toggle { padding: 8px; }
  .ac-icon-button { width: 36px; height: 36px; }
  .user-bubble,
  .assistant-card { max-width: 100%; }
  .cause-line { align-items: center; }
  .step-title { align-items: flex-start; }
  .input-dock {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}
