/**
 * image-gallery.css — `::gallery` block: the interactive carousel in the
 * editor (`.cm-gallery-*`) and the static stacked output used by the read
 * renderer / print (`.gallery-*`).
 *
 * Everything is built from the `main.css` tokens so the gallery sits in the same
 * paper palette as the link previews and paste embeds. `--gallery-height` is the
 * one per-block knob: the viewport is a fixed-height container and the slides
 * are fitted into it with `object-fit`, exactly as the block syntax promises.
 *
 * The static `.gallery-static*` rules below are only ever reached on the read and
 * print surfaces. Inside the editor the block is replaced by the `.cm-gallery`
 * widget, so the print overrides in `print.css` that flatten `.cm-gallery` never
 * have to compete with them.
 */

/* ===== Editor carousel =================================================== */

.cm-gallery {
  position: relative;
  display: block;
  box-sizing: border-box;
  margin: 0.6em 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius, 4px);
  background: var(--color-surface);
  overflow: hidden;
  font-family: var(--font-sans);
  font-size: var(--fontsize-ui);
  color: var(--color-text);
}

/* With a `bg=` attribute the container colour is the chosen one, so the viewport
 * must not paint its own tint over it. */
.cm-gallery[data-custom-bg] .cm-gallery-viewport {
  background: transparent;
}

.cm-gallery-viewport {
  position: relative;
  width: 100%;
  height: var(--gallery-height, 320px);
  background: var(--color-bg);
  overflow: hidden;
}

/* Only the active slide is displayed; the others stay mounted so switching
   slides is instant and lazy-loaded images keep their decoded bitmap. */
.cm-gallery-slide {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 10px;
  box-sizing: border-box;
}

.cm-gallery-slide.is-active {
  display: flex;
}

.cm-gallery-slide img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  background: transparent;
}

/* Placeholder tint while a slide is still decoding. */
.cm-gallery-slide img[data-loading='true'] {
  background: linear-gradient(90deg, var(--color-surface-2) 25%, var(--color-bg) 50%, var(--color-surface-2) 75%);
  background-size: 200% 100%;
  animation: cm-gallery-shimmer 1.4s linear infinite;
}

@keyframes cm-gallery-shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

/* ===== Arrows ============================================================ */

.cm-gallery-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 50%;
/*   background: var(--color-surface-2); */
  background: rgba(103, 109, 122, 0.08);
  backdrop-filter: blur(10px);
  color: var(--color-text);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition-fast), background var(--transition-fast);
}

.cm-gallery:hover .cm-gallery-arrow,
.cm-gallery:focus-within .cm-gallery-arrow {
  opacity: 1;
}

.cm-gallery-arrow:hover {
  background: var(--color-border);
}

.cm-gallery-arrow:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
}

.cm-gallery-arrow--prev { left: 8px; }
.cm-gallery-arrow--next { right: 8px; }

/* A single visible slide has nothing to page through. */
.cm-gallery[data-single] .cm-gallery-arrow,
.cm-gallery[data-single] .cm-gallery-pagination {
  display: none;
}

/* ===== Caption =========================================================== */

.cm-gallery-caption {
  padding: 0px 12px 8px;
  /* border-top: 1px solid var(--color-border); */
  /* background: #faf7f2(--color-surface); */
  color: var(--color-text);
  font-family: var(--font-serif);
  font-size: 0.9rem;
  font-style: italic;
  line-height: 1.45;
  text-align: center;
}

.cm-gallery-caption:empty {
  display: none;
}

/* ===== Pagination: dots ================================================== */

.cm-gallery-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 0 9px;
/*   background: var(--color-surface); */
}

.cm-gallery-dot {
  width: 5px;
  height: 5px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--color-border-2);
  cursor: pointer;
  transition: transform var(--transition-fast), background var(--transition-fast);
}

.cm-gallery-dot.is-active {
  background: var(--color-primary);
  transform: scale(1.1);
}

.cm-gallery-dot:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
}

/* ===== Pagination: thumbnails ============================================ */

.cm-gallery-pagination--thumbs {
  gap: 5px;
  overflow-x: auto;
  justify-content: flex-start;
  padding: 0 9px 9px;
  scrollbar-width: thin;
}

.cm-gallery-thumb {
  flex: 0 0 auto;
  width: 46px;
  height: 32px;
  padding: 0;
  object-fit: cover;
  border: 2px solid transparent;
  border-radius: var(--radius-sm, 2px);
  background: var(--color-bg);
  cursor: pointer;
  opacity: 0.55;
  transition: opacity var(--transition-fast), border-color var(--transition-fast);
}

.cm-gallery-thumb.is-active {
  border-color: var(--color-primary);
  opacity: 1;
}

.cm-gallery-thumb:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
}

/* ===== Manage trigger ==================================================== */

.cm-gallery-tools {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 3;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.cm-gallery:hover .cm-gallery-tools,
.cm-gallery:focus-within .cm-gallery-tools {
  opacity: 1;
}

.cm-gallery-tool {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface-2);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.cm-gallery-tool:hover {
  background: var(--color-border);
  color: var(--color-text);
}

.cm-gallery-tool:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
}

.cm-gallery-tool.is-active {
  background: var(--color-primary);
  color: var(--color-surface-2);
}

.cm-gallery-tool svg {
  width: 15px;
  height: 15px;
  pointer-events: none;
}

/* ===== Manage panel (overlay on #editor-content) ========================= */

.cm-gallery-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  width: min(360px, 100%);
  border-left: 1px solid var(--color-border);
  background: var(--color-surface-2);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.12);
  font-family: var(--font-sans);
  font-size: var(--fontsize-ui);
  color: var(--color-text);
}

.cm-gallery-panel-head {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--color-border);
}

.cm-gallery-panel-title {
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.cm-gallery-panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 10px;
  scrollbar-width: thin;
}

.cm-gallery-panel-empty {
  padding: 12px 0;
  color: var(--color-text-muted);
}

/* ===== Panel rows ======================================================== */

.cm-gallery-row {
  display: flex;
  align-items: flex-start;
  gap: 7px;
  margin-bottom: 6px;
  padding: 6px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--color-surface);
  cursor: grab;
  user-select: none;
}

.cm-gallery-row.is-dragging {
  opacity: 0.4;
}

.cm-gallery-row.is-drop-target {
  border-color: var(--color-primary);
}

.cm-gallery-row.is-disabled .cm-gallery-row-src {
  text-decoration: line-through;
  opacity: 0.6;
}

.cm-gallery-row-grip {
  flex: 0 0 auto;
  padding-top: 3px;
  color: var(--color-border-2);
}

.cm-gallery-row-grip svg {
  width: 14px;
  height: 14px;
  pointer-events: none;
}

.cm-gallery-row-thumb {
  flex: 0 0 auto;
  width: 46px;
  height: 34px;
  object-fit: cover;
  border-radius: var(--radius-sm, 2px);
  background: var(--color-bg);
}

.cm-gallery-row-main {
  flex: 1;
  min-width: 0;
}

.cm-gallery-row-src {
  margin-bottom: 4px;
  overflow: hidden;
  color: var(--color-text-muted);
  font-size: 0.7rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cm-gallery-row-caption,
.cm-gallery-row-src-input {
  width: 100%;
  box-sizing: border-box;
  padding: 4px 6px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm, 2px);
  background: var(--color-surface-2);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  line-height: 1.4;
  resize: vertical;
  transition: border-color var(--transition-fast);
}

.cm-gallery-row-caption:focus,
.cm-gallery-row-src-input:focus {
  outline: none;
  border-color: var(--color-primary);
}

/* The change-path input is collapsed until its row asks for it. */
.cm-gallery-row-src-input {
  display: none;
  margin-top: 4px;
}

/* ===== Block settings (pagination + background) ======================== */

.cm-gallery-settings {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--color-border);
}

/* ----- thumbnail pagination switch ----- */

.cm-gallery-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  user-select: none;
  color: var(--color-text);
  font-size: 0.78rem;
}

/* The checkbox stays focusable and screen-reader addressable; only its box is
 * replaced by the track below. */
.cm-gallery-toggle input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

.cm-gallery-toggle-track {
  position: relative;
  flex: 0 0 auto;
  width: 30px;
  height: 17px;
  border: 1px solid var(--color-border);
  border-radius: 9px;
  background: var(--color-bg);
  transition: background var(--transition-fast), border-color var(--transition-fast);
}

.cm-gallery-toggle-track::after {
  content: '';
  position: absolute;
  top: 1px;
  left: 1px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--color-text-muted);
  transition: transform var(--transition-fast), background var(--transition-fast);
}

.cm-gallery-toggle input:checked + .cm-gallery-toggle-track {
  border-color: var(--color-primary);
  background: var(--color-primary);
}

.cm-gallery-toggle input:checked + .cm-gallery-toggle-track::after {
  background: var(--color-surface-2);
  transform: translateX(13px);
}

.cm-gallery-toggle input:focus-visible + .cm-gallery-toggle-track {
  box-shadow: 0 0 0 2px var(--color-primary);
}

/* ----- background colour ----- */

.cm-gallery-colour {
  display: flex;
  align-items: flex-start;
  gap: 5px;
  color: var(--color-text-muted);
}

.cm-gallery-colour svg {
  width: 14px;
  height: 14px;
  margin-top: 4px;
  pointer-events: none;
}

/* The shared Radix grid dropped into `data-gallery-bg-picker` — the same two
 * swatch rows the segment/casing gear popover opens, so the picker never drifts
 * between the panel and the popover. The grid's own styles live in editor.css. */
.cm-gallery-colour .cm-fence-swatch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.cm-gallery-colour .cm-fence-swatch input[type="color"] {
  appearance: none;
  -webkit-appearance: none;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0px solid currentColor;
  border-radius: 3px;
  background: none;
  cursor: pointer;
}

.cm-gallery-colour .cm-fence-swatch input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.cm-gallery-colour .cm-fence-swatch input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 3px;
}

.cm-gallery-colour .cm-fence-alpha {
  width: 60px;
  height: 2px;
  margin: 5px;
  cursor: pointer;
  accent-color: currentColor;
}

.cm-gallery-colour-reset {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm, 2px);
  background: none;
  color: var(--color-text-muted);
  cursor: pointer;
}

.cm-gallery-colour-reset:hover:not(:disabled) {
  border-color: var(--color-border);
  color: var(--color-text);
}

.cm-gallery-colour-reset:disabled {
  opacity: 0.3;
  cursor: default;
}

.cm-gallery-colour-reset svg {
  width: 11px;
  height: 11px;
}

/* ----- remove the whole gallery ----- */

/* The colour control and the Remove button share the right half of the
 * settings bar, so the toggle keeps the left and the two of them sit
 * together on the right. */
.cm-gallery-settings-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* The way out of the gallery itself — the per-row trash only drops one image.
 * Its colours are the link preview's Remove, so the two read as one control. */
.cm-gallery-remove {
  padding: 4px 8px;
  border: 1px solid var(--color-danger-border, #d73a49);
  border-radius: var(--radius-sm, 2px);
  background: transparent;
  color: var(--color-danger, #b31d28);
  font: inherit;
  font-size: 0.78rem;
  line-height: 1.3;
  white-space: nowrap;
  cursor: pointer;
}

.cm-gallery-remove:hover {
  background: var(--color-danger-soft, rgba(215, 58, 73, 0.1));
}

.cm-gallery-remove:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}

/* ===== Add form ======================================================== */

/* Shaped like a row — a thumbnail beside stacked fields — so the two halves of
 * the panel read the same way. */
.cm-gallery-add {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--color-border);
}

.cm-gallery-add-head {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 6px;
  color: var(--color-text-muted);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.cm-gallery-add-head svg {
  width: 12px;
  height: 12px;
  pointer-events: none;
}

.cm-gallery-add-body {
  display: flex;
  align-items: flex-start;
  gap: 7px;
  padding: 6px;
  border: 2px dashed var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.cm-gallery-add-body:focus-within {
  border-style: solid;
  border-color: var(--color-primary);
}

.cm-gallery-add-thumb {
  flex: 0 0 auto;
  width: 46px;
  height: 34px;
  object-fit: cover;
  border-radius: var(--radius-sm, 2px);
  background: var(--color-bg);
}

/* No path typed yet: show the frame without asking the browser for a source. */
.cm-gallery-add-thumb.is-empty {
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  opacity: 0.5;
}

.cm-gallery-add-fields {
  flex: 1 1 auto;
  min-width: 0;
}

/* The add form's path field is always visible, unlike a row's, which reveals
 * its own behind the change-path button. */
.cm-gallery-add-input {
  display: block;
  margin-top: 4px;
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  font-size: 0.75rem;
}

.cm-gallery-add-btn {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  color: var(--color-success);
}

.cm-gallery-row.is-editing .cm-gallery-row-src-input {
  display: block;
}

.cm-gallery-row-actions {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  gap: 3px;
}

.cm-gallery-row-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm, 2px);
  background: var(--color-surface-2);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.cm-gallery-row-btn:hover {
  background: var(--color-border);
  color: var(--color-text);
}

.cm-gallery-row-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
}

.cm-gallery-row-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.cm-gallery-row-btn.is-on {
  color: var(--color-success);
}

.cm-gallery-row-btn.is-danger:hover {
  border-color: var(--color-danger);
  color: var(--color-danger);
}

.cm-gallery-row-btn svg {
  width: 13px;
  height: 13px;
  pointer-events: none;
}

/* ===== Read renderer / print (markdown-print) ============================ */

.gallery-static {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: 1em 0;
}

.gallery-static-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin: 0;
}

.gallery-static-item img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  object-fit: contain;
}

.gallery-static-item figcaption {
  color: var(--color-text-muted, #6b6257);
  font-family: var(--font-serif, Georgia, serif);
  font-size: 0.85rem;
  line-height: 1.45;
  text-align: center;
}

.gallery-static-item figcaption:empty {
  display: none;
}
