:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c2230;
  --muted: #7a8294;
  --line: #e6e8ee;
  --accent: #2f5bea;
  --accent-soft: #e9efff;
  --green: #18864b;
  --green-soft: #e3f5ea;
  --amber: #a15c00;
  --amber-soft: #fff1dc;
  --grey-soft: #eef0f3;
  --red: #c0392b;
  --red-soft: #fdecea;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --card: #1b1f27; --text: #e6e9ef; --muted: #8b93a5; --line: #2a303b;
    --accent: #6d8dff; --accent-soft: #232c45; --green: #4cc584; --green-soft: #1d3327;
    --amber: #f0a64a; --amber-soft: #3a2c17; --grey-soft: #262b35; --red: #ff7b6e; --red-soft: #3a2220;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }

/* Каркас */
.layout { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.sidebar { background: var(--card); border-right: 1px solid var(--line); padding: 18px 14px; display: flex; flex-direction: column; gap: 14px; position: sticky; top: 0; height: 100vh; }
.content { padding: 24px 28px; min-width: 0; }
.brand { display: flex; gap: 10px; align-items: center; color: var(--text); }
.brand small { display: block; color: var(--muted); font-size: 11px; }
.brand-mark { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.bot-state { font-size: 12px; padding: 6px 10px; border-radius: 8px; display: flex; align-items: center; gap: 8px; }
.bot-state .dot { width: 8px; height: 8px; border-radius: 50%; }
.bot-state.on { background: var(--green-soft); color: var(--green); } .bot-state.on .dot { background: var(--green); }
.bot-state.off { background: var(--red-soft); color: var(--red); } .bot-state.off .dot { background: var(--red); }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-group { text-transform: uppercase; font-size: 11px; color: var(--muted); letter-spacing: .04em; margin: 12px 8px 4px; }
nav a, .me a { color: var(--text); padding: 8px 10px; border-radius: 8px; }
nav a:hover, .me a:hover { background: var(--grey-soft); }
nav a.active, .me a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.me { border-top: 1px solid var(--line); padding-top: 10px; display: flex; justify-content: space-between; align-items: center; }
button.link { background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; }

/* Элементы */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.card.narrow { max-width: 420px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 16px; }
.page-head .muted { display: block; margin-top: 2px; }
.btn { display: inline-block; border: 1px solid var(--line); background: var(--card); color: var(--text); border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
input, select { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
input:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; }
.alert.ok { background: var(--green-soft); color: var(--green); }
.alert.error { background: var(--red-soft); color: var(--red); }
.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.bot { background: var(--green-soft); color: var(--green); }
.pill.manager { background: var(--amber-soft); color: var(--amber); }
.pill.off { background: var(--grey-soft); color: var(--muted); }

/* Фильтры и таблица */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.filters .grow { flex: 1; min-width: 220px; }
.check { display: flex; gap: 6px; align-items: center; color: var(--muted); }
.table-wrap { padding: 0; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 12px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--card); }
td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
table.chats tbody tr { cursor: pointer; }
table.chats tbody tr:hover { background: var(--grey-soft); }
td.car { min-width: 150px; max-width: 200px; }
td.preview { max-width: 420px; color: var(--muted); }
td.preview .who { color: var(--text); font-weight: 600; }
td.empty { text-align: center; color: var(--muted); padding: 40px; }
td.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; }
.form-row { display: flex; flex-wrap: wrap; gap: 8px; }
.form-col { display: flex; flex-direction: column; gap: 12px; }
.form-col label, .login-card label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }

/* Карточка чата */
.back { display: inline-block; margin-bottom: 12px; }
.chat-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.thread { padding: 0; }
.thread-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; padding: 16px; border-bottom: 1px solid var(--line); }
.messages { padding: 16px; display: flex; flex-direction: column; gap: 10px; max-height: calc(100vh - 220px); overflow-y: auto; }
.msg { display: flex; flex-direction: column; max-width: 75%; }
.msg time { font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg .bubble { padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-break: break-word; }
.msg.in { align-self: flex-start; } .msg.in .bubble { background: var(--grey-soft); border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; align-items: flex-end; } .msg.out .bubble { background: var(--accent-soft); border-top-right-radius: 4px; }
.msg.out.manager .bubble { background: var(--amber-soft); }
.msg .who { display: block; font-size: 11px; font-weight: 700; color: var(--muted); margin-bottom: 2px; }
.msg.system { align-self: center; align-items: center; max-width: 90%; }
.msg.system .bubble { background: transparent; border: 1px dashed var(--line); color: var(--muted); font-size: 12px; text-align: center; }
.side .card p { margin: 0 0 10px; }
.phone { font-size: 18px; font-weight: 700; }

/* Вход */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 28px; width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }

/* Телефон */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; }
  nav { flex-direction: row; flex-wrap: wrap; flex: 1 1 100%; }
  .nav-group { display: none; }
  .me { border: 0; padding: 0; }
  .content { padding: 16px; }
  .chat-layout { grid-template-columns: 1fr; }
  .msg { max-width: 90%; }
}

/* Дашборд */
:root { --series-1: #2a78d6; --series-2: #1baf7a; }
@media (prefers-color-scheme: dark) { :root { --series-1: #3987e5; --series-2: #199e70; } }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg a { padding: 8px 12px; color: var(--text); background: var(--card); }
.seg a + a { border-left: 1px solid var(--line); }
.seg a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 12px; background: var(--grey-soft); color: var(--text); }
.chip.ok { background: var(--green-soft); color: var(--green); }
.chip.bad { background: var(--red-soft); color: var(--red); }
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.tile { display: flex; flex-direction: column; gap: 4px; }
.tile-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.tile-value { font-size: 30px; font-weight: 700; line-height: 1.1; }
.delta { font-size: 12px; }
.delta.up { color: var(--green); } .delta.down { color: var(--red); } .delta.flat { color: var(--muted); }
.chart-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.s1 { background: var(--series-1); } .sw.s2 { background: var(--series-2); }
.chart-wrap { position: relative; }
.chart-wrap svg { width: 100%; height: auto; display: block; }
.chart-wrap .grid { stroke: var(--line); stroke-width: 1; }
.chart-wrap .axis { fill: var(--muted); font-size: 11px; }
.chart-wrap .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-wrap .line.s1 { stroke: var(--series-1); } .chart-wrap .line.s2 { stroke: var(--series-2); }
.chart-wrap .end-label { fill: var(--muted); font-size: 11px; }
.chart-wrap .crosshair { stroke: var(--muted); stroke-dasharray: 3 3; }
.chart-wrap .hit { fill: transparent; cursor: crosshair; }
.tooltip { position: absolute; top: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 14px rgba(0,0,0,.08); pointer-events: none; min-width: 130px; }
.table-view summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-top: 8px; }
.table-view table { margin-top: 8px; max-width: 360px; }
.cards3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.kv { display: grid; grid-template-columns: 1fr auto; margin: 0; }
.kv dt, .kv dd { padding: 8px 0; border-bottom: 1px solid var(--line); margin: 0; }
.kv dt { color: var(--muted); } .kv dd { font-weight: 700; text-align: right; }
.kv dt:last-of-type, .kv dd:last-of-type { border-bottom: 0; }
@media (max-width: 860px) { .tiles, .cards3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 520px) { .tiles, .cards3 { grid-template-columns: 1fr; } }

/* Переключатели и карточка машины */
form.inline { display: inline; }
.switch { position: relative; width: 40px; height: 22px; border-radius: 999px; border: 0; background: var(--line); cursor: pointer; padding: 0; transition: background .15s; flex: none; }
.switch span { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch.on { background: var(--green); }
.switch.on span { left: 21px; }
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.setting { display: flex; gap: 12px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--line); }
.setting:last-of-type { border-bottom: 0; }
.setting p { margin: 2px 0 0; }
textarea { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; resize: vertical; }
td .pill { margin-left: 4px; }

/* Настройки AI */
.settings-form h4 { margin: 14px 0 4px; font-size: 14px; }
.grid2, .grid3 { display: grid; gap: 12px; margin: 8px 0; }
.grid2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.field input, .field textarea { color: var(--text); }
.block { display: block; }
.toggle-row { cursor: pointer; align-items: center; }
.switch-input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-ui { position: relative; width: 40px; height: 22px; border-radius: 999px; background: var(--line); flex: none; transition: background .15s; }
.switch-ui span { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch-input:checked + .switch-ui { background: var(--green); }
.switch-input:checked + .switch-ui span { left: 21px; }
.switch-input:focus-visible + .switch-ui { outline: 2px solid var(--accent); outline-offset: 2px; }
.modes { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin: 8px 0; }
.mode { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 4px; cursor: pointer; }
.mode input { display: none; }
.mode:has(input:checked), .mode.on { border-color: var(--accent); background: var(--accent-soft); }
.promos { display: flex; flex-direction: column; gap: 8px; }
.promo-row { display: flex; gap: 8px; flex-wrap: wrap; }
.promo-row .grow { flex: 1; min-width: 220px; }
.save-bar { position: sticky; bottom: 0; display: flex; justify-content: space-between; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; box-shadow: 0 -4px 14px rgba(0,0,0,.06); }
@media (max-width: 860px) { .grid2, .grid3, .modes { grid-template-columns: 1fr; } }
.ban-form { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 12px; }
