/* One opening action, a separate audio mode, and quiet file management.
   Only the middle section scrolls; dismissal and import stay reachable. */
body.library-open { overflow: hidden; }
.library-dialog {
  --library-danger: #aa302b;
  --library-danger-soft: #fbe8e5;
  width: calc(100% - 32px); max-width: 620px;
  height: min(84vh, 780px); height: min(84dvh, 780px);
  max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  padding: 0; overflow: hidden; overscroll-behavior: contain;
  border: 1px solid var(--line); border-radius: 24px;
  background: var(--bg); color: var(--text);
  transform: translateY(var(--library-drag-y, 0px));
  box-shadow: 0 20px 70px #0004;
}
[data-theme="dark"] .library-dialog { --library-danger: #ffb1a7; --library-danger-soft: #402b28; }
.library-dialog[open] { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
.library-dialog.library-dragging { will-change: transform; }
.library-dialog.library-snapping { transition: transform 160ms ease-out; }
.library-dialog::backdrop { background: #08100cbf; }
.library-dialog p { font-size: 1rem; line-height: 1.5; overflow-wrap: anywhere; }
.library-dialog button {
  min-width: 48px; min-height: 48px; max-width: 100%;
  font-size: .9375rem; line-height: 1.3; font-weight: 550;
  white-space: normal; overflow-wrap: anywhere; -webkit-tap-highlight-color: transparent;
}
.library-dialog button:focus-visible, .library-dialog input:focus-visible {
  outline: 2px solid var(--focus-color); outline-offset: 3px;
}
.library-dialog button:disabled { opacity: .45; cursor: default; }
.library-dialog button svg { width: 20px; height: 20px; stroke-width: 1.8; flex: 0 0 20px; }
.library-header {
  min-width: 0; padding: 0 max(18px, env(safe-area-inset-right)) 14px max(18px, env(safe-area-inset-left));
  background: var(--bg);
}
.library-handle {
  display: grid; place-items: center; width: 100%; height: 48px;
  padding: 0; border-radius: 12px; background: transparent; color: var(--muted);
  cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none;
}
.library-handle span { width: 36px; height: 4px; border-radius: 4px; background: currentColor; opacity: .75; }
.library-handle:active { cursor: grabbing; }
.library-handle:focus-visible { outline-offset: -4px; }
.library-heading-row, .library-heading-copy { display: flex; align-items: center; min-width: 0; }
.library-heading-row { justify-content: space-between; gap: 12px; min-height: 48px; }
.library-heading-copy { flex-wrap: wrap; gap: 4px 10px; }
.library-dialog .library-header h2 { margin: 0; font-size: 1.5rem; font-weight: 650; line-height: 1.2; letter-spacing: -.025em; }
.library-count {
  padding: 4px 8px; border-radius: 8px; background: var(--paper);
  color: var(--muted); font-size: .8125rem; font-weight: 500; line-height: 1.3; font-variant-numeric: tabular-nums;
}
.library-count:empty { display: none; }
.library-icon-button {
  display: inline-grid; place-items: center; width: 48px; height: 48px;
  flex: 0 0 48px; padding: 0; border-radius: 14px;
  background: var(--paper); color: var(--text); border: 1px solid var(--line);
}
.library-icon-button[aria-expanded="true"] { background: var(--soft); border-color: var(--accent); color: var(--accent); }
.library-search { position: relative; margin-top: 12px; }
.library-search > svg {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 20px; height: 20px; color: var(--muted); pointer-events: none;
}
.library-search input {
  display: block; width: 100%; min-width: 0; min-height: 48px; padding: 11px 14px 11px 44px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--paper); color: var(--text); font-size: 1rem; line-height: 1.4;
}
.library-search input::placeholder { color: var(--muted); opacity: 1; }
.library-content {
  min-width: 0; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  scrollbar-gutter: stable; scrollbar-width: thin; scroll-padding-block: 16px;
  padding: 3px max(18px, env(safe-area-inset-right)) 16px max(18px, env(safe-area-inset-left));
}
.library-dialog .library-status {
  margin: 0 0 12px; padding: 10px 12px; border-radius: 12px;
  background: var(--paper); color: var(--muted); font-size: .875rem; line-height: 1.45;
}
.library-status:empty { display: none; }
.library-dialog .library-status.is-error { color: var(--accent); background: var(--soft); }
.library-list { display: grid; gap: 12px; margin: 0; padding: 0; list-style: none; }
.library-item {
  min-width: 0; padding: 14px; border: 1px solid var(--line); border-radius: 18px;
  background: var(--paper); box-shadow: 0 2px 8px #0000000b; scroll-margin-block: 16px;
}
.library-item-heading { display: flex; align-items: flex-start; gap: 10px; min-width: 0; }
.library-item-art {
  display: grid; place-items: center; flex: 0 0 40px; width: 40px; height: 48px;
  border-radius: 11px; background: var(--bg); color: var(--green);
}
.library-item-art svg { width: 24px; height: 24px; stroke-width: 1.7; }
.library-item-copy { flex: 1; min-width: 0; align-self: center; }
.library-dialog .library-item-name {
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;
  overflow: hidden; min-width: 0; margin: 0; color: var(--text);
  font-size: 1.0625rem; font-weight: 600; line-height: 1.35; overflow-wrap: anywhere;
}
.library-dialog .library-item-meta { margin: 5px 0 0; color: var(--muted); font-size: .8125rem; line-height: 1.4; }
.library-item-more { margin: -3px -3px 0 0; background: transparent; border-color: transparent; }
.library-progress { height: 4px; margin: 13px 0 12px; border-radius: 4px; background: var(--line); overflow: hidden; }
.library-progress > span { display: block; width: var(--library-progress, 0%); max-width: 100%; height: 100%; border-radius: inherit; background: var(--green); }
.library-item-controls { display: flex; flex-wrap: wrap; align-items: stretch; justify-content: flex-end; gap: 10px; min-width: 0; }
.library-audio-toggle {
  display: grid; grid-template-columns: 18px minmax(0, 1fr) 32px; align-items: center; gap: 6px;
  flex: 1 1 8.75rem; min-width: 0; padding: 6px 8px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); color: var(--muted); text-align: left;
}
.library-dialog .library-audio-toggle { font-size: .875rem; font-weight: 500; line-height: 1.25; }
.library-dialog .library-audio-toggle > svg { width: 18px; height: 18px; }
.library-audio-label { min-width: 0; overflow-wrap: normal; }
.library-switch { display: block; position: relative; width: 32px; height: 20px; padding: 3px; border-radius: 12px; background: var(--track); }
.library-switch i { display: block; width: 14px; height: 14px; border-radius: 50%; background: var(--muted); }
.library-audio-toggle[aria-checked="true"] { border-color: var(--green); color: var(--text); }
.library-audio-toggle[aria-checked="true"] .library-switch { background: var(--button); }
.library-audio-toggle[aria-checked="true"] .library-switch i { transform: translateX(12px); background: var(--button-text); }
.library-open-button {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  flex: 0 0 auto; padding: 11px 16px; border-radius: 12px;
  background: var(--button); color: var(--button-text); border: 1px solid transparent;
}
.library-item-details { min-width: 0; margin-top: 14px; padding: 14px 0 0; border-top: 1px solid var(--line); }
.library-dialog .library-file-label { margin: 0; color: var(--muted); font-size: .75rem; font-weight: 550; line-height: 1.5; }
.library-dialog .library-filename { margin: 3px 0 12px; color: var(--text); font-size: .875rem; line-height: 1.45; overflow-wrap: anywhere; }
.library-management-actions { display: grid; gap: 8px; }
.library-action-row {
  display: flex; align-items: center; justify-content: flex-start; gap: 12px;
  width: 100%; padding: 12px; text-align: left; border-radius: 12px;
  background: var(--bg); color: var(--text); border: 1px solid transparent;
}
.library-dialog .is-destructive { color: var(--library-danger); background: var(--library-danger-soft); }
.library-management-actions .is-destructive { margin-top: 4px; }
.library-manage { margin: 0 0 14px; padding: 14px; background: var(--paper); border: 1px solid var(--line); border-radius: 18px; }
.library-dialog .library-manage p { margin: 0 0 12px; color: var(--muted); font-size: .875rem; }
.library-confirm { margin-top: 14px; padding: 14px; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; }
.library-dialog .library-confirm p { margin: 0 0 12px; font-size: .875rem; }
.library-confirm-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.library-confirm-actions button { display: inline-flex; justify-content: center; align-items: center; gap: 8px; flex: 1 1 90px; padding: 10px 14px; border-radius: 12px; }
#libraryConfirmCancel { color: var(--text); border: 1px solid var(--line); background: var(--paper); }
#libraryConfirmAccept { color: var(--text); border: 1px solid var(--line); background: var(--paper); }
#libraryConfirmAccept.is-destructive { color: var(--library-danger); background: var(--library-danger-soft); border-color: transparent; }
.library-recovery { margin-top: 14px; padding: 12px; border-radius: 14px; background: var(--soft); color: var(--text); }
.library-recovery-message { display: flex; align-items: flex-start; gap: 10px; }
.library-recovery-message > svg { width: 20px; height: 20px; flex: 0 0 20px; margin-top: 2px; color: var(--accent); }
.library-dialog .library-recovery p { margin: 0; font-size: .875rem; line-height: 1.45; }
.library-reconnect-button {
  display: inline-flex; justify-content: center; align-items: center; gap: 8px; width: 100%;
  margin-top: 10px; padding: 10px 12px; border: 1px solid var(--accent); border-radius: 12px;
  background: transparent; color: var(--text);
}
.library-empty { margin: 28px auto; max-width: 30ch; text-align: center; }
.library-empty-art { display: grid; place-items: center; width: 72px; height: 72px; margin: 0 auto 18px; border-radius: 22px; background: var(--paper); color: var(--green); border: 1px solid var(--line); }
.library-empty-art > svg { width: 34px; height: 34px; stroke-width: 1.5; }
.library-empty strong { display: block; color: var(--text); font-size: 1.125rem; font-weight: 600; line-height: 1.4; }
.library-dialog .library-empty p { margin: 10px 0 0; color: var(--muted); font-size: 1rem; line-height: 1.5; }
.library-dialog #libraryLoading, .library-dialog #libraryNoResults { color: var(--muted); font-size: .9375rem; }
#libraryRetryButton { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 16px; margin-top: 16px; border-radius: 12px; color: var(--text); background: var(--paper); border: 1px solid var(--line); }
.library-footer {
  min-width: 0; padding: 12px max(18px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  background: var(--bg); border-top: 1px solid var(--line);
}
.library-footer .button { width: 100%; min-height: 52px; border-radius: 14px; font-size: 1rem; font-weight: 600; border: 1px solid transparent; }
.library-footer .button svg { width: 22px; height: 22px; }
.has-library-items .library-footer .button { background: var(--paper); color: var(--text); border-color: var(--line); }
.library-dialog .library-help { margin: 10px 0 0; color: var(--muted); font-size: .8125rem; line-height: 1.35; text-align: center; }
/* Press feedback changes the surface, not the position or size of content. */
.library-dialog button:not(:disabled):active { box-shadow: inset 0 0 0 2px currentColor; }
.library-dialog button:not(:disabled):hover { text-decoration: none; }
@media (hover: hover) {
  .library-icon-button:not(:disabled):hover, .library-action-row:not(:disabled):hover, #libraryConfirmCancel:not(:disabled):hover, #libraryRetryButton:not(:disabled):hover { border-color: var(--muted); }
  .library-open-button:not(:disabled):hover, .library-footer .button:not(:disabled):hover { filter: brightness(1.07); }
  .library-audio-toggle:not(:disabled):hover { color: var(--text); border-color: var(--muted); }
}
@media (max-width: 600px), (max-width: 960px) and (max-height: 500px) {
  .library-dialog {
    position: fixed; inset: auto 0 0; margin: 0 auto;
    width: 100%; max-width: 640px;
    height: min(88vh, 800px); height: min(88dvh, 800px);
    max-height: calc(100vh - max(12px, env(safe-area-inset-top)));
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top)));
    border-bottom: 0; border-radius: 24px 24px 0 0;
    box-shadow: 0 -12px 50px #0003;
  }
}
@media (max-width: 360px) {
  .library-header { padding-inline: max(14px, env(safe-area-inset-left)) max(14px, env(safe-area-inset-right)); }
  .library-content { padding-inline: max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right)); }
  .library-footer { padding-inline: max(14px, env(safe-area-inset-left)) max(14px, env(safe-area-inset-right)); }
  .library-item { padding: 12px; }
  .library-item-art { flex-basis: 34px; width: 34px; }
  .library-item-heading { gap: 8px; }
  .library-dialog .library-item-name { font-size: 1rem; }
  .library-item-controls { gap: 8px; }
  .library-open-button { padding-inline: 12px; }
}
@media (max-height: 500px) {
  .library-dialog {
    height: calc(100vh - max(8px, env(safe-area-inset-top)));
    height: calc(100dvh - max(8px, env(safe-area-inset-top)));
    max-height: calc(100vh - max(8px, env(safe-area-inset-top)));
    max-height: calc(100dvh - max(8px, env(safe-area-inset-top)));
  }
  .library-header { padding-bottom: 10px; }
  .library-search { margin-top: 8px; }
  .library-footer { padding-top: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .library-dialog .library-help { display: none; }
  .library-empty { margin-block: 16px; }
  .library-empty-art { width: 52px; height: 52px; margin-bottom: 12px; border-radius: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .library-dialog.library-snapping { transition: none; }
}
