:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #94a3b8;
  --line: #e7ebf2;
  --brand: #5b4bff;
  --brand-soft: #ede9ff;
  --good: #18a957;
  --warn: #e8a020;
  --bad: #e5484d;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 4px 16px rgba(16,24,40,.06);
  --radius: 14px;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.wrap { max-width: 1160px; margin: 0 auto; padding: 28px 24px 80px; }

/* Header */
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.head h1 { font-size: 24px; font-weight: 700; letter-spacing: -.02em; margin: 0; }
.head .sub { color: var(--ink-2); margin-top: 4px; font-size: 13px; }
.head .src { color: var(--ink-3); font-size: 12px; }
.head .src a { color: var(--brand); text-decoration: none; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 15px; box-shadow: var(--shadow); }
.kpi .label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); font-weight: 600; }
.kpi .val { font-size: 24px; font-weight: 700; letter-spacing: -.02em; margin-top: 6px; }
.kpi .hint { font-size: 11px; color: var(--ink-2); margin-top: 2px; }
@media (max-width: 920px) { .kpis { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 560px) { .kpis { grid-template-columns: repeat(2, 1fr); } }

/* Panel */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; }
.panel-h { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 6px; }
.panel-h h2 { font-size: 15px; font-weight: 700; margin: 0; }
.panel-h .note { color: var(--ink-3); font-size: 12px; }
.panel-b { padding: 8px 18px 18px; }

/* Trend chart */
.chart { width: 100%; display: block; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-2); padding: 0 18px 14px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.legend i.bars { height: 10px; width: 10px; border-radius: 2px; }

/* Controls */
.controls { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.controls input, .controls select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); }
.controls input[type=search] { min-width: 220px; }
.controls label { font-size: 12px; color: var(--ink-2); display: inline-flex; align-items: center; gap: 6px; }
.controls .spacer { flex: 1; }
.toggle { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; user-select: none; font-size: 13px; color: var(--ink-2); }
.toggle input { width: 16px; height: 16px; accent-color: var(--brand); }
.count-pill { background: var(--brand-soft); color: var(--brand); font-weight: 600; font-size: 12px; padding: 4px 10px; border-radius: 999px; }

/* Reasons overview — donut + legend */
.ov-wrap { display: flex; gap: 28px; align-items: center; flex-wrap: wrap; }
.donut { width: 210px; height: 210px; flex: none; }
.donut circle { transition: opacity .15s ease, stroke-width .15s ease; }
.donut .center-n { font-size: 30px; font-weight: 700; fill: var(--ink); }
.donut .center-l { font-size: 10px; fill: var(--ink-3); letter-spacing: .04em; }
.rlegend { flex: 1; min-width: 300px; display: grid; grid-template-columns: repeat(2, 1fr); grid-auto-flow: column; grid-template-rows: repeat(var(--rows, 8), auto); gap: 2px 20px; }
@media (max-width: 760px) { .rlegend { grid-template-columns: 1fr; grid-auto-flow: row; grid-template-rows: none; } }
.lrow { display: grid; grid-template-columns: 13px 1fr auto; gap: 9px; align-items: center; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
.lrow:hover { background: var(--surface-2); }
.lrow.active { background: var(--brand-soft); }
.lrow.dim { opacity: .4; }
.lrow .sw { width: 11px; height: 11px; border-radius: 3px; }
.lrow .ln { font-size: 12.5px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lrow .ln small { display: block; font-weight: 400; color: var(--ink-3); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lrow .lv { font-variant-numeric: tabular-nums; color: var(--ink); font-weight: 700; font-size: 13px; text-align: right; }
.lrow .lv small { color: var(--ink-3); font-weight: 500; }
.rsum-clear { font-size: 12px; color: var(--brand); cursor: pointer; }

/* Steps tree */
.step { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; overflow: hidden; }
.step-h { display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 14px; padding: 16px 18px; cursor: pointer; }
.step-h:hover { background: var(--surface-2); }
.chev { transition: transform .18s ease; color: var(--ink-3); font-size: 13px; }
.open > .step-h .chev, .open > .reason-h .chev { transform: rotate(90deg); }
.step-title { font-size: 15px; font-weight: 700; }
.step-meta { font-size: 12px; color: var(--ink-2); margin-top: 3px; }
.barwrap { display: flex; align-items: center; gap: 12px; min-width: 220px; }
.bar { flex: 1; height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.bar > span { display: block; height: 100%; border-radius: 999px; background: var(--brand); }
.bar-num { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 70px; text-align: right; }
.bar-num small { color: var(--ink-3); font-weight: 500; }

.step-body { display: none; border-top: 1px solid var(--line); background: var(--surface-2); padding: 8px 14px 14px; }
.open > .step-body { display: block; }

/* Reason rows */
.reason { border-radius: 10px; margin-top: 8px; background: var(--surface); border: 1px solid var(--line); overflow: hidden; }
.reason-h { display: grid; grid-template-columns: 18px 220px 1fr auto; align-items: center; gap: 12px; padding: 11px 13px; cursor: pointer; }
.reason-h:hover { background: var(--surface-2); }
.cat { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; }
.dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.cat-desc { font-size: 12px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reason-body { display: none; border-top: 1px dashed var(--line); padding: 4px 0; }
.open > .reason-body { display: block; }

/* Call rows */
.call { display: grid; grid-template-columns: 84px 46px 1fr auto; gap: 12px; align-items: center; padding: 9px 14px; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 13px; }
.call:last-child { border-bottom: none; }
.call:hover { background: var(--brand-soft); }
.call .date { color: var(--ink-3); font-variant-numeric: tabular-nums; font-size: 12px; }
.call .summary { color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.call .has-tr { color: var(--brand); font-size: 11px; font-weight: 600; }
.call .no-tr { color: var(--ink-3); font-size: 11px; }
.chip { font-size: 11px; padding: 2px 7px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-2); font-weight: 600; }
.chip.lang { text-transform: uppercase; }
.empty { color: var(--ink-3); font-size: 12px; padding: 14px; text-align: center; }

/* Drawer */
.scrim { position: fixed; inset: 0; background: rgba(15,23,42,.32); opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 40; }
.scrim.show { opacity: 1; pointer-events: auto; }
.drawer { position: fixed; top: 0; right: 0; height: 100%; width: min(560px, 94vw); background: var(--surface); box-shadow: -8px 0 40px rgba(16,24,40,.18); transform: translateX(100%); transition: transform .24s cubic-bezier(.4,0,.2,1); z-index: 50; display: flex; flex-direction: column; }
.drawer.show { transform: translateX(0); }
.drawer-h { padding: 18px 20px; border-bottom: 1px solid var(--line); }
.drawer-h .x { position: absolute; top: 14px; right: 16px; cursor: pointer; border: none; background: var(--surface-2); width: 30px; height: 30px; border-radius: 8px; font-size: 16px; color: var(--ink-2); }
.drawer-h h3 { margin: 0 30px 4px 0; font-size: 16px; }
.drawer-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.drawer-b { padding: 16px 20px; overflow-y: auto; flex: 1; }
.summary-box { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: 13px; color: var(--ink); margin-bottom: 16px; }
.summary-box b { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); margin-bottom: 5px; }

/* Transcript chat */
.turn { display: flex; margin-bottom: 10px; }
.turn.user { justify-content: flex-end; }
.bubble { max-width: 80%; padding: 9px 12px; border-radius: 14px; font-size: 13px; line-height: 1.4; }
.turn.agent .bubble { background: var(--surface-2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.turn.user .bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.turn .who { font-size: 10px; text-transform: uppercase; letter-spacing: .05em; opacity: .7; margin-bottom: 3px; font-weight: 700; }
.no-transcript { color: var(--ink-3); text-align: center; padding: 30px; font-size: 13px; }
