:root {
  /* Заполняется из Telegram themeParams в app.js; значения ниже – дефолт (light). */
  --bg: #ffffff;
  --secondary-bg: #f1f3f5;
  --text: #0d1117;
  --hint: #6b7280;
  --link: #2a7bd6;
  --button: #2a7bd6;
  --button-text: #ffffff;
  --accent: var(--link);

  --consensus: #1f9d57;
  --consensus-bg: rgba(31, 157, 87, 0.12);
  --disagree: #d9822b;
  --disagree-bg: rgba(217, 130, 43, 0.14);
  --single: #7a5cd6;
  --single-bg: rgba(122, 92, 214, 0.12);
  --danger: #d64545;

  --radius: 14px;
  --radius-sm: 10px;
  --gap: 14px;
  --maxw: 720px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

.screen { animation: fade .18s ease; }
.hidden { display: none !important; }
@keyframes fade { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: none; } }

/* Шапки */
.top {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  min-height: 36px;
}
.top .spacer, .top .title { flex: 1; }
.title { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; margin: 0; }
.title.sm { font-size: 18px; }
.lead { color: var(--hint); margin: 0 0 18px; font-size: 15px; }

/* Кнопки */
.primary-btn {
  width: 100%;
  border: 0;
  border-radius: var(--radius);
  background: var(--button);
  color: var(--button-text);
  font-size: 16px;
  font-weight: 600;
  padding: 15px 18px;
  cursor: pointer;
  transition: filter .15s, transform .05s;
}
.primary-btn:active { transform: scale(.99); filter: brightness(.95); }
.primary-btn[disabled] { opacity: .5; cursor: default; }
.ghost-btn {
  border: 0; background: transparent; color: var(--link);
  font-size: 15px; padding: 8px 6px; cursor: pointer; border-radius: 8px;
  white-space: nowrap;
}
.ghost-btn:active { background: var(--secondary-bg); }
.link-btn { border: 0; background: transparent; color: var(--link); font-size: 14px; cursor: pointer; padding: 4px; }
.fallback-run { margin-top: 8px; }

/* Поле ввода */
.field-label { display: block; font-size: 13px; color: var(--hint); margin: 0 2px 6px; }
.question {
  width: 100%;
  resize: vertical;
  border: 1px solid color-mix(in srgb, var(--hint) 28%, transparent);
  border-radius: var(--radius);
  background: var(--secondary-bg);
  color: var(--text);
  padding: 14px;
  font: inherit;
  margin-bottom: 18px;
}
.question:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Панель ролей / модель */
.panel-block, .model-block { margin-bottom: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.panel-title { font-size: 13px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; }

.experts-toggles { display: flex; flex-direction: column; gap: 8px; }
.expert-toggle {
  display: flex; align-items: center; gap: 10px;
  background: var(--secondary-bg); border-radius: var(--radius-sm);
  padding: 11px 13px; cursor: pointer; user-select: none;
}
.expert-toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.expert-toggle .et-name { font-weight: 600; font-size: 15px; }
.expert-toggle .et-tag { color: var(--hint); font-size: 13px; }

.segmented { display: flex; gap: 6px; background: var(--secondary-bg); padding: 4px; border-radius: var(--radius); }
.seg {
  flex: 1; border: 0; background: transparent; color: var(--text);
  padding: 10px; border-radius: var(--radius-sm); font-size: 14px; cursor: pointer; font-weight: 500;
}
.seg.active { background: var(--bg); box-shadow: var(--shadow); font-weight: 600; }

/* Прогресс */
.rounds { display: flex; flex-direction: column; gap: 18px; }
.round-block .round-title { font-size: 13px; color: var(--hint); margin: 0 0 8px; text-transform: uppercase; letter-spacing: .04em; }
.progress-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pcard {
  background: var(--secondary-bg); border-radius: var(--radius-sm);
  padding: 10px 12px; display: flex; align-items: center; gap: 9px; min-height: 52px;
  border: 1px solid transparent; transition: border-color .2s, background .2s;
}
.pcard .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--hint); }
.pcard.thinking { border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.pcard.thinking .dot { background: var(--accent); animation: pulse 1s infinite; }
.pcard.done .dot { background: var(--consensus); }
.pcard.error .dot { background: var(--danger); }
.pcard .pc-name { font-size: 13px; font-weight: 600; line-height: 1.2; }
.pcard .pc-state { font-size: 11px; color: var(--hint); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
.progress-note { color: var(--hint); font-size: 14px; margin-top: 18px; text-align: center; }

/* Результат */
.result-head {
  background: var(--secondary-bg); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.bottom-line { font-size: 17px; font-weight: 600; margin: 0 0 14px; line-height: 1.4; }
.gauge-row { display: flex; align-items: center; gap: 14px; }
.gauge { position: relative; width: 64px; height: 64px; flex: none; }
.gauge svg { transform: rotate(-90deg); }
.gauge .gv { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 700; font-size: 17px; }
.gauge-label { font-size: 13px; color: var(--hint); }
.conv-badge { display: inline-block; font-size: 13px; font-weight: 600; padding: 3px 10px; border-radius: 999px; margin-top: 6px; }

.map-section { margin-bottom: 18px; }
.map-section h3 { font-size: 14px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.map-section h3 .swatch { width: 10px; height: 10px; border-radius: 3px; }
.map-item { border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 8px; font-size: 14px; }
.map-item.c { background: var(--consensus-bg); }
.map-item.d { background: var(--disagree-bg); }
.map-item.s { background: var(--single-bg); }
.map-item .mi-point { font-weight: 600; margin-bottom: 4px; }
.map-item .mi-meta { font-size: 12.5px; color: var(--hint); }
.map-item .mi-sides { margin-top: 6px; font-size: 13px; }
.map-item .mi-sides span { display: block; padding: 3px 0; }
.empty-note { color: var(--hint); font-size: 13px; font-style: italic; }

/* График траектории */
.trajectory { background: var(--secondary-bg); border-radius: var(--radius); padding: 14px; margin-bottom: 18px; }
.trajectory h3 { font-size: 14px; margin: 0 0 10px; }
.traj-legend { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.traj-legend .lg { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--hint); }
.traj-legend .lg i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }

/* Карточки экспертов (раскрываемые) */
.expert-cards { display: flex; flex-direction: column; gap: 8px; }
.ecard { background: var(--secondary-bg); border-radius: var(--radius-sm); overflow: hidden; }
.ecard summary {
  list-style: none; cursor: pointer; padding: 13px 14px;
  display: flex; align-items: center; gap: 10px;
}
.ecard summary::-webkit-details-marker { display: none; }
.ecard .ec-name { font-weight: 600; font-size: 15px; flex: 1; }
.ecard .ec-conf { font-size: 13px; color: var(--hint); font-variant-numeric: tabular-nums; }
.ecard .ec-chev { color: var(--hint); transition: transform .15s; }
.ecard[open] .ec-chev { transform: rotate(90deg); }
.ec-body { padding: 0 14px 14px; }
.ec-round { border-top: 1px solid color-mix(in srgb, var(--hint) 18%, transparent); padding: 10px 0; }
.ec-round .ecr-h { font-size: 12px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 4px; display: flex; justify-content: space-between; }
.ec-stance { font-weight: 600; margin: 2px 0 6px; }
.ec-list { margin: 6px 0; padding-left: 18px; }
.ec-list li { margin-bottom: 3px; }
.ec-field { font-size: 13.5px; margin-top: 6px; }
.ec-field b { color: var(--hint); font-weight: 600; }
.ec-risk { background: var(--disagree-bg); border-radius: 8px; padding: 8px 10px; margin-top: 8px; font-size: 13.5px; }
.ecard.failed summary { color: var(--danger); }

/* История */
.history-list { display: flex; flex-direction: column; gap: 8px; }
.hitem { background: var(--secondary-bg); border-radius: var(--radius-sm); padding: 13px 14px; cursor: pointer; }
.hitem .hi-q { font-weight: 600; font-size: 15px; margin-bottom: 4px; }
.hitem .hi-meta { font-size: 12.5px; color: var(--hint); display: flex; gap: 10px; }
.hitem .hi-del { float: right; color: var(--hint); }

/* Редактор ролей */
.roles-editor { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.role-card { background: var(--secondary-bg); border-radius: var(--radius-sm); padding: 12px 14px; }
.role-card .rc-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.role-card .rc-name { flex: 1; font-weight: 600; font-size: 15px; border: 0; background: transparent; color: var(--text); }
.role-card textarea { width: 100%; min-height: 80px; border-radius: 8px; border: 1px solid color-mix(in srgb, var(--hint) 25%, transparent); background: var(--bg); color: var(--text); padding: 10px; font: inherit; font-size: 13.5px; resize: vertical; }
.role-card .rc-row { display: flex; align-items: center; gap: 12px; margin-top: 8px; font-size: 13px; color: var(--hint); }
.role-card .rc-row label { display: flex; align-items: center; gap: 6px; }
.role-card .rc-del { margin-left: auto; color: var(--danger); background: transparent; border: 0; cursor: pointer; font-size: 13px; }
.role-card input[type="range"] { accent-color: var(--accent); }

/* Тосты */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 11px 16px; border-radius: 999px; font-size: 14px; z-index: 50;
  box-shadow: var(--shadow); max-width: 90vw; text-align: center;
  animation: fade .18s ease;
}

.spinner { width: 16px; height: 16px; border: 2px solid color-mix(in srgb, var(--hint) 35%, transparent); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 380px) {
  .progress-cards { grid-template-columns: 1fr; }
}
