:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --text-subtle: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #eff6ff;
  --pro-bg: linear-gradient(135deg, #059669, #0d9488);
  --pro-soft: #ecfdf5;
  --pro-border: #a7f3d0;
  --std-bg: #334155;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --warn-border: #fde68a;
  --tip-soft: #f0fdf4;
  --tip-border: #bbf7d0;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 8px 28px rgba(15, 23, 42, 0.08);
  --radius: 12px;
  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #273449;
  --text: #f1f5f9;
  --text-muted: #cbd5e1;
  --text-subtle: #94a3b8;
  --accent: #60a5fa;
  --accent-hover: #3b82f6;
  --accent-soft: #172554;
  --pro-soft: #064e3b;
  --pro-border: #059669;
  --danger-soft: #450a0a;
  --danger-border: #991b1b;
  --warn-soft: #451a03;
  --warn-border: #b45309;
  --tip-soft: #052e16;
  --tip-border: #15803d;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------------- Login Page ---------------- */
.login-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at top right, rgba(37, 99, 235, 0.1), transparent 45%),
              radial-gradient(circle at bottom left, rgba(5, 150, 105, 0.08), transparent 45%),
              var(--bg);
}
.login-card {
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 36px 32px;
  box-shadow: var(--shadow-md);
}
.brand-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 14px;
}
.login-title {
  margin: 0 0 8px;
  font-size: 24px;
  line-height: 1.3;
  letter-spacing: -0.02em;
}
.login-sub {
  margin: 0 0 24px;
  color: var(--text-subtle);
  font-size: 14px;
}
.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-muted);
}
.input-lg {
  width: 100%;
  padding: 13px 15px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 15px;
  letter-spacing: 0.06em;
  outline: none;
  transition: border-color 0.18s, box-shadow 0.18s;
}
.input-lg:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.16);
}
.btn-primary {
  width: 100%;
  margin-top: 16px;
  padding: 13px 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.18s, transform 0.1s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }
.alert-error {
  margin-top: 14px;
  padding: 11px 14px;
  border-radius: 10px;
  background: var(--danger-soft);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  font-size: 13px;
  display: none;
}
.alert-error.show { display: block; }
.login-rules {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}
.rule-pill {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 6px;
  font-size: 12px;
  color: var(--text-muted);
}
.rule-pill strong { display: block; color: var(--text); font-size: 13px; margin-bottom: 2px; }
.login-footer {
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-subtle);
  display: flex;
  gap: 14px;
  align-items: center;
}

/* ---------------- Docs Header & Layout ---------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: 60px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  gap: 12px;
}
[data-theme="dark"] .topbar { background: rgba(17, 24, 39, 0.9); }
.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.mobile-menu-btn {
  display: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 15px;
}
.site-brand {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tier-pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}
.tier-pro { background: var(--pro-bg); }
.tier-standard { background: var(--std-bg); }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}
.status-chip {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 999px;
  white-space: nowrap;
}
.btn-ghost {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }

.docs-shell {
  max-width: 1280px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr);
  gap: 28px;
  padding: 24px 24px 72px;
}
.sidebar {
  position: sticky;
  top: 84px;
  height: calc(100vh - 104px);
  overflow-y: auto;
  padding-right: 12px;
  border-right: 1px solid var(--border);
}
.sidebar-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  margin: 0 0 10px 8px;
}
.toc-link {
  display: block;
  padding: 5px 10px;
  border-radius: 7px;
  color: var(--text-muted);
  font-size: 13.5px;
  line-height: 1.45;
  margin-bottom: 2px;
  transition: background 0.14s, color 0.14s;
}
.toc-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.toc-l2 { font-weight: 600; color: var(--text); margin-top: 8px; }
.toc-l3 { padding-left: 22px; font-size: 13px; }
.toc-link.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.doc-main {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 42px 54px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.doc-hero {
  padding-bottom: 22px;
  margin-bottom: 26px;
  border-bottom: 1px solid var(--border);
}
.doc-hero h1 {
  margin: 0 0 10px;
  font-size: 28px;
  line-height: 1.3;
  letter-spacing: -0.02em;
}
.doc-hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 13px;
  color: var(--text-subtle);
}

/* ---------------- Prose Typography ---------------- */
.prose h2 {
  margin: 36px 0 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  font-size: 21px;
  scroll-margin-top: 78px;
}
.prose h3 {
  margin: 26px 0 12px;
  font-size: 17px;
  color: var(--text);
  scroll-margin-top: 78px;
}
.prose p { margin: 10px 0; color: var(--text-muted); }
.prose strong { color: var(--text); }
.prose ul, .prose ol { padding-left: 22px; margin: 10px 0; color: var(--text-muted); }
.prose li { margin: 6px 0; }
.prose hr { border: none; border-top: 1px solid var(--border); margin: 28px 0; }
.prose code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 5px;
  color: #be123c;
}
[data-theme="dark"] .prose code { color: #fda4af; }

/* Code block with copy button */
.code-block-wrap {
  position: relative;
  margin: 14px 0;
}
.prose pre {
  margin: 0;
  padding: 16px 18px;
  border-radius: 10px;
  background: #0f172a;
  color: #e2e8f0;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.65;
}
.prose pre code {
  background: transparent;
  border: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}
.copy-code-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(30, 41, 59, 0.9);
  color: #e2e8f0;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.copy-code-btn:hover { background: var(--accent); border-color: var(--accent); }

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  margin: 16px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.prose table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.prose th, .prose td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.prose th {
  background: var(--surface-2);
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.prose tr:last-child td { border-bottom: none; }

/* Callout cards */
.callout {
  margin: 16px 0;
  padding: 14px 18px;
  border-radius: 10px;
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  font-size: 14px;
}
.callout p { margin: 4px 0; color: var(--text); }
.callout ul { margin: 6px 0 2px; color: var(--text); }
.callout-warn { border-left-color: var(--warn); background: var(--warn-soft); }
.callout-danger { border-left-color: var(--danger); background: var(--danger-soft); }
.callout-tip { border-left-color: #16a34a; background: var(--tip-soft); }
.callout-note { border-left-color: #64748b; background: var(--surface-2); }

/* Flow Board (pure CSS replacement for Mermaid, 0ms load) */
.flow-board {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin: 18px 0;
}
.flow-col {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.flow-head {
  font-weight: 700;
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 8px;
  color: #fff;
  text-align: center;
}
.flow-head-ind { background: #2563eb; }
.flow-head-fam { background: #0d9488; }
.flow-node {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13.5px;
  font-weight: 600;
  text-align: center;
}
.flow-arrow { text-align: center; color: var(--text-subtle); font-weight: 700; line-height: 1; }
.flow-branches { display: grid; gap: 8px; }
.flow-leaf {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
}
.flow-leaf strong { display: block; margin-bottom: 2px; }
.flow-leaf span { color: var(--text-subtle); }
.leaf-ok { border-left-color: #16a34a; }
.leaf-warn { border-left-color: #d97706; }
.leaf-err { border-left-color: #dc2626; }

/* Figures & Lightbox */
.doc-figure {
  margin: 18px 0 6px;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  text-align: center;
}
.zoomable-img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  cursor: zoom-in;
  display: block;
  margin: 0 auto;
}
.img-caption {
  text-align: center;
  font-size: 13px;
  color: var(--text-subtle) !important;
  margin: 0 0 18px !important;
}
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(15, 23, 42, 0.86);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  cursor: zoom-out;
}
.lightbox.open { display: flex; }
.lightbox img {
  max-width: 95vw;
  max-height: 92vh;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  background: #fff;
}

/* Task checklist */
.task-item { list-style: none; margin-left: -18px; }
.task-label { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.task-checkbox { margin-top: 5px; width: 16px; height: 16px; accent-color: var(--accent); flex-shrink: 0; }
.task-checkbox:checked + span { text-decoration: line-through; opacity: 0.65; }

/* Tier Cards (Locked Standard vs Pro Download) */
.locked-card {
  margin: 20px 0;
  padding: 20px 22px;
  border-radius: 12px;
  border: 1.5px dashed var(--warn);
  background: var(--warn-soft);
}
.locked-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  color: #92400e;
  background: rgba(217, 119, 6, 0.16);
  padding: 3px 10px;
  border-radius: 999px;
  margin-bottom: 8px;
}
[data-theme="dark"] .locked-badge { color: #fcd34d; }
.locked-card h4 { margin: 4px 0 8px; font-size: 16px; color: var(--text); }
.locked-card p { margin: 6px 0; font-size: 14px; color: var(--text-muted); }
.locked-hint { font-size: 13px !important; color: var(--text) !important; }

.download-card {
  margin: 18px 0;
  padding: 18px 20px;
  border-radius: 12px;
  border: 1.5px solid var(--pro-border);
  background: var(--pro-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.download-badge {
  display: inline-block;
  font-size: 11.5px;
  font-weight: 700;
  color: #fff;
  background: #059669;
  padding: 2px 9px;
  border-radius: 999px;
  margin-bottom: 6px;
}
.download-title { font-weight: 700; font-size: 15px; color: var(--text); }
.download-meta { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 9px;
  background: #059669;
  color: #fff !important;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none !important;
  white-space: nowrap;
}
.btn-download:hover { background: #047857; }

/* ---------------- Admin Dashboard ---------------- */
.admin-wrap {
  max-width: 1220px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}
.kpi-label { font-size: 12.5px; color: var(--text-subtle); }
.kpi-val { font-size: 26px; font-weight: 700; margin-top: 4px; color: var(--text); }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 22px;
  margin-bottom: 20px;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.panel-head h2 { margin: 0; font-size: 17px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
  align-items: end;
}
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--text-subtle); margin-bottom: 5px; }
.field input, .field select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13.5px;
}
.btn-sm {
  padding: 7px 12px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }
.btn-sm-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-sm-primary:hover { background: var(--accent-hover); color: #fff; }
.btn-sm-danger { color: var(--danger); border-color: var(--danger-border); }
.btn-sm-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.gen-output {
  margin-top: 14px;
  padding: 14px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  display: none;
}
.gen-output.show { display: block; }
.gen-output textarea {
  width: 100%;
  min-height: 110px;
  font-family: var(--font-mono);
  font-size: 13px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
.toolbar-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}
.toolbar-row input, .toolbar-row select {
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
}
.b-pro { background: #d1fae5; color: #065f46; }
.b-std { background: #e2e8f0; color: #334155; }
.b-active { background: #dbeafe; color: #1e40af; }
.b-unused { background: #f1f5f9; color: #475569; }
.b-expired { background: #fef3c7; color: #92400e; }
.b-revoked { background: #fee2e2; color: #991b1b; }
.mono-code { font-family: var(--font-mono); font-weight: 600; font-size: 13px; cursor: pointer; }
.actions-cell { display: flex; flex-wrap: wrap; gap: 6px; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal-card {
  width: 100%;
  max-width: 680px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px 24px;
  box-shadow: var(--shadow-md);
}
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* ---------------- Responsive ---------------- */
@media (max-width: 960px) {
  .docs-shell { grid-template-columns: 1fr; padding: 14px 12px 56px; }
  .mobile-menu-btn { display: inline-flex; }
  .status-chip { display: none; }
  .sidebar {
    position: fixed;
    top: 60px;
    left: 0;
    bottom: 0;
    width: 280px;
    height: auto;
    z-index: 50;
    background: var(--surface);
    padding: 16px;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .doc-main { padding: 22px 18px 36px; }
  .flow-board { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid { grid-template-columns: repeat(2, 1fr); }
}
