/* ── Tokens ──────────────────────────────────────────── */

:root {
  color-scheme: light;

  --font-sans: "Inter", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Cascadia Code", Consolas, monospace;

  --bg-root: #f8f8f8;
  --bg-surface: #ffffff;
  --bg-hover: #f3f4f6;
  --bg-input: #f4f4f5;

  --border-light: #e8e8e8;
  --border-default: #d9d9d9;

  --text-primary: #1a1a1a;
  --text-secondary: #6b6b6b;
  --text-tertiary: #9e9e9e;
  --text-inverse: #ffffff;

  --accent: #4d49fc;
  --accent-soft: #f0efff;
  --accent-hover: #3d39e6;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,.06);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.1);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  --transition-fast: 140ms ease;
  --transition-base: 220ms ease;
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --bg-root: #1a1a1f;
  --bg-surface: #21212a;
  --bg-hover: #2a2a36;
  --bg-input: #2a2a36;

  --border-light: #333340;
  --border-default: #3e3e4d;

  --text-primary: #ececec;
  --text-secondary: #9d9dab;
  --text-tertiary: #6a6a7a;
  --text-inverse: #1a1a1f;

  --accent: #7c78ff;
  --accent-soft: #1e1d3a;
  --accent-hover: #9490ff;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.2);
  --shadow-md: 0 4px 16px rgba(0,0,0,.3);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.45);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    color-scheme: dark;

    --bg-root: #1a1a1f;
    --bg-surface: #21212a;
    --bg-hover: #2a2a36;
    --bg-input: #2a2a36;

    --border-light: #333340;
    --border-default: #3e3e4d;

    --text-primary: #ececec;
    --text-secondary: #9d9dab;
    --text-tertiary: #6a6a7a;
    --text-inverse: #1a1a1f;

    --accent: #7c78ff;
    --accent-soft: #1e1d3a;
    --accent-hover: #9490ff;

    --shadow-sm: 0 1px 2px rgba(0,0,0,.2);
    --shadow-md: 0 4px 16px rgba(0,0,0,.3);
    --shadow-lg: 0 12px 40px rgba(0,0,0,.45);
  }
}

/* ── Reset ───────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  width: 100%; height: 100%; margin: 0;
}

body {
  overflow: hidden;
  background: var(--bg-root);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button, textarea, input {
  font: inherit; color: inherit;
}

button { cursor: pointer; border: 0; background: none; padding: 0; }
textarea, input { outline: none; border: 0; background: none; }

svg {
  display: block; width: 18px; height: 18px;
  fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
  flex-shrink: 0;
}

/* ── App Shell ───────────────────────────────────────── */

.app {
  display: grid;
  grid-template-rows: auto 1fr;
  width: 100%;
  height: 100dvh;
  min-height: 0;
}

/* ── Topbar ──────────────────────────────────────────── */

.topbar {
  flex-shrink: 0;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-surface);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  padding: 0 24px;
  max-width: 100%;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--text-inverse);
  font-size: 14px; font-weight: 700;
}

.brand-name {
  font-size: 15px; font-weight: 600;
  letter-spacing: -.01em;
}

.topbar-right {
  display: flex; align-items: center; gap: 12px;
}

.status-badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-secondary);
}

.status-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #f5a623;
}

.status-badge[data-tone="ready"] .status-dot { background: #34c759; }
.status-badge[data-tone="error"] .status-dot { background: #ff3b30; }

.btn-manual {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-size: 13px; color: var(--text-secondary);
  transition: all var(--transition-fast);
}

.btn-manual:hover {
  border-color: var(--accent); color: var(--accent);
  background: var(--accent-soft);
}

/* ── Chat Area ───────────────────────────────────────── */

.chat-area {
  display: grid;
  grid-template-rows: 1fr auto;
  min-height: 0;
  overflow: hidden;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  padding: 0 24px;
}

.messages.is-welcome {
  display: flex;
  align-items: center;
  overflow-y: hidden;
}

/* ── Message list inner container ────────────────────── */

.messages-inner {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 32px 0 20px;
}

.messages.is-welcome .messages-inner {
  padding: 24px 0;
}

/* ── Message Row ─────────────────────────────────────── */

.message {
  display: flex;
  gap: 14px;
  margin-bottom: 28px;
  animation: msgIn .24s ease;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
}

.message-user    { justify-content: flex-end; }
.message-assistant { justify-content: flex-start; }

/* ── Avatar ──────────────────────────────────────────── */

.avatar {
  display: grid; place-items: center;
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
  color: var(--text-secondary);
  font-size: 14px; font-weight: 700;
  user-select: none;
}

.message-assistant .avatar {
  background: var(--accent-soft);
  color: var(--accent);
}

/* ── Message Panel ───────────────────────────────────── */

.message-panel {
  min-width: 0;
  max-width: 720px;
}

.message-assistant .message-panel {
  flex: 1;
  max-width: 720px;
}

.message-user .message-panel {
  max-width: 600px;
}

/* ── Message Body ────────────────────────────────────── */

.message-body {
  font-size: 15px;
  line-height: 1.75;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.message-assistant .message-body {
  color: var(--text-primary);
}

.message-user .message-body {
  padding: 8px 16px;
  border-radius: var(--radius-lg);
  background: var(--accent);
  color: var(--text-inverse);
}

.message-error .message-body {
  color: #ef4444;
}

/* ── Timestamp ───────────────────────────────────────── */

.message-time {
  margin-top: 5px;
  font-size: 11px;
  color: var(--text-tertiary);
}

.message-user .message-time { text-align: right; }

/* ── Welcome ─────────────────────────────────────────── */

.welcome-hero {
  text-align: center;
  padding: 0 24px 32px;
}

.welcome-hero-icon {
  display: inline-grid; place-items: center;
  width: 56px; height: 56px;
  margin-bottom: 20px;
  border-radius: var(--radius-md);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 24px; font-weight: 700;
}

.welcome-hero h2 {
  margin: 0 0 8px;
  font-size: 22px; font-weight: 660;
  letter-spacing: -.02em;
}

.welcome-hero p {
  margin: 0 auto;
  max-width: 480px;
  color: var(--text-secondary);
  font-size: 14px; line-height: 1.65;
}

.welcome-divider {
  display: flex; align-items: center; gap: 16px;
  margin: 0 0 20px;
}

.welcome-divider::before,
.welcome-divider::after {
  content: ""; flex: 1; height: 1px;
  background: var(--border-light);
}

.welcome-divider span {
  font-size: 11px; color: var(--text-tertiary); font-weight: 500;
  text-transform: uppercase; letter-spacing: .06em;
  flex-shrink: 0;
}

.quick-questions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.quick-question {
  min-height: 46px; padding: 10px 18px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  font-size: 14px; text-align: left; line-height: 1.5;
  transition: all var(--transition-fast);
}

.quick-question:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 2px 8px rgba(77,73,252,.1);
}

/* ── Loading ─────────────────────────────────────────── */

.loading-body {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 6px 0;
}

.loading-dot {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--accent);
  animation: dotBounce 1.2s ease-in-out infinite;
}

.loading-dot:nth-child(2) { animation-delay: .16s; }
.loading-dot:nth-child(3) { animation-delay: .32s; }

/* ── Sources ─────────────────────────────────────────── */

.sources {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 12px;
}

.sources-label {
  font-size: 11px; color: var(--text-tertiary);
  font-weight: 500; text-transform: uppercase; letter-spacing: .05em;
}

.source-chip {
  display: inline-flex; align-items: center;
  min-height: 28px; padding: 3px 12px;
  border: 1px solid var(--border-light);
  border-radius: 999px;
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-size: 12px;
  transition: all var(--transition-fast);
}

.source-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

/* ── Composer ────────────────────────────────────────── */

.composer-wrapper {
  padding: 0 24px 20px;
  background: var(--bg-root);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  max-width: 860px;
  margin: 0 auto;
  padding: 12px 18px;
  border: 1.5px solid var(--border-light);
  border-radius: var(--radius-lg);
  background: var(--bg-surface);
  transition: border-color var(--transition-base), box-shadow var(--transition-base);
}

.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(77,73,252,.12);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  max-height: 150px;
  min-height: 38px;
  padding: 7px 0;
  resize: none;
  background: transparent;
  font-size: 15px; line-height: 1.55;
}

.composer textarea:placeholder-shown {
  padding: 0;
  line-height: 38px;
}

.composer textarea::placeholder {
  color: var(--text-tertiary);
}

.btn-send {
  display: grid; place-items: center;
  flex-shrink: 0;
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--text-inverse);
  transition: all var(--transition-fast);
}

.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: .4; cursor: not-allowed; }
.btn-send:disabled:hover { background: var(--accent); }

.composer-footer {
  display: flex;
  justify-content: space-between;
  max-width: 860px;
  margin: 6px auto 0;
  padding: 0 4px;
}

.counter, .composer-hint {
  font-size: 11px; color: var(--text-tertiary);
}

/* ── Markdown ────────────────────────────────────────── */

.markdown-body > :first-child { margin-top: 0; }
.markdown-body > :last-child  { margin-bottom: 0; }

.markdown-body p  { margin: 0 0 10px; }
.markdown-body p:last-child { margin-bottom: 0; }

.markdown-body h1,.markdown-body h2,.markdown-body h3,
.markdown-body h4,.markdown-body h5,.markdown-body h6 {
  margin: 18px 0 8px;
  font-weight: 650; line-height: 1.35;
}

.markdown-body h1 { font-size: 18px; }
.markdown-body h2 { font-size: 17px; }
.markdown-body h3 { font-size: 16px; }
.markdown-body h4,.markdown-body h5,.markdown-body h6 { font-size: 15px; }

.markdown-body ul,.markdown-body ol {
  margin: 8px 0 12px; padding-left: 22px;
}

.markdown-body li { margin: 4px 0; }

.markdown-body code {
  padding: 2px 5px; border-radius: 4px;
  background: var(--bg-input);
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: .9em;
}

.markdown-body pre {
  overflow-x: auto; margin: 12px 0; padding: 16px;
  border-radius: var(--radius-md);
  background: #1e1e24;
  color: #e1e1e6;
  font-size: 13px; line-height: 1.65;
}

.markdown-body pre code {
  padding: 0; background: transparent; color: inherit; font-size: inherit;
}

.markdown-body blockquote {
  margin: 10px 0; padding: 8px 16px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  color: var(--text-secondary);
}

.markdown-body a {
  color: var(--accent); text-decoration: none;
}

.markdown-body a:hover { text-decoration: underline; }

.markdown-body hr {
  margin: 16px 0; border: 0;
  border-top: 1px solid var(--border-light);
}

/* ── Document Overlay ────────────────────────────────── */

.doc-overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0,0,0,.3);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  opacity: 0; pointer-events: none;
  transition: opacity var(--transition-base);
}

.doc-overlay.is-open {
  opacity: 1; pointer-events: auto;
}

/* ── Document Panel ──────────────────────────────────── */

.doc-panel {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 21;
  display: grid; grid-template-rows: auto 1fr;
  width: min(800px, 92vw);
  overflow: hidden;
  border-left: 1px solid var(--border-light);
  background: var(--bg-surface);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  transition: transform var(--transition-base);
  pointer-events: none;
}

.doc-panel.is-open {
  transform: translateX(0);
  pointer-events: auto;
}

.doc-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-light);
}

.doc-title {
  margin: 0;
  font-size: 16px; font-weight: 650;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.doc-head-actions {
  display: flex; align-items: center; gap: 10px;
  flex-shrink: 0;
}

.doc-search {
  display: flex; align-items: center; gap: 8px;
  width: 220px; padding: 7px 12px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  background: var(--bg-root);
  transition: border-color var(--transition-fast);
}

.doc-search:focus-within {
  border-color: var(--accent);
}

.doc-search svg { width: 16px; height: 16px; color: var(--text-tertiary); }

.doc-search input {
  flex: 1; min-width: 0;
  font-size: 13px;
}

.doc-search input::placeholder { color: var(--text-tertiary); }

.btn-close {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: all var(--transition-fast);
}

.btn-close:hover {
  background: var(--bg-hover); color: var(--text-primary);
}

.doc-body {
  display: grid;
  grid-template-columns: 230px 1fr;
  min-height: 0; overflow: hidden;
}

.doc-toc {
  min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 12px;
  border-right: 1px solid var(--border-light);
  background: var(--bg-root);
}

.toc-tree {
  display: grid;
  gap: 2px;
}

.toc-node {
  min-width: 0;
}

.toc-node-row {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  align-items: center;
  min-width: 0;
  padding-left: calc(var(--toc-level, 0) * 14px);
}

.toc-toggle,
.toc-spacer {
  display: grid;
  place-items: center;
  width: 22px;
  height: 32px;
  color: var(--text-tertiary);
}

.toc-toggle {
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.toc-toggle svg {
  width: 15px;
  height: 15px;
  transition: transform var(--transition-fast);
}

.toc-toggle.is-collapsed svg {
  transform: rotate(-90deg);
}

.toc-toggle:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.toc-node-button {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 32px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.45;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: all var(--transition-fast);
}

.toc-node-button:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.toc-node-button.is-parent {
  font-weight: 560;
  color: var(--text-primary);
}

.toc-node-button.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.toc-children {
  display: grid;
  gap: 1px;
}

.doc-content {
  min-width: 0; min-height: 0;
  overflow-y: auto; overscroll-behavior: contain;
  padding: 24px 28px 40px;
  scroll-behavior: smooth;
}

/* ── Manual Sections ─────────────────────────────────── */

.manual-section {
  scroll-margin-top: 20px;
  padding: 20px 0;
  border-bottom: 1px solid var(--border-light);
}

.manual-section:last-child { border-bottom: 0; }

.manual-section.is-highlighted {
  animation: sectionFlash 1.6s ease;
}

.manual-section-title {
  margin: 0 0 14px;
  font-size: 17px; font-weight: 650;
}

.manual-section .markdown-body {
  font-size: 14px;
}

.doc-empty {
  padding: 32px 24px; text-align: center;
  color: var(--text-tertiary); font-size: 13px;
}

/* ── Document Markdown ───────────────────────────────── */

.document-markdown table {
  display: block; width: 100%; overflow-x: auto;
  border-collapse: collapse; margin: 12px 0;
  font-size: 13px;
}

.document-markdown th,.document-markdown td {
  padding: 8px 12px; border: 1px solid var(--border-light);
  text-align: left; vertical-align: top;
}

.document-markdown th {
  background: var(--bg-root); font-weight: 600;
  color: var(--text-secondary);
}

.document-markdown img {
  display: block; max-width: 100%; height: auto;
  margin: 12px 0;
  border-radius: var(--radius-sm);
}

/* ── Animations ──────────────────────────────────────── */

@keyframes dotBounce {
  0%, 60%, 100% { opacity: .25; transform: scale(.75); }
  30%           { opacity: 1;   transform: scale(1.1);  }
}

@keyframes sectionFlash {
  0%   { background: rgba(77,73,252,.08); }
  100% { background: transparent; }
}

/* ── Responsive ──────────────────────────────────────── */

@media (max-width: 720px) {
  .topbar-inner { padding: 0 14px; }

  .status-badge { display: none; }

  .btn-manual {
    width: 32px; height: 32px; padding: 0; gap: 0; font-size: 0;
  }

  .messages { padding: 0 14px; }

  .messages-inner { padding: 24px 0 16px; }

  .message { gap: 10px; margin-bottom: 22px; }

  .message-assistant {
    display: grid;
    grid-template-columns: 34px 1fr;
  }

  .message-panel { max-width: 100%; }
  .message-assistant .message-panel,
  .message-user .message-panel { max-width: 100%; }

  .message-user .message-body { padding: 8px 14px; }

  .welcome-hero { padding: 40px 16px 28px; }
  .welcome-hero h2 { font-size: 20px; }

  .quick-questions { grid-template-columns: 1fr; }

  .composer-wrapper { padding: 0 14px 14px; }
  .composer { padding: 10px 14px; }
  .composer-footer { display: none; }

  .doc-panel { width: 100vw; }
  .doc-head { padding: 12px 14px; }
  .doc-search { width: auto; flex: 1; }

  .doc-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .doc-toc {
    max-height: 190px;
    overflow-y: auto;
    border-right: 0;
    border-bottom: 1px solid var(--border-light);
    padding: 10px 12px;
  }

  .toc-node-row {
    padding-left: calc(var(--toc-level, 0) * 10px);
  }

  .doc-content { padding: 18px; }
}
