:root {
  --bg: #f8f6f1;
  --surface: #ffffff;
  --surface-2: #f1eee6;
  --text: #1f2a26;
  --muted: #66716c;
  --border: #e2ddd1;
  --primary: #1d5c4a;
  --primary-hover: #164a3b;
  --primary-soft: #e3efe9;
  --on-primary: #ffffff;
  --accent: #a8823a;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --ok-soft: #e6f2ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 25, 0.06), 0 2px 8px rgba(20, 30, 25, 0.04);
  --font-body: 'Noto Kufi Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  --font-title: var(--font-body);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121715;
    --surface: #1a211e;
    --surface-2: #222b27;
    --text: #e6ebe8;
    --muted: #9aa6a0;
    --border: #2e3934;
    --primary: #5fb595;
    --primary-hover: #77c7a8;
    --primary-soft: #1f342c;
    --on-primary: #0d1512;
    --accent: #d2ac62;
    --danger: #f2867d;
    --danger-soft: #3a1f1d;
    --ok-soft: #1d3329;
    --shadow: none;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.7;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
/* Book listing pages: lib_Mid.png tiled behind the cards (tinted down in dark mode) */
body.listing-page { background: var(--bg) url('/images/lib_Mid.png') 0 0 / 520px repeat; }
@media (prefers-color-scheme: dark) {
  body.listing-page { background-image: linear-gradient(rgba(18, 23, 21, .9), rgba(18, 23, 21, .9)), url('/images/lib_Mid.png'); background-size: auto, 520px; }
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { font-family: var(--font-title); font-weight: 700; line-height: 1.5; margin: 0 0 .5em; }
.icon { width: 20px; height: 20px; flex: none; vertical-align: -4px; margin-inline-end: 6px; }
.title-icon { width: 36px; height: 36px; vertical-align: middle; margin-inline-end: 10px; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.25rem; }
.container { width: 100%; max-width: 1100px; margin: 0 auto; padding: 0 16px; }
.main { flex: 1; padding-top: 24px; padding-bottom: 48px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.nowrap { white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Header */
/* Ornamented cream band (Top.PNG) in both colour schemes, so the header keeps a fixed light palette */
.site-header { background: #f3edd2 url('/images/Top.PNG') center / 100% 100% no-repeat; color: #1d5c4a; border-bottom: 3px solid #a8823a; box-shadow: 0 2px 8px rgba(80, 60, 20, .12); }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 64px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; color: inherit; font-family: var(--font-title); font-weight: 700; font-size: 1.2rem; padding: 8px 0; }
.brand:hover { text-decoration: none; }
.brand-logo { height: 48px; width: auto; border-radius: 6px; box-shadow: 0 0 0 1px #d8c89c; }
.nav { display: flex; gap: 4px; }
.nav a { display: inline-flex; align-items: center; color: inherit; font-weight: 500; padding: 6px 12px; border-radius: 8px; }
.nav a:hover, .nav a.active { background: rgba(29, 92, 74, .1); text-decoration: none; }
.admin-bar { background: var(--surface-2); color: var(--text); border-bottom: 1px solid var(--border); }
.admin-bar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; min-height: 44px; }
.admin-bar nav { display: flex; gap: 2px; flex-wrap: wrap; }
.admin-bar nav a { display: inline-flex; align-items: center; color: var(--text); padding: 4px 10px; border-radius: 6px; font-size: .925rem; }
.admin-bar nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.logout { display: flex; align-items: center; gap: 10px; font-size: .875rem; }

/* Footer */
.site-footer { border-top: 1px solid var(--border); padding: 20px 0; font-size: .875rem; color: var(--muted); }
.footer-inner { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* Buttons */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--primary); color: var(--on-primary);
  border: 1px solid var(--primary); border-radius: 8px;
  padding: 8px 18px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.button:hover { background: var(--primary-hover); text-decoration: none; }
.button-ghost { background: transparent; color: var(--primary); border-color: var(--border); }
.button-ghost:hover { background: var(--primary-soft); }
.button.small { padding: 4px 12px; font-size: .875rem; }
.link-button { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.link-button:hover { text-decoration: underline; }
.danger { color: var(--danger); }
form.inline { display: inline; }

/* Search / hero: two library photos framing a decorated search panel */
.hero { display: grid; grid-template-columns: minmax(0, 200px) minmax(0, 1fr) minmax(0, 200px); gap: 14px; align-items: stretch; margin-top: 4px; }
/* The side photos carry their own gilded frame, so show them whole (no crop, no rounding) */
.hero-side { width: 100%; height: auto; aspect-ratio: 255 / 472; align-self: center; filter: drop-shadow(0 4px 10px rgba(80, 60, 20, .18)); }
.hero-main {
  min-width: 0; text-align: center; padding: 22px 24px 26px;
  border: 7px solid #e9dfc2; outline: 1px solid #cdbb8a; outline-offset: -8px;
  box-shadow: 0 4px 10px rgba(80, 60, 20, .18); color: #1f2a26;
  background: #f6f1dc url('/images/lib_Mid.png') center bottom / cover no-repeat;
  display: flex; flex-direction: column; justify-content: center;
}
.hero-logo { width: 200px; height: auto; margin: 0 auto 10px; border-radius: 8px; box-shadow: 0 2px 10px rgba(80, 60, 20, .15); }
.hero h1 { font-size: 2rem; color: #1d5c4a; margin-bottom: .2em; }
.hero p { margin: 0; color: #5a5040; }
.search { display: flex; gap: 8px; flex-wrap: wrap; max-width: 860px; margin: 20px auto 0; }
.search.compact { margin: 0 0 12px; max-width: none; }
.search > * { min-width: 0; }
.search input[type=search] { flex: 1 1 280px; }
.search select { flex: 0 1 190px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; min-height: 42px;
}
textarea { resize: vertical; }
/* Selects and suggestion inputs (datalist) share one chevron instead of the browsers' mismatched native arrows */
select, input[list], input[data-suggest] {
  appearance: none; -webkit-appearance: none; height: 42px; padding-inline-end: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%238a8170' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-size: 14px; background-position: left 12px center;
}
[dir=ltr] select, [dir=ltr] input[list], [dir=ltr] input[data-suggest] { background-position: right 12px center; }
.suggest-menu {
  position: absolute; z-index: 50; margin: 0; padding: 4px; list-style: none;
  max-height: 260px; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(20, 30, 25, .14);
}
.suggest-menu li { padding: 7px 12px; border-radius: 6px; cursor: pointer; overflow-wrap: anywhere; }
.suggest-menu li:hover, .suggest-menu li[aria-selected=true] { background: var(--primary-soft); }
select { cursor: pointer; }
input[list]::-webkit-calendar-picker-indicator { display: none !important; }
input[list]::-webkit-list-button { display: none !important; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.result-count { color: var(--muted); margin: 22px 0 12px; }

/* Book cards */
.book-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.book-card { min-width: 0; overflow-wrap: anywhere; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 6px; }
.book-title { font-size: 1.05rem; margin: 0; display: flex; align-items: flex-start; gap: 8px; }
.book-icon { width: 26px; height: 26px; flex: none; margin-top: 3px; }
.book-note {
  margin: 2px 0 0; font-size: .85rem; color: var(--muted); background: var(--surface-2);
  border-inline-start: 3px solid var(--accent); border-radius: 6px; padding: 6px 10px;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.book-title a { color: var(--text); }
.book-title a:hover { color: var(--primary); }
.book-meta { font-size: .925rem; color: var(--muted); display: flex; flex-direction: column; gap: 2px; }
.book-meta.lead { font-size: 1.05rem; }
.contrib-role { color: var(--accent); font-weight: 500; margin-left: 4px; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 6px; }
.tag { font-size: .8rem; padding: 2px 10px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); }
.tag-plain { background: var(--surface-2); color: var(--muted); }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* Infinite scroll */
.load-more { grid-column: 1 / -1; justify-self: center; padding: 8px 22px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); }
.load-more:hover { text-decoration: none; border-color: var(--primary); }
.feed-status { text-align: center; margin-top: 18px; }

/* Pagination */
.pagination { display: flex; justify-content: center; align-items: center; gap: 18px; margin-top: 28px; }
.pagination a { padding: 6px 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }

/* Detail pages */
.crumbs { font-size: .875rem; color: var(--muted); margin-bottom: 10px; }
.detail-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.book-detail { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); }
.details { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 0 24px; margin: 20px 0 0; }
.details > div { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.details dt { color: var(--muted); }
.details dd { margin: 0; font-weight: 500; text-align: left; }
.notice { margin-top: 20px; background: var(--surface-2); padding: 14px 18px; border-radius: 8px; }
.notice h2 { font-size: 1rem; margin-bottom: 4px; }
.notice p { margin: 0; white-space: pre-line; }
.topic-grid { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.topic-grid a { display: flex; justify-content: space-between; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; color: var(--text); }
.topic-grid a:hover { border-color: var(--primary); text-decoration: none; }
.count { font-size: .8rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 0 8px; }

/* Admin */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0 16px; }
.toolbar h1, .toolbar h2 { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 28px; }
.stat-icon { width: 44px; height: 44px; margin-bottom: 6px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; display: flex; flex-direction: column; color: var(--text); box-shadow: var(--shadow); }
.stat:hover { border-color: var(--primary); text-decoration: none; }
.stat-value { font-size: 2rem; font-weight: 600; color: var(--primary); font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; background: var(--surface); }
.table th, .table td { text-align: right; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-weight: 600; font-size: .875rem; color: var(--muted); background: var(--surface-2); }
.table tr:last-child td { border-bottom: 0; }
.table .actions { white-space: nowrap; display: flex; gap: 14px; justify-content: flex-end; }
.flash { background: var(--ok-soft); color: var(--primary); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; }
.alert { background: var(--danger-soft); color: var(--danger); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; }
.alert ul { margin: 4px 0 0; padding-right: 20px; }
.hint { color: var(--muted); font-size: .875rem; margin-top: 0; }

/* Forms */
.form { display: flex; flex-direction: column; gap: 16px; }
.form.narrow { max-width: 560px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; font-size: .925rem; }
.form fieldset { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 16px 18px 20px; margin: 0; }
.form legend { font-family: var(--font-title); font-weight: 700; padding: 0 6px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px 16px; }
.span-2 { grid-column: span 2; }
.req { color: var(--danger); }
.contributors { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.contributor-row > * { min-width: 0; }
.contributor-row { display: grid; grid-template-columns: 1fr 140px 32px; gap: 8px; align-items: center; }
.form-actions { display: flex; gap: 10px; }
.danger-zone { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--border); }
.auth-logo { display: block; width: 220px; height: auto; margin: 0 auto 18px; border-radius: 8px; }
.auth-card { max-width: 420px; margin: 40px auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); }
.error-page { text-align: center; padding: 60px 0; }
.error-code { font-size: 4rem; font-weight: 600; color: var(--accent); margin: 0; }

@media (max-width: 860px) {
  .hero { grid-template-columns: minmax(0, 130px) minmax(0, 1fr) minmax(0, 130px); gap: 10px; }
}

@media (max-width: 640px) {
  h1 { font-size: 1.4rem; }
  .hero h1 { font-size: 1.55rem; }
  .hero { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero-main { grid-column: 1 / -1; grid-row: 1; padding: 18px 14px 20px; }
  .hero-side { aspect-ratio: 255 / 200; object-fit: cover; object-position: center 40%; border: 5px solid #e9dfc2; }
  .hero-logo { width: 160px; }
  .brand-logo { height: 38px; }
  .brand { font-size: 1.05rem; }
  .book-list { grid-template-columns: minmax(0, 1fr); }
  .search input[type=search] { flex-basis: 100%; }
  .span-2 { grid-column: auto; }
  .search select { flex: 1 1 45%; }
  .search .button { flex: 1 1 100%; }
  .contributor-row { grid-template-columns: 1fr 110px 28px; }
  .book-detail { padding: 16px; }
}
