:root {
  --paper: #F3F2F7;
  --surface: #FFFFFF;
  --ink: #1D1A2B;
  --ink-2: #5E5A70;
  --ink-3: #8D899E;
  --line: #DEDBE8;
  --line-2: #ECEAF2;
  --plum: #5B2A86;
  --plum-2: #EFE7F7;
  --go: #1F8A5B;
  --go-2: #E3F3EB;
  --warn: #B26B00;
  --warn-2: #FBF0DC;
  --bad: #B3261E;
  --bad-2: #FBE6E4;
  --r: 6px;
  --font: "Golos Text", system-ui, sans-serif;
  --display: "Unbounded", "Golos Text", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  overflow-x: hidden;
  background: var(--paper); color: var(--ink);
  font: 14px/1.45 var(--font); font-variant-numeric: tabular-nums;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--plum); }
:focus-visible { outline: 2px solid var(--plum); outline-offset: 2px; }

/* ---------- каркас ---------- */
.app { display: grid; grid-template-columns: 216px minmax(0, 1fr); min-height: 100vh; }
.rail {
  background: var(--ink); color: #E9E6F2; padding: 20px 14px;
  display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh;
}
.brand { font: 600 17px/1.1 var(--display); letter-spacing: -0.01em; color: #fff; padding: 0 6px; }
.brand small { display: block; font: 400 12px var(--font); color: #A7A2BA; margin-top: 4px; letter-spacing: 0; }
.rail select {
  width: 100%; background: #2C2840; border: 1px solid #3B3653; border-radius: var(--r);
  padding: 7px 8px; color: #fff;
}
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav button {
  text-align: left; background: none; border: 0; color: #C9C4DA; padding: 8px 10px;
  border-radius: var(--r); cursor: pointer;
}
.nav button:hover { background: #2C2840; color: #fff; }
.nav button[aria-current="page"] { background: var(--plum); color: #fff; }
.rail-foot { margin-top: auto; font-size: 12px; color: #A7A2BA; display: grid; gap: 6px; }
.rail-foot button { background: none; border: 0; color: #A7A2BA; padding: 0; text-align: left; cursor: pointer; text-decoration: underline; }

main { padding: 24px 28px 64px; min-width: 0; }
.head { display: flex; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.head h1 { font: 500 22px/1.15 var(--display); margin: 0; letter-spacing: -0.01em; }
.head .sub { color: var(--ink-2); font-size: 13px; }
.head .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }

.dry {
  background: var(--warn-2); border: 1px solid #EBCB8E; color: #6B4300;
  padding: 10px 14px; border-radius: var(--r); margin-bottom: 16px; font-size: 13px;
}

/* ---------- элементы ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); padding: 7px 12px;
  border-radius: var(--r); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--plum); border-color: var(--plum); color: #fff; }
.btn.primary:hover { background: #4A206F; }
.btn.danger { color: var(--bad); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }

input[type=text], input[type=number], input[type=password], input[type=date], input[type=datetime-local],
input[type=time], select, textarea {
  border: 1px solid var(--line); background: var(--surface); border-radius: var(--r); padding: 6px 8px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--plum); outline: none; box-shadow: 0 0 0 3px var(--plum-2); }
input.num { width: 92px; text-align: right; }

.pill {
  display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px;
  background: var(--line-2); color: var(--ink-2); white-space: nowrap;
}
.pill.go { background: var(--go-2); color: var(--go); }
.pill.warn { background: var(--warn-2); color: var(--warn); }
.pill.bad { background: var(--bad-2); color: var(--bad); }
.pill.plum { background: var(--plum-2); color: var(--plum); }
.muted { color: var(--ink-3); }
.small { font-size: 12px; }

/* ---------- таблицы ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--ink-2); padding: 10px 12px;
  border-bottom: 1px solid var(--line); background: #FAF9FC; white-space: nowrap;
}
td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; white-space: nowrap; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: #FAF8FD; }
tr.off td { color: var(--ink-3); }
.name { font-weight: 500; }
.id { color: var(--ink-3); font-size: 12px; }

/* расход / лимит */
.meter { width: 120px; height: 6px; border-radius: 3px; background: var(--line-2); overflow: hidden; margin-top: 5px; }
.meter i { display: block; height: 100%; background: var(--go); }
.meter i.hot { background: var(--warn); }
.meter i.over { background: var(--bad); }

/* ---------- аукционная лестница ---------- */
.ladder { position: relative; height: 44px; min-width: 260px; margin: 2px 6px 0; }
.ladder .track { position: absolute; left: 0; right: 0; top: 15px; height: 4px; background: var(--line-2); border-radius: 2px; }
.ladder .rung {
  position: absolute; top: 9px; width: 2px; height: 16px; background: var(--ink-3);
}
.ladder .rung.tgt { background: var(--plum); width: 3px; }
.ladder .rung span {
  position: absolute; top: 17px; left: 50%; transform: translateX(-50%);
  font-size: 10.5px; color: var(--ink-3); white-space: nowrap;
}
.ladder .rung.tgt span { color: var(--plum); font-weight: 600; }
.ladder .rung.low span { top: 29px; }
.ladder-vals { display: flex; gap: 4px 12px; flex-wrap: wrap; font-size: 11.5px; color: var(--ink-2); margin: 0 0 4px; }
.ladder-vals .tgt { color: var(--plum); font-weight: 600; }
.ladder-vals .capv { color: var(--bad); }
.ladder .bid {
  position: absolute; top: -2px; transform: translateX(-50%);
  background: var(--ink); color: #fff; font-size: 11px; padding: 0 5px; border-radius: 3px; white-space: nowrap;
  transition: left .4s ease;
}
.ladder .bid::after {
  content: ""; position: absolute; left: 50%; top: 100%; transform: translateX(-50%);
  border: 4px solid transparent; border-top-color: var(--ink);
}
.ladder .cap { position: absolute; top: 9px; height: 16px; border-left: 2px dashed var(--bad); }

/* ---------- drawer ---------- */
.scrim { position: fixed; inset: 0; background: rgba(29, 26, 43, .32); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; }
.scrim.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(560px, 100vw); background: var(--paper);
  border-left: 1px solid var(--line); transform: translateX(100%); transition: transform .25s ease;
  z-index: 21; display: flex; flex-direction: column; visibility: hidden;
}
.drawer.open { transform: none; visibility: visible; }
.drawer header { padding: 18px 22px; border-bottom: 1px solid var(--line); background: var(--surface); display: flex; gap: 12px; align-items: flex-start; }
.drawer header h2 { font: 500 17px/1.2 var(--display); margin: 0 0 4px; }
.drawer header .x { margin-left: auto; }
.drawer .body { overflow-y: auto; padding: 8px 22px 24px; flex: 1; }
.drawer footer { padding: 14px 22px; border-top: 1px solid var(--line); background: var(--surface); display: flex; gap: 8px; }

fieldset { border: 0; border-bottom: 1px solid var(--line); padding: 16px 0; margin: 0; }
fieldset:last-of-type { border-bottom: 0; }
legend { font-weight: 600; padding: 0; margin-bottom: 4px; float: left; width: 100%; }
legend + * { clear: both; }
.hint { color: var(--ink-2); font-size: 12.5px; margin: 0 0 10px; max-width: 60ch; }
.row { display: flex; gap: 10px 14px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.row label { display: flex; gap: 6px; align-items: center; }
.field { display: grid; gap: 4px; }
.field > span { font-size: 12px; color: var(--ink-2); }
.toggle { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.toggle input { width: 16px; height: 16px; accent-color: var(--plum); }
.days { display: flex; gap: 4px; }
.days label { cursor: pointer; }
.days input { position: absolute; opacity: 0; }
.days span { display: inline-block; width: 34px; text-align: center; padding: 4px 0; border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); font-size: 12px; }
.days input:checked + span { background: var(--plum); border-color: var(--plum); color: #fff; }
.days input:focus-visible + span { outline: 2px solid var(--plum); outline-offset: 2px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; background: var(--surface); }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; }
.seg span { display: block; padding: 6px 12px; border-left: 1px solid var(--line); }
.seg label:first-child span { border-left: 0; }
.seg input:checked + span { background: var(--plum); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--plum); outline-offset: -2px; }
.disabled-block { opacity: .45; pointer-events: none; }

/* ---------- формы на странице ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 18px 20px; max-width: 760px; margin-bottom: 18px; }
.panel h2 { font: 500 15px/1.3 var(--display); margin: 0 0 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }

/* журнал */
.ev { display: grid; grid-template-columns: 128px 86px 1fr; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--line-2); font-size: 13px; }
.ev:last-child { border-bottom: 0; }
.ev time { color: var(--ink-3); }

.empty { padding: 36px 20px; text-align: center; color: var(--ink-2); }
.empty p { margin: 0 0 12px; }

/* тосты */
.toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; }
.toast { background: var(--ink); color: #fff; padding: 9px 14px; border-radius: var(--r); font-size: 13px; box-shadow: 0 6px 20px rgba(29,26,43,.25); }
.toast.bad { background: var(--bad); }

/* вход */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login form { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 28px; width: min(360px, 100%); display: grid; gap: 14px; }
.login h1 { font: 600 20px var(--display); margin: 0; }

.diverge { background: var(--warn-2); }
.cell-edit { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .rail { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 12px 16px; gap: 10px; }
  .rail select { width: auto; max-width: 50vw; }
  .nav { flex-direction: row; overflow-x: auto; width: 100%; }
  .nav button { white-space: nowrap; }
  .head .actions { margin-left: 0; }
  .rail-foot { display: none; }
  main { padding: 16px 16px 48px; }
  .grid2 { grid-template-columns: 1fr; }
  .ev { grid-template-columns: 1fr; gap: 2px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
[hidden] { display: none !important; }

/* статистика */
.st-sum { display: flex; flex-wrap: wrap; gap: 10px 36px; margin: 0 0 16px; padding: 14px 18px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); }
.st-sum div { display: grid; gap: 2px; }
.st-sum span { font-size: 12px; color: var(--ink-2); }
.st-sum b { font: 500 20px/1.2 var(--display); }
.st-sum small { font-size: 12px; color: var(--ink-3); }
table.st td { vertical-align: bottom; padding-top: 6px; padding-bottom: 8px; white-space: nowrap; }
table.st td.date { white-space: nowrap; }
table.st td.date small { display: block; font-size: 11px; color: var(--ink-3); }
table.st td i { display: block; font-style: normal; font-size: 10.5px; line-height: 1.2; margin-bottom: 1px; }
table.st td i.up { color: var(--go); }
table.st td i.down { color: var(--bad); }
table.st td.over { color: var(--bad); font-weight: 600; }
table.st tbody.st-agg td { background: #FAF9FC; font-weight: 600; }
table.st tbody.st-agg tr:last-child td { border-bottom: 1px solid var(--line); }
table.st tr.today td { background: #FCFBF6; }

/* выбор кампаний и массовая настройка */
th.chk, td.chk { width: 36px; padding-right: 0; cursor: default; }
td.chk input, th.chk input { width: 16px; height: 16px; accent-color: var(--plum); cursor: pointer; }
.bulkbar { position: sticky; bottom: 16px; margin-top: 14px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: var(--ink); color: #fff; padding: 10px 14px; border-radius: var(--r); box-shadow: 0 8px 24px rgba(29,26,43,.25); }
.bulkbar span { margin-right: auto; font-weight: 500; }
.bulkbar .btn:not(.primary) { background: transparent; color: #fff; border-color: #4A4560; }
.bulk-sec .bulk-body { margin-top: 10px; padding-left: 24px; }
.bulk-sec.on { background: linear-gradient(var(--plum-2), var(--plum-2)) left / 3px 100% no-repeat; padding-left: 10px; }
tfoot tr.tot td { border-top: 1px solid var(--line); background: #FAF9FC; }
.field .seg { justify-self: start; }
input[name=rec_percent] { width: 132px; }
.recrow { white-space: nowrap; }
.recrow span { color: var(--ink-3); }
.recrow.below b { color: var(--bad); }
.recrow.above b { color: var(--warn); }
.recrow.eq b { color: var(--go); }
