.mermaid-figure {
  position: relative;
  margin: 1.5rem 0;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--card-bg);
  overflow: hidden;
  width: 100%;
}

.mermaid-toolbar {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 2;
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 6px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.12);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

.mermaid-figure:hover .mermaid-toolbar,
.mermaid-figure:focus-within .mermaid-toolbar,
.mermaid-figure--lightbox .mermaid-toolbar {
  opacity: 1;
  pointer-events: auto;
}

.mermaid-stage {
  overflow-x: hidden;
  overflow-y: auto;
  width: 100%;
  min-height: 12rem;
  max-height: min(80vh, 900px);
  cursor: grab;
  padding: 1rem;
  touch-action: none;
  background: transparent;
}

.mermaid-figure--lightbox .mermaid-stage,
.mermaid-lightbox-body .mermaid-stage {
  min-height: 0;
  max-height: none;
  flex: 1 1 auto;
  height: 100%;
}

.mermaid-stage.is-dragging {
  cursor: grabbing;
}

.mermaid-inner {
  display: block;
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  /* translate only — never scale() here (causes SVG blur) */
  transform-origin: 0 0;
}

.mermaid-figure--lightbox .mermaid-inner {
  width: max-content;
  max-width: none;
}

.mermaid-inner pre.mermaid,
.mermaid-inner div.mermaid {
  display: block !important;
  width: 100% !important;
  max-width: 100% !important;
  margin: 0 !important;
  background: transparent !important;
  border: none !important;
  padding: 0 !important;
  overflow: visible !important;
}

.mermaid-figure--lightbox .mermaid-inner pre.mermaid,
.mermaid-figure--lightbox .mermaid-inner div.mermaid {
  width: max-content !important;
  max-width: none !important;
}

.mermaid-figure.is-width-fit .mermaid-inner svg {
  width: 100% !important;
  height: auto !important;
  max-width: 100% !important;
}

.mermaid-inner svg {
  display: block !important;
  max-height: none !important;
  shape-rendering: geometricPrecision;
  text-rendering: geometricPrecision;
}

.mermaid-figure:not(.is-width-fit) .mermaid-inner svg {
  max-width: none !important;
}

.post-content .mermaid-figure.is-width-fit .mermaid-inner svg,
.post-content .mermaid-figure.is-width-fit pre.mermaid svg {
  width: 100% !important;
  max-width: 100% !important;
}

.post-content .mermaid-figure:not(.is-width-fit) .mermaid-inner svg,
.post-content .mermaid-figure:not(.is-width-fit) pre.mermaid svg {
  max-width: none !important;
}

.mermaid-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 0;
}

.mermaid-btn:hover {
  background: var(--btn-border-color);
}

.mermaid-hint {
  padding: 0.35rem 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted-color);
  border-top: 1px solid var(--border-color);
}

/* Full viewport overlay */
.mermaid-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  padding: 0;
  margin: 0;
  width: 100vw;
  height: 100vh;
  width: 100dvw;
  height: 100dvh;
}

.mermaid-lightbox[hidden] {
  display: none !important;
}

.mermaid-lightbox-dialog {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  margin: 0;
  border: none;
  border-radius: 0;
  background: var(--card-bg);
  box-shadow: none;
}

.mermaid-lightbox-dialog:fullscreen {
  width: 100%;
  height: 100%;
  background: var(--card-bg);
}

.mermaid-lightbox-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border-color);
}

.mermaid-lightbox-actions {
  display: flex;
  gap: 4px;
  align-items: center;
}

.mermaid-lightbox-title {
  font-size: 0.875rem;
  color: var(--text-muted-color);
}

.mermaid-lightbox-close {
  width: 32px;
  height: 32px;
  font-size: 20px;
}

.mermaid-lightbox-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0.5rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.mermaid-lightbox-body .mermaid-figure {
  margin: 0;
  border: none;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

body.mermaid-lightbox-open {
  overflow: hidden;
}
