/* Rotary Inverted Pendulum — レイアウトとテーマ */

:root {
  --bg: #070b12;
  --panel: #0e1320;
  --panel2: #131a2a;
  --line: rgba(120, 150, 190, 0.16);
  --text: #dbe6f3;
  --muted: #8ea3bd;
  --accent: #38bdf8;
  --accent2: #fb7185;
  --good: #4ade80;
  --warn: #fbbf24;
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family:
    ui-sans-serif, system-ui, -apple-system, 'Hiragino Kaku Gothic ProN', 'Noto Sans JP',
    sans-serif;
  font-size: 15px;
  line-height: 1.6;
}

code,
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ── ヘッダ ───────────────────────────────────────── */
.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #0d1422, #090d16);
}

.brand h1 {
  margin: 0;
  font-size: 19px;
  letter-spacing: 0.02em;
}

.brand p {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}

.transport {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── ボタン ───────────────────────────────────────── */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 9px;
  font-size: 13.5px;
  cursor: pointer;
  transition:
    background 0.15s,
    border-color 0.15s,
    transform 0.05s;
}

.btn:hover {
  background: #1b2537;
  border-color: rgba(120, 150, 190, 0.35);
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: #0e3a52;
  border-color: rgba(56, 189, 248, 0.45);
  color: #bfe9ff;
}

.btn.warn {
  background: #4a2c12;
  border-color: rgba(251, 191, 36, 0.4);
  color: #ffe1a8;
}

.btn.wide {
  width: 100%;
  margin-top: 10px;
}

/* ── レイアウト ────────────────────────────────────── */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 14px;
  padding: 14px 20px 0;
  align-items: start;
}

.stage {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 340px;
  aspect-ratio: 16 / 10;
  /* aspect-ratio と min-height が両立しないとき、ブラウザは高さの下限を満たすために
     「幅」を広げてしまう。max-width で抑えないと横スクロールが出る */
  max-width: 100%;
  /* ステージが縦に伸びすぎると下の時系列プロットが画面外に押し出されるので、
     ビューポート高さで上限をかけて 1 画面に収める */
  max-height: 52vh;
}

#view {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

#view.grabbing {
  cursor: grabbing;
}

.stageHint {
  position: absolute;
  left: 12px;
  bottom: 10px;
  font-size: 11.5px;
  color: var(--muted);
  background: rgba(8, 12, 20, 0.7);
  padding: 4px 9px;
  border-radius: 7px;
  pointer-events: none;
}

.badge {
  position: absolute;
  left: 12px;
  top: 12px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 5px 11px;
  border-radius: 999px;
  background: rgba(8, 12, 20, 0.78);
  border: 1px solid var(--line);
  color: var(--muted);
}

.badge.swing {
  color: #ffe1a8;
  border-color: rgba(251, 191, 36, 0.45);
}

.badge.balance {
  color: #bbf7d0;
  border-color: rgba(74, 222, 128, 0.45);
}

/* ── サイドパネル ──────────────────────────────────── */
.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: calc(52vh + 28px);
  overflow-y: auto;
  padding-right: 4px;
}

.group {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 13px;
}

.group h2 {
  margin: 0 0 9px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

.group h2 .sub {
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
  color: #5b6b82;
  font-family: ui-monospace, monospace;
}

.hint {
  margin: 9px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.warn-inline {
  margin: 10px 0 0;
  font-size: 12px;
  color: #ffd9a0;
  background: rgba(251, 191, 36, 0.09);
  border: 1px solid rgba(251, 191, 36, 0.28);
  border-radius: 8px;
  padding: 7px 9px;
}

/* ── 故障の再現 ────────────────────────────────────── */
.fault {
  display: block;
  margin: 7px 0;
  font-size: 12.5px;
  cursor: pointer;
}

.fault input {
  margin-right: 7px;
  accent-color: var(--accent2);
}

.fault span {
  display: block;
  margin: 1px 0 0 23px;
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.45;
}

#grpFaults:has(input:checked) {
  border-color: rgba(251, 113, 133, 0.55);
}

/* ── モード切替 ────────────────────────────────────── */
.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.mode {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--muted);
  padding: 9px 6px;
  border-radius: 9px;
  font-size: 12.5px;
  cursor: pointer;
  transition: all 0.15s;
}

.mode:hover {
  border-color: rgba(120, 150, 190, 0.35);
  color: var(--text);
}

.mode.active {
  background: #0e3a52;
  border-color: rgba(56, 189, 248, 0.5);
  color: #bfe9ff;
}

/* ── 読み出し ─────────────────────────────────────── */
.readout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.readout div {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  padding: 3px 7px;
  background: var(--panel2);
  border-radius: 6px;
}

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

.readout b {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* ── スライダ ─────────────────────────────────────── */
.sliders {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.slider label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 3px;
}

.slider label b {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 500;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.slider input[type='range'] {
  width: 100%;
  height: 4px;
  appearance: none;
  background: #263349;
  border-radius: 3px;
  outline: none;
}

.slider input[type='range']::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #0b1220;
}

.slider input[type='range']::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #0b1220;
}

.gainBox {
  margin-top: 10px;
  border-top: 1px solid var(--line);
  padding-top: 9px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.gainRow {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 11.5px;
  color: var(--muted);
}

.gainRow code {
  color: var(--text);
  font-size: 11px;
  text-align: right;
}

/* ── スコープ ─────────────────────────────────────── */
.scopeWrap {
  padding: 14px 20px 0;
}

#scope {
  display: block;
  width: 100%;
  height: 224px;
  background: #0a0e16;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

/* ── フッタ ───────────────────────────────────────── */
.foot {
  padding: 16px 20px 28px;
  color: var(--muted);
  font-size: 12.5px;
  max-width: 1100px;
}

.foot p {
  margin: 0 0 6px;
}

.foot a {
  color: var(--accent);
}

/* ── レスポンシブ ──────────────────────────────────── */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 12px 12px 0;
  }

  .panel {
    max-height: none;
    overflow: visible;
  }

  .stage {
    aspect-ratio: 4 / 3;
    min-height: 0;
    max-height: 46vh;
  }

  .scopeWrap {
    padding: 12px 12px 0;
  }

  #scope {
    height: 300px;
  }

  .topbar {
    padding: 12px;
  }

  .foot {
    padding: 14px 12px 24px;
  }
}

@media (max-width: 520px) {
  .modes {
    grid-template-columns: 1fr;
  }

  .readout {
    grid-template-columns: 1fr;
  }

  .transport {
    width: 100%;
  }

  .transport .btn {
    flex: 1;
    padding: 8px 6px;
    font-size: 12.5px;
  }
}
