:root {
  --app-vh: 100vh;
  --modal-shell-padding: clamp(12px, 2.2vw, 28px);
}

body.modal-open {
  overflow: hidden;
  padding-right: 0;
}

body:not(.landing-template) .modal {
  position: fixed;
  inset: 0;
  display: none !important;
  opacity: 0 !important;
  pointer-events: none !important;
  place-items: center;
  padding: var(--modal-shell-padding);
  overflow: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(2px);
  z-index: 2500;
}

body:not(.landing-template) .modal.is-open {
  display: grid !important;
  opacity: 1 !important;
  pointer-events: auto !important;
}

/*
 * No-JS / pre-JS fallback: only apply :target while the modal system hasn't
 * booted. Once JS adds `data-modal-system="ready"` to <html>, ownership of
 * the visible state moves to the `.is-open` class. This avoids the
 * Chrome/WebKit quirk where `:target` stays stale after `history.replaceState`
 * removes the hash, leaving the modal visible even though JS closed it.
 */
html:not([data-modal-system="ready"]) body:not(.landing-template) .modal:target {
  display: grid !important;
  opacity: 1 !important;
  pointer-events: auto !important;
}

body:not(.landing-template) .modal-card {
  --modal-width: 780px;
  width: min(var(--modal-width), calc(100vw - (var(--modal-shell-padding) * 2)));
  margin: auto !important;
  align-self: center;
  max-height: min(940px, calc(var(--app-vh) - (var(--modal-shell-padding) * 2)));
  background: linear-gradient(180deg, #f8fafd 0%, #eef4fb 100%);
  border: 1px solid rgba(15, 23, 42, 0.16);
  border-radius: 20px;
  box-shadow: 0 24px 64px rgba(15, 23, 42, 0.32);
  overflow: auto;
  display: flex;
  flex-direction: column;
  transform: translateY(0);
  animation: modalCardIn 0.18s ease;
}

body:not(.landing-template) .modal-card.modal-wide {
  --modal-width: 1180px;
}

body:not(.landing-template) .modal-entity-detail .modal-card--entity-detail {
  --modal-width: 1180px;
  overflow: hidden;
  background: linear-gradient(180deg, #f9fbff 0%, #f1f5fb 100%);
  border: 1px solid rgba(203, 213, 225, 0.82);
  border-radius: 22px;
  box-shadow: 0 26px 44px -30px rgba(15, 23, 42, 0.88), 0 16px 24px -20px rgba(15, 23, 42, 0.2);
}

body:not(.landing-template) .modal-entity-detail .entity-detail-frame-wrap {
  padding: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(248, 251, 255, 0.62), rgba(241, 246, 253, 0.9));
}

body:not(.landing-template) .modal-entity-detail .modal-header--entity-detail {
  position: static;
  top: auto;
  z-index: auto;
  padding: 20px 24px 18px;
  border-bottom: 1px solid rgba(226, 232, 240, 0.95);
  background: #ffffff;
}

body:not(.landing-template) .modal-entity-detail .modal-close--entity-detail {
  width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid #dbe4f2;
  background: #f8fbff;
  color: #64748b;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  line-height: 1;
}

body:not(.landing-template) .modal-entity-detail .modal-close--entity-detail:hover,
body:not(.landing-template) .modal-entity-detail .modal-close--entity-detail:focus-visible {
  border-color: #c5d4ec;
  background: #eef4ff;
  color: #334155;
}

body:not(.landing-template) .modal-entity-detail .entity-detail-frame {
  min-height: clamp(460px, 74vh, 860px);
  border: 1px solid rgba(203, 213, 225, 0.84);
  border-radius: 18px;
  box-shadow: 0 16px 24px -24px rgba(15, 23, 42, 0.72), inset 0 0 0 1px rgba(226, 232, 240, 0.65);
}

body:not(.landing-template) .modal-card.modal-compact {
  --modal-width: 560px;
}

body:not(.landing-template) .modal-card > .modal-header {
  margin: 0;
}

body:not(.landing-template) .modal-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 22px;
  border-bottom: 1px solid rgba(15, 23, 42, 0.08);
  background: #ffffff;
  backdrop-filter: saturate(140%);
}

body:not(.landing-template) .modal-header h1,
body:not(.landing-template) .modal-header h2,
body:not(.landing-template) .modal-header h3,
body:not(.landing-template) .modal-header h4 {
  margin: 0;
}

body:not(.landing-template) .modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  appearance: none;
  -webkit-appearance: none;
  width: 34px;
  height: 34px;
  min-height: 34px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: rgba(15, 23, 42, 0.55);
  text-decoration: none;
  line-height: 1;
  font-size: 0; /* hide any text node leaks (legacy "X") */
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}

/* Replace any text content (legacy "X") with a clean SVG × icon. */
body:not(.landing-template) .modal-close::before {
  content: '';
  width: 16px;
  height: 16px;
  background-color: currentColor;
  mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round'><line x1='4' y1='4' x2='12' y2='12'/><line x1='12' y1='4' x2='4' y2='12'/></svg>");
  -webkit-mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.8' stroke-linecap='round'><line x1='4' y1='4' x2='12' y2='12'/><line x1='12' y1='4' x2='4' y2='12'/></svg>");
  mask-repeat: no-repeat;
  -webkit-mask-repeat: no-repeat;
  mask-position: center;
  -webkit-mask-position: center;
}

body:not(.landing-template) .modal-close:hover {
  background: rgba(15, 23, 42, 0.06);
  border-color: rgba(15, 23, 42, 0.08);
  color: rgba(15, 23, 42, 0.95);
}

body:not(.landing-template) .modal-close:focus-visible {
  outline: 0;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.95),
    0 0 0 4px rgba(37, 99, 235, 0.3);
}

body:not(.landing-template) .modal-card > .form,
body:not(.landing-template) .modal-card > .import-hub {
  flex: 0 0 auto;
  min-height: auto;
}

body:not(.landing-template) .modal .form,
body:not(.landing-template) .modal .import-hub {
  overflow: visible;
  padding: 20px 24px 24px;
  gap: 16px;
}

body:not(.landing-template) .modal .form + .form {
  border-top: 1px solid rgba(15, 23, 42, 0.18);
  margin-top: 6px;
  padding-top: 20px;
}

body:not(.landing-template) .modal-card > .note {
  margin: 0;
  padding: 12px 24px 0;
  background: transparent;
}

body:not(.landing-template) .modal .table-wrap {
  overflow-x: auto;
  overflow-y: visible;
}

body:not(.landing-template) .modal .form-actions,
body:not(.landing-template) .modal .step-actions {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid rgba(15, 23, 42, 0.12);
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

body:not(.landing-template) .stepper.stepper-ready {
  display: grid;
  gap: 18px;
}

body:not(.landing-template) .stepper-progress {
  list-style: none;
  margin: 0;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px;
  border-radius: 14px;
  border: 1px solid rgba(15, 23, 42, 0.12);
  background: rgba(248, 250, 252, 0.9);
}

body:not(.landing-template) .stepper-progress__item {
  min-width: 0;
}

body:not(.landing-template) .stepper-progress__button {
  width: 100%;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 12px;
  background: #ffffff;
  color: #0f172a;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-start;
  padding: 10px 12px;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
}

body:not(.landing-template) .stepper-progress__button:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.3);
  outline-offset: 2px;
}

body:not(.landing-template) .stepper-progress__index {
  width: 24px;
  height: 24px;
  border-radius: 999px;
  border: 1px solid rgba(15, 23, 42, 0.25);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  flex: 0 0 auto;
}

body:not(.landing-template) .stepper-progress__label {
  font-size: 0.85rem;
  font-weight: 700;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

body:not(.landing-template) .stepper-progress__item.is-active .stepper-progress__button {
  border-color: rgba(37, 99, 235, 0.62);
  background: rgba(37, 99, 235, 0.08);
  box-shadow: 0 8px 16px rgba(37, 99, 235, 0.16);
}

body:not(.landing-template) .stepper-progress__item.is-complete .stepper-progress__button {
  border-color: rgba(16, 185, 129, 0.5);
  background: rgba(16, 185, 129, 0.08);
}

body:not(.landing-template) .stepper-progress__item.is-complete .stepper-progress__index {
  border-color: rgba(16, 185, 129, 0.75);
  background: rgba(16, 185, 129, 0.18);
}

body:not(.landing-template) .stepper .step {
  display: none;
}

body:not(.landing-template) .stepper .step.is-active {
  display: grid;
  gap: 14px;
  animation: modalStepFade 0.18s ease;
}

@keyframes modalCardIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes modalStepFade {
  from {
    opacity: 0;
    transform: translateY(4px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 900px) {
  body:not(.landing-template) .stepper-progress {
    display: flex;
    overflow-x: auto;
    grid-template-columns: none;
  }

  body:not(.landing-template) .stepper-progress__item {
    flex: 0 0 min(230px, 70vw);
  }
}

@media (max-width: 720px) {
  body:not(.landing-template) .modal {
    place-items: center;
    --modal-shell-padding: 8px;
    padding: 8px;
  }

  body:not(.landing-template) .modal-card {
    width: min(100vw - 16px, 1000px);
    max-height: calc(var(--app-vh) - 16px);
    border-radius: 16px;
  }

  body:not(.landing-template) .modal-header {
    padding: 14px 16px;
  }

  body:not(.landing-template) .modal .form,
  body:not(.landing-template) .modal .import-hub {
    padding: 14px 16px 18px;
  }

  body:not(.landing-template) .modal-entity-detail .modal-card--entity-detail {
    width: calc(100vw - 16px);
    max-height: calc(var(--app-vh) - 16px);
  }

  body:not(.landing-template) .modal-entity-detail .modal-header--entity-detail {
    padding: 14px 14px 12px;
  }

  body:not(.landing-template) .modal-entity-detail .entity-detail-frame-wrap {
    padding: 8px;
  }

  body:not(.landing-template) .modal-entity-detail .entity-detail-frame {
    min-height: clamp(340px, 64vh, 680px);
    border-radius: 12px;
  }
}


/* Modal form controls and reset helpers (migrated from app.css) */
.modal .import-card__header h4 {
  margin: 0 0 6px;
  font-size: 1.15rem;
  font-weight: 800;
  color: #0f172a
}

.modal .import-card__header p {
  margin: 0;
  color: #475569
}

.modal label {
  display: grid;
  gap: 8px;
  font-weight: 700;
  color: #111827;
  font-size: 0.95rem
}

/* Reset component-labels inside modals so .modal label doesn't force grid */
.modal .widget-template-card {
  display: block;
  font-weight: inherit;
  font-size: inherit;
  color: inherit;
  gap: 0
}

.modal .toggle-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: inherit;
  color: inherit
}


.modal .quick-filter-toggle {
  display: inline-block;
  font-weight: inherit;
  font-size: inherit;
  gap: 0
}

.modal input[type="text"],
.modal input[type="url"],
.modal input[type="password"],
.modal input[type="email"],
.modal input[type="number"],
.modal select,
.modal textarea {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(15, 23, 42, 0.12);
  background: #ffffff;
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.05);
  transition: border-color 0.2s ease, box-shadow 0.2s ease
}

.modal input:focus,
.modal select:focus,
.modal textarea:focus {
  outline: none;
  border-color: rgba(37, 99, 235, 0.7);
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.18)
}

.modal .field-hint {
  font-size: 0.88rem;
  color: #4b5563
}

.modal .hint-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  background: rgba(37, 99, 235, 0.18);
  color: #1e40af;
  font-size: 0.75rem;
  font-weight: 800;
  margin-left: 6px
}

.modal .toggle {
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(15, 23, 42, 0.12);
  background: #ffffff;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06)
}

/*
 * Visually hide the native checkbox – only the .toggle-ui pill should render.
 * Old CSS left the checkbox visible alongside the toggle which made it look
 * like two controls. Keep accessibility: input is still focusable + clickable.
 */
.modal .toggle .toggle-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
  appearance: none;
  -webkit-appearance: none;
  opacity: 0;
}

.modal .toggle-text {
  font-weight: 600
}

.modal .toggle-ui {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.1);
  position: relative;
  transition: background 0.2s ease
}

.modal .toggle-ui::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.2);
  transition: transform 0.2s ease
}

.modal .toggle-input:checked+.toggle-ui {
  background: rgba(37, 99, 235, 0.8)
}

.modal .toggle-input:checked+.toggle-ui::after {
  transform: translateX(20px)
}

.modal .toggle-input:focus-visible+.toggle-ui {
  outline: 3px solid rgba(37, 99, 235, 0.35);
  outline-offset: 2px
}

@media (max-width:720px) {
  .modal {
    padding: 16px
  }

  .modal-card {
    margin-top: 24px;
    max-height: calc(100vh - 48px)
  }

  .modal-header {
    padding: 16px 18px
  }

  .modal .form {
    padding: 16px 18px 20px
  }

  .modal-card>.note {
    padding: 0 18px 14px
  }
}


/* Modal stepper fieldsets */
.modal .stepper fieldset {
  border: 0;
  padding: 0;
  margin: 0;
  min-width: 0
}


/* Modal import cards: remove inner frame */
.modal .import-card {
  border: 0;
  box-shadow: none;
  background: transparent;
  padding: 0;
  border-radius: 0
}
