/* ==========================================================================
   Montiliya — the video library (js/library.js): a layer like the profile, a grid of video cards, the player and the delete dialog.
   Same tokens as the app (css/styles.css): black and white, hierarchy from scale and space, red only for problems and deleting. Logical properties: LTR and RTL share it.
   ========================================================================== */

/* the sidebar link, under New Project */
.side-lib { margin-top: 0; }
.side-new[aria-current='page'] { background: var(--panel-3); }

/* the layer */
.library { position: absolute; inset: 0; overflow-y: auto; padding: var(--s6) var(--s8) var(--s12); }
.lib-inner { margin-inline: auto; width: 100%; max-width: 1240px; }
.lib-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4) var(--s6); flex-wrap: wrap; margin-bottom: var(--s6); }
.lib-head-t { min-width: 0; flex: 1 1 320px; }
.lib-head h1 { margin: 0 0 var(--s1); font-size: var(--fs-h2); font-weight: 650; letter-spacing: -0.02em; }
.lib-head h1:focus { outline: none; }
.lib-head h1:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px; border-radius: var(--r-sm); }
.lib-sub { margin: 0; color: var(--ink-3); font-size: var(--fs-md); text-wrap: pretty; }
.lib-filter { flex: none; grid-auto-columns: auto; }
.lib-filter .seg-opt { min-height: 36px; padding-inline: var(--s4); white-space: nowrap; }
.lib-filter .num { color: inherit; opacity: 0.6; font-weight: 500; }
.lib-note { margin: var(--s6) 0 0; color: var(--ink-3); font-size: var(--fs-sm); text-align: center; }

/* the grid */
.lib-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 196px), 1fr)); gap: var(--s6) var(--s4); }
.lib-card { display: flex; flex-direction: column; gap: var(--s2); min-width: 0; }

/* the thumbnail: one 9:16 frame for every card (vertical is the main format), other formats letterboxed in it */
.lib-thumb { position: relative; display: block; width: 100%; aspect-ratio: 9 / 16; border-radius: var(--r-lg); overflow: hidden; background: var(--panel-2); border: 1px solid var(--line); color: var(--ink-3); isolation: isolate; }
button.lib-thumb { cursor: pointer; transition: border-color var(--t-fast) var(--ease), transform var(--t-med) var(--ease); }
button.lib-thumb:hover { border-color: var(--line-strong); }
button.lib-thumb:active { transform: scale(0.99); }
.lib-thumb video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity var(--t-slow) var(--ease), transform var(--t-slow) var(--ease); pointer-events: none; }
.lib-thumb:not([data-ratio='9-16']) video { object-fit: contain; background: var(--panel-3); }
.lib-thumb.on video { opacity: 1; }
@media (hover: hover) and (pointer: fine) { button.lib-thumb.on:hover video { transform: scale(1.02); } }
.lib-thumb-off { position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center; gap: var(--s2); font-size: var(--fs-sm); }
.lib-thumb.off .lib-thumb-off { display: flex; }
.lib-thumb-off .icon { width: 28px; height: 28px; }
/* the play glyph and the length sit on the picture: white on a soft dark disc in both themes (the picture is footage, not the page) */
.lib-playic { position: absolute; inset-inline-start: var(--s3); inset-block-end: var(--s3); z-index: 1; display: grid; place-items: center; width: 36px; height: 36px; border-radius: var(--r-full); background: rgba(0, 0, 0, 0.55); color: #fff; backdrop-filter: blur(6px); transition: transform var(--t-fast) var(--ease), background-color var(--t-fast); }
.lib-playic .icon { width: 16px; height: 16px; fill: currentColor; stroke: none; }
button.lib-thumb:hover .lib-playic, button.lib-thumb:focus-visible .lib-playic { transform: scale(1.08); background: rgba(0, 0, 0, 0.72); }
.lib-dur { position: absolute; inset-inline-end: var(--s3); inset-block-end: var(--s3); z-index: 1; padding: 3px var(--s2); border-radius: var(--r-full); background: rgba(0, 0, 0, 0.6); color: #fff; font-size: var(--fs-xs); font-weight: 600; line-height: 1.3; }
button.lib-thumb:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; border-radius: var(--r-lg); }

/* a card with no picture yet (waiting, processing) or none at all (failed) */
.lib-thumb-state { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--s2); padding: var(--s4); text-align: center; font-size: var(--fs-sm); line-height: 1.4; }
.lib-thumb-state .icon { width: 26px; height: 26px; color: var(--ink-2); }
.lib-thumb-state small { font-size: var(--fs-xs); color: var(--ink-3); }
.k-failed .lib-thumb { border-style: dashed; }
.k-failed .lib-thumb-state .icon { color: var(--danger); }
.lib-bar { display: block; width: min(70%, 140px); height: 4px; margin-top: var(--s1); border-radius: var(--r-full); background: var(--line-strong); overflow: hidden; }
.lib-bar i { display: block; height: 100%; width: calc(var(--frac, 0) * 100%); min-width: 6%; border-radius: inherit; background: var(--ink); transition: width var(--t-slow) var(--ease); }
.k-processing .lib-thumb-state .icon, .k-queued .lib-thumb-state .icon { animation: lib-breathe 1.8s var(--ease-in-out) infinite alternate; }
@keyframes lib-breathe { from { opacity: 0.45; } to { opacity: 1; } }

/* the edits that stopped before a video was made: a short list under the grid */
.lib-fails { margin-top: var(--s10); }
.lib-grid[hidden] + .lib-fails { margin-top: 0; }
.lib-fails-head { margin-bottom: var(--s3); }
.lib-fails-head h2 { margin: 0 0 2px; font-size: var(--fs-lg); font-weight: 650; }
.lib-fails-head p { margin: 0; color: var(--ink-3); font-size: var(--fs-sm); }
.lib-rows { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; }
.lib-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; gap: var(--s1) var(--s4); padding: var(--s3) var(--s3) var(--s3) var(--s4); border-top: 1px solid var(--line); }
[dir='rtl'] .lib-row { padding: var(--s3) var(--s4) var(--s3) var(--s3); }
.lib-row:first-child { border-top: 0; }
.lib-row-ic { display: grid; place-items: center; width: 36px; height: 36px; border-radius: var(--r-full); background: var(--danger-bg); color: var(--danger); }
.lib-row-ic .icon { width: 18px; height: 18px; }
.lib-row .lib-meta2 { margin: 0; }
.lib-row .lib-acts { margin: 0; }
.lib-why { color: var(--ink-3); }

/* the words under it */
.lib-info { display: flex; flex-direction: column; gap: 3px; min-width: 0; padding-inline: 2px; }
.lib-name { margin: 0; font-size: var(--fs-md); font-weight: 600; line-height: 1.35; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-meta { margin: 0; font-size: var(--fs-sm); color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-ver { color: var(--ink-2); font-weight: 500; }
.lib-meta .dot { margin-inline: 6px; }
.lib-meta2 { margin: var(--s1) 0 0; display: flex; align-items: center; flex-wrap: wrap; gap: var(--s1) var(--s2); font-size: var(--fs-sm); color: var(--ink-3); }
.lib-acts { display: flex; align-items: center; gap: 2px; margin-inline-start: -6px; }
.lib-act { width: 38px; height: 38px; }
.lib-act .icon { width: 18px; height: 18px; }
.lib-act.danger:hover { color: var(--danger); background: var(--danger-bg); }
.lib-act[aria-disabled='true'] { opacity: 0.45; }
.lib-act[aria-busy='true'] { cursor: progress; opacity: 0.6; }

/* loading: quiet shapes where the cards will be */
.lib-skel .lib-thumb { border-color: transparent; animation: lib-breathe 1.2s var(--ease-in-out) infinite alternate; }
.lib-line { display: block; height: 12px; border-radius: 6px; background: var(--panel-2); }
.lib-line.short { width: 55%; }

/* empty, nothing for this filter, could not load */
.lib-state { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--s3); max-width: 440px; margin: 0 auto; padding: var(--s12) var(--s4); color: var(--ink-3); }
.lib-state.small { padding-block: var(--s10); }
.lib-state h2 { margin: 0; color: var(--ink); font-size: var(--fs-xl); font-weight: 650; letter-spacing: -0.01em; }
.lib-state p { margin: 0; line-height: 1.55; text-wrap: pretty; }
.lib-state .mascot { margin-bottom: var(--s1); }
.lib-state > .icon { width: 30px; height: 30px; color: var(--danger); }
.lib-state .btn { margin-top: var(--s3); }

/* the player */
.lib-player { width: min(600px, calc(100vw - 32px)); }
.lib-pl { display: flex; flex-direction: column; gap: var(--s4); padding: var(--s5); }
.lib-pl-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s3); }
.lib-pl-t { min-width: 0; }
.lib-pl-head h2 { margin: 0; font-size: var(--fs-lg); font-weight: 650; overflow-wrap: anywhere; }
.lib-pl-head .icon-btn { flex: none; margin: calc(-1 * var(--s2)) calc(-1 * var(--s2)) 0 0; }
[dir='rtl'] .lib-pl-head .icon-btn { margin: calc(-1 * var(--s2)) 0 0 calc(-1 * var(--s2)); }
.lib-pl-frame { position: relative; margin-inline: auto; max-width: 100%; aspect-ratio: 9 / 16; height: min(62dvh, 680px); border-radius: var(--r-lg); overflow: hidden; background: #000; }
.lib-pl-frame[data-ratio='16-9'] { aspect-ratio: 16 / 9; height: auto; width: 100%; }
.lib-pl-frame[data-ratio='1-1'] { aspect-ratio: 1 / 1; height: min(62dvh, 520px); }
.lib-pl-frame[data-ratio='4-5'] { aspect-ratio: 4 / 5; height: min(62dvh, 600px); }
.lib-pl-frame video { display: block; width: 100%; height: 100%; object-fit: contain; background: #000; }
.lib-pl-err { position: absolute; inset: 0; display: grid; place-items: center; padding: var(--s5); text-align: center; color: #fff; background: rgba(0, 0, 0, 0.7); }
.lib-pl-err[hidden] { display: none; }
.lib-pl .dlg-actions { margin-top: 0; }
.lib-del-err { color: var(--danger); font-size: var(--fs-sm); }

/* ---- responsive ----------------------------------------------------------- */
@media (max-width: 900px) { .library { padding: var(--s5) var(--s5) var(--s10); } }
@media (max-width: 640px) {
  .library { padding: var(--s4) var(--s4) var(--s10); padding-bottom: max(var(--s10), env(safe-area-inset-bottom, 0px)); }
  .lib-head { margin-bottom: var(--s5); }
  .lib-filter { width: 100%; grid-auto-columns: minmax(0, 1fr); }
  .lib-filter .seg-opt { padding-inline: var(--s2); }
  .lib-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s5) var(--s3); }
  .lib-pl { padding: var(--s4); }
  .lib-pl-frame { height: min(58dvh, 620px); }
}
@media (max-width: 760px) {
  .lib-row { grid-template-columns: auto minmax(0, 1fr) auto; }
  .lib-row .lib-meta2 { grid-column: 2 / 4; grid-row: 2; }
  .lib-row .lib-acts { grid-column: 3; grid-row: 1; }
}
@media (max-width: 340px) { .lib-grid { grid-template-columns: minmax(0, 1fr); } }
@media (prefers-reduced-motion: reduce) {
  .lib-skel .lib-thumb, .k-processing .lib-thumb-state .icon, .k-queued .lib-thumb-state .icon { animation: none; }
  .lib-thumb video, .lib-bar i, button.lib-thumb { transition: none; }
}
