/* واجهة البحث المرجعية: توزيع كثيف مطابق للصور المرفقة */
body.search-workspace-mode {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #f2f0ec;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
}

body.search-workspace-mode .site-footer {
  display: none;
}

body.search-workspace-mode .main-wrapper {
  width: 100%;
  max-width: none;
  height: auto;
  min-height: 0;
  margin: 0;
  padding: 0;
}

body.search-workspace-mode #view-search,
.reference-search-shell {
  height: 100%;
  min-height: 0;
}

.search-setup-workspace {
  display: grid;
  grid-template-columns: 82px minmax(250px, 24fr) minmax(390px, 41fr) minmax(320px, 29fr);
  grid-template-rows: minmax(0, 1fr) 68px;
  width: 100%;
  height: 100%;
  direction: rtl;
  background: #efeeeb;
  color: #2a1712;
  font-size: 16.8px;
}

.search-setup-workspace[hidden] { display: none; }

.search-setup-workspace button,
.search-setup-workspace input,
.search-setup-workspace select,
.search-results-workspace button,
.search-results-workspace input {
  font: inherit;
}

.search-mode-rail,
.search-selection-panel,
.search-books-panel,
.search-tools-panel {
  min-width: 0;
  min-height: 0;
  background: #f6f5f2;
  border-left: 1px solid #bfbab3;
}

.search-mode-rail {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 6px 5px;
  gap: 3px;
  background: #eeeae4;
}

.search-mode-button {
  min-height: 76px;
  padding: 6px 2px;
  border: 1px solid transparent;
  background: transparent;
  color: #2a1712;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
}

.search-mode-button svg {
  width: 34px;
  height: 34px;
  fill: none;
  stroke: #5e0006;
  stroke-width: 1.8;
}

.search-mode-button.active {
  background: #eed9b9;
  border-color: #9b0f06;
  color: #5e0006;
  font-weight: 700;
}

.search-selection-panel,
.search-books-panel,
.search-tools-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.compact-pane-head {
  height: 38px;
  flex: 0 0 38px;
  padding: 7px 12px;
  display: grid;
  grid-template-columns: 1fr 64px;
  align-items: center;
  border-bottom: 1px solid #bfbab3;
  background: #faf9f6;
  color: #5e0006;
}

.compact-pane-head span:last-child {
  text-align: center;
}

.compact-filter-field {
  height: 38px;
  margin: 6px;
  padding: 0 8px;
  border: 1px solid #aaa49c;
  background: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
}

.compact-filter-field:focus-within {
  border-color: #9b0f06;
  box-shadow: 0 0 0 2px rgba(155, 15, 6, .14);
}

.compact-filter-field svg,
.icon-search-submit svg,
.tiny-icon-button svg,
.results-navigation-buttons svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  flex: 0 0 auto;
}

.compact-filter-field input {
  width: 100%;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: #2a1712;
}

.selection-scroll-list,
.books-selection-list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  scrollbar-color: #9b0f06 #e1ddd7;
  scrollbar-width: thin;
}

.search-selection-row,
.search-book-select-row {
  min-height: 33px;
  border-bottom: 1px solid #d6d1ca;
  display: grid;
  align-items: center;
  cursor: pointer;
  background: #fff;
}

.search-selection-row {
  grid-template-columns: 28px 1fr 64px;
  padding: 0 8px;
}

.search-book-select-row {
  grid-template-columns: 28px minmax(160px, 1fr) minmax(130px, .78fr);
  padding: 0 7px;
}

.search-selection-row:nth-child(even),
.search-book-select-row:nth-child(even) {
  background: #f4f2ee;
}

.search-selection-row:hover,
.search-book-select-row:hover {
  background: #eed9b9;
}

.search-selection-row.selected,
.search-book-select-row.selected {
  background: #f7d8c8;
  color: #5e0006;
}

.search-selection-row input,
.search-book-select-row input,
.inline-check-group input {
  accent-color: #9b0f06;
}

.selection-count {
  text-align: center;
  color: #765c50;
  font-variant-numeric: tabular-nums;
}

.book-filter-head {
  height: 50px;
  flex: 0 0 50px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 6px;
  border-bottom: 1px solid #bfbab3;
}

.book-filter-head .compact-filter-field {
  flex: 1;
  margin: 0;
}

.icon-search-submit,
.tiny-icon-button {
  border: 1px solid #9b0f06;
  background: #5e0006;
  color: #fff9f0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-search-submit {
  width: 38px;
  height: 38px;
}

.books-columns-head {
  grid-template-columns: 1.28fr .78fr;
  padding-right: 42px;
}

.search-book-name,
.search-book-author {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-book-name { color: #5e0006; font-weight: 700; }
.search-book-author { color: #5d4036; }

.search-tools-panel {
  background: #f7f6f3;
}

.search-tools-title-row {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px 5px 12px;
  border-bottom: 1px solid #bfbab3;
  color: #5e0006;
}

.tiny-icon-button {
  width: 34px;
  height: 34px;
  background: #eed9b9;
  color: #5e0006;
}

.inline-check-group {
  border: 0;
  border-bottom: 1px solid #c8c3bc;
  padding: 10px 8px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.inline-check-group label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.normalization-options {
  min-height: 76px;
  align-items: center;
}

.reference-terms-builder {
  margin: 0;
  padding: 8px 5px 6px;
  border: 0;
  border-bottom: 1px solid #c8c3bc;
}

.reference-terms-builder .search-relation-switch {
  justify-content: flex-start;
  margin: 0 0 6px;
}

.numbered-query {
  min-height: 37px;
  display: grid;
  grid-template-columns: 26px 1fr;
  align-items: center;
  border: 1px solid #aaa49c;
  border-bottom: 0;
  background: #fff;
}

.numbered-query:last-child { border-bottom: 1px solid #aaa49c; }
.numbered-query span { text-align: center; color: #5e0006; font-weight: 700; }
.numbered-query input { height: 35px; min-width: 0; border: 0; border-right: 1px solid #ddd7cf; padding: 0 8px; outline: 0; }
.numbered-query:focus-within { border-color: #9b0f06; position: relative; z-index: 1; }

.sort-reference-row {
  min-height: 41px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid #c8c3bc;
}

.sort-reference-row select {
  min-width: 88px;
  height: 31px;
  border: 1px solid #aaa49c;
  background: #fff;
  color: #2a1712;
}

.selected-search-filters {
  flex: 1;
  min-height: 70px;
  margin: 8px 6px 0;
  border: 1px solid #c8c3bc;
  background: #fff;
  overflow: auto;
}

.selected-search-empty {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
  color: #765c50;
  text-align: center;
}

.selected-search-filter {
  min-height: 36px;
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 32px;
  align-items: center;
  gap: 6px;
  padding: 3px 5px;
  border-bottom: 1px solid #ddd8d1;
  background: #fff;
}

.selected-search-filter:nth-child(even) { background: #f7f5f1; }
.selected-filter-kind { color: #9b0f06; font-size: 14.4px; font-weight: 700; }
.selected-filter-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #2a1712;
}

.selected-search-filter button {
  width: 28px;
  height: 28px;
  padding: 5px;
  border: 1px solid #9b0f06;
  background: #fff;
  color: #5e0006;
  cursor: pointer;
}

.selected-search-filter button:hover,
.selected-search-filter button:focus-visible { background: #eed9b9; }
.selected-search-filter button svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; }

.search-scope-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.search-scope-status {
  min-height: 35px;
  padding: 7px 8px;
  color: #5e0006;
  border-top: 1px solid #c8c3bc;
  background: #eed9b9;
}

.clear-all-search-selections {
  min-width: 62px;
  min-height: 28px;
  padding: 2px 10px;
  border: 1px solid #9b0f06;
  background: #fff;
  color: #5e0006;
  cursor: pointer;
}

.clear-all-search-selections:disabled {
  border-color: #bfbab3;
  color: #8c8179;
  cursor: default;
  opacity: .65;
}

.reference-bottom-bar {
  grid-column: 1 / -1;
  min-height: 38px;
  padding: 3px 8px;
  border-top: 1px solid #aaa49c;
  background: #f7f5f1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.reference-bottom-bar > div { display: flex; gap: 5px; }
.reference-bottom-bar button {
  min-height: 30px;
  padding: 3px 10px;
  border: 1px solid #9b0f06;
  background: #fff;
  color: #5e0006;
  cursor: pointer;
}
.reference-bottom-bar .primary-bottom-action {
  min-width: 150px;
  min-height: 60px;
  padding: 6px 20px;
  background: #5e0006;
  color: #fff9f0;
  font-size: 200%;
  line-height: 1;
}

/* شاشة النتائج: قارئ علوي وجدول ثابت في الأسفل */
.search-results-workspace {
  display: grid;
  grid-template-rows: 42px minmax(280px, 1fr) 38px minmax(230px, 34vh);
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: #efeeeb;
  color: #2a1712;
}

.search-results-workspace[hidden] { display: none; }

.results-top-toolbar {
  display: grid;
  grid-template-columns: 220px 1fr 220px;
  align-items: center;
  padding: 3px 8px;
  border-bottom: 1px solid #aaa49c;
  background: #f6f4f0;
}

.toolbar-text-button,
.results-navigation-buttons button,
.preview-page-controls button,
.results-status-strip button {
  min-height: 32px;
  border: 1px solid #9b0f06;
  background: #fff;
  color: #5e0006;
  cursor: pointer;
}

.toolbar-text-button { justify-self: end; padding: 2px 12px; }
.result-book-heading { text-align: center; color: #5e0006; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-book-heading span { margin-right: 8px; color: #765c50; font-size: .85em; }
.results-navigation-buttons { display: flex; gap: 4px; direction: ltr; }
.results-navigation-buttons button { width: 34px; display: inline-flex; align-items: center; justify-content: center; }

.results-reader-area {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(245px, 19%) 1fr;
  direction: rtl;
  overflow: hidden;
}

.search-preview-toc {
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-left: 1px solid #aaa49c;
  background: #f8f7f4;
}

.preview-toc-head {
  min-height: 36px;
  padding: 7px 10px;
  border-bottom: 1px solid #c8c3bc;
  color: #5e0006;
  font-weight: 700;
}

.preview-toc-list { flex: 1; min-height: 0; overflow: auto; }
.preview-toc-item {
  width: 100%;
  min-height: 31px;
  padding: 5px 10px;
  border: 0;
  border-bottom: 1px solid #ddd8d1;
  background: #fff;
  color: #2a1712;
  text-align: right;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.preview-toc-item:hover,
.preview-toc-item.active { background: #eed9b9; color: #5e0006; }

.search-preview-page {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr 38px;
  background: #eed9b9;
  border: 12px solid #e9e7e2;
}

.search-preview-text {
  min-height: 0;
  overflow: auto;
  padding: 18px 28px;
  border: 1px solid #9b0f06;
  background: #fff8e6;
  font-family: var(--font-book);
  font-size: clamp(1.2rem, 1.7vw, 1.75rem);
  line-height: 2;
  text-align: justify;
  direction: rtl;
}

.search-preview-text p { margin-bottom: .7em; }
.search-preview-text mark,
.reference-results-table mark { background: transparent; color: #d53e0f; font-weight: 700; }

.preview-page-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border-top: 1px solid #aaa49c;
  background: #f8f6f2;
}
.preview-page-controls button { min-width: 72px; min-height: 30px; }

.results-status-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 3px 10px;
  border-top: 1px solid #aaa49c;
  border-bottom: 1px solid #aaa49c;
  background: #f7f5f1;
  color: #5e0006;
}
.results-status-strip button { padding: 2px 10px; }

.reference-results-container { min-height: 0; overflow: auto; background: #fff; }
.reference-results-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 13px; direction: rtl; }
.reference-results-table th,
.reference-results-table td { border: 1px solid #c3beb7; padding: 4px 7px; vertical-align: top; }
.reference-results-table th { position: sticky; top: 0; z-index: 2; background: #f5f2ed; color: #5e0006; font-weight: 700; }
.reference-results-table th:nth-child(1) { width: 54px; }
.reference-results-table th:nth-child(2) { width: 21%; }
.reference-results-table th:nth-child(3) { width: auto; }
.reference-results-table th:nth-child(4) { width: 18%; }
.reference-results-table th:nth-child(5) { width: 95px; }
.reference-results-table tbody tr { cursor: pointer; background: #fff; }
.reference-results-table tbody tr:nth-child(even) { background: #f7f5f1; }
.reference-results-table tbody tr:hover { background: #eed9b9; }
.reference-results-table tbody tr.active { background: #9b0f06; color: #fff9f0; }
.reference-results-table tbody tr.active td:first-child { background: #fff; color: #5e0006; }
.reference-results-table tbody tr.active a,
.reference-results-table tbody tr.active mark { color: #fff9f0; }
.reference-results-table a { color: inherit; text-decoration: none; }
.result-cell-book { color: #5e0006; font-weight: 700; }
.result-cell-location { text-align: center; white-space: nowrap; }
.search-empty-reference { height: 100%; display: grid; place-items: center; text-align: center; color: #765c50; padding: 30px; }
.search-loading-reference::after { content: ''; width: 28px; height: 28px; border: 3px solid #eed9b9; border-top-color: #9b0f06; border-radius: 50%; animation: searchSpin .8s linear infinite; }
@keyframes searchSpin { to { transform: rotate(360deg); } }

@media (max-width: 980px) {
  body.search-workspace-mode { display: block; height: auto; overflow: visible; overflow-x: clip; }
  body.search-workspace-mode .main-wrapper { height: auto; min-height: 0; }
  .search-setup-workspace { height: auto; min-height: 100dvh; grid-template-columns: 68px minmax(0, 1fr); grid-template-rows: auto auto auto auto; }
  .search-mode-rail { grid-row: 1 / span 3; }
  .search-selection-panel { grid-column: 2; min-height: 300px; max-height: 42vh; }
  .search-books-panel { grid-column: 2; min-height: 360px; max-height: 48vh; }
  .search-tools-panel { grid-column: 2; min-height: 430px; }
  .reference-bottom-bar { grid-column: 1 / -1; position: sticky; bottom: 0; z-index: 5; }
  .search-results-workspace { height: 100dvh; grid-template-rows: 50px minmax(280px, 1fr) 42px minmax(210px, 38vh); }
  .results-top-toolbar { grid-template-columns: auto 1fr auto; }
  .search-preview-toc { display: none; }
  .results-reader-area { grid-template-columns: 1fr; }
  .reference-results-table { min-width: 820px; }
}

@media (max-width: 600px) {
  .search-setup-workspace { display: flex; flex-direction: column; }
  .search-mode-rail { order: 0; flex-direction: row; padding: 3px; position: sticky; top: 0; z-index: 6; }
  .search-mode-button { flex: 1; min-height: 58px; font-size: 13.2px; }
  .search-mode-button svg { width: 25px; height: 25px; }
  .search-books-panel { order: 1; min-height: 420px; max-height: none; }
  .search-tools-panel { order: 2; min-height: 470px; }
  .search-selection-panel { order: 3; min-height: 320px; max-height: none; }
  .reference-bottom-bar { order: 4; flex-wrap: wrap; min-height: 70px; }
  .reference-bottom-bar > span { width: 100%; font-size: 14.4px; }
  .reference-bottom-bar > div { width: 100%; flex-wrap: wrap; }
  .reference-bottom-bar .primary-bottom-action { flex: 1 1 150px; }
  .reference-bottom-bar button { min-height: 44px; }
  .selected-search-filter { min-height: 44px; grid-template-columns: 50px minmax(0, 1fr) 44px; }
  .selected-search-filter button { width: 44px; height: 44px; padding: 12px; }
  .clear-all-search-selections { min-height: 44px; }
  .search-book-select-row { min-height: 44px; grid-template-columns: 32px minmax(120px, 1fr) minmax(100px, .7fr); }
  .search-selection-row { min-height: 44px; }
  .results-top-toolbar { grid-template-columns: auto 1fr; }
  .result-book-heading { text-align: right; }
  .results-navigation-buttons { display: none; }
  .search-preview-page { border-width: 5px; }
  .search-preview-text { padding: 14px; font-size: 1.15rem; line-height: 1.9; }
}

@media (prefers-reduced-motion: reduce) {
  .search-loading-reference::after { animation-duration: 1.8s; }
}
