:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --text: #1d2330;
  --muted: #6b7385;
  --border: #e3e6ec;
  --side: #22262d;
  --side-text: #c9ced8;
  --side-active: #2f343d;
  --accent: #4b5bdc;
  --accent-soft: #eceefc;
  --ok: #1f9d5b;
  --ok-soft: #e5f6ec;
  --warn: #b7791f;
  --warn-soft: #fdf3e1;
  --danger: #d14343;
  --danger-soft: #fbeaea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 24, 36, .06), 0 1px 8px rgba(20, 24, 36, .04);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; font-size: 12.5px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 16px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }

/* ---------- layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 250px; flex: 0 0 250px; background: var(--side); color: var(--side-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { color: #fff; font-weight: 600; font-size: 16px; padding: 20px 20px 16px; }
.sidebar nav { display: flex; flex-direction: column; padding: 0 8px; gap: 2px; }
.sidebar nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 8px;
  color: var(--side-text); text-decoration: none;
}
.sidebar nav a:hover { background: var(--side-active); color: #fff; }
.sidebar nav a.active { background: var(--side-active); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.sidebar .ico { width: 20px; text-align: center; }
.sidebar .lbl { flex: 1; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11.5px; padding: 0 7px; line-height: 18px; font-weight: 600; }
.logout { margin-top: auto; padding: 16px 20px; color: var(--side-text); }
.main { flex: 1; min-width: 0; padding: 20px 28px 48px; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.topbar h1 { font-size: 22px; margin: 0; flex: 1; min-width: 0; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.burger { display: none; background: none; border: 0; font-size: 22px; cursor: pointer; padding: 4px 8px; color: var(--text); }
.backdrop { display: none; }

@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .backdrop.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
  .burger { display: block; }
  .main { padding: 12px 16px 40px; }
  .topbar h1 { font-size: 18px; }
}

/* ---------- cards / grid ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card > h2:first-child { margin-top: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); align-items: start; }
.grid.tiles { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .label { color: var(--muted); font-size: 12.5px; }
.tile .value { font-size: 22px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { color: var(--muted); font-size: 12.5px; }
a.tile { color: inherit; text-decoration: none; }
a.tile:hover { border-color: var(--accent); }
.tile.attention { border-color: var(--warn); background: var(--warn-soft); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.hidden-row td { opacity: .55; }
.empty { color: var(--muted); padding: 24px; text-align: center; }

/* ---------- forms ---------- */
form.stack { display: flex; flex-direction: column; gap: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; font-size: 13px; }
label.inline { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.hint { font-weight: 400; color: var(--muted); font-size: 12px; }
input[type=text], input[type=number], input[type=url], input[type=password], input[type=datetime-local], input:not([type]), select, textarea {
  font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
textarea { min-height: 110px; resize: vertical; }
textarea.tall { min-height: 220px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.radio-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 8px; }
.radio-cards label { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; cursor: pointer; font-weight: 400; flex-direction: row; align-items: flex-start; gap: 8px; }
.radio-cards label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-cards b { display: block; }
.inline-form { display: inline; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input, .filters select { width: auto; min-width: 180px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 500; border-radius: 8px; padding: 7px 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap; text-decoration: none;
}
.btn:hover { text-decoration: none; border-color: #c9ceda; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--danger); border-color: #efc5c5; }
.btn.small { padding: 3px 9px; font-size: 12.5px; }

/* ---------- pills / flash ---------- */
.pill { display: inline-block; border-radius: 999px; padding: 1px 9px; font-size: 12px; font-weight: 500; background: var(--surface-2); border: 1px solid var(--border); white-space: nowrap; }
.pill.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.pill.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.pill.danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.pill.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.flash { border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; background: var(--ok-soft); color: #17693f; }
.flash.error { background: var(--danger-soft); color: #9b2c2c; }
.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs .badge { margin-left: 4px; }
.pre { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; word-break: break-word; }
.cat-block { margin-bottom: 16px; }
.cat-title { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.cat-title h2 { margin: 0; flex: 1; }
.pager { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart .bar { fill: var(--accent); }
.chart .bar:hover { fill: #3342c4; }
.chart .axis text { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .grid-line { stroke: var(--border); }
.bars-h { display: flex; flex-direction: column; gap: 8px; }
.bars-h .row { display: grid; grid-template-columns: minmax(90px, 40%) 1fr auto; gap: 8px; align-items: center; }
.bars-h .track { background: var(--surface-2); border-radius: 4px; height: 10px; }
.bars-h .fill { background: var(--accent); height: 10px; border-radius: 4px; }

/* ---------- login ---------- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.chart-tip { position: fixed; z-index: 50; pointer-events: none; background: var(--side); color: #fff; font-size: 12px; padding: 4px 8px; border-radius: 6px; white-space: nowrap; }
[hidden] { display: none !important; }
html, body { max-width: 100%; overflow-x: hidden; }
.cat-title h2 { min-width: 0; }

/* На телефоне широкие таблицы превращаются в карточки */
@media (max-width: 700px) {
  table.stack-table, table.stack-table tbody { display: block; }
  table.stack-table tr { display: flex; flex-wrap: wrap; gap: 4px 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
  table.stack-table tr:first-child:has(th) { display: none; }
  table.stack-table td { display: block; border: 0; padding: 0; }
  table.stack-table td:first-child { flex-basis: 100%; }
  table.stack-table td.num { text-align: left; }
  table.stack-table td.right, table.stack-table td.right .row-actions { justify-content: flex-start !important; text-align: left; }
  .table-wrap { margin: 0; padding: 0; }
  .card { padding: 14px; }
  .grid.two { grid-template-columns: minmax(0, 1fr); }
  .filters input, .filters select { min-width: 0; flex: 1 1 140px; }
}
