/* ──────────────────────────────────────────────
   Library view: folder sidebar + panel + template card grid
   ────────────────────────────────────────────── */

.library-view { display: grid; gap: var(--space-lg); min-width: 0; }

/* Two-column layout, mirror of the settings sub-sidebar so the user lands on
   a familiar pattern across views. On narrow viewports the sidebar collapses
   into a horizontal scrolling row of chips. */
.library-layout {
  display: grid;
  gap: var(--space-lg);
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
}

@media (min-width: 860px) {
  .library-layout {
    grid-template-columns: 14rem minmax(0, 1fr);
  }
}

.library-sidebar {
  align-self: start;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-3xs);
  padding: var(--space-xs);
  position: sticky;
  top: var(--space-md);
}

.library-sidebar__loading {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  margin: 0;
  padding: var(--space-sm);
}

.library-sidebar__item {
  align-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  display: flex;
  gap: var(--space-2xs);
  transition: background-color var(--transition-fast), border-color var(--transition-fast);
}

.library-sidebar__item:hover {
  background: var(--bg-hover);
}

.library-sidebar__item.is-active {
  background: var(--bg-accent-soft);
  border-color: var(--border-subtle);
}

.library-sidebar__item.is-drag-over {
  background: var(--bg-accent-soft);
  border-color: var(--vibrance-hover);
}

.library-sidebar__btn {
  align-items: center;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  flex: 1;
  font-size: var(--text-sm);
  font-weight: 500;
  gap: var(--space-sm);
  min-height: var(--control-h-lg);
  min-width: 0;
  padding: 0 var(--space-sm);
  text-align: left;
}

.library-sidebar__item.is-active .library-sidebar__btn {
  color: var(--text-primary);
  font-weight: 600;
}

.library-sidebar__btn:focus-visible {
  outline: var(--focus-outline);
  outline-offset: var(--focus-offset);
}

.library-sidebar__icon {
  align-items: center;
  color: var(--text-muted);
  display: inline-flex;
  flex-shrink: 0;
  justify-content: center;
}

.library-sidebar__icon svg {
  height: var(--icon-md);
  width: var(--icon-md);
}

.library-sidebar__item.is-active .library-sidebar__icon {
  color: var(--vibrance-hover);
}

.library-sidebar__label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-sidebar__count {
  background: var(--bg-card-soft);
  border-radius: var(--radius-pill);
  color: var(--text-muted);
  flex-shrink: 0;
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  min-width: 1.5rem;
  padding: 0 var(--space-xs);
  text-align: center;
}

.library-sidebar__item.is-active .library-sidebar__count {
  background: var(--bg-card);
  color: var(--text-primary);
}

.library-sidebar__item-actions {
  display: flex;
  gap: var(--space-3xs);
  opacity: 0;
  padding-right: var(--space-2xs);
  transition: opacity var(--transition-fast);
}

.library-sidebar__item:hover .library-sidebar__item-actions,
.library-sidebar__item.is-active .library-sidebar__item-actions {
  opacity: 1;
}

.library-sidebar__action {
  align-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-xs);
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  font-size: var(--text-xs);
  height: var(--control-h-xs);
  justify-content: center;
  padding: 0;
  transition: background-color var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
  width: var(--control-h-xs);
}

.library-sidebar__action:hover {
  background: var(--bg-hover);
  border-color: var(--border-subtle);
  color: var(--text-primary);
}

.library-sidebar__action--danger:hover {
  background: var(--bg-danger-soft);
  border-color: var(--vibrance-danger);
  color: var(--vibrance-danger);
}

.library-sidebar__divider {
  background: var(--border-subtle);
  height: 1px;
  margin: var(--space-2xs) var(--space-xs);
}

.library-sidebar__new {
  align-items: center;
  background: transparent;
  border: 1px dashed var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  font-size: var(--text-sm);
  gap: var(--space-xs);
  justify-content: center;
  margin-top: var(--space-2xs);
  min-height: var(--control-h-lg);
  padding: 0 var(--space-sm);
  transition: background-color var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.library-sidebar__new:hover {
  background: var(--bg-hover);
  border-color: var(--vibrance-hover);
  color: var(--text-primary);
}

.library-sidebar__new span {
  font-size: var(--text-lg);
  line-height: 1;
}

@media (max-width: 859px) {
  .library-sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    position: static;
  }
  .library-sidebar__item {
    flex: 1 1 auto;
    min-width: 0;
  }
  .library-sidebar__divider {
    display: none;
  }
  .library-sidebar__label {
    display: none;
  }
  .library-sidebar__new {
    flex: 0 0 auto;
  }
}

.library-panel { padding: var(--space-lg); }
.library-panel__header { align-items: center; display: flex; gap: var(--space-sm); justify-content: space-between; margin-bottom: var(--space-lg); }
.library-panel__title { font-size: var(--text-xl); font-weight: 600; letter-spacing: -0.02em; margin: 0; }
.library-panel__text { color: var(--text-secondary); margin-top: var(--space-2xs); }
.library-panel__loading { color: var(--text-secondary); }
.library-panel__error { color: var(--vibrance-danger); }

.library-grid { display: grid; gap: var(--space-md); grid-template-columns: repeat(auto-fill, minmax(var(--card-min), 1fr)); }

.library-card { background: var(--bg-card-soft); border: 1px solid var(--border-subtle); border-radius: var(--radius-md); display: grid; gap: var(--space-sm); overflow: hidden; transition: opacity var(--transition-fast), transform var(--transition-fast); }

.library-card.is-dragging {
  opacity: 0.4;
  transform: scale(0.98);
}
.library-card__preview { aspect-ratio: 4/5; background: var(--bg-accent-soft); display: grid; place-items: center; overflow: hidden; }
.library-card__preview-loading { border: 2px solid var(--border-subtle); border-top-color: var(--text-secondary); border-radius: 50%; height: 1.5rem; width: 1.5rem; animation: spin 0.7s linear infinite; }
.library-card__preview-loading::after { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.library-card__img { display: block; height: 100%; object-fit: cover; width: 100%; opacity: 0; animation: library-img-reveal 2s ease 1s forwards; }
@keyframes library-img-reveal { to { opacity: 1; } }
.library-card__header { align-items: center; display: flex; gap: var(--space-sm); justify-content: space-between; padding: 0 var(--space-md); }
.library-card__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-card__field { display: grid; gap: var(--space-2xs); padding: 0 var(--space-md); }
.library-card__actions { display: flex; gap: var(--space-xs); padding: 0 var(--space-md) var(--space-md); }

.btn-icon {
  display: inline-flex;
  margin-right: var(--space-2xs);
  vertical-align: middle;
}
.btn-icon svg { height: var(--icon-md); width: var(--icon-md); }

/* ──────────────────────────────────────────────
   Library Configure Modal
   Shell: flex column, sticky header, split body
   Left : 4:5 framed preview + reset
   Right: scrollable controls with sectioned form
   ────────────────────────────────────────────── */

/* FIXED height — not max-height — so opening/closing accordions never changes
   the modal's outer dimensions. The flex children absorb the change internally
   via the accordion scroll mechanism. */
.library-modal__dialog {
  display: flex;
  flex-direction: column;
  height: min(92vh, 60rem);
  overflow: hidden;
  padding: 0;
}

/* ── Header ───────────────────────────────────── */

.library-modal__header {
  align-items: center;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  flex-shrink: 0;
  gap: var(--space-md);
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
}

.library-modal__header-main {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-sm);
  min-width: 0;
}

.library-modal__header-actions {
  display: flex;
  flex-shrink: 0;
  gap: var(--space-sm);
}

.library-modal__eyebrow {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.library-modal__title {
  color: var(--text-primary);
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-modal__id-pill {
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
}

/* ── Split body ───────────────────────────────── */

/* Fills all remaining vertical space between the header and the dialog edge.
   Without `flex: 1 1 auto` + `min-height: 0` the grid would take its intrinsic
   (content) height and the right-column accordion would never scroll.
   `grid-template-rows: minmax(0, 1fr)` makes the grid cells resolve to a
   definite height so their flex-column children (preview / controls) can
   themselves compute a bounded height and enable internal scroll. */
.library-modal__split {
  display: grid;
  flex: 1 1 auto;
  gap: 0;
  grid-template-columns: minmax(0, 5fr) minmax(0, 7fr);
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

/* ── Preview column (left) ─────────────────────── */

.library-modal__preview-col {
  align-items: center;
  background: var(--bg-card-soft);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  justify-content: center;
  min-height: 0;
  padding: var(--space-lg);
}

/* The 4:5 frame is fixed — it never changes ratio. Background is pure black
   so any sliver the canvas doesn't cover reads as intentional dark framing
   rather than a white border. */
.library-modal__preview-frame {
  align-items: center;
  aspect-ratio: 4 / 5;
  background: #000;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  display: flex;
  justify-content: center;
  max-height: calc(92vh - 12rem);
  max-width: 100%;
  overflow: hidden;
  position: relative;
  width: 100%;
}

.library-modal__preview-frame--loading::after {
  animation: spin 0.7s linear infinite;
  border: 2px solid var(--border-subtle);
  border-radius: 50%;
  border-top-color: var(--text-secondary);
  content: '';
  height: 1.5rem;
  position: absolute;
  width: 1.5rem;
}

/* The canvas inside the frame is sized by JS to the template's native ratio,
   fitted inside the 4:5 box so it's always centered with matching framing.
   Black background = no visible seam against the frame. */
.library-modal__preview-canvas {
  background: #000;
  overflow: hidden;
  position: relative;
}

/* Let the Creatomate SDK manage its own iframe sizing (it sets width/height
   attributes to 100% and toggles display:none/"" around setSource). We only
   kill the inline whitespace gap and any UA border. */
.library-modal__preview-canvas iframe {
  border: 0;
  vertical-align: top;
}

.library-modal__reset {
  align-self: center;
}

/* ── Controls column (right) ───────────────────── */

/* The controls column itself never scrolls — scroll happens inside the open
   accordion body. Both accordion heads stay pinned at all times so the user
   can always switch between Dynamic / Rules. */
.library-modal__controls {
  background: var(--bg-card);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  min-height: 0;
  padding: var(--space-lg);
}

/* Sections use native <details> for mutually exclusive accordion behavior
   (name="library-modal-accordion" on Chrome 120+ / Safari 17.2+). A tiny JS
   fallback ensures exclusivity on older browsers.

   Closed: natural summary height.
   Open:   flex:1 so it fills remaining vertical space in the controls column,
           with its body scrolling internally. */
.library-modal__section {
  background: var(--bg-card-soft);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  display: block;
  flex-shrink: 0;
  min-height: 0;
  overflow: hidden;
  transition: border-color var(--transition-fast);
}

/* When open, the section becomes a 2-row grid: sticky summary + scrollable body.
   `grid-template-rows: auto minmax(0, 1fr)` gives the body a definite row that
   can shrink to zero, which makes `overflow-y: auto` on the body actually
   engage when content exceeds the available space. `flex: 1 1 auto` on the
   section itself lets it absorb the remaining vertical space in the controls
   column, and `min-height: 0` breaks the default flex min-content floor. */
.library-modal__section[open] {
  border-color: var(--border-strong);
  display: grid;
  flex: 1 1 auto;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
}

.library-modal__section-head {
  align-items: center;
  cursor: pointer;
  display: flex;
  flex-shrink: 0;
  gap: var(--space-sm);
  list-style: none;
  padding: var(--space-md);
  transition: background-color var(--transition-fast);
  user-select: none;
}

.library-modal__section-head::-webkit-details-marker { display: none; }
.library-modal__section-head::marker { content: ''; }

.library-modal__section-head:hover {
  background: var(--bg-hover);
}

.library-modal__section[open] > .library-modal__section-head {
  border-bottom: 1px solid var(--border-subtle);
}

.library-modal__section-icon {
  align-items: center;
  color: var(--text-muted);
  display: inline-flex;
  justify-content: center;
  flex-shrink: 0;
}

.library-modal__section-icon svg { height: 16px; width: 16px; }

.library-modal__section-title {
  color: var(--text-primary);
  flex: 1;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  margin: 0;
  text-transform: uppercase;
}

.library-modal__section-count {
  background: var(--bg-accent-soft);
  border-radius: var(--radius-pill);
  color: var(--text-secondary);
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  min-width: 1.5rem;
  padding: 0.125rem var(--space-xs);
  text-align: center;
}

.library-modal__section-count:empty { display: none; }

.library-modal__section-chevron {
  align-items: center;
  color: var(--text-muted);
  display: inline-flex;
  flex-shrink: 0;
  justify-content: center;
  transition: transform var(--transition-base);
}

.library-modal__section[open] > .library-modal__section-head .library-modal__section-chevron {
  transform: rotate(180deg);
}

.library-modal__section-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-md);
}

/* ── Rule fields (top section) ─────────────────── */

.library-modal__field {
  display: grid;
  gap: var(--space-2xs);
}

.library-modal__field > .settings-form__hint {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* Per-text-field character constraints group. Rendered as a small titled
   block so the writer visually distinguishes the global rules (role,
   description) from the per-field character budgets. */
.library-modal__constraints-group {
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding-top: var(--space-md);
}

.library-modal__constraints-title {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ── Dynamic element cards ─────────────────────── */

/* An element card is a <details>, so it inherits native expand/collapse.
   Closed: only the summary is visible. Open: body reveals linked property
   rows (text content + its color, for example). The border highlights the
   active element so long lists stay scannable. */
.library-modal__element {
  background: var(--bg-card-soft);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  overflow: hidden;
  transition: border-color var(--transition-fast);
}

.library-modal__element:hover {
  border-color: var(--border-strong);
}

.library-modal__element[open] {
  border-color: var(--border-strong);
}

.library-modal__element-head {
  align-items: center;
  background: var(--bg-card);
  cursor: pointer;
  display: flex;
  gap: var(--space-sm);
  list-style: none;
  padding: var(--space-sm) var(--space-md);
  user-select: none;
}

.library-modal__element-head::-webkit-details-marker { display: none; }
.library-modal__element-head::marker { content: ''; }

.library-modal__element[open] > .library-modal__element-head {
  border-bottom: 1px solid var(--border-subtle);
}

.library-modal__element-chevron {
  align-items: center;
  color: var(--text-muted);
  display: inline-flex;
  flex-shrink: 0;
  justify-content: center;
  margin-left: auto;
  transition: transform var(--transition-base);
}

.library-modal__element-chevron svg { height: 14px; width: 14px; }

.library-modal__element[open] > .library-modal__element-head .library-modal__element-chevron {
  transform: rotate(180deg);
}

.library-modal__element-icon {
  align-items: center;
  background: var(--bg-accent-soft);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  display: inline-flex;
  flex-shrink: 0;
  height: 1.75rem;
  justify-content: center;
  width: 1.75rem;
}

.library-modal__element-icon svg { height: 14px; width: 14px; }

.library-modal__element-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.library-modal__element-name {
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-modal__element-type {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.library-modal__element-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-md);
}

.library-modal__prop {
  display: grid;
  gap: var(--space-2xs);
}

.library-modal__prop > .settings-form__hint {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* Text field with live character counter + send-to-rules action */

.library-modal__text-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2xs);
  position: relative;
}

.library-modal__text-meta {
  align-items: center;
  display: flex;
  gap: var(--space-sm);
  justify-content: space-between;
}

.library-modal__send-to-rules {
  align-items: center;
  background: transparent;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  display: inline-flex;
  font-family: var(--font-sans);
  font-size: var(--text-2xs);
  font-weight: 600;
  gap: var(--space-2xs);
  letter-spacing: 0.04em;
  padding: var(--space-2xs) var(--space-xs);
  text-transform: uppercase;
  transition: background-color var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.library-modal__send-to-rules svg {
  height: 12px;
  width: 12px;
}

.library-modal__send-to-rules:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.library-modal__char-count {
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ── Media field (URL input + file picker button) ─ */

/* Image / video / audio import slot — two independent sub-slots laid out
   as a grid so they stay visually harmonized:
     1. URL slot  → <input type="url"> + trailing ✕
     2. Upload slot → <label> wrapping a hidden <input type="file">, acting
        as one big clickable zone. Its entire surface triggers the native
        file dialog; the ✕ button inside stops propagation to delete just
        the upload. */
.library-modal__media-field {
  display: grid;
  gap: var(--space-xs);
  grid-template-columns: 1fr 1fr;
}

.library-modal__media-slot {
  align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  display: flex;
  gap: var(--space-2xs);
  min-width: 0;
  padding: 0 var(--space-2xs) 0 0;
  transition: border-color var(--transition-fast);
}

.library-modal__media-slot--url > input[type="url"] {
  background: transparent;
  border: none;
  color: var(--text-primary);
  flex: 1;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  min-width: 0;
  outline: none;
  padding: var(--space-2xs) var(--space-sm);
}

.library-modal__media-slot--url:focus-within {
  border-color: var(--border-strong);
}

.library-modal__media-slot--upload {
  cursor: pointer;
  padding: var(--space-2xs) var(--space-2xs) var(--space-2xs) var(--space-sm);
  user-select: none;
}

.library-modal__media-slot--upload:hover {
  border-color: var(--border-strong);
}

.library-modal__media-slot--upload > svg {
  color: var(--text-muted);
  flex-shrink: 0;
  height: 14px;
  width: 14px;
}

.library-modal__media-upload-name {
  color: var(--text-muted);
  flex: 1;
  font-size: var(--text-xs);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-modal__media-slot--upload.is-filled .library-modal__media-upload-name {
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.library-modal__icon-btn {
  align-items: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  flex-shrink: 0;
  height: 22px;
  justify-content: center;
  padding: 0;
  transition: background-color var(--transition-fast), color var(--transition-fast);
  width: 22px;
}

.library-modal__icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.library-modal__icon-btn svg {
  height: 14px;
  width: 14px;
}

/* Hidden attribute must win over the explicit `display: flex` on slots
   and `display: inline-flex` on the icon buttons, otherwise removing a
   slot via `.hidden = true` would leave it visible. */
.library-modal__media-slot[hidden],
.library-modal__icon-btn[hidden] {
  display: none !important;
}

/* ── Color pair (swatch button + hex input) ────── */

.library-modal__color-pair {
  align-items: center;
  display: flex;
  gap: var(--space-xs);
}

/* Rounded-square swatch button. The color picker module adds its own
   .color-picker-trigger class + inline --swatch background on top. */
.library-modal__color-swatch {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex-shrink: 0;
  height: 2.25rem;
  padding: 0;
  width: 2.25rem;
}

.library-modal__color-hex {
  flex: 1;
  font-variant-numeric: tabular-nums;
  text-transform: uppercase;
}

/* ── Hints ─────────────────────────────────────── */

.library-modal__hint {
  color: var(--text-muted);
  font-size: var(--text-xs);
  margin: 0;
  text-align: center;
}

.library-modal__hint--error { color: var(--vibrance-danger); }

/* ── Responsive collapse ───────────────────────── */

@media (max-width: 899px) {
  .library-modal__dialog { max-height: 95vh; }
  .library-modal__split {
    grid-template-columns: 1fr;
    overflow-y: auto;
  }
  .library-modal__preview-col {
    border-bottom: 1px solid var(--border-subtle);
    border-right: none;
  }
  .library-modal__preview-frame { max-height: 32rem; }
}
