.mg-dialog {
  --mg-text: #17212b;
  --mg-muted: #556372;
  --mg-border: #d8dee6;
  --mg-surface: #f5f7fa;
  --mg-ink: #25374a;
  box-sizing: border-box;
  width: min(30rem, calc(100% - 2rem));
  max-width: none;
  max-height: calc(100% - 2rem);
  padding: 0;
  overflow: auto;
  border: 1px solid var(--mg-border);
  border-radius: 1.25rem;
  background: #fff;
  color: var(--mg-text);
  box-shadow: 0 18px 80px rgb(23 33 43 / 18%);
}

.mg-dialog-wide {
  width: min(57rem, calc(100% - 2rem));
}

.mg-dialog::backdrop {
  background: rgb(22 33 45 / 42%);
}

.mg-dialog *,
.mg-dialog *::before,
.mg-dialog *::after {
  box-sizing: border-box;
}

.mg-dialog [hidden] {
  display: none !important;
}

.mg-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 4.25rem;
  padding: 0.8rem 1.5rem;
  border-bottom: 1px solid var(--mg-border);
  background: #fff;
}

.mg-header h2 {
  margin: 0;
  font-size: 1.2rem;
  line-height: 1.35;
}

.mg-header h2:focus {
  outline: none;
}

.mg-close {
  display: grid;
  width: 2.25rem;
  height: 2.25rem;
  flex: none;
  place-items: center;
  padding: 0 0 0.12rem;
  border: 0;
  border-radius: 50%;
  background: var(--mg-surface);
  color: var(--mg-ink);
  font: inherit;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.mg-close:hover {
  background: #e9eef3;
}

.mg-body {
  padding: 1.5rem;
}

.mg-form,
.mg-field {
  display: grid;
  min-width: 0;
  gap: 0.85rem;
}

.mg-field {
  gap: 0.4rem;
  font-size: 0.9rem;
  font-weight: 600;
}

.mg-pack-picker {
  display: grid;
  min-width: 0;
  gap: 0.55rem;
  margin: 0;
  padding: 0.75rem;
  border: 1px solid var(--mg-border);
  border-radius: 0.65rem;
}

.mg-pack-picker legend { padding: 0 0.35rem; }

.mg-pack-query, .mg-pack-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.mg-pack-query input { flex: 1; min-width: 0; }

.mg-dialog input:not([type="file"]),
.mg-dialog select {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 2.625rem;
  margin: 0;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--mg-border);
  border-radius: 0.6rem;
  outline-offset: 2px;
  background: #fff;
  color: var(--mg-text);
  box-shadow: none;
  font: inherit;
  font-size: 1rem;
  font-weight: 400;
}

.mg-dialog select:disabled {
  background: var(--mg-surface);
  color: var(--mg-muted);
}

.mg-dialog :is(button, input, select):focus-visible {
  outline: 2px solid #526b85;
  outline-offset: 2px;
}

.mg-actions,
.mg-record-actions,
.mg-review-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
}

.mg-button {
  display: inline-flex;
  min-height: 2.625rem;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--mg-border);
  border-radius: 0.6rem;
  background: #fff;
  color: var(--mg-text);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.4;
  cursor: pointer;
}

.mg-button:hover:not(:disabled) {
  background: var(--mg-surface);
}

.mg-primary {
  border-color: var(--mg-ink);
  background: var(--mg-ink);
  color: #fff;
}

.mg-primary:hover:not(:disabled) {
  background: #354e66;
}

.mg-button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.mg-small {
  min-height: 2.25rem;
  padding: 0.35rem 0.65rem;
  font-size: 0.84rem;
}

.mg-danger {
  color: #962c2c;
}

.mg-preview {
  margin-top: 1.35rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--mg-border);
}

.mg-preview-heading {
  margin: 0 0 0.65rem;
  font-size: 0.9rem;
  line-height: 1.4;
}

.mg-preview-roles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(6rem, 1fr));
  gap: 0.5rem;
}

.mg-preview-roles [aria-current="true"] {
  border-color: var(--mg-ink);
  background: var(--mg-surface);
}

.mg-preview .mg-status:not(:empty) {
  margin-top: 0.55rem;
}

.mg-message,
.mg-note,
.mg-status,
.mg-empty,
.mg-account-phone {
  margin: 0;
  overflow-wrap: anywhere;
  line-height: 1.5;
}

.mg-note,
.mg-status,
.mg-empty,
.mg-account-phone {
  color: var(--mg-muted);
  font-size: 0.87rem;
}

.mg-message,
.mg-account {
  margin-bottom: 0.65rem;
}

.mg-status:empty {
  display: none;
}

.mg-status.mg-error,
.mg-file-status {
  color: #9d2727;
}

.mg-account-name {
  font-size: 1.1rem;
}

.mg-badge {
  display: inline-block;
  margin-inline-start: 0.55rem;
  padding: 0.15rem 0.45rem;
  border-radius: 0.35rem;
  background: #edf2f6;
  color: #415468;
  font-size: 0.77rem;
  font-weight: 600;
}

.mg-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 1.15rem;
  padding-bottom: 0.7rem;
  border-bottom: 1px solid var(--mg-border);
}

.mg-tab {
  min-height: 2.4rem;
  padding: 0.45rem 0.85rem;
  border: 1px solid transparent;
  border-radius: 0.55rem;
  background: #fff;
  color: var(--mg-muted);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.mg-tab:hover,
.mg-tab[aria-pressed="true"] {
  border-color: var(--mg-border);
  background: var(--mg-surface);
  color: var(--mg-ink);
}

.mg-record-query {
  display: grid;
  grid-template-columns: minmax(6.5rem, 0.35fr) minmax(0, 1fr) auto;
  align-items: end;
  gap: 0.65rem;
  margin-bottom: 1rem;
}

.mg-record-list,
.mg-review-list {
  margin-top: 0.55rem;
}

.mg-record,
.mg-review {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.65rem 1rem;
  min-width: 0;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--mg-border);
}

.mg-record-name,
.mg-review-info strong {
  overflow-wrap: anywhere;
  font-size: 0.93rem;
  line-height: 1.5;
}

.mg-record-content .mg-note,
.mg-review-info .mg-note {
  margin-top: 0.25rem;
}

.mg-review-actions select,
.mg-dialog .mg-review-actions .mg-phone-binding {
  width: auto;
  min-height: 2.25rem;
  padding: 0.3rem 0.35rem;
  font-size: 0.85rem;
}

.mg-dialog .mg-review-actions .mg-phone-binding {
  width: 8rem;
}

.mg-more {
  margin-top: 1rem;
}

.mg-section-title {
  margin: 0 0 0.85rem;
  font-size: 1rem;
}

.mg-library-field {
  max-width: 16rem;
}

.mg-fields,
.mg-attachments {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
}

.mg-attachments:empty {
  display: none;
}

.mg-drop {
  position: relative;
  display: grid;
  min-height: 5.3rem;
  align-content: center;
  gap: 0.35rem;
  padding: 0.75rem 0.9rem;
  overflow: hidden;
  border: 1px dashed #a7b5c3;
  border-radius: 0.7rem;
  background: #fafbfd;
  font-size: 0.88rem;
  cursor: pointer;
}

.mg-drop span {
  color: var(--mg-muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.mg-drop:hover,
.mg-drop.mg-dragging {
  border-color: #607990;
  background: #edf2f6;
}

.mg-drop:focus-within {
  outline: 2px solid #526b85;
  outline-offset: 2px;
}

.mg-file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  opacity: 0;
}

.mg-selected-files,
.mg-current-files {
  margin: 0;
  padding: 0;
  list-style: none;
}

.mg-selected-files li,
.mg-current-files li {
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: 0.3rem;
  padding-top: 0.35rem;
  overflow-wrap: anywhere;
  color: var(--mg-muted);
  font-size: 0.8rem;
}

.mg-current-files {
  grid-column: 1 / -1;
}

.mg-file-remove {
  flex: none;
  padding: 0.2rem 0.35rem;
  border: 0;
  border-radius: 0.25rem;
  background: var(--mg-surface);
  color: var(--mg-ink);
  font: inherit;
  cursor: pointer;
}

.mg-file-status {
  margin: 0.35rem 0 0;
  font-size: 0.78rem;
}

.mg-file-status:empty {
  display: none;
}

.mg-excel-upload {
  padding: 1rem;
  border: 1px solid var(--mg-border);
  border-radius: 0.65rem;
  background: var(--mg-surface);
}

.mg-excel-form {
  display: grid;
  gap: 0.65rem;
  margin-top: 0.85rem;
}

.mg-excel-form input[type="file"] {
  width: min(100%, 28rem);
  padding: 0.55rem;
  border: 1px solid var(--mg-border);
  border-radius: 0.55rem;
  background: #fff;
  font: inherit;
  font-weight: 400;
}

.mg-excel-section {
  margin-top: 1.35rem;
}

.mg-excel-section .mg-empty,
.mg-excel-upload + .mg-status {
  margin-top: 0.7rem;
}

.mg-excel-row {
  align-items: start;
}

.mg-excel-row a,
.mg-excel-form a {
  text-decoration: none;
}

.mg-excel-badge {
  margin: 0.4rem 0 0;
}

.mg-excel-summary {
  margin-top: 0.45rem;
  color: var(--mg-muted);
  font-size: 0.86rem;
}

.mg-excel-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 0.55rem;
  margin-bottom: 0.35rem;
}

.mg-excel-metrics span {
  padding: 0.15rem 0.4rem;
  border-radius: 0.3rem;
  background: var(--mg-surface);
}

.mg-excel-summary summary {
  width: fit-content;
  min-height: 1.65rem;
  cursor: pointer;
}

.mg-excel-changes {
  max-height: 14rem;
  margin: 0.25rem 0 0.5rem;
  padding-left: 1.3rem;
  overflow: auto;
  overflow-wrap: anywhere;
}

.mg-excel-values {
  margin: 0.2rem 0 0.55rem;
  padding-left: 1.15rem;
}

.mg-excel-error {
  color: #9d2727;
}

.mg-excel-version-heading {
  margin: 0.9rem 0 0.45rem;
  color: var(--mg-muted);
  font-size: 0.9rem;
}

@media (max-width: 39rem) {
  .mg-dialog {
    width: calc(100% - 1rem);
    max-height: calc(100% - 1rem);
    border-radius: 0.95rem;
  }

  .mg-header,
  .mg-body {
    padding-inline: 1rem;
  }

  .mg-tabs {
    gap: 0.3rem;
  }

  .mg-tab {
    padding-inline: 0.5rem;
  }

  .mg-record,
  .mg-review {
    grid-template-columns: minmax(0, 1fr);
  }

  .mg-attachments {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 23rem) {
  .mg-fields {
    grid-template-columns: minmax(0, 1fr);
  }

  .mg-record-query {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .mg-record-query .mg-field:first-child {
    grid-column: 1 / -1;
  }
}
