/* ============================================================
   CODEX — Main Stylesheet
   Warm literary noir: deep charcoal, antique gold, fine typography
   ============================================================ */

/* --- Custom Properties (Dark default) --- */
:root {
  /* Palette */
  --bg-primary: #0e0e11;
  --bg-secondary: #161619;
  --bg-card: #1c1c20;
  --bg-card-hover: #222228;
  --bg-surface: #28282e;

  --text-primary: #e2d8c4;
  --text-secondary: #9e9684;
  --text-muted: #6b6459;

  --accent: #c9a44c;
  --accent-dim: #a6873b;
  --accent-glow: rgba(201, 164, 76, 0.15);

  --border: #2a2a30;
  --border-subtle: #1f1f25;

  /* Typography */
  --font-display: 'Cormorant Garamond', 'Georgia', serif;
  --font-body: 'DM Sans', 'Segoe UI', sans-serif;
  --font-reading: 'Lora', 'Georgia', serif;

  /* Spacing */
  --page-pad: clamp(1rem, 4vw, 3rem);
  --header-height: 60px;

  /* Transitions */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

/* --- Light theme --- */
[data-theme="light"] {
  --bg-primary: #f4f0e8;
  --bg-secondary: #ebe6db;
  --bg-card: #faf7f1;
  --bg-card-hover: #f0ece3;
  --bg-surface: #e6e1d6;

  --text-primary: #2a2520;
  --text-secondary: #6b6355;
  --text-muted: #9e9585;

  --accent: #9e7b2d;
  --accent-dim: #7d6124;
  --accent-glow: rgba(158, 123, 45, 0.12);

  --border: #d8d2c4;
  --border-subtle: #e2ddd0;
}

/* --- Reset & Base --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  transition: background 0.4s var(--ease-out), color 0.4s var(--ease-out);
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color 0.2s;
}

a:hover {
  color: var(--text-primary);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

img {
  max-width: 100%;
  display: block;
}

code {
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 0.875em;
  background: var(--bg-surface);
  padding: 0.15em 0.4em;
  border-radius: 3px;
}

/* --- Grain Overlay --- */
.grain-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.035;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-repeat: repeat;
  background-size: 200px 200px;
}

[data-theme="light"] .grain-overlay {
  opacity: 0.025;
}

/* --- Site Header --- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-subtle);
  backdrop-filter: blur(12px);
  transition: background 0.4s var(--ease-out);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--page-pad);
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-primary);
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.logo-icon {
  color: var(--accent);
  font-size: 1.1em;
}

.logo:hover {
  color: var(--accent);
}

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

.nav-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-muted);
  font-weight: 500;
}

/* --- Theme Toggle --- */
.theme-toggle {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: color 0.2s, background 0.2s;
}

.theme-toggle:hover {
  color: var(--accent);
  background: var(--accent-glow);
}

.icon-moon { display: none; }

[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* --- Library --- */
.library {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--page-pad);
  padding-bottom: 4rem;
}

.library-hero {
  padding: 4rem 0 3rem;
  text-align: center;
}

.library-title {
  font-family: var(--font-display);
  font-size: clamp(2.5rem, 6vw, 4rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text-primary);
  line-height: 1.1;
}

.library-subtitle {
  margin-top: 0.75rem;
  font-size: 1rem;
  color: var(--text-muted);
  font-style: italic;
  font-family: var(--font-display);
  font-size: 1.15rem;
}

/* --- Book Grid --- */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

.book-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.35s var(--ease-out),
              box-shadow 0.35s var(--ease-out),
              border-color 0.35s var(--ease-out),
              background 0.35s var(--ease-out);
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  animation: cardIn 0.6s var(--ease-out) both;
}

.book-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.3);
  border-color: var(--accent-dim);
  background: var(--bg-card-hover);
  color: inherit;
}

@keyframes cardIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.book-card-cover {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-bottom: 1px solid var(--border);
}

.book-card-cover-placeholder {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: linear-gradient(135deg, var(--bg-surface) 0%, var(--bg-secondary) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-display);
  font-size: 3rem;
  color: var(--accent-dim);
  opacity: 0.5;
}

.book-card-body {
  padding: 1.5rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.book-card-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.25;
  margin-bottom: 0.25rem;
}

.book-card-author {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
  font-style: italic;
}

.book-card-desc {
  font-size: 0.9rem;
  color: var(--text-secondary);
  line-height: 1.55;
  flex: 1;
}

.book-card-meta {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.book-card-tags {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.tag {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0.2rem 0.55rem;
  border-radius: 4px;
  background: var(--accent-glow);
  color: var(--accent);
  font-weight: 500;
}

.book-card-chapters {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* --- Continue reading badge --- */
.book-card-continue {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.book-card-continue::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* --- Empty state --- */
.library-empty {
  text-align: center;
  padding: 4rem 2rem;
  color: var(--text-muted);
  font-size: 1rem;
}

/* --- Footer --- */
.site-footer {
  text-align: center;
  padding: 2rem var(--page-pad);
  font-size: 0.75rem;
  color: var(--text-muted);
  letter-spacing: 0.08em;
  border-top: 1px solid var(--border-subtle);
}

/* --- Responsive --- */
@media (max-width: 640px) {
  .book-grid {
    grid-template-columns: 1fr;
  }

  .library-hero {
    padding: 2.5rem 0 2rem;
  }
}
