:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --primary-ring: rgba(79, 70, 229, 0.15);
  --success: #059669;
  --success-soft: #ecfdf5;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Header ── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-left: 0.6rem;
  border-left: 1px solid var(--border);
}

.user-email {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-weight: 500;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 800;
}

.nav {
  display: flex;
  gap: 0.25rem;
  background: var(--bg);
  padding: 3px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.nav a {
  text-decoration: none;
  color: var(--text-secondary);
  padding: 0.4rem 0.85rem;
  border-radius: 7px;
  font-size: 0.82rem;
  font-weight: 500;
  transition: all 0.15s;
}

.nav a:hover { color: var(--text); background: var(--surface); }
.nav a.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

/* ── Module tabs ── */
.module-nav {
  display: flex;
  gap: 0.25rem;
  background: var(--bg);
  padding: 3px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.module-tab {
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 0.45rem 1rem;
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.15s;
}

.module-tab:hover { color: var(--text); background: var(--surface); }
.module-tab.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

.module-tab-link {
  text-decoration: none;
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 0.45rem 1rem;
  border-radius: 7px;
  transition: all 0.15s;
}

.module-tab-link:hover { color: var(--text); background: var(--surface); }
.active-page { background: var(--primary-soft) !important; color: var(--primary) !important; font-weight: 600; }

.module-panel { display: none; height: calc(100vh - 56px); overflow: hidden; }
.module-panel.active { display: block; }

.module-panel .app-shell { height: 100%; }

/* Embedded module (Parts-Bleed) fills the panel below the top nav */
.module-frame { width: 100%; height: 100%; border: 0; display: block; }

/* ── Delete-audit modal ── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(15, 23, 42, 0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal-overlay.hidden { display: none; }
.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 1.5rem; width: 460px;
  max-width: calc(100vw - 2rem); box-shadow: var(--shadow-md);
}
.modal-card h3 { margin: 0 0 0.6rem; font-size: 1.1rem; }
.modal-card p { margin: 0 0 1rem; font-size: 0.86rem; color: var(--text-secondary); line-height: 1.55; }
.modal-check {
  display: flex; gap: 0.55rem; align-items: flex-start; cursor: pointer;
  background: var(--bg); border: 1px solid var(--border); border-radius: 9px;
  padding: 0.75rem 0.85rem; font-size: 0.84rem; line-height: 1.45; color: var(--text-secondary);
}
.modal-check input { margin: 2px 0 0; flex-shrink: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.25rem; }
.btn.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn.btn-danger:hover { filter: brightness(0.94); background: var(--danger); }

/* Per-run delete button (appears on hover in run lists) */
.run-item { position: relative; }
.run-del {
  position: absolute; top: 6px; right: 6px;
  background: transparent; border: none; cursor: pointer;
  font-size: 0.82rem; line-height: 1; padding: 3px 5px; border-radius: 6px;
  opacity: 0; transition: opacity 0.15s, background 0.15s; color: var(--text-secondary);
}
.run-item:hover .run-del { opacity: 0.7; }
.run-del:hover { opacity: 1; background: var(--danger-soft); color: var(--danger); }

.header-meta { font-size: 0.78rem; color: var(--text-muted); }

.placeholder-module {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  color: var(--text-muted);
}

.placeholder-module h2 {
  margin: 0.5rem 0 0.35rem;
  color: var(--text-secondary);
  font-size: 1.2rem;
}

.placeholder-module p { margin: 0; font-size: 0.9rem; max-width: 400px; }

/* ── Layout ── */
.app-shell {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: calc(100vh - 56px);
  overflow: hidden;
}

.module-panel .app-shell {
  height: 100%;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 1.1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.sidebar-header p {
  margin: 0.25rem 0 0;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.run-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
  overflow-y: auto;
  flex: 1;
}

.run-item {
  padding: 0.75rem 0.85rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.12s;
  border: 1px solid transparent;
}

.run-item:hover { background: var(--bg); }
.run-item.active {
  background: var(--primary-soft);
  border-color: var(--primary-ring);
}

.run-item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.run-item .title {
  font-weight: 600;
  font-size: 0.84rem;
  color: var(--text);
}

.run-item .meta {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.run-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
}

.run-dot.partial { background: var(--warn); }
.run-dot.fail { background: var(--danger); }

.main-workspace {
  overflow-y: auto;
  padding: 1.25rem 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── Run banner ── */
.run-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  border-radius: var(--radius);
  color: #fff;
  box-shadow: var(--shadow-md);
}

.run-banner.hidden { display: none; }

.run-banner-info h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.run-banner-info p {
  margin: 0.2rem 0 0;
  font-size: 0.82rem;
  opacity: 0.85;
}

.run-banner-actions .btn {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.25);
  color: #fff;
  backdrop-filter: blur(4px);
}

.run-banner-actions .btn:hover { background: rgba(255,255,255,0.25); }

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.15rem;
  border-bottom: 1px solid var(--border);
}

.card-header h3 {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 600;
}

.card-body { padding: 1.15rem; }

/* ── Export controls ── */
.export-form {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.field label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.field input[type="date"],
.field input[type="search"] {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-ring);
}

/* ── Buttons ── */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.84rem;
  font-weight: 500;
  font-family: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn:hover { background: var(--bg); border-color: var(--border-strong); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.78rem; }

/* ── KPI stats ── */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
}

.kpi-row.hidden { display: none; }

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow-sm);
}

.kpi-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin-top: 0.15rem;
  line-height: 1.1;
}

.kpi-sub {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
}

.kpi.accent { border-left: 3px solid var(--primary); }
.kpi.success { border-left: 3px solid var(--success); }
.kpi.danger { border-left: 3px solid var(--danger); }

/* ── Progress ── */
.progress-panel {
  padding: 1rem 1.15rem;
  background: var(--primary-soft);
  border: 1px solid var(--primary-ring);
  border-radius: var(--radius);
}

.progress-panel.hidden { display: none; }

.progress-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.6rem;
}

.progress-top span { font-size: 0.84rem; color: var(--text-secondary); }
.progress-top strong { font-size: 1.1rem; color: var(--primary); }

.progress-bar {
  height: 6px;
  background: rgba(79, 70, 229, 0.15);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #818cf8);
  border-radius: 999px;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  width: 0%;
}

.progress-detail {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 0.45rem;
}

/* ── Tabs ── */
.tab-bar {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  padding: 0 1.15rem;
}

.tab-btn {
  padding: 0.7rem 1rem;
  border: none;
  background: none;
  font-family: inherit;
  font-size: 0.84rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: all 0.15s;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── School status ── */
.school-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1.15rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.chip-group { display: flex; gap: 0.35rem; }

.chip {
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.74rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.12s;
}

.chip:hover { border-color: var(--border-strong); }
.chip.active {
  background: var(--primary-soft);
  border-color: var(--primary-ring);
  color: var(--primary);
}

.school-search {
  margin-left: auto;
  padding: 0.35rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  width: 180px;
}

.school-grid {
  max-height: 480px;
  overflow-y: auto;
}

.school-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 1.15rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
  transition: background 0.1s;
}

.school-row:hover { background: var(--bg); }
.school-row:last-child { border-bottom: none; }

.school-row.fail { background: var(--danger-soft); }

.school-name-text {
  font-weight: 500;
  color: var(--text);
}

.badge {
  font-size: 0.7rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-weight: 600;
  white-space: nowrap;
}

.badge.ok { background: var(--success-soft); color: var(--success); }
.badge.fail { background: var(--danger-soft); color: var(--danger); }
.badge.zero { background: var(--bg); color: var(--text-muted); border: 1px solid var(--border); }

/* ── Data table ── */
.data-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1.15rem;
  border-bottom: 1px solid var(--border);
}

.data-toolbar input {
  flex: 1;
  max-width: 320px;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
}

.table-scroll {
  overflow: auto;
  max-height: 520px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.data-table th {
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 2;
  padding: 0.6rem 0.85rem;
  text-align: left;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.data-table tbody tr:hover td { background: var(--primary-soft); }
.data-table tbody tr:nth-child(even) td { background: rgba(248, 250, 252, 0.6); }
.data-table tbody tr:nth-child(even):hover td { background: var(--primary-soft); }

.pager {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.15rem;
  border-top: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.pager-actions { display: flex; gap: 0.35rem; }

/* ── Empty state ── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3.5rem 2rem;
  text-align: center;
  color: var(--text-muted);
}

.empty-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  margin-bottom: 0.85rem;
}

.empty-state h3 {
  margin: 0 0 0.35rem;
  font-size: 0.95rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.empty-state p {
  margin: 0;
  font-size: 0.84rem;
  max-width: 320px;
}

/* ── Audit tab ── */
.audit-source-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.85rem;
}

.audit-source-field label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.audit-select {
  width: 100%;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.84rem;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}

.audit-date-row {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.audit-date-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.audit-date-fields {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.audit-date-fields .field {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.audit-date-fields .field label {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 500;
}

.audit-date-fields input[type="date"] {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
  font-family: inherit;
}

.audit-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1.15rem;
  border-bottom: 1px solid var(--border);
}

.audit-toolbar input {
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
}

/* Copy button in serial cell */
.serial-cell {
  white-space: nowrap;
}

.copy-btn {
  opacity: 0;
  margin-left: 6px;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.78rem;
  cursor: pointer;
  transition: opacity 0.12s, color 0.12s;
  vertical-align: middle;
}

tr:hover .copy-btn { opacity: 1; }
.copy-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-soft); }

/* Flagged rows */
tr.audit-flagged td { background: #fff7ed !important; }
tr.audit-flagged:hover td { background: #ffedd5 !important; }

.serial-cell.audit-flagged { color: #92400e; font-weight: 600; }
.flag-badge {
  font-size: 0.75rem;
  color: #b45309;
  margin-left: 4px;
}

/* Difference column */
td.diff-cell {
  font-weight: 700;
  color: var(--danger);
}

/* Case detail columns — lighter background */
th.case-col { background: #f8fafc; color: var(--text-muted); }
td.case-col { color: var(--text-secondary); background: rgba(248,250,252,0.5); }

@media (max-width: 900px) {
  .audit-source-grid { grid-template-columns: 1fr; }
}

/* ── Upload drop zone ── */
.upload-drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  background: var(--bg);
}

.upload-drop-zone:hover,
.upload-drop-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.upload-drop-zone .upload-icon {
  font-size: 2.2rem;
  margin-bottom: 0.6rem;
}

.upload-drop-zone .upload-label {
  margin: 0 0 0.35rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.upload-browse {
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.upload-hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.upload-progress {
  margin-top: 1rem;
}

.hidden { display: none !important; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  padding: 0.75rem 1.15rem;
  background: var(--text);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
  font-weight: 500;
  z-index: 200;
  box-shadow: var(--shadow-md);
  animation: slideUp 0.25s ease;
}

.toast.err { background: var(--danger); }

@keyframes slideUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Responsive ── */
@media (max-width: 1100px) {
  .app-shell { grid-template-columns: 1fr; height: auto; overflow: visible; }
  .sidebar { max-height: 240px; border-right: none; border-bottom: 1px solid var(--border); }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
  .main-workspace { overflow: visible; }
}

@media (max-width: 600px) {
  .kpi-row { grid-template-columns: 1fr 1fr; }
  .export-form { flex-direction: column; align-items: stretch; }
  .run-banner { flex-direction: column; align-items: flex-start; }
}
