:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --primary: #111827;
  --primary-hover: #27272a;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --danger: #dc2626;
  --success: #059669;
  --radius: 10px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.full-width { width: 100%; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.topbar-left { display: flex; align-items: center; gap: 24px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); display: inline-block; }
.brand-sub { font-weight: 400; font-size: 12px; color: var(--muted); }

.nav-tabs { display: flex; gap: 6px; }
.nav-tab {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 500;
}
.nav-tab:hover { background: var(--bg); color: var(--text); }
.nav-tab.active { background: var(--primary); color: #fff; }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  background: var(--bg);
  padding: 5px 12px;
  border-radius: 999px;
}
.role-pill {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.container { max-width: 1440px; margin: 0 auto; padding: 20px 24px 48px; }
.view { display: none; }
.view.active { display: block; }

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: #fef2f2; border-color: #fecaca; color: var(--danger); }

.input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
}
.input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.input-sm { width: 130px; }
.textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.gallery-layout { display: grid; grid-template-columns: 250px 1fr; gap: 20px; align-items: start; }
.sidebar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  position: sticky;
  top: 76px;
}
.sidebar-title { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; margin-bottom: 10px; }
.library-list { display: flex; flex-direction: column; gap: 4px; }
.lib-item {
  border: none;
  background: transparent;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--text);
}
.lib-item:hover { background: var(--bg); }
.lib-item.active { background: var(--primary); color: #fff; }
.lib-count { font-size: 11px; opacity: 0.75; }
.lib-section-label { font-size: 11px; color: var(--muted); margin: 12px 0 4px 6px; font-weight: 600; }

.mcp-hint-box {
  margin-top: 16px;
  padding: 12px;
  background: var(--accent-soft);
  border-radius: 8px;
  font-size: 12px;
  color: #1e3a8a;
}
.mcp-hint-title { font-weight: 600; margin-bottom: 4px; }
.mcp-hint-box p { margin: 0; }

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 14px;
}
.toolbar h1 { margin: 0 0 4px; font-size: 20px; }
.toolbar-actions { width: 280px; }

.family-strip { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.ref-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}
.ref-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  display: flex;
  flex-direction: column;
}
.ref-card:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0,0,0,0.06); }
.ref-thumb-wrap { aspect-ratio: 4 / 3; background: #f3f4f6; overflow: hidden; }
.ref-thumb-wrap img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ref-card-body { padding: 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.ref-card-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; font-size: 12px; }
.ref-code { font-weight: 600; font-family: ui-monospace, monospace; }
.badge {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
}
.badge-protected { background: #ecfdf5; color: var(--success); }
.ref-title { font-size: 13px; font-weight: 600; }
.ref-notes { font-size: 12px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.section-grid { display: grid; gap: 20px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.card-header h2, .card h3 { margin: 0; font-size: 16px; }
.inline-form { display: flex; gap: 8px; align-items: center; }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.form-grid label, .form-stack label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; font-weight: 600; color: var(--text); }
.full-span { grid-column: 1 / -1; }
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--danger); font-size: 12px; }

.token-list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.token-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 8px;
  font-size: 13px;
}
.token-val { font-family: ui-monospace, monospace; font-size: 12px; background: #fff; padding: 3px 8px; border-radius: 6px; border: 1px solid var(--border); }

.code-box-wrap { margin-top: 14px; }
.code-box-header { display: flex; justify-content: space-between; align-items: center; font-size: 12px; font-weight: 600; margin-bottom: 6px; }
.code-box {
  margin: 0;
  background: #0f172a;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 8px;
  font-size: 12px;
  overflow-x: auto;
}

.admin-subtabs { display: flex; gap: 8px; margin-bottom: 16px; }
.subtab {
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}
.subtab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.admin-panel { display: none; gap: 20px; flex-direction: column; }
.admin-panel.active { display: flex; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi-value { font-size: 24px; font-weight: 700; margin-top: 4px; }

.admin-two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); }
th { font-size: 12px; color: var(--muted); font-weight: 600; }

.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.modal-card {
  position: relative;
  background: var(--surface);
  border-radius: 12px;
  max-height: 90vh;
  overflow-y: auto;
  width: 100%;
  padding: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}
.modal-sm { max-width: 400px; }
.modal-lg { max-width: 880px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-header h3 { margin: 0; font-size: 17px; }
.modal-close { border: none; background: transparent; font-size: 22px; cursor: pointer; color: var(--muted); }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

.ref-detail-body { display: grid; grid-template-columns: 44% 56%; gap: 20px; }
.ref-detail-img { width: 100%; max-height: 420px; object-fit: contain; background: #f3f4f6; border-radius: 8px; border: 1px solid var(--border); }
.dna-list { display: flex; flex-direction: column; gap: 8px; font-size: 12px; margin-top: 10px; }
.dna-item { background: var(--bg); padding: 8px 10px; border-radius: 6px; }
.prompt-shield {
  margin: 10px 0 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  font-size: 12px;
  color: #1e40af;
}
.workpane-title { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px; }
.req-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.req-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; }
.req-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.dna-details { margin-top: 12px; font-size: 12px; }
.dna-details summary { cursor: pointer; font-weight: 600; color: var(--accent); }

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--primary);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
}

@media (max-width: 900px) {
  .gallery-layout, .admin-two-col, .ref-detail-body, .form-grid { grid-template-columns: 1fr; }
  .sidebar { position: static; }
}
