:root {
  --ink: #252724;
  --muted: #62665f;
  --paper: #fbfaf6;
  --canvas: #f2f0ea;
  --line: #dedbd2;
  --green: #526c5b;
  --green-dark: #405646;
  --green-pale: #e9eee8;
  --clay: #995d42;
  --serif: "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "STSong", Georgia, serif;
  --sans: "Avenir Next", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--sans);
}

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

button {
  color: inherit;
}

button,
summary,
select,
input[type="checkbox"] {
  touch-action: manipulation;
}

.hidden {
  display: none !important;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid rgba(82, 108, 91, 0.55);
  outline-offset: 2px;
}

::selection {
  background: #dce5db;
}

.studio-shell {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--canvas);
}

.topbar {
  position: relative;
  z-index: 50;
  flex: 0 0 auto;
  height: 68px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(251, 250, 246, 0.96);
  box-shadow: 0 1px 0 rgba(31, 33, 29, 0.02);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  width: max-content;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px 4px 10px 4px;
  background: var(--green);
  color: #fff;
  font-family: var(--serif);
  font-size: 19px;
  box-shadow: 0 5px 14px rgba(64, 86, 70, 0.18);
}

.brand-name {
  font-family: var(--serif);
  font-size: 21px;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.brand-subtitle {
  margin-left: 2px;
  padding-left: 12px;
  border-left: 1px solid #cbc8bf;
  color: #62665f;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.19em;
}

.document-state {
  display: flex;
  align-items: center;
  gap: 7px;
  color: #62665f;
  font-size: 12px;
}

.saved-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #78917e;
}

.document-state.save-error .saved-dot {
  background: var(--clay);
}

.save-retry {
  border: 1px solid var(--green);
  border-radius: 6px;
  padding: 5px 8px;
  background: transparent;
  color: var(--green-dark);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.top-actions {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 13px;
}

.api-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid #d8d9d3;
  border-radius: 99px;
  background: #fff;
  color: #686b65;
  font-size: 12px;
  font-weight: 600;
}

.api-pill > span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #b7aaa0;
}

.api-pill.ready > span {
  background: #5f8c6d;
  box-shadow: 0 0 0 3px #e4ede6;
}

.api-pill.checking > span {
  background: #c29a56;
}

.avatar {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: #dddfd5;
  color: #526055;
  font-family: var(--serif);
  font-weight: 700;
}

.export-button {
  height: 30px;
  padding: 0 11px;
  border: 1px solid #d8d9d3;
  border-radius: 7px;
  background: transparent;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
}

.export-button:hover {
  background: #efeee9;
}

.mobile-menu,
.mobile-ai {
  display: none;
}

.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 252px minmax(440px, 1fr) 354px;
  overflow: hidden;
}

.manuscript-panel,
.ai-panel {
  position: relative;
  z-index: 20;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #f8f7f2;
}

.manuscript-panel {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
}

.panel-scroll,
.ai-scroll {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: #d2d0c9 transparent;
}

.panel-scroll::-webkit-scrollbar,
.ai-scroll::-webkit-scrollbar {
  width: 7px;
}

.panel-scroll::-webkit-scrollbar-thumb,
.ai-scroll::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: 99px;
  background: #c8c8c1;
  background-clip: padding-box;
}

.panel-scroll {
  flex: 1;
  padding: 26px 18px 18px;
}

.eyebrow-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #62665f;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.13em;
}

.more-button,
.section-label button {
  border: 0;
  background: transparent;
  color: #62665f;
  cursor: pointer;
}

.new-book-button {
  height: 25px;
  padding: 0 8px;
  border: 1px solid #ced6ce;
  border-radius: 6px;
  background: #edf1ec;
  color: #56705e;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.new-book-button:hover {
  background: #e3eae3;
}

.book-switcher {
  width: 100%;
  height: 33px;
  margin-top: 10px;
  padding: 0 9px;
  border: 1px solid #dedbd3;
  border-radius: 7px;
  background: #fffefa;
  color: #6d7069;
  font-size: 12px;
}

.book-title {
  width: 100%;
  margin-top: 12px;
  padding: 0;
  border: 0;
  background: transparent;
  color: #292b27;
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.book-meta {
  display: flex;
  gap: 14px;
  margin-top: 7px;
  color: #62665f;
  font-size: 12px;
}

.source-library {
  margin: 22px 0 24px;
}

.source-state {
  margin: 0 3px 8px;
  color: #62665f;
  font-size: 12px;
  line-height: 1.5;
}

.upload-source {
  width: 100%;
  height: 41px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid #cfd6ce;
  border-radius: 8px;
  background: #f0f3ee;
  color: #53685a;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.upload-source:hover {
  border-color: #9eada1;
  background: #e9eee8;
}

.upload-source span {
  font-size: 16px;
}

.source-library > p {
  margin: 7px 3px 0;
  color: #62665f;
  font-size: 12px;
  line-height: 1.5;
}

.source-info {
  min-height: 52px;
  display: grid;
  grid-template-columns: 32px 1fr 22px;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border: 1px solid #cdd6ce;
  border-radius: 8px;
  background: #edf2ec;
}

.source-icon {
  display: grid;
  place-items: center;
  width: 30px;
  height: 34px;
  border-radius: 4px 8px 4px 4px;
  background: var(--green);
  color: #fff;
  font-family: var(--serif);
  font-size: 12px;
}

.source-info > div {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.source-info strong {
  overflow: hidden;
  color: #4e5f53;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.source-info small {
  color: #62665f;
  font-size: 12px;
}

.source-info button {
  border: 0;
  background: transparent;
  color: #62665f;
  font-size: 16px;
  cursor: pointer;
}

.memory-tool {
  margin-top: 10px;
  padding: 11px;
  border: 1px solid #ddd9cf;
  border-radius: 9px;
  background: #fffdfa;
}

.memory-tool-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.memory-tool-head span {
  color: #686b64;
  font-size: 12px;
  font-weight: 700;
}

.memory-tool-head small {
  color: #62665f;
  font-size: 12px;
}

.memory-button {
  width: 100%;
  min-height: 37px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 7px 9px;
  border: 1px solid #85988a;
  border-radius: 7px;
  background: var(--green);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.memory-button:hover {
  background: var(--green-dark);
}

.memory-button.generating {
  border-color: #997d6d;
  background: #7d6559;
}

.memory-details {
  margin-top: 9px;
  border-top: 1px solid #e3e0d8;
}

.memory-details summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 1px 6px;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
  list-style: none;
}

.memory-details summary::-webkit-details-marker {
  display: none;
}

.memory-details textarea {
  width: 100%;
  min-height: 220px;
  padding: 9px;
  resize: vertical;
  border: 1px solid #dedbd3;
  border-radius: 7px;
  background: #fbfaf6;
  color: #5b5d57;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.65;
}

.section-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  color: #6e7069;
  font-size: 12px;
  font-weight: 700;
}

.section-label small {
  color: #62665f;
  font-family: Georgia, serif;
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0.12em;
}

.chapter-section-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.chapter-section-heading .section-label {
  min-width: 0;
  flex: 1;
  margin-bottom: 0;
}

.batch-chapter-toggle {
  padding: 4px 7px;
  border: 1px solid #d9ddd6;
  border-radius: 6px;
  background: #f8f8f3;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
}

.batch-chapter-toggle[aria-pressed="true"] {
  border-color: #9bac9f;
  background: #e7ede7;
  color: #3e5847;
}

.batch-chapter-toggle:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.chapter-batch-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 7px;
  padding: 7px;
  border: 1px solid #dfe2dc;
  border-radius: 7px;
  background: #f7f8f4;
}

.chapter-batch-toolbar span {
  min-width: 0;
  flex: 1;
  color: #62665f;
  font-size: 12px;
  text-align: center;
}

.chapter-batch-toolbar button {
  padding: 5px 7px;
  border: 1px solid #d7dbd4;
  border-radius: 5px;
  background: #fffefa;
  color: #677068;
  font-size: 12px;
  cursor: pointer;
}

.chapter-batch-toolbar button.danger {
  border-color: #dcc5be;
  color: #9a5b4c;
}

.chapter-batch-toolbar button:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

.chapter-list {
  display: grid;
  gap: 3px;
}

.chapter-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  border-radius: 7px;
}

.chapter-row.selecting {
  grid-template-columns: 34px minmax(0, 1fr);
}

.chapter-row.selecting .chapter.active {
  background: transparent;
}

.chapter-row.selecting.selected,
.chapter-row.selecting.selected .chapter {
  background: #dfe9e1;
}

.chapter-select {
  width: 16px;
  height: 16px;
  margin: 0 0 0 10px;
  accent-color: var(--green);
  cursor: pointer;
}

.chapter {
  position: relative;
  width: 100%;
  min-height: 58px;
  display: grid;
  grid-template-columns: 27px 1fr;
  align-items: center;
  gap: 7px;
  padding: 8px 10px;
  overflow: hidden;
  border: 0;
  border-radius: 7px;
  background: transparent;
  text-align: left;
  cursor: pointer;
  transition: background 0.18s ease;
}

.chapter-row.active {
  background: #e8ece6;
}

.chapter:hover {
  background: #efeee8;
}

.chapter.active {
  background: #e8ece6;
}

.chapter-number {
  align-self: start;
  padding-top: 4px;
  color: #62665f;
  font-family: Georgia, serif;
  font-size: 12px;
}

.chapter.active .chapter-number {
  color: var(--green);
}

.chapter-copy {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.chapter-copy strong {
  overflow: hidden;
  color: #5c5e57;
  font-family: var(--serif);
  font-size: 13px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter.active .chapter-copy strong {
  color: #314538;
}

.chapter-copy small {
  color: #62665f;
  font-size: 12px;
}

.chapter-actions {
  display: flex;
  gap: 1px;
  padding-right: 6px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.chapter-row:hover .chapter-actions,
.chapter-row:focus-within .chapter-actions {
  opacity: 1;
  pointer-events: auto;
}

.chapter-actions button {
  width: 25px;
  height: 25px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: #62665f;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.chapter-actions button:hover,
.chapter-actions button:focus-visible {
  background: #dfe5de;
  color: #405a49;
}

@media (hover: none) {
  .chapter-actions {
    opacity: 1;
    pointer-events: auto;
  }
}

.active-line {
  position: absolute;
  inset: 12px auto 12px 0;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--green);
}

.add-chapter {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 7px;
  padding: 0 12px;
  border: 1px dashed #d2d0c9;
  border-radius: 7px;
  background: transparent;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
}

.add-chapter span {
  color: var(--green);
  font-size: 16px;
}

.story-bible {
  margin-top: 24px;
  border-top: 1px solid #e1dfd9;
  border-bottom: 1px solid #e1dfd9;
}

.story-bible summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 2px;
  color: #6c6e67;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
}

.story-bible summary::-webkit-details-marker {
  display: none;
}

.story-bible summary small {
  color: #62665f;
  font-size: 12px;
  font-weight: 500;
}

.story-bible textarea {
  width: 100%;
  min-height: 120px;
  margin-bottom: 12px;
  padding: 10px;
  resize: vertical;
  border: 1px solid #dddcd6;
  border-radius: 7px;
  background: #fffefa;
  color: #63645e;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.75;
}

.setting-tip {
  margin: -5px 2px 12px;
  color: #62665f;
  font-size: 12px;
  line-height: 1.5;
}

.sidebar-footer {
  flex: 0 0 62px;
  height: 62px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px;
  border-top: 1px solid var(--line);
  color: #62665f;
}

.sidebar-footer > span {
  font-size: 20px;
}

.sidebar-footer div {
  display: grid;
  gap: 2px;
}

.sidebar-footer strong {
  color: #70746d;
  font-size: 12px;
  font-weight: 600;
}

.sidebar-footer small {
  color: #62665f;
  font-size: 12px;
}

.editor-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 20%, rgba(255, 255, 255, 0.9), transparent 45%),
    var(--paper);
}

.editor-toolbar {
  height: 51px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 27px;
  border-bottom: 1px solid #e5e2db;
  color: #62665f;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.tool-group button {
  min-width: 28px;
  height: 28px;
  padding: 0 7px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: #62665f;
  font-size: 13px;
  cursor: pointer;
}

.tool-group button:hover {
  background: #efeee9;
}

.tool-divider {
  width: 1px;
  height: 15px;
  margin: 0 6px;
  background: #d9d6ce;
}

.tool-group .format-label {
  min-width: 54px;
  padding: 0 7px;
  color: #686b65;
  font-size: 12px;
}

.focus-footer-button {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 29px;
  margin-left: 14px;
  padding: 5px 10px;
  border: 1px solid #e0ded7;
  border-radius: 99px;
  color: #62665f;
  font-size: 12px;
  background: #f8f7f2;
  cursor: pointer;
}

.focus-footer-button b {
  font-size: inherit;
  font-weight: 600;
}

.focus-footer-button:hover {
  border-color: #bdcabe;
  background: #edf2ed;
  color: #506456;
}

.chapter-footer-nav {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-left: 12px;
}

.chapter-footer-nav button {
  min-height: 29px;
  padding: 5px 10px;
  border: 1px solid #e0ded7;
  border-radius: 99px;
  background: #f8f7f2;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
}

.chapter-footer-nav button:hover:not(:disabled) {
  border-color: #bdcabe;
  background: #edf2ed;
  color: #506456;
}

.chapter-footer-nav button:disabled {
  cursor: default;
  opacity: 0.38;
}

.focus-writing .studio-shell {
  height: 100vh;
  height: 100dvh;
}

.focus-writing .topbar,
.focus-writing .editor-toolbar,
.focus-writing .chapter-kicker,
.focus-writing .chapter-title-input,
.focus-writing .title-rule,
.focus-writing .drawer-backdrop {
  display: none !important;
}

.focus-writing .workspace {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

.focus-writing .manuscript-panel,
.focus-writing .ai-panel {
  display: none !important;
}

.focus-writing .editor-panel {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: var(--paper);
}

.focus-writing .writing-sheet {
  width: min(980px, 100%);
  height: 100%;
  margin: 0 auto;
  padding: clamp(24px, 5vh, 58px) clamp(22px, 8vw, 120px) max(80px, calc(env(safe-area-inset-bottom) + 66px));
}

.focus-writing .novel-editor {
  min-height: 0;
  padding: 0 0 64px;
  font-size: clamp(17px, 1.45vw, 21px);
  line-height: 2.05;
}

.focus-writing .editor-footer {
  position: fixed;
  z-index: 120;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  width: auto;
  height: auto;
  padding: 0;
  border: 0;
  background: transparent;
  transform: translateX(-50%);
}

.focus-writing .editor-footer > :not(.chapter-footer-nav):not(.focus-footer-button) {
  display: none !important;
}

.focus-writing .chapter-footer-nav {
  gap: 7px;
  margin: 0;
}

.focus-writing .chapter-footer-nav button {
  min-height: 38px;
  padding: 8px 14px;
  border-color: rgba(65, 89, 72, 0.18);
  background: rgba(251, 250, 246, 0.94);
  color: #526457;
  box-shadow: 0 8px 24px rgba(40, 54, 44, 0.13);
  backdrop-filter: blur(8px);
}

.focus-writing .focus-footer-button {
  min-height: 38px;
  margin: 0;
  padding: 8px 16px;
  border-color: rgba(65, 89, 72, 0.2);
  background: rgba(74, 101, 82, 0.94);
  color: #fff;
  box-shadow: 0 8px 24px rgba(40, 54, 44, 0.2);
  backdrop-filter: blur(8px);
}

.writing-sheet {
  width: min(720px, calc(100% - 64px));
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  padding: 58px 28px 28px;
}

.chapter-kicker {
  color: #62665f;
  font-family: Georgia, serif;
  font-size: 12px;
  letter-spacing: 0.24em;
}

.chapter-title-input {
  width: 100%;
  margin-top: 11px;
  padding: 0;
  border: 0;
  background: transparent;
  color: #272924;
  font-family: var(--serif);
  font-size: clamp(28px, 3vw, 38px);
  font-weight: 700;
  letter-spacing: 0.03em;
}

.title-rule {
  width: 65px;
  height: 1px;
  margin: 22px 0 32px;
  background: #d9d2c8;
}

.title-rule span {
  display: block;
  width: 20px;
  height: 1px;
  background: var(--clay);
}

.novel-editor {
  width: 100%;
  flex: 1;
  min-height: 260px;
  padding: 0 2px 60px;
  resize: none;
  border: 0;
  background: transparent;
  color: #3f403b;
  caret-color: var(--clay);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 2.15;
  letter-spacing: 0.055em;
  scrollbar-width: thin;
}

.novel-editor::placeholder {
  color: #62665f;
}

.editor-footer {
  height: 39px;
  display: flex;
  align-items: center;
  padding: 0 27px;
  border-top: 1px solid #e5e2db;
  color: #62665f;
  font-size: 12px;
}

.footer-separator {
  margin: 0 8px;
}

.footer-spacer {
  flex: 1;
}

.quiet-tip {
  color: #62665f;
}

.ai-panel {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: #f7f6f1;
}

.ai-heading {
  padding: 25px 24px 17px;
  border-bottom: 1px solid #e0ded6;
}

.ai-heading > div {
  display: flex;
  align-items: center;
  gap: 9px;
}

.spark {
  display: grid;
  place-items: center;
  width: 27px;
  height: 27px;
  border-radius: 8px;
  background: var(--green-pale);
  color: var(--green);
  font-size: 14px;
}

.ai-heading h2 {
  margin: 0;
  font-family: var(--serif);
  font-size: 17px;
  letter-spacing: 0.05em;
}

.ai-heading p {
  margin: 9px 0 0 36px;
  color: #62665f;
  font-size: 12px;
}

.ai-tabs {
  display: grid !important;
  grid-template-columns: 1fr 1fr;
  gap: 4px !important;
  margin-top: 17px;
  padding: 3px;
  border-radius: 8px;
  background: #ecebe5;
}

.ai-tabs button {
  height: 29px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #62665f;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.ai-tabs button.active {
  background: #fffefa;
  color: #435c4a;
  box-shadow: 0 1px 4px rgba(44, 48, 43, 0.08);
}

.source-context-control {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-top: 10px;
  padding: 9px 10px;
  border: 1px solid #d9ddd6;
  border-radius: 8px;
  background: #f1f3ee;
  cursor: pointer;
}

.source-context-control input {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  margin: 1px 0 0;
  accent-color: var(--green);
  cursor: pointer;
}

.source-context-control > span {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.source-context-control b {
  color: #5d695f;
  font-size: 12px;
  font-weight: 650;
}

.source-context-control small {
  color: #62665f;
  font-size: 12px;
  line-height: 1.4;
}

.recent-context-note {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin-top: 8px;
  color: #62665f;
}

.recent-context-note span {
  color: #62665f;
  font-size: 12px;
  line-height: 1.4;
}

.recent-context-note small {
  font-size: 12px;
  line-height: 1.45;
}

.close-ai {
  display: none;
}

.ai-scroll {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px 22px 24px;
}

#creationPanel {
  width: 100%;
}

.chat-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: -20px -22px -24px;
}

.chat-messages {
  flex: 1;
  min-height: 280px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  padding: 20px 18px;
}

.chat-empty {
  max-width: 245px;
  display: grid;
  justify-items: center;
  gap: 8px;
  margin: auto;
  color: #62665f;
  text-align: center;
}

.chat-empty > span {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--green-pale);
  color: var(--green);
  font-size: 18px;
}

.chat-empty strong {
  color: #666a63;
  font-family: var(--serif);
  font-size: 14px;
}

.chat-empty p {
  margin: 0;
  font-size: 12px;
  line-height: 1.65;
}

.chat-message {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: 10px;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.75;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-message.user {
  align-self: flex-end;
  border-bottom-right-radius: 3px;
  background: var(--green);
  color: #fff;
}

.chat-message.assistant {
  align-self: flex-start;
  border: 1px solid #e1dfd7;
  border-bottom-left-radius: 3px;
  background: #fffefa;
  color: #555750;
}

.chat-message.streaming::after {
  content: "";
  display: inline-block;
  width: 1px;
  height: 1em;
  margin-left: 3px;
  vertical-align: -2px;
  background: var(--clay);
  animation: blink 0.8s steps(1) infinite;
}

.chat-composer {
  position: relative;
  padding: 12px 14px 27px;
  border-top: 1px solid #dfddd6;
  background: #f8f7f2;
}

.chat-drafts {
  display: grid;
  gap: 8px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  background: var(--paper);
}

.chat-drafts label {
  font-weight: 700;
  font-size: 12px;
}

.chat-drafts select,
.chat-drafts textarea {
  width: 100%;
  min-width: 0;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  font-size: 12px;
}

.chat-drafts textarea {
  height: 180px;
  resize: vertical;
  line-height: 1.75;
}

.chat-drafts p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.chat-draft-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chat-draft-actions button {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--green);
  border-radius: 6px;
  color: var(--green-dark);
  background: var(--paper);
  font-size: 12px;
  cursor: pointer;
}

.chat-composer textarea {
  width: 100%;
  min-height: 88px;
  padding: 11px 48px 11px 12px;
  resize: none;
  border: 1px solid #d9d8d1;
  border-radius: 9px;
  background: #fffefa;
  color: #555750;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.65;
}

.chat-composer button {
  position: absolute;
  right: 24px;
  bottom: 45px;
  width: 31px;
  height: 31px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  cursor: pointer;
}

.chat-composer button b {
  display: none;
}

.chat-composer small {
  position: absolute;
  bottom: 10px;
  left: 16px;
  color: #62665f;
  font-size: 12px;
}

.field-label,
.prompt-label {
  color: #62665f;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px;
  margin-top: 9px;
}

.mode-card {
  min-height: 55px;
  display: grid;
  justify-items: start;
  align-content: center;
  gap: 3px;
  padding: 8px 11px;
  border: 1px solid #dfddd6;
  border-radius: 8px;
  background: #fffefa;
  text-align: left;
  cursor: pointer;
  transition: 0.18s ease;
}

.mode-card:hover {
  border-color: #c8cec6;
}

.mode-card.active {
  border-color: #829486;
  background: var(--green-pale);
  box-shadow: inset 0 0 0 1px rgba(82, 108, 91, 0.08);
}

.mode-card strong {
  color: #5c5f58;
  font-size: 12px;
  font-weight: 600;
}

.mode-card.active strong {
  color: #3f5a48;
}

.mode-card small {
  color: #62665f;
  font-size: 12px;
}

.prompt-label {
  display: block;
  margin-top: 20px;
}

.prompt-label span {
  color: #62665f;
  font-weight: 400;
  letter-spacing: 0;
}

.instruction-input {
  width: 100%;
  height: 91px;
  margin-top: 8px;
  padding: 11px 12px;
  resize: vertical;
  border: 1px solid #dedcd5;
  border-radius: 8px;
  background: #fffefa;
  color: #60615b;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.65;
}

.instruction-input::placeholder {
  color: #62665f;
}

.instruction-priority-note {
  margin: 7px 2px 0;
  color: #62665f;
  font-size: 12px;
  line-height: 1.55;
}

.control-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 13px;
}

.control-row label {
  display: grid;
  gap: 6px;
  color: #62665f;
  font-size: 12px;
}

.control-row select {
  width: 100%;
  height: 35px;
  padding: 0 9px;
  border: 1px solid #dedcd5;
  border-radius: 7px;
  background: #fffefa;
  color: #686a63;
  font-size: 12px;
}

.generate-button {
  width: 100%;
  height: 45px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
  border: 0;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 7px 15px rgba(64, 86, 70, 0.16);
  cursor: pointer;
  transition: 0.18s ease;
}

.generate-button:hover {
  background: var(--green-dark);
  transform: translateY(-1px);
}

.generate-button.generating {
  background: #7d6559;
}

.generate-button kbd {
  position: absolute;
  right: 34px;
  padding: 2px 5px;
  border: 1px solid rgba(255, 255, 255, 0.17);
  border-radius: 4px;
  color: #fff;
  font-family: var(--sans);
  font-size: 12px;
}

.config-note,
.error-note {
  margin-top: 12px;
  padding: 11px 12px;
  border: 1px solid #ded7c7;
  border-radius: 8px;
  background: #f7f2e8;
}

.config-note strong,
.error-note strong {
  color: #746754;
  font-size: 12px;
}

.config-note p,
.error-note p {
  margin: 4px 0 0;
  color: #62665f;
  font-size: 12px;
  line-height: 1.55;
}

.config-note code {
  font-size: 12px;
}

.error-note {
  border-color: #e2ceca;
  background: #f9eeeb;
}

.error-note strong,
.error-note p {
  color: #93695f;
}

.error-note.saved-note {
  border-color: #cad9cc;
  background: #edf4ed;
}

.error-note.saved-note strong,
.error-note.saved-note p {
  color: #55705d;
}

.result-card {
  margin-top: 16px;
  overflow: hidden;
  border: 1px solid #d7dcd5;
  border-radius: 9px;
  background: #fffefa;
}

.result-head {
  height: 37px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  border-bottom: 1px solid #e7e5df;
  color: #6b746d;
  font-size: 12px;
}

.result-head > div {
  display: flex;
  align-items: center;
  gap: 6px;
}

.result-head button {
  border: 0;
  background: transparent;
  color: #62665f;
  font-size: 12px;
  cursor: pointer;
}

.live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #6d9878;
}

.pulse {
  animation: pulse 1.3s infinite;
}

@keyframes pulse {
  50% { opacity: 0.35; transform: scale(0.8); }
}

.result-text {
  max-height: 280px;
  overflow-y: auto;
  padding: 15px;
  color: #51534e;
  font-family: var(--serif);
  font-size: 12px;
  line-height: 1.9;
  white-space: pre-wrap;
}

.result-text.streaming::after {
  content: "";
  display: inline-block;
  width: 1px;
  height: 1em;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--clay);
  animation: blink 0.8s steps(1) infinite;
}

.cursor {
  display: inline-block;
  width: 1px;
  height: 1em;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--clay);
  animation: blink 0.8s steps(1) infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

.result-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px;
  padding: 10px;
  border-top: 1px solid #e7e5df;
}

.result-actions button {
  height: 32px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}

.insert-button {
  border: 0;
  background: var(--green);
  color: #fff;
}

.replace-button,
.restore-button {
  min-height: 36px;
  padding: 0 11px;
  border: 1px solid #8b9c8e;
  border-radius: 6px;
  background: #edf2ed;
  color: #405a49;
  cursor: pointer;
}

.restore-button {
  border-color: #bca99e;
  background: #f5efeb;
  color: #704f42;
}

.insert-button:disabled {
  background: #e6ece6;
  color: #617466;
  cursor: default;
}

.retry-button {
  padding: 0 11px;
  border: 1px solid #d8d7d1;
  background: transparent;
  color: #62665f;
}

.ai-footer-note {
  min-height: 41px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 15px;
  border-top: 1px solid var(--line);
  color: #62665f;
  font-size: 12px;
  text-align: center;
}

.drawer-backdrop {
  display: none;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 225px minmax(420px, 1fr) 325px;
  }

  .topbar {
    padding: 0 17px;
  }

  .brand-subtitle {
    display: none;
  }

  .writing-sheet {
    width: min(650px, calc(100% - 42px));
    padding-left: 18px;
    padding-right: 18px;
  }

  .ai-scroll {
    padding-left: 17px;
    padding-right: 17px;
  }
}

@media (max-width: 960px) {
  .topbar {
    grid-template-columns: auto 1fr auto;
    gap: 12px;
  }

  .mobile-menu {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 1px solid #dedcd5;
    border-radius: 7px;
    background: transparent;
    cursor: pointer;
  }

  .brand-name {
    font-size: 18px;
  }

  .document-state {
    display: none;
  }

  .workspace {
    grid-template-columns: minmax(420px, 1fr) 330px;
  }

  .manuscript-panel {
    position: fixed;
    z-index: 80;
    inset: 68px auto 0 0;
    width: 270px;
    transform: translateX(-105%);
    visibility: hidden;
    pointer-events: none;
    box-shadow: 18px 0 45px rgba(38, 40, 36, 0.14);
    transition: transform 0.25s ease, visibility 0.25s ease;
  }

  .manuscript-panel.open {
    transform: translateX(0);
    visibility: visible;
    pointer-events: auto;
  }

  .drawer-backdrop {
    position: fixed;
    z-index: 70;
    inset: 68px 0 0;
    display: block;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    border: 0;
    background: rgba(35, 37, 33, 0.24);
    backdrop-filter: blur(2px);
  }

  .drawer-backdrop.hidden {
    display: block !important;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }
}

@media (max-width: 750px) {
  .topbar {
    height: 60px;
    padding: 0 11px;
  }

  .brand-mark {
    width: 31px;
    height: 31px;
  }

  .api-pill,
  .avatar,
  .export-button {
    display: none;
  }

  .mobile-ai {
    display: block;
    height: 33px;
    padding: 0 12px;
    border: 0;
    border-radius: 7px;
    background: var(--green);
    color: #fff;
    font-size: 12px;
  }

  .workspace {
    height: auto;
    display: block;
  }

  .manuscript-panel {
    top: 60px;
  }

  .ai-panel {
    position: fixed;
    z-index: 85;
    inset: 60px 0 0 auto;
    width: min(370px, 92vw);
    transform: translateX(105%);
    visibility: hidden;
    pointer-events: none;
    box-shadow: -18px 0 45px rgba(38, 40, 36, 0.14);
    transition: transform 0.25s ease, visibility 0.25s ease;
  }

  .ai-panel.open {
    transform: translateX(0);
    visibility: visible;
    pointer-events: auto;
  }

  .close-ai {
    position: absolute;
    top: 17px;
    right: 18px;
    display: block;
    border: 0;
    background: transparent;
    color: #62665f;
    font-size: 22px;
  }

  .drawer-backdrop {
    top: 60px;
  }

  .editor-panel {
    height: 100%;
  }

  .editor-toolbar {
    height: 44px;
    padding: 0 14px;
  }

  .focus-footer-button {
    min-height: 31px;
    margin-left: 8px;
    padding: 5px 9px;
    flex: 0 0 auto;
  }

  .writing-sheet {
    width: 100%;
    padding: 35px 24px 20px;
  }

  .chapter-title-input {
    font-size: 29px;
  }

  .title-rule {
    margin: 19px 0 25px;
  }

  .novel-editor {
    font-size: 16px;
    line-height: 2;
  }

  .editor-footer {
    height: 46px;
    padding: 0 15px;
  }

  .editor-footer #currentWords,
  .editor-footer #readingTime,
  .editor-footer .footer-separator,
  .editor-footer .footer-spacer {
    display: none;
  }

  .chapter-footer-nav {
    gap: 4px;
    margin: 0 auto 0 0;
  }

  .chapter-footer-nav button {
    padding: 5px 9px;
  }

  .quiet-tip {
    display: none;
  }

  .focus-writing .writing-sheet {
    width: 100%;
    padding: 22px 18px max(78px, calc(env(safe-area-inset-bottom) + 64px));
  }

  .focus-writing .novel-editor {
    padding-bottom: 68px;
    font-size: 17px;
    line-height: 2;
  }

  .focus-writing .editor-footer {
    width: calc(100% - 20px);
    justify-content: center;
    gap: 6px;
  }

  .focus-writing .chapter-footer-nav {
    gap: 5px;
    margin: 0;
  }

  .focus-writing .chapter-footer-nav button {
    padding: 8px 10px;
  }

  .focus-writing .focus-footer-button {
    padding: 8px 12px;
  }
}

/* Small secondary copy stays readable against the warm paper palette. */
.brand-subtitle,
.eyebrow-row,
.book-meta,
.source-library > p,
.source-info small,
.memory-tool-head small,
.section-label small,
.batch-chapter-toggle,
.chapter-batch-toolbar span,
.chapter-batch-toolbar button,
.chapter-copy small,
.story-bible summary small,
.setting-tip,
.sidebar-footer small,
.editor-footer,
.ai-heading p,
.source-context-control small,
.recent-context-note,
.chat-empty,
.chat-composer small,
.mode-card small,
.instruction-priority-note,
.control-row label,
.prompt-label span,
.config-note p,
.error-note p,
.result-head,
.ai-footer-note {
  color: #62665f;
}

@media (pointer: coarse) {
  button,
  summary,
  select,
  input:not([type="checkbox"]):not([type="file"]) {
    min-height: 44px;
  }

  button:not(.chapter):not(.mode-card):not(.generate-button):not(.upload-source):not(.add-chapter) {
    min-width: 44px;
  }

  .chapter-actions button,
  .source-info button,
  .chat-composer button,
  .close-ai,
  .mobile-menu {
    width: 44px;
    height: 44px;
  }

  .mobile-ai {
    width: auto;
    min-width: 44px;
    height: 44px;
  }

  .source-context-control input,
  .chapter-select {
    width: 28px;
    height: 28px;
  }

  .source-info {
    grid-template-columns: 32px minmax(0, 1fr) 44px;
  }

  .chapter-row.selecting {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .chapter-select {
    margin-left: 8px;
  }
}

.error-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  overflow: auto;
  padding: 24px;
  background:
    radial-gradient(circle at 50% 20%, rgba(255, 255, 255, 0.92), transparent 45%),
    var(--canvas);
}

.error-card {
  width: min(440px, 100%);
  padding: 42px 34px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--paper);
  color: var(--ink);
  text-align: center;
  box-shadow: 0 18px 50px rgba(40, 44, 38, 0.08);
}

.error-card .brand-mark {
  margin: 0 auto 22px;
}

.error-code {
  margin: 0;
  color: var(--clay);
  font-family: Georgia, serif;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.2em;
}

.error-card h1 {
  margin: 10px 0 9px;
  font-family: var(--serif);
  font-size: clamp(25px, 5vw, 34px);
}

.error-card > p:not(.error-code) {
  margin: 0;
  color: #62665f;
  font-size: 14px;
  line-height: 1.7;
}

.error-home {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 25px;
  padding: 0 20px;
  border-radius: 8px;
  background: var(--green);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
}

.error-home:hover {
  background: var(--green-dark);
}

.error-home:focus-visible {
  outline: 2px solid rgba(82, 108, 91, 0.65);
  outline-offset: 3px;
}

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