:root {
  --app-bg: #f4f7fb;
  --panel-bg: #ffffff;
  --text: #172033;
  --muted: #6b7280;
  --line: #e5e7eb;
  --soft: #eef2ff;
  --shadow: 0 16px 40px rgba(15, 23, 42, .08);
}

body {
  background: var(--app-bg);
  color: var(--text);
  font-family: Vazirmatn, Tahoma, Arial, sans-serif;
}

body.dark {
  --app-bg: #101827;
  --panel-bg: #172033;
  --text: #eef2ff;
  --muted: #aeb7c8;
  --line: #293449;
  --soft: #222b40;
  --shadow: 0 16px 40px rgba(0, 0, 0, .28);
}

.app-nav {
  background: rgba(255, 255, 255, .88);
  backdrop-filter: blur(12px);
}

body.dark .app-nav,
body.dark .modal-content,
body.dark .dropdown-menu {
  background: #172033;
  color: var(--text);
}

.setup-shell {
  align-items: center;
  display: flex;
  min-height: 100vh;
  padding: 1rem;
}

.login-shell {
  align-items: center;
  display: flex;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  margin: 0 auto;
  max-width: 430px;
  padding: 1.5rem;
  width: 100%;
}

.login-card h1 {
  font-size: 1.45rem;
  margin: 0 0 .6rem;
}

.login-card > p {
  color: var(--muted);
  font-size: .88rem;
  line-height: 1.8;
  margin-bottom: 1.25rem;
}

.setup-card {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 1.5rem;
  grid-template-columns: minmax(280px, .9fr) minmax(320px, 1.1fr);
  margin: 0 auto;
  max-width: 1080px;
  padding: 1.25rem;
  width: 100%;
}

.setup-copy {
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.25rem;
}

.setup-kicker {
  color: #2563eb;
  display: inline-block;
  font-size: .8rem;
  font-weight: 800;
  margin-bottom: .65rem;
}

.setup-copy h1 {
  font-size: 1.6rem;
  line-height: 1.55;
  margin: 0 0 .75rem;
}

.setup-copy p {
  color: var(--muted);
  line-height: 1.9;
  margin: 0;
}

.setup-steps {
  display: grid;
  gap: .75rem;
  margin-top: 1rem;
}

.setup-steps div,
.setup-status {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: .85rem;
}

.setup-steps strong {
  display: block;
  font-size: .86rem;
  margin-bottom: .45rem;
}

.setup-steps code {
  direction: ltr;
  display: block;
  text-align: left;
  white-space: normal;
}

.setup-status {
  color: var(--muted);
  line-height: 1.8;
  margin-top: 1rem;
}

.setup-status.success {
  border-color: #86efac;
  color: #166534;
}

.setup-status.error {
  border-color: #fca5a5;
  color: #b91c1c;
}

.setup-status.checking {
  border-color: #93c5fd;
  color: #1d4ed8;
}

.manual-config {
  color: var(--text);
}

.manual-config > strong {
  display: block;
  font-size: .95rem;
  margin-bottom: .45rem;
}

.manual-config p {
  color: var(--muted);
  line-height: 1.9;
  margin: 0 0 .85rem;
  white-space: pre-line;
}

.manual-config-head {
  align-items: center;
  display: flex;
  gap: .75rem;
  justify-content: space-between;
  margin-bottom: .5rem;
}

.manual-config-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  justify-content: flex-end;
}

.manual-config-head span {
  direction: ltr;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: .84rem;
}

.manual-config pre {
  background: #0f172a;
  border-radius: 12px;
  color: #e5e7eb;
  direction: ltr;
  font-size: .78rem;
  line-height: 1.7;
  margin: 0;
  max-height: 260px;
  overflow: auto;
  padding: .85rem;
  text-align: left;
  white-space: pre-wrap;
}

.setup-form {
  align-self: center;
}

.setup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) minmax(280px, 1.08fr) minmax(300px, 1.1fr) minmax(320px, 1.2fr);
  gap: 1rem;
  min-height: calc(100vh - 96px);
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 640px;
  max-height: calc(100vh - 96px);
  overflow: hidden;
}

.panel-head {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  padding: 1rem;
  position: sticky;
  top: 0;
  background: var(--panel-bg);
  z-index: 2;
}

.panel-head h2 {
  font-size: 1rem;
  margin: 0;
}

.panel-head p {
  color: var(--muted);
  font-size: .78rem;
  margin: .25rem 0 0;
}

.panel > .form-control,
.panel > .selected-title,
.panel > .list-stack,
.panel > .config-form,
.panel > .live-preview {
  margin-left: 1rem;
  margin-right: 1rem;
}

.list-stack,
.config-form,
.live-preview {
  overflow: auto;
  padding-bottom: 1rem;
}

.list-stack {
  display: flex;
  flex-direction: column;
  gap: .75rem;
}

.card-row {
  background: linear-gradient(180deg, var(--panel-bg), var(--soft));
  border: 1px solid var(--line);
  border-radius: 16px;
  cursor: pointer;
  padding: .9rem;
  transition: .15s ease;
}

.card-row:hover {
  transform: translateY(-1px);
  border-color: #93c5fd;
}

.card-row.active {
  border-color: #2563eb;
  box-shadow: inset 4px 0 0 #2563eb;
}

.card-row.dragging {
  opacity: .55;
}

.row-title {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  align-items: flex-start;
  font-weight: 700;
}

.row-meta {
  color: var(--muted);
  font-size: .78rem;
  margin-top: .35rem;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  margin-top: .7rem;
}

#formatsList {
  gap: .45rem;
}

.format-row {
  padding: .5rem .65rem;
}

.format-row-title {
  align-items: center;
  min-width: 0;
}

.format-row-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.format-row.expanded {
  padding: .7rem .75rem;
}

.format-row-description {
  line-height: 1.8;
  margin-top: .55rem;
}

.format-row-actions {
  margin-top: .55rem;
}

.format-row-action {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 7px;
  display: inline-flex;
  height: 26px;
  justify-content: center;
  padding: 0;
  transition: .15s ease;
  width: 26px;
}

.format-row-action.edit {
  color: #2563eb;
}

.format-row-action.edit:hover {
  background: rgba(37, 99, 235, .1);
}

.format-row-action.delete {
  color: #dc3545;
}

.format-row-action.delete:hover {
  background: rgba(220, 53, 69, .1);
}

.format-row-action svg {
  fill: currentColor;
  height: 15px;
  width: 15px;
}

#formatFieldsList {
  gap: .45rem;
}

.format-field-row {
  align-items: center;
  display: grid;
  gap: .45rem;
  grid-template-columns: minmax(0, 1fr) auto auto auto;
  padding: .5rem .65rem;
}

.format-field-title {
  align-items: center;
  justify-content: flex-start;
  min-width: 0;
}

.format-field-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.format-field-required {
  color: var(--muted);
  font-size: .72rem;
  white-space: nowrap;
}

.format-field-edit,
.format-field-remove {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 7px;
  display: inline-flex;
  height: 26px;
  justify-content: center;
  padding: 0;
  transition: .15s ease;
  width: 26px;
}

.format-field-edit {
  color: #2563eb;
}

.format-field-remove {
  color: #dc3545;
}

.format-field-edit:hover {
  background: rgba(37, 99, 235, .1);
}

.format-field-remove:hover {
  background: rgba(220, 53, 69, .1);
}

.format-field-edit svg,
.format-field-remove svg {
  fill: currentColor;
  height: 15px;
  width: 15px;
}

.selected-title {
  background: var(--soft);
  border-radius: 14px;
  color: var(--muted);
  font-size: .85rem;
  margin-bottom: 1rem;
  margin-top: 1rem;
  padding: .75rem;
}

.relation-summary,
.relation-required,
.relation-editor {
  margin-bottom: 1rem;
}

.relation-editor {
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: .9rem;
}

.relation-editor-head {
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  margin-bottom: .75rem;
}

.relation-editor-head h3 {
  font-size: .96rem;
  margin: 0;
}

.relation-editor-head p {
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.8;
  margin: .25rem 0 0;
}

.quick-grid {
  display: grid;
  gap: .6rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-bottom: .75rem;
}

.quick-field span {
  color: var(--muted);
  display: block;
  font-size: .78rem;
  margin-bottom: .35rem;
}

.friendly-empty {
  background: var(--panel-bg);
  border: 1px dashed var(--line);
  border-radius: 12px;
  color: var(--muted);
  font-size: .8rem;
  line-height: 1.8;
  padding: .7rem;
}

.legacy-notice {
  background: rgba(245, 158, 11, .1);
  border: 1px solid rgba(245, 158, 11, .3);
  border-radius: 12px;
  color: #92400e;
  font-size: .78rem;
  line-height: 1.8;
  padding: .65rem .75rem;
}

.legacy-notice-action {
  align-items: center;
  display: flex;
  gap: .65rem;
  justify-content: space-between;
  margin-top: .7rem;
}

.legacy-notice-action button {
  flex: 0 0 auto;
}

body.dark .legacy-notice {
  color: #fcd34d;
}

.condition-builder {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  margin-bottom: .85rem;
  padding: .65rem;
}

.condition-friendly-grid {
  display: grid;
  gap: .7rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.condition-friendly-grid label > span {
  color: var(--muted);
  display: block;
  font-size: .78rem;
  margin-bottom: .35rem;
}

.condition-preview-badge {
  background: rgba(37, 99, 235, .08);
  border-radius: 9px;
  color: #2563eb;
  font-size: .72rem;
  line-height: 1.7;
  margin: .15rem 0 .55rem;
  padding: .35rem .55rem;
}

body.dark .condition-preview-badge {
  color: #93c5fd;
}

.condition-builder-head {
  align-items: center;
  display: flex;
  gap: .75rem;
  justify-content: space-between;
  margin-bottom: .65rem;
}

.condition-builder-head strong {
  font-size: .86rem;
}

.condition-list {
  display: grid;
  gap: .5rem;
}

.condition-row {
  display: grid;
  gap: .5rem;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
}

.condition-row input {
  direction: ltr;
  text-align: left;
}

.condition-empty {
  color: var(--muted);
  font-size: .8rem;
  padding: .35rem 0;
}

.json-editor {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

.json-editor-toolbar {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: .6rem;
  justify-content: space-between;
  padding: .65rem;
}

.json-editor-toolbar label {
  direction: ltr;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: .78rem;
  font-weight: 700;
  margin: 0;
  text-align: left;
}

.json-editor-toolbar div {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  justify-content: flex-end;
}

.json-textarea {
  border: 0;
  border-radius: 0;
  direction: ltr;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: .82rem;
  line-height: 1.7;
  resize: vertical;
  text-align: left;
}

.json-editor-status {
  border-top: 1px solid var(--line);
  color: var(--muted);
  direction: rtl;
  font-size: .76rem;
  padding: .45rem .65rem;
}

.json-editor-status.is-valid {
  color: #15803d;
}

.json-editor-status.is-invalid {
  color: #b91c1c;
}

.empty-message {
  align-items: center;
  color: var(--muted);
  display: flex;
  justify-content: center;
  min-height: 180px;
  text-align: center;
}

.badge-soft {
  background: var(--soft);
  color: var(--text);
  border: 1px solid var(--line);
}

.editor-box {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1rem;
  background: var(--soft);
}

.editor-items {
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.chip-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
}

.chip {
  align-items: center;
  background: #dbeafe;
  border-radius: 999px;
  color: #1e40af;
  display: inline-flex;
  gap: .35rem;
  padding: .3rem .65rem;
}

.chip button {
  border: 0;
  background: transparent;
  color: inherit;
  line-height: 1;
}

.preview-card {
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1rem;
  background: var(--panel-bg);
}

.preview-control {
  opacity: .82;
}

body.dark .form-control,
body.dark .form-select,
body.dark .table,
body.dark textarea {
  background-color: #111827;
  border-color: #374151;
  color: var(--text);
}

body.dark .btn-close {
  filter: invert(1);
}

@media (max-width: 1400px) {
  .admin-grid {
    grid-template-columns: repeat(2, minmax(280px, 1fr));
  }
}

@media (max-width: 780px) {
  .setup-card {
    grid-template-columns: 1fr;
  }

  .quick-grid,
  .condition-row,
  .condition-friendly-grid {
    grid-template-columns: 1fr;
  }

  .legacy-notice-action {
    align-items: stretch;
    flex-direction: column;
  }

  .admin-grid {
    grid-template-columns: 1fr;
  }

  .panel {
    max-height: none;
    min-height: 420px;
  }
}
