:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6b6a65;
  --line: rgba(11, 11, 11, .10);
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, .10);
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #252523;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --line: rgba(255, 255, 255, .10);
    --accent: #3987e5;
    --accent-soft: rgba(57, 135, 229, .16);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #252523;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --line: rgba(255, 255, 255, .10);
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, .16);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

h1, h2, p { margin: 0; }
h2 { font-size: 15px; font-weight: 600; }
.sub { color: var(--muted); font-size: 12.5px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px 18px;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 12px;
}

.head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.btn {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: none; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { color: var(--ink); background: var(--surface-2); }
.btn.danger { color: var(--critical); }
.btn.danger-solid { background: var(--critical); border-color: var(--critical); color: #fff; }
.btn.small { padding: 2px 8px; font-size: 12px; }
.btn.wide { width: 100%; padding: 8px; }
:is(.btn, input, select, summary, .route-option, .seg-opt, .copy):focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select, textarea {
  font: inherit;
  color: var(--ink);
  background: var(--page);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 9px;
}
.search { width: 150px; padding: 4px 9px; font-size: 13px; }
select { padding: 4px 6px; font-size: 13px; }

/* вход */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 340px; padding: 26px; display: grid; gap: 14px; }
.login-card h1 { font-size: 20px; font-weight: 600; }

/* шапка */
.top {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand strong { font-size: 15px; font-weight: 600; }
.farm-state { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--ink-2); min-width: 0; }
.top-actions { margin-left: auto; display: flex; gap: 2px; }

main { max-width: 1520px; margin: 0 auto; padding: 16px 24px 28px; display: grid; gap: 14px; }

/* статус: значок + подпись, цвет не единственный носитель смысла */
.st { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.st i {
  width: 16px;
  height: 16px;
  flex: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font: 700 11px/1 system-ui, sans-serif;
  color: #fff;
  background: var(--muted);
}
.st.good i { background: var(--good); }
.st.warning i { background: var(--warning); color: #0b0b0b; }
.st.serious i { background: var(--serious); color: #0b0b0b; }
.st.critical i { background: var(--critical); }

/* уведомления */
#alerts { display: grid; gap: 8px; }
#alerts:empty { display: none; }
.alert {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--critical);
  border-radius: 8px;
  background: var(--surface);
}
.alert.warn { border-left-color: var(--warning); }

/* плитки */
.tiles { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; min-width: 0; }
.tile .label { color: var(--muted); font-size: 12.5px; }
.tile .value { font-size: 24px; font-weight: 600; line-height: 1.3; }
.tile .value.small { font-size: 18px; line-height: 1.65; font-family: var(--mono); font-weight: 500; }
.tile .value em { font-style: normal; font-weight: 400; font-size: 14px; color: var(--muted); margin-left: 4px; }
.tile .note { color: var(--ink-2); font-size: 12.5px; overflow-wrap: anywhere; }
.tile .note .st { white-space: normal; }

/* SIM и тарифы */
.sims { display: grid; gap: 0; }
.sim-row {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr) 230px auto;
  gap: 14px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.sim-row:last-child { border-bottom: 0; }
.sim-days { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.1; }
.sim-days .big { font-size: 30px; font-weight: 600; }
.sim-days .big.none { color: var(--muted); font-weight: 400; }
.sim-days .unit { font-size: 12.5px; color: var(--muted); }
.sim-info { min-width: 0; overflow-wrap: anywhere; }
.sim-info b { font-weight: 600; }
.sim-info .second, .sim-meter .second { color: var(--muted); font-size: 12px; }
.sim-meter { display: grid; gap: 4px; }
.sim-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.meter.sim { height: 6px; border-radius: 3px; margin-top: 0; }
.meter.sim span { border-radius: 3px; }
/* заполнение — срочность, дорожка — светлый шаг того же цвета */
.meter.lvl-good { background: var(--accent-soft); }
.meter.lvl-good span { background: var(--accent); }
.meter.lvl-warning { background: color-mix(in srgb, var(--warning) 22%, transparent); }
.meter.lvl-warning span { background: var(--warning); }
.meter.lvl-serious { background: color-mix(in srgb, var(--serious) 22%, transparent); }
.meter.lvl-serious span { background: var(--serious); }
.meter.lvl-critical, .meter.lvl-over { background: color-mix(in srgb, var(--critical) 22%, transparent); }
.meter.lvl-critical span, .meter.lvl-over span { background: var(--critical); }

/* температура модема */
.temp { font-size: 12.5px; color: var(--ink-2); border: 1px solid var(--line); border-radius: 20px; padding: 0 8px; font-variant-numeric: tabular-nums; }
.temp.limit { border-color: var(--serious); color: var(--ink); }
.temp.down { border-color: var(--critical); color: var(--ink); }
.temp-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.temp-label { min-width: 84px; }
.spark { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spark-plot { position: relative; line-height: 0; }
.spark-svg { display: block; overflow: visible; touch-action: none; cursor: crosshair; }
.spark-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.spark-cross { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 2 2; }
.spark-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.spark-range { font-size: 12.5px; color: var(--muted); min-width: 150px; font-variant-numeric: tabular-nums; }
.spark-range b { font-weight: 600; color: var(--ink); }
.input-with input { flex: 1; min-width: 0; }
.sim-line { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.raw-note { margin: 4px 0 2px; }

/* маршрут */
.route .card-head { margin-bottom: 10px; }
.route-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.route-option {
  font: inherit;
  color: var(--ink);
  text-align: left;
  background: none;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 13px;
  cursor: pointer;
  display: grid;
  grid-template-columns: 18px 1fr auto;
  gap: 3px 10px;
  align-items: start;
}
.route-option:hover:not(:disabled) { border-color: var(--muted); }
.route-option:disabled { cursor: progress; }
.route-option.on { border-color: var(--accent); background: var(--accent-soft); }
.route-option .dot {
  width: 16px;
  height: 16px;
  margin-top: 2px;
  border: 2px solid var(--muted);
  border-radius: 50%;
}
.route-option.on .dot { border: 5px solid var(--accent); }
.route-option b { font-weight: 600; }
.route-option .desc { grid-column: 2 / 4; color: var(--ink-2); font-size: 12.5px; }
.route-note { margin: 8px 0 0; }

/* маршрут одного модема */
.seg { display: inline-flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.seg-opt {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: none;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 3px 9px;
  display: inline-flex;
  gap: 8px;
  align-items: center;
  cursor: pointer;
}
.seg-opt:hover:not(:disabled) { border-color: var(--muted); }
.seg-opt:disabled { cursor: progress; }
.seg-opt.on { border-color: var(--accent); background: var(--accent-soft); }
.seg-opt b { font-weight: 600; }

/* таблица прокси */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  padding: 6px 10px 6px 0;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
td {
  padding: 8px 10px 8px 0;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  font-variant-numeric: tabular-nums;
}
tbody tr:last-child td { border-bottom: 0; }
td .second { color: var(--muted); font-size: 12px; }
td.name { max-width: 180px; overflow-wrap: anywhere; }
td.name b { font-weight: 600; }
td.row-actions { white-space: nowrap; text-align: right; padding-right: 0; }

.copy {
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--ink);
  background: none;
  border: 0;
  border-bottom: 1px dotted var(--muted);
  padding: 0;
  cursor: copy;
  text-align: left;
  overflow-wrap: anywhere;
}
.copy:hover { color: var(--accent); border-bottom-color: var(--accent); }
.copy.link { max-width: 220px; display: inline-block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }

.meter { height: 4px; border-radius: 2px; background: var(--surface-2); margin-top: 5px; overflow: hidden; min-width: 80px; }
.meter span { display: block; height: 100%; border-radius: 2px; background: var(--accent); }
.meter.full span { background: var(--critical); }

.empty { color: var(--muted); padding: 18px 0 8px; text-align: center; }

details.menu { position: relative; display: inline-block; }
details.menu summary { list-style: none; }
details.menu summary::-webkit-details-marker { display: none; }
.menu-list {
  position: fixed;
  z-index: 6;
  min-width: 230px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .22);
  display: grid;
}
.menu-list button { font: inherit; font-size: 13px; color: var(--ink); text-align: left; background: none; border: 0; border-radius: 5px; padding: 6px 8px; cursor: pointer; }
.menu-list button:hover { background: var(--surface-2); }

/* модемы и журнал */
.split { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 14px; align-items: start; }
.slots { display: grid; gap: 10px; }
.slot { border: 1px solid var(--line); border-radius: 9px; padding: 10px 13px; display: grid; gap: 7px; }
.slot-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.slot-head b { font-weight: 600; }
.slot-head .btns { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.facts { display: flex; flex-wrap: wrap; gap: 3px 16px; color: var(--ink-2); font-size: 13px; font-variant-numeric: tabular-nums; }
.facts b { font-weight: 500; color: var(--ink); }
.paths { display: flex; flex-wrap: wrap; gap: 3px 18px; font-size: 13px; color: var(--ink-2); align-items: center; }
.paths label { display: inline-flex; gap: 6px; align-items: center; }
.job { font-size: 13px; color: var(--ink-2); }
.raw summary { cursor: pointer; font-size: 12.5px; color: var(--muted); }
.raw table { margin-top: 4px; }
.raw td, .raw th { padding: 3px 12px 3px 0; font-size: 12.5px; }
.idle { margin-top: 10px; }
.idle summary { cursor: pointer; color: var(--ink-2); font-size: 13px; }
.idle-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 5px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.idle-row:last-child { border-bottom: 0; }

.log-card { display: flex; flex-direction: column; }
.log { list-style: none; margin: 0; padding: 0; height: 330px; overflow-y: auto; font-size: 13px; }
.log li { display: grid; grid-template-columns: 62px 16px 1fr; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.log time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; line-height: 19px; }
.log .st { align-self: start; margin-top: 1px; }
.log span.msg { overflow-wrap: anywhere; }

.foot { color: var(--muted); font-size: 12px; display: flex; flex-wrap: wrap; gap: 4px 18px; }

/* диалог */
dialog {
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0;
  width: min(480px, calc(100vw - 24px));
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dlg { padding: 18px 20px; display: grid; gap: 12px; }
.dlg h2 { font-size: 16px; }
.dlg p { color: var(--ink-2); }
.field { display: grid; gap: 4px; }
.field > span { font-size: 12.5px; color: var(--ink-2); }
.field input, .field select, .field textarea { width: 100%; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: start; }
.input-with { display: flex; gap: 6px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.check { display: flex; gap: 8px; align-items: center; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.dlg-extra { display: flex; gap: 6px; flex-wrap: wrap; padding-top: 10px; border-top: 1px solid var(--line); }
.form-error { color: var(--critical); font-size: 13px; }
.dlg-section { display: grid; gap: 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.dlg-section b { font-weight: 600; }
.dlg .line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.history { margin: 0; padding-left: 18px; font-size: 12.5px; color: var(--ink-2); display: grid; gap: 3px; max-height: 180px; overflow-y: auto; }
.history b { font-weight: 600; color: var(--ink); }
dialog { max-height: calc(100vh - 24px); }
dialog .dlg { overflow-y: auto; }
dialog:has(.dlg.wide) { width: min(680px, calc(100vw - 24px)); }
.field input.num { width: 90px; }
.sms-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0; max-height: 60vh; overflow: hidden auto; }
.sms-list li { padding: 8px; border-bottom: 1px solid var(--line); border-radius: 6px; }
.sms-list li:last-child { border-bottom: 0; }
.sms-list li.new { background: var(--accent-soft); }
.sms-head { display: flex; gap: 10px; align-items: baseline; font-size: 12.5px; color: var(--muted); }
.sms-head b { color: var(--ink); font-weight: 600; font-size: 13px; }
.sms-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13.5px; margin-top: 2px; }
.badge { font-size: 11px; color: #fff; background: var(--accent); border-radius: 10px; padding: 0 7px; }
table.radio th, table.radio td { padding: 5px 10px 5px 0; font-size: 13px; white-space: nowrap; }
table.radio tbody th { text-align: left; color: var(--ink-2); font-weight: 500; border-bottom: 1px solid var(--line); }
table.radio b { font-weight: 600; }

.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 20; display: grid; gap: 8px; max-width: 380px; }
.toast { background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--good); border-radius: 8px; padding: 8px 12px; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); }
.toast.error { border-left-color: var(--critical); }

@media (max-width: 1400px) {
  .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .sim-row { grid-template-columns: 78px minmax(0, 1fr); }
  .sim-meter { grid-column: 1 / -1; }
  .sim-actions { grid-column: 1 / -1; justify-content: flex-start; }
}

@media (max-width: 1240px) {
  .split { grid-template-columns: minmax(0, 1fr); }
  .log { height: 260px; }
}

/* на узких экранах таблица прокси становится карточками */
@media (max-width: 1080px) {
  table.proxies thead { display: none; }
  table.proxies, table.proxies tbody, table.proxies tr, table.proxies td { display: block; width: 100%; }
  table.proxies tr { border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px; margin-bottom: 10px; }
  table.proxies td { border: 0; padding: 3px 0; display: grid; grid-template-columns: 96px 1fr; gap: 8px; text-align: left; }
  table.proxies td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  td.name { max-width: none; }
  table.proxies td.row-actions { display: flex; gap: 8px; padding-top: 6px; }
  table.proxies td.row-actions::before { display: none; }
  .copy.link { max-width: 100%; }
  .top { flex-wrap: wrap; padding: 8px 14px; gap: 8px 14px; }
  main { padding: 12px 14px 24px; }
}

@media (max-width: 640px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .route-options, .field-row { grid-template-columns: 1fr; }
  .seg-label { flex-basis: 100%; }
  .card-head { flex-direction: column; }
  .head-actions { justify-content: flex-start; }
}
