/* ============================================================
   Merlin KPI Compass - Performance Dashboard
   ============================================================ */

:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface2: #f0ebe4;
  --border: #e5dbcf;
  --border2: #cfc3b4;
  --accent: #0f766e;
  --accent2: #ea580c;
  --green: #16a34a;
  --green-bg: rgba(22, 163, 74, 0.08);
  --green-border: rgba(22, 163, 74, 0.3);
  --red: #dc2626;
  --red-bg: rgba(220, 38, 38, 0.08);
  --red-border: rgba(220, 38, 38, 0.3);
  --amber: #f59e0b;
  --amber-bg: rgba(245, 158, 11, 0.12);
  --amber-border: rgba(245, 158, 11, 0.35);
  --text: #1f2933;
  --text2: #52616b;
  --text3: #8a96a3;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Space Grotesk', sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(circle at 12% 15%, rgba(15, 118, 110, 0.08), transparent 45%),
    radial-gradient(circle at 85% 10%, rgba(234, 88, 12, 0.08), transparent 42%),
    linear-gradient(rgba(15, 118, 110, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(15, 118, 110, 0.05) 1px, transparent 1px);
  background-size: auto, auto, 40px 40px, 40px 40px;
  pointer-events: none;
  z-index: 0;
}

/* ---- HEADER ---- */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  padding: 0 32px;
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-badge {
  width: 38px;
  height: 38px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  box-shadow: 0 2px 10px rgba(15, 118, 110, 0.25);
  font-family: 'Fraunces', serif;
}

.header-title {
  font-family: 'Fraunces', serif;
  font-size: 21px;
  color: var(--text);
  letter-spacing: -0.3px;
}

.header-subtitle {
  font-size: 11px;
  color: var(--text3);
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 0.8px;
}

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

/* ---- BUTTONS ---- */
.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 18px;
  border-radius: 10px;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.btn-outline {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text2);
}

.btn-outline:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(15, 118, 110, 0.06);
}

/* ---- ENTITY SELECTOR BAR ---- */
.school-bar {
  position: relative;
  z-index: 1;
  padding: 20px 32px 0;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.scope-shell {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.school-label {
  font-size: 11px;
  color: var(--text3);
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 1px;
  text-transform: uppercase;
  white-space: nowrap;
}

.school-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.scope-context {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.context-label {
  font-size: 10px;
  color: var(--text3);
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.context-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.context-tab {
  border-style: dashed;
}

.scope-path {
  font-size: 11px;
  color: var(--text2);
  font-family: 'JetBrains Mono', monospace;
}

.school-tab {
  padding: 9px 18px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.school-tab.active {
  background: rgba(15, 118, 110, 0.08);
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(15, 118, 110, 0.1);
}

.school-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.school-dot.global {
  background: #0f766e;
  box-shadow: 0 0 6px rgba(15, 118, 110, 0.6);
}

.school-dot.regional {
  background: #ea580c;
  box-shadow: 0 0 6px rgba(234, 88, 12, 0.6);
}

.school-dot.local {
  background: #16a34a;
  box-shadow: 0 0 6px rgba(22, 163, 74, 0.6);
}

/* ---- SUMMARY STRIP ---- */
.summary-strip {
  position: relative;
  z-index: 1;
  padding: 20px 32px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.stat-card {
  flex: 1;
  min-width: 180px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  transition: all 0.2s;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.03);
}

.stat-card:hover {
  border-color: var(--border2);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
}

.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text2);
  flex-shrink: 0;
  font-family: 'JetBrains Mono', monospace;
}

.stat-icon.green {
  background: var(--green-bg);
  color: var(--green);
}

.stat-icon.red {
  background: var(--red-bg);
  color: var(--red);
}

.stat-icon.amber {
  background: var(--amber-bg);
  color: var(--amber);
}

.stat-icon.blue {
  background: rgba(15, 118, 110, 0.08);
  color: var(--accent);
}

.stat-num {
  font-family: 'Fraunces', serif;
  font-size: 30px;
  line-height: 1;
  color: var(--text);
}

.stat-label {
  font-size: 12px;
  color: var(--text3);
  margin-top: 3px;
  font-weight: 600;
  letter-spacing: 0.3px;
}

.score-val {
  font-family: 'Fraunces', serif;
  font-size: 24px;
  transition: color 0.3s;
}

/* ---- FILTER BAR ---- */
.filter-bar {
  position: relative;
  z-index: 1;
  padding: 4px 32px 16px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-chip {
  padding: 7px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.filter-chip:hover {
  border-color: var(--border2);
  color: var(--text);
}

.filter-chip.active-filter {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(15, 118, 110, 0.06);
}

.filter-sep {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 6px;
}

/* ---- CONTRIBUTION FLOW ---- */
.hierarchy-wrap {
  position: relative;
  z-index: 1;
  padding: 0 32px 18px;
}

.hierarchy-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 18px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
}

.hierarchy-head {
  margin-bottom: 12px;
}

.hierarchy-title {
  font-family: 'Fraunces', serif;
  font-size: 19px;
  color: var(--text);
  line-height: 1.2;
}

.hierarchy-subtitle {
  font-size: 12px;
  color: var(--text3);
  margin-top: 4px;
}

.hierarchy-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 12px;
}

.flow-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.flow-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: start;
}

.flow-unit {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}

.flow-tag {
  font-size: 10px;
  color: var(--accent);
  border: 1px solid rgba(15, 118, 110, 0.2);
  background: rgba(15, 118, 110, 0.08);
  border-radius: 4px;
  padding: 2px 6px;
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
}

.flow-metrics {
  display: grid;
  gap: 4px;
  margin-top: 10px;
}

.flow-metric {
  font-size: 11px;
  color: var(--text2);
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.flow-metric strong {
  font-family: 'JetBrains Mono', monospace;
  color: var(--text);
}

.flow-lift {
  margin-top: 10px;
  font-size: 11px;
  color: var(--accent2);
  font-family: 'JetBrains Mono', monospace;
}

.flow-empty {
  font-size: 12px;
  color: var(--text3);
  padding: 8px 0;
}

/* ---- MAIN TABLE AREA ---- */
.main {
  position: relative;
  z-index: 1;
  padding: 0 32px 48px;
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: visible;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.04);
}

.scan-progress {
  height: 3px;
  background: linear-gradient(90deg, var(--accent), var(--accent2), var(--accent));
  background-size: 200% 100%;
  border-radius: 2px;
  animation: scanShimmer 1.2s ease-in-out infinite;
  display: none;
}

@keyframes scanShimmer {
  0% {
    background-position: 200% 0;
  }

  100% {
    background-position: -200% 0;
  }
}

.table-header-row {
  display: grid;
  grid-template-columns: 36px minmax(200px, 1.2fr) minmax(180px, 1fr) minmax(280px, 1.8fr);
  padding: 13px 20px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  gap: 14px;
  align-items: center;
  min-width: 860px;
}

.th {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--text3);
  font-weight: 600;
}

/* Category dividers */
.theme-group {
  display: flex;
  flex-direction: column;
}

.category-divider {
  padding: 16px 20px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 16px;
  cursor: pointer;
  transition: background 0.2s;
}

.category-divider:hover {
  background: rgba(15, 118, 110, 0.05);
}

.category-tag {
  font-size: 10px;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--accent);
  background: rgba(15, 118, 110, 0.08);
  border: 1px solid rgba(15, 118, 110, 0.18);
  padding: 4px 12px;
  border-radius: 6px;
  font-weight: 600;
}

.theme-chevron {
  color: var(--text3);
  font-size: 11px;
  transition: transform 0.3s ease;
  margin-left: 8px;
}

.category-divider.collapsed .theme-chevron {
  transform: rotate(-90deg);
}

.theme-content {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: max-height 0.4s ease, opacity 0.3s ease;
  max-height: 5000px;
  opacity: 1;
}

.theme-content.collapsed {
  max-height: 0;
  opacity: 0;
  pointer-events: none;
}

/* Item cards */
.item-pill {
  width: 190px;
  height: 100%;
  min-height: 90px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  padding: 10px;
  font-size: 11px;
  text-align: left;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.item-pill:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.item-pill .pill-title {
  font-weight: 700;
  font-size: 11px;
  margin-bottom: 4px;
  line-height: 1.4;
  color: var(--text);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
}

.item-pill .pill-meta {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  opacity: 0.8;
  color: var(--text2);
}

.item-pill.on-track {
  background: var(--green-bg);
  color: var(--green);
  border-color: var(--green-border);
}

.item-pill.at-risk {
  background: var(--amber-bg);
  color: var(--amber);
  border-color: var(--amber-border);
}

.item-pill.off-track {
  background: var(--red-bg);
  color: var(--red);
  border-color: var(--red-border);
}

.pill-tooltip {
  position: relative;
  display: inline-flex;
}

.pill-tooltip .tooltiptext {
  visibility: hidden;
  width: max-content;
  max-width: 300px;
  background-color: var(--text);
  color: #fff;
  text-align: left;
  border-radius: 8px;
  padding: 10px 14px;
  position: absolute;
  z-index: 1000;
  bottom: calc(100% + 10px);
  left: 0;
  opacity: 0;
  transition: opacity 0.3s;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 11px;
  font-weight: 400;
  line-height: 1.5;
  white-space: pre-wrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.pill-tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 30px;
  border-width: 5px;
  border-style: solid;
  border-color: var(--text) transparent transparent transparent;
}

.pill-tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}

/* KPI rows */
.reg-row {
  display: grid;
  grid-template-columns: 36px minmax(200px, 1.2fr) minmax(180px, 1fr) minmax(280px, 1.8fr);
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  gap: 14px;
  align-items: start;
  transition: background 0.15s;
  cursor: pointer;
  position: relative;
  min-width: 860px;
}

.reg-row:hover {
  background: rgba(15, 118, 110, 0.02);
}

.reg-row:last-child {
  border-bottom: none;
}

.reg-row.expanded {
  background: rgba(15, 118, 110, 0.04);
}

.row-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--text3);
  padding-top: 2px;
}

.reg-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.45;
  padding-top: 1px;
}

.reg-desc {
  font-size: 11px;
  color: var(--text3);
  margin-top: 4px;
  line-height: 1.5;
}

.reg-trigger {
  font-size: 12px;
  color: var(--text2);
  font-family: 'JetBrains Mono', monospace;
  line-height: 1.6;
  padding-top: 2px;
}

.status-evidence-cell {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 14px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 700;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 0.4px;
  white-space: nowrap;
  align-self: start;
  margin-top: 1px;
}

.status-badge.on-track {
  background: var(--green-bg);
  border: 1px solid var(--green-border);
  color: var(--green);
}

.status-badge.at-risk {
  background: var(--amber-bg);
  border: 1px solid var(--amber-border);
  color: var(--amber);
}

.status-badge.off-track {
  background: var(--red-bg);
  border: 1px solid var(--red-border);
  color: var(--red);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.on-track .status-dot {
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
}

.at-risk .status-dot {
  background: var(--amber);
  box-shadow: 0 0 6px var(--amber);
}

.off-track .status-dot {
  background: var(--red);
  box-shadow: 0 0 6px var(--red);
}

.evidence-cell {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.6;
  padding-top: 2px;
}

.metric-kv {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--text3);
  margin-top: 6px;
}

.chevron {
  position: absolute;
  right: 16px;
  top: 16px;
  color: var(--text3);
  font-size: 11px;
  transition: transform 0.25s;
}

.reg-row.expanded .chevron {
  transform: rotate(180deg);
}

/* ---- EXPANDED ROW DETAIL ---- */
.row-detail {
  max-height: 0;
  overflow: hidden;
  grid-column: 1 / -1;
  transition: max-height 0.35s ease, padding 0.35s ease, opacity 0.25s ease;
  opacity: 0;
  padding: 0 64px 0 20px;
  margin: 0 -20px;
  background: rgba(15, 118, 110, 0.02);
  border-top: 0px solid var(--border);
}

.row-detail.open {
  max-height: 420px;
  opacity: 1;
  padding: 16px 64px 16px 20px;
  border-top-width: 1px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.detail-box {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}

.detail-box-title {
  font-size: 10px;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--text3);
  margin-bottom: 8px;
}

.detail-box-body {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.7;
}

/* ---- MODAL OVERLAY ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 32px;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  width: 100%;
  max-width: 800px;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.15);
  animation: modalIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(8px);
  }

  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

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

.modal-title {
  font-family: 'Fraunces', serif;
  font-size: 18px;
  color: var(--text);
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text2);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.modal-close:hover {
  color: var(--text);
  border-color: var(--border2);
  background: var(--surface2);
}

.modal-body {
  overflow-y: auto;
  padding: 20px 24px;
  flex: 1;
}

/* ---- DICTIONARY MODAL ---- */
.rules-group {
  margin-bottom: 24px;
}

.rules-group-title {
  font-size: 11px;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.rule-item {
  display: flex;
  align-items: start;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.rule-item:last-child {
  border-bottom: none;
}

.rule-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text3);
  width: 28px;
  flex-shrink: 0;
  padding-top: 2px;
}

.rule-content {
  flex: 1;
}

.rule-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.rule-meta {
  display: flex;
  gap: 8px;
  margin-top: 4px;
  align-items: center;
  flex-wrap: wrap;
}

.rule-cite {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text3);
}

.level-badge {
  font-size: 9px;
  padding: 2px 7px;
  border-radius: 4px;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  letter-spacing: 1px;
}

.level-badge.global {
  color: var(--accent);
  background: rgba(15, 118, 110, 0.08);
  border: 1px solid rgba(15, 118, 110, 0.18);
}

.level-badge.regional {
  color: var(--accent2);
  background: rgba(234, 88, 12, 0.08);
  border: 1px solid rgba(234, 88, 12, 0.18);
}

.level-badge.local {
  color: var(--green);
  background: rgba(22, 163, 74, 0.08);
  border: 1px solid rgba(22, 163, 74, 0.18);
}

.rule-trigger-text {
  font-size: 11px;
  color: var(--text3);
  margin-top: 4px;
  font-family: 'JetBrains Mono', monospace;
}

/* ---- LOADING STATE ---- */
.loading-state {
  text-align: center;
  padding: 80px 40px;
  color: var(--text3);
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

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

/* ---- RESPONSIVE ---- */
@media (max-width: 860px) {
  .header {
    padding: 0 16px;
  }

  .school-bar,
  .summary-strip,
  .filter-bar,
  .hierarchy-wrap,
  .main {
    padding-left: 16px;
    padding-right: 16px;
  }

  .stat-card {
    min-width: calc(50% - 12px);
  }

  .scope-shell {
    width: 100%;
  }
}
