:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bg: #f9fafb;
  --card: #ffffff;
  --radius: 8px;

  --green: #15803d;  --green-bg: #dcfce7;
  --blue: #1d4ed8;   --blue-bg: #dbeafe;
  --amber: #b45309;  --amber-bg: #fef3c7;
  --red: #b91c1c;    --red-bg: #fee2e2;
  --purple: #7e22ce; --purple-bg: #f3e8ff;
  --grey-bg: #f3f4f6;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* Layout */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0 16px; height: 56px;
  background: var(--card); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); }
.brand:hover { text-decoration: none; }
.topnav { display: flex; align-items: center; gap: 1rem; }

.container { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }

.stack > * + * { margin-top: 1rem; }
.row { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; }
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.empty-state { text-align: center; padding: 48px 20px; max-width: 520px; margin: 40px auto; }

/* Tabs */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1.25rem; overflow-x: auto; }
.tabs a {
  padding: .6rem .9rem; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* Forms */
.field { display: block; }
.field > span { display: block; font-weight: 600; margin-bottom: .3rem; }
.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; margin-top: .25rem; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], select, textarea {
  width: 100%;
  padding: .55rem .7rem;
  font: inherit;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1rem;
  font: inherit; font-weight: 600;
  color: var(--text); background: #fff;
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer;
}
.btn:hover { background: var(--grey-bg); text-decoration: none; }
.btn-primary { color: #fff; background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: var(--red); border-color: #fecaca; }
.btn-danger:hover { background: var(--red-bg); }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Alerts */
.alert { padding: .7rem .9rem; border-radius: var(--radius); border: 1px solid transparent; }
.alert-error { color: var(--red); background: var(--red-bg); border-color: #fecaca; }
.alert-success { color: var(--green); background: var(--green-bg); border-color: #bbf7d0; }
.alert-info { color: var(--purple); background: var(--purple-bg); border-color: #e9d5ff; }

/* Badges */
.badge {
  display: inline-block; padding: .1rem .5rem;
  font-size: 12px; font-weight: 600;
  border-radius: 999px; background: var(--grey-bg); color: var(--muted);
}
.badge-completed { background: var(--green-bg); color: var(--green); }
.badge-running, .badge-queued { background: var(--blue-bg); color: var(--blue); }
.badge-waiting { background: var(--amber-bg); color: var(--amber); }
.badge-failed { background: var(--red-bg); color: var(--red); }
.badge-dry { background: var(--purple-bg); color: var(--purple); }
.badge-cancelled { background: var(--grey-bg); color: var(--muted); }

/* Tables: scroll inside their wrapper, never the page */
.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .6rem .8rem; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--bg); }
tr:last-child td { border-bottom: 0; }

/* Auth */
body.bare { background: var(--bg); }
.auth-wrap { min-height: calc(100vh - 48px); display: flex; align-items: center; justify-content: center; }
.auth-card { width: 100%; max-width: 380px; }
.auth-title { color: var(--primary); }

/* Top nav */
.topnav a { color: var(--muted); font-weight: 500; }
.topnav a.active, .topnav a:hover { color: var(--text); text-decoration: none; }
.topnav-user { display: none; }
@media (min-width: 640px) { .topnav-user { display: inline; } }
form.inline { display: inline; margin: 0; }
.link-button { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.link-button:hover { text-decoration: underline; }

/* Page structure */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; }
.section { margin-top: 1.5rem; }
.flash { margin-bottom: 1rem; }
.danger-zone { border-color: #fecaca; }

/* Client cards */
.client-card { display: block; color: var(--text); transition: border-color .15s; }
.client-card:hover { border-color: var(--primary); text-decoration: none; }
.client-card h2 { margin-bottom: .15rem; }

/* Forms */
.form-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
fieldset.field { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset.field legend { float: left; width: 100%; font-weight: 600; padding: 0; margin: .5rem 0 .2rem; }
fieldset.field legend + * { clear: both; }
.field-error { color: var(--red); font-size: 12px; margin-top: .25rem; }
.check-list { display: grid; gap: .4rem; margin-top: .5rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.check { display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem; border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; }
.check input { margin: 0; }
[hidden] { display: none !important; }

/* Tenant pages */
.tenant-head { margin-bottom: 1rem; }
.tenant-head h1 { margin-bottom: .1rem; }
.stat-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.stat { padding: 16px 20px; }
.stat-label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.stat-value { font-size: 1.75rem; font-weight: 700; }
.stat-text { font-weight: 600; margin-top: .35rem; }
.steps { margin: 0; padding-left: 1.25rem; }
.steps li + li { margin-top: .4rem; }
.pill-tabs { display: inline-flex; gap: .25rem; padding: .25rem; background: var(--grey-bg); border-radius: var(--radius); }
.pill-tabs a { padding: .35rem .8rem; border-radius: 6px; color: var(--muted); font-weight: 600; }
.pill-tabs a:hover { text-decoration: none; color: var(--text); }
.pill-tabs a.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.chip-list { display: flex; flex-wrap: wrap; gap: .4rem; }
.chip { padding: .3rem .6rem; border: 1px solid var(--border); border-radius: 999px; background: #fff; }
td.wrap { white-space: normal; min-width: 220px; }
.grow { flex: 1 1 240px; width: auto; }
.pager { display: flex; align-items: center; justify-content: center; gap: 1rem; margin-top: 1rem; }
dl.meta { display: grid; grid-template-columns: minmax(120px, 200px) 1fr; gap: .5rem 1rem; margin: .5rem 0 0; }
dl.meta dt { color: var(--muted); }
dl.meta dd { margin: 0; overflow-wrap: anywhere; }
.text-ok { color: var(--green); }
.text-error { color: var(--red); }
.page-head h2 { margin: 0; }

/* Upload drop zone */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: .4rem; text-align: center;
  padding: 32px 16px; background: #fff;
  border: 2px dashed var(--border); border-radius: var(--radius);
  transition: border-color .15s, background .15s;
}
.dropzone.is-over { border-color: var(--primary); background: var(--primary-soft); }
.dropzone.is-busy { opacity: .7; pointer-events: none; }
.dropzone-title { font-weight: 600; font-size: 1rem; margin: 0; }
.dropzone p { margin: 0; }
.dropzone label.btn { margin-top: .5rem; position: relative; overflow: hidden; }
.dropzone input[type=file] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* File preview grid */
.data-grid-wrap { max-height: 70vh; overflow: auto; }
table.data-grid th { position: sticky; top: 0; z-index: 1; }
table.data-grid td { max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
table.data-grid .rownum { color: var(--muted); font-variant-numeric: tabular-nums; position: sticky; left: 0; background: var(--bg); z-index: 2; }
table.data-grid thead .rownum { z-index: 3; }
.badge-empty { background: var(--amber-bg); color: var(--amber); font-size: 11px; }

/* Mapping editor */
.row.between { justify-content: space-between; }
.mapped-count { margin: 0; }
.check-inline { display: inline-flex; align-items: center; gap: .35rem; font-weight: 400; white-space: nowrap; }
.mapping-table td { vertical-align: top; white-space: normal; }
.mapping-table .col-name { min-width: 160px; }
.mapping-table .examples { min-width: 200px; max-width: 320px; }
.mapping-table .examples .chip { display: inline-block; margin: 0 .25rem .25rem 0; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: top; }
.mapping-table select[data-target] { min-width: 240px; }
.mapping-table tr:not(.is-mapped) .col-name strong { color: var(--muted); font-weight: 500; }
.col-options { min-width: 240px; }
.col-options .opt { display: block; margin-bottom: .4rem; }
.col-options .opt:last-child { margin-bottom: 0; }
input.tiny { width: 3.5rem; padding: .3rem .4rem; }
.valuemap { border-left: 2px solid var(--primary-soft); padding-left: .6rem; }
.vm-row { display: flex; align-items: center; gap: .4rem; margin-top: .3rem; }
.vm-row input, .vm-row select { padding: .3rem .45rem; min-width: 0; }
.vm-row input { width: 7rem; }
.check-results h2 { margin-bottom: .4rem; }
.issues { list-style: none; margin: 0; padding: 0; }
.issue { padding: .55rem 0; border-top: 1px solid var(--border); }
.unmatched { margin: .4rem 0 0 1rem; padding: 0; list-style: none; }
.unmatched li { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; padding: .15rem 0; }
.map-to { width: auto; padding: .2rem .4rem; font-size: 12px; }

/* Import pages */
.progress-line { margin-bottom: .5rem; }
progress.progress { width: 100%; height: 10px; border: 0; border-radius: 999px; overflow: hidden; background: var(--grey-bg); appearance: none; }
progress.progress::-webkit-progress-bar { background: var(--grey-bg); border-radius: 999px; }
progress.progress::-webkit-progress-value { background: var(--primary); border-radius: 999px; transition: width .4s; }
progress.progress::-moz-progress-bar { background: var(--primary); border-radius: 999px; }
.stat-created .stat-value { color: var(--green); }
.stat-updated .stat-value { color: var(--blue); }
.stat-failed .stat-value { color: var(--red); }
.settings-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
table.compact th, table.compact td { padding: .35rem .5rem; }
.rows-table td { vertical-align: top; }
.badge-action-created { background: var(--green-bg); color: var(--green); }
.badge-action-updated { background: var(--blue-bg); color: var(--blue); }
.badge-action-skipped { background: var(--grey-bg); color: var(--muted); }
.badge-action-failed { background: var(--red-bg); color: var(--red); }
.badge-action-pending { background: var(--amber-bg); color: var(--amber); }
.text-warn { color: var(--amber); }
.estimate h3 { margin-bottom: .3rem; }
.rows-table details summary { cursor: pointer; list-style: none; }
.rows-table details summary::-webkit-details-marker { display: none; }
.rows-table details[open] summary .muted { display: none; }
