:root {
  --bg: #f4f4f7; --surface: #ffffff; --surface-2: #f0eff4; --border: #e2e1e8;
  --text: #18181b; --muted: #6b6b76; --accent: #9147ff; --accent-2: #772ce8; --accent-soft: #f0e8ff;
  --ok: #0a8a4a; --ok-soft: #e3f6ec; --bad: #d7263d; --bad-soft: #fde8eb; --warn: #a36100; --warn-soft: #fff3dc;
  --bubble: #ffffff; --bubble-border: #e2e1e8; --tg-link: #2a83c6;
  --radius: 12px; --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e10; --surface: #18181b; --surface-2: #1f1f23; --border: #2f2f35;
    --text: #efeff1; --muted: #a0a0ab; --accent: #a970ff; --accent-2: #bf94ff; --accent-soft: #2a1f3d;
    --ok: #3ddc84; --ok-soft: #12321f; --bad: #ff6b7d; --bad-soft: #3a1a20; --warn: #ffb84d; --warn-soft: #3a2b12;
    --bubble: #212d3b; --bubble-border: #2b3a4c; --tg-link: #6ab3f3;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 0 0 4px; }
h2 { font-size: 18px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Каркас */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
}
.brand { font-weight: 700; font-size: 16px; padding: 0 10px 18px; display: flex; gap: 8px; align-items: center; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.nav a {
  display: flex; gap: 10px; align-items: center; padding: 9px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none; margin-bottom: 2px;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sidebar .logout { margin-top: auto; }
.content { flex: 1; min-width: 0; padding: 28px 32px 60px; max-width: 1100px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 10; padding: 10px 16px;
    flex-direction: row; align-items: center; gap: 8px; border-right: 0; border-bottom: 1px solid var(--border);
  }
  .brand { padding: 0; }
  .brand span:last-child { display: none; }
  .nav { display: flex; overflow-x: auto; gap: 2px; flex: 1; scrollbar-width: none; }
  .nav a { white-space: nowrap; margin: 0; padding: 7px 10px; }
  .sidebar .logout { margin: 0; }
  .content { padding: 20px 16px 60px; }
}

/* Карточки и сетки */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid .card { margin-bottom: 0; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1; min-width: 180px; }
.stack > * + * { margin-top: 12px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 10px 0; border-top: 1px solid var(--border); display: flex; gap: 12px; justify-content: space-between; align-items: center; }
.list li:first-child { border-top: 0; padding-top: 0; }

/* Сообщения */
.flash { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-weight: 500; }
.flash.ok { background: var(--ok-soft); color: var(--ok); }
.flash.err { background: var(--bad-soft); color: var(--bad); }
.flash.warn { background: var(--warn-soft); color: var(--warn); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.live { background: #eb0400; color: #fff; }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

/* Формы */
label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; font-weight: 400; }
input[type=text], input[type=url], input[type=number], input[type=time], input[type=datetime-local], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { min-height: 150px; resize: vertical; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; line-height: 1.5; }
.check { display: flex; gap: 8px; align-items: flex-start; font-weight: 500; font-size: 14px; margin-bottom: 8px; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--accent); width: 16px; height: 16px; }
.pills { display: flex; gap: 6px; flex-wrap: wrap; }
.pill input { display: none; }
.pill span {
  display: inline-block; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  cursor: pointer; font-weight: 600; font-size: 13px; user-select: none; background: var(--surface);
}
.pill input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.targets { max-height: 260px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 2px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 8px; border: 1px solid transparent;
  background: var(--accent); color: #fff; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--accent-2); text-decoration: none; }
.btn.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--border); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--bad-soft); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.icon { padding: 5px 9px; }
form.inline { display: inline; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.sticky-save { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; border-top: 1px solid var(--border); margin-top: 8px; z-index: 5; }

/* Таблицы */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
tr.dim td { opacity: .55; }

/* Редактор шаблонов */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 360px); gap: 16px; }
@media (max-width: 1000px) { .editor-grid { grid-template-columns: 1fr; } }
.variant { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; background: var(--surface); }
.variant-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.toolbar { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 6px; }
.toolbar button { border: 1px solid var(--border); background: var(--surface-2); color: var(--text); border-radius: 6px; padding: 3px 9px; cursor: pointer; font: inherit; font-size: 13px; min-width: 32px; }
.toolbar button:hover { border-color: var(--accent); }
.counter { font-size: 12px; color: var(--muted); }
.counter.over { color: var(--bad); font-weight: 700; }
.variant-error { color: var(--bad); font-size: 13px; margin-top: 6px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font-family: ui-monospace, Consolas, monospace; font-size: 12px; padding: 4px 8px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); border: 0; cursor: pointer; }
.chip:hover { outline: 1px solid var(--accent); }
.vars dt { margin-top: 8px; }
.vars dd { margin: 0; font-size: 13px; color: var(--muted); }

/* Пузырь сообщения Telegram */
.bubble { background: var(--bubble); border: 1px solid var(--bubble-border); border-radius: 14px 14px 14px 4px; overflow: hidden; max-width: 420px; }
.bubble .bubble-photo { height: 120px; background: linear-gradient(135deg, #9147ff, #3f51b5); display: grid; place-items: center; color: rgb(255 255 255 / .85); font-size: 13px; }
.bubble .bubble-text { padding: 9px 12px; white-space: pre-wrap; word-wrap: break-word; font-size: 14px; min-height: 20px; }
.bubble .bubble-text a { color: var(--tg-link); }
.bubble .bubble-text blockquote { margin: 4px 0; padding: 2px 10px; border-left: 3px solid var(--tg-link); background: rgb(106 179 243 / .1); border-radius: 4px; }
.bubble .bubble-text code, .bubble .bubble-text pre { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.tg-spoiler { background: var(--muted); color: transparent; border-radius: 3px; transition: .2s; }
.tg-spoiler:hover { background: transparent; color: inherit; }
.bubble-buttons { display: grid; gap: 4px; margin-top: 4px; max-width: 420px; }
.bubble-buttons span { background: rgb(106 179 243 / .15); color: var(--tg-link); text-align: center; padding: 7px; border-radius: 8px; font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Картинки */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.gallery figure { margin: 0; position: relative; }
.gallery img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.gallery form { position: absolute; top: 4px; right: 4px; }
.preview-img { max-width: 320px; width: 100%; border-radius: 8px; border: 1px solid var(--border); display: block; margin-bottom: 10px; }
.radio-cards { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.radio-cards label { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; cursor: pointer; font-weight: 600; margin: 0; }
.radio-cards input { display: none; }
.radio-cards label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-cards small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }

/* Вход */
.center { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { max-width: 440px; width: 100%; }
.steps { padding-left: 20px; }
.steps li { margin-bottom: 8px; }
.empty { text-align: center; padding: 24px; color: var(--muted); }

/* Статистика: графики (одна серия — синий из палитры графиков, текст — цветами текста) */
.chart-wrap { position: relative; }
svg.chart {
  width: 100%; height: auto; display: block; overflow: visible;
  --series: #2a78d6; --grid: #e1e0d9; --axis-line: #c3c2b7; --tick: #898781; --ring: var(--surface);
}
@media (prefers-color-scheme: dark) {
  svg.chart { --series: #3987e5; --grid: #2c2c2a; --axis-line: #383835; }
}
svg.chart .grid { stroke: var(--grid); stroke-width: 1; }
svg.chart .axis { stroke: var(--axis-line); stroke-width: 1; }
svg.chart .tick { fill: var(--tick); font-size: 11px; font-variant-numeric: tabular-nums; }
svg.chart .value { fill: var(--text); font-size: 12px; font-weight: 600; }
svg.chart .bar { fill: var(--series); }
svg.chart a:hover .bar, svg.chart .bar.hover { opacity: .8; }
svg.chart .line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
svg.chart .area { fill: var(--series); opacity: .1; }
svg.chart .dot { fill: var(--series); stroke: var(--ring); stroke-width: 2; }
svg.chart .guide { stroke: var(--tick); stroke-width: 1; }
svg.chart .hit { fill: transparent; cursor: pointer; }
svg.chart .crosshair { cursor: crosshair; }
.chart-tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; line-height: 1.4;
  box-shadow: 0 4px 16px rgb(0 0 0 / .18); white-space: pre-line; max-width: 280px;
}

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.segmented a { padding: 7px 12px; color: var(--text); font-size: 13px; font-weight: 600; text-decoration: none; border-left: 1px solid var(--border); }
.segmented a:first-child { border-left: 0; }
.segmented a.active { background: var(--accent); color: #fff; }

.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile-label { font-size: 13px; color: var(--muted); }
.tile-value { font-size: 26px; font-weight: 650; margin-top: 2px; }
.tile-delta { font-size: 12px; font-weight: 600; margin-top: 2px; }
.tile-delta.up { color: var(--ok); }
.tile-delta.down { color: var(--bad); }
.tile-delta.flat { color: var(--muted); }

.hbars { display: grid; gap: 10px; }
.hbar-row { display: grid; grid-template-columns: minmax(90px, 200px) 1fr auto; gap: 12px; align-items: center; font-size: 14px; }
.hbar-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-track { height: 10px; }
.hbar-fill { display: block; height: 10px; background: #2a78d6; border-radius: 0 4px 4px 0; }
@media (prefers-color-scheme: dark) { .hbar-fill { background: #3987e5; } }
.hbar-value { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.num td:nth-child(n+3) { font-variant-numeric: tabular-nums; }
/* Иконки кнопок (кастомные эмодзи Telegram) */
.emoji-img { vertical-align: middle; object-fit: contain; }
.emoji-char { font-size: 20px; line-height: 1; }
.icon-picker summary { cursor: pointer; font-weight: 600; font-size: 14px; padding: 8px 0; list-style: none; display: flex; gap: 8px; align-items: center; }
.icon-picker summary::-webkit-details-marker { display: none; }
.icon-picker summary::after { content: "▾"; color: var(--muted); }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; max-height: 260px; overflow: auto; padding: 4px; }
.icon-opt { margin: 0; cursor: pointer; }
.icon-opt input { display: none; }
.icon-opt span, .icon-opt button {
  display: grid; place-items: center; width: 100%; aspect-ratio: 1; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); font-size: 22px; position: relative; cursor: pointer; padding: 0;
}
.icon-opt input:checked + span { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent); }
.icon-opt.removable i { position: absolute; top: 2px; right: 4px; font-style: normal; font-size: 11px; color: var(--bad); opacity: 0; }
.icon-opt.removable button:hover i { opacity: 1; }
.btn-preview {
  display: inline-flex; gap: 6px; align-items: center; padding: 8px 16px; border-radius: 10px; font-weight: 600; font-size: 14px;
  background: rgb(106 179 243 / .15); color: var(--tg-link, #2a83c6); min-width: 180px; justify-content: center;
}
.btn-preview.style-primary { background: #3390ec; color: #fff; }
.btn-preview.style-success { background: #31b545; color: #fff; }
.btn-preview.style-danger { background: #e53935; color: #fff; }
