/* ============================================================
   OPEN LIBRARY — Dark Luxury Design System
   Cinzel + Cormorant Garamond + IBM Plex Mono
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;500;600;700&family=Cormorant+Garamond:ital,wght@0,300;0,400;0,500;0,600;1,300;1,400;1,500&family=IBM+Plex+Mono:wght@300;400;500&display=swap');

/* ---- TOKENS ---- */
:root {
  /* Dark mode palette */
  --bg:          #0d0c0b;
  --bg2:         #131210;
  --bg3:         #1c1a17;
  --bg4:         #242220;
  --border:      rgba(180,150,80,0.18);
  --border-hi:   rgba(180,150,80,0.45);
  --gold:        #c9a84c;
  --gold-hi:     #e2c47a;
  --gold-dim:    #8a6e32;
  --text:        #f0ead8;
  --sub:         #c8bfa8;
  --muted:       #8a7f6a;
  --mut2:        #5a5248;
  --red:         #c0392b;
  --card-shadow: 0 4px 32px rgba(0,0,0,0.6), 0 1px 0 rgba(180,150,80,0.08);

  /* Typography scale */
  --font-display: 'Cinzel', serif;
  --font-body:    'Cormorant Garamond', serif;
  --font-mono:    'IBM Plex Mono', monospace;

  --ease: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Light mode */
body.light {
  --bg:          #f5f0e8;
  --bg2:         #ede7da;
  --bg3:         #e2daca;
  --bg4:         #d4cbba;
  --border:      rgba(120,90,30,0.2);
  --border-hi:   rgba(120,90,30,0.5);
  --gold:        #8a5f1e;
  --gold-hi:     #6b4a14;
  --gold-dim:    #b89050;
  --text:        #1a1612;
  --sub:         #3a3028;
  --muted:       #6a5e4a;
  --mut2:        #9a8e7a;
  --card-shadow: 0 4px 32px rgba(0,0,0,0.12), 0 1px 0 rgba(120,90,30,0.1);
}

/* ---- GRAIN OVERLAY ---- */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.032;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='300' height='300' filter='url(%23n)' opacity='1'/%3E%3C/svg%3E");
  background-repeat: repeat;
}
body.light::before { opacity: 0.022; }

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

html { scroll-behavior: smooth; }

body {
  font-family: var(--font-body);
  font-size: 18px;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  transition: background 0.4s var(--ease), color 0.4s var(--ease);
  overflow-x: hidden;
}

/* ---- SCROLLBAR ---- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg2); }
::-webkit-scrollbar-thumb { background: var(--gold-dim); border-radius: 3px; }

/* ---- LAYOUT ---- */
.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 0 clamp(1rem, 4vw, 3rem);
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  backdrop-filter: blur(12px);
  transition: background 0.4s var(--ease), border-color 0.4s var(--ease);
}

.logo {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  text-decoration: none;
}

.logo-title {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  color: var(--gold);
  text-transform: uppercase;
}

.logo-sub {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: border-color 0.2s, color 0.2s, background 0.2s;
}
.theme-toggle:hover {
  border-color: var(--gold);
  color: var(--gold);
  background: rgba(201,168,76,0.08);
}

/* ---- MAIN CONTENT ---- */
.main-content {
  flex: 1;
  padding: clamp(2rem, 5vw, 4rem) clamp(1rem, 4vw, 3rem);
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* ---- HERO SEARCH ---- */
.hero {
  text-align: center;
  padding: clamp(2rem, 6vw, 5rem) 0 clamp(1.5rem, 4vw, 3rem);
}

.hero-eyebrow {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.25em;
  color: var(--gold-dim);
  text-transform: uppercase;
  margin-bottom: 1rem;
}

.hero-title {
  font-family: var(--font-display);
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: 400;
  letter-spacing: 0.04em;
  color: var(--text);
  line-height: 1.2;
  margin-bottom: 0.6rem;
}

.hero-title em {
  font-style: italic;
  color: var(--gold);
}

.hero-desc {
  font-size: 1.15rem;
  color: var(--sub);
  font-weight: 300;
  margin-bottom: 2.5rem;
  font-style: italic;
}

/* ---- SEARCH BOX ---- */
.search-container {
  max-width: 680px;
  margin: 0 auto;
}

.search-type-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 0;
  border-radius: 6px 6px 0 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-bottom: none;
  width: fit-content;
}

.tab-btn {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 0.55rem 1.4rem;
  border: none;
  background: var(--bg3);
  color: var(--muted);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.tab-btn:first-child { border-right: 1px solid var(--border); }
.tab-btn.active {
  background: var(--bg4);
  color: var(--gold);
}
.tab-btn:hover:not(.active) {
  background: var(--bg4);
  color: var(--sub);
}

.search-row {
  display: flex;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: 0 6px 6px 6px;
  overflow: hidden;
  background: var(--bg3);
  transition: border-color 0.2s;
}
.search-row:focus-within { border-color: var(--gold); }

.search-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: var(--font-body);
  font-size: 1.15rem;
  color: var(--text);
  padding: 1rem 1.25rem;
  caret-color: var(--gold);
}
.search-input::placeholder { color: var(--muted); font-style: italic; }

.search-btn {
  background: var(--gold);
  border: none;
  color: #0d0c0b;
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 0 1.8rem;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}
.search-btn:hover { background: var(--gold-hi); }
.search-btn:active { background: var(--gold-dim); }

/* ---- RESULTS ---- */
.results-section {
  margin-top: 3rem;
}

.results-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.results-count {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  color: var(--muted);
  text-transform: uppercase;
}

.results-count strong {
  color: var(--gold);
  font-weight: 500;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
  gap: 1.25rem;
}

/* ---- BOOK CARD ---- */
.book-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem 1.4rem;
  cursor: pointer;
  display: flex;
  gap: 1.1rem;
  align-items: flex-start;
  transition: border-color 0.2s var(--ease), background 0.2s var(--ease), transform 0.15s var(--ease), box-shadow 0.2s var(--ease);
}
.book-card:hover {
  border-color: var(--border-hi);
  background: var(--bg3);
  transform: translateY(-2px);
  box-shadow: var(--card-shadow);
}

.book-card-cover {
  width: 56px;
  min-width: 56px;
  height: 80px;
  border-radius: 3px;
  overflow: hidden;
  background: var(--bg4);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.book-card-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.book-card-cover .cover-placeholder {
  font-size: 1.5rem;
  opacity: 0.3;
}

.book-card-info {
  flex: 1;
  min-width: 0;
}

.book-card-title {
  font-family: var(--font-body);
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--text);
  line-height: 1.35;
  margin-bottom: 0.3rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-card-author {
  font-size: 0.92rem;
  color: var(--sub);
  margin-bottom: 0.4rem;
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-card-meta {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.book-card-year,
.book-card-editions {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-transform: uppercase;
}

/* ---- BOOK DETAIL PANEL ---- */
.detail-panel {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(5,4,3,0.75);
  backdrop-filter: blur(6px);
  padding: clamp(1rem, 3vw, 2.5rem);
  overflow-y: auto;
}
.detail-panel.open { display: flex; align-items: flex-start; justify-content: center; }

.detail-card {
  background: var(--bg2);
  border: 1px solid var(--border-hi);
  border-radius: 12px;
  width: 100%;
  max-width: 900px;
  box-shadow: 0 24px 80px rgba(0,0,0,0.7);
  overflow: hidden;
  position: relative;
  margin: auto;
}

.detail-close {
  position: absolute;
  top: 1.25rem;
  right: 1.25rem;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg4);
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: border-color 0.2s, color 0.2s;
  z-index: 10;
}
.detail-close:hover { border-color: var(--gold); color: var(--gold); }

/* Detail hero */
.detail-hero {
  display: flex;
  gap: clamp(1.5rem, 4vw, 3rem);
  padding: clamp(1.5rem, 4vw, 3rem);
  padding-right: 4rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, var(--bg3) 0%, var(--bg2) 100%);
}

.detail-cover-wrap {
  flex-shrink: 0;
}

.detail-cover {
  width: clamp(120px, 18vw, 180px);
  aspect-ratio: 2/3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg4);
  border: 1px solid var(--border);
  box-shadow: 8px 12px 32px rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}
.detail-cover img { width: 100%; height: 100%; object-fit: cover; }
.detail-cover .cover-placeholder-lg {
  font-size: 3rem;
  opacity: 0.25;
  text-align: center;
}

.detail-hero-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.detail-eyebrow {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.22em;
  color: var(--gold-dim);
  text-transform: uppercase;
  margin-bottom: 0.7rem;
}

.detail-title {
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 3.5vw, 2.2rem);
  font-weight: 500;
  color: var(--text);
  line-height: 1.25;
  margin-bottom: 0.6rem;
  letter-spacing: 0.02em;
}

.detail-subtitle {
  font-size: 1.1rem;
  font-style: italic;
  color: var(--sub);
  margin-bottom: 0.5rem;
}

.detail-authors {
  font-size: 1.05rem;
  color: var(--gold);
  margin-bottom: 1.25rem;
  font-weight: 500;
}

.detail-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: auto;
}

.badge {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.3rem 0.75rem;
  border-radius: 20px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: var(--bg4);
}
.badge.gold {
  border-color: var(--gold-dim);
  color: var(--gold);
  background: rgba(201,168,76,0.08);
}

/* Detail body */
.detail-body {
  padding: clamp(1.5rem, 4vw, 2.5rem);
  display: grid;
  gap: 2rem;
}

.detail-section-label {
  font-family: var(--font-display);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold-dim);
  margin-bottom: 0.9rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.detail-section-label::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.detail-description {
  font-size: 1.1rem;
  line-height: 1.8;
  color: var(--sub);
  font-weight: 300;
}

/* Metadata grid */
.meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.meta-item {
  padding: 1rem 1.1rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.meta-label {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  letter-spacing: 0.15em;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 0.4rem;
}

.meta-value {
  font-size: 1.05rem;
  color: var(--text);
  font-weight: 400;
  line-height: 1.4;
}

/* Subjects */
.subjects-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.subject-tag {
  font-size: 0.85rem;
  padding: 0.3rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--sub);
  background: var(--bg3);
  transition: border-color 0.15s, color 0.15s;
  cursor: default;
}
.subject-tag:hover { border-color: var(--border-hi); color: var(--text); }

/* Editions */
.editions-list {
  display: grid;
  gap: 0.75rem;
}

.edition-item {
  display: flex;
  gap: 1rem;
  align-items: center;
  padding: 0.9rem 1.1rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.edition-year {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--gold);
  min-width: 48px;
}

.edition-info {
  flex: 1;
}

.edition-title {
  font-size: 1rem;
  color: var(--text);
  margin-bottom: 0.2rem;
}

.edition-pub {
  font-size: 0.85rem;
  color: var(--muted);
  font-style: italic;
}

/* Author block */
.author-block {
  display: flex;
  gap: 1.25rem;
  padding: 1.25rem;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.author-photo {
  width: 72px;
  height: 72px;
  min-width: 72px;
  border-radius: 50%;
  overflow: hidden;
  background: var(--bg4);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  color: var(--muted);
}
.author-photo img { width: 100%; height: 100%; object-fit: cover; }

.author-info { flex: 1; }

.author-name {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.25rem;
}

.author-dates {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.1em;
  margin-bottom: 0.6rem;
}

.author-bio {
  font-size: 0.95rem;
  color: var(--sub);
  line-height: 1.65;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* OL link */
.ol-link {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  color: var(--gold);
  text-decoration: none;
  padding: 0.5rem 1rem;
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  transition: background 0.2s, border-color 0.2s;
  margin-top: 0.5rem;
  width: fit-content;
}
.ol-link:hover {
  background: rgba(201,168,76,0.1);
  border-color: var(--gold);
}

/* ---- LOADING ---- */
.loading-state {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4rem 2rem;
  gap: 1.25rem;
}
.loading-state.visible { display: flex; }

.spinner {
  width: 36px;
  height: 36px;
  border: 2px solid var(--border);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.15em;
  color: var(--muted);
  text-transform: uppercase;
}

/* ---- EMPTY / ERROR STATES ---- */
.state-message {
  text-align: center;
  padding: 4rem 2rem;
  display: none;
}
.state-message.visible { display: block; }
.state-icon { font-size: 2.5rem; margin-bottom: 1rem; opacity: 0.5; }
.state-title {
  font-family: var(--font-display);
  font-size: 1.2rem;
  color: var(--sub);
  margin-bottom: 0.5rem;
}
.state-body { font-size: 1rem; color: var(--muted); font-style: italic; }

/* ---- PAGINATION ---- */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 2.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.page-btn {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.55rem 1.25rem;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s, background 0.2s;
}
.page-btn:hover { border-color: var(--gold); color: var(--gold); background: var(--bg4); }
.page-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.page-info {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--muted);
  letter-spacing: 0.1em;
}

/* ---- FOOTER ---- */
.app-footer {
  padding: 1.5rem clamp(1rem, 4vw, 3rem);
  border-top: 1px solid var(--border);
  text-align: center;
}

.footer-text {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  color: var(--mut2);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.footer-text a {
  color: var(--muted);
  text-decoration: none;
  transition: color 0.2s;
}
.footer-text a:hover { color: var(--gold); }

/* ---- DETAIL LOADING OVERLAY ---- */
.detail-loading {
  position: absolute;
  inset: 0;
  background: var(--bg2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  border-radius: 12px;
  z-index: 5;
}

/* ---- RESPONSIVE ---- */
@media (max-width: 640px) {
  .detail-hero {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding-right: 1.5rem;
  }
  .detail-badges { justify-content: center; }
  .detail-authors { text-align: center; }
  .meta-grid { grid-template-columns: 1fr 1fr; }
  .results-grid { grid-template-columns: 1fr; }
  .author-block { flex-direction: column; align-items: center; text-align: center; }
}

@media (max-width: 400px) {
  .meta-grid { grid-template-columns: 1fr; }
}
