:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --panel: #ffffff;
  --ink: #202124;
  --muted: #6c6a64;
  --line: #dedbd4;
  --teal: #2f766f;
  --teal-dark: #1d5551;
  --amber: #b96f2b;
  --red: #ad3434;
  --shadow: 0 8px 22px rgba(32, 33, 36, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
}

.chapter-rail {
  min-height: 100vh;
  border-right: 1px solid var(--line);
  background: #efede8;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--ink);
  color: #fffdf8;
  font-weight: 800;
}

.brand-block h1,
.brand-block p,
.hero-overlay h2,
.hero-overlay p,
.panel-title h3 {
  margin: 0;
}

.brand-block h1 {
  font-size: 17px;
}

.brand-block p,
.eyebrow,
.meta-row span,
label span {
  color: var(--muted);
  font-size: 12px;
}

.project-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.project-button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 9px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.project-button svg {
  width: 15px;
  height: 15px;
  flex: 0 0 auto;
}

.primary-project {
  background: var(--teal);
  border-color: var(--teal);
  color: #fffdf8;
}

.project-status {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.project-list {
  display: grid;
  gap: 10px;
  max-height: min(520px, 68vh);
  overflow: auto;
}

.project-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffefa;
  padding: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

.project-row h4 {
  margin: 0 0 6px;
  font-size: 15px;
}

.project-row p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.rail-actions,
.toolbar-buttons,
.tabs,
.timeline-head {
  display: flex;
  gap: 8px;
  align-items: center;
}

.rail-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.rail-command {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 9px;
  font-size: 12px;
  font-weight: 700;
}

.rail-command.primary {
  grid-column: 1 / -1;
}

.rail-command.primary {
  background: var(--teal);
  color: #fffdf8;
  border-color: var(--teal);
}

.icon-button {
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  display: grid;
  place-items: center;
}

.icon-button svg,
.rail-command svg,
.tab svg,
.command-button svg,
.search-wrap svg {
  width: 16px;
  height: 16px;
}

.icon-button.primary,
.command-button {
  background: var(--teal);
  color: white;
  border-color: var(--teal);
}

.secondary-command {
  background: var(--panel);
  color: var(--ink);
  border-color: var(--line);
}

.compact-command {
  min-height: 30px;
  font-size: 12px;
  padding: 0 8px;
}

.icon-button.danger {
  color: var(--red);
}

.danger-command {
  background: #fff7f4;
  border-color: rgba(173, 52, 52, 0.42);
  color: var(--red);
}

.file-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.search-wrap {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  display: grid;
  grid-template-columns: 34px 1fr;
  align-items: center;
  padding: 0 10px 0 2px;
}

.search-wrap svg {
  justify-self: center;
  color: var(--muted);
}

.search-wrap input,
.title-input,
.meta-row input,
.meta-row select,
label input,
label select,
textarea {
  border: 1px solid var(--line);
  background: #fffefa;
  border-radius: 8px;
  color: var(--ink);
  outline: none;
}

.search-wrap input {
  border: 0;
  background: transparent;
  min-width: 0;
}

.chapter-list {
  display: grid;
  gap: 10px;
  overflow: auto;
  padding-right: 2px;
}

.rail-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 10px 2px;
}

.chapter-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.72);
  padding: 10px;
  display: grid;
  gap: 8px;
  text-align: left;
}

.chapter-card.active {
  border-color: var(--teal);
  box-shadow: inset 3px 0 0 var(--teal);
  background: var(--panel);
}

.chapter-card h2 {
  font-size: 15px;
  margin: 0;
}

.chapter-meta,
.pill-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--muted);
  background: #f9f5ec;
}

.workspace {
  min-width: 0;
  padding: 12px 14px 16px;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 10px;
}

.hero-band {
  min-height: 56px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: none;
}

.hero-band img {
  display: none;
}

.hero-overlay {
  position: relative;
  min-height: 56px;
  padding: 9px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: transparent;
  color: var(--ink);
}

.hero-overlay h2 {
  font-size: 21px;
  line-height: 1.15;
}

.eyebrow {
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 4px;
}

.hero-stats {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hero-stats .stat {
  min-width: 58px;
  border: 1px solid var(--line);
  background: #faf9f6;
  border-radius: 8px;
  padding: 5px 8px;
}

.stat strong {
  display: block;
  font-size: 15px;
}

.stat span {
  color: var(--muted);
  font-size: 12px;
}

.tabs {
  border-bottom: 1px solid var(--line);
}

.tab {
  height: 34px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 10px;
  font-size: 13px;
}

.tab.active {
  color: var(--teal-dark);
  border-bottom-color: var(--teal);
}

.view {
  display: none;
  min-height: 0;
}

.view.active {
  display: block;
}

.chapter-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 12px;
  min-height: 0;
  align-items: start;
}

.editor-panel,
.side-panel,
.detail-panel,
.proof-context,
.proof-list {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  min-height: 0;
}

.editor-panel {
  padding: 10px;
  display: grid;
  grid-template-rows: auto auto auto minmax(520px, 1fr);
  gap: 8px;
  min-width: 0;
}

.panel-toolbar,
.panel-title,
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-toolbar {
  align-items: stretch;
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.toolbar-buttons {
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center;
}

.card-head h4 {
  min-width: 0;
}

.card-actions,
.dialog-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-actions .icon-button {
  width: 34px;
  height: 34px;
}

.title-input {
  width: 100%;
  height: 36px;
  font-size: 18px;
  font-weight: 750;
  padding: 0 12px;
  min-width: 0;
}

.meta-row {
  display: grid;
  grid-template-columns: 140px 1fr 120px;
  gap: 8px;
}

label {
  display: grid;
  gap: 6px;
}

.meta-row input,
.meta-row select,
label input,
label select {
  min-height: 32px;
  padding: 0 9px;
}

textarea {
  width: 100%;
  min-height: 110px;
  resize: vertical;
  padding: 12px;
  line-height: 1.62;
}

#chapterBody {
  min-height: 560px;
  font-size: 17px;
  line-height: 1.8;
  background: #fff;
}

.editor-formatbar {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfaf7;
  padding: 5px;
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 3px;
  padding-right: 6px;
  margin-right: 3px;
  border-right: 1px solid var(--line);
}

.toolbar-group:last-child {
  border-right: 0;
  margin-right: 0;
  padding-right: 0;
}

.toolbar-select {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 4px;
}

.editor-formatbar select {
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 0 6px;
  font-size: 12px;
}

.format-button {
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  display: grid;
  place-items: center;
  padding: 0;
}

.format-button svg {
  width: 15px;
  height: 15px;
}

.format-button:hover,
.editor-formatbar select:hover {
  border-color: rgba(47, 118, 111, 0.55);
}

.format-button:disabled,
.editor-formatbar select:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.rich-editor {
  min-height: 560px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  outline: none;
  padding: 12px 16px;
  font-family: "Noto Serif TC", "Times New Roman", serif;
  font-size: 14px;
  line-height: 1.46;
  overflow: auto;
}

.rich-editor:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(47, 118, 111, 0.12);
}

.rich-editor p,
.rich-editor div {
  margin: 0 0 0.24em;
}

.rich-editor p:empty,
.rich-editor div:empty,
.rich-editor p:has(> br:only-child),
.rich-editor div:has(> br:only-child) {
  min-height: 0.35em;
  margin: 0 0 0.12em;
  line-height: 0.8;
}

.rich-editor h1,
.rich-editor h2,
.rich-editor h3 {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.22;
  margin: 0.6em 0 0.25em;
}

.rich-editor h1 {
  font-size: 1.28em;
}

.rich-editor h2 {
  font-size: 1.14em;
}

.rich-editor h3 {
  font-size: 1.04em;
}

.rich-editor blockquote {
  margin: 0.4em 0;
  padding: 0.05em 0 0.05em 0.85em;
  border-left: 3px solid var(--teal);
  color: #4d4a44;
  background: #faf9f6;
}

.rich-editor ul,
.rich-editor ol {
  margin: 0 0 0.28em 1.15em;
  padding: 0;
}

.rich-editor li {
  margin: 0.1em 0;
}

.rich-editor sup,
.rich-editor sub {
  font-size: 0.72em;
  line-height: 0;
}

.rich-editor [style*="font-size: 24px"],
.rich-editor [style*="font-size: 28px"] {
  font-size: 16px !important;
}

.rich-editor [style*="line-height: 1.85"],
.rich-editor [style*="line-height: 2.1"] {
  line-height: 1.46 !important;
}

.rich-editor hr {
  border: 0;
  border-top: 1px solid #bcb7ad;
  margin: 0.5em 0;
}

.side-panel {
  padding: 12px;
  overflow: auto;
  max-height: calc(100vh - 160px);
}

.part-stack,
.review-stack,
.prompt-stack,
.library-list,
.proof-list,
.timeline {
  display: grid;
  gap: 10px;
}

.part-card,
.review-card,
.prompt-card,
.proof-card,
.event-card,
.library-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffefa;
  padding: 12px;
}

.empty-card {
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #faf9f6;
  color: var(--muted);
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
}

.review-card {
  border-color: rgba(47, 118, 111, 0.45);
}

.review-card h4,
.prompt-card h4 {
  margin: 0 0 8px;
  font-size: 15px;
}

.review-card select,
.review-card input {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffefa;
  padding: 0 9px;
}

.review-fields {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin: 10px 0;
}

.review-context,
.prompt-preview {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.split-title {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.prompt-card textarea {
  min-height: 160px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.prompt-meta {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.prompt-model-select {
  margin: 10px 0;
  display: grid;
  gap: 5px;
}

.prompt-model-select span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.prompt-model-select select {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 9px;
}

.image-upload-label input {
  min-height: auto;
  padding: 8px;
}

.part-image-stack,
.inline-image-stack,
.prompt-reference-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.part-image-thumb,
.inline-image-thumb,
.prompt-reference-thumb {
  position: relative;
  width: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #faf9f6;
  overflow: hidden;
}

.part-image-thumb img,
.inline-image-thumb img,
.prompt-reference-thumb img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}

.part-image-thumb button {
  position: absolute;
  right: 4px;
  top: 4px;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 999px;
  background: rgba(32, 33, 36, 0.72);
  color: white;
  display: grid;
  place-items: center;
}

.part-image-thumb span,
.prompt-reference-thumb span {
  display: block;
  padding: 4px 5px;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inline-image-stack {
  margin-top: 8px;
}

.inline-image-thumb {
  width: 46px;
}

.prompt-reference-images {
  margin: 10px 0;
}

.prompt-reference-thumb {
  width: 58px;
}

.generated-image-stack {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.generated-image-card {
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #faf9f6;
  overflow: hidden;
}

.generated-image-card img {
  width: 100%;
  display: block;
}

.generated-image-card figcaption {
  padding: 6px 8px;
  color: var(--muted);
  font-size: 11px;
}

.part-card.active,
.library-card.active {
  border-color: var(--teal);
  box-shadow: inset 3px 0 0 var(--teal);
}

.part-card h4,
.library-card h4,
.event-card h4,
.proof-card h4 {
  margin: 0 0 8px;
  font-size: 15px;
}

.part-card textarea {
  min-height: 72px;
  margin-top: 8px;
}

.library-layout {
  display: grid;
  grid-template-columns: 180px minmax(320px, 1fr) 360px;
  gap: 14px;
}

.filters {
  display: grid;
  align-content: start;
  gap: 8px;
}

.filter-button {
  min-height: 32px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  text-align: left;
  padding: 8px 10px;
}

.filter-button.active {
  border-color: var(--teal);
  color: var(--teal-dark);
  font-weight: 700;
}

.detail-panel {
  padding: 14px;
  display: grid;
  gap: 12px;
  align-content: start;
}

.detail-panel textarea {
  min-height: 160px;
}

.command-button {
  min-height: 30px;
  border: 1px solid var(--teal);
  border-radius: 8px;
  padding: 0 9px;
  display: inline-flex;
  gap: 6px;
  font-size: 12px;
  align-items: center;
  justify-content: center;
}

.toolbar-buttons .command-button {
  flex: 0 0 auto;
}

.command-button:disabled,
.rail-command:disabled,
.icon-button:disabled,
.format-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.reader-layout {
  display: grid;
  grid-template-columns: 260px minmax(420px, 1fr) 340px;
  gap: 14px;
}

.reader-tools,
.reader-panel,
.ai-brief-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
}

.reader-tools {
  display: grid;
  align-content: start;
  gap: 12px;
}

.reader-tools select {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffefa;
  padding: 0 10px;
}

.reader-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.reader-panel {
  min-height: 680px;
  max-width: 860px;
}

.reader-panel h2 {
  margin: 0 0 8px;
  font-size: 30px;
}

.reader-meta {
  margin: 0 0 24px;
  color: var(--muted);
  font-size: 13px;
}

.reader-body {
  font-family: "Times New Roman", "Noto Serif TC", serif;
  font-size: 18px;
  line-height: 1.9;
}

.reader-body p {
  margin: 0 0 1.1em;
}

.ai-brief-panel pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.55;
}

.timeline-head {
  justify-content: space-between;
  margin-bottom: 14px;
}

.timeline-head select {
  min-height: 40px;
  min-width: 220px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  padding: 0 10px;
}

.timeline {
  position: relative;
  padding-left: 20px;
}

.timeline::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 6px;
  bottom: 6px;
  width: 2px;
  background: var(--line);
}

.event-card {
  position: relative;
}

.event-card::before {
  content: "";
  position: absolute;
  left: -20px;
  top: 18px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--amber);
}

.proof-grid {
  display: grid;
  grid-template-columns: minmax(320px, 460px) 1fr;
  gap: 14px;
}

.proof-list,
.proof-context {
  padding: 14px;
}

.severity-high {
  border-color: rgba(173, 52, 52, 0.5);
}

.severity-medium {
  border-color: rgba(185, 111, 43, 0.5);
}

dialog {
  border: 0;
  border-radius: 10px;
  padding: 0;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(32, 33, 36, 0.32);
}

.dialog-card {
  width: min(520px, calc(100vw - 28px));
  padding: 16px;
  display: grid;
  gap: 12px;
  background: var(--panel);
}

.dialog-note {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.dialog-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 40;
  min-width: 150px;
  max-width: min(360px, calc(100vw - 36px));
  border: 1px solid rgba(47, 118, 111, 0.35);
  border-radius: 8px;
  background: rgba(32, 33, 36, 0.94);
  color: #fffdf8;
  padding: 9px 12px;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 10px 24px rgba(32, 33, 36, 0.18);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1080px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .chapter-rail {
    min-height: auto;
  }

  .chapter-grid,
  .library-layout,
  .proof-grid,
  .reader-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .workspace,
  .chapter-rail {
    padding: 12px;
  }

  .hero-overlay {
    align-items: start;
    flex-direction: column;
  }

  .meta-row {
    grid-template-columns: 1fr;
  }

  .tabs {
    overflow-x: auto;
  }

  .toolbar-buttons {
    justify-content: stretch;
  }

  .command-button,
  .rail-command {
    width: 100%;
  }
}
