:root {
  --bg-1: #dfe5ec;
  --bg-2: #eff3f8;
  --surface: rgba(244, 247, 251, 0.92);
  --surface-strong: rgba(255, 255, 255, 0.98);
  --border: rgba(29, 52, 74, 0.2);
  --text-main: #0f2438;
  --text-muted: #4b6278;
  --accent: #005e89;
  --accent-alt: #f07a1a;
  --shadow: 0 10px 22px rgba(23, 42, 61, 0.1);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Space Grotesk", sans-serif;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0)),
    linear-gradient(145deg, var(--bg-1), var(--bg-2));
  color: var(--text-main);
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 8px;
  gap: 6px;
}

.topbar {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.topbar-row-primary {
  justify-content: space-between;
}

.topbar-row-controls {
  justify-content: space-between;
  gap: 10px;
}

.topbar-main {
  min-width: 0;
  flex: 1;
  display: flex;
  align-items: baseline;
  gap: 8px;
  overflow: hidden;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(0.86rem, 1vw, 1rem);
  line-height: 1.05;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}

.topbar p {
  margin: 0;
  font-size: 0.58rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1;
}

.status-strip {
  display: grid;
  grid-template-columns: 164px 112px 112px 60px 92px 58px;
  gap: 4px;
  justify-content: end;
  flex: 0 0 auto;
}

.status-tile {
  min-height: 24px;
  padding: 3px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  white-space: nowrap;
  width: 100%;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.control-strip {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 4px;
  min-width: 0;
  flex: 1;
  overflow: hidden;
}

.info-strip {
  display: grid;
  grid-template-columns: minmax(120px, 1.4fr) repeat(3, minmax(56px, 1fr));
  gap: 3px;
  width: min(340px, 100%);
  flex-shrink: 1;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px;
  background: var(--surface-strong);
}

.info-item {
  min-height: 24px;
  border-radius: 5px;
  border: 1px solid rgba(17, 45, 78, 0.15);
  background: #ffffff;
  padding: 2px 5px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.cpu-model-item {
  min-width: 0;
}

.info-label {
  font-size: 0.48rem;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.info-value {
  font-size: 0.6rem;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.control {
  min-height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  padding: 2px 5px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.6rem;
  font-weight: 700;
}

.compact-control {
  min-width: 0;
}

.fixed-control {
  flex: 0 0 auto;
  justify-content: space-between;
}

.fixed-sample {
  width: 92px;
}

.fixed-refresh {
  width: 82px;
}

.fixed-spec {
  width: 86px;
}

.fixed-extref {
  width: 68px;
}

.fixed-ngref,
.fixed-npref {
  width: 86px;
}

.compact-readout {
  min-width: 150px;
  justify-content: center;
}

.fixed-acq {
  width: 164px;
  min-width: 164px;
  justify-content: center;
}

.fixed-link {
  width: 126px;
  min-width: 126px;
}

.control span {
  color: var(--text-muted);
}

.control select,
.control input {
  border: 1px solid rgba(17, 45, 78, 0.2);
  border-radius: 5px;
  background: #fdfefe;
  min-height: 18px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.62rem;
}

.control select {
  min-width: 60px;
  padding: 0 3px;
}

.toggle-control input[type="checkbox"] {
  width: 14px;
  height: 14px;
  min-height: auto;
  accent-color: var(--accent-alt);
}

.numeric-control input[type="number"] {
  width: 68px;
  padding: 0 4px;
}

.numeric-control input[type="number"]:disabled {
  opacity: 0.55;
}

.page-link {
  min-height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  padding: 3px 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--text-main);
  text-decoration: none;
}

.stream-toggle-btn {
  cursor: pointer;
  font-family: "Space Grotesk", sans-serif;
}

.stream-toggle-btn.is-stopped {
  background: rgba(240, 122, 26, 0.14);
  border-color: rgba(240, 122, 26, 0.45);
  color: #7f3f00;
}

.compact-link {
  white-space: nowrap;
}

.mono {
  font-family: "IBM Plex Mono", monospace;
}

#channel-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.stream-debug-panel {
  position: fixed;
  right: 10px;
  bottom: 10px;
  z-index: 30;
  width: min(290px, calc(100vw - 20px));
  padding: 7px 9px;
  border: 1px solid rgba(11, 28, 44, 0.28);
  border-radius: 8px;
  background: rgba(10, 18, 27, 0.84);
  color: #d7f4ff;
  box-shadow: 0 8px 24px rgba(8, 18, 29, 0.28);
  font-size: 0.58rem;
  line-height: 1.45;
  pointer-events: none;
  white-space: pre-line;
}

.stream-debug-panel[data-level="warn"] {
  border-color: rgba(240, 122, 26, 0.6);
  color: #ffe5c7;
}

.stream-debug-panel[data-level="error"] {
  border-color: rgba(237, 85, 59, 0.72);
  color: #ffd8d2;
}

@media (max-width: 900px) {
  .stream-debug-panel {
    width: min(240px, calc(100vw - 18px));
    right: 9px;
    bottom: 9px;
    padding: 6px 8px;
    font-size: 0.54rem;
  }

  .status-strip {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
  }
}

.channel-card {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: visible;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 0;
  min-height: 34px;
  max-height: 34px;
  overflow: visible;
}

.card-title-group {
  display: flex;
  align-items: center;
  min-width: 0;
  flex: 1;
  min-height: 0;
}

.channel-title {
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-main);
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.76rem;
  font-weight: 700;
  line-height: 1.1;
  cursor: default;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  min-width: 0;
}

.card-select {
  min-width: 56px;
  min-height: 22px;
  border: 1px solid rgba(17, 45, 78, 0.2);
  border-radius: 7px;
  background: #ffffff;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.6rem;
  padding: 0 4px;
}

.freq-label {
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.58rem;
  line-height: 1.1;
  color: var(--text-muted);
  min-width: 46px;
  text-align: right;
}

.mode-btn {
  border: none;
  border-radius: 7px;
  min-width: 38px;
  min-height: 22px;
  padding: 0 5px;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: white;
  background: linear-gradient(135deg, var(--accent), #1779aa);
  cursor: pointer;
}

.mode-btn.fft {
  background: linear-gradient(135deg, var(--accent-alt), #ff9c3d);
}

.plot-wrap {
  position: relative;
  flex: 1;
  min-height: 230px;
  padding: 2px 6px 20px 46px;
}

.card-metrics {
  display: flex;
  gap: 4px;
  padding: 0 6px 2px;
  color: var(--text-muted);
  font-size: 0.56rem;
  font-family: "IBM Plex Mono", monospace;
  min-height: 18px;
  max-height: 18px;
  overflow: hidden;
}

.metric-chip {
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid rgba(17, 45, 78, 0.15);
  background: rgba(255, 255, 255, 0.78);
  font-size: 0.56rem;
}

.freq-range-control {
  padding: 0 6px 2px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 34px;
  max-height: 34px;
  overflow: hidden;
}

.freq-range-label {
  font-size: 0.62rem;
  color: var(--text-muted);
}

.freq-range-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.peak-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.62rem;
  color: var(--text-muted);
  user-select: none;
}

.peak-toggle input {
  margin: 0;
}

.peak-toggle input:disabled + span {
  opacity: 0.45;
}

.freq-range-track {
  position: relative;
  height: 26px;
}

.freq-range-track::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 10px;
  height: 6px;
  border-radius: 999px;
  background: rgba(17, 45, 78, 0.12);
}

.freq-range-fill {
  position: absolute;
  top: 10px;
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(90deg, #f08a5d, #ed553b);
}

.freq-range-slider {
  position: absolute;
  inset: 0;
  width: 100%;
  margin: 0;
  background: transparent;
  pointer-events: none;
  -webkit-appearance: none;
  appearance: none;
}

.freq-range-slider::-webkit-slider-runnable-track {
  height: 26px;
  background: transparent;
}

.freq-range-slider::-moz-range-track {
  height: 26px;
  background: transparent;
}

.freq-range-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  margin-top: 5px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  background: #16385c;
  box-shadow: 0 2px 10px rgba(17, 45, 78, 0.22);
  pointer-events: auto;
  cursor: ew-resize;
}

.freq-range-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  background: #16385c;
  box-shadow: 0 2px 10px rgba(17, 45, 78, 0.22);
  pointer-events: auto;
  cursor: ew-resize;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.plot-axis-x,
.plot-axis-y {
  position: absolute;
  pointer-events: none;
  color: rgba(57, 80, 110, 0.92);
  font-size: 0.68rem;
  font-family: "IBM Plex Mono", monospace;
  letter-spacing: 0.04em;
  z-index: 2;
}

.plot-axis-x {
  left: 46px;
  right: 6px;
  bottom: 3px;
  text-align: center;
}

.plot-axis-y {
  left: 4px;
  top: 50%;
  transform: translateY(-50%) rotate(-90deg);
  transform-origin: center;
  white-space: nowrap;
  width: 42px;
  text-align: center;
}

.peak-overlay {
  position: absolute;
  inset: 2px 6px 20px 46px;
  pointer-events: none;
  z-index: 3;
}

.peak-tag {
  position: absolute;
  max-width: 120px;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid rgba(17, 45, 78, 0.18);
  color: #112d4e;
  font-size: 0.64rem;
  line-height: 1.25;
  font-family: "IBM Plex Mono", monospace;
  box-shadow: 0 8px 18px rgba(17, 45, 78, 0.12);
}

.peak-tag strong {
  display: block;
  font-size: 0.66rem;
}

.hidden {
  display: none;
}

#detail-modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(17, 45, 78, 0.35);
  backdrop-filter: blur(5px);
  padding: 20px;
}

.detail-shell {
  width: min(1450px, 100%);
  height: 100%;
  margin: 0 auto;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.detail-header h2 {
  margin: 0;
  font-size: 1.04rem;
}

.detail-header p {
  margin: 2px 0 0;
  color: var(--text-muted);
  font-size: 0.72rem;
}

#detail-close {
  min-width: 88px;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fdf1e3;
  font-weight: 700;
  cursor: pointer;
}

.detail-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: minmax(0, 1fr) minmax(180px, 0.78fr);
  gap: 8px;
  padding: 8px;
}

.detail-card {
  position: relative;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: white;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.detail-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 10px 6px;
  border-bottom: 1px solid var(--border);
}

.detail-card h3 {
  margin: 0;
  font-size: 0.84rem;
}

.detail-meta {
  font-size: 0.62rem;
  color: var(--text-muted);
  text-align: right;
  white-space: nowrap;
}

.detail-card canvas,
.detail-plot-host {
  flex: 1;
  min-height: 0;
}

.detail-plot-host {
  position: relative;
}

#detail-spectrum {
  position: relative;
  width: 100%;
  height: 100%;
}

.peak-overlay-detail {
  inset: 8px 12px 12px 12px;
}

.peak-overlay-detail .peak-tag {
  max-width: 140px;
}

.uplot {
  width: 100%;
  height: 100%;
}

.waterfall-card {
  grid-column: span 2;
}

@media (max-width: 1180px) {
  #channel-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
  }

  .topbar-row-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .control-strip,
  .status-strip {
    display: flex;
    flex-wrap: wrap;
  }

  .info-strip {
    width: 100%;
  }
}

@media (max-width: 760px) {
  #app {
    padding: 10px;
    gap: 10px;
  }

  .topbar-main {
    display: block;
    width: 100%;
  }

  .topbar-row-primary,
  .topbar-row-controls {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .topbar h1 {
    white-space: normal;
  }

  .topbar p {
    white-space: normal;
  }

  .status-strip,
  .control-strip {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  .fixed-control,
  .fixed-acq,
  .fixed-link {
    width: auto;
    min-width: 0;
  }

  .info-strip {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .cpu-model-item {
    min-width: 0;
    grid-column: span 2;
  }

  #channel-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: repeat(8, minmax(160px, 1fr));
  }

  #detail-modal {
    padding: 10px;
  }

  .detail-grid {
    grid-template-columns: 1fr;
    grid-template-rows: repeat(3, minmax(0, 1fr));
  }

  .waterfall-card {
    grid-column: span 1;
  }
}
