/*!
 * Adeeb Al-Silwy - Mermaid diagram presentation (v3)
 *
 * Layout modes (set by adeeb-diagram.js):
 *   data-mode="fluid"  -> SVG scales with the container (mobile -> 2xl screens)
 *   data-mode="scroll" -> SVG keeps its natural size and the viewport scrolls
 *                         horizontally, so labels stay readable on phones
 *
 * Structure built by the JS:
 *   <div class="diagram-mermaid" data-mode="...">
 *     <div class="diagram-mermaid__bar">caption + toolbar</div>
 *     <div class="diagram-mermaid__viewport"><svg/></div>
 *     <div class="diagram-mermaid__hint">scroll hint</div>
 *   </div>
 */

/* ---------- frame ---------- */
pre.diagram-mermaid,
div.diagram-mermaid {
  position: relative;
  display: flex;
  flex-direction: column;
  margin: 1.5em 0;
  border: 1px solid #d7e4f8;
  border-inline-start: 5px solid #3b74c4;
  border-radius: 16px;
  background: #ffffff;
  overflow: hidden;
  direction: ltr;
  unicode-bidi: isolate;
  text-align: left;
  box-shadow: 0 10px 30px rgba(59, 116, 196, 0.1);
  opacity: 0;
  transition: opacity 200ms ease-out;
}

/* pre flash-protection: hidden until the JS swaps it for a div */
pre.diagram-mermaid {
  padding: 14px 16px;
  color: #334155;
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.65;
  white-space: pre;
  visibility: hidden;
}

/* ---------- header strip: caption + controls ---------- */
.diagram-mermaid__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 8px;
  min-height: 42px;
  padding: 6px 10px;
  background: #f8fafc;
  border-bottom: 1px dashed #e2e8f0;
}

.diagram-mermaid__caption {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  font-weight: 700;
  color: #64748b;
  letter-spacing: 0.02em;
  user-select: none;
  -webkit-user-select: none;
}

.diagram-mermaid__caption::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #3b74c4;
  box-shadow: 0 0 0 3px rgba(59, 116, 196, 0.15);
  flex: none;
}

/* ---------- viewport: the scroller that holds the SVG ---------- */
.diagram-mermaid__viewport {
  flex: 1 1 auto;
  min-width: 0;
  padding: 16px;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  max-height: 78vh;
  scrollbar-width: thin;
  scrollbar-color: #c3d3ef transparent;
}

.diagram-mermaid__viewport::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
.diagram-mermaid__viewport::-webkit-scrollbar-thumb {
  background: #c3d3ef;
  border-radius: 99px;
}
.diagram-mermaid__viewport::-webkit-scrollbar-track {
  background: transparent;
}
.diagram-mermaid__viewport.is-dragging {
  cursor: grabbing;
  user-select: none;
  -webkit-user-select: none;
}

.diagram-mermaid__viewport svg {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
}

.diagram-mermaid[data-mode="scroll"] .diagram-mermaid__viewport svg {
  max-width: none;
  margin: 0;
}

/* all SVG text inherits the page font (Arabic + Latin) */
.diagram-mermaid svg,
.diagram-mermaid svg text,
.diagram-mermaid .nodeLabel,
.diagram-mermaid .edgeLabel {
  font-family: inherit;
}

/* keep native touch scrolling pleasant on phones:
   fluid diagrams -> vertical page scroll, horizontal goes to the zoom logic
   scroll diagrams -> full native scrolling (both axes)                    */
@media (hover: none) and (pointer: coarse) {
  .diagram-mermaid[data-mode="fluid"] .diagram-mermaid__viewport {
    touch-action: pan-y pinch-zoom;
  }
}

/* ---------- "scroll to see more" affordance (scroll mode) ---------- */
.diagram-mermaid[data-mode="scroll"][data-scrollable]::after {
  content: "";
  position: absolute;
  top: 43px;
  right: 0;
  bottom: 0;
  width: 64px;
  z-index: 1;
  pointer-events: none;
  background: linear-gradient(to left, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0));
  transition: opacity 0.25s ease;
}

.diagram-mermaid[data-mode="scroll"][data-scrollable][data-scrolled-end]::after {
  opacity: 0;
}

.diagram-mermaid__hint {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  z-index: 3;
  pointer-events: none;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.78);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.25s ease;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.diagram-mermaid[data-scrollable] .diagram-mermaid__hint {
  opacity: 1;
}
.diagram-mermaid[data-scrolled-end] .diagram-mermaid__hint {
  opacity: 0;
}

/* ---------- error state ---------- */
.diagram-mermaid .diagram-mermaid__error {
  margin: 16px;
  padding: 14px 16px;
  border-radius: 10px;
  background: #fdecec;
  border: 1px solid #f5c2c2;
  color: #7f1d1d;
  font-size: 0.92rem;
}
.diagram-mermaid .diagram-mermaid__error-text {
  font-weight: 700;
}

/* ---------- fullscreen ---------- */
.diagram-mermaid:fullscreen {
  border: none;
  border-radius: 0;
  background: #ffffff;
}
.diagram-mermaid:fullscreen .diagram-mermaid__viewport {
  max-height: none;
  padding: 24px;
}

/* ---------- dark mode ---------- */
.dark-mode div.diagram-mermaid {
  background: #101a30;
  border-color: #26324e;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.dark-mode .diagram-mermaid__bar {
  background: #0d1526;
  border-bottom-color: #26324e;
}
.dark-mode .diagram-mermaid__caption {
  color: #93a4c3;
}
.dark-mode .diagram-mermaid__viewport {
  scrollbar-color: #33415c transparent;
}
.dark-mode .diagram-mermaid__viewport::-webkit-scrollbar-thumb {
  background: #33415c;
}
.dark-mode .diagram-mermaid[data-mode="scroll"][data-scrollable]::after {
  background: linear-gradient(to left, rgba(16, 26, 48, 0.95), rgba(16, 26, 48, 0));
}
.dark-mode .diagram-mermaid__hint {
  background: rgba(59, 116, 196, 0.9);
}
.dark-mode .diagram-mermaid .diagram-mermaid__error {
  background: #2a1414;
  border-color: #5c2b2b;
  color: #fecaca;
}
.dark-mode .diagram-mermaid:fullscreen {
  background: #101a30;
}
.dark-mode pre.diagram-mermaid {
  color: #cbd5e1;
}

/* ---------- breakpoints ---------- */
@media (max-width: 575.98px) {
  div.diagram-mermaid {
    border-radius: 14px;
    margin: 1.2em 0;
  }
  .diagram-mermaid__bar {
    min-height: 38px;
    padding: 5px 8px;
  }
  .diagram-mermaid__caption {
    font-size: 11px;
  }
  .diagram-mermaid__viewport {
    padding: 10px;
    max-height: 70vh;
  }
  .diagram-mermaid__hint {
    font-size: 10.5px;
    padding: 3px 10px;
    bottom: 12px;
  }
  pre.diagram-mermaid {
    padding: 10px;
    font-size: 11.5px;
  }
  .diagram-mermaid[data-mode="scroll"][data-scrollable]::after {
    width: 48px;
  }
}

@media (min-width: 1536px) {
  .diagram-mermaid__viewport {
    padding: 24px;
  }
}
