:root {
  --bg: #0f1115;
  --bg-elev: #161922;
  --bg-elev-2: #1d212c;
  --border: #272c38;
  --text: #e6e9ef;
  --text-dim: #9aa3b2;
  --text-faint: #6b7280;
  --accent: #7c5cff;
  --accent-2: #5b8cff;
  --accent-soft: rgba(124, 92, 255, 0.14);
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must win over class-level `display` rules
   (.login / .app / .modal-backdrop all set display explicitly). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 22px;
}

.brand { display: flex; align-items: center; gap: 11px; padding: 4px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center;
  font-weight: 800; font-size: 15px; color: white;
  box-shadow: 0 4px 14px rgba(124, 92, 255, 0.4);
}
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { color: var(--text-dim); font-size: 12px; }

.actions { display: flex; flex-direction: column; gap: 5px; }
.actions-label {
  color: var(--text-faint); font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.08em; padding: 0 6px 4px; font-weight: 600;
}
.action {
  display: flex; align-items: center; gap: 10px;
  background: transparent; border: 1px solid transparent;
  color: var(--text); text-align: left;
  padding: 10px 11px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 14px; transition: all 0.13s ease;
}
.action:hover { background: var(--bg-elev-2); border-color: var(--border); }
.action-ico { font-size: 16px; width: 20px; text-align: center; }

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; }
.ghost {
  background: transparent; border: 1px solid var(--border);
  color: var(--text-dim); padding: 9px 11px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 13px; text-align: left; transition: all 0.13s ease;
}
.ghost:hover { color: var(--text); border-color: var(--accent); }
.conn {
  display: flex; align-items: center; gap: 7px;
  color: var(--text-faint); font-size: 12px; padding: 6px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.dot.ok { background: var(--good); }
.dot.bad { background: var(--bad); }

/* ---------- Main ---------- */
/* min-height:0 lets the flex column shrink so .stream can scroll instead of
   pushing the page past the viewport (default min-height:auto blocks this). */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100vh; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 26px; border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar h1 { margin: 0; font-size: 16px; font-weight: 600; }
.thread { color: var(--text-faint); font-size: 12px; margin-top: 2px; font-family: var(--mono); }
.cost { text-align: right; }
.cost-label { display: block; color: var(--text-faint); font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; }
.cost-val { font-family: var(--mono); font-size: 15px; color: var(--good); font-weight: 600; }

/* ---------- Stream ---------- */
.stream {
  flex: 1; min-height: 0; overflow-y: auto; padding: 26px;
  display: flex; flex-direction: column; gap: 16px;
}
.welcome { margin: auto; text-align: center; max-width: 460px; color: var(--text-dim); }
.welcome-emoji { font-size: 44px; }
.welcome h2 { color: var(--text); margin: 10px 0 6px; }
.welcome p { line-height: 1.5; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 18px; }
.chip {
  background: var(--accent-soft); border: 1px solid transparent; color: var(--text);
  padding: 8px 14px; border-radius: 999px; cursor: pointer; font-size: 13px;
  transition: all 0.13s ease;
}
.chip:hover { border-color: var(--accent); }

.msg { display: flex; gap: 12px; max-width: 92%; animation: rise 0.2s ease; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.avatar {
  width: 32px; height: 32px; border-radius: 9px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 15px;
}
.msg.user .avatar { background: var(--bg-elev-2); }
.msg.bot .avatar { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.bubble {
  background: var(--bg-elev); border: 1px solid var(--border);
  padding: 12px 15px; border-radius: var(--radius); line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
}
.msg.user .bubble { background: var(--accent-soft); border-color: transparent; }
.bubble .meta { color: var(--text-faint); font-size: 11px; margin-top: 8px; font-family: var(--mono); }

/* result cards */
.card-title { font-weight: 700; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.section { margin: 12px 0; }
.section h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent-2); }
.section ul { margin: 0; padding-left: 18px; }
.section li { margin: 3px 0; line-height: 1.45; }
.kv { color: var(--text-dim); font-size: 13px; }
.pill {
  display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px;
  font-weight: 600; margin-left: 6px;
}
.pill.high { background: rgba(248,113,113,0.18); color: var(--bad); }
.pill.medium { background: rgba(251,191,36,0.18); color: var(--warn); }
.pill.low { background: rgba(52,211,153,0.16); color: var(--good); }
details.raw { margin-top: 10px; }
details.raw summary { cursor: pointer; color: var(--text-faint); font-size: 12px; }
pre.json {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px; overflow-x: auto; font-family: var(--mono); font-size: 12px;
  color: var(--text-dim); margin: 8px 0 0; max-height: 320px;
}

/* approval card */
.approval { border-color: var(--accent); background: linear-gradient(180deg, var(--accent-soft), var(--bg-elev)); }
.approval-preview {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px; margin: 10px 0; white-space: pre-wrap; font-size: 13px; line-height: 1.5;
}
.approval-meta { display: flex; gap: 16px; flex-wrap: wrap; color: var(--text-dim); font-size: 13px; margin-bottom: 10px; }
.approval-btns { display: flex; gap: 10px; }

/* typing */
.typing .bubble { display: flex; gap: 5px; padding: 16px 15px; }
.typing .bubble span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite; }
.typing .bubble span:nth-child(2) { animation-delay: 0.2s; }
.typing .bubble span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,60%,100% { opacity: 0.25; } 30% { opacity: 1; } }

/* ---------- Composer ---------- */
.composer {
  display: flex; gap: 10px; padding: 16px 26px 22px;
  border-top: 1px solid var(--border); background: var(--bg);
}
#input {
  flex: 1; resize: none; max-height: 180px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 13px 15px; font-family: var(--font); font-size: 15px;
  line-height: 1.45; outline: none; transition: border 0.13s ease;
}
#input:focus { border-color: var(--accent); }
.send {
  width: 48px; border: none; border-radius: var(--radius); cursor: pointer;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white; font-size: 18px; flex-shrink: 0; transition: transform 0.1s ease, opacity 0.13s ease;
}
.send:hover { transform: scale(1.04); }
.send:disabled { opacity: 0.4; cursor: default; transform: none; }

/* ---------- Buttons ---------- */
.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: white;
  border: none; padding: 10px 18px; border-radius: var(--radius-sm); cursor: pointer;
  font-size: 14px; font-weight: 600; transition: transform 0.1s ease;
}
.primary:hover { transform: translateY(-1px); }
.btn-ok { background: var(--good); color: #062b1e; border: none; padding: 9px 18px; border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; }
.btn-no { background: transparent; color: var(--bad); border: 1px solid var(--bad); padding: 9px 18px; border-radius: var(--radius-sm); cursor: pointer; font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop, .drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6); backdrop-filter: blur(3px);
  display: grid; place-items: center; z-index: 50; animation: fade 0.15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  width: min(560px, 92vw); box-shadow: var(--shadow); max-height: 88vh; display: flex; flex-direction: column;
}
.modal-head, .drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px; border-bottom: 1px solid var(--border);
}
.modal-head h3, .drawer-head h3 { margin: 0; font-size: 16px; }
.modal-close { background: none; border: none; color: var(--text-dim); font-size: 18px; cursor: pointer; }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--border); }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.field input, .field textarea, .field select {
  width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); padding: 10px 12px; font-family: var(--font); font-size: 14px; outline: none;
}
.field textarea { resize: vertical; min-height: 120px; font-family: var(--mono); font-size: 13px; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.hint { color: var(--text-faint); font-size: 12px; margin-top: 4px; }

/* ---------- Drawer ---------- */
.drawer-backdrop { place-items: stretch; justify-content: flex-end; }
.drawer { background: var(--bg-elev); border-left: 1px solid var(--border); width: min(420px, 92vw); height: 100%; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.drawer-body { padding: 18px 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.mem-group h4 { margin: 0 0 6px; color: var(--accent-2); font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; }
.mem-item { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; line-height: 1.45; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev-2); border: 1px solid var(--border); color: var(--text);
  padding: 12px 18px; border-radius: var(--radius-sm); box-shadow: var(--shadow);
  z-index: 100; font-size: 14px; animation: rise 0.2s ease;
}
.toast.err { border-color: var(--bad); color: var(--bad); }

/* ---------- Login ---------- */
.login {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, rgba(124,92,255,0.18), var(--bg));
}
.login-card {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  width: min(380px, 92vw); padding: 32px 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.login-card h2 { margin: 6px 0 0; }
.login-sub { color: var(--text-dim); margin: 0 0 8px; font-size: 13px; }
.login-card .field { text-align: left; }
.brand-mark.big { width: 56px; height: 56px; border-radius: 16px; font-size: 22px; margin: 0 auto; }
.login-err { color: var(--bad); font-size: 13px; min-height: 18px; }
.link-btn {
  background: none; border: none; color: var(--accent-2); cursor: pointer;
  font-size: 13px; padding: 2px; text-decoration: underline;
}
.link-btn:hover { color: var(--accent); }

/* ---------- Agent selector ---------- */
.agent-select { display: flex; flex-direction: column; gap: 5px; }
.agent-select label {
  color: var(--text-faint); font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.08em; padding: 0 6px; font-weight: 600;
}
.agent-select select {
  background: var(--bg-elev-2); border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius-sm); padding: 9px 11px; font-size: 13px; outline: none; cursor: pointer;
}
.agent-select select:focus { border-color: var(--accent); }
#whoami { color: var(--text-faint); }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
