:root {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #262b36;
  --text: #e6e8ec;
  --muted: #9aa3b2;
  --user: #2563eb;
  --assistant: #1f2530;
  --accent: #ec4899;
  --panel-w: 340px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

body { height: 100vh; overflow: hidden; }

.layout {
  display: flex;
  height: 100vh;
}

/* ===== 故事設定面板 ===== */
.panel {
  width: var(--panel-w);
  flex: 0 0 var(--panel-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease;
}
.panel.collapsed { margin-left: calc(-1 * var(--panel-w)); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: 15px; margin: 0; font-weight: 600; }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 24px;
}

.field { margin-bottom: 18px; }
.field > label,
.group-title {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 5px;
  font-weight: 500;
}
.group-title {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
  margin-top: 4px;
}
.field > label:first-child { margin-top: 0; }

.panel textarea,
.panel input[type="text"],
.panel select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  line-height: 1.45;
  resize: vertical;
}
.panel textarea:focus,
.panel input:focus,
.panel select:focus { outline: none; border-color: var(--accent); }

/* 角色卡片 */
.char-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.02);
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.char-card-head { display: flex; gap: 8px; align-items: center; }
.char-card-head .char-name { flex: 1; font-weight: 600; }

.add-btn {
  width: 100%;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  padding: 9px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.add-btn:hover { color: var(--accent); border-color: var(--accent); }

.del-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 7px;
  cursor: pointer;
  flex: 0 0 auto;
}
.del-btn:hover { color: #fca5a5; border-color: #7f1d2e; }

/* ===== 右側主區 ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; flex: 1; }

.icon-btn,
.topbar button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.icon-btn:hover,
.topbar button:hover { color: var(--text); border-color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

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

.bubble {
  max-width: 88%;
  padding: 11px 14px;
  border-radius: 16px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 15px;
}
.row.user .bubble {
  background: var(--user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.row.assistant .bubble {
  background: var(--assistant);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.bubble.error {
  background: #3b1d22;
  border-color: #7f1d2e;
  color: #fca5a5;
}

.bubble .cursor {
  display: inline-block;
  width: 7px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

#input {
  flex: 1;
  resize: none;
  max-height: 180px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.4;
}
#input:focus { outline: none; border-color: var(--accent); }

#send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0 20px;
  font-size: 15px;
  cursor: pointer;
  font-weight: 600;
}
#send:disabled { opacity: 0.5; cursor: not-allowed; }

.empty {
  color: var(--muted);
  text-align: center;
  margin: auto;
  font-size: 14px;
}

/* 手機版：面板變抽屜，預設收合 */
@media (max-width: 720px) {
  .panel {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
  }
  .messages, .composer { max-width: 100%; }
}

/* ===================================================================
   以下為多頁面（書架 / 登入 / 我的作品 / 閱讀）新增的元件樣式。
   全部沿用上方 :root 的設計 token，維持只有暗色主題。
   =================================================================== */

:root {
  --ok: #34d399;
  --warn: #fbbf24;
}

/* 一般頁面（非寫作頁）：可捲動的直式版面 */
body.page { height: auto; overflow: auto; }
body.page .container {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 16px 64px;
}

/* ===== 頂欄 ===== */
.nav {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}
.nav-brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 16px; }
.nav-spacer { flex: 1; }
.nav-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
  padding: 5px 0;
  border-bottom: 2px solid transparent;
}
.nav-link:hover { color: var(--text); }
.nav-link.active { color: var(--text); border-bottom-color: var(--accent); }
.nav-user { color: var(--muted); font-size: 13px; }

/* ===== 按鈕 ===== */
.btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  text-decoration: none;
  display: inline-block;
  line-height: 1.4;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-ghost { color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-danger:hover { color: #fca5a5; border-color: #7f1d2e; }
.btn-sm { padding: 5px 10px; font-size: 13px; }

/* ===== 表單 ===== */
.form-row { margin-bottom: 14px; }
.form-row label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 5px;
  font-weight: 500;
}
.form-row input,
.form-row textarea,
.form-row select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  resize: vertical;
}
.form-row input:focus,
.form-row textarea:focus,
.form-row select:focus { outline: none; border-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.form-error {
  color: #fca5a5;
  background: #3b1d22;
  border: 1px solid #7f1d2e;
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 13px;
  margin-bottom: 14px;
}
.form-error:empty { display: none; }

/* ===== 登入 / 註冊 ===== */
.auth-card {
  max-width: 380px;
  margin: 56px auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 26px;
}
.auth-tabs { display: flex; gap: 6px; margin-bottom: 22px; }
.auth-tabs button {
  flex: 1;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
}
.auth-tabs button.active { color: #fff; background: var(--accent); border-color: var(--accent); }

/* ===== 卡片 ===== */
.page-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-head h1 { font-size: 20px; margin: 0; flex: 1; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.card h3 { margin: 0; font-size: 16px; }
.card h3 a { color: var(--text); text-decoration: none; }
.card h3 a:hover { color: var(--accent); }
.card-meta { font-size: 12px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.card-text {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; padding-top: 4px; }

/* ===== 狀態標籤 ===== */
.badge {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge.published { color: var(--ok); border-color: var(--ok); }
.badge.pending { color: var(--warn); border-color: var(--warn); }
.badge.failed { color: #fca5a5; border-color: #7f1d2e; }

/* ===== 閱讀頁 ===== */
.reader { max-width: 720px; margin: 0 auto; }
.reader h1 { font-size: 22px; margin: 0 0 6px; }
.reader .byline { color: var(--muted); font-size: 13px; margin-bottom: 26px; }
.reader-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 2;
  font-size: 17px;
  margin-bottom: 36px;
}
.reader-nav {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--border);
  padding-top: 18px;
  flex-wrap: wrap;
}

/* ===== 章節目錄 ===== */
.chapter-list { display: flex; flex-direction: column; gap: 6px; }
.chapter-row {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-size: 13px;
  text-decoration: none;
  color: var(--text);
}
.chapter-row:hover { border-color: var(--accent); }
.chapter-row.active { border-color: var(--accent); background: rgba(236, 72, 153, 0.08); }
.chapter-row .ch-no { color: var(--muted); flex: 0 0 auto; font-variant-numeric: tabular-nums; }
.chapter-row .ch-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Modal ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 620px;
  max-height: 88vh;
  overflow-y: auto;
}
.modal h2 { margin: 0 0 16px; font-size: 17px; }
.modal-actions { display: flex; gap: 9px; justify-content: flex-end; margin-top: 16px; }
.modal textarea.long { min-height: 320px; line-height: 1.8; }

.muted-note { color: var(--muted); font-size: 14px; padding: 36px 0; text-align: center; }

/* ===== Toast ===== */
#toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 80;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  max-width: calc(100vw - 32px);
}
#toast.error { color: #fca5a5; border-color: #7f1d2e; background: #3b1d22; }
#toast[hidden] { display: none; }

/* ===== 寫作頁新增元件 ===== */
.panel-head .save-state { font-size: 11px; color: var(--muted); margin-left: auto; padding-right: 8px; }
.panel .hint { margin-bottom: 4px; }
.panel input[type="number"] {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
}
.panel input[type="number"]:focus { outline: none; border-color: var(--accent); }

.chapter-bar {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  flex-wrap: wrap;
}
.chapter-badge {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
#chapter-title {
  flex: 1;
  min-width: 120px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
}
#chapter-title:focus { outline: none; border-color: var(--accent); }
.pub-toggle {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  cursor: pointer;
}
.pub-toggle input { accent-color: var(--accent); }

/* 訊息泡泡下方的動作列 */
.bubble-actions { display: flex; gap: 7px; margin-top: 7px; flex-wrap: wrap; }
.row.assistant { flex-direction: column; align-items: flex-start; }

.chapter-row .badge { flex: 0 0 auto; }
.chapter-list:empty::after {
  content: '還沒有已儲存的章節';
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 720px) {
  .chapter-bar { max-width: 100%; }
}

.form-row input:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
