:root {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel-2: #f6f8fa;
  --border: #dde3ea;
  --text: #1c2430;
  --muted: #6b7786;
  --accent: #2f6fed;
  --accent-soft: #e8f0fe;
  --ok: #17874f;
  --ok-soft: #e3f5ea;
  --warn: #a86700;
  --warn-soft: #fdf1d8;
  --bad: #d43c3c;
  --bad-soft: #fde8e8;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .05), 0 4px 14px rgba(20, 30, 50, .04);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
h1 { font-size: 24px; margin: 0 0 6px; text-align: center; }
h2 { font-size: 18px; margin: 30px 0 12px; text-align: center; }
a { color: var(--accent); }
code { background: var(--panel-2); border: 1px solid var(--border); padding: 0 5px; border-radius: 5px; font-size: 13px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }
.lead { text-align: center; color: var(--muted); margin: 0 auto 18px; max-width: 620px; }

/* Иконки */
.icon { width: 18px; height: 18px; flex-shrink: 0; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vertical-align: -3px; }
.btn .icon { margin-right: 6px; }

/* Каркас: меню сбоку, содержимое — по центру оставшегося места */
.app { display: flex; min-height: 100%; }
.sidebar {
  width: 236px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--border);
  padding: 20px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { font-weight: 700; font-size: 17px; padding: 0 12px 16px; }
nav { display: flex; flex-direction: column; gap: 3px; min-height: calc(100% - 44px); }
nav a {
  color: var(--text); text-decoration: none; padding: 10px 12px; border-radius: 10px; cursor: pointer;
  white-space: nowrap; display: flex; align-items: center; gap: 10px; user-select: none;
}
nav a .icon { color: var(--muted); }
nav a:hover { background: var(--panel-2); }
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
nav a.active .icon { color: var(--accent); }
#logout { color: var(--muted); margin-top: auto; }
.badge { background: var(--bad); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; font-weight: 600; margin-left: auto; }
.content { flex: 1; min-width: 0; }
.page { max-width: 1240px; margin: 0 auto; padding: 28px 24px 90px; }
.page.wide { max-width: 1500px; }
.actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin: 14px 0 18px; }

/* Карточки и спойлеры */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 12px; box-shadow: var(--shadow); }
details.card { padding: 0; }
details.card > summary {
  list-style: none; cursor: pointer; padding: 14px 18px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  font-weight: 600; user-select: none; border-radius: var(--radius);
}
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::before {
  content: ""; width: 7px; height: 7px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg); transition: transform .15s; margin: 0 6px 0 2px; flex-shrink: 0;
}
details.card[open] > summary::before { transform: rotate(45deg); }
details.card > summary:hover { background: var(--panel-2); }
details.card[open] > summary { border-bottom: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0; }
.details-body { padding: 18px; }
.sum-title { margin-right: auto; }
.children { margin-top: 16px; padding-left: 14px; border-left: 3px solid var(--border); }
.children > details.card { box-shadow: none; }

/* Формы */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 0 12px; }
label, .field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
label, .field-label { font-size: 13px; color: var(--muted); }
/* В сетке поля выравниваются по нижнему краю: длинная подпись не сдвигает своё поле относительно соседних */
.grid > label { justify-content: flex-end; }
/* Токены и ключи: символы скрыты, но поле не считается паролем — браузер не подставляет в него логин и пароль */
input.secret { -webkit-text-security: disc; }
input.secret::placeholder { -webkit-text-security: none; }
label.check { flex-direction: row; align-items: center; gap: 10px; cursor: pointer; color: var(--text); font-size: 15px; margin-bottom: 8px; }
input, textarea, select, .editor {
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 9px;
  padding: 10px 12px; font: inherit; width: 100%;
}
/* Переключатель вместо галочки: серый — выключено, зелёный — включено */
input[type="checkbox"] {
  appearance: none; -webkit-appearance: none; width: 42px; height: 24px; flex-shrink: 0; padding: 0; margin: 0;
  border: none; border-radius: 999px; background: #c5ccd6; position: relative; cursor: pointer; transition: background .15s;
}
input[type="checkbox"]::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(20, 30, 50, .3); transition: transform .15s;
}
input[type="checkbox"]:checked { background: #22a559; }
input[type="checkbox"]:checked::after { transform: translateX(18px); }
input[type="checkbox"]:focus { outline: none; }
input[type="checkbox"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
textarea { min-height: 96px; resize: vertical; }
input:focus, textarea:focus, select:focus, .editor:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.hint { font-size: 12.5px; color: var(--muted); margin: -8px 0 14px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.center { justify-content: center; }
.spacer { flex: 1; }
.error { color: var(--bad); min-height: 20px; font-size: 13px; }

/* Редактор текста с форматированием */
.editor { min-height: 96px; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); font-size: 15px; cursor: text; }
.editor code { font-family: ui-monospace, Consolas, monospace; }
.editor a { color: var(--accent); }
.spoiler { background: #c9d0d9; border-radius: 4px; padding: 0 3px; }
.ctx {
  position: fixed; z-index: 30; background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 34px rgba(20, 30, 50, .2); padding: 5px; display: flex; flex-direction: column; min-width: 220px;
}
.ctx button { display: flex; align-items: center; gap: 8px; background: none; border: none; text-align: left; padding: 8px 12px; border-radius: 7px; font: inherit; color: var(--text); cursor: pointer; white-space: nowrap; }
.ctx button:hover { background: var(--accent-soft); }
.ctx code { border: none; background: none; padding: 0; font-size: inherit; }
/* На телефоне меню форматирования — панель внизу экрана */
.ctx.bar {
  left: 0 !important; right: 0; top: auto !important; bottom: 0; flex-direction: row; overflow-x: auto; min-width: 0;
  border-radius: 14px 14px 0 0; border-width: 1px 0 0; padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
}
.ctx.bar button { border: 1px solid var(--border); margin-right: 6px; flex-shrink: 0; }

/* Вложение */
.media { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; margin-top: 3px; }
.media img, .media video { max-width: 100%; max-height: 240px; border-radius: 9px; border: 1px solid var(--border); background: var(--panel-2); }

.btn {
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 9px;
  padding: 9px 16px; font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.danger:hover { border-color: var(--bad); color: var(--bad); }
.btn.sm { padding: 5px 11px; font-size: 13px; }
/* Кнопка-иконка (сохранить, перезапустить, удалить) — подпись во всплывающей подсказке */
.btn.icon-btn { width: 42px; height: 42px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
.btn.icon-btn .icon { margin: 0; width: 20px; height: 20px; }
.btn.icon-btn.sm { width: 34px; height: 34px; }
.btn.icon-btn.sm .icon { width: 17px; height: 17px; }
.btn.wide { width: 100%; }

.pill { border-radius: 20px; padding: 2px 10px; font-size: 12px; font-weight: 600; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.pill.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.pill.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; white-space: normal; }

/* Вход */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { width: 100%; max-width: 360px; margin: 0; padding: 26px; }
.login-card h1 { margin-bottom: 18px; }

/* Таблицы */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: center; padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 12.5px; white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td.num { white-space: nowrap; }
.row.nowrap { flex-wrap: nowrap; }
/* Колонка действий всегда видна, даже если таблица не помещается по ширине */
table.cards th:last-child, table.cards td:last-child { position: sticky; right: 0; background: var(--panel); }

/* Статистика */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 12px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; text-align: center; box-shadow: var(--shadow); }
.stat .value { font-size: 24px; font-weight: 700; }
.stat .label { color: var(--muted); font-size: 13px; }
.card h2 { margin-top: 0; }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 160px; }
.chart .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.chart .bar { width: 100%; background: var(--accent); border-radius: 5px 5px 0 0; min-height: 2px; }
.chart .bar.empty { background: var(--border); }
.chart .day { font-size: 11px; color: var(--muted); }

/* Уведомления */
.feed-item { display: flex; align-items: center; gap: 14px; padding: 12px 4px; border-bottom: 1px solid var(--border); }
.feed-item:last-child { border-bottom: none; }
.feed-icon { width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center; background: var(--accent-soft); color: var(--accent); }
.feed-icon.payment { background: var(--ok-soft); color: var(--ok); }
.feed-icon.out_of_stock { background: var(--warn-soft); color: var(--warn); }
.feed-icon.error { background: var(--bad-soft); color: var(--bad); }
.feed-body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.feed-item .title { font-weight: 600; }
.feed-item.unread .title::after { content: "новое"; margin-left: 8px; font-size: 11px; color: #fff; background: var(--accent); border-radius: 10px; padding: 1px 7px; }
.feed-item > .btn.icon-btn { flex-shrink: 0; border-color: transparent; background: none; }
.feed-item > .btn.icon-btn:hover { background: var(--bad-soft); }
.feed-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; text-align: right; }

/* Фильтры-чипы */
.chips { display: flex; justify-content: center; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--panel); border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; font: inherit; font-size: 14px; color: var(--text); cursor: pointer; white-space: nowrap; }
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

/* Две колонки: основная форма и боковая панель */
.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 400px); gap: 22px; align-items: start; }
.split.even { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: stretch; }
.split.even > .field > .editor { flex: 1; }
.panel { background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 14px; }
.panel-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; font-weight: 600; }
.form-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 4px; }
.form-foot label.check { margin: 0; }

/* Ключи товара */
.keys-panel textarea { min-height: 76px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.keys-panel form label { margin-bottom: 8px; }
.seg { display: flex; background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 3px; margin: 14px 0 8px; }
.seg button { flex: 1; background: none; border: none; border-radius: 7px; padding: 6px 8px; font: inherit; font-size: 13.5px; color: var(--muted); cursor: pointer; }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.keys-list { max-height: 340px; overflow-y: auto; }
.key-row { display: flex; align-items: center; gap: 6px; padding: 5px 0; border-bottom: 1px solid var(--border); }
.key-row:last-child { border-bottom: none; }
.key-row code { flex: 1; min-width: 0; background: none; border: none; padding: 0; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.key-row .btn.icon-btn.sm { width: 30px; height: 30px; border-color: transparent; background: none; }
.key-row .btn.icon-btn.sm:hover { background: var(--panel); border-color: var(--border); }

.toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 40; width: min(420px, calc(100vw - 24px)); }
.toast { background: var(--text); color: #fff; border-radius: 10px; padding: 11px 16px; box-shadow: 0 8px 26px rgba(20, 30, 50, .28); text-align: center; }
.toast.bad { background: var(--bad); }
.toast.info { background: var(--accent); }

/* Телефон */
@media (max-width: 700px) {
  body { font-size: 16px; }
  /* меню становится прокручиваемой полосой сверху */
  .app { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: sticky; top: 0; z-index: 5; padding: 0; border-right: none; border-bottom: 1px solid var(--border); box-shadow: var(--shadow); overflow: visible; }
  .brand { display: none; }
  nav { flex-direction: row; min-height: 0; overflow-x: auto; padding: 8px 10px; gap: 4px; scrollbar-width: none; }
  nav::-webkit-scrollbar { display: none; }
  nav a { padding: 8px 12px; gap: 7px; }
  #logout { margin-top: 0; }
  .badge { margin-left: 2px; }
  .page { padding: 18px 10px 110px; }
  .feed-item { align-items: flex-start; flex-wrap: wrap; }
  .feed-meta { flex-direction: row; align-items: center; width: 100%; padding-left: 52px; order: 3; }
  .feed-item > .btn.icon-btn { width: 34px; height: 34px; }
  h1 { font-size: 21px; }
  .card { padding: 14px; }
  details.card > summary { padding: 13px 14px; }
  .details-body { padding: 14px; }
  .children { padding-left: 8px; }
  .grid { grid-template-columns: 1fr; }
  input, textarea, select, .editor { font-size: 16px; } /* иначе iOS увеличивает страницу при вводе */
  .btn { padding: 11px 16px; }
  .btn.sm { padding: 8px 12px; }
  .form-buttons .btn:not(.icon-btn) { flex: 1 1 100%; }
  .btn.icon-btn { width: 48px; height: 48px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 12px 8px; }
  .stat .value { font-size: 19px; }

  /* Таблицы превращаются в карточки */
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr.head { display: none; }
  table.cards tr { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; }
  table.cards td { border: none; padding: 5px 0; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; }
  table.cards td::before { content: attr(data-label); color: var(--muted); font-size: 13px; text-align: left; flex-shrink: 0; }
  table.cards td:empty { display: none; }
  table.cards td:last-child { position: static; }
  table.cards td:not([data-label]), table.cards td[data-label=""] { justify-content: center; text-align: center; }
}

/* Узкий экран: колонки складываются одна под другую */
@media (max-width: 980px) {
  .split, .split.even { grid-template-columns: 1fr; gap: 6px; }
}
