@import url("https://fonts.googleapis.com/css2?family=Aref+Ruqaa:wght@400;700&family=Source+Serif+4:opsz,wght@8..60,400;8..60,600;8..60,700&display=swap");

:root {
  color-scheme: dark;
  --bg: #060606;
  --paper: #0b0b0b;
  --panel: #111111;
  --ink: #eeeeee;
  --muted: rgba(238, 238, 238, 0.72);
  --line: rgba(238, 238, 238, 0.42);
  --line-strong: rgba(238, 238, 238, 0.74);
  --accent: #eeeeee;
  --accent-strong: #ffffff;
  --accent-soft: rgba(238, 238, 238, 0.1);
  --gold: #eeeeee;
  --red: #eeeeee;
  --danger: #eeeeee;
  --warn-bg: #151515;
  --ok-bg: #151515;
  --err-bg: #151515;
  --info-bg: #151515;
  --library-bg: #081f23;
  --library-card: #173236;
  --library-card-hover: #1b3a3f;
  --library-card-line: rgba(161, 181, 186, 0.24);
  --library-card-line-strong: rgba(186, 202, 205, 0.38);
  --library-card-ink: #edf3f2;
  --library-card-muted: rgba(223, 234, 233, 0.72);
  --library-card-icon: rgba(221, 232, 233, 0.7);
  --library-card-shadow: 0 1px 2px rgba(0, 0, 0, 0.32), 0 10px 22px rgba(0, 0, 0, 0.12);
  --shadow: none;
  --font-ui: "Source Serif 4", "Noto Serif", Georgia, serif;
  --font-display: "Aref Ruqaa", "Source Serif 4", Georgia, serif;
}

* { box-sizing: border-box; }
::selection {
  background: #cbded6;
  color: var(--ink);
}
body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(#075f6f 0 92px, var(--library-bg) 92px 100%),
    var(--library-bg);
  color: var(--ink);
  font-family: var(--font-ui);
  line-height: 1.45;
}
header {
  border-bottom: 1px solid rgba(186, 202, 205, 0.26);
  background: #075f6f;
  color: var(--library-card-ink);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04), 0 8px 18px rgba(0, 0, 0, 0.16);
}
.shell {
  width: min(1280px, calc(100% - 32px));
  margin: 0 auto;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 92px;
}
h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.85rem, 3vw, 2.8rem);
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1.05;
}
.metric {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(223, 234, 233, 0.28);
  border-radius: 999px;
  background: rgba(4, 38, 44, 0.34);
  color: rgba(237, 243, 242, 0.78);
  font-size: 0.94rem;
  font-weight: 600;
  white-space: nowrap;
}
main {
  padding: 22px 0 48px;
  background: var(--library-bg);
}
.library-controls {
  margin-bottom: 16px;
  border: 1px solid var(--library-card-line);
  border-radius: 4px;
  background: var(--library-card);
  box-shadow: var(--library-card-shadow);
}
.library-controls summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 50px;
  padding: 0 14px;
  color: var(--library-card-ink);
  cursor: pointer;
  font-weight: 740;
  list-style: none;
}
.library-controls summary::-webkit-details-marker {
  display: none;
}
.library-controls summary::after {
  content: "";
  width: 9px;
  height: 9px;
  flex: 0 0 auto;
  border-right: 2px solid var(--library-card-muted);
  border-bottom: 2px solid var(--library-card-muted);
  transform: rotate(45deg) translateY(-2px);
  transition: transform 140ms ease;
}
.library-controls[open] summary {
  border-bottom: 1px solid var(--library-card-line);
}
.library-controls[open] summary::after {
  transform: rotate(225deg) translate(-1px, -1px);
}
.library-controls summary:focus-visible {
  outline: 2px solid rgba(237, 243, 242, 0.2);
  outline-offset: 2px;
}
.library-controls-hint {
  margin-left: auto;
  color: var(--library-card-muted);
  font-size: 0.88rem;
  font-weight: 600;
  text-align: right;
}
.library-controls-body {
  padding: 14px;
}
.toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1.4fr) minmax(170px, 0.8fr) minmax(170px, 0.8fr) minmax(170px, 0.75fr) auto;
  gap: 12px;
  align-items: center;
  margin-bottom: 0;
}
.ai-settings {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}
.ai-settings label {
  display: grid;
  grid-template-columns: auto minmax(220px, 360px);
  gap: 12px;
  align-items: center;
  width: min(100%, 560px);
  color: var(--library-card-muted);
  font-weight: 700;
}
.ai-settings select:disabled {
  opacity: 0.68;
  cursor: not-allowed;
}
.ai-settings-note {
  color: var(--library-card-muted);
  font-size: 0.88rem;
  text-align: right;
}
input[type="search"], select {
  width: 100%;
  border: 1px solid var(--library-card-line-strong);
  border-radius: 3px;
  min-height: 44px;
  padding: 0 13px;
  font: inherit;
  background: rgba(4, 31, 36, 0.72);
  color: var(--library-card-ink);
}
input[type="search"]::placeholder {
  color: rgba(223, 234, 233, 0.48);
}
input[type="search"]:focus,
select:focus,
button:focus-visible,
.button-link:focus-visible {
  border-color: rgba(237, 243, 242, 0.76);
  outline: 2px solid rgba(237, 243, 242, 0.2);
  outline-offset: 1px;
}
select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--library-card-muted) 50%), linear-gradient(135deg, var(--library-card-muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 18px, calc(100% - 11px) 18px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}
button,
.button-link {
  border: 1px solid rgba(237, 243, 242, 0.42);
  border-radius: 3px;
  min-height: 40px;
  padding: 0 15px;
  background: rgba(237, 243, 242, 0.92);
  color: #062329;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease, transform 140ms ease;
}
.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
button:hover,
.button-link:hover {
  background: transparent;
  color: var(--library-card-ink);
  transform: translateY(-1px);
}
button.secondary,
.button-link.secondary {
  background: rgba(4, 31, 36, 0.72);
  color: var(--library-card-ink);
}
button.secondary:hover,
.button-link.secondary:hover {
  border-color: rgba(237, 243, 242, 0.64);
  color: var(--library-card-ink);
  background: rgba(237, 243, 242, 0.08);
}
button.danger,
.button-link.danger {
  border-color: rgba(237, 243, 242, 0.32);
  background: rgba(4, 31, 36, 0.72);
  color: var(--library-card-ink);
}
button.danger:hover,
.button-link.danger:hover {
  border-color: rgba(237, 243, 242, 0.64);
  background: rgba(237, 243, 242, 0.12);
  color: var(--library-card-ink);
}
.button-link.disabled {
  pointer-events: none;
  opacity: 0.55;
}
button:disabled {
  cursor: progress;
  opacity: 0.65;
}
.book-list {
  display: grid;
  gap: 10px;
}
.book-card {
  position: relative;
  display: grid;
  grid-template-rows: minmax(70px, auto) auto;
  min-height: 128px;
  overflow: hidden;
  border: 1px solid var(--library-card-line);
  border-radius: 4px;
  background: var(--library-card);
  color: var(--library-card-ink);
  box-shadow: var(--library-card-shadow);
  transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}
.book-card:hover,
.book-card:focus-within {
  border-color: var(--library-card-line-strong);
  background: var(--library-card-hover);
  transform: translateY(-1px);
}
.book-card-link {
  position: relative;
  display: grid;
  grid-template-areas: "title meta";
  grid-template-columns: minmax(0, 1fr) minmax(120px, 260px);
  gap: 14px;
  min-height: 78px;
  padding: 14px 18px 4px;
  color: inherit;
  cursor: pointer;
  text-decoration: none;
}
.book-card-title-stack {
  grid-area: title;
  display: grid;
  align-content: start;
  gap: 4px;
  min-width: 0;
}
.book-card-title {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  font-family: var(--font-ui);
  font-size: clamp(1.18rem, 2.4vw, 1.72rem);
  font-weight: 650;
  line-height: 1.08;
  text-align: right;
}
.book-card-author {
  align-self: start;
  color: var(--library-card-muted);
  font-size: clamp(0.9rem, 1.4vw, 1rem);
  font-weight: 600;
  line-height: 1.12;
  overflow-wrap: anywhere;
  text-align: right;
}
.book-card-side {
  grid-area: meta;
  align-self: start;
  max-width: 260px;
  color: var(--library-card-muted);
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: left;
  overflow-wrap: anywhere;
}
.book-card-action-row {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  min-height: 46px;
  margin: 0 18px;
  padding: 10px 0 12px;
}
.book-card-action-row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, var(--library-card-line-strong), var(--library-card-line), var(--library-card-line-strong));
}
.book-card-action-row::after {
  content: "";
  position: absolute;
  top: -2px;
  left: 18%;
  width: 5px;
  height: 5px;
  border-radius: 999px;
  background: var(--library-card-icon);
  box-shadow: 36vw 0 0 var(--library-card-icon);
}
.title-sub, .meta, .status {
  display: none;
  color: var(--library-card-muted);
  font-size: 0.88rem;
  margin-top: 3px;
}
code {
  display: inline-block;
  margin-top: 5px;
  padding: 2px 5px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--bg);
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  overflow-wrap: anywhere;
}
.actions {
  width: auto;
}
.action-control {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-height: 28px;
}
.download-form {
  display: flex;
  align-items: center;
  gap: 6px;
}
.downloaded-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}
.book-card-action-row button {
  min-height: 30px;
  padding: 0 9px;
  border-color: transparent;
  background: transparent;
  color: var(--library-card-icon);
  font-size: 0.84rem;
  font-weight: 700;
}
.book-card-action-row button.secondary:hover,
.book-card-action-row button.danger:hover,
.book-card-action-row button:hover {
  border-color: var(--library-card-line-strong);
  background: rgba(238, 246, 246, 0.08);
  color: var(--library-card-ink);
}
.delete-download-form {
  margin: 0;
}
.delete-download-form.htmx-request button {
  cursor: wait;
  opacity: 0.68;
}
.download-form.htmx-request button {
  display: none;
}
.download-form .busy {
  display: none;
  color: var(--library-card-muted);
  font-size: 0.86rem;
  align-items: center;
  gap: 7px;
}
.download-form.htmx-request .busy {
  display: inline-flex;
}
.download-indicator {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 30px;
  padding: 0 8px;
  border: 1px solid var(--library-card-line-strong);
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.12);
  color: var(--library-card-muted);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}
.download-spinner {
  width: 13px;
  height: 13px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 999px;
  animation: download-spin 800ms linear infinite;
}
@keyframes download-spin {
  to { transform: rotate(360deg); }
}
.downloaded-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid var(--library-card-line-strong);
  background: rgba(0, 0, 0, 0.12);
  color: var(--library-card-ink);
  font-size: 0.78rem;
  font-weight: 720;
}
.downloaded-detail {
  display: block;
  color: var(--library-card-muted);
  font-size: 0.82rem;
  margin-top: 5px;
}
.notice {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 0;
  border: 1px solid var(--line);
  font-size: 0.88rem;
}
.notice.success {
  background: var(--ok-bg);
  border-color: var(--line-strong);
}
.notice.error {
  background: var(--err-bg);
  border-color: var(--line-strong);
  color: var(--danger);
}
.empty {
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 0;
  background: var(--panel);
  color: var(--muted);
}
.result-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 650;
}
.pager {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(36, 31, 26, 0.5);
  backdrop-filter: blur(5px);
}
.modal {
  width: min(560px, 100%);
  max-height: min(720px, calc(100vh - 40px));
  overflow: auto;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
}
.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 18px 12px;
  border-bottom: 1px solid var(--line);
}
.modal-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 760;
}
.modal-body {
  padding: 18px;
}
.modal-close {
  min-height: 32px;
  padding: 0 10px;
}
.progress {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--bg);
  margin: 14px 0;
}
.progress-bar {
  height: 100%;
  width: var(--progress);
  border-radius: inherit;
  background: var(--accent);
  transition: width 180ms ease;
}
.job-phase {
  font-weight: 740;
}
.job-message {
  color: var(--muted);
  margin-top: 4px;
}
.path-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
@media (max-width: 720px) {
  .shell { width: min(100% - 20px, 1280px); }
  .topbar { align-items: flex-start; flex-direction: column; padding: 16px 0; }
  .metric { white-space: normal; }
  .library-controls {
    margin-bottom: 12px;
  }
  .library-controls summary {
    min-height: 48px;
    padding: 0 12px;
  }
  .library-controls-hint {
    display: none;
  }
  .library-controls-body {
    padding: 12px;
  }
  .ai-settings,
  .ai-settings label {
    display: grid;
    grid-template-columns: 1fr;
  }
  .ai-settings-note {
    text-align: left;
  }
  .toolbar { grid-template-columns: 1fr; padding: 12px; }
  .book-list { gap: 9px; }
  .book-card {
    grid-template-rows: minmax(84px, auto) auto;
    min-height: 138px;
    border-radius: 3px;
  }
  .book-card-link {
    display: grid;
    grid-template-areas: "title" "meta";
    grid-template-columns: 1fr;
    min-height: 88px;
    gap: 7px;
    padding: 13px 14px 2px;
  }
  .book-card-title-stack {
    gap: 4px;
  }
  .book-card-title {
    font-size: 1.18rem;
    line-height: 1.12;
  }
  .book-card-author {
    font-size: 0.88rem;
  }
  .book-card-side {
    max-width: none;
    text-align: right;
    font-size: 0.82rem;
  }
  .book-card-action-row {
    align-items: stretch;
    flex-direction: row;
    justify-content: flex-end;
    min-height: 44px;
    margin: 0 14px;
    padding: 9px 0 10px;
  }
  .actions { width: auto; }
  .action-control {
    justify-content: flex-end;
  }
  .downloaded-actions {
    display: flex;
    width: auto;
  }
  .book-card-action-row button {
    min-height: 32px;
    padding-inline: 8px;
    font-size: 0.8rem;
  }
}
