/* ================= Loop — thème ================= */
:root {
  color-scheme: dark;
  --bg: #0d0f14;
  --bg-2: #12151c;
  --panel: #161a23;
  --panel-2: #1c2130;
  --line: #262c3b;
  --line-2: #323a4e;
  --text: #e8ebf2;
  --text-2: #aab1c3;
  --muted: #7a8296;
  --accent: #8b7cff;
  --accent-2: #a99dff;
  --judge: #f5b544;
  --judge-hi: #ffd88a;
  --judge-lo: #b9781a;
  --judge-soft: rgba(245, 181, 68, 0.14);
  --worker: #45c8f0;
  --worker-hi: #9be6ff;
  --worker-lo: #1878a3;
  --worker-soft: rgba(69, 200, 240, 0.14);
  --ok: #3fd18b;
  --ok-soft: rgba(63, 209, 139, 0.14);
  --warn: #f5b544;
  --bad: #ff6b6b;
  --bad-soft: rgba(255, 107, 107, 0.14);
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --font: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'Cascadia Code', 'JetBrains Mono', Menlo, Consolas, monospace;
  --topbar-h: 56px;
  --tabbar-h: 0px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
a { color: var(--accent-2); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); min-height: 1.2em; margin: 0; font-size: 14px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ================= Boutons ================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line-2); background: var(--panel-2); border-radius: 10px;
  padding: 9px 16px; font-weight: 600; transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { background: #242a3b; }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #0d0b1f; }
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--panel-2); color: var(--text); }
.btn-sm { padding: 6px 11px; font-size: 13.5px; }
.btn-block { width: 100%; }
.btn svg { width: 17px; height: 17px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; flex: none;
  border-radius: 10px; border: 1px solid transparent; background: transparent; color: var(--text-2);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn:disabled { opacity: .4; cursor: default; }

/* ================= Marque ================= */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 750; letter-spacing: -.01em; font-size: 17px; }
.brand-lg { font-size: 30px; justify-content: center; gap: 12px; }
.logo {
  width: 26px; height: 26px; border-radius: 50%; flex: none; position: relative;
  background: conic-gradient(from 200deg, var(--judge), var(--accent), var(--worker), var(--judge));
  -webkit-mask: radial-gradient(circle, transparent 42%, #000 44%);
  mask: radial-gradient(circle, transparent 42%, #000 44%);
}
.brand-lg .logo { width: 38px; height: 38px; }

/* ================= Connexion ================= */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(1200px 600px at 50% -10%, rgba(139,124,255,.18), transparent 60%), var(--bg); }
.login-card { width: min(380px, 100%); display: grid; gap: 16px; text-align: center; background: var(--panel);
  border: 1px solid var(--line); border-radius: 20px; padding: 32px 26px; box-shadow: var(--shadow); }
.login-card p { margin: 0; }

/* ================= Champs ================= */
.field { display: grid; gap: 6px; text-align: left; }
.field > span { font-size: 13px; color: var(--text-2); font-weight: 600; }
input[type=password], input[type=text], input[type=number], select, textarea {
  width: 100%; background: var(--bg-2); border: 1px solid var(--line-2); border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; color: var(--text-2); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.seg { display: inline-flex; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; color: var(--text-2); padding: 5px 10px; border-radius: 7px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.seg button[aria-selected=true], .seg button.on { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 0 rgba(255,255,255,.04) inset, 0 1px 4px rgba(0,0,0,.3); }
.seg button:disabled { opacity: .35; cursor: not-allowed; }
.seg-fill { display: flex; }
.seg-fill button { flex: 1; }

/* ================= Structure ================= */
.app { height: 100dvh; display: flex; flex-direction: column; padding-top: var(--safe-t); }
.topbar {
  height: var(--topbar-h); flex: none; display: flex; align-items: center; gap: 10px; padding: 0 10px 0 8px;
  border-bottom: 1px solid var(--line); background: rgba(13,15,20,.85); backdrop-filter: blur(10px); z-index: 5;
}
.loop-heading { min-width: 0; flex: 1; display: flex; align-items: center; gap: 10px; padding-left: 8px; border-left: 1px solid var(--line); margin-left: 4px; }
.loop-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-2); font-weight: 550; }
.project-tag { flex: none; font: 600 12px var(--mono); color: var(--accent-2); background: rgba(139,124,255,.12); padding: 3px 8px; border-radius: 6px; white-space: nowrap; }
.project-pick { display: grid; gap: 6px; }
.project-pick > span { font-size: 13px; color: var(--text-2); font-weight: 600; }
.project-pick small { font-size: 12px; }
.topbar-actions { display: flex; align-items: center; gap: 2px; }

.status-pill {
  display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; font-weight: 700; padding: 4px 10px; border-radius: 99px;
  background: var(--panel-2); color: var(--text-2); white-space: nowrap; border: 1px solid var(--line);
}
.status-pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-pill[data-status=running] { color: var(--accent-2); }
.status-pill[data-status=running]::before { animation: blink 1.2s infinite; }
.status-pill[data-status=approved] { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.status-pill[data-status=awaiting_user] { color: var(--judge); background: var(--judge-soft); border-color: transparent; }
.status-pill[data-status=failed], .status-pill[data-status=max_iterations] { color: var(--bad); background: var(--bad-soft); border-color: transparent; }
@keyframes blink { 50% { opacity: .25; } }

.layout {
  flex: 1; min-height: 0; display: grid;
  grid-template-columns: minmax(340px, 430px) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "chat loop" "chat result";
}
.pane { min-height: 0; min-width: 0; }
.pane-chat { grid-area: chat; display: flex; flex-direction: column; border-right: 1px solid var(--line); background: var(--bg-2); }
.pane-loop { grid-area: loop; padding: 14px 18px 10px; border-bottom: 1px solid var(--line); }
.pane-result { grid-area: result; display: flex; flex-direction: column; overflow: hidden; }
.tabbar { display: none; }

/* ================= Visualisation ================= */
.viz-wrap { display: grid; place-items: center; }
.viz { width: 100%; max-width: 640px; height: auto; overflow: visible; }
.viz .arc { fill: none; stroke: var(--line-2); stroke-width: 2.5; stroke-dasharray: 3 7; transition: stroke .3s; }
.viz .arrow-head { fill: var(--line-2); }
.viz .arc.flow { stroke: var(--accent); stroke-width: 3; stroke-dasharray: 8 8; animation: flow .7s linear infinite; filter: drop-shadow(0 0 6px rgba(139,124,255,.6)); }
.viz .arc-label { fill: var(--muted); font-size: 13px; font-weight: 600; letter-spacing: .02em; }
@keyframes flow { to { stroke-dashoffset: -16; } }

.node { cursor: pointer; outline: none; }
.node .node-body { transition: opacity .3s, filter .3s; opacity: .38; filter: saturate(.4); }
.node .node-ring { fill: none; stroke: var(--line-2); stroke-width: 2; stroke-dasharray: 2 6; transform-box: fill-box; transform-origin: center; transition: stroke .3s; }
.node .node-icon path, .node .node-icon rect { fill: none; stroke: #10131a; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.node .node-icon .fill { fill: #10131a; }
.node .node-icon { opacity: .6; }
.node .node-name { font-size: 11px; font-weight: 800; letter-spacing: .1em; fill: #10131a; opacity: .7; }
.node .worker-name { font-size: 9.5px; letter-spacing: .05em; }
.node:hover .node-body, .node:focus-visible .node-body { opacity: .7; }
.node.active .node-body { opacity: 1; filter: none; }
.node.active .node-icon, .node.active .node-name { opacity: 1; }
.node.active .node-ring { stroke-width: 3; stroke-dasharray: 14 10; animation: spin 3s linear infinite; }
#node-judge.active .node-ring { stroke: var(--judge); filter: drop-shadow(0 0 10px var(--judge)); }
#node-worker.active .node-ring { stroke: var(--worker); filter: drop-shadow(0 0 10px var(--worker)); }
.node.done .node-body { opacity: 1; filter: none; }
.node.done .node-ring { stroke: var(--ok); stroke-dasharray: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.viz-iter { font-size: 28px; font-weight: 800; fill: var(--text); letter-spacing: -.02em; }
.viz-phase { font-size: 15px; fill: var(--text-2); font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.viz-phase.ok { fill: var(--ok); }

.agent-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 6px; }
.agent-card {
  position: relative; text-align: left; display: grid; gap: 4px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--line); transition: border-color .2s, background .2s, box-shadow .2s; min-width: 0;
}
.agent-card:hover { border-color: var(--line-2); background: var(--panel-2); }
.agent-card.active[data-agent=judge] { border-color: var(--judge); box-shadow: 0 0 0 3px var(--judge-soft); }
.agent-card.active[data-agent=worker] { border-color: var(--worker); box-shadow: 0 0 0 3px var(--worker-soft); }
.agent-card-head { display: flex; align-items: center; gap: 8px; }
.agent-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.agent-dot.judge { background: var(--judge); }
.agent-dot.worker { background: var(--worker); }
.agent-state { margin-left: auto; font-size: 11.5px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
  padding: 2px 8px; border-radius: 99px; background: var(--bg-2); white-space: nowrap; }
.agent-state.working { color: #0d0f14; background: var(--accent-2); animation: pulse 1.6s ease-in-out infinite; }
.agent-state.done { color: var(--ok); background: var(--ok-soft); }
@keyframes pulse { 50% { opacity: .65; } }
.agent-meta { font-size: 12.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-activity { font: 12.5px/1.4 var(--mono); color: var(--accent-2); min-height: 1.4em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-stats { font-size: 12px; color: var(--muted); }
.agent-open { position: absolute; right: 12px; bottom: 10px; font-size: 12px; color: var(--muted); font-weight: 600; }
.agent-card:hover .agent-open { color: var(--text); }

.score-strip { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; min-height: 26px; align-items: center; }
.score-strip .label { font-size: 12px; color: var(--muted); margin-right: 4px; }
.score-chip { font: 700 12px/1 var(--mono); padding: 6px 8px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); }
.score-chip.bad { color: var(--bad); border-color: rgba(255,107,107,.35); }
.score-chip.mid { color: var(--warn); border-color: rgba(245,181,68,.35); }
.score-chip.good { color: var(--ok); border-color: rgba(63,209,139,.45); background: var(--ok-soft); }

/* ================= Résultat ================= */
.result-head { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.result-head h2 { margin: 0; font-size: 15px; }
.result-badge { font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: var(--panel-2); color: var(--text-2); }
.result-badge.approved { background: var(--ok-soft); color: var(--ok); }
.result-badge.rejected { background: var(--bad-soft); color: var(--bad); }
.result-badge.reviewing { background: var(--judge-soft); color: var(--judge); }
.result-actions { margin-left: auto; display: flex; gap: 2px; }
#result-tabs { margin-left: 4px; }
.result-body { flex: 1; overflow: auto; padding: 18px 22px 40px; overscroll-behavior: contain; }
.empty { color: var(--muted); text-align: center; padding: 40px 20px; }
.files-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.files-list li { display: flex; align-items: center; gap: 10px; padding: 9px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; font: 13px var(--mono); }
.files-list .fname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files-list .fsize { color: var(--muted); }
.files-list a { text-decoration: none; font-family: var(--font); font-weight: 600; font-size: 13px; }

/* ================= Markdown ================= */
.markdown { word-wrap: break-word; }
.markdown > :first-child { margin-top: 0; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.25; margin: 1.3em 0 .5em; }
.markdown h1 { font-size: 1.45em; } .markdown h2 { font-size: 1.22em; } .markdown h3 { font-size: 1.06em; }
.markdown p, .markdown ul, .markdown ol { margin: .6em 0; }
.markdown li + li { margin-top: .2em; }
.markdown code { font: .88em var(--mono); background: var(--panel-2); padding: .15em .4em; border-radius: 5px; }
.markdown pre { background: #0a0c10; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; overflow: auto; }
.markdown pre code { background: none; padding: 0; font-size: 12.8px; }
.markdown blockquote { margin: .8em 0; padding: .2em 1em; border-left: 3px solid var(--line-2); color: var(--text-2); }
.markdown table { border-collapse: collapse; display: block; overflow: auto; max-width: 100%; }
.markdown th, .markdown td { border: 1px solid var(--line); padding: 6px 10px; }
.markdown hr { border: 0; border-top: 1px solid var(--line); }
.markdown img { max-width: 100%; }

/* ================= Conversation ================= */
.chat-log { flex: 1; overflow: auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 12px; overscroll-behavior: contain; }
.welcome { margin: auto 0; padding: 10px 6px; display: grid; gap: 14px; }
.welcome h1 { font-size: 22px; margin: 0; letter-spacing: -.02em; }
.welcome p { margin: 0; color: var(--text-2); }
.flow-steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; counter-reset: s; }
.flow-steps li { counter-increment: s; display: flex; gap: 10px; align-items: flex-start; font-size: 14px; color: var(--text-2); }
.flow-steps li::before { content: counter(s); flex: none; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 800; background: var(--panel-2); color: var(--text); }
.config-chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; padding: 5px 10px; border-radius: 99px; background: var(--panel); border: 1px solid var(--line); color: var(--text-2); }
button.chip:hover { border-color: var(--line-2); color: var(--text); }

.msg { display: flex; flex-direction: column; gap: 4px; max-width: 100%; animation: rise .25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg-user { align-self: flex-end; max-width: 88%; }
.msg-user .bubble { background: var(--accent); color: #0d0b1f; border-radius: 16px 16px 4px 16px; padding: 9px 13px; white-space: pre-wrap; font-weight: 500; }
.msg-user .msg-note { font-size: 12px; color: var(--muted); text-align: right; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 8px; padding: 9px 12px; font-size: 13px; font-weight: 700; border-bottom: 1px solid var(--line); }
.card-head .who { display: inline-flex; align-items: center; gap: 7px; }
.card-head .when { margin-left: auto; font-weight: 500; color: var(--muted); font-size: 12px; }
.card-body { padding: 10px 13px; font-size: 14px; }
.card.judge .card-head { color: var(--judge); background: linear-gradient(90deg, var(--judge-soft), transparent); }
.card.worker .card-head { color: var(--worker); background: linear-gradient(90deg, var(--worker-soft), transparent); }
.card.final { border-color: rgba(63,209,139,.5); box-shadow: 0 0 0 3px var(--ok-soft); }
.card.final .card-head { color: var(--ok); background: linear-gradient(90deg, var(--ok-soft), transparent); }
.card.questions { border-color: rgba(245,181,68,.5); }
.score-tag { font: 800 12px/1 var(--mono); padding: 4px 7px; border-radius: 6px; background: var(--bg-2); }
.score-tag.bad { color: var(--bad); } .score-tag.mid { color: var(--warn); } .score-tag.good { color: var(--ok); }
details.more { border-top: 1px solid var(--line); }
details.more > summary { cursor: pointer; padding: 8px 13px; font-size: 13px; color: var(--text-2); font-weight: 600; list-style: none; }
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::before { content: '▸ '; }
details.more[open] > summary::before { content: '▾ '; }
details.more > .markdown { padding: 0 13px 12px; font-size: 13.5px; }
.preview { max-height: 9.5em; overflow: hidden; position: relative; }
.preview::after { content: ''; position: absolute; inset: auto 0 0 0; height: 3em; background: linear-gradient(transparent, var(--panel)); }
.card-actions { display: flex; gap: 6px; padding: 0 10px 10px; }
.sys { align-self: center; text-align: center; font-size: 12.5px; color: var(--muted); max-width: 92%; padding: 4px 10px; }
.sys.error { color: var(--bad); }

.typing { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-2); padding: 8px 12px; border-radius: 12px; background: var(--panel); border: 1px dashed var(--line-2); cursor: pointer; }
.typing .dots { display: inline-flex; gap: 3px; }
.typing .dots i { width: 5px; height: 5px; border-radius: 50%; background: currentColor; animation: bounce 1s infinite; }
.typing .dots i:nth-child(2) { animation-delay: .15s; } .typing .dots i:nth-child(3) { animation-delay: .3s; }
.typing.judge { color: var(--judge); } .typing.worker { color: var(--worker); }
.typing .act { color: var(--muted); font: 12px var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-3px); opacity: 1; } }

.composer { flex: none; padding: 8px 12px calc(10px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg-2); }
.composer-hint { font-size: 12px; color: var(--muted); padding: 0 4px 6px; min-height: 0; }
.composer-hint:empty { display: none; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border: 1px solid var(--line-2); border-radius: 14px; padding: 5px 5px 5px 12px; }
.composer-row:focus-within { border-color: var(--accent); }
.composer textarea:focus-visible { outline: none; }
.composer textarea { border: 0; background: transparent; padding: 7px 0; resize: none; max-height: 40vh; line-height: 1.45; font-size: 16px; }
.btn-send { background: var(--accent); color: #0d0b1f; }
.btn-send:hover { background: var(--accent-2); color: #0d0b1f; }
.btn-stop { color: var(--bad); }
.btn-stop svg { fill: currentColor; stroke: none; width: 16px; }

/* ================= Tiroir / modales ================= */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.drawer { position: fixed; z-index: 21; top: 0; bottom: 0; left: 0; width: min(340px, 88vw); background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding-top: var(--safe-t); animation: slide-in .22s ease-out; box-shadow: var(--shadow); }
@keyframes slide-in { from { transform: translateX(-100%); } }
.drawer-head, .drawer-foot { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px; }
.drawer-head { border-bottom: 1px solid var(--line); }
.drawer-foot { border-top: 1px solid var(--line); padding-bottom: calc(14px + var(--safe-b)); }
.loop-list { list-style: none; margin: 0; padding: 8px; overflow: auto; flex: 1; display: grid; gap: 2px; align-content: start; }
.loop-item { display: flex; align-items: center; gap: 10px; padding: 10px 10px; border-radius: 10px; cursor: pointer; }
.loop-item:hover { background: var(--panel-2); }
.loop-item.current { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
.loop-item .li-main { flex: 1; min-width: 0; }
.loop-item .li-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; font-weight: 550; }
.loop-item .li-sub { font-size: 12px; color: var(--muted); }
.loop-item .li-del { opacity: 0; width: 30px; height: 30px; }
.loop-item:hover .li-del, .loop-item.current .li-del { opacity: 1; }
.sdot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sdot.running { background: var(--accent); animation: blink 1.2s infinite; }
.sdot.approved { background: var(--ok); }
.sdot.awaiting_user { background: var(--judge); }
.sdot.failed, .sdot.max_iterations { background: var(--bad); }

.modal { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 16px; background: rgba(0,0,0,.55); animation: fade .2s; }
.modal-card { width: min(720px, 100%); max-height: calc(100dvh - 32px); overflow: auto; background: var(--panel); border: 1px solid var(--line-2);
  border-radius: 18px; padding: 18px 20px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.modal-sm { width: min(400px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; }
.agent-config-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.agent-config { border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px 14px; margin: 0; display: grid; gap: 12px; min-width: 0; }
.agent-config legend { display: flex; align-items: center; gap: 8px; font-weight: 750; padding: 0 6px; }
.loop-config { display: grid; grid-template-columns: 1fr 2fr; gap: 14px; align-items: end; }
.fast-hint { font-size: 12px; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 8px; color: var(--text-2); }
.kbd { font: 12px var(--mono); padding: 1px 6px; border: 1px solid var(--line-2); border-radius: 5px; }

/* ================= Terminal ================= */
.terminal-sheet {
  position: fixed; z-index: 15; right: 0; bottom: 0; left: min(430px, 40vw); height: 58vh;
  display: flex; flex-direction: column; background: #07080b; border-top: 1px solid var(--line-2); border-left: 1px solid var(--line-2);
  border-radius: 16px 0 0 0; box-shadow: 0 -20px 50px rgba(0,0,0,.5); animation: sheet-up .25s ease-out;
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }
.terminal-head { display: flex; align-items: center; gap: 9px; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--line); min-width: 0; }
.terminal-head strong { white-space: nowrap; }
.term-activity { font: 12px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
.terminal-actions { display: flex; align-items: center; gap: 4px; margin-left: auto; flex: none; }
.terminal-body { flex: 1; min-height: 0; padding: 8px 4px 4px 12px; padding-bottom: calc(4px + var(--safe-b)); }
.terminal-body .xterm { height: 100%; }
.terminal-body .xterm-viewport { background: transparent !important; }
.terminal-head .agent-state { margin-left: 0; }

.toast { position: fixed; left: 50%; bottom: calc(24px + var(--tabbar-h) + var(--safe-b)); transform: translateX(-50%); z-index: 40;
  background: var(--panel-2); border: 1px solid var(--line-2); padding: 9px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px; animation: rise .2s; }

/* ================= Mobile ================= */
@media (max-width: 900px) {
  :root { --tabbar-h: 60px; --topbar-h: 52px; }
  .brand-name { display: none; }
  .loop-heading { border-left: 0; padding-left: 0; margin-left: 0; }
  .layout { display: block; position: relative; }
  .pane { position: absolute; inset: 0; display: none; }
  .layout[data-tab=loop] .pane-loop { display: block; overflow: auto; }
  .layout[data-tab=chat] .pane-chat { display: flex; }
  .layout[data-tab=result] .pane-result { display: flex; }
  .pane-chat { border-right: 0; }
  .pane-loop { padding: 10px 14px 20px; border-bottom: 0; }
  .viz { max-width: 520px; }
  .viz .arc-label { font-size: 19px; }
  .viz-iter { font-size: 38px; }
  .viz-phase { font-size: 19px; }
  .node .node-name { font-size: 15px; }
  .node .worker-name { font-size: 12.5px; }
  .agent-card-head { flex-wrap: wrap; row-gap: 6px; }
  .agent-card-head .agent-state { margin-left: 0; order: 3; }
  .agent-cards { gap: 8px; }
  .agent-card { padding: 10px 11px 26px; }
  .result-head { padding: 10px 14px; }
  .result-body { padding: 14px 16px 30px; }
  .tabbar {
    display: flex; flex: none; height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
    border-top: 1px solid var(--line); background: var(--bg-2);
  }
  .tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border: 0; background: none;
    color: var(--muted); font-size: 11.5px; font-weight: 650; position: relative; }
  .tabbar button[aria-selected=true] { color: var(--text); }
  .tabbar button[aria-selected=true] svg { color: var(--accent-2); }
  .tab-dot { position: absolute; top: 8px; left: calc(50% + 10px); width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
  .terminal-sheet { left: 0; top: var(--safe-t); height: auto; border-radius: 0; border-left: 0; z-index: 25; }
  .agent-config-grid, .loop-config { grid-template-columns: 1fr; }
  .composer { padding-bottom: 10px; }
  .modal { padding: 0; align-items: end; }
  .modal-card { border-radius: 18px 18px 0 0; max-height: 92dvh; padding-bottom: calc(18px + var(--safe-b)); }
}
@media (max-width: 420px) {
  .seg button { padding: 5px 7px; font-size: 12.5px; }
  .result-actions .icon-btn { width: 34px; }
  .agent-meta { white-space: normal; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0s !important; animation-iteration-count: 1 !important; transition: none !important; }
}
