:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-soft: #f2f4f7;
  --ink: #1f2329;
  --ink-2: #3d434d;
  --ink-3: #565d68;
  --muted: #8d939e;
  --muted-2: #a8adb7;
  --line: #e8eaef;
  --line-strong: #dfe2e9;
  --brand: #2e8b5e;
  --brand-dark: #24714c;
  --brand-soft: #e9f5ee;
  --accent: #3b82f6;
  --accent-dark: #2f6fe0;
  --accent-soft: #eaf2ff;
  --bubble-bot: #f2f3f5;
  --bubble-user: #e8f0fe;
  --danger: #d4534a;
  --amber: #bf8737;
  --radius-card: 18px;
  --shadow-soft: 0 1px 2px rgba(16, 24, 40, .04);
  --shadow-card: 0 1px 2px rgba(16, 24, 40, .03), 0 10px 28px rgba(16, 24, 40, .05);
  --font: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-width: 320px;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection { background: #d8e6ff; }

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
[hidden] { display: none !important; }

/* ---------- Header ---------- */

.app-header {
  flex: 0 0 auto;
  height: 62px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: rgba(255, 255, 255, .92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: saturate(1.2) blur(10px);
  z-index: 20;
}

.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; display: block; }
.brand-name { font-size: 17px; font-weight: 700; letter-spacing: .01em; }

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

.model-status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 220px;
  margin-right: 4px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.model-status .model-label { overflow: hidden; text-overflow: ellipsis; }
.status-dot { width: 7px; height: 7px; flex: 0 0 auto; border-radius: 50%; background: var(--amber); box-shadow: 0 0 0 3px rgba(191, 135, 55, .14); }
.model-status.is-ready { color: var(--brand-dark); }
.model-status.is-ready .status-dot { background: #34a853; box-shadow: 0 0 0 3px rgba(52, 168, 83, .15); }
.model-status.is-error { color: var(--danger); }
.model-status.is-error .status-dot { background: var(--danger); box-shadow: 0 0 0 3px rgba(212, 83, 74, .14); }

.icon-button {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-3);
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.icon-button svg { width: 18px; height: 18px; }
.icon-button:hover { background: var(--surface-soft); color: var(--ink); }
.icon-button:disabled { opacity: .35; cursor: default; }
.doc-action:disabled { display: none; }

.library-button {
  height: 36px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 560;
  box-shadow: var(--shadow-soft);
  transition: background .15s ease, border-color .15s ease;
}
.library-button svg { width: 16px; height: 16px; color: var(--muted); }
.library-button:hover { background: var(--surface-soft); border-color: #d3d7df; }

/* ---------- Chat shell ---------- */

.app-main { flex: 1 1 auto; min-height: 0; display: flex; justify-content: center; }

.chat-shell {
  width: 100%;
  max-width: 880px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0;
}

.control-row {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px 10px;
}

.control-chips { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; min-width: 0; }

.category-tabs { display: flex; align-items: center; gap: 8px; min-width: 0; }
.category-tab {
  height: 38px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-3);
  font-size: 13.5px;
  white-space: nowrap;
  box-shadow: var(--shadow-soft);
  transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.category-tab svg { width: 16px; height: 16px; flex: 0 0 auto; color: #7b8290; transition: color .15s ease; }
.category-tab:hover { border-color: #d3d7df; background: #fdfdfe; }
.category-tab.is-active { background: var(--brand-soft); border-color: #cfe7da; color: var(--brand-dark); }
.category-tab.is-active svg { color: var(--brand); }
.category-tab[data-category="liaozhai"].is-active { background: #eef1fb; border-color: #d7ddf1; color: #566796; }
.category-tab[data-category="liaozhai"].is-active svg { color: #7787b9; }
.category-tabs.is-locked .category-tab:not(.is-active) { display: none; }
.category-tabs.is-locked .category-tab.is-active { pointer-events: none; }

.chip {
  height: 38px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-3);
  font-size: 13.5px;
  white-space: nowrap;
  box-shadow: var(--shadow-soft);
  transition: background .15s ease, border-color .15s ease, color .15s ease, box-shadow .15s ease;
}
.chip:hover { border-color: #d3d7df; background: #fdfdfe; }
.chip-icon { width: 16px; height: 16px; flex: 0 0 auto; color: #7b8290; }

.chip-field { padding: 0 10px 0 13px; cursor: pointer; }
.chip-category .chip-icon-moon { display: none; }
.chip-category.is-liaozhai .chip-icon-leaf { display: none; }
.chip-category.is-liaozhai .chip-icon-moon { display: inline-block; color: #7787b9; }
.chip-field select {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  outline: none;
  background: transparent;
  padding: 0 2px;
  margin: 0;
  max-width: 118px;
  color: inherit;
  font-size: inherit;
  cursor: pointer;
}
.chip-field select:focus-visible { outline: none; }
.chip-chevron { width: 13px; height: 13px; color: var(--muted-2); flex: 0 0 auto; }
.chip-icon-moon { color: #7787b9; }

.control-actions { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }

.text-button {
  min-height: 34px;
  padding: 0 11px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 12.5px;
  transition: background .15s ease, color .15s ease;
}
.text-button:hover:not(:disabled) { background: var(--surface-soft); color: var(--ink-2); }
.text-button:not(:disabled) { color: var(--accent-dark); }
.text-button:disabled { opacity: .45; cursor: default; }
.text-button[hidden] { display: none; }

.new-story-button {
  height: 38px;
  margin-left: 6px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid #cfe0ff;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 13.5px;
  font-weight: 600;
  transition: background .15s ease, border-color .15s ease;
}
.new-story-button svg { width: 15px; height: 15px; }
.new-story-button:hover { background: #ddebff; border-color: #b8d2ff; }

/* ---------- Messages ---------- */

.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 24px 8px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: #d8dbe2 transparent;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-thumb { background: #d8dbe2; border-radius: 99px; }

.msg {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin: 0 0 22px;
  animation: msg-in .18s ease both;
}
@keyframes msg-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.msg-avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 10px;
  display: block;
  user-select: none;
  margin-top: 2px;
}

.msg-body { display: flex; flex-direction: column; align-items: flex-start; min-width: 0; max-width: min(100%, 720px); }

.bubble {
  max-width: 100%;
  padding: 13px 18px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.8;
  overflow-wrap: anywhere;
}
.bubble p { margin: 0; }
.bubble p + p { margin-top: 6px; }

.bubble-bot { background: var(--bubble-bot); color: var(--ink-2); border-top-left-radius: 6px; }
.bubble-user { background: var(--bubble-user); color: var(--ink); border-top-right-radius: 6px; }

.msg-user { justify-content: flex-end; }
.msg-user .msg-body { align-items: flex-end; max-width: min(100%, 560px); }
.msg-user .bubble-user { white-space: pre-wrap; }

.msg-time { margin-top: 7px; padding: 0 4px; color: var(--muted-2); font-size: 11.5px; letter-spacing: .01em; }

.greeting-bubble { padding: 16px 20px; }
.greeting-bubble .text-link { margin-top: 10px; }

.text-link {
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--accent-dark);
  font-size: 13.5px;
  font-weight: 550;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: #b8d0f7;
}
.text-link:hover { color: #1d5bd0; }

/* result card */

.result-message { margin-bottom: 26px; }
.result-message .msg-body { flex: 1; width: 100%; max-width: 100%; }

.result-message .result-bubble {
  width: 100%;
  padding: 22px 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  background: var(--surface);
  box-shadow: var(--shadow-card);
  color: var(--ink);
  border-top-left-radius: var(--radius-card);
}

.result-intro { display: flex; align-items: center; gap: 8px; min-width: 0; color: var(--ink-2); font-size: 14px; font-weight: 600; }
.result-spark { color: #8aa9d8; font-size: 12px; }
.result-intro > span:not(.result-spark):not(.result-intro-note) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-intro-note { margin-left: auto; flex: 0 0 auto; color: var(--muted-2); font-size: 11.5px; font-weight: 400; }

.code-block { border: 1px solid var(--line); border-radius: 14px; background: #fbfcfd; overflow: hidden; }
.code-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 8px 7px 14px;
  background: #f4f6f9;
  border-bottom: 1px solid var(--line);
}
.code-block-head > span { color: var(--ink-3); font-size: 12px; font-weight: 600; letter-spacing: .02em; }
.code-pre {
  margin: 0;
  padding: 14px 16px 16px;
  background: transparent;
  color: var(--ink-2);
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 12.8px;
  line-height: 1.78;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 520px;
  overflow: auto;
}
.code-pre::-webkit-scrollbar { width: 6px; height: 6px; }
.code-pre::-webkit-scrollbar-thumb { background: #d8dbe2; border-radius: 99px; }

.result-document { display: flex; flex-direction: column; gap: 16px; }

.result-title-label { display: block; }
.result-title-input {
  width: 100%;
  padding: 2px 0 10px;
  border: 0;
  border-bottom: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font-size: 21px;
  font-weight: 700;
  line-height: 1.4;
  transition: border-color .15s ease;
}
.result-title-input:hover { border-bottom-color: var(--line); }
.result-title-input:focus { outline: none; border-bottom-color: var(--accent); }

.result-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.result-field > span,
.prompt-field > span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 550;
  letter-spacing: .02em;
}
.result-field textarea,
.prompt-field textarea,
.character-meta textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-2);
  line-height: 1.85;
  resize: none;
  transition: border-color .15s ease, background .15s ease;
}
.result-field textarea:hover,
.prompt-field textarea:hover,
.character-meta textarea:hover { background: #f9fafc; }
.result-field textarea:focus,
.prompt-field textarea:focus,
.character-meta textarea:focus { outline: none; border-color: #bcd4fb; background: #fff; }
.result-field textarea { min-height: 120px; font-size: 15px; }

/* story code block */
.story-block {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fbfcfd;
  overflow: hidden;
}
.story-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px 8px 14px;
  background: #f4f6f9;
  border-bottom: 1px solid var(--line);
}
.story-block-label { color: var(--muted); font-size: 12px; font-weight: 550; letter-spacing: .02em; }
.story-block-actions { display: flex; align-items: center; gap: 2px; }
.mini-button {
  height: 26px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--ink-3);
  font-size: 12px;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.mini-button:hover { background: #fff; border-color: var(--line-strong); color: var(--ink); }
.story-code {
  margin: 0;
  padding: 15px 16px 17px;
  background: transparent;
  color: var(--ink-2);
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 13.5px;
  line-height: 1.9;
  white-space: pre-wrap;
  word-break: break-word;
}
.story-editor {
  display: block;
  width: 100%;
  min-height: 220px;
  padding: 14px 16px;
  border: 0;
  background: #fff;
  color: var(--ink-2);
  font-size: 15px;
  line-height: 1.9;
  resize: none;
}
.story-editor:focus { outline: none; }
.story-editor[hidden] { display: none; }
.story-code[hidden] { display: none; }

.inline-title {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  font-weight: 650;
  transition: border-color .15s ease, background .15s ease;
}
.inline-title:hover { background: rgba(255, 255, 255, .7); }
.inline-title:focus { outline: none; border-color: #bcd4fb; background: #fff; }

.result-section { margin-top: 2px; }
.result-section > summary {
  min-height: 38px;
  display: flex;
  align-items: center;
  gap: 9px;
  border-radius: 10px;
  list-style: none;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 600;
  user-select: none;
}
.result-section > summary::-webkit-details-marker { display: none; }
.result-section > summary:hover { color: var(--ink); }
.section-index {
  width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  border-radius: 7px;
  font-size: 11px;
  font-weight: 700;
}
.index-amber { background: #fdf3e3; color: #9a712e; }
.index-blue { background: #e9f0fd; color: #3d6ec0; }
.section-aside { margin-left: auto; color: var(--muted-2); font-size: 12px; font-weight: 400; }

.result-section-body {
  margin-top: 8px;
  padding: 6px 16px;
  border-radius: 14px;
  background: #f8f9fb;
}
.result-section-body > p.section-aside { padding: 10px 0; }

.character-row {
  display: grid;
  grid-template-columns: minmax(140px, .4fr) 1fr;
  gap: 14px;
  padding: 14px 0;
}
.character-row + .character-row { border-top: 1px solid #edf0f3; }
.character-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.character-meta textarea { min-height: 74px; font-size: 13px; line-height: 1.7; }
.character-row .prompt-field textarea { min-height: 74px; font-size: 12.5px; line-height: 1.7; }
.prompt-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.prompt-field textarea {
  border-color: #eceef2;
  background: #fff;
  font-size: 12.5px;
  line-height: 1.7;
}

.scene-row {
  position: relative;
  padding: 16px 0 16px 38px;
}
.scene-row + .scene-row { border-top: 1px solid #eef0f3; }
.scene-number {
  position: absolute;
  left: 4px;
  top: 16px;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #e3e7ee;
  color: #6c7684;
  font-size: 10.5px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.scene-title { margin-bottom: 8px; padding-left: 2px; }
.scene-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.scene-fields textarea { min-height: 84px; }
.scene-meta { grid-column: 1 / -1; display: grid; grid-template-columns: 150px 1fr; gap: 10px 12px; }
.scene-meta input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #eceef2;
  border-radius: 9px;
  background: #fff;
  color: var(--ink-2);
  font-size: 12.5px;
  line-height: 1.6;
}
.scene-meta input:focus { outline: none; border-color: #bcd4fb; background: #fff; }
.scene-meta .prompt-field > span { color: var(--muted); }
.scene-narration { grid-column: 1 / -1; }
.scene-narration textarea, .scene-visual textarea { min-height: 62px; }

/* archived / read-only */

.archived-bubble { padding-bottom: 10px; }
.archived-version { border-top: 1px solid var(--line); margin-top: 12px; }
.archived-version > summary {
  padding: 10px 0;
  cursor: pointer;
  color: var(--brand-dark);
  font-size: 13.5px;
  font-weight: 600;
}
.archived-copy { padding-bottom: 10px; }
.archived-story { margin: 2px 0 8px; white-space: pre-wrap; color: var(--ink-2); font-size: 14px; line-height: 1.85; }
.archived-moral { margin: 0 0 10px; color: #8a6d3b; font-size: 13px; }
.archived-copy pre {
  max-height: 220px;
  overflow: auto;
  margin: 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: #f8f9fb;
  color: #5d6673;
  white-space: pre-wrap;
  font: 12px/1.7 var(--font);
}

/* thinking */

.thinking-bubble { width: 76px; padding: 17px 18px; display: flex; align-items: center; gap: 6px; }
.thinking-bubble span { width: 7px; height: 7px; border-radius: 50%; background: #b0b7c3; animation: thinking-pulse 1.1s infinite ease-in-out; }
.thinking-bubble span:nth-child(2) { animation-delay: .14s; }
.thinking-bubble span:nth-child(3) { animation-delay: .28s; }
@keyframes thinking-pulse { 0%, 70%, 100% { opacity: .35; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-3px); } }

/* ---------- Composer ---------- */

.chat-form {
  flex: 0 0 auto;
  padding: 10px 24px calc(16px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(246, 247, 249, 0) 0%, rgba(246, 247, 249, .92) 38%, var(--bg) 72%);
}

.composer-notes {
  margin: 0 0 6px;
  padding: 2px 4px 10px 4px;
  border-bottom: 1px solid var(--line);
}
.composer-notes label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 12px; font-weight: 600; }
.composer-notes textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: #f7f8fa;
  font-size: 13px;
  line-height: 1.6;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
}
.composer-notes textarea:focus { outline: none; border-color: #bcd4fb; background: #fff; }

.composer-box {
  max-width: 100%;
  padding: 10px 12px 10px 14px;
  border: 1px solid #e3e6ec;
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 0 2px 6px rgba(16, 24, 40, .03), 0 12px 32px rgba(16, 24, 40, .06);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.composer-box:focus-within {
  border-color: #c9d8f4;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, .08), 0 12px 32px rgba(16, 24, 40, .07);
}

.composer-box > textarea {
  display: block;
  width: 100%;
  min-height: 26px;
  max-height: 168px;
  padding: 6px 4px;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
  overflow-y: auto;
}
.composer-box > textarea::placeholder { color: var(--muted-2); }

.composer-footer { display: flex; align-items: center; gap: 10px; }
.composer-plus {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 1px solid #dbe7fb;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-dark);
  transition: background .15s ease, transform .15s ease;
}
.composer-plus svg { width: 16px; height: 16px; }
.composer-plus:hover { background: #ddebff; }
.composer-plus.is-active { background: var(--accent); border-color: var(--accent); color: #fff; transform: rotate(45deg); }

.composer-hint { color: var(--muted-2); font-size: 12px; }
.composer-spacer { flex: 1; }

.send-button {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  transition: background .15s ease, transform .15s ease;
}
.send-button svg { width: 18px; height: 18px; }
.send-button:hover:not(:disabled) { background: var(--accent-dark); }
.send-button:active:not(:disabled) { transform: scale(.96); }
.send-button:disabled { opacity: .45; cursor: wait; }

.composer-error { margin: 8px 4px 0; color: var(--danger); font-size: 12.5px; }

/* ---------- Library drawer ---------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(15, 23, 42, .3);
  backdrop-filter: blur(3px);
  animation: fade-in .2s ease both;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.library-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  width: min(384px, 92vw);
  padding: 22px 20px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  background: var(--surface);
  box-shadow: -24px 0 64px rgba(16, 24, 40, .14);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform .28s cubic-bezier(.22, .61, .36, 1), visibility .28s;
}
.library-panel.is-open { transform: none; visibility: visible; }

.library-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.library-title { display: flex; align-items: center; gap: 10px; }
.library-title h2 { margin: 0; font-size: 19px; font-weight: 700; }
.library-scope {
  margin-left: auto;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 11.5px;
  font-weight: 500;
  white-space: nowrap;
}
.record-count {
  min-width: 26px;
  height: 22px;
  padding: 0 8px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  background: var(--surface-soft);
  color: #6b7280;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.library-tabs {
  margin-top: 18px;
  padding: 3px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  border-radius: 12px;
  background: var(--surface-soft);
}
.library-tab {
  height: 34px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
  transition: background .15s ease, color .15s ease;
}
.library-tab.is-active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-soft); }

.search-field {
  height: 40px;
  margin-top: 12px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: #f5f6f8;
  color: var(--muted-2);
  transition: border-color .15s ease, background .15s ease;
}
.search-field:focus-within { background: var(--surface); border-color: #c9d8f4; box-shadow: 0 0 0 3px rgba(59, 130, 246, .08); }
.search-field svg { width: 16px; height: 16px; flex: 0 0 auto; }
.search-field input { flex: 1; min-width: 0; height: 100%; padding: 0; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 13px; }
.search-field input::placeholder { color: var(--muted-2); }

.record-list-wrap { position: relative; flex: 1; min-height: 200px; margin: 12px -8px 0; overflow-y: auto; scrollbar-width: thin; scrollbar-color: #d8dbe2 transparent; }
.record-list { list-style: none; margin: 0; padding: 0 8px; }
.record-item {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 30px;
  align-items: center;
  gap: 4px;
  padding: 11px 10px;
  border-radius: 12px;
  transition: background .12s ease;
}
.record-item:hover { background: #f7f8fa; }
.record-open { min-width: 0; padding: 0; border: 0; background: transparent; text-align: left; }
.record-title { display: block; overflow: hidden; color: var(--ink-2); font-size: 13.5px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.record-item:hover .record-title { color: var(--ink); }
.record-meta { display: flex; align-items: center; gap: 6px; margin-top: 3px; color: var(--muted-2); font-size: 11.5px; }
.record-category { color: var(--brand); }
.record-category.liaozhai { color: #6d7cb5; }
.record-star { color: #d9a13e; }
.record-delete {
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted-2);
  font-size: 17px;
  line-height: 1;
  opacity: 0;
  transition: opacity .12s ease, background .12s ease, color .12s ease;
}
.record-item:hover .record-delete, .record-delete:focus-visible { opacity: 1; }
.record-delete:hover { background: #fdecea; color: var(--danger); }

.library-empty {
  position: absolute;
  top: 44%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  color: var(--muted-2);
}
.library-empty-mark { font-size: 22px; letter-spacing: 4px; line-height: 1; }
.library-empty p { margin: 8px 0 0; font-size: 12.5px; }

.library-footer { padding-top: 12px; color: var(--muted-2); font-size: 11px; text-align: center; }

/* ---------- Dialogs ---------- */

dialog {
  border: 0;
  padding: 0;
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: rgba(15, 23, 42, .4); backdrop-filter: blur(4px); }

.settings-dialog {
  width: min(470px, calc(100vw - 32px));
  max-height: min(760px, calc(100dvh - 32px));
  overflow: auto;
  border-radius: 20px;
  box-shadow: 0 24px 80px rgba(15, 23, 42, .24);
}
.settings-dialog form { padding: 24px 24px 20px; }

.dialog-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.dialog-header h2 { margin: 0; font-size: 19px; font-weight: 700; }
.dialog-subtitle { margin: 5px 0 0; color: var(--muted); font-size: 12.5px; }

.settings-field { display: flex; flex-direction: column; gap: 6px; margin: 16px 0 0; }
.settings-field > span { display: flex; align-items: center; justify-content: space-between; color: var(--ink-3); font-size: 12.5px; font-weight: 600; }
.settings-field em { color: var(--muted-2); font-size: 11.5px; font-style: normal; font-weight: 400; }
.settings-field input {
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: #fbfcfd;
  font-size: 13px;
  transition: border-color .15s ease, background .15s ease, box-shadow .15s ease;
}
.settings-field input:focus { outline: none; background: #fff; border-color: #bcd4fb; box-shadow: 0 0 0 3px rgba(59, 130, 246, .08); }

.settings-security {
  margin: 16px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: #f5f7fa;
  color: #6b7280;
  font-size: 11.5px;
  line-height: 1.65;
}
.settings-security code { padding: 1px 4px; border-radius: 4px; background: #e9edf3; font-size: 11px; }
.settings-error { margin: 12px 0 0; color: var(--danger); font-size: 12.5px; }

.dialog-actions { display: flex; align-items: center; gap: 8px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.dialog-spacer { flex: 1; }

.ghost-button, .primary-button {
  height: 38px;
  padding: 0 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  transition: background .15s ease, border-color .15s ease;
}
.ghost-button { border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink-3); }
.ghost-button:hover { background: var(--surface-soft); }
.primary-button { border: 1px solid var(--accent); background: var(--accent); color: #fff; }
.primary-button:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.primary-button.danger, .button-danger { border-color: var(--danger); background: var(--danger); color: #fff; }
.primary-button.danger:hover { background: #b6433b; border-color: #b6433b; }
.danger-text { color: var(--danger); }
.danger-text:hover:not(:disabled) { background: #fdecea; color: var(--danger); }

.confirm-dialog {
  width: min(400px, calc(100vw - 32px));
  border-radius: 20px;
  box-shadow: 0 24px 80px rgba(15, 23, 42, .28);
}
.confirm-dialog-content { padding: 24px; }
.confirm-warning-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: #fdecea;
  color: var(--danger);
  font-weight: 800;
}
.confirm-dialog h2 { margin: 14px 0 6px; font-size: 18px; font-weight: 700; }
.confirm-dialog-content > p:not(.settings-error) { margin: 0; color: #6b7280; font-size: 13px; line-height: 1.7; }
.confirm-dialog .dialog-actions { justify-content: flex-end; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  z-index: 80;
  left: 50%;
  bottom: 30px;
  transform: translate(-50%, 12px);
  max-width: min(520px, calc(100vw - 32px));
  padding: 10px 18px;
  border-radius: 999px;
  background: #1f2329;
  color: #fff;
  font-size: 13px;
  box-shadow: 0 12px 36px rgba(15, 23, 42, .2);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.toast.is-visible { transform: translate(-50%, 0); opacity: 1; }
.toast.is-error { background: #b6433b; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .control-row { padding: 14px 16px 8px; flex-wrap: wrap; }
  .control-actions { margin-left: auto; }
  .chat-messages { padding: 8px 16px 6px; }
  .chat-form { padding: 8px 16px calc(12px + env(safe-area-inset-bottom)); }
  .msg-body { max-width: 100%; }
}

@media (max-width: 640px) {
  body { font-size: 15px; }
  .app-header { height: 56px; padding: 0 12px; }
  .brand { gap: 8px; }
  .brand-mark { width: 30px; height: 30px; border-radius: 9px; }
  .brand-name { font-size: 16px; }
  .header-actions { gap: 2px; }
  .model-status { max-width: 0; margin: 0; padding: 0; overflow: hidden; }
  .model-status .model-label { display: none; }
  .library-button { height: 34px; padding: 0 11px; }

  .control-row { padding: 10px 12px 6px; flex-direction: column; align-items: stretch; gap: 6px; }
  .category-tabs { width: 100%; gap: 7px; }
  .category-tab { flex: 1; justify-content: center; height: 36px; padding: 0 12px; font-size: 13px; }
  .control-chips { flex-wrap: wrap; gap: 7px; }
  .chip { flex: 0 0 auto; height: 34px; padding: 0 12px; font-size: 13px; }
  .control-actions { align-self: flex-end; margin-left: 0; gap: 2px; }
  .new-story-button { height: 32px; margin-left: 0; padding: 0 13px; font-size: 12.5px; }

  .chat-messages { padding: 6px 13px 4px; }
  .msg { gap: 9px; margin-bottom: 18px; }
  .msg-avatar { width: 30px; height: 30px; border-radius: 9px; }
  .bubble { padding: 11px 15px; font-size: 14.5px; line-height: 1.75; }
  .msg-user .msg-body { max-width: 84%; }
  .msg-time { font-size: 11px; margin-top: 5px; }
  .greeting-bubble { padding: 14px 16px; }

  .result-message .result-bubble { padding: 16px 15px; border-radius: 16px; }
  .result-intro-note { display: none; }
  .code-pre { padding: 12px 13px 14px; font-size: 12.4px; max-height: 440px; }
  .result-title-input { font-size: 19px; }
  .result-field textarea { font-size: 14.5px; min-height: 150px; }
  .result-field:first-of-type textarea { min-height: 180px; }
  .result-section-body { padding: 6px 10px; }
  .character-row { grid-template-columns: 1fr; gap: 6px; }
  .character-row .prompt-field textarea { min-height: 68px; }
  .scene-row { padding-left: 34px; }
  .scene-number { left: 0; }
  .scene-meta { grid-template-columns: 1fr; gap: 8px; }
  .scene-fields { grid-template-columns: 1fr; }
  .scene-narration { grid-column: auto; }

  .chat-form { padding: 6px 12px calc(10px + env(safe-area-inset-bottom)); }
  .composer-box { padding: 8px 10px 8px 12px; border-radius: 22px; }
  .composer-hint { display: none; }
  .composer-spacer { flex: 1; }
  .send-button { width: 36px; height: 36px; }

  .library-panel { width: 92vw; padding: 18px 16px calc(14px + env(safe-area-inset-bottom)); }
  .settings-dialog { width: calc(100vw - 24px); max-height: calc(100dvh - 24px); }
  .settings-dialog form { padding: 20px 18px 16px; }
  .toast { bottom: calc(22px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
