/* Compact mode uses CSS Grid - see COMPACT MODE section below */
@import url('https://cdn.jsdelivr.net/npm/@fontsource/monaspace-krypton/index.css');

/** TraceRite **/
:root {
  --tracerite-var: #8af;
  --tracerite-type: #5c8;
  --tracerite-val: #8af;
  --tracerite-highlight: #ff8;
  --tracerite-highlight-text: #000;
  --tracerite-call-symbol-color: #ff8;
  --tracerite-call-symbol-shadow: 0 0 .1em black;
  --tracerite-call-highlight: #da0;
  --tracerite-caret: #f00;
  --tracerite-exception: #777;
  --tracerite-code: inherit;
  --tracerite-lineno: #888;
  --tracerite-link-bg: #fff5;
  --tracerite-link-hover: #fff8;
  --tracerite-function: #68f;
  --tracerite-location: #5a6;
  --tracerite-code-font: 'Monaspace Krypton', 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Source Code Pro', 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', 'Ubuntu Mono', 'Consolas', 'Courier New';
  --tracerite-ui-font: system-ui, -apple-system, 'Segoe UI', 'Roboto', 'Ubuntu', 'Cantarell', 'Noto Sans', sans-serif;
}

:root:has(.tracerite.autodark) {
  color-scheme: light dark;
}

:root:has(.tracerite.autodark) {
  @media (prefers-color-scheme: dark) {
    --tracerite-var: #8af;
    --tracerite-type: #5c8;
    --tracerite-val: #8af;
    --tracerite-highlight: #ff0;
    --tracerite-highlight-text: #000;
    --tracerite-call-symbol-color: #ff0;
    --tracerite-call-symbol-shadow: none;
    --tracerite-call-highlight: #ff0;
    --tracerite-caret: #f55;
    --tracerite-exception: #aaa;
    --tracerite-code: #ccc;
    --tracerite-lineno: #888;
    --tracerite-link-bg: #0005;
    --tracerite-link-hover: #0008;
    --tracerite-function: #8af;
    --tracerite-location: #6b8;
  }
}

:root .tracerite { font-family: var(--tracerite-ui-font); font-size: 16px; }
:root .tracerite,
:root .tracerite *,
:root .tracerite .traceback-details table,
:root .tracerite > h2,
:root .tracerite > h3 { margin: 0; padding: 0; outline: none; box-sizing: border-box; line-height: 1.2; font: var(--tracerite-ui-font); font-weight: 700;}

:root .tracerite > h2 { font-size: 1.1em; }
:root .tracerite > h3 { font-size: 1em; }

/* Code font declarations with high specificity - only for code tags */
:root .tracerite pre,
:root .tracerite code {
  font-family: var(--tracerite-code-font);
  font-feature-settings:
    "calt" 1,   /* texture healing / contextual alternates */
    "liga" 1,   /* standard & repeating-character ligatures */
    "ss01" 1, "ss02" 1, "ss03" 1, "ss04" 1,
    "ss05" 1, "ss06" 1, "ss07" 1, "ss08" 1, "ss09" 1,  /* coding ligatures */
    "cv01" 2,   /* slashed zero */
    "cv31" 1;   /* six-pointed asterisk */
  font-variant-ligatures: common-ligatures;
  background: none;
  color: var(--tracerite-code);
  text-overflow: ellipsis;
  word-break: normal;
}

:root .tracerite strong,
:root .tracerite > h3 { font-weight: bold; padding: 0.2em 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
:root .tracerite .excmessage { font-size: 0.8em; border-left: .2em solid var(--tracerite-exception); margin-left: 0.2em; padding-left: 0.5em; white-space: pre-wrap; overflow-wrap: break-word; overflow: hidden; }
:root .tracerite .excmessage-ellipsis { color: var(--tracerite-lineno); }
:root .tracerite .exctype { color: var(--tracerite-exception); }

:root .tracerite .traceback-details p { margin: 1em 0; }
:root .tracerite .traceback-details pre { width: 50vw; padding: .5em; font-size: 0.8em; }
:root .tracerite .traceback-details .codeline { text-indent: 4ch each-line; }
:root .tracerite .traceback-details .codeline::before {
  content: attr(data-lineno);
  color: var(--tracerite-lineno);
  opacity: 0.0;
  transition: all 0.4s;
  display: inline-block;
  text-align: right;
  text-indent: 0;
  white-space: nowrap;
  word-break: keep-all;
  padding-right: 1ch;
  width: 4ch;
}
:root .tracerite .traceback-details pre:hover .codeline::before { opacity: 1.0; }

:root .tracerite .traceback-details mark { background: var(--tracerite-highlight); color: var(--tracerite-highlight-text); padding: 0.2em; margin: -0.1em; }
:root .tracerite .traceback-details em { font-style: normal; color: var(--tracerite-caret); }

/* Symbol element - display symbol emoji and description together */
:root .tracerite .traceback-details .tracerite-symbol {
  display: inline-block;
  margin: -0.15em 0 -0.15em 0.5ch;
  padding: 0.1em;
  font-size: 1.25em;
  font-weight: bold;
  vertical-align: middle;
  font-family: var(--tracerite-ui-font);
  white-space: nowrap;
}

/* Call symbols get special color and shadow */
:root .tracerite .traceback-call .tracerite-symbol {
  color: var(--tracerite-call-symbol-color);
  text-shadow: var(--tracerite-call-symbol-shadow);
}

/* Display symbol and description using pseudo-element and data attribute */
:root .tracerite .traceback-details .tracerite-symbol::before { content: attr(data-text); }

:root .tracerite .traceback-details {
  position: relative;
  min-width: 20ch;
  max-width: 100%;
  margin: 0 .2em;
  flex-shrink: 0;
  border-radius: .5em;
  padding: .2em;
  padding-left: 0;
}

:root .tracerite .traceback-details:last-child { width: 100%; }
:root .tracerite .traceback-ellipsis { min-width: 4ch; text-align: center; }

/* Base styles for frame-function and frame-location */
:root .tracerite .frame-function { font-weight: 600; color: var(--tracerite-function); line-height: 1.2; }
:root .tracerite .frame-location { color: var(--tracerite-location); line-height: 1.2; }
:root .tracerite .frame-colon { color: var(--tracerite-code); font-weight: 700; }
:root .tracerite .frame-lineno { color: var(--tracerite-lineno); display: inline; }
:root .tracerite .frame-link {
  margin-left: 0.3em;
  padding: 0.1em 0.4em;
  background: var(--tracerite-link-bg);
  border-radius: 0.3em;
  color: inherit;
  text-decoration: none;
  font-size: 0.85em;
}
:root .tracerite .frame-link:hover { background: var(--tracerite-link-hover); }

/* Variable inspector: grid layout with fixed first column, flexible second */
:root .tracerite dl.inspector {
  display: grid;
  grid-template-columns: auto 1fr;
  margin: 0;
  min-width: 8em;
  width: 100%;
  word-break: break-word;
}

:root .tracerite dl.inspector dt,
:root .tracerite dl.inspector dd {
  margin: 0;
  padding: 0;
  width: 100%;
  text-align: right;
  vertical-align: top;
  font-weight: normal;
  float: none;
}
:root .tracerite dl.inspector dt {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
:root .tracerite .inspector .var { font-weight: bold; color: var(--tracerite-var); }
:root .tracerite .inspector .type { white-space: nowrap; color: var(--tracerite-type); }
:root .tracerite .inspector .val { white-space: pre; text-overflow: ellipsis; overflow: hidden; color: var(--tracerite-val); }

/* Block format for multi-line strings - left aligned */
:root .tracerite .inspector .val-block {
  text-align: left;
  white-space: pre-wrap;
  overflow: visible;
  max-width: none;
  word-break: break-word;
}

:root .tracerite .inspector .val-block pre {
  margin: 0;
  padding: 0;
  padding-top: 0.4em;
  border-radius: 3px;
  font-size: 0.8em;
  white-space: pre;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 30em;
  color: inherit;
  font-family: var(--tracerite-code-font);
}

/* Inline format - left aligned (default) */
:root .tracerite .inspector .val-inline {
  text-align: left;
}

/* Nested table styling (for matrices) */
:root .tracerite .inspector table td {
  color: var(--tracerite-val);
  word-break: keep-all;
  overflow: hidden;
  font-size: 0.8em;
  border-collapse: collapse;
  text-align: right;
}

:root .tracerite .inspector tr {
  background: none;
}

/* matrix value on a variable */
:root .tracerite .inspector .val-inline table td {
  padding: 0 0.4em;
  min-width: 2em;
}

/* Key-value dl styling (dicts, dataclasses) */
:root .tracerite .inspector dl.keyvalue-dl {
  display: grid;
  grid-template-columns: auto 1fr;
  margin: 0;
  padding-top: 0.2em;
  font-size: 0.8em;
}
:root .tracerite .inspector dl.keyvalue-dl dt,
:root .tracerite .inspector dl.keyvalue-dl dd {
  margin: 0;
  padding: 0 0.3em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
:root .tracerite .inspector dl.keyvalue-dl dt {
  text-align: right;
  color: var(--tracerite-var);
  font-weight: bold;
  width: max-content;
  float: none;
}
:root .tracerite .inspector dl.keyvalue-dl dd {
  text-align: left;
  width: auto;
  float: none;
}

/* Array with scale factor container */
:root .tracerite .inspector .array-with-scale {
  display: flex;
  align-items: center;
  gap: 0.3em;
}

/* Scale suffix for arrays (e.g., ×10⁶) */
:root .tracerite .inspector .scale-suffix {
  font-size: 1.4em;
  font-weight: bold;
  white-space: nowrap;
  align-self: center;
}


/* ============================================
   COMPACT MODE TOGGLE AND STYLING (with :has)
   ============================================ */

/* CSS Grid layout for compact mode */
:root .tracerite {
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  align-items: baseline;
  gap: 0 0.5em;
  justify-content: start;
}

/* Hidden checkbox for toggle */
:root .tracerite .frame-toggle-checkbox {
  display: none;
}

/* Expand wrapper - uses grid-template-rows for height animation */
:root .tracerite .expand-wrapper {
  grid-column: 1 / -1;
  display: grid;
  grid-template-rows: 0fr;
  grid-template-columns: subgrid;
  transition: grid-template-rows 0.25s ease-out;
}
:root .tracerite .expand-content {
  overflow: hidden;
  min-height: 0;
  display: grid;
  grid-template-columns: subgrid;
  grid-column: 1 / -1;
}
:root .tracerite .expand-content > pre {
  grid-column: 1 / 4;
  width: auto;
  max-width: calc(112ch + 1em);  /* 108 code chars + 4ch line-number gutter + .5em padding each side */
  white-space: pre-wrap;
  word-wrap: break-word;
  text-overflow: clip;
  overflow: hidden;  /* Prevent scrollbar flash during animation */
}
:root .tracerite .expand-content > dl.inspector {
  grid-column: 4;
  align-self: start;
  margin-top: auto;
  margin-bottom: auto;
  height: fit-content;
}

/* When checkbox is checked, expand the wrapper */
:root .tracerite .frame-toggle-checkbox:checked ~ .compact-call-line {
  display: none;
}
:root .tracerite .frame-toggle-checkbox:checked ~ .expand-wrapper {
  grid-template-rows: 1fr;
}

/* Colon always visible */
:root .tracerite .frame-colon {
  color: var(--tracerite-code);
  font-weight: 700;
}

/* Exception headers and messages span all columns */
:root .tracerite > h2,
:root .tracerite > h3,
:root .tracerite > pre.excmessage {
  grid-column: 1 / -1;
}

/* Make intermediate containers transparent to grid */
:root .tracerite .traceback-details {
  display: contents;
}

/* Grid column assignments for all frame elements */
/* Location comes first, then function */
:root .tracerite .traceback-details .frame-location {
  grid-column: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 20em;
}
:root .tracerite .traceback-details .frame-function {
  grid-column: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 20em;
}
:root .tracerite .traceback-details .compact-call-line {
  grid-column: 3 / -1;
}

/* Wrapper label for clickable location+function - uses display:contents to not affect grid */
:root .tracerite .frame-label-wrapper {
  display: contents;
  cursor: pointer;
}

/* Ellipsis frame spans all columns */
:root .tracerite .traceback-ellipsis {
  grid-column: 1 / -1;
  min-width: auto;
  text-align: left;
  padding: 0 0.5em;
  color: var(--tracerite-lineno);
}

/* Compact code line styling */
:root .tracerite .compact-call-line {
  display: inline;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 40em;
  line-height: 1;
  vertical-align: baseline;
}
:root .tracerite .compact-call-line code.compact-code {
  display: inline;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: inherit;
  vertical-align: baseline;
  background: transparent;
}
:root .tracerite .compact-call-line .compact-symbol {
  font-size: 1.1em;
  vertical-align: baseline;
  flex-shrink: 0;
}

/* Call symbols in compact lines get special color and shadow */
:root .tracerite .traceback-call .compact-call-line .compact-symbol {
  color: var(--tracerite-call-symbol-color);
  text-shadow: var(--tracerite-call-symbol-shadow);
}
:root .tracerite .compact-call-line em {
  color: var(--tracerite-call-highlight);
}
/* Error/stop frames use red caret in compact mode */
:root .tracerite .traceback-error .compact-call-line em,
:root .tracerite .traceback-stop .compact-call-line em {
  color: var(--tracerite-caret);
}

/* Parallel branches for ExceptionGroups - side by side layout */
:root .tracerite .parallel-branches {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5em;
  align-items: flex-start;
  border-left: .2em solid var(--tracerite-exception);
  margin-left: 0.2em;
  padding-left: 0.5em;
}

:root .tracerite .parallel-branch {
  flex: 1 1 300px;
  min-width: 250px;
  max-width: 100%;
  /* Same grid layout as .tracerite for consistent frame alignment */
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  align-items: baseline;
  gap: 0 0.5em;
  justify-content: start;
}

/* Exception headers and messages inside parallel branches span all columns */
:root .tracerite .parallel-branch > h3,
:root .tracerite .parallel-branch > pre.excmessage {
  grid-column: 1 / -1;
}
:root .tracerite .parallel-branch > h3 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
