/* ============ Reset + tokens ============ */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 14px;
  color: #0f172a;
  background: #f7f8fb;
  -webkit-font-smoothing: antialiased;
}
a { color: #4f46e5; text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; }
:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --sidebar-bg: #0b1220;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #1e293b;
  --border: #e5e7eb;
  --muted: #64748b;
  --text: #0f172a;
  --bg: #f7f8fb;
  --card: #ffffff;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #0ea5e9;
}

/* ============ Layout ============ */
.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  min-height: 100vh;
}
.sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 20px;
  color: white;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.01em;
}
.sidebar .brand .dot {
  width: 26px; height: 26px;
  border-radius: 8px;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  display: flex; align-items: center; justify-content: center;
  color: white; font-size: 13px; font-weight: 800;
}
.sidebar .nav-group-label {
  color: #64748b;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  padding: 12px 10px 6px;
}
.sidebar a.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 13.5px;
  transition: background 0.12s;
}
.sidebar a.nav-item:hover { background: rgba(255,255,255,0.05); text-decoration: none; }
.sidebar a.nav-item.active { background: var(--sidebar-active); color: white; }
.sidebar a.nav-item .ico { width: 16px; text-align: center; opacity: 0.9; }
.sidebar .spacer { flex: 1; }
.sidebar .workspace {
  border-top: 1px solid rgba(255,255,255,0.06);
  padding: 12px 10px;
  margin-top: 10px;
  font-size: 12.5px;
}
.sidebar .workspace .name { color: white; font-weight: 600; }
.sidebar .workspace .sub { color: #64748b; margin-top: 2px; }

.topbar {
  grid-area: topbar;
  background: white;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .search {
  flex: 1;
  max-width: 480px;
  position: relative;
}
.topbar .search input {
  width: 100%;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px 0 32px;
  font-size: 13px;
  background: #f7f8fb;
  outline: none;
}
.topbar .search input:focus { border-color: var(--primary); background: white; }
.topbar .search::before {
  content: "\1F50D"; /* magnifier */
  position: absolute; left: 10px; top: 7px;
  font-size: 12px; opacity: 0.5;
}
.topbar .grow { flex: 1; }
.topbar .pill {
  background: #eef2ff; color: #4338ca;
  padding: 4px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 600;
}
.topbar .avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: linear-gradient(135deg, #f97316, #ec4899);
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 12.5px;
  cursor: pointer;
}

.main {
  grid-area: main;
  padding: 28px 32px;
  overflow-x: hidden;
}

/* ============ Typography + primitives ============ */
.page-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0 0 4px;
}
.page-sub {
  color: var(--muted);
  margin: 0 0 24px;
  font-size: 13.5px;
}
.section-title {
  font-size: 15px; font-weight: 600; margin: 0 0 12px;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}
.card h3 { margin: 0 0 4px; font-size: 14px; font-weight: 600; }
.card .muted { color: var(--muted); font-size: 12.5px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--primary); color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s;
  text-decoration: none;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; color: white; }
.btn.secondary { background: white; color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: #f1f5f9; }
.btn.ghost { background: transparent; color: var(--text); }
.btn.small { padding: 5px 10px; font-size: 12px; border-radius: 6px; }

.badge {
  display: inline-block;
  font-size: 11px; font-weight: 600;
  padding: 3px 8px; border-radius: 999px;
  background: #eef2ff; color: #4338ca;
}
.badge.green { background: #dcfce7; color: #166534; }
.badge.amber { background: #fef3c7; color: #92400e; }
.badge.red { background: #fee2e2; color: #991b1b; }
.badge.gray { background: #f1f5f9; color: #475569; }
.badge.blue { background: #dbeafe; color: #1e40af; }

.dot-status {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  margin-right: 6px;
}
.dot-status.ok { background: var(--success); }
.dot-status.warn { background: var(--warning); }
.dot-status.err { background: var(--danger); }
.dot-status.idle { background: #94a3b8; }

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) {
  .grid.cols-3, .grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
}

/* ============ Stats ============ */
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.stat .delta { font-size: 12px; margin-top: 4px; }
.stat .delta.up { color: var(--success); }
.stat .delta.down { color: var(--danger); }

/* ============ Tables ============ */
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th, table.data td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
table.data th {
  background: #f8fafc;
  color: var(--muted);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}
table.data tr:last-child td { border-bottom: none; }
table.data tr:hover td { background: #fafbff; }

/* ============ Chat ============ */
.chat-wrap {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 20px;
  min-height: calc(100vh - 56px - 56px);
}
.conv-list {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  height: fit-content;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
  position: sticky;
  top: 76px;
}
.conv-list .new {
  width: 100%; margin-bottom: 8px;
}
.conv-list .conv {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  font-size: 13px;
}
.conv-list .conv:hover { background: #f1f5f9; }
.conv-list .conv.active { background: #eef2ff; color: #3730a3; font-weight: 500; }
.conv-list .conv .sub { color: var(--muted); font-size: 11.5px; margin-top: 2px; }

.chat-main {
  display: flex; flex-direction: column;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.scope-banner {
  background: #eef2ff;
  color: #3730a3;
  padding: 10px 20px;
  font-size: 12.5px;
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid #e0e7ff;
}
.scope-banner .badge { background: white; color: #4338ca; }
.chat-scroll {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  min-height: 400px;
}
.msg { margin-bottom: 22px; }
.msg .who {
  font-size: 12px; color: var(--muted); font-weight: 600;
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.04em;
}
.msg.user .bubble {
  background: #f1f5f9; padding: 12px 16px; border-radius: 10px;
  max-width: 720px;
}
.msg.assistant .bubble {
  background: transparent; max-width: 820px;
  line-height: 1.65;
  font-size: 14px;
}
.msg .cite {
  display: inline-block;
  background: #eef2ff; color: #4338ca;
  border-radius: 4px;
  font-size: 10.5px;
  font-weight: 700;
  padding: 1px 5px;
  margin: 0 1px;
  vertical-align: super;
  cursor: pointer;
  border: 1px solid #e0e7ff;
}
.msg .cite:hover { background: #4f46e5; color: white; }
.sources {
  margin-top: 16px;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.sources .src-title { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.04em; }
.sources .src {
  display: flex; gap: 10px; padding: 8px 0;
  border-top: 1px solid var(--border);
  align-items: flex-start;
  font-size: 12.5px;
}
.sources .src:first-of-type { border-top: none; }
.sources .src .num {
  background: #eef2ff; color: #4338ca;
  border-radius: 4px; padding: 1px 6px; font-weight: 700;
  font-size: 11px; flex-shrink: 0;
}
.sources .src .body { flex: 1; }
.sources .src .doc { font-weight: 600; color: var(--text); }
.sources .src .snippet { color: var(--muted); margin-top: 2px; }

.verify-note {
  margin-top: 10px; font-size: 12px; color: var(--success);
  display: inline-flex; gap: 6px; align-items: center;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  gap: 10px;
  background: white;
}
.composer input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13.5px;
  outline: none;
}
.composer input:focus { border-color: var(--primary); }
.composer .btn { height: 38px; }

/* ============ Progress bars ============ */
.progress {
  height: 6px; background: #e5e7eb; border-radius: 999px; overflow: hidden;
}
.progress > div {
  height: 100%; background: var(--primary); border-radius: 999px;
}

/* ============ Login ============ */
.login-wrap {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: white;
}
.login-hero {
  background: linear-gradient(135deg, #0b1220 0%, #1e293b 60%, #4338ca 130%);
  color: white;
  padding: 48px;
  display: flex; flex-direction: column; justify-content: space-between;
  position: relative; overflow: hidden;
}
.login-hero .brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 18px;
}
.login-hero .brand .dot {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 800;
}
.login-hero .tagline {
  max-width: 420px;
}
.login-hero .tagline h1 {
  font-size: 32px; font-weight: 700; line-height: 1.2; letter-spacing: -0.02em;
  margin: 0 0 12px;
}
.login-hero .tagline p { color: #cbd5e1; line-height: 1.6; }
.login-hero .quote {
  border-left: 3px solid #6366f1;
  padding-left: 14px;
  color: #cbd5e1; font-size: 13px; line-height: 1.55;
  max-width: 420px;
}
.login-hero .quote .who { color: white; margin-top: 8px; font-weight: 600; }
.login-form {
  padding: 48px;
  display: flex; align-items: center; justify-content: center;
}
.login-form form {
  width: 100%; max-width: 380px;
}
.login-form h2 { margin: 0 0 6px; font-size: 22px; font-weight: 700; }
.login-form p.sub { color: var(--muted); margin: 0 0 24px; font-size: 14px; }
.field { margin-bottom: 16px; }
.field label {
  display: block; font-size: 12.5px; font-weight: 600;
  margin-bottom: 6px; color: var(--text);
}
.field input {
  width: 100%; height: 40px; border: 1px solid var(--border);
  border-radius: 8px; padding: 0 12px; font-size: 14px;
  outline: none; transition: border 0.12s;
}
.field input:focus { border-color: var(--primary); }
.login-form .btn { width: 100%; justify-content: center; height: 40px; }
.login-form .divider {
  text-align: center; color: var(--muted); font-size: 12px; margin: 18px 0;
  position: relative;
}
.login-form .divider::before, .login-form .divider::after {
  content: ""; position: absolute; top: 50%;
  width: 40%; height: 1px; background: var(--border);
}
.login-form .divider::before { left: 0; }
.login-form .divider::after { right: 0; }
.login-form .btn.secondary { width: 100%; justify-content: center; height: 40px; }
.login-form .footer-note {
  text-align: center; margin-top: 24px; color: var(--muted); font-size: 12.5px;
}
@media (max-width: 900px) {
  .login-wrap { grid-template-columns: 1fr; }
  .login-hero { padding: 32px; min-height: 240px; }
}

/* ============ Misc ============ */
.tag {
  display: inline-block;
  background: #f1f5f9; color: #475569;
  padding: 2px 8px; border-radius: 4px;
  font-size: 11.5px; font-weight: 500;
  margin-right: 4px;
}
.divider-h { height: 1px; background: var(--border); margin: 20px 0; }

.donut {
  width: 120px; height: 120px;
  border-radius: 50%;
  background: conic-gradient(var(--primary) 0 92%, #e5e7eb 92% 100%);
  display: flex; align-items: center; justify-content: center;
  position: relative;
}
.donut::after {
  content: ""; position: absolute;
  width: 84px; height: 84px; border-radius: 50%; background: white;
}
.donut .num {
  position: relative; z-index: 1;
  font-size: 22px; font-weight: 700; letter-spacing: -0.02em;
}

.spark {
  display: flex; gap: 3px; align-items: flex-end;
  height: 40px; margin-top: 8px;
}
.spark span {
  flex: 1;
  background: linear-gradient(180deg, #6366f1, #a5b4fc);
  border-radius: 2px;
  min-height: 3px;
}

/* ============ Modal (light demo) ============ */
.modal-mask {
  display: none; position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 100; align-items: center; justify-content: center;
}
.modal-mask.open { display: flex; }
.modal {
  background: white; border-radius: 12px;
  width: 460px; max-width: 90vw;
  padding: 24px;
  box-shadow: 0 20px 40px rgba(0,0,0,0.15);
}
.modal h3 { margin: 0 0 6px; font-size: 16px; font-weight: 600; }
.modal p { color: var(--muted); margin: 0 0 16px; font-size: 13px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ============ Mobile drawer + hamburger ============ */
.menu-toggle {
  display: none;
  background: transparent;
  border: none;
  padding: 6px;
  cursor: pointer;
  width: 32px; height: 32px;
  border-radius: 6px;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.menu-toggle:hover { background: #f1f5f9; }
.menu-toggle span {
  display: block;
  width: 18px; height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: transform 0.15s;
}
.sidebar-mask {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 20;
}
.hide-sm { }

/* ============ Responsive: tablet (≤ 900px) ============ */
@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 260px;
    height: 100vh;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.2s ease-out;
    box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open .sidebar-mask { display: block; }
  .menu-toggle { display: inline-flex; }
  .topbar { padding: 0 16px; gap: 10px; }
  .topbar .search { max-width: none; }
  .main { padding: 20px 16px; }

  /* Chat: conv-list moves below the chat and unpins */
  .chat-wrap {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  .chat-main { order: 1; }
  .conv-list {
    order: 2;
    position: static;
    max-height: none;
  }

  /* Cards: allow wide tables to scroll horizontally */
  .card { overflow-x: auto; }
  table.data { min-width: 520px; }
}

/* ============ Responsive: small (≤ 640px) ============ */
@media (max-width: 640px) {
  .hide-sm { display: none; }
  .main { padding: 16px 12px; }
  .page-title { font-size: 19px; }
  .page-sub { font-size: 13px; margin-bottom: 18px; }

  /* Every grid collapses to a single column */
  .grid.cols-2,
  .grid.cols-3,
  .grid.cols-4 { grid-template-columns: 1fr; }

  /* Card interior tightens */
  .card { padding: 16px; border-radius: 10px; }
  .stat .value { font-size: 22px; }

  /* Chat composer + scroll a bit tighter */
  .chat-scroll { padding: 16px; min-height: 320px; }
  .composer { padding: 10px 12px; }
  .composer input { padding: 8px 12px; }
  .scope-banner { padding: 8px 14px; font-size: 12px; flex-wrap: wrap; }

  /* Sources: donut + list stack */
  .donut { width: 96px; height: 96px; }
  .donut::after { width: 68px; height: 68px; }

  /* Header rows in pages that use inline flex should wrap so their button drops below the title */
  .main > div[style*="justify-content:space-between"] { flex-wrap: wrap; }

  .topbar { height: 56px; }
  .topbar .avatar { width: 30px; height: 30px; font-size: 12px; }
  .sidebar { width: 240px; }
}
