:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1b1d22;
  --muted: #6b7280;
  --border: #e3e5ea;
  --accent: #7000ff;
  --accent-text: #ffffff;
  --accent-soft: #f1e8ff;
  --error-bg: #fdecec;
  --error-text: #a12020;
  --info-bg: #eef2f7;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121317;
    --surface: #1b1d23;
    --text: #e8e9ec;
    --muted: #9aa0ab;
    --border: #2c2f37;
    --accent: #9b5cff;
    --accent-soft: #2a1d44;
    --error-bg: #3a1b1b;
    --error-text: #ffb3b3;
    --info-bg: #23262e;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}
h1 { font-size: 24px; margin: 0 0 8px; }
a { color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }

/* шапка */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; font-size: 17px; }
.account { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.account form { margin: 0; }
.who { display: inline-flex; align-items: center; gap: 8px; }
.email { font-weight: 500; overflow-wrap: anywhere; }
.badge {
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
.link { color: var(--accent); text-decoration: none; }
.link:hover { text-decoration: underline; }

/* раскладка */
.page { max-width: 960px; margin: 0 auto; padding: 28px 16px; }
.page.wide { max-width: 1200px; }
.center { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px;
}
.login { width: 100%; max-width: 380px; text-align: center; }
.login .btn { margin-top: 12px; }

/* кнопки */
.btn {
  display: inline-block; border: 1px solid transparent; border-radius: 10px; padding: 9px 16px;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; text-align: center;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn-wide { width: 100%; }

.notice { border-radius: 10px; padding: 10px 12px; margin: 12px 0 0; text-align: left; }
.notice-error { background: var(--error-bg); color: var(--error-text); }
.notice-info { background: var(--info-bg); }

/* три кнопки на главной */
.tiles {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-top: 20px;
}
.tile {
  display: flex; align-items: center; justify-content: center; min-height: 120px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 18px; font-weight: 600; text-decoration: none; transition: border-color .15s, transform .15s;
}
.tile:hover { border-color: var(--accent); color: var(--accent); transform: translateY(-1px); }

/* журнал */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-top: 12px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
tr:last-child td { border-bottom: none; }
tr.denied td { color: var(--error-text); }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ── шаг 2: роли, заявки, пользователи ── */
:root {
  --ok-bg: #e7f6ec; --ok-text: #1d6b3a;
  --warn-bg: #fff4dc; --warn-text: #8a5a00;
  --danger: #c62828;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ok-bg: #173323; --ok-text: #8fdcaa;
    --warn-bg: #3a2e12; --warn-text: #f3c969;
    --danger: #ff7b7b;
  }
}
.notice { margin-bottom: 14px; }
.notice-ok { background: var(--ok-bg); color: var(--ok-text); }
.page > .notice:first-child { margin-top: 0; }

.count {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700;
  line-height: 20px; text-align: center; vertical-align: middle;
}
.count-lg { margin-left: 10px; font-size: 14px; min-width: 26px; line-height: 26px; }

.badge-curator { background: var(--ok-bg); color: var(--ok-text); }
.badge-user { background: var(--info-bg); color: var(--text); }
.badge-pending { background: var(--warn-bg); color: var(--warn-text); }

.status { font-size: 13px; font-weight: 600; white-space: nowrap; }
.status-active { color: var(--ok-text); }
.status-pending { color: var(--warn-text); }
.status-disabled, .status-rejected { color: var(--danger); }
tr.inactive td { opacity: .65; }
tr.inactive td:last-child { opacity: 1; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin: 16px 0 16px; }
.tab {
  padding: 8px 14px; text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px;
  color: var(--muted); font-weight: 600;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.actions form { margin: 0; }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }

.waiting { max-width: 520px; margin: 40px auto; text-align: center; }
.waiting-icon { font-size: 40px; line-height: 1; margin-bottom: 12px; }

.prose h2 { font-size: 17px; margin: 18px 0 6px; }
.prose p, .prose ul { margin: 0 0 8px; }
.prose ul { padding-left: 20px; }

/* ── шаг 3: диалог оператора ── */
h2 { font-size: 17px; margin: 0; }
.dialog-page { max-width: 980px; }
.start-card { margin-bottom: 16px; }
.client-form { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.client-form input {
  flex: 1 1 220px; font: inherit; font-size: 18px; letter-spacing: .04em;
  padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.client-form input:focus, .composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field-error { color: var(--error-text); margin-top: 10px; }
.today-card .row { margin-bottom: 10px; }
.table-wrap.flat { border: none; margin: 0; }
.mask-cell { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }

.chat-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0;
  padding: 10px 14px;
}
.chat-client { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.client-mask { font-weight: 700; font-size: 17px; font-variant-numeric: tabular-nums; }
.chat-actions { display: flex; gap: 8px; }

.chat-box {
  background: var(--bg); border-left: 1px solid var(--border); border-right: 1px solid var(--border);
  height: calc(100vh - 300px); min-height: 320px; overflow-y: auto; padding: 16px 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.msg { display: flex; flex-direction: column; max-width: 82%; }
.msg-bot { align-self: flex-start; }
.msg-user { align-self: flex-end; align-items: flex-end; }
.bubble { border-radius: 14px; padding: 10px 12px 6px; border: 1px solid var(--border); }
.msg-bot .bubble { background: var(--surface); border-top-left-radius: 4px; }
.msg-user .bubble { background: var(--accent-soft); border-color: transparent; border-top-right-radius: 4px; }
.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-foot { display: flex; gap: 10px; align-items: center; justify-content: flex-end; font-size: 12px; color: var(--muted); margin-top: 4px; }
.copy { background: none; border: none; padding: 0; font: inherit; color: var(--accent); cursor: pointer; }
.copy:hover { text-decoration: underline; }
.choice { font-size: 13px; color: var(--muted); margin: 4px 0 0 6px; }
.msg-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  font: inherit; font-size: 14px; padding: 6px 12px; border-radius: 999px; cursor: pointer;
  background: var(--surface); color: var(--accent); border: 1px solid var(--accent);
}
.chip:hover:not(:disabled) { background: var(--accent); color: var(--accent-text); }
.chip:disabled { opacity: .5; cursor: default; }
.chip-menu { font-weight: 600; }
.msg-system { align-self: center; text-align: center; font-size: 13px; color: var(--muted); max-width: 90%; }
.msg-system.error { color: var(--error-text); }
.typing { align-self: flex-start; font-size: 13px; color: var(--muted); padding: 4px 8px; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .4; } }

.composer {
  display: flex; gap: 8px; align-items: flex-end; background: var(--surface); padding: 10px;
  border: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius);
}
.composer textarea {
  flex: 1; resize: none; font: inherit; padding: 9px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); max-height: 160px;
}
.btn:disabled { opacity: .55; cursor: default; }
#dialog.busy .client-form button { opacity: .55; }
@media (max-width: 640px) {
  .msg { max-width: 94%; }
  .chat-box { height: calc(100vh - 300px); }
}

/* ── шаг 4: статистика ── */
.stats-page h2 { margin-bottom: 12px; }
.stats-page h3 { font-size: 14px; margin: 16px 0 8px; color: var(--muted); font-weight: 600; }
.stats-page .card { margin-bottom: 16px; }
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; padding: 14px 16px; }
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.filters select, .filters input {
  font: inherit; font-size: 14px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 140px;
}
.filters [hidden] { display: none; }

.tiles-kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-size: 26px; font-weight: 600; margin-top: 2px; }
.kpi-hero .kpi-value { font-size: 40px; line-height: 1.15; }
.kpi-note { font-size: 12px; color: var(--muted); margin-top: 2px; }

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px 24px; }
.chart { margin: 0; }
.chart figcaption { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.chart-empty { padding: 30px 0; }
.colchart { width: 100%; height: auto; display: block; overflow: visible; }
.colchart .grid { stroke: var(--border); stroke-width: 1; }
.colchart .tick { fill: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.colchart .val { fill: var(--text); font-size: 10px; font-weight: 600; }
.colchart .mark { fill: var(--accent); }
.colchart .hit { fill: transparent; }
.colchart .bar.on .hit { fill: var(--accent-soft); opacity: .6; }
.table-view { margin-top: 12px; font-size: 13px; }
.table-view summary { cursor: pointer; color: var(--accent); }
.table-pair { display: flex; gap: 24px; flex-wrap: wrap; margin-top: 8px; }
.table-pair table { width: auto; min-width: 220px; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.scen-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.hbar { display: grid; grid-template-columns: minmax(140px, 46%) 1fr auto; gap: 10px; align-items: center; margin: 6px 0; font-size: 14px; }
.hbar-label { overflow-wrap: anywhere; }
.hbar-track { width: 100%; height: 14px; display: block; }
.hbar-bg { fill: var(--bg); }
.hbar-fill { fill: var(--accent); }
.hbar-value { font-variant-numeric: tabular-nums; white-space: nowrap; min-width: 64px; text-align: right; }
table.compact td, table.compact th { padding: 6px 8px; }
.code { font-weight: 600; font-variant-numeric: tabular-nums; }
.mini-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 16px; }
.quality-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 0 28px; }
.quality-grid h3:first-child { margin-top: 0; }
.scroll-y { max-height: 420px; overflow-y: auto; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: 4px 0; border-bottom: 1px solid var(--border); }
.downloads { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
table.sortable th { cursor: pointer; user-select: none; }
table.sortable th[data-sort="asc"]::after { content: " ↑"; }
table.sortable th[data-sort="desc"]::after { content: " ↓"; }
.tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--text); color: var(--surface);
  font-size: 12px; padding: 5px 8px; border-radius: 6px; max-width: 320px;
}
@media (max-width: 640px) {
  .scen-grid, .quality-grid, .charts { grid-template-columns: 1fr; }
  .hbar { grid-template-columns: 1fr auto; }
  .hbar-track { grid-column: 1 / -1; order: 3; }
}
.tiles-kpi { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.charts-wide { margin-top: 20px; }
.charts-wide .colchart { max-height: 220px; }
.back-link { margin: 0 0 12px; }
