/* ============================================================
   Raising Agents — trace-diff.css
   Styles for <trace-diff> custom element.
   Round 05 substrate · ships against agent-behavior-trace/v0.

   Loaded once per page (alongside trace-diff.js). Light-DOM
   component, so brand custom properties inherit from the host
   page. No shadow root, no style isolation barrier.

   Three render modes:
     · compact   (default) — ~200px tall, prose-embeddable
     · expanded  (data-mode="expanded") — ~420px tall, full unfold
     · collapsed (mobile <640px default) — one-line summary, tap-to-expand

   Naming: .tracediff__* (BEM-ish). Single underscore-suffixed
   prefix so authoring HTML inside the host page never shadows
   these rules.
   ============================================================ */


/* ─────────────────────────────────────────────────────────────
   Host element + reset
   ───────────────────────────────────────────────────────────── */

trace-diff {
  display: block;
  margin: 18px 0;
  font-family: var(--sans, system-ui, sans-serif);
  color: var(--ink, #1A1812);
  /* clip the bracket SVG bleed at narrow widths */
  contain: layout style;
}

/* before mount: occupy the target height so lazy-mount doesn't
   cause layout shift when the data renders */
trace-diff:not(.tracediff--ready) {
  min-height: 200px;
}
trace-diff[data-mode="expanded"]:not(.tracediff--ready) {
  min-height: 420px;
}

/* skeleton (shows while data loads) */
trace-diff .tracediff__skel {
  height: 100%;
  min-height: 200px;
  border: 1px solid var(--rule, #DCD7C6);
  border-radius: 3px;
  background:
    linear-gradient(90deg, transparent, rgba(26,24,18,0.04), transparent) 0 0 / 200% 100% no-repeat,
    transparent;
  animation: tracediff-skel 1.6s ease-in-out infinite;
}
@keyframes tracediff-skel {
  0%   { background-position: -100% 0; }
  100% { background-position:  200% 0; }
}

/* error line (schema mismatch, fetch fail, etc) */
trace-diff .tracediff__err {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 12px;
  color: var(--accent, #9C3A24);
  padding: 10px 14px;
  border: 1px dashed var(--accent, #9C3A24);
  border-radius: 3px;
  background: rgba(156,58,36,0.04);
}


/* ─────────────────────────────────────────────────────────────
   Frame
   ───────────────────────────────────────────────────────────── */

.tracediff__frame {
  border: 1px solid var(--rule, #DCD7C6);
  border-radius: 3px;
  background: #FFFFFF;
  padding: 12px 14px 12px;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 8px;
  /* the component renders as a focus target; outline replaces
     the default to match brand */
  outline-offset: 2px;
}
.tracediff__frame:focus-visible {
  outline: 2px solid var(--ink, #1A1812);
}

/* ── header: case label + contract + inspect link ── */

.tracediff__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 14px;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-3, #6F6A5B);
  line-height: 1.3;
}
.tracediff__case {
  color: var(--ink, #1A1812);
}
.tracediff__contract {
  /* the contract reference is the load-bearing identifier */
  color: var(--ink-2, #4A4636);
}
.tracediff__contract code {
  font-family: inherit;
  font-size: inherit;
  background: transparent;
  color: inherit;
  padding: 0;
}
.tracediff__inspect {
  margin-left: auto;
  color: var(--ink, #1A1812);
  text-decoration: none;
  border-bottom: 1px solid var(--ink-3, #6F6A5B);
  padding-bottom: 1px;
  white-space: nowrap;
}
.tracediff__inspect:hover {
  color: var(--accent, #9C3A24);
  border-bottom-color: var(--accent, #9C3A24);
}

/* updated date — the live-lab signal. Always shown when present
   in the JSON. Sits on the right side of the head, before the
   inspect link if one exists. */
.tracediff__updated {
  margin-left: auto;
  color: var(--ink-3, #6F6A5B);
  white-space: nowrap;
}
.tracediff__updated + .tracediff__inspect {
  margin-left: 14px;
}

/* ── rails ── */

.tracediff__rails {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  position: relative;
}

.tracediff__rail {
  border-left: 2px solid var(--rule, #DCD7C6);
  padding-left: 12px;
  position: relative;
  /* keep bracket SVG visible at the column gutter */
  overflow: visible;
}
.tracediff__rail--regression {
  border-left-color: var(--accent, #9C3A24);
}

.tracediff__rail-label {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 9.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-3, #6F6A5B);
  margin: 0 0 6px;
}
.tracediff__rail--regression .tracediff__rail-label {
  color: var(--accent, #9C3A24);
}

/* ── step ── */

.tracediff__step {
  display: grid;
  grid-template-columns: 22px 12px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 3px 0;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 11.5px;
  line-height: 1.4;
  color: var(--ink-2, #4A4636);
  position: relative;
  /* steps are focusable in expanded mode */
  border-radius: 2px;
}
.tracediff__step:focus-visible {
  outline: 1.5px solid var(--ink, #1A1812);
  outline-offset: 1px;
}

.tracediff__step-n {
  color: var(--ink-3, #6F6A5B);
  font-size: 10px;
  text-align: right;
  padding-right: 2px;
}
.tracediff__marker {
  width: 9px;
  height: 9px;
  border: 1.5px solid var(--ink, #1A1812);
  border-radius: 50%;
  box-sizing: border-box;
  justify-self: center;
}
.tracediff__step-tool {
  color: var(--ink, #1A1812);
  /* truncate long tool names in compact mode */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tracediff__step-result {
  color: var(--ink-3, #6F6A5B);
  font-size: 10.5px;
  white-space: nowrap;
}

/* in-violation rows: light oxide wash + extend into gutter */
.tracediff__step--inv {
  background: rgba(156,58,36,0.06);
  margin-left: -14px;
  padding-left: 14px;
  margin-right: -14px;
  padding-right: 14px;
}

/* consequential (apex of violation): heavier wash + filled square */
.tracediff__step--apex {
  background: rgba(156,58,36,0.11);
  margin-left: -14px;
  padding-left: 14px;
  margin-right: -14px;
  padding-right: 14px;
}
.tracediff__step--apex .tracediff__marker {
  background: var(--accent, #9C3A24);
  border-color: var(--accent, #9C3A24);
  border-radius: 1px;
  /* the filled square is the load-bearing visual signal */
}
.tracediff__step--apex .tracediff__step-tool {
  color: var(--accent, #9C3A24);
  font-weight: 600;
}

/* the hand-drawn bracket sits at the regression rail's leading edge,
   not in the column gutter — visually unambiguous that it marks
   THIS rail's violation, not the gap between rails. */
.tracediff__bracket {
  position: absolute;
  left: -4px;
  width: 12px;
  pointer-events: none;
}
.tracediff__bracket path {
  fill: none;
  stroke: var(--accent, #9C3A24);
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ── verdict footer ── */

.tracediff__verdict {
  display: flex;
  gap: 18px;
  align-items: baseline;
  padding-top: 6px;
  border-top: 1px solid var(--rule, #DCD7C6);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  flex-wrap: wrap;
  line-height: 1.4;
}
.tracediff__v {
  display: inline-flex;
  gap: 6px;
  align-items: baseline;
}
.tracediff__v-label {
  color: var(--ink-3, #6F6A5B);
}
.tracediff__v-pass {
  color: var(--ink, #1A1812);
}
.tracediff__v-fail {
  color: var(--accent, #9C3A24);
  font-weight: 600;
}
/* Pen-mark layer on verdict values — the mark IS the verdict;
   the mono label stays for screen-reader fallback. Mark + label
   read together: ✓ PASS · ✗ FAIL. Per doc 38 restoration. */
.tracediff__v-pass::before,
.tracediff__v-fail::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-right: 4px;
  vertical-align: -2px;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
.tracediff__v-pass::before {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none' stroke='%231A1812' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'><path d='M3 11 Q 6 14, 9 17 Q 13 9, 18 3'/></svg>");
}
.tracediff__v-fail::before {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none' stroke='%239C3A24' stroke-width='2.2' stroke-linecap='round'><path d='M4 4 Q 11 11, 17 17'/><path d='M16 4 Q 11 10, 4 17'/></svg>");
}
.tracediff__v-reason {
  color: var(--ink-2, #4A4636);
  text-transform: none;
  letter-spacing: 0.02em;
  font-size: 11px;
  flex-basis: 100%;
  margin-top: 2px;
}


/* ─────────────────────────────────────────────────────────────
   Expanded mode — taller layout, return values shown,
   contract-clause inline
   ───────────────────────────────────────────────────────────── */

trace-diff[data-mode="expanded"] .tracediff__frame {
  padding: 16px 18px 16px;
  /* expanded mode is content-driven; no min-height baked in.
     The host:not(.--ready) selector handles the pre-mount placeholder. */
}
trace-diff[data-mode="expanded"] .tracediff__step {
  font-size: 12.5px;
  padding: 5px 0;
}
trace-diff[data-mode="expanded"] .tracediff__step-result {
  font-size: 11.5px;
}
trace-diff[data-mode="expanded"] .tracediff__verdict {
  padding-top: 12px;
  font-size: 11.5px;
  gap: 26px;
}


/* ─────────────────────────────────────────────────────────────
   Mobile — collapsed by default, tap-to-expand
   The breakpoint matches the spec (<640px). Below that, the
   component renders as a single summary row; tap expands it
   into a stacked rails layout.
   ───────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  trace-diff:not([data-expanded]) .tracediff__rails,
  trace-diff:not([data-expanded]) .tracediff__verdict .tracediff__v-reason {
    display: none;
  }
  trace-diff:not([data-expanded]) .tracediff__frame {
    grid-template-rows: auto auto auto;
    padding: 12px 14px;
  }
  trace-diff:not([data-expanded]) .tracediff__head .tracediff__inspect::before {
    content: "tap to expand · ";
    color: var(--ink-3, #6F6A5B);
  }

  /* expanded on mobile: stack the two rails vertically */
  trace-diff[data-expanded] .tracediff__rails {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  trace-diff[data-expanded] .tracediff__rail--regression {
    order: 2; /* stable rail first, regression below for narrative */
  }
}


/* mobile verdict — stack vertically for legibility.
   behavior_reason gets its own line under the verdict pair so the
   contract clause reference (load-bearing content) stays readable. */
@media (max-width: 640px) {
  trace-diff[data-expanded] .tracediff__verdict,
  trace-diff[data-mode="expanded"] .tracediff__verdict {
    flex-direction: column;
    gap: 4px;
    align-items: flex-start;
  }
  trace-diff[data-expanded] .tracediff__verdict .tracediff__v,
  trace-diff[data-mode="expanded"] .tracediff__verdict .tracediff__v {
    width: 100%;
  }
  trace-diff .tracediff__v-reason {
    margin-top: 4px;
    line-height: 1.4;
  }
  /* header reflow: stack onto multiple rows on narrow widths */
  trace-diff .tracediff__head {
    flex-wrap: wrap;
    row-gap: 4px;
  }
  trace-diff .tracediff__updated,
  trace-diff .tracediff__inspect {
    margin-left: 0;
  }
  trace-diff .tracediff__updated + .tracediff__inspect {
    margin-left: 12px;
  }
}


/* ─────────────────────────────────────────────────────────────
   Reduced motion — respect user preference
   ───────────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  trace-diff .tracediff__skel {
    animation: none;
  }
}


/* ─────────────────────────────────────────────────────────────
   Print — render as a static figure
   ───────────────────────────────────────────────────────────── */

@media print {
  trace-diff {
    page-break-inside: avoid;
    break-inside: avoid;
  }
  trace-diff .tracediff__frame {
    border-color: #000000;
  }
  trace-diff .tracediff__inspect {
    display: none;
  }
}
