:root {
  --bg: #f5f3f8;
  --surface: #ffffff;
  --surface-2: #f8f6fb;
  --text: #1d1b22;
  --muted: #6c6677;
  --border: #e2dde9;
  --accent: #7c3fd0;
  --accent-text: #ffffff;
  --accent-soft: rgba(124, 63, 208, 0.1);
  --chip-text: #5b2ea3;

  --high: #d93c3c;
  --high-soft: rgba(217, 60, 60, 0.1);
  --mid: #d98a12;
  --mid-soft: rgba(217, 138, 18, 0.12);
  --low: #1f9a55;
  --low-soft: rgba(31, 154, 85, 0.12);
  --rule: #2f73d6;
  --rule-soft: rgba(47, 115, 214, 0.1);
  --on-tone: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15131a;
    --surface: #1e1b25;
    --surface-2: #25212e;
    --text: #ece8f2;
    --muted: #a39cb0;
    --border: #332e3d;
    --accent: #b98ae6;
    --accent-text: #1a1320;
    --accent-soft: rgba(185, 138, 230, 0.14);
    --chip-text: #d4b6f2;

    --high: #f06a6a;
    --high-soft: rgba(240, 106, 106, 0.14);
    --mid: #f0b048;
    --mid-soft: rgba(240, 176, 72, 0.14);
    --low: #4cc987;
    --low-soft: rgba(76, 201, 135, 0.14);
    --rule: #7aa8f0;
    --rule-soft: rgba(122, 168, 240, 0.14);
    --on-tone: #15131a;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  padding: 24px 16px 40px;
}

main { max-width: 1120px; margin: 0 auto; }

h1, h2, h3, p { margin: 0; }

.top { max-width: 1120px; margin: 0 auto 18px; }
.top h1 { font-size: 1.7rem; letter-spacing: -0.01em; }
.sub { color: var(--muted); margin-top: 4px; max-width: 62ch; }

button, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.55; cursor: default; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.primary:hover:not(:disabled) { filter: brightness(1.08); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------- Toolbar, note, stats ---------------- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px 16px;
  margin-bottom: 12px;
}
.group { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filters label { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 0.9rem; }
.filters select { padding: 6px 8px; }

.note {
  font-size: 0.86rem;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  margin-bottom: 14px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 0 0 16px;
}
.stats div {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
}
.stats dt { font-size: 0.8rem; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }

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

.layout {
  display: grid;
  grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.queue h2 { font-size: 0.95rem; color: var(--muted); font-weight: 600; margin: 0 0 8px 2px; }
#queue { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

.item {
  width: 100%;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  text-align: left;
  padding: 10px 12px;
  border-radius: 12px;
}
.item.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); background: var(--accent-soft); }
.item-body { min-width: 0; display: grid; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-meta { font-size: 0.8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.score {
  width: 42px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-weight: 700;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
.score.none { background: var(--surface-2); color: var(--muted); border: 1px dashed var(--border); }
.score.high { background: var(--high); color: var(--on-tone); }
.score.mid { background: var(--mid); color: var(--on-tone); }
.score.low { background: var(--low); color: var(--on-tone); }
.score.busy { background: var(--accent-soft); }
.score.busy::after {
  content: "";
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  border-right-color: transparent;
  animation: spin 0.8s linear infinite;
}

.chip {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--chip-text);
  white-space: nowrap;
}
.chip.muted { background: var(--surface-2); color: var(--muted); }
.chip.busy { background: transparent; color: var(--accent); }

.empty { color: var(--muted); padding: 14px 4px; }

/* ---------------- Detail pane ---------------- */

.detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px 20px;
  min-width: 0;
  scroll-margin-top: 12px;
}
.detail:focus { outline: none; }
.detail-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.detail-head h2 { font-size: 1.3rem; line-height: 1.25; }
.eyebrow { font-size: 0.8rem; color: var(--muted); margin-bottom: 2px; }
.run { flex-shrink: 0; }
.desc { margin-top: 10px; }

.fields {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 0;
}
.fields div { background: var(--surface-2); border-radius: 8px; padding: 7px 10px; min-width: 0; }
.fields dt { font-size: 0.75rem; color: var(--muted); }
.fields dd { margin: 0; font-weight: 600; font-size: 0.9rem; overflow-wrap: anywhere; }
.fields dd.warn { color: var(--high); }

.detail h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 18px 0 8px; }

.hint {
  margin-top: 18px;
  padding: 16px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  color: var(--muted);
}

/* Result */
.result {
  margin-top: 16px;
  border-radius: 12px;
  padding: 14px 16px 16px;
  border: 1px solid var(--border);
}
.result.enter { animation: rise 0.35s ease-out; }
.result.high { --tone: var(--high); background: var(--high-soft); border-color: transparent; }
.result.mid { --tone: var(--mid); background: var(--mid-soft); border-color: transparent; }
.result.low { --tone: var(--low); background: var(--low-soft); border-color: transparent; }
.result h3:first-of-type { margin-top: 14px; }

.meter-row { display: flex; gap: 14px; align-items: center; }
.big-score { color: var(--tone); line-height: 1; }
.big-score strong { font-size: 2.4rem; font-variant-numeric: tabular-nums; }
.big-score span { font-weight: 600; margin-left: 2px; }
.meter-info { flex: 1; min-width: 0; }
.meter-label { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 6px; }
.level { font-weight: 700; color: var(--tone); }
.meter { height: 8px; border-radius: 999px; background: var(--surface); overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--tone); border-radius: inherit; transform-origin: left; }
.result.enter .meter span { animation: grow 0.6s ease-out; }

.actions { margin: 0; padding-left: 20px; display: grid; gap: 4px; }
.gaps { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.gaps li { display: flex; gap: 8px; align-items: baseline; }
.src {
  flex-shrink: 0;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
}
.src.ai { background: var(--accent-soft); color: var(--chip-text); }
.src.rule { background: var(--rule-soft); color: var(--rule); }
.muted { color: var(--muted); }

/* Agent timeline */
.timeline { list-style: none; margin: 0; padding: 0; position: relative; }
.timeline::before {
  content: "";
  position: absolute;
  left: 7px;
  top: 10px;
  bottom: 10px;
  width: 2px;
  background: var(--border);
}
.step { position: relative; display: flex; gap: 12px; padding: 5px 0; }
.step.enter { animation: rise 0.3s ease-out; }
.step .dot {
  position: relative;
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  margin-top: 3px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--surface);
}
.step.rules .dot { background: var(--rule); }
.step.thinking .dot { background: var(--surface); border: 2px solid var(--accent); animation: pulse 1s ease-in-out infinite; }
.step-body { min-width: 0; flex: 1; }
.step-title { font-weight: 600; }
.step.thinking .step-title { color: var(--muted); font-weight: 500; }
.step-num { font-size: 0.75rem; color: var(--muted); font-weight: 600; margin-right: 8px; }
.step-out { color: var(--muted); font-size: 0.9rem; }

details summary { cursor: pointer; color: var(--accent); font-size: 0.8rem; width: fit-content; }
details pre {
  margin: 6px 0 4px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.78rem;
  line-height: 1.4;
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.call { margin-top: 2px; }
.api { margin-top: 18px; }
.api summary { font-size: 0.85rem; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: 0.35; } }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } }
@keyframes grow { from { transform: scaleX(0); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}

/* ---------------- Narrow screens (and small desktop windows) ---------------- */

@media (max-width: 760px) {
  body { padding: 16px 16px 32px; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar .group:first-child button { flex: 1; }
}
