:root {
  --paper: #f3f6fb;
  --ink: #1e2e47;
  --muted: #5a7196;
  --line: #d5e3f5;
  --card: #ffffff;
  --card-2: #eef4fb;
  --accent: #1560bd;
  --accent-2: #5ca5ff;
  --ok: #1f7a4d;
  --ok-bg: #e5f5ec;
  --bad: #c0392b;
  --bad-bg: #fdecea;
  --warn: #c47d12;
  --warn-bg: #fff4de;
  --health: #1e2e47;
  --health-2: #15325f;
  --radius: 20px;
  --shadow: 0 8px 28px rgba(159, 202, 255, 0.35);
}

[data-theme="dark"] {
  --paper: #0e1624;
  --ink: #eef4fb;
  --muted: #8aa0c0;
  --line: #24344d;
  --card: #152238;
  --card-2: #1b2c45;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.28);
  --health: #0f1c33;
  --health-2: #15325f;
}
[data-theme="dark"] .topbar {
  background: rgba(15, 28, 51, 0.92);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; overflow-x: clip; }
html { scroll-padding-top: 84px; }
body {
  font-family: Onest, "Segoe UI", sans-serif;
  background:
    radial-gradient(900px 420px at 12% -10%, rgba(21, 96, 189, 0.12), transparent 55%),
    var(--paper);
  color: var(--ink);
}
h1, h2, .wordmark b { font-family: Unbounded, Onest, sans-serif; font-weight: 500; }
button, input, select { font: inherit; color: inherit; }

[hidden] { display: none !important; }

.app { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 8;
  display: flex; gap: 16px; align-items: flex-start;
  padding: 12px 28px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}
.wordmark { display: flex; gap: 10px; align-items: center; flex: 0 0 auto; padding-top: 4px; }
.header-mark {
  width: 36px; height: 36px; border-radius: 10px;
  background: #1e2e47; color: #fff;
  display: grid; place-items: center;
  font-family: Unbounded, Onest, sans-serif;
  font-size: 16px; font-weight: 700; letter-spacing: -0.04em;
  flex-shrink: 0;
}
.header-logo {
  height: 36px;
  width: auto;
  background: #1e2e47;
  border-radius: 10px;
  padding: 6px 8px;
}
.wm-mark {
  width: 34px; height: 34px; border-radius: 10px;
  background: var(--ink); color: var(--paper);
  display: grid; place-items: center; font-weight: 800;
}
.wordmark b { display: block; font-size: 15px; letter-spacing: -0.03em; }
.wordmark small { color: var(--muted); font-size: 11px; letter-spacing: 0.04em; }
.bar-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bar-filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.bar-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.field { display: grid; gap: 4px; font-size: 10px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); min-width: 0; }
.field select, .field input, .dates input, .btn {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 12px;
  min-height: 42px;
  touch-action: manipulation;
}
.field select { min-width: 140px; max-width: 220px; width: 100%; }
.goal-picker { position: relative; min-width: 180px; max-width: 280px; flex: 1 1 180px; }
.goal-picker-btn {
  width: 100%;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 12px;
  min-height: 42px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  font-size: 13px;
}
.goal-picker-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  width: min(360px, 80vw);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 10px;
  z-index: 20;
}
.goal-opt {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 7px 8px;
  border-radius: 10px;
  font-size: 13px;
  letter-spacing: 0;
  text-transform: none;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}
.goal-opt:hover { background: var(--card-2); }
.goal-opt input { margin-top: 3px; }
.goal-opt small { display: block; color: var(--muted); font-weight: 400; font-size: 11px; }
.goal-opt-all { border-bottom: 1px solid var(--line); margin-bottom: 6px; padding-bottom: 10px; }
.goal-opt-list { max-height: 280px; overflow: auto; }
.goal-group { margin: 8px 4px 4px; color: var(--muted); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; }
.goal-picker-menu .btn { width: 100%; margin-top: 8px; }
.dates { display: flex; align-items: center; gap: 6px; }
.dates input { min-width: 138px; }
.dash { color: var(--muted); }
.btn { cursor: pointer; background: var(--card); }
.btn.solid {
  background: linear-gradient(180deg, #1560bd 0%, #5ca5ff 100%);
  color: #fff;
  border-color: #1560bd;
  font-weight: 700;
}
.btn.icon { width: 42px; padding: 0; }
.btn.solid:hover { background: #1560bd; border-color: #1560bd; color: #fff; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.pdf-btn {
  background: linear-gradient(180deg, #1560bd 0%, #5ca5ff 100%);
  border-color: #1560bd;
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.01em;
  box-shadow: 7px 4px 22px rgba(159, 202, 255, 0.55);
}
.pdf-btn:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
.pdf-btn:disabled { box-shadow: none; }

.connect-list { display: grid; gap: 10px; width: min(640px, 100%); }
.connect-row { display: grid; grid-template-columns: 160px 1fr auto; gap: 8px; align-items: center; }
.connect-row b { display: block; font-size: 14px; }
.connect-row small { display: block; color: var(--muted); font-size: 11px; }
.connect-row a { font-size: 12px; color: var(--accent); }

.idle-card { padding: 36px 28px; max-width: 640px; }
.idle-card p { color: var(--muted); margin: 8px 0 0; line-height: 1.5; }

.rules-card { margin-top: 12px; }
.rules-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 14px 0; }
.rules-grid label { display: grid; gap: 6px; font-size: 11px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.rules-grid input {
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 12px;
  min-height: 42px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
}
.rules-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.rules-actions input[type="file"] { display: none; }

body.pdf-export .no-pdf,
body.pdf-export .drawer,
body.pdf-export .backdrop { display: none !important; }

@media print {
  .topbar, .agency-bar, .banner, .no-pdf, .drawer, .backdrop { display: none !important; }
  .stage { padding: 0; max-width: none; }
  .card { break-inside: avoid; }
}

.agency-bar {
  margin: 14px 28px 0;
  padding: 14px 16px;
  border: 1px dashed #c9896a;
  background: #f3ead8;
  border-radius: 16px;
  display: flex; gap: 16px; justify-content: space-between; align-items: center; flex-wrap: wrap;
}
[data-theme="dark"] .agency-bar {
  background: #2a241c;
  border-color: #6a4536;
}
.agency-bar h2 { margin: 0 0 4px; font-size: 16px; }
.agency-bar p { margin: 0; color: var(--muted); font-size: 13px; }
.agency-bar form { display: flex; gap: 8px; flex-wrap: wrap; }
.agency-bar input[type="text"] {
  min-width: min(380px, 70vw);
  border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; background: var(--card);
}

.stage { padding: 28px 28px 72px; max-width: 1280px; }
.stage-head { display: flex; justify-content: space-between; gap: 20px; align-items: end; margin-bottom: 22px; }
.eyebrow { margin: 0 0 6px; color: var(--muted); font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(32px, 5vw, 52px); letter-spacing: -0.045em; line-height: 0.95; font-weight: 800; }
.lede { margin: 10px 0 0; color: var(--muted); max-width: 46ch; }
.meta-chip {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 10px 14px;
}
.meta-chip span { display: block; color: var(--muted); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.meta-chip b { font-size: 15px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
}
.card h2 { margin: 0; font-size: 20px; letter-spacing: -0.03em; }
.hint { margin: 4px 0 14px; color: var(--muted); font-size: 13px; }

.health {
  color: #f4f8ff;
  background:
    radial-gradient(700px 220px at 90% 0%, rgba(92, 165, 255, 0.35), transparent 55%),
    linear-gradient(180deg, var(--health-2), var(--health));
  display: grid;
  grid-template-columns: 190px 1fr 300px;
  gap: 22px;
  align-items: center;
  border: 0;
}
.gauge { position: relative; width: 168px; height: 168px; margin: 0 auto; }
.gauge svg { transform: rotate(-90deg); }
.gauge .score { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.gauge .score b { font-size: 44px; line-height: 0.9; }
.gauge .score span { color: #b7aea3; font-size: 12px; }
.health h2 { font-size: 28px; }
.delta { font-weight: 700; margin-top: 4px; }
.delta.down { color: #f0a198; }
.delta.up { color: #9ed7b2; }
.issues { margin: 12px 0 0; padding-left: 18px; color: #d8d0c6; }
.spark { display: flex; gap: 4px; align-items: end; height: 42px; margin-top: 16px; }
.spark i { flex: 1; background: rgba(255,255,255,.22); border-radius: 3px 3px 0 0; min-height: 4px; }
.situation { background: rgba(255,255,255,.06); border-radius: 16px; padding: 16px 18px; }
.situation small { color: #b7aea3; letter-spacing: 0.1em; text-transform: uppercase; font-size: 11px; }
.situation p { margin: 8px 0 12px; line-height: 1.45; }
.step { display: block; font-weight: 700; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 14px 0; }
.kpi h3 { margin: 0; color: var(--muted); font-size: 11px; letter-spacing: 0.12em; }
.kpi .val { font-size: 28px; font-weight: 800; margin-top: 10px; letter-spacing: -0.04em; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); margin-top: 8px; font-size: 13px; }
.kpi.danger .val { color: var(--bad); }

.pill {
  display: inline-flex; align-items: center; gap: 4px;
  border-radius: 999px; padding: 3px 8px; font-size: 12px; font-weight: 700;
}
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.mute { background: var(--card-2); color: var(--muted); }

.section-title { margin: 8px 2px 10px; font-size: 13px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.alerts { display: grid; gap: 8px; margin-bottom: 16px; }
.alert { display: grid; grid-template-columns: 108px 1fr; gap: 12px; padding: 14px 16px; }
.alert .lvl { font-size: 11px; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; padding-top: 2px; }
.alert.critical { box-shadow: inset 3px 0 0 var(--bad); }
.alert.warning { box-shadow: inset 3px 0 0 var(--warn); }
.alert ul { margin: 6px 0 0; color: var(--muted); padding-left: 16px; }

.grid-2 { display: grid; grid-template-columns: 0.92fr 1.08fr; gap: 12px; margin-bottom: 12px; }
.funnel-row { display: grid; grid-template-columns: 92px 1fr 150px; gap: 12px; align-items: center; margin: 12px 0; }
.track { height: 14px; background: var(--card-2); border-radius: 999px; overflow: hidden; }
.funnel-fill { height: 100%; width: var(--w); border-radius: 999px; background: linear-gradient(90deg, #1560bd, #5ca5ff); }
.funnel-row b { font-size: 18px; font-variant-numeric: tabular-nums; }
.note { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th {
  text-align: left; color: var(--muted); font-size: 10px; letter-spacing: 0.08em;
  font-weight: 700; padding: 8px 8px 10px; cursor: pointer; white-space: nowrap;
}
td { padding: 11px 8px; border-top: 1px solid var(--line); vertical-align: middle; }
td.num, th.num { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--card-2); }
.name-cell { font-weight: 600; max-width: 280px; }
.type-pill { border-radius: 999px; padding: 3px 8px; font-weight: 700; font-size: 11px; background: var(--card-2); }
.type-pill.товарная { background: #e7f0ff; color: #15325f; }
.type-pill.мастер { background: #e5f5ec; color: #1f7a4d; }
.type-pill.rsya { background: #dce9fb; color: #1560bd; }

.chart-card { margin-bottom: 12px; }
.chart-head { display: flex; justify-content: space-between; gap: 12px; align-items: start; }
.toggles { display: flex; gap: 4px; flex-wrap: wrap; background: var(--card-2); padding: 4px; border-radius: 999px; }
.toggles button { border: 0; background: transparent; color: var(--muted); border-radius: 999px; padding: 6px 11px; cursor: pointer; }
.toggles button.on { background: var(--card); color: var(--ink); font-weight: 700; box-shadow: var(--shadow); }
.chart-wrap { height: 236px; }

.muted { color: var(--muted); font-size: 12px; }
.banner { margin: 14px 28px 0; padding: 14px 16px; border-radius: 16px; background: var(--bad-bg); color: var(--bad); }
.banner.wait { background: var(--warn-bg); color: var(--warn); }
.skeleton { padding: 48px 8px; color: var(--muted); }

.drawer {
  position: fixed; inset: 0 0 0 auto; width: min(460px, 100%);
  background: var(--card); border-left: 1px solid var(--line);
  padding: 22px; overflow: auto; z-index: 20;
}
.drawer[hidden], .backdrop[hidden] { display: none; }
.backdrop { position: fixed; inset: 0; background: rgba(20, 16, 12, .4); z-index: 19; }
.drawer-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }
.stat { background: var(--card-2); border-radius: 14px; padding: 12px; }
.stat span { display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; }
.stat b { font-size: 20px; }

@media (max-width: 1100px) {
  .topbar, .stage { padding-left: 16px; padding-right: 16px; }
  .topbar { flex-direction: column; align-items: stretch; }
  .health, .grid-2, .rules-grid, .connect-row { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .stage-head { display: grid; }
}

@media (max-width: 820px) {
  html { scroll-padding-top: 12px; }
  .topbar {
    position: sticky;
    display: grid;
    gap: 10px;
    align-items: stretch;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }
  .wordmark { min-width: 0; padding-top: 0; }
  .bar-main { width: 100%; }
  .bar-filters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
    align-items: stretch;
  }
  .field.goals,
  .field.dates-field { grid-column: 1 / -1; }
  .field select,
  .goal-picker,
  .dates,
  .dates input,
  .bar-actions #show,
  .bar-actions #pdf,
  .agency-bar input[type="text"] {
    min-width: 0;
    width: 100%;
    max-width: 100%;
    font-size: 16px;
  }
  .field select, .dates input, .btn { font-size: 16px; }
  .goal-picker { max-width: none; }
  .goal-picker-menu {
    position: fixed;
    left: 12px;
    right: 12px;
    width: auto;
    top: auto;
    max-height: min(70vh, 520px);
    overflow: auto;
  }
  .bar-actions { display: grid; grid-template-columns: 1fr 1fr auto; }
  .bar-actions #show { grid-column: 1 / -1; width: 100%; font-size: 16px; }
  .bar-actions #pdf { grid-column: 1 / 3; }
  .agency-bar, .banner { margin-left: 12px; margin-right: 12px; }
  .agency-bar input[type="text"] { min-width: 0; width: 100%; }
  .funnel-row { grid-template-columns: 72px 1fr; }
  .funnel-row > div:last-child { grid-column: 1 / -1; }
  .alert { grid-template-columns: 1fr; }
  .table-wrap { -webkit-overflow-scrolling: touch; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .header-logo { height: 30px; padding: 4px 6px; }
  h1 { font-size: 28px; }
  .kpi .val { font-size: 20px; }
  .cabinets-panel { inset: 0; border-radius: 0; }
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card { width: min(420px, 100%); padding: 28px; }
.login-card h1 { font-size: 32px; margin: 18px 0 0; }
.login-card form { display: grid; gap: 12px; margin-top: 18px; }
.login-card .btn.solid { width: 100%; }

.cabinets-panel {
  position: fixed;
  inset: 12px 12px 12px auto;
  width: min(560px, calc(100vw - 24px));
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  z-index: 30;
  overflow: auto;
  padding: 18px;
}
.cabinets-panel h2 { margin: 0 0 8px; }
.cabinet-card {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px;
  margin: 10px 0;
  background: var(--card-2);
  display: grid;
  gap: 8px;
}
.cabinet-head { display: flex; justify-content: space-between; gap: 8px; align-items: start; }
.cabinet-head small { display: block; color: var(--muted); font-weight: 400; }
.howto { background: var(--card-2); border-radius: 16px; padding: 14px 16px; margin: 12px 0; }
.howto ol { margin: 8px 0 0; padding-left: 18px; color: var(--ink); }
.howto li { margin: 6px 0; }
.howto code { font-size: 12px; background: var(--card); padding: 1px 6px; border-radius: 6px; }
.cabinet-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.pdf-root {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 390px;
  z-index: -1;
  pointer-events: none;
}
.pdf-page {
  width: 390px;
  min-height: 552px;
  padding: 16px 14px 18px;
  background: #f3f6fb;
  color: #1e2e47;
  box-sizing: border-box;
  font-family: Onest, sans-serif;
}
.pdf-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #1e2e47;
  color: #fff;
  border-radius: 14px;
  padding: 8px 10px;
  margin-bottom: 12px;
}
.pdf-head b {
  font-family: Unbounded, Onest, sans-serif;
  font-size: 14px;
  font-weight: 500;
}
.pdf-head small { display: block; opacity: 0.7; font-size: 10px; }
.pdf-page h1 {
  font-family: Unbounded, Onest, sans-serif;
  font-size: 22px;
  line-height: 1.15;
  margin: 0 0 6px;
  font-weight: 500;
  color: #1e2e47;
}
.pdf-meta { color: #5a7196; font-size: 12px; margin: 0 0 12px; line-height: 1.4; }
.pdf-health {
  background: linear-gradient(180deg, #15325f, #1e2e47);
  color: #fff;
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 10px;
}
.pdf-health b { font-family: Unbounded, Onest, sans-serif; font-size: 28px; }
.pdf-kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.pdf-kpi {
  background: #fff;
  border: 1px solid #d5e3f5;
  border-radius: 14px;
  padding: 10px;
}
.pdf-kpi span { display: block; color: #5a7196; font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; }
.pdf-kpi b { display: block; font-size: 18px; margin-top: 4px; }
.pdf-alert {
  background: #fff;
  border-radius: 12px;
  padding: 10px 12px;
  margin: 6px 0;
  border-left: 3px solid #1560bd;
  font-size: 13px;
}
.pdf-alert.critical { border-left-color: #c0392b; }
.pdf-alert.warning { border-left-color: #c47d12; }
.pdf-camp {
  background: #fff;
  border: 1px solid #d5e3f5;
  border-radius: 14px;
  padding: 10px;
  margin: 8px 0;
}
.pdf-camp-top { display: flex; justify-content: space-between; gap: 8px; align-items: start; }
.pdf-camp-top b { font-size: 13px; line-height: 1.3; }
.pdf-camp-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  margin-top: 8px;
  font-size: 12px;
}
.pdf-camp-grid span { color: #5a7196; }
.pdf-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #d5e3f5;
  font-size: 13px;
}
.pdf-foot { margin-top: 12px; color: #5a7196; font-size: 10px; text-align: center; }
