:root {
  --bg: #0b0e14;
  --bg-2: #11151f;
  --bg-3: #171c29;
  --border: #232a3a;
  --border-2: #2e3749;
  --text: #e6e9f0;
  --text-dim: #9aa3b5;
  --text-faint: #6b7488;
  --accent: #7c5cff;
  --accent-2: #5b8cff;
  --green: #2fd08a;
  --yellow: #f5b545;
  --red: #ef5b6b;
  --radius: 12px;
  --radius-sm: 8px;
  --font: "Inter", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  transition: margin-left .25s ease;
}

.sidebar.collapsed { margin-left: -300px; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.logo { display: flex; align-items: center; gap: 8px; }
.logo-mark { color: var(--accent); font-size: 20px; }
.logo-text { font-weight: 800; font-size: 16px; letter-spacing: -0.02em; }

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  display: grid;
  place-items: center;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }

.sidebar-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  overflow-y: auto;
  min-height: 0;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-faint);
  margin-bottom: 10px;
}

.add-btn {
  background: transparent;
  border: 1px dashed var(--border-2);
  color: var(--text-dim);
  width: 24px;
  height: 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
}
.add-btn:hover { border-color: var(--accent); color: var(--accent); }

.provider-list { display: flex; flex-direction: column; gap: 8px; }

.provider-item {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.provider-item:hover { border-color: var(--border-2); }
.provider-item.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.provider-item .p-name { font-weight: 600; font-size: 13px; }
.provider-item .p-meta { font-size: 11px; color: var(--text-faint); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.badge {
  font-size: 10px;
  padding: 1px 7px;
  border-radius: 20px;
  background: rgba(124, 92, 255, .16);
  color: #b39cff;
  font-weight: 600;
}
.badge.primary { background: rgba(47, 208, 138, .16); color: var(--green); }
.provider-item .row { display: flex; justify-content: space-between; align-items: center; }
.mini-btn {
  background: transparent; border: none; color: var(--text-faint);
  cursor: pointer; font-size: 12px; padding: 2px 4px; border-radius: 4px;
}
.mini-btn:hover { color: var(--red); background: rgba(239,91,107,.1); }

.hint { font-size: 11px; color: var(--text-faint); margin-top: 8px; }

.model-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
  margin-top: 8px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px;
}
.model-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
}
.model-item:hover { background: var(--bg-2); }
.model-item input { accent-color: var(--accent); width: 15px; height: 15px; }
.gw-btns { display: flex; gap: 8px; margin-bottom: 8px; }
.m-dot {
  width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0;
  background: var(--text-faint); display: inline-block;
}
.m-dot.ok { background: var(--green); }
.m-dot.err { background: var(--red); }
.m-dot.testing { background: var(--yellow); animation: pulse 1s infinite; }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--text-dim);
}
.field.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.field input[type="text"], .field input[type="password"], .field input[type="number"], .field select {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 13px;
  font-family: var(--font);
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); }
.field input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
.field input[type="number"] { width: 80px; }

.sidebar-foot { padding: 14px 16px; }

/* ---------- Main ---------- */
.main { display: flex; flex-direction: column; min-height: 0; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}

.topbar-title { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.topbar-title h1 { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.status { font-size: 12px; color: var(--text-faint); display: flex; align-items: center; gap: 6px; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.status.busy::before { background: var(--yellow); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.mobile-menu { display: none; }

.steps { display: flex; gap: 6px; align-items: center; }
.step {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-faint);
  padding: 4px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.step.done { color: var(--green); border-color: rgba(47,208,138,.3); background: rgba(47,208,138,.08); }
.step.current { color: #fff; border-color: var(--accent); background: rgba(124,92,255,.18); }

/* ---------- Chat ---------- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg { display: flex; gap: 12px; max-width: 900px; width: 100%; }
.msg.user { flex-direction: row-reverse; align-self: flex-end; }
.msg .avatar {
  width: 32px; height: 32px; border-radius: 8px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 15px; font-weight: 700;
}
.msg.assistant .avatar { background: rgba(124,92,255,.18); color: #b39cff; }
.msg.user .avatar { background: rgba(91,140,255,.18); color: #8fb1ff; }
.msg.system .avatar { background: rgba(245,181,69,.16); color: var(--yellow); }
.msg .bubble {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  max-width: 100%;
  overflow-wrap: break-word;
}
.msg.user .bubble { background: rgba(124,92,255,.14); border-color: rgba(124,92,255,.35); }
.msg .bubble p { margin-bottom: 8px; }
.msg .bubble p:last-child { margin-bottom: 0; }
.msg .bubble ul, .msg .bubble ol { margin: 8px 0 8px 20px; }
.msg .bubble pre {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 8px 0;
}
.msg .bubble code { font-family: var(--mono); font-size: 12.5px; }

.msg .meta {
  font-size: 10px; color: var(--text-faint); text-transform: uppercase;
  letter-spacing: .06em; font-weight: 600; margin-bottom: 6px;
}

.msg .speaker {
  font-size: 12px; font-weight: 700; margin-bottom: 4px;
  letter-spacing: -0.01em;
}

.agent-critique { display: flex; flex-direction: column; gap: 6px; }
.agent-critique .critique-text { font-size: 13px; color: var(--text-dim); white-space: pre-wrap; }

/* Typing indicator */
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 50% { opacity: .2; } }

/* Result card */
.result-card {
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  overflow: hidden;
}
.result-card .rc-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--bg-3);
}
.result-card .rc-title { font-weight: 700; }
.result-card .rc-actions { display: flex; gap: 8px; }
.result-card .rc-body { padding: 16px; }

/* Debate round */
.round-box {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  overflow: hidden;
}
.round-box .rb-head {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--bg-3); font-weight: 600; font-size: 13px;
}
.round-box .rb-head .scores { display: flex; gap: 6px; flex-wrap: wrap; }
.score-pill {
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px;
  background: var(--bg-2); border: 1px solid var(--border);
}
.score-pill.high { color: var(--green); border-color: rgba(47,208,138,.4); }
.score-pill.mid { color: var(--yellow); border-color: rgba(245,181,69,.4); }
.score-pill.low { color: var(--red); border-color: rgba(239,91,107,.4); }
.round-box .rb-body { padding: 10px 12px; font-size: 13px; }
.round-box details summary { cursor: pointer; color: var(--text-dim); font-size: 12px; margin-bottom: 6px; }
.round-box details summary:hover { color: var(--text); }

/* ---------- Composer ---------- */
.composer {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 20px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-2);
}
.composer-top { display: flex; align-items: center; }
.model-select { display: flex; align-items: center; gap: 8px; font-size: 12px; }
.model-select .ms-label { color: var(--text-faint); font-weight: 600; }
.model-select select {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 12px;
  font-family: var(--font);
  max-width: 340px;
}
.model-select select:focus { outline: none; border-color: var(--accent); }
.model-select .ms-agents { color: var(--text-faint); }

.composer-row { display: flex; gap: 10px; }
.composer-row textarea {
  flex: 1;
  resize: none;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-family: var(--font);
  font-size: 14px;
  max-height: 160px;
  min-height: 46px;
}
.composer-row textarea:focus { outline: none; border-color: var(--accent); }

.send-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  font-weight: 700;
  padding: 0 22px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
}
.send-btn:hover { background: #6b4bf0; }
.send-btn:disabled { opacity: .5; cursor: not-allowed; }
.cancel-btn {
  align-self: flex-end;
  background: transparent;
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
}
.cancel-btn:hover { background: rgba(239, 91, 107, .12); }

/* ---------- Buttons ---------- */
.primary-btn {
  background: var(--accent);
  border: none; color: #fff; font-weight: 600;
  padding: 9px 18px; border-radius: var(--radius-sm); cursor: pointer;
}
.primary-btn:hover { background: #6b4bf0; }
.ghost-btn {
  background: transparent; border: 1px solid var(--border); color: var(--text-dim);
  padding: 8px 14px; border-radius: var(--radius-sm); cursor: pointer; font-weight: 500;
  width: 100%;
}
.ghost-btn:hover { background: var(--bg-3); color: var(--text); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: none; align-items: center; justify-content: center; z-index: 100;
  backdrop-filter: blur(4px);
}
.modal.open { display: flex; }
.modal-card {
  background: var(--bg-2); border: 1px solid var(--border-2);
  border-radius: var(--radius); width: 440px; max-width: 92vw;
  max-height: 90vh; overflow-y: auto;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 15px; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); }
.close { border: none; background: transparent; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--bg-3); border: 1px solid var(--border-2); color: var(--text);
  padding: 10px 18px; border-radius: var(--radius); font-size: 13px;
  opacity: 0; transition: all .3s ease; z-index: 200; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--red); color: var(--red); }

/* ---------- Empty state ---------- */
.empty {
  margin: auto;
  text-align: center;
  color: var(--text-faint);
  max-width: 420px;
}
.empty .empty-mark { font-size: 42px; margin-bottom: 12px; }
.empty h2 { color: var(--text); font-size: 18px; margin-bottom: 8px; }
.empty p { font-size: 13px; line-height: 1.6; }

/* ---------- Scrollbar ---------- */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 20px; }
::-webkit-scrollbar-thumb:hover { background: #3a4458; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 300px; z-index: 50; }
  .sidebar.collapsed { margin-left: -300px; }
  .mobile-menu { display: grid; }
  .steps .step:not(.current):not(.done) { display: none; }
  .topbar-title h1 { font-size: 14px; }
  .composer { padding: 10px 12px; }
  .composer-row { gap: 6px; }
  .cancel-btn, .send-btn { padding: 10px; }
}

/* ---------- Sidebar (nova) ---------- */
.sidebar-section.grow { flex: 1; overflow-y: auto; }
.new-chat-btn {
  margin: 10px 16px;
  background: rgba(124, 92, 255, .12);
  border: 1px solid rgba(124, 92, 255, .35);
  color: #c4b3ff;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  width: calc(100% - 32px);
  text-align: left;
}
.new-chat-btn:hover { background: rgba(124, 92, 255, .22); }
.config-btn { display: flex; align-items: center; gap: 8px; justify-content: center; }

/* ---------- Config modal ---------- */
.modal-card-wide { width: 720px; }
.tabs { display: flex; gap: 6px; margin-left: auto; margin-right: 16px; }
.tab {
  background: transparent; border: 1px solid var(--border); color: var(--text-dim);
  padding: 6px 14px; border-radius: 20px; cursor: pointer; font-size: 12px; font-weight: 600;
}
.tab:hover { color: var(--text); }
.tab.active { border-color: var(--accent); color: #fff; background: rgba(124,92,255,.18); }
.section-title-row {
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 700; font-size: 13px; margin-bottom: 12px;
}
.section-title-row .primary-btn { font-size: 12px; padding: 7px 14px; }
.provider-list-config { display: flex; flex-direction: column; gap: 8px; }

/* ---------- Provider item (config) ---------- */
.star-btn {
  background: transparent; border: none; cursor: pointer; font-size: 17px;
  color: var(--text-faint); padding: 0 4px; line-height: 1;
}
.star-btn.on { color: var(--yellow); }
.star-btn:hover { color: var(--yellow); }
.debate-toggle {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--text-dim); margin-top: 4px; cursor: pointer;
}
.debate-toggle input { accent-color: var(--accent); width: 15px; height: 15px; }

/* ---------- Login ---------- */
.auth-overlay {
  position: fixed; inset: 0; z-index: 300;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
}
body:not(.auth-pending) .auth-overlay { display: none; }
body.auth-pending > .app,
body.auth-pending > .modal,
body.auth-pending > .toast { visibility: hidden; }
body:not(.auth-required) .auth-form { display: none; }
body.auth-required .auth-loading { display: none; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-card {
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  padding: 32px;
  width: 340px;
  max-width: 92vw;
  display: flex; flex-direction: column; gap: 12px;
}
.auth-card .auth-sub { color: var(--text-dim); text-align: center; font-size: 13px; }
.auth-card input {
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 11px 14px;
  font-size: 14px;
  font-family: var(--font);
}
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-error { color: var(--red); text-align: center; font-size: 12px; }
