/* ========================================
   Media stacks / lightbox / post anchors
   2026-10-16
======================================== */

.markdown-body h1[id],
.markdown-body h2[id],
.markdown-body h3[id],
.markdown-body h4[id],
.markdown-body h5[id],
.markdown-body h6[id],
.post-anchor-marker {
  scroll-margin-top: 88px;
}

.markdown-body h1[id],
.markdown-body h2[id],
.markdown-body h3[id],
.markdown-body h4[id],
.markdown-body h5[id],
.markdown-body h6[id] {
  position: relative;
}

.heading-permalink {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 0.45em;
  color: var(--faint);
  font-size: 0.72em;
  font-weight: 600;
  text-decoration: none !important;
  opacity: 0;
  transform: translateY(-0.03em);
  transition: opacity 120ms ease, color 120ms ease;
}

.markdown-body h1[id]:hover > .heading-permalink,
.markdown-body h2[id]:hover > .heading-permalink,
.markdown-body h3[id]:hover > .heading-permalink,
.markdown-body h4[id]:hover > .heading-permalink,
.markdown-body h5[id]:hover > .heading-permalink,
.markdown-body h6[id]:hover > .heading-permalink,
.heading-permalink:focus-visible {
  color: var(--accent);
  opacity: 1;
}

.post-anchor-marker {
  display: inline-block;
  width: 0;
  height: 0;
  overflow: hidden;
  vertical-align: top;
}

.post-image-stack {
  display: block;
  width: var(--stack-width, 100%);
  max-width: 100%;
  margin: 1.5rem auto;
}

.post-image-stack-stage {
  position: relative;
  width: 100%;
  min-width: 0;
  touch-action: pan-y;
}

.post-image-stack .post-image-stack-item {
  display: none;
  width: 100%;
  max-width: 100%;
  margin: 0;
  flex: none;
}

.post-image-stack .post-image-stack-item.is-active {
  display: block;
}

.post-image-stack .post-image-stack-item > img {
  display: block;
  width: 100%;
  height: auto;
}

.post-image-stack-nav {
  position: absolute;
  top: 50%;
  z-index: 6;
  display: grid;
  place-items: center;
  width: 38px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: rgba(15, 15, 15, 0.48);
  color: #fff;
  font: 400 30px/1 system-ui, sans-serif;
  cursor: pointer;
  opacity: 0.72;
  transform: translateY(-50%);
  transition: opacity 120ms ease, background 120ms ease;
}

.post-image-stack-nav:hover,
.post-image-stack-nav:focus-visible {
  background: rgba(15, 15, 15, 0.7);
  opacity: 1;
}

.post-image-stack-nav.is-prev {
  left: 10px;
}

.post-image-stack-nav.is-next {
  right: 10px;
}

.post-image-stack-count {
  position: absolute;
  right: 12px;
  bottom: 10px;
  z-index: 6;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(15, 15, 15, 0.58);
  color: #fff;
  font: 600 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  pointer-events: none;
}

.preview-editable .post-image-stack {
  position: relative;
}

.preview-editable .post-image-stack[draggable="true"] {
  cursor: grab;
}

.preview-editable .post-image-stack.is-dragging {
  opacity: 0.48;
}

.preview-editable .post-image-node.is-multi-selected {
  outline-style: dashed;
}

.image-zoom-button {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 9;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 7px;
  background: rgba(15, 15, 15, 0.58);
  color: #fff;
  cursor: pointer;
  opacity: 0;
  transition: opacity 120ms ease, background 120ms ease;
}

.preview-editable .post-image-node:hover > .image-zoom-button,
.image-zoom-button:focus-visible {
  opacity: 1;
}

.markdown-body img.is-lightbox-target {
  cursor: zoom-in;
}

.media-lightbox {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  grid-template-columns: minmax(48px, 84px) minmax(0, 1fr) minmax(48px, 84px);
  grid-template-rows: minmax(0, 1fr);
  align-items: center;
  justify-items: center;
  padding: 18px;
  background: rgba(8, 8, 8, 0.94);
  color: #fff;
  overscroll-behavior: contain;
}

.media-lightbox[hidden] {
  display: none;
}

.media-lightbox-stage {
  grid-column: 2;
  grid-row: 1;
  width: 100%;
  height: calc(100dvh - 36px);
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  cursor: zoom-in;
}

.media-lightbox.is-zoomed .media-lightbox-stage {
  cursor: grab;
}

.media-lightbox-image {
  display: block;
  max-width: min(100%, 1600px);
  max-height: calc(100dvh - 72px);
  width: auto;
  height: auto;
  object-fit: contain;
  transform-origin: center center;
  will-change: transform;
  user-select: none;
  -webkit-user-drag: none;
}

.media-lightbox-close,
.media-lightbox-nav {
  z-index: 3;
  border: 0;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
}

.media-lightbox-close {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  border-radius: 999px;
  font-size: 28px;
  line-height: 1;
}

.media-lightbox-nav {
  width: 48px;
  height: 64px;
  border-radius: 999px;
  font-size: 38px;
  line-height: 1;
}

.media-lightbox-nav.is-prev {
  grid-column: 1;
}

.media-lightbox-nav.is-next {
  grid-column: 3;
}

.media-lightbox-count {
  position: absolute;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: 50%;
  z-index: 3;
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.36);
  font: 600 12px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  transform: translateX(-50%);
}

html.media-lightbox-open {
  overflow: hidden;
  overscroll-behavior: none;
}

html.media-lightbox-open body {
  overflow: hidden;
  overscroll-behavior: none;
}

.stack-editor-bar {
  padding: 10px 12px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.stack-editor-bar[hidden] {
  display: none;
}

.stack-editor-bar-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
}

.stack-editor-thumbnails {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 2px 6px;
  scrollbar-width: thin;
}

.stack-editor-thumb {
  position: relative;
  flex: 0 0 72px;
  width: 72px;
  height: 58px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg);
}

.stack-editor-thumb.is-current {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.stack-editor-thumb.is-dragging {
  opacity: 0.45;
}

.stack-editor-thumb.is-drop-target {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.stack-editor-thumb-select {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: inherit;
  overflow: hidden;
  background: transparent;
  cursor: pointer;
}

.stack-editor-thumb-select img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.stack-editor-thumb-number {
  position: absolute;
  left: 4px;
  bottom: 4px;
  padding: 2px 5px;
  border-radius: 999px;
  background: rgba(15, 15, 15, 0.62);
  color: #fff;
  font-size: 10px;
  line-height: 1.2;
  pointer-events: none;
}

.stack-editor-thumb-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  z-index: 2;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 15px;
  line-height: 18px;
}

.cm-image-stack-widget::after {
  content: "stack";
  margin-left: 0.45rem;
  color: var(--faint);
  font-size: 0.68rem;
}

.cm-anchor-widget {
  display: inline-flex;
  align-items: center;
  max-width: min(320px, 80vw);
  padding: 0.12rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font: 600 0.74rem/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  vertical-align: middle;
}

.editor-link-modal {
  width: min(520px, calc(100vw - 32px));
}

.editor-link-section {
  display: grid;
  gap: 7px;
  margin-top: 16px;
}

.editor-link-section > label,
.editor-anchor-modal > label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.editor-link-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.editor-link-modal input,
.editor-link-modal select {
  width: 100%;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: 0;
  background: var(--bg);
  color: var(--text);
}

.editor-link-modal input:focus,
.editor-link-modal select:focus {
  border-color: var(--accent);
}

.editor-link-help {
  margin: 0;
  color: var(--faint);
  font-size: 11px;
  line-height: 1.55;
}

.editor-anchor-modal {
  display: grid;
  gap: 10px;
}

@media (max-width: 700px) {
  .heading-permalink {
    opacity: 0.42;
  }

  .post-image-stack-nav {
    width: 42px;
    height: 54px;
    opacity: 0.82;
  }

  .post-image-stack-nav.is-prev {
    left: 6px;
  }

  .post-image-stack-nav.is-next {
    right: 6px;
  }

  .image-zoom-button {
    width: 36px;
    height: 36px;
    opacity: 0.82;
  }

  .media-lightbox {
    grid-template-columns: 1fr;
    padding: 0;
  }

  .media-lightbox-stage {
    grid-column: 1;
    width: 100vw;
    height: 100dvh;
    padding: max(52px, env(safe-area-inset-top)) 0 max(46px, env(safe-area-inset-bottom));
  }

  .media-lightbox-image {
    max-width: 100vw;
    max-height: calc(100dvh - 98px);
  }

  .media-lightbox-nav {
    position: absolute;
    top: 50%;
    width: 42px;
    height: 56px;
    transform: translateY(-50%);
    background: rgba(0, 0, 0, 0.32);
  }

  .media-lightbox-nav.is-prev {
    left: 4px;
  }

  .media-lightbox-nav.is-next {
    right: 4px;
  }

  .editor-link-row {
    grid-template-columns: 1fr;
  }

  .stack-editor-thumb {
    flex-basis: 66px;
    width: 66px;
    height: 54px;
  }
}

.markdown-body p:has(> .post-anchor-marker:only-child) {
  height: 0;
  margin: 0;
  padding: 0;
}

.preview-editable .post-image-node.drop-stack {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
  box-shadow: inset 0 0 0 999px color-mix(in srgb, var(--accent) 10%, transparent);
}
