:root {
  color-scheme: dark;
  --bg: #10130f;
  --bg-soft: #171b16;
  --panel: rgba(24, 28, 23, 0.88);
  --panel-strong: rgba(32, 37, 31, 0.96);
  --line: rgba(228, 229, 215, 0.14);
  --text: #f4f3ea;
  --muted: #a8ac9c;
  --faint: #727867;
  --green: #30d890;
  --green-dark: #12724e;
  --amber: #f0b94e;
  --coral: #f36f5f;
  --cyan: #58c7d8;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
  --radius: 8px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Noto Sans TC", "Microsoft JhengHei", Arial, sans-serif;
}

body.light {
  color-scheme: light;
  --bg: #f4f2e9;
  --bg-soft: #ebe8dc;
  --panel: rgba(255, 252, 242, 0.9);
  --panel-strong: rgba(255, 252, 242, 0.98);
  --line: rgba(24, 28, 23, 0.14);
  --text: #20251f;
  --muted: #65705f;
  --faint: #8a9182;
  --shadow: 0 24px 70px rgba(24, 28, 23, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    linear-gradient(135deg, rgba(48, 216, 144, 0.14), transparent 30%),
    linear-gradient(315deg, rgba(243, 111, 95, 0.13), transparent 34%),
    var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

#signalCanvas {
  position: fixed;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  opacity: 0.48;
  pointer-events: none;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(1440px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(48, 216, 144, 0.82), rgba(88, 199, 216, 0.78)),
    #1a3429;
  color: #06140f;
  font-weight: 900;
  letter-spacing: 0;
}

.brand strong,
.brand small {
  display: block;
  line-height: 1.05;
}

.brand strong {
  max-width: 42vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(1rem, 1.4rem, 1.4rem);
}

.brand small {
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.78rem;
}

.topbar-actions,
.panel-head,
.legend,
.status-pill,
.start-button,
.icon-button {
  display: inline-flex;
  align-items: center;
}

.topbar-actions {
  gap: 10px;
}

.status-pill {
  gap: 8px;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid color-mix(in srgb, var(--green) 36%, var(--line));
  border-radius: 999px;
  background: color-mix(in srgb, var(--green) 12%, transparent);
  color: var(--text);
  font-size: 0.82rem;
  white-space: nowrap;
}

.pulse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 0 rgba(48, 216, 144, 0.72);
  animation: pulse 1.8s infinite;
}

@keyframes pulse {
  70% {
    box-shadow: 0 0 0 12px rgba(48, 216, 144, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(48, 216, 144, 0);
  }
}

.icon-button {
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel-strong) 78%, transparent);
  cursor: pointer;
}

.icon-button:hover,
.start-button:hover {
  border-color: color-mix(in srgb, var(--green) 50%, var(--line));
}

.icon-button svg,
.start-button svg {
  width: 18px;
  height: 18px;
}

.dashboard {
  display: grid;
  grid-template-columns: minmax(340px, 0.95fr) minmax(420px, 1.35fr) minmax(280px, 0.7fr);
  gap: 16px;
  padding-top: 16px;
  align-items: stretch;
}

.test-panel,
.telemetry-panel,
.node-panel,
.chart-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.test-panel,
.node-panel {
  padding: 18px;
}

.telemetry-panel {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 16px;
  padding: 0;
  background: transparent;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
}

.panel-head {
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.panel-head.compact {
  margin-bottom: 12px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(1.6rem, 3vw, 3rem);
  line-height: 1.04;
}

h2 {
  font-size: 1rem;
  line-height: 1.2;
}

.mode-badge {
  flex: 0 0 auto;
  min-width: 78px;
  padding: 8px 10px;
  border: 1px solid color-mix(in srgb, var(--cyan) 45%, var(--line));
  border-radius: 8px;
  background: color-mix(in srgb, var(--cyan) 11%, transparent);
  color: color-mix(in srgb, var(--cyan) 78%, var(--text));
  font-size: 0.72rem;
  font-weight: 800;
  text-align: center;
}

.speed-stage {
  display: grid;
  justify-items: center;
  gap: 18px;
  padding: 12px 0 16px;
}

.gauge {
  --progress: 0;
  width: min(320px, 72vw);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background:
    radial-gradient(circle at center, var(--panel-strong) 0 58%, transparent 59%),
    conic-gradient(
      from -130deg,
      var(--green) 0deg,
      var(--cyan) calc(var(--progress) * 1.8deg),
      color-mix(in srgb, var(--line) 70%, transparent) calc(var(--progress) * 1.8deg),
      color-mix(in srgb, var(--line) 70%, transparent) 260deg,
      transparent 260deg
    );
  position: relative;
  box-shadow: inset 0 0 0 1px var(--line);
}

.gauge::after {
  content: "";
  position: absolute;
  inset: 10%;
  border: 1px solid var(--line);
  border-radius: 50%;
}

.gauge-inner {
  position: relative;
  z-index: 1;
  width: 74%;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  border-radius: 50%;
  background:
    linear-gradient(180deg, color-mix(in srgb, var(--panel-strong) 92%, transparent), var(--panel)),
    var(--panel-strong);
  text-align: center;
}

.speed-label,
.speed-unit {
  color: var(--muted);
  font-size: 0.85rem;
}

#speedValue {
  font-size: clamp(3rem, 8vw, 5.7rem);
  line-height: 0.96;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}

.start-button {
  justify-content: center;
  gap: 9px;
  width: min(250px, 100%);
  min-height: 48px;
  border: 1px solid color-mix(in srgb, var(--green) 48%, var(--line));
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(48, 216, 144, 0.92), rgba(88, 199, 216, 0.84)),
    #1f4d3b;
  color: #06140f;
  font-weight: 850;
  cursor: pointer;
  box-shadow: 0 16px 32px rgba(18, 114, 78, 0.28);
}

.start-button:disabled {
  cursor: wait;
  filter: saturate(0.78);
  opacity: 0.76;
}

.primary-metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.metric,
.mini-metric {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--panel-strong) 76%, transparent);
}

.metric {
  padding: 16px;
}

.metric span,
.mini-metric span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 750;
  text-transform: uppercase;
}

.metric strong,
.mini-metric strong {
  display: block;
  margin-top: 8px;
  font-size: clamp(1.9rem, 4vw, 2.8rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.metric small,
.mini-metric small {
  color: var(--faint);
}

.metric-download {
  border-color: color-mix(in srgb, var(--green) 30%, var(--line));
}

.metric-upload {
  border-color: color-mix(in srgb, var(--coral) 34%, var(--line));
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.mini-metric {
  padding: 14px;
  min-height: 112px;
}

.mini-metric strong {
  font-size: clamp(1.45rem, 3vw, 2.25rem);
}

.chart-panel {
  min-height: 360px;
  padding: 18px;
}

.legend {
  gap: 10px;
  color: var(--muted);
  font-size: 0.76rem;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend b {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.legend-download {
  background: var(--green);
}

.legend-upload {
  background: var(--coral);
}

#traceCanvas {
  display: block;
  width: 100%;
  height: 270px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(color-mix(in srgb, var(--line) 54%, transparent) 1px, transparent 1px),
    linear-gradient(90deg, color-mix(in srgb, var(--line) 54%, transparent) 1px, transparent 1px),
    color-mix(in srgb, var(--panel-strong) 78%, transparent);
  background-size: 100% 25%, 12.5% 100%, auto;
}

.node-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100%;
}

.node-list,
.history-list {
  display: grid;
  gap: 9px;
}

.node-item,
.history-item {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel-strong) 70%, transparent);
}

.node-item.active {
  border-color: color-mix(in srgb, var(--green) 54%, var(--line));
}

.node-row,
.history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.node-row strong,
.history-row strong {
  min-width: 0;
  overflow-wrap: anywhere;
}

.node-meta,
.history-meta {
  color: var(--muted);
  font-size: 0.78rem;
}

.node-load {
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: color-mix(in srgb, var(--line) 70%, transparent);
}

.node-load span {
  display: block;
  height: 100%;
  width: var(--load, 40%);
  border-radius: inherit;
  background: linear-gradient(90deg, var(--green), var(--amber));
}

.chip {
  flex: 0 0 auto;
  padding: 4px 7px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--green) 12%, transparent);
  color: color-mix(in srgb, var(--green) 72%, var(--text));
  font-size: 0.7rem;
  font-weight: 800;
}

.history-block {
  margin-top: auto;
}

.empty-state {
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 1180px) {
  .dashboard {
    grid-template-columns: minmax(320px, 0.9fr) minmax(420px, 1.1fr);
  }

  .node-panel {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .node-panel > .panel-head {
    grid-column: 1 / -1;
  }

  .history-block {
    margin-top: 0;
  }
}

@media (max-width: 820px) {
  .app-shell {
    padding: 10px;
  }

  .topbar {
    align-items: flex-start;
  }

  .status-pill {
    display: none;
  }

  .dashboard,
  .node-panel {
    grid-template-columns: 1fr;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .node-panel > .panel-head {
    grid-column: auto;
  }
}

@media (max-width: 520px) {
  .topbar {
    gap: 8px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .brand small {
    display: none;
  }

  .primary-metrics,
  .metric-grid {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 1.55rem;
  }

  .test-panel,
  .node-panel,
  .chart-panel {
    padding: 14px;
  }

  #traceCanvas {
    height: 230px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
