/* ==========================================================================
   B13 Flasher — flasher.b13.ltd
   Дизайн-система «Pro Graphite» (тёмная) / «Aluminium» (светлая).
   Продолжение настольного B13 Flasher (lib/ui/theme.py).

   Правила цвета:
   - красный B13 только там, где пишут в машину (.btn-write, статус Writing);
   - зелёный — ок/активно, янтарный — предупреждение/ожидание, синий — инфо;
   - всё остальное — графит, тонкие линии, много воздуха.

   Тема переключается атрибутом data-theme="light" на <html>; без атрибута — тёмная.
   ========================================================================== */

/* ---------- Шрифты (локально, OFL) ---------- */
@font-face { font-family: "Geist"; src: url("../fonts/Geist-Light.a5119ae31d1a.ttf") format("truetype"); font-weight: 300; font-style: normal; font-display: swap; }
@font-face { font-family: "Geist"; src: url("../fonts/Geist-Regular.93ac544c9737.ttf") format("truetype"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Geist"; src: url("../fonts/Geist-Medium.fa2488d6bbe7.ttf") format("truetype"); font-weight: 500; font-style: normal; font-display: swap; }
@font-face { font-family: "Geist"; src: url("../fonts/Geist-SemiBold.28c36bb063ac.ttf") format("truetype"); font-weight: 600; font-style: normal; font-display: swap; }
@font-face { font-family: "Geist Mono"; src: url("../fonts/GeistMono-Regular.578eac6a3f10.ttf") format("truetype"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Geist Mono"; src: url("../fonts/GeistMono-Medium.cf130d3dc035.ttf") format("truetype"); font-weight: 500; font-style: normal; font-display: swap; }

/* ---------- Токены: тёмная тема (по умолчанию) ---------- */
:root {
  color-scheme: dark;

  /* поверхности — пять ступеней графита */
  --bg: #141416;          /* фон окна */
  --side: #1A1A1D;        /* боковое меню */
  --panel: #1E1E21;       /* карточки */
  --raised: #2A2A2E;      /* приподнятые элементы: кнопки, выпадающие списки */
  --raised-hi: #323237;   /* hover приподнятых */
  --inset: #101012;       /* поля ввода, треки, консоль */
  --line: #2A2A2E;        /* линии на карточках */
  --line-soft: #232326;   /* линии на фоне окна */
  --inset-ring: #2C2C31;  /* обводка полей ввода */
  --popup-border: #3A3A40;
  --nav-active: #2A2A2E;
  --selected-row: #34343A;

  /* текст */
  --text: #F2F2F3;
  --text-muted: #A6A6AE;
  --text-faint: #85858E;
  --text-inverse: #141416;

  /* акцент B13 — только запись */
  --accent: #D21F26;
  --accent-2: #DE2A31;
  --accent-deep: #BC171D;
  --accent-dim: #5A1A1D;
  --red-soft: #FF8A8E;

  /* статусы */
  --ok: #4CC38A;
  --ok-soft: #7FDDB0;
  --warn: #E7B04A;
  --err: #FF6166;
  --info: #7FA7F5;
  --ok-bg: rgba(76, 195, 138, 0.14);
  --warn-bg: rgba(231, 176, 74, 0.14);
  --err-bg: rgba(255, 97, 102, 0.14);
  --info-bg: rgba(127, 167, 245, 0.14);
  --muted-bg: rgba(166, 166, 174, 0.12);

  /* кнопки */
  --btn-primary-bg: #F2F2F3;
  --btn-primary-bg-hover: #DADADF;
  --btn-primary-text: #141416;

  /* служебное */
  --ring: rgba(255, 255, 255, 0.08);
  --ring-accent: rgba(210, 31, 38, 0.35);
  --shadow-card: none;
  --shadow-pop: 0 12px 32px rgba(0, 0, 0, 0.45), 0 0 0 1px var(--popup-border);
  --highlight: rgba(255, 255, 255, 0.06); /* светлая линия по верхней грани приподнятого */
  --spark: #6E6E76;
  --dot-off: #5D5D64;
  --backdrop: rgba(0, 0, 0, 0.55);

  --radius: 12px;      /* карточки */
  --radius-ctl: 9px;   /* контролы */
  --radius-sm: 6px;

  --font: "Geist", -apple-system, "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  --mono: "Geist Mono", "Cascadia Mono", Consolas, ui-monospace, monospace;

  --sidebar-w: 240px;
  --topbar-h: 56px;
}

/* ---------- Токены: светлая тема Aluminium ---------- */
[data-theme="light"] {
  color-scheme: light;

  --bg: #F5F5F7;
  --side: #ECECEF;
  --panel: #FFFFFF;
  --raised: #FFFFFF;
  --raised-hi: #F2F2F5;
  --inset: #EFEFF2;
  --line: #E3E3E8;
  --line-soft: #E3E3E8;
  --inset-ring: #D9D9DE;
  --popup-border: #D6D6DB;
  --nav-active: #DEDEE3;
  --selected-row: #E8E8ED;

  --text: #1D1D1F;
  --text-muted: #5F5F66;
  --text-faint: #76767C;
  --text-inverse: #FFFFFF;

  --accent: #C8161C;
  --accent-2: #D8262C;
  --accent-deep: #B01419;
  --accent-dim: #F2C9CA;
  --red-soft: #B3141A;

  --ok: #1F9D55;
  --ok-soft: #167540;
  --warn: #B26A00;
  --err: #C62828;
  --info: #1F5FBF;
  --ok-bg: rgba(31, 157, 85, 0.12);
  --warn-bg: rgba(178, 106, 0, 0.12);
  --err-bg: rgba(198, 40, 40, 0.10);
  --info-bg: rgba(31, 95, 191, 0.10);
  --muted-bg: rgba(95, 95, 102, 0.10);

  --btn-primary-bg: #1D1D1F;
  --btn-primary-bg-hover: #3A3A3F;
  --btn-primary-text: #FFFFFF;

  --ring: rgba(0, 0, 0, 0.08);
  --ring-accent: rgba(200, 22, 28, 0.25);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-pop: 0 12px 32px rgba(0, 0, 0, 0.12), 0 0 0 1px var(--popup-border);
  --highlight: rgba(255, 255, 255, 0.6);
  --spark: #A1A1A6;
  --dot-off: #C7C7CC;
  --backdrop: rgba(29, 29, 31, 0.35);
}

/* ---------- База ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scrollbar-color: var(--raised-hi) transparent; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
}
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.01em; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
h4 { font-size: 13px; }
p { margin: 0 0 1em; }
p:last-child { margin-bottom: 0; }
a { color: inherit; text-decoration: none; }
a:hover { text-decoration: none; }
a.link { color: var(--info); }
a.link:hover { text-decoration: underline; }
img, svg { display: block; max-width: 100%; }
hr, .divider { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
small, .small { font-size: 12px; }
strong, b { font-weight: 600; }
code, kbd { font-family: var(--mono); font-size: 0.92em; }
::selection { background: var(--selected-row); }
:focus-visible { outline: 2px solid var(--text-faint); outline-offset: 2px; border-radius: var(--radius-sm); }

/* утилитарные */
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.vin { font-family: var(--mono); font-size: 13px; letter-spacing: 0.04em; text-transform: uppercase; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; }
.row.between { justify-content: space-between; }
.row.nowrap { flex-wrap: nowrap; }
.stack { display: grid; gap: 12px; }
.mt-0 { margin-top: 0 !important; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-16 { margin-bottom: 16px; }
.hide { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* иконки (inline SVG, stroke = currentColor) */
.ic { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.ic-lg { width: 20px; height: 20px; }

/* логотип: две картинки, показываем по теме */
.logo-dark { display: block; }
.logo-light { display: none; }
[data-theme="light"] .logo-dark { display: none; }
[data-theme="light"] .logo-light { display: block; }

/* ==========================================================================
   Каркас приложения: body.app > .shell > .sidebar + .main
   ========================================================================== */
body.app { overflow-x: hidden; }
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}

/* --- боковое меню --- */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--side);
  border-right: 1px solid var(--line-soft);
  overflow-y: auto;
  z-index: 40;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 20px 14px;
  min-height: var(--topbar-h);
}
.brand img { height: 26px; width: auto; flex: none; }
.brand { flex: none; }
/* Логотип в меню: по центру высоты верхней панели и чуть крупнее, чтобы читалась строка PERFORMANCE */
.sidebar .brand { padding: 0 20px; height: calc(var(--topbar-h) + 16px); min-height: 0; }
.sidebar .brand img { height: 34px; }
.brand-sub { font-size: 11px; color: var(--text-faint); letter-spacing: 0.08em; text-transform: uppercase; }

.nav { padding: 6px 12px 16px; display: flex; flex-direction: column; }
.nav-section {
  padding: 16px 12px 6px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--text-muted);
  font-weight: 500;
  font-size: 13.5px;
  line-height: 1.3;
  transition: background 0.12s, color 0.12s;
}
.nav a:hover { background: var(--muted-bg); color: var(--text); }
.nav a.active { background: var(--nav-active); color: var(--text); }
.nav a .ic { color: var(--text-faint); }
.nav a.active .ic, .nav a:hover .ic { color: var(--text); }
.nav a .count {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--text-faint);
  background: var(--muted-bg);
  padding: 1px 7px;
  border-radius: 999px;
}
.nav a.active .count { color: var(--text-muted); }

.sidebar-foot {
  margin-top: auto;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--line-soft);
  font-size: 12px;
  color: var(--text-faint);
}

/* --- бургер (только мобильный) --- */
.nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.nav-toggle:hover { background: var(--muted-bg); }
.nav-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  z-index: 35;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
body.nav-open .nav-backdrop { opacity: 1; pointer-events: auto; }

/* --- основная область --- */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--topbar-h);
  padding: 0 28px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--line-soft);
}
@supports not (background: color-mix(in srgb, red, blue)) {
  .topbar { background: var(--bg); }
}
.topbar-title { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.topbar-title .crumb { color: var(--text-faint); font-weight: 400; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.theme-toggle:hover { background: var(--muted-bg); color: var(--text); }
.theme-toggle .ic-moon { display: none; }
[data-theme="light"] .theme-toggle .ic-sun { display: none; }
[data-theme="light"] .theme-toggle .ic-moon { display: block; }

/* меню пользователя: <details class="user-menu"> с <summary> */
.user-menu { position: relative; }
.user-menu summary, .user-menu > .user-btn {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 4px 4px;
  border-radius: 999px;
  cursor: pointer;
  user-select: none;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary:hover { background: var(--muted-bg); }
.user-menu .user-name { font-size: 13px; font-weight: 500; line-height: 1.2; }
.user-menu .user-role { font-size: 11px; color: var(--text-faint); line-height: 1.2; }
.user-menu .menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 200px;
  padding: 6px;
  background: var(--raised);
  border-radius: 10px;
  box-shadow: var(--shadow-pop);
  z-index: 50;
}
.user-menu .menu a, .user-menu .menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}
.user-menu .menu a:hover, .user-menu .menu button:hover { background: var(--raised-hi); }
.user-menu .menu .divider { margin: 6px 0; }

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--raised-hi);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  flex: none;
}
.avatar.sm { width: 24px; height: 24px; font-size: 10px; }
.avatar.lg { width: 44px; height: 44px; font-size: 15px; }

.content { padding: 28px 28px 48px; max-width: 1360px; width: 100%; }
.content.narrow { max-width: 760px; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}
.page-head h1 { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.page-sub { margin-top: 4px; color: var(--text-muted); font-size: 13.5px; }
.page-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ==========================================================================
   Компоненты
   ========================================================================== */

/* --- карточка --- */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.card-head h2, .card-head h3 { font-size: 15px; }
.card-head .sub { font-size: 12.5px; color: var(--text-faint); font-weight: 400; margin-left: 8px; }
.card-head .actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; min-width: 0; max-width: 100%; }
.card-body { padding: 18px; min-width: 0; }
.card-body.flush { padding: 0; }
.card-body + .card-body { border-top: 1px solid var(--line); }
.card-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--line);
  font-size: 12.5px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card.inset { background: var(--inset); border-color: var(--inset-ring); box-shadow: none; }
.card + .card { margin-top: 16px; }

/* --- сетки --- */
.grid-2, .grid-3, .grid-4 { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 > *, .grid-3 > *, .grid-4 > *, .grid-main > * { min-width: 0; } /* иначе таблица внутри растянет колонку */
.grid-2 > .card, .grid-3 > .card, .grid-4 > .card { margin-top: 0; }
.span-2 { grid-column: span 2; }
.grid-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 16px; }
.section-gap { margin-top: 24px; }

/* --- stat --- */
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 16px 18px 14px;
  min-width: 0;
}
.stat-label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.stat-value {
  margin-top: 6px;
  font-family: var(--mono);
  font-size: 28px;
  font-weight: 500;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.stat-value .unit { font-size: 14px; color: var(--text-faint); margin-left: 4px; font-weight: 400; }
.stat-hint { margin-top: 6px; font-size: 12px; color: var(--text-faint); }
.stat-hint.ok { color: var(--ok); }
.stat-hint.warn { color: var(--warn); }
.stat-hint.err { color: var(--err); }
.stat.warn .stat-value { color: var(--warn); }
.stat.ok .stat-value { color: var(--ok); }
/* мини-спарклайн из столбиков (как в настольном флешере) */
.spark { display: flex; align-items: flex-end; gap: 3px; height: 28px; margin-top: 10px; }
.spark i { flex: 1; min-width: 3px; background: var(--spark); border-radius: 2px 2px 0 0; opacity: 0.55; }
.spark i:last-child { opacity: 1; background: var(--text-muted); }

/* --- таблицы --- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.card > .table-wrap, .card-body.flush > .table-wrap { border-radius: 0 0 var(--radius) var(--radius); }
table.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { padding: 11px 16px; text-align: left; vertical-align: middle; }
.table th, .table .num, .table .vin, .table .mono, .table .nowrap, .table .actions { white-space: nowrap; }
.table th {
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 1px solid var(--line);
}
.table td { border-top: 1px solid var(--line-soft); }
.table tbody tr:first-child td { border-top: 0; }
.table tbody tr:hover td { background: var(--muted-bg); }
.table tr.selected td { background: var(--selected-row); }
.table .num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.table .cell-main { font-weight: 500; white-space: nowrap; }
.table a.row-link { white-space: nowrap; }
.table .cell-sub { display: block; font-size: 12px; color: var(--text-faint); margin-top: 1px; }
.table .actions { text-align: right; }
.table .actions .btn { vertical-align: middle; }
.table a.row-link { font-weight: 500; }
.table a.row-link:hover { text-decoration: underline; text-underline-offset: 3px; }
.table-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--text-faint); }
.pager { display: flex; gap: 4px; }
.pager a, .pager span { min-width: 28px; height: 28px; padding: 0 8px; display: inline-flex; align-items: center; justify-content: center; border-radius: 7px; font-family: var(--mono); font-size: 12px; color: var(--text-muted); }
.pager a:hover { background: var(--muted-bg); color: var(--text); }
.pager .active { background: var(--raised); color: var(--text); }

/* --- бейджи --- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  line-height: 18px;
  white-space: nowrap;
  color: var(--text-muted);
  background: var(--muted-bg);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.badge.no-dot::before { display: none; }
.badge-ok { color: var(--ok); background: var(--ok-bg); }
.badge-warn { color: var(--warn); background: var(--warn-bg); }
.badge-err { color: var(--err); background: var(--err-bg); }
.badge-info { color: var(--info); background: var(--info-bg); }
.badge-muted { color: var(--text-faint); background: var(--muted-bg); }
/* статус «Writing» — единственный красный бейдж */
.badge-write { color: #fff; background: var(--accent); }
.badge-write::before { background: #fff; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }
/* стейдж — моно-подпись */
.badge-stage { font-family: var(--mono); color: var(--text); background: var(--raised); box-shadow: inset 0 0 0 1px var(--line); }
[data-theme="light"] .badge-stage { background: var(--inset); }
.badge-stage::before { display: none; }
.badge-stage.stock { color: var(--text-faint); background: var(--muted-bg); }

/* точка-статус без текста */
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--dot-off); vertical-align: middle; }
.dot-ok { background: var(--ok); }
.dot-warn { background: var(--warn); }
.dot-err { background: var(--err); }
.dot-info { background: var(--info); }

/* чип (тэг) */
.chips { display: inline-flex; gap: 4px; white-space: nowrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 7px; font-size: 12px; background: var(--muted-bg); color: var(--text-muted); font-family: var(--mono); }

/* --- кнопки --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-ctl);
  background: var(--raised);
  color: var(--text);
  font: 500 13.5px/1 var(--font);
  letter-spacing: 0.005em;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s, box-shadow 0.12s, transform 0.08s;
  -webkit-user-select: none;
  user-select: none;
}
.btn:active { transform: translateY(0.5px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }
.btn .ic { width: 15px; height: 15px; }

.btn-primary { background: var(--btn-primary-bg); color: var(--btn-primary-text); }
.btn-primary:hover { background: var(--btn-primary-bg-hover); }

.btn-secondary { background: var(--raised); border-color: var(--line); box-shadow: inset 0 1px 0 var(--highlight); }
.btn-secondary:hover { background: var(--raised-hi); }
[data-theme="light"] .btn-secondary { border-color: var(--inset-ring); box-shadow: 0 1px 1px rgba(0,0,0,0.04); }

.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--muted-bg); color: var(--text); }

/* единственная красная кнопка — запись в машину */
.btn-write {
  background: linear-gradient(var(--accent-2), var(--accent-deep));
  color: #fff;
  border-color: var(--accent-deep);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.14);
}
.btn-write:hover { background: linear-gradient(var(--accent-2), var(--accent)); }
.btn-write:focus-visible { outline-color: var(--accent); }

.btn-danger-text { background: transparent; color: var(--err); }
.btn-danger-text:hover { background: var(--err-bg); }

.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; border-radius: 7px; }
.btn-sm .ic { width: 14px; height: 14px; }
.btn-lg { height: 44px; padding: 0 20px; font-size: 15px; border-radius: 11px; }
.btn-block { width: 100%; }
.btn-icon { width: 36px; padding: 0; }
.btn-icon.btn-sm { width: 30px; }
.btn-group { display: inline-flex; gap: 8px; flex-wrap: wrap; }

/* --- формы --- */
.form { display: grid; gap: 16px; }
.form-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-actions { display: flex; align-items: center; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding-top: 4px; }
.form-actions.start { justify-content: flex-start; }
.field { display: grid; gap: 6px; min-width: 0; }
.field label, .field > .label { font-size: 13px; font-weight: 500; color: var(--text); }
.field label .opt { color: var(--text-faint); font-weight: 400; }
.field input, .field select, .field textarea, .input {
  width: 100%;
  min-height: 38px;
  padding: 8px 12px;
  background: var(--inset);
  border: 1px solid var(--inset-ring);
  border-radius: var(--radius-ctl);
  color: var(--text);
  font: 400 14px/1.4 var(--font);
  transition: border-color 0.12s, box-shadow 0.12s;
  -webkit-appearance: none;
  appearance: none;
}
.field input::placeholder, .field textarea::placeholder { color: var(--text-faint); }
.field input:hover, .field select:hover, .field textarea:hover { border-color: var(--popup-border); }
.field input:focus, .field select:focus, .field textarea:focus, .input:focus {
  outline: none;
  border-color: var(--text-faint);
  box-shadow: 0 0 0 3px var(--ring);
}
.field input:disabled, .field select:disabled, .field textarea:disabled { opacity: 0.55; cursor: not-allowed; }
.field input.mono, .field input[data-mono] { font-family: var(--mono); letter-spacing: 0.04em; }
.field select {
  padding-right: 34px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2385858E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='m6 9 6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
}
.field textarea { min-height: 96px; resize: vertical; line-height: 1.5; }
.field .help, .help { font-size: 12px; color: var(--text-faint); }
.field input[type="checkbox"], .field input[type="radio"], .check input {
  width: 16px; height: 16px; min-height: 0; padding: 0; margin: 0;
  accent-color: var(--text); -webkit-appearance: auto; appearance: auto; cursor: pointer;
}
.check { display: flex; align-items: center; gap: 10px; font-size: 13.5px; cursor: pointer; }
.check .help { margin-left: 26px; }
.field.inline { grid-template-columns: auto 1fr; align-items: center; }
.input-group { display: flex; }
.input-group input { border-radius: var(--radius-ctl) 0 0 var(--radius-ctl); }
.input-group .btn { border-radius: 0 var(--radius-ctl) var(--radius-ctl) 0; height: 38px; border: 1px solid var(--inset-ring); border-left: 0; }
.input-addon { display: flex; align-items: center; padding: 0 12px; background: var(--raised); border: 1px solid var(--inset-ring); border-left: 0; border-radius: 0 var(--radius-ctl) var(--radius-ctl) 0; color: var(--text-faint); font-family: var(--mono); font-size: 13px; }

/* ошибки Django: <ul class="errorlist"> рядом с полем или над формой */
ul.errorlist { list-style: none; margin: 0; padding: 0; color: var(--err); font-size: 12.5px; }
ul.errorlist li + li { margin-top: 2px; }
.field ul.errorlist ~ input, .field ul.errorlist ~ select, .field ul.errorlist ~ textarea,
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--err); }
ul.errorlist.nonfield { padding: 10px 14px; border-radius: 10px; background: var(--err-bg); }

/* поиск в тулбаре */
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar .grow { flex: 1; }
.search { position: relative; min-width: 220px; }
.search input {
  width: 100%; height: 36px; padding: 0 12px 0 34px;
  background: var(--inset); border: 1px solid var(--inset-ring); border-radius: var(--radius-ctl);
  color: var(--text); font: 400 13.5px var(--font); -webkit-appearance: none; appearance: none;
}
.search input:focus { outline: none; border-color: var(--text-faint); box-shadow: 0 0 0 3px var(--ring); }
.search .ic { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-faint); pointer-events: none; }

/* --- сообщения Django (messages) --- */
.alert {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 13.5px;
  color: var(--text);
}
.alert::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); margin-top: 6px; flex: none; }
.alert strong { font-weight: 600; }
.alert-ok { background: var(--ok-bg); border-color: transparent; }
.alert-ok::before { background: var(--ok); }
.alert-warn { background: var(--warn-bg); border-color: transparent; }
.alert-warn::before { background: var(--warn); }
.alert-err { background: var(--err-bg); border-color: transparent; }
.alert-err::before { background: var(--err); }
.alert-info { background: var(--info-bg); border-color: transparent; }
.alert-info::before { background: var(--info); }
.alert .close { margin-left: auto; background: none; border: 0; color: var(--text-faint); cursor: pointer; padding: 0; }
.messages { display: grid; gap: 8px; margin-bottom: 20px; }

/* --- пустое состояние --- */
.empty {
  padding: 44px 24px;
  text-align: center;
  color: var(--text-muted);
  border: 1px dashed var(--popup-border);
  border-radius: var(--radius);
}
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty p { font-size: 13.5px; max-width: 420px; margin: 0 auto; }
.empty .btn { margin-top: 16px; }
.empty .ic { width: 28px; height: 28px; margin: 0 auto 12px; color: var(--text-faint); stroke-width: 1.3; }
.card > .empty { border: 0; }

/* --- вкладки --- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 20px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  position: relative;
  padding: 10px 12px;
  color: var(--text-muted);
  font-weight: 500;
  font-size: 13.5px;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.12s;
}
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--text); border-bottom-color: var(--text); }
.tabs a .count { margin-left: 6px; font-family: var(--mono); font-size: 11.5px; color: var(--text-faint); }
/* сегментный переключатель */
.seg { display: inline-flex; max-width: 100%; overflow-x: auto; scrollbar-width: none; padding: 3px; background: var(--inset); border: 1px solid var(--inset-ring); border-radius: 9px; }
.seg::-webkit-scrollbar { display: none; }
.seg a, .seg button { flex: none; padding: 5px 12px; border: 0; border-radius: 6px; background: none; color: var(--text-muted); font: 500 12.5px var(--font); cursor: pointer; }
.seg .active { background: var(--raised-hi); color: var(--text); box-shadow: inset 0 1px 0 var(--highlight); }
[data-theme="light"] .seg .active { background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.08); }

/* --- key/value --- */
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 20px; margin: 0; font-size: 13.5px; }
.kv dt { color: var(--text-faint); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.kv.cols { grid-template-columns: max-content 1fr max-content 1fr; }

/* --- простой список строк (как tbody без таблицы) --- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-top: 1px solid var(--line-soft); font-size: 13.5px; }
.list > li:first-child { border-top: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .title { display: block; font-weight: 500; }
.list .sub { display: block; font-size: 12px; color: var(--text-faint); margin-top: 1px; }
.list .when { font-family: var(--mono); font-size: 12px; color: var(--text-faint); white-space: nowrap; }
.list > li.link:hover { background: var(--muted-bg); cursor: pointer; }

/* --- лента событий --- */
.timeline { list-style: none; margin: 0; padding: 0 0 0 4px; }
.timeline > li { position: relative; display: grid; grid-template-columns: 1fr auto; gap: 2px 16px; padding: 10px 0 10px 22px; font-size: 13.5px; }
.timeline > li::before { content: ""; position: absolute; left: 0; top: 17px; width: 8px; height: 8px; border-radius: 50%; background: var(--dot-off); }
.timeline > li::after { content: ""; position: absolute; left: 3.5px; top: 29px; bottom: -10px; width: 1px; background: var(--line); }
.timeline > li:last-child::after { display: none; }
.timeline > li.ok::before { background: var(--ok); }
.timeline > li.warn::before { background: var(--warn); }
.timeline > li.err::before { background: var(--err); }
.timeline > li.info::before { background: var(--info); }
.timeline > li.write::before { background: var(--accent); }
.timeline .when { grid-row: span 2; font-family: var(--mono); font-size: 12px; color: var(--text-faint); white-space: nowrap; align-self: start; padding-top: 2px; }
.timeline .sub { font-size: 12.5px; color: var(--text-faint); }

/* --- прогресс --- */
.progress { height: 6px; background: var(--inset); border: 1px solid var(--inset-ring); border-radius: 999px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--text-muted); border-radius: 999px; }
.progress.write > i { background: var(--accent); }
.progress.ok > i { background: var(--ok); }

/* --- модуль машины (ECU / DSG / Haldex) --- */
.module { display: grid; gap: 10px; }
.module-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.module-name { font-weight: 600; font-size: 14px; }
.module-kind { font-size: 11.5px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.06em; }
.module .kv { font-size: 13px; }

/* флаг страны: двухбуквенный код в моно-плашке */
.flag { display: inline-flex; align-items: center; justify-content: center; min-width: 28px; height: 20px; padding: 0 6px; border-radius: 5px; background: var(--raised); font-family: var(--mono); font-size: 11px; font-weight: 500; letter-spacing: 0.06em; color: var(--text-muted); }

/* баланс дилера */
.balance { display: flex; align-items: baseline; gap: 8px; }
.balance .amount { font-family: var(--mono); font-size: 34px; font-weight: 500; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.balance .cur { font-family: var(--mono); font-size: 14px; color: var(--text-faint); }

/* --- модальное окно (native <dialog class="modal">) --- */
.modal { border: 0; padding: 0; background: transparent; max-width: min(560px, calc(100vw - 32px)); width: 100%; color: var(--text); }
.modal::backdrop { background: var(--backdrop); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.modal .card { box-shadow: var(--shadow-pop); border-color: var(--popup-border); margin: 0; }
.modal .card-head .close { margin-left: auto; }

/* --- ключи/коды (download card) --- */
.code-box { font-family: var(--mono); font-size: 13px; padding: 10px 12px; background: var(--inset); border: 1px solid var(--inset-ring); border-radius: 8px; overflow-wrap: anywhere; }

/* ==========================================================================
   Авторизация: body.auth
   ========================================================================== */
body.auth { display: grid; place-items: center; padding: 24px 16px; }
.auth-wrap { width: 100%; max-width: 400px; }
.auth-brand { display: flex; justify-content: center; margin-bottom: 24px; }
.auth-brand img { height: 36px; width: auto; }
.auth-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-card);
  padding: 28px;
}
.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card .lead { color: var(--text-muted); font-size: 13.5px; margin-bottom: 20px; }
.auth-card .form-actions { padding-top: 4px; }
.auth-card .form-actions .btn { width: 100%; }
.auth-foot { margin-top: 18px; text-align: center; font-size: 12.5px; color: var(--text-faint); }
.auth-foot a { color: var(--text-muted); }
.auth-foot a:hover { color: var(--text); }
.auth-links { display: flex; justify-content: space-between; align-items: center; font-size: 12.5px; }
.auth-links a { color: var(--text-muted); }
.auth-links a:hover { color: var(--text); }
.auth-theme { position: fixed; top: 14px; right: 14px; }

/* ==========================================================================
   Лендинг: body.site
   ========================================================================== */
body.site { font-size: 15px; }
.container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 24px; }

.site-header {
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--line-soft);
}
@supports not (background: color-mix(in srgb, red, blue)) { .site-header { background: var(--bg); } }
.site-header .container { display: flex; align-items: center; gap: 24px; height: 64px; }
.site-header .brand { padding: 0; min-height: 0; }
.site-header .brand img { height: 28px; }
.site-nav { display: flex; align-items: center; gap: 4px; margin-left: 12px; }
.site-nav a { padding: 8px 12px; border-radius: 8px; font-size: 14px; font-weight: 500; color: var(--text-muted); }
.site-nav a:hover { color: var(--text); background: var(--muted-bg); }
.site-header .actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.hero { padding: 96px 0 72px; }
.hero .container { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr); gap: 56px; align-items: center; }
.hero .eyebrow { display: inline-flex; align-items: center; gap: 8px; font-size: 12.5px; font-weight: 500; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-faint); margin-bottom: 20px; }
.hero h1 { font-size: 48px; font-weight: 600; letter-spacing: -0.025em; line-height: 1.08; }
.hero h1 .dim { color: var(--text-faint); font-weight: 500; }
.hero .lead { margin-top: 20px; font-size: 18px; line-height: 1.55; color: var(--text-muted); max-width: 520px; }
.hero-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 32px; }
.hero-actions .note { width: 100%; font-size: 12.5px; color: var(--text-faint); }

/* визуал героя: макет окна флешера на CSS */
.hero-visual { position: relative; }
.app-window {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 30px 80px -30px rgba(0,0,0,0.6), var(--shadow-card);
  overflow: hidden;
  font-size: 13px;
}
.app-window .wbar { display: flex; align-items: center; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--side); }
.app-window .wbar i { width: 10px; height: 10px; border-radius: 50%; background: var(--raised-hi); }
.app-window .wbar span { margin-left: 8px; font-size: 12px; color: var(--text-faint); }
.app-window .wbody { padding: 18px; display: grid; gap: 14px; }
.app-window .wrow { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.app-window .wlabel { color: var(--text-faint); font-size: 12px; }
.app-window .wval { font-family: var(--mono); font-size: 13px; }
.app-window .wlog { font-family: var(--mono); font-size: 11.5px; color: var(--text-muted); background: var(--inset); border: 1px solid var(--inset-ring); border-radius: 8px; padding: 10px 12px; line-height: 1.7; }
.app-window .wlog .ok { color: var(--ok); }

.section { padding: 72px 0; border-top: 1px solid var(--line-soft); }
.section-head { max-width: 640px; margin-bottom: 36px; }
.section-head h2 { font-size: 30px; letter-spacing: -0.02em; }
.section-head p { margin-top: 10px; font-size: 16px; color: var(--text-muted); }
.section .eyebrow { font-size: 12.5px; font-weight: 500; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-faint); margin-bottom: 10px; }

.features { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.feature { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow-card); }
.feature .ic { width: 22px; height: 22px; stroke-width: 1.4; color: var(--text-muted); margin-bottom: 16px; }
.feature h3 { font-size: 15px; margin-bottom: 6px; }
.feature p { font-size: 13.5px; color: var(--text-muted); line-height: 1.55; }

.steps { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; counter-reset: step; }
.step { position: relative; padding: 22px 22px 22px 22px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-card); }
.step .n { font-family: var(--mono); font-size: 12px; color: var(--text-faint); margin-bottom: 14px; }
.step h3 { font-size: 15px; margin-bottom: 6px; }
.step p { font-size: 13.5px; color: var(--text-muted); line-height: 1.55; }
.step.write .n { color: var(--accent); }

.platforms { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.platform { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow-card); }
.platform .kind { font-size: 11.5px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-faint); }
.platform h3 { font-size: 18px; margin: 6px 0 10px; }
.platform p { font-size: 13.5px; color: var(--text-muted); line-height: 1.55; }
.platform ul { margin: 12px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; font-size: 13px; }
.platform li { display: flex; gap: 8px; align-items: center; color: var(--text-muted); }
.platform li::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: var(--text-faint); flex: none; }

.cta { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 48px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 24px; align-items: center; box-shadow: var(--shadow-card); }
.cta h2 { font-size: 26px; letter-spacing: -0.02em; }
.cta p { margin-top: 8px; color: var(--text-muted); max-width: 560px; }
.cta .actions { display: flex; gap: 10px; flex-wrap: wrap; }

.site-footer { padding: 32px 0 40px; border-top: 1px solid var(--line-soft); font-size: 12.5px; color: var(--text-faint); }
.site-footer .container { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.site-footer nav { display: flex; gap: 16px; flex-wrap: wrap; }
.site-footer a:hover { color: var(--text); }

/* ==========================================================================
   Адаптив
   ========================================================================== */
@media (max-width: 1100px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-main { grid-template-columns: 1fr; }
  .features, .steps { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero h1 { font-size: 40px; }
}

@media (max-width: 900px) {
  /* боковое меню становится выезжающим */
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(300px, 86vw);
    height: 100vh;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: none;
  }
  body.nav-open .sidebar { transform: translateX(0); box-shadow: 0 0 60px rgba(0,0,0,0.4); }
  .nav-toggle { display: inline-flex; }
  .topbar { padding: 0 16px; }
  .content { padding: 20px 16px 40px; }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .platforms { grid-template-columns: 1fr; }
  .hero { padding: 56px 0 48px; }
  .hero .container { grid-template-columns: 1fr; gap: 32px; }
  .site-nav { display: none; }
  .cta { grid-template-columns: 1fr; padding: 32px; }
}

@media (max-width: 700px) {
  body { font-size: 14px; }
  h1 { font-size: 20px; }
  .grid-2, .grid-3, .grid-4, .features, .steps { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .form-row { grid-template-columns: 1fr; }
  .kv.cols { grid-template-columns: max-content 1fr; }
  .stat-value { font-size: 24px; }
  .page-head { align-items: flex-start; flex-direction: column; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1; }
  .user-menu .user-name, .user-menu .user-role { display: none; }
  .topbar-title { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .card-head { flex-wrap: wrap; }
  .card-body { padding: 14px; }
  .list > li { padding: 12px 14px; flex-wrap: wrap; }
  .hero h1 { font-size: 32px; }
  .hero .lead { font-size: 16px; }
  .section { padding: 48px 0; }
  .section-head h2 { font-size: 24px; }
  .cta { padding: 24px; }
  .cta h2 { font-size: 22px; }
  .site-header .container { height: 56px; padding: 0 16px; }
  .site-header .brand img { height: 24px; }
  .site-header .actions .btn-primary { display: none; } /* Download есть в hero */
  .site-header .actions { gap: 4px; }
  .container { padding: 0 16px; }
  .auth-card { padding: 22px; }
  .modal { max-width: calc(100vw - 24px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

@media print {
  .sidebar, .topbar, .nav-toggle, .page-actions, .theme-toggle { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  body { background: #fff; color: #000; }
}
