@import url("https://fonts.googleapis.com/css2?family=Manrope:wght@500;600;700&display=swap");

:root {
  --surface-primary: #ffffff;
  --surface-secondary: #fbfbfc;
  --ui-quaternary: #f7f7f7;
  --ui-brand-accent-primary: #0281e8;
  --ui-brand-accent-secondary: #0076d6;
  --ui-brand-muted-secondary: rgba(2, 129, 232, .16);
  --text-primary: #213249;
  --text-secondary: #717c8b;
  --text-invert: #ffffff;
  --border-quaternary: #eeeff1;
  --border-tertiary: #dbdee2;
  --border-brand: #0281e8;
  --radius-8: 8px;
  --radius-12: 12px;
  --radius-16: 16px;
  --space-4: 4px;
  --space-8: 8px;
  --space-12: 12px;
  --space-16: 16px;
  --space-20: 20px;
  --space-24: 24px;
  --shadow-m: 0 8px 48px rgba(33, 50, 73, .12);
  --bg: #f7f8fa;
  --panel: var(--surface-primary);
  --ink: var(--text-primary);
  --muted: var(--text-secondary);
  --line: var(--border-quaternary);
  --accent: var(--ui-brand-accent-primary);
  --accent-2: #f59e0b;
  --danger: #b91c1c;
  --ok: #15803d;
  --focus: var(--ui-brand-accent-primary);
  font-family: Manrope, Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  overflow-x: hidden;
}

#app { min-width: 0; }

button, input, select, textarea {
  font: inherit;
}

button {
  align-items: center;
  border: 1px solid var(--border-tertiary);
  background: var(--surface-primary);
  color: var(--text-primary);
  border-radius: var(--radius-12);
  display: inline-flex;
  font-weight: 600;
  gap: var(--space-8);
  justify-content: center;
  min-height: 40px;
  min-width: 80px;
  max-width: 100%;
  padding: var(--space-8) var(--space-16);
  white-space: normal;
  overflow-wrap: anywhere;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, box-shadow .15s ease, transform .15s ease;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  min-height: 48px;
  min-width: 96px;
  padding-left: var(--space-16);
  padding-right: var(--space-20);
}

button.primary:hover {
  background: var(--ui-brand-accent-secondary);
  border-color: var(--ui-brand-accent-secondary);
  box-shadow: 0 8px 24px rgba(2, 129, 232, .18);
}

button.warning {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #fff;
}

button.danger {
  background: #fff;
  border-color: #fecaca;
  color: var(--danger);
}

button.danger:hover {
  background: #fef2f2;
  border-color: #fca5a5;
}

button:disabled {
  opacity: .55;
  cursor: not-allowed;
}

input, select, textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--ui-quaternary);
  border-radius: var(--radius-12);
  color: var(--text-primary);
  font-weight: 600;
  min-height: 48px;
  padding: var(--space-12) var(--space-16);
  outline: none;
  transition: background .15s ease, border-color .15s ease, box-shadow .15s ease;
}

input:focus, select:focus, textarea:focus {
  background: var(--surface-primary);
  border-color: var(--border-brand);
  box-shadow: 0 0 0 3px var(--ui-brand-muted-secondary);
}

textarea { min-height: 78px; resize: vertical; }

label {
  display: grid;
  gap: var(--space-8);
  font-size: 13px;
  color: var(--muted);
}

.shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 100vh;
  min-width: 0;
}

.sidebar {
  background: var(--surface-primary);
  border-right: 1px solid var(--border-quaternary);
  color: var(--text-primary);
  padding: var(--space-24) var(--space-16);
  display: flex;
  flex-direction: column;
  gap: var(--space-20);
  min-width: 0;
}

.brand {
  padding: 0 var(--space-8) var(--space-16);
  border-bottom: 1px solid var(--border-quaternary);
}

.brand b { display: block; font-size: 18px; letter-spacing: 0; }
.brand span { color: var(--text-secondary); font-size: 12px; }

.nav {
  display: grid;
  gap: 6px;
}

.nav button {
  width: 100%;
  text-align: left;
  justify-content: flex-start;
  color: var(--text-secondary);
  background: transparent;
  border-color: transparent;
  min-height: 48px;
  line-height: 1.25;
}

.nav button.active, .nav button:hover {
  background: var(--ui-brand-muted-secondary);
  color: var(--text-primary);
}

.userbox {
  margin-top: auto;
  color: var(--text-secondary);
  font-size: 13px;
  display: grid;
  gap: var(--space-8);
  min-width: 0;
  overflow-wrap: anywhere;
}

.main {
  min-width: 0;
  padding: clamp(16px, 2.4vw, 28px);
  width: 100%;
  overflow: hidden;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-16);
  margin-bottom: var(--space-20);
  flex-wrap: wrap;
  min-width: 0;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(22px, 2.2vw, 28px);
  font-weight: 700;
  letter-spacing: 0;
  overflow-wrap: anywhere;
}

.grid {
  display: grid;
  gap: var(--space-16);
  min-width: 0;
}

.grid > * { min-width: 0; }

.grid.cols-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.grid.cols-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.panel, .card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-16);
  min-width: 0;
  overflow: hidden;
}

.panel { padding: var(--space-20); }
.card { padding: var(--space-16); }

.card:hover {
  background: var(--ui-quaternary);
}

.panel h2, .card h3 {
  margin-top: 0;
  overflow-wrap: anywhere;
}

.metric {
  display: grid;
  gap: 6px;
}

.metric span { color: var(--muted); font-size: 13px; }
.metric b { font-size: 28px; color: var(--text-primary); }

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  border-radius: var(--radius-16);
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  min-width: 760px;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-16);
  overflow: hidden;
}

th, td {
  text-align: left;
  padding: var(--space-16);
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  font-size: 14px;
  line-height: 1.4;
  overflow-wrap: anywhere;
  word-break: normal;
}

th {
  color: var(--muted);
  background: var(--ui-quaternary);
  font-size: 14px;
  font-weight: 600;
  height: 48px;
  text-transform: none;
  white-space: normal;
}

.dense-table table {
  min-width: 1180px;
  table-layout: fixed;
}

.dense-table th,
.dense-table td {
  padding: var(--space-12);
  font-size: 13px;
}

tr:hover td { background: #f9fbfc; }

.tabs {
  display: flex;
  gap: var(--space-8);
  flex-wrap: wrap;
  margin-bottom: var(--space-16);
  min-width: 0;
}

.tabs button.active {
  border-color: var(--border-brand);
  background: var(--ui-brand-muted-secondary);
  color: var(--text-primary);
}

.tabs button {
  flex: 1 1 180px;
  min-width: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-16);
}

.form-grid > label {
  min-width: 0;
}

.field-help {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.required {
  color: var(--danger);
}

.status {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: var(--space-4) var(--space-12);
  background: var(--ui-brand-muted-secondary);
  color: var(--ui-brand-accent-secondary);
  font-size: 12px;
  font-weight: 700;
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
}

.status.ok {
  background: #dcfce7;
  color: var(--ok);
}

.status.warn {
  background: #fef3c7;
  color: #92400e;
}

.status.danger {
  background: #fee2e2;
  color: var(--danger);
}

.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.auth-card {
  width: min(440px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-16);
  box-shadow: var(--shadow-m);
  padding: var(--space-24);
  display: grid;
  gap: var(--space-16);
}

.auth-card h1 { margin: 0; font-size: 26px; letter-spacing: 0; }
.auth-card p { margin: 0; color: var(--muted); }

.actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.relational-toolbar {
  display: grid;
  grid-template-columns: minmax(260px, 420px) 1fr;
  gap: var(--space-16);
  align-items: end;
  min-width: 0;
}

.relation-summary {
  display: flex;
  gap: var(--space-8);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.relational-grid {
  grid-template-columns: 320px minmax(0, 1fr);
  margin-top: var(--space-16);
}

.entity-list {
  display: grid;
  align-content: start;
  gap: var(--space-8);
  max-height: 760px;
  overflow: auto;
}

.entity-list button {
  display: grid;
  gap: 6px;
  justify-content: stretch;
  text-align: left;
  width: 100%;
  min-height: 64px;
  align-content: center;
  padding-top: var(--space-12);
  padding-bottom: var(--space-12);
}

.entity-list button.active {
  border-color: var(--border-brand);
  background: var(--ui-brand-muted-secondary);
}

.entity-list small {
  color: var(--muted);
  display: block;
  font-size: 12px;
  line-height: 1.25;
}

.record-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-12);
  padding: var(--space-16);
  margin-top: var(--space-16);
  min-width: 0;
}

.record-head {
  display: flex;
  justify-content: space-between;
  gap: var(--space-16);
  margin-bottom: var(--space-16);
  flex-wrap: wrap;
  min-width: 0;
}

.relation-map {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--space-12);
  margin-bottom: var(--space-16);
}

.relation-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-12);
  display: grid;
  gap: var(--space-8);
  padding: var(--space-16);
}

.relation-card span:not(.status) {
  color: var(--muted);
}

.relation-card small {
  color: var(--muted);
  line-height: 1.35;
}

.relation-card p {
  margin: 0;
  color: var(--muted);
}

.export-presets {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-8);
  margin-bottom: var(--space-16);
}

.export-layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: var(--space-16);
}

.export-entities,
.export-fields {
  min-width: 0;
}

.export-entities {
  max-height: 560px;
  overflow: auto;
}

.field-group {
  border: 1px solid var(--line);
  border-radius: var(--radius-12);
  padding: var(--space-16);
  margin-bottom: var(--space-12);
}

.field-checkboxes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--space-8);
  margin-top: var(--space-12);
}

.check-row {
  align-items: start;
  border: 1px solid var(--line);
  border-radius: var(--radius-12);
  display: grid;
  gap: var(--space-8);
  grid-template-columns: auto 1fr;
  padding: var(--space-12);
}

.check-row input {
  height: 18px;
  min-height: 18px;
  width: 18px;
}

.check-row small {
  color: var(--muted);
  display: block;
  font-size: 12px;
  line-height: 1.3;
  margin-top: 2px;
}

code {
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: normal;
}

.backup-panel {
  grid-column: 1 / -1;
}

.muted { color: var(--muted); }
.error { color: var(--danger); }
.success { color: var(--ok); }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; padding: var(--space-16); }
  .nav { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .nav button { justify-content: center; text-align: center; min-height: 42px; }
  .grid.cols-4, .grid.cols-2, .form-grid, .relational-grid, .relational-toolbar, .export-layout { grid-template-columns: 1fr; }
  .relation-summary { justify-content: flex-start; }
  .topbar { align-items: flex-start; flex-direction: column; }
}

@media (max-width: 640px) {
  body { font-size: 13px; }
  .main { padding: var(--space-12); }
  .panel { padding: var(--space-16); }
  .brand { padding-left: 0; padding-right: 0; }
  .grid { gap: var(--space-12); }
  .metric b { font-size: 24px; }
  .tabs button { flex-basis: 100%; }
  .table-wrap {
    overflow: visible;
    border-radius: 0;
  }
  table {
    min-width: 0;
    border: 0;
    background: transparent;
  }
  thead {
    display: none;
  }
  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }
  tr {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-12);
    margin-bottom: var(--space-12);
    overflow: hidden;
  }
  td {
    border-bottom: 1px solid var(--line);
    padding: var(--space-12);
  }
  td:last-child {
    border-bottom: 0;
  }
  td[data-label]::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: 12px;
    margin-bottom: var(--space-4);
  }
  td[colspan] {
    display: table-cell;
  }
  .record-head {
    display: grid;
  }
}
