/* Ebook Manager — reading-room design system (see DESIGN.md) */
:root {
  --scrim: rgba(38, 34, 28, 0.5);  /* dialog backdrop + drawer scrim (night overrides) */
  --paper: #F6F3EC;
  --card: #FFFDF8;
  --ink: #26221C;
  --ink-soft: #5C5648;
  --line: #E3DDD0;
  --accent: #7C2D2D;
  --accent-dk: #63201F;
  --accent-ink: #FFFDF8;
  --danger: #8A3B3B;
  --font-display: Georgia, 'Palatino Linotype', Palatino, serif;
  --font-body: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --fs-sm: 0.8125rem; --fs-base: 1rem; --fs-lg: 1.25rem; --fs-xl: 1.625rem; --fs-display: 2.5rem;
  --r-sm: 4px; --r-md: 8px;
  --e-xs: 0 1px 2px rgba(38, 34, 28, 0.05);   /* books at rest — whisper shadow (DESIGN.md) */
  --e-sm: 0 1px 3px rgba(38, 34, 28, 0.08);
  --e-md: 0 4px 16px rgba(38, 34, 28, 0.12);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--paper); color: var(--ink);
  font: 400 var(--fs-base)/1.5 var(--font-body);
  min-height: 100vh;
}
h1, h2, .wordmark, .book-title { font-family: var(--font-display); font-weight: 600; }

/* ---------- shell (sidebar + content) ---------- */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0; position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 2px;
  padding: 16px 12px; background: var(--card); border-right: 1px solid var(--line);
  overflow-y: auto;
}
.sidebar .wordmark { padding: 0 8px 12px; }
.side-heading {
  font-size: var(--fs-sm); font-weight: 600; color: var(--ink-soft);
  padding: 16px 8px 4px;
}
.side-link {
  display: flex; align-items: center; gap: 10px; text-align: left;
  background: transparent; border: none; color: var(--ink);
  padding: 7px 8px; border-radius: var(--r-sm); width: 100%;
}
.side-link svg { width: 16px; height: 16px; flex-shrink: 0; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.side-link:hover { background: var(--line); }
.side-link.active { background: var(--accent); color: var(--accent-ink); }
.side-spacer { flex: 1; min-height: 24px; }
.side-foot { display: flex; flex-direction: column; gap: 8px; padding: 8px; }
#side-collections { display: flex; flex-direction: column; gap: 2px; }
input[type="search"] {
  font: inherit; color: var(--ink); background: var(--paper);
  border: 1px solid var(--line); border-radius: var(--r-sm);
  padding: 8px 12px; min-width: 0; margin-bottom: 8px;
}
input[type="search"]:focus { outline: none; border-color: var(--accent); }
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.content main { flex: 1; }

/* ---------- header ---------- */
header {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 16px 24px; background: var(--card);
  border-bottom: 1px solid var(--line);
}
.wordmark { font-size: var(--fs-xl); letter-spacing: -0.01em; margin: 0; }
.wordmark span { color: var(--accent); }
header .spacer { flex: 1; }

/* ---------- mobile drawer (sidebar toggle) ---------- */
#menu-btn { display: none; padding: 8px 10px; }
#menu-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }
#scrim { display: none; }  /* drawer-mode only — see the media query below */

/* ---------- controls ---------- */
input[type="text"], input[type="email"], input[type="password"] {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: var(--r-sm);
  padding: 8px 12px; min-width: 0;
}
input:focus { outline: none; border-color: var(--accent); }
button {
  font: inherit; cursor: pointer; border-radius: var(--r-sm);
  padding: 8px 16px; border: 1px solid transparent;
  transition: background 0.15s ease, transform 0.1s ease;
}
button:focus-visible, input:focus-visible, a:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: var(--accent-dk); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--line); }
.btn-ghost.active { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--line); }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: var(--accent-ink); }
a.btn-ghost {
  display: inline-flex; align-items: center; text-decoration: none;
  padding: 8px 16px; border-radius: var(--r-sm); font-size: var(--fs-base);
}
button:active:not(:disabled) { transform: translateY(1px); }

/* ---------- auth ---------- */
#auth-view {
  max-width: 360px; margin: 96px auto; padding: 32px;
  background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-sm);
  display: flex; flex-direction: column; gap: 12px;
}
#auth-view .wordmark { font-size: var(--fs-display); text-align: center; }
#auth-view .wordmark span { color: var(--accent); }
.error-line { color: var(--danger); font-size: var(--fs-sm); min-height: 1.2em; }

/* ---------- shelf ---------- */
main { max-width: 1200px; width: 100%; margin: 0 auto; padding: 24px; }
.view-head {
  display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap;
  margin-bottom: 24px;
}
.view-head h2 { margin: 0; font-size: var(--fs-xl); }
.view-actions { display: flex; gap: 8px; margin-left: auto; }
.view-actions button { padding: 4px 10px; font-size: var(--fs-sm); }
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 24px; flex-wrap: wrap; }
.toolbar input[type="text"] { flex: 1; min-width: 200px; }
.source-toggle { display: flex; gap: 0; flex-wrap: wrap; }
.source-toggle .btn-ghost + .btn-ghost { margin-left: -1px; }  /* joined segmented control */
.source-toggle .btn-ghost:first-child { border-radius: var(--r-sm) 0 0 var(--r-sm); }
.source-toggle .btn-ghost:last-child { border-radius: 0 var(--r-sm) var(--r-sm) 0; }
.source-toggle .btn-ghost.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.grid {
  display: grid; gap: 20px;
  grid-template-columns: repeat(auto-fill, minmax(136px, 1fr));
}
.tile { position: relative; }
.view-toggle .btn-ghost { padding: 6px 10px; }
.view-toggle svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; display: block; }
.tile-text { display: none; }  /* list-only: title + author beside the cover */
/* list view: same tiles as catalogue rows — hairline rules, right-aligned meta */
#grid.as-list { gap: 0; grid-template-columns: 1fr; }
#grid.as-list .tile {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
  min-width: 0;  /* grid item min-size: let the 1fr track constrain nowrap titles */
}
#grid.as-list .tile .cover { flex: none; width: 56px; }
#grid.as-list .tile .spine-title { display: none; }  /* unreadable at 56px; title sits beside */
#grid.as-list .tile-text {
  display: flex; flex-direction: column; gap: 2px;
  flex: 1; min-width: 0; cursor: pointer;
}
#grid.as-list .tile-title {
  font-family: var(--font-display); font-size: var(--fs-base); line-height: 1.3;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#grid.as-list .tile-author {
  font-size: var(--fs-sm); color: var(--ink-soft);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#grid.as-list .tile .tile-meta { margin-top: 0; flex: none; }
/* 3D book, Apple-style: art is FULL-BLEED; the case (--cover-c = the art's own
   edge colour) only shows through as letterbox bars, melting into the art; the
   spine is a translucent overlay printed on the art (DESIGN.md book3d). */
.tile .cover {
  border-radius: 2px 6px 6px 2px; box-shadow: var(--e-xs); overflow: hidden;
  background: var(--cover-c, var(--ink));
}
.tile .cover img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: contain;  /* bars take the case colour — seamless edge match */
}
.tile .cover::before {  /* spine: transparent-to-ink overlay, art shows through */
  content: ""; position: absolute; inset: 0 auto 0 0; width: 4.5%; z-index: 2;
  background:
    linear-gradient(90deg, rgba(255, 253, 248, 0.22), transparent 12%),
    linear-gradient(90deg,
      rgba(38, 34, 28, 0.30), rgba(38, 34, 28, 0.10) 55%,
      rgba(38, 34, 28, 0.28) 75%, transparent 100%);
  pointer-events: none;
}
.tile:hover .cover { box-shadow: var(--e-md); transform: translateY(-2px); }
.tile .cover { transition: box-shadow 0.15s ease, transform 0.15s ease; }
.tile-meta {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-top: 6px; min-height: 24px;
}
.tile-progress {
  font-size: var(--fs-sm); color: var(--ink-soft); font-variant-numeric: tabular-nums;
}
.tile-new {
  font-size: var(--fs-sm); font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--accent);
}
.tile-more {
  background: none; border: 0; color: var(--ink-soft);
  border-radius: 999px; width: 24px; height: 24px; padding: 0;
  font-size: 1rem; line-height: 1; cursor: pointer;
  transition: background 0.15s ease;
}
.tile-more:hover { background: var(--line); }
.tile-more:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile-more:disabled { opacity: 0.4; cursor: default; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; }
}
.cover {
  aspect-ratio: 2 / 3; background: var(--line);
  display: flex; align-items: center; justify-content: center;
  padding: 12px; text-align: center; position: relative; cursor: pointer;
}
.cover .spine-title {
  font-family: var(--font-display); font-size: var(--fs-base);
  color: var(--ink); line-height: 1.3;
  /* legible on any case colour (shows only when the cover img failed) */
  background: var(--paper); border-radius: var(--r-sm); padding: 6px 8px;
}
/* library tiles: .tile .cover img insets the jacket inside the coloured case */
.book-meta { padding: 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.book-title { font-size: 1rem; line-height: 1.3; }
.book-author { font-size: var(--fs-sm); color: var(--ink-soft); }
.badge {
  align-self: flex-start; font-size: 0.6875rem; letter-spacing: 0.05em;
  text-transform: uppercase; color: var(--ink-soft);
  border: 1px solid var(--line); border-radius: var(--r-sm); padding: 1px 6px;
}
.book-actions { display: flex; gap: 4px; padding: 0 12px 12px; flex-wrap: wrap; }
.book-actions button { padding: 4px 10px; font-size: var(--fs-sm); }
.shared-by { font-size: var(--fs-sm); color: var(--accent); font-style: italic; }

/* ---------- boot watchdog (inline script in index.html) ---------- */
#boot-fail {
  position: fixed; inset: auto 16px 16px 16px; z-index: 999;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
  background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-md);
  padding: 16px 24px; font-size: var(--fs-base); color: var(--ink);
}
#boot-fail button { padding: 6px 14px; }

/* ---------- find (z-library) ---------- */
.results { display: flex; flex-direction: column; }
.result-row {
  display: grid; grid-template-columns: 24px 90px 1fr auto; gap: 16px; align-items: start;
  padding: 16px 0; border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.result-row:hover { background: var(--card); box-shadow: var(--e-sm); }
.result-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.result-row img {
  width: 90px; height: 135px; object-fit: cover;
  background: var(--line); border-radius: var(--r-sm); box-shadow: var(--e-sm);
}
.rank { font-family: var(--font-display); font-size: var(--fs-lg); color: var(--ink-soft); padding-top: 2px; }
.result-main { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.result-title { font-family: var(--font-display); font-size: var(--fs-lg); }
.result-authors { font-style: italic; color: var(--accent); }
.result-sub { font-size: var(--fs-sm); color: var(--ink-soft); }
.result-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 4px;
  font-size: var(--fs-sm); color: var(--ink-soft); text-align: right; white-space: nowrap;
}
.result-rating { color: var(--accent); }
.queue-add.on-shelf {
  border-color: var(--accent); color: var(--accent);
  font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  font-size: 0.6875rem;
}
.queue-add.on-shelf:disabled { opacity: 1; cursor: default; }  /* state marker, not a dimmed action */

/* ---------- home ---------- */
.stats { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 32px; }
.stat {
  background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-sm);
  padding: 16px 24px; min-width: 140px;
}
.stat-num {
  font-family: var(--font-display); font-size: var(--fs-xl); font-weight: 600;
  font-variant-numeric: tabular-nums; line-height: 1.2;
}
.stat-label { font-size: var(--fs-sm); color: var(--ink-soft); }
.home-section { margin-bottom: 32px; }
.home-section h3 {
  font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 600;
  margin: 0 0 12px;
}
.strip {
  display: grid; gap: 20px;
  grid-template-columns: repeat(auto-fill, minmax(136px, 1fr));
  padding: 4px 4px 12px;
}

/* ---------- book store ---------- */
.source-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px; margin-bottom: 24px;
}
.source-card {
  background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-sm);
  padding: 20px 24px;
}
.source-card h3 { margin: 0 0 4px; font-size: var(--fs-lg); }
.source-card .result-sub { border-top: 1px solid var(--line); padding-top: 8px; margin: 8px 0 0; }
.source-status { color: var(--accent); font-style: italic; margin: 0; }

/* ---------- context menu ---------- */
.menu {
  position: fixed; z-index: 50; min-width: 200px;
  background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-md);
  padding: 4px; display: flex; flex-direction: column;
}
.menu button {
  background: transparent; border: none; color: var(--ink);
  text-align: left; padding: 8px 12px; border-radius: var(--r-sm);
}
.menu button:hover { background: var(--line); }
.menu button.danger { color: var(--danger); }
.menu-sep { height: 1px; background: var(--line); margin: 4px 0; }
.collection-row { justify-content: flex-start; gap: 8px; padding: 8px 4px; }

/* ---------- book detail panel ---------- */
#detail-dialog {
  width: min(720px, 94vw); max-height: 90vh; overflow-y: auto;
  padding: 24px; position: relative;
}
.detail-close { position: absolute; top: 16px; right: 16px; padding: 4px 10px; }
.detail-head { display: grid; grid-template-columns: 160px 1fr; gap: 24px; margin-bottom: 24px; padding-right: 40px; }
.detail-cover img {
  width: 160px; height: 240px; object-fit: cover;
  background: var(--line); border-radius: var(--r-sm); box-shadow: var(--e-sm);
}
.detail-info { display: flex; flex-direction: column; gap: 12px; }
.detail-title { margin: 0; font-size: var(--fs-xl); line-height: 1.25; }
.detail-authors { font-style: italic; color: var(--accent); }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.detail-facts {
  display: grid; grid-template-columns: max-content 1fr; gap: 8px 24px;
  margin: 0 0 24px; padding-top: 16px; border-top: 1px solid var(--line);
}
.detail-facts dt { color: var(--ink-soft); font-size: var(--fs-sm); }
.detail-facts dd { margin: 0; font-size: var(--fs-sm); overflow-wrap: anywhere; }
.detail-desc { margin: 0 0 24px; line-height: 1.6; }
#detail-related h3 {
  font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 600;
  margin: 0 0 12px; padding-top: 16px; border-top: 1px solid var(--line);
}
.related-strip {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 12px;
}
.related-item {
  background: transparent; border: none; padding: 0;
  display: flex; flex-direction: column; gap: 6px; text-align: left; cursor: pointer;
}
.related-item img {
  width: 100%; aspect-ratio: 2 / 3; object-fit: cover;
  background: var(--line); border-radius: var(--r-sm); box-shadow: var(--e-sm);
}
.related-item:hover .related-name { color: var(--accent); }
.related-name { font-size: var(--fs-sm); color: var(--ink-soft); line-height: 1.3; }

@media (max-width: 900px) {
  #menu-btn { display: inline-flex; }
  #scrim { display: block; position: fixed; inset: 0; z-index: 55; background: var(--scrim); }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    width: min(280px, 86vw); height: 100dvh;
    transform: translateX(-100%); transition: transform 0.2s ease;
    z-index: 60;
    padding-left: max(12px, env(safe-area-inset-left));
  }
  .sidebar.open { transform: none; box-shadow: var(--e-md); }
  .side-link { padding: 10px 8px; }
  header { padding: max(12px, env(safe-area-inset-top)) 16px 12px; }
}

@media (max-width: 640px) {
  main { padding: 16px; }
  header { padding-inline: 12px; }
  .admin-row, #admin-user-form { flex-wrap: wrap; }
  .admin-row input, #admin-user-form input, #admin-user-form select { flex: 1; min-width: 140px; }
  .stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat { padding: 16px; }
  .result-row { grid-template-columns: 20px 64px 1fr; }
  .result-row img { width: 64px; height: 96px; }
  .result-meta {
    grid-column: 3; flex-direction: row; flex-wrap: wrap;
    justify-content: flex-start; text-align: left; gap: 4px 12px; white-space: normal;
  }
  .detail-head { grid-template-columns: 1fr; gap: 16px; padding-right: 40px; }
  .detail-cover img { width: 128px; height: 192px; }
}

.empty {
  text-align: center; color: var(--ink-soft); padding: 64px 16px;
  font-family: var(--font-display); font-size: var(--fs-lg);
}
.empty small { display: block; font-family: var(--font-body); font-size: var(--fs-sm); margin-top: 8px; }

/* ---------- dialog ---------- */
dialog {
  border: none; border-radius: var(--r-md); box-shadow: var(--e-md);
  background: var(--card); color: var(--ink); padding: 24px;
  min-width: min(320px, calc(100vw - 24px));
}
dialog::backdrop { background: var(--scrim); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: var(--fs-lg); }

.skeleton { background: linear-gradient(90deg, var(--line) 25%, var(--card) 50%, var(--line) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: var(--r-sm); }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ---------- progress bars, transfers, download queue ---------- */
.bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; margin: 6px 0 4px; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 2px; transition: width 0.3s ease; }
.bar.indeterminate .bar-fill { width: 40% !important; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { margin-left: -40%; } to { margin-left: 100%; } }

#transfers {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 50;
  display: flex; flex-direction: column; gap: 8px;
  width: min(300px, calc(100vw - 32px));
}
.transfer { background: var(--card); border-radius: var(--r-md); box-shadow: var(--e-md); padding: 12px 16px; }
.transfer-name { font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.transfer-state { font-size: var(--fs-sm); color: var(--ink-soft); }
.transfer.failed .transfer-state { color: var(--danger); }

#downloads-badge {
  background: var(--accent); color: var(--accent-ink); border-radius: 999px;
  font-size: 0.6875rem; padding: 1px 7px; margin-left: 6px;
}
#downloads-dialog { width: min(560px, 94vw); max-height: 85vh; overflow-y: auto; }
#downloads-dialog h2 { font-family: var(--font-display); padding-right: 40px; }
#downloads-quota { margin: 4px 0 8px; }
.queue-row { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.queue-row:last-child { border-bottom: none; }
.queue-cover { width: 44px; height: 66px; object-fit: cover; background: var(--line); border-radius: var(--r-sm); flex: none; }
div.queue-cover.generated {
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display); font-size: var(--fs-lg); color: var(--ink-soft);
}
.queue-main { flex: 1; min-width: 0; }
.queue-title { font-family: var(--font-display); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queue-state { font-size: var(--fs-sm); color: var(--ink-soft); overflow-wrap: anywhere; }
.queue-state.failed { color: var(--danger); }
.queue-actions { display: flex; gap: 4px; flex: none; }
.queue-actions button { padding: 4px 10px; font-size: var(--fs-sm); }
.queue-add { font-size: var(--fs-sm); padding: 2px 8px; }

/* ---------- AI assistant chat ---------- */
#ai-dialog {
  width: min(560px, 94vw); height: min(640px, 85vh);
}
#ai-dialog[open] { display: flex; flex-direction: column; }
#ai-dialog h2 { font-family: var(--font-display); padding-right: 40px; }
#ai-messages {
  flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px 0 8px;
}
.ai-msg {
  max-width: 85%; padding: 8px 12px; border-radius: var(--r-md);
  font-size: var(--fs-base); line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.ai-user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); }
.ai-ai { align-self: flex-start; background: var(--paper); }
.ai-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.ai-action {
  display: flex; align-items: center; gap: 8px;
  background: var(--card); border-radius: var(--r-sm); box-shadow: var(--e-sm);
  padding: 8px 12px;
}
.ai-action-main { flex: 1; min-width: 0; }
.ai-action-title { font-family: var(--font-display); font-size: var(--fs-base); }
.ai-done { font-size: var(--fs-sm); color: var(--accent); font-style: italic; }
.ai-warn { font-size: var(--fs-sm); color: var(--danger); font-style: italic; }
.ai-diff { font-size: var(--fs-sm); color: var(--ink-soft); overflow-wrap: anywhere; }
.ai-diff b { color: var(--ink); font-weight: 600; }
.ai-action button { padding: 4px 10px; font-size: var(--fs-sm); flex: none; }
.ai-typing { display: inline-flex; gap: 4px; padding: 12px 14px; }
.ai-typing span {
  width: 6px; height: 6px; border-radius: 999px; background: var(--ink-soft);
  animation: ai-blink 1.2s infinite;
}
.ai-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes ai-blink { 0%, 60%, 100% { opacity: 0.3; } 30% { opacity: 1; } }
#ai-starters { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 0; }
.ai-starter { font-size: var(--fs-sm); padding: 4px 10px; }
#ai-form { flex-direction: row; gap: 8px; }
#ai-form input { flex: 1; }

/* ---------- upload dialog ---------- */
#upload-dialog { width: min(480px, 94vw); }
#upload-dialog h2 { font-family: var(--font-display); padding-right: 40px; }
.dropzone {
  width: 100%; display: flex; flex-direction: column; gap: 8px;
  align-items: center; justify-content: center; text-align: center;
  padding: 40px 24px; margin: 8px 0;
  background: var(--paper); border: 2px dashed var(--line); border-radius: var(--r-md);
  color: var(--ink); cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.dropzone:hover { border-color: var(--ink-soft); }
.dropzone:active { transform: translateY(1px); }
.dropzone.drag { border-color: var(--accent); background: var(--card); }
.dropzone-big { font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 600; }
.dropzone-sub { font-size: var(--fs-sm); color: var(--ink-soft); }

.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- admin dialog ---------- */
#admin-view { max-width: 720px; margin: 0 auto; }
#admin-view h3 {
  font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 600;
  margin: 18px 0 4px; padding-top: 14px; border-top: 1px solid var(--line);
}
.admin-tabs { display: flex; gap: 0; margin: 8px 0 4px; flex-wrap: wrap; }
.admin-tabs .btn-ghost { border-radius: 0; }
.admin-tabs .btn-ghost:first-child { border-radius: var(--r-sm) 0 0 var(--r-sm); }
.admin-tabs .btn-ghost:last-child { border-radius: 0 var(--r-sm) var(--r-sm) 0; }
.admin-table { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.admin-row {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px;
  border-bottom: 1px solid var(--line);
}
.admin-row:last-child { border-bottom: none; }
.admin-row:first-child { border-top: 1px solid var(--line); }
/* shared column grid so sibling sections (accounts / history / library)
   align: name | badge | detail | actions */
.admin-grid {
  display: grid; grid-template-columns: minmax(0, 1.6fr) 90px minmax(0, 1fr) auto;
  gap: 8px; align-items: center; padding: 6px 8px;
  border-bottom: 1px solid var(--line);
}
.admin-grid:last-child { border-bottom: none; }
.admin-grid:first-child { border-top: 1px solid var(--line); }
.admin-grid-head { color: var(--ink-soft); font-size: var(--fs-sm); font-weight: 600; }
.admin-grid .au-email { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-grid .au-actions { min-width: 178px; justify-content: flex-end; }
@media (max-width: 640px) {
  .admin-grid { grid-template-columns: minmax(0, 1fr) 90px auto; }
  .admin-grid .g-size { display: none; }  /* size/year drops out on phones */
}
.kindle-device {
  display: flex; align-items: baseline; gap: 8px; width: 100%; text-align: left;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--r-sm);
  background: none; cursor: pointer;
}
.kindle-device:hover { border-color: var(--ink-soft); }
.kindle-device:disabled { opacity: 0.6; cursor: default; }
.au-email { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.au-actions { display: flex; gap: 4px; flex-shrink: 0; }
.au-actions .btn-ghost, .au-actions .btn-primary { font-size: var(--fs-sm); padding: 4px 8px; }
.badge-warn { border-color: var(--ink-soft); color: var(--ink-soft); }
.badge-danger { border-color: var(--danger); color: var(--danger); }
.admin-pager { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
#admin-settings-form { display: flex; flex-direction: column; gap: 8px; }
.secret-row { display: flex; gap: 8px; align-items: center; }
.secret-row input { flex: 1; }
.secret-row .link { border: none; padding: 2px 4px; font-size: var(--fs-sm); }
.check-row { display: flex; gap: 6px; align-items: center; }
#admin-user-form { flex-direction: row; align-items: center; gap: 8px; border-bottom: none; }
#admin-user-form input[type="email"] { flex: 1; }
#admin-user-form input, #admin-user-form select { width: auto; }

/* ---- reader themes: token overrides, same warm hue family (DESIGN.md) ---- */
:root {
  --sepia-paper: #EFE8D8; --sepia-card: #F7F1E2; --sepia-ink: #3D3428;
  --sepia-ink-soft: #544C3E; --sepia-line: #DCD2BC;
  --night-paper: #211D18; --night-card: #2A251E; --night-ink: #D8D2C4;
  --night-ink-soft: #D6CFBF; --night-line: #3A342B; --night-accent: #EAC8C8;
  --night-accent-dk: #F0D2D2; --night-accent-ink: #211D18; --night-danger: #F4C4C4;
}
body[data-theme="sepia"] {
  --paper: var(--sepia-paper);
  --card: var(--sepia-card);
  --ink: var(--sepia-ink);
  --ink-soft: var(--sepia-ink-soft);
  --line: var(--sepia-line);
}
body[data-theme="night"] {
  color-scheme: dark;
  --paper: var(--night-paper);
  --card: var(--night-card);
  --ink: var(--night-ink);
  --ink-soft: var(--night-ink-soft);
  --line: var(--night-line);
  --accent: var(--night-accent);
  --accent-dk: var(--night-accent-dk);
  --accent-ink: var(--night-accent-ink);
  --danger: var(--night-danger);
  --e-xs: 0 1px 2px rgba(0, 0, 0, 0.2);
  --e-sm: 0 1px 3px rgba(0, 0, 0, 0.25);
  --e-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --scrim: rgba(0, 0, 0, 0.55);
}
@media (prefers-reduced-motion: no-preference) {
  body[data-theme] { transition: background-color .25s ease, color .25s ease; }
}

/* ---------- docs (in-app documentation; see web/docs.html) ---------- */
#docs-view { max-width: 720px; margin: 0 auto; }
.docs-body { padding-bottom: 32px; }
.docs-body h3 {
  font-family: var(--font-display); font-size: var(--fs-lg); font-weight: 600;
  margin: 18px 0 4px; padding-top: 14px; border-top: 1px solid var(--line);
}
.docs-body h3:first-child { border-top: none; padding-top: 0; margin-top: 8px; }
.docs-body h4 { font-size: var(--fs-base); font-weight: 600; margin: 18px 0 6px; }
.docs-body h5 {
  font-size: var(--fs-sm); font-weight: 600; color: var(--ink-soft);
  margin: 14px 0 4px;
}
.docs-body p, .docs-body ul, .docs-body ol { margin: 6px 0; }
.docs-body li { margin: 3px 0; }
.docs-body a { color: var(--accent); }
.docs-body code {
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: var(--fs-sm); background: var(--card);
  border: 1px solid var(--line); border-radius: var(--r-sm); padding: 1px 5px;
}
.docs-body pre {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r-sm);
  padding: 10px 12px; overflow-x: auto; margin: 8px 0;
}
.docs-body pre code { background: none; border: none; padding: 0; font-size: var(--fs-sm); line-height: 1.5; }
.docs-body table { width: 100%; border-collapse: collapse; margin: 6px 0 12px; }
.docs-body th {
  text-align: left; font-size: var(--fs-sm); font-weight: 600; color: var(--ink-soft);
  padding: 6px 8px 4px; border-bottom: 1px solid var(--line);
}
.docs-body td {
  font-size: var(--fs-sm); padding: 6px 8px;
  border-bottom: 1px solid var(--line); vertical-align: top;
}
.docs-body tbody tr:last-child td { border-bottom: none; }
.docs-body td:first-child code { white-space: nowrap; }
.docs-body td, .docs-body th, .docs-body li code { overflow-wrap: anywhere; }  /* 390px: long paths/tokens must break */
.docs-warn {
  font-size: var(--fs-sm); color: var(--ink-soft);
  border-left: 2px solid var(--accent); background: var(--card);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  padding: 8px 12px; margin: 12px 0;
}

/* ---------- Home: reading stats (CSS-only bar strip, no chart lib) ---------- */
#home-reading-stats { margin-top: 12px; }
.stat-bars {
  display: flex; align-items: flex-end; gap: 2px;
  height: 48px; margin: 8px 0 12px;
}
.stat-bar { flex: 1; background: var(--accent); opacity: .75; border-radius: 1px 1px 0 0; min-height: 2px; }
.stat-top { display: flex; flex-direction: column; gap: 4px; }
.stat-top-row { display: flex; justify-content: space-between; gap: 12px; font-size: var(--fs-sm); }
.stat-top-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-top-min { color: var(--ink-soft); font-variant-numeric: tabular-nums; flex: none; }
@media (prefers-reduced-motion: reduce) { .stat-bar { transition: none; } }

/* ---------- read status + rating ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 4px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--ink-soft);
  font: inherit; font-size: var(--fs-sm); padding: 4px 12px; border-radius: 999px; cursor: pointer;
}
.chip:hover { color: var(--ink); }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip:disabled { opacity: .4; cursor: default; }
.tile-status { color: var(--accent); font-size: var(--fs-sm); flex: none; }
.tile-rating { color: var(--accent); font-size: var(--fs-sm); flex: none; letter-spacing: -1px; }
.chip-sep { width: 12px; }
.series-group h3 { margin: 14px 0 8px; font-family: var(--font-display); }
.series-group .strip { display: flex; gap: 16px; overflow-x: auto; padding-bottom: 8px; }
@media (prefers-reduced-motion: reduce) { .chip { transition: none; } }

/* ---------- TTS error toast (no voices / engine failure) ---------- */
.tts-error {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 120px; z-index: 20;
  background: var(--danger); color: var(--accent-ink);
  padding: 10px 16px; border-radius: var(--r-md); box-shadow: var(--e-md);
  font-size: var(--fs-sm); cursor: pointer; max-width: min(420px, calc(100vw - 32px));
}
