/*
 * PostGIS Query Explain — stylesheet.
 *
 * One file, no framework, no build step. Colours are defined once as custom
 * properties and overridden wholesale for dark mode, so every component picks
 * up the right theme without repeating media queries.
 */

:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --bg-panel: #ffffff;
  --bg-sunken: #f0f2f5;
  --bg-code: #f4f5f7;
  --text: #16191d;
  --text-muted: #5a626c;
  --border: #d8dce2;
  --border-strong: #b9c0ca;
  --accent: #0b6b52;
  --accent-text: #ffffff;
  --focus: #1264a3;

  --critical: #a51b1b;
  --critical-bg: #fdeaea;
  --high: #a35400;
  --high-bg: #fdf1e3;
  --medium: #6b5800;
  --medium-bg: #fbf6e0;
  --info: #1f5175;
  --info-bg: #e9f2f9;
  --ok: #14653f;
  --ok-bg: #e6f4ec;

  --heat-hot: #c0392b;
  --heat-warm: #c77c1a;
  --heat-cool: #2f7d6a;

  --radius: 8px;
  --mono: ui-monospace, "SFMono-Regular", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --bg-panel: #1b1f24;
    --bg-sunken: #22272e;
    --bg-code: #12151a;
    --text: #e6e9ed;
    --text-muted: #a2acb8;
    --border: #2e343c;
    --border-strong: #414952;
    --accent: #3fbf9b;
    --accent-text: #0b1210;
    --focus: #6cb6ff;

    --critical: #ff8f8f;
    --critical-bg: #3a1c1c;
    --high: #ffbe73;
    --high-bg: #382718;
    --medium: #e8d47a;
    --medium-bg: #33301b;
    --info: #8fc7ea;
    --info-bg: #172935;
    --ok: #7fd4a8;
    --ok-bg: #16301f;

    --heat-hot: #e5645a;
    --heat-warm: #e0a552;
    --heat-cool: #4fbfa4;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

.wrap {
  max-width: 78rem;
  margin: 0 auto;
  padding: 0 1.25rem;
}

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

a:hover {
  text-decoration: none;
}

code,
kbd,
pre {
  font-family: var(--mono);
  font-size: 0.86em;
}

kbd {
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3em;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--bg-panel);
  padding: 0.6rem 1rem;
  z-index: 10;
}

.skip-link:focus {
  left: 0;
}

/* ---------------------------------------------------------------- header - */

.site-header {
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  padding: 2rem 0 1.5rem;
}

.site-header h1 {
  margin: 0 0 0.4rem;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 0;
  max-width: 62ch;
  color: var(--text-muted);
}

main {
  padding: 1.5rem 1.25rem 3rem;
  display: grid;
  gap: 1.5rem;
}

/* ----------------------------------------------------------------- input - */

.input-panel,
.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.input-panel h2,
.panel h2,
.panel h3 {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}

.panel-note {
  margin: -0.35rem 0 0.9rem;
  color: var(--text-muted);
  font-size: 0.88rem;
  max-width: 74ch;
}

textarea {
  width: 100%;
  min-height: 13rem;
  resize: vertical;
  padding: 0.75rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-code);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.5;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.85rem;
}

.btn {
  font: inherit;
  font-size: 0.9rem;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-sunken);
  color: var(--text);
  cursor: pointer;
}

.btn:hover {
  border-color: var(--text-muted);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.btn-small {
  font-size: 0.8rem;
  padding: 0.3rem 0.6rem;
}

.status {
  margin: 0.85rem 0 0;
  min-height: 1.4em;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.status-error {
  color: var(--critical);
  font-weight: 600;
}

.status-success {
  color: var(--ok);
}

/* -------------------------------------------------------------- overview - */

.overview {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  gap: 0.75rem;
}

.stat {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.stat-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.stat-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.banner {
  grid-column: 1 / -1;
  margin: 0;
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 0.9rem;
}

.banner-ok {
  background: var(--ok-bg);
  color: var(--ok);
  border-color: currentColor;
}

.banner-warn {
  background: var(--high-bg);
  color: var(--high);
  border-color: currentColor;
}

.banner-info {
  background: var(--info-bg);
  color: var(--info);
  border-color: currentColor;
}

.banner-error {
  background: var(--critical-bg);
  color: var(--critical);
  border-color: currentColor;
}

.results {
  display: grid;
  gap: 1.5rem;
}

.results-grid {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  gap: 1.5rem;
  align-items: start;
}

@media (max-width: 62rem) {
  .results-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* -------------------------------------------------------------- findings - */

.finding-group + .finding-group {
  margin-top: 1.5rem;
}

.severity-heading {
  margin: 0 0 0.6rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
}

.severity-critical {
  color: var(--critical);
}
.severity-high {
  color: var(--high);
}
.severity-medium {
  color: var(--medium);
}
.severity-info {
  color: var(--info);
}

.finding {
  border: 1px solid var(--border);
  border-left: 4px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  margin-bottom: 0.85rem;
  background: var(--bg-sunken);
}

.finding.severity-critical {
  border-left-color: var(--critical);
}
.finding.severity-high {
  border-left-color: var(--high);
}
.finding.severity-medium {
  border-left-color: var(--medium);
}
.finding.severity-info {
  border-left-color: var(--info);
}

.finding-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.finding-header h5 {
  margin: 0;
  font-size: 1rem;
  color: var(--text);
}

.finding-node {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.05rem 0.4rem;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

.finding-explanation,
.finding-action {
  margin: 0 0 0.6rem;
  font-size: 0.92rem;
  max-width: 76ch;
}

.evidence {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 0.15rem 0.8rem;
  margin: 0 0 0.7rem;
  font-size: 0.83rem;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
}

.evidence dt {
  color: var(--text-muted);
}

.evidence dd {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.9em;
  overflow-wrap: anywhere;
}

.sql-block {
  position: relative;
  margin-bottom: 0.7rem;
}

.sql-block pre {
  margin: 0;
  padding: 0.7rem 0.8rem;
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow-x: auto;
  white-space: pre;
}

.copy-btn {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
}

.finding-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.rule-id {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.empty {
  color: var(--text-muted);
  font-size: 0.92rem;
  margin: 0;
}

/* ----------------------------------------------------------------- flame - */

.flame-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.55rem;
}

.flame-label {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.82rem;
  margin-bottom: 0.15rem;
}

.flame-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.flame-time {
  color: var(--text-muted);
  font-family: var(--mono);
  white-space: nowrap;
}

.flame-track {
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 4px;
  height: 0.85rem;
  overflow: hidden;
}

.flame-bar {
  height: 100%;
  min-width: 2px;
  border-radius: 3px 0 0 3px;
}

.flame-hot {
  background: var(--heat-hot);
}
.flame-warm {
  background: var(--heat-warm);
}
.flame-cool {
  background: var(--heat-cool);
}

/* ------------------------------------------------------------------ tree - */

.tree,
.tree-group {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree-group {
  margin-left: 1.1rem;
  padding-left: 0.85rem;
  border-left: 2px solid var(--border);
}

.tree-item {
  padding: 0.35rem 0 0.35rem 0.2rem;
  border-radius: 6px;
}

.tree-item:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
}

.tree-item[aria-expanded='false'] > .tree-group {
  display: none;
}

.tree-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  cursor: default;
}

.tree-item[aria-expanded] > .tree-row {
  cursor: pointer;
}

.tree-twisty::before {
  content: '▾';
  display: inline-block;
  width: 1em;
  color: var(--text-muted);
}

.tree-item[aria-expanded='false'] > .tree-row .tree-twisty::before {
  content: '▸';
}

.tree-type {
  font-weight: 600;
  font-family: var(--mono);
  font-size: 0.85rem;
}

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  border: 1px solid currentColor;
  white-space: nowrap;
}

.badge-ok {
  color: var(--ok);
  background: var(--ok-bg);
}
.badge-warn {
  color: var(--high);
  background: var(--high-bg);
}
.badge-bad {
  color: var(--critical);
  background: var(--critical-bg);
}

.badge-finding.severity-critical {
  background: var(--critical-bg);
}
.badge-finding.severity-high {
  background: var(--high-bg);
}
.badge-finding.severity-medium {
  background: var(--medium-bg);
}
.badge-finding.severity-info {
  background: var(--info-bg);
}

.tree-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 0.9rem;
  margin: 0.3rem 0 0 1.2rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.tree-metrics dt {
  font-weight: 500;
}

.tree-metrics dt::after {
  content: ':';
}

.tree-metrics dd {
  margin: 0 0.5rem 0 0;
  font-family: var(--mono);
  color: var(--text);
}

.tree-conditions {
  list-style: none;
  margin: 0.35rem 0 0 1.2rem;
  padding: 0;
  display: grid;
  gap: 0.15rem;
  font-size: 0.78rem;
}

.tree-conditions code {
  overflow-wrap: anywhere;
  background: var(--bg-code);
  border-radius: 4px;
  padding: 0 0.25rem;
}

.cond-label {
  color: var(--text-muted);
}

/* ------------------------------------------------------------- catalogue - */

.catalogue {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.87rem;
}

.catalogue th,
.catalogue td {
  text-align: left;
  vertical-align: top;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
}

.catalogue th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.catalogue td:first-child {
  min-width: 14rem;
}

.catalogue td a {
  display: block;
  font-weight: 600;
}

.severity-cell {
  text-transform: uppercase;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

@media (max-width: 48rem) {
  .catalogue,
  .catalogue tbody,
  .catalogue tr,
  .catalogue td {
    display: block;
    width: 100%;
  }

  .catalogue thead {
    display: none;
  }

  .catalogue tr {
    border-bottom: 1px solid var(--border);
    padding: 0.5rem 0;
  }

  .catalogue td {
    border: 0;
    padding: 0.15rem 0;
  }
}

/* ---------------------------------------------------------------- footer - */

.site-footer {
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  padding: 1.5rem 0;
  font-size: 0.88rem;
}

.site-footer p {
  margin: 0 0 0.3rem;
}

.footer-note {
  color: var(--text-muted);
  font-size: 0.82rem;
}
