:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --surface-strong: #eef3f7;
  --text: #18212b;
  --muted: #5f6f82;
  --line: #d5dde7;
  --line-strong: #c3ceda;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --action: #255c99;
  --action-strong: #1f4e82;
  --focus: #2563eb;
  --shadow: 0 10px 30px rgba(22, 34, 51, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
  font-size: 14px;
}

.app-header {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--line-strong);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
}

.app-header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
}

.app-header p {
  margin: 4px 0 0;
  color: var(--muted);
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.auth-status {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  max-width: 420px;
  padding: 4px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #2c4057;
  font-size: 12px;
  font-weight: 700;
}

.auth-status[data-state="valid"] {
  border-color: #9cc8bd;
  background: #e7f5f1;
  color: #0f5d50;
}

.auth-status[data-state="warning"] {
  border-color: #e3c779;
  background: #fff7db;
  color: #775b12;
}

.auth-status[data-state="invalid"] {
  border-color: #e6aaaa;
  background: #fff0f0;
  color: #8a2f2f;
}

main {
  min-height: calc(100vh - 76px);
}

.directory-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-height: calc(100vh - 76px);
}

.directory-shell.contact-panel-open {
  grid-template-columns: minmax(0, 1fr) 390px;
}

.search-pane {
  min-width: 0;
  padding: 20px 24px 24px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 360px) minmax(0, 1fr) auto;
  grid-template-areas: "search filters actions";
  gap: 14px;
  align-items: start;
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.search-box,
.filter-control {
  display: grid;
  gap: 5px;
  align-self: start;
  color: #43566c;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.search-box {
  grid-area: search;
}

input,
select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  padding: 0 10px;
}

input:focus,
select:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.filters {
  grid-area: filters;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  align-self: start;
}

.toolbar-actions {
  grid-area: actions;
  display: flex;
  gap: 8px;
  align-self: start;
  padding-top: 19px;
}

.primary-button,
.secondary-button,
.danger-button,
.contact-close {
  height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #2c4057;
  font: inherit;
  font-weight: 700;
  padding: 0 14px;
  cursor: pointer;
}

.primary-button {
  border-color: var(--action);
  background: var(--action);
  color: #ffffff;
}

.primary-button:hover {
  background: var(--action-strong);
}

.danger-button {
  border-color: #a83a3a;
  background: #8f2d2d;
  color: #ffffff;
}

.danger-button:hover {
  background: #742424;
}

.primary-button:disabled,
.danger-button:disabled {
  border-color: var(--line-strong);
  background: #eef2f5;
  color: #93a1b1;
  cursor: default;
}

.secondary-button:hover,
.contact-close:hover {
  background: #e4ebf2;
}

.secondary-button:disabled {
  color: #93a1b1;
  cursor: default;
  background: #eef2f5;
}

.secondary-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #2c4057;
  font-weight: 700;
  text-decoration: none;
}

.secondary-link:hover {
  background: #e4ebf2;
}

.secondary-link[hidden] {
  display: none;
}

.admin-shell {
  padding: 20px 24px 24px;
}

.admin-panel {
  max-width: 760px;
  padding: 18px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.admin-workspace {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 16px;
  margin-top: 16px;
  max-width: 1180px;
}

.admin-panel-wide {
  max-width: 1180px;
}

.setup-state-panel {
  max-width: 1180px;
  margin-top: 16px;
  border-color: #d7c184;
  background: #fffaf0;
  box-shadow: none;
}

.setup-state-panel[hidden] {
  display: none;
}

.setup-steps {
  margin: 10px 0 0 18px;
  padding: 0;
  color: #40546a;
}

.setup-next-action {
  margin: 10px 0 0;
  color: #263f59;
  font-weight: 700;
}

.setup-steps li {
  margin: 4px 0;
}

.setup-step-complete {
  color: #0f5d50;
}

.setup-step-needed {
  color: #715b18;
  font-weight: 700;
}

.admin-overview {
  display: grid;
  gap: 16px;
  margin-top: 16px;
}

.admin-overview[hidden] {
  display: none;
}

.admin-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.admin-panel-header h2 {
  margin: 0;
  font-size: 20px;
}

.admin-panel-header p {
  margin: 5px 0 0;
  color: var(--muted);
}

.admin-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.admin-section-header h2 {
  margin: 0;
  font-size: 18px;
}

.admin-section-header p {
  margin: 5px 0 0;
  color: var(--muted);
}

.admin-button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.admin-summary-list {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  gap: 8px 12px;
  margin: 0 0 14px;
}

.admin-summary-list dt {
  color: #4a5f76;
  font-weight: 700;
}

.admin-summary-list dd {
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
}

.admin-table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.admin-table-auto {
  table-layout: auto;
  min-width: 720px;
}

.admin-field-table {
  table-layout: fixed;
  min-width: 1120px;
}

.admin-mapping-table {
  table-layout: fixed;
  min-width: 920px;
}

.admin-rule-table {
  table-layout: fixed;
  min-width: 720px;
}

.admin-table th,
.admin-table td {
  min-height: 40px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.admin-field-table th,
.admin-field-table td {
  padding: 8px 10px;
  vertical-align: middle;
}

.admin-field-table th:nth-child(n+4):nth-child(-n+10),
.admin-field-table td:nth-child(n+4):nth-child(-n+10) {
  width: 76px;
  text-align: center;
}

.admin-field-table th:nth-child(1),
.admin-field-table td:nth-child(1) {
  width: 120px;
}

.admin-field-table th:nth-child(2),
.admin-field-table td:nth-child(2) {
  width: 160px;
}

.admin-field-table th:nth-child(3),
.admin-field-table td:nth-child(3) {
  width: 104px;
}

.admin-field-table th:nth-child(11),
.admin-field-table td:nth-child(11) {
  width: 116px;
}

.admin-field-table th:nth-child(12),
.admin-field-table td:nth-child(12) {
  width: 48px;
  text-align: center;
}

.admin-mapping-table th:nth-child(5),
.admin-mapping-table td:nth-child(5),
.admin-rule-table th:nth-child(4),
.admin-rule-table td:nth-child(4) {
  width: 48px;
  text-align: center;
}

.admin-table th {
  background: var(--surface-strong);
  color: #40546a;
  font-size: 12px;
  text-transform: uppercase;
}

.admin-table tbody tr:last-child td {
  border-bottom: 0;
}

.admin-field-table input[type="text"],
.admin-field-table input[type="number"] {
  min-width: 0;
  height: 34px;
}

.field-toggle-cell {
  text-align: center;
}

.field-toggle {
  appearance: none;
  -webkit-appearance: none;
  display: inline-grid;
  place-content: center;
  width: 18px;
  min-width: 18px;
  height: 18px;
  margin: 0;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  background: var(--surface);
  color: #ffffff;
  cursor: pointer;
  vertical-align: middle;
}

.field-toggle::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 1px;
  background: currentColor;
  transform: scale(0);
}

.field-toggle:checked {
  border-color: var(--action);
  background: var(--action);
}

.field-toggle:checked::before {
  transform: scale(1);
}

.field-toggle:focus {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.field-editor-add {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(160px, 220px) minmax(160px, 220px) auto;
  gap: 12px;
  align-items: end;
  margin: 0 0 12px;
}

.mapping-editor-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  gap: 12px;
  margin: 0 0 12px;
}

.mapping-editor-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  margin: 0 0 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--surface-muted);
}

.mapping-add-row {
  grid-template-columns: repeat(4, minmax(150px, 1fr)) auto;
}

.transform-picker {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.transform-picker legend {
  display: block;
  margin: 0 0 6px;
  color: #40546a;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.transform-options {
  display: flex;
  min-height: 38px;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
}

.transform-options-table {
  min-height: 0;
}

.transform-option {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #263f59;
  font-size: 12px;
  white-space: nowrap;
}

.transform-option .field-toggle {
  width: 16px;
  min-width: 16px;
  height: 16px;
}

.transform-option .field-toggle::before {
  width: 7px;
  height: 7px;
}

.transform-advanced {
  margin-top: 6px;
  color: #637286;
  font-size: 12px;
}

.mapping-rules {
  margin-top: 18px;
}

.mapping-subheader {
  margin-bottom: 10px;
}

.mapping-subheader h3 {
  margin: 0;
  color: #344a61;
  font-size: 15px;
}

.mapping-rule-add-row {
  grid-template-columns: minmax(150px, 1fr) minmax(150px, 190px) minmax(180px, 1fr) auto;
}

.webex-source-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(88px, 120px);
  gap: 12px;
  align-items: end;
  margin-top: 16px;
}

.webex-source-actions {
  grid-column: 1 / -1;
  justify-content: flex-start;
}

.source-status {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 7px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #40546a;
  font-size: 12px;
  font-weight: 700;
}

.source-status-mapped {
  border-color: #9cc8bd;
  background: #e7f5f1;
  color: #0f5d50;
}

.source-status-unmapped {
  border-color: #d7c184;
  background: #fff7db;
  color: #715b18;
}

.source-status-stale {
  border-color: #e6aaaa;
  background: #fff0f0;
  color: #8a2f2f;
}

.icon-button {
  width: 30px;
  height: 30px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #2c4057;
  font-weight: 700;
  cursor: pointer;
}

.icon-button:hover {
  background: #e4ebf2;
}

.upload-form {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) auto;
  gap: 12px;
  align-items: end;
  margin-top: 16px;
}

.upload-field {
  display: grid;
  gap: 5px;
  color: #43566c;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.upload-field input[type="file"] {
  height: auto;
  min-height: 38px;
  padding: 8px 10px;
}

.upload-status {
  min-height: 24px;
  margin-top: 14px;
  color: var(--muted);
}

.upload-status[data-state="info"] {
  color: #4a5f76;
}

.upload-status[data-state="error"] {
  color: #8a2f2f;
}

.upload-status[data-state="success"] {
  color: #0f5d50;
}

.admin-token-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
}

.admin-token {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 9px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface-strong);
  color: #2c4057;
  font-weight: 700;
}

.field-settings {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.field-settings[hidden] {
  display: none;
}

.field-settings h2 {
  margin: 0 0 10px;
  color: #344a61;
  font-size: 13px;
  text-transform: uppercase;
}

.field-option-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px 12px;
}

.field-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  color: #2c4057;
}

.field-option input {
  width: 16px;
  height: 16px;
}

.results-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.results-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.results-table th,
.results-table td {
  height: 46px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.results-table th {
  position: sticky;
  top: 0;
  background: var(--surface-strong);
  color: #40546a;
  font-size: 12px;
  text-transform: uppercase;
  user-select: none;
}

.results-table th[data-sortable="true"] {
  cursor: pointer;
}

.results-table tr {
  cursor: pointer;
}

.results-table tbody tr:hover {
  background: #eef7f6;
}

.results-table tbody tr.selected {
  background: #e1f2f0;
  box-shadow: inset 3px 0 0 var(--accent);
}

.unauthorized-state {
  display: grid;
  align-content: center;
  justify-items: center;
  min-height: calc(100vh - 160px);
  padding: 32px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}

.unauthorized-state[hidden] {
  display: none;
}

.unauthorized-state h2 {
  margin: 0 0 8px;
  color: #8a2f2f;
  font-size: 24px;
}

.unauthorized-state p {
  max-width: 520px;
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.contact-panel {
  display: none;
  border-left: 1px solid var(--line);
  background: var(--surface);
  padding: 22px;
  overflow: auto;
  box-shadow: -10px 0 26px rgba(22, 34, 51, 0.06);
}

.contact-panel-open .contact-panel {
  display: block;
}

.empty-panel {
  color: var(--muted);
}

.contact-card-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
}

.contact-card h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.contact-card header p {
  margin: 6px 0 0;
  color: var(--muted);
}

dl {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 10px 12px;
  margin: 16px 0 0;
}

dt {
  color: #4a5f76;
  font-weight: 700;
}

dd {
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
}

.contact-field-value {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
}

.contact-inline-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--action);
  text-decoration: none;
  vertical-align: middle;
}

.contact-inline-action:hover {
  background: #e3edf7;
  border-color: var(--line-strong);
  color: var(--action-strong);
}

.contact-inline-action svg {
  display: block;
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.temporary-protocol-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.temporary-protocol-actions a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 76px;
  height: 34px;
  border-radius: 4px;
  background: var(--action);
  color: #ffffff;
  font-weight: 700;
  text-decoration: none;
}

.temporary-protocol-actions a:hover {
  background: var(--action-strong);
}

@media (max-width: 860px) {
  .directory-shell {
    grid-template-columns: 1fr;
  }

  .directory-shell.contact-panel-open {
    grid-template-columns: 1fr;
  }

  .contact-panel {
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .toolbar {
    grid-template-columns: 1fr;
    grid-template-areas:
      "search"
      "filters"
      "actions";
  }

  .toolbar-actions {
    width: 100%;
    padding-top: 0;
  }

  .secondary-button {
    width: 100%;
  }

  .upload-form {
    grid-template-columns: 1fr;
  }

  .field-editor-add {
    grid-template-columns: 1fr;
  }

  .mapping-editor-grid,
  .mapping-add-row,
  .mapping-rule-add-row,
  .webex-source-form {
    grid-template-columns: 1fr;
  }

  .admin-button-row {
    width: 100%;
    justify-content: stretch;
  }

  .primary-button {
    width: 100%;
  }

  .field-settings {
    grid-template-columns: 1fr;
  }

  .app-header {
    align-items: flex-start;
    flex-direction: column;
    padding: 14px 18px;
  }

  .auth-status {
    max-width: 100%;
  }

  .header-actions {
    width: 100%;
    justify-content: flex-start;
  }
}
