/* ------------------------------------------------------------------
   Traffic · AS Monitor — тёмная минималистичная тема (адаптивная)
------------------------------------------------------------------ */
:root {
  --bg:        #0a0b0d;
  --bg-soft:   #0e1013;
  --panel:     #111417;
  --panel-2:   #15181d;
  --border:    #1e2228;
  --border-2:  #262b33;
  --text:      #e7eaf0;
  --muted:     #858d99;
  --muted-2:   #5f6672;
  --accent:    #5eead4;
  --accent-2:  #38bdf8;
  --warn:      #fbbf24;
  --danger:    #f87171;
  --radius:    14px;
  --radius-sm: 9px;
  --shadow:    0 1px 0 rgba(255,255,255,.02), 0 10px 30px rgba(0,0,0,.35);
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Mono", Consolas, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

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

html { -webkit-text-size-adjust: 100%; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--text);
  font-family: var(--sans); font-size: 14px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(900px 500px at 12% -10%, rgba(94,234,212,.06), transparent 60%),
    radial-gradient(800px 500px at 92% 0%, rgba(56,189,248,.05), transparent 55%);
}

h2 { font-size: 14px; font-weight: 600; margin: 0; letter-spacing: .01em; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 12px; margin: 2px 0 0; }

/* ---------------- topbar ---------------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 22px; flex-wrap: wrap;
  padding: 14px 24px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: rgba(10,11,13,.82);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 210px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px; flex: none;
  background: linear-gradient(140deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 0 1px rgba(94,234,212,.35), 0 0 22px rgba(94,234,212,.22);
}
.brand-title { font-weight: 650; letter-spacing: .02em; }
.brand-sub { font-size: 11px; color: var(--muted); max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.controls { display: flex; align-items: flex-end; gap: 18px; flex-wrap: wrap; margin-left: auto; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }

select, input[type="search"] {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border-2); border-radius: var(--radius-sm);
  padding: 7px 10px; font-family: inherit; font-size: 13px; min-width: 170px; outline: none;
  max-width: 100%;
}
select:focus, input[type="search"]:focus { border-color: var(--accent); }

.segmented {
  display: inline-flex; background: var(--panel-2); border: 1px solid var(--border-2);
  border-radius: var(--radius-sm); padding: 2px; max-width: 100%;
}
.segmented button {
  background: transparent; color: var(--muted); border: 0; cursor: pointer;
  padding: 6px 11px; border-radius: 7px; font: inherit; font-size: 12.5px; white-space: nowrap;
  transition: background .15s, color .15s;
}
.segmented button:hover { color: var(--text); }
.segmented button.active { background: #1d232b; color: var(--text); box-shadow: inset 0 0 0 1px var(--border-2); }
.segmented.sm button { padding: 5px 10px; font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--panel-2); color: var(--text); text-decoration: none;
  border: 1px solid var(--border-2); border-radius: var(--radius-sm);
  padding: 8px 13px; font-size: 13px; cursor: pointer; white-space: nowrap;
  transition: border-color .15s, background .15s, transform .1s;
}
.btn:hover { border-color: #38404a; background: #191d23; }
.btn:active { transform: translateY(1px); }
.btn.ghost { color: var(--muted); }
.btn[disabled] { opacity: .5; cursor: progress; }

.status { display: flex; align-items: center; gap: 10px; min-width: 190px; }
.status-text { display: flex; flex-direction: column; line-height: 1.25; font-size: 12px; }
.live { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 0 rgba(94,234,212,.6); animation: pulse 2.4s infinite; flex: none; }
.live.stale { background: var(--warn); animation: none; }
.live.error { background: var(--danger); animation: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(94,234,212,.45); }
  70% { box-shadow: 0 0 0 9px rgba(94,234,212,0); }
  100% { box-shadow: 0 0 0 0 rgba(94,234,212,0); }
}

/* ---------------- layout ---------------- */
main {
  position: relative; z-index: 1; max-width: 1440px; margin: 0 auto;
  padding: 20px 24px calc(48px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}
.card {
  background: linear-gradient(180deg, var(--panel), var(--bg-soft));
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  min-width: 0;
}
.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; padding: 15px 18px; border-bottom: 1px solid var(--border);
}
.card-head > div { min-width: 0; }
.card-head-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 12px; }
.kpi { padding: 14px 16px; min-height: 104px; display: flex; flex-direction: column; justify-content: space-between; }
.kpi-label { font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.kpi-value { font-family: var(--mono); font-size: 24px; font-weight: 600; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-value.accent { color: var(--accent); }
.kpi-value.warn { color: var(--warn); }
.kpi-sub { font-size: 11.5px; color: var(--muted-2); }
.kpi.stale { border-color: rgba(251,191,36,.32); }

/* ---------------- chart ---------------- */
.chart-card .chart { position: relative; padding: 8px 8px 0; height: 300px; }
#chartSvg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart-foot { display: flex; align-items: center; gap: 14px 18px; flex-wrap: wrap; padding: 10px 18px 14px; font-size: 12px; color: var(--muted); }
.legend-line { display: inline-flex; align-items: center; gap: 7px; }
.swatch { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.swatch.line { background: var(--accent); }
.swatch.bar { background: rgba(56,189,248,.6); height: 9px; width: 8px; border-radius: 2px; }
.swatch.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--warn); }
.swatch.stale { width: 14px; height: 9px; border-radius: 2px; background: rgba(251,191,36,.18); border: 1px solid rgba(251,191,36,.35); }

.chart-tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: rgba(12,14,17,.96); border: 1px solid var(--border-2);
  border-radius: 10px; padding: 9px 11px; font-size: 12px; min-width: 168px;
  box-shadow: 0 12px 28px rgba(0,0,0,.5); transform: translate(-50%, -108%); white-space: nowrap;
}
.chart-tooltip .tt-time { color: var(--muted); font-size: 11px; margin-bottom: 5px; }
.chart-tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; }
.chart-tooltip .tt-row b { font-family: var(--mono); font-weight: 600; font-variant-numeric: tabular-nums; }
.chart-tooltip .tt-delta { color: var(--accent-2); }
.chart-tooltip .tt-upd { color: var(--warn); }

/* ---------------- map ---------------- */
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); gap: 16px; }
.map-wrap { position: relative; height: 380px; border-bottom-left-radius: var(--radius); border-bottom-right-radius: var(--radius); overflow: hidden; }
#map { position: absolute; inset: 0; background: #0b0d10; }
.map-fallback { position: absolute; inset: 0; }
.map-fallback canvas { width: 100%; height: 100%; display: block; }
.map-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center; padding: 24px 18px; color: var(--muted);
  background: repeating-linear-gradient(45deg, #0c0e11, #0c0e11 12px, #0e1114 12px, #0e1114 24px);
}
.map-placeholder .ph-icon { font-size: 30px; color: var(--accent-2); opacity: .8; }
.map-placeholder .ph-title { color: var(--text); font-weight: 600; }
.map-placeholder .ph-text { font-size: 12px; max-width: 460px; line-height: 1.6; }
.map-placeholder code { background: var(--panel-2); border: 1px solid var(--border-2); border-radius: 5px; padding: 1px 5px; font-family: var(--mono); font-size: 11.5px; word-break: break-all; }

.leaflet-container { background: #0b0d10; font-family: var(--sans); }
.leaflet-control-attribution { background: rgba(10,11,13,.7) !important; color: var(--muted-2) !important; font-size: 10px; }
.leaflet-control-attribution a { color: var(--muted) !important; }
.leaflet-bar a { background: var(--panel-2) !important; color: var(--text) !important; border-color: var(--border-2) !important; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: #12151a; color: var(--text); border: 1px solid var(--border-2); }
.leaflet-popup-content { margin: 10px 12px; font-size: 12.5px; }
.leaflet-popup-content b { font-family: var(--mono); }
.leaflet-tooltip.as-label { background: rgba(12,14,17,.86); border: 1px solid var(--border-2); color: var(--text); font-size: 10.5px; padding: 2px 6px; border-radius: 6px; box-shadow: none; }
.leaflet-tooltip.as-label::before { display: none; }

.legend { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 16px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 20px;
  padding: 4px 10px; font-size: 11.5px; color: var(--muted); user-select: none; max-width: 100%;
}
.legend-item:hover { color: var(--text); border-color: var(--border-2); }
.legend-item.off { opacity: .38; }
.legend-item i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }
.legend-item.hint { cursor: default; color: var(--muted-2); background: transparent; border-style: dashed; }
.legend-item.hint:hover { color: var(--muted-2); border-color: var(--border); }
.legend-item .dot-dashed { width: 11px; height: 11px; border-radius: 50%; border: 2px dashed var(--muted); }

/* ---------------- AS list ---------------- */
.as-list { list-style: none; margin: 0; padding: 6px 8px 12px; max-height: 430px; overflow: auto; }
.as-list li { padding: 9px 10px; border-radius: var(--radius-sm); }
.as-list li:hover { background: var(--panel-2); }
.as-row { display: flex; align-items: baseline; gap: 9px; min-width: 0; }
.as-row i { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.as-name { font-weight: 550; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.as-num { font-family: var(--mono); font-size: 11px; color: var(--muted-2); white-space: nowrap; }
.as-meta { display: flex; justify-content: space-between; gap: 10px; font-size: 11.5px; color: var(--muted); margin-top: 5px; }
.as-bar { height: 4px; border-radius: 3px; background: var(--panel-2); margin-top: 7px; overflow: hidden; }
.as-bar > span { display: block; height: 100%; border-radius: 3px; }

/* ---------------- matrix (AS × время) ---------------- */
.matrix { position: relative; padding: 10px 14px 14px; }
.matrix-rows { display: flex; flex-direction: column; gap: 3px; }
.matrix-row {
  display: grid; grid-template-columns: 215px minmax(0, 1fr) 110px;
  align-items: center; gap: 10px; min-width: 0;
}
.matrix-label { display: flex; align-items: center; gap: 7px; min-width: 0; font-size: 11.5px; }
.matrix-label i { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.matrix-label .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.matrix-label .as-num { font-family: var(--mono); font-size: 10.5px; color: var(--muted-2); }
.matrix-strip { width: 100%; height: 16px; display: block; border-radius: 3px; }
.matrix-total { font-size: 10.5px; color: var(--muted); white-space: nowrap; text-align: right; }
.matrix-axis { height: 16px; }
.matrix-track { position: relative; height: 100%; }
.matrix-hour { position: absolute; top: 0; transform: translateX(-50%); font-size: 10px; color: var(--muted-2); white-space: nowrap; }
.scale { display: inline-flex; align-items: center; gap: 7px; font-size: 11px; }
.scale-bar { width: 90px; height: 8px; border-radius: 4px; display: inline-block;
  background: linear-gradient(90deg, rgba(94,234,212,.08), rgba(94,234,212,.85)); }

/* ---------------- table ---------------- */
.table-card .table-wrap { overflow: auto; max-height: 460px; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  position: sticky; top: 0; z-index: 2; background: #0f1215; color: var(--muted);
  font-size: 11px; font-weight: 500; text-transform: uppercase; letter-spacing: .05em;
  text-align: left; padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap;
}
thead th:hover { color: var(--text); }
thead th.num, td.num { text-align: right; }
tbody td { padding: 9px 16px; border-bottom: 1px solid rgba(30,34,40,.7); vertical-align: middle; }
tbody tr:hover { background: rgba(21,24,29,.65); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.inactive td { opacity: .62; }
.ip-cell { font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.state-badge {
  display: inline-block; margin-left: 8px; padding: 1px 7px; border-radius: 6px;
  font-family: var(--sans); font-size: 10.5px; letter-spacing: .02em;
  border: 1px solid transparent; white-space: nowrap; vertical-align: middle;
}
.state-badge.on { color: var(--accent); background: rgba(94,234,212,.1); border-color: rgba(94,234,212,.3); }
.state-badge.off { color: var(--warn); background: rgba(251,191,36,.08); border-color: rgba(251,191,36,.3); }
.now-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); margin-right: 7px; box-shadow: 0 0 7px rgba(94,234,212,.5); vertical-align: middle; }
.load-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--border-2); margin-right: 7px; vertical-align: middle; }
.as-cell { display: flex; align-items: center; gap: 8px; min-width: 0; }
.as-cell i { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.as-cell span.nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px; }
.badge { display: inline-block; padding: 2px 7px; border-radius: 6px; font-size: 11px; font-family: var(--mono); background: var(--panel-2); border: 1px solid var(--border-2); color: var(--muted); white-space: nowrap; }
.presence { display: flex; gap: 2px; align-items: flex-end; height: 22px; min-width: 90px; }
.presence i { width: 3px; height: 100%; background: var(--border-2); border-radius: 2px; display: block; flex: none; }
.presence i.on { background: var(--accent); box-shadow: 0 0 6px rgba(94,234,212,.35); }
.empty { padding: 26px 18px; color: var(--muted); font-size: 13px; text-align: center; }

/* ---------------- footer ---------------- */
.foot { display: flex; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap; font-size: 11.5px; color: var(--muted-2); padding: 4px 2px 0; }
.switch { display: inline-flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); cursor: pointer; }
.switch input { accent-color: var(--accent); }

/* ---------------- планшеты ---------------- */
@media (max-width: 1250px) { .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 1150px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }

/* ---------------- телефоны ---------------- */
@media (max-width: 760px) {
  body { font-size: 13.5px; }
  .topbar {
    gap: 12px; padding: 10px 12px; align-items: stretch;
  }
  .brand { min-width: 0; flex: 1 1 auto; }
  .brand-sub { max-width: 100%; }
  .status { order: 2; min-width: 0; }
  .controls {
    order: 3; margin-left: 0; width: 100%; gap: 10px;
    display: grid; grid-template-columns: 1fr; align-items: stretch;
  }
  .field { width: 100%; }
  .field > label { font-size: 10px; }
  select { width: 100%; min-width: 0; }
  .row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .row .btn { padding: 9px 10px; }
  /* переключатели окон и режимов прокручиваются по горизонтали */
  .segmented { width: 100%; overflow-x: auto; scrollbar-width: none; }
  .segmented::-webkit-scrollbar { display: none; }
  .segmented button { flex: 1 0 auto; }

  main { padding: 12px 12px calc(32px + env(safe-area-inset-bottom)); gap: 12px; }
  .card-head { padding: 12px 14px; gap: 10px; }
  .card-head h2 { font-size: 13px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { min-height: 88px; padding: 11px 12px; }
  .kpi-value { font-size: 19px; }
  .kpi-label { font-size: 10px; letter-spacing: .05em; }
  .kpi-sub { font-size: 10.5px; }

  .chart-card .chart { height: 230px; padding: 6px 4px 0; }
  .chart-foot { padding: 8px 12px 12px; gap: 10px 14px; font-size: 11px; }
  .chart-tooltip { min-width: 150px; font-size: 11.5px; padding: 8px 10px; }

  .map-wrap { height: 260px; }
  .map-placeholder { padding: 18px 14px; }
  .map-placeholder .ph-text { font-size: 11.5px; }
  .legend { padding: 10px 12px; }
  .as-list { max-height: none; }

  /* матрица AS: подпись сверху, полоса во всю ширину */
  .matrix { padding: 8px 12px 12px; }
  .matrix-row { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "label total" "strip strip"; gap: 2px 10px; }
  .matrix-label { grid-area: label; }
  .matrix-strip { grid-area: strip; height: 14px; }
  .matrix-total { grid-area: total; }
  .matrix-axis { display: none; }

  /* таблица IP превращается в карточки */
  .table-card .table-wrap { max-height: none; padding: 8px 10px 12px; overflow: visible; }
  #ipTable thead { display: none; }
  #ipTable, #ipTable tbody { display: block; width: 100%; }
  #ipTable tr {
    display: block; border: 1px solid var(--border); border-radius: 12px;
    background: rgba(21,24,29,.55); padding: 10px 12px; margin-bottom: 8px;
  }
  #ipTable tr:last-child { margin-bottom: 0; }
  #ipTable td {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 3px 0; border: 0; font-size: 12.5px; text-align: right;
  }
  #ipTable td::before {
    content: attr(data-label); flex: none; color: var(--muted); font-size: 10px;
    text-transform: uppercase; letter-spacing: .05em; text-align: left;
  }

  /* IP-ячейка: label слева, IP+бейдж — справа вплотную */
  #ipTable td.ip-cell {
    justify-content: space-between;
    gap: 8px;
  }
  #ipTable td.ip-cell .ip-right {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
  }

  #ipTable td.num { text-align: right; }
  #ipTable td.presence-cell { display: block; text-align: left; margin-top: 8px; }
  #ipTable td.presence-cell::before { display: block; margin-bottom: 6px; }
  .presence { min-width: 0; width: 100%; height: 18px; }
  .presence i { flex: 1 1 auto; width: auto; min-width: 1px; }
  .as-cell span.nm { max-width: none; }
  .foot { font-size: 11px; }
}

/* ---------------- очень узкие экраны ---------------- */
@media (max-width: 400px) {
  .brand-sub { display: none; }
  .kpi-value { font-size: 18px; }
  .kpi-sub { font-size: 10px; }
}
@media (max-width: 330px) {
  .kpis { grid-template-columns: 1fr; }
  .kpi { min-height: 0; }
  .row { grid-template-columns: 1fr; }
}