:root {
  --c-blue: #2563eb;       --c-blue-hover: #1d4ed8;
  --c-green: #16a34a;      --c-green-hover: #15803d;
  --c-yellow: #ca8a04;     --c-yellow-hover: #a16207;
  --c-red: #dc2626;        --c-red-hover: #b91c1c;
  --c-dark: #1e293b;
  --c-text: #334155;
  --c-muted: #64748b;
  --c-border: #e2e8f0;
  --c-bg: #f8fafc;
  --c-surface: #ffffff;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: var(--c-bg); color: var(--c-text); }
.header { background: var(--c-dark); color: #fff; padding: 16px 24px; display: flex; align-items: center; justify-content: space-between; }
.header h1 { font-size: 20px; font-weight: 600; }
.csv-menu { position: relative; }
.csv-menu-btn { background: rgba(255,255,255,0.15); color: #fff; border: none; padding: 6px 14px; border-radius: 4px; cursor: pointer; font-size: 13px; }
.csv-menu-btn:hover { background: rgba(255,255,255,0.25); }
.csv-menu-drop { display: none; position: absolute; right: 0; top: calc(100% + 6px); background: var(--c-surface); border: 1px solid var(--c-border); border-radius: 6px; box-shadow: 0 4px 16px rgba(0,0,0,0.15); padding: 16px; min-width: 340px; z-index: 2000; color: var(--c-text); font-size: 13px; line-height: 1.5; }
.csv-menu-drop.open { display: block; }
.csv-menu-drop h4 { margin: 0 0 8px; font-size: 14px; }
.csv-menu-drop code { background: var(--c-bg); padding: 2px 5px; border-radius: 3px; font-size: 12px; }
.csv-menu-drop .csv-example { background: var(--c-bg); padding: 8px 10px; border-radius: 4px; font-family: monospace; font-size: 12px; margin: 8px 0; white-space: pre; }
.csv-upload-label { display: inline-block; background: var(--c-blue); color: #fff; padding: 7px 14px; border-radius: 4px; cursor: pointer; font-size: 13px; margin-top: 8px; }
.csv-upload-label:hover { background: var(--c-blue-hover); }
.csv-loaded-list { margin-top: 10px; font-size: 12px; color: var(--c-muted); }
.csv-loaded-list > span { display: block; }
.tabs { display: flex; background: var(--c-surface); padding: 8px 16px; gap: 6px; border-bottom: 1px solid var(--c-border); }
.tab { padding: 8px 16px; color: var(--c-text); cursor: pointer; border: none; border-radius: 4px; font-size: 14px; background: #e8e8e8; transition: background 0.2s; text-decoration: none; }
.tab:hover { background: var(--c-border); }
.tab.active { background: var(--c-blue); color: #fff; box-shadow: 0 2px 0 0 var(--c-blue); }
.content { max-width: 1200px; margin: 24px auto; padding: 0 24px; }
.panel { display: none; }
.panel.active { display: block; }
.card { background: var(--c-surface); border-radius: 8px; padding: 20px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); overflow-x: auto; }
.card h3 { margin-bottom: 12px; font-size: 16px; color: var(--c-dark); }
.card h4 { margin: 10px 0 6px; font-size: 14px; }
.form-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; min-width: 0; }
.form-row input, .form-row select { padding: 6px 10px; border: 1px solid var(--c-border); border-radius: 4px; font-size: 14px; }
.form-row input[type="number"] { width: 80px; }
.form-row select { min-width: 150px; }
.ac-wrapper { position: relative; display: inline-block; flex: 1; min-width: 0; }
.ac-input { width: 100%; min-width: 0; padding: 8px 12px; border: 1px solid var(--c-border); border-radius: 4px; font-size: 14px; box-sizing: border-box; }
.ac-dropdown { position: absolute; top: 100%; left: 0; min-width: 100%; max-height: 200px; overflow-y: auto;
  background: var(--c-surface); border: 1px solid var(--c-border); border-top: none; border-radius: 0 0 4px 4px;
  z-index: 1000; box-shadow: 0 4px 8px rgba(0,0,0,0.1); display: none; }
.ac-option { padding: 6px 10px; cursor: pointer; font-size: 14px; }
.ac-option:hover, .ac-option.active { background: #f0f0f0; }
.ac-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #999;
  font-size: 16px;
  padding: 0;
  width: 20px;
  height: 20px;
  display: none;
  z-index: 5;
}
.ac-clear:hover { color: var(--c-muted); }
.ac-clear.show { display: block; }
.weight-sum { font-size: 13px; font-weight: 600; padding: 2px 0; border-radius: 3px; width: 80px; text-align: center; display: inline-block; white-space: nowrap; box-sizing: border-box; }
.weight-sum.ok { color: var(--c-green); }
.weight-sum.warn { color: var(--c-yellow); }
.weight-sum.error-text { color: var(--c-red); }
.leverage-input { width: 54px; font-size: 13px; font-weight: 600; border: 1px solid var(--c-border); border-radius: 3px; padding: 0 2px; text-align: center; color: inherit; background: transparent; }
.btn { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background 0.2s; }
.btn-primary { background: var(--c-blue); color: #fff; }
.btn-primary:hover { background: var(--c-blue-hover); }
@keyframes btn-pulse { 0%{box-shadow:0 0 0 0 rgba(59,130,246,0.5)} 70%{box-shadow:0 0 0 8px rgba(59,130,246,0)} 100%{box-shadow:0 0 0 0 rgba(59,130,246,0)} }
.btn-stale { animation: btn-pulse 0.6s ease-out; box-shadow: 0 0 0 2px rgba(59,130,246,0.4); }
.btn-faded { opacity: 0.45; }
.btn-secondary { background: #e8e8e8; color: var(--c-text); }
.btn-secondary:hover { background: var(--c-border); }
.btn-danger { background: var(--c-red); color: #fff; }
.btn-danger:hover { background: var(--c-red-hover); }
.btn-success { background: var(--c-green); color: #fff; }
.btn-success:hover { background: var(--c-green-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-lock { background: none; border: 1px solid transparent; border-radius: 4px; cursor: pointer; font-size: 12px; padding: 2px 0; color: #bbb; line-height: 1; transition: all 0.15s; filter: grayscale(1); opacity: 0.5; width: 26px; text-align: center; box-sizing: border-box; }
.btn-lock:hover { color: #888; opacity: 0.8; }
.btn-lock.locked { border-color: var(--c-yellow); color: var(--c-yellow); filter: none; opacity: 1; }
.chart-container { position: relative; height: 400px; margin-top: 16px; display: flex; flex-direction: column; }
.chart-container canvas { flex: 1; min-height: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 12px; table-layout: fixed; }
th, td { padding: 8px 12px; text-align: right; border-bottom: 1px solid var(--c-border); font-size: 14px; overflow-wrap: break-word; word-break: break-word; }
th { font-weight: 600; color: var(--c-dark); background: var(--c-bg); }
th:first-child, td:first-child { text-align: left; }
.metrics-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 16px; }
.metric-card { background: var(--c-bg); padding: 12px; border-radius: 6px; text-align: center; }
.metric-card .value { font-size: 24px; font-weight: 700; color: var(--c-dark); }
.metric-card .label { font-size: 12px; color: var(--c-muted); margin-top: 4px; }
.optimal-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; margin-bottom: 16px; }
.optimal-card { background: var(--c-bg); padding: 16px; border-radius: 6px; border-left: 4px solid var(--c-blue); }
.optimal-card h4 { margin-bottom: 8px; color: var(--c-dark); }
.optimal-card .weights { font-size: 13px; color: var(--c-muted); margin-bottom: 6px; }
.optimal-card .stats { font-size: 13px; }
.optimal-card .stats span { margin-right: 12px; }
.error { color: var(--c-red); padding: 12px; background: #fef2f2; border-radius: 4px; margin-top: 8px; }
.dual-range { position: relative; height: 20px; flex: 1; min-width: 100px; }
.dual-range-track { position: absolute; left: 12px; right: 12px; top: 50%; height: 4px; transform: translateY(-50%); border-radius: 2px; background: var(--c-border); z-index: 0; pointer-events: none; }
.dual-range-fill { position: absolute; top: 0; height: 100%; background: var(--c-blue); border-radius: 2px; }
.dual-range input[type="range"] { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 0; pointer-events: none; -webkit-appearance: none; appearance: none; background: transparent; z-index: 2; }
.dual-range input[type="range"]::-webkit-slider-thumb { pointer-events: all; -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--c-blue); cursor: pointer; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3); margin-top: -10px; }
.dual-range input[type="range"]::-moz-range-thumb { pointer-events: all; width: 16px; height: 16px; border-radius: 50%; background: var(--c-blue); cursor: pointer; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.dual-range input[type="range"]::-webkit-slider-runnable-track { height: 0; background: transparent; }
.dual-range input[type="range"]::-moz-range-track { height: 0; background: transparent; }
.portfolio-container { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 4px; }
.portfolio-group { border: 1px solid var(--c-border); border-radius: 8px; padding: 14px; background: var(--c-surface); box-shadow: 0 1px 3px rgba(0,0,0,0.1); flex: 0 0 auto; width: 420px; min-width: 320px; overflow: visible; }
#margin-portfolios .portfolio-group { width: auto; min-width: 420px; }
#rebalance-portfolios .portfolio-group { width: auto; min-width: 720px; }
#rebalance-portfolios .portfolio-group .ac-wrapper { min-width: 150px; }
.rf-controls { display: contents; font-size: 12px; color: var(--c-muted); }
.rf-controls select, .rf-controls input { font-size: 12px; padding: 3px 6px; border: 1px solid var(--c-border); border-radius: 4px; }
.rf-controls input[type="number"] { width: 60px; }
.sub-group-wrapper { display: flex; flex-wrap: wrap; width: 100%; }
.sub-group { border-left: 3px solid var(--c-blue); background: var(--c-bg); padding: 4px 6px; border-radius: 4px; width: calc(100% - 12px); margin-top: 4px; margin-left: 12px; }
.sub-group-wrapper .sub-group-title { font-size: 13px; font-weight: 600; background: none; border: 1px solid transparent; border-radius: 4px; padding: 6px 10px; flex: 1; min-width: 0; }
.sub-group-wrapper .sub-group-title:hover, .sub-group-wrapper .sub-group-title:focus { border-color: var(--c-border); background: var(--c-surface); outline: none; }
.sub-group-controls { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.sub-group.collapsed .sub-group-rows, .sub-group.collapsed .sub-group-controls { display: none; }
.btn-collapse { background: none; border: none; cursor: pointer; font-size: 12px; padding: 2px 4px; color: var(--c-muted); }
.btn-clear-title { background: none; border: none; cursor: pointer; font-size: 14px; padding: 2px 4px; color: var(--c-muted); display: none; }
.btn-clear-title:hover { color: var(--c-red); }
.portfolio-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.portfolio-title { font-size: 14px; font-weight: 600; color: var(--c-dark); background: none; border: 1px solid transparent; border-radius: 4px; padding: 3px 6px; flex: 1; min-width: 0; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
.portfolio-title:hover, .portfolio-title:focus { border-color: var(--c-border); background: var(--c-surface); outline: none; }
.loading { text-align: center; padding: 40px; color: var(--c-muted); }
.progress-bar { background: var(--c-border); border-radius: 4px; height: 8px; max-width: 400px; flex: 1; }
.progress-bar-fill { background: var(--c-blue); height: 100%; border-radius: 4px; transition: width 0.15s; }
.progress-row { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 12px auto; max-width: 430px; }
.progress-cancel { cursor: pointer; color: var(--c-muted); font-size: 18px; line-height: 1; user-select: none; }
.progress-cancel:hover { color: var(--c-red); }
.cancelled-notice { color: var(--c-red); font-size: 13px; padding: 40px; text-align: center; }
.heatmap-table td { text-align: center; font-weight: 600; }
/* Per-currency colored row/col labels — JS sets --ccy-color inline.
   Goes through CSS so dark mode (or a future palette swap) can
   redefine colors centrally instead of touching every label. */
.heatmap-table .heatmap-ccy-label { color: var(--ccy-color); }
.text-inception { color: var(--c-yellow); }

/* Weights Modal */
.modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); }
.modal-content { background-color: var(--c-surface); margin: 8% auto; padding: 20px; border-radius: 8px; width: 90%; max-width: 600px; position: relative; }
.close-btn { position: absolute; right: 15px; top: 10px; font-size: 24px; font-weight: bold; cursor: pointer; color: #aaa; }
.close-btn:hover { color: var(--c-text); }
#weights-content { margin-top: 15px; display: flex; gap: 20px; align-items: flex-start; }
#weights-pie-wrap { flex: 0 0 180px; }
#weights-pie-wrap canvas { width: 180px !important; height: 180px !important; }
#weights-details { flex: 1; }
#weights-details .w-row { margin: 6px 0; padding: 5px 10px; background: var(--c-bg); border-radius: 4px; display: flex; justify-content: space-between; font-size: 14px; }
#weights-details .w-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
#weights-metrics { margin-top: 12px; padding-top: 8px; border-top: 1px solid var(--c-border); font-size: 13px; display: flex; gap: 12px; flex-wrap: wrap; }
#weights-metrics span { color: #555; }
.date-input-wrap { position:relative; display:inline-block }
.date-input-wrap input { padding-right:18px; width:120px; max-width:100% }
.date-clear { position:absolute; right:6px; top:50%; transform:translateY(-50%); background:none; border:none; cursor:pointer; color:#999; font-size:14px; padding:0; line-height:1 }
.date-clear:hover { color:var(--c-red) }

/* Utility classes (extracted from inline styles in index.html). */
.tl { text-align: left; }
.hidden { display: none; }
.nowrap { white-space: nowrap; }
.row-wrap-tight { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 4px; }
.form-row.tight { margin-bottom: 0; }
/* Hedge/frontier slider row: <label class="slider-row"><span class="slider-label">…</span><input>…</label> */
.slider-row { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; width: 100%; }
.slider-row input[type="number"] { width: 80px; }
.slider-label { display: inline-block; width: 78px; text-align: right; flex-shrink: 0; }
.slider-input { accent-color: var(--c-blue); flex: 1; min-width: 80px; }
.unit-suffix { display: inline-block; width: 30px; font-size: 12px; color: var(--c-muted); }
.unit-suffix-w { display: inline-block; width: 38px; font-size: 12px; color: var(--c-muted); }
.btn-xs { padding: 2px 8px; }
.btn-xxs { font-size: 11px; padding: 2px 8px; }
.btn-row { text-align: right; margin-top: 4px; }
.btn-bar { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.help-text { color: var(--c-muted); font-size: 13px; line-height: 1.5; margin-top: 10px; }
.help-text-lg { color: var(--c-muted); font-size: 14px; line-height: 1.6; margin-bottom: 10px; }
.help-text-top { color: var(--c-muted); font-size: 14px; line-height: 1.6; margin: 10px 0; }
.card h3 a { color: inherit; text-decoration: none; border-bottom: 1px dashed var(--c-border); }
.metrics-grid.compact .metric-card .value { font-size: 16px; }
#holdings-results { display: flex; flex-direction: column; gap: 24px; }
#holdings-results > .card { margin-bottom: 0; }
.holdings-summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-bottom: 0; }
.holdings-summary-card { margin-bottom: 0; }
.card.holdings-chart-card { overflow: hidden; }
.holdings-chart-wrap { position: relative; min-height: 360px; width: 100%; max-width: 100%; overflow: hidden; }
.holdings-chart-card .chart-legend { max-width: 100%; overflow: hidden; }
#holdings-category-chart { display: block; width: 100% !important; max-width: 100%; }
.metric-grid.compact { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; margin-top: 10px; }
.metric-grid.compact span { display: block; color: var(--c-muted); font-size: 12px; }
.metric-grid.compact strong { display: block; margin-top: 2px; font-size: 15px; font-variant-numeric: tabular-nums; }
.text-muted-sm { font-size: 12px; color: var(--c-muted); }
.intro-lang-btn { font-size: 18px; padding: 4px 10px; border: 2px solid var(--c-border); border-radius: 6px; background: white; }
.results-wrap { margin-top: 16px; }
.inline-row { display: inline-flex; align-items: center; gap: 6px; }
.side-stack { display: flex; flex-direction: column; gap: 8px; }
.slider-label.wide { width: 120px; }

/* Responsive: tablet & mobile */
@media (max-width: 768px) {
  .header { padding: 12px 16px; }
  .header h1 { font-size: 16px; }
  .csv-menu-drop { min-width: auto; left: 0; right: auto; max-width: calc(100vw - 32px); }
  .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 6px 8px; gap: 4px; }
  .tab { white-space: nowrap; font-size: 13px; padding: 8px 12px; }
  .content { padding: 0 10px; }
  .card { padding: 14px; }
  .portfolio-container { gap: 8px; }
  .portfolio-group { width: 100% !important; min-width: 0 !important; }
  #margin-portfolios .portfolio-group { width: 100% !important; min-width: 0 !important; }
  .form-row { flex-wrap: wrap; gap: 4px; }
  .form-row input[type="number"] { width: 60px; }
  .sub-group { margin-left: 6px; padding: 2px 4px; }
  .btn-lock { width: 20px; font-size: 10px; }
  .date-input-wrap input { width: 100%; }
  .date-input-wrap { width: 100%; }
  input[type="range"] { width: 100% !important; }
  .chart-container { height: 280px; }
  .modal-content { width: 96%; max-width: none; margin: 4% auto; max-height: 90vh; overflow-y: auto; }
  #weights-content { flex-direction: column; align-items: center; }
  #weights-pie-wrap { flex: 0 0 auto; }
  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .heatmap-table { min-width: 420px; }
}
/* --- Currency tab ----------------------------------------------------- */
/* All currency-tab one-off styles concentrated here so the JS stays
   structural. Per-currency colors are passed via the --ccy-color CSS
   custom property — JS sets it inline; CSS picks it up via var(--ccy-color). */

.ccy-pair-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 6px; }
.ccy-mc-x-row { text-align: center; margin-top: 4px; }
.ccy-mc-x-row label { margin-right: 12px; cursor: pointer; }
.ccy-mc-x-row label:last-child { margin-right: 0; }

/* The dual-A/B dropdowns above the pair-FX chart. JS sets style="--ccy-color:#..."
   per element; everything else is class-driven. */
.currency-pair-select {
  font-size: 13px;
  font-weight: 600;
  padding: 3px 22px 3px 8px;
  border-radius: 4px;
  border: 1.5px solid var(--ccy-color, var(--c-border));
  color: var(--ccy-color, var(--c-text));
  background: #fff;
  cursor: pointer;
  min-width: 70px;
}

/* Ranking table: shrink-to-content so the Composition column doesn't
   absorb leftover width and create a visual gap before the numerics. */
.data-table { width: auto; }
/* Weights column in the ranking table — one row per currency. */
.ccy-weights-cell { min-width: 120px; vertical-align: top; text-align: left; }
.ccy-weight-row { white-space: nowrap; line-height: 1.4; font-variant-numeric: tabular-nums; text-align: left; }
.ccy-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ccy-color, #888); margin-right: 6px; vertical-align: middle; }
.ccy-code { display: inline-block; width: 32px; text-align: left; }
.ccy-pct  { display: inline-block; min-width: 42px; text-align: right; }

/* Currency-detail modal (clicked from MC scatter). Reuses the shared
   .modal + .modal-content + .close-btn scaffold from above; only the
   internal layout helpers are bespoke. */
.currency-detail-pie-wrap { margin-top: 14px; display: flex; justify-content: center; }
.currency-detail-actions { margin-top: 14px; display: flex; gap: 8px; justify-content: flex-end; }

/* Heatmap cells that are click-to-set the FX pair. */
.heatmap-table td.clickable { cursor: pointer; }

@media (max-width: 480px) {
  .metrics-grid { grid-template-columns: 1fr; }
  .optimal-cards { grid-template-columns: 1fr; }
  .header h1 { font-size: 14px; }
}
