/*
 * Contexa docs diagram system (dg-).
 * One set of parts for every diagram: flow, outcomes, lanes, layers, messages, compare, cards.
 * Roles have fixed colors so a reader learns them once:
 *   request  — what comes in (the visitor's request)
 *   host     — the application that uses Contexa (its own code and Spring Security)
 *   contexa  — a Contexa component
 *   store    — where something is kept (decision store, database, vector store)
 *   model    — an AI model call
 *   allow · challenge · block · escalate · pending — the five decisions
 * Every part wraps; nothing has a fixed width, so the diagrams fit a 390px phone.
 */

:root {
  --dg-request: #64748B;
  --dg-host: #78716C;
  --dg-contexa: #2563EB;
  --dg-store: #0D9488;
  --dg-model: #DB2777;
  --dg-allow: #059669;
  --dg-challenge: #D97706;
  --dg-block: #DC2626;
  --dg-escalate: #7C3AED;
  --dg-pending: #0891B2;
  --dg-line: var(--border-medium);
  --dg-surface: var(--bg-primary);
  --dg-radius: 10px;
  --dg-gap: 10px;
}

[data-theme="dark"] {
  --dg-request: #94A3B8;
  --dg-host: #A8A29E;
  --dg-contexa: #60A5FA;
  --dg-store: #2DD4BF;
  --dg-model: #F472B6;
  --dg-allow: #34D399;
  --dg-challenge: #FBBF24;
  --dg-block: #F87171;
  --dg-escalate: #A78BFA;
  --dg-pending: #22D3EE;
}

/* ── frame ─────────────────────────────────────────────────────────────── */
.dg {
  margin: 24px 0 28px;
  padding: 20px;
  border: 1px solid var(--border-light);
  border-radius: 14px;
  background: var(--bg-secondary);
  overflow-wrap: break-word;
  /* Korean words stay whole; a line breaks between words, as in prose. */
  word-break: keep-all;
}
.dg-caption {
  margin: 0 0 16px;
  color: var(--text-secondary);
  font-size: var(--text-sm, 0.875rem);
  line-height: 1.5;
}
.dg-caption strong {
  display: block;
  margin-bottom: 2px;
  color: var(--text-primary);
  font-size: var(--text-base, 1rem);
}
.dg code { overflow-wrap: anywhere; }

/* ── node: one box ─────────────────────────────────────────────────────── */
.dg-node {
  --dg-c: var(--dg-contexa);
  position: relative;
  padding: 10px 14px 10px 16px;
  border: 1px solid color-mix(in srgb, var(--dg-c) 35%, var(--border-light));
  border-left: 4px solid var(--dg-c);
  border-radius: var(--dg-radius);
  background: color-mix(in srgb, var(--dg-c) 7%, var(--dg-surface));
  color: var(--text-primary);
  line-height: 1.45;
}
.dg-node-title { display: block; font-weight: 600; }
.dg-node-desc { display: block; margin-top: 2px; color: var(--text-secondary); font-size: var(--text-sm, 0.875rem); }
.dg-node-tag {
  display: inline-block;
  margin-bottom: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--dg-c) 16%, transparent);
  color: var(--dg-c);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.dg-store.dg-node { border-radius: var(--dg-radius) var(--dg-radius) 18px 18px; }

/* ── flow: steps in order, arrows drawn between them ───────────────────── */
.dg-flow {
  display: flex;
  flex-direction: column;
  gap: 22px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.dg-flow > li { margin: 0; }
.dg-flow > .dg-node:not(:last-child)::after,
.dg-flow > .dg-step:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -18px;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 8px solid var(--dg-line);
  transform: translateX(-50%);
}
.dg-flow > .dg-node:not(:last-child)::before {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -14px;
  width: 2px;
  height: 10px;
  background: var(--dg-line);
  transform: translateX(-50%);
}
/* a numbered step: number circle + node */
.dg-step { position: relative; display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: 10px; align-items: start; }
.dg-step-no {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--dg-c, var(--dg-contexa));
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
}
[data-theme="dark"] .dg-step-no { color: #0D0D12; }
.dg-flow > .dg-step:not(:last-child)::after { left: 14px; transform: translateX(-50%); }
/* an edge label between two steps */
.dg-edge {
  margin: -12px 0 -12px 22px;
  color: var(--text-tertiary);
  font-size: 0.78rem;
}
/* horizontal flow on wide screens */
@media (min-width: 900px) {
  .dg-flow.dg-flow-h { flex-direction: row; align-items: stretch; gap: 26px; }
  .dg-flow.dg-flow-h > .dg-node { flex: 1 1 0; }
  .dg-flow.dg-flow-h > .dg-node:not(:last-child)::after {
    left: auto; right: -19px; top: 50%; bottom: auto;
    border-top: 6px solid transparent; border-bottom: 6px solid transparent; border-left: 8px solid var(--dg-line); border-right: 0;
    transform: translateY(-50%);
  }
  .dg-flow.dg-flow-h > .dg-node:not(:last-child)::before {
    left: auto; right: -14px; top: 50%; bottom: auto; width: 10px; height: 2px; transform: translateY(-50%);
  }
}

/* ── stack: parallel items, no order (no arrows) ───────────────────────── */
.dg-stack { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; }
.dg-stack > li { margin: 0; }

/* ── outcomes: one choice, several results ─────────────────────────────── */
.dg-outcomes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: var(--dg-gap);
  margin: 0;
  padding: 0;
  list-style: none;
}
.dg-outcomes > li { margin: 0; }
.dg-outcomes-title { margin: 6px 0 8px; color: var(--text-secondary); font-size: var(--text-sm, 0.875rem); font-weight: 600; }

/* ── lanes: two tracks that meet at a shared part ─────────────────────── */
.dg-lanes {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(120px, 0.6fr) minmax(0, 1fr);
  gap: 16px;
  align-items: center;
}
.dg-lane {
  padding: 14px;
  border: 1px dashed var(--border-medium);
  border-radius: 12px;
  background: var(--dg-surface);
}
.dg-lane-title { margin: 0 0 12px; font-size: var(--text-sm, 0.875rem); font-weight: 700; color: var(--text-primary); }
.dg-lane-sub { display: block; font-weight: 400; color: var(--text-tertiary); }
.dg-bridge { display: flex; flex-direction: column; align-items: stretch; gap: 8px; text-align: center; }
.dg-bridge-arrow { color: var(--text-tertiary); font-size: 0.78rem; }
@media (max-width: 899px) {
  .dg-lanes { grid-template-columns: minmax(0, 1fr); }
}

/* ── layers: stacked modules or tiers ──────────────────────────────────── */
.dg-layers { display: flex; flex-direction: column; gap: 8px; }
.dg-layer {
  --dg-c: var(--dg-contexa);
  display: grid;
  grid-template-columns: minmax(130px, 0.32fr) minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  border: 1px solid color-mix(in srgb, var(--dg-c) 30%, var(--border-light));
  border-radius: var(--dg-radius);
  background: color-mix(in srgb, var(--dg-c) 6%, var(--dg-surface));
}
.dg-layer-name { font-weight: 700; color: var(--text-primary); }
.dg-layer-name small { display: block; font-weight: 400; color: var(--text-tertiary); font-size: 0.78rem; }
@media (max-width: 599px) { .dg-layer { grid-template-columns: minmax(0, 1fr); } }
.dg-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.dg-chip {
  padding: 2px 9px;
  border: 1px solid var(--border-light);
  border-radius: 999px;
  background: var(--dg-surface);
  color: var(--text-secondary);
  font-size: 0.78rem;
}

/* ── messages: who sends what to whom, in order ────────────────────────── */
.dg-msgs { display: flex; flex-direction: column; gap: 8px; margin: 0; padding: 0; list-style: none; counter-reset: dgmsg; }
.dg-msg {
  display: grid;
  grid-template-columns: 26px minmax(0, max-content) 18px minmax(0, max-content) minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  margin: 0;
  counter-increment: dgmsg;
}
.dg-msg::before {
  content: counter(dgmsg);
  display: grid; place-items: center;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--bg-tertiary); color: var(--text-secondary); font-size: 0.75rem; font-weight: 700;
}
.dg-actor {
  --dg-c: var(--dg-contexa);
  padding: 2px 10px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--dg-c) 14%, transparent);
  color: var(--dg-c);
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}
.dg-msg-arrow { color: var(--text-tertiary); text-align: center; }
.dg-msg-text { color: var(--text-primary); font-size: var(--text-sm, 0.875rem); }
@media (max-width: 599px) {
  /* The text spans a flexible track, so it wraps under the actors instead of widening them. */
  .dg-msg { grid-template-columns: 26px max-content 18px max-content minmax(0, 1fr); }
  .dg-actor { justify-self: start; }
  .dg-msg-text { grid-column: 2 / -1; min-width: 0; overflow-wrap: anywhere; }
}

/* ── compare: two ways side by side ────────────────────────────────────── */
.dg-compare { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.dg-side {
  --dg-c: var(--dg-contexa);
  padding: 14px;
  border: 1px solid color-mix(in srgb, var(--dg-c) 35%, var(--border-light));
  border-top: 4px solid var(--dg-c);
  border-radius: var(--dg-radius);
  background: var(--dg-surface);
}
.dg-side-title { margin: 0 0 8px; font-weight: 700; color: var(--text-primary); }
.dg-side ul { margin: 0; padding-left: 18px; color: var(--text-secondary); font-size: var(--text-sm, 0.875rem); }

/* ── cards: a set of parts with one line each ──────────────────────────── */
.dg-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: var(--dg-gap); }

/* ── legend ───────────────────────────────────────────────────────────── */
.dg-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border-light); font-size: 0.78rem; color: var(--text-secondary); }
.dg-legend span { display: inline-flex; align-items: center; gap: 6px; }
.dg-legend span::before { content: ''; width: 10px; height: 10px; border-radius: 3px; background: var(--dg-c); }

/* ── roles: the color of a part. Written last and with two classes so a role beats every part's own default. ── */
.dg .dg-request { --dg-c: var(--dg-request); }
.dg .dg-host { --dg-c: var(--dg-host); }
.dg .dg-contexa { --dg-c: var(--dg-contexa); }
.dg .dg-store { --dg-c: var(--dg-store); }
.dg .dg-model { --dg-c: var(--dg-model); }
.dg .dg-allow { --dg-c: var(--dg-allow); }
.dg .dg-challenge { --dg-c: var(--dg-challenge); }
.dg .dg-block { --dg-c: var(--dg-block); }
.dg .dg-escalate { --dg-c: var(--dg-escalate); }
.dg .dg-pending { --dg-c: var(--dg-pending); }
