/* ============================================================
   Conturo UI — дизайн-система платформы.

   ЭТОТ ФАЙЛ — ЕДИНСТВЕННЫЙ источник внешнего вида: и панели, и витрины, и
   продуктов. Продукты подключают его одной строкой:

       <link rel="stylesheet" href="/route/pub/ui/conturo.css">

   Почему один файл на всех, а не копия в каждом продукте: копии разъезжаются.
   Обновили панель — у продуктов остались старые стили, и платформа снова
   выглядит как набор разных программ. Одна правда — одно место.

   Порядок в файле:
     1. токены (тёмная тема по умолчанию + светлая)
     2. база (сброс, типографика, скроллбары)
     3. компоненты (card, btn, badge, table, form, toast, empty)
     4. утилиты (muted, mono, row, stack)

   ПРАВИЛО: НЕ объявляй свои --bg/--surface/--accent. Если нужно своё — заведи
   новый токен с префиксом продукта (--myapp-chart-color). Единство токенов
   проверяет tests/test_design_system.py.

   ОВЕРРАЙД — ЭТО ШТАТНЫЙ ПУТЬ, А НЕ ИСКЛЮЧЕНИЕ. Три уровня, от мягкого к полному:

   1. ПЕРЕКРАСИТЬ ТОКЕНЫ (рекомендуется). Подключи этот файл, затем свой — и
      переопредели в нём только то, что нужно:

          <link rel="stylesheet" href="/route/pub/ui/conturo.css">
          <link rel="stylesheet" href="static/style.css">

          /* static/style.css */
          :root { --accent: #16a34a; --radius: 4px; }

      Компоненты (.card, .btn, .table) останутся платформенными, но в твоих
      цветах и с твоей геометрией. Так продукт выглядит частью платформы и при
      этом своим.

   2. ПЕРЕОПРЕДЕЛИТЬ КОМПОНЕНТ. Тот же порядок подключения, но правило сильнее
      по специфичности — например `.card { border-radius: 0; }`. Работает, но
      помни: при обновлении платформы компонент может измениться, и твоё правило
      наложится на новую версию.

   3. ОТКАЗАТЬСЯ ЦЕЛИКОМ. В service.yaml продукта:

          inject: false        # платформа не подставляет свои скрипты
          ui: none             # платформа не подставляет свои стили

      Продукт живёт со своим дизайном. Платформа не навязывает вид — она даёт
      общий по умолчанию. Но и «выглядеть одинаково» тогда не обещает: это
      осознанный выбор владельца продукта, а не ошибка.

   Тема (светлая/тёмная) и брендинг приезжают через conturo.js. Если продукт
   отказался от него (inject: false), он должен сам поставить data-theme на
   <html> — иначе останется в тёмной теме по умолчанию.
   ============================================================ */

/* ---------------------------------------------------------- 1. ТОКЕНЫ */

:root {
  /* Поверхности: от фона к самому верхнему слою. */
  --bg: #0a0f1a;
  --bg-soft: #0e1526;
  --surface: #111a2b;
  --surface-2: #16223a;
  --surface-3: #1c2b49;

  /* Границы. */
  --border: #223250;
  --border-soft: #1a2740;

  /* Текст: основной, приглушённый, еле видный. */
  --text: #e7eef9;
  --text-dim: #94a6c3;
  --text-faint: #64748d;

  /* Акцент: задаётся брендингом инстанса (см. brand.json). */
  --accent: #4f8ff7;
  --accent-2: #7aa8ff;
  --accent-soft: rgba(79, 143, 247, 0.14);
  --accent-contrast: #ffffff;

  /* Состояния. */
  --ok: #34d399;
  --ok-bg: rgba(52, 211, 153, 0.12);
  --warn: #fbbf24;
  --warn-bg: rgba(251, 191, 36, 0.12);
  --bad: #f87171;
  --bad-bg: rgba(248, 113, 113, 0.13);

  /* Геометрия и тени. */
  --radius: 12px;
  --radius-s: 8px;
  --radius-xs: 6px;
  --shadow-1: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-2: 0 16px 44px rgba(0, 0, 0, 0.5);

  /* Типографика. */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Consolas, monospace;
  --font-size: 14.5px;
  --line-height: 1.5;

  /* Плотность: продукт может сделать интерфейс компактнее одним атрибутом. */
  --gap: 12px;
  --pad: 16px;

  color-scheme: dark;
}

/* Светлая тема. Ставится атрибутом на <html>: data-theme="light".
   Отдельный набор токенов, а не фильтр — иначе цвета выглядят грязно. */
html[data-theme="light"] {
  --bg: #f6f8fc;
  --bg-soft: #eef2f9;
  --surface: #ffffff;
  --surface-2: #f4f7fc;
  --surface-3: #e8eef8;

  --border: #d3dce9;
  --border-soft: #e3eaf3;

  --text: #16202e;
  --text-dim: #55637a;
  --text-faint: #8494a9;

  /* Акцент в светлой теме темнее: на белом фоне исходный слишком бледный
     (контраст с текстом падает). Брендинг переопределяет оба токена. */
  --accent: #2f6fd0;
  --accent-2: #2f6fd0;
  --accent-soft: rgba(47, 111, 208, 0.12);

  --ok: #0f9d63;
  --ok-bg: rgba(15, 157, 99, 0.12);
  --warn: #b7791f;
  --warn-bg: rgba(183, 121, 31, 0.13);
  --bad: #d64545;
  --bad-bg: rgba(214, 69, 69, 0.11);

  --shadow-1: 0 1px 2px rgba(16, 32, 56, 0.08);
  --shadow-2: 0 16px 40px rgba(16, 32, 56, 0.14);

  color-scheme: light;
}

/* Компактный режим: data-density="compact" на <html>. */
html[data-density="compact"] {
  --gap: 8px;
  --pad: 12px;
  --font-size: 13.5px;
}

/* ------------------------------------------------------------ 2. БАЗА */

*, *::before, *::after { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  font: var(--font-size) / var(--line-height) var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* Панель и витрина используют «глубокий» фон с подсветкой. Продуктам он не
   обязателен: у них может быть своя композиция. */
body.cu-app {
  background:
    radial-gradient(1100px 700px at 85% -15%, var(--accent-soft), transparent 60%),
    radial-gradient(900px 700px at -10% 115%, rgba(139, 92, 246, 0.05), transparent 60%),
    var(--bg);
  background-attachment: fixed;
}

::selection { background: var(--accent-soft); }

* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: content-box; }

h1, h2, h3, h4 { margin: 0 0 0.5em; line-height: 1.25; font-weight: 650; }
h1 { font-size: 1.45em; }
h2 { font-size: 1.2em; }
h3 { font-size: 1.05em; }
p { margin: 0 0 0.8em; }

a { color: var(--accent-2); text-decoration: none; }
a:hover { text-decoration: underline; }

code, pre, kbd { font-family: var(--mono); font-size: 0.92em; }
code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: var(--radius-xs);
}
pre {
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-s);
  padding: var(--pad);
  overflow: auto;
}
pre code { background: none; padding: 0; }

hr { border: 0; border-top: 1px solid var(--border-soft); margin: var(--gap) 0; }

img, svg { max-width: 100%; }

/* ------------------------------------------------------- 3. КОМПОНЕНТЫ */

/* --- карточка ------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: var(--pad);
  box-shadow: var(--shadow-1);
}
.card + .card { margin-top: var(--gap); }
.card > h3:first-child { margin-top: 0; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: var(--gap);
}

/* --- кнопка --------------------------------------------------------- */
.btn,
button {
  font: inherit;
  font-weight: 550;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: var(--radius-s);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s, filter 0.12s;
  white-space: nowrap;
}
.btn:hover, button:hover { border-color: var(--accent); }
.btn:active, button:active { transform: translateY(1px); }
.btn:disabled, button:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.btn-primary, button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
.btn-primary:hover, button.primary:hover { filter: brightness(1.08); }

.btn-ghost { background: transparent; }
.btn-danger { border-color: var(--bad); color: var(--bad); }
.btn-sm { padding: 4px 9px; font-size: 0.92em; }

/* --- бейдж ---------------------------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.85em;
  font-weight: 550;
  background: var(--surface-3);
  color: var(--text-dim);
  border: 1px solid var(--border-soft);
  white-space: nowrap;
}
.badge.ok, .badge-ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.badge.warn, .badge-warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.badge.err, .badge-err { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent-2); border-color: transparent; }

/* --- таблица -------------------------------------------------------- */
.table, table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.97em;
}
.table th, table th {
  text-align: left;
  font-weight: 600;
  color: var(--text-dim);
  font-size: 0.9em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table td, table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: top;
}
.table tbody tr:hover, table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td, table tbody tr:last-child td { border-bottom: 0; }

/* --- формы ---------------------------------------------------------- */
.form-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--gap);
}
.form-row > label { color: var(--text-dim); font-size: 0.93em; }

.input, .select, .textarea,
input[type="text"], input[type="number"], input[type="password"],
input[type="email"], input[type="search"], input[type="url"], select, textarea {
  font: inherit;
  padding: 7px 10px;
  border-radius: var(--radius-s);
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  min-width: 0;
}
.input:focus, .select:focus, .textarea:focus,
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { color: var(--text-dim); font-size: 0.9em; }
.field-help { color: var(--text-faint); font-size: 0.88em; }

/* --- уведомление ---------------------------------------------------- */
.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 100;
  max-width: 420px;
  padding: 11px 15px;
  border-radius: var(--radius-s);
  background: var(--surface-3);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-2);
  display: none;
}
.toast.error { border-color: var(--bad); color: var(--bad); }

/* --- пустое состояние ----------------------------------------------- */
.empty, .empty-note {
  padding: 24px var(--pad);
  text-align: center;
  color: var(--text-faint);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* --- вкладки -------------------------------------------------------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border-soft); }
.tabs > a, .tabs > button {
  padding: 8px 14px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  color: var(--text-dim);
}
.tabs > a.active, .tabs > button.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

/* --- разделитель-заголовок ------------------------------------------ */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: var(--gap) 0 calc(var(--gap) / 2);
  font-size: 0.85em;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-faint);
}

/* --- сетка карточек выбора (вкладка «Создать») ----------------------- */
.create-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--gap);
  margin-top: var(--gap);
}
.create-card { display: flex; flex-direction: column; }
.create-card > :last-child { margin-top: auto; }

/* --- блок версий продукта -------------------------------------------- */
.prd-versions {
  margin-top: var(--gap);
  padding-top: var(--gap);
  border-top: 1px solid var(--border-soft);
}
.prd-versions .table { font-size: 0.93em; }
.prd-versions tr.ver-active td { background: var(--accent-soft); }
.prd-versions tr.ver-active td:first-child { border-radius: var(--radius-xs) 0 0 var(--radius-xs); }
.prd-versions tr.ver-active td:last-child { border-radius: 0 var(--radius-xs) var(--radius-xs) 0; }

/* --- дашборды: сетка виджетов и графики ------------------------------ */
.dash-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: var(--gap);
}
.dash-widget { min-width: 0; }
.dash-widget-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.dash-chart { width: 100%; height: auto; display: block; }
.dash-axis { fill: var(--text-faint); font-size: 11px; font-family: var(--font); }

/* Одно число — крупно: это главный ответ на вопрос «сколько». */
.dash-number {
  font-size: 2.6em;
  font-weight: 650;
  line-height: 1.1;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  padding: 12px 0;
}

/* Доли: горизонтальные полосы читаются лучше круга. */
.dash-bars { display: flex; flex-direction: column; gap: 8px; padding: 6px 0; }
.dash-bar-row { display: grid; grid-template-columns: 120px 1fr 48px; gap: 8px; align-items: center; }
.dash-bar-label { font-size: 0.9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-bar-track { background: var(--surface-2); border-radius: 999px; height: 10px; overflow: hidden; }
.dash-bar-fill { display: block; height: 100%; border-radius: 999px; }
.dash-bar-value { text-align: right; font-size: 0.9em; color: var(--text-dim); font-variant-numeric: tabular-nums; }

/* Виджет не посчитался — причина видна, а не пустой прямоугольник. */
.dash-widget-error {
  border: 1px dashed var(--bad);
  border-radius: var(--radius-s);
  padding: 12px;
  color: var(--bad);
}

/* --- расписания ------------------------------------------------------ */
.prd-schedules {
  margin-top: var(--gap);
  padding-top: var(--gap);
  border-top: 1px solid var(--border-soft);
}
.prd-schedules .table { font-size: 0.93em; }
/* Выключенное расписание видно, но не мозолит глаза. */
tr.sched-off td { opacity: 0.55; }

/* --- вход через внешних провайдеров ---------------------------------- */
.provider-list { display: flex; flex-direction: column; gap: 8px; margin: 14px 0 0; }
.provider-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  text-decoration: none;
}
.provider-btn:hover { border-color: var(--provider-color, var(--accent)); text-decoration: none; }
/* Точка цвета провайдера: человек узнаёт его по цвету, а не по тексту. */
.provider-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--provider-color, var(--accent));
  flex-shrink: 0;
}
.provider-sep {
  display: flex; align-items: center; gap: 10px;
  margin: 16px 0 12px; color: var(--text-faint); font-size: 0.88em;
}
.provider-sep::before, .provider-sep::after {
  content: ""; flex: 1; height: 1px; background: var(--border-soft);
}

/* --- MCP: инструменты для агентов ------------------------------------ */
.mcp-tools { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.mcp-tool {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-s);
  padding: 10px 12px;
  background: var(--surface-2);
}
.mcp-tool-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.mcp-tool-head code { font-weight: 600; }

/* --- готовые продукты и коннекторы ----------------------------------- */
.section-subtitle {
  font-size: 0.82em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  margin: 16px 0 8px;
}

/* --- коннекторы: карточки-галерея (как шаблоны в n8n) ---------------- */
.connector-card {
  display: flex;
  flex-direction: column;
  color: var(--text);
  text-decoration: none;
  transition: border-color 0.12s, transform 0.12s;
}
.connector-card:hover {
  border-color: var(--accent);
  text-decoration: none;
  transform: translateY(-1px);
}
.connector-head { display: flex; align-items: center; gap: 10px; font-size: 1.05em; }
.connector-icon { font-size: 1.5em; line-height: 1; }
.connector-features {
  margin: 0 0 12px;
  padding-left: 18px;
  color: var(--text-dim);
  font-size: 0.92em;
}
.connector-features li + li { margin-top: 3px; }
.connector-go { margin-top: auto; color: var(--accent-2); font-weight: 550; font-size: 0.93em; }

/* --- вебхуки: список слева, рабочая область справа (по мотивам n8n) --- */
.wh-layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: var(--gap);
  align-items: start;
}
.wh-list {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 8px;
  position: sticky;
  top: 12px;
}
.wh-list-title {
  font-size: 0.82em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  padding: 6px 8px;
}
.wh-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-s);
  color: var(--text);
  text-decoration: none;
}
.wh-list-item:hover { background: var(--surface-2); text-decoration: none; }
.wh-list-name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; }
.wh-list-meta { display: flex; gap: 4px; flex-shrink: 0; }
.wh-main { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }

/* Секрет показывается один раз — крупно и с кнопкой копирования. */
.wh-secret { border-color: var(--warn); }
.wh-secret-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 0;
  border-top: 1px solid var(--border-soft);
}
.wh-secret-row .muted { min-width: 200px; }
.wh-secret-value {
  flex: 1;
  min-width: 220px;
  padding: 6px 10px;
  background: var(--bg-soft);
  border-radius: var(--radius-xs);
  word-break: break-all;
}
.wh-url-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wh-url { font-size: 0.9em; word-break: break-all; flex: 1; min-width: 200px; }

@media (max-width: 820px) {
  .wh-layout { grid-template-columns: 1fr; }
  .wh-list { position: static; }
}

/* --------------------------------------------------------- 4. УТИЛИТЫ */

.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
.right { text-align: right; }

.row { display: flex; align-items: center; gap: var(--gap); flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.grow { flex: 1; min-width: 0; }

.mt { margin-top: var(--gap); }
.mb { margin-bottom: var(--gap); }
.hidden { display: none !important; }

/* Скрыть визуально, но оставить для скринридеров. */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Уважать системную настройку «меньше движения». */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
