/* Reference library (web/library.mjs) on the Sat Globe design system (DESIGN.md).
 *
 * A rail panel like Layers: docked left beside the rail, full height between the strip and
 * the status bar on desktop and tablet landscape; a bottom sheet above the status bar / bottom
 * bar in portrait; docked right on a phone in landscape. Geometry comes from console.css's
 * frame variables (--cx-top/-bot/-left/-right, --ad-sheet-dw), the same ones #sheet uses.
 *
 * Tokens: --cx-* with the DESIGN.md value as fallback; night (body.nvg) comes through
 * console.css re-pointing the --cx-* set. Blue is selection only (the current tab).
 * The root sits outside #map so the NVG filter on the map does not dim the panel.
 */
.lib {
  --lb-bg: var(--cx-panel, #1c2127);
  --lb-raised: var(--cx-raised, #252a31);
  --lb-hover: var(--cx-hover, #2f343c);
  --lb-line: var(--cx-line, rgba(255, 255, 255, .10));
  --lb-line-hi: var(--cx-line-hi, rgba(255, 255, 255, .22));
  --lb-ink: var(--cx-ink, #f6f7f9);
  --lb-dim: var(--cx-dim, #abb3bf);
  --lb-mute: var(--cx-mute, #8f99a8);
  --lb-sel: var(--cx-select, #2d72d2);
  --lb-sel-ink: var(--cx-select-ink, #fff);
  --lb-bad: var(--cx-bad, #e76a6e);
  --lb-font: var(--cx-font, "Public Sans", system-ui, sans-serif);
  --lb-mono: var(--cx-mono, "IBM Plex Mono", ui-monospace, monospace);
  --lb-r: var(--cx-r, 2px);
  --lb-shadow: var(--cx-shadow, 0 6px 20px rgba(0, 0, 0, .45));
  --lb-input: rgba(0, 0, 0, .28);
  --lb-head: 40px;         /* panel header */
  --lb-ctl: 28px;          /* field, tabs, header buttons are 32 */
  --lb-hbtn: 32px;
  --lb-pad: 12px;
  --lb-lead: 40px;         /* thumbnail column, 4:3 */
  font: 400 13px/1.35 var(--lb-font);
  letter-spacing: normal;
  color: var(--lb-ink);
}
:root:is([data-input="touch"], [data-input="pen"]) .lib,
:root:is([data-form="phone"], [data-form="tablet"]) .lib {
  --lb-head: 48px;
  --lb-ctl: 44px;
  --lb-hbtn: 44px;
  --lb-pad: 16px;
}

.lib, .lib *, .lib *::before, .lib *::after { box-sizing: border-box; letter-spacing: normal; }
.lib [hidden] { display: none !important; }
/* Focus: console.css's ring, drawn inside so the panel's overflow never clips it; the field
 * shows focus with its select border, the panel itself (focused on open for touch) none. */
.ui-console:not(#x#x) .lib button:focus-visible { outline: 2px solid var(--cx-focus, #4c90f0); outline-offset: -2px; }
.ui-console:not(#x#x) .lib :is(input.lib-q, .lib-panel):focus-visible { outline: none; }
.lib :where(button, input) {
  font: inherit; color: inherit; background: none; border: 0; padding: 0; margin: 0;
  cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.lib :where(p, h2, dl, figure) { margin: 0; }
.lib svg { display: block; flex: none; }

/* The module's own launcher; the console shell uses the rail instead (adaptive.css hides it). */
.lib button.lib-btn {
  position: fixed; z-index: 26;
  top: calc(var(--cx-top, 48px)); right: var(--cx-right, 8px);
  height: var(--lb-ctl); padding: 0 8px;
  font: 500 13px/1 var(--lb-font);
  color: var(--lb-dim); background: var(--lb-bg);
  border: 1px solid var(--lb-line); border-radius: var(--lb-r);
}
.lib.is-open button.lib-btn { display: none; }

/* ———————————————————— panel and its placement ———————————————————— */
.lib-panel {
  position: fixed; z-index: 26;
  display: flex; flex-direction: column;
  /* desktop, tablet landscape: beside the rail, strip to status bar, like #sheet */
  top: var(--cx-top, calc(var(--safe-t, 0px) + 48px));
  bottom: var(--cx-bot, calc(var(--safe-b, 0px) + 34px));
  left: var(--cx-left, calc(var(--safe-l, 0px) + 56px));
  width: min(var(--ad-sheet-dw, 340px), calc(100vw - var(--cx-left, 56px) - 8px));
  background: var(--lb-bg); color: var(--lb-ink);
  border: 1px solid var(--lb-line); border-radius: var(--lb-r);
  box-shadow: var(--lb-shadow);
  overflow: hidden;
}
/* Portrait tablet and phone: a bottom sheet above the status bar / bottom bar. */
:root[data-orient="portrait"]:is([data-form="phone"], [data-form="tablet"]) .lib-panel {
  top: auto;
  bottom: var(--cx-bot-edge, var(--safe-b, 0px));
  left: var(--cx-left, 8px);
  right: var(--cx-right, 8px);
  width: auto;
  height: min(64dvh, calc(100dvh - var(--cx-top, 60px) - var(--cx-bot-edge, 0px)));
  border-bottom: 0;
  border-radius: var(--lb-r) var(--lb-r) 0 0;
}
:root[data-orient="portrait"][data-form="phone"] .lib-panel {
  left: var(--cx-pad, 8px);
  right: var(--cx-pad, 8px);
  height: min(72dvh, calc(100dvh - var(--cx-top, 100px) - var(--cx-bot-edge, 58px)));
}
/* Phone landscape: docked right. */
:root[data-orient="landscape"][data-form="phone"] .lib-panel {
  left: auto;
  right: var(--cx-right, 8px);
  width: min(340px, 46vw);
}

/* ———————————————————— header (DESIGN.md panel primitive) ———————————————————— */
.lib-head {
  flex: none;
  display: flex; align-items: center; gap: 4px;
  height: var(--lb-head);
  padding: 0 calc((var(--lb-head) - var(--lb-hbtn)) / 2) 0 var(--lb-pad);
  border-bottom: 1px solid var(--lb-line);
}
.lib.is-detail .lib-head { padding-left: calc((var(--lb-head) - var(--lb-hbtn)) / 2); }
.lib-title {
  flex: 1; min-width: 0;
  font: 600 13px/1.2 var(--lb-font);
  color: var(--lb-ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lib button.lib-back,
.lib button.lib-x {
  flex: none;
  display: grid; place-items: center;
  width: var(--lb-hbtn); height: var(--lb-hbtn);
  color: var(--lb-dim);
  border-radius: var(--lb-r);
}
.lib button.lib-back svg, .lib button.lib-x svg { width: 16px; height: 16px; }
@media (hover: hover) {
  .lib button.lib-back:hover,
  .lib button.lib-x:hover { color: var(--lb-ink); background: var(--lb-hover); }
}

/* ———————————————————— search field and tabs ———————————————————— */
.lib-tools {
  flex: none;
  display: grid; gap: 8px;
  padding: var(--lb-pad) var(--lb-pad) 8px;
}
.lib.is-detail .lib-tools,
.lib.is-detail .lib-list,
.lib.is-detail > .lib-panel > .lib-status { display: none !important; }

.lib-field {
  position: relative;
  display: flex; align-items: center;
  height: var(--lb-ctl);
  background: var(--lb-input);
  box-shadow: inset 0 0 0 1px var(--lb-line); /* hairline inside: the input keeps the full 44 px */
  border-radius: var(--lb-r);
  cursor: text;
}
.lib-field:focus-within { box-shadow: inset 0 0 0 1px var(--lb-sel); }
.lib-field > svg { width: 16px; height: 16px; margin: 0 8px; color: var(--lb-mute); pointer-events: none; }
.lib input.lib-q {
  flex: 1; min-width: 0; height: 100%;
  padding: 0 8px 0 0;
  font: 400 13px/1 var(--lb-font);
  color: var(--lb-ink);
  cursor: text; user-select: text;
  -webkit-appearance: none; appearance: none;
}
/* ≥ 16 px on touch: iOS zooms into smaller fields on focus. */
:root:is([data-input="touch"], [data-input="pen"]) .lib input.lib-q,
:root:is([data-form="phone"], [data-form="tablet"]) .lib input.lib-q { font-size: 16px; }
.lib input.lib-q::placeholder { color: var(--lb-mute); opacity: 1; }
.lib input.lib-q::-webkit-search-cancel-button,
.lib input.lib-q::-webkit-search-decoration { -webkit-appearance: none; display: none; }
.lib input.lib-q:focus-visible { outline: 0; }
.lib button.lib-clear {
  flex: none; display: grid; place-items: center;
  width: var(--lb-ctl); height: 100%;
  color: var(--lb-mute);
}
.lib button.lib-clear svg { width: 14px; height: 14px; }
@media (hover: hover) { .lib button.lib-clear:hover { color: var(--lb-ink); } }

/* Segmented control: quiet buttons, the current one a solid select fill. No outer frame. */
.lib-kinds { display: flex; gap: 2px; min-width: 0; }
.lib button.lib-kind {
  flex: 0 1 auto; min-width: 0;
  height: var(--lb-ctl); padding: 0 8px;
  color: var(--lb-dim);
  border-radius: var(--lb-r);
  font: 500 13px/1 var(--lb-font);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
:root:is([data-input="touch"], [data-input="pen"]) .lib button.lib-kind,
:root:is([data-form="phone"], [data-form="tablet"]) .lib button.lib-kind { flex: 1 1 auto; padding: 0 10px; }
@media (hover: hover) {
  .lib button.lib-kind:not([aria-checked="true"]):hover { color: var(--lb-ink); background: var(--lb-hover); }
}
.lib button.lib-kind[aria-checked="true"] { background: var(--lb-sel); color: var(--lb-sel-ink); }

/* ———————————————————— status: count, empty, error ———————————————————— */
.lib-status {
  flex: none;
  padding: 4px var(--lb-pad) 8px;
  font: 400 12px/1.4 var(--lb-font);
  color: var(--lb-mute);
  border-bottom: 1px solid var(--lb-line);
}
.lib-status b { font: 400 12px/1.4 var(--lb-mono); font-variant-numeric: tabular-nums slashed-zero; color: var(--lb-dim); }
.lib-status.is-empty, .lib-status.is-off {
  padding: 16px var(--lb-pad);
  border-bottom: 0;
}
.lib-msg-t { display: block; font: 600 13px/1.35 var(--lb-font); color: var(--lb-ink); }
.lib-msg-s { display: block; margin-top: 4px; color: var(--lb-dim); }
.lib-status.is-off .lib-msg-t::before {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  margin: 0 8px 1px 0;
  border-radius: 1px;
  background: var(--lb-bad);
}
.lib button.lib-act {
  margin: 8px 0 0 -8px;
  height: var(--lb-ctl); padding: 0 8px;
  font: 500 13px/1 var(--lb-font);
  color: var(--lb-ink);
  border-radius: var(--lb-r);
  text-decoration: underline;
  text-decoration-color: var(--lb-line-hi);
  text-underline-offset: 3px;
}
@media (hover: hover) { .lib button.lib-act:hover { text-decoration-color: currentColor; } }

/* ———————————————————— result rows ———————————————————— */
.lib-list, .lib-detail {
  flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin; scrollbar-color: var(--lb-line-hi) transparent;
}
.lib button.lib-row {
  display: flex; align-items: center; gap: 10px;
  width: 100%; min-height: 44px; padding: 6px var(--lb-pad);
  text-align: left; color: var(--lb-ink);
  border-bottom: 1px solid var(--lb-line);
}
:root:is([data-input="touch"], [data-input="pen"]) .lib button.lib-row { min-height: 52px; }
@media (hover: hover) { .lib button.lib-row:hover { background: var(--lb-hover); } }
.lib button.lib-row:active { background: var(--lb-hover); }
.lib-lead {
  flex: none;
  display: grid; place-items: center;
  width: var(--lb-lead); height: 30px;
  color: var(--lb-mute);
  border-radius: var(--lb-r);
  overflow: hidden;
}
.lib-lead svg { width: 16px; height: 16px; }
.lib-lead.has-img { background: var(--lb-raised); }
.lib-thumb { display: block; width: 100%; height: 100%; object-fit: cover; }
.lib-row-main { min-width: 0; display: grid; gap: 2px; }
.lib-row-name {
  font: 500 13px/16px var(--lb-font); color: var(--lb-ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lib-row-meta {
  font: 400 12px/15px var(--lb-font); color: var(--lb-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Loading: skeleton rows and a skeleton entry, never a blank panel. */
.lib-skel { pointer-events: none; }
.lib-skel .lib-row-sk {
  display: flex; align-items: center; gap: 10px;
  min-height: 44px; padding: 6px var(--lb-pad);
  border-bottom: 1px solid var(--lb-line);
}
:root:is([data-input="touch"], [data-input="pen"]) .lib-skel .lib-row-sk { min-height: 52px; }
.lib-sk {
  display: block; flex: none;
  height: 10px;
  border-radius: var(--lb-r);
  background: var(--lb-raised);
  animation: lib-sk 1s ease-in-out infinite alternate;
}
.lib-sk.is-lead { width: var(--lb-lead); height: 30px; }
.lib-sk-main { flex: 1; display: grid; gap: 7px; }
.lib-detail-sk { display: grid; gap: 10px; padding: var(--lb-pad); }
.lib-detail-sk .lib-sk:first-child { height: 180px; margin: calc(var(--lb-pad) * -1) calc(var(--lb-pad) * -1) 4px; border-radius: 0; }
@keyframes lib-sk { from { opacity: .45; } to { opacity: 1; } }

/* ———————————————————— entry ———————————————————— */
.lib-detail { padding-bottom: var(--lb-pad); }
.lib-kv {
  display: grid; grid-template-columns: auto minmax(0, 1fr);
  gap: 6px 12px; align-items: baseline;
  margin: var(--lb-pad) var(--lb-pad) 0;
}
/* Field names are data captions (DESIGN.md Typography): 11/600, uppercase, mute. */
.lib-kv dt {
  font: 600 11px/1.2 var(--lb-font);
  letter-spacing: .06em; text-transform: uppercase; color: var(--lb-mute);
}
.lib-kv dd { margin: 0; min-width: 0; font: 400 13px/1.35 var(--lb-font); color: var(--lb-ink); overflow-wrap: anywhere; }
/* Neutral tag (DESIGN.md Tags): simulated or invented parameters. */
.lib-sim {
  display: inline-block; margin: 12px var(--lb-pad) 0; padding: 0 4px;
  border: 1px solid var(--lb-line-hi); border-radius: var(--lb-r);
  font: 600 11px/16px var(--lb-font);
  color: var(--lb-dim);
}
.lib-summary {
  margin: 12px var(--lb-pad) 0;
  font: 400 13px/1.5 var(--lb-font); color: var(--lb-ink);
  overflow-wrap: anywhere; user-select: text;
}
.lib-src, .lib-cap {
  font: 400 11px/1.45 var(--lb-font);
  color: var(--lb-mute); overflow-wrap: anywhere; user-select: text;
}
.lib-src { margin: 8px var(--lb-pad) 0; }
.lib-src:first-of-type { margin-top: 12px; }
.lib-photo { margin: 16px 0 0; }
.lib-photo img {
  display: block; width: 100%; height: auto; max-height: 200px;
  object-fit: cover; background: var(--lb-raised);
}
.lib-cap { padding: 6px var(--lb-pad) 0; }
.lib-detail .lib-status { border-bottom: 0; }
body.nvg .lib-photo img,
body.nvg .lib-thumb {
  filter: grayscale(1) brightness(.45) sepia(1) saturate(3) hue-rotate(-38deg);
}

@media (prefers-reduced-motion: reduce) {
  .lib-sk { animation: none; opacity: .7; }
}
