:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #dde3ec;
  --text: #1a2438;
  --text2: #5a6a85;
  --accent: #2d6cdf;
  --accent-soft: rgba(45, 108, 223, 0.1);
  --hail: #e85d75;
  --non-hail: #6b8cae;
  --mono: 'DM Mono', monospace;
  --sans: 'DM Sans', sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  min-height: 100vh;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-left { min-width: 0; }

.logo {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 1.2px;
  color: var(--accent);
}

.subtitle {
  font-size: 12px;
  color: var(--text2);
  margin-top: 2px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.filter-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text2);
}

select {
  background: #f0f4fa;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: var(--sans);
  font-size: 13px;
  padding: 8px 12px;
  min-width: 120px;
  outline: none;
}

select:focus { border-color: var(--accent); }

.icon-btn {
  background: #f0f4fa;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 18px;
  color: var(--text2);
  transition: all 0.15s;
}

.icon-btn:hover { border-color: var(--accent); color: var(--accent); }

.icon-btn:disabled {
  opacity: 0.65;
  cursor: wait;
}

.icon-btn.refreshing {
  animation: spin 0.8s linear infinite;
  color: var(--accent);
  border-color: var(--accent);
}

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

.badge {
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

.badge.live { background: rgba(39, 174, 96, 0.12); color: #1a9d5c; }
.badge.cached { background: rgba(245, 158, 11, 0.14); color: #b45309; }

.tabs {
  display: flex;
  gap: 0;
  padding: 0 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 14px 20px;
  font-family: var(--sans);
  font-size: 14px;
  font-weight: 600;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.15s;
}

.tab:hover { color: var(--accent); }

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

main {
  padding: 24px 28px 48px;
  max-width: 1600px;
  margin: 0 auto;
}

.hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.hero h1 {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 4px;
}

.muted { color: var(--text2); font-size: 14px; }

.hero-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 22px;
  text-align: right;
}

.hero-stat-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--text2);
}

.hero-stat-value {
  font-family: var(--mono);
  font-size: 32px;
  font-weight: 500;
  line-height: 1.1;
  margin-top: 4px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.kpi-source-card {
  min-width: 0;
}

.kpi-source-label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.35;
  word-break: break-word;
}

.kpi-source-label.is-path {
  font-family: var(--mono);
  font-size: 11px;
  word-break: break-all;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  border-top: 3px solid var(--kpi-accent, var(--accent));
  box-shadow: 0 1px 8px rgba(26, 36, 56, 0.04);
}

.kpi-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.7px;
  text-transform: uppercase;
  color: var(--text2);
  margin-bottom: 6px;
}

.kpi-value {
  font-family: var(--mono);
  font-size: 22px;
  font-weight: 500;
  line-height: 1.2;
  color: var(--text);
  word-break: break-word;
}

.kpi-sub {
  font-size: 11px;
  color: var(--text2);
  margin-top: 4px;
}

.export-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.export-btn:hover { opacity: 0.9; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
}

.modal-panel {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 16px 48px rgba(26, 36, 56, 0.18);
}

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

.modal-header h2 {
  font-size: 17px;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  line-height: 1;
  color: var(--text2);
  cursor: pointer;
  padding: 0 4px;
}

.modal-body { padding: 18px 20px; }

.export-fieldset {
  border: none;
  margin: 0 0 18px;
  padding: 0;
}

.export-fieldset:last-child { margin-bottom: 0; }

.export-fieldset legend {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text2);
  margin-bottom: 10px;
}

.export-check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--text);
  margin-bottom: 8px;
  cursor: pointer;
}

.export-check input { accent-color: var(--accent); }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
}

.btn-secondary, .btn-primary {
  border-radius: 6px;
  padding: 9px 16px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.btn-secondary {
  background: #f0f4fa;
  border: 1px solid var(--border);
  color: var(--text2);
}

.btn-primary {
  background: var(--accent);
  border: none;
  color: #fff;
}

.btn-primary:disabled { opacity: 0.6; cursor: wait; }

.loading, .error {
  padding: 16px 20px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 14px;
}

.loading {
  background: var(--accent-soft);
  color: var(--accent);
}

.error {
  background: rgba(231, 76, 60, 0.1);
  color: #c0392b;
}

.hidden { display: none !important; }

.breadcrumb {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 16px;
  font-size: 13px;
}

.crumb-sep { color: var(--text2); margin: 0 2px; }

.crumb.current {
  font-weight: 600;
  color: var(--text);
}

.crumb-btn {
  background: none;
  border: none;
  padding: 4px 6px;
  border-radius: 4px;
  font-family: var(--sans);
  font-size: 13px;
  color: var(--accent);
  cursor: pointer;
}

.crumb-btn:hover { background: var(--accent-soft); }

.pivot-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 2px 16px rgba(26, 36, 56, 0.06);
  overflow: hidden;
}

.pivot-columns {
  display: flex;
  flex-direction: column;
}

.pivot-col {
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.pivot-col:last-child { border-bottom: none; }

.pivot-col-enter {
  animation: slideDown 0.3s ease both;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.pivot-col-header {
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.pivot-col-header-main { min-width: 0; }

.pivot-stage {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 4px;
}

.pivot-parent {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  word-break: break-word;
  white-space: normal;
  line-height: 1.35;
}

.pivot-parent.is-url {
  font-family: var(--mono);
  font-size: 12px;
  word-break: break-all;
}

.pivot-parent-val {
  font-family: var(--mono);
  font-size: 24px;
  font-weight: 500;
  flex-shrink: 0;
}

.pivot-rows {
  padding: 12px 16px 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 8px;
}

.pivot-rows-stack {
  grid-template-columns: 1fr;
  max-width: 720px;
}

.pivot-row-card {
  border: 1px solid transparent;
  border-radius: 8px;
  transition: border-color 0.15s, background 0.15s;
}

.pivot-row-card.selected {
  background: var(--accent-soft);
  border-color: rgba(45, 108, 223, 0.35);
}

.pivot-row-card.job-leaf {
  background: rgba(26, 157, 92, 0.06);
  border-color: rgba(26, 157, 92, 0.15);
}

.pivot-row-card.job-leaf .row-label {
  font-family: var(--mono);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.no-pin-badge {
  display: inline-block;
  font-family: var(--sans);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #b45309;
  background: rgba(245, 158, 11, 0.14);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 999px;
  padding: 2px 7px;
  vertical-align: middle;
}

.job-number-cell {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.pivot-row-inner {
  display: flex;
  align-items: stretch;
  min-width: 0;
}

.pivot-row {
  display: block;
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  cursor: default;
  font-family: var(--sans);
  transition: background 0.15s;
}

.pivot-row.drillable { cursor: pointer; }
.pivot-row-card:has(.pivot-row.drillable:hover) { background: #f0f4fa; border-color: var(--border); }
.pivot-row.leaf { opacity: 0.92; }

.row-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.row-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.3;
  flex: 1;
  min-width: 0;
  word-break: break-word;
  white-space: normal;
}

.row-label.is-url {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.4;
  word-break: break-all;
}

.row-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
  min-width: 108px;
}

.row-value {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  min-width: 2ch;
  text-align: right;
}

.row-pct {
  font-size: 11px;
  color: var(--text2);
  min-width: 38px;
  text-align: right;
}

.row-chevron {
  font-size: 16px;
  color: var(--accent);
  font-weight: 600;
  line-height: 1;
}

.row-bar-track {
  height: 6px;
  background: #e8eef5;
  border-radius: 999px;
  overflow: hidden;
}

.row-bar-fill {
  height: 100%;
  border-radius: 999px;
  min-width: 2px;
  transition: width 0.3s ease;
}

.pivot-empty {
  grid-column: 1 / -1;
  padding: 24px 12px;
  text-align: center;
  color: var(--text2);
  font-size: 13px;
}

.pivot-row.detail-row {
  cursor: pointer;
}
.pivot-row-card:has(.pivot-row.detail-row:hover) {
  border-color: var(--accent);
  background: rgba(45, 108, 223, 0.06);
}
.pivot-row-card:has(.pivot-row.detail-row).selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.detail-chevron {
  font-size: 14px;
}
.detail-chevron-btn {
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-left: 1px solid var(--border);
  background: transparent;
  font-size: 14px;
  line-height: 1;
  color: var(--accent);
  cursor: pointer;
  padding: 0 12px;
  border-radius: 0 8px 8px 0;
  flex-shrink: 0;
}
.detail-chevron-btn:hover {
  color: var(--text);
  background: rgba(45, 108, 223, 0.08);
}
.pivot-row-card:has(.detail-chevron-btn:hover) {
  border-color: var(--border);
}

.detail-panel {
  margin-top: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.detail-panel.hidden { display: none; }

.detail-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}
.detail-panel-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  flex: 1;
  min-width: 0;
}
.detail-panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.detail-export-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.detail-export-btn:hover:not(:disabled) { opacity: 0.9; }
.detail-export-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.detail-export-btn.hidden { display: none; }
.detail-close {
  border: none;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  color: var(--text2);
  cursor: pointer;
  padding: 0 4px;
}
.detail-close:hover { color: var(--text); }

.detail-panel-body {
  padding: 12px 16px 16px;
  overflow-x: auto;
}
.detail-source-note {
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--text2);
}
.detail-source-note code {
  font-family: var(--mono);
  font-size: 11px;
}
.detail-empty,
.detail-loading {
  margin: 0;
  font-size: 13px;
  color: var(--text2);
}
.detail-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.detail-table th,
.detail-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.detail-table th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text2);
  font-weight: 600;
}
.detail-table tbody tr:hover {
  background: rgba(45, 108, 223, 0.04);
}
.detail-table .mono {
  font-family: var(--mono);
  font-size: 12px;
}

.loss-reason-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(231, 76, 60, 0.12);
  color: #c0392b;
  border: 1px solid rgba(231, 76, 60, 0.3);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.4;
  white-space: normal;
}

.loss-reason-empty {
  color: var(--text2);
  font-style: italic;
  font-size: 12px;
}

.activity-stale-legend {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.18);
  color: #b45309;
  font-weight: 600;
  font-size: 11px;
}

.detail-table-pins tr.activity-stale-row {
  background: rgba(245, 158, 11, 0.1);
}

.detail-table-pins tr.activity-stale-row:hover {
  background: rgba(245, 158, 11, 0.16);
}

.activity-stale-cell {
  font-weight: 600;
}

.activity-stale-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.2);
  color: #b45309;
  border: 1px solid rgba(245, 158, 11, 0.45);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  margin-right: 4px;
  vertical-align: middle;
}

.activity-title {
  font-weight: 600;
  color: var(--text);
}

.activity-template {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 4px;
  background: #eef2f7;
  color: var(--text2);
  font-family: var(--mono);
  font-size: 10px;
}

.activity-empty {
  color: var(--text2);
  font-style: italic;
  font-size: 12px;
}

.stale-alerts {
  margin-bottom: 16px;
}

.stale-alerts-banner {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid rgba(245, 158, 11, 0.45);
  background: linear-gradient(135deg, rgba(254, 243, 199, 0.95), rgba(255, 251, 235, 0.9));
  box-shadow: 0 1px 0 rgba(245, 158, 11, 0.08);
}

.stale-alerts-icon {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #f59e0b;
  color: #fff;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.stale-alerts-title {
  font-size: 15px;
  font-weight: 700;
  color: #92400e;
}

.stale-alerts-desc {
  margin: 4px 0 0;
  font-size: 12px;
  color: #a16207;
}

.stale-alert-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.stale-alert-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(245, 158, 11, 0.45);
  background: #fff;
  color: #92400e;
  cursor: pointer;
  font: inherit;
}

.stale-alert-chip:hover {
  background: rgba(245, 158, 11, 0.12);
}

.stale-alert-chip-count {
  font-weight: 700;
  font-family: var(--mono);
}

.stale-alert-pins {
  display: grid;
  gap: 6px;
}

.stale-alert-pin {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(245, 158, 11, 0.25);
  background: rgba(255, 255, 255, 0.75);
  cursor: pointer;
  font: inherit;
}

.stale-alert-pin:hover {
  background: #fff;
  border-color: rgba(245, 158, 11, 0.5);
}

.stale-alert-pin-name {
  font-weight: 600;
  color: #78350f;
}

.stale-alert-pin-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 12px;
  color: #a16207;
}

.stale-alert-pin-rep {
  font-weight: 600;
}

.stale-alert-more {
  font-size: 12px;
  padding: 4px 2px;
}

.sales-rep-name {
  font-weight: 600;
  color: var(--text);
}

.sales-rep-empty {
  color: var(--text2);
  font-style: italic;
  font-size: 12px;
}

.hint {
  margin-top: 14px;
  font-size: 12px;
}

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #f4f7fb 0%, #eef2f7 45%, #e8eef6 100%);
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 28px 24px;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.08);
}

.login-logo {
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent);
  font-size: 12px;
  margin-bottom: 10px;
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 28px;
}

.login-subtitle {
  margin: 0 0 20px;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
}

.login-label input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  background: #fff;
}

.login-label input:focus {
  outline: 2px solid rgba(45, 108, 223, 0.25);
  border-color: var(--accent);
}

.login-error {
  margin: 0;
  color: #b91c1c;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

.login-submit {
  width: 100%;
  margin-top: 4px;
}

.user-badge {
  background: #eef2ff;
  color: #3730a3;
}

@media (max-width: 700px) {
  header, main, .tabs { padding-left: 16px; padding-right: 16px; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .pivot-rows { grid-template-columns: 1fr; }
}
