:root {
  color-scheme: light dark;
  --bg: #0f1115;
  --panel: #171a21;
  --border: #2a2f3a;
  --text: #e6e8eb;
  --muted: #8b93a1;
  --accent: #4f8cff;
  --danger: #ff5c5c;
  --ok: #3ecf8e;
  --warn: #ffc400;
  --input-bg: #0d0f13;
  --teal: #2dd4cf;
  font-family: -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f5f6f8;
    --panel: #ffffff;
    --border: #dde1e7;
    --text: #1a1d23;
    --muted: #5b6472;
    --accent: #2f6fed;
    --danger: #d9362e;
    --ok: #1a8f57;
    --warn: #a56800;
    --input-bg: #ffffff;
    --teal: #0f8b8f;
  }
}

:root[data-theme="light"] {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #dde1e7;
  --text: #1a1d23;
  --muted: #5b6472;
  --accent: #2f6fed;
  --danger: #d9362e;
  --ok: #1a8f57;
  --warn: #a56800;
  --input-bg: #ffffff;
  --teal: #0f8b8f;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #2a2f3a;
  --text: #e6e8eb;
  --muted: #8b93a1;
  --accent: #4f8cff;
  --danger: #ff5c5c;
  --ok: #3ecf8e;
  --warn: #ffc400;
  --input-bg: #0d0f13;
  --teal: #2dd4cf;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header a { color: var(--text); text-decoration: none; margin-right: 16px; }
header a:hover { color: var(--accent); }

main { padding: 24px; max-width: 1240px; margin: 0 auto; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 16px;
}

input, button, select {
  font: inherit;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
}

button {
  background: var(--accent);
  border: none;
  cursor: pointer;
  color: #fff;
}
button:hover { opacity: .9; }
button.secondary { background: transparent; border: 1px solid var(--border); color: var(--text); }
button.danger { background: var(--danger); }

.badge { padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.badge.active { background: rgba(62,207,142,.15); color: var(--ok); }
.badge.pending { background: rgba(255,196,0,.15); color: var(--warn); }
.badge.revoked { background: rgba(255,92,92,.15); color: var(--danger); }

.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); margin: 8px 0; }
.ok-msg { color: var(--ok); margin: 8px 0; }

input.comment-input { width: 100%; }

/* --- Таблица роутеров: компактная, всё в одну строку без горизонтальной
   прокрутки. table-layout:fixed + узкие фиксированные колонки, остальное
   (комментарий) забирает свободное место. --- */
.routers-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 12px; }
.routers-table th, .routers-table td {
  text-align: left;
  padding: 8px 8px;
  border-bottom: 3px solid var(--border);
  vertical-align: top;
  overflow-wrap: break-word;
}
.routers-table thead th { border-bottom: 1px solid var(--border); }
.routers-table th { color: var(--muted); font-weight: 500; font-size: 11px; }
.routers-table th:nth-child(1), .routers-table td:nth-child(1) { width: 34%; }
.routers-table th:nth-child(2), .routers-table td:nth-child(2) { width: 15%; }
.routers-table th:nth-child(3), .routers-table td:nth-child(3) { width: 12%; }
.routers-table th:nth-child(4), .routers-table td:nth-child(4) { width: 18%; }
.routers-table th:nth-child(5), .routers-table td:nth-child(5) { width: auto; }

.routers-table input, .routers-table select, .routers-table button {
  font-size: 11px;
  padding: 3px 5px;
  width: 100%;
}
.routers-table button { width: auto; }

/* Индикатор состояния (онлайн/pending/офлайн) — зелёный/жёлтый/красный */
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.dot.green { background: var(--ok); }
.dot.yellow { background: var(--warn); }
.dot.red { background: var(--danger); }

/* Устройство: статус + модель + MAC + аптайм, один карандашик на весь блок */
.device-cell { line-height: 1.3; }
.device-head { display: flex; align-items: center; gap: 4px; }
.device-model { font-weight: 600; }
.device-mac { font-family: ui-monospace, Consolas, monospace; color: var(--muted); font-size: 10px; }
.device-head .pencil { margin-left: auto; }

/* Комментарий — своя строка, свой карандашик, переключается независимо
   от общего конфига ниже (см. startCommentEdit). */
.comment-row { margin-top: 4px; }
.comment-row .comment-edit-input { display: none; }
.comment-row.editing .field-view { display: none; }
.comment-row.editing .comment-edit-input { display: inline-block; }

/* Сеть: LAN/WAN/Wi-Fi/прошивка — коротко подписанные строки друг под другом.
   field-input скрыт, пока не нажат карандашик (см. .device-cell.editing).
   resize позволяет руками расширить блок, если подписанные значения не
   помещаются в узкую колонку. */
.net-cell {
  display: grid;
  gap: 2px;
  margin-top: 4px;
  resize: horizontal;
  overflow: auto;
  min-width: 100%;
  max-width: 640px;
}
/* Каждое поле — 2 колонки данных: слева то, что в базе (baseline), справа
   цветом морской волны — то, что реально прислал роутер (reported_*), если
   отличается. Карандашик у шапки устройства делает левую колонку
   редактируемой сразу для всех полей; сохранение/отмена — общей дискетой/
   крестиком внизу страницы (см. #editBar), не тут. */
.field-row { display: grid; grid-template-columns: 34px 1fr 1fr; gap: 4px; align-items: center; }
.field-row label { color: var(--muted); font-size: 9px; white-space: nowrap; }
.field-row input, .field-row select { min-width: 0; width: 100%; }
.field-base { min-width: 0; }
.field-input { display: none; }
.device-cell.editing .field-text { display: none; }
.device-cell.editing .field-input { display: inline-block; }
.device-cell.editing .field-new-col { visibility: hidden; }
.field-new-col { min-width: 0; font-size: 10px; overflow-wrap: anywhere; }
.field-new {
  color: var(--teal);
  cursor: pointer;
  border-bottom: 1px dashed var(--teal);
}
.field-new:hover { opacity: .8; }
/* Пока конфиг устройства открыт на редактирование — рамка-подсказка,
   т.к. карандашик больше не превращается в 💾 (сохранение теперь общим
   баром внизу страницы, см. #editBar). */
.device-cell.editing { outline: 1px dashed var(--accent); border-radius: 6px; padding: 4px; margin: -4px; }

/* Подсветка всей строки роутера по живому статусу — зелёный насыщеннее */
tr.row-online { background: rgba(62,207,142,.28); }
tr.row-offline { background: rgba(255,92,92,.12); }
tr.row-pending { background: rgba(255,196,0,.12); }

/* Несовпадение baseline vs то, что прислал сам роутер */
.mismatch-banner {
  margin-top: 4px;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(255,196,0,.15);
  border: 1px solid var(--warn);
  font-size: 10px;
}
.mismatch-banner button { margin-top: 3px; font-size: 10px; padding: 2px 6px; width: auto; }

/* Порты: старый сервер и rt.v2okno.shop друг под другом, разделены чертой */
.port-links-col { display: flex; flex-direction: column; gap: 6px; }
.port-divider { width: 100%; border: none; border-top: 1px solid var(--border); margin: 0; }
.port-group { display: flex; flex-direction: column; gap: 2px; }
.port-btn-row { display: flex; align-items: center; gap: 2px; }
.port-btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 10px;
  white-space: nowrap;
}
.port-btn:hover { opacity: .9; }
.copy-btn { padding: 2px 4px; font-size: 10px; line-height: 1; width: auto; }

.pencil {
  background: transparent;
  border: none;
  cursor: pointer;
  width: auto;
  padding: 1px 3px;
  font-size: 11px;
  line-height: 1;
}
.pencil:hover { opacity: .7; }

/* DPI-обход: Zapret / Zapret2 / ByeDPI — чекбокс + значение, компактно.
   resize — так же, как у .net-cell, на случай длинных значений стратегий. */
.dpi-cell { display: grid; gap: 3px; resize: horizontal; overflow: auto; min-width: 100%; max-width: 480px; }
.dpi-row { display: flex; align-items: center; gap: 3px; }
.dpi-row label { display: flex; align-items: center; gap: 2px; font-size: 10px; white-space: nowrap; flex: none; }
.dpi-value { flex: 1; min-width: 0; }
.dpi-value:disabled { opacity: .45; }

/* Подписка / TG */
.sub-cell { display: grid; gap: 3px; }
.checkbox-line { display: flex; align-items: center; gap: 3px; font-size: 10px; }
.checkbox-line input { width: auto; }

/* Управление: бэкап/прошивка/действия — кнопки друг под другом */
.manage-cell { display: grid; gap: 3px; }
.manage-cell button { display: block; }

/* Переключатель темы */
.theme-toggle {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  width: auto;
}
.theme-toggle-corner { position: fixed; top: 16px; right: 16px; }

/* Общий бар сохранения/отмены редактирования конфига устройства — внизу
   страницы, зафиксирован, появляется только пока открыт карандашик у
   какого-то одного роутера (см. toggleDeviceEdit/saveEditBar/cancelEditBar). */
.edit-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: center;
  padding: 10px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0,0,0,.15);
}
.edit-bar button { width: auto; }
