:root {
  --bg: #0f0f1a;
  --bg-panel: #1a1a2e;
  --bg-card: #16213e;
  --border: #2a2a4a;
  --text: #cdd6f4;
  --text-muted: #6c7086;
  --text-dim: #45475a;
  --accent: #89b4fa;
  --principal: #89dceb;
  --growth: #a6e3a1;
  --contrib: #cba6f7;
  --warning: #f9e2af;
  --success: #a6e3a1;
  --error: #f38ba8;
  --radius: 8px;
  --transition: 0.22s ease;
  --topbar-h: 52px;
  --col-left-w: min(360px, 100%);
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== TOP BAR & HEADER LEDGER ===== */
#top-bar {
  min-height: var(--topbar-h);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  flex-wrap: wrap;
  z-index: 50;
  flex-shrink: 0;
}

#top-bar-left { flex-shrink: 0; }
#app-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
}

#header-ledger {
  display: flex;
  flex: 1;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  min-width: 0;
}

.ledger-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 88px;
}

.ledger-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.ledger-value {
  font-size: 14px;
  font-weight: 700;
  font-family: ui-monospace, monospace;
  color: var(--growth);
}

#top-bar-right {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

#top-bar-right button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition), background var(--transition);
}

#top-bar-right button:hover,
#top-bar-right button.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(137, 180, 250, 0.08);
}

/* ===== LAYOUT ===== */
#app {
  display: grid;
  grid-template-columns: var(--col-left-w) 1fr;
  flex: 1;
  min-height: 0;
}

#col-left {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

#col-center {
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 10px;
  gap: 8px;
}

h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

/* ===== HELP PANEL ===== */
#help-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}

#help-text {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-muted);
}

/* ===== INPUTS ===== */
.input-group {
  margin-bottom: 10px;
}

.input-group label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.input-group .var {
  color: var(--accent);
  font-weight: 700;
}

.optional {
  font-size: 10px;
  color: var(--text-dim);
}

.input-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

.input-row input,
.input-row select {
  flex: 1;
  min-width: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
}

.input-row input:focus,
.input-row select:focus {
  outline: none;
  border-color: var(--accent);
}

.input-prefix,
.input-suffix {
  color: var(--text-muted);
  font-size: 13px;
  flex-shrink: 0;
}

/* ===== FORMULAS ===== */
#formula-panel,
#inputs-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}

.formula-block {
  margin-bottom: 12px;
}

.formula-block:last-child { margin-bottom: 0; }

.formula-title {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.formula-math {
  overflow-x: auto;
  padding: 6px 0;
  font-size: 15px;
}

/* ===== CHART ===== */
#chart-wrap {
  flex: 1;
  min-height: 220px;
  position: relative;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

#growth-chart {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 220px;
}

#chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(15, 15, 26, 0.92);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  font-family: ui-monospace, monospace;
  white-space: nowrap;
  z-index: 10;
}

#chart-legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 11px;
  color: var(--text-muted);
  justify-content: center;
}

.legend-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: middle;
}

.legend-swatch.principal { background: var(--principal); }
.legend-swatch.contributions { background: var(--contrib); }
.legend-swatch.total { background: var(--growth); }

.hidden { display: none !important; }

/* ===== RESPONSIVE ===== */
@media (max-width: 860px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  #col-left {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 45vh;
  }

  #col-center {
    min-height: 280px;
  }
}

@media (max-width: 480px) {
  #header-ledger {
    width: 100%;
    order: 3;
  }

  .ledger-item {
    flex: 1;
    min-width: calc(50% - 6px);
  }

  .input-row {
    flex-wrap: wrap;
  }
}
