/* Traceline: "Minimalism & Swiss" developer-tool style, dense dashboard spacing.
   Palette: slate "code dark" with a green "run" accent. Dark is the default theme;
   data-theme="light" | "dark" | "system" on <html> switches it.
   Text/badge pairs meet 4.5:1 and input borders 3:1 in both themes. */

@font-face { font-family: "IBM Plex Sans"; font-style: normal; font-weight: 400; font-display: swap; src: url("fonts/ibm-plex-sans-latin-400-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Sans"; font-style: normal; font-weight: 500; font-display: swap; src: url("fonts/ibm-plex-sans-latin-500-normal.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Sans"; font-style: normal; font-weight: 600; font-display: swap; src: url("fonts/ibm-plex-sans-latin-600-normal.woff2") format("woff2"); }
@font-face { font-family: "JetBrains Mono"; font-style: normal; font-weight: 400; font-display: swap; src: url("fonts/jetbrains-mono-latin-400-normal.woff2") format("woff2"); }
@font-face { font-family: "JetBrains Mono"; font-style: normal; font-weight: 600; font-display: swap; src: url("fonts/jetbrains-mono-latin-600-normal.woff2") format("woff2"); }

:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f172a;
  --panel: #1b2336;
  --raised: #222c42;
  --code-bg: #131b2e;
  --inp-bg: #0f172a;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #334155;
  --border-strong: #64748b;
  --accent: #4ade80;
  --accent-strong: #22c55e;
  --on-accent: #0f172a;
  --accent-weak: rgba(34, 197, 94, 0.14);
  --focus: #ffffff;
  --warn: #fbbf24;
  --warn-bg: rgba(251, 191, 36, 0.12);
  --ok: #4ade80;
  --ok-bg: rgba(74, 222, 128, 0.12);
  --danger: #f87171;
  --bad-row: rgba(248, 113, 113, 0.1);
  --c0: #60a5fa;
  --c1: #a78bfa;
  --c2: #fb923c;
  --c3: #4ade80;
  --c4: #f472b6;
  --c5: #22d3ee;
  --cn: #94a3b8;
  --on-layer: #0f172a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f5f9;
  --panel: #ffffff;
  --raised: #f8fafc;
  --code-bg: #f8fafc;
  --inp-bg: #ffffff;
  --text: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --border-strong: #8391a7;
  --accent: #15803d;
  --accent-strong: #22c55e;
  --on-accent: #0f172a;
  --accent-weak: #dcfce7;
  --focus: #0f172a;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --danger: #dc2626;
  --bad-row: #fef2f2;
  --c0: #2563eb;
  --c1: #7c3aed;
  --c2: #c2410c;
  --c3: #15803d;
  --c4: #be185d;
  --c5: #0e7490;
  --cn: #64748b;
  --on-layer: #ffffff;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

@media (prefers-color-scheme: light) {
  :root[data-theme="system"] {
    color-scheme: light;
    --bg: #f1f5f9;
    --panel: #ffffff;
    --raised: #f8fafc;
    --code-bg: #f8fafc;
    --inp-bg: #ffffff;
    --text: #0f172a;
    --muted: #475569;
    --border: #e2e8f0;
    --border-strong: #8391a7;
    --accent: #15803d;
    --accent-strong: #22c55e;
    --on-accent: #0f172a;
    --accent-weak: #dcfce7;
    --focus: #0f172a;
    --warn: #b45309;
    --warn-bg: #fef3c7;
    --ok: #15803d;
    --ok-bg: #dcfce7;
    --danger: #dc2626;
    --bad-row: #fef2f2;
    --c0: #2563eb;
    --c1: #7c3aed;
    --c2: #c2410c;
    --c3: #15803d;
    --c4: #be185d;
    --c5: #0e7490;
    --cn: #64748b;
    --on-layer: #ffffff;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
  }
}

:root {
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
  --radius: 8px;
  --ease: 150ms ease;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}

/* Keyboard focus is always visible; mouse clicks don't draw a ring. */
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

button, .tab, .icon, .btn, .chip, .scen, .inp, summary {
  transition: color var(--ease), background-color var(--ease), border-color var(--ease), box-shadow var(--ease);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  flex: none;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  background: var(--accent-weak);
  color: var(--accent);
}
.brand-mark svg { width: 22px; height: 22px; }
.app-header h1 { margin: 0; font: 600 17px/1.2 var(--mono); letter-spacing: -0.01em; }
.app-header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.theme-switch {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.theme-switch button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font: 500 13px var(--sans);
  cursor: pointer;
}
.theme-switch button:hover { color: var(--text); }
.theme-switch button[aria-pressed="true"] { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.theme-switch svg { width: 15px; height: 15px; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(360px, 470px) minmax(0, 1fr);
  gap: 14px;
  padding: 14px;
  flex: 1;
  min-height: 0;
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-body { flex: 1; overflow: auto; padding: 14px 16px 24px; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 2px;
  padding: 6px 8px 0;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}
.tab {
  display: inline-flex;
  align-items: center;
  border: 0;
  background: none;
  color: var(--muted);
  padding: 8px 10px 9px;
  font: 500 14px var(--sans);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab:focus-visible { outline-offset: -2px; }
.tab .count {
  margin-left: 6px;
  padding: 0 7px;
  border-radius: 10px;
  background: var(--code-bg);
  color: var(--muted);
  font: 500 12px var(--mono);
}
.tab.active .count { background: var(--accent-weak); color: var(--accent); }
.tabs.subtabs { padding: 4px 8px 0 18px; background: var(--code-bg); }
.tabs.subtabs .tab { padding: 6px 10px 7px; font-size: 13px; }

/* ---------- Type ---------- */
h3 { margin: 20px 0 8px; font: 600 14px var(--mono); }
h4 { margin: 14px 0 6px; font: 600 12px var(--mono); text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.muted { color: var(--muted); }
p.muted { margin: 0 0 10px; font-size: 13px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }

/* ---------- Form controls ---------- */
.fld { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.fld.wide { grid-column: 1 / -1; }
.lbl { font-size: 12px; color: var(--muted); font-weight: 500; }
.hint { font-size: 12px; color: var(--muted); }

.inp {
  width: 100%;
  padding: 6px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--inp-bg);
  color: var(--text);
  font: inherit;
}
.inp:hover { border-color: var(--muted); }
.inp:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.inp.mono { font-family: var(--mono); font-size: 12.5px; }
textarea.inp { resize: vertical; }
.inp.compact { width: auto; max-width: 320px; padding: 4px 8px; }
textarea.paste { min-height: 320px; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent-strong); width: 16px; height: 16px; cursor: pointer; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 6px 0; }
.row.end { align-items: flex-end; }
.row.end .fld { flex: 1; margin-bottom: 0; }
.spacer { flex: 1; }

.chk, .radio { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 4px 0; cursor: pointer; }
.chips { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.seg { display: flex; gap: 16px; margin-bottom: 8px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--raised);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  font: 500 14px var(--sans);
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--on-accent); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); color: var(--on-accent); }
.btn.danger:hover { border-color: var(--danger); color: var(--danger); }

.icon {
  display: inline-grid;
  place-items: center;
  flex: none;
  border: 1px solid transparent;
  background: none;
  color: var(--muted);
  width: 30px;
  height: 30px;
  border-radius: 6px;
  cursor: pointer;
}
.icon svg { width: 16px; height: 16px; }
.icon:hover:not([disabled]) { border-color: var(--border); background: var(--raised); color: var(--text); }
.icon.danger:hover:not([disabled]) { color: var(--danger); }
.icon[disabled] { opacity: 0.35; cursor: default; }

/* ---------- Badges, chips, layer colours ---------- */
.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font: 600 12px var(--sans);
  color: var(--on-layer);
  background: var(--lc, var(--cn));
  white-space: nowrap;
}

.c0 { --lc: var(--c0); } .c1 { --lc: var(--c1); } .c2 { --lc: var(--c2); }
.c3 { --lc: var(--c3); } .c4 { --lc: var(--c4); } .c5 { --lc: var(--c5); }
.cn { --lc: var(--cn); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border-radius: 12px;
  background: var(--code-bg);
  color: var(--muted);
  font: 500 12px var(--sans);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.chip.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.chip.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
button.chip { cursor: pointer; }
button.chip:hover { border-color: currentColor; }
.chip.info { background: var(--accent-weak); color: var(--accent); border-color: transparent; }

/* ---------- Cards and the scenario editor ---------- */
.card, .node {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px 6px;
  margin-bottom: 10px;
  background: var(--panel);
}
.node { border-left: 3px solid var(--lc); background: var(--raised); }
details.card > summary { cursor: pointer; padding: 2px 0 8px; list-style-position: inside; }
.node-head { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.children { margin: 6px 0 4px 6px; padding-left: 10px; border-left: 1px dashed var(--border-strong); }
.addrow { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.scen-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.scen {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--raised);
}
.scen:hover { border-color: var(--border-strong); }
.scen.selected { border-color: var(--accent); background: var(--accent-weak); }
.scen-name {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 0;
  background: none;
  color: var(--text);
  font: 500 14px var(--sans);
  text-align: left;
  cursor: pointer;
  padding: 2px 0;
}
.scen-name > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.inp.sessions { width: 58px; padding: 3px 6px; }
.scen-editor { margin-top: 6px; padding-top: 2px; border-top: 1px solid var(--border); }
.err { color: var(--danger); font-size: 12px; margin: -4px 0 6px; }
.err:empty { display: none; }

/* ---------- Messages ---------- */
.fatal { padding: 10px 20px; background: #b91c1c; color: #ffffff; font-weight: 600; }

.banner {
  margin: 10px 14px 0;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--warn-bg);
  color: var(--warn);
  font-size: 13px;
}
.banner ul { margin: 4px 0 0; padding-left: 18px; }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar > .muted { font-size: 13px; flex: 1 1 280px; }
.empty { padding: 40px 10px; text-align: center; color: var(--muted); }
.legend { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }

/* ---------- Tables ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--code-bg);
  text-align: left;
  font: 600 12px var(--mono);
  color: var(--muted);
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.grid td { padding: 6px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.grid tbody tr:hover td { background: var(--accent-weak); }
table.grid tbody tr > td:first-child { border-left: 3px solid var(--lc, transparent); }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12.5px; }
td.flow { color: var(--lc); font-weight: 600; }
table.debug tr.bad td { background: var(--bad-row); }
table.debug tr.rawrow td { border-left: 0 !important; padding-top: 0; color: var(--muted); word-break: break-all; font-size: 12px; }
table.debug tbody tr:not(.rawrow) td { border-bottom: 0; }
table.debug tbody tr:not(.rawrow) td:last-child { min-width: 200px; }
table.rules td { vertical-align: top; }
table.rules .chip { white-space: nowrap; }
.bad-text { color: var(--danger); font-weight: 500; }
.ok-text { color: var(--ok); }

/* ---------- Code and logs ---------- */
pre.code, pre.logs {
  margin: 0;
  padding: 12px 14px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: 12.5px/1.6 var(--mono);
  overflow: auto;
  white-space: pre;
}
pre.logs .ln { border-left: 3px solid var(--lc); padding-left: 8px; }
pre.code.puml { max-height: 300px; }
pre.tree { margin: 0; font: 12px/1.55 var(--mono); white-space: pre; }
.variant + .variant { margin-top: 8px; padding-top: 6px; border-top: 1px dashed var(--border); }

/* ---------- Sequence diagram ---------- */
.preview-head { margin: 18px 0 8px; }
.preview-head h4 { margin: 0; }
.seq-wrap {
  overflow: auto;
  max-height: 75vh;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--code-bg);
}
.seq-svg { display: block; }
.seq-svg text { font: 12px var(--sans); fill: var(--text); }
.seq-svg .seq-title { font: 600 14px var(--sans); }
.seq-svg .seq-num { font-weight: 600; fill: var(--accent); }
.seq-svg .seq-box, .seq-svg .seq-actor { fill: var(--panel); stroke: var(--c0); stroke-width: 1.2; }
.seq-svg .seq-figure { stroke: var(--c0); stroke-width: 1.5; }
.seq-svg .seq-life { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 5 4; }
.seq-svg .seq-act { fill: var(--raised); stroke: var(--c0); stroke-width: 1; }
.seq-svg .seq-msg { stroke: var(--text); stroke-width: 1.2; }
.seq-svg .seq-dashed { stroke-dasharray: 5 3; }
.seq-svg .seq-head { fill: var(--text); }
.seq-svg .seq-note { fill: var(--warn-bg); stroke: var(--warn); stroke-width: 1; }

/* ---------- Processing Rule stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; margin-bottom: 6px; }
.stat { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: var(--raised); }
.stat-v { font: 600 22px/1.2 var(--mono); font-variant-numeric: tabular-nums; }
.stat-l { font-size: 12px; color: var(--muted); }
.stat.warn .stat-v { color: var(--warn); }

/* ---------- Responsive and touch ---------- */
@media (max-width: 900px) {
  html, body { height: auto; }
  .layout { grid-template-columns: minmax(0, 1fr); flex: none; padding: 10px 16px; }
  .panel { max-height: none; }
  .panel-body { overflow: visible; }
  #out .panel-body { overflow-x: auto; }
  .app-header { padding: 12px 16px; flex-wrap: wrap; }
}

@media (max-width: 520px) {
  .grid2 { grid-template-columns: 1fr; }
  .app-header p { display: none; }
}

/* Coarse pointers get 44px targets. */
@media (pointer: coarse) {
  .icon { width: 44px; height: 44px; }
  .btn, .tab, .theme-switch button, .inp { min-height: 44px; }
  .chk, .radio { min-height: 44px; }
}

/* Show code exactly as typed: no "->" or "!=" ligatures in the monospace font.
   Kept last because the `font` shorthand above resets font-variant-ligatures. */
.mono, .inp.mono, pre.code, pre.logs, pre.tree, code, .app-header h1, h3, h4,
.tab .count, table.grid th, table.grid td.num, .stat-v {
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
}

/* Processing Rule: step / item / rule table */
table.steps td.item { white-space: nowrap; }
table.steps .tree-prefix { color: var(--muted); white-space: pre; }
table.steps .rule-list { margin: 0; padding-left: 16px; }
table.steps .rule-list li + li { margin-top: 2px; }
table.steps td:nth-child(3) { min-width: 340px; }
