:root {
  --bg: #101214;
  --panel: #1b2025;
  --surface: #20262c;
  --surface-strong: #262d34;
  --text: #f4f2ed;
  --muted: #aab3ad;
  --accent: #38bdf8;
  --edge: #3b82f6;
  --traj: #94a3b8;
  --node: #f87171;
  --grid: #3b454f;
  --border: #3a424a;
  --gold: #e3b341;
  --green: #54b96f;
  --orange: #dd7d3c;
  --cyan: #4fb3d8;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.diagnostics-preview-page {
  height: auto;
  min-height: 100%;
  background: #15191d;
}

a {
  color: var(--accent);
}

a:focus-visible,
button:focus-visible,
select:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0;
}

.topbar .sub {
  color: var(--muted);
  font-size: 0.85rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-left: auto;
}

label {
  font-size: 0.8rem;
  color: var(--muted);
}

select,
button {
  font: inherit;
  font-size: 0.875rem;
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  border: 1px solid #4a525a;
  background: #121619;
  color: var(--text);
}

button {
  cursor: pointer;
  background: #2d343b;
}

button:hover {
  background: #3a424a;
}

.stage-wrap {
  flex: 1;
  position: relative;
  min-height: 420px;
  background: #101214;
}

#svg-root {
  width: 100%;
  height: 100%;
  min-height: 520px;
  display: block;
  cursor: grab;
  touch-action: none;
}

#svg-root:active {
  cursor: grabbing;
}

.hud {
  position: absolute;
  left: 12px;
  bottom: 12px;
  padding: 8px 12px;
  background: rgba(16, 18, 20, 0.88);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 11px;
  color: var(--muted);
  max-width: min(420px, 90vw);
  pointer-events: none;
}

.legend {
  position: absolute;
  right: 12px;
  top: 12px;
  padding: 10px 12px;
  background: rgba(32, 38, 44, 0.92);
  border: 1px solid #4a525a;
  border-radius: 8px;
  font-size: 12px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
  color: var(--muted);
}

.swatch {
  width: 28px;
  height: 4px;
  border-radius: 2px;
}

.swatch.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.results {
  padding: 22px 16px 20px;
  background: #15191d;
  border-top: 1px solid var(--border);
}

.results-inner {
  display: grid;
  gap: 16px;
  max-width: 1120px;
  margin: 0 auto;
}

.section-heading {
  display: grid;
  gap: 5px;
  max-width: 760px;
}

.section-heading h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 750;
  line-height: 1.2;
  letter-spacing: 0;
}

.section-heading p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.eyebrow,
.metric-kicker {
  color: #d0a03d;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.result-meta {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
  line-height: 1.35;
}

.result-card {
  display: grid;
  grid-template-columns: minmax(240px, 520px) minmax(220px, 1fr);
  gap: 18px;
  align-items: center;
  padding: 12px;
  color: var(--text);
  text-decoration: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  transition: border-color 160ms ease, transform 160ms ease;
}

.result-card:hover {
  border-color: var(--gold);
  transform: translateY(-1px);
}

.result-media {
  display: block;
  overflow: hidden;
  border: 1px solid #4a525a;
  border-radius: 6px;
  background: #f8fafc;
  aspect-ratio: 16 / 9;
}

.result-card img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.result-copy {
  display: grid;
  gap: 7px;
}

.result-copy strong {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.2;
}

.result-copy span {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.result-copy .eyebrow {
  color: #d0a03d;
  font-size: 0.72rem;
}

.result-copy .result-meta {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 10px;
}

.metric-card {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 7px;
  min-height: 128px;
  padding: 12px;
  color: var(--text);
  text-decoration: none;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  border-top-width: 3px;
  border-radius: 8px;
  transition: border-color 160ms ease, transform 160ms ease;
}

.metric-card:hover {
  border-color: var(--gold);
  transform: translateY(-1px);
}

.metric-card strong {
  font-size: 1.18rem;
  line-height: 1.2;
}

.metric-card span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.metric-card .metric-kicker {
  color: #d0a03d;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.2;
}

.metric-card.route {
  border-top-color: var(--gold);
}

.metric-card.accuracy {
  border-top-color: var(--green);
}

.metric-card.tuning {
  border-top-color: var(--orange);
}

.metric-card.memory {
  border-top-color: var(--cyan);
}

.diagnostics-panel {
  display: grid;
  grid-template-columns: minmax(220px, 0.95fr) minmax(0, 1.05fr);
  gap: 14px;
  align-items: stretch;
  padding: 14px;
  background: #11171c;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.diagnostics-copy {
  display: grid;
  align-content: center;
  gap: 8px;
}

.diagnostics-copy strong {
  font-size: 1.08rem;
  line-height: 1.25;
}

.diagnostics-copy span {
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.diagnostics-copy a {
  width: fit-content;
  font-size: 0.84rem;
  font-weight: 700;
}

.diagnostics-compare {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.diagnostics-loading,
.diagnostics-error {
  display: grid;
  align-content: center;
  min-height: 180px;
  min-width: 0;
  padding: 12px;
  background: #080b0e;
  border: 1px solid #303942;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.45;
}

.diagnostics-error {
  border-color: #7f3c3c;
  color: #fca5a5;
}

.diagnostics-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.diagnostics-engine {
  display: grid;
  gap: 5px;
  min-width: 0;
  padding: 10px;
  background: #080b0e;
  border: 1px solid #303942;
  border-left: 4px solid var(--cyan);
  border-radius: 6px;
}

.diagnostics-engine.fallback {
  border-left-color: var(--orange);
}

.diagnostics-engine .metric-kicker {
  color: #d0a03d;
  font-size: 0.72rem;
}

.diagnostics-engine span {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.35;
}

.diagnostics-engine strong {
  color: var(--text);
  font-size: 1.04rem;
  line-height: 1.2;
}

.diagnostics-pill-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.diagnostics-pill {
  display: inline-flex;
  width: fit-content;
  max-width: 100%;
  padding: 2px 6px;
  color: #d8f3ff;
  background: #13222b;
  border: 1px solid #2d5263;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.4;
}

.diagnostics-pill.warn {
  color: #ffe6cb;
  background: #2a1d12;
  border-color: #6f4524;
}

.diagnostics-table-wrap {
  min-width: 0;
  overflow-x: auto;
  background: #080b0e;
  border: 1px solid #303942;
  border-radius: 6px;
}

.diagnostics-table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
  color: #d8f3ff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 0.74rem;
  line-height: 1.45;
}

.diagnostics-table th,
.diagnostics-table td {
  padding: 8px 9px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid #27313a;
}

.diagnostics-table th {
  color: #e7d7a3;
  font-weight: 750;
}

.diagnostics-table td {
  color: #c9e5ef;
}

.diagnostics-table tr:last-child td {
  border-bottom: 0;
}

.diagnostics-note {
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.4;
}

.diagnostics-preview-shell {
  display: grid;
  align-items: center;
  min-height: 100vh;
  padding: 28px;
}

.diagnostics-preview-panel {
  grid-template-columns: minmax(320px, 0.58fr) minmax(0, 1.42fr);
  width: min(1280px, 100%);
  margin: 0 auto;
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.32);
}

.footer {
  padding: 0.6rem 1rem;
  font-size: 0.75rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

@media (max-width: 860px) {
  .result-card {
    grid-template-columns: 1fr;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .diagnostics-panel {
    grid-template-columns: 1fr;
  }

  .diagnostics-table {
    min-width: 560px;
  }
}

@media (max-width: 520px) {
  .topbar {
    align-items: flex-start;
  }

  .controls {
    width: 100%;
    margin-left: 0;
  }

  .metric-grid {
    grid-template-columns: 1fr;
  }

  .diagnostics-summary {
    grid-template-columns: 1fr;
  }

  .diagnostics-preview-shell {
    padding: 16px;
  }
}
