:root {
  --bg: #0d1117;
  --bg-2: #161b22;
  --bg-3: #1f2937;
  --fg: #e6edf3;
  --fg-dim: #8b949e;
  --accent: #d97757;
  --accent-2: #b25c3a;
  --border: #30363d;
  --good: #3fb950;
  --bad: #f85149;
  --warn: #d29922;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 14px;
  height: 100%;
}

body { display: flex; flex-direction: column; min-height: 100vh; }

a { color: var(--accent); }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  cursor: pointer;
}
.brand .logo { color: var(--accent); font-size: 18px; }
.crumbs { color: var(--fg-dim); font-size: 13px; }
.crumbs .crumb { cursor: pointer; }
.crumbs .crumb:hover { color: var(--fg); }
.crumbs .sep { margin: 0 6px; color: var(--border); }
.user { display: flex; align-items: center; gap: 12px; color: var(--fg-dim); }

button {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--bg-3);
  color: var(--fg);
  border-radius: var(--radius);
  padding: 6px 14px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
button.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.ghost { background: transparent; }

input, textarea, select {
  font: inherit;
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
}
textarea { resize: vertical; min-height: 60px; font-family: var(--sans); }

main#root {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ---------- pages ---------- */
.page {
  padding: 20px 24px;
  width: 100%;
  overflow: auto;
}
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.page-header h2 { margin: 0; }
.page h3 { color: var(--fg-dim); font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px; margin-top: 28px; }
.actions { display: flex; gap: 8px; }
.hint { color: var(--fg-dim); font-size: 12px; margin: 4px 0 0; font-family: var(--mono); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

.card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.05s;
}
.card:hover { border-color: var(--accent); }
.card:active { transform: scale(0.99); }
.card .title { font-weight: 600; font-size: 15px; }
.card .meta { color: var(--fg-dim); font-size: 12px; margin-top: 6px; display: flex; justify-content: space-between; }
.card .preview { color: var(--fg-dim); font-size: 12px; margin-top: 8px; line-height: 1.4; max-height: 60px; overflow: hidden; }
.card .role-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.card .role-emoji { font-size: 18px; }
.card .delete-x {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  border: none;
  color: var(--fg-dim);
  padding: 4px 8px;
}
.card { position: relative; }
.card .delete-x:hover { color: var(--bad); }

.status-pill {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-3);
  color: var(--fg-dim);
  font-family: var(--mono);
}
.status-pill.running { background: var(--warn); color: #1b1300; }
.status-pill.idle { background: #233; color: var(--fg-dim); }
.status-pill.error { background: var(--bad); color: #fff; }
.status-pill.closed { opacity: 0.5; }

/* ---------- chat ---------- */
.chat-page {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}
.chat-header h2 { margin: 0; font-size: 16px; }
.chat-header .hint { margin-top: 2px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.msg {
  max-width: min(820px, 90%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  background: var(--bg-2);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
}
.msg.user { align-self: flex-end; background: #2a3441; border-color: #3a4a60; }
.msg.assistant { align-self: flex-start; background: var(--bg-2); }
.msg.system { align-self: center; background: transparent; border-style: dashed; color: var(--fg-dim); font-size: 12px; font-family: var(--mono); }
.msg .who { font-size: 11px; color: var(--fg-dim); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.msg .tool {
  background: #0d1620;
  border-left: 3px solid var(--accent);
  padding: 6px 10px;
  font-family: var(--mono);
  font-size: 12px;
  margin: 6px 0;
  border-radius: 4px;
  color: var(--fg-dim);
}
.msg img { max-width: 100%; border-radius: 6px; margin-top: 6px; }

.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-2);
  align-items: flex-end;
}
.composer textarea { flex: 1; max-height: 200px; }
.upload-btn {
  font-size: 18px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-3);
  cursor: pointer;
  user-select: none;
}
.upload-btn:hover { border-color: var(--accent); }
.upload-list {
  padding: 0 16px 8px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.upload-chip {
  background: var(--bg-3);
  border: 1px solid var(--border);
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  color: var(--fg-dim);
}

/* ---------- modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  min-width: 360px;
  max-width: 540px;
  width: 90%;
}
.modal-card h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal-card label { display: block; margin-top: 10px; font-size: 13px; color: var(--fg-dim); }
.modal-card input, .modal-card textarea, .modal-card select { margin-top: 4px; }

.role-checks { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.role-checks label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--fg); }
.role-checks input { width: auto; }

.error { color: var(--bad); font-size: 13px; min-height: 1em; margin-top: 8px; }

/* ---------- login ---------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0d1117 0%, #161b22 100%);
}
.login-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 360px;
  max-width: 92vw;
}
.login-card h1 { margin: 0 0 4px; color: var(--accent); }
.login-card .subtitle { color: var(--fg-dim); margin: 0 0 20px; font-size: 13px; }
.login-card label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--fg-dim); }
.login-card label input { margin-top: 4px; }
.login-card button { width: 100%; padding: 10px; margin-top: 8px; }

/* ---------- markdown ---------- */
.markdown { line-height: 1.55; }
.markdown > *:first-child { margin-top: 0; }
.markdown > *:last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 8px; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 {
  margin: 14px 0 6px;
  line-height: 1.25;
  font-weight: 600;
}
.markdown h1 { font-size: 1.4em; }
.markdown h2 { font-size: 1.2em; }
.markdown h3 { font-size: 1.05em; color: var(--accent); }
.markdown h4 { font-size: 1em; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.markdown ul, .markdown ol { margin: 4px 0 8px; padding-left: 22px; }
.markdown li { margin: 2px 0; }
.markdown code {
  background: #0d1620;
  padding: 1px 6px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: #f0d49a;
}
.markdown pre {
  background: #0d1620;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.45;
}
.markdown pre code {
  background: transparent;
  padding: 0;
  color: var(--fg);
  font-size: inherit;
}
.markdown blockquote {
  border-left: 3px solid var(--accent);
  padding: 2px 12px;
  color: var(--fg-dim);
  margin: 8px 0;
  background: rgba(217, 119, 87, 0.05);
}
.markdown a { color: var(--accent); }
.markdown table {
  border-collapse: collapse;
  margin: 8px 0;
}
.markdown th, .markdown td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  font-size: 13px;
}
.markdown th { background: var(--bg-3); }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }

.msg .who { font-family: var(--mono); }
.msg .body.markdown { white-space: normal; }
.msg.user .body.markdown { white-space: normal; }

.tool {
  background: #0d1620;
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  padding: 6px 10px;
  margin: 6px 0;
  font-family: var(--mono);
  font-size: 12px;
}
.tool .tool-name { font-weight: 600; color: var(--accent); }
.tool .tool-input {
  margin-top: 4px;
  color: var(--fg-dim);
  white-space: pre-wrap;
  word-break: break-all;
}

/* ---------- toasts ---------- */
.toast-container {
  position: fixed;
  top: 64px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
  max-width: 360px;
}
.toast {
  pointer-events: auto;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  cursor: pointer;
  opacity: 0;
  transform: translateX(20px);
  transition: opacity 0.25s, transform 0.25s;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast:hover { border-color: var(--accent); }
.toast.error { border-left-color: var(--bad); }
.toast.msg { border-left-color: var(--good); }
.toast-title { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.toast-body {
  font-size: 12px;
  color: var(--fg-dim);
  white-space: pre-wrap;
  max-height: 80px;
  overflow: hidden;
}
