/* ============================================================
   Tokens
   ============================================================ */
:root {
  color-scheme: light;

  --bg: #faf6ee;
  --surface: #ffffff;
  --surface-raised: #efe6d3;
  --border: rgba(60, 47, 25, 0.14);
  --border-strong: rgba(60, 47, 25, 0.26);
  --text: #2a2115;
  --text-muted: #6b5d47;
  --text-faint: #9c8d74;
  --accent: #a5711f;
  --accent-strong: #8a5e1a;
  --accent-rust: #a24a35;
  --focus-ring: #8a5e1a;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  --error: #a23e2b;
  --topbar-bg: rgba(250, 246, 238, 0.96);
  --modal-close-bg: rgba(250, 246, 238, 0.7);

  --font-serif: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", ui-serif, serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
  color-scheme: dark;

  --bg: #16120e;
  --surface: #1f1911;
  --surface-raised: #251e15;
  --border: rgba(240, 230, 214, 0.12);
  --border-strong: rgba(240, 230, 214, 0.22);
  --text: #efe6d6;
  --text-muted: #b6a992;
  --text-faint: #8a7d68;
  --accent: #cf9a44;
  --accent-strong: #e0ac52;
  --accent-rust: #a24a35;
  --focus-ring: #e0ac52;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --error: #e2a48f;
  --topbar-bg: rgba(22, 18, 14, 0.96);
  --modal-close-bg: rgba(22, 18, 14, 0.7);
}

/* ============================================================
   Reset / base
   ============================================================ */
* {
  box-sizing: border-box;
}

/* Author-stylesheet `display` rules (grid/flex containers etc.) otherwise
   beat the browser's built-in `[hidden]{display:none}` rule, since author
   styles always win over user-agent styles regardless of specificity. */
[hidden] {
  display: none !important;
}

html, body {
  overflow-x: hidden;
  max-width: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.5;
  min-height: 100vh;
  transition: background-color 0.15s ease, color 0.15s ease;
}

h1, h2, h3 {
  margin: 0;
  font-weight: 600;
}

p {
  margin: 0;
}

button, select, input {
  font-family: inherit;
  font-size: 1rem;
  color: inherit;
}

button {
  cursor: pointer;
}

a {
  color: var(--accent-strong);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ============================================================
   Buttons / controls (shared)
   ============================================================ */
.control-button {
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 0.55rem 0.9rem;
  font-size: 0.92rem;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.control-button:hover {
  border-color: var(--accent);
}

.control-button.accent {
  background: linear-gradient(180deg, #b9843a, #a06e2e);
  border-color: #8a5e28;
  color: #1a1207;
  font-weight: 600;
}

.control-button.accent:hover {
  background: linear-gradient(180deg, #c79147, #ab7833);
}

.icon-button {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  border-radius: 6px;
  width: 2.1rem;
  height: 2.1rem;
  font-size: 1.3rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-button:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

.text-button {
  background: none;
  border: none;
  color: var(--accent-strong);
  padding: 0.3rem 0;
  font-size: 0.9rem;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.count-badge {
  background: var(--accent);
  color: #1a1207;
  font-size: 0.72rem;
  font-weight: 700;
  border-radius: 999px;
  min-width: 1.3em;
  padding: 0.05em 0.4em;
  display: inline-block;
  text-align: center;
}

select {
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
}

/* ============================================================
   Top bar
   ============================================================ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--topbar-bg);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
  padding: 0.9rem 1.25rem;
}

.topbar-row {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.7rem;
  flex-wrap: wrap;
}

.site-title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  letter-spacing: 0.01em;
  color: var(--text);
}

.result-count {
  color: var(--text-faint);
  font-size: 0.85rem;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.search-field {
  position: relative;
  flex: 1 1 220px;
  min-width: 160px;
}

.search-field input {
  width: 100%;
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 8px;
  padding: 0.55rem 2.2rem 0.55rem 0.8rem;
}

.search-field input::placeholder {
  color: var(--text-faint);
}

.search-clear {
  position: absolute;
  right: 0.3rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 1.2rem;
  width: 1.8rem;
  height: 1.8rem;
  border-radius: 6px;
}

.search-clear:hover {
  color: var(--text);
}

.view-toggle {
  display: flex;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  overflow: hidden;
  margin-left: auto;
}

.view-toggle-btn {
  background: var(--surface-raised);
  border: none;
  color: var(--text-muted);
  padding: 0.5rem 0.65rem;
  display: flex;
  align-items: center;
}

.view-toggle-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #1a1207;
}

.view-toggle-btn svg rect {
  fill: currentColor;
}

.theme-toggle svg {
  fill: currentColor;
}

.theme-toggle .icon-sun {
  display: none;
}

[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}

[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}

/* ============================================================
   Filter panel
   ============================================================ */
.filter-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 29;
}

.filter-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(360px, 90vw);
  background: var(--surface);
  border-left: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
  z-index: 30;
  display: flex;
  flex-direction: column;
  padding: 1rem;
  overflow-y: auto;
}

.filter-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: -1rem -1rem 0.75rem;
  padding: 1rem 1rem 0.75rem;
  position: sticky;
  top: -1rem;
  background: var(--surface);
  z-index: 1;
}

.filter-panel-header h2 {
  font-family: var(--font-serif);
  font-size: 1.2rem;
}

.active-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.9rem;
  padding-bottom: 0.9rem;
  border-bottom: 1px solid var(--border);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  color: var(--text);
  border-radius: 999px;
  padding: 0.25rem 0.5rem 0.25rem 0.7rem;
  font-size: 0.8rem;
}

.chip button {
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 1rem;
  line-height: 1;
  padding: 0;
}

.chip button:hover {
  color: var(--accent-strong);
}

.filter-section {
  margin-bottom: 1.1rem;
}

.filter-section h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin-bottom: 0.5rem;
}

.filter-section-search {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 0.35rem 0.55rem;
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
}

.filter-options {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.filter-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.87rem;
  padding: 0.25rem 0.2rem;
  border-radius: 5px;
  cursor: pointer;
}

.filter-option:hover {
  background: var(--surface-raised);
}

.filter-option input {
  accent-color: var(--accent);
}

.filter-option .opt-label {
  flex: 1;
  color: var(--text);
}

.filter-option .opt-count {
  color: var(--text-faint);
  font-size: 0.78rem;
}

.filter-panel-footer {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.filter-summary {
  color: var(--text-faint);
  font-size: 0.8rem;
}

/* ============================================================
   Results / states
   ============================================================ */
#resultsMain {
  padding: 1.25rem;
  max-width: 1600px;
  margin: 0 auto;
}

.state-message {
  text-align: center;
  color: var(--text-muted);
  padding: 4rem 1rem;
  font-size: 1.02rem;
}

.state-message.error {
  color: var(--error);
}

/* ============================================================
   Grid view
   ============================================================ */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
  gap: 1.4rem 1.1rem;
}

.book-card {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.book-card:hover,
.book-card:focus-visible {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.card-cover {
  aspect-ratio: 2 / 3;
  background: var(--surface-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.card-body {
  padding: 0.65rem 0.75rem 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
}

.card-title {
  font-family: var(--font-serif);
  font-size: 0.98rem;
  line-height: 1.28;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-author {
  color: var(--text-muted);
  font-size: 0.83rem;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.78rem;
  color: var(--text-faint);
  margin-top: auto;
  padding-top: 0.35rem;
}

.card-rating {
  color: var(--accent-strong);
}

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

.mini-tag {
  font-size: 0.7rem;
  color: var(--text-faint);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.08rem 0.35rem;
}

/* ============================================================
   Cover placeholder (generated in JS)
   ============================================================ */
.cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 0.4rem;
  padding: 0.6rem;
  background: var(--placeholder-color, #4a3a26);
  color: rgba(255, 255, 255, 0.92);
}

.cover-placeholder svg {
  opacity: 0.75;
}

.cover-placeholder .ph-title {
  font-family: var(--font-serif);
  font-size: 0.78rem;
  line-height: 1.25;
}

.cover-placeholder .ph-author {
  font-size: 0.68rem;
  opacity: 0.8;
}

.row-cover .cover-placeholder .ph-title {
  font-size: 0.62rem;
}

.row-cover .cover-placeholder .ph-author {
  display: none;
}

/* ============================================================
   List view
   ============================================================ */
.book-list {
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--border);
}

.book-row {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.55rem 0.6rem;
  border-bottom: 1px solid var(--border);
  background: transparent;
  transition: background 0.15s ease;
}

.book-row:hover,
.book-row:focus-visible {
  background: var(--surface);
}

.row-cover {
  width: 40px;
  height: 58px;
  flex: none;
  border-radius: 4px;
  overflow: hidden;
  background: var(--surface-raised);
}

.row-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.row-title-block {
  flex: 1 1 260px;
  min-width: 0;
}

.row-title {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  font-weight: 400;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-author {
  color: var(--text-muted);
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-year,
.row-meta,
.row-rating {
  color: var(--text-faint);
  font-size: 0.82rem;
  flex: none;
  white-space: nowrap;
}

.row-year {
  width: 3.5rem;
}

.row-meta {
  display: none;
}

.row-rating {
  color: var(--accent-strong);
  width: 5rem;
  text-align: right;
}

/* ============================================================
   Modal
   ============================================================ */
.book-modal {
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 0;
  width: min(700px, 92vw);
  max-height: 85vh;
  box-shadow: var(--shadow);
}

.book-modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.modal-close {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  z-index: 2;
  background: var(--modal-close-bg);
}

.modal-content {
  overflow-y: auto;
  max-height: 85vh;
  padding: 1.5rem;
}

.modal-cover {
  float: left;
  width: 160px;
  margin: 0 1.2rem 0.8rem 0;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 2 / 3;
  background: var(--surface-raised);
}

.modal-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.modal-title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  line-height: 1.3;
  margin-bottom: 0.25rem;
}

.modal-author {
  color: var(--text-muted);
  font-size: 1rem;
  margin-bottom: 0.75rem;
}

.modal-rating {
  color: var(--accent-strong);
  margin-bottom: 0.75rem;
  display: block;
}

.modal-fields {
  clear: none;
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.4rem 0.9rem;
  font-size: 0.88rem;
  margin-top: 0.5rem;
}

.modal-fields dt {
  color: var(--text-faint);
}

.modal-fields dd {
  margin: 0;
}

.modal-text-block {
  clear: both;
  margin-top: 1.1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.modal-text-block h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin-bottom: 0.4rem;
}

.modal-text-block p {
  margin-bottom: 0.6rem;
  white-space: pre-line;
}

.modal-again {
  clear: both;
  display: block;
  margin-top: 1.2rem;
}

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 0.6rem 1rem;
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  z-index: 40;
}

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 720px) {
  .topbar {
    padding: 0.75rem 0.9rem;
  }

  .view-toggle {
    margin-left: 0;
  }

  .filter-panel {
    width: 100%;
    max-width: 100%;
  }

  .book-modal {
    width: 100vw;
    max-height: 100vh;
    height: 100%;
    border-radius: 0;
  }

  .modal-content {
    max-height: 100vh;
  }

  .modal-cover {
    float: none;
    width: 130px;
    margin: 0 auto 1rem;
  }

  .modal-title,
  .modal-author,
  .modal-rating,
  .modal-fields {
    text-align: center;
  }

  .modal-fields {
    grid-template-columns: 1fr;
  }

  .row-meta {
    display: none;
  }

  .row-title-block {
    flex: 1 1 auto;
  }
}
