:root {
  color-scheme: light;
  --bg: #f7f7f4;
  --surface: #ffffff;
  --surface-soft: #f3f4f2;
  --line: #dedfdb;
  --line-soft: #ececea;
  --text: #20221f;
  --muted: #6d746d;
  --subtle: #a1a69f;
  --brand: #2f5f56;
  --brand-soft: #eef5f2;
  --danger: #a33a32;
  --danger-soft: #fff5f3;
  --shadow-soft: 0 1px 2px rgba(24, 27, 24, 0.05);
  --shadow-float: 0 18px 55px rgba(24, 27, 24, 0.1);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", Arial, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0; min-height: 100vh; color: var(--text); background: var(--bg);
}
button, input, textarea { font: inherit; }
button { cursor: pointer; }
.app-shell { min-height: 100vh; }

.app-header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 18px;
  min-height: 62px; border-bottom: 1px solid var(--line-soft);
  padding: 8px 24px;
  background: rgba(247, 247, 244, 0.9);
  backdrop-filter: blur(14px);
}

.brand-mini strong, .brand-mini span {
  display: block; line-height: 1.22;
}
.brand-mini strong { font-size: 14px; font-weight: 720; }
.brand-mini span { color: var(--muted); font-size: 12px; }

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

.header-actions span, .pill-field, .prompt-examples button {
  border: 1px solid var(--line); border-radius: 999px;
  color: var(--muted); background: rgba(255,255,255,0.76);
  font-size: 12px; font-weight: 620; line-height: 1;
}
.pill-field {
  display: flex; align-items: center; gap: 5px;
  min-height: 30px; padding: 6px 8px;
}
.pill-field input {
  border: 0; padding: 0; color: var(--text); background: transparent;
  text-align: left; font-size: 12px; font-weight: 680;
}
.pill-field input:focus { outline: none; }

.workspace {
  position: relative;
  display: grid; grid-template-rows: minmax(0,1fr) auto;
  min-height: calc(100vh - 62px);
}

.messages {
  display: grid; align-content: center; gap: 14px;
  width: min(880px, 100%); margin: 0 auto; padding: 18px 24px 340px;
}
body.has-conversation .messages {
  align-content: start; padding-top: 26px;
}

.message {
  width: min(740px, 96%);
  border: 1px solid var(--line-soft); border-radius: 8px;
  padding: 14px 16px; background: var(--surface); box-shadow: var(--shadow-soft);
}
.message.user {
  margin-left: auto; width: min(660px, 92%);
  border-color: #dde7e3; background: var(--brand-soft);
}
.message.error {
  border-color: rgba(163,58,50,0.26); color: var(--danger); background: var(--danger-soft);
}
.message p { margin: 0; color: var(--text); line-height: 1.72; white-space: pre-wrap; }
.message-label {
  margin-bottom: 8px; color: var(--muted); font-size: 12px; font-weight: 700;
}

.welcome-card {
  justify-self: center; width: min(720px, 96%);
  border: 0; background: transparent; box-shadow: none; text-align: center;
}
.welcome-card h1 {
  margin: 0 0 10px;
  font-size: clamp(25px, 3vw, 34px); font-weight: 680; line-height: 1.16;
}
.welcome-card p { max-width: 560px; margin: 0 auto; color: var(--muted); font-size: 14px; }

.prompt-examples {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 20px;
}
.prompt-examples button { min-height: 32px; padding: 7px 11px; }
.prompt-examples button:hover { border-color: #cfd4cf; color: var(--text); background: #fff; }

.composer {
  position: absolute; left: 50%; top: 42%; z-index: 9;
  display: grid; gap: 9px;
  width: min(840px, calc(100% - 48px));
  border: 1px solid var(--line); border-radius: 20px; padding: 12px;
  background: rgba(255,255,255,0.97); box-shadow: var(--shadow-float);
  transform: translateX(-50%);
}
body.has-conversation .composer {
  position: sticky; top: auto; bottom: 16px;
  width: min(900px, calc(100% - 48px));
  margin: 0 auto 16px; transform: none; left: auto;
}



.composer-bar { display: flex; align-items: center; }

.send-button {
  width: 36px; height: 36px;
  border: 0; border-radius: 999px;
  color: #fff; background: #5f6661; font-size: 20px; line-height: 1;
  display: grid; place-items: center;
}
.send-button:hover { background: #303531; }
.send-button:disabled { opacity: 0.5; cursor: not-allowed; }

.status-message { color: #38433e; background: #fbfbfa; }

.script-title { margin: 0 0 12px; font-size: 17px; line-height: 1.35; }
.script-body { white-space: pre-wrap; word-break: break-word; line-height: 1.78; }

.message-actions { display: flex; justify-content: flex-end; margin-top: 12px; }

.meta-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0,1fr));
  gap: 8px; margin-top: 8px; color: var(--muted); font-size: 12px;
}

.secondary-button {
  border: 1px solid var(--line); border-radius: 8px;
  min-height: 36px; padding: 8px 11px;
  color: var(--text); background: #fff; font-size: 13px; font-weight: 640;
}
.secondary-button:hover { background: var(--surface-soft); }

.saved-files { margin-top: 8px; }
.saved-files-label {
  color: var(--muted); font-size: 12px; line-height: 1.5;
}
.saved-files-link {
  display: inline-flex; align-items: center; gap: 6px;
  margin-top: 4px; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 8px;
  color: var(--brand); font-size: 13px; font-weight: 640;
  background: var(--brand-soft); text-decoration: none;
}
.saved-files-link:hover { background: #ddebe5; }

/* 候选故事入口卡片 */
.candidate-list {
  display: grid; gap: 6px; margin: 10px 0;
}
.candidate-item {
  display: grid; grid-template-columns: 20px 1fr;
  gap: 4px 8px; align-items: start;
  width: 100%;
  padding: 10px 14px; border: 1px solid var(--line-soft); border-radius: 10px;
  text-align: left; font-size: 13px; line-height: 1.55;
  color: var(--text); background: var(--surface-soft);
  cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.candidate-idx {
  grid-row: 1 / 3; padding-top: 2px;
  font-size: 12px; font-weight: 700; color: var(--muted);
}
.candidate-story { font-weight: 640; }
.candidate-meta {
  font-size: 11px; color: var(--muted); line-height: 1.45;
}
.candidate-item:hover { border-color: var(--brand); background: var(--brand-soft); }
.candidate-item:disabled { opacity: 0.4; cursor: not-allowed; }

.candidate-fallback {
  color: var(--muted); font-size: 13px; line-height: 1.7;
  padding: 12px; border-radius: 8px; background: var(--bg);
}

.step-c-details { margin: 10px 0; }
.step-c-details summary {
  color: var(--muted); font-size: 12px; cursor: pointer;
}
.step-c-full {
  margin-top: 8px; padding: 12px;
  border-radius: 8px; background: var(--bg);
  font-size: 12px; line-height: 1.7; white-space: pre-wrap;
  max-height: 300px; overflow-y: auto;
}

.candidate-feedback {
  display: flex; gap: 8px; margin-top: 12px;
}
.feedback-input {
  flex: 1; min-height: 36px;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 10px; font-size: 13px;
}
.feedback-input:focus { outline: none; border-color: var(--brand); }
.feedback-input:disabled { opacity: 0.4; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }

.connection-state {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 12px; border-radius: 999px;
  font-size: 12px; font-weight: 620; color: var(--muted);
  background: var(--surface-soft);
  transition: color 0.2s, background 0.2s;
}
.connection-state.running { color: #2f5f56; background: #eef5f2; }
.connection-state.error { color: var(--danger); background: var(--danger-soft); }
.connection-state.done { color: #2f5f56; background: #eef5f2; }

.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--subtle);
}
.connection-state.running .status-dot { background: #2f5f56; animation: pulse-dot 1.2s ease-in-out infinite; }
.connection-state.error .status-dot { background: var(--danger); }
.connection-state.done .status-dot { background: #2f5f56; }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}
/* 新旧样式：输入行 */
/* 输入行 */
.input-row {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px;
  background: var(--surface-soft); border: 1px solid var(--line);
  border-radius: 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input-row:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(47,95,86,0.08);
}

/* URL 栏 */
.url-bar {
  display: flex; align-items: center; gap: 6px;
}
#kolUrl {
  flex: 1; height: 36px; padding: 0 12px;
  border: 0; border-radius: 10px;
  font-size: 13px; color: var(--text); background: var(--surface);
}
#kolUrl::placeholder { color: var(--subtle); font-size: 12px; }
#kolUrl:focus { outline: none; }

/* 对话栏 */
.chat-bar {
  display: flex; align-items: center; gap: 6px;
}
#creativePromptInput {
  flex: 1; height: 44px; padding: 0 14px;
  border: 0; border-radius: 12px;
  font-size: 15px; color: var(--text); background: var(--surface);
}
#creativePromptInput::placeholder { color: var(--subtle); }
#creativePromptInput:focus { outline: none; }
.chat-bar .send-button {
  flex: 0 0 44px; width: 44px; height: 44px;
}
.add-button {
  flex: 0 0 28px; width: 28px; height: 28px;
  border: 1.5px dashed var(--line); border-radius: 8px;
  color: var(--muted); background: transparent;
  font-size: 16px; font-weight: 500; line-height: 1;
  display: grid; place-items: center;
  transition: border-color 0.15s, color 0.15s;
}
.add-button:hover { border-color: var(--brand); color: var(--brand); }

/* 模态框 */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.3); backdrop-filter: blur(4px);
}
.modal-card {
  width: min(560px, 92vw); max-height: 85vh; overflow-y: auto;
  padding: 24px; border-radius: 16px;
  background: var(--surface); box-shadow: var(--shadow-float);
  display: flex; flex-direction: column; gap: 14px;
}
.modal-card h2 { margin: 0; font-size: 18px; font-weight: 680; }
#newScriptName, #newScriptContent {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: 14px; color: var(--text); background: var(--surface-soft);
}
#newScriptName:focus, #newScriptContent:focus {
  outline: none; border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(47,95,86,0.08);
}
#newScriptContent { resize: vertical; min-height: 160px; line-height: 1.65; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }


/* 上传区域 */
.upload-row { display: flex; align-items: center; gap: 10px; }


/* 下载按钮 */
.dl-button {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 16px; border: 1px solid var(--brand); border-radius: 8px;
  color: var(--brand); background: var(--brand-soft);
  font-size: 13px; font-weight: 640; cursor: pointer;
  transition: background 0.15s;
}
.dl-button:hover { background: #ddebe5; }
