:root {
  color-scheme: light dark;
  --bg: #fff;
  --panel: #fafafa;
  --text: #1a1a1a;
  --muted: #737373;
  --line: #e7e7e7;
  --hover: #f0f0f0;
  --accent: #1a1a1a;
  --on-accent: #fff;
  --changed: #b7791f;
  --danger: #c0392b;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616;
    --panel: #1b1b1b;
    --text: #e8e8e8;
    --muted: #8f8f8f;
    --line: #2c2c2c;
    --hover: #262626;
    --accent: #e8e8e8;
    --on-accent: #161616;
    --changed: #d9a441;
    --danger: #ef6f5e;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", sans-serif;
}

button, input, textarea { font: inherit; color: inherit; }

button {
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 10px;
  cursor: pointer;
}
button:hover { background: var(--hover); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
button.primary:hover { opacity: 0.85; }
button.danger { color: var(--danger); }
button.quiet { border-color: transparent; color: var(--muted); }
button.quiet:hover { color: var(--text); }
:focus-visible { outline: 2px solid var(--muted); outline-offset: 1px; }

/* Sidebar */
#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 14px 10px;
  overflow-y: auto;
}
#search {
  width: 100%;
  padding: 5px 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 4px;
  padding-left: 9px;
  font-size: 12px;
  color: var(--muted);
}
.section button { padding: 0 8px; }
.group { margin: 10px 0 2px; padding-left: 9px; font-size: 12px; color: var(--muted); }
.entry {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 4px 9px;
  border: 0;
  text-align: left;
}
.entry span:first-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry.active { background: var(--hover); font-weight: 600; }
.dot { flex: none; width: 6px; height: 6px; border-radius: 50%; background: var(--changed); }
.empty { padding: 4px 9px; color: var(--muted); }

/* Main */
#main { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.head { padding: 22px 32px 14px; border-bottom: 1px solid var(--line); }
.head h1 { margin: 0; font-size: 20px; font-weight: 600; }
.head .description {
  max-width: 76ch;
  margin: 6px 0 0;
  color: var(--muted);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.head .description.open { display: block; }
.row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.row.end { justify-content: flex-end; }
.spread { justify-content: space-between; }
.head .row { margin-top: 12px; }
.muted { color: var(--muted); }
.toggle { display: inline-flex; align-items: center; gap: 6px; }
.toggle i { width: 6px; height: 6px; border-radius: 50%; background: var(--line); }
.toggle[aria-pressed="true"] i { background: #2f9e62; }

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 32px;
  border-bottom: 1px solid var(--line);
}
.files { display: flex; gap: 2px; flex: 1; min-width: 0; overflow-x: auto; }
.files button { border: 0; white-space: nowrap; font-family: var(--mono); font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.files button.active { background: var(--hover); color: var(--text); }
.path { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content { flex: 1; min-height: 0; display: flex; }
.scroll { flex: 1; min-width: 0; overflow-y: auto; padding: 24px 32px 80px; }
.outline {
  flex: none;
  width: 220px;
  overflow-y: auto;
  padding: 24px 16px;
  border-left: 1px solid var(--line);
  font-size: 12px;
}
.outline button { display: block; width: 100%; border: 0; padding: 3px 6px; text-align: left; color: var(--muted); }
.outline button:hover { color: var(--text); }
@media (max-width: 1050px) { .outline { display: none; } }

#editor {
  flex: 1;
  width: 100%;
  padding: 24px 32px 80px;
  border: 0;
  outline: 0;
  resize: none;
  background: var(--bg);
  font: 13px/1.6 var(--mono);
  tab-size: 2;
}

/* Rendered markdown */
.doc { max-width: 76ch; }
.doc > :first-child { margin-top: 0; }
.doc h1 { font-size: 22px; font-weight: 600; margin: 28px 0 10px; }
.doc h2 { font-size: 17px; font-weight: 600; margin: 32px 0 8px; }
.doc h3 { font-size: 14px; font-weight: 600; margin: 22px 0 6px; }
.doc h4, .doc h5, .doc h6 { font-size: 14px; font-weight: 500; color: var(--muted); margin: 18px 0 4px; }
.doc p { margin: 0 0 12px; }
.doc ul, .doc ol { margin: 0 0 12px; padding-left: 22px; }
.doc ul ul, .doc ol ol, .doc ul ol, .doc ol ul { margin-bottom: 0; }
.doc a { color: inherit; text-underline-offset: 2px; }
.doc hr { border: 0; border-top: 1px solid var(--line); margin: 24px 0; }
.doc blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 2px solid var(--line); color: var(--muted); }
.doc code { font: 12.5px var(--mono); background: var(--hover); padding: 1px 4px; border-radius: 4px; }
.doc pre, pre.raw { font: 12.5px/1.6 var(--mono); background: var(--panel); border: 1px solid var(--line); padding: 10px 12px; border-radius: 6px; overflow-x: auto; margin: 0 0 12px; }
.doc pre code { background: none; padding: 0; }
pre.raw { background: none; border: 0; padding: 0; }
.doc table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.doc th, .doc td { border: 1px solid var(--line); padding: 4px 9px; text-align: left; vertical-align: top; }
.doc th { font-weight: 600; }

#toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 70ch;
  padding: 6px 14px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
}

dialog {
  width: 360px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  padding: 18px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.3); }
dialog h2 { margin: 0 0 12px; font-size: 15px; font-weight: 600; }
dialog label { display: block; margin-bottom: 10px; color: var(--muted); font-size: 12px; }
dialog input { display: block; width: 100%; margin-top: 3px; padding: 5px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--text); font-size: 14px; }
#dialog-error { color: var(--danger); margin: 0 0 10px; font-size: 13px; }

@media (max-width: 700px) {
  body { grid-template-columns: 180px minmax(0, 1fr); }
  .head, .bar, .scroll, #editor { padding-left: 16px; padding-right: 16px; }
}
a.entry { color: inherit; text-decoration: none; border-radius: 6px; }
a.entry:hover { background: var(--hover); }

/* Funnel analytics */
.fn-sub { margin: 4px 0 0; font-size: 12.5px; }
.fn-bar { justify-content: space-between; flex-wrap: wrap; }
.fn-pills { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.fn-pill { font-size: 12.5px; padding: 3px 9px; border-radius: 6px; border: 1px solid var(--line); color: var(--muted); }
.fn-pill.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.fn-select { font: inherit; font-size: 12.5px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }
.fn-scroll { padding: 20px 32px 64px; }
.fn-wrap { display: grid; gap: 16px; max-width: 1120px; }
.fn-error { padding: 12px 14px; border: 1px solid var(--danger); color: var(--danger); border-radius: 8px; }

.fn-kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 1100px) { .fn-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 640px) { .fn-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.fn-kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.fn-kpi-accent { border-color: #2f9e62; }
.fn-kpi-label { font-size: 12px; color: var(--muted); }
.fn-kpi-val { font-size: 24px; font-weight: 700; line-height: 1.25; margin-top: 2px; font-variant-numeric: tabular-nums; }
.fn-kpi-sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.fn-card { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; min-width: 0; }
.fn-card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.fn-card h2 { margin: 0; font-size: 15px; font-weight: 600; }
.fn-hint { font-size: 12px; }
.fn-footnote { font-size: 12px; margin: 8px 0 0; }
.fn-grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
@media (max-width: 960px) { .fn-grid-2 { grid-template-columns: 1fr; } }

.fn-funnel-header, .fn-step-main {
  display: grid;
  grid-template-columns: minmax(170px, 240px) minmax(120px, 1fr) 64px 76px 64px;
  gap: 10px;
  align-items: center;
}
.fn-funnel-header { font-size: 11.5px; color: var(--muted); padding: 0 0 6px; border-bottom: 1px solid var(--line); }
.fn-steps { display: grid; gap: 6px; margin-top: 8px; }
.fn-step { padding: 4px 0; border-bottom: 1px dashed var(--line); }
.fn-step:last-child { border-bottom: 0; }
.fn-step-name { display: flex; flex-direction: column; min-width: 0; }
.fn-step-name span { font-size: 13px; font-weight: 500; }
.fn-step-name code { font-size: 11px; font-family: var(--mono); color: var(--muted); }
.fn-step-num, .fn-step-pct, .fn-step-delta { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.fn-step-num { font-weight: 600; }
.fn-step-pct { color: var(--muted); }
.fn-step-delta { font-size: 12px; color: var(--muted); }
.fn-step-delta.drop { color: var(--danger); }
.fn-step-notes { display: flex; flex-wrap: wrap; gap: 6px; margin: 5px 0 2px 12px; }
.fn-chip { font-size: 11.5px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px; }
.fn-chip strong { color: var(--text); font-weight: 600; }

.fn-bar-svg { display: block; width: 100%; height: 10px; }
.fn-bar-track { fill: var(--hover); }
.fn-bar-fill { fill: #4f7df3; }
.fn-bar-scored { fill: #2a9d8f; }
.fn-bar-paywall { fill: #d9822b; }
.fn-bar-tapped { fill: #9b6bff; }
.fn-bar-purchased { fill: #2f9e62; }
.fn-bar-open { fill: #4f7df3; }
.fn-bar-checkin { fill: #2f9e62; }

.fn-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.fn-leg-item { display: inline-flex; align-items: center; gap: 6px; }
.fn-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.swatch-welcome, .swatch-open { background: #4f7df3; }
.swatch-scored { background: #2a9d8f; }
.swatch-paywall { background: #d9822b; }
.swatch-purchased, .swatch-checkin { background: #2f9e62; }

.fn-chart-svg { display: block; width: 100%; height: auto; max-height: 240px; }
.fn-grid { stroke: var(--line); stroke-width: 1; }
.fn-axis-text { fill: var(--muted); font-size: 10.5px; font-family: var(--mono); }
.fn-area-welcome { fill: #4f7df3; fill-opacity: 0.1; }
.fn-line-welcome { fill: none; stroke: #4f7df3; stroke-width: 2; }
.fn-line-scored { fill: none; stroke: #2a9d8f; stroke-width: 2; }
.fn-line-paywall { fill: none; stroke: #d9822b; stroke-width: 1.75; stroke-dasharray: 4 3; }
.fn-series-purchased-bar { fill: #2f9e62; fill-opacity: 0.75; }
.fn-dot-welcome { fill: #4f7df3; }
.fn-dot-scored { fill: #2a9d8f; }
.fn-dot-paywall { fill: #d9822b; }

.fn-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.fn-table th, .fn-table td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.fn-table th { font-size: 11.5px; font-weight: 600; color: var(--muted); }
.fn-table tr:last-child td { border-bottom: 0; }
.fn-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.fn-table code { font: 12px var(--mono); }
.fn-col-bar { width: 120px; }
.fn-clickable tbody tr { cursor: pointer; }
.fn-clickable tbody tr:hover { background: var(--hover); }
.fn-clickable tbody tr.active { background: var(--hover); font-weight: 600; }
.fn-table-scroll { max-height: 360px; overflow-y: auto; }

.fn-kv-list { display: grid; gap: 6px; }
.fn-kv { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; padding: 4px 0; border-bottom: 1px dashed var(--line); font-variant-numeric: tabular-nums; }
.fn-kv:last-child { border-bottom: 0; }

@media (max-width: 700px) {
  .fn-scroll { padding: 14px 16px 48px; }
  .fn-funnel-header, .fn-step-main { grid-template-columns: minmax(110px, 1fr) 70px 46px 54px 48px; gap: 6px; }
}

/* VM status */
.vm-meter { height: 8px; margin-top: 6px; }
.vm-meter-ok { fill: #2f9e62; }
.vm-meter-warn { fill: #d9822b; }
.vm-meter-danger { fill: var(--danger); }
.vm-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; flex: none; }
.vm-dot.ok { background: #2f9e62; }
.vm-dot.warn { background: #d9822b; }
.vm-dot.bad { background: var(--danger); }
.vm-ep-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; }
.vm-ep { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; }
.vm-ep-top { display: flex; align-items: center; gap: 7px; font-size: 13px; }
.vm-ep-meta { display: flex; justify-content: space-between; gap: 8px; margin-top: 5px; font-size: 11.5px; }
.vm-ep-meta code { font-family: var(--mono); color: var(--muted); }
.vm-badge { margin-left: auto; font-size: 11px; font-family: var(--mono); padding: 1px 6px; border-radius: 999px; border: 1px solid var(--line); }
.vm-badge.ok { color: #2f9e62; border-color: #2f9e62; }
.vm-badge.bad { color: var(--danger); border-color: var(--danger); }
.vm-chips-block { display: grid; gap: 8px; }
.vm-subhead { font-size: 12px; color: var(--muted); }
.vm-chips { margin: 0; }
.vm-svc-chip { display: inline-flex; align-items: center; gap: 6px; }
.vm-table-wrap { overflow-x: auto; }
.vm-cname { display: inline-flex; align-items: center; gap: 7px; }
.vm-csub { font-size: 11px; margin-top: 1px; }
.vm-ports { font-family: var(--mono); font-size: 11.5px; max-width: 210px; word-break: break-all; }
.vm-ts { font-size: 12px; white-space: nowrap; }

/* Server copy: sign-in page and sign-out */
.signout { margin-top: 18px; }
body.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; grid-template-columns: none; }
.login form { width: min(340px, 100%); display: grid; gap: 12px; }
.login h1 { margin: 0 0 4px; font-size: 20px; font-weight: 600; }
.login label { display: grid; gap: 6px; color: var(--muted); }
.login input { padding: 7px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; color: var(--text); }
.login p { margin: 0; color: var(--danger); }
