:root {
  --bg: #f4efe6;
  --surface: #fffdf8;
  --surface-2: #eee7db;
  --text: #25231f;
  --muted: #746f66;
  --border: #d8d0c3;
  --accent: #6b4931;
  --accent-contrast: #fffaf2;
  --danger: #9a3f3f;
  --shadow: 0 16px 40px rgb(56 44 30 / 0.08);
  --reader-font-size: 19px;
  --reader-line-height: 1.9;
  --reader-max-width: 760px;
  --reader-font: ui-serif, "Noto Serif SC", "Songti SC", "Source Han Serif SC", STSong, serif;
  --radius: 18px;
}

:root[data-theme="dark"] {
  --bg: #161513;
  --surface: #1f1e1b;
  --surface-2: #292722;
  --text: #eee8dd;
  --muted: #aaa398;
  --border: #3b3832;
  --accent: #d6ad85;
  --accent-contrast: #211b16;
  --danger: #e07c7c;
  --shadow: 0 16px 40px rgb(0 0 0 / 0.22);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161513;
    --surface: #1f1e1b;
    --surface-2: #292722;
    --text: #eee8dd;
    --muted: #aaa398;
    --border: #3b3832;
    --accent: #d6ad85;
    --accent-contrast: #211b16;
    --danger: #e07c7c;
    --shadow: 0 16px 40px rgb(0 0 0 / 0.22);
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; }
button { color: inherit; }
a { color: inherit; }
img { max-width: 100%; }

.button-reset { border: 0; background: none; padding: 0; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 62px;
  padding: 10px max(18px, env(safe-area-inset-left));
  border-bottom: 1px solid color-mix(in srgb, var(--border) 75%, transparent);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
}
.brand { display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 720; }
.brand-mark {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 11px;
  background: var(--text); color: var(--bg); font-family: serif;
}
.top-actions, .row-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn, .text-btn, .primary-btn, .secondary-btn, .danger-btn, .danger-ghost-btn, .chip-btn {
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 9px 13px;
  cursor: pointer;
}
.icon-btn { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; font-size: 20px; }
.text-btn { background: transparent; }
.primary-btn { border-color: var(--text); background: var(--text); color: var(--bg); font-weight: 700; }
.secondary-btn { background: var(--surface); }
.danger-btn { border-color: var(--danger); background: var(--danger); color: white; }
.danger-ghost-btn { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); color: var(--danger); background: transparent; }
.chip-btn { padding: 7px 10px; background: var(--surface-2); border: 0; }
button:disabled { opacity: .5; cursor: not-allowed; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app-shell { width: min(1160px, calc(100% - 32px)); margin: 0 auto; padding: 42px 0 90px; }
.page-head { display: flex; justify-content: space-between; gap: 18px; align-items: end; margin-bottom: 28px; }
.page-head h1 { margin: 4px 0 0; font-family: ui-serif, serif; font-size: clamp(30px, 6vw, 52px); line-height: 1.08; }
.eyebrow { margin: 0; color: var(--muted); text-transform: uppercase; letter-spacing: .12em; font-size: 12px; font-weight: 750; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty-state {
  border: 1px dashed var(--border); border-radius: var(--radius); padding: 42px 24px; text-align: center; background: color-mix(in srgb, var(--surface) 55%, transparent);
}

.shelf-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 18px; }
.book-card {
  display: flex; flex-direction: column; min-height: 320px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  background: var(--surface); box-shadow: var(--shadow); cursor: pointer; transition: transform .18s ease, box-shadow .18s ease;
}
.book-card:hover { transform: translateY(-2px); }
.book-cover { aspect-ratio: 16 / 9; background: linear-gradient(135deg, var(--surface-2), var(--border)); object-fit: cover; width: 100%; }
.book-cover.placeholder { display: grid; place-items: center; color: var(--muted); font-family: serif; font-size: 38px; }
.book-body { padding: 18px; display: flex; flex-direction: column; gap: 11px; flex: 1; }
.book-body h2 { margin: 0; font-size: 21px; font-family: ui-serif, serif; }
.book-desc { color: var(--muted); margin: 0; line-height: 1.65; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.meta-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; color: var(--muted); font-size: 12px; margin-top: auto; }
.badge { border: 1px solid var(--border); padding: 4px 8px; border-radius: 999px; }
.badge.private { color: var(--danger); }

.novel-hero { display: grid; grid-template-columns: 210px 1fr; gap: 30px; align-items: start; margin-bottom: 34px; }
.novel-cover-large { width: 100%; aspect-ratio: 3/4; border-radius: var(--radius); object-fit: cover; background: var(--surface-2); border: 1px solid var(--border); }
.novel-cover-large.placeholder { display: grid; place-items: center; font: 56px/1 serif; color: var(--muted); }
.novel-summary h1 { margin: 8px 0 12px; font: 700 clamp(32px, 5vw, 54px)/1.14 ui-serif, serif; }
.novel-summary p.description { margin: 16px 0; line-height: 1.8; white-space: pre-wrap; }
.chapter-list { display: grid; gap: 8px; }
.chapter-row { display: grid; grid-template-columns: 1fr auto; gap: 14px; align-items: center; padding: 15px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); }
.chapter-main { cursor: pointer; min-width: 0; }
.chapter-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chapter-meta { margin-top: 5px; font-size: 12px; color: var(--muted); }

.reader-shell { width: min(var(--reader-max-width), 100%); margin: 0 auto; }
.reader-head { text-align: center; padding: 18px 0 28px; }
.reader-head .crumb { display: inline-flex; gap: 6px; color: var(--muted); font-size: 13px; cursor: pointer; }
.reader-head h1 { margin: 16px 0 10px; font: 700 clamp(28px, 5vw, 44px)/1.2 ui-serif, serif; }
.reader-toolbar {
  position: sticky; top: 72px; z-index: 20; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; margin: 0 auto 26px;
  border: 1px solid var(--border); border-radius: 14px; background: color-mix(in srgb, var(--surface) 91%, transparent); backdrop-filter: blur(15px); box-shadow: var(--shadow);
}
.reader-toolbar .spacer { flex: 1; }
.reader-article {
  font-family: var(--reader-font);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  letter-spacing: .01em;
  word-break: break-word;
}
.reader-article p { margin: 0 0 1.25em; text-indent: 2em; }
.reader-article h1, .reader-article h2, .reader-article h3 { text-indent: 0; margin: 1.7em 0 .8em; line-height: 1.35; }
.reader-article blockquote { margin: 1.5em 0; padding: .2em 1em; border-left: 3px solid var(--border); color: var(--muted); }
.reader-article hr { border: 0; border-top: 1px solid var(--border); margin: 2.5em 0; }
.reader-article figure { margin: 2.2em 0; text-align: center; }
.reader-article figure img { border-radius: 12px; max-height: 82vh; object-fit: contain; box-shadow: var(--shadow); }
.reader-article figcaption { color: var(--muted); font: 13px/1.5 ui-sans-serif, sans-serif; margin-top: 8px; }
.reader-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 50px; padding-top: 24px; border-top: 1px solid var(--border); }
.reader-nav button { min-height: 58px; }

.editor-panel { display: grid; gap: 12px; }
.editor-top { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.save-status { margin-left: auto; color: var(--muted); font-size: 13px; }
.editor-textarea {
  width: 100%; min-height: 62vh; resize: vertical; padding: 20px; border-radius: 14px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: 16px/1.8 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.editor-help { padding: 14px 16px; border-radius: 12px; background: var(--surface-2); font-size: 13px; color: var(--muted); line-height: 1.7; }
.preview-box { border: 1px solid var(--border); background: var(--surface); border-radius: 14px; padding: 22px; }
.hidden { display: none !important; }

.modal { border: 0; padding: 0; background: transparent; color: var(--text); width: min(620px, calc(100% - 28px)); }
.modal::backdrop { background: rgb(0 0 0 / .48); backdrop-filter: blur(2px); }
.modal-card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 22px; box-shadow: var(--shadow); }
.modal-card.wide { width: 100%; }
.modal-head { display: flex; align-items: start; justify-content: space-between; gap: 18px; margin-bottom: 20px; }
.modal-head h2 { margin: 4px 0 0; }
.field { display: grid; gap: 7px; margin: 14px 0; font-size: 14px; font-weight: 650; }
.field input, .field textarea, .field select {
  width: 100%; padding: 11px 12px; color: var(--text); border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-weight: 400;
}
.field textarea { resize: vertical; }
.field-grid { display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: end; }
.check-field { display: flex; align-items: center; gap: 9px; min-height: 42px; }
.check-field input { width: 18px; height: 18px; }
.modal-actions { margin-top: 22px; display: flex; justify-content: flex-end; gap: 9px; }
.modal-actions.split { justify-content: space-between; }

.toast { position: fixed; left: 50%; bottom: max(22px, env(safe-area-inset-bottom)); z-index: 100; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: .2s ease; background: var(--text); color: var(--bg); border-radius: 999px; padding: 10px 16px; max-width: min(90vw, 600px); text-align: center; box-shadow: var(--shadow); }
.toast.show { transform: translate(-50%, 0); opacity: 1; }
.toast.error { background: var(--danger); color: white; }

.setup-card { max-width: 760px; margin: 40px auto; padding: 28px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.setup-card code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; }
.loading { display: grid; place-items: center; min-height: 40vh; color: var(--muted); }
.skeleton { animation: pulse 1.2s infinite alternate; }
@keyframes pulse { from { opacity: .45; } to { opacity: 1; } }

@media (max-width: 720px) {
  .app-shell { width: min(100% - 22px, 1160px); padding-top: 26px; }
  .page-head { align-items: start; flex-direction: column; }
  .novel-hero { grid-template-columns: 92px 1fr; gap: 16px; }
  .novel-cover-large { border-radius: 12px; }
  .novel-summary h1 { font-size: 30px; margin-top: 2px; }
  .novel-summary p.description { grid-column: 1 / -1; }
  .reader-toolbar { top: 67px; }
  .reader-article { font-size: var(--reader-font-size); }
  .chapter-row { grid-template-columns: 1fr; }
  .row-actions { justify-content: flex-end; }
  .field-grid { grid-template-columns: 1fr; }
  .topbar { min-height: 58px; }
  .reader-nav { grid-template-columns: 1fr; }
  .reader-toolbar .spacer { display: none; }
  .save-status { width: 100%; margin-left: 0; }
}
