:root {
  --bg: #faf9f6;
  --fg: #26221c;
  --muted: #8a8377;
  --accent: #7c5cbf;
  --accent-dark: #5f4494;
  --card: #ffffff;
  --border: #e4e0d8;
  --error-bg: #fbeaea;
  --error-fg: #9c3434;
  --info-bg: #eaf3fb;
  --info-fg: #2c5f8a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.55;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.topbar .brand {
  font-weight: bold;
  font-size: 1.15rem;
  color: var(--accent-dark);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}
.brand-logo {
  height: 1.6em;
  width: 1.6em;
  border-radius: 6px;
}
.topbar nav a {
  margin-left: 1.2rem;
  color: var(--fg);
  text-decoration: none;
}
.topbar nav a:hover { color: var(--accent-dark); }
.topbar nav { display: inline-flex; align-items: baseline; }
.logout-form { display: inline; margin-left: 1.2rem; }
button.linklike {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
}
button.linklike:hover { background: none; color: var(--accent-dark); }

main { max-width: 62rem; margin: 0 auto; padding: 1.5rem; }

h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; margin-top: 1.8rem; }
h3 { font-size: 1rem; }

a { color: var(--accent-dark); }

.flashes { max-width: 62rem; margin: 1rem auto 0; padding: 0 1.5rem; }
.flash { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 0.5rem; }
.flash-error { background: var(--error-bg); color: var(--error-fg); }
.flash-info { background: var(--info-bg); color: var(--info-fg); }

.status {
  font-size: 0.75rem;
  font-family: system-ui, sans-serif;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--border);
  vertical-align: middle;
}
.status-outline_review, .status-outline_approved { background: #e7defa; color: var(--accent-dark); }
.status-generating, .status-outline_pending { background: #fdf0d5; color: #8a6d1d; }
.status-complete { background: #ddf0dd; color: #2f6b2f; }

table.story-list { width: 100%; border-collapse: collapse; }
.story-list th, .story-list td {
  text-align: left;
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--border);
}
.story-list th { font-family: system-ui, sans-serif; font-size: 0.8rem; color: var(--muted); }

form.stack { display: flex; flex-direction: column; gap: 1rem; max-width: 46rem; }
form.stack.narrow { max-width: 30rem; }
form.inline { display: inline-flex; gap: 0.5rem; align-items: center; margin: 0.4rem 0; }

label { display: flex; flex-direction: column; gap: 0.3rem; font-weight: 600; }
label small { font-weight: normal; color: var(--muted); }

input, textarea, select {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--fg);
}
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.row { display: flex; gap: 1rem; }
.row label { flex: 1; }

button {
  font: inherit;
  font-family: system-ui, sans-serif;
  padding: 0.55rem 1.2rem;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  align-self: flex-start;
}
button:hover { background: var(--accent-dark); }
button:disabled { background: var(--muted); cursor: not-allowed; }
button.secondary { background: transparent; color: var(--accent-dark); border: 1px solid var(--accent); }
button.secondary:hover { background: #f1ecfa; }
button.danger { background: transparent; color: var(--error-fg); border: 1px solid var(--error-fg); }
button.small { padding: 0.1rem 0.5rem; font-size: 0.75rem; }

.grid { display: grid; grid-template-columns: 3fr 2fr; gap: 2rem; }
@media (max-width: 50rem) { .grid { grid-template-columns: 1fr; } }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.2rem;
  margin-bottom: 1rem;
}

.muted { color: var(--muted); }
.empty { color: var(--muted); font-style: italic; }

.filebox {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  font-size: 0.8rem;
  overflow-x: auto;
  white-space: pre-wrap;
  max-height: 24rem;
  overflow-y: auto;
}

.doc-list { list-style: none; padding: 0; }
.doc-list li { padding: 0.3rem 0; border-bottom: 1px dashed var(--border); }

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr));
  gap: 0.8rem;
  margin-bottom: 1rem;
}
.chapter-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
}
.chapter-card.chapter-generating { border-color: var(--accent); }
.chapter-card.chapter-flagged { border-color: var(--error-fg); }
.chapter-head { display: flex; justify-content: space-between; align-items: baseline; }
.chapter-title { font-weight: 600; margin: 0.2rem 0; }
.status-done { background: #ddf0dd; color: #2f6b2f; }
.status-generating, .status-reviewing, .status-revising { background: #fdf0d5; color: #8a6d1d; }
.status-flagged { background: var(--error-bg); color: var(--error-fg); }
.finding {
  font-size: 0.8rem;
  color: var(--error-fg);
  background: var(--error-bg);
  border-radius: 5px;
  padding: 0.3rem 0.5rem;
  margin-top: 0.4rem;
}
/* Advisory (non-blocking) tags, e.g. the length note. */
.finding-warn {
  color: #8a6d1d;
  background: #fdf0d5;
}
/* Clean result, e.g. the continuity review finding nothing. */
.finding-pass {
  color: #2f6b2f;
  background: #ddf0dd;
}

/* Tag chips in the library table — links that filter the library by tag
   (the form-picker variant is button.tag-chip below). */
span.tag-chip, a.tag-chip, .tag-chip-active {
  display: inline-block;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.05rem 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0 0.2rem 0.15rem 0;
  white-space: nowrap;
}
a.tag-chip { text-decoration: none; }
a.tag-chip:hover { border-color: var(--accent); color: var(--accent-dark); background: #f1ecfa; }
.tag-chip-active { border-color: var(--accent); color: var(--accent-dark); background: #f1ecfa; }
span.tag-chip-neg { border-color: var(--error-fg); color: var(--error-fg); background: var(--error-bg); }
.library-controls { display: flex; gap: 0.5rem; margin-bottom: 0.8rem; }
.library-controls input[type="search"] { flex: 1; max-width: 24rem; }

/* Story covers: fixed 2:3 book aspect regardless of the exact size the image
   provider returned (400x600 may be clamped to a nearby supported size). */
.cover-thumb, .cover-medium, .cover-reader {
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.cover-thumb { width: 2rem; vertical-align: middle; margin-right: 0.5rem; }
.title-cell { white-space: nowrap; }
.cover-medium { width: 8.5rem; }
.cover-reader {
  display: block;
  width: 14rem;
  margin: 1rem auto 1.5rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}
.cover-section { display: flex; align-items: flex-start; gap: 1rem; margin: 0.8rem 0; }

/* Story ratings (AI + human). */
.rating-overall {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent-dark);
  font-family: system-ui, sans-serif;
  margin-right: 0.4rem;
}
.rating-dims { list-style: none; padding: 0; margin: 0.5rem 0 0.8rem; }
.rating-dims li { margin: 0.15rem 0; }

.read-link { font-size: 0.85rem; font-family: system-ui, sans-serif; margin-left: 0.8rem; }

.chapter-read {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.2rem 1.6rem;
  margin-bottom: 1.4rem;
}
.chapter-read-head {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-bottom: 0.4rem;
}
.prose { max-width: 42rem; margin: 0 auto; }
.prose h1 { font-size: 1.3rem; text-align: center; margin: 0.5rem 0 1.2rem; }
.prose p { text-indent: 1.4em; margin: 0.4rem 0; }
.prose p:first-of-type { text-indent: 0; }

.chapter-actions { margin-top: 1rem; border-top: 1px dashed var(--border); padding-top: 0.7rem; }
.chapter-actions summary { cursor: pointer; color: var(--accent-dark); font-family: system-ui, sans-serif; font-size: 0.9rem; }
.chapter-actions form.stack { margin-top: 0.8rem; }
.checkbox-label {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 0.4rem;
  font-weight: normal;
  font-size: 0.85rem;
}

/* --- reader reviews --- */
.hp-field { position: absolute; left: -9999px; top: -9999px; }
.reader-reviews { margin-top: 2.5rem; border-top: 1px solid var(--border); padding-top: 1rem; }
.reader-review { margin: 1rem 0; }
.reader-review .review-meta { margin: 0 0 0.2rem; }
.reader-review .review-comment { margin: 0; white-space: pre-line; }
.review-hidden { opacity: 0.5; }

/* --- settings page --- */
fieldset.settings-group {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.settings-group legend { font-weight: 600; font-family: system-ui, sans-serif; padding: 0 0.3rem; }
.settings-group div.stack { display: flex; flex-direction: column; gap: 1rem; }

/* --- tag picker (new-story form) & tag library --- */
fieldset.tag-picker {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  padding: 0.6rem 1rem 0.9rem;
}
.tag-picker legend { font-weight: 600; padding: 0 0.3rem; }
.tag-help { margin: 0.1rem 0 0.7rem; }
.tag-group { margin-bottom: 0.7rem; }
.tag-group:last-of-type { margin-bottom: 0; }
.tag-group-name {
  font-family: system-ui, sans-serif;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.tag-chips { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.25rem; }
button.tag-chip {
  padding: 0.15rem 0.6rem;
  font-size: 0.8rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
}
button.tag-chip:hover { background: #f1ecfa; border-color: var(--accent); color: var(--fg); }
button.tag-chip[data-state="pos"] { background: #ddf0dd; border-color: #2f6b2f; color: #2f6b2f; }
button.tag-chip[data-state="pos"]::before { content: "+ "; }
button.tag-chip[data-state="neg"] {
  background: var(--error-bg);
  border-color: var(--error-fg);
  color: var(--error-fg);
  text-decoration: line-through;
}
button.tag-chip[data-state="neg"]::before { content: "\2212 "; }

/* --- search page --- */
#search-controls .controls { margin-bottom: 0.8rem; }
#search-tag-picker { margin: 0 0 1.75rem; }
.story-table .chip { margin: 0 0.15rem 0.15rem 0; }
.story-table .thumb-col { width: 2.6rem; padding-right: 0; }
.table-thumb {
  display: block;
  width: 2.4rem;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}
.table-synopsis {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  font-size: 0.82rem;
  line-height: 1.4;
  color: var(--muted);
  margin-top: 0.2rem;
}

.tag-list { list-style: none; padding: 0; margin: 0; }
.tag-list li { padding: 0.4rem 0; border-bottom: 1px dashed var(--border); }
.tag-list li:last-child { border-bottom: none; }
.tag-list-head { display: flex; align-items: baseline; gap: 0.5rem; }
.tag-list-actions { margin-left: auto; display: inline-flex; gap: 0.5rem; align-items: baseline; font-size: 0.85rem; }
.tag-list-actions form.inline { margin: 0; }

.progress-card { display: flex; align-items: center; gap: 0.7rem; }
.spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- metrics dashboard --- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
  gap: 1rem;
  margin: 1.5rem 0;
}
.tile {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}
.tile-n { font-size: 1.9rem; font-weight: bold; color: var(--accent-dark); line-height: 1.1; }
.tile-label { font-family: system-ui, sans-serif; font-size: 0.78rem; color: var(--muted); }
.tile-warn .tile-n { color: var(--error-fg); }

.bars { list-style: none; padding: 0; margin: 0.4rem 0 0; }
.bars li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}
.bars li:last-child { border-bottom: none; }
.bar-n { font-weight: bold; font-variant-numeric: tabular-nums; }

.err-cell { font-size: 0.85rem; color: var(--error-fg); font-family: system-ui, sans-serif; }

/* --- plot map (story detail) --- */
.plotmap summary { cursor: pointer; }
.plotmap-scroll { overflow-x: auto; padding: 0.3rem 0; }
.plotmap-svg { display: block; }
.plotmap-svg .plotmap-payoff:hover path { stroke-width: 3.5; }
.plotmap-svg .plotmap-chapter:hover rect { stroke: var(--accent); }
.plotmap-legend {
  list-style: none;
  padding: 0;
  margin: 0.4rem 0 0;
  font-size: 0.85rem;
}
.plotmap-legend li { margin: 0.15rem 0; }
.plotmap-legend .chip {
  display: inline-block;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 2px;
  margin-right: 0.35rem;
  vertical-align: baseline;
}
