/* ==========================================================================
   Reset & base tokens
   ========================================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* Hue-tinted neutrals (cool slate) */
  --hue: 220;
}

html[data-theme="dark"],
html:not([data-theme]) {
  color-scheme: dark;
  --bg: hsl(var(--hue) 14% 6%);
  --bg-elev-1: hsl(var(--hue) 14% 9%);
  --bg-elev-2: hsl(var(--hue) 14% 12%);
  --bg-soft: hsl(var(--hue) 14% 14%);
  --border: hsl(var(--hue) 14% 18% / 0.9);
  --border-strong: hsl(var(--hue) 14% 26%);
  --fg: hsl(var(--hue) 16% 96%);
  --fg-muted: hsl(var(--hue) 10% 70%);
  --fg-dim: hsl(var(--hue) 10% 56%);
  --accent: hsl(196 100% 60%);
  --accent-fg: hsl(196 100% 14%);
  --ok: hsl(150 70% 55%);
  --warn: hsl(38 100% 62%);
  --err: hsl(0 80% 65%);
  --rx: hsl(196 90% 60%);
  --tx: hsl(280 80% 70%);
  --shadow-1: 0 1px 0 hsl(var(--hue) 14% 100% / 0.04) inset,
    0 0 0 1px hsl(var(--hue) 14% 100% / 0.04),
    0 8px 24px hsl(var(--hue) 30% 2% / 0.5);
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: hsl(var(--hue) 22% 97%);
  --bg-elev-1: hsl(0 0% 100%);
  --bg-elev-2: hsl(var(--hue) 22% 99%);
  --bg-soft: hsl(var(--hue) 22% 95%);
  --border: hsl(var(--hue) 14% 86%);
  --border-strong: hsl(var(--hue) 14% 76%);
  --fg: hsl(var(--hue) 24% 12%);
  --fg-muted: hsl(var(--hue) 14% 36%);
  --fg-dim: hsl(var(--hue) 10% 50%);
  --accent: hsl(208 90% 44%);
  --accent-fg: hsl(0 0% 100%);
  --ok: hsl(150 60% 36%);
  --warn: hsl(28 90% 42%);
  --err: hsl(0 70% 46%);
  --rx: hsl(208 90% 42%);
  --tx: hsl(280 70% 50%);
  --shadow-1: 0 1px 0 hsl(0 0% 100% / 0.6) inset,
    0 0 0 1px hsl(var(--hue) 14% 0% / 0.04),
    0 8px 18px hsl(var(--hue) 30% 30% / 0.08);
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  background: radial-gradient(
      1200px 600px at 80% -10%,
      hsl(var(--hue) 60% 30% / 0.18),
      transparent 60%
    ),
    var(--bg);
  color: var(--fg);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.01em;
}

p {
  margin: 0;
}

button {
  font: inherit;
}

select,
button {
  color: inherit;
}

/* ==========================================================================
   Utilities
   ========================================================================== */
.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 8px 12px;
  background: var(--accent);
  color: var(--accent-fg);
  border-radius: 8px;
  text-decoration: none;
  transform: translateY(-200%);
  transition: transform 160ms ease;
  z-index: 100;
}
.skip-link:focus-visible {
  transform: translateY(0);
}

:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-radius: 8px;
}

[data-num],
.kpi-value,
.iface-stat-value,
.u-tooltip-value {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

/* ==========================================================================
   Header
   ========================================================================== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  padding-top: max(14px, env(safe-area-inset-top));
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  background: hsl(var(--hue) 14% 6% / 0.6);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}

html[data-theme="light"] .app-header {
  background: hsl(0 0% 100% / 0.7);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: conic-gradient(
    from 200deg,
    hsl(196 100% 60%),
    hsl(280 80% 65%),
    hsl(150 70% 55%),
    hsl(196 100% 60%)
  );
  box-shadow: var(--shadow-1);
  flex: none;
}

.brand-text h1 {
  font-size: 16px;
  line-height: 1.2;
}

.brand-sub {
  color: var(--fg-muted);
  font-size: 12px;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 36ch;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 8px;
  min-height: 36px;
  box-shadow: var(--shadow-1);
}

.field-label {
  color: var(--fg-muted);
  font-size: 12px;
  letter-spacing: 0.02em;
}

.field select {
  appearance: none;
  background: transparent;
  border: 0;
  padding: 4px 24px 4px 4px;
  background-image: linear-gradient(45deg, transparent 50%, var(--fg-muted) 50%),
    linear-gradient(135deg, var(--fg-muted) 50%, transparent 50%);
  background-position: right 8px top 50%, right 4px top 50%;
  background-size: 4px 4px, 4px 4px;
  background-repeat: no-repeat;
  font-size: 13px;
  min-height: 28px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--fg);
}
.field select:focus-visible {
  box-shadow: 0 0 0 2px var(--accent);
  border-radius: 6px;
}
.field-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-left: 1px solid var(--border);
  margin-left: 4px;
  padding-left: 6px;
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
  border-radius: 0 6px 6px 0;
  font-size: 14px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.field-action:hover {
  color: var(--fg);
  background: var(--bg-soft);
}
.field-host {
  padding-right: 4px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--fg);
  cursor: pointer;
  box-shadow: var(--shadow-1);
  transition: background-color 120ms ease, border-color 120ms ease,
    transform 80ms ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover {
  background: var(--bg-soft);
  border-color: var(--border-strong);
}
.btn:active {
  transform: translateY(1px);
}
.btn[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}
.btn-primary:hover {
  filter: brightness(1.08);
}
.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-ghost:hover {
  background: var(--bg-soft);
}
.btn-icon-only {
  width: 36px;
  padding: 0;
  justify-content: center;
}
.btn-label {
  font-size: 13px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--fg-muted);
  font-size: 12px;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--fg-dim);
  box-shadow: 0 0 0 0 hsl(var(--hue) 0% 100% / 0);
  transition: background-color 200ms ease, box-shadow 600ms ease;
}
.status-dot[data-state="ok"] {
  background: var(--ok);
  box-shadow: 0 0 0 4px hsl(150 70% 55% / 0.15);
}
.status-dot[data-state="loading"] {
  background: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}
.status-dot[data-state="error"] {
  background: var(--err);
  box-shadow: 0 0 0 4px hsl(0 80% 65% / 0.15);
}
.status-dot[data-state="paused"] {
  background: var(--warn);
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.3);
    opacity: 0.6;
  }
}

@media (prefers-reduced-motion: reduce) {
  .status-dot[data-state="loading"] {
    animation: none;
  }
  .skip-link {
    transition: none;
  }
}

/* ==========================================================================
   Main layout
   ========================================================================== */
.app-main {
  max-width: 1600px;
  margin: 0 auto;
  padding: 24px;
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  display: grid;
  gap: 24px;
}

.kpis {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.kpi {
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: var(--shadow-1);
  display: grid;
  gap: 6px;
  min-width: 0;
}
.kpi h3 {
  font-size: 12px;
  font-weight: 500;
  color: var(--fg-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.kpi-value {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  display: flex;
  align-items: baseline;
  gap: 4px;
  line-height: 1.1;
}
.kpi-value [data-kpi-value] {
  text-wrap: nowrap;
}
.kpi-unit {
  font-size: 14px;
  color: var(--fg-muted);
  font-weight: 500;
}
.kpi-foot {
  color: var(--fg-muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}

/* Panels */
.panel {
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow-1);
}
.panel-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.panel-header h2 {
  font-size: 15px;
}
.panel-sub {
  color: var(--fg-muted);
  font-size: 12px;
}
.panel-body {
  display: grid;
  gap: 12px;
}

.card {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: grid;
  gap: 8px;
  min-width: 0;
}
.card-wide {
  grid-column: 1 / -1;
}
.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}
.card-header h3 {
  font-size: 13px;
  font-weight: 600;
}
.card-sub {
  color: var(--fg-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

/* ==========================================================================
   网卡分组
   ========================================================================== */
.iface-group {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elev-2);
  margin-top: 12px;
  overflow: hidden;
}
.iface-group:first-child {
  margin-top: 0;
}
.iface-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 8px 8px 4px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(
    180deg,
    hsl(var(--hue) 14% 100% / 0.02),
    transparent 80%
  );
}
.iface-group-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  min-width: 0;
  background: transparent;
  border: 0;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  color: inherit;
  min-height: 36px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.iface-group-toggle:hover {
  background: var(--bg-soft);
}
.iface-group-caret {
  display: inline-block;
  font-size: 11px;
  color: var(--fg-muted);
  transition: transform 160ms ease;
  transform-origin: 50% 50%;
}
.iface-group-toggle[aria-expanded="false"] .iface-group-caret {
  transform: rotate(-90deg);
}
.iface-group-title {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 10px;
  min-width: 0;
}
.iface-group-title strong {
  font-size: 14px;
  font-weight: 600;
}
.iface-group-meta {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  font-size: 12px;
  color: var(--fg-muted);
}
.iface-group-sep {
  color: var(--fg-dim);
}
.iface-group-stats {
  font-size: 12px;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  padding-right: 8px;
}
.iface-group-body {
  padding: 12px;
  display: grid;
  gap: 12px;
}
.iface-group-body[hidden] {
  display: none;
}
.iface-group-agg .chart {
  --canvas-h: 160px;
}
@media (prefers-reduced-motion: reduce) {
  .iface-group-caret {
    transition: none;
  }
}

.sys-grid {
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

/* Iface card specifics */
.link-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex: none;
}
.link-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--fg-dim);
}
.link-state[data-state="up"] .link-dot {
  background: var(--ok);
  box-shadow: 0 0 0 3px hsl(150 70% 55% / 0.18);
}
.link-state[data-state="down"] .link-dot {
  background: var(--err);
}

.iface-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 8px;
  align-items: end;
}
.iface-stat {
  display: grid;
  gap: 2px;
  min-width: 0;
}
.iface-stat-label {
  font-size: 11px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.iface-stat-value {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.iface-stat-value.warn {
  color: var(--warn);
}

/* Chart container：高度自适应（画布高度由 --canvas-h 决定，legend 自然撑开） */
.chart {
  width: 100%;
  position: relative;
  --canvas-h: 200px;
  min-height: var(--canvas-h);
}
.chart-sm {
  --canvas-h: 140px;
}
.chart:empty::before {
  content: "加载中…";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--fg-dim);
  font-size: 12px;
}

/* uPlot tweaks */
.uplot {
  font-family: inherit !important;
}
.u-legend {
  font-size: 11px !important;
  color: var(--fg-muted) !important;
  padding: 6px 4px 0 !important;
  /* 防止过多 series 时换行后撑开布局又跑出去 */
  max-width: 100%;
}
.u-legend .u-marker {
  border-radius: 2px !important;
}
.u-legend .u-value {
  font-variant-numeric: tabular-nums;
}
.u-cursor-pt {
  box-shadow: 0 0 0 2px var(--bg);
}
/* 网卡迷你图：卡片上方已展示 RX/TX，隐藏图例以节省空间 */
.chart-sm .u-legend {
  display: none !important;
}

.footer-hint {
  color: var(--fg-dim);
  font-size: 12px;
  text-align: center;
  padding: 8px 0 24px;
  font-variant-numeric: tabular-nums;
}

/* Empty / error states */
.chart[data-state="error"]::before {
  content: attr(data-error);
  color: var(--err);
}

@media (max-width: 720px) {
  .app-header {
    padding: 10px 16px;
  }
  .brand-text h1 {
    font-size: 14px;
  }
  .controls {
    width: 100%;
    justify-content: space-between;
  }
  .kpi-value {
    font-size: 22px;
  }
  .chart {
    height: 160px;
  }
}

/* ==========================================================================
   主机管理对话框
   ========================================================================== */
.dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  max-width: min(560px, calc(100vw - 32px));
  width: 100%;
  border-radius: 14px;
  overflow: visible;
}
.dialog::backdrop {
  background: hsl(var(--hue) 30% 2% / 0.55);
  backdrop-filter: blur(2px);
}
.dialog-inner {
  background: var(--bg-elev-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 60px hsl(var(--hue) 30% 2% / 0.5);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 40px);
}
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.dialog-header h2 {
  font-size: 15px;
}
.btn-close {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 18px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-close:hover {
  background: var(--bg-soft);
  color: var(--fg);
}
.dialog-body {
  padding: 16px;
  overflow-y: auto;
  display: grid;
  gap: 20px;
  overscroll-behavior: contain;
}
.dialog-section {
  display: grid;
  gap: 8px;
}
.dialog-section-title {
  font-size: 12px;
  font-weight: 500;
  color: var(--fg-muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.host-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}
.host-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 48px;
}
.host-row-text {
  flex: 1 1 auto;
  min-width: 0;
  display: grid;
  gap: 2px;
}
.host-row-text strong {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.host-row-url {
  font-size: 12px;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.btn-remove {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--err);
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  flex: none;
  min-height: 32px;
}
.btn-remove:hover {
  background: hsl(0 80% 65% / 0.12);
  border-color: var(--err);
}

.form-row {
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(140px, 1fr) minmax(200px, 2fr);
}
@media (max-width: 520px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}
.form-field {
  display: grid;
  gap: 4px;
  min-width: 0;
}
.form-field-grow {
  min-width: 0;
}
.form-field span {
  font-size: 12px;
  color: var(--fg-muted);
}
.form-field input {
  appearance: none;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  min-height: 36px;
  min-width: 0;
}
.form-field input:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px hsl(196 100% 60% / 0.25);
  outline: none;
}

.dialog-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}
.dialog-actions-grow {
  flex: 1 1 auto;
}

.hint {
  font-size: 12px;
  color: var(--fg-muted);
  margin: 0;
}
.hint-warn {
  background: hsl(38 100% 50% / 0.12);
  border: 1px solid hsl(38 100% 50% / 0.35);
  color: var(--warn);
  padding: 8px 10px;
  border-radius: 8px;
  line-height: 1.45;
}
