/* Стили интерфейса. Светлая и тёмная тема переключаются по настройке системы. */

:root {
  --background: #f6f7f9;
  --text: #1b1f24;
  --card: #ffffff;
  --border: #d8dde3;
  --accent: #2563eb;
  --danger: #b91c1c;
  --muted: #667085;
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: #14171a;
    --text: #e6e9ed;
    --card: #1d2125;
    --border: #2e343a;
    --accent: #6ea0ff;
    --danger: #ff8a8a;
    --muted: #98a2b3;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--background);
  color: var(--text);
  font: 15px/1.5 system-ui, sans-serif;
}

main { max-width: 1100px; margin: 0 auto; padding: 16px; }
h1 { font-size: 20px; margin: 8px 0 16px; }
a { color: var(--accent); }

/* ---------- блоки ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin: 0 0 14px;
}

.message {
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  padding: 8px 12px;
  margin: 0 0 12px;
}

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.spread { justify-content: space-between; }
.muted { color: var(--muted); font-size: 13px; }

/* ---------- таблица клиентов ---------- */
/* Колонок много: на узком экране таблица прокручивается внутри карточки, а не вылезает за неё */
.card { overflow-x: auto; }
.card:has(.menu[open]) { overflow: visible; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }

/* ---------- поля и кнопки ---------- */
input, button, textarea { font: inherit; color: inherit; }

input[type="text"], input[type="password"] {
  width: 100%;
  max-width: 280px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--background);
}

button, .button {
  display: inline-block;
  padding: 8px 14px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}

button.secondary, .button.secondary { background: transparent; color: var(--accent); }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }

/* ---------- конфиг, ключ, QR ---------- */
pre, textarea {
  width: 100%;
  padding: 10px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--background);
  font: 12px/1.4 ui-monospace, monospace;
  white-space: pre-wrap;
  word-break: break-all;
}

/* ряд с кнопкой под полем */
pre + .row, textarea + .row { margin-top: 8px; }

.qr svg { max-width: 100%; height: auto; padding: 8px; border-radius: 8px; background: #fff; }

/* ---------- статус клиента: цветные точки и подсветка ---------- */
.dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  vertical-align: middle;
  background: var(--muted);
}
.dot-online  { background: #16a34a; box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2); }
.dot-recent  { background: #d97706; }
.dot-offline { background: #dc2626; }
.dot-never   { background: transparent; border: 2px solid #94a3b8; }
.dot-unknown { background: transparent; border: 2px dashed #94a3b8; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  align-items: center;
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--muted);
}
.legend-sep { width: 1px; height: 16px; background: var(--border); }

/* ↓ скачивание — синий, ↑ отдача — оранжевый; скорость подсвечивается по нагрузке */
.down { color: #2563eb; }
.up { color: #ea580c; }
@media (prefers-color-scheme: dark) { .down { color: #6ea0ff; } .up { color: #fb923c; } }
.lvl-low { color: inherit; }
.lvl-medium { color: #d97706; font-weight: 600; }
.lvl-high { color: #dc2626; font-weight: 700; }

/* ---------- сводка над таблицей ---------- */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 0 0 14px; }
.tile { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; }
.tile-value { display: block; font-size: 24px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.tile-label { color: var(--muted); font-size: 12px; }
.tile-online { color: #16a34a; }
.tile-recent { color: #d97706; }

/* ---------- поиск и фильтры ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 0 0 10px; }
.toolbar input[type="search"] {
  flex: 1 1 240px; max-width: 360px; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--background);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 4px 12px; border: 1px solid var(--border); border-radius: 999px;
  background: transparent; color: inherit; font-size: 13px; cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- таблица клиентов: компактная, с фиксированными колонками ---------- */
table.clients { table-layout: fixed; }
table.clients th, table.clients td {
  padding: 5px 8px; border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  overflow-wrap: anywhere; vertical-align: middle; line-height: 1.3;
}
table.clients th { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
table.clients tbody tr { cursor: pointer; }
table.clients tbody tr:hover { background: rgba(128, 128, 128, 0.08); }
table.clients tbody tr[hidden] { display: none; }
table.clients .right { text-align: right; }
table.clients td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.client-name { font-weight: 600; text-decoration: none; }
.client-name:hover { text-decoration: underline; }
.client-ip { font-size: 12px; }
td.wrap { overflow-wrap: anywhere; }

/* сортируемые заголовки */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sortable::after { content: " " attr(data-arrow); color: var(--accent); }

/* значения: число ярче, единица бледнее; одинаковое выравнивание во всех строках */
.value { font-weight: 700; }
.unit { color: var(--muted); font-size: 12px; font-weight: 400; }
td.num .lines { display: block; }
td.num .empty { display: none; color: var(--muted); }
td.num.is-empty .lines { display: none; }
td.num.is-empty .empty { display: inline; }

/* индикатор рядом со временем активности */
.activity-dot { display: inline-block; width: 8px; height: 8px; margin-right: 5px; border-radius: 50%; background: #16a34a; }
.activity-dot.hidden { display: none; }

/* действия */
.actions { white-space: nowrap; }
.icon-button {
  display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px;
  border-radius: 8px; border: 1px solid transparent; color: var(--accent);
  background: transparent; text-decoration: none; cursor: pointer; font-size: 18px; line-height: 1; list-style: none;
}
.icon-button:hover { background: rgba(128, 128, 128, 0.15); }
details.menu { position: relative; display: inline-block; }
details.menu summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: 30px; z-index: 5; min-width: 170px; text-align: left;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18); padding: 4px;
}
.menu-list a { display: block; padding: 6px 10px; border-radius: 6px; color: var(--text); text-decoration: none; font-size: 14px; }
.menu-list a:hover { background: rgba(128, 128, 128, 0.15); }

/* ---------- телефон и узкие экраны ---------- */
.sort-select {
  display: none; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--background); color: inherit; font: inherit;
}

@media (max-width: 720px) {
  main { padding: 10px; }
  h1 { font-size: 18px; }
  .card { padding: 10px; }

  /* формы: поля на всю ширину */
  input[type="text"], input[type="password"], .toolbar input[type="search"] { max-width: none; width: 100%; flex: 1 1 100%; }
  .row { gap: 6px; }

  /* поиск, сортировка и фильтры в столбик; фильтры прокручиваются по горизонтали */
  .sort-select { display: block; width: 100%; }
  .chips { flex-wrap: nowrap; overflow-x: auto; width: 100%; padding-bottom: 2px; -webkit-overflow-scrolling: touch; }
  .chip { white-space: nowrap; flex: 0 0 auto; }

  /* сводка 2×2 */
  .summary { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .tile-value { font-size: 20px; }

  /* таблица клиентов -> карточки */
  table.clients, table.clients tbody { display: block; }
  table.clients thead, table.clients colgroup { display: none; }
  table.clients tbody tr {
    display: grid;
    grid-template-columns: 22px minmax(0, 1fr) minmax(0, 1fr) auto;
    grid-template-areas:
      "dot . . ."
      ". user user user"
      ". act act act"
      ". traffic speed speed"
      ". addr addr addr";
    column-gap: 8px; row-gap: 3px;
    padding: 10px 2px; border-bottom: 1px solid rgba(128, 128, 128, 0.2);
  }
  table.clients tbody tr[hidden] { display: none; }
  table.clients td { display: block; min-width: 0; padding: 0; border: 0; text-align: left; overflow-wrap: anywhere; }
  table.clients td.right { text-align: left; }
  table.clients td.c-dot { grid-area: 1 / 1; align-self: center; }
  table.clients td.c-name { grid-area: 1 / 2 / 2 / 4; }
  table.clients td.c-user { grid-area: user; font-size: 13px; }
  table.clients td.no-display-name { display: none; }
  table.clients td.c-activity { grid-area: act; font-size: 13px; }
  table.clients td.c-traffic { grid-area: traffic; }
  table.clients td.c-speed { grid-area: speed; }
  table.clients td.c-address { grid-area: addr; font-size: 12px; color: var(--muted); }
  table.clients td.c-actions { grid-area: 1 / 4; align-self: start; }

  /* подписи значений вместо заголовков столбцов */
  table.clients td[data-label]:not([data-label=""])::before {
    content: attr(data-label) ": "; color: var(--muted); font-size: 12px;
  }
  table.clients td.num::before { display: block; }
  table.clients td.num.is-empty .empty { display: inline; }

  /* страница клиента */
  pre, textarea { font-size: 11px; }
  .qr svg { width: 100%; max-width: 320px; }
  .menu-list { right: 0; }
}

/* ---------- вкладки протоколов ---------- */
.tabs { display: flex; gap: 6px; margin: 0 0 14px; overflow-x: auto; }
.tab {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 999px; color: var(--text);
  text-decoration: none; white-space: nowrap; font-size: 14px;
}
.tab:hover { background: rgba(128, 128, 128, 0.12); }
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
