@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700;800&display=swap');

:root {
  color-scheme: light;
  --bg: #f7f5ef;
  --surface: #ffffff;
  --surface-soft: #f1efe7;
  --text: #1d2521;
  --muted: #68736d;
  --line: #d8d3c5;
  --teal: #0f766e;
  --teal-dark: #0b5f59;
  --gold: #aa7f24;
  --red: #9f3a3a;
  --shadow: 0 18px 50px rgba(29, 37, 33, 0.1);
  font-family: 'Cairo', Tahoma, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Cairo', Tahoma, Arial, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1440px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 8px 0 22px;
}

.topbar h1,
.section-heading h2,
.report-header h2 {
  margin: 0;
  letter-spacing: 0;
}

.topbar h1 {
  font-size: 34px;
  line-height: 1.2;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--gold);
  font-size: 13px;
  font-weight: 700;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  align-items: start;
}

.search-workspace {
  grid-template-columns: 1fr;
}

.admin-layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

.admin-sidebar {
  position: sticky;
  top: 16px;
  display: grid;
  gap: 16px;
  min-width: 0;
  padding: 18px;
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.sidebar-brand h2 {
  margin: 0;
  font-size: 21px;
  line-height: 1.35;
}

.sidebar-nav {
  display: grid;
  gap: 8px;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 0 12px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-weight: 700;
  text-decoration: none;
  transition: transform 120ms ease, border-color 120ms ease;
}

.sidebar-nav a:hover {
  border-color: var(--teal);
  transform: translateY(-1px);
}

.sidebar-nav a[aria-current='page'] {
  border-color: var(--teal);
  background: #e6f4ef;
  color: var(--teal-dark);
}

.sidebar-nav svg {
  width: 18px;
  height: 18px;
  color: var(--teal);
}

.manage-workspace {
  display: grid;
  gap: 18px;
}

.search-area,
.library-panel,
.manage-panel {
  min-width: 0;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.search-area {
  padding: 22px;
}

.library-panel {
  padding: 18px;
  position: sticky;
  top: 16px;
}

.manage-panel {
  padding: 18px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.report-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.section-heading h2 {
  font-size: 22px;
}

.topbar-actions,
.report-actions,
.search-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button,
.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: transform 120ms ease, background-color 120ms ease, border-color 120ms ease;
}

.icon-button {
  width: 42px;
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border-color: var(--line);
}

.icon-button.small {
  width: 34px;
  min-height: 34px;
  flex: 0 0 34px;
}

.icon-button.small svg {
  width: 17px;
  height: 17px;
}

.icon-button.danger {
  color: var(--red);
}

.icon-button:hover,
.primary-button:hover {
  transform: translateY(-1px);
}

.icon-button:disabled,
.primary-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.icon-button svg,
.primary-button svg {
  width: 19px;
  height: 19px;
}

.primary-button {
  padding: 0 18px;
  color: #fff;
  background: var(--teal);
  border-color: var(--teal);
  font-weight: 700;
  text-decoration: none;
}

.primary-button:hover {
  background: var(--teal-dark);
}

.primary-button.full {
  width: 100%;
}

.search-box {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: stretch;
}

.search-form {
  display: grid;
  gap: 12px;
}

.scope-panel {
  display: grid;
  gap: 12px;
  padding: 14px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.scope-head h3 {
  margin: 0;
  font-size: 18px;
  line-height: 1.4;
}

.scope-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 0;
  padding: 0;
  border: 0;
}

.scope-option {
  min-width: 0;
}

.scope-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.scope-option span {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 44px;
  padding: 0 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
}

.scope-option svg {
  width: 18px;
  height: 18px;
}

.scope-option input:checked + span {
  border-color: var(--teal);
  background: #e6f4ef;
  color: var(--teal-dark);
}

.scope-option input:focus-visible + span {
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.scope-detail {
  display: grid;
  gap: 10px;
}

.scope-detail label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.selected-books-line {
  color: var(--teal-dark);
  font-size: 13px;
  font-weight: 700;
}

.book-dropdown {
  position: relative;
  display: grid;
  gap: 6px;
}

.book-dropdown > label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.book-dropdown-control {
  position: relative;
}

.book-dropdown-control input {
  padding-inline-end: 42px;
}

.book-dropdown-control svg {
  position: absolute;
  inset-block-start: 50%;
  inset-inline-end: 13px;
  width: 18px;
  height: 18px;
  color: var(--muted);
  pointer-events: none;
  transform: translateY(-50%);
}

.book-dropdown-menu {
  position: absolute;
  inset-inline: 0;
  top: calc(100% + 6px);
  z-index: 12;
  display: grid;
  gap: 6px;
  max-height: 300px;
  overflow: auto;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.book-dropdown-item {
  display: grid;
  grid-template-columns: 26px minmax(180px, 2fr) minmax(130px, 1fr) minmax(110px, 0.8fr);
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 6px 8px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.book-dropdown-item input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--teal);
}

.book-dropdown-title,
.book-dropdown-author,
.book-dropdown-classification {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.4;
}

.book-dropdown-title {
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}

.book-dropdown-author,
.book-dropdown-classification {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.dropdown-empty,
.dropdown-hint {
  padding: 10px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.selected-books-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.selected-book-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 260px;
  min-height: 32px;
  padding: 0 9px;
  color: var(--teal-dark);
  background: #e6f4ef;
  border: 1px solid rgba(15, 118, 110, 0.22);
  border-radius: 999px;
  font-weight: 700;
}

.selected-book-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selected-book-chip svg {
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
}

.query-stack {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.search-box textarea,
.scope-detail input,
.scope-detail select,
.upload-form input,
.upload-form select,
.filters-bar input,
.filters-bar select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}

.search-box textarea {
  min-height: 104px;
  resize: vertical;
  padding: 14px;
  line-height: 1.8;
}

.search-box textarea:focus,
.scope-detail input:focus,
.scope-detail select:focus,
.upload-form input:focus,
.upload-form select:focus,
.filters-bar input:focus,
.filters-bar select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.search-controls {
  flex-direction: column;
}

.search-modes {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 0;
  border: 0;
}

.mode-option {
  min-width: 88px;
  flex: 1 1 88px;
}

.mode-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.mode-option span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.mode-option input:checked + span {
  border-color: var(--teal);
  background: #e6f4ef;
  color: var(--teal-dark);
}

.mode-option input:focus-visible + span {
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.search-controls .primary-button {
  min-width: 112px;
  flex: 1;
}

.search-controls .voice {
  flex: 1;
  width: 100%;
}

.status-line {
  min-height: 26px;
  margin: 14px 0;
  color: var(--muted);
  font-size: 14px;
}

.status-line.busy {
  color: var(--teal);
}

.status-line.error {
  color: var(--red);
}

.ocr-note {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 14px 0 0;
  padding: 12px 14px;
  border: 1px solid #ead18a;
  border-radius: 8px;
  background: #fff8dc;
  color: #5f4a13;
}

.ocr-note svg {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  margin-top: 3px;
}

.ocr-note p {
  margin: 0;
  font-size: 14px;
  line-height: 1.8;
}

.report {
  margin-top: 16px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.report pre {
  margin: 12px 0 0;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.9;
}

.print-query-block {
  display: none;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.result-card,
.book-row {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.result-card {
  padding: 15px;
}

.result-card header,
.book-row header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.result-card h3,
.book-row h3 {
  margin: 0;
  font-size: 16px;
  line-height: 1.5;
}

.result-card p,
.book-row p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.8;
}

.search-hit {
  padding: 0 3px;
  border-radius: 4px;
  background: #ffe45c;
  color: #171a16;
  font-weight: 800;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.pill.warning {
  background: #fff0de;
  color: #8a5b13;
}

.pill.ok {
  background: #e6f4ef;
  color: var(--teal-dark);
}

.open-source {
  margin-top: 14px;
}

.open-source a {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--teal-dark);
  font-weight: 700;
  text-decoration: none;
}

.upload-form {
  display: grid;
  gap: 12px;
}

.filters-bar {
  display: grid;
  grid-template-columns: minmax(220px, 1.5fr) minmax(180px, 1fr) minmax(180px, 1fr);
  gap: 10px;
  margin-bottom: 14px;
  padding: 12px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.filters-bar label {
  display: grid;
  gap: 6px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.filters-bar.single-filter {
  grid-template-columns: minmax(220px, 1fr);
}

.metadata-form {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.metadata-form .wide {
  grid-column: 1 / -1;
}

.upload-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.upload-form input,
.upload-form select,
.scope-detail input,
.scope-detail select,
.filters-bar input,
.filters-bar select {
  min-height: 42px;
  padding: 0 11px;
}

.upload-form input[type='file'] {
  padding: 9px 11px;
}

.books-list {
  display: grid;
  gap: 10px;
  margin-top: 18px;
  max-height: calc(100vh - 470px);
  overflow: auto;
  padding-left: 4px;
}

.book-row {
  padding: 12px;
}

.book-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.table-wrap {
  width: 100%;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.books-table {
  width: 100%;
  min-width: 980px;
  border-collapse: collapse;
}

.classifications-table {
  min-width: 720px;
}

.books-table th,
.books-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  vertical-align: top;
  font-size: 14px;
}

.books-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-soft);
  color: var(--text);
  font-weight: 700;
}

.books-table tbody tr:last-child td {
  border-bottom: 0;
}

.books-table .title-cell {
  min-width: 230px;
  font-weight: 700;
}

.row-actions {
  display: flex;
  gap: 6px;
  white-space: nowrap;
}

.table-empty {
  color: var(--muted);
  text-align: center !important;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 14px;
}

.compact-pagination {
  margin-top: 12px;
}

.pagination-page {
  min-width: 34px;
  min-height: 34px;
  padding: 0 10px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-weight: 700;
}

.pagination-page.active {
  color: #fff;
  background: var(--teal);
  border-color: var(--teal);
}

.pagination-gap {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 0 4px;
  color: var(--muted);
}

.edit-dialog {
  width: min(760px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.compact-dialog {
  width: min(520px, calc(100vw - 28px));
}

.edit-dialog::backdrop {
  background: rgba(29, 37, 33, 0.28);
}

.dialog-actions {
  display: flex;
  justify-content: flex-start;
  gap: 8px;
}

.empty {
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.compact-empty {
  padding: 12px;
  font-size: 13px;
}

@media (max-width: 1040px) {
  .workspace,
  .admin-layout {
    grid-template-columns: 1fr;
  }

  .library-panel,
  .admin-sidebar {
    position: static;
  }

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

@media (max-width: 720px) {
  .app-shell {
    padding: 14px;
  }

  .topbar {
    align-items: flex-start;
  }

  .topbar h1 {
    font-size: 27px;
  }

  .search-area,
  .library-panel,
  .manage-panel {
    padding: 14px;
  }

  .search-box,
  .results-grid,
  .scope-options {
    grid-template-columns: 1fr;
  }

  .book-dropdown-item {
    grid-template-columns: 24px minmax(0, 1.5fr) minmax(90px, 1fr);
  }

  .book-dropdown-classification {
    display: none;
  }

  .metadata-form {
    grid-template-columns: 1fr;
  }

  .filters-bar {
    grid-template-columns: 1fr;
  }

  .search-controls {
    flex-direction: row;
  }

  .search-controls .voice {
    width: 48px;
    flex: 0 0 48px;
  }
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .library-panel,
  .search-area > .section-heading,
  .search-box,
  .status-line,
  .ocr-note,
  .results-grid,
  .report-actions {
    display: none !important;
  }

  .report-header .icon-button {
    display: none !important;
  }

  .app-shell,
  .search-area {
    width: 100%;
    min-height: auto;
    margin: 0;
    padding: 0;
    border: 0;
    box-shadow: none;
  }

  .workspace {
    display: block;
  }

  .report {
    display: block !important;
    border: 0;
    padding: 0;
  }

  .print-query-block {
    display: block;
    margin: 0 0 24px;
  }

  .print-query-block h2,
  .report-header .eyebrow {
    margin: 0 0 10px;
    color: #9a6f16;
    font-size: 18px;
    font-weight: 700;
  }

  .print-query-block p {
    margin: 0;
    color: #111;
    font-size: 16px;
    line-height: 1.9;
  }

  .report-header h2 {
    display: none;
  }

  .report pre {
    margin-top: 0;
  }
}
