:root {
  --rail: #122135;
  --rail-active: #6644ff;
  --surface: #f7f9fd;
  --panel: #ffffff;
  --panel-low: #eef3fb;
  --panel-high: #dfe7f1;
  --line: #d6deeb;
  --line-soft: #e6edf6;
  --text: #18243a;
  --muted: #6f7f99;
  --muted-light: #9cafcc;
  --primary: #5f3df6;
  --primary-strong: #6644ff;
  --primary-soft: #eeeaff;
  --danger: #d43c3c;
  --danger-soft: #ffe7e7;
  --warn: #a55a00;
  --warn-soft: #fff0d8;
  --ok: #217a45;
  --ok-soft: #e3f7ea;
  --radius: 6px;
  --rail-width: 56px;
  --sidebar: 308px;
  --sidebar-collapsed: 0px;
  --topbar: 88px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  display: flex;
  overflow: hidden;
  background: #fff;
  color: var(--text);
  font: 14px/1.45 "Inter", "Arial", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.auth-page {
  display: block;
  overflow: hidden;
  background: #0d1219;
  color: #f4f7fb;
}

.auth-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 496px minmax(0, 1fr);
  background: #101723;
}

.auth-panel {
  position: relative;
  min-height: 100vh;
  padding: 44px 75px;
  background: #0d1219;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.auth-logo {
  width: 56px;
  height: 56px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  background: #6946ff;
  color: #fff;
  font-weight: 900;
}

.auth-title {
  color: #f4f7fb;
  font-size: 18px;
  font-weight: 800;
}

.auth-subtitle {
  color: #6e7481;
  font-size: 15px;
}

.auth-form-wrap {
  margin-top: 345px;
}

.auth-form-wrap h1 {
  margin: 0 0 22px;
  color: #f4f7fb;
  font-size: 42px;
  line-height: 1.1;
}

.auth-hint {
  margin: -10px 0 18px;
  color: #7f8694;
}

.auth-error {
  width: 340px;
  margin-bottom: 14px;
  padding: 10px 12px;
  border: 1px solid #5f2730;
  border-radius: 5px;
  color: #ffb4bc;
  background: #1f1117;
}

.auth-form {
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.auth-form input {
  width: 100%;
  height: 58px;
  border: 1px solid #2a303a;
  border-radius: 5px;
  background: transparent;
  color: #f4f7fb;
  padding: 0 16px;
  font-size: 15px;
  font-weight: 700;
}

.auth-form input::placeholder {
  color: #6e7481;
}

.password-row {
  position: relative;
}

.password-row button {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: #7f8694;
  cursor: pointer;
  font-size: 16px;
}

.auth-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.auth-actions span {
  color: #5e6471;
  font-weight: 700;
}

.auth-submit {
  width: 154px;
  height: 52px;
  border: 0;
  border-radius: 5px;
  background: #6946ff;
  color: #101723;
  font: inherit;
  font-weight: 900;
  cursor: pointer;
}

.auth-status {
  position: absolute;
  left: 82px;
  bottom: 42px;
  color: #686f7b;
  font-weight: 800;
}

.auth-art {
  min-height: 100vh;
  background:
    radial-gradient(circle at 24% 102%, rgba(105, 70, 255, .78), transparent 23%),
    radial-gradient(circle at 44% 100%, rgba(188, 93, 183, .58), transparent 28%),
    radial-gradient(circle at 70% 103%, rgba(134, 85, 183, .45), transparent 24%),
    #102033;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code, textarea.code-editor { font-family: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace; }

.rail {
  width: var(--rail-width);
  flex: 0 0 var(--rail-width);
  height: 100vh;
  background: var(--rail);
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #b8c8df;
  z-index: 2;
}

.rail-logo {
  width: 100%;
  height: 56px;
  display: grid;
  place-items: center;
  background: var(--rail-active);
  color: #fff;
  font-size: 12px;
  font-weight: 900;
}

.rail-icon {
  width: 100%;
  height: 52px;
  display: grid;
  place-items: center;
  color: #b8c8df;
  font-size: 22px;
  text-decoration: none;
  border-left: 3px solid transparent;
  background: transparent;
  border-top: 0;
  border-right: 0;
  border-bottom: 0;
  cursor: pointer;
}
.rail-button { font-size: 18px; font-family: inherit; }
.rail-icon:hover { background: rgba(255,255,255,.06); text-decoration: none; }
.rail-icon.active { color: #fff; border-left-color: #7b61ff; }
.rail-bottom { margin-top: auto; }

.sidebar {
  width: var(--sidebar);
  flex: 0 0 var(--sidebar);
  height: 100vh;
  border-right: 1px solid #d8e0ec;
  background: #eef3fa;
  display: flex;
  flex-direction: column;
  transition: width .16s ease, flex-basis .16s ease, transform .16s ease;
  overflow: hidden;
}

body.sidebar-collapsed .sidebar {
  width: 0;
  flex-basis: 0;
  transform: translateX(-8px);
}

.brand {
  height: 60px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 10px;
  border-bottom: 1px solid #dfe7f1;
}

.sidebar-toggle {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 22px;
}
.sidebar-toggle:hover { background: #dfe7f1; color: var(--text); }

.brand-copy { min-width: 0; }
.brand-title { color: #0a1d36; font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-subtitle { color: var(--muted-light); font-size: 13px; margin-top: 1px; }

.nav {
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  height: 100%;
}

.nav-section {
  padding: 12px 12px 6px;
  color: var(--muted-light);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.muted-section { margin-top: 10px; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 7px 10px;
  border-radius: var(--radius);
  color: #0a1d36;
  font-weight: 600;
  text-decoration: none;
}
.nav-link:hover { background: #e2e9f3; text-decoration: none; }
.nav-link.active { background: #dfe7f1; color: #0a1d36; }
.nav-link.disabled { opacity: .55; pointer-events: none; }
.nav-ico { width: 22px; color: var(--primary); font-size: 18px; text-align: center; }
.nav-bottom { margin-top: auto; }

.app { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100vh; }

.topbar {
  height: var(--topbar);
  flex: 0 0 var(--topbar);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 32px 12px;
  background: var(--panel);
  border-bottom: 0;
  align-items: flex-start;
}

.content-title {
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.content-title strong {
  color: #0a1d36;
  font-size: 24px;
  line-height: 1.15;
}
.title-kicker {
  color: var(--muted-light);
  font-size: 12px;
  font-weight: 700;
}

.global-search input, .filterbar input, select, input, textarea {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

.global-search input { height: 36px; width: 240px; padding: 6px 12px; border-radius: 18px; background: #fff; }

.topbar-meta { display: flex; align-items: center; gap: 10px; }
.db-label { color: var(--muted); font-size: 12px; }
.main { flex: 1; overflow: auto; padding: 0 32px 24px; background: #fff; }
.safety-banner { background: var(--danger-soft); color: var(--danger); padding: 8px 16px; border-bottom: 1px solid var(--danger); font-weight: 700; }

.page-head, .editor-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}
.page-head h1, .editor-head h1 { margin: 0; font-size: 22px; line-height: 1.2; color: #0a1d36; }
.page-head p, .editor-head p { margin: 4px 0 0; color: var(--muted); }
.breadcrumbs { color: var(--muted); font-size: 13px; margin-bottom: 4px; }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.metric-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}
.metric-card.danger { border-color: var(--danger); background: #fff7f6; }
.metric-label { display: block; color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; }
.metric-card strong { display: block; margin-top: 8px; font-size: 26px; }

.split, .detail-grid {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) minmax(420px, 2fr);
  gap: 16px;
  margin-bottom: 16px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  overflow: hidden;
}
.panel-head, .pane-head {
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  background: #f8fbff;
  border-bottom: 1px solid var(--line-soft);
}
.panel-head h2, .pane-head h2 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }

.filterbar {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  margin-bottom: 12px;
}
.filterbar input { width: 320px; padding: 5px 8px; }
.filterbar select, select { padding: 5px 28px 5px 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  border: 1px solid transparent;
  padding: 6px 10px;
  font-weight: 700;
  cursor: pointer;
  min-height: 30px;
}
.btn.primary { background: var(--primary-strong); color: white; border-radius: 18px; padding-left: 14px; padding-right: 14px; }
.btn.secondary { background: var(--panel); color: var(--muted); border-color: var(--line); }
.btn.ghost { color: var(--muted); background: transparent; }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); border-radius: 18px; padding-left: 14px; padding-right: 14px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #fff;
  color: #0a1d36;
  font-size: 12px;
  font-weight: 800;
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line-soft);
}
.data-table thead th.sortable {
  cursor: pointer;
  user-select: none;
}
.data-table thead th.sortable::after {
  content: "↕";
  margin-left: 6px;
  color: var(--muted-light);
  font-size: 10px;
}
.data-table thead th.sortable.sort-asc::after {
  content: "↑";
  color: var(--primary);
}
.data-table thead th.sortable.sort-desc::after {
  content: "↓";
  color: var(--primary);
}
.data-table td {
  padding: 12px;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: top;
}
.data-table tbody tr:hover { background: #f6f9fd; }
.documents-table tbody tr { cursor: pointer; }
.table-panel { max-height: calc(100vh - 180px); overflow: auto; }
.strong { font-weight: 700; }
.muted { color: var(--muted); font-size: 12px; }
.num { text-align: right !important; }
.empty { color: var(--muted); text-align: center; padding: 24px !important; }

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  border: 1px solid transparent;
  padding: 2px 6px;
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
}
.badge.ok { color: var(--ok); background: var(--ok-soft); border-color: #bbf7d0; }
.badge.error { color: var(--danger); background: var(--danger-soft); border-color: #ffb4ac; }
.badge.warn { color: var(--warn); background: var(--warn-soft); border-color: #ffb596; }
.badge.neutral { color: var(--primary); background: var(--primary-soft); border-color: #b4c5ff; }
.badge.muted { color: var(--muted); background: var(--panel-high); border-color: var(--line); }

.kv { display: grid; grid-template-columns: 120px 1fr; gap: 8px 12px; padding: 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }

.actions { display: flex; align-items: center; gap: 8px; }
.notice { padding: 10px 12px; margin-bottom: 12px; border: 1px solid var(--line); border-radius: var(--radius); }
.notice.ok { color: var(--ok); background: var(--ok-soft); border-color: #bbf7d0; }
.notice.warn { color: var(--warn); background: var(--warn-soft); border-color: #ffb596; }

.editor-form { display: flex; flex-direction: column; gap: 12px; }
.editor-grid {
  display: grid;
  grid-template-columns: minmax(420px, 1.2fr) minmax(420px, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  min-height: calc(100vh - 260px);
}
.editor-pane { background: var(--panel); display: flex; flex-direction: column; min-width: 0; }
.pane-head span { color: var(--muted); font-size: 12px; }
.code-editor {
  width: 100%;
  flex: 1;
  resize: vertical;
  min-height: 420px;
  border: 0;
  border-radius: 0;
  padding: 12px;
  line-height: 1.55;
  white-space: pre;
  overflow: auto;
}
.code-editor.small { min-height: 110px; flex: 0 0 auto; border-top: 1px solid var(--line-soft); border-bottom: 1px solid var(--line-soft); }
.text-editor { width: 100%; min-height: 80px; padding: 8px; border-left: 0; border-right: 0; border-radius: 0; resize: vertical; }
.text-editor.large { min-height: 180px; border: 0; border-radius: 0; padding: 12px; }
.config-grid { display: grid; grid-template-columns: 1fr 180px; gap: 8px; padding: 12px; }
.config-grid label, .block-label { display: block; color: var(--muted); font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; }
.config-grid select, .config-grid input { display: block; width: 100%; margin-top: 4px; }
.block-label { padding: 10px 12px 4px; }

.validation-panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.issue-list { margin: 0; padding: 10px 12px 10px 32px; }
.issue-list li { margin: 4px 0; }
.issue-list li.error { color: var(--danger); }
.issue-list li.warning { color: var(--warn); }
.ok-text { color: var(--ok); }
.notice-inline { padding: 10px 12px; }
.editor-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); }
.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line-soft);
  background: #fff;
}

.ui-fields-editor-grid {
  display: grid;
  grid-template-columns: minmax(460px, 1fr) minmax(520px, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  min-height: calc(100vh - 250px);
}

.fields-preview-table {
  flex: 1;
  min-height: 420px;
  overflow: auto;
}

.fields-preview-table .data-table thead th { top: 0; }
.ui-config-grid { grid-template-columns: minmax(0, 1fr) 180px; border-top: 1px solid var(--line-soft); }
.ui-config-grid label { min-width: 0; }
.ui-config-grid textarea { width: 100%; min-height: 76px; margin-top: 4px; }
.ui-config-grid input { width: 100%; padding: 7px 9px; }

.diff-panel { margin-bottom: 16px; }
.diff-html { overflow: auto; padding: 12px; background: var(--panel); }
.diff-html table.diff { width: 100%; font-family: "SFMono-Regular", Consolas, monospace; font-size: 12px; border-collapse: collapse; }
.diff-html .diff_header { background: var(--panel-low); }
.diff-html td { padding: 2px 6px; border-bottom: 1px solid var(--line-soft); }
.diff_add { background: #dcfce7; }
.diff_sub { background: #ffdad6; }
.diff_chg { background: #ffdbcd; }

.json-preview,
.danger-zone pre {
  margin: 0;
  padding: 12px;
  overflow: auto;
  max-height: 520px;
  background: var(--panel);
  font-family: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre;
}

.danger-zone {
  padding: 12px;
  color: var(--muted);
}

.danger-panel { border-color: #ffb4ac; margin-top: 16px; }
.danger-panel .panel-head { background: #fff7f6; }
.danger-zone p { margin: 0 0 12px; }
.delete-preview { padding: 12px; }
.delete-preview .kv { padding: 0 0 12px; }
.delete-form { padding: 12px; }
.delete-form p { margin: 0 0 8px; color: var(--muted); }
.delete-form input {
  display: block;
  width: 100%;
  max-width: 520px;
  margin-top: 10px;
  padding: 8px 10px;
}

.danger-zone pre {
  margin-top: 12px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-low);
}

.wizard-form {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.wizard-steps {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.wizard-step {
  display: grid;
  grid-template-columns: 28px 1fr;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 42px;
  padding: 8px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.wizard-step span {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: var(--panel-low);
  color: var(--muted);
  font-weight: 800;
}

.wizard-step strong { font-size: 13px; }
.wizard-step.active { border-color: #b4c5ff; background: var(--primary-soft); }
.wizard-step.active span { background: var(--primary-strong); color: #fff; }

.wizard-body {
  min-width: 0;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: var(--panel);
  overflow: hidden;
}

.wizard-panel { display: none; }
.wizard-panel.active { display: block; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 12px;
  padding: 12px;
}

.form-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.form-grid input {
  width: 100%;
  padding: 7px 9px;
  color: var(--text);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
}

.form-grid small {
  color: var(--muted);
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
}

.user-form {
  grid-template-columns: repeat(3, minmax(220px, 1fr));
}

.inline-field {
  display: flex;
  gap: 8px;
}

.inline-field input {
  min-width: 0;
  flex: 1;
}

.inline-checkbox {
  align-self: end;
  padding: 0;
}

.form-actions {
  display: flex;
  align-items: end;
  justify-content: flex-end;
}

.system-layout {
  display: grid;
  grid-template-columns: minmax(220px, 260px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.system-menu {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.system-menu a {
  display: block;
  padding: 12px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel);
  text-decoration: none;
}

.system-menu a:hover {
  border-color: #b4c5ff;
  background: #f8fbff;
}

.system-menu a.active {
  border-color: #b4c5ff;
  background: var(--primary-soft);
}

.system-menu strong {
  display: block;
  color: #0a1d36;
}

.system-menu span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.system-content {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.wizard-editor {
  min-height: 430px;
  border-top: 1px solid var(--line-soft);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  color: var(--text);
  font-weight: 700;
}
.checkbox-row.compact {
  justify-content: flex-start;
  gap: 20px;
  padding-top: 0;
}
.checkbox-row.compact label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.muted-disabled {
  opacity: .52;
}

.wizard-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--line-soft);
  background: #f8fbff;
}

@media (max-width: 1100px) {
  .sidebar { display: none; }
  .rail { width: 48px; flex-basis: 48px; }
  .metric-grid, .split, .detail-grid, .editor-grid, .wizard-form, .form-grid { grid-template-columns: 1fr; }
  .global-search input { width: 220px; }
  .wizard-steps { position: static; }
}
