@import url("design-tokens.css");

/* ==========================================================================
   Maktaba Design System v2
   --------------------------------------------------------------------------
   Built on docs/design-system.md. Consumes only semantic tokens.

   Layout contract:
     - Logical properties only (margin-inline-start, inset-inline-end, …) so
       the whole sheet mirrors for RTL with no per-direction overrides.
     - Control height comes from --control-h, not ad-hoc padding, so the top
       bar stays --bar-h tall regardless of which control sits in it.
     - Every interactive element clears --tap-min on its smallest axis.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Reset + base
   -------------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-color: var(--gray-300) transparent;
  /* The off-canvas sidebar is parked off-screen with a transform. In RTL that
     parks it past the right edge, which would otherwise widen the document and
     produce a horizontal scrollbar on every narrow screen.
     This has to live on `html`, not `body`: the drawer is position:fixed, so
     its containing block is the viewport and body cannot clip it. Overflow set
     on the root propagates to the viewport, which does. `clip` (unlike `hidden`)
     creates no scroll container, so position:sticky keeps working. */
  overflow-x: clip;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--surface-1);
  color: var(--ink-1);
  font-family: var(--font);
  font-size: var(--fs-14);
  line-height: var(--lh-normal);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

[lang="ar"] body { line-height: var(--lh-loose); }

h1, h2, h3, h4 { margin: 0; font-weight: 650; line-height: var(--lh-tight); letter-spacing: -0.01em; }
p { margin: 0; }
img { max-width: 100%; }

/* One focus treatment everywhere. The ring colour is a token, and the halo
   keeps it legible when the focused element sits on a tinted surface. */
:focus-visible {
  outline: var(--focus-w) solid var(--focus-ring);
  outline-offset: var(--focus-offset);
  border-radius: var(--radius-xs);
}
:focus:not(:focus-visible) { outline: none; }

::selection { background: var(--brand-bg-2); color: var(--ink-1); }

.ic { flex-shrink: 0; vertical-align: -0.15em; }
[dir="rtl"] .flip-rtl { transform: scaleX(-1); }
[dir="ltr"] .flip-ltr { transform: scaleX(-1); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}

.skip {
  position: fixed;
  inset-inline-start: var(--space-3);
  top: calc(-1 * var(--space-12));
  z-index: var(--z-skip);
  display: inline-flex;
  align-items: center;
  min-height: var(--control-h-lg);
  padding: 0 var(--space-4);
  background: var(--brand-solid);
  color: var(--brand-on);
  border-radius: var(--radius-md);
  text-decoration: none;
  font-weight: 600;
  transition: top var(--dur) var(--ease);
}
.skip:focus { top: var(--space-3); }

/* --------------------------------------------------------------------------
   2. Tooltips — CSS only, driven by [data-tip]
      Uses inset-inline-start so it stays centred in both directions.
   -------------------------------------------------------------------------- */
[data-tip] { position: relative; }
[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  inset-inline-start: 50%;
  top: calc(100% + 6px);
  transform: translate(-50%, -4px);
  z-index: var(--z-menu);
  padding: 5px 9px;
  border-radius: var(--radius-sm);
  background: var(--gray-900);
  color: var(--gray-25);
  font-size: var(--fs-12);
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease), visibility var(--dur-fast);
  box-shadow: var(--shadow-md);
}
[data-theme="dark"] [data-tip]::after { background: var(--gray-200); color: var(--gray-900); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) [data-tip]::after { background: var(--gray-200); color: var(--gray-900); }
}
[data-tip]:hover::after,
[data-tip]:focus-visible::after {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
  transition-delay: 180ms;
}
/* Flip above when the trigger hugs the bottom of the viewport. */
[data-tip-below]::after {
  top: auto;
  bottom: calc(100% + 6px);
  transform: translate(-50%, 4px);
}
[data-tip-below]:hover::after,
[data-tip-below]:focus-visible::after { transform: translate(-50%, 0); }

/* --------------------------------------------------------------------------
   3. Buttons + icon buttons
   -------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-h);
  padding: 0 var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  color: var(--ink-1);
  font: inherit;
  font-size: var(--fs-13);
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.btn:hover { background: var(--surface-2); border-color: var(--input-border-hover); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; pointer-events: none; }

.btn.primary {
  background: var(--brand-solid);
  border-color: var(--brand-solid);
  color: var(--brand-on);
}
.btn.primary:hover { background: var(--brand-solid-hover); border-color: var(--brand-solid-hover); }

.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--ink-1); }

.btn.danger { background: var(--danger-solid); border-color: var(--danger-solid); color: #fff; }
.btn.danger:hover { background: var(--danger); border-color: var(--danger); }
.btn.danger.ghost { background: transparent; color: var(--danger); border-color: var(--danger-border); }
.btn.danger.ghost:hover { background: var(--danger-bg); }

/* Square, icon-only control. */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: var(--control-h);
  block-size: var(--control-h);
  min-inline-size: var(--tap-min);
  min-block-size: var(--tap-min);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink-1); }
.icon-btn[aria-pressed="true"],
.icon-btn.active,
.icon-btn.is-active,
.icon-btn[aria-current="true"] { background: var(--brand-bg); color: var(--brand-ink); }
.icon-btn.solid { border-color: var(--line-1); background: var(--surface-0); }

/* --------------------------------------------------------------------------
   4. Top bar — compact
      Bar is --bar-h. Brand, search and nav controls are all --control-h, so
      the leftover (bar-h - control-h) / 2 reads as padding rather than as a
      bar that grew to fit its contents. Search is centred with auto margins.
   -------------------------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-bar);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  block-size: var(--bar-h);
  padding-inline: var(--space-4);
  background: color-mix(in srgb, var(--surface-0) 88%, transparent);
  border-bottom: 1px solid var(--line-2);
}
@supports (backdrop-filter: blur(12px)) {
  .topbar { backdrop-filter: blur(12px) saturate(1.4); }
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  block-size: var(--control-h);
  /* Never shrink: the wordmark is nowrap, so squeezing the box pushes the text
     out of it (and in RTL that overflow goes past the viewport edge). The
     search form is the element that gives up space instead. */
  flex-shrink: 0;
  padding-inline: var(--space-1);
  color: var(--ink-1);
  font-size: var(--fs-14);
  font-weight: 700;
  letter-spacing: -0.01em;
  text-decoration: none;
  white-space: nowrap;
  border-radius: var(--radius-md);
  transition: opacity var(--dur-fast) var(--ease);
}
.brand:hover { opacity: 0.75; }
.brand .ic { color: var(--brand-ink); }

/* --- Search: at the inline-end edge (left in RTL), grows into available space --- */
.search {
  position: relative;
  display: flex;
  align-items: center;
  /* min-inline-size:0 lets the form actually shrink below its content width,
     which is what makes the bar hold together on narrow screens. */
  flex: 0 1 auto;
  min-inline-size: 0;
  max-inline-size: 520px;
  margin-inline-start: auto;
}
.search .lead-ic {
  position: absolute;
  inset-inline-start: 10px;
  display: inline-flex;
  color: var(--ink-3);
  pointer-events: none;
}
.search input[type="search"],
.search input:not([type]) {
  inline-size: 100%;
  min-inline-size: 0;
  block-size: var(--control-h);
  padding-inline: 34px 40px;
  border: 1px solid var(--input-border);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--input-ink);
  font: inherit;
  font-size: var(--fs-13);
  appearance: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.search input:focus { background: var(--surface-0); border-color: var(--brand-border); }
.search input::placeholder { color: var(--input-placeholder); }
.search input::-webkit-search-cancel-button { appearance: none; }

.search .kbd-hint {
  position: absolute;
  inset-inline-end: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 20px;
  block-size: 20px;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-xs);
  background: var(--surface-0);
  color: var(--ink-3);
  font-family: var(--font-mono);
  font-size: var(--fs-11);
  pointer-events: none;
}
.search:focus-within .kbd-hint { opacity: 0; }

/* Clear-search affordance, revealed only when there is a query. */
.search .clear {
  position: absolute;
  inset-inline-end: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 26px;
  block-size: 26px;
  border-radius: 50%;
  color: var(--ink-3);
  text-decoration: none;
  background: var(--surface-3);
}
.search .clear:hover { background: var(--line-1); color: var(--ink-1); }
.search.has-query .kbd-hint { display: none; }

/* --- Nav cluster --- */
.topnav {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.topnav .more { position: relative; }
.topnav .more > summary {
  list-style: none;
  cursor: pointer;
}
.topnav .more > summary::-webkit-details-marker { display: none; }
.topnav .more > summary::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
}
.topnav .more[open] > summary { background: var(--surface-2); color: var(--ink-1); }

.menu {
  position: absolute;
  inset-inline-end: 0;
  top: calc(100% + 6px);
  z-index: var(--z-menu);
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-inline-size: 216px;
  padding: var(--space-2);
  background: var(--surface-0);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}
.menu a {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-block-size: var(--control-h);
  padding-inline: var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--ink-1);
  font-size: var(--fs-13);
  font-weight: 500;
  text-decoration: none;
}
.menu a:hover { background: var(--surface-2); }
.menu a .ic { color: var(--ink-3); }
.menu hr {
  margin: var(--space-2) 0;
  border: 0;
  border-top: 1px solid var(--line-2);
}
.menu .menu-label {
  padding: var(--space-1) var(--space-3) var(--space-2);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.menu-form { display: flex; }
.menu-form button {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  inline-size: 100%;
  min-block-size: var(--control-h);
  padding-inline: var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-1);
  font: inherit;
  font-size: var(--fs-13);
  font-weight: 500;
  text-align: start;
  cursor: pointer;
}
.menu-form button:hover { background: var(--surface-2); }
.menu-form button .ic { color: var(--ink-3); }

/* --------------------------------------------------------------------------
   4a. Theme toggle
       All three icons are rendered server-side; CSS shows the one matching
       the active theme. This keeps the control correct before first paint
       (the head script resolves the theme before the stylesheet applies) and
       removes the JS icon-swap entirely.
   -------------------------------------------------------------------------- */
.theme-toggle .ti { display: none; }
[data-theme="light"] .theme-toggle .ti-light { display: inline-flex; }
[data-theme="dark"] .theme-toggle .ti-dark { display: inline-flex; }
:root:not([data-theme]) .theme-toggle .ti-system { display: inline-flex; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .theme-toggle .ti-system { display: none; }
  :root:not([data-theme]) .theme-toggle .ti-dark { display: inline-flex; }
}

/* --------------------------------------------------------------------------
   5. Page shell
   -------------------------------------------------------------------------- */
.layout {
  display: flex;
  flex: 1;
  inline-size: 100%;
  max-inline-size: 1560px;
  margin-inline: auto;
  padding-inline: var(--space-4);
}

main {
  flex: 1;
  min-inline-size: 0;
  padding: var(--space-5) var(--space-4) var(--space-10);
}

/* Sidebar — drawer by default, pinned (docked) on wide screens
   ------------------------------------------------------------------
   Default: off-canvas overlay drawer (fixed, out of layout flow), so
   the grid gets the full .layout width. The toolbar filter button
   toggles it (.layout.sidebar-open). Pinning adds .sidebar-pinned:
   on wide screens the panel docks in-flow (sticky, 236px) and the
   grid narrows; closing (unpin) returns to the full-width drawer
   state and the grid expands automatically. */
.sidebar {
  position: fixed;
  inset-block: var(--bar-h) 0;
  inset-inline-start: 0;
  z-index: var(--z-drawer);
  inline-size: min(300px, 84vw);
  max-block-size: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-4) var(--space-3) var(--space-5);
  background: var(--surface-0);
  box-shadow: var(--shadow-lg);
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform var(--dur) var(--ease), visibility var(--dur) var(--ease);
  scrollbar-width: thin;
  scrollbar-color: var(--gray-300) transparent;
}
[dir="rtl"] .sidebar { transform: translateX(100%); }
.layout.sidebar-open .sidebar { transform: none; visibility: visible; }

/* Scrim behind the open overlay drawer (click to close). */
.sidebar-scrim {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-drawer) - 1);
  padding: 0;
  border: 0;
  background: rgb(0 0 0 / 0.4);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease);
}
.layout.sidebar-open .sidebar-scrim {
  opacity: 1;
  visibility: visible;
}

/* Pinned: docked in the layout flow on wide screens only. */
@media (min-width: 1025px) {
  .layout.sidebar-pinned .sidebar {
    position: sticky;
    inset-block: var(--bar-h) auto;
    inset-inline-start: auto;
    top: var(--bar-h);
    align-self: flex-start;
    flex-shrink: 0;
    inline-size: 236px;
    max-block-size: calc(100vh - var(--bar-h));
    padding: var(--space-5) var(--space-3) var(--space-5) 0;
    background: transparent;
    border-inline-end: 1px solid var(--line-2);
    box-shadow: none;
    transform: none;
    visibility: visible;
  }
  .layout.sidebar-pinned .sidebar-scrim { opacity: 0; visibility: hidden; pointer-events: none; }
}
/* Pinning has no meaning on narrow screens — always a drawer there. */
@media (max-width: 1024px) {
  .sidebar-pin { display: none; }
}

.sidebar::-webkit-scrollbar { inline-size: 6px; block-size: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }
.sidebar::-webkit-scrollbar-thumb:hover { background: var(--gray-400); }
.sidebar::-webkit-scrollbar-track { background: transparent; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-block-end: var(--space-3);
}
.sidebar h2 {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  min-inline-size: 0;
}
.sidebar-actions {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.sidebar-close { inline-size: 32px; block-size: 32px; min-inline-size: 32px; }
.sidebar-pin {
  min-block-size: 28px;
  padding-inline: var(--space-3);
  border-radius: var(--radius-pill);
  font-size: var(--fs-12);
  font-weight: 700;
}
.sidebar-pin[aria-pressed="true"] {
  border-color: var(--brand-border);
  background: var(--brand-bg);
  color: var(--brand-ink);
}

.group-filter {
  inline-size: 100%;
  block-size: var(--control-h);
  margin-block-end: var(--space-2);
  padding-inline: var(--space-3);
  border: 1px solid var(--input-border);
  border-radius: var(--radius-md);
  background: var(--input-bg);
  color: var(--input-ink);
  font: inherit;
  font-size: var(--fs-13);
}
.group-filter::placeholder { color: var(--input-placeholder); }

.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar li a {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-block-size: var(--control-h);
  padding-inline: var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--ink-2);
  font-size: var(--fs-13);
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.sidebar li a > span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar li a:hover { background: var(--surface-2); color: var(--ink-1); }
.sidebar li a.active {
  background: var(--brand-bg);
  color: var(--brand-ink);
  font-weight: 600;
}
.sidebar li a i { font-style: italic; opacity: 0.7; }
.sidebar .count-pill { margin-inline-start: auto; }

.count-pill {
  flex-shrink: 0;
  padding: 1px 7px;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
}
.sidebar li a.active .count-pill { background: var(--brand-bg-2); color: var(--brand-ink); }

/* --------------------------------------------------------------------------
   6. Toolbar — one row, wraps cleanly
   -------------------------------------------------------------------------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  margin-block-end: var(--space-4);
  min-block-size: var(--control-h);
}

/* Start-edge group (sidebar side): the double-chevron drawer toggle sits
   immediately before the book count, so it reads as belonging to the
   sidebar at the inline-start edge instead of the far controls cluster. */
.toolbar-start {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-3);
  min-inline-size: 0;
}

.chips { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-2); }
.chips > .count { color: var(--ink-2); font-size: var(--fs-13); }
.chips > .count b { color: var(--ink-1); font-variant-numeric: tabular-nums; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 3px 3px 3px var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-pill);
  background: var(--surface-0);
  color: var(--ink-2);
  font-size: var(--fs-12);
}
[dir="rtl"] .chip { padding: 3px var(--space-3) 3px 3px; }
.chip b { color: var(--ink-1); font-weight: 600; }
.chip .ic { color: var(--ink-3); }
.chip a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 22px;
  block-size: 22px;
  border-radius: 50%;
  color: var(--ink-3);
}
.chip a:hover { background: var(--surface-2); color: var(--ink-1); }

/* The controls sit above the grid they act on. The extra inline-end padding
   keeps the icon cluster from hugging the page edge in either direction. */
.toolbar-controls {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding-inline-end: var(--space-2);
  flex-wrap: wrap;
}
.toolbar-controls form { display: flex; align-items: center; }

/* Mobile: collapse toolbar controls into a single row with a "more" button */
@media (max-width: 768px) {
  .toolbar {
    gap: var(--space-2);
    margin-block-end: var(--space-3);
  }
  .toolbar-controls {
    gap: var(--space-1);
  }
  .toolbar-controls .view-seg { display: none; }
  .toolbar-controls .col-toggle { display: none; }
  .toolbar-controls .page-filter { display: none; }
  .toolbar-controls form select.select { min-inline-size: 100px; }
  .toolbar-controls form select.select { min-inline-size: 80px; }
  /* Show a compact "more" button for hidden controls on mobile */
  .toolbar-controls .mobile-more {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 0 4px;
  }
  .toolbar-controls .mobile-more .menu {
    min-inline-size: 180px;
  }
  .toolbar-controls .mobile-more .menu a {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 8px 12px;
  }
}
@media (min-width: 769px) {
  .toolbar-controls .mobile-more { display: none; }
}

.view-seg {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
}
.view-seg .icon-btn {
  inline-size: 32px;
  block-size: 32px;
  border-color: transparent;
  background: transparent;
}
.view-seg .icon-btn:hover { background: var(--surface-2); }
.view-seg .icon-btn.is-active {
  border-color: var(--brand-border);
  background: var(--brand-bg);
  color: var(--brand-ink);
}

.select {
  block-size: var(--control-h);
  min-inline-size: 132px;
  padding: 0 var(--space-2) 0 var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background-color: var(--surface-0);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath fill='%237a8593' d='M1.4 1.7 6 6.3l4.6-4.6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  background-size: 10px;
  color: var(--ink-1);
  font: inherit;
  font-size: var(--fs-13);
  font-weight: 500;
  cursor: pointer;
  appearance: none;
}
[dir="rtl"] .select { padding: 0 var(--space-3) 0 var(--space-2); background-position: left var(--space-3) center; }
.select:hover { border-color: var(--input-border-hover); }

/* Always visible: the groups drawer toggle (double chevrons pointing
   toward the sidebar at the inline-start edge). */
.filter-toggle { display: inline-flex; flex-shrink: 0; }
.filter-toggle[aria-expanded="true"] { background: var(--brand-bg); color: var(--brand-ink); border-color: var(--brand-border); }

/* --------------------------------------------------------------------------
   7. Book grid
      Equal-height cards: grid rows stretch by default, and the meta block
      below the cover has a fixed track height so one long title cannot push
      a single card taller than its neighbours.
   -------------------------------------------------------------------------- */
/* Cards-per-row (4–8) is a user preference rendered as --cards on the grid.
   auto-fill + an exact 1/N min width gives N columns when they fit, then
   gracefully drops columns instead of shrinking cards below the 120px floor. */
.grid {
  display: grid;
  --cards: 6;
  grid-template-columns: repeat(auto-fill,
    minmax(max(120px, calc((100% - (var(--cards) - 1) * var(--space-3)) / var(--cards))), 1fr));
  gap: var(--space-3);
}
/* Phones: a fixed 3-up shelf, 2-up only on very narrow screens.
   The count is declared on grid-template-columns rather than by overriding
   --cards: index.php writes --cards as an inline style, and an inline
   declaration outranks every stylesheet rule — a media-query override of the
   variable would be dead code (the wide-screen preference would leak through).
   The tracks are 1fr (no 120px floor): 3 × 120px + gaps needs ~376px, so a
   floor would force overflow or silently drop to 2 columns on 360px phones —
   the single most common mobile width. Shrinking the tracks instead keeps the
   3-up shelf on every phone and never scrolls sideways.
   340px, not 360px, is the 2-up boundary for the same reason. */
@media (max-width: 600px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--space-2);
  }
}
@media (max-width: 340px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-2);
  }
}

.card {
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--surface-0);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  color: var(--ink-1);
  text-decoration: none;
  overflow: hidden;
  transition: box-shadow var(--dur) var(--ease), transform var(--dur) var(--ease),
              border-color var(--dur) var(--ease);
}
.card:hover {
  transform: translateY(-3px);
  border-color: var(--line-1);
  box-shadow: var(--shadow-md);
}
.card:focus-visible { outline-offset: -2px; }

/* --- Card links + quick actions -------------------------------------------
   The card is an <article> so the cover/title links and the action row can
   coexist without nesting interactive elements inside one another. */
.card-cover { display: block; color: inherit; text-decoration: none; }
.card-title { color: inherit; text-decoration: none; }
.card-title:hover { color: var(--brand-ink); }

/* Favourite stays over the artwork, reachable without opening the book.
   The extra `.card` qualifier is required: the generic .fav-toggle rule below
   would otherwise reset the size/background/border. */
.card .card-fav {
  position: absolute;
  inset-block-start: var(--space-2);
  inset-inline-end: var(--space-2);
  z-index: 3;
  inline-size: 30px;
  block-size: 30px;
  min-inline-size: 30px;
  border: 1px solid var(--line-1);
  border-radius: 50%;
  background: var(--surface-0);
  box-shadow: var(--shadow-sm);
}
.card .card-fav:hover { background: var(--surface-0); transform: scale(1.06); }

.card-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-block-start: auto;
  padding-block-start: var(--space-2);
  border-block-start: 1px solid var(--line-2);
}
.card-act {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 28px;
  block-size: 28px;
  border-radius: var(--radius-sm);
  color: var(--ink-3);
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.card-act:hover { background: var(--surface-2); color: var(--brand-ink); }
.card-act:focus-visible { outline-offset: 0; }

/* --- Cover -----------------------------------------------------------------
   object-fit:cover fills the 2:3 frame completely for every input ratio, so a
   cover never renders as a letterboxed strip with empty bands. The skeleton
   lives *behind* the image (z-index 0) and fades once the bytes arrive. */
.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  overflow: hidden;
  background: var(--cover-bg);
  border-end: 1px solid var(--line-2);
}
.cover::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background: linear-gradient(
    100deg,
    var(--surface-2) 20%,
    var(--surface-3) 42%,
    var(--surface-2) 64%
  );
  background-size: 300% 100%;
  animation: shimmer 1.5s linear infinite;
  opacity: 1;
  transition: opacity var(--dur-slow) var(--ease);
}
.cover.is-loaded::before,
.cover.has-no-image::before { opacity: 0; animation: none; }
@keyframes shimmer {
  from { background-position: 150% 0; }
  to   { background-position: -150% 0; }
}

.cover img {
  position: relative;
  z-index: 2;
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity var(--dur) var(--ease);
}
.cover.is-loaded img { opacity: 1; }
/* Once the real art is on screen the placeholder is dead weight — drop it
   rather than relying on paint order alone. */
.cover.is-loaded .nocover { display: none; }

/* Fallback plate for books with no cover at all. */
.nocover {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3);
  background: var(--brand-bg);
  color: var(--brand-ink);
  font-size: var(--fs-24);
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  overflow: hidden;
  word-break: break-word;
}

/* --- Card meta -------------------------------------------------------------
   Fixed track heights keep every card in a row the same size. */
.card-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1;
  padding: var(--space-2) var(--space-3) var(--space-3);
}
.card-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  /* Two lines plus slack. Arabic titles routinely carry stacked diacritics
     whose glyphs overhang the line box and get sliced by the clamp. */
  min-block-size: 3.2em;
  color: var(--ink-1);
  font-size: var(--fs-13);
  font-weight: 600;
  line-height: 1.55;
}
.card-author,
.card-meta {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-block-size: 1.4em;
  color: var(--ink-3);
  font-size: var(--fs-12);
  line-height: 1.4;
}

/* --------------------------------------------------------------------------
   8. Favourite star — outline ⇄ filled
      Two distinct icon files, swapped by CSS on aria-pressed. A single
      outline <path> cannot become solid by setting `fill` on its parent.
   -------------------------------------------------------------------------- */
.favstar { color: var(--brand-ink); display: inline-flex; vertical-align: -0.18em; }
.fav-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: var(--tap-min);
  block-size: var(--tap-min);
  min-inline-size: var(--tap-min);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--ink-3);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.fav-toggle:hover { background: var(--surface-2); color: var(--ink-1); }
.fav-toggle[aria-pressed="true"] { color: var(--brand-ink); }
.fav-toggle .ic-outline { display: inline-flex; }
.fav-toggle .ic-fill { display: none; }
.fav-toggle[aria-pressed="true"] .ic-outline { display: none; }
.fav-toggle[aria-pressed="true"] .ic-fill { display: inline-flex; }
/* Pending state while the AJAX call is in flight. */
.fav-toggle[data-pending="1"] { opacity: 0.55; pointer-events: none; }
/* Icon-only by default. When a text label is present (book detail page) the
   button widens into a regular pill so the action has a name. */
.fav-toggle:has(.fav-lbl) {
  inline-size: auto;
  gap: var(--space-2);
  padding-inline: var(--space-3);
  border-color: var(--line-1);
  background: var(--surface-0);
  color: var(--ink-1);
  font-size: var(--fs-13);
  font-weight: 600;
}
.fav-toggle:has(.fav-lbl):hover { background: var(--surface-2); }
.fav-toggle:has(.fav-lbl)[aria-pressed="true"] { color: var(--brand-ink); border-color: var(--brand-border); }

/* --------------------------------------------------------------------------
   9. Table view
   -------------------------------------------------------------------------- */
.tbl-wrap {
  overflow-x: auto;
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  /* Edge shadows that appear only when the table actually overflows. */
  background-image:
    linear-gradient(to right, var(--surface-0) 30%, rgb(0 0 0 / 0)),
    linear-gradient(to left, var(--surface-0) 30%, rgb(0 0 0 / 0)),
    linear-gradient(to right, rgb(0 0 0 / 0.12), rgb(0 0 0 / 0)),
    linear-gradient(to left, rgb(0 0 0 / 0.12), rgb(0 0 0 / 0));
  background-repeat: no-repeat;
  background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
  background-position: left center, right center, left center, right center;
  background-attachment: local, local, scroll, scroll;
}
[dir="rtl"] .tbl-wrap {
  background-image:
    linear-gradient(to left, var(--surface-0) 30%, rgb(0 0 0 / 0)),
    linear-gradient(to right, var(--surface-0) 30%, rgb(0 0 0 / 0)),
    linear-gradient(to left, rgb(0 0 0 / 0.12), rgb(0 0 0 / 0)),
    linear-gradient(to right, rgb(0 0 0 / 0.12), rgb(0 0 0 / 0));
}

.tbl, .booktable {
  inline-size: 100%;
  border-collapse: collapse;
  background: var(--surface-0);
  font-size: var(--fs-13);
}
.tbl th, .tbl td, .booktable th, .booktable td {
  padding: var(--space-2) var(--space-3);
  border-block-end: 1px solid var(--line-2);
  text-align: start;
  vertical-align: middle;
}
.tbl thead th, .booktable thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-1);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}
.tbl thead th a, .booktable thead th a {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: inherit;
  text-decoration: none;
}
.tbl thead th a:hover { color: var(--ink-1); }
.tbl tbody tr:hover, .booktable tbody tr:hover { background: var(--surface-1); }
/* Table links read as text, not as link chrome. */
.tbl td a, .booktable td a { color: var(--ink-1); text-decoration: none; }
.tbl td a:hover, .booktable td a:hover { color: var(--brand-ink); text-decoration: underline; }

.booktable .thumb { inline-size: 52px; padding-inline-end: 0; }
.booktable .thumb img {
  inline-size: 34px;
  block-size: 51px;
  object-fit: cover;
  border-radius: var(--radius-xs);
  border: 1px solid var(--line-2);
  display: block;
}
.booktable .title-cell { max-inline-size: 420px; }
.row-actions { display: flex; gap: var(--space-1); align-items: center; }
.row-actions .btn { min-block-size: 32px; padding-inline: var(--space-2); font-size: var(--fs-12); }

/* --- Bulk selection column --- */
.booktable .bulk-col { inline-size: 40px; text-align: center; }
.booktable .bulk-col input[type="checkbox"] { transform: scale(1.1); }
.booktable tbody tr.selected { background: var(--brand-bg-2) !important; }
.booktable thead th input[type="checkbox"] { transform: scale(1.1); }

/* Bulk action bar */
.bulk-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-block: var(--space-3) var(--space-4);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--brand-border);
  border-radius: var(--radius-md);
  background: var(--brand-bg-2);
  color: var(--brand-ink);
}
.bulk-count { font-weight: 600; font-size: var(--fs-13); }
.bulk-actions { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-2); }
.bulk-group { display: flex; align-items: center; gap: var(--space-2); }
.bulk-group .select { min-inline-size: 160px; }
.bulk-bar .btn { min-block-size: 32px; font-size: var(--fs-12); }
.bulk-bar .btn.danger { background: var(--danger-solid); border-color: var(--danger-solid); color: #fff; }
.bulk-bar .btn.danger:hover { background: var(--danger); border-color: var(--danger); }

/* --------------------------------------------------------------------------
   9b. Toast notifications
   -------------------------------------------------------------------------- */
#maktaba-toasts {
  position: fixed;
  inset-inline-end: var(--space-4);
  bottom: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
}
@media (max-width: 480px) {
  #maktaba-toasts {
    inset-inline: var(--space-3);
    inset-inline-end: var(--space-3);
    bottom: var(--space-3);
    align-items: stretch;
  }
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  border: 1px solid var(--line-1);
  box-shadow: var(--shadow-lg);
  color: var(--ink-1);
  font-size: var(--fs-13);
  line-height: 1.4;
  min-inline-size: 240px;
  max-inline-size: 360px;
  pointer-events: auto;
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.toast.show { opacity: 1; transform: translateY(0) scale(1); }
.toast.hiding { opacity: 0; transform: translateY(-8px) scale(0.96); }
.toast-icon { flex-shrink: 0; color: var(--ink-3); margin-block-start: 1px; }
.toast-text { flex: 1; min-inline-size: 0; }
.toast-close {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 24px;
  block-size: 24px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-3);
  cursor: pointer;
}
.toast-close:hover { background: var(--surface-2); color: var(--ink-1); }

/* Type variants */
.toast-success { border-color: var(--success-border); }
.toast-success .toast-icon { color: var(--success); }
.toast-error { border-color: var(--danger-border); }
.toast-error .toast-icon { color: var(--danger); }
.toast-warning { border-color: var(--warning-border); }
.toast-warning .toast-icon { color: var(--warning); }
.toast-info { border-color: var(--brand-border); }
.toast-info .toast-icon { color: var(--brand-ink); }

/* --------------------------------------------------------------------------
   10. Pager
   -------------------------------------------------------------------------- */
.pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  margin-block-start: var(--space-8);
}
.pager a, .pager .cur {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  min-inline-size: var(--control-h);
  min-block-size: var(--control-h);
  padding-inline: var(--space-2);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  color: var(--ink-2);
  font-size: var(--fs-13);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-decoration: none;
}
.pager a:hover { border-color: var(--brand-border); color: var(--brand-ink); background: var(--brand-bg); }
.pager .cur { background: var(--brand-solid); border-color: var(--brand-solid); color: var(--brand-on); }
.pager .disabled { opacity: 0.4; pointer-events: none; }
.pager .gap { border-color: transparent; color: var(--ink-3); }

/* --------------------------------------------------------------------------
   11. Empty state
   -------------------------------------------------------------------------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-12) var(--space-4);
  color: var(--ink-3);
  text-align: center;
}
.empty .ic { color: var(--gray-300); }
.empty h2 { color: var(--ink-1); font-size: var(--fs-18); }
.empty p { font-size: var(--fs-13); }

/* --------------------------------------------------------------------------
   12. Book detail
   -------------------------------------------------------------------------- */
.detail {
  display: flex;
  gap: var(--space-8);
  inline-size: 100%;
  max-inline-size: 1080px;
  margin-inline: auto;
  padding: var(--space-6) var(--space-4) var(--space-10);
}
.dmeta { flex: 1; min-inline-size: 0; }
/* The book cover reuses .cover-preview (same stacking contract as the grid
   cards), so it needs only sizing here — no second copy of the layering rules. */
.dcover { flex-shrink: 0; }
.dcover .cover-preview {
  inline-size: 220px;
  box-shadow: var(--shadow-lg);
}

.crumbs {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-block-end: var(--space-2);
  color: var(--ink-3);
  font-size: var(--fs-12);
}
.crumbs a { color: var(--brand-ink); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.dmeta h1 { margin-block-end: var(--space-1); font-size: var(--fs-24); }
.dauthor { color: var(--ink-2); font-size: var(--fs-16); }

.badges { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-block: var(--space-3); }
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px var(--space-2);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-sm);
  background: var(--surface-0);
  color: var(--ink-2);
  font-size: var(--fs-12);
  font-weight: 600;
}
.badge.brand { border-color: var(--brand-border); background: var(--brand-bg); color: var(--brand-ink); }

.facts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-3) var(--space-5);
  margin-block: var(--space-5);
  padding-block: var(--space-4);
  border-block: 1px solid var(--line-2);
  font-size: var(--fs-13);
}
.facts dt { color: var(--ink-3); font-size: var(--fs-11); text-transform: uppercase; letter-spacing: 0.04em; }
.facts dd { margin: 2px 0 0; color: var(--ink-1); font-weight: 600; }

.actions { display: flex; flex-direction: column; gap: var(--space-3); }
.actions .row { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.excerpt {
  padding: var(--space-4);
  border-inline-start: 3px solid var(--brand-border);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  background: var(--surface-0);
  color: var(--ink-2);
  font-size: var(--fs-13);
  line-height: var(--lh-loose);
}
[dir="rtl"] .excerpt { border-radius: var(--radius-md) 0 0 var(--radius-md); }
.excerpt p + p { margin-block-start: var(--space-3); }

/* --------------------------------------------------------------------------
   13. Forms
   -------------------------------------------------------------------------- */
.form-card {
  padding: var(--space-5);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  background: var(--surface-0);
}
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: var(--space-4); }
.form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 680px) { .form-grid.two { grid-template-columns: 1fr; } }
.form-card + .form-card,
.form-card + .form-section,
.form-section + .form-card { margin-block-start: var(--space-4); }
.form-title {
  margin-block-end: var(--space-4);
  padding-block-end: var(--space-2);
  border-block-end: 1px solid var(--line-2);
  color: var(--ink-1);
  font-size: var(--fs-14);
  font-weight: 650;
}
.field { display: flex; flex-direction: column; gap: 6px; min-inline-size: 0; }
.field.full { grid-column: 1 / -1; }
.field > label, .field > .label, .field > span:first-child {
  color: var(--ink-2);
  font-size: var(--fs-12);
  font-weight: 600;
}
.field .req { color: var(--danger); }
.field .hint { color: var(--ink-3); font-size: var(--fs-12); }
.field .err { display: flex; align-items: center; gap: 5px; color: var(--danger); font-size: var(--fs-12); }

/* `input:not([type])` is the important selector: most text fields in this app
   omit type="text", so matching only [type="text"] left them with raw UA
   styling (20px tall, square corners) beside fully styled selects. */
.input, .text, input:not([type]), input[type="text"], input[type="search"],
input[type="number"], input[type="password"], input[type="email"], input[type="url"],
input[type="tel"], input[type="date"], input[type="month"], input[type="datetime-local"],
select, textarea {
  inline-size: 100%;
  min-block-size: var(--control-h-lg);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--input-border);
  border-radius: var(--radius-md);
  background-color: var(--input-bg);
  color: var(--input-ink);
  font: inherit;
  font-size: var(--fs-14);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
textarea { min-block-size: 168px; line-height: var(--lh-normal); resize: vertical; }
select {
  appearance: none;
  padding-inline-end: var(--space-8);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath fill='%237a8593' d='M1.4 1.7 6 6.3l4.6-4.6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--space-3) center;
  background-size: 10px;
  cursor: pointer;
}
[dir="rtl"] select { background-position: left var(--space-3) center; }
input[type="date"], input[type="month"], input[type="datetime-local"] { font-variant-numeric: tabular-nums; }
input[type="date"]::-webkit-calendar-picker-indicator { opacity: 0.6; cursor: pointer; }
[data-theme="dark"] input[type="date"]::-webkit-calendar-picker-indicator,
[data-theme="dark"] input[type="month"]::-webkit-calendar-picker-indicator { filter: invert(0.8); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(0.8); }
}
input[type="date"]:invalid { color: var(--input-placeholder); }
.input:hover, input:hover, select:hover, textarea:hover { border-color: var(--input-border-hover); }
.input:focus, input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand-solid);
  box-shadow: 0 0 0 3px var(--focus-halo);
}
.input::placeholder, input::placeholder, textarea::placeholder { color: var(--input-placeholder); }
.input:disabled, input:disabled, select:disabled, textarea:disabled {
  background: var(--input-disabled-bg);
  color: var(--input-disabled-ink);
  cursor: not-allowed;
}
.field.invalid input, .field.invalid textarea, .field.invalid select { border-color: var(--danger); }

.charcount { color: var(--ink-3); font-size: var(--fs-11); font-variant-numeric: tabular-nums; text-align: end; }
.actions-bar { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-block-start: var(--space-5); }

/* Inline search field with a leading icon. */
.search-row { display: flex; align-items: center; gap: var(--space-2); }
.search-row .input-wrap { flex: 1; min-inline-size: 0; }
.search-row .btn { flex-shrink: 0; }
@media (max-width: 520px) {
  .search-row { flex-wrap: wrap; }
  .search-row .btn { inline-size: 100%; justify-content: center; }
}
.search-form .input-wrap { position: relative; display: flex; align-items: center; }
.search-form .input-wrap .ic {
  position: absolute;
  inset-inline-start: var(--space-3);
  color: var(--ink-3);
  pointer-events: none;
}
.search-form .input-wrap input { padding-inline-start: 38px; }

.check-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-block-size: var(--control-h);
  color: var(--ink-2);
  font-size: var(--fs-13);
  cursor: pointer;
}
.check-row input[type="checkbox"] { inline-size: 18px; block-size: 18px; accent-color: var(--brand-solid); cursor: pointer; }

.form-section {
  margin-block-start: var(--space-5);
  padding: var(--space-4);
  border: 1px solid var(--brand-border);
  border-radius: var(--radius-md);
  background: var(--brand-bg);
}
.form-section h2 { margin-block-end: var(--space-3); color: var(--brand-ink); font-size: var(--fs-14); }

/* Cover editor -------------------------------------------------------------- */
.cover-field { display: flex; flex-wrap: wrap; gap: var(--space-5); align-items: flex-start; }
.cover-preview {
  position: relative;
  flex-shrink: 0;
  inline-size: 168px;
  aspect-ratio: 2 / 3;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--cover-bg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.cover-preview img, .cover-preview .nocover {
  position: absolute;
  inset: 0;
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
}
/* Same stacking contract as .cover: art above the initials plate. */
.cover-preview img { z-index: 2; }
.cover-preview .nocover { z-index: 1; font-size: var(--fs-20); }
.cover-preview.is-loaded .nocover { display: none; }

/* File input rendered as a button: the real input stays visually hidden but
   focusable, and the label is the pointer target. */
label.btn { cursor: pointer; }
label.btn:focus-within { outline: var(--focus-w) solid var(--focus-ring); outline-offset: var(--focus-offset); }
.cover-actions { display: flex; flex-direction: column; gap: var(--space-2); align-items: flex-start; flex: 1; min-inline-size: 200px; }
.cover-actions .btn { align-self: flex-start; }
.cover-status { color: var(--ink-3); font-size: var(--fs-12); min-block-size: 1.4em; }
.cover-status[data-state="error"] { color: var(--danger); }
.cover-status[data-state="ok"] { color: var(--success); }

/* Book editor: cover first, metadata beside it, one compact description card.
   Keeping the cover visible while editing shortens the page and makes the
   preview feedback obvious. */
.edit-layout {
  display: grid;
  grid-template-columns: 210px minmax(0, 1fr);
  gap: var(--space-4);
  align-items: start;
}
.edit-cover-card {
  position: sticky;
  top: calc(var(--bar-h) + var(--space-3));
}
.edit-fields {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-inline-size: 0;
}
.edit-fields .form-card + .form-card { margin-block-start: 0; }
.cover-field-stack { flex-direction: column; gap: var(--space-3); }
.cover-field-stack .cover-preview { inline-size: 100%; max-inline-size: 168px; }
.cover-field-stack .cover-actions { inline-size: 100%; min-inline-size: 0; }
.cover-field-stack .cover-actions .btn { align-self: stretch; justify-content: center; }
.desc-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  margin-block-end: var(--space-3);
}
.desc-head .form-title {
  margin-block-end: 2px;
  padding-block-end: 0;
  border-block-end: 0;
}
.desc-head .hint { color: var(--ink-3); font-size: var(--fs-12); }
.ai-generate { flex-shrink: 0; }
#aiDesc { min-block-size: 220px; }
.charcount[data-state="ok"] { color: var(--success); }
.charcount[data-state="over"] { color: var(--danger); }
@media (max-width: 860px) {
  .edit-layout { grid-template-columns: 1fr; }
  .edit-cover-card { position: static; }
  .cover-field-stack { flex-direction: row; }
  .cover-field-stack .cover-actions { inline-size: auto; flex: 1; }
  .cover-field-stack .cover-actions .btn { align-self: flex-start; }
}

/* Dropzone ------------------------------------------------------------------ */
.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-8) var(--space-4);
  border: 2px dashed var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  color: var(--ink-2);
  text-align: center;
  cursor: pointer;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.dropzone:hover, .dropzone.over { border-color: var(--brand-solid); background: var(--brand-bg); }
.dropzone:focus-visible { border-color: var(--brand-solid); }
.dropzone .dz-ic { color: var(--brand-ink); }
.dropzone p { font-size: var(--fs-13); }
.dropzone .hint { color: var(--ink-3); font-size: var(--fs-12); }

.upload-progress {
  block-size: 6px;
  margin-block-start: var(--space-4);
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  overflow: hidden;
}
.upload-progress span { display: block; block-size: 100%; inline-size: 0; background: var(--brand-solid); transition: inline-size var(--dur) var(--ease); }

/* --------------------------------------------------------------------------
   14. Notices
   -------------------------------------------------------------------------- */
.notice, .warn, .error {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  margin-block: var(--space-4);
  padding: var(--space-3) var(--space-4);
  border: 1px solid;
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
}
.notice { border-color: var(--success-border); background: var(--success-bg); color: var(--ink-1); }
.warn   { border-color: var(--warning-border); background: var(--warning-bg); color: var(--ink-1); }
.error  { border-color: var(--danger-border);  background: var(--danger-bg);  color: var(--ink-1); }

.danger-zone {
  margin-block-start: var(--space-6);
  padding: var(--space-4);
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-md);
  background: var(--danger-bg);
}
.danger-zone h2 { margin-block-end: var(--space-2); color: var(--danger); font-size: var(--fs-14); }

/* --------------------------------------------------------------------------
   15. Answer / QA surfaces
   -------------------------------------------------------------------------- */
.answer-card {
  margin-block: var(--space-4);
  padding: var(--space-5);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  color: var(--ink-1);
  font-size: var(--fs-14);
  line-height: var(--lh-loose);
}
.sources ol { display: flex; flex-direction: column; gap: var(--space-4); margin: 0; padding-inline-start: var(--space-6); }
.sources h2 { margin-block-end: var(--space-3); font-size: var(--fs-14); }
.sources blockquote {
  margin-block-start: var(--space-2);
  padding-inline-start: var(--space-3);
  border-inline-start: 3px solid var(--brand-border);
  color: var(--ink-2);
  font-size: var(--fs-13);
}

/* --------------------------------------------------------------------------
   16. Annotation swatches
   -------------------------------------------------------------------------- */
.swatch { display: inline-block; inline-size: 0.85em; block-size: 0.85em; border-radius: 3px; vertical-align: -1px; }
.swatch-yellow { background: var(--sw-yellow); }
.swatch-green  { background: var(--sw-green); }
.swatch-blue   { background: var(--sw-blue); }
.swatch-pink   { background: var(--sw-pink); }
.swatch-underline { border-block-end: 3px solid currentColor; }

/* --------------------------------------------------------------------------
   17. Footer + theme switch
   -------------------------------------------------------------------------- */
footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--space-3) var(--space-4);
  margin-block-start: auto;
  padding: var(--space-5) var(--space-4);
  border-block-start: 1px solid var(--line-2);
  color: var(--ink-3);
  font-size: var(--fs-12);
}
.theme-switch {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-pill);
  background: var(--surface-0);
}
.theme-switch button {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-block-size: 28px;
  padding-inline: var(--space-3);
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: var(--fs-11);
  font-weight: 600;
  cursor: pointer;
}
.theme-switch button:hover { color: var(--ink-1); }
.theme-switch button[aria-pressed="true"] { background: var(--brand-bg); color: var(--brand-ink); }

/* --------------------------------------------------------------------------
   18. Reader shell bar
   -------------------------------------------------------------------------- */
body.reader { display: block; overflow: hidden; }
.readerbar {
  display: flex;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  block-size: var(--bar-h);
  padding-inline: var(--space-3) var(--space-4);
  background: var(--surface-0);
  border-block-end: 1px solid var(--line-2);
}
/* Back link reads as a button, not bare text, so it never merges with
   the title or the icon cluster beside it. */
.readerbar .reader-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  min-block-size: var(--control-h);
  padding-inline: var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  color: var(--brand-ink);
  font-size: var(--fs-13);
  font-weight: 600;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.readerbar .reader-back:hover { background: var(--surface-2); border-color: var(--input-border-hover); text-decoration: none; }
.readerbar a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--brand-ink);
  font-size: var(--fs-13);
  font-weight: 600;
  text-decoration: none;
}
.readerbar a:hover { text-decoration: underline; }
.readerbar .t {
  flex: 1;
  min-inline-size: 0;
  overflow: hidden;
  color: var(--ink-2);
  font-size: var(--fs-13);
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Icon buttons are individually bordered with real gaps (not a shared
   transparent hit-area), so each one reads — and focuses — on its own. */
.reader-actions {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}
.reader-actions .icon-btn {
  flex-shrink: 0;
  box-shadow: var(--shadow-xs);
}
.reader-actions .icon-btn:hover { background: var(--surface-2); }
.reader-actions .icon-btn[aria-pressed="true"] {
  border-color: var(--brand-border);
  background: var(--brand-bg);
  color: var(--brand-ink);
}
@media (max-width: 560px) {
  .readerbar .t { display: none; }
  .readerbar { padding-inline: var(--space-3); }
}
body.reader iframe {
  display: block;
  inline-size: 100%;
  block-size: calc(100dvh - var(--bar-h));
  border: 0;
  background: var(--surface-0);
}

/* --------------------------------------------------------------------------
   19a. Reader appearance panel
        The controls live in the shell (read.php) and drive the reader iframe
        over postMessage, so the engine stays stock. Persistence is one
        localStorage key shared by both halves.
   -------------------------------------------------------------------------- */
.settings {
  position: fixed;
  inset-block: var(--bar-h) 0;
  inset-inline-end: 0;
  z-index: var(--z-menu);
  inline-size: min(348px, 92vw);
  display: flex;
  flex-direction: column;
  background: var(--surface-0);
  border-inline-start: 1px solid var(--line-1);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform var(--dur) var(--ease), visibility var(--dur) var(--ease);
}
/* `100%` is the element's own width, so this slides out of view in either
   writing direction without a direction-specific rule. */
[dir="rtl"] .settings { transform: translateX(-100%); }
.settings.open { transform: none; visibility: visible; }

.settings-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border-block-end: 1px solid var(--line-2);
}
.settings-head h2 { flex: 1; font-size: var(--fs-14); }

.settings-body {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--space-4);
  scrollbar-width: thin;
  scrollbar-color: var(--gray-300) transparent;
}
.settings-body::-webkit-scrollbar { inline-size: 6px; }
.settings-body::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }

.set-group { margin-block-end: var(--space-5); }
.set-group:last-child { margin-block-end: 0; }
.set-group > h3 {
  margin-block-end: var(--space-2);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.set-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-block-size: 34px;
  padding-block: 5px;
}
.set-row + .set-row { border-block-start: 1px solid var(--line-3); }
.set-label { color: var(--ink-2); font-size: var(--fs-13); }
.set-readout {
  flex-shrink: 0;
  color: var(--ink-3);
  font-size: var(--fs-12);
  font-variant-numeric: tabular-nums;
}

/* Whole-row slider: the label and track share a line so long panels stay short. */
.set-slider { display: block; padding-block: 6px var(--space-2); }
.set-slider-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-2);
  margin-block-end: 2px;
}

input[type="range"].set-range {
  inline-size: 100%;
  block-size: 22px;
  margin: 0;
  accent-color: var(--brand-solid);
  background: transparent;
  cursor: pointer;
}

.set-seg {
  display: inline-flex;
  flex-shrink: 0;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  background: var(--surface-2);
}
.set-seg button {
  min-block-size: 28px;
  min-inline-size: 40px;
  padding-inline: var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: var(--fs-12);
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.set-seg button:hover { color: var(--ink-1); background: rgb(0 0 0 / 0.05); }
.set-seg button:focus-visible { outline: var(--focus-w) solid var(--focus-ring); outline-offset: 1px; }
.set-seg button[aria-pressed="true"] {
  background: var(--surface-0);
  color: var(--brand-ink);
  box-shadow: var(--shadow-xs);
}

/* Font pickers render in their own face, which is the whole point. */
.set-fonts { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--space-2); }
.set-font {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  color: var(--ink-2);
  font: inherit;
  font-size: var(--fs-16);
  text-align: start;
  cursor: pointer;
}
.set-font small { color: var(--ink-3); font-size: var(--fs-11); }
.set-font:hover { border-color: var(--input-border-hover); }
.set-font[aria-pressed="true"] {
  border-color: var(--brand-solid);
  background: var(--brand-bg);
  color: var(--brand-ink);
}

/* Theme swatches: the button *is* the colour it selects. */
.set-themes { display: flex; gap: var(--space-2); }
.set-swatch {
  position: relative;
  flex: 1;
  block-size: 52px;
  padding: 0;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  cursor: pointer;
  overflow: hidden;
}
.set-swatch::after {
  content: attr(data-name);
  position: absolute;
  inset-block-end: 0;
  inset-inline: 0;
  padding-block: 2px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0.75;
}
.set-swatch[aria-pressed="true"] {
  border-color: var(--brand-solid);
  box-shadow: 0 0 0 2px var(--focus-halo);
}
.set-swatch:focus-visible { outline-offset: 1px; }

/* Scrim behind the open panel (small screens only, where it covers content). */
.settings-scrim {
  position: fixed;
  inset: var(--bar-h) 0 0;
  z-index: calc(var(--z-menu) - 1);
  background: rgb(0 0 0 / 0.35);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility var(--dur);
}
@media (max-width: 900px) {
  .settings-scrim.open { opacity: 1; visibility: visible; }
}

/* --------------------------------------------------------------------------
   19b. Global AI assistant
        One floating entry point on every application page. The panel is a
        normal (non-modal) dialog so the library stays usable behind it.
   -------------------------------------------------------------------------- */
.ai-fab {
  position: fixed;
  inset-block-end: var(--space-5);
  inset-inline-start: var(--space-5);
  z-index: calc(var(--z-menu) + 2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 52px;
  block-size: 52px;
  padding: 0;
  border: 1px solid var(--brand-solid);
  border-radius: 50%;
  background: var(--brand-solid);
  color: var(--brand-on);
  font: inherit;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: transform var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.ai-fab:hover { background: var(--brand-solid-hover); transform: translateY(-1px); }
.ai-fab[aria-expanded="true"] { background: var(--brand-solid-hover); }
.ai-fab .ic { vertical-align: 0; }

.ai-panel {
  position: fixed;
  inset-block-end: calc(var(--space-5) + 58px);
  inset-inline-start: var(--space-5);
  z-index: calc(var(--z-menu) + 2);
  display: flex;
  flex-direction: column;
  inline-size: min(404px, calc(100vw - 2 * var(--space-4)));
  max-block-size: min(640px, calc(100dvh - 140px));
  border: 1px solid var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-0);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: ai-in var(--dur) var(--ease);
}
.ai-panel[hidden] { display: none; }
@keyframes ai-in { from { opacity: 0; transform: translateY(8px); } }

.ai-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border-block-end: 1px solid var(--line-2);
  background: var(--surface-0);
}
.ai-head h2 { flex: 1; font-size: var(--fs-13); font-weight: 650; }
.ai-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 28px;
  block-size: 28px;
  border-radius: var(--radius-sm);
  background: var(--brand-bg);
  color: var(--brand-ink);
}

.ai-thread {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: var(--space-2);
  min-block-size: 180px;
  padding: var(--space-4);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface-1);
}
.ai-msg {
  max-inline-size: 94%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  font-size: var(--fs-13);
  line-height: var(--lh-loose);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.ai-msg p + p { margin-block-start: var(--space-2); }
.ai-msg-bot { align-self: flex-start; background: var(--surface-0); }
.ai-msg-user { align-self: flex-end; border-color: var(--brand-border); background: var(--brand-bg); color: var(--brand-ink); }
.ai-msg-error { border-color: var(--danger-border); background: var(--danger-bg); }
.ai-msg-thinking { color: var(--ink-3); }
.ai-msg-thinking::after {
  content: "";
  display: inline-block;
  inline-size: 6px;
  block-size: 6px;
  margin-inline-start: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: ai-pulse 900ms var(--ease) infinite alternate;
}
@keyframes ai-pulse { to { opacity: 0.2; } }

.ai-sources {
  margin-block-start: var(--space-2);
  padding-block-start: var(--space-2);
  border-block-start: 1px solid var(--line-2);
  font-size: var(--fs-12);
}
.ai-sources b { display: block; margin-block-end: 2px; color: var(--ink-3); font-size: var(--fs-11); font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; }
.ai-sources a { display: block; color: var(--brand-ink); text-decoration: none; }
.ai-sources a:hover { text-decoration: underline; }

.ai-quick { display: flex; flex-wrap: wrap; gap: var(--space-1); margin-block-start: var(--space-1); }
.ai-quick button {
  padding: 5px var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-pill);
  background: var(--surface-0);
  color: var(--ink-2);
  font: inherit;
  font-size: var(--fs-12);
  cursor: pointer;
}
.ai-quick button:hover { border-color: var(--brand-border); background: var(--brand-bg); color: var(--brand-ink); }

.ai-form {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-3);
  border-block-start: 1px solid var(--line-2);
  background: var(--surface-0);
}
.ai-form textarea {
  flex: 1;
  min-block-size: 44px;
  max-block-size: 132px;
  padding: 10px var(--space-3);
  resize: none;
  line-height: var(--lh-normal);
}
.ai-send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  inline-size: 42px;
  block-size: 42px;
  border: 1px solid var(--brand-solid);
  border-radius: var(--radius-md);
  background: var(--brand-solid);
  color: var(--brand-on);
  cursor: pointer;
}
.ai-send:hover { background: var(--brand-solid-hover); }
.ai-send:disabled { opacity: 0.5; cursor: wait; }
.ai-note { padding: 0 var(--space-3) var(--space-2); color: var(--ink-3); font-size: var(--fs-11); }

/* --------------------------------------------------------------------------
   19c. Settings page
   -------------------------------------------------------------------------- */
.prefs { display: flex; flex-direction: column; gap: var(--space-4); }
/* `.prefs` sets display:flex, which would otherwise beat the UA [hidden]
   rule and keep inactive tabs visible underneath each other. */
[hidden] { display: none !important; }
.prefs .form-card { margin: 0; }
.settings-tabs {
  display: flex;
  gap: 2px;
  margin-block: var(--space-4) var(--space-4);
  border-block-end: 1px solid var(--line-2);
}
.settings-tabs button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-block-end: 2px solid transparent;
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: var(--fs-13);
  font-weight: 600;
  cursor: pointer;
}
.settings-tabs button:hover { color: var(--ink-1); }
.settings-tabs button[aria-selected="true"] {
  border-block-end-color: var(--brand-solid);
  color: var(--brand-ink);
}
.settings-tabs button:focus-visible { outline-offset: -2px; }

/* --------------------------------------------------------------------------
   19d. Single-user sign-in
   -------------------------------------------------------------------------- */
.auth-page {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: clamp(var(--space-6), 8vh, 120px) var(--space-4) var(--space-10);
}
.auth-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  inline-size: min(420px, 100%);
}
.auth-head { display: flex; align-items: center; gap: var(--space-3); }
.auth-head h1 { font-size: var(--fs-20); }
.auth-head .hint { color: var(--ink-3); font-size: var(--fs-12); }
.auth-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 46px;
  block-size: 46px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--brand-bg);
  color: var(--brand-ink);
}
.auth-card .actions-bar { margin-block-start: 0; }
.status-pill {
  flex-shrink: 0;
  padding: 3px var(--space-2);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-pill);
  background: var(--surface-1);
  color: var(--ink-3);
  font-size: var(--fs-11);
  font-weight: 700;
}
.status-pill.is-on { border-color: var(--brand-border); background: var(--brand-bg); color: var(--brand-ink); }
.ai-form-grid { margin-block-start: var(--space-3); }
.prefs-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3); margin-block-end: var(--space-3); }
.prefs-head h2 { font-size: var(--fs-14); }
.prefs-head p { margin-block-start: 2px; color: var(--ink-3); font-size: var(--fs-12); }
.prefs-range { inline-size: 100%; accent-color: var(--brand-solid); }
.prefs-scale { display: flex; justify-content: space-between; color: var(--ink-3); font-size: var(--fs-11); font-variant-numeric: tabular-nums; }
.prefs-value {
  min-inline-size: 2.2em;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  background: var(--brand-bg);
  color: var(--brand-ink);
  font-size: var(--fs-12);
  font-weight: 700;
  text-align: center;
}
.prefs-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-2); }

/* A strip of placeholders that mirrors the chosen card count. */
.prefs-preview {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 4px;
  margin-block-start: var(--space-4);
  padding: var(--space-2);
  border: 1px dashed var(--line-1);
  border-radius: var(--radius-md);
  background: var(--surface-1);
}
.prefs-preview span {
  block-size: 42px;
  border-radius: var(--radius-xs);
  background: var(--brand-border);
  transition: opacity var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.prefs-preview span.is-off { opacity: 0.18; background: var(--line-1); }
.prefs-preview span:nth-child(n+1) { opacity: 0.9; }
.prefs-preview span.is-off:nth-child(n) { opacity: 0.18; }

.seg-choice {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 2px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
}
.seg-choice label,
.seg-choice button,
.seg-choice a {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-block-size: 34px;
  padding-inline: var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-3);
  font: inherit;
  font-size: var(--fs-12);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.seg-choice input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.seg-choice input:checked + span,
.seg-choice button[aria-pressed="true"],
.seg-choice a[aria-pressed="true"] {
  border-color: var(--line-1);
  background: var(--surface-0);
  color: var(--brand-ink);
  box-shadow: var(--shadow-xs);
}
.seg-choice label:hover,
.seg-choice button:hover,
.seg-choice a:hover { color: var(--ink-1); }
.seg-choice input:focus-visible + span,
.seg-choice a:focus-visible { outline: var(--focus-w) solid var(--focus-ring); outline-offset: 2px; }

/* Maintenance backups ------------------------------------------------------- */
.backup-card { display: flex; flex-direction: column; gap: 0; }
.backup-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding-block: var(--space-4);
  border-block-end: 1px solid var(--line-3);
}
.backup-item:first-child { padding-block-start: 0; }
.backup-item:last-child { padding-block-end: 0; border-block-end: 0; }
.backup-item h3 { margin-block-end: 2px; font-size: var(--fs-13); }
.backup-item .hint { max-inline-size: 62ch; }
.backup-item form, .backup-item > .btn { flex-shrink: 0; }
.cmd {
  margin-block-start: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--line-1);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  color: var(--ink-2);
  font-size: var(--fs-11);
  overflow-x: auto;
  white-space: nowrap;
}
.cmd code { font-family: var(--font-mono); }
.key-status { color: var(--ink-3); font-size: var(--fs-12); }
.key-status code {
  padding: 1px 6px;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--ink-1);
  font-family: var(--font-mono);
  font-size: var(--fs-11);
}
@media (max-width: 620px) {
  .backup-item { flex-direction: column; gap: var(--space-2); }
}

/* Events log --------------------------------------------------------------
   The page uses the shared .detail shell (single .dmeta column). Without
   these rules the filter form fell back to a plain block stack and the
   table columns had no breathing room. */
.ev-filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: var(--space-3) var(--space-4);
  margin-block-start: var(--space-4);
  align-items: end;
}
@media (min-width: 900px) {
  .ev-filters { grid-template-columns: 1.2fr 1fr 1.4fr 1.1fr 1.1fr auto; }
}
.ev-filters .field { min-inline-size: 0; }
.ev-submit { flex-direction: row; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.ev-sort { display: flex; }
.ev-sort .select { inline-size: auto; min-inline-size: 150px; }
.events-table .ev-ts { font-variant-numeric: tabular-nums; white-space: nowrap; }
.events-table .ev-detail { max-inline-size: 560px; overflow-wrap: anywhere; }
.events-table .ev-type { white-space: nowrap; }
@media (max-width: 620px) {
  .events-table .ev-detail { max-inline-size: none; }
  .events-table .ev-ts { white-space: normal; }
}

/* --------------------------------------------------------------------------
   19. Responsive
   -------------------------------------------------------------------------- */
/* The card count is intentionally not breakpoint-fixed: the grid's min width
   (120px) handles narrow screens, so a 4–8 preference still degrades to 2–3
   columns on phones without a second set of rules. */

/* Below the tablet breakpoint the sidebar is always a drawer;
   pinning (wide screens only) is ignored there. */
@media (max-width: 1024px) {
  .layout { padding-inline: var(--space-3); }
}

@media (max-width: 860px) {
  .detail { flex-direction: column; gap: var(--space-5); }
  .dcover .cover-preview { inline-size: 180px; margin-inline: auto; }
}

@media (max-width: 720px) {
  :root { --bar-h: 52px; }
  /* Brand collapses to the mark; the search keeps the full width on row two. */
  .topbar {
    flex-wrap: wrap;
    align-content: center;
    gap: var(--space-2);
    block-size: auto;
    min-block-size: var(--bar-h);
    padding-block: 6px;
    padding-inline: var(--space-3);
  }
  .brand .brand-text { display: none; }
  .search { order: 3; flex-basis: 100%; max-inline-size: none; margin-inline: 0; }
  .search .kbd-hint { display: none; }
  .topnav { margin-inline-start: auto; }
  main { padding: var(--space-4) var(--space-2) var(--space-10); }
  .toolbar { align-items: stretch; }
  .toolbar-controls { inline-size: 100%; justify-content: space-between; }
  .booktable .hide-sm { display: none; }
  .ai-fab { inset-inline-start: var(--space-3); inset-block-end: var(--space-3); }
  .ai-panel { inset-inline-start: var(--space-3); inset-inline-end: auto; inset-block-end: 72px; inline-size: auto; max-block-size: calc(100dvh - 96px); }
  .actions-bar {
    position: sticky;
    inset-block-end: 0;
    z-index: var(--z-sticky);
    margin-inline: calc(-1 * var(--space-2));
    padding: var(--space-3) var(--space-2);
    background: var(--surface-1);
    border-block-start: 1px solid var(--line-2);
  }
}

@media (max-width: 420px) {
  .grid { gap: var(--space-2); }
  .card-body { padding: var(--space-2) var(--space-2) var(--space-2); }
}

/* Hover-capable pointers only: avoid sticky hover states on touch. */
@media (hover: none) {
  .card:hover { transform: none; }
  .btn:hover, .icon-btn:hover { background: inherit; }
  [data-tip]::after { display: none; }
}

/* --------------------------------------------------------------------------
   20. Preferences
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .card:hover { transform: none; }
  .cover img { transition: none; }
}

@media print {
  .topbar, .sidebar, .toolbar, .pager, footer { display: none !important; }
  body { background: #fff; color: #000; }
  main { padding: 0; }
}
/* ==========================================================================
   Pending requirements (context-071126): top bar centering, hover card actions,
   about-list alignment, article grid, markdown body.
   ========================================================================== */

.topbar { justify-content: space-between; }
.brand { position: absolute; inset-inline-start: 50%; transform: translateX(-50%); z-index: 2; }
.nav-drawer { position: relative; }
.nav-drawer > summary { list-style: none; cursor: pointer; }
.nav-drawer > summary::-webkit-details-marker { display: none; }
.nav-drawer[open] > summary { background: var(--surface-2); color: var(--ink-1); }
.menu { inset-inline-start: 0; inset-inline-end: auto; }

@media (max-width: 720px) {
  .brand { position: static; transform: none; inset-inline-start: auto; }
}

.card-actions { opacity: 0; pointer-events: none; transition: opacity var(--dur-fast) var(--ease); }
.card:hover .card-actions,
.card:focus-within .card-actions { opacity: 1; pointer-events: auto; }
/* Touch has no hover state to reveal the row, so it would be unusable: keep
   the quick actions visible on devices that cannot hover. Phones drop the row
   entirely — see the mobile-density block at the end of this file. */
@media (hover: none) {
  .card-actions { opacity: 1; pointer-events: auto; }
}

.about-list { display: grid; grid-template-columns: auto 1fr; gap: var(--space-2) var(--space-4); margin: 0; }
.about-list dt { font-weight: 600; color: var(--ink-2); margin: 0; }
.about-list dd { margin: 0; color: var(--ink-1); }

.article-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--space-3); }
.article-card {
  background: var(--surface-0);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.article-card-title { margin: 0; font-size: var(--fs-15); }
.article-card-title a { color: var(--ink-1); text-decoration: none; }
.article-card-title a:hover { color: var(--brand-ink); }
.article-card-date { margin: 0; color: var(--ink-3); font-size: var(--fs-12); }
.article-card-actions { display: flex; gap: var(--space-2); margin-block-start: auto; align-items: center; }
.article-card-excerpt { margin: 0; color: var(--ink-2); font-size: var(--fs-13); line-height: 1.5; }

.md-body { line-height: 1.7; }
.md-body h1, .md-body h2, .md-body h3 { margin-block: var(--space-3) var(--space-2); }
.md-body p { margin-block: 0 var(--space-3); }
.md-body ul, .md-body ol { padding-inline-start: var(--space-5); margin-block: 0 var(--space-3); }
.md-body code { background: var(--surface-2); padding: 0.1em 0.3em; border-radius: var(--radius-xs); }
.md-body pre { background: var(--surface-2); padding: var(--space-3); border-radius: var(--radius-sm); overflow: auto; }
.md-body blockquote { border-inline-start: 3px solid var(--line-1); padding-inline-start: var(--space-3); color: var(--ink-2); margin-inline: 0; }
.md-body table { width: 100%; border-collapse: collapse; }
.md-body th, .md-body td { border: 1px solid var(--line-2); padding: var(--space-2); }

@media (max-width: 860px) {
  .article-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 480px) {
  .article-grid { grid-template-columns: 1fr; }
}

/* --------------------------------------------------------------------------
   Mobile density (≤600px)
   --------------------------------------------------------------------------
   Phones run the 3-up shelf, so a card has very little room: the admin
   quick-action row is dropped outright (the cover and the title are the tap
   targets, and every action it carried lives on the book page), the small type
   steps down one notch, and the page/card gutters tighten.
   Only the small type tokens and gutters move — headings, control heights and
   --tap-min stay put so nothing becomes harder to hit.
   -------------------------------------------------------------------------- */
@media (max-width: 600px) {
  :root {
    --fs-12: 0.6875rem; /* 11px */
    --fs-13: 0.75rem;   /* 12px */
    --fs-14: 0.8125rem; /* 13px */
  }

  /* No hover row to fall back on: remove it rather than reserve space. */
  .card-actions { display: none; }

  main { padding: var(--space-4) var(--space-3) var(--space-8); }
  .topbar { padding-inline: var(--space-3); }
  .toolbar { margin-block-end: var(--space-2); }

  .card-body { padding: var(--space-2); }
  .card-title { line-height: 1.45; }
  .card .card-fav {
    inline-size: 26px;
    block-size: 26px;
    min-inline-size: 26px;
  }
  .nocover { padding: var(--space-2); font-size: var(--fs-20); }

  .pager { margin-block-start: var(--space-5); }
}
