:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1a2336;
  --surface-3: #222e46;
  --border: #28354d;
  --border-subtle: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  --accent: #38bdf8;
  --accent-strong: #0ea5e9;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --green: #34d399;
  --green-soft: rgba(52, 211, 153, 0.14);
  --amber: #fbbf24;
  --amber-soft: rgba(251, 191, 36, 0.14);
  --red: #f87171;
  --red-soft: rgba(248, 113, 113, 0.14);
  --purple: #a78bfa;
  --purple-soft: rgba(167, 139, 250, 0.14);
  --radius: 10px;
  --radius-sm: 6px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, .mono { font-family: var(--mono); }

/* Layout Shell */
.app-shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 14px; display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px; font-weight: 700; font-size: 16px; color: #f8fafc; letter-spacing: -0.02em; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: linear-gradient(135deg, #38bdf8, #818cf8); box-shadow: 0 0 10px rgba(56, 189, 248, 0.7); }
.nav-section { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-dim); padding: 8px 10px 4px; font-weight: 600; }
.nav-list { display: flex; flex-direction: column; gap: 3px; }
.nav-link { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; border-radius: var(--radius-sm); color: var(--text-muted); font-weight: 500; transition: background 0.12s, color 0.12s; }
.nav-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-link.active { background: var(--accent-soft); color: var(--accent); }
.sidebar-footer { margin-top: auto; padding-top: 12px; border-top: 1px solid var(--border-subtle); display: flex; flex-direction: column; gap: 8px; font-size: 12px; color: var(--text-dim); }

.main-wrap { display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 52px; border-bottom: 1px solid var(--border); background: rgba(17, 24, 39, 0.85); backdrop-filter: blur(8px); padding: 0 24px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 20; }
.topbar-right { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--text-muted); }
.content { padding: 22px 26px; max-width: 1440px; width: 100%; }

/* Cards & Grids */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-title { margin: 0; font-size: 21px; font-weight: 700; letter-spacing: -0.02em; color: #f8fafc; }
.page-sub { margin: 3px 0 0; color: var(--text-muted); font-size: 13px; }

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

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.card-title { margin: 0 0 10px; font-size: 14px; font-weight: 600; color: #f1f5f9; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.kpi-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); font-weight: 600; }
.kpi-val { font-size: 24px; font-weight: 700; color: #f8fafc; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 12px; color: var(--text-muted); margin-top: 3px; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 13px; font-weight: 500; cursor: pointer; transition: background 0.12s, border-color 0.12s; white-space: nowrap; }
.btn:hover { background: var(--surface-3); border-color: #3b4f73; text-decoration: none; }
.btn-primary { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.btn-primary:hover { background: #0284c7; border-color: #0284c7; }
.btn-danger { background: var(--red-soft); border-color: rgba(248, 113, 113, 0.35); color: var(--red); }
.btn-danger:hover { background: rgba(248, 113, 113, 0.25); }
.btn-sm { padding: 4px 9px; font-size: 12px; }
.btn-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Badges */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; border: 1px solid transparent; white-space: nowrap; }
.badge-blue { background: var(--accent-soft); color: var(--accent); border-color: rgba(56, 189, 248, 0.25); }
.badge-green { background: var(--green-soft); color: var(--green); border-color: rgba(52, 211, 153, 0.25); }
.badge-amber { background: var(--amber-soft); color: var(--amber); border-color: rgba(251, 191, 36, 0.25); }
.badge-red { background: var(--red-soft); color: var(--red); border-color: rgba(248, 113, 113, 0.25); }
.badge-purple { background: var(--purple-soft); color: var(--purple); border-color: rgba(167, 139, 250, 0.25); }
.badge-slate { background: var(--surface-2); color: var(--text-muted); border-color: var(--border); }

/* Forms */
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-full { grid-column: 1 / -1; }
label { font-size: 12px; font-weight: 500; color: var(--text-muted); }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 10px; font-size: 13px; font-family: inherit;
}
textarea { resize: vertical; font-family: var(--mono); font-size: 12px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.18); }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); padding: 9px 10px; border-bottom: 1px solid var(--border); font-weight: 600; }
td { padding: 10px; border-bottom: 1px solid var(--border-subtle); vertical-align: top; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255, 255, 255, 0.015); }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Code / Pre / Alerts */
.code-block { background: #070b14; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 11px 12px; font-family: var(--mono); font-size: 12px; overflow-x: auto; white-space: pre-wrap; word-break: break-word; color: #cbd5e1; }
.cv-auto { content-visibility: auto; contain-intrinsic-size: auto 180px; }
.alert { padding: 10px 14px; border-radius: var(--radius-sm); border: 1px solid; font-size: 13px; margin-bottom: 14px; }
.alert-green { background: var(--green-soft); border-color: rgba(52, 211, 153, 0.35); color: #a7f3d0; }
.alert-red { background: var(--red-soft); border-color: rgba(248, 113, 113, 0.35); color: #fecaca; }
.alert-blue { background: var(--accent-soft); border-color: rgba(56, 189, 248, 0.35); color: #bae6fd; }

/* Tabs */
.subnav { display: flex; gap: 6px; border-bottom: 1px solid var(--border); margin-bottom: 18px; padding-bottom: 10px; flex-wrap: wrap; }
.subnav a { padding: 6px 12px; border-radius: var(--radius-sm); font-size: 13px; font-weight: 500; color: var(--text-muted); }
.subnav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.subnav a.active { background: var(--accent-soft); color: var(--accent); }

/* Knowledge Graph Explorer */
.kg-layout { display: grid; grid-template-columns: 1fr 370px; gap: 14px; align-items: start; }
.kg-canvas { width: 100%; height: 610px; background: #070b14; border: 1px solid var(--border); border-radius: var(--radius); position: relative; }
.kg-drawer { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; max-height: 610px; overflow-y: auto; }
.legend { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; font-size: 11px; color: var(--text-muted); }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

/* Utility */
.mt-1 { margin-top: 6px; }
.mt-2 { margin-top: 12px; }
.mt-3 { margin-top: 18px; }
.mb-2 { margin-bottom: 12px; }
.muted { color: var(--text-muted); }
.dim { color: var(--text-dim); }
.small { font-size: 12px; }
.empty { text-align: center; padding: 36px 16px; color: var(--text-muted); }
.htmx-indicator { opacity: 0; transition: opacity 0.15s ease; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
.timing-pill { font-family: var(--mono); font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--green); }

@media (max-width: 1024px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; }
  .grid-4, .grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kg-layout { grid-template-columns: 1fr; }
}
@media (max-width: 680px) {
  .grid-2, .grid-3, .grid-4, .grid-5, .form-grid { grid-template-columns: 1fr; }
}
