:root {
  --bg: #111418;
  --card: #1a1f26;
  --card2: #232a33;
  --text: #eef2f6;
  --muted: #8b97a6;
  --line: #2c3540;
  --accent: #ff9f1c;
  --ok: #2ecc71;
  --near: #f1c40f;
  --off: #ff5d5d;
  --spec: rgba(255, 159, 28, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding: env(safe-area-inset-top) 0 calc(150px + env(safe-area-inset-bottom));
  max-width: 560px;
  margin: 0 auto;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--card2);
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--accent); color: #1b1203; font-weight: 700; }
button.ghost { background: var(--card2); }
button.small { padding: 6px 10px; font-size: 14px; }
button.danger { color: var(--off); }

.muted { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 4px;
}
.topbar h1 { font-size: 19px; margin: 0; }

main { padding: 0 16px; display: flex; flex-direction: column; gap: 10px; }

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px 0;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip { white-space: nowrap; border-radius: 999px; padding: 8px 14px; }
.chip.on { background: var(--accent); color: #1b1203; font-weight: 600; }

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--card);
  border-radius: 12px;
  padding: 3px;
  gap: 3px;
}
.seg button { background: transparent; padding: 9px 6px; border-radius: 10px; }
.seg button.on { background: var(--card2); color: var(--accent); font-weight: 600; }

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 14px;
}

.target {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.target .label { color: var(--muted); }
.target input {
  width: 84px;
  text-align: center;
  background: var(--card2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 4px;
  font-size: 18px;
  font-weight: 600;
}
.step { width: 40px; height: 40px; padding: 0; font-size: 22px; }
.unit { color: var(--muted); margin-left: 4px; }

.view { text-align: center; }
.freq { font-size: 64px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.freq .unit { font-size: 22px; font-weight: 500; }
.note-line { font-size: 22px; margin-top: 2px; }

.gauge {
  position: relative;
  height: 34px;
  margin: 18px 4px 4px;
  border-radius: 10px;
  background: linear-gradient(90deg, rgba(255,93,93,.35), rgba(241,196,15,.25) 35%, rgba(46,204,113,.2) 50%, rgba(241,196,15,.25) 65%, rgba(255,93,93,.35));
}
.gauge .zone { position: absolute; left: 46%; width: 8%; top: 0; bottom: 0; background: rgba(46,204,113,.35); }
.gauge .center { position: absolute; left: 50%; top: -4px; bottom: -4px; width: 2px; margin-left: -1px; background: var(--ok); }
.needle {
  position: absolute;
  top: -8px;
  bottom: -8px;
  width: 6px;
  margin-left: -3px;
  border-radius: 3px;
  background: var(--text);
  transition: left 0.25s ease-out;
}
.needle.ok { background: var(--ok); }
.needle.near { background: var(--near); }
.needle.off { background: var(--off); }
.needle.idle { opacity: 0.25; }
.gauge-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin: 8px 2px 0; }

.status { min-height: 2.6em; margin: 12px 0 6px; font-size: 16px; }

.actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.history { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin-top: 10px; }
.pill { background: var(--card2); color: var(--muted); padding: 3px 8px; border-radius: 999px; font-size: 13px; font-variant-numeric: tabular-nums; }

#lugSvg { width: 100%; max-width: 340px; display: block; margin: 0 auto; }
#lugSvg .shell { fill: none; stroke: var(--line); stroke-width: 10; }
#lugSvg .head { fill: var(--card2); }
#lugSvg .mean { fill: var(--text); font-size: 30px; font-weight: 700; text-anchor: middle; }
#lugSvg .mean-sub { fill: var(--muted); font-size: 13px; text-anchor: middle; }
#lugSvg .lug { cursor: pointer; }
#lugSvg .lug circle { fill: var(--card2); stroke: var(--line); stroke-width: 2; }
#lugSvg .lug text { fill: var(--text); font-size: 15px; font-weight: 700; text-anchor: middle; pointer-events: none; }
#lugSvg .lug.measured circle { fill: #3a4550; }
#lugSvg .lug.ok circle { fill: var(--ok); }
#lugSvg .lug.near circle { fill: var(--near); }
#lugSvg .lug.off circle { fill: var(--off); }
#lugSvg .lug.ok text, #lugSvg .lug.near text, #lugSvg .lug.off text { fill: #111; }
#lugSvg .lug.active circle { stroke: var(--accent); stroke-width: 4; }
#lugSvg .lug-label { fill: var(--muted); font-size: 12px; text-anchor: middle; }
#lugSvg .dev { font-weight: 700; }
#lugSvg .dev.ok { fill: var(--ok); }
#lugSvg .dev.near { fill: var(--near); }
#lugSvg .dev.off { fill: var(--off); }

.stats { color: var(--muted); font-size: 14px; line-height: 1.6; margin-bottom: 10px; }
.c-tight { color: var(--off); }
.c-loose { color: var(--near); }
.c-ok { color: var(--ok); }

#spectrum { width: 100%; display: block; }

.bottombar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: rgba(17, 20, 24, 0.96);
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 560px;
  margin: 0 auto;
}
.bottombar .actions button { flex: 1; padding: 14px; font-size: 17px; }
.meter { position: relative; height: 8px; border-radius: 4px; background: var(--card2); overflow: hidden; }
#meterFill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--ok); transition: width 0.05s linear; }
#meterThr { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); left: 0; }
.sens { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.sens input { flex: 1; accent-color: var(--accent); }

dialog {
  width: min(560px, 100vw);
  max-width: 100vw;
  max-height: 100dvh;
  margin: auto auto 0;
  border: 0;
  border-radius: 18px 18px 0 0;
  background: var(--card);
  color: var(--text);
  padding: 0;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.dlg { padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; }
.dlg header { display: flex; justify-content: space-between; align-items: center; }
.dlg h2 { margin: 0; font-size: 20px; }
.dlg h3 { margin: 10px 0 0; font-size: 15px; color: var(--muted); }
.dlg .row { display: flex; gap: 8px; flex-wrap: wrap; }
.dlg select, .dlg input {
  background: var(--card2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  min-width: 0;
}
#setupSelect { flex: 1; }
.drum-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px 8px;
  padding: 10px;
  margin-top: 6px;
  background: var(--card2);
  border-radius: 12px;
  position: relative;
}
.drum-row label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); gap: 3px; }
.drum-row input, .drum-row select { width: 100%; background: var(--bg); font-size: 15px; color: var(--text); }
.drum-row .f-name { grid-column: 1 / 3; }
.drum-row .d-del {
  position: absolute;
  top: -8px;
  right: -6px;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--line);
  font-size: 13px;
}
.opt { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
