/* Override any theme-specific styles for the sidebar headers */
.wbg-filter-sidebar h3,
.文章類型,
.出版日期,
.wbg-parent-wrapper .文章類型,
.wbg-parent-wrapper .出版日期 {
  color: #2e2e2e !important;
  border-bottom: 2px solid #bb1f35 !important;
}

/* Ensure all links in the filter are correct color */
.wbg-filter-sidebar a,
.wbg-filter-sidebar a:visited {
  color: #2e2e2e !important;
}

.wbg-filter-sidebar a:hover {
  color: #bb1f35 !important;
}

/* Book Gallery Filter Sidebar Styles */

/* Main container layout - Controls the two-column layout */
.wbg-parent-wrapper {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 100%;
  gap: 20px;
  margin-bottom: 20px;
  position: relative;
}

/* --- NEW: Search Bar Styles --- */
.wbg-search-bar-container {
  display: flex;
  width: calc(100% - 30px);
  margin-left: 15px;
  margin-right: 15px;
  margin-bottom: 15px;
  order: 0 !important;
}

.wbg-search-input {
  flex: 1;
  padding: 10px 15px;
  border: 1px solid #ccc;
  border-radius: 4px 0 0 4px;
  font-size: 14px;
  height: 40px;
  box-sizing: border-box;
}

.wbg-search-input:focus {
  outline: none;
  border-color: #bb1f35;
  box-shadow: 0 0 0 2px rgba(187, 31, 53, 0.2);
}

.wbg-search-button {
  padding: 0 20px;
  border: 1px solid #bb1f35;
  background-color: #bb1f35;
  color: white;
  cursor: pointer;
  border-radius: 0;
  font-size: 14px;
  height: 40px;
  box-sizing: border-box;
  transition: background-color 0.2s ease;
  margin-left: -1px;
}

.wbg-search-button:hover {
  background-color: #a01a2e;
  border-color: #a01a2e;
}

#wbg-search-button {
  border-radius: 0; /* Keep middle button square */
}

.wbg-advanced-search-button {
  border-radius: 0 4px 4px 0; /* Right corners rounded */
  background-color: #e7e9e6; /* Secondary button color */
  color: #2e2e2e;
  border-color: #ccc;
}

.wbg-advanced-search-button:hover {
  background-color: #d9dbd8;
  border-color: #bbb;
}

/* --- UPDATED: Total Books Title Styles --- */
.wbg-total-books-title {
  color: #2e2e2e;
  font-size: 0.8em;
  font-weight: bold;
  margin-bottom: 10px;
  order: 1 !important;
  /* NEW: Force full width */
  flex: 0 0 100%;
  width: 100%;
  margin-left: 15px; /* Align with search bar */
  box-sizing: border-box;
}

/* Filter sidebar */
.wbg-filter-sidebar {
  flex: 0 0 250px;
  width: 250px;
  padding: 20px;
  background-color: #f4f6f3;
  border: 1px solid #f4f6f3;
  border-radius: 5px;
  box-sizing: border-box;
  position: sticky;
  top: 20px;
  align-self: flex-start;
  order: 2 !important;
  /* Enable internal scrolling when content exceeds viewport */
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Main wrapper for book list */
.wbg-main-wrapper.list {
  flex: 1;
  min-width: 0;
  box-sizing: border-box;
  order: 2 !important;
}

/* Ensure no other elements break the layout */
.wbg-parent-wrapper
  > *:not(.wbg-search-bar-container):not(.wbg-total-books-title):not(
    .wbg-filter-sidebar
  ):not(.wbg-main-wrapper):not(.wbg-item-list-wrapper.filtered-out) {
  display: none !important;
}

/* Filter section styles */
.wbg-filter-section {
  margin-bottom: 25px;
}

/* --- NEW: Sort Section Specific Styles --- */
.wbg-sort-section {
  margin-bottom: 25px;
}

.wbg-sort-select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  background-color: #fff;
  font-size: 14px;
  cursor: pointer;
  height: 38px;
  box-sizing: border-box;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23888888%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 10px auto;
  padding-right: 30px;
}

.wbg-sort-select:focus {
  outline: none;
  border-color: #bb1f35;
  box-shadow: 0 0 0 2px rgba(187, 31, 53, 0.2);
}

/* Filter options */
.wbg-filter-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.wbg-filter-option {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 14px;
  color: #2e2e2e;
}

.wbg-filter-checkbox,
.wbg-filter-radio {
  margin-right: 8px;
}

.wbg-filter-count {
  color: #666;
  margin-left: 4px;
}

.wbg-loading {
  font-style: italic;
  color: #666;
  font-size: 13px;
  padding: 10px 0;
}

/* Custom date range */
.wbg-custom-date-range {
  margin-top: 10px;
  padding: 10px 0;
}

.wbg-date-inputs {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.wbg-date-start,
.wbg-date-end {
  width: 80px;
  padding: 5px;
  border: 1px solid #ddd;
  border-radius: 3px;
  background-color: #fff;
  color: #2e2e2e;
}

.wbg-date-separator {
  margin: 0 8px;
}

/* Filter action buttons */
.wbg-filter-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.wbg-apply-filters,
.wbg-reset-filters,
.wbg-filter-date-btn {
  padding: 8px 15px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s ease;
  height: 40px;
  box-sizing: border-box;
}

.wbg-apply-filters {
  background-color: #bb1f35;
  color: white;
}

.wbg-apply-filters:hover {
  background-color: #a01a2e;
}

.wbg-reset-filters {
  background-color: #e7e9e6;
  color: #2e2e2e;
}

.wbg-reset-filters:hover {
  background-color: #d9dbd8;
}

.wbg-filter-date-btn {
  background-color: #bb1f35;
  color: white;
  width: 100%;
}

.wbg-filter-date-btn:hover {
  background-color: #a01a2e;
}

/* Styling for book items when filtered */
.wbg-item-list-wrapper.filtered-out {
  display: none !important; /* Changed from grid to none to hide items */
}

/* Checkbox and radio button styling */
.wbg-filter-checkbox:checked,
.wbg-filter-radio:checked {
  accent-color: #bb1f35;
}

/* Page Loading Overlay */
.wbg-loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.9);
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  transition: opacity 0.3s ease-out;
}

.wbg-loading-spinner {
  width: 60px;
  height: 60px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #bb1f35;
  border-radius: 50%;
  animation: wbg-spin 1s linear infinite;
  margin-bottom: 15px;
}

.wbg-loading-text {
  font-size: 16px;
  color: #2e2e2e;
  font-weight: bold;
}

@keyframes wbg-spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* --- NEW: Advanced Search Modal Styles --- */
.wbg-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6); /* Semi-transparent overlay */
  z-index: 10000; /* Ensure it's above other content */
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
  opacity: 0; /* Initially hidden */
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.wbg-modal-overlay.visible {
  opacity: 1;
  visibility: visible;
}

.wbg-modal-content {
  background-color: #fff;
  padding: 25px;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  width: 100%;
  max-width: 800px; /* Adjust max width as needed */
  max-height: 90vh; /* Limit height */
  overflow-y: auto; /* Allow scrolling if content overflows */
  display: flex;
  flex-direction: column;
  transform: scale(0.95); /* Slight scale down effect */
  transition: transform 0.3s ease;
}

.wbg-modal-overlay.visible .wbg-modal-content {
  transform: scale(1); /* Scale back to normal when visible */
}

.wbg-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding-bottom: 15px;
  margin-bottom: 20px;
}

.wbg-modal-header h2 {
  margin: 0;
  font-size: 1.4em;
  color: #333;
}

.wbg-modal-close {
  background: none;
  border: none;
  font-size: 2em;
  line-height: 1;
  cursor: pointer;
  color: #aaa;
  padding: 0 5px;
}

.wbg-modal-close:hover {
  color: #333;
}

.wbg-modal-body {
  flex-grow: 1; /* Allow body to take available space */
  margin-bottom: 20px;
}

/* --- NEW: Precision Builder Styles --- */
#wbg-precision-builder {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.wbg-modal-section-title {
  font-size: 1.1em;
  font-weight: bold;
  color: #444;
  margin-bottom: 15px;
  padding-bottom: 5px;
  border-bottom: 1px dashed #ddd;
}

#wbg-precision-rows-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 15px;
}

.wbg-precision-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.wbg-precision-row-logic {
  flex: 0 0 90px; /* Fixed width for AND/OR/NOT */
}
.wbg-precision-row-logic select {
  width: 100%;
}

.wbg-precision-row-field {
  flex: 0 0 150px; /* Fixed width for field selection */
}
.wbg-precision-row-field select {
  width: 100%;
}

.wbg-precision-row-value {
  flex: 1; /* Value input takes remaining space */
}
.wbg-precision-row-value input {
  width: 100%;
}

.wbg-precision-row-remove {
  flex: 0 0 auto; /* Button takes minimum space */
}

.wbg-remove-precision-row {
  background: none;
  border: 1px solid #ccc;
  color: #888;
  font-size: 1.2em;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.wbg-remove-precision-row:hover {
  background-color: #f0f0f0;
  color: #bb1f35;
  border-color: #bbb;
}

#wbg-precision-builder-controls {
  text-align: left;
}

#wbg-add-precision-row:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Visually hidden class for accessibility */
.visually-hidden {
  position: absolute !important;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap; /* Prevent line breaks */
}
/* --- End Precision Builder Styles --- */

.wbg-modal-row {
  display: grid;
  grid-template-columns: repeat(
    auto-fit,
    minmax(200px, 1fr)
  ); /* Responsive columns */
  gap: 20px;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px dashed #eee; /* Separator between rows */
}
.wbg-modal-row:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.wbg-modal-field-group {
  display: flex;
  flex-direction: column;
}

.wbg-modal-field-group label {
  font-weight: bold;
  margin-bottom: 8px;
  font-size: 0.9em;
  color: #555;
}

/* Common style for selects and inputs in modal */
.wbg-modal-field-group select,
.wbg-modal-field-group input[type="text"],
.wbg-modal-field-group input[type="number"],
.wbg-precision-row select,
.wbg-precision-row input[type="text"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
  height: 40px; /* Match button height */
}

.wbg-modal-field-group select:focus,
.wbg-modal-field-group input:focus,
.wbg-precision-row select:focus,
.wbg-precision-row input:focus {
  outline: none;
  border-color: #bb1f35;
  box-shadow: 0 0 0 2px rgba(187, 31, 53, 0.2);
}

.wbg-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.wbg-modal-button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  transition: background-color 0.2s ease;
  height: 40px; /* Standard height */
  box-sizing: border-box;
}

.wbg-modal-button-primary {
  background-color: #bb1f35;
  color: white;
}

.wbg-modal-button-primary:hover {
  background-color: #a01a2e;
}

.wbg-modal-button-secondary {
  background-color: #e7e9e6;
  color: #2e2e2e;
  border: 1px solid #ccc; /* Add border to match other secondary buttons */
}

.wbg-modal-button-secondary:hover {
  background-color: #d9dbd8;
  border-color: #bbb;
}

/* Responsive layout */
@media (max-width: 768px) {
  .wbg-parent-wrapper {
    flex-direction: column !important; /* Keep !important for specificity */
    gap: 10px;
  }

  /* Search bar on mobile */
  .wbg-search-bar-container {
    order: 0 !important; /* Keep !important */
  }
  .wbg-search-button {
    padding: 0 15px; /* Slightly reduce padding */
  }

  /* Title on mobile */
  .wbg-total-books-title {
    order: 1 !important; /* Keep !important */
    padding-left: 0; /* Existing */
    margin-left: 0; /* NEW: Reset desktop margin */
    margin-bottom: 10px;
    font-size: 1em;
  }

  .wbg-filter-sidebar {
    flex: 0 0 100%;
    width: 100%;
    margin-right: 0;
    margin-bottom: 20px;
    position: static;
    order: 2 !important; /* Keep !important */
    max-height: none;
    overflow-y: visible;
  }

  .wbg-main-wrapper.list {
    flex: 0 0 100%;
    max-width: 100%;
    order: 3 !important; /* Keep !important */
  }

  /* Modal on mobile */
  .wbg-modal-content {
    max-width: 95%;
    padding: 15px;
  }
  .wbg-modal-header h2 {
    font-size: 1.2em;
  }
  .wbg-modal-row {
    grid-template-columns: 1fr; /* Stack fields vertically */
    gap: 15px;
  }
  .wbg-modal-footer {
    justify-content: space-between; /* Spread buttons on small screens */
  }

  /* --- NEW: Precision Builder Responsive Styles --- */
  .wbg-precision-row {
    flex-wrap: wrap; /* Allow wrapping */
    gap: 8px; /* Reduce gap */
  }
  .wbg-precision-row-logic {
    flex: 1 1 100%; /* Take full width first */
    order: 1; /* Logic operator first */
    visibility: visible !important; /* Always show on mobile */
  }
  .wbg-precision-row-field {
    flex: 1 1 calc(50% - 4px); /* Approx half width */
    order: 2;
  }
  .wbg-precision-row-value {
    flex: 1 1 calc(50% - 4px); /* Approx half width */
    order: 3;
  }
  .wbg-precision-row-remove {
    flex: 1 1 100%; /* Full width below */
    order: 4;
    text-align: right; /* Align button right */
    margin-top: 5px;
  }
  /* Hide logic operator visually for the very first row on mobile too */
  #wbg-precision-rows-container .wbg-precision-row:first-child .wbg-precision-row-logic {
      display: none; /* Completely hide first logic operator on mobile */
  }
  #wbg-precision-builder-controls {
      margin-top: 10px;
  }
}