:root {
  --bg: #f4f6f9; --card: #fff; --line: #e3e7ee; --text: #1d2433; --muted: #6b7485;
  --primary: #2f6fed; --primary-d: #2459c4; --ok: #1f9d55; --warn: #d97706; --bad: #dc3545;
  --plan: #9aa3b2; --issued: #2f6fed; --done: #1f9d55; --forecast: rgba(47, 111, 237, .25);
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 20px; padding: 0 24px; height: 54px; background: #172033; color: #fff; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; white-space: nowrap; }
.logo-mark { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; font-size: 13px; font-weight: 700; letter-spacing: .5px; flex-shrink: 0; }
.tabs { display: flex; gap: 4px; }
.tabs a { color: #c5cddc; text-decoration: none; padding: 7px 12px; border-radius: 6px; white-space: nowrap; }
.tabs a:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.tabs a.active { background: rgba(255, 255, 255, .14); color: #fff; }
.top .link { color: #c5cddc; white-space: nowrap; }
.top-user { color: #fff; font-size: 13px; white-space: nowrap; }
.subbar { display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap; background: #1f2a40; color: #aeb8ca; font-size: 12.5px; padding: 6px 24px; font-variant-numeric: tabular-nums; }
.subbar:empty { display: none; }
.subbar b { color: #fff; font-weight: 600; }
/* шкала выполнения за сегодня */
.top-progress { display: flex; align-items: center; gap: 10px; min-width: 260px; }
.top-progress .bar { flex: 1; height: 6px; min-width: 80px; background: rgba(255, 255, 255, .13); border-radius: 3px; overflow: hidden; }
.top-progress .bar > i { display: block; height: 100%; width: 0; background: var(--done); border-radius: 3px; transition: width .4s ease; }
/* зелёная точка — сводка идёт живым потоком; серая — поток оборвался, работает опрос раз в 5 с */
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: #5a6578; flex-shrink: 0; margin-left: auto; }
.live-dot.on { background: var(--ok); animation: live-pulse 2s ease-in-out infinite; }
@keyframes live-pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
/* прогресс задачи в списке */
.progress { display: block; height: 4px; background: #edf0f5; border-radius: 2px; overflow: hidden; margin-top: 5px; }
.progress > i { display: block; height: 100%; background: var(--done); border-radius: 2px; }
.grid td .progress { min-width: 60px; }

main { max-width: 1400px; margin: 0 auto; padding: 20px 24px 60px; }
.toolbar, .row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.row.end { justify-content: flex-end; margin: 14px 0 0; }
.spacer { flex: 1; }
.grow { flex: 1; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; margin-bottom: 16px; overflow-x: auto; }
.card h3 { margin: 0 0 12px; font-size: 15px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.card-head h3 { margin: 0; }
.empty { padding: 18px 4px; color: var(--muted); }

button { font: inherit; padding: 7px 14px; border-radius: 7px; border: 1px solid var(--line); background: #fff; color: var(--text); cursor: pointer; }
button:hover { background: #f0f3f8; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: var(--primary-d); }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; padding: 4px 6px; color: var(--primary); }
button.small { padding: 3px 9px; font-size: 12.5px; }
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid #cfd6e1; border-radius: 7px; background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(47, 111, 237, .25); border-color: var(--primary); }
textarea { width: 100%; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
label > input, label > select, label > textarea { color: var(--text); font-size: 14px; }
#taskSearch, #statsSearch { width: 320px; }
#statsTableHead { flex-wrap: wrap; gap: 8px 12px; }

/* Единая высота полей, списков и кнопок — в одной строке они стоят ровно */
input:not([type="checkbox"]):not([type="radio"]), select, button { height: 36px; }
button.small { height: 30px; }
button.link, textarea { height: auto; }
/* В строке с подписанными полями кнопки выравниваются по нижнему краю полей, а не по центру блока «подпись + поле» */
.row:has(> label), .toolbar:has(> label), .toolbar:has(> .period) { align-items: flex-end; }
/* равные колонки: поля не разъезжаются из-за разной длины подписей */
.row.cols-3 > label { flex: 1 1 150px; min-width: 0; }
.row > label, .toolbar > label, .period > label { margin-bottom: 0; }

.period { display: flex; gap: 8px; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.presets button.small { height: 36px; } /* в одну линию с полями дат */
#snapshotBtn { margin-left: auto; } /* при переносе строки остаётся справа */

table.grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.grid th, .grid td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.grid th { font-size: 12px; font-weight: 500; color: var(--muted); }
.grid td.num, .grid th.num { text-align: right; }
.grid tbody tr { cursor: default; }
.grid tbody tr.clickable { cursor: pointer; }
.grid tbody tr.clickable:hover { background: #f7f9fc; }
.grid tbody tr.selected { background: #eef4ff; }
.grid td:first-child { white-space: normal; min-width: 180px; max-width: 320px; }
.grid td .sub { display: block; color: var(--muted); font-size: 12px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grid td a.sub { color: var(--primary); text-decoration: none; }
.grid td a.sub:hover { text-decoration: underline; }
.grid td .actions { display: flex; gap: 6px; justify-content: flex-end; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; padding-top: 10px; color: var(--muted); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; background: #eef1f6; color: var(--muted); }
.badge.ok { background: #e3f6ec; color: var(--ok); }
.badge.warn { background: #fff1dc; color: var(--warn); }
.badge.bad { background: #fde8ea; color: var(--bad); }
.badge.sys { background: #eef4ff; color: var(--primary); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 12px; }
.chip { background: #f3f5f9; border: 1px solid var(--line); border-radius: 999px; padding: 3px 11px; font-size: 12.5px; }
.chip b { font-weight: 600; }
.chip.removable { display: inline-flex; align-items: center; gap: 6px; padding-right: 5px; }
.chip.removable button { height: 20px; width: 20px; padding: 0; border: 0; border-radius: 50%; background: none; color: var(--muted); font-size: 15px; line-height: 1; cursor: pointer; }
.chip.removable button:hover { background: var(--bad); color: #fff; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.sw { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.sw-plan { border: 1.5px solid var(--plan); }
.sw-issued { background: var(--issued); }
.sw-done { background: var(--done); }
.sw-forecast { background: var(--forecast); }
.sw-now { background: var(--warn); }
.error-line { margin-top: 10px; color: var(--bad); font-size: 13px; }

svg.chart { width: 100%; height: auto; display: block; }
svg.chart .gl { stroke: #edf0f5; }
svg.chart .ax { fill: #8a93a3; font-size: 10px; }
svg.chart .b-plan { fill: none; stroke: var(--plan); stroke-width: 1.5; }
svg.chart .b-issued { fill: var(--issued); }
svg.chart .b-done { fill: var(--done); }
svg.chart .b-forecast { fill: var(--forecast); }
svg.chart .b-weight { fill: #6f95f2; }
svg.chart .b-weight:hover { fill: var(--primary); }
svg.chart .now { stroke: var(--warn); stroke-dasharray: 3 3; }
svg.chart.editor { cursor: crosshair; touch-action: none; user-select: none; background: #fafbfd; border: 1px solid var(--line); border-radius: 8px; }
svg.chart.mini { max-height: 90px; margin: -4px 0 10px; }

/* вкладка «Ресурсы» */
.alerts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.alerts:empty { display: none; }
.alert { padding: 9px 12px; border-radius: 8px; border: 1px solid; font-size: 13px; }
.alert.ok { background: #e9f8f0; border-color: #c3ead4; color: #17703e; }
.alert.warn { background: #fff6e5; border-color: #f4d9a8; color: #8a4b00; }
.alert.bad { background: #fde8ea; border-color: #f5c2c8; color: #9b1c2c; }
.res-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.res-grid > .card { margin-bottom: 0; }
.card h3 .small, .card h4 .small { font-weight: 400; margin-left: 6px; }
.card h3.sub-h { margin-top: 18px; }
.kv { display: grid; grid-template-columns: minmax(150px, max-content) minmax(0, 1fr); gap: 7px 18px; margin: 0; font-variant-numeric: tabular-nums; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.kv .sub { color: var(--muted); font-size: 12.5px; }
.meter { height: 6px; background: #edf0f5; border-radius: 3px; overflow: hidden; margin-top: 5px; max-width: 320px; }
.meter > i { display: block; height: 100%; background: var(--primary); border-radius: 3px; }
.meter.warn > i { background: var(--warn); }
.meter.bad > i { background: var(--bad); }
.grid td .meter { width: 90px; margin-left: auto; }
.grid tr.total td { font-weight: 600; background: #f7f9fc; }
.grid.compact td:first-child { min-width: 0; max-width: none; white-space: normal; }
.host + .host { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.host h4 { margin: 0 0 8px; font-size: 14px; overflow-wrap: anywhere; }
.sparks { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px 22px; }
.spark h4 { display: flex; justify-content: space-between; gap: 8px; margin: 0 0 4px; font-size: 12.5px; font-weight: 400; color: var(--muted); }
.spark h4 b { color: var(--text); font-weight: 600; white-space: nowrap; }
.spark svg { display: block; width: 100%; height: 56px; background: #fafbfd; border-radius: 6px; }
.spark .line { fill: none; stroke: var(--primary); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.spark .area { fill: rgba(47, 111, 237, .10); }
.spark-axis { display: flex; justify-content: space-between; font-size: 11px; color: #8a93a3; margin-top: 2px; }

.profiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; margin-bottom: 16px; }
.profile-card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; }
.profile-card h4 { margin: 0 0 4px; font-size: 14px; display: flex; gap: 8px; align-items: center; }
.profile-card .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
button { white-space: nowrap; }
/* ссылка, выглядящая как кнопка (например, на документацию API) */
.button-link { display: inline-flex; align-items: center; height: 36px; padding: 0 14px; border: 1px solid var(--line); border-radius: 7px; background: #fff; color: var(--text); text-decoration: none; white-space: nowrap; }
.button-link:hover { background: #f0f3f8; }
@media (min-width: 761px) {
  #profileName { min-width: 300px; }
  #keyName { width: 300px; }
}
.weights { display: grid; grid-template-columns: repeat(auto-fill, minmax(50px, 1fr)); gap: 4px; margin: 10px 0; }
.weights label { margin: 0; text-align: center; font-size: 11px; gap: 2px; }
.weights input { padding: 4px 2px; text-align: center; font-size: 12px; width: 100%; height: 30px; }
.weights .share { font-size: 10.5px; color: var(--muted); }

dialog { border: 0; border-radius: 12px; padding: 22px 24px; width: min(640px, 94vw); box-shadow: 0 20px 60px rgba(15, 25, 45, .25); }
dialog::backdrop { background: rgba(15, 25, 45, .35); }
dialog h3 { margin: 0 0 14px; }
dialog details { margin: 4px 0 6px; }
dialog summary { cursor: pointer; color: var(--primary); margin-bottom: 10px; }
.field-error { color: var(--bad); font-size: 12.5px; min-height: 16px; margin: -4px 0 6px; }
#importResult { margin: 12px 0 0; }
#importResult .grid { margin-top: 10px; }
.import-errors { margin: 8px 0 0; padding-left: 20px; max-height: 220px; overflow: auto; }
.import-errors li { margin-bottom: 3px; }

body.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #172033; }
.login-card { width: min(380px, 92vw); background: #fff; border-radius: 12px; padding: 28px 28px 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.login-card h1 { margin: 0; font-size: 18px; }
.login-card > p { margin: 0 0 18px; }
.login-card button { width: 100%; height: 40px; margin-top: 4px; }
.login-card .small { margin: 14px 0 0; line-height: 1.6; }

.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: #172033; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 8px 24px rgba(0, 0, 0, .2); max-width: 420px; font-size: 13px; }
.toast.bad { background: #b42335; }

/* =============================== адаптивность =============================== */

/* планшет и уже: таблицы превращаются в карточки (две колонки), шапка поджимается */
@media (max-width: 1100px) {
  .top { gap: 14px; padding: 0 16px; }
  .subbar { padding: 6px 16px; }
  main { padding: 16px 16px 48px; }
  #taskSearch, #statsSearch { width: 260px; }
  .weights { grid-template-columns: repeat(12, 1fr); } /* два ровных ряда по 12 часов */

  /* таблица → карточки: название сверху, ниже «поле — значение», кнопки внизу */
  .card:has(> table.cards) { background: none; border: 0; padding: 0; overflow: visible; }
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tbody { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  table.cards tr { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
  table.cards tr:has(> td.empty) { grid-column: 1 / -1; }
  table.cards tr.selected { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 1px var(--primary); }
  table.cards td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; border: 0; padding: 4px 0; white-space: normal; text-align: right; min-width: 0; max-width: none; }
  table.cards td::before { content: attr(data-label); color: var(--muted); font-size: 12.5px; text-align: left; flex-shrink: 0; }
  table.cards td:first-child { display: block; text-align: left; font-size: 15px; padding: 2px 0 8px; margin-bottom: 4px; border-bottom: 1px solid var(--line); min-width: 0; max-width: none; }
  table.cards td:first-child::before, table.cards td[data-label=""]::before { content: none; }
  table.cards td .sub { max-width: none; white-space: normal; margin-top: 2px; }
  table.cards td .actions { justify-content: flex-start; flex-wrap: wrap; width: 100%; padding-top: 6px; }
  table.cards td.empty { text-align: center; border: 0; padding: 18px 4px; }
  .pager { justify-content: center; }
  .res-grid { grid-template-columns: 1fr; }
  .sparks { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  table.cards tr.total { background: #f7f9fc; }
  table.cards tr.total td { background: none; }
  table.cards td:empty { display: none; } /* пустые ячейки итоговой строки — не нужны в карточке */
  table.cards td > .val { text-align: right; }
}

/* телефон */
@media (max-width: 760px) {
  .top { position: static; flex-wrap: wrap; height: auto; padding: 10px 12px 8px; gap: 8px 12px; }
  .top .spacer { display: none; }
  .brand { flex: 1; }
  .brand-name { display: none; } /* на телефоне — только знак DS */
  .tabs { order: 2; flex-basis: 100%; gap: 2px; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs a { padding: 6px 9px; font-size: 13.5px; }
  .subbar { padding: 6px 12px; font-size: 12px; }
  main { padding: 12px 10px 40px; }
  .card { padding: 12px; border-radius: 8px; }

  .toolbar { gap: 8px; }
  .toolbar .spacer { display: none; }
  .toolbar > button, .toolbar > label, #taskSearch, #statsSearch { width: 100%; }
  .toolbar > label > select { width: 100%; }
  .period { width: 100%; }
  .period > label { flex: 1; min-width: 0; }
  .period input { width: 100%; }
  .presets { width: 100%; }
  .presets button { flex: 1 1 calc(33.333% - 6px); } /* по три в ряд, остаток делит строку поровну */
  button.small { padding: 0 12px; font-size: 13px; height: 34px; }
  .row > label { flex: 1 1 100%; }
  .row > label > input, .row > label > select { width: 100%; }
  .row > .spacer { display: none; }
  form.row > button { width: 100%; }

  table.cards tbody { grid-template-columns: 1fr; gap: 10px; }

  .profiles { grid-template-columns: 1fr; }
  .weights { grid-template-columns: repeat(6, 1fr); }
  .sparks { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; gap: 1px; }
  .kv dd { margin-bottom: 9px; }
  .grid.compact th, .grid.compact td { padding: 7px 5px; }
  .chips { gap: 6px; }
  .chip { font-size: 12px; padding: 3px 9px; }

  /* диалоги — на весь экран */
  dialog { width: 100%; max-width: none; height: 100%; max-height: none; margin: 0; border-radius: 0; padding: 18px 14px; }
  dialog .row.end { position: sticky; bottom: -18px; background: #fff; padding: 12px 0 6px; }
  dialog .row.end button { flex: 1; }
  dialog .row.end #taskDelete { flex: 1 1 100%; order: 2; }

  .toasts { left: 10px; right: 10px; bottom: 10px; }
  .toast { max-width: none; }
}
