@import url('https://fonts.googleapis.com/css2?family=Source+Serif+4:opsz,wght@8..60,400;8..60,600;8..60,700&family=Newsreader:opsz,wght@6..72,400;6..72,500&display=swap');

:root {
  --ink: #1a1a1a;
  --ink-soft: #4a4a48;
  --ink-faint: #8b8b86;
  --line: #e6e6e1;
  --paper: #fffefb;
  --paper-2: #faf9f5;
  --gold: #b0872c;
  --serif: 'Source Serif 4', Georgia, 'Times New Roman', serif;
  --display: 'Newsreader', Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --wrap: 42rem;
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #ece9e3;
    --ink-soft: #b7b3aa;
    --ink-faint: #7d7a72;
    --line: #33322e;
    --paper: #16150f;
    --paper-2: #1c1b15;
    --gold: #d9ab55;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 10px 30px rgba(0,0,0,0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; }

button { font-family: var(--sans); cursor: pointer; }

.btn {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  border-radius: 999px;
  padding: 0.5rem 1.15rem;
  font-size: 0.9rem;
  font-weight: 500;
  transition: opacity 0.15s ease;
  text-decoration: none;
  display: inline-block;
}
.btn:hover { opacity: 0.8; }
.btn.ghost { background: transparent; color: var(--ink); }
.btn.ghost:hover { background: var(--paper-2); opacity: 1; }
.btn.ghost.danger { color: #c0392b; border-color: color-mix(in srgb, #c0392b 40%, var(--line)); }
.btn.tiny { padding: 0.3rem 0.75rem; font-size: 0.8rem; }
.btn:disabled { opacity: 0.4; cursor: default; }

.linkbtn {
  border: none;
  background: none;
  color: var(--ink-faint);
  font-size: 0.85rem;
  padding: 0.3rem 0.4rem;
  text-decoration: none;
}
.linkbtn:hover { color: var(--ink); }
.linkbtn:disabled { opacity: 0.4; }

/* ---------- top bar ---------- */
.topbar {
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  z-index: 20;
}
.topbar-inner {
  max-width: 64rem;
  margin: 0 auto;
  padding: 0.85rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.wordmark {
  font-family: var(--display);
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: -0.01em;
  text-decoration: none;
  display: flex;
  align-items: baseline;
  gap: 0.15rem;
}
.wordmark .dot { color: var(--ink-faint); }
.topbar-actions { display: flex; align-items: center; gap: 0.6rem; }

/* ---------- generic page ---------- */
.page {
  max-width: var(--wrap);
  margin: 0 auto;
  padding: 3.5rem 1.25rem 6rem;
}

.eyebrow {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-faint);
  margin-bottom: 0.6rem;
}

/* ---------- covers ---------- */
.cover {
  flex: none;
  border-radius: 4px;
  overflow: hidden;
  background: var(--paper-2);
  box-shadow: var(--shadow);
  aspect-ratio: 2 / 3;
}
.cover img { width: 100%; height: 100%; object-fit: contain; display: block; background: var(--paper-2); }
.cover-fallback {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  background: linear-gradient(150deg, var(--paper-2), color-mix(in srgb, var(--ink) 8%, var(--paper-2)));
  border: 1px solid var(--line);
  border-radius: 4px;
}
.cover-fallback span {
  font-family: var(--display);
  font-size: 1.1rem;
  color: var(--ink-faint);
  letter-spacing: 0.05em;
}
.cover.sm { width: 62px; }
.cover.md { width: 104px; }
.cover.md .cover-fallback span { font-size: 1.5rem; }
.cover.lg { width: 132px; }
.cover.lg .cover-fallback span { font-size: 1.9rem; }

/* ---------- stars ---------- */
.stars { display: inline-flex; gap: 0.1rem; align-items: center; }
.stars .star {
  border: none;
  background: none;
  padding: 0;
  line-height: 0;
  color: color-mix(in srgb, var(--ink-faint) 45%, transparent);
}
.stars .star svg { fill: currentColor; display: block; }
.stars .star.on { color: var(--gold); }
.stars.interactive .star { cursor: pointer; padding: 0.1rem; }
.stars.interactive .star:hover { transform: scale(1.08); }

/* ---------- feed ---------- */
.feed-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.feed-head h1 {
  font-family: var(--display);
  font-weight: 400;
  font-size: 2rem;
  margin: 0;
  letter-spacing: -0.02em;
}
.feed-head p { margin: 0.3rem 0 0; color: var(--ink-faint); font-size: 0.95rem; }

.feed-controls {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  padding-bottom: 1.25rem;
  margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--line);
}
.feed-search {
  flex: 1;
  min-width: 12rem;
  font-family: var(--serif);
  font-size: 1rem;
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-2);
  color: var(--ink);
}
.feed-search:focus { outline: 2px solid var(--ink); outline-offset: 1px; }

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.segmented button {
  border: none;
  background: var(--paper);
  color: var(--ink-faint);
  font-size: 0.82rem;
  padding: 0.5rem 0.85rem;
}
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button.on { background: var(--ink); color: var(--paper); }

.entry-card {
  display: flex;
  gap: 1.15rem;
  text-decoration: none;
  padding: 1.6rem 0;
  border-bottom: 1px solid var(--line);
}
.entry-card:hover h2 { text-decoration: underline; text-underline-offset: 3px; }
.entry-card .cover.sm { transition: transform 0.15s ease; }
.entry-card:hover .cover.sm { transform: translateY(-2px); }
.entry-body { min-width: 0; }
.entry-card .meta {
  font-size: 0.8rem;
  color: var(--ink-faint);
  display: flex;
  gap: 0.4rem;
  align-items: center;
  margin-bottom: 0.4rem;
}
.entry-card .draft-tag { color: #b06a2c; }
.entry-card h2 {
  font-family: var(--serif);
  font-size: 1.45rem;
  font-weight: 700;
  line-height: 1.25;
  margin: 0 0 0.2rem;
  letter-spacing: -0.01em;
}
.entry-card .byline {
  font-family: var(--serif);
  font-style: italic;
  color: var(--ink-soft);
  font-size: 1rem;
  margin: 0 0 0.35rem;
}
.entry-card .stars { margin-bottom: 0.4rem; }
.entry-card .excerpt {
  font-family: var(--serif);
  color: var(--ink-soft);
  font-size: 1rem;
  line-height: 1.55;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.empty {
  text-align: center;
  padding: 5rem 1rem;
  color: var(--ink-faint);
}
.empty.small { padding: 3.5rem 1rem; }
.empty h2 {
  font-family: var(--display);
  font-weight: 400;
  color: var(--ink);
  font-size: 1.6rem;
  margin: 0 0 0.5rem;
}

/* ---------- editor ---------- */
.editor-bar {
  max-width: 64rem;
  margin: 0 auto;
  padding: 0.85rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.editor-back { font-size: 1.15rem; }
.editor-bar-right { display: flex; align-items: center; gap: 0.7rem; }
.word-count { font-size: 0.8rem; color: var(--ink-faint); white-space: nowrap; }
.save-state { font-size: 0.82rem; color: var(--ink-faint); min-width: 5.5rem; white-space: nowrap; }
.save-state.dirty { color: #b06a2c; }

.composer {
  max-width: var(--wrap);
  margin: 0 auto;
  padding: 2.5rem 1.25rem 8rem;
}
.composer-head {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
  margin-bottom: 1.75rem;
}
.composer-fields { flex: 1; min-width: 0; }

.field-title {
  font-family: var(--serif);
  font-size: 2.4rem;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: var(--ink);
  resize: none;
  overflow: hidden;
  margin-bottom: 0.4rem;
}
.field-author {
  font-family: var(--serif);
  font-style: italic;
  font-size: 1.2rem;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: var(--ink-soft);
  margin-bottom: 1rem;
}
.field-title::placeholder, .field-author::placeholder { color: var(--ink-faint); opacity: 0.6; }

.editor-meta { display: flex; align-items: center; gap: 1rem; }
.rating-field { display: flex; align-items: center; gap: 0.55rem; }
.rating-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--ink-faint);
}

.cover-panel { flex: none; width: 104px; text-align: center; }
.cover-slot { margin-bottom: 0.5rem; }
.cover-actions { display: flex; flex-direction: column; gap: 0.1rem; align-items: center; }
.cover-actions .linkbtn { font-size: 0.78rem; }

.prose {
  font-family: var(--serif);
  font-size: 1.26rem;
  line-height: 1.7;
  color: var(--ink);
  outline: none;
  min-height: 40vh;
}
.prose:empty::before {
  content: attr(data-placeholder);
  color: var(--ink-faint);
  opacity: 0.6;
}
.prose p { margin: 0 0 1.4rem; }
.prose h2 { font-size: 1.65rem; font-weight: 700; letter-spacing: -0.01em; margin: 2.2rem 0 0.9rem; }
.prose h3 { font-size: 1.32rem; font-weight: 700; margin: 1.9rem 0 0.7rem; }
.prose blockquote {
  margin: 1.6rem 0;
  padding-left: 1.2rem;
  border-left: 3px solid var(--ink);
  font-style: italic;
  color: var(--ink-soft);
}
.prose ul, .prose ol { margin: 0 0 1.4rem; padding-left: 1.4rem; }
.prose li { margin: 0.35rem 0; }
.prose a { text-decoration: underline; text-underline-offset: 2px; }
.prose code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0.05em 0.35em;
}
.prose pre { background: var(--paper-2); border: 1px solid var(--line); border-radius: 8px; padding: 1rem; overflow-x: auto; }
.prose pre code { background: none; border: none; padding: 0; }
.prose hr { border: none; text-align: center; margin: 2.5rem 0; }
.prose hr::before { content: '· · ·'; letter-spacing: 0.6em; color: var(--ink-faint); }

/* ---------- selection toolbar ---------- */
.fmt-toolbar {
  position: absolute;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.1rem;
  background: var(--ink);
  color: var(--paper);
  border-radius: 8px;
  padding: 0.25rem;
  box-shadow: 0 8px 30px rgba(0,0,0,0.25);
  transform: translate(-50%, -125%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s ease;
}
.fmt-toolbar.show { opacity: 1; pointer-events: auto; }
.fmt-toolbar button {
  border: none;
  background: none;
  color: var(--paper);
  min-width: 2rem;
  height: 2rem;
  padding: 0 0.4rem;
  border-radius: 5px;
  font-family: var(--serif);
  font-size: 0.95rem;
  display: grid;
  place-items: center;
}
.fmt-toolbar button svg { display: block; }
.fmt-toolbar button:hover { background: rgba(255,255,255,0.16); }
.fmt-toolbar button.active { background: rgba(255,255,255,0.28); }
.fmt-toolbar .sep { width: 1px; height: 1.3rem; background: rgba(255,255,255,0.25); margin: 0 0.2rem; }

/* ---------- reader ---------- */
.reader-head {
  display: flex;
  gap: 2rem;
  align-items: flex-start;
  justify-content: space-between;
  padding-bottom: 1.5rem;
  margin-bottom: 2.5rem;
  border-bottom: 1px solid var(--line);
}
.reader-head-text { min-width: 0; }
.reader h1 {
  font-family: var(--serif);
  font-size: 2.6rem;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 0 0 0.5rem;
}
.reader .byline {
  font-family: var(--serif);
  font-style: italic;
  font-size: 1.28rem;
  color: var(--ink-soft);
  margin: 0 0 0.6rem;
}
.reader-head .stars { margin-bottom: 0.7rem; }
.reader .info {
  font-size: 0.85rem;
  color: var(--ink-faint);
  display: flex;
  gap: 0.55rem;
  align-items: center;
  flex-wrap: wrap;
}
.reader .body { font-family: var(--serif); font-size: 1.26rem; line-height: 1.75; }
.reader .body :is(p,ul,ol,blockquote,h2,h3,pre) { margin-bottom: 1.5rem; }
.reader .body h2 { font-size: 1.65rem; margin-top: 2.4rem; }
.reader .body h3 { font-size: 1.32rem; margin-top: 2rem; }
.reader .body blockquote {
  padding-left: 1.2rem;
  border-left: 3px solid var(--ink);
  font-style: italic;
  color: var(--ink-soft);
}
.reader .body ul, .reader .body ol { padding-left: 1.4rem; }
.reader .body a { text-decoration: underline; text-underline-offset: 2px; }
.reader .body hr { border: none; text-align: center; }
.reader .body hr::before { content: '· · ·'; letter-spacing: 0.6em; color: var(--ink-faint); }
.reader-actions {
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 0.6rem;
}

/* ---------- login ---------- */
.login { max-width: 22rem; margin: 0 auto; padding: 7rem 1.25rem; text-align: center; }
.login .wordmark { font-size: 2.4rem; justify-content: center; margin-bottom: 0.5rem; }
.login p { color: var(--ink-faint); margin: 0 0 2rem; font-family: var(--serif); }
.login form { display: flex; flex-direction: column; gap: 0.75rem; }
.login input {
  font-family: var(--serif);
  font-size: 1.05rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-2);
  color: var(--ink);
  text-align: center;
}
.login input:focus { outline: 2px solid var(--ink); outline-offset: 1px; }
.login .err { color: #c0392b; font-size: 0.88rem; min-height: 1.2rem; }

/* ---------- toast + modal ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 2rem;
  transform: translateX(-50%) translateY(1rem);
  background: var(--ink);
  color: var(--paper);
  padding: 0.7rem 1.1rem;
  border-radius: 999px;
  font-size: 0.88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: grid;
  place-items: center;
  z-index: 80;
  padding: 1rem;
}
.modal {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1.75rem;
  max-width: 24rem;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}
.modal h3 { font-family: var(--display); font-weight: 400; font-size: 1.4rem; margin: 0 0 0.5rem; }
.modal p { color: var(--ink-soft); font-family: var(--serif); margin: 0 0 1.5rem; }
.modal .row { display: flex; gap: 0.6rem; justify-content: center; }
.modal-input {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper-2);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1rem;
}
.modal-input:focus { outline: 2px solid var(--ink); outline-offset: 1px; }

@media (max-width: 640px) {
  .field-title { font-size: 1.9rem; }
  .reader h1 { font-size: 1.9rem; }
  .prose, .reader .body { font-size: 1.16rem; }
  .page { padding-top: 2rem; }
  .composer { padding-top: 1.75rem; }
  .composer-head { flex-direction: column-reverse; gap: 1rem; align-items: flex-start; }
  .cover-panel { width: 100%; display: flex; align-items: center; gap: 1rem; text-align: left; }
  .cover-panel .cover.md { width: 84px; }
  .cover-actions { flex-direction: row; flex-wrap: wrap; }
  .reader-head { flex-direction: column-reverse; gap: 1rem; }
  .reader-head .cover.lg { width: 108px; }
  .editor-bar-right { gap: 0.5rem; }
  .word-count { display: none; }
}
