/* ── Reset & Base ─────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #fdfaf5;
  --surface: #fffbf2;
  --border: #e8dcc8;
  --text: #3a3228;
  --text-muted: #8a7e6e;
  --accent: #b8532a;
  --accent-hover: #9a3f1e;
  --green: #5a8a4a;
  --amber: #c4943a;
  --red: #c44a3a;
  --radius: 8px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "SimSun", "STSong", serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1b16;
    --surface: #28241e;
    --border: #3d362b;
    --text: #d4ccbe;
    --text-muted: #8a8070;
    --accent: #d4744a;
    --accent-hover: #e08a62;
    --green: #6a9e56;
    --amber: #d4a44a;
    --red: #d45a4a;
  }
}

html {
  font-size: 17px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  html { color-scheme: dark; }
}
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

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

/* ── Layout ────────────────────────────────────── */
.container { max-width: 960px; margin: 0 auto; padding: 24px 20px 60px; }
.reading-container { max-width: 760px; margin: 0 auto; padding: 24px 20px 60px; }

/* ── Header ────────────────────────────────────── */
.page-header { text-align: center; padding: 40px 0 32px; }
.page-header h1 { font-size: 2rem; font-weight: 700; letter-spacing: 0.04em; }
.subtitle { color: var(--text-muted); margin-top: 6px; font-size: 0.95rem; }

/* ── Breadcrumb ────────────────────────────────── */
.breadcrumb { margin-bottom: 16px; }
.breadcrumb a { font-size: 0.9rem; color: var(--text-muted); }
.breadcrumb a:hover { color: var(--text); }

/* ── Project Cards ─────────────────────────────── */
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; }
.project-card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  transition: box-shadow 0.2s, transform 0.15s;
}
.project-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  transform: translateY(-1px);
}

.card-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 14px; }
.card-title { font-size: 1.2rem; font-weight: 600; color: var(--text); }

.status-badge {
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}
.status-badge.completed { background: #d4edda; color: #2d5a2d; }
.status-badge.in_progress { background: #fff3cd; color: #7a5d00; }
.status-badge.validating { background: #d1ecf1; color: #0c5460; }
.status-badge.planning { background: #e2e3e5; color: #555; }

@media (prefers-color-scheme: dark) {
  .status-badge.completed { background: #1a3a20; color: #7ecb8a; }
  .status-badge.in_progress { background: #3a2e0a; color: #dbc04a; }
  .status-badge.validating { background: #0a2e3a; color: #6cc8e0; }
  .status-badge.planning { background: #333; color: #aaa; }
}

.progress-section { margin-bottom: 12px; }
.progress-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.progress-bar.large { height: 10px; border-radius: 5px; margin: 16px 0 8px; }
.progress-fill { height: 100%; background: var(--accent); border-radius: inherit; transition: width 0.4s ease; }
.progress-text { display: flex; justify-content: space-between; font-size: 0.82rem; color: var(--text-muted); margin-top: 6px; }
.progress-label { font-size: 0.85rem; color: var(--text-muted); text-align: center; }

.card-meta { font-size: 0.8rem; color: var(--text-muted); display: flex; justify-content: space-between; align-items: center; }
.mode-tag {
  font-size: 0.72rem;
  background: var(--border);
  color: var(--text-muted);
  padding: 1px 8px;
  border-radius: 10px;
}

/* ── Empty State ───────────────────────────────── */
.empty-state { text-align: center; padding: 80px 20px; }
.empty-icon { font-size: 3rem; margin-bottom: 16px; }
.empty-state h2 { font-size: 1.4rem; margin-bottom: 8px; }
.empty-state p { color: var(--text-muted); }
.empty-state code { background: var(--surface); border: 1px solid var(--border); padding: 2px 8px; border-radius: 4px; font-size: 0.9rem; }

/* ── Novel Header ──────────────────────────────── */
.novel-header { margin-bottom: 20px; }
.novel-header h1 { font-size: 1.8rem; font-weight: 700; }
.novel-meta { display: flex; gap: 10px; align-items: center; margin-top: 8px; }

/* ── Action Bar ────────────────────────────────── */
.action-bar { margin-bottom: 24px; }
.btn-primary {
  display: inline-block;
  padding: 10px 24px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); color: #fff; }

.prompt-preview {
  margin-top: 14px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.prompt-preview p { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 8px; }
.prompt-preview code {
  display: block;
  padding: 12px;
  background: var(--bg);
  border-radius: 4px;
  font-size: 0.9rem;
  word-break: break-all;
  margin-bottom: 10px;
}
.btn-copy {
  padding: 6px 16px;
  background: var(--border);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text);
}
.btn-copy:hover { background: var(--text-muted); color: #fff; }
.copied-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 0.85rem;
  z-index: 100;
  animation: fadeInOut 2s ease forwards;
}
@keyframes fadeInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(10px); }
  15% { opacity: 1; transform: translateX(-50%) translateY(0); }
  70% { opacity: 1; }
  100% { opacity: 0; }
}

/* ── Tabs ──────────────────────────────────────── */
.tab-nav {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  margin-bottom: 24px;
}
.tab-btn {
  padding: 10px 20px;
  background: none;
  border: none;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Overview Stats ────────────────────────────── */
.overview-stats { display: flex; gap: 16px; margin-bottom: 20px; }
.stat-card {
  flex: 1;
  text-align: center;
  padding: 18px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 0.82rem; color: var(--text-muted); margin-top: 4px; }

/* ── Chapter Table ─────────────────────────────── */
.chapter-table { width: 100%; border-collapse: collapse; }
.chapter-table th { text-align: left; padding: 8px 12px; font-size: 0.8rem; color: var(--text-muted); border-bottom: 1px solid var(--border); }
.chapter-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
.chapter-table a { font-weight: 500; }
.row-done { }
.row-active { background: #fffde8; }
.row-failed { background: #ffe8e4; }
@media (prefers-color-scheme: dark) {
  .row-active { background: #2a2610; }
  .row-failed { background: #2a1410; }
}

/* ── Chapter List (tab view) ───────────────────── */
.chapter-list { display: flex; flex-direction: column; gap: 4px; }
.chapter-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}
.chapter-item:hover { border-color: var(--accent); }
.chapter-item.done { border-left: 3px solid var(--green); }
.chapter-item.active { border-left: 3px solid var(--amber); }
.chapter-item.failed { border-left: 3px solid var(--red); }
.ch-number { font-weight: 600; white-space: nowrap; }
.ch-title { flex: 1; }
.ch-status { font-size: 0.85rem; color: var(--text-muted); white-space: nowrap; }
.ch-read {
  padding: 4px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: 4px;
  font-size: 0.82rem;
  white-space: nowrap;
}
.ch-read:hover { background: var(--accent-hover); color: #fff; }

/* ── Markdown Body ─────────────────────────────── */
.markdown-body {
  font-family: var(--font-serif);
  line-height: 1.85;
  word-break: break-word;
}
.markdown-body h1 { font-size: 1.6rem; margin: 0.8em 0 0.5em; font-family: var(--font-sans); }
.markdown-body h2 { font-size: 1.3rem; margin: 0.8em 0 0.4em; font-family: var(--font-sans); }
.markdown-body h3 { font-size: 1.1rem; margin: 0.6em 0 0.3em; font-family: var(--font-sans); }
.markdown-body p { margin: 0.6em 0; text-indent: 2em; }
.markdown-body p:first-of-type { text-indent: 0; }
.markdown-body ul, .markdown-body ol { padding-left: 1.5em; margin: 0.5em 0; }
.markdown-body li { margin: 0.2em 0; }
.markdown-body table { width: 100%; border-collapse: collapse; margin: 1em 0; font-size: 0.9rem; font-family: var(--font-sans); }
.markdown-body th, .markdown-body td { padding: 8px 12px; border: 1px solid var(--border); text-align: left; }
.markdown-body th { background: var(--surface); font-weight: 600; }
.markdown-body blockquote {
  border-left: 4px solid var(--accent);
  padding: 8px 16px;
  margin: 1em 0;
  background: var(--surface);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-muted);
}
.markdown-body code {
  background: var(--surface);
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 0.88em;
  font-family: "SF Mono", "Fira Code", monospace;
}
.markdown-body pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  overflow-x: auto;
  font-size: 0.85rem;
  margin: 0.8em 0;
}
.markdown-body pre code { background: none; padding: 0; }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
.markdown-body strong { font-weight: 700; }

/* ── Chapter Reader ────────────────────────────── */
.chapter-header { text-align: center; margin-bottom: 32px; padding-bottom: 20px; border-bottom: 1px solid var(--border); }
.chapter-header h1 { font-size: 1.6rem; font-weight: 700; font-family: var(--font-sans); }

.chapter-content {
  position: relative;
  padding: 8px 0 40px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  transition: border-color 0.15s, background 0.15s;
}
.chapter-content.editable {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  background: color-mix(in srgb, var(--surface) 42%, transparent);
}
.edit-toolbar {
  position: sticky;
  top: 8px;
  z-index: 20;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: -4px 0 18px;
  font-family: var(--font-sans);
}
.edit-btn,
.save-btn,
.cancel-btn {
  min-width: 56px;
  padding: 6px 14px;
  border: 1px solid transparent;
  border-radius: 5px;
  font-size: 0.86rem;
  line-height: 1.35;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
}
.edit-btn,
.save-btn {
  background: var(--accent);
  color: #fff;
}
.edit-btn:hover,
.save-btn:hover {
  background: var(--accent-hover);
  color: #fff;
}
.cancel-btn {
  background: var(--surface);
  color: var(--text-muted);
  border-color: var(--border);
}
.cancel-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}
.edit-btn:disabled,
.save-btn:disabled,
.cancel-btn:disabled {
  opacity: 0.62;
  cursor: wait;
}
.chapter-edit-body > [contenteditable],
.chapter-edit-body li[contenteditable],
.chapter-edit-body blockquote[contenteditable] {
  margin-left: -7px;
  margin-right: -7px;
  padding: 2px 6px;
  border: 1px dashed color-mix(in srgb, var(--accent) 42%, var(--border));
  border-radius: 5px;
  background: color-mix(in srgb, var(--surface) 70%, var(--bg));
  transition: background 0.15s, border-color 0.15s, outline-color 0.15s;
}
.chapter-content [contenteditable]:hover {
  border-color: color-mix(in srgb, var(--accent) 68%, var(--border));
  background: color-mix(in srgb, var(--surface) 84%, var(--bg));
}
.chapter-content [contenteditable]:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 58%, transparent);
  outline-offset: 2px;
  border-color: var(--accent);
  background: var(--surface);
}
.copied-toast.error {
  background: var(--red);
  color: #fff;
}

.reading-nav { padding: 12px 0; }
.reading-nav.top { margin-bottom: 8px; }
.reading-nav.bottom { margin-top: 24px; border-top: 1px solid var(--border); }
.reading-nav a { font-size: 0.9rem; color: var(--text-muted); }
.reading-nav a:hover { color: var(--accent); }

.nav-links { display: flex; justify-content: space-between; align-items: center; }
.nav-prev, .nav-next { font-family: var(--font-sans); }
.nav-next { text-align: right; }

/* ── Responsive ────────────────────────────────── */
@media (max-width: 640px) {
  html { font-size: 16px; }
  .project-grid { grid-template-columns: 1fr; }
  .overview-stats { flex-direction: column; }
  .chapter-item { flex-wrap: wrap; }
  .ch-read { margin-left: auto; }
  .tab-nav { overflow-x: auto; }
  .tab-btn { padding: 10px 14px; font-size: 0.85rem; white-space: nowrap; }
}

/* ── Novel Claude AI ───────────────────────────── */
.ncai-entry-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin: 0 0 28px;
  padding: 22px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  color: var(--text);
  transition: box-shadow 0.2s, transform 0.15s, border-color 0.15s;
}
.ncai-entry-card:hover {
  color: var(--text);
  border-color: var(--accent);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  transform: translateY(-1px);
}
.ncai-entry-card h2 {
  margin: 8px 0 4px;
  font-size: 1.2rem;
  line-height: 1.35;
}
.ncai-entry-card p {
  color: var(--text-muted);
  font-size: 0.92rem;
}
.ncai-entry-arrow {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 1.25rem;
}

.ncai-page-header { padding-top: 18px; }
.ncai-project-grid { margin-top: 4px; }
.ncai-project-card .card-title { line-height: 1.35; }
.ncai-card-current {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 42px;
  margin: 12px 0;
  color: var(--text);
  font-size: 0.9rem;
}
.ncai-card-current span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ncai-novel-header h1 { line-height: 1.25; }
.ncai-workspace {
  max-width: 1180px;
}
.ncai-tab-nav {
  overflow-x: auto;
}
.ncai-stats .stat-value {
  word-break: break-word;
  line-height: 1.2;
}
.ncai-volume-value {
  font-size: 1rem;
  color: var(--text);
}
.ncai-overview-section {
  margin-top: 28px;
  padding-top: 22px;
  border-top: 1px solid var(--border);
}
.ncai-overview-section h2 {
  margin-bottom: 10px;
  font-size: 1.1rem;
}
.ncai-muted { color: var(--text-muted); }
.ncai-asset-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
}
.ncai-asset-grid div {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.ncai-asset-grid strong {
  color: var(--accent);
  font-size: 1.35rem;
  line-height: 1.1;
}
.ncai-asset-grid span {
  color: var(--text-muted);
  font-size: 0.78rem;
}
.ncai-doc-stack {
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.ncai-doc-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.ncai-doc-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 78%, var(--bg));
}
.ncai-doc-header h2 {
  font-size: 1rem;
  line-height: 1.35;
}
.ncai-doc-header p {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 0.76rem;
  word-break: break-all;
}
.ncai-doc-body {
  padding: 14px 18px 18px;
}
.ncai-raw-block {
  margin: 0;
  padding: 16px 18px;
  max-height: 620px;
  overflow: auto;
  background: var(--bg);
  color: var(--text);
  border: none;
  font-size: 0.78rem;
  line-height: 1.55;
}
.ncai-raw-block code {
  font-family: "SF Mono", "Fira Code", Consolas, monospace;
  white-space: pre;
}
.ncai-file-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ncai-file-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
}
.ncai-file-row span {
  min-width: 0;
  word-break: break-all;
  font-family: "SF Mono", "Fira Code", Consolas, monospace;
  font-size: 0.82rem;
}
.ncai-file-row small {
  flex: 0 0 auto;
  color: var(--text-muted);
  font-size: 0.76rem;
}
.ncai-chapter-list .chapter-item {
  align-items: flex-start;
}
.ncai-chapter-list .chapter-item.pending {
  opacity: 0.72;
}
.ncai-chapter-list .ch-title {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.ncai-chapter-list .ch-title small {
  display: -webkit-box;
  overflow: hidden;
  color: var(--text-muted);
  font-size: 0.78rem;
  line-height: 1.45;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

@media (max-width: 760px) {
  .ncai-entry-card {
    align-items: flex-start;
    padding: 18px;
  }
  .ncai-entry-arrow {
    width: 30px;
    height: 30px;
    font-size: 1rem;
  }
  .ncai-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .ncai-asset-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .ncai-file-row {
    flex-direction: column;
    gap: 2px;
  }
  .ncai-chapter-list .ch-status,
  .ncai-chapter-list .ch-read {
    margin-left: auto;
  }
}

/* ── Dashboard Cards ────────────────────────────── */
.ncai-dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  margin: 20px 0 0;
}
.ncai-dash-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.ncai-dash-card-title {
  padding: 12px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 78%, var(--bg));
}
.ncai-dash-card-body {
  padding: 14px 18px;
  flex: 1;
}
.ncai-dash-card-body .markdown-body {
  font-size: 0.9rem;
}
.ncai-dash-card-body .markdown-body p {
  text-indent: 0;
  margin: 0.3em 0;
}

/* Key-value list */
.ncai-kv-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  font-size: 0.9rem;
}
.ncai-kv-list dt {
  color: var(--text-muted);
  font-weight: 500;
}
.ncai-kv-list dd {
  color: var(--text);
}

/* Anchor items */
.ncai-anchor-item {
  margin-bottom: 10px;
  font-size: 0.88rem;
  line-height: 1.5;
}
.ncai-anchor-item:last-child { margin-bottom: 0; }
.ncai-anchor-item strong {
  color: var(--text-muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.ncai-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.76rem;
  margin: 2px 4px 2px 0;
}
.ncai-tag-forbidden {
  background: #ffe8e4;
  color: var(--red);
  border: 1px solid var(--red);
}
@media (prefers-color-scheme: dark) {
  .ncai-tag-forbidden {
    background: #2a1410;
  }
}

/* Summary body */
.ncai-summary-body {
  max-height: 500px;
  overflow-y: auto;
  padding: 4px 0;
}

/* Quick action buttons */
.ncai-quick-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.ncai-action-btn {
  padding: 8px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.ncai-action-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ── Accordion ──────────────────────────────────── */
.ncai-section-group-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  padding: 20px 0 10px;
  margin: 0;
  border-bottom: 1px solid var(--border);
}
.ncai-section-group-title:first-child {
  padding-top: 0;
}

.ncai-accordion-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 24px;
}
.ncai-accordion {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.ncai-accordion[open] {
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
}

.ncai-accordion-summary {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  user-select: none;
  list-style: none;
  transition: background 0.15s;
}
.ncai-accordion-summary:hover {
  background: color-mix(in srgb, var(--surface) 90%, var(--accent));
}
.ncai-accordion-summary::-webkit-details-marker {
  display: none;
}
.ncai-accordion-summary::after {
  content: "▸";
  flex-shrink: 0;
  align-self: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  transition: transform 0.2s;
}
.ncai-accordion[open] > .ncai-accordion-summary::after {
  transform: rotate(90deg);
}

.ncai-accordion-summary-left {
  flex: 1;
  min-width: 0;
}
.ncai-accordion-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  display: block;
}
.ncai-accordion-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  display: block;
  margin-top: 2px;
}
.ncai-accordion-preview {
  flex: 0 1 360px;
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.45;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: none;
}
@media (min-width: 760px) {
  .ncai-accordion-preview {
    display: block;
  }
}

.ncai-accordion-body {
  padding: 0 16px 16px;
  border-top: 1px solid var(--border);
}
.ncai-accordion-body .markdown-body {
  font-size: 0.92rem;
  padding-top: 12px;
}

/* ── Chat Bubble Widget ─────────────────────────── */
#ncai-chat-root {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 9999;
  font-family: var(--font-sans);
  font-size: 0.9rem;
  pointer-events: none;
  color-scheme: light;
}
body.ncai-chat-open {
  overflow: hidden;
}

.ncai-chat-bubble {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0,0,0,0.18);
  transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
  z-index: 10000;
  pointer-events: auto;
  touch-action: manipulation;
}
.ncai-chat-bubble:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 24px rgba(0,0,0,0.24);
}
.ncai-chat-bubble.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
}
.ncai-chat-bubble svg {
  color: #fff;
}

/* ── Chat Panel ─────────────────────────────────── */
.ncai-chat-panel {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  max-height: none;
  background: var(--surface);
  color: var(--text);
  border: none;
  border-radius: 0;
  box-shadow: 0 8px 32px rgba(0,0,0,0.14);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: contain;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 10001;
}
.ncai-chat-panel.open {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.ncai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(14px, env(safe-area-inset-top)) 18px 14px;
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
}
.ncai-chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.ncai-chat-logo {
  font-size: 1.5rem;
}
.ncai-chat-title {
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.2;
}
.ncai-chat-subtitle {
  font-size: 0.75rem;
  opacity: 0.82;
}
.ncai-chat-close {
  background: rgba(255,255,255,0.18);
  border: none;
  color: #fff;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  transition: background 0.15s;
  touch-action: manipulation;
}
.ncai-chat-close:hover {
  background: rgba(255,255,255,0.3);
}

/* ── Presets ────────────────────────────────────── */
.ncai-chat-presets {
  display: flex;
  gap: 6px;
  padding: 10px 14px;
  overflow-x: auto;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  scrollbar-width: thin;
}
.ncai-chat-presets::-webkit-scrollbar {
  height: 3px;
}
.ncai-chat-presets::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
.ncai-preset-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 36px;
  padding: 7px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  font-size: 0.78rem;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
  touch-action: manipulation;
}
.ncai-preset-chip:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.ncai-preset-icon {
  font-size: 0.85rem;
}

/* ── Messages ───────────────────────────────────── */
.ncai-chat-messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
.ncai-chat-welcome {
  text-align: center;
  padding: 20px 8px;
  color: var(--text-muted);
  margin: auto 0;
}
.ncai-welcome-icon {
  font-size: 2.5rem;
  margin-bottom: 10px;
}
.ncai-chat-welcome p {
  margin: 6px 0;
  font-size: 0.88rem;
  line-height: 1.55;
}
.ncai-context-hint {
  margin-top: 10px;
}
.ncai-context-hint code {
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.82rem;
}

.ncai-msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.88rem;
  line-height: 1.55;
  word-break: break-word;
  animation: ncaiMsgIn 0.2s ease;
}
@keyframes ncaiMsgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.ncai-msg-user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.ncai-msg-assistant {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  color: var(--text);
}
.ncai-msg-assistant h1,
.ncai-msg-assistant h2,
.ncai-msg-assistant h3 {
  font-size: 0.95rem;
  margin: 6px 0 3px;
  font-family: var(--font-sans);
  color: var(--accent);
}
.ncai-msg-assistant p { margin: 4px 0; }
.ncai-msg-assistant ul,
.ncai-msg-assistant ol {
  padding-left: 1.3em;
  margin: 4px 0;
}
.ncai-msg-assistant li { margin: 2px 0; }
.ncai-msg-assistant code {
  background: var(--surface);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.85em;
}
.ncai-msg-assistant strong { font-weight: 600; }

.ncai-msg-streaming .ncai-typing-cursor::after {
  content: "▊";
  animation: ncaiBlink 0.8s infinite;
  color: var(--accent);
}
@keyframes ncaiBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ── Input Area ─────────────────────────────────── */
.ncai-chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.ncai-chat-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
  appearance: none;
  -webkit-appearance: none;
  outline: none;
  max-height: 120px;
  transition: border-color 0.15s;
}
.ncai-chat-input::placeholder {
  color: var(--text-muted);
}
.ncai-chat-input:focus {
  border-color: var(--accent);
}
.ncai-chat-send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
  touch-action: manipulation;
}
.ncai-chat-send:hover {
  background: var(--accent-hover);
}
.ncai-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.ncai-chat-send.streaming {
  background: var(--red);
  animation: ncaiPulse 1s infinite;
}
@keyframes ncaiPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

@media (prefers-color-scheme: dark) {
  #ncai-chat-root {
    color-scheme: dark;
  }
  .ncai-chat-bubble,
  .ncai-chat-send {
    background: #c96747;
    box-shadow: 0 8px 24px rgba(0,0,0,0.34);
  }
  .ncai-chat-bubble:hover,
  .ncai-chat-send:hover {
    background: #dc7959;
  }
  .ncai-chat-panel {
    background: #171615;
    color: #eee7dc;
    box-shadow: none;
  }
  .ncai-chat-header {
    background: #211d1a;
    border-bottom: 1px solid #3b332d;
  }
  .ncai-chat-close {
    background: rgba(255,255,255,0.12);
    color: #fff7ef;
  }
  .ncai-chat-close:hover {
    background: rgba(255,255,255,0.2);
  }
  .ncai-chat-presets,
  .ncai-chat-input-area {
    background: #1d1b19;
    border-color: #332d28;
  }
  .ncai-chat-presets::-webkit-scrollbar-thumb {
    background: #3b332d;
  }
  .ncai-preset-chip {
    background: #27231f;
    border-color: #3b332d;
    color: #e9dfd2;
  }
  .ncai-preset-chip:hover {
    background: #c96747;
    border-color: #c96747;
    color: #fffaf6;
  }
  .ncai-chat-welcome,
  .ncai-chat-subtitle {
    color: #a89d91;
  }
  .ncai-context-hint code,
  .ncai-msg-assistant code {
    background: #302a25;
    color: #f1e8dc;
  }
  .ncai-msg-user {
    background: #b95f41;
    color: #fffaf6;
  }
  .ncai-msg-assistant {
    background: #24211e;
    border-color: #3b332d;
    color: #eee7dc;
  }
  .ncai-msg-assistant h1,
  .ncai-msg-assistant h2,
  .ncai-msg-assistant h3,
  .ncai-msg-streaming .ncai-typing-cursor::after {
    color: #e08a62;
  }
  .ncai-chat-input {
    background: #24211e;
    border-color: #3b332d;
    color: #f3eadf;
    caret-color: #e08a62;
  }
  .ncai-chat-input::placeholder {
    color: #8f857b;
  }
  .ncai-chat-input:focus {
    border-color: #d4744a;
  }
  .ncai-chat-send:disabled {
    background: #6f4436;
  }
  .ncai-chat-send.streaming {
    background: #c45545;
  }
}

/* ── Responsive ─────────────────────────────────── */
@media (max-width: 480px) {
  .ncai-chat-bubble {
    right: 14px;
    bottom: 18px;
    width: 48px;
    height: 48px;
  }
  .ncai-entry-card {
    flex-direction: column;
  }
  .ncai-entry-arrow {
    align-self: flex-end;
  }
  .ncai-stats {
    grid-template-columns: 1fr;
  }
  .ncai-asset-grid {
    grid-template-columns: 1fr;
  }
}
