:root {
  --bg: #F7F8F4;
  --surface: #FFFFFF;
  --surface-2: #EEF4E8;
  --border: #DCE0D6;
  --text: #1A1A1A;
  --text-2: #4A4A44;
  --muted: #6D7568;
  --green: #2E8B2E;
  --green-ink: #FFFFFF;
  --orange: #F28C28;
  --orange-soft: #FDF1E4;
  --error: #B3261E;
  --code: #F1F3EE;
  --radius: 14px;
  --serif: "Century Schoolbook", "Georgia", "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141A12; --surface: #1C241A; --surface-2: #233021; --border: #33402F;
    --text: #EEF1EA; --text-2: #C9CFC3; --muted: #95A08E; --green: #4CAF50; --green-ink: #0E140C;
    --orange: #F5A04A; --orange-soft: #2E2416; --error: #FF8A80; --code: #232C21;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--sans); }
[hidden] { display: none !important; }
picture { display: block; line-height: 0; }
.muted { color: var(--muted); }

.btn { font: inherit; border-radius: 10px; padding: 9px 16px; cursor: pointer; border: 1px solid transparent; }
.btn.primary { background: var(--green); color: var(--green-ink); font-weight: 600; }
.btn.primary:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: transparent; color: var(--text-2); border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--orange); outline-offset: 2px; }

/* ---- вход ---- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px 28px; display: grid; gap: 14px; }
.login-logo { width: 170px; height: auto; }
.login-card h1 { font: 600 28px/1.2 var(--serif); margin: 6px 0 0; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 6px; font-weight: 600; font-size: 14px; color: var(--text-2); }
.login-card input { font: inherit; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); }
.error { color: var(--error); font-size: 14px; }
.fine { font-size: 12.5px; color: var(--muted); }

/* ---- чат ---- */
.app { height: 100%; display: flex; flex-direction: column; }
.top { display: flex; align-items: center; gap: 12px; padding: 10px 20px; background: var(--surface);
  border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.top-logo { height: 30px; width: auto; }
.top-title { font: 600 19px/1 var(--serif); }
.top-user { margin-left: auto; color: var(--muted); font-size: 13.5px; }

.messages { flex: 1; overflow-y: auto; padding: 24px 16px 8px; }
.messages > * { max-width: 880px; margin-left: auto; margin-right: auto; }

.empty { text-align: center; padding: 6vh 0 0; }
.empty h2 { font: 600 26px/1.2 var(--serif); margin: 0 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 20px; }
.chip { font: inherit; font-size: 14px; text-align: left; background: var(--surface); color: var(--text-2);
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; cursor: pointer; max-width: 420px; }
.chip:hover { border-color: var(--green); color: var(--text); }

.msg { margin-bottom: 18px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--surface-2); border: 1px solid var(--border); border-radius: 14px 14px 4px 14px;
  padding: 10px 14px; max-width: 80%; white-space: pre-wrap; }
.msg.bot .bubble { background: var(--surface); border: 1px solid var(--border); border-radius: 4px 14px 14px 14px;
  padding: 14px 18px; }
.msg.bot.error .bubble { border-color: var(--error); color: var(--error); }

.steps { font-size: 13px; color: var(--muted); display: grid; gap: 3px; margin-bottom: 8px; }
.step::before { content: "● "; color: var(--green); }
.step.live::before { color: var(--orange); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }
.thinking { color: var(--muted); font-size: 14px; }

.meta { display: flex; gap: 10px; align-items: flex-start; margin-top: 6px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.meta details { flex: 1; min-width: 0; }
.meta summary { cursor: pointer; }
.meta pre { white-space: pre-wrap; word-break: break-word; }
.link-btn { font: inherit; background: none; border: none; color: var(--muted); cursor: pointer; padding: 0; text-decoration: underline; }

/* ---- оформление ответа (markdown) ---- */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md h3, .md h4 { font-family: var(--serif); margin: 16px 0 8px; line-height: 1.25; }
.md h3 { font-size: 19px; } .md h4 { font-size: 16.5px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md li ul { margin: 2px 0 0; padding-left: 20px; list-style-type: circle; }
.md strong { font-weight: 650; }
.md code { background: var(--code); border-radius: 5px; padding: 1px 5px; font-size: 13px; }
.md pre { background: var(--code); border-radius: 10px; padding: 12px; overflow-x: auto; font-size: 13px; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 0 0 10px; padding: 8px 12px; border-left: 4px solid var(--orange); background: var(--orange-soft); border-radius: 0 8px 8px 0; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.table-wrap { overflow-x: auto; margin: 0 0 12px; border: 1px solid var(--border); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; table-layout: auto; font-size: 13.5px; line-height: 1.4; font-variant-numeric: tabular-nums; }
.md th, .md td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); font-weight: 650; white-space: normal; vertical-align: bottom; min-width: 56px; }
.md td { white-space: nowrap; }
.md td:first-child { white-space: normal; min-width: 110px; max-width: 260px; }
.md tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--surface-2) 45%, transparent); }
.md tbody tr:hover td { background: var(--surface-2); }
.md tr:last-child td { border-bottom: none; }
.md td.num, .md th.num { text-align: right; white-space: nowrap; }

/* ---- поле ввода ---- */
.composer { display: flex; gap: 10px; align-items: flex-end; padding: 10px 16px 4px; width: 100%; max-width: 912px; margin: 0 auto; }
.composer textarea { flex: 1; font: inherit; resize: none; max-height: 220px; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.composer-note { text-align: center; font-size: 12px; color: var(--muted); margin: 2px 16px 12px; }

@media (max-width: 640px) {
  .top { padding: 10px 16px; gap: 8px; }
  .top-user { order: 5; width: 100%; margin-left: 0; }
  .top .btn { padding: 6px 10px; font-size: 13.5px; }
  .top-title { display: none; }
  .top-logo { margin-right: auto; }
  .msg.user .bubble { max-width: 92%; }
  .msg.bot .bubble { padding: 12px 14px; }
}

/* ================= панель показателей ================= */
:root { --panel-bg: #F1F3EE; --up: #2E8B2E; --down: #D23B2E; }
@media (prefers-color-scheme: dark) { :root { --panel-bg: #10150E; --up: #5CC15C; --down: #FF7A6E; } }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 2fr) minmax(360px, 1fr); }
.chat-col { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.panel { overflow-y: auto; background: var(--panel-bg); border-left: 1px solid var(--border); padding: 12px 14px 24px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; font-size: 12.5px; margin: 0 2px 10px; }
.tabs-mobile { display: none; }

.card { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; }
.card.skeleton { height: 120px; background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.4s infinite; }
@keyframes sk { to { background-position: -200% 0; } }
.card-title { display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--text-2); margin-bottom: 8px; }
.card-title svg { flex: none; width: 22px; height: 22px; color: var(--green); }
.info { flex: none; width: 18px; height: 18px; border-radius: 50%; border: 1.5px solid var(--muted); color: var(--muted); font: 700 11px/15px var(--sans); text-align: center; text-transform: none; background: none; cursor: help; padding: 0; }
.info:hover, .info:focus-visible { border-color: var(--orange); color: var(--orange); }

.kpi { display: flex; align-items: center; gap: 14px; }
.kpi-value { font-size: 30px; font-weight: 600; line-height: 1.1; white-space: nowrap; font-variant-numeric: tabular-nums; }
.kpi-value small { font-size: 16px; font-weight: 500; color: var(--text-2); }
.kpi-value.alert { color: var(--down); }
.deltas { border-left: 1px solid var(--border); padding-left: 12px; font-size: 13px; color: var(--text-2); display: grid; gap: 2px; }
.deltas div { display: flex; justify-content: space-between; gap: 12px; white-space: nowrap; }
.d-up { color: var(--up); font-weight: 600; } .d-down { color: var(--down); font-weight: 600; } .d-flat { color: var(--muted); font-weight: 600; }
.kpi-sub { font-size: 12.5px; color: var(--muted); margin-top: 8px; }
.note { margin-top: 10px; font-size: 12.5px; line-height: 1.45; background: var(--orange-soft); border-left: 3px solid var(--orange); border-radius: 0 8px 8px 0; padding: 7px 10px; color: var(--text-2); }
.note b { color: var(--text); }

/* тренеры */
.periods { display: flex; gap: 4px; margin: 0 0 10px; }
.periods button { flex: 1; font: inherit; font-size: 12.5px; padding: 5px 0; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text-2); cursor: pointer; }
.periods button.on { background: var(--green); border-color: var(--green); color: var(--green-ink); font-weight: 600; }
.ct { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; table-layout: fixed; }
.ct th { color: var(--muted); font-weight: 500; padding: 0 2px 6px; text-align: right; }
.ct th:first-child, .ct td:first-child { text-align: left; }
.ct th svg { width: 16px; height: 16px; vertical-align: middle; cursor: help; }
.ct td { padding: 5px 2px; text-align: right; border-top: 1px solid var(--border); white-space: nowrap; }
.ct td:first-child { overflow: hidden; text-overflow: ellipsis; }
.ct col.c-name { width: 30%; } .ct col.c-num { width: 11.5%; } .ct col.c-score { width: 12.5%; }
.ct tr.best-name td { font-weight: 700; font-size: 15px; padding: 4px 2px 0; border-top: none; white-space: normal; }
.ct tr.best td { font-weight: 700; font-size: 14px; padding: 4px 2px 10px; border-top: none; }
.ct tr.idle td { color: var(--muted); }
.ct .score { color: var(--green); font-weight: 700; }
.ct tr.place td:first-child::before { color: var(--muted); }
.more { margin-top: 8px; width: 100%; font: inherit; font-size: 13px; padding: 7px; border-radius: 8px; border: 1px dashed var(--border); background: none; color: var(--text-2); cursor: pointer; }
.more:hover { border-color: var(--green); color: var(--text); }
.muted-row { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* рекомендации */
.reco .md { font-size: 13.5px; }
.reco .md ul, .reco .md ol { padding-left: 18px; }
.reco .md li { margin: 0 0 8px; }
.reco-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; font-size: 12px; color: var(--muted); gap: 8px; }
.reco-wait { color: var(--muted); font-size: 13.5px; }

/* всплывающая подсказка */
.tip { position: fixed; z-index: 50; max-width: 340px; background: #1F2A1C; color: #F3F5EF; font-size: 12.8px; line-height: 1.5;
  padding: 10px 12px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25); white-space: pre-line; pointer-events: none; }
@media (prefers-color-scheme: dark) { .tip { background: #F3F5EF; color: #1A1A1A; } }

@media (max-width: 1000px) {
  .tabs-mobile { display: flex; gap: 6px; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
  .tab-m { flex: 1; font: inherit; font-size: 14px; padding: 7px; border-radius: 9px; border: 1px solid var(--border); background: none; color: var(--text-2); cursor: pointer; }
  .tab-m.active { background: var(--surface-2); color: var(--text); font-weight: 600; border-color: var(--green); }
  .layout { display: block; overflow: hidden; }
  .layout[data-view="panel"] .chat-col { display: none; }
  .layout[data-view="chat"] .panel { display: none; }
  .chat-col { height: 100%; }
  .panel { height: 100%; border-left: none; }
}

/* кривая дожития */
.curve-box { margin-top: 10px; }
.curve { width: 100%; height: auto; display: block; }
.curve polyline { fill: none; stroke: var(--green); stroke-width: 2; vector-effect: non-scaling-stroke; }
.curve circle { fill: var(--orange); }
.curve .half { stroke: var(--border); stroke-dasharray: 3 3; vector-effect: non-scaling-stroke; }
.curve-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.curve-legend b { color: var(--text); }

/* графики в ответах */
.chart-wrap { margin: 4px 0 14px; padding: 12px 12px 8px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.chart-title { font-weight: 650; font-size: 14px; margin: 0 0 8px; color: var(--text); }
.chart-box { position: relative; width: 100%; }
.chart-box canvas { display: block; }
.chart-bad { color: var(--muted); font-size: 13px; padding: 8px 0; height: auto !important; }
.chart-data-btn { font-size: 12px; margin-top: 6px; }
.chart-wrap .table-wrap { margin: 8px 0 0; }
.md .chart-wrap .table-wrap table { min-width: 0; }
.life-curve { margin-top: 10px; }
