:root {
  --bg:      #0d0d0f;
  --surface: #161618;
  --raised:  #1e1e22;
  --border:  #2a2a30;
  --ink:     #f0eadc;
  --muted:   #7a7580;
  --dim:     #3d3a45;
  --accent:  #e8894a;
  --accent2: #c86090;
  --green:   #3db87a;
  --page-w:  min(860px, 94vw);
  --radius:  14px;
  --font-serif: 'Iowan Old Style', 'Palatino Linotype', Georgia, serif;
  --font-mono:  ui-monospace, 'SF Mono', Menlo, monospace;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-serif);
  -webkit-font-smoothing: antialiased;
}

/* ── Scrollbar ──────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--dim); border-radius: 3px; }

/* ── Header ─────────────────────────────────── */
.top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.top-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--ink);
}
.top-logo .wordmark { font-size: 20px; font-weight: 700; letter-spacing: 0.5px; }
.top-logo .spark {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}
.top .spacer { flex: 1; }
.top .storage-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.back-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 14px;
  font-family: var(--font-mono);
}
.back-link:hover { color: var(--ink); }

/* ── Hero / Create ───────────────────────────── */
.hero {
  max-width: var(--page-w);
  margin: 56px auto 40px;
  padding: 0 16px;
}
.hero-label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 20px;
}
.create-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.create-card textarea {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 16px;
  font-family: var(--font-serif);
  resize: vertical;
  min-height: 90px;
  outline: none;
  transition: border-color 0.15s;
}
.create-card textarea:focus { border-color: var(--accent); }
.create-card textarea::placeholder { color: var(--dim); }

.create-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.create-hint { color: var(--muted); font-size: 12px; font-family: var(--font-mono); }
.btn-primary {
  padding: 10px 22px;
  font-size: 14px;
  font-weight: 700;
  font-family: var(--font-serif);
  background: var(--accent);
  color: #0d0d0f;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.15s;
}
.btn-primary:hover { opacity: 0.88; }
.btn-primary:disabled { opacity: 0.4; cursor: progress; }

/* ── Library ─────────────────────────────────── */
.library {
  max-width: var(--page-w);
  margin: 0 auto 100px;
  padding: 0 16px;
}
.section-head {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 16px;
}
.grid {
  display: grid;
  gap: 18px;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  transition: border-color 0.15s, transform 0.15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-2px); }
.card .cover {
  aspect-ratio: 4/3;
  background: var(--raised) center/cover no-repeat;
}
.card .cover.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--dim);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
}
.card .meta { padding: 12px 14px; flex: 1; }
.card .meta .t { font-weight: 700; font-size: 14px; margin-bottom: 4px; line-height: 1.3; }
.card .meta .s { color: var(--muted); font-size: 12px; line-height: 1.35; margin-bottom: 8px; }
.status-pill {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--raised);
  color: var(--muted);
}
.status-pill.ready { background: #1a2e22; color: var(--green); }
.status-pill.generating, .status-pill.illustrating { background: #2a1e10; color: var(--accent); }
.status-pill.failed { background: #2a1010; color: #e86060; }

/* ── Toast ───────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--raised);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 20px;
  font-family: var(--font-mono);
  font-size: 13px;
  transition: transform 0.25s;
  z-index: 100;
  white-space: nowrap;
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* ── Book Reader ─────────────────────────────── */
.reader-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.reader-header .book-title {
  font-size: 16px;
  font-weight: 700;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.page-counter {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}

/* Reader frame — narrow mode (default, stacked) */
.reader-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: calc(100vh - 57px);
  background: var(--bg);
  padding-bottom: 100px;
}

.book-stage {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}

.spread {
  display: none;
  flex-direction: column;
  width: 100%;
}
.spread.active { display: flex; }

/* Cover spread (narrow) */
.spread.cover-spread .cover-img {
  width: 100%;
  aspect-ratio: 16/9;
  background: var(--raised) center/cover no-repeat;
  display: block;
}
.spread.cover-spread .cover-img.empty {
  display: flex; align-items: center; justify-content: center;
  color: var(--dim); font-family: var(--font-mono); font-size: 12px; letter-spacing: 2px;
}
.spread.cover-spread .cover-text { padding: 36px 40px 28px; }
.spread.cover-spread .cover-text h1 { margin: 0 0 10px; font-size: 36px; line-height: 1.15; }
.spread.cover-spread .cover-text .synopsis { color: var(--muted); font-size: 16px; line-height: 1.5; font-style: italic; }
.spread.cover-spread .cover-text .prompt-tag { margin-top: 14px; font-family: var(--font-mono); font-size: 11px; color: var(--dim); }

/* Story page spread (narrow) — image on top, text below, one spread per page */
.spread.page-spread {
  min-height: calc(100vh - 57px);
}
.spread.page-spread .page-img {
  width: 100%;
  aspect-ratio: 4/3;
  background: var(--raised) center/cover no-repeat;
  display: block;
  border-bottom: 1px solid var(--border);
}
.spread.page-spread .page-img.empty {
  display: flex; align-items: center; justify-content: center;
  color: var(--dim); font-family: var(--font-mono); font-size: 12px; letter-spacing: 2px;
}
.spread.page-spread .page-img.loading { position: relative; overflow: hidden; }
.spread.page-spread .page-img.loading::after {
  content: ''; position: absolute; inset: 0;
  background: repeating-linear-gradient(90deg, transparent 0, rgba(255,255,255,0.04) 50%, transparent 100%);
  animation: shimmer 1.4s infinite; background-size: 200% 100%;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.spread.page-spread .page-text { padding: 32px 40px; }
.spread.page-spread .page-num {
  font-family: var(--font-mono); font-size: 11px; letter-spacing: 3px; text-transform: uppercase;
  color: var(--muted); margin-bottom: 14px; display: block;
}
.spread.page-spread .page-body { font-size: 19px; line-height: 1.65; }

/* ── Wide-screen layout (≥1100px): image left 2/3, text right 1/3, no scroll ──
   The active spread owns the full viewport height so the text column can
   overflow internally — no page scroll needed. Image is 'contain' so the
   full illustration is visible without cropping. */
@media (min-width: 1100px) {
  .book-stage {
    max-width: none;
    padding: 0 24px;
  }
  .spread.active {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(360px, 1fr);
    gap: 28px;
    height: calc(100vh - 57px - 80px); /* header + nav bar */
    padding: 16px 0 0;
  }
  .spread.cover-spread .cover-img,
  .spread.page-spread .page-img {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
    border-bottom: none;
    border-radius: 8px;
    background-size: contain;  /* feature the image whole */
    background-color: #0a0a0a;
  }
  .spread.cover-spread .cover-text,
  .spread.page-spread .page-text {
    overflow-y: auto;
    padding: 8px 24px 24px 0;
    align-self: stretch;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .spread.cover-spread .cover-text h1 { font-size: 42px; }
  .spread.page-spread .page-body { font-size: 20px; }
  /* Subtle scrollbar so the text column feels intentional, not broken */
  .spread .page-text::-webkit-scrollbar,
  .spread .cover-text::-webkit-scrollbar { width: 6px; }
  .spread .page-text::-webkit-scrollbar-thumb,
  .spread .cover-text::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.1); border-radius: 3px;
  }
}

/* ── Navigation ──────────────────────────────── */
.nav-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 14px 20px;
  z-index: 20;
}
.nav-btn {
  background: var(--raised);
  border: 1px solid var(--border);
  color: var(--ink);
  border-radius: 8px;
  padding: 8px 20px;
  font-size: 20px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  line-height: 1;
}
.nav-btn:hover:not(:disabled) { background: var(--dim); border-color: var(--muted); }
.nav-btn:disabled { opacity: 0.3; cursor: default; }
.nav-dots {
  display: flex;
  gap: 6px;
  align-items: center;
}
.nav-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--dim);
  transition: background 0.15s, width 0.15s;
  cursor: pointer;
}
.nav-dot.active { background: var(--accent); width: 18px; border-radius: 3px; }

/* ── Generating status ───────────────────────── */
.status-bar {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.status-bar .pulse { color: var(--accent); }

/* ── Footer / ODC badge ──────────────────────── */
footer {
  border-top: 1px solid var(--border);
  padding: 24px 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 11px;
}
.odc-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px 3px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.5px;
  text-decoration: none;
  transition: border-color 0.15s, color 0.15s;
}
.odc-badge:hover { border-color: var(--green); color: var(--green); }
.odc-badge .check {
  width: 12px;
  height: 12px;
  background: var(--green);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #0d0d0f;
  font-size: 8px;
  font-weight: 900;
  flex-shrink: 0;
}

/* ── Empty state ──────────────────────────────── */
.empty {
  text-align: center;
  padding: 48px 0;
  color: var(--muted);
  font-size: 14px;
}

/* ── Responsive ──────────────────────────────── */
@media (max-width: 600px) {
  .top { padding: 14px 18px; }
  .hero, .library { padding: 0 12px; }
  .create-card { padding: 20px; }
  .spread.page-spread .page-text { padding: 22px 22px; }
  .spread.page-spread .page-body { font-size: 17px; }
  .spread.cover-spread .cover-text { padding: 24px 22px 20px; }
  .spread.cover-spread .cover-text h1 { font-size: 26px; }
  .create-footer { flex-direction: column; align-items: flex-end; }
}

/* ── Auth ──────────────────────────────── */
.auth-slot { display: flex; align-items: center; gap: 10px; }
.auth-email {
  font-size: 12px; color: var(--muted); font-family: 'Iowan Old Style', 'Palatino', Georgia, serif;
}
.btn-compact {
  padding: 6px 12px; font-size: 12px; border-radius: 6px;
}
.btn-ghost {
  background: transparent; color: var(--fg);
  border: 1px solid rgba(255,255,255,0.15); cursor: pointer;
  font-family: inherit;
}
.btn-ghost:hover { border-color: rgba(255,255,255,0.35); background: rgba(255,255,255,0.04); }
.btn-danger { color: #f87171; border-color: rgba(248,113,113,0.25); }
.btn-danger:hover { background: rgba(248,113,113,0.08); border-color: rgba(248,113,113,0.5); }

.auth-card input[type="email"], .auth-card input[type="password"] {
  width: 100%;
  background: rgba(255,255,255,0.04); color: var(--fg);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px; padding: 12px 14px; font-size: 14px;
  font-family: inherit; margin-bottom: 10px;
}
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.auth-tab {
  flex: 0 0 auto; padding: 6px 14px; font-size: 12px; font-family: inherit;
  background: transparent; color: var(--muted);
  border: 1px solid rgba(255,255,255,0.1); border-radius: 999px;
  cursor: pointer;
}
.auth-tab.active { color: #0d0d0f; background: var(--accent); border-color: var(--accent); font-weight: 700; }

/* ── Visibility toggle on create form ──── */
.visibility-toggle {
  display: inline-flex; align-items: center; gap: 6px; margin-right: 14px;
  font-size: 12px; color: var(--muted); cursor: pointer;
}
.visibility-toggle input { margin: 0; }

/* ── Book card pills ──────────────────────────────── */
.card .pills { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.vis-pill {
  font-size: 10px; padding: 2px 8px; border-radius: 4px;
  text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700;
  font-family: 'Geist Mono', 'JetBrains Mono', monospace;
}
.vis-pill.private { background: rgba(248,113,113,0.12); color: #fca5a5; }
.vis-pill.yours   { background: rgba(234,179,8,0.14); color: #fbbf24; }

/* ── Reader owner controls ──────────── */
.owner-controls { display: flex; gap: 6px; align-items: center; }
.owner-controls button { font-family: inherit; }

/* ── Listen button ──────────────────────────────── */
.listen-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; font-size: 13px; font-family: inherit; font-weight: 600;
  background: rgba(255,255,255,0.04); color: var(--fg);
  border: 1px solid rgba(255,255,255,0.12); border-radius: 999px;
  cursor: pointer; transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.listen-btn:hover { background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.25); }
.listen-btn:active { transform: scale(0.97); }
.listen-btn.active {
  background: var(--accent); color: #0d0d0f; border-color: var(--accent);
}
.listen-btn.active .listen-icon { animation: listen-pulse 1.2s ease-in-out infinite; }
@keyframes listen-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.55; }
}
.listen-icon { font-size: 15px; line-height: 1; }
@media (max-width: 600px) {
  .listen-btn .listen-label { display: none; }
  .listen-btn { padding: 6px 10px; }
}

/* Failure-state block on cover */
.fail-box {
  margin-top: 18px; padding: 14px 16px;
  border: 1px solid rgba(248,113,113,0.3); border-radius: 10px;
  background: rgba(248,113,113,0.06);
}
.fail-row { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 12px; }
.fail-pill {
  font-family: var(--font-mono); font-size: 10px; font-weight: 700;
  padding: 2px 8px; border-radius: 4px; letter-spacing: 0.06em;
  text-transform: uppercase;
  background: rgba(248,113,113,0.18); color: #fca5a5; flex-shrink: 0; margin-top: 1px;
}
.fail-msg { font-family: var(--font-mono); font-size: 11.5px; color: #fca5a5; line-height: 1.45; word-break: break-word; }
.fail-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.fail-actions .btn-primary { padding: 8px 16px; font-size: 13px; }

/* ── Live-progress tile ─────────────── */
.card.card-inflight {
  border-color: rgba(232,137,74,0.45);
  box-shadow: 0 0 0 1px rgba(232,137,74,0.15), 0 0 24px rgba(232,137,74,0.08);
  animation: card-breathe 2.2s ease-in-out infinite;
}
@keyframes card-breathe {
  0%, 100% { box-shadow: 0 0 0 1px rgba(232,137,74,0.15), 0 0 24px rgba(232,137,74,0.06); }
  50%      { box-shadow: 0 0 0 1px rgba(232,137,74,0.4), 0 0 28px rgba(232,137,74,0.16); }
}
.progress-line {
  margin: 6px 0 2px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--accent);
  letter-spacing: 0.02em;
}
.card-spinner {
  display: inline-block;
  width: 28px; height: 28px;
  border: 3px solid rgba(232,137,74,0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Clickable toast variants ─────────────── */
#toast.clickable { cursor: pointer; transition: transform 0.25s, background-color 0.15s; }
#toast.clickable:hover { background: var(--accent); color: #0d0d0f; border-color: var(--accent); }
#toast.toast-ready { border-color: var(--green); background: #15301f; }
#toast.toast-ready:hover { background: var(--green); color: #0d0d0f; border-color: var(--green); }
#toast.toast-failed { border-color: rgba(248,113,113,0.5); background: #2a1515; color: #fca5a5; }

/* ── Slim anonymous sign-in banner ─────────── */
.auth-banner {
  max-width: var(--page-w);
  margin: 24px auto 32px;
  padding: 14px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
}
.auth-banner-msg { flex: 1; font-size: 14px; color: var(--muted); min-width: 240px; }
.auth-banner-msg strong { color: var(--ink); font-weight: 700; }
.auth-banner-actions { display: flex; gap: 8px; flex-shrink: 0; }
@media (max-width: 520px) {
  .auth-banner { margin: 16px 12px; padding: 12px 14px; }
  .auth-banner-actions { width: 100%; }
  .auth-banner-actions button { flex: 1; }
}
