:root {
  --bg: #ffffff;
  --bg-deep: #f6f7f8;
  --ink: #171717;
  --muted: #6b7280;
  --accent: #1f2937;
  --accent-deep: #111827;
  --card: #ffffff;
  --border: #e5e7eb;
  --shadow: 0 8px 24px rgba(17, 24, 39, 0.04);
  --radius: 0.4rem;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font-family: "Avenir Next", "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

a {
  color: inherit;
}

main {
  width: min(72rem, calc(100vw - 2rem));
  margin: 0 auto 4rem;
}

.site-header {
  width: min(72rem, calc(100vw - 2rem));
  margin: 0 auto;
  padding: 1rem 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

.site-header nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand {
  font-size: 1.15rem;
  font-weight: 700;
  text-decoration: none;
}

.button,
input[type="submit"] {
  appearance: none;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  padding: 0.75rem 1.15rem;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}

.button-light {
  background: #ffffff;
  color: var(--ink);
  border: 1px solid var(--border);
}

.subtle-link {
  color: #4b5563;
  text-decoration: none;
}

.flash-stack {
  width: min(72rem, calc(100vw - 2rem));
  margin: 0 auto 1rem;
}

.flash {
  margin: 0.25rem 0;
  padding: 0.8rem 1rem;
  border-radius: var(--radius);
  background: #ffffff;
  border: 1px solid var(--border);
}

.library-grid,
.card-stack,
.admin-grid {
  display: grid;
  gap: 1rem;
}

.library-grid {
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  align-items: start;
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

.page-sheet {
  position: relative;
  overflow: hidden;
  min-height: 19rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(17, 24, 39, 0.04);
}

.page-sheet__kicker {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.page-sheet__body {
  color: #374151;
  line-height: 1.65;
}

.library-book,
.library-card,
.card,
.reader-panel,
.page-card,
.book-row,
.page-link-card,
.page-preview-card,
.page-navigation__link {
  padding: 1.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
}

.library-book {
  display: grid;
  gap: 0.9rem;
  text-decoration: none;
  color: inherit;
  border: none;
  border-radius: 0;
  padding: 1rem;
  background: none;
  box-shadow: none;
  backdrop-filter: none;
}

.library-book:focus-visible {
  outline: 3px solid rgba(31, 41, 55, 0.18);
  outline-offset: 3px;
}

.library-book__cover {
  position: relative;
  aspect-ratio: 2 / 3;
  padding-right: 0.7rem;
  transform-origin: left center;
  transition: transform 160ms ease;
}

.library-book__cover::before {
  content: "";
  position: absolute;
  top: 0.45rem;
  right: 0;
  bottom: 0.45rem;
  width: 0.72rem;
  border-radius: 0 0.85rem 0.85rem 0;
  background:
    linear-gradient(180deg, #d1d5db, #9ca3af),
    linear-gradient(90deg, rgba(255, 255, 255, 0.5), transparent);
  box-shadow:
    inset 1px 0 0 rgba(255, 255, 255, 0.35),
    inset -1px 0 0 rgba(17, 24, 39, 0.08);
}

.library-book__cover-face {
  position: relative;
  height: 100%;
  overflow: hidden;
  border-radius: 0.9rem;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.7), transparent 34%),
    linear-gradient(145deg, #ffffff 0%, #f3f4f6 46%, #e5e7eb 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.78),
    inset 0 -14px 28px rgba(17, 24, 39, 0.04);
  transition: box-shadow 160ms ease;
}

.library-book__cover-face::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, transparent 58%, rgba(17, 24, 39, 0.04)),
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.45), transparent 30%);
  pointer-events: none;
}

.library-book__cover--image .library-book__cover-face {
  background: #f3f4f6;
}

.library-book:hover .library-book__cover {
  transform: perspective(1200px) translateY(-0.1rem) rotateY(-7deg) rotateX(2deg);
}

.library-book:hover .library-book__cover-face {
  box-shadow:
    0 14px 22px rgba(17, 24, 39, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.78),
    inset 0 -14px 28px rgba(17, 24, 39, 0.04);
}

.library-book__cover-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.library-book__fallback {
  height: 100%;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: #111827;
}

.library-book__fallback-kicker {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.library-book__fallback-title {
  margin: 0;
  font-size: clamp(1.15rem, 2.9vw, 1.7rem);
  line-height: 1.05;
}

.library-book__fallback-author {
  margin: 0;
  color: var(--muted);
}

.library-book__meta {
  display: grid;
  gap: 0.3rem;
}

.library-book__title {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.2;
}

.library-book__author,
.library-book__subtitle {
  margin: 0;
  color: var(--muted);
}

.admin-shell,
.reader-page {
  padding-bottom: 3rem;
}

.admin-header,
.section-header,
.page-card-header,
.book-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

.row-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.eyebrow,
.subtle {
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.8rem;
}

.stacked-form {
  display: grid;
  gap: 1rem;
}

.stacked-form label {
  display: grid;
  gap: 0.4rem;
}

.stacked-form input[type="text"],
.stacked-form input[type="email"],
.stacked-form input[type="password"],
.stacked-form input[type="file"],
.stacked-form select,
.stacked-form textarea {
  width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 0.9rem 1rem;
  font: inherit;
  background: rgba(255, 255, 255, 0.8);
}

.checkbox-row {
  grid-auto-flow: column;
  justify-content: start;
  align-items: center;
}

.reader-hero {
  padding: 3.5rem 0 2rem;
}

.reader-hero__actions {
  margin-top: 1.25rem;
}

.reader-hero h1 {
  font-size: clamp(2.25rem, 6vw, 4.5rem);
  margin-bottom: 0.5rem;
}

.reader-page {
  padding-top: 2rem;
}

.reader-page__book-link,
.reader-panel,
.reader-page .page-navigation {
  width: min(100%, 720px);
  margin-left: auto;
  margin-right: auto;
}

.reader-page__book-link {
  margin-bottom: 0.75rem;
}

.reader-panel {
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.reader-panel__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.reader-page__title {
  margin-top: 0;
  margin-bottom: 0;
}

.toc ol {
  padding-left: 1.25rem;
}

.pages-layout {
  display: grid;
  gap: 1rem;
}

.pages-layout[data-layout="grid"] {
  grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
}

.page-card {
  display: grid;
  gap: 1rem;
}

.prose {
  font-size: 1.1rem;
  line-height: 1.8;
}

.reader-page .prose {
  font-size: 1.2rem;
}

.reader-page .prose > .trix-content {
  line-height: inherit;
}

.trix-content h2 {
  font-size: 1.2rem;
}

.trix-content h3 {
  font-size: 1.1rem;
}

lexxy-editor h2 {
  font-size: 1.2rem;
}

lexxy-editor h3 {
  font-size: 1.1rem;
}

.page-preview-panel {
  padding: 0;
  background: transparent;
  box-shadow: none;
}

.page-preview-panel h2 {
  margin: 0 0 1.25rem;
}

.page-preview-panel .prose {
  font-size: 1.2rem;
  line-height: 1.8;
}

.page-preview-panel .prose > .trix-content {
  line-height: inherit;
}

.page-preview-panel .trix-content > :first-child {
  margin-top: 0;
}

.page-preview-panel .trix-content > :last-child {
  margin-bottom: 0;
}

.page-preview-panel .prose :where(h1, h2, h3, h4, h5, h6) {
  margin: 2rem 0 0.9rem;
  line-height: 1.25;
}

.page-preview-panel .prose :where(p, ul, ol, blockquote, pre, figure) {
  margin: 0 0 1.25rem;
}

.page-preview-panel .prose :where(ul, ol) {
  padding-left: 1.4rem;
}

.page-preview-panel .prose blockquote {
  margin-left: 0;
  padding-left: 1rem;
  border-left: 3px solid #d1d5db;
  color: var(--muted);
}

.detail-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(320px, 0.85fr);
  gap: 1rem;
}

.detail-layout > * {
  min-width: 0;
}

.detail-sidebar {
  display: grid;
  gap: 1rem;
  align-content: start;
}

.detail-list {
  margin: 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.85rem 1rem;
}

.detail-list dt {
  color: var(--muted);
  font-weight: 600;
}

.detail-list dd {
  margin: 0;
}

.stack-list {
  display: grid;
  gap: 0.75rem;
}

.sortable-pages-list {
  gap: 0.75rem;
}

.sortable-pages-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

.page-link-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: 1rem;
}

.page-link-card__main {
  min-width: 0;
  display: grid;
  gap: 0.75rem;
}

.page-link-card.is-dragging {
  opacity: 0.45;
}

.page-link-card.is-drop-target {
  border-radius: var(--radius);
  box-shadow: inset 0 0 0 2px rgba(31, 41, 55, 0.12);
}

.page-drag-handle {
  width: 2.25rem;
  height: 2.25rem;
  display: grid;
  place-items: center;
  border: 1px solid rgba(31, 26, 23, 0.08);
  border-radius: 999px;
  background: #ffffff;
  color: var(--muted);
  cursor: grab;
  padding: 0;
}

.page-drag-handle:active {
  cursor: grabbing;
}

.page-drag-handle__grip {
  width: 0.85rem;
  height: 1rem;
  background:
    radial-gradient(circle, currentColor 1.2px, transparent 1.3px) left top / 0.42rem 0.33rem repeat-y,
    radial-gradient(circle, currentColor 1.2px, transparent 1.3px) right top / 0.42rem 0.33rem repeat-y;
  opacity: 0.75;
}

.page-link-card p {
  margin: 0.2rem 0 0;
  color: var(--muted);
}

.page-link-card .page-sheet__kicker {
  margin: 0;
}

.page-link-card__meta {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}

.page-preview-card h2,
.page-navigation__link strong {
  margin: 0.35rem 0;
}

.page-preview-card {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 0.75rem;
  align-content: start;
  aspect-ratio: 5 / 7;
  text-decoration: none;
  color: inherit;
  padding: 1.4rem 1.3rem 1.2rem;
}

.page-preview-card.page-sheet h2 {
  font-size: 1.22rem;
  line-height: 1.2;
  margin: 0;
}

.page-preview-card__preview {
  max-height: 13.5rem;
  overflow: hidden;
  font-size: 0.98rem;
  color: #4b5563;
  mask-image: linear-gradient(180deg, #000 70%, transparent 100%);
}

.page-preview-card__preview p {
  margin: 0;
}

.page-link-card.page-sheet {
  min-height: 16rem;
  padding: 1.1rem 1rem 1rem;
}

.sortable-pages-grid .page-link-card.page-sheet {
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
}

.sortable-pages-grid .page-link-card > .subtle-link {
  justify-self: end;
  align-self: end;
}

.admin-book-show__meta,
.admin-book-show__pages {
  margin-bottom: 1rem;
}

.page-link-card.page-sheet .page-link-card__main strong a {
  color: inherit;
  text-decoration: none;
}

.page-link-card.page-sheet .page-link-card__main strong a:hover,
.page-link-card.page-sheet .page-link-card__main strong a:focus-visible {
  text-decoration: underline;
}

.page-link-card.page-sheet .page-sheet__body {
  max-height: 7.5rem;
  overflow: hidden;
  mask-image: linear-gradient(180deg, #000 72%, transparent 100%);
}

.page-link-card.page-sheet .page-sheet__body p {
  margin: 0;
  color: #4b5563;
}

.page-navigation {
  margin-top: 2rem;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

.page-navigation__link {
  display: grid;
  gap: 0.25rem;
  text-decoration: none;
}

.page-navigation__link--next {
  text-align: right;
}

.card-narrow {
  max-width: 52rem;
}

.reader-session-nav {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 8px 20px rgba(17, 24, 39, 0.08);
}

.reader-session-nav__identity {
  color: var(--muted);
  font-size: 0.95rem;
}

.page-autosave-status {
  position: sticky;
  right: 0;
  bottom: 0.5em;
  z-index: 30;
  width: 15%;
  min-width: 8rem;
  margin: 1rem 0 0 auto;
  padding: 0.55rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 4px 12px rgba(17, 24, 39, 0.05);
  color: var(--muted);
  text-align: center;
}

.page-autosave-status[data-state="unsaved"] {
  color: #374151;
}

.trix-content,
lexxy-editor,
trix-editor {
  min-height: 18rem;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  border-radius: var(--radius);
}

lexxy-toolbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: white;
}

trix-toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: block;
  padding: 0.35rem 0 0.5rem;
  background: white;
  backdrop-filter: blur(8px);
}

.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  overflow: hidden;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #ffffff;
  box-shadow: 0 4px 14px rgba(17, 24, 39, 0.04);
}

.prose th,
.prose td {
  padding: 0.9rem 1rem;
  border: 1px solid rgba(31, 26, 23, 0.08);
  text-align: left;
  vertical-align: top;
}

.prose th {
  background: #f9fafb;
  color: var(--ink);
  font-weight: 700;
}

.prose tr:nth-child(even) td {
  background: #fafafa;
}

@media (max-width: 800px) {
  .library-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .detail-layout,
  .site-header,
  .admin-header,
  .section-header,
  .page-card-header,
  .book-row,
  .reader-panel__header {
    flex-direction: column;
    align-items: flex-start;
  }

  .detail-layout {
    grid-template-columns: 1fr;
  }

  .page-navigation {
    grid-template-columns: 1fr;
  }

  .page-preview-card {
    aspect-ratio: auto;
    min-height: 16rem;
  }

  .page-preview-card__preview,
  .page-link-card.page-sheet .page-sheet__body {
    max-height: 8.5rem;
  }

  .sortable-pages-grid {
    grid-template-columns: 1fr;
  }

  .page-autosave-status {
    width: 100%;
    min-width: 0;
  }

  .page-navigation__link--next {
    text-align: left;
  }

  main,
  .site-header,
  .flash-stack {
    width: calc(100vw - 1.25rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  .library-book,
  .library-book__cover,
  .library-book__cover-face {
    transition: none;
  }

  .library-book:hover,
  .library-book:hover .library-book__cover {
    transform: none;
  }
}
