html, body { height: 100%; }
body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 14px; color: #1f2937; background: #f3f4f6; }
.ai-chat-root {
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --bg: #ffffff;
  --surface: #eef4ff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
  --radius: 12px;
  display: flex;
  height: 100vh;
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, rgba(37,99,235,.14), rgba(37,99,235,0) 46%),
    linear-gradient(180deg, #f8fbff 0%, #f2f6ff 48%, #eef3ff 100%);
}
.ai-chat-sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.ai-chat-sidebar-header {
  padding: 16px 14px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.ai-chat-sidebar-logo {
  display: flex;
  align-items: center;
  gap: 8px;
}
.ai-chat-sidebar-logo-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}
.ai-chat-sidebar-logo-title {
  font-weight: 700;
  font-size: 15px;
}
.ai-chat-context-wrap {
    display: none;
    align-items: center;
  margin-left: 0;
}
.ai-chat-context-indicator {
    cursor: help;
}
.ai-chat-btn-primary,
.ai-chat-btn-new {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  transition: background .12s;
}
.ai-chat-btn-primary:hover,
.ai-chat-btn-new:hover { background: var(--primary-h); }
.ai-chat-btn-new {
  width: 100%;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.ai-chat-sessions {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
}
.ai-chat-session {
  padding: 10px 14px;
  cursor: pointer;
  transition: background .1s;
  border-left: 3px solid transparent;
}
.ai-chat-session:hover { background: var(--surface); }
.ai-chat-session.is-active { background: #eff6ff; border-left-color: var(--primary); }
.ai-chat-session-msg {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 3px;
}
.ai-chat-session-meta,
.ai-chat-empty-sessions {
  font-size: 11px;
  color: var(--muted);
}
.ai-chat-empty-sessions {
  padding: 24px 14px;
  text-align: center;
}
.ai-chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: transparent;
}
.ai-chat-main-header {
  padding: 14px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.ai-chat-main-header-spacer { flex: 1; }
.ai-chat-header-btn {
  padding: 8px 12px;
}
.ai-chat-header-btn--history {
  display: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
}
.ai-chat-header-btn--history:hover {
  border-color: var(--primary);
  color: var(--primary);
}
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  background: transparent;
}
.ai-chat-msg { display: flex; flex-direction: column; max-width: 72%; min-width: 0; }
.ai-chat-msg--user { align-self: flex-end; align-items: flex-end; }
.ai-chat-msg--assistant { align-self: flex-start; align-items: flex-start; }
.ai-chat-msg--assistant.ai-chat-msg--pd-form {
  width: min(640px, 100%);
  max-width: 640px;
  box-sizing: border-box;
}
.ai-chat-msg--system { align-self: center; align-items: center; max-width: 100%; }
.ai-chat-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.55;
  word-break: break-word;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
.ai-chat-msg--user .ai-chat-bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.ai-chat-msg--assistant .ai-chat-bubble { background: var(--bg); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,.05); }
.ai-chat-msg--system .ai-chat-bubble { background: #fef9c3; color: #854d0e; font-size: 12px; border-radius: 20px; padding: 5px 14px; }
.ai-chat-time { font-size: 11px; color: var(--muted); margin-top: 3px; padding: 0 4px; }
.ai-chat-typing { display: flex; gap: 5px; padding: 12px 14px; }
.ai-chat-typing span { width: 7px; height: 7px; background: #9ca3af; border-radius: 50%; animation: ai-chat-bounce 1.1s ease-in-out infinite; }
.ai-chat-typing span:nth-child(2) { animation-delay: .18s; }
.ai-chat-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes ai-chat-bounce { 0%,80%,100% { transform: translateY(0); opacity: .4; } 40% { transform: translateY(-6px); opacity: 1; } }
.ai-chat-tool-status { display: inline-flex; align-items: center; gap: 9px; color: #334155; }
.ai-chat-tool-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--primary);
    animation: ai-chat-tool-pulse 1.2s ease-in-out infinite;
    box-shadow: 0 0 0 0 rgba(37,99,235,.45);
}
.ai-chat-tool-text { font-size: 13px; }
@keyframes ai-chat-tool-pulse {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(37,99,235,.45); opacity: .9; }
    70% { transform: scale(1.18); box-shadow: 0 0 0 8px rgba(37,99,235,0); opacity: 1; }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(37,99,235,0); opacity: .9; }
}
.ai-chat-bubble p { margin: 0 0 5px; }
.ai-chat-bubble p:last-child { margin: 0; }
.ai-chat-bubble code { background: rgba(0,0,0,.07); padding: 1px 5px; border-radius: 4px; font-size: 12px; font-family: 'SF Mono', Consolas, monospace; }
.ai-chat-bubble pre { background: #0f172a; color: #e2e8f0; padding: 12px 14px; border-radius: 8px; overflow-x: auto; font-size: 12px; font-family: 'SF Mono', Consolas, monospace; margin: 6px 0; white-space: pre; }
.ai-chat-bubble pre code { background: none; padding: 0; color: inherit; }
.ai-chat-bubble ul, .ai-chat-bubble ol { padding-left: 18px; margin: 4px 0; }
.ai-chat-bubble li { margin: 2px 0; }
.ai-chat-bubble h1, .ai-chat-bubble h2, .ai-chat-bubble h3 { font-weight: 600; margin: 6px 0 3px; }
.ai-chat-bubble h1 { font-size: 16px; }
.ai-chat-bubble h2 { font-size: 15px; }
.ai-chat-bubble .ai-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-gutter: stable; margin: 6px 0; width: 100%; max-width: 100%; }
.ai-chat-bubble table { border-collapse: collapse; font-size: 13px; }
.ai-chat-bubble th, .ai-chat-bubble td { padding: 5px 10px; border: 1px solid var(--border); text-align: left; white-space: nowrap; }
.ai-chat-bubble thead th, .ai-chat-bubble tr:first-child td { background: rgba(0,0,0,.04); font-weight: 600; }
.ai-chat-msg--user .ai-chat-bubble th,
.ai-chat-msg--user .ai-chat-bubble td { border-color: rgba(255,255,255,.25); }
.ai-chat-msg--user .ai-chat-bubble thead th,
.ai-chat-msg--user .ai-chat-bubble tr:first-child td { background: rgba(255,255,255,.12); }
.ai-chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  gap: 8px;
  padding: 40px;
}
.ai-chat-empty-icon {
  width: 166px;
  height: 164px;
  object-fit: contain;
  display: block;
  margin: -30px;
}
.ai-chat-empty-title { font-size: 18px; font-weight: 600; color: #374151; }
.ai-chat-empty-sub { font-size: 14px; text-align: center; line-height: 1.6; }
.ai-chat-input-area {
  padding: 12px 14px 14px;
  background: linear-gradient(180deg, rgba(248,251,255,.55) 0%, rgba(255,255,255,.94) 35%);
  border-top: 1px solid rgba(148,163,184,.24);
  box-shadow: 0 -12px 30px rgba(37,99,235,.08);
  flex-shrink: 0;
}
.ai-chat-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 14px 2px;
  background: transparent;
}
.ai-chat-chip {
  padding: 5px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s;
}
.ai-chat-chip:hover { background: #eff6ff; border-color: var(--primary); color: var(--primary); }
.ai-chat-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 6px;
  border-radius: 14px;
  border: 1px solid rgba(148,163,184,.28);
  background: rgba(255,255,255,.9);
  backdrop-filter: blur(8px);
}
.ai-chat-input-row .ai-chat-textarea {
  flex: 1;
  min-height: 40px;
  max-height: 130px;
  padding: 10px 13px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.4;
  resize: none;
  outline: none;
  background: transparent;
  color: var(--text);
  overflow-y: auto;
  box-shadow: none;
}
.ai-chat-input-row .ai-chat-textarea:focus { border: none; background: transparent; box-shadow: none; }
.ai-chat-send {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 55%, #1d4ed8 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  box-shadow: 0 8px 18px rgba(37,99,235,.28);
}
.ai-chat-send:hover { filter: brightness(1.05); }
.ai-chat-send:disabled { background: #93c5fd; cursor: not-allowed; box-shadow: none; }
.ai-chat-hint { font-size: 11px; color: #64748b; text-align: center; margin-top: 7px; }
.ai-chat-no-token {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
  color: var(--muted);
  padding: 40px;
}
.ai-chat-no-token-title { font-size: 18px; font-weight: 600; color: #dc2626; }
.ai-chat-no-token pre { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 12px 16px; font-size: 13px; color: var(--text); }
.ai-chat-choice-card { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin: 6px 0; background: var(--surface); }
.ai-chat-ui-question { font-size: 13px; font-weight: 600; margin-bottom: 8px; color: var(--text); }
.ai-chat-ui-btns { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-chat-ui-btn { padding: 6px 14px; background: #eff6ff; border: 1px solid #93c5fd; border-radius: 14px; font-size: 12px; font-family: inherit; cursor: pointer; color: #1e40af; transition: background .12s; }
.ai-chat-ui-btn:hover { background: #dbeafe; }
.ai-chat-ui-radio-group, .ai-chat-ui-check-group { display: flex; flex-direction: column; gap: 6px; }
.ai-chat-ui-radio-group label, .ai-chat-ui-check-group label { display: flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; }
.ai-chat-ui-confirm { margin-top: 8px; padding: 6px 14px; background: var(--primary); color: #fff; border: none; border-radius: 14px; font-size: 12px; font-family: inherit; cursor: pointer; }
.ai-chat-bubble[data-answered] .ai-chat-ui-btn,
.ai-chat-bubble[data-answered] .ai-chat-ui-confirm,
.ai-chat-bubble[data-answered] input { opacity: 0.5; pointer-events: none; cursor: default; }
.ai-chat-pd-form { display: flex; flex-direction: column; gap: 10px; }
.ai-chat-msg--pd-form .ai-chat-bubble,
.ai-chat-msg--pd-form .ai-chat-choice-card {
  width: 100%;
  box-sizing: border-box;
}
.ai-chat-pd-field { display: flex; flex-direction: column; gap: 4px; }
.ai-chat-pd-label { font-size: 12px; font-weight: 600; color: var(--text); }
.ai-chat-pd-field .input-wrapper { width: 100%; }
.ai-chat-pd-field .input-wrapper input { font-size: 13px; width: 100%; box-sizing: border-box; }
.ai-chat-pd-field .textarea-component { width: 100%; }
.ai-chat-pd-field .textarea-component textarea { font-size: 13px; width: 100%; box-sizing: border-box; min-height: 108px; resize: vertical; }
.ai-chat-pd-error { border-color: #ef4444 !important; }
.ai-chat-pd-consent { display: flex; align-items: flex-start; gap: 8px; font-size: 12px; line-height: 1.4; color: var(--muted); cursor: pointer; }
.ai-chat-pd-consent input { flex: 0 0 auto; width: 16px; height: 16px; margin: 1px 0 0; accent-color: var(--primary); }
.ai-chat-pd-consent a { color: var(--primary); text-decoration: underline; text-underline-offset: 2px; }
.ai-chat-pd-consent a:hover { color: var(--primary-h); }
.ai-chat-pd-consent--error { color: #dc2626; }
.ai-chat-pd-consent--error input { outline: 2px solid rgba(239, 68, 68, .35); outline-offset: 2px; }
.ai-chat-pd-submit-btn { margin-top: 4px; padding: 6px 14px; background: var(--primary); color: #fff; border: none; border-radius: 14px; font-size: 12px; font-family: inherit; cursor: pointer; }
.ai-chat-pd-submit-btn:hover { background: var(--primary-h); }
.ai-chat-bubble[data-answered] .ai-chat-pd-submit-btn { opacity: 0.5; pointer-events: none; cursor: default; }
.ai-mermaid-pending { padding: 8px 12px; background: #f8fafc; border: 1px dashed #cbd5e1; border-radius: 8px; color: #94a3b8; font-size: 12px; margin: 6px 0; }
.ai-mermaid-block { position: relative; margin: 8px 0; overflow: auto; }
.ai-mermaid-block svg { max-width: 100%; height: auto; display: block; }
.ai-mermaid-error { padding: 8px 12px; background: #fef2f2; border: 1px solid #fca5a5; border-radius: 8px; color: #dc2626; font-size: 12px; margin: 6px 0; }
.ai-mermaid-expand-btn { position: absolute; top: 6px; right: 6px; background: rgba(255,255,255,.85); border: 1px solid #e2e8f0; border-radius: 5px; padding: 4px 5px; cursor: pointer; color: #64748b; line-height: 1; opacity: 0; transition: opacity .15s; }
.ai-mermaid-block:hover .ai-mermaid-expand-btn { opacity: 1; }
.ai-mermaid-expand-btn:hover { background: #fff; color: #1e40af; border-color: #93c5fd; }

@media (max-width: 860px) {
    .ai-chat-root {
        position: relative;
    }

    .ai-chat-sidebar {
        display: flex;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(82vw, 320px);
        z-index: 40;
        border-right: 1px solid var(--border);
        box-shadow: 8px 0 30px rgba(15, 23, 42, 0.18);
        transform: translateX(-105%);
        transition: transform .18s ease;
    }

    .ai-chat-root.is-mobile-sidebar-open .ai-chat-sidebar {
        transform: translateX(0);
    }

    .ai-chat-mobile-backdrop {
        position: absolute;
        inset: 0;
        z-index: 35;
        background: rgba(15, 23, 42, 0.22);
        opacity: 0;
        pointer-events: none;
        transition: opacity .18s ease;
    }

    .ai-chat-root.is-mobile-sidebar-open .ai-chat-mobile-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    .ai-chat-main-header {
        padding: 10px 12px;
    }

    .ai-chat-messages {
        padding: 12px;
    }

    .ai-chat-msg {
        max-width: 88%;
    }

    .ai-chat-chips {
        padding: 8px 12px 2px;
    }

    .ai-chat-input-area {
        padding: 10px 12px 12px;
    }

    .ai-chat-header-btn--history {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

@media (max-width: 560px) {
    .ai-chat-msg {
        max-width: 94%;
    }

    .ai-chat-send {
        width: 38px;
        height: 38px;
    }
}

.ai-chat-root[data-ui-mode="widget"] .ai-chat-header-btn--history {
  display: none !important;
}
.ai-chat-root[data-ui-mode="widget"] .ai-chat-main-header {
  display: none !important;
}
.ai-chat-root[data-ui-mode="widget"] .ai-chat-msg {
  max-width: 100%;
}
.ai-chat-root[data-ui-mode="widget"] .ai-chat-msg--assistant.ai-chat-msg--pd-form {
  width: 100%;
  max-width: 100%;
}

/* Theme transition */
html.theme-fade * {
  transition: background-color 500ms ease, color 500ms ease, border-color 500ms ease, fill 500ms ease, stroke 500ms ease !important;
}

/* Dark theme */
[data-theme="dark"],
html[data-theme="dark"] {
  color-scheme: dark;
}
[data-theme="dark"] body,
html[data-theme="dark"] body {
  background: #0f172a;
  color: #e2e8f0;
}
[data-theme="dark"] .ai-chat-root,
html[data-theme="dark"] .ai-chat-root {
  --bg: #111827;
  --surface: #1e293b;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-h: #2563eb;
  background:
    radial-gradient(120% 80% at 50% -10%, rgba(59,130,246,.08), rgba(59,130,246,0) 46%),
    linear-gradient(180deg, #0f172a 0%, #0d1825 48%, #0c1e2e 100%);
}
[data-theme="dark"] .ai-chat-input-area,
html[data-theme="dark"] .ai-chat-input-area {
  background: linear-gradient(180deg, rgba(15,23,42,.55) 0%, rgba(17,24,39,.94) 35%);
  border-top-color: rgba(71,85,105,.35);
  box-shadow: 0 -12px 30px rgba(0,0,0,.2);
}
[data-theme="dark"] .ai-chat-input-row,
html[data-theme="dark"] .ai-chat-input-row {
  background: rgba(15,23,42,.9);
  border-color: rgba(71,85,105,.4);
}
[data-theme="dark"] .ai-chat-session.is-active,
html[data-theme="dark"] .ai-chat-session.is-active { background: #1e3a5f; }
[data-theme="dark"] .ai-chat-msg--system .ai-chat-bubble,
html[data-theme="dark"] .ai-chat-msg--system .ai-chat-bubble { background: #2a1f00; color: #fde68a; }
[data-theme="dark"] .ai-chat-tool-status,
html[data-theme="dark"] .ai-chat-tool-status { color: #cbd5e1; }
[data-theme="dark"] .ai-chat-tool-dot,
html[data-theme="dark"] .ai-chat-tool-dot {
  background: #60a5fa;
  box-shadow: 0 0 0 0 rgba(96,165,250,.5);
}
[data-theme="dark"] .ai-chat-ui-btn,
html[data-theme="dark"] .ai-chat-ui-btn { background: #1e3a5f; border-color: #3b5fc0; color: #93c5fd; }
[data-theme="dark"] .ai-chat-ui-btn:hover,
html[data-theme="dark"] .ai-chat-ui-btn:hover { background: #1e40af; }
[data-theme="dark"] .ai-chat-empty-title,
html[data-theme="dark"] .ai-chat-empty-title { color: #e2e8f0; }
[data-theme="dark"] .ai-chat-chip:hover,
html[data-theme="dark"] .ai-chat-chip:hover { background: #1e3a5f; border-color: var(--primary); color: #93c5fd; }
[data-theme="dark"] .ai-chat-typing span,
html[data-theme="dark"] .ai-chat-typing span { background: #64748b; }
[data-theme="dark"] .ai-mermaid-pending,
html[data-theme="dark"] .ai-mermaid-pending { background: #1e293b; border-color: #334155; color: #64748b; }
[data-theme="dark"] .ai-mermaid-error,
html[data-theme="dark"] .ai-mermaid-error { background: #2d1515; border-color: #b91c1c; color: #fca5a5; }
[data-theme="dark"] .ai-mermaid-expand-btn,
html[data-theme="dark"] .ai-mermaid-expand-btn { background: rgba(30,41,59,.85); border-color: #334155; color: #94a3b8; }
[data-theme="dark"] .ai-mermaid-expand-btn:hover,
html[data-theme="dark"] .ai-mermaid-expand-btn:hover { background: #1e293b; color: #93c5fd; border-color: #3b82f6; }
[data-theme="dark"] .ai-chat-mobile-backdrop,
html[data-theme="dark"] .ai-chat-mobile-backdrop { background: rgba(0,0,0,.45); }

[data-theme="dark"] .ai-chat-sessions,
[data-theme="dark"] .ai-chat-messages,
html[data-theme="dark"] .ai-chat-sessions,
html[data-theme="dark"] .ai-chat-messages {
  scrollbar-color: #334155 transparent;
  scrollbar-width: thin;
}
[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar,
[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar,
html[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar,
html[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar { width: 6px; }
[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-track,
[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-track,
html[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-track,
html[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-track { background: transparent; }
[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-thumb,
[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-thumb,
html[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-thumb,
html[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-thumb { background: #334155; border-radius: 3px; }
[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-thumb:hover,
[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-thumb:hover,
html[data-theme="dark"] .ai-chat-sessions::-webkit-scrollbar-thumb:hover,
html[data-theme="dark"] .ai-chat-messages::-webkit-scrollbar-thumb:hover { background: #475569; }
