:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #5d6779;
  --accent: #3355d6;
  --accent-soft: #e8edfd;
  --ok: #1f7a4d;
  --ok-soft: #e3f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --err: #b42335;
  --err-soft: #fbe6e9;
  --thinking: #6b4fbb;
  --thinking-soft: #f1edfb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1e26;
    --surface-2: #222733;
    --border: #2f3542;
    --text: #e6e9ef;
    --muted: #9aa3b5;
    --accent: #7d98ff;
    --accent-soft: #232c4a;
    --ok: #5fd39a;
    --ok-soft: #173326;
    --warn: #f0b75a;
    --warn-soft: #362a14;
    --err: #ff7a8a;
    --err-soft: #3a1c22;
    --thinking: #b9a3ff;
    --thinking-soft: #2a2442;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; }
code { font-family: var(--mono); font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* ── Top bar & tabs ─────────────────────────────────────────── */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark { color: var(--accent); font-size: 1.6rem; }
.brand h1 { font-size: 1.15rem; }
.brand-sub { margin: 0; color: var(--muted); font-size: 0.85rem; }
.status-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.tabs {
  display: flex; gap: 4px; padding: 0 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); overflow-x: auto;
}
.tab {
  border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 12px 14px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }

main { padding: 20px 24px 48px; max-width: 1400px; margin: 0 auto; }
.view[hidden] { display: none; }

/* ── Panels, fields, buttons ────────────────────────────────── */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.panel + .panel, .panel + div, div + .panel { margin-top: 16px; }
.panel-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.panel-actions { display: flex; gap: 8px; }

.field { display: flex; flex-direction: column; gap: 6px; margin: 14px 0 0; border: 0; padding: 0; }
.field > span, .field > legend { font-weight: 600; font-size: 0.85rem; }
.field em { font-weight: 400; color: var(--muted); font-style: normal; }
textarea, input {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; width: 100%;
}
textarea { resize: vertical; }
#context { font-family: var(--mono); font-size: 0.85rem; }
textarea:focus, input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field-error { color: var(--err); }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 7px 14px; cursor: pointer; font-size: 0.9rem; }
.segmented label + label span { border-left: 1px solid var(--border); }
.segmented input:checked + span { background: var(--accent); color: #fff; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

.btn {
  font: inherit; font-weight: 600; font-size: 0.9rem; border-radius: 8px; padding: 8px 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn-small { padding: 4px 10px; font-size: 0.82rem; }
.btn-link { border: 0; background: none; color: var(--accent); padding: 4px 0; }
.btn-link:hover:not(:disabled) { background: none; text-decoration: underline; }
.input-panel .btn-primary { margin-top: 16px; width: 100%; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 8px 0 0; }

/* ── Chips & badges ─────────────────────────────────────────── */
.chip, .badge {
  display: inline-flex; align-items: center; gap: 5px; border-radius: 999px; font-size: 0.78rem;
  font-weight: 600; padding: 3px 10px; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.chip-ok, .badge-ok { background: var(--ok-soft); color: var(--ok); }
.chip-warn, .badge-warn { background: var(--warn-soft); color: var(--warn); }
.chip-err, .badge-err { background: var(--err-soft); color: var(--err); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badge-thinking { background: var(--thinking-soft); color: var(--thinking); }

/* ── Decide layout ──────────────────────────────────────────── */
.decide-layout { display: grid; grid-template-columns: minmax(280px, 360px) 1fr; gap: 20px; align-items: start; }
.input-panel { position: sticky; top: 16px; }
.result { min-width: 0; }
.empty-state {
  border: 1px dashed var(--border); border-radius: var(--radius); padding: 40px 28px; text-align: center; color: var(--muted);
}
.empty-state h2 { color: var(--text); margin-bottom: 8px; }

@media (max-width: 900px) {
  .decide-layout { grid-template-columns: 1fr; }
  .input-panel { position: static; }
  main { padding: 16px; }
  .topbar, .tabs { padding-left: 16px; padding-right: 16px; }
}

.example-banner {
  background: var(--warn-soft); color: var(--warn); border-radius: var(--radius); padding: 10px 14px;
  font-weight: 600; font-size: 0.88rem; margin-bottom: 14px;
}
.error-banner {
  background: var(--err-soft); color: var(--err); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px;
}

.summary {
  display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; justify-content: space-between;
}
.summary-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.summary-id { font-family: var(--mono); color: var(--muted); font-size: 0.82rem; }

/* Pipeline stepper */
.stepper { display: flex; gap: 6px; flex-wrap: wrap; margin: 14px 0 4px; padding: 0; list-style: none; }
.stepper a {
  display: flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text);
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 11px 4px 4px; font-size: 0.82rem;
}
.stepper a:hover { border-color: var(--accent); }
.step-num {
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 0.75rem;
}
.step-ok .step-num { background: var(--ok-soft); color: var(--ok); }
.step-warn .step-num { background: var(--warn-soft); color: var(--warn); }
.step-err .step-num { background: var(--err-soft); color: var(--err); }

/* Layer cards */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-top: 14px; scroll-margin-top: 16px;
}
.card-head {
  display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; justify-content: space-between;
  padding: 13px 16px; border-bottom: 1px solid var(--border);
}
.card-title { display: flex; align-items: center; gap: 10px; }
.card-title small { color: var(--muted); font-family: var(--mono); font-size: 0.78rem; }
.card-body { padding: 14px 16px; }
.card-tags { display: flex; gap: 6px; flex-wrap: wrap; }

.output {
  white-space: pre-wrap; word-break: break-word; margin: 0; font-size: 0.9rem; line-height: 1.55;
}
.output-empty { color: var(--muted); font-style: italic; }

details.thinking {
  margin-top: 12px; border-left: 3px solid var(--thinking); background: var(--thinking-soft); border-radius: 0 8px 8px 0;
}
details.thinking summary {
  cursor: pointer; padding: 8px 12px; font-weight: 600; font-size: 0.85rem; color: var(--thinking);
}
.thinking-body { padding: 0 12px 10px; }
.thinking-block { white-space: pre-wrap; font-size: 0.86rem; margin: 0 0 8px; }

.tune {
  margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--border);
  display: flex; flex-direction: column; gap: 8px;
}
.tune-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.88rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
tr.is-selected td { background: var(--accent-soft); }
td.mono, .mono { font-family: var(--mono); font-size: 0.82rem; word-break: break-all; }
.evidence { color: var(--muted); font-style: italic; }
.clickable { cursor: pointer; }
.clickable:hover td { background: var(--surface-2); }

/* Selection */
.selection-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; margin-bottom: 14px; }
.kv { background: var(--surface-2); border-radius: 8px; padding: 9px 12px; }
.kv dt { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; }
.kv dd { margin: 2px 0 0; font-weight: 600; word-break: break-word; }

/* Pre-dispatch */
.question { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.question label { font-weight: 600; font-size: 0.85rem; }
.checkbox { display: flex; gap: 8px; align-items: center; font-weight: 400 !important; }
.checkbox input { width: auto; }

pre.json {
  background: var(--surface-2); border-radius: 8px; padding: 12px; overflow: auto; max-height: 420px;
  font-family: var(--mono); font-size: 0.8rem; margin: 10px 0 0;
}

/* Compare */
.compare-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.compare-grid .output { background: var(--surface-2); border-radius: 8px; padding: 10px; }
.diff-same { color: var(--muted); font-size: 0.85rem; }
@media (max-width: 900px) { .compare-grid { grid-template-columns: 1fr; } }

/* HostedScan */
.hs-inputs { display: grid; grid-template-columns: 1fr 140px auto; gap: 12px; align-items: end; }
.hs-inputs .field { margin: 0; }
@media (max-width: 700px) { .hs-inputs { grid-template-columns: 1fr; } }
.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.metric { background: var(--surface-2); border-radius: 8px; padding: 12px; }
.metric strong { display: block; font-size: 1.5rem; }
.metric span { color: var(--muted); font-size: 0.82rem; }

/* Guide */
.guide h2 { margin-top: 6px; }
.guide h2 + * { margin-top: 10px; }
.guide-steps { padding-left: 20px; margin: 10px 0 22px; }
.guide-steps li { margin-bottom: 8px; }
.levers td:first-child { font-weight: 600; white-space: nowrap; }

/* Confirmation dialog */
.btn-danger { background: var(--err); border-color: var(--err); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--err); filter: brightness(1.1); }
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 0; max-width: min(560px, 92vw);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(10, 14, 22, 0.6); }
#confirm-form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.confirm-risk { margin: 0; background: var(--err-soft); color: var(--err); border-radius: 8px; padding: 10px 12px; font-weight: 600; }
.confirm-details { margin: 0; display: grid; gap: 8px; }
.confirm-details dt { color: var(--muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; }
.confirm-details dd { margin: 2px 0 0; word-break: break-word; }
.confirm-details pre { margin: 4px 0 0; }
.confirm-check { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); margin-top: 0; }
.confirm-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* Toast & loading */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; font-size: 0.88rem; box-shadow: 0 6px 20px rgba(0,0,0,0.2); max-width: 90vw;
}
.loading { display: flex; gap: 10px; align-items: center; color: var(--muted); padding: 30px 0; }
.spinner {
  width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }
