:root {
  --border: #ddd;
  --muted: #666;
  --bg: #fff;
  --pill: #ccc;
}

* { box-sizing: border-box; }

/* Prevent iOS Safari "right gap" / horizontal overflow issues */
html, body {
  width: 100%;
  overflow-x: hidden;
}

body {
  font-family: "Open Sans", sans-serif;
  margin: 0;
  background: var(--bg);
  color: #333;
  -webkit-text-size-adjust: 100%;
}

/* Prevent iOS Safari zoom-on-focus */
input, select, textarea, button {
  font-size: 16px;
}

/* =========================================================
   CONTROLS
   Goal: keep Filters icon on same row as Search at ALL widths
   ========================================================= */
.controls {
  display: grid;
  gap: .6rem;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "search filters"
    "sort   sort";
  align-items: center;
}

#search { grid-area: search; min-width: 0; }
#open-filters { grid-area: filters; }
#sort { grid-area: sort; }

@media (min-width: 601px) {
  .controls {
    grid-template-columns: 1fr 220px auto;
    grid-template-areas: "search sort filters";
  }
}

/* =========================================================
   LAYOUT
   ========================================================= */
.layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 1.25rem;
  align-items: start;
}

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

/* =========================================================
   FILTERS (Desktop sticky sidebar)
   ========================================================= */
aside.filters {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 0.75rem 1rem;
  background: #fafafa;
  position: sticky;
  top: 0.75rem;
}

.filters h3 { margin: 0 0 .5rem; font-size: 1rem; }
.filters .group { margin-bottom: 1rem; }
.filters .list { max-height: 260px; overflow: auto; padding-right: .25rem; }
.filters .actions { display: flex; gap: .5rem; margin-top: .5rem; }

.form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
}

.btn {
  padding: .35rem .6rem;
  border: 1px solid #333;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}

.btn.muted {
  border-color: var(--border);
  color: #444;
}

.badge {
  display: inline-block;
  font-size: .75rem;
  color: #444;
  border: 1px solid var(--pill);
  border-radius: 999px;
  padding: .1rem .5rem;
}

/* =========================================================
   BOOK GRID/CARDS
   ========================================================= */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 1rem;
}

.card {
  border: 1px solid #ccc;
  border-radius: 8px;
  overflow: hidden;
  background: #fafafa;
  cursor: pointer;
}

/* --- NEW: Stable cover frame (recommended) --- */
.card-cover {
  width: 100%;
  aspect-ratio: 2 / 3;          /* book-cover shape */
  background: #fff;             /* letterbox color */
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}

/* Image inside the cover frame: never crop */
.card-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;          /* key: no cropping */
  display: block;
}

/* --- Fallback: if you still have <img> directly under .card --- */
/* This prevents crop even without the wrapper, though the wrapper is better */
.card > img {
  width: 100%;
  height: auto;
  aspect-ratio: 2 / 3;
  object-fit: contain;
  background: #fff;
  display: block;
}

/* Remove the old fixed-height crop rule */
.card img {
  /* intentionally no height: 220px; and no object-fit: cover; */
}

.card-body { padding: 0.5rem; }

.pill {
  font-size: 0.8em;
  color: var(--muted);
  border: 1px solid var(--pill);
  border-radius: 999px;
  padding: 0 .5rem;
}

/* =========================================================
   DETAIL
   ========================================================= */
.detail img { max-width: 280px; height: auto; }

.button {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  border: 1px solid #333;
  border-radius: 6px;
  text-decoration: none;
  color: #333;
}

.meta {
  color: var(--muted);
  font-size: .9rem;
  margin-top: .25rem;
}

/* =========================================================
   PAGINATION
   ========================================================= */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: .5rem;
  margin: 1rem 0;
  flex-wrap: wrap;
  text-align: center;
}

.pagination .page-btn {
  padding: .35rem .6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}

.pagination .page-btn[disabled] {
  opacity: .5;
  cursor: not-allowed;
}

.pagination .page-info {
  color: var(--muted);
  font-size: .9rem;
  margin-left: .75rem;
}

/* =========================================================
   ICON BUTTONS
   ========================================================= */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  flex: 0 0 auto;
}

.icon-btn .material-icons {
  font-size: 22px;
  line-height: 1;
}

.icon-btn:hover { background: #f3f3f3; }

/* =========================================================
   MOBILE FILTERS DRAWER
   - Drawer content scrolls
   - Page behind does NOT scroll (handled by JS + this class)
   - iOS-safe: avoid 100vw (use inset + width:100%)
   ========================================================= */
.filters-open { display: none; }

.filters-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 999;
}

.filters-header {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .9rem 1rem;
  border-bottom: 1px solid var(--border);
  background: #fff;
}

.filters-content {}

body.filters-open {
  position: fixed;
  width: 100%;
  left: 0;
  right: 0;
  overflow: hidden;
}

@media (max-width: 899px) {
  .filters-open { display: inline-flex; }

  aside.filters {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100dvh;
    z-index: 1000;

    border-radius: 0;
    border: none;
    background: #fff;
    padding: 0;
    overflow: hidden;
    transform: translateY(100%);
    transition: transform 180ms ease;
  }

  .filters-header { display: flex; }

  .filters-content {
    height: calc(100dvh - 56px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 1rem;
  }

  .filters .list { max-height: none; }

  body.filters-open aside.filters { transform: translateY(0); }
  body.filters-open .filters-overlay { display: block; }
}
