:root {
  --bg: #0b0f19;
  --bg-elev: #111827;
  --bg-card: #151e31;
  --bg-hover: #1c2840;
  --border: #24314a;
  --border-strong: #334566;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --ok: #10b981;
  --warn: #f59e0b;
  --danger: #ef4444;
  --info: #38bdf8;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.dim { color: var(--text-dim); }
.mono { font-family: var(--mono); font-size: 12px; }

a { color: var(--info); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-xs { padding: 4px 8px; font-size: 11px; }
.btn-full { width: 100%; }
.btn-icon { width: 32px; height: 32px; padding: 0; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-card); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg-hover); border-color: var(--border-strong); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn-danger { background: rgba(239, 68, 68, 0.14); color: #fca5a5; border-color: rgba(239, 68, 68, 0.35); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.25); }

/* Form fields */
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.form-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; color: var(--text-muted); font-weight: 500; }

input[type="text"],
input[type="password"],
input[type="url"],
input[type="date"],
input[type="search"],
input[type="color"],
select,
textarea {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  outline: none;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}
textarea { min-height: 90px; resize: vertical; }
input[type="color"] { height: 36px; padding: 3px; cursor: pointer; }

.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
}
.checkbox-inline input { width: auto; }

/* Alerts */
.alert {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  border: 1px solid transparent;
}
.alert-info { background: rgba(56, 189, 248, 0.1); border-color: rgba(56, 189, 248, 0.3); color: #bae6fd; }
.alert-error { background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.35); color: #fecaca; }
.alert-warn { background: rgba(245, 158, 11, 0.12); border-color: rgba(245, 158, 11, 0.35); color: #fde68a; }

/* Auth Screen */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #18223d 0%, var(--bg) 60%);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.auth-brand { display: flex; align-items: center; gap: 14px; }
.auth-brand h1 { margin: 0; font-size: 20px; }
.auth-brand p { margin: 2px 0 0; font-size: 12px; }
.brand-icon { width: 46px; height: 46px; }
.brand-icon-sm { width: 32px; height: 32px; }

/* App Shell */
.app-shell {
  display: flex;
  min-height: 100vh;
}
.sidebar {
  width: 245px;
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
.brand-title { font-weight: 700; font-size: 15px; line-height: 1.2; }
.brand-sub { font-size: 11px; color: var(--text-muted); }

.nav-menu { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active {
  background: rgba(99, 102, 241, 0.16);
  color: #e0e7ff;
}
.nav-dot {
  width: 7px;
  height: 7px;
  border-radius: 99px;
  background: var(--border-strong);
  flex-shrink: 0;
}
.nav-item.active .nav-dot { background: var(--primary); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
}
.status-dot { width: 8px; height: 8px; border-radius: 99px; flex-shrink: 0; }
.status-dot.ok { background: var(--ok); }
.status-dot.warn { background: var(--warn); }
.status-dot.danger { background: var(--danger); }

.main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  height: 58px;
  border-bottom: 1px solid var(--border);
  background: rgba(17, 24, 39, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 22px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.topbar-left h2 { margin: 0; font-size: 16px; font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.mobile-only { display: none; }

.view-root {
  padding: 20px 22px 40px;
  max-width: 1500px;
  width: 100%;
  margin: 0 auto;
}

/* Cards & Grids */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.kpi-label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.kpi-value { font-size: 26px; font-weight: 700; line-height: 1.1; }
.kpi-sub { font-size: 12px; color: var(--text-dim); margin-top: 6px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }

.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.panel-header h3 { margin: 0; font-size: 14px; font-weight: 600; }

/* Pills & Badges */
.pills { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 99px;
  padding: 5px 11px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.pill:hover { border-color: var(--border-strong); color: var(--text); }
.pill.active { background: rgba(99, 102, 241, 0.2); border-color: var(--primary); color: #e0e7ff; }
.pill-count { font-size: 11px; opacity: 0.8; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 500;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-muted);
  white-space: nowrap;
}
.badge-ok { background: rgba(16, 185, 129, 0.14); border-color: rgba(16, 185, 129, 0.35); color: #6ee7b7; }
.badge-warn { background: rgba(245, 158, 11, 0.14); border-color: rgba(245, 158, 11, 0.35); color: #fcd34d; }
.badge-danger { background: rgba(239, 68, 68, 0.14); border-color: rgba(239, 68, 68, 0.35); color: #fca5a5; }
.badge-info { background: rgba(56, 189, 248, 0.14); border-color: rgba(56, 189, 248, 0.35); color: #7dd3fc; }
.badge-primary { background: rgba(99, 102, 241, 0.16); border-color: rgba(99, 102, 241, 0.4); color: #c7d2fe; }

.color-dot { width: 9px; height: 9px; border-radius: 99px; display: inline-block; flex-shrink: 0; }

/* Lists */
.list-stack { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  transition: border-color 0.15s, background 0.15s;
}
.list-item.clickable { cursor: pointer; }
.list-item.clickable:hover,
.list-item.selected { border-color: var(--primary); background: var(--bg-hover); }
.item-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.item-title { font-weight: 600; font-size: 13px; color: var(--text); }
.item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 11px; color: var(--text-muted); }
.item-snippet { font-size: 12px; color: var(--text-muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.toolbar .grow { flex: 1; min-width: 200px; }
.toolbar select { width: auto; min-width: 170px; }

/* Studies Split Layout */
.studies-layout {
  display: grid;
  grid-template-columns: 380px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.studies-list {
  max-height: calc(100vh - 235px);
  overflow-y: auto;
  padding-right: 4px;
}
.study-reader {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px;
  min-height: 460px;
}
.reader-header {
  border-bottom: 1px solid var(--border);
  padding-bottom: 14px;
  margin-bottom: 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.reader-header h3 { margin: 0 0 6px; font-size: 18px; }
.reader-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Markdown Prose */
.md-prose { color: #e2e8f0; line-height: 1.65; font-size: 14px; word-break: break-word; }
.md-prose h1 { font-size: 20px; margin: 18px 0 10px; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.md-prose h2 { font-size: 17px; margin: 18px 0 8px; color: #c7d2fe; }
.md-prose h3 { font-size: 15px; margin: 14px 0 6px; color: #bae6fd; }
.md-prose h4 { font-size: 14px; margin: 12px 0 6px; }
.md-prose p { margin: 0 0 10px; }
.md-prose ul, .md-prose ol { margin: 0 0 12px; padding-left: 22px; }
.md-prose li { margin-bottom: 4px; }
.md-prose code {
  font-family: var(--mono);
  font-size: 12px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
  color: #a5f3fc;
}
.md-prose pre {
  background: #090d16;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0 14px;
}
.md-prose pre code { border: none; padding: 0; background: transparent; color: #e2e8f0; }
.md-prose blockquote {
  margin: 10px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--primary);
  background: rgba(99, 102, 241, 0.08);
  color: #cbd5e1;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.md-prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}
.md-prose th, .md-prose td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: left;
}
.md-prose th { background: var(--bg-elev); color: #cbd5e1; }
.md-prose hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* Projects Strip */
.projects-strip {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}
.project-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 0.15s;
}
.project-card:hover { border-color: var(--border-strong); }
.project-card.active { border-color: var(--primary); background: rgba(99, 102, 241, 0.1); }
.project-card-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.project-card-name { display: flex; align-items: center; gap: 7px; font-weight: 600; font-size: 13px; }

/* Kanban Board */
.kanban-board {
  display: grid;
  grid-template-columns: repeat(5, minmax(220px, 1fr));
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
}
.kanban-col {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 260px;
}
.kanban-col-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px 6px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 12px;
  color: var(--text-muted);
}
.task-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.task-card:hover { border-color: var(--border-strong); }
.task-card-title { font-weight: 600; font-size: 13px; cursor: pointer; }
.task-card-title:hover { color: #c7d2fe; }
.task-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-top: 2px;
}
.status-select {
  width: auto !important;
  padding: 3px 6px !important;
  font-size: 11px !important;
  background: var(--bg-elev) !important;
}

/* Tables */
.table-wrap { overflow-x: auto; }
table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.data-table th, .data-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.data-table th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

/* Bots grid */
.bots-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 12px;
}
.bot-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bot-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.bot-name { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.bot-role { font-size: 12px; color: #cbd5e1; font-weight: 500; }
.bot-desc { font-size: 12px; color: var(--text-muted); }

/* Quick links */
.links-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.link-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 11px;
  font-size: 12px;
  color: var(--text);
}
.link-chip:hover { border-color: var(--primary); text-decoration: none; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 16, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal-dialog {
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 18px; }
.modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 14px;
}
.modal-actions-right { display: flex; gap: 8px; margin-left: auto; }

/* Toast */
.toast-container {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}
.toast {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: var(--shadow);
}
.toast.ok { border-color: var(--ok); }
.toast.error { border-color: var(--danger); }

.empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--text-muted);
  font-size: 13px;
}

/* Responsive */
@media (max-width: 1080px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .studies-layout { grid-template-columns: 1fr; }
  .studies-list { max-height: 360px; }
}
@media (max-width: 780px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed;
    left: -260px;
    top: 0;
    z-index: 60;
    transition: left 0.2s ease;
  }
  .sidebar.open { left: 0; box-shadow: var(--shadow); }
  .form-row { grid-template-columns: 1fr; }
  .view-root { padding: 14px; }
}
