/* File library pages (F4f, 04 §3.1): /library/documents, /library/documents/{id}, /library/brandbook.
   Ported from docs/design/f1/a/library.html + site.css («Нээлттэй»); tokens, .doc rows, .field, .chip, .pager and
   .page-head come from site.css. Colours only through the site tokens (One Blue Rule). Some rules set margins
   explicitly over the reboot-style reset in site.css. */

/* ---------- List: categories at the side, the files grouped by category ---------- */
.lib { display: grid; grid-template-columns: 280px 1fr; gap: var(--s8); align-items: start; }
.lib > * { min-width: 0; }
.side { position: sticky; isolation: isolate; top: calc(var(--header-h) + 24px); display: grid; gap: 4px; }
.side h2 { margin: 0; padding: 0 16px 8px; font-size: var(--fs-meta); font-weight: 600; color: var(--ink-2); letter-spacing: 0; }
.side a { position: relative; isolation: isolate; display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 52px; padding: 10px 16px; border-radius: var(--r-md); color: var(--ink); font-weight: 500; line-height: 1.3; text-decoration: none; overflow-wrap: anywhere; transition: background var(--t-fast) var(--ease), color var(--t-med) var(--ease), transform var(--t-fast) var(--ease); }
.side a:hover { background: var(--surface); color: var(--ink); }
.side a:active { transform: scale(.97); }
.side a[aria-current] { background: transparent; color: var(--blue); font-weight: 600; }
.side .count { flex: none; font-size: 1rem; font-weight: 500; color: var(--ink-2); }
.side a[aria-current] .count { color: var(--blue); }
/* The tint of the current category is its own box, so a view transition can slide it from the old page's link to the
   new one (the «selected chip slides» pattern, across real page loads; no client-side filtering). */
.side-hl { position: absolute; inset: 0; z-index: -1; border-radius: inherit; background: var(--blue-tint); pointer-events: none; }

.lib-results { margin-top: 40px; }
.result-note { margin: 0 0 var(--s5); color: var(--ink-2); }
.list-count { margin: 0 0 var(--s4); }
.group + .group { margin-top: var(--s8); }
.group > h2 { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 0 0 var(--s4); font-size: 1.5rem; overflow-wrap: anywhere; }
.group > h2 .meta { font-weight: 500; letter-spacing: 0; white-space: nowrap; }
.lib .field .btn { flex: none; }
.field input[type="search"]::-webkit-search-cancel-button { cursor: pointer; }
.doc-body { min-width: 0; }
.doc h3 { overflow-wrap: anywhere; }
.doc-desc { margin: 4px 0 0; color: var(--ink-2); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }

/* ---------- Document page ---------- */
.reader .page-head { max-width: 900px; }
.reader .page-head h1 { font-size: clamp(2rem, 1.3rem + 2.4vw, 3.25rem); overflow-wrap: anywhere; }
.reader .page-head .lead { max-width: var(--measure); overflow-wrap: anywhere; }
.doc-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: var(--s4) 0 0; }
.reader .actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: var(--s5); }
.doc-notice { margin: var(--s4) 0 0; padding: 12px 16px; border-radius: var(--r-md); background: var(--surface); color: var(--ink); font-weight: 500; }
.doc-notice::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 10px; border-radius: 50%; background: var(--gold); vertical-align: middle; }

/* Файл / Текст хувилбар: a segmented control. Without JS the two links jump to the panels; never hidden at any width. */
.doc-tabs { position: relative; isolation: isolate; display: inline-flex; flex-wrap: wrap; gap: 4px; max-width: 100%; margin: 0 0 var(--s5); padding: 4px; border-radius: var(--r-pill); background: var(--surface); }
.doc-tab { position: relative; z-index: 1; display: inline-flex; align-items: center; min-height: var(--tap); padding: 0 22px; border: 0; border-radius: var(--r-pill); background: transparent; color: var(--ink); font: 500 1rem/1.2 var(--font); text-decoration: none; cursor: pointer; transition: background var(--t-fast) var(--ease), color var(--t-med) var(--ease), transform var(--t-fast) var(--ease); }
.doc-tab:hover { background: var(--surface-2); color: var(--ink); }
.doc-tab:active { transform: scale(.97); }
.doc-tab[aria-selected="true"] { background: var(--blue); color: #fff; }
.doc-tabs-ind { position: absolute; left: 0; top: 0; z-index: 0; width: 0; height: 0; border-radius: var(--r-pill); background: var(--blue); opacity: 0; pointer-events: none; }
/* library.js places the pill; then the pill paints the state and slides between the tabs */
.doc-tabs.is-ready .doc-tabs-ind { opacity: 1; transition: transform var(--t-med) var(--ease-out), width var(--t-med) var(--ease-out), height var(--t-med) var(--ease-out); }
.doc-tabs.is-ready .doc-tab[aria-selected="true"] { background: transparent; }
.pane + .pane { margin-top: var(--s7); }
.pane[hidden] { display: none; }
.doc-tabs.is-ready ~ .pane + .pane { margin-top: 0; }
.pane.is-entering { animation: pane-in var(--t-reveal) var(--ease-out) both; }
@keyframes pane-in { from { opacity: 0; transform: translateY(12px); } }

/* PDF: pages are drawn by pdf.js as they near the screen and fade in once drawn. */
.pdf-viewer { max-height: 85vh; overflow-y: auto; overscroll-behavior: contain; padding: 12px; border-radius: var(--r-lg); background: var(--surface); }
.pdf-page { width: 100%; margin: 0 auto 12px; border-radius: 6px; background: #fff; box-shadow: var(--e1); overflow: hidden; }
.pdf-page:last-child { margin-bottom: 0; }
.pdf-page canvas { display: block; width: 100%; height: auto; opacity: 0; transform: translateY(10px); transition: opacity var(--t-reveal) var(--ease-out), transform var(--t-reveal) var(--ease-out); }
.pdf-page.is-drawn canvas { opacity: 1; transform: none; }
.pdf-status { margin: 0; padding: 40px 16px; text-align: center; color: var(--ink-2); }
.doc-image { display: block; max-width: 100%; height: auto; margin: 0 auto; border-radius: var(--r-lg); }

.reader-title { margin: 0 0 var(--s4); font-size: var(--fs-h3); }
.reader-text { max-width: var(--measure); overflow-wrap: anywhere; }
.reader-text > :first-child { margin-top: 0; }
.reader-text :is(p, ul, ol, table, blockquote) { margin: 0 0 1em; }
.reader-text :is(ul, ol) { padding-left: 1.4em; }
.reader-text :is(h1, h2, h3, h4) { margin: 1.4em 0 .5em; font-size: 1.375rem; line-height: 1.3; }
.reader-text img, .reader-text iframe { max-width: 100%; height: auto; }
.reader-text table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; }
.reader-text :is(td, th) { padding: 6px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.back { margin: var(--s7) 0 0; }

/* ---------- Brandbook: preview cards on the cool surface ---------- */
.brand-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s5); margin: 0; padding: 0; list-style: none; }
.brand-card { display: grid; grid-template-rows: auto auto 1fr auto; gap: var(--s3); height: 100%; padding: var(--s4) var(--s4) var(--s5); border-radius: var(--r-lg); background: var(--surface); transition: background var(--t-med) var(--ease), box-shadow var(--t-med) var(--ease); }
.brand-card:hover { background: #fff; box-shadow: var(--e2); }
.brand-ph { position: relative; display: grid; place-items: center; aspect-ratio: 4 / 3; margin-bottom: var(--s2); border-radius: var(--r-md); background: #fff; overflow: hidden; }
/* Absolute, so a portrait preview never stretches the 4:3 box (no layout shift while it loads). */
.brand-ph img { position: absolute; inset: 16px; width: calc(100% - 32px); height: calc(100% - 32px); object-fit: contain; transition: opacity 400ms var(--ease), transform var(--t-med) var(--ease); }
.brand-card:hover .brand-ph img { transform: scale(1.04); }
.brand-ph .well { background: var(--blue-tint); }
.brand-card h2 { margin: 0; padding: 0 var(--s2); font-size: 1.25rem; font-weight: 600; line-height: 1.3; letter-spacing: -0.01em; overflow-wrap: anywhere; }
.brand-desc { margin: 0; padding: 0 var(--s2); color: var(--ink-2); overflow-wrap: anywhere; }
.brand-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; padding: var(--s3) var(--s2) 0; border-top: 1px solid var(--line); }

/* ---------- Wide screens: cross-page view transitions keep the categories still and slide the tint ---------- */
@media (min-width: 1081px) {
  .side { view-transition-name: lib-side; }
  .side-hl { view-transition-name: lib-cat; }
}
::view-transition-group(lib-cat) { animation-duration: 420ms; animation-timing-function: var(--ease-out); mix-blend-mode: multiply; }
::view-transition-new(lib-side):only-child, ::view-transition-new(lib-cat):only-child { animation: vt-in var(--t-reveal) var(--ease-out) both; }
::view-transition-old(lib-side):only-child, ::view-transition-old(lib-cat):only-child { animation: vt-out 180ms var(--ease) both; }

/* ---------- Phones and tablets (≤ 1080 px; checked at 360 px) ---------- */
@media (max-width: 1080px) {
  .lib { grid-template-columns: 1fr; gap: var(--s6); }
  /* the categories become a horizontal chip scroller (DESIGN.md Layout); library.js scrolls the current one into view */
  .side { position: relative; top: auto; display: flex; gap: 8px; overflow-x: auto; margin-inline: calc(-1 * var(--gutter)); padding-inline: var(--gutter); scrollbar-width: none; -webkit-mask-image: linear-gradient(90deg, black 85%, transparent); mask-image: linear-gradient(90deg, black 85%, transparent); }
  .side::-webkit-scrollbar { display: none; }
  .side h2 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; padding: 0; }
  .side a { flex: none; min-height: var(--tap); padding: 0 16px; border-radius: var(--r-pill); background: var(--surface); white-space: nowrap; gap: 8px; overflow-wrap: normal; }
  .side a[aria-current] { background: transparent; }
  .lib-results { margin-top: var(--s6); }
  .group + .group { margin-top: var(--s7); }
  .lib .field { padding-left: 16px; }
  .lib .field .btn { padding: 0 16px; }
  .reader .actions .btn { flex: 1 1 100%; }
  .doc-tabs { display: flex; width: 100%; }
  .doc-tab { flex: 1 1 auto; justify-content: center; padding: 0 16px; }
  .pdf-viewer { max-height: none; padding: 6px; border-radius: var(--r-md); }
  .brand-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s4); }
}
@media (max-width: 600px) {
  .brand-grid { grid-template-columns: 1fr; }
}

/* ---------- Reduced motion: no travel; the pill and tint still change place, instantly ---------- */
@media (prefers-reduced-motion: reduce) {
  .pdf-page canvas { transform: none; transition: opacity var(--t-fast) var(--ease); }
  .pane.is-entering { animation: none; }
  .doc-tabs.is-ready .doc-tabs-ind { transition: none; }
  .side a:active, .doc-tab:active, .brand-card:hover .brand-ph img { transform: none; }
}
