/* ============================================================
   HTC Buyer Candidate Dashboard — JP Morgan-Inspired Design System
   ============================================================ */

/* --- CSS Custom Properties --- */
:root {
  /* Primary palette */
  --chase-blue: #117ACA;
  --navy: #004B87;
  --white: #FFFFFF;
  --bg-alt: #F7F8FA;
  --text-primary: #1A1A1A;
  --text-secondary: #4A5568;

  /* Semantic colors */
  --positive: #0A7C42;
  --negative: #C41230;
  --neutral: #6B7280;
  --border: #D1D5DB;
  --accent: #B8860B;

  /* Lighter tints for backgrounds */
  --positive-bg: #E6F5ED;
  --negative-bg: #FDE8EB;
  --accent-bg: #FDF6E3;
  --blue-bg: #E8F2FB;

  /* Spacing (4px grid) */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;
  --sp-10: 40px;
  --sp-12: 48px;
  --sp-16: 64px;

  /* Typography */
  --font-serif: Georgia, 'Times New Roman', serif;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', 'Liberation Mono', monospace;

  /* Shadows */
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-hover: 0 2px 8px rgba(0, 0, 0, 0.12);

  /* Transitions */
  --transition: 150ms ease;
}

/* --- Reset & Base --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--white);
  -webkit-font-smoothing: antialiased;
}

/* --- Typography --- */
h1, h2, h3 {
  font-family: var(--font-serif);
  font-weight: 700;
  line-height: 1.3;
  color: var(--navy);
}

h1 { font-size: 1.75rem; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }

.section-label {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--neutral);
}

.financial-figure {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* --- Layout --- */
.page-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--sp-6) var(--sp-6);
}

.section {
  padding: var(--sp-10) 0;
}

.section + .section {
  border-top: 1px solid var(--border);
}

.section-alt {
  background: var(--bg-alt);
}

/* --- Header (Home Page) --- */
.site-header {
  background: var(--navy);
  color: var(--white);
  padding: var(--sp-10) var(--sp-6);
}

.site-header .header-inner {
  max-width: 1200px;
  margin: 0 auto;
}

.site-header h1 {
  color: var(--white);
  font-size: 1.6rem;
  margin-bottom: var(--sp-1);
}

.site-header .subtitle {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.7);
  font-weight: 400;
}

.site-header .meta {
  font-family: var(--font-sans);
  font-size: 0.72rem;
  color: rgba(255, 255, 255, 0.5);
  margin-top: var(--sp-2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* --- Header (Deep Dive) --- */
.entity-header {
  background: var(--navy);
  color: var(--white);
  padding: var(--sp-8) var(--sp-6);
}

.entity-header .header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: var(--sp-4);
}

.entity-header h1 {
  color: var(--white);
  font-size: 1.5rem;
  margin-bottom: var(--sp-1);
}

.entity-header .entity-type {
  font-family: var(--font-sans);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, 0.6);
}

.entity-header .entity-meta {
  text-align: right;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.65);
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  font-family: var(--font-sans);
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.6);
  text-decoration: none;
  margin-bottom: var(--sp-3);
  transition: color var(--transition);
}

.back-link:hover {
  color: var(--white);
}

/* --- Tabs --- */
.tab-bar {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--sp-6);
  overflow-x: auto;
}

.tab-btn {
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 600;
  padding: var(--sp-3) var(--sp-5);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  color: var(--neutral);
  cursor: pointer;
  white-space: nowrap;
  transition: color var(--transition), border-color var(--transition);
}

.tab-btn:hover {
  color: var(--text-primary);
}

.tab-btn.active {
  color: var(--chase-blue);
  border-bottom-color: var(--chase-blue);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* --- Risk Flag Tooltips --- */
.risk-flags {
  display: inline-block;
}

.risk-dot-wrap {
  position: relative;
  cursor: help;
  padding: 2px;
}

.risk-dot-wrap:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 8px);
  right: -8px;
  width: 260px;
  padding: 8px 12px;
  background: var(--navy);
  color: var(--white);
  font-family: var(--font-sans);
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.5;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  white-space: pre-line;
  z-index: 100;
  pointer-events: none;
}

.risk-dot-wrap:hover::before {
  content: '';
  position: absolute;
  bottom: calc(100% + 2px);
  right: 2px;
  border: 6px solid transparent;
  border-top-color: var(--navy);
  z-index: 100;
  pointer-events: none;
}

/* --- Cards --- */
.card {
  background: var(--white);
  border-radius: 4px;
  padding: var(--sp-6);
  margin-bottom: var(--sp-5);
  box-shadow: var(--shadow-card);
}

.card-title {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--neutral);
  margin-bottom: var(--sp-4);
}

.card-warning {
  background: var(--accent-bg);
}

.card-danger {
  background: var(--negative-bg);
}

.card-positive {
  background: var(--positive-bg);
}

.card-info {
  background: var(--blue-bg);
}

/* --- KPI Metrics Grid --- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--sp-4);
}

.kpi {
  text-align: center;
  padding: var(--sp-5) var(--sp-3);
  background: var(--bg-alt);
  border-radius: 4px;
}

.kpi-value {
  font-family: var(--font-mono);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--navy);
  font-variant-numeric: tabular-nums;
}

.kpi-value.positive { color: var(--positive); }
.kpi-value.negative { color: var(--negative); }

.kpi-label {
  font-family: var(--font-sans);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--neutral);
  margin-top: var(--sp-1);
}

.kpi-delta {
  font-family: var(--font-sans);
  font-size: 0.72rem;
  margin-top: var(--sp-1);
}

.kpi-delta.up { color: var(--positive); }
.kpi-delta.down { color: var(--negative); }
.kpi-delta.flat { color: var(--neutral); }

/* --- Tables --- */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th {
  font-family: var(--font-sans);
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--neutral);
  text-align: left;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 2px solid var(--border);
}

td {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid #EBEDF0;
  vertical-align: top;
}

tr:last-child td {
  border-bottom: none;
}

.num {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.pos { color: var(--positive); font-weight: 600; }
.neg { color: var(--negative); }

.row-total td {
  font-weight: 700;
  border-top: 2px solid var(--border);
}

/* --- Sortable Table (Home Page) --- */
.comparison-table {
  width: 100%;
}

.comparison-table th {
  user-select: none;
  position: relative;
}

.comparison-table tbody tr {
  transition: background var(--transition);
}

.comparison-table tbody tr:hover {
  background: var(--bg-alt);
}

/* --- Drag-to-reorder --- */
.drag-handle {
  width: 24px;
  text-align: center;
  cursor: grab;
  color: var(--neutral);
  user-select: none;
  font-size: 1rem;
  line-height: 1;
}
.drag-handle:hover { color: var(--chase-blue); }
.comparison-table tbody tr.sortable-chosen { background: var(--blue-bg); }
.comparison-table tbody tr.sortable-ghost { opacity: 0.4; }

/* --- Ruled-out row treatment --- */
.comparison-table tbody tr.ruled-out td:not(.row-status) {
  opacity: 0.45;
}
.comparison-table tbody tr.ruled-out .entity-link,
.comparison-table tbody tr.ruled-out td:not(.row-status) span,
.comparison-table tbody tr.ruled-out td:not(.row-status) {
  text-decoration: line-through;
  text-decoration-thickness: 1px;
}
.row-status {
  width: 28px;
  text-align: center;
}
.status-toggle {
  cursor: pointer;
  background: none;
  border: none;
  padding: 4px 6px;
  font-size: 0.95rem;
  color: var(--neutral);
  border-radius: 3px;
  line-height: 1;
}
.status-toggle:hover { background: var(--bg-alt); color: var(--chase-blue); }
tr.ruled-out .status-toggle { color: var(--negative); }

/* Tooltip for ruled-out reason — reuses risk-dot pattern */
.ruled-out-tip { position: relative; cursor: help; }
.ruled-out-tip:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  width: 240px;
  padding: 8px 12px;
  background: var(--navy);
  color: var(--white);
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.5;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  white-space: pre-line;
  z-index: 100;
  pointer-events: none;
  text-decoration: none;
  opacity: 1;
}

/* --- Toolbar --- */
.toolbar {
  display: flex;
  gap: var(--sp-2);
  align-items: center;
  margin-bottom: var(--sp-3);
  font-size: 0.82rem;
}
.toolbar .dirty-indicator {
  margin-left: auto;
  color: var(--accent);
  font-style: italic;
}
.toolbar .dirty-indicator.clean { color: var(--neutral); }
.btn {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: var(--white);
  color: var(--text-primary);
  border-radius: 3px;
  cursor: pointer;
  transition: all var(--transition);
}
.btn:hover:not(:disabled) { border-color: var(--chase-blue); color: var(--chase-blue); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--chase-blue); color: var(--white); border-color: var(--chase-blue); }
.btn-primary:hover:not(:disabled) { background: var(--navy); border-color: var(--navy); color: var(--white); }

.comparison-table .entity-link {
  color: var(--chase-blue);
  text-decoration: none;
  font-weight: 600;
}

.comparison-table .entity-link:hover {
  text-decoration: underline;
}

/* --- Traffic Light / Risk Indicators --- */
.indicator {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: var(--sp-1);
  vertical-align: middle;
}

.indicator-green { background: var(--positive); }
.indicator-yellow { background: var(--accent); }
.indicator-red { background: var(--negative); }
.indicator-gray { background: var(--neutral); }

/* --- Trend Icons --- */
.trend {
  font-family: var(--font-sans);
  font-size: 0.78rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
}

.trend-growing { color: var(--positive); }
.trend-stable { color: var(--neutral); }
.trend-declining { color: var(--negative); }
.trend-volatile { color: var(--accent); }

/* --- Badge --- */
.badge {
  display: inline-block;
  font-family: var(--font-sans);
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 8px;
  border-radius: 3px;
  vertical-align: middle;
  margin-left: var(--sp-2);
}

.badge-new { background: var(--chase-blue); color: var(--white); }
.badge-watch { background: var(--accent); color: var(--white); }
.badge-alert { background: var(--negative); color: var(--white); }

/* --- Waterfall Chart (CSS-based) --- */
.waterfall {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}

.waterfall-row {
  display: flex;
  align-items: center;
}

.waterfall-label {
  width: 180px;
  font-size: 0.8rem;
  text-align: right;
  padding-right: var(--sp-3);
  flex-shrink: 0;
}

.waterfall-label.total {
  font-weight: 700;
}

.waterfall-track {
  flex: 1;
  height: 28px;
  background: var(--bg-alt);
  border-radius: 3px;
  position: relative;
  overflow: hidden;
}

.waterfall-fill {
  height: 100%;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--white);
  min-width: 40px;
}

.fill-revenue { background: var(--chase-blue); }
.fill-expense { background: var(--negative); opacity: 0.75; }
.fill-provision { background: var(--accent); }
.fill-net { background: var(--positive); }
.fill-net-negative { background: var(--negative); }

/* --- Verdict / Callout --- */
.verdict {
  background: var(--navy);
  color: var(--white);
  border-radius: 4px;
  padding: var(--sp-8);
  margin-top: var(--sp-6);
}

.verdict h2 {
  color: var(--accent);
  font-size: 1.15rem;
  margin-bottom: var(--sp-4);
}

.verdict p {
  margin-bottom: var(--sp-3);
  opacity: 0.92;
  font-size: 0.9rem;
  line-height: 1.65;
}

.verdict .callout {
  background: rgba(255, 255, 255, 0.08);
  border-left: 3px solid var(--accent);
  padding: var(--sp-3) var(--sp-4);
  margin: var(--sp-4) 0;
  border-radius: 0 4px 4px 0;
  font-size: 0.85rem;
}

/* --- Chart Grid --- */
.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-5);
  margin-bottom: var(--sp-5);
}

.chart-grid canvas {
  max-width: 100%;
}

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

/* --- Narrative Block --- */
.narrative {
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--text-secondary);
}

.narrative strong {
  color: var(--text-primary);
}

/* --- Source Note / Footer --- */
.source-note {
  font-family: var(--font-sans);
  font-size: 0.7rem;
  color: var(--neutral);
  text-align: center;
  margin-top: var(--sp-12);
  padding: var(--sp-6) 0;
  border-top: 1px solid var(--border);
  line-height: 1.7;
}

/* --- Utility --- */
.text-center { text-align: center; }
.text-right { text-align: right; }
.mt-2 { margin-top: var(--sp-2); }
.mt-4 { margin-top: var(--sp-4); }
.mt-6 { margin-top: var(--sp-6); }
.mb-4 { margin-bottom: var(--sp-4); }
.mb-6 { margin-bottom: var(--sp-6); }

/* --- Responsive --- */
@media (max-width: 768px) {
  .page-container {
    padding: var(--sp-4);
  }

  .site-header,
  .entity-header {
    padding: var(--sp-6) var(--sp-4);
  }

  .entity-header .header-inner {
    flex-direction: column;
  }

  .entity-header .entity-meta {
    text-align: left;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .tab-bar {
    padding: 0 var(--sp-4);
  }

  table {
    font-size: 0.78rem;
  }

  td, th {
    padding: var(--sp-2) var(--sp-3);
  }

  .waterfall-label {
    width: 120px;
    font-size: 0.72rem;
  }
}

/* --- Print Styles --- */
@media print {
  body {
    background: white;
    color: black;
    font-size: 11pt;
  }

  .site-header,
  .entity-header {
    background: white !important;
    color: black !important;
    border-bottom: 2px solid black;
    padding: 12pt 0;
  }

  .site-header h1,
  .entity-header h1,
  .site-header .subtitle,
  .entity-header .entity-type {
    color: black !important;
  }

  .tab-bar,
  .back-link {
    display: none !important;
  }

  .tab-panel {
    display: block !important;
    page-break-inside: avoid;
  }

  .card {
    box-shadow: none;
    border: 1px solid #ccc;
    page-break-inside: avoid;
  }

  .verdict {
    background: white !important;
    color: black !important;
    border: 2px solid var(--navy);
  }

  .verdict h2 {
    color: var(--navy) !important;
  }

  .verdict .callout {
    background: #f5f5f5 !important;
  }

  .chart-grid {
    grid-template-columns: 1fr 1fr;
  }

  canvas {
    max-width: 100% !important;
    max-height: 200pt !important;
  }

  .source-note {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    font-size: 8pt;
    border-top: 1px solid #ccc;
    padding: 6pt 0;
    background: white;
  }

  a { color: black; text-decoration: none; }
  .indicator { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .pos, .neg { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
}
