:root {
  --teal: #0d9488;
  --teal-dark: #0f766e;
  --amber: #d97706;
  --bg: #f4f6f8;
  --surface: #fff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.25rem 0;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 1.25rem 1.25rem;
  font-weight: 700;
  font-size: 1.1rem;
}
.brand-mark {
  width: 36px; height: 36px;
  background: var(--teal);
  color: #fff;
  border-radius: 10px;
  display: grid; place-items: center;
  font-weight: 700;
}
.brand-lg { justify-content: center; margin-bottom: 0.5rem; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  padding: 0.55rem 1.25rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.92rem;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: #f1f5f9; color: var(--text); }
.nav-item.active {
  color: var(--teal-dark);
  background: #ecfdf5;
  border-left-color: var(--teal);
  font-weight: 500;
}

.main-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.search { position: relative; flex: 1; max-width: 420px; }
.search input {
  width: 100%;
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font: inherit;
  background: var(--bg);
}
.search-results {
  position: absolute;
  top: 100%;
  left: 0; right: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 50;
  max-height: 280px;
  overflow-y: auto;
}
.search-results.hidden { display: none; }
.search-results a {
  display: block;
  padding: 0.6rem 1rem;
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.search-results a:hover { background: #f8fafc; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  font-size: 0.88rem;
}
.user-info { display: flex; flex-direction: column; line-height: 1.25; text-align: right; }
.user-login { font-size: 0.75rem; color: var(--muted); }
.role-badge {
  background: #ecfdf5;
  color: var(--teal-dark);
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}
.btn-logout {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  white-space: nowrap;
}
.btn-logout:hover { color: #b91c1c; border-color: #fecaca; background: #fef2f2; }

.content {
  padding: 1.5rem 2rem 3rem;
  max-width: 1100px;
  width: 100%;
  min-width: 0;
}

.page-title { margin: 0 0 0.5rem; font-size: 1.75rem; font-weight: 700; }
.page-lead { color: var(--muted); margin: 0 0 1.5rem; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 28px rgba(15, 23, 42, 0.1);
  border-color: #99f6e4;
}
.card-icon { font-size: 1.75rem; display: block; margin-bottom: 0.5rem; }
.card h2 { margin: 0 0 0.35rem; font-size: 1.1rem; }
.card p { margin: 0; color: var(--muted); font-size: 0.9rem; }
.card-link { display: inline-block; margin-top: 0.75rem; color: var(--teal); font-size: 0.85rem; font-weight: 500; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.panel h3 { margin: 0 0 1rem; font-size: 1rem; }

.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table td { padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--border); }
.table a { color: var(--teal-dark); }

.breadcrumb {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 1rem;
}
.breadcrumb a { color: var(--teal); text-decoration: none; }
.breadcrumb span { margin: 0 0.35rem; color: #cbd5e1; }

.doc-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}
.doc-header-main { flex: 1; min-width: 200px; }
.doc-lead {
  margin: 0.35rem 0 0.75rem;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.55;
  max-width: 42rem;
}
.doc-meta { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.doc-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: flex-start; }
.doc-children { margin-bottom: 1.5rem; }
.doc-children h3 { margin: 0 0 0.75rem; font-size: 0.95rem; }
.doc-list { margin: 0; padding: 0; list-style: none; }
.doc-list li { padding: 0.55rem 0; border-bottom: 1px solid var(--border); }
.doc-list li:last-child { border-bottom: none; }
.doc-list a { color: var(--teal-dark); text-decoration: none; font-weight: 500; }
.doc-list a:hover { text-decoration: underline; }
.chip {
  display: inline-block;
  background: #ecfdf5;
  color: var(--teal-dark);
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-size: 0.78rem;
  margin-right: 0.35rem;
}
.chip-muted { background: #f1f5f9; color: var(--muted); }

.doc-body {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 1.75rem 2rem;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-width: 0;
}

.markdown {
  font-size: 1rem;
  line-height: 1.65;
  color: var(--text);
  min-width: 0;
}
.markdown > :first-child { margin-top: 0; }
.markdown > p,
.markdown > ul,
.markdown > ol,
.markdown > blockquote,
.markdown > .admonition,
.markdown > h2,
.markdown > h3 {
  max-width: 42rem;
}
.markdown p { margin: 0 0 1rem; }
.markdown h1 { display: none; }
.markdown h2 {
  margin: 2rem 0 0.75rem;
  padding-bottom: 0.4rem;
  font-size: 1.2rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  color: #0f172a;
}
.markdown h3 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1.05rem;
  font-weight: 600;
  color: #334155;
}
.markdown ul, .markdown ol {
  margin: 0 0 1rem;
  padding-left: 1.35rem;
}
.markdown li { margin: 0.35rem 0; }
.markdown li > p { margin: 0.25rem 0; }
.markdown strong { font-weight: 600; color: #0f172a; }
.markdown code {
  font-family: ui-monospace, "Cascadia Code", "Segoe UI Mono", Consolas, monospace;
  font-size: 0.88em;
  background: #f1f5f9;
  color: #0f172a;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  border: 1px solid #e2e8f0;
}
.markdown pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 1rem 1.15rem;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 0.84rem;
  line-height: 1.5;
  margin: 0.75rem 0 1.25rem;
  border: 1px solid #1e293b;
}
.markdown pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: inherit;
  color: inherit;
  word-break: normal;
}

/* Широкие таблицы — прокрутка внутри карточки */
.markdown .table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 1rem 0 1.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fafbfc;
}
.markdown .table-scroll:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}
.markdown .table-scroll table {
  width: 100%;
  min-width: 32rem;
  border-collapse: collapse;
  margin: 0;
  font-size: 0.9rem;
}
.markdown th,
.markdown td {
  border: 1px solid var(--border);
  padding: 0.6rem 0.85rem;
  vertical-align: top;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.markdown th {
  background: #f1f5f9;
  text-align: left;
  font-weight: 600;
  color: #334155;
  white-space: nowrap;
}
.markdown td code {
  font-size: 0.82em;
  white-space: normal;
  word-break: break-all;
}
.markdown a { color: var(--teal-dark); text-decoration: none; }
.markdown a:hover { text-decoration: underline; }
.markdown blockquote {
  border-left: 4px solid var(--amber);
  margin: 1rem 0;
  padding: 0.5rem 1rem;
  background: #fffbeb;
}
.markdown .admonition {
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin: 1rem 0;
  border-left: 4px solid var(--border);
  background: #f8fafc;
}
.markdown .admonition-title { font-weight: 600; margin: 0 0 0.35rem; }
.markdown .admonition-info { border-left-color: #3b82f6; background: #eff6ff; }
.markdown .admonition-tip { border-left-color: var(--teal); background: #ecfdf5; }
.markdown .admonition-warning { border-left-color: var(--amber); background: #fffbeb; }
.markdown .admonition p { margin: 0.35rem 0; }

.markdown .doc-cards > ul {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  list-style: none;
  padding: 0;
  margin: 1.25rem 0;
}
.markdown .doc-cards > ul > li {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}
.markdown .doc-cards > ul > li hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.65rem 0;
}
.markdown .doc-cards > ul > li p { margin: 0.35rem 0; color: var(--muted); font-size: 0.9rem; }
.markdown .doc-cards > ul > li a { color: var(--teal-dark); font-weight: 500; }

.admin-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.admin-panel { overflow-x: auto; }
.admin-table th { text-align: left; font-size: 0.8rem; color: var(--muted); padding: 0.5rem 0.75rem; border-bottom: 2px solid var(--border); }
.perm-col { text-align: center; font-size: 0.72rem; max-width: 2.5rem; }
.perm-cell { text-align: center; }
.actions-cell { white-space: nowrap; }
.muted { color: var(--muted); font-size: 0.85rem; }
.role-tag {
  display: inline-block;
  background: #f1f5f9;
  color: var(--text);
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
  font-size: 0.75rem;
  margin: 0.1rem 0.15rem 0.1rem 0;
}
.perm-badge {
  display: inline-block;
  min-width: 1.5rem;
  text-align: center;
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
  font-size: 0.72rem;
  font-weight: 600;
}
.perm-edit { background: #dcfce7; color: #166534; }
.perm-read { background: #dbeafe; color: #1e40af; }
.perm-none { background: #f1f5f9; color: #94a3b8; }
.perm-legend { margin: 1rem 0 0; font-size: 0.82rem; color: var(--muted); }
.admin-form label { display: block; margin-bottom: 1rem; font-size: 0.88rem; font-weight: 500; }
.admin-form input[type="text"],
.admin-form input[type="password"],
.admin-form select {
  display: block; width: 100%; max-width: 400px; margin-top: 0.35rem;
  padding: 0.6rem 0.75rem; border: 1px solid var(--border); border-radius: 8px; font: inherit;
  background: var(--surface);
}
.roles-fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 1rem; margin: 1rem 0; }
.roles-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 0.5rem; }
.role-check { font-weight: 400; display: flex; align-items: center; gap: 0.4rem; }
.admin-delete { margin-top: 1.5rem; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.82rem; }
.btn-danger { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.btn-danger:hover { background: #fee2e2; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  border: none;
}
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:hover { background: var(--teal-dark); }
.btn-secondary { background: var(--surface); border: 1px solid var(--border); color: var(--text); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-block { width: 100%; justify-content: center; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #ecfdf5 0%, #f4f6f8 50%, #fffbeb 100%);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.login-sub { text-align: center; color: var(--muted); margin: 0 0 1.5rem; }
.login-form label { display: block; margin-bottom: 1rem; font-size: 0.88rem; font-weight: 500; }
.login-form input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
}
.alert-error {
  background: #fef2f2;
  color: #b91c1c;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.88rem;
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

#editor, .EasyMDEContainer { margin-bottom: 1rem; }


/* Mermaid diagrams in docs */
.doc-body .mermaid-chart {
  margin: 1rem 0 1.75rem;
  padding: 1.25rem 1rem 0.75rem;
  background: linear-gradient(180deg, #f8fafc 0%, #fff 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  text-align: center;
}
.doc-body .mermaid-chart--zoomable {
  cursor: zoom-in;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}
.doc-body .mermaid-chart--zoomable:hover {
  border-color: #99d5cf;
  box-shadow: 0 6px 24px rgba(13, 148, 136, 0.12);
}
.doc-body .mermaid-chart .mermaid {
  display: block;
  width: 100%;
  min-height: 4rem;
  overflow: visible;
}
.doc-body .mermaid-chart svg {
  display: block;
  margin: 0 auto;
  max-width: none !important;
  width: 100% !important;
  height: auto !important;
  min-height: 3rem;
  overflow: visible !important;
}
.doc-body .mermaid-chart svg foreignObject {
  overflow: visible !important;
}
.doc-body .mermaid-chart svg .nodeLabel,
.doc-body .mermaid-chart svg .label {
  overflow: visible !important;
  white-space: nowrap !important;
}
.doc-body .mermaid-chart-error {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: #b45309;
}
.doc-body .mermaid-chart-hint {
  margin: 0.65rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.mermaid-modal {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.mermaid-modal.hidden { display: none; }
.mermaid-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
}
.mermaid-modal-panel {
  position: relative;
  z-index: 1;
  width: min(96vw, 72rem);
  max-height: 92vh;
  overflow: auto;
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
  padding: 1.25rem 1rem 1.5rem;
}
.mermaid-modal-close {
  position: sticky;
  top: 0;
  float: right;
  border: none;
  background: #f1f5f9;
  color: var(--text);
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 8px;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
}
.mermaid-modal-body {
  clear: both;
  padding: 0.5rem 0.5rem 0;
  text-align: center;
  overflow: auto;
}
.mermaid-modal-body svg {
  display: block;
  margin: 0 auto;
  max-width: none !important;
  width: 100% !important;
  height: auto !important;
  overflow: visible !important;
}
.mermaid-modal-body foreignObject {
  overflow: visible !important;
}

/* Collapsible diagram blocks (legacy) */
.doc-body details.doc-diagram {
  margin: 1.25rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafc;
  overflow: hidden;
}
.doc-body details.doc-diagram summary {
  cursor: pointer;
  padding: 0.85rem 1.1rem;
  font-weight: 600;
  list-style: none;
  user-select: none;
}
.doc-body details.doc-diagram summary::-webkit-details-marker { display: none; }
.doc-body details.doc-diagram summary::before {
  content: "▸ ";
  color: var(--teal);
  font-weight: 700;
}
.doc-body details.doc-diagram[open] summary::before { content: "▾ "; }
.doc-body details.doc-diagram[open] summary {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.doc-body details.doc-diagram > pre,
.doc-body details.doc-diagram > .highlight,
.doc-body details.doc-diagram > p {
  margin: 0;
  padding: 1rem 1.1rem 1.1rem;
}
.doc-body details.doc-diagram pre {
  margin: 0 1rem 1rem;
  font-size: 0.82rem;
}

@media (max-width: 768px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 0;
  }
  .brand { padding: 0 1rem 0.75rem; }
  .nav {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 0.5rem 0.5rem;
    gap: 0.25rem;
  }
  .nav-item {
    flex-shrink: 0;
    border-left: none;
    border-bottom: 2px solid transparent;
    border-radius: 8px;
    padding: 0.45rem 0.85rem;
    font-size: 0.85rem;
  }
  .nav-item.active {
    border-left: none;
    border-bottom-color: var(--teal);
  }
  .topbar {
    flex-wrap: wrap;
    padding: 0.65rem 1rem;
    gap: 0.65rem;
  }
  .search { max-width: none; order: 1; flex: 1 1 100%; }
  .user-chip {
    order: 2;
    flex: 1 1 auto;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 0.4rem;
  }
  .user-info { text-align: left; }
  .role-badge { display: none; }
  .content { padding: 1rem 0.75rem 2rem; }
  .page-title { font-size: 1.35rem; }
  .doc-header { flex-direction: column; align-items: stretch; }
  .doc-actions { width: 100%; }
  .doc-actions .btn { flex: 1; justify-content: center; min-width: 0; }
  .doc-body { padding: 1rem 1rem 1.5rem; }
  .doc-lead { font-size: 0.95rem; max-width: none; }
  .markdown > p,
  .markdown > ul,
  .markdown > ol,
  .markdown > blockquote,
  .markdown > .admonition,
  .markdown > h2,
  .markdown > h3 { max-width: none; }
  .markdown .table-scroll table { min-width: 28rem; font-size: 0.84rem; }
  .markdown th, .markdown td { padding: 0.5rem 0.65rem; }
  .markdown pre { font-size: 0.78rem; padding: 0.85rem; }
  .cards { grid-template-columns: 1fr; }
  .breadcrumb { font-size: 0.78rem; line-height: 1.4; }
  .admin-panel { margin: 0 -0.25rem; }
}

@media (max-width: 400px) {
  .btn-logout { padding: 0.3rem 0.5rem; font-size: 0.78rem; }
  .user-name { font-size: 0.85rem; }
}
