:root {
  --bg: #081120;
  --bg-soft: #0d1e34;
  --ink: #eff5ff;
  --muted: #9fb4cf;
  --accent: #4cf2be;
  --accent-2: #ffcc66;
  --danger: #ff5d73;
  --line: #173054;
  --line-bright: #2b578f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Space Grotesk", sans-serif;
  background:
    radial-gradient(1200px 700px at 0% 0%, #14355f, transparent 55%),
    radial-gradient(900px 500px at 100% 100%, #3b2b12, transparent 50%),
    linear-gradient(160deg, var(--bg), #050910 65%);
  color: var(--ink);
  overflow-x: hidden;
}

body.crt-mode {
  background:
    radial-gradient(900px 560px at 52% 36%, #0d2e15, transparent 58%),
    radial-gradient(1200px 760px at 50% 120%, #061307, transparent 70%),
    linear-gradient(165deg, #040a05, #010301 70%);
  color: #def3e0;
}

body.crt-mode .orb-a,
body.crt-mode .orb-b {
  opacity: 0.09;
  filter: blur(120px);
}

body.crt-mode .orb-a {
  background: #55d47b;
}

body.crt-mode .orb-b {
  background: #2a5f35;
}

.bg-orb {
  position: fixed;
  border-radius: 999px;
  filter: blur(80px);
  opacity: 0.3;
  pointer-events: none;
}

.orb-a {
  width: 340px;
  height: 340px;
  background: #39f7be;
  top: -100px;
  right: -80px;
}

.orb-b {
  width: 420px;
  height: 420px;
  background: #ffb15e;
  bottom: -180px;
  left: -140px;
}

.app-shell {
  width: min(1100px, 94vw);
  margin: 32px auto;
  display: grid;
  gap: 20px;
  animation: rise-in 0.7s ease-out;
}

.app-header {
  padding: 8px 6px;
}

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.eyebrow {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.18em;
  color: var(--accent-2);
}

h1 {
  margin: 8px 0 0;
  font-size: clamp(34px, 6vw, 64px);
  line-height: 1.03;
}

.crt-top-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid #27466f;
  border-radius: 10px;
  background: rgba(10, 21, 37, 0.65);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.crt-top-toggle input {
  transform: scale(1.15);
}

body.crt-mode .crt-top-toggle {
  border-color: #2d7040;
  background: rgba(8, 26, 12, 0.7);
  color: #9fdbac;
}

.subtitle {
  margin: 8px 0 0;
  color: var(--muted);
}

.scope-panel {
  border: 1px solid #29466f;
  background: linear-gradient(180deg, rgba(10, 25, 43, 0.95), rgba(6, 12, 22, 0.96));
  border-radius: 16px;
  padding: 14px;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.45);
}

body.crt-mode .scope-panel {
  border: 1px solid #2a5d3a;
  background: linear-gradient(180deg, rgba(6, 18, 9, 0.95), rgba(2, 7, 3, 0.96));
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.62), inset 0 0 22px rgba(74, 160, 102, 0.16);
}

canvas {
  display: block;
  width: 100%;
  height: min(54vh, 420px);
  border-radius: 10px;
  border: 1px solid #2e4f7d;
  background: #020915;
}

body.crt-mode canvas {
  border: 1px solid #2b5f3d;
  background: #020904;
  box-shadow: inset 0 0 32px rgba(90, 180, 120, 0.16);
}

.hud {
  margin-top: 10px;
  display: flex;
  flex-wrap: nowrap;
  gap: 14px;
  align-items: center;
  min-height: 22px;
  overflow: hidden;
  color: var(--muted);
  font-family: "IBM Plex Mono", monospace;
  font-size: 13px;
}

.hud > span:not(.status-dot) {
  flex: 1 1 0;
  min-width: 0;
  line-height: 1.25;
  min-height: 1.25em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#statusText {
  flex: 0 0 auto;
}

body.crt-mode .hud {
  color: #8ec29a;
}

#triggerStateLabel {
  color: var(--accent);
}

body.crt-mode #triggerStateLabel {
  color: #86f3a7;
}

#outputStateLabel {
  color: var(--accent-2);
}

body.crt-mode #outputStateLabel,
body.crt-mode #measurementLabel {
  color: #b1e3bf;
}

#measurementLabel {
  color: var(--accent-2);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 99px;
  display: inline-block;
  margin-right: 6px;
  vertical-align: middle;
}

.idle {
  background: #6a7f9b;
}

.live {
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

.freeze {
  background: var(--accent-2);
  box-shadow: 0 0 10px var(--accent-2);
}

.control-panel {
  border: 1px solid #29466f;
  border-radius: 16px;
  padding: 14px;
  background: linear-gradient(180deg, rgba(8, 20, 36, 0.92), rgba(5, 11, 20, 0.95));
}

.controls-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(280px, 1fr));
  gap: 16px;
  min-width: 0;
}

.panel-title {
  margin: 0 0 10px;
  font-size: 13px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent-2);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}

.button-row-toggle {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid #27466f;
  border-radius: 10px;
  background: rgba(10, 21, 37, 0.65);
  font-size: 13px;
  color: var(--muted);
}

.button-row-toggle input {
  transform: scale(1.15);
}

.btn {
  appearance: none;
  border: 1px solid #37669d;
  color: var(--ink);
  background: #0e2340;
  border-radius: 10px;
  padding: 9px 16px;
  font-family: "Space Grotesk", sans-serif;
  font-size: 15px;
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease;
}

.btn:hover:enabled {
  transform: translateY(-1px);
  border-color: #58a5f0;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(120deg, #1f8f6d, #3adba8);
  border-color: #3adba8;
  color: #042114;
  font-weight: 700;
}

.btn-output-running {
  background: linear-gradient(120deg, #9b2740, #ff5d73);
  border-color: #ff5d73;
}

.btn-danger {
  background: linear-gradient(120deg, #9b2740, #ff5d73);
  border-color: #ff5d73;
  color: #fff0f2;
  font-weight: 700;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  min-width: 0;
}

.control-item {
  border: 1px solid #27466f;
  border-radius: 10px;
  padding: 10px;
  background: rgba(10, 21, 37, 0.65);
  display: grid;
  gap: 8px;
  font-size: 14px;
  min-width: 0;
}

.control-item > * {
  min-width: 0;
}

.control-item output {
  font-family: "IBM Plex Mono", monospace;
  color: var(--accent);
}

.freq-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.gain-meta-left {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.btn-fit {
  appearance: none;
  border: 1px solid #3a6da9;
  color: #cfe6ff;
  background: #102949;
  border-radius: 7px;
  padding: 2px 8px;
  min-height: 24px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.14s ease, color 0.14s ease, transform 0.14s ease;
}

.btn-fit:hover {
  border-color: #7ec0ff;
  color: #eff7ff;
  transform: translateY(-1px);
}

body.crt-mode .btn-fit {
  border-color: #3e8452;
  color: #b9e7c3;
  background: #12321b;
}

.inline-lock {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.tooltip-anchor {
  position: relative;
  display: inline-block;
  cursor: help;
}

.tooltip-anchor::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 0;
  top: calc(100% + 8px);
  width: min(260px, 75vw);
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #37669d;
  background: rgba(4, 14, 27, 0.98);
  color: #d5e4f7;
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 20;
}

.tooltip-anchor::before {
  content: "";
  position: absolute;
  left: 12px;
  top: calc(100% + 3px);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 6px solid #37669d;
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 21;
}

.tooltip-anchor:hover::after,
.tooltip-anchor:hover::before {
  opacity: 1;
  transform: translateY(0);
}

.control-item input[type="range"] {
  width: 100%;
  min-width: 0;
  max-width: 100%;
}

.control-item input[type="number"],
.control-item select {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: 1px solid #37669d;
  border-radius: 8px;
  background: #0e2340;
  color: var(--ink);
  padding: 8px;
  font-family: "IBM Plex Mono", monospace;
}

.phase-options {
  display: grid;
  gap: 8px;
}

.phase-option {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  border: 1px solid #37669d;
  border-radius: 8px;
  background: #0e2340;
  padding: 8px 10px;
}

.phase-option input[type="radio"] {
  transform: scale(1.05);
}

.phase-option input[type="number"] {
  width: 92px;
  max-width: 100%;
  margin-left: 2px;
}

.phase-custom span:last-child {
  color: var(--muted);
}

.checkbox-item {
  grid-template-columns: auto 1fr;
  align-items: center;
}

.checkbox-inline {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.checkbox-inline input {
  transform: scale(1.2);
}

.timebase-stability {
  font-family: "IBM Plex Mono", monospace;
  font-size: 13px;
  cursor: help;
  flex-shrink: 0;
}

.timebase-stability[data-level="good"] { color: var(--accent); }
.timebase-stability[data-level="ok"]   { color: var(--accent-2); }
.timebase-stability[data-level="bad"]  { color: var(--danger); }

/* 無効化グレーアウト */
.control-item-dim {
  opacity: 0.4;
  user-select: none;
}

/* 入力要素のみ操作不可、テキストはホバー可能に保つ */
.control-item-dim input,
.control-item-dim select,
.control-item-dim output {
  pointer-events: none;
}

.control-item-dim .tooltip-anchor {
  pointer-events: auto;
  cursor: help;
}

/* 周波数プリセットボタン */
.freq-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn-preset {
  appearance: none;
  border: 1px solid #37669d;
  color: var(--muted);
  background: #0a1929;
  border-radius: 6px;
  padding: 4px 8px;
  font-family: "IBM Plex Mono", monospace;
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.14s ease, color 0.14s ease;
  flex: 1 1 auto;
  min-width: 0;
}

.btn-preset:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-preset:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.hint {
  margin: 12px 0 2px;
  font-size: 13px;
  color: var(--muted);
}

@keyframes rise-in {
  from {
    opacity: 0;
    transform: translateY(14px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 820px) {
  .app-shell {
    margin: 18px auto 24px;
  }

  .title-row {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .controls-layout {
    grid-template-columns: 1fr;
  }

  .control-grid {
    grid-template-columns: 1fr;
  }

  canvas {
    height: 46vh;
  }
}

@media (max-width: 640px) {
  .control-panel {
    padding: 12px;
  }

  .button-row {
    gap: 8px;
  }

  .btn {
    flex: 1 1 120px;
    padding: 9px 12px;
  }

  .control-item input[type="number"],
  .control-item select {
    font-size: 15px;
    padding: 7px;
  }

  .phase-option input[type="number"] {
    width: min(120px, 52vw);
    margin-left: 0;
  }
}

@media (max-width: 460px) {
  .app-shell {
    width: 96vw;
    gap: 14px;
  }

  .control-item {
    padding: 9px;
    gap: 6px;
  }

  .phase-option {
    padding: 7px 8px;
    gap: 6px;
  }

  .phase-option input[type="number"] {
    width: 100%;
  }
}
