:root {
  color-scheme: light dark;
  --bg: #f6f5f2;
  --panel: #ffffff;
  --panel-2: #f0eee9;
  --fg: #1b1a18;
  --muted: #6b675f;
  --line: #e2dfd8;
  --accent: #2f5bd3;
  --accent-fg: #ffffff;
  --ok: #1f7a4d;
  --ok-bg: #e3f3ea;
  --info: #2a5fb0;
  --info-bg: #e5eefb;
  --warn: #8a5a00;
  --warn-bg: #fbf0d9;
  --bad: #b3261e;
  --bad-bg: #fbe5e3;
  --idle: #5f5b53;
  --idle-bg: #ecebe7;
  --shadow: 0 1px 2px rgb(0 0 0 / 5%), 0 2px 8px rgb(0 0 0 / 4%);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131312;
    --panel: #1c1c1a;
    --panel-2: #242421;
    --fg: #ecebe6;
    --muted: #a19d94;
    --line: #33322e;
    --accent: #7ea1ff;
    --accent-fg: #0f1320;
    --ok: #6fd3a0;
    --ok-bg: #173226;
    --info: #8db4ff;
    --info-bg: #1b2740;
    --warn: #f0c36a;
    --warn-bg: #3a2e14;
    --bad: #ff8f86;
    --bad-bg: #3d1c1a;
    --idle: #b5b1a8;
    --idle-bg: #2a2a27;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

a { color: var(--accent); }

.app { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh;
        background: linear-gradient(to right, var(--panel) 220px, transparent 220px); }

/* ── Навигация ─────────────────────────────────────────── */

.nav {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 0 8px 18px; }
.brand img { border-radius: 6px; }
/* Баланс Рег.облака — только администратору (GET /overview/balance). */
/* Балансы провайдеров — столбиком справа от логотипа. */
.balances { margin-left: auto; display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.balance { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums;
  padding: 2px 8px; border-radius: 999px; background: var(--panel-2); color: var(--muted); white-space: nowrap;
}
.balance { text-decoration: none; }
.balance:hover { color: var(--fg); }
.balance.low { background: var(--warn-bg); color: var(--warn); }
.costs-note { margin: 4px 0 0; padding-left: 18px; color: var(--muted); font-size: 13.5px; }
.money { font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.balance.unknown { font-weight: 400; }
.balance .who { font-weight: 400; opacity: .75; margin-right: 4px; }

#menu { display: flex; flex-direction: column; gap: 2px; }

#menu a {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--fg);
  text-decoration: none;
}
#menu a:hover { background: var(--panel-2); }
#menu a[aria-current="page"] { background: var(--panel-2); font-weight: 600; }
#menu .count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.nav-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 0 10px; font-size: 14px; }

/* ── Основная колонка ──────────────────────────────────── */

.main { padding: 24px 28px 48px; min-width: 0; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.top h1 { margin: 0; font-size: 22px; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

h2 { font-size: 16px; margin: 28px 0 10px; }
h2:first-child { margin-top: 0; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-size: 13px; }
.nowrap { white-space: nowrap; }

/* ── Плитки ────────────────────────────────────────────── */

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }

.tile {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.tile .label { color: var(--muted); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tile.bad .value { color: var(--bad); }
.tile.text .value { font-size: 16px; font-weight: 600; line-height: 1.35; margin-top: 6px; }
.tile.ok .value { color: var(--ok); }

/* ── Панели и таблицы ──────────────────────────────────── */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel.pad { padding: 16px; }

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; background: var(--panel-2); }
tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--panel-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td .clip { display: block; max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { padding: 22px 16px; color: var(--muted); text-align: center; }

/* ── Метки состояний ───────────────────────────────────── */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 550;
  white-space: nowrap;
  background: var(--idle-bg);
  color: var(--idle);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }

/* ── Кнопки и формы ────────────────────────────────────── */

button, .btn {
  font: inherit;
  font-size: 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--fg);
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: var(--panel-2); }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; padding: 0; color: var(--accent); text-align: left; font-size: 14px; }
button.small { padding: 3px 9px; font-size: 13px; }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.chip[aria-pressed="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }

form.stack { display: grid; gap: 12px; max-width: 520px; }
label.field { display: grid; gap: 4px; font-weight: 600; font-size: 14px; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 12.5px; }
input, select, textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  width: 100%;
  font-weight: 400;
}
textarea { min-height: 90px; resize: vertical; }
.form-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.form-error { background: var(--bad-bg); color: var(--bad); padding: 8px 12px; border-radius: 8px; font-size: 14px; }
.form-ok { background: var(--ok-bg); color: var(--ok); padding: 8px 12px; border-radius: 8px; font-size: 14px; white-space: pre-wrap; }

/* ── Карточки секретов ─────────────────────────────────── */

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px; }
.card h3 { margin: 0; font-size: 15px; }
.card .desc { color: var(--muted); font-size: 13px; margin: 0; }
.card .foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: auto; flex-wrap: wrap; }

/* ── Детали ────────────────────────────────────────────── */

dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: 13px; }
dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

pre.json {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 12px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  max-height: 360px;
  margin: 0;
}

.drawer-backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / 30%); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(640px, 100%);
  background: var(--panel); border-left: 1px solid var(--line); z-index: 21;
  padding: 20px 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px;
}
/* Панель — колонка фиксированной высоты: без этого длинное содержимое сжимает блоки до нуля
   вместо того, чтобы прокручиваться. */
.drawer > * { flex-shrink: 0; }
.drawer-head { display: flex; justify-content: space-between; align-items: start; gap: 12px; }
.drawer-head h2 { margin: 0; }

/* Окно по центру — для коротких действий с выбором («Добавить проект»). Панель справа — для
   просмотра карточки. */
.modal-backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / 35%); z-index: 22;
                  display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; overflow-y: auto; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(560px, 100%); padding: 20px 22px; display: flex; flex-direction: column; gap: 16px;
}
.modal form.stack, .modal .stack { max-width: none; }
.modal div.field { display: grid; gap: 4px; font-weight: 600; font-size: 14px; }
.modal .chips { margin: 0; }
.modal .field .chips { margin-top: 2px; font-weight: 400; }
.modal-sub { margin: 4px 0 0; font-size: 15px; }
div.stack { display: grid; gap: 12px; }
div.stack[hidden] { display: none; }
.site-list { display: grid; gap: 6px; margin-top: 8px; }
.site-option { display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: start; padding: 8px 10px;
               border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-size: 14px; }
.site-option > span { display: grid; gap: 2px; overflow-wrap: anywhere; }
.site-option input { width: auto; margin-top: 3px; }
.site-option:has(input:checked) { border-color: var(--accent); background: var(--info-bg); }
.site-option.disabled { opacity: .6; cursor: default; }

.notice { background: var(--warn-bg); color: var(--warn); padding: 10px 14px; border-radius: 8px; font-size: 14px; margin-bottom: 14px; }
.questions { margin: 6px 0 0; padding-left: 20px; display: grid; gap: 4px; }
.clarify { display: grid; gap: 8px; }
.clarify .chips { margin-bottom: 0; }
.clarify textarea { width: 100%; box-sizing: border-box; }
.clarify-step { font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin-top: 4px; }
.clarify-question { margin: 0; font-size: 15px; font-weight: 600; color: var(--fg); }
.secret-once { background: var(--ok-bg); border-radius: 8px; padding: 12px; display: grid; gap: 8px; }
.secret-once code { font-family: var(--mono); font-size: 13px; overflow-wrap: anywhere; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  max-width: min(560px, calc(100% - 32px)); z-index: 30; font-size: 14px; box-shadow: var(--shadow);
}
.toast.bad { background: var(--bad); color: #fff; }

.login { max-width: 420px; margin: 12vh auto; text-align: center; }
.login h1 { font-size: 22px; }
.login code { font-family: var(--mono); background: var(--panel-2); padding: 2px 6px; border-radius: 4px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.split { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; }
code.inline { font-family: var(--mono); font-size: .92em; background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }

/* ── Телефон ───────────────────────────────────────────── */

@media (max-width: 1100px) {
  .split { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); background: none; }
  .nav { min-width: 0; position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 10px 16px; }
  .brand { padding: 0 0 8px; }
  #menu { flex-direction: row; overflow-x: auto; gap: 4px; }
  #menu a { white-space: nowrap; padding: 6px 10px; }
  #menu .count { margin-left: 6px; }
  .nav-foot { flex-direction: row; gap: 16px; padding: 8px 0 0; margin-top: 0; }
  .main { padding: 16px 16px 40px; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  td .clip { max-width: 220px; }
}

/* Уведомления: итоги задач и алерты платформы — текстом как есть, с переносами. */
.notes { display: grid; gap: 10px; }
.note.unread { border-left: 3px solid var(--accent); }

/* Этапы задачи: кружочки. Пройден — зелёный, текущий — по статусу (идёт — синий, ждёт человека —
   жёлтый, готово — зелёный), остановились — красный, впереди — пустой, пропущен — пунктир. */
.stage-dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; box-sizing: border-box;
             border: 2px solid var(--line); background: transparent; flex: none; }
.stage-dot.done { background: var(--ok); border-color: var(--ok); }
.stage-dot.failed { background: var(--bad); border-color: var(--bad); }
.stage-dot.skipped { border-style: dashed; border-color: var(--muted); opacity: .55; }
.stage-dot.current { border-color: var(--info); background: var(--info-bg); box-shadow: 0 0 0 3px var(--info-bg); }
.stage-dot.current.status-running { background: var(--info); animation: stage-pulse 1.6s ease-in-out infinite; }
.stage-dot.current.status-waiting,
.stage-dot.current.status-clarify { border-color: var(--warn); background: var(--warn); box-shadow: 0 0 0 3px var(--warn-bg); }
.stage-dot.current.status-finished { border-color: var(--ok); background: var(--ok); box-shadow: none; }
@keyframes stage-pulse { 50% { box-shadow: 0 0 0 5px var(--info-bg); } }
@media (prefers-reduced-motion: reduce) { .stage-dot.current.status-running { animation: none; } }

.stage-chain { display: inline-flex; align-items: center; }
.stage-item { display: inline-flex; align-items: center; padding: 4px 0; cursor: default; }
.stage-item + .stage-item::before { content: ""; width: 10px; height: 2px; background: var(--line); margin: 0 2px; }

.stage-list { list-style: none; margin: 6px 0 0; padding: 0; }
.stage-row { display: grid; grid-template-columns: 12px auto 1fr auto; align-items: center; gap: 10px;
             position: relative; padding: 5px 0; }
.stage-row + .stage-row::before { content: ""; position: absolute; left: 5px; top: -7px; height: 14px;
                                  width: 2px; background: var(--line); }
.stage-row.pending .stage-label, .stage-row.skipped .stage-label { color: var(--muted); }
.stage-row.skipped .stage-label { text-decoration: line-through; }
.stage-row.current .stage-label, .stage-row.failed .stage-label { font-weight: 600; }
.stage-state { justify-self: start; }
.stage-time { justify-self: end; font-variant-numeric: tabular-nums; }

/* Markdown в уведомлениях. */
.md { font-size: 14px; line-height: 1.5; overflow-wrap: anywhere; margin-top: 6px; }
.md > * { margin: 0 0 8px; }
.md > *:last-child { margin-bottom: 0; }
.md .md-h { margin: 12px 0 6px; font-size: 14.5px; }
.md h5.md-h { font-size: 14px; color: var(--muted); }
.md .md-list { padding-left: 20px; }
.md .md-list li + li { margin-top: 2px; }
.md code { font-family: var(--mono); font-size: 12.5px; background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }
.md .md-code { background: var(--panel-2); padding: 8px 10px; border-radius: 6px; overflow-x: auto; }
.md .md-code code { background: none; padding: 0; white-space: pre; }
.md .md-table { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 13px; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.md th { background: var(--panel-2); }
