:root {
  --font-sans: "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-serif: Georgia, "Iowan Old Style", Palatino, "Palatino Linotype", "Times New Roman", serif;
  --text: #1a2030;
  --muted: #4e5668;
  --bg: #ffffff;
  --sidebar: #1b3a7a;
  --sidebar-text: #f5f7fc;
  --sidebar-muted: #c5d2ef;
  --sidebar-line: rgba(255, 255, 255, 0.22);
  --accent: #1b3a7a;
  --accent-hover: #152e62;
  --accent-soft: rgba(27, 58, 122, 0.1);
  --line: #e3e7ee;
  --danger: #b42318;
  --field: #f6f7fa;
  --measure: 65ch;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
}

.layout {
  display: grid;
  grid-template-columns: 16.75rem 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 2.1rem 1.45rem 1.4rem;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  height: 100vh;
  position: sticky;
  top: 0;
  overflow: hidden;
}
.sidebar .brand h1 { color: var(--sidebar-text); }
.sidebar .brand-sub,
.sidebar .empty-hint,
.sidebar .credits,
.sidebar .debug-panel,
.sidebar .debug-panel summary,
.sidebar .collaborator-list {
  color: var(--sidebar-muted);
}
.sidebar .list-heading,
.sidebar .collaborators h2 {
  color: var(--sidebar-text);
}
.sidebar .story-list,
.sidebar .collaborators,
.sidebar .credits,
.sidebar .debug-panel {
  border-top-color: var(--sidebar-line);
}
.sidebar .btn-secondary {
  color: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.8);
}
.sidebar .btn-secondary:hover {
  background: #ffffff;
  color: var(--sidebar);
}

.brand h1 {
  font-family: var(--font-serif);
  font-size: 1.42rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.02em;
  line-height: 1.2;
  text-wrap: balance;
  overflow-wrap: break-word;
}
.brand .brand-sub {
  margin: 0.35rem 0 0;
  font-family: var(--font-serif);
  font-size: 0.88rem;
  font-weight: 400;
  line-height: 1.3;
  color: var(--sidebar-muted);
}

.list-heading,
.collaborators h2 {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  color: var(--text);
  margin: 0.35rem 0 0;
  font-weight: 600;
}

.story-list {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0.35rem 0 0.5rem;
  border-top: 1px solid var(--line);
}
.story-list button {
  font: inherit;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 0.4rem 0.15rem;
  color: var(--sidebar-text);
  cursor: pointer;
  border-radius: 0.2rem;
}
.story-list button:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.08);
}
.story-list button.active {
  font-weight: 600;
  color: #ffffff;
  background: rgba(255, 255, 255, 0.12);
  box-shadow: inset 2px 0 0 #ffffff;
  padding-left: 0.55rem;
}

.empty-hint { color: var(--muted); font-size: 0.92rem; margin: 0; }

.collaborators {
  flex-shrink: 0;
  padding-top: 0.9rem;
  border-top: 1px solid var(--line);
}
.collaborator-list {
  list-style: none;
  margin: 0.55rem 0 0;
  padding: 0;
  color: var(--muted);
  font-size: 0.92rem;
}
.collaborator-list li {
  position: relative;
  padding: 0.18rem 0 0.18rem 0.85rem;
}
.collaborator-list li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.7em;
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: var(--sidebar-line);
}
.collaborator-list li.is-online::before { background: #ffffff; opacity: 0.75; }
.collaborator-list li.is-working::before {
  opacity: 1;
  animation: pulse 1.4s ease-in-out infinite;
}
.collaborator-list li.is-working { color: var(--sidebar-text); }
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.5; }
  50% { transform: scale(1.35); opacity: 1; }
}

.credits {
  flex-shrink: 0;
  margin: 0;
  padding-top: 0.7rem;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-family: var(--font-serif);
  font-size: 0.82rem;
  line-height: 1.45;
}

.debug-panel {
  flex-shrink: 0;
  margin-top: 0.2rem;
  padding-top: 0.7rem;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.8rem;
}
.debug-panel summary {
  cursor: pointer;
  color: var(--muted);
  list-style: none;
}
.debug-panel summary::-webkit-details-marker { display: none; }
.debug-panel summary::after { content: " ▾"; font-size: 0.75em; }
.debug-panel:not([open]) summary::after { content: " ▸"; }
.debug-body { margin-top: 0.55rem; }
.debug-body p, #debug-scores { margin: 0.3rem 0; }
#debug-scores { white-space: pre-wrap; }

main {
  padding: 2.4rem 3rem 4.5rem;
  max-width: calc(var(--measure) + 8rem);
}

.page-head {
  display: flex;
  justify-content: space-between;
  gap: 1.4rem;
  align-items: flex-start;
  margin-bottom: 1.6rem;
}
.page-head h2,
#composer-title {
  font-family: var(--font-serif);
  margin: 0 0 0.35rem;
  font-size: 2rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.15;
}
.meta { margin: 0; color: var(--muted); }
.actions { display: flex; gap: 0.85rem; align-items: center; flex-wrap: wrap; margin-left: auto; }
.toggle { font-size: 0.92rem; color: var(--muted); display: flex; gap: 0.4rem; align-items: center; }

.btn-primary, .btn-secondary {
  font: inherit;
  cursor: pointer;
  border-radius: 999px;
  padding: 0.48rem 1.05rem;
}
.btn-primary {
  background: var(--accent);
  color: #ffffff;
  border: 0;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 0;
  box-shadow: inset 0 0 0 1px var(--line);
}
.btn-secondary:hover { background: var(--accent-soft); }

.composer { max-width: var(--measure); }
.intro { margin: 0 0 1.15rem; color: var(--muted); }

.manuscript-head {
  margin: 0 0 1.8rem;
  max-width: var(--measure);
}
.kicker {
  margin: 0 0 0.45rem;
  font-size: 0.72rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.progress {
  margin: 0 0 0.9rem;
  font-size: 0.92rem;
  font-style: italic;
  color: var(--muted);
}
.manuscript-head h2 {
  font-family: var(--font-serif);
  font-size: 2.55rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.12;
  margin: 0 0 0.6rem;
}
.subtitle {
  margin: 0;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1.15rem;
  color: var(--muted);
  line-height: 1.45;
  max-width: var(--measure);
}
.subtitle:empty { display: none; }

.direction-wrap {
  max-width: var(--measure);
  margin: 0 0 2.4rem;
  padding-bottom: 1.3rem;
  border-bottom: 1px solid var(--line);
}
.direction-wrap summary {
  cursor: pointer;
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  list-style: none;
}
.direction-wrap summary::-webkit-details-marker { display: none; }
.direction { margin-top: 0.8rem; }

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.88rem;
  color: var(--muted);
  margin-bottom: 0.9rem;
}
input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--field);
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 0.45rem 0.15rem 0.5rem;
  width: 100%;
}
input::placeholder, textarea::placeholder {
  color: var(--muted);
  opacity: 1;
}
input:focus, textarea:focus {
  outline: none;
  border-bottom-color: var(--accent);
}
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.2rem 1.4rem; }

.hidden { display: none !important; }

.entries {
  list-style: none;
  padding: 0;
  margin: 0 0 0.8rem;
  max-width: var(--measure);
}
.entries li {
  padding: 2rem 0 1.7rem;
  border-top: 1px solid var(--line);
}
.entries li:first-child { border-top: 0; padding-top: 0.2rem; }
.chapter-label {
  margin: 0 0 0.45rem;
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}
.chapter-title {
  font-family: var(--font-serif);
  font-size: 1.45rem;
  font-weight: 600;
  margin: 0 0 0.7rem;
  letter-spacing: -0.02em;
  line-height: 1.25;
}
.epigraph {
  font-family: var(--font-serif);
  font-style: italic;
  color: var(--muted);
  margin: 0 0 1.1rem;
  line-height: 1.55;
}
.entries .content {
  margin: 0;
  white-space: pre-wrap;
  line-height: 1.8;
  font-family: var(--font-serif);
  font-size: 1.14rem;
}
.entries li:first-child .content::first-letter {
  font-size: 2.7rem;
  float: left;
  line-height: 0.8;
  padding: 0.12em 0.12em 0 0;
  font-weight: 600;
  color: var(--accent);
}
.credit {
  margin: 0.9rem 0 0;
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  color: var(--accent);
  opacity: 0;
}
.entries li.just-arrived .credit {
  animation: credit-in 1.1s ease-out forwards;
}
.how-written {
  margin: 1rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}
.how-written summary {
  cursor: pointer;
  list-style: none;
}
.how-written summary::-webkit-details-marker { display: none; }
.how-written .notes { margin: 0.45rem 0 0; line-height: 1.5; }

.entries li.just-arrived {
  animation: arrive 0.6s ease-out, land 1.8s ease-out;
}
@keyframes arrive {
  from { opacity: 0; transform: translateY(0.55rem); }
  to { opacity: 1; transform: none; }
}
@keyframes land {
  from { background-color: var(--accent-soft); }
  to { background-color: transparent; }
}
@keyframes credit-in {
  0% { opacity: 0; }
  30% { opacity: 1; }
  100% { opacity: 0.85; }
}

.live-note {
  color: var(--accent);
  min-height: 1.4em;
  margin: 0.5rem 0 0;
  font-size: 0.95rem;
  max-width: var(--measure);
}
.live-note.is-working::after {
  display: inline-block;
  width: 1em;
  margin-left: 0.1em;
  animation: dots 1.2s steps(4, end) infinite;
  content: "";
}
@keyframes dots {
  0% { content: ""; }
  25% { content: "."; }
  50% { content: ".."; }
  75% { content: "..."; }
}

.error-banner {
  color: var(--danger);
  padding: 0 0 1rem;
  margin: 0 0 0.4rem;
}

@media (prefers-reduced-motion: reduce) {
  .entries li.just-arrived,
  .entries li.just-arrived .credit,
  .collaborator-list li.is-working::before,
  .live-note.is-working::after {
    animation: none;
  }
  .live-note.is-working::after { content: "..."; }
  .entries li.just-arrived .credit { opacity: 0.85; }
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    height: auto;
    position: static;
    overflow: visible;
    border-bottom: 1px solid var(--line);
  }
  main { padding: 1.3rem 1.15rem 2.6rem; }
  .page-head { flex-direction: column; }
  .actions { margin-left: 0; }
  .grid { grid-template-columns: 1fr; }
  .manuscript-head h2 { font-size: 2rem; }
  .entries li:first-child .content::first-letter { font-size: 2.2rem; }
}
