/* Traceline PoC: one hand-written stylesheet. No framework, no CDN. Light theme, dark via prefers-color-scheme. */

:root {
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;
  --fs: 14.5px;
  --lh: 1.55;
  --radius: 6px;
  --maxw: 1200px;
  --sidebar: 232px;

  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #d8dce2;
  --border-strong: #b9c0ca;
  --text: #1b2330;
  --muted: #5a6473;
  --faint: #8a93a1;
  --accent: #2250b8;
  --accent-hover: #1a3f93;
  --accent-soft: #e6edfb;
  --focus: #2f6bff;
  --header-bg: #1d2633;
  --header-text: #e8ecf2;
  --header-muted: #a7b1c0;

  --ok: #1d7a3e;      --ok-bg: #e3f4e8;      --ok-bd: #a9d9b8;
  --warn: #8a5a00;    --warn-bg: #fdf2d8;    --warn-bd: #ecd08a;
  --danger: #b42318;  --danger-bg: #fdeceb;  --danger-bd: #f1b2ac;
  --info: #1f55b8;    --info-bg: #e5edfb;    --info-bd: #a9c0ec;
  --violet: #5b3cc4;  --violet-bg: #eeeafc;  --violet-bd: #c6b9f1;
  --teal: #0f6b6b;    --teal-bg: #e0f3f2;    --teal-bd: #9fd3d0;
  --neutral: #4a5463; --neutral-bg: #eceef2; --neutral-bd: #cfd4db;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --surface: #181d25;
    --surface-2: #1f2530;
    --border: #2c3440;
    --border-strong: #3d4757;
    --text: #e2e7ee;
    --muted: #a0aab8;
    --faint: #77818f;
    --accent: #7fa4ff;
    --accent-hover: #a5c0ff;
    --accent-soft: #1f2a44;
    --focus: #7fa4ff;
    --header-bg: #0b0e13;
    --header-text: #e2e7ee;
    --header-muted: #8c97a6;

    --ok: #6fd08f;      --ok-bg: #15301f;      --ok-bd: #275a39;
    --warn: #f0c060;    --warn-bg: #33280f;    --warn-bd: #5e4a1b;
    --danger: #ff8f85;  --danger-bg: #3a1916;  --danger-bd: #6b2c26;
    --info: #8db3ff;    --info-bg: #17243d;    --info-bd: #2c4372;
    --violet: #b9a6ff;  --violet-bg: #251d42;  --violet-bd: #45367a;
    --teal: #6fd3cf;    --teal-bg: #112d2d;    --teal-bd: #22575a;
    --neutral: #b4bdc9; --neutral-bg: #232a34; --neutral-bd: #39424f;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { font-size: var(--fs); -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  line-height: var(--lh);
  color: var(--text);
  background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; }

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

h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .6em; font-weight: 600; }
h1 { font-size: 1.55rem; letter-spacing: -0.01em; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.04rem; }
h4 { font-size: .95rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0 0 .8em; }
code, kbd, pre, .mono { font-family: var(--mono); font-size: .92em; }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 .3em; }
pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); padding: .8em 1em;
      overflow-x: auto; line-height: 1.45; }
pre code { background: none; border: 0; padding: 0; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
small, .small { font-size: .88em; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Header ---------- */

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--header-bg); color: var(--header-text);
  border-bottom: 1px solid rgba(255,255,255,.06);
}
.topbar-inner {
  max-width: calc(var(--maxw) + var(--sidebar) + 48px); margin: 0 auto; padding: 0 20px;
  display: flex; align-items: center; gap: 18px; height: 48px;
}
.brand { font-weight: 700; letter-spacing: .02em; color: var(--header-text); white-space: nowrap; }
.brand:hover { color: #fff; text-decoration: none; }
.brand .tag { font-weight: 500; font-size: .78rem; color: var(--header-muted); margin-left: .4em; }
.topnav { display: flex; gap: 2px; }
.topnav a { color: var(--header-muted); padding: 6px 10px; border-radius: 5px; }
.topnav a:hover, .topnav a[aria-current="page"] { color: var(--header-text); background: rgba(255,255,255,.08); text-decoration: none; }
.switcher { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.switcher label { color: var(--header-muted); font-size: .85rem; }
.switcher select { background: rgba(255,255,255,.08); color: var(--header-text); border: 1px solid rgba(255,255,255,.18);
                   padding: 4px 8px; border-radius: 5px; max-width: 240px; }
.switcher select option { color: #1b2330; background: #fff; }
.switcher button { padding: 4px 10px; }
.userbox { display: flex; align-items: center; gap: 10px; color: var(--header-muted); white-space: nowrap; }
.userbox form { margin: 0; }
.userbox .linklike { color: var(--header-muted); }
.userbox .linklike:hover { color: var(--header-text); }

/* ---------- Layout ---------- */

.shell {
  max-width: calc(var(--maxw) + var(--sidebar) + 48px); margin: 0 auto; padding: 20px;
  display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); gap: 24px; align-items: start;
}
.shell.no-sidebar { grid-template-columns: minmax(0, 1fr); max-width: calc(var(--maxw) + 40px); }
main { min-width: 0; }

.sidebar {
  position: sticky; top: 68px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 0; font-size: .93rem; max-height: calc(100vh - 88px); overflow-y: auto;
}
.sidebar .sb-title { padding: 2px 14px 8px; font-weight: 600; border-bottom: 1px solid var(--border); margin-bottom: 8px; }
.sidebar .sb-title small { display: block; font-weight: 400; color: var(--muted); }
.sidebar h4 { margin: 12px 14px 4px; font-size: .74rem; color: var(--faint); }
.sidebar ol, .sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar li > a, .sidebar li > span {
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  padding: 5px 14px; color: var(--text); border-left: 3px solid transparent;
}
.sidebar li > a:hover { background: var(--surface-2); text-decoration: none; }
.sidebar li.current > a, .sidebar li.current > span { border-left-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.sidebar li.done > a, .sidebar li.done > span { color: var(--muted); }
.sidebar li.future > span { color: var(--faint); }
.sidebar .sub li > a, .sidebar .sub li > span { padding-left: 28px; font-size: .9rem; }
.sidebar .step { display: inline-flex; align-items: center; gap: 8px; }
.sidebar .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border-strong); flex: none; }
.sidebar li.done .dot { background: var(--ok); }
.sidebar li.current .dot { background: var(--accent); }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.page-head h1 { margin-bottom: .2em; }
.page-head .meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; color: var(--muted); }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- Panels ---------- */

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 18px; }
.panel > .panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface-2);
  border-radius: var(--radius) var(--radius) 0 0;
}
.panel > .panel-head h2, .panel > .panel-head h3 { margin: 0; font-size: 1rem; }
.panel > .panel-body { padding: 14px 16px; }
.panel > .panel-body > :last-child { margin-bottom: 0; }
.panel > .panel-foot { padding: 10px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.panel.placeholder { border-style: dashed; }
.panel.placeholder .panel-body { color: var(--muted); }
.panel.danger-zone { border-color: var(--danger-bd); }
.panel.danger-zone > .panel-head { background: var(--danger-bg); color: var(--danger); }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.stack > * + * { margin-top: 12px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 18px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

.stats { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; min-width: 140px; }
.stat .v { font-size: 1.35rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: .85rem; }

/* ---------- Tables ---------- */

.table-wrap { overflow: auto; max-height: 70vh; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.panel .table-wrap { border: 0; border-radius: 0; }
table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: .94rem; }
th, td { padding: 7px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: .82rem;
  text-transform: uppercase; letter-spacing: .03em; white-space: nowrap;
  border-bottom: 1px solid var(--border-strong);
}
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td.actions { white-space: nowrap; text-align: right; }
.md table { width: auto; margin: 0 0 1em; border: 1px solid var(--border); border-radius: var(--radius); }
.md th, .md td { border-bottom: 1px solid var(--border); }

/* ---------- Badges ---------- */

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; line-height: 1.6;
  border: 1px solid var(--neutral-bd); background: var(--neutral-bg); color: var(--neutral);
  white-space: nowrap; vertical-align: middle; letter-spacing: .01em;
}
.badge.square { border-radius: 4px; }
.b-ok, .b-approved, .b-done, .b-active, .b-pass { color: var(--ok); background: var(--ok-bg); border-color: var(--ok-bd); }
.b-warn, .b-suspect, .b-stopped, .b-minimal, .b-acknowledged { color: var(--warn); background: var(--warn-bg); border-color: var(--warn-bd); }
.b-danger, .b-rejected, .b-failed, .b-error, .b-fail, .b-open { color: var(--danger); background: var(--danger-bg); border-color: var(--danger-bd); }
.b-info, .b-generating, .b-running, .b-queued, .b-full { color: var(--info); background: var(--info-bg); border-color: var(--info-bd); }
.b-claimed, .b-review, .b-template { color: var(--violet); background: var(--violet-bg); border-color: var(--violet-bd); }
.b-ai-act { color: var(--teal); background: var(--teal-bg); border-color: var(--teal-bd); }
.b-draft, .b-pending, .b-none, .b-decommissioned, .b-resolved { color: var(--neutral); background: var(--neutral-bg); border-color: var(--neutral-bd); }
.b-stage { color: var(--accent); background: var(--accent-soft); border-color: var(--info-bd); border-radius: 4px; }
.b-generating::before, .b-running::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .b-generating::before, .b-running::before { animation: none; } }

/* ---------- Forms ---------- */

form { margin: 0; }
label { font-weight: 600; font-size: .92rem; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; min-width: 0; }
.field .hint { color: var(--muted); font-size: .85rem; font-weight: 400; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
input[type="text"], input[type="password"], input[type="url"], input[type="search"], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); width: 100%;
  border: 1px solid var(--border-strong); border-radius: 5px; padding: 6px 9px; line-height: 1.4;
}
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input[readonly], input:disabled, select:disabled, textarea[readonly] { background: var(--surface-2); color: var(--muted); }
textarea { resize: vertical; }
textarea.editor {
  font-family: var(--mono); font-size: .9rem; line-height: 1.5; min-height: 460px; tab-size: 2;
  white-space: pre-wrap; padding: 10px 12px;
}
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px 4px; margin: 0 0 14px; min-width: 0; }
legend { font-weight: 600; padding: 0 6px; }
.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.radio-list { display: flex; flex-direction: column; gap: 8px; }
.radio-list label { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-weight: 400; padding: 8px 10px;
                    border: 1px solid var(--border); border-radius: 5px; cursor: pointer; }
.radio-list label:hover { background: var(--surface-2); }
.radio-list input { margin-top: 4px; grid-row: span 2; }
.radio-list strong { font-weight: 600; }
.radio-list .hint { color: var(--muted); font-size: .86rem; }
.key-state { font-size: .85rem; color: var(--muted); }

.btn, button {
  font: inherit; font-size: .93rem; font-weight: 600; line-height: 1.3;
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 6px 13px; border-radius: 5px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); text-decoration: none; white-space: nowrap;
}
.btn:hover, button:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.btn.primary, button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover, button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: #fff; }
@media (prefers-color-scheme: dark) {
  .btn.primary, button.primary { color: #0b0e13; }
  .btn.primary:hover, button.primary:hover { color: #0b0e13; }
}
.btn.danger, button.danger { color: var(--danger); border-color: var(--danger-bd); }
.btn.danger:hover, button.danger:hover { background: var(--danger-bg); }
.btn.small, button.small { padding: 3px 9px; font-size: .84rem; }
button:disabled, .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
.linklike { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 400; cursor: pointer; }
.linklike:hover { background: none; text-decoration: underline; }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline-form { display: inline-flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline; }

/* ---------- Messages ---------- */

.flashes { margin-bottom: 16px; display: flex; flex-direction: column; gap: 8px; }
.flash, .notice {
  padding: 9px 14px; border-radius: var(--radius); border: 1px solid var(--info-bd);
  background: var(--info-bg); color: var(--text); border-left: 4px solid var(--info);
}
.flash.success, .notice.success { border-color: var(--ok-bd); border-left-color: var(--ok); background: var(--ok-bg); }
.flash.warning, .notice.warning { border-color: var(--warn-bd); border-left-color: var(--warn); background: var(--warn-bg); }
.flash.error, .notice.error { border-color: var(--danger-bd); border-left-color: var(--danger); background: var(--danger-bg); }
.notice { margin-bottom: 14px; }
.notice > :last-child { margin-bottom: 0; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 50; max-width: 460px;
  background: var(--surface); border: 1px solid var(--danger-bd); border-left: 4px solid var(--danger);
  border-radius: var(--radius); padding: 10px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.18);
}
.toast[hidden] { display: none; }
.toast .toast-close { float: right; margin-left: 10px; }

/* ---------- Content ---------- */

.md { overflow-wrap: anywhere; }
.md h1 { font-size: 1.4rem; border-bottom: 1px solid var(--border); padding-bottom: .3em; margin-top: 1.2em; }
.md h1:first-child, .md h2:first-child { margin-top: 0; }
.md h2 { font-size: 1.15rem; margin-top: 1.4em; }
.md h3 { font-size: 1.02rem; margin-top: 1.2em; }
.md ul, .md ol { padding-left: 1.5em; margin: 0 0 .8em; }
.md li + li { margin-top: .15em; }
.md blockquote { margin: 0 0 .8em; padding: .2em 1em; border-left: 3px solid var(--border-strong); color: var(--muted); }
.preview { max-height: 640px; overflow-y: auto; }

.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; }
.files { list-style: none; padding: 0; margin: 0; font-family: var(--mono); font-size: .88rem; }
.files li { padding: 2px 0; border-bottom: 1px dotted var(--border); }
.check-list { list-style: none; margin: 0; padding: 0; }
.check-list li { display: grid; grid-template-columns: 90px 70px 1fr; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.check-list li:last-child { border-bottom: 0; }

.empty { color: var(--muted); padding: 24px; text-align: center; }

.login-wrap { min-height: calc(100vh - 48px); display: flex; align-items: flex-start; justify-content: center; padding-top: 12vh; }
.login-card { width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 22px 24px; }
.login-card h1 { font-size: 1.3rem; }

.error-detail { white-space: pre-wrap; font-family: var(--mono); font-size: .88rem; }

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; }
  .grid-2, .grid-3, .split { grid-template-columns: minmax(0, 1fr); }
  .topnav { display: none; }
}
@media (max-width: 600px) {
  .topbar-inner { flex-wrap: wrap; height: auto; min-height: 48px; padding: 6px 12px; gap: 6px 12px; }
  .brand .tag, .switcher label { display: none; }
  .switcher { margin-left: 0; flex: 1 1 0; min-width: 0; }
  .switcher select { width: 100%; max-width: 100%; }
  .userbox { margin-left: auto; }
  .login-wrap { min-height: auto; padding-top: 8vh; }
  .login-card { width: 100%; max-width: 360px; }
}

.form-block { margin-bottom: 18px; }
