:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --border: #e4e8f0;
  --text: #1d2330;
  --muted: #6b7588;
  --primary: #2563eb;
  --primary-hover: #1d4fd1;
  --primary-light: #e8efff;
  --accent: #0f9d58;
  --error: #c0392b;
  --error-bg: #fdecea;
  --shadow: 0 1px 2px rgba(20, 30, 60, .04), 0 4px 12px rgba(20, 30, 60, .04);
  --radius: 8px;
}

* { box-sizing: border-box; }

/* The `hidden` attribute defaults to display:none, but any class that sets a
 * `display:` value (.overlay, .user-area, etc.) overrides it. Force it. */
[hidden] { display: none !important; }

.hint {
  margin-top: 6px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  background: #f7f9fc;
}
.hint.hint-ok    { border-color: #a7f3d0; background: #ecfdf5; color: #065f46; }
.hint.hint-warn  { border-color: #fde68a; background: #fffbeb; color: #854d0e; }

.suggestion-list { margin: 6px 0 0; padding-left: 18px; }
.suggestion-list li { margin: 2px 0; }
.suggestion-list .swap-btn {
  margin-left: 8px;
  padding: 1px 8px;
  font-size: 11.5px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
}
.suggestion-list .swap-btn:hover { background: #f0f3f9; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 24px;
  gap: 16px;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.user-area {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
}

/* ---------- Stepper ---------- */

.stepper {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.stepper ol {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.stepper li {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-right: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  position: relative;
}

.stepper li:last-child { border-right: none; }

.stepper li .num {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 12px;
}

.stepper li.active {
  color: var(--primary);
  background: var(--primary-light);
}

.stepper li.active .num {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.stepper li.done {
  color: var(--accent);
}

.stepper li.done .num {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* ---------- Panels ---------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px 28px;
}

.panel h2 {
  margin: 0 0 4px;
  font-size: 18px;
}

.panel h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 540px;
  margin-top: 16px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.form label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-weight: 400;
}

input[type=text],
input[type=email],
input[type=password],
input[type=search],
select,
textarea {
  padding: 8px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  font-family: inherit;
  color: var(--text);
  outline: none;
  transition: border-color .12s, box-shadow .12s;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}

button {
  font-family: inherit;
  font-size: 14px;
  border-radius: 6px;
  padding: 8px 16px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
}

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

button.primary {
  background: var(--primary);
  color: white;
}

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

button.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

button.ghost:hover:not(:disabled) {
  background: #f0f3f9;
}

button.danger {
  color: var(--error);
  border-color: #f5b9b3;
}

button.danger:hover:not(:disabled) {
  background: var(--error-bg);
}

.actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.actions button.ghost { margin-right: auto; }

.error {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid #f5b9b3;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
}

.notice {
  background: #eaf7ef;
  color: #166534;
  border: 1px solid #bbebcc;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
}

/* ---------- Tables ---------- */

.table-wrap {
  margin-top: 8px;
  overflow: auto;
  max-height: 280px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
}

.table-wrap.small { max-height: 220px; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

table th, table td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  vertical-align: top;
}

table th {
  background: #fafbfd;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}

table td {
  max-width: 360px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- File drop ---------- */

.file-drop {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 28px;
  text-align: center;
  background: #fafbfd;
  cursor: pointer;
  display: block;
  font-size: 14px;
  color: var(--muted);
  transition: border-color .12s, background .12s;
}

.file-drop:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.file-drop input { display: none; }

.info-row {
  margin: 12px 0 4px;
  font-size: 13px;
}

/* ---------- Grid for map step ---------- */

.grid-2 {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 24px;
  margin-top: 16px;
  align-items: start;
}

.grid-2 > div { min-width: 0; }

#panel-map label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  font-weight: 500;
}

#panel-map select {
  width: 100%;
  min-width: 0;
}

#panel-map .table-wrap.small table th,
#panel-map .table-wrap.small table td {
  white-space: normal;
  overflow-wrap: anywhere;
}

@media (max-width: 880px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* ---------- Toolbar (columns step) ---------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 16px 0 8px;
}

.toolbar input[type=search] { flex: 1; }

/* ---------- Cards on run panel ---------- */

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 16px;
  background: #fafbfd;
}

.card .form { margin-top: 8px; }

.result {
  margin-top: 8px;
  padding: 10px 12px;
  background: #ecfdf5;
  border: 1px solid #a7f3d0;
  border-radius: 6px;
  font-size: 13px;
  color: #065f46;
}

.result.error {
  background: var(--error-bg);
  border-color: #f5b9b3;
  color: var(--error);
}

.result a { color: inherit; font-weight: 600; }

.detail-list {
  margin: 6px 0 0 18px;
  padding: 0;
  color: var(--muted);
  font-size: 12px;
}

.detail-list li {
  margin: 2px 0;
  overflow-wrap: anywhere;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  background: #eef1f7;
  padding: 1px 5px;
  border-radius: 3px;
}

/* ---------- Loading overlay ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 60, .35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.spinner-box {
  background: white;
  padding: 20px 28px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  max-width: min(960px, calc(100vw - 48px));
  gap: 14px;
  box-shadow: var(--shadow);
  font-size: 14px;
}

#loading-text {
  min-width: 0;
}

.spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- Columns table specifics ---------- */

#columns-table td input[type=text] {
  width: 100%;
  padding: 4px 8px;
  font-size: 12.5px;
}

#columns-table td.field-path {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--text);
}

#columns-table td.sample {
  color: var(--muted);
}

/* ---------- App-level auth gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eef2f9 0%, #f7f9fc 100%);
  padding: 24px;
  z-index: 500;
}

.gate-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(20, 30, 60, .12);
  padding: 32px 36px;
  width: 100%;
  max-width: 420px;
}

.gate-card h1 {
  margin: 0 0 4px;
  font-size: 22px;
}

.gate-card .form { margin-top: 18px; }

.gate-card p.muted.small { margin-top: 18px; }

/* Intro splash. Reuses the gate-card shell but is much wider because the
 * card actually has content to read, not just a single login form. */
.intro-card {
  max-width: 640px;
  padding: 36px 40px;
}

.intro-card h3 {
  margin: 22px 0 6px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

.intro-list {
  margin: 4px 0 0;
  padding-left: 20px;
}

.intro-list li { margin: 6px 0; }

.intro-callout {
  margin-top: 22px;
  padding: 12px 14px;
  border-left: 3px solid #b91c1c;
  background: #fef2f2;
  color: #7f1d1d;
  border-radius: 4px;
  font-size: 13px;
}

.intro-card .actions { margin-top: 22px; }
.intro-card .actions button { margin-left: auto; }

/* ---------- Top bar additions ---------- */

.app-user-info {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 13px;
}

#app-user-name { font-weight: 600; }

.role-pill {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .02em;
}

.role-pill.role-admin { background: #eef2ff; color: #4338ca; }
.role-pill.role-user  { background: #ecfdf5; color: #047857; }

/* ---------- PHI banner / row treatment ---------- */

#phi-block-banner {
  margin: 12px 0 0;
  border-color: #fecaca;
  background: #fef2f2;
  color: #b91c1c;
}

#columns-table tr.phi-row {
  background: #fef2f2;
  color: #b91c1c;
}
#columns-table tr.phi-row td.field-path { color: #b91c1c; font-weight: 600; }
#columns-table tr.phi-row td.sample { color: #dc2626; font-style: italic; }
#columns-table tr.phi-row input[type=text]:disabled {
  background: #fef2f2;
  color: #b91c1c;
  border-color: #fecaca;
}

.phi-pill {
  display: inline-block;
  margin-left: 6px;
  padding: 0 6px;
  border-radius: 999px;
  background: #fee2e2;
  color: #b91c1c;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.toolbar-checkbox {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: 6px;
  font-weight: 400;
  color: var(--muted);
  font-size: 12.5px;
}

/* ---------- Modal (admin panel) ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 60, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1100;
  padding: 24px;
}

.modal-card {
  background: var(--panel);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(20, 30, 60, .18);
  width: 100%;
  max-width: 900px;
  max-height: calc(100vh - 48px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

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

.modal-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 14px 0;
  border-bottom: 1px solid var(--border);
  background: #fafbfd;
}

.modal-tabs .tab {
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  color: var(--muted);
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}

.modal-tabs .tab.active {
  background: white;
  border-color: var(--border);
  color: var(--primary);
  font-weight: 600;
}

.modal-pane {
  padding: 16px 20px 20px;
  overflow: auto;
}

.modal-pane h3 {
  margin: 14px 0 8px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}

.modal-pane h3:first-child { margin-top: 4px; }

.form.inline {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  max-width: none;
}

.form.inline input, .form.inline select { min-width: 0; }
.form.inline input[type=text] { width: 180px; }
.form.inline input[type=password] { width: 180px; }
.form.inline select { width: 110px; }

.actions-cell {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.actions-cell button {
  font-size: 12px;
  padding: 4px 10px;
}

#admin-users-table .role-select {
  width: 90px;
  padding: 4px 6px;
  font-size: 12.5px;
}

/* ---------- Audit log table ---------- */

.audit-wrap { max-height: 60vh; }

#admin-audit-table td { vertical-align: top; }

#admin-audit-table th,
#admin-audit-table td { white-space: normal; }

#admin-audit-table .audit-summary {
  font-size: 12.5px;
  color: var(--text);
  word-break: break-word;
  min-width: 220px;
}

/* Failed-action rows get a soft red tint so the eye lands on them first. */
#admin-audit-table tr.audit-fail { background: #fef2f2; }
#admin-audit-table tr.audit-fail .audit-summary { color: #b91c1c; }

.status-pill {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}

.status-pill.status-ok { background: #ecfdf5; color: #047857; }
.status-pill.status-fail { background: #fef2f2; color: #b91c1c; }
