:root {
  --bg: #f3f4f6;
  --card: #fff;
  --border: #dfe3e8;
  --text: #1b1f27;
  --muted: #667085;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --accent-soft: #e8f0fe;
  --ok: #157347;
  --ok-soft: #e6f4ec;
  --warn: #9a5b00;
  --warn-soft: #fff4dc;
  --bad: #c42b1c;
  --bad-soft: #fdeceb;
  --radius: 12px;
  --nav-h: 60px;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
}
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45; overflow-x: hidden; }
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid rgba(37, 99, 235, .45); outline-offset: 2px; }
.noscript { padding: 16px; }

/* Каркас */
.topbar { background: var(--card); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 0 16px; min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.brand { font-weight: 600; font-size: 16px; }
.nav { display: flex; gap: 4px; }
.nav-link { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 14px; border-radius: 10px; color: var(--muted); text-decoration: none; font-weight: 500; }
.nav-link:hover { background: var(--bg); }
.nav-link[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
.page { max-width: 1100px; margin: 0 auto; padding: 16px 16px calc(var(--nav-h) + 24px); }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; font-weight: 600; }
.page-head-actions { display: flex; gap: 8px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.stack-sm { display: flex; flex-direction: column; gap: 12px; }
.center { display: flex; justify-content: center; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.hint { color: var(--muted); font-size: 14px; }
.icon { flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

@media (max-width: 767px) {
  .nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--card); border-top: 1px solid var(--border); justify-content: space-around; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); gap: 0; }
  .nav-link { flex: 1; flex-direction: column; gap: 2px; justify-content: center; min-height: 52px; padding: 4px 2px; font-size: 12px; text-align: center; }
  .topbar-inner { justify-content: center; }
  body.is-editor .nav { display: none; }
}

/* Карточки и блоки */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.block-head { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.block-num { flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 600; display: inline-flex; align-items: center; justify-content: center; font-size: 14px; }
.block-title { font-size: 17px; font-weight: 600; }
.block-desc { color: var(--muted); font-size: 14px; margin-top: 2px; }
.block-body { display: flex; flex-direction: column; gap: 14px; }
.block > .block-title { margin-bottom: 8px; }
.block > .hint { margin-bottom: 8px; }

/* Кнопки */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { background: var(--bg); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #a82217; }
.btn-danger-ghost { color: var(--bad); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-small { min-height: 40px; padding: 0 12px; font-size: 14px; }
.btn-icon { width: 44px; padding: 0; flex: none; }
.btn.is-busy { position: relative; }
.btn.is-busy::after { content: ""; width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }

/* Формы */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.label { font-weight: 500; font-size: 14px; }
.input { width: 100%; min-height: 44px; padding: 10px 12px; border: 1px solid #c8ced6; border-radius: 10px; background: #fff; font-size: 16px; }
textarea.input { resize: vertical; min-height: 76px; line-height: 1.4; }
.input:focus { border-color: var(--accent); outline: 3px solid rgba(37, 99, 235, .25); outline-offset: 0; }
.input.has-error, .chips-box.has-error { border-color: var(--bad); background: #fffafa; }
.field-meta { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.counter { color: var(--muted); font-size: 13px; white-space: nowrap; margin-left: auto; }
.counter.over { color: var(--bad); font-weight: 600; }
.field-error { color: var(--bad); font-size: 14px; }
.select { appearance: auto; }

.chips-box { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px; border: 1px solid #c8ced6; border-radius: 10px; background: #fff; min-height: 48px; align-items: center; cursor: text; }
.chips-box:focus-within { border-color: var(--accent); outline: 3px solid rgba(37, 99, 235, .25); }
.chips-list { display: contents; }
.chips-input { flex: 1 1 120px; min-width: 0; min-height: 36px; border: 0; outline: 0; background: transparent; padding: 0 6px; font-size: 16px; }
.chip { display: inline-flex; align-items: center; gap: 2px; background: var(--accent-soft); color: var(--accent-dark); border-radius: 999px; padding: 0 4px 0 12px; min-height: 34px; font-weight: 500; max-width: 100%; }
.chip-text { overflow-wrap: anywhere; }
.chip-x { width: 32px; height: 32px; border: 0; background: transparent; border-radius: 50%; color: inherit; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.chip-x:hover { background: rgba(37, 99, 235, .15); }
.chip-static { padding: 0 12px; min-height: 28px; font-size: 14px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }

.segmented { display: flex; flex-wrap: wrap; gap: 8px; }
.seg { min-height: 44px; padding: 0 14px; border: 1px solid #c8ced6; background: #fff; border-radius: 10px; cursor: pointer; font-weight: 500; }
.seg[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); }

.switch { flex: none; width: 48px; height: 28px; border-radius: 999px; border: 0; background: #c3c9d2; padding: 2px; cursor: pointer; position: relative; transition: background .15s; }
.switch-knob { display: block; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"] .switch-knob { transform: translateX(20px); }
.switch:disabled { opacity: .6; }
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; min-height: 44px; }
.switch-text { display: flex; flex-direction: column; gap: 2px; }

/* Варианты */
.variant-list { display: flex; flex-direction: column; gap: 12px; }
.variant { display: flex; gap: 8px; align-items: flex-start; }
.variant .field { flex: 1; }
.variant .btn-icon { margin-top: 24px; }
.variants-foot { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.variants-info { font-size: 14px; color: var(--muted); }
.variants-info.warn { color: var(--warn); font-weight: 500; }
.link-row { display: flex; gap: 8px; align-items: flex-start; padding: 12px; border: 1px solid var(--border); border-radius: 10px; }
.link-fields { flex: 1; display: grid; grid-template-columns: 1fr; gap: 10px; min-width: 0; }
.link-row .btn-icon { margin-top: 24px; }
.test-box { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; align-items: end; padding: 12px; background: var(--bg); border-radius: 10px; }
.test-box .btn { margin-bottom: 0; }
.test-result { grid-column: 1 / -1; font-weight: 600; min-height: 1.4em; }
.test-result.ok { color: var(--ok); }
.test-result.bad { color: var(--bad); }

.notice { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 10px; background: var(--accent-soft); color: #1e3a8a; font-size: 14px; }
.notice .icon { margin-top: 2px; }
.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice-bad { background: var(--bad-soft); color: var(--bad); margin-bottom: 16px; }

.advanced { padding: 0; }
.advanced summary { display: flex; justify-content: space-between; align-items: center; min-height: 52px; padding: 0 16px; cursor: pointer; font-weight: 600; font-size: 16px; list-style: none; }
.advanced summary::-webkit-details-marker { display: none; }
.advanced[open] summary .icon { transform: rotate(180deg); }
.advanced-body { padding: 0 16px 16px; }

/* Редактор */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; padding-bottom: 80px; }
.editor-aside { min-width: 0; }
.preview-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 4px; }
.action-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; background: var(--card); border-top: 1px solid var(--border); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.action-bar-inner { max-width: 1100px; margin: 0 auto; display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 767px) { .action-bar-inner .btn { flex: 1; } }
@media (min-width: 1024px) {
  .editor-grid { grid-template-columns: minmax(0, 1fr) 380px; align-items: start; }
  .editor-aside { position: sticky; top: 16px; }
}

/* Предпросмотр */
.chat { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; background: var(--bg); border-radius: 10px; padding: 12px; }
.chat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin-top: 6px; }
.chat-note { font-size: 13px; color: var(--muted); font-style: italic; }
.comment { display: flex; gap: 8px; align-items: flex-start; }
.comment-reply { margin-left: 28px; }
.comment-body { background: #fff; border-radius: 12px; padding: 8px 12px; display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; }
.comment-user { font-weight: 600; font-size: 13px; }
.avatar { flex: none; width: 28px; height: 28px; border-radius: 50%; background: #c3c9d2; color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 600; }
.avatar-me { background: var(--accent); }
.bubble { align-self: flex-start; max-width: 90%; background: #fff; border-radius: 14px 14px 14px 4px; padding: 9px 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble-empty { color: var(--muted); font-style: italic; }
.bubble-buttons { display: flex; flex-direction: column; gap: 4px; max-width: 90%; align-self: flex-start; width: 240px; }
.bubble-btn { display: flex; align-items: center; justify-content: center; gap: 6px; background: #fff; color: var(--accent); font-weight: 600; border-radius: 10px; padding: 8px 12px; text-align: center; overflow-wrap: anywhere; }

/* Медиа */
.media-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; min-height: 40px; }
.media-selected { font-weight: 500; }
.media-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 8px 0 12px; }
@media (min-width: 560px) { .media-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 1024px) { .media-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.media-tile { display: flex; flex-direction: column; gap: 4px; text-align: left; padding: 6px; border: 2px solid var(--border); border-radius: 12px; background: #fff; cursor: pointer; min-width: 0; align-items: stretch; }
.media-tile[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.media-pic { position: relative; }
.thumb { position: relative; width: 100%; aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: #e5e8ec; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-ph { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; color: var(--muted); font-size: 12px; }
.thumb-sm { width: 56px; height: 56px; aspect-ratio: auto; flex: none; }
.media-type { position: absolute; left: 6px; top: 6px; background: rgba(27, 31, 39, .75); color: #fff; font-size: 11px; padding: 2px 6px; border-radius: 6px; }
.media-check { position: absolute; right: 6px; top: 6px; width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 2px solid #c3c9d2; color: transparent; display: flex; align-items: center; justify-content: center; }
.media-tile[aria-pressed="true"] .media-check { background: var(--accent); border-color: var(--accent); color: #fff; }
.media-caption { font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; min-height: 2.8em; }
.media-date { font-size: 12px; color: var(--muted); }
.media-used { font-size: 12px; color: var(--warn); font-weight: 500; }

/* Список автоматизаций */
.sc { display: flex; flex-direction: column; gap: 12px; }
.sc-off .sc-name { color: var(--muted); }
.sc-head { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.sc-name { font-size: 17px; font-weight: 600; overflow-wrap: anywhere; }
.sc-switch { display: flex; align-items: center; gap: 8px; flex: none; }
.sw-label { font-size: 13px; color: var(--muted); }
.sc-media { display: flex; align-items: center; gap: 8px; color: var(--muted); flex-wrap: wrap; }
.more-count { font-weight: 600; color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat { background: var(--bg); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; min-width: 0; }
.stat-n { font-size: 20px; font-weight: 600; }
.stat-l { font-size: 12px; color: var(--muted); }
.stat-bad .stat-n { color: var(--bad); }
.sc-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.status-top { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.status-account { font-size: 18px; font-weight: 600; overflow-wrap: anywhere; }
.status-lines { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.status-lines li { display: flex; gap: 6px; align-items: center; }
.status-lines .warn { color: var(--warn); font-weight: 500; }
.pill { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px; font-size: 13px; font-weight: 500; white-space: nowrap; }
.pill-ok { background: var(--ok-soft); color: var(--ok); }
.pill-warn { background: var(--warn-soft); color: var(--warn); }
.pill-bad { background: var(--bad-soft); color: var(--bad); }
.pill-info { background: var(--accent-soft); color: var(--accent-dark); }
.pill-muted { background: #eceff3; color: var(--muted); }
.state { display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; padding: 28px 16px; color: var(--muted); }
.state-title { font-size: 17px; font-weight: 600; color: var(--text); }
.steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; text-align: left; max-width: 420px; color: var(--text); }
.steps li { display: flex; gap: 10px; align-items: flex-start; }

/* Журнал */
.filter { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; max-width: 420px; }
.ev-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.ev { display: flex; flex-direction: column; gap: 6px; }
.ev-top { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; align-items: center; }
.ev-time { color: var(--muted); font-size: 13px; }
.ev-who { display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.ev-scn { color: var(--muted); font-size: 14px; }
.ev-text { overflow-wrap: anywhere; }
.ev-reply, .ev-detail { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.ev-detail { color: var(--bad); }
.ev-k { font-weight: 500; }

/* Вход */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.login-card h1 { font-size: 22px; font-weight: 600; }

/* Состояния и тосты */
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 12px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.skeletons { display: flex; flex-direction: column; gap: 12px; }
.skeleton-card { display: flex; flex-direction: column; gap: 10px; }
.sk { background: #e9ecf0; border-radius: 6px; }
.sk-line { height: 14px; }
.sk-w40 { width: 40%; } .sk-w55 { width: 55%; } .sk-w70 { width: 70%; }
.media-loading .skeleton-card { min-height: 80px; }
.toasts { position: fixed; left: 12px; right: 12px; bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); z-index: 40; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
body.is-editor .toasts { bottom: 84px; }
.toast { pointer-events: auto; max-width: 480px; width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; border-radius: 10px; background: #1f2937; color: #fff; box-shadow: 0 4px 14px rgba(0, 0, 0, .2); }
.toast-ok { background: var(--ok); }
.toast-error { background: var(--bad); }
.toast-action { min-height: 36px; padding: 0 12px; border: 1px solid rgba(255, 255, 255, .6); background: transparent; color: #fff; border-radius: 8px; cursor: pointer; font-weight: 600; }
@media (min-width: 768px) { .toasts { bottom: 16px; } body.is-editor .toasts { bottom: 84px; } .page { padding-bottom: 32px; } }

.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(17, 24, 39, .5); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: #fff; border-radius: var(--radius); padding: 20px; width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 12px; }
.modal:focus { outline: none; }
.modal-title { font-size: 18px; font-weight: 600; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
@media (max-width: 480px) { .modal-actions .btn { flex: 1; white-space: normal; } }
