/* ============================================================
   x3 LAB — light theme, lab only
   ============================================================ */

:root {
  /* surfaces */
  --plane:       #f5f5f1;
  --plane-2:     #eeeee9;
  --panel:       #ffffff;
  --panel-2:     #fbfbf9;
  --hair:        #e5e4de;
  --hair-2:      #d3d2ca;

  /* ink */
  --ink:         #17181c;
  --ink-2:       #55585f;
  --muted:       #8a8d94;

  /* series (validated, light surface) */
  --s-etf:       #2a78d6;   /* ETF x3 daily reset */
  --s-bh:        #eb6834;   /* buy & hold, borrowed */
  --s-fut:       #4a3aa7;   /* futures x3 */
  --s-fut2:      #1a936f;   /* futures, adjustable leverage */
  --s-idx:       #9a9d9f;   /* underlying 1x reference */

  --good:        #0a8f3c;
  --bad:         #d03b3b;

  /* type */
  --f-display: "Space Grotesk", system-ui, sans-serif;
  --f-body:    "IBM Plex Sans", system-ui, sans-serif;
  --f-mono:    "IBM Plex Mono", ui-monospace, monospace;

  --maxw: 1280px;
  --gap: clamp(16px, 3vw, 30px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--plane);
  color: var(--ink);
  font-family: var(--f-body);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection { background: rgba(42,120,214,0.20); }

.wrap { max-width: var(--maxw); margin: 0 auto; padding: 0 var(--gap); }
.swatch { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }

/* ---------- top bar ---------- */
.topbar {
  border-bottom: 1px solid var(--hair);
  background: var(--panel);
}
.topbar .wrap { display: flex; align-items: center; height: 58px; }
.brand { display: flex; align-items: baseline; gap: 12px; }
.brand .mark { font-family: var(--f-display); font-weight: 700; font-size: 19px; letter-spacing: -0.02em; }
.brand .mark b { color: var(--s-etf); }
.brand .sub { font-family: var(--f-mono); font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
@media (max-width: 640px){ .brand .sub { display: none; } }

/* ============================================================
   LAB LAYOUT
   ============================================================ */
.lab-wrap { padding-top: clamp(20px, 3vw, 34px); padding-bottom: 60px; }

.lab-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 20px;
  align-items: start;
}
@media (max-width: 880px){ .lab-grid { grid-template-columns: 1fr; } }

/* control rail */
.rail {
  background: var(--panel);
  border: 1px solid var(--hair); border-radius: 12px;
  padding: 18px; position: sticky; top: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
@media (max-width: 880px){ .rail { position: static; } }
.rail h3 {
  font-family: var(--f-mono); font-size: 11px; letter-spacing: 0.16em;
  text-transform: uppercase; color: var(--muted); margin: 0 0 13px;
  padding-bottom: 11px; border-bottom: 1px solid var(--hair);
}
.rail h3:not(:first-child){ margin-top: 24px; }

.regimes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sub-label { font-family: var(--f-mono); font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); margin: 12px 0 8px; }
.chip {
  font-family: var(--f-mono); font-size: 12px;
  padding: 9px 8px; border-radius: 8px; cursor: pointer; text-align: center;
  border: 1px solid var(--hair-2); background: var(--panel); color: var(--ink-2);
  transition: all .15s;
}
.chip:hover { color: var(--ink); border-color: var(--muted); }
.chip.active { background: rgba(42,120,214,0.10); border-color: var(--s-etf); color: var(--s-etf); font-weight: 600; }
.chip:focus-visible { outline: 2px solid var(--s-fut); outline-offset: 2px; }

.field { margin-top: 15px; }
.field .row { display: flex; justify-content: space-between; align-items: baseline; }
.field label, .inline-field label { font-family: var(--f-mono); font-size: 11.5px; letter-spacing: 0.03em; color: var(--ink-2); text-transform: uppercase; }
.field .num { font-family: var(--f-mono); font-size: 13px; color: var(--ink); font-variant-numeric: tabular-nums; }

input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 4px; margin-top: 9px;
  background: var(--hair-2); border-radius: 3px; outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: var(--s-etf); cursor: pointer; border: 2px solid var(--panel);
  box-shadow: 0 0 0 1px var(--s-etf);
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%; background: var(--s-etf);
  cursor: pointer; border: 2px solid var(--panel);
}
input[type="range"]:focus-visible { outline: 2px solid var(--s-fut); outline-offset: 4px; }

.inline-field { display: flex; justify-content: space-between; align-items: center; margin-top: 13px; }
.numin {
  width: 84px; text-align: right; font-family: var(--f-mono); font-size: 13px;
  background: var(--panel); border: 1px solid var(--hair-2); color: var(--ink);
  border-radius: 6px; padding: 6px 8px; font-variant-numeric: tabular-nums;
}
.numin:focus-visible { outline: 2px solid var(--s-fut); outline-offset: 1px; border-color: var(--s-etf); }

.rail-actions { margin-top: 22px; display: grid; gap: 9px; }
.btn {
  font-family: var(--f-mono); font-size: 12.5px; letter-spacing: 0.05em; text-transform: uppercase;
  padding: 11px 16px; border-radius: 9px; cursor: pointer;
  border: 1px solid var(--hair-2); background: var(--panel); color: var(--ink);
  transition: all .13s;
}
.btn:hover { border-color: var(--s-etf); }
.btn.primary { background: var(--s-etf); border-color: var(--s-etf); color: #fff; }
.btn.primary:hover { background: #2569bf; }
.btn:focus-visible { outline: 2px solid var(--s-fut); outline-offset: 2px; }

/* main column */
.lab-main { display: grid; gap: 18px; min-width: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--hair); border-radius: 12px; padding: 17px;
  min-width: 0; box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.panel-head .title { font-family: var(--f-display); font-weight: 600; font-size: 16px; letter-spacing: -0.01em; }
.panel-head .hint { font-family: var(--f-mono); font-size: 11px; color: var(--muted); letter-spacing: 0.03em; cursor: default; }
#scaleHint { cursor: pointer; }

/* legend */
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin: 4px 0 10px; }
.legend .item { display: flex; align-items: center; gap: 7px; font-family: var(--f-mono); font-size: 11.5px; color: var(--ink-2); }
.legend .line { width: 16px; height: 3px; border-radius: 2px; }
.legend .line.dash { background-image: linear-gradient(90deg, currentColor 60%, transparent 0); background-size: 6px 3px; }

/* stat strip */
.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); gap: 1px;
  background: var(--hair); border: 1px solid var(--hair); border-radius: 12px; overflow: hidden;
}
@media (max-width: 640px){ .stats { grid-template-columns: repeat(2,1fr); } }
.stat { background: var(--panel); padding: 14px 15px; }
.stat .k { font-family: var(--f-mono); font-size: 10.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.stat .v { font-family: var(--f-mono); font-size: 22px; font-weight: 600; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.stat .d { font-family: var(--f-mono); font-size: 11.5px; margin-top: 3px; color: var(--ink-2); }
.pos { color: var(--good); } .neg { color: var(--bad); }

/* divergence explanation */
.explain { color: var(--ink-2); font-size: 14.5px; line-height: 1.6; }
.explain p { margin: 6px 0 10px; }
.explain b { color: var(--ink); font-weight: 600; }
.explain ul { margin: 10px 0; padding-left: 18px; }
.explain li { margin: 8px 0; }
.explain li b { color: var(--ink); }
.explain .verdict-line { color: var(--muted); font-size: 13.5px; margin-bottom: 4px; }

.decomp {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px;
  background: var(--hair); border: 1px solid var(--hair); border-radius: 10px;
  overflow: hidden; margin-top: 14px;
}
@media (max-width: 640px){ .decomp { grid-template-columns: repeat(2, 1fr); } }
.decomp .d-cell { background: var(--panel-2); padding: 12px 13px; }
.decomp .d-k { font-family: var(--f-mono); font-size: 10px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); line-height: 1.3; min-height: 26px; }
.decomp .d-v { font-family: var(--f-mono); font-size: 18px; font-weight: 600; margin-top: 5px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.decomp .d-s { font-family: var(--f-mono); font-size: 10.5px; margin-top: 2px; color: var(--ink-2); }

#divergeVerdict { font-family: var(--f-mono); font-size: 11.5px; letter-spacing: 0.02em; padding: 3px 9px; border-radius: 20px; border: 1px solid var(--hair-2); }
#divergeVerdict.up { color: var(--good); border-color: rgba(10,143,60,0.35); background: rgba(10,143,60,0.07); }
#divergeVerdict.down { color: var(--bad); border-color: rgba(208,59,59,0.35); background: rgba(208,59,59,0.07); }

/* small multiples */
.sm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 720px){ .sm-grid { grid-template-columns: 1fr; } }

/* chart shell */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.tip {
  position: absolute; pointer-events: none; z-index: 20;
  background: var(--panel); border: 1px solid var(--hair-2);
  border-radius: 8px; padding: 9px 11px; font-family: var(--f-mono); font-size: 11.5px;
  color: var(--ink); min-width: 150px; opacity: 0; transition: opacity .1s;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,0.25); font-variant-numeric: tabular-nums;
}
.tip .th { color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; font-size: 10px; margin-bottom: 6px; }
.tip .tr { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 2px; }
.tip .tr .nm { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }

/* monte carlo */
.mc-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; flex-wrap: wrap; }
.mc-note { font-family: var(--f-mono); font-size: 11px; color: var(--muted); margin: 4px 0 0; }
.mc-empty { font-family: var(--f-mono); font-size: 12.5px; color: var(--muted); padding: 22px 4px; text-align: center; }
.mc-wrap { overflow-x: auto; margin-top: 14px; border: 1px solid var(--hair); border-radius: 10px; }
table.mc { border-collapse: collapse; width: 100%; font-family: var(--f-mono); font-size: 12.5px; }
table.mc th, table.mc td { padding: 9px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--hair); font-variant-numeric: tabular-nums; }
table.mc thead th { color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; font-size: 10px; background: var(--panel-2); }
table.mc td:first-child, table.mc th:first-child { text-align: left; }
table.mc tbody tr:last-child td { border-bottom: none; }
table.mc .strat { display: flex; align-items: center; gap: 8px; color: var(--ink); }
table.mc .med { font-weight: 600; color: var(--ink); }
table.mc .mn { color: var(--ink-2); }
table.mc tbody tr:hover td { background: rgba(42,120,214,0.04); }
.spin { opacity: 0.5; }

/* the tape */
.tape-wrap { overflow-x: auto; overflow-y: auto; max-height: 420px; border: 1px solid var(--hair); border-radius: 10px; }
table.tape { border-collapse: collapse; width: 100%; font-family: var(--f-mono); font-size: 12.5px; }
table.tape th, table.tape td { padding: 8px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--hair); font-variant-numeric: tabular-nums; }
table.tape th { color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.06em; font-size: 10.5px; position: sticky; top: 0; background: var(--panel); }
table.tape td:first-child, table.tape th:first-child { text-align: left; color: var(--muted); }
table.tape tr:hover td { background: rgba(42,120,214,0.05); }
table.tape td.editable { cursor: text; }
table.tape td.editable:focus { outline: 1px solid var(--s-etf); background: rgba(42,120,214,0.08); border-radius: 3px; }

@media (prefers-reduced-motion: no-preference){
  .draw-path { stroke-dasharray: var(--len); stroke-dashoffset: var(--len); animation: draw .9s ease forwards; }
  @keyframes draw { to { stroke-dashoffset: 0; } }
}
