:root{
  --bg: #0f1218;
  --panel: #141a22;
  --text: #e5eef8;
  --muted: #94a3b8;
  --accent: #5b9cff;
  --accent-2: #9b59b6;
  --bar: #2b87ff;
  --bar-hover: #5aa3ff;
  --border: #263241;
  --danger: #ff6b6b;
  --btn-h: 34px;
  --hover-bg: #1c2430;
}

body.light{
  --bg: #ffffff;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #475569;
  --accent: #2563eb;
  --accent-2: #7c3aed;
  --bar: #3b82f6;
  --bar-hover: #60a5fa;
  --border: #e2e8f0;
  --danger: #ef4444;
  --hover-bg: #f1f5f9;
}

*{ box-sizing: border-box; }
html, body { height: 100%; overflow-x: hidden; }
body{
  margin:0; font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text); background: var(--bg);
}

#app{ height: 100%; position: relative; }
main#main{ padding: 12px 16px 16px 16px; height: 100%; display:flex; flex-direction: column; }

h1{ font-size: 18px; margin: 0 0 12px; }
.control-group{ margin-bottom: 14px; }
.control-group.compact{ display: flex; gap: 8px; }
.row{ display:flex; align-items:center; gap:10px; }
.row.space-between{ justify-content: space-between; }

label{ font-weight: 600; font-size: 12px; color: var(--muted); display:block; margin-bottom:4px; }
label.radio{ font-weight: 500; display:flex; align-items:center; gap:6px; margin-right: 12px; }

select, input[type="text"]{
  width:100%; background: var(--bg); color:var(--text); border:1px solid var(--border); border-radius:8px; padding:8px 10px; outline:none;
}
select:focus, input[type="text"]:focus{ border-color: var(--accent); }

.stage-list{ border:1px solid var(--border); border-radius:8px; background: var(--bg); max-height: none; overflow:auto; padding:6px; flex: 1 1 auto; }
.stage-item{ display:flex; align-items:center; justify-content: space-between; padding:6px 8px; border-radius:6px; cursor: pointer; user-select: none; }
.stage-item:hover{ background: var(--hover-bg); }
.stage-item .left{ display:flex; align-items:center; gap:8px; }
.stage-item .marker{ width: 10px; height: 10px; border-radius: 50%; border:1px solid var(--border); display:inline-block; }
.stage-item.selected .marker{ background: var(--accent); border-color: var(--accent); }
.stage-value{ color: var(--muted); font-size: 12px; }
#stagePanel.collapsed{ display:none; }

.btn{ background: var(--accent); color:white; border:none; border-radius:8px; padding:8px 10px; cursor:pointer; font-weight:600; }
.btn.secondary{ background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn:disabled{ opacity:.6; cursor:not-allowed; }

.muted{ color: var(--muted); }
.small{ font-size: 12px; }

#chartHeader{ margin-bottom: 12px; min-height: var(--btn-h); display:flex; align-items:center; justify-content: space-between; position: relative; }
#chartHeader .panel-wrap{ position: relative; }
.text-btn{ background: transparent; border:1px solid var(--border); color: var(--text); padding: 0 10px; border-radius: 8px; cursor:pointer; font-weight:600; display:inline-flex; align-items:center; height: var(--btn-h); }
.text-btn .chev{ display:inline-flex; margin-left: 6px; color: var(--muted); }
.text-btn .chev svg{ display:block; width: 12px; height: 6px; transition: transform .16s ease, color .16s ease; }
.text-btn[aria-expanded="true"]{ color: var(--accent); border-color: var(--accent); }
.text-btn[aria-expanded="true"] .chev{ color: var(--accent); }
.text-btn[aria-expanded="true"] .chev svg{ transform: rotate(180deg); }
.dropdown-panel{ position: absolute; top: calc(100% + 6px); right: 0; left: auto; width: 320px; max-height: 70vh; background: var(--panel); border:1px solid var(--border); border-radius: 10px; padding: 10px; box-shadow: 0 10px 20px rgba(0,0,0,.35); z-index: 1000; opacity: 0; transform: translateY(-4px); visibility: hidden; pointer-events: none; transition: opacity .16s ease, transform .16s ease, visibility 0s linear .16s; display:flex; flex-direction: column; }
.dropdown-panel.open{ opacity: 1; transform: translateY(0); visibility: visible; pointer-events: auto; transition: opacity .16s ease, transform .16s ease; }

.legend-wrap{
  flex: 1 1 auto;
  min-width: 0;
  max-height: 44px; /* ~2 ряда по 20px + зазор */
  overflow-y: scroll;
  padding-right: 8px;
}
.legend{
  display:flex;
  flex-wrap:wrap;
  align-items:center;
  gap: 6px 12px;
  margin-bottom: 0;
  font-size: 11px;
}
.legend.hidden{ display:none; }
.legend-item{
  display:inline-flex;
  align-items:center;
  gap:6px;
  border-radius: 999px;
  border:1px solid var(--border);
  background: var(--panel);
  padding:2px 8px;
  cursor:pointer;
  font-size: 11px;
  color: var(--text);
}
.legend-item:hover{
  background: var(--hover-bg);
}
.legend-item.selected{
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.4);
}
.legend-swatch{
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.legend-label{
  white-space: nowrap;
}

.header-actions{
  flex-shrink: 0;
}

#chart{ flex:1 1 auto; min-height:0; overflow:hidden; padding: 12px 6px 12px 6px; position: relative; }
.chart-vertical{ display:flex; flex-direction: row; align-items: flex-end; gap:18px; }
.chart-horizontal{ display:block; }
/* Horizontal grid: three columns shared across rows */
#chart.chart-horizontal{ display:flex; flex-direction: column; gap: 16px; }

/* Zero line for vertical chart */
#chart.chart-vertical::after{
  content: "";
  position: absolute;
  left: 6px;
  right: 6px;
  bottom: 34px;
  height: 1px;
  background: #E4E4E7;
  pointer-events: none;
}

/* Conversion arrows strip */
.conv-strip{ margin-top: 6px; }
.conv-strip.hidden{ display:none; }
.conv-strip{ display: grid; grid-template-columns: repeat(var(--pairs, 1), 1fr); column-gap: 12px; align-items: center; }
.conv-item{ position: relative; height: 14px; }
.conv-item .line{ position:absolute; left:0; right:0; top:50%; height: 2px; background: var(--border); transform: translateY(-50%); }
.conv-item .line::after{ content: ""; position:absolute; right:0; top:50%; transform: translateY(-50%); border-left:6px solid var(--border); border-top:4px solid transparent; border-bottom:4px solid transparent; }
.conv-item .pct{ position:absolute; top:-10px; left:50%; transform: translateX(-50%); font-size: 11px; color: var(--muted); background: transparent; padding: 0 4px; white-space: nowrap; }

/* Header icon button */
.icon-btn{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:6px;
  width: var(--btn-h);
  height: var(--btn-h);
  padding: 0;
  border-radius:8px;
  border:1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor:pointer;
  margin-right: 0;
  transition: transform .08s ease, box-shadow .08s ease, background-color .08s ease, border-color .08s ease, color .08s ease;
}
.icon-btn:hover{ border-color: var(--accent); color: var(--accent); }
.icon-btn:active{
  transform: translateY(1px) scale(0.96);
  background: var(--hover-bg);
  box-shadow: none;
}
.icon-btn img{ width: 20px; height: 20px; object-fit: contain; display:block; }
#toggleView.icon-btn{ padding: 4px; }

/* Vertical bars layout */
.bar-col{
  display:flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 0 2px;
  flex: 1 1 0;
  min-width: 0;
}
.bar-col.drag-over{ outline:1px dashed var(--accent-2); }
.bar-outer{
  width: 100%;
  height: 100%;
  display:flex;
  align-items: flex-end;
  justify-content: center;
  background: transparent;
  padding: 0;
}
.bar{
  width: 100%;
  min-height: 6px;
  background: linear-gradient(180deg, var(--bar), var(--bar-hover));
  border-radius: 4px 4px 0 0;
  transition: height .15s ease;
}
.bar-group{
  display:flex;
  align-items:flex-end;
  justify-content:center;
  gap: 3px;
  width: 100%;
  height: 100%;
}
.bar-group .bar-seg{
  flex: 1 1 0;
}
.col-value{
  font-size: 12px;
  color: var(--text);
  font-weight: 700;
  text-align: center;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  max-width: 100%;
}
.col-value .col-val-main{
  line-height: 1.2;
}
.col-value .split-name{
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
  margin-bottom: 2px;
}
.col-value .col-pct{
  color: #B4B4B4;
  display:block;
  margin-top: 2px;
}
.col-label{
  width: 100%;
  text-align: center;
  font-size: 12px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.col-sub{ font-size: 11px; color: var(--muted); }

/* removed labels tilt */

/* Horizontal bars layout (mirrors vertical with axes swapped) */
.hbar-col{ display:grid; grid-template-columns: var(--labelw, max-content) 1fr; align-items:center; gap:10px; padding:4px 2px; }
.hbar-col.drag-over{ outline:1px dashed var(--accent-2); }
.bar-outer-h{ width: 100%; height: 34px; background: transparent; position: relative; }
.bar-h{ height: 100%; background: linear-gradient(90deg, var(--bar), var(--bar-hover)); border-radius: 4px; transition: width .15s ease; width: 0%; }
.bar-group-h{
  display:flex;
  align-items:center;
  gap: 4px;
  width: 100%;
  height: 100%;
}
.bar-group-h .bar-h-seg{
  flex: 0 0 auto;
}

/* Gradient overlay for split segments and legend swatches */
.bar-seg,
.bar-h-seg,
.legend-swatch{
  background-image: linear-gradient(180deg, rgba(255,255,255,0.25), rgba(0,0,0,0.2));
}

/* Dim non-selected split segments when one is active */
.bar-seg.seg-muted,
.bar-h-seg.seg-muted{
  opacity: 0.25;
}
.row-value{ position:absolute; top:50%; transform: translateY(-50%); white-space: nowrap; font-size: 12px; color: var(--text); font-weight: 700; padding-right: 8px; }
.row-value .sep{ color: var(--muted); margin: 0; }
.row-value .pct{ color: #B4B4B4; }
.row-label{ color: var(--text); font-size: 12px; white-space: nowrap; text-align: left; }

/* Segment color palette (shared for vertical, horizontal and legend) */
.seg-color-0{ background-color: #4A0A77; }  /* deep purple */
.seg-color-1{ background-color: #6C2BA3; }  /* medium purple */
.seg-color-2{ background-color: #AB3585; }  /* magenta */
.seg-color-3{ background-color: #C25DA1; }  /* light magenta */
.seg-color-4{ background-color: #023E8A; }  /* deep blue */
.seg-color-5{ background-color: #0353A4; }  /* blue */
.seg-color-6{ background-color: #3B8AAB; }  /* teal-blue */
.seg-color-7{ background-color: #63BAAB; }  /* teal */
.seg-color-8{ background-color: #2A9D8F; }  /* green-teal */
.seg-color-9{ background-color: #EAA221; }  /* golden yellow */
.seg-color-10{ background-color: #F4A261; } /* soft orange */
.seg-color-11{ background-color: #F77F00; } /* vivid orange */

.tooltip{ position: fixed; z-index: 9999; background: var(--panel); border:1px solid var(--border); color: var(--text); padding:8px 10px; border-radius:8px; box-shadow: 0 10px 20px rgba(0,0,0,.15); max-width: 320px; }
.tooltip .stage{ font-weight: 600; margin-bottom: 2px; }
.tooltip .row2{ display:flex; align-items: baseline; gap: 6px; }
.tooltip .row2 .num{ font-weight: 700; }
.tooltip .row2 .sep{ color: var(--muted); }
.tooltip .row2 .pct{ color: var(--muted); }

#status{ margin-top: 8px; font-size: 12px; }
.col-sub{ font-size: 11px; color: var(--muted); }

/* removed labels tilt */

/* legacy horizontal styles (no longer used) */

.tooltip{ position: fixed; z-index: 9999; background: var(--panel); border:1px solid var(--border); color: var(--text); padding:8px 10px; border-radius:8px; box-shadow: 0 10px 20px rgba(0,0,0,.15); max-width: 320px; }
.tooltip .stage{ font-weight: 600; margin-bottom: 2px; }
.tooltip .row2{ display:flex; align-items: baseline; gap: 6px; }
.tooltip .row2 .num{ font-weight: 700; }
.tooltip .row2 .sep{ color: var(--muted); }
.tooltip .row2 .pct{ color: var(--muted); }

#status{ margin-top: 8px; font-size: 12px; }
