:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #fafaf9;
  --border: #e7e5e4;
  --border-strong: #d6d3d1;
  --text: #1c1917;
  --text-2: #57534e;
  --text-3: #8a8580;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --ok: #15803d;
  --ok-soft: #f0fdf4;
  --warn: #b45309;
  --warn-soft: #fffbeb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(28, 25, 23, .05), 0 1px 3px rgba(28, 25, 23, .04);
  --shadow-lg: 0 10px 30px rgba(28, 25, 23, .12);
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.5 Inter, system-ui, -apple-system, Segoe UI, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1 { font-size: 22px; font-weight: 650; letter-spacing: -.01em; margin: 0; }
h2 { font-size: 16px; font-weight: 600; margin: 0; }
h3 { font-size: 14px; font-weight: 600; margin: 0; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.small { font-size: 12.5px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
[hidden] { display: none !important; }

/* ── Layout ── */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; padding: 2px 8px 18px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
}
.brand-lg { font-size: 18px; padding: 0 0 6px; }
.brand-lg .brand-mark { width: 34px; height: 34px; font-size: 18px; }
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500; text-decoration: none;
}
#nav a:hover { background: var(--surface-2); color: var(--text); }
#nav a.active { background: var(--accent-soft); color: var(--accent); }
.logout { margin-top: auto; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 5; background: rgba(247, 247, 245, .9); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border); padding: 12px 28px;
}
.search { position: relative; max-width: 560px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-3); }
.search input[type=search] { padding-left: 36px; }
.view { padding: 24px 28px 64px; max-width: 1180px; width: 100%; outline: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }

/* ── Controls ── */
input[type=text], input[type=search], input[type=password], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 8px 11px; width: 100%; outline: none; transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79, 70, 229, .15); }
select { width: auto; max-width: 100%; min-width: 0; padding-right: 28px; text-overflow: ellipsis; }
.toolbar select { max-width: 260px; }
textarea { font-family: var(--mono); font-size: 13px; line-height: 1.55; resize: vertical; }
label { display: block; font-weight: 500; margin-bottom: 6px; }
.btn {
  font: inherit; font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 7px 13px; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text); cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); border-color: var(--text-3); }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger-solid:hover { background: #b91c1c; border-color: #b91c1c; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-2); justify-content: flex-start; }
.btn-ghost:hover { background: var(--surface-2); border-color: transparent; color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: transparent; padding: 6px; border-radius: 8px; color: var(--text-2); cursor: pointer; display: grid; place-items: center; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 12.5px; font-weight: 500; padding: 5px 11px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text-2);
}
.chip:hover { border-color: var(--text-3); color: var(--text); }
.chip.on { background: var(--text); border-color: var(--text); color: #fff; }
.chip .count { opacity: .65; margin-left: 4px; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar .search-inline { flex: 1; min-width: 200px; max-width: 340px; }

/* ── Cards & stats ── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 16px 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { padding: 14px 16px; }
.stat-label { color: var(--text-2); font-size: 12.5px; font-weight: 500; }
.stat-value { font-size: 26px; font-weight: 650; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--text-3); font-size: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 12px 18px; border-bottom: 1px solid var(--border); }
.list > li:last-child { border-bottom: 0; }
.list > li.clickable { cursor: pointer; }
.list > li.clickable:hover { background: var(--surface-2); }

/* ── Badges ── */
.badge {
  display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 550; line-height: 1;
  padding: 4px 8px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border);
  white-space: nowrap;
}
.badge.k-preference { background: #f5f3ff; color: #6d28d9; border-color: #ede9fe; }
.badge.k-event { background: #eff6ff; color: #1d4ed8; border-color: #dbeafe; }
.badge.k-fact { background: #f0fdfa; color: #0f766e; border-color: #ccfbf1; }
.badge.k-commitment { background: #fff7ed; color: #c2410c; border-color: #ffedd5; }
.badge.k-belief { background: #fdf2f8; color: #be185d; border-color: #fce7f3; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: #dcfce7; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: #fef3c7; }
.badge.danger { background: var(--danger-soft); color: var(--danger); border-color: #fee2e2; }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; background: var(--text-3); }
.dot.ok { background: #22c55e; } .dot.warn { background: #f59e0b; } .dot.danger { background: #ef4444; }

/* ── Facts ── */
.facts { display: flex; flex-direction: column; gap: 10px; }
.fact { padding: 14px 18px; }
.fact.expired { background: var(--surface-2); }
.fact.expired .fact-text { color: var(--text-3); text-decoration: line-through; text-decoration-color: var(--border-strong); }
.fact-top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.fact-text { font-size: 14.5px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.fact-meta { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; color: var(--text-3); font-size: 12.5px; align-items: center; }
.fact-meta .spacer { flex: 1; }
details.prov { margin-top: 8px; }
details.prov summary { cursor: pointer; color: var(--text-3); font-size: 12.5px; list-style: none; }
details.prov summary::before { content: '▸ '; }
details.prov[open] summary::before { content: '▾ '; }
details.prov div { margin-top: 6px; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; border: 1px solid var(--border); font-size: 12.5px; color: var(--text-2); white-space: pre-wrap; word-break: break-word; }
.entity-link { cursor: pointer; font-family: var(--mono); font-size: 12px; }

/* ── Tables ── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-2); padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); white-space: nowrap; }
td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); }

/* ── Entities ── */
.entity-groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.entity-card { padding: 14px 16px; cursor: pointer; transition: border-color .12s, box-shadow .12s; }
.entity-card:hover { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79, 70, 229, .08); }
.entity-card .slug { font-family: var(--mono); font-size: 12px; color: var(--text-3); }
.entity-card .name { font-weight: 600; font-size: 15px; margin: 2px 0 8px; }
.section-label { font-size: 12px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .06em; margin: 22px 0 10px; }

/* ── Drawer ── */
.drawer { position: fixed; inset: 0; z-index: 30; }
.drawer-backdrop, .modal-backdrop { position: absolute; inset: 0; background: rgba(28, 25, 23, .28); animation: fade .15s; }
.drawer-panel {
  position: absolute; right: 0; top: 0; bottom: 0; width: min(860px, 100%); background: var(--surface);
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; animation: slide .18s ease-out;
}
.drawer-head { display: flex; align-items: flex-start; gap: 12px; padding: 18px 22px; border-bottom: 1px solid var(--border); }
.drawer-title-wrap { flex: 1; min-width: 0; }
.drawer-title-wrap h2 { font-size: 18px; word-break: break-word; }
.drawer-body { overflow: auto; padding: 20px 22px 40px; flex: 1; }
.meta-grid { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 13px; margin-bottom: 18px; padding: 14px 16px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); }
.meta-grid dt { color: var(--text-3); }
.meta-grid dd { margin: 0; word-break: break-word; }
.editor textarea { min-height: 60vh; }
.editor .row { margin-top: 12px; }
@keyframes slide { from { transform: translateX(30px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }

/* ── Markdown ── */
.md { font-size: 14.5px; line-height: 1.7; word-break: break-word; }
.md h1, .md h2, .md h3 { margin: 1.4em 0 .5em; line-height: 1.3; }
.md h1 { font-size: 20px; } .md h2 { font-size: 17px; } .md h3 { font-size: 15px; }
.md p { margin: .6em 0; }
.md pre { background: var(--surface-2); border: 1px solid var(--border); padding: 12px 14px; border-radius: 8px; overflow-x: auto; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: .8em 0; padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.md table { margin: 1em 0; border: 1px solid var(--border); }
.md td, .md th { border: 1px solid var(--border); }
.md img { max-width: 100%; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.5em 0; }

/* ── Modal & toasts ── */
.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 16px; }
.modal-card { position: relative; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); width: min(480px, 100%); padding: 22px; animation: fade .15s; }
.modal-card h3 { font-size: 16px; margin-bottom: 10px; }
.modal-card p { margin: 0 0 12px; color: var(--text-2); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.quote { padding: 10px 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; font-size: 13px; max-height: 160px; overflow: auto; margin-bottom: 14px; white-space: pre-wrap; }
.toasts { position: fixed; bottom: 20px; right: 20px; z-index: 50; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: #fff; padding: 10px 14px; border-radius: 9px; box-shadow: var(--shadow-lg); font-weight: 500; animation: fade .15s; max-width: 380px; }
.toast.error { background: var(--danger); }

/* ── States ── */
.empty { text-align: center; padding: 48px 20px; color: var(--text-2); }
.empty svg { width: 34px; height: 34px; color: var(--text-3); margin-bottom: 8px; }
.loading { padding: 40px; text-align: center; color: var(--text-3); }
.spinner { width: 20px; height: 20px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.alert { padding: 10px 14px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); border: 1px solid #fde68a; margin-bottom: 16px; font-size: 13px; }
.alert.error { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }
.hl { background: #fef08a; color: inherit; border-radius: 2px; padding: 0 1px; }

/* ── Sign in ── */
body.signin { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.signin-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 28px; }
.signin-card p.muted { margin: 4px 0 20px; }
.signin-card input { margin-bottom: 14px; }
.form-error { color: var(--danger); font-size: 13px; margin: -6px 0 12px; }

/* ── Responsive ── */
@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; height: auto; position: static; flex-direction: row; align-items: center; padding: 10px 16px; border-right: 0; border-bottom: 1px solid var(--border); gap: 8px; overflow-x: auto; }
  .brand { padding: 0 8px 0 0; } .brand span:last-child { display: none; }
  #nav { flex-direction: row; gap: 2px; }
  #nav a { padding: 7px 9px; white-space: nowrap; font-size: 13px; }
  #nav a svg { display: none; }
  .logout { margin: 0 0 0 auto; } .logout .btn { padding: 6px 8px; font-size: 0; gap: 0; } .logout .btn svg { font-size: 14px; }
  .topbar { padding: 10px 16px; }
  .view { padding: 18px 16px 48px; }
  .grid-2 { grid-template-columns: 1fr; }
  .drawer-head, .drawer-body { padding-left: 16px; padding-right: 16px; }
  .hide-sm { display: none; }
  .toolbar > * { max-width: 100% !important; }
  .toolbar .search-inline, .toolbar select { flex: 1 1 100%; }
  .layout, .main { max-width: 100vw; overflow-x: clip; }
}
