.rail-lab__body {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  min-height: 470px;
}

.rail-controls {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 22px;
  border-right: 1px solid var(--line);
  background: var(--bg-sunk);
}

.rail-control { display: grid; gap: 7px; }

.rail-control label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.rail-control output {
  float: right;
  color: var(--accent-strong);
  font: 600 11px/1.6 var(--mono);
}

.rail-control select {
  width: 100%;
  min-height: 44px;
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 0;
  background: var(--bg-panel);
  color: var(--fg-strong);
}

.rail-control input[type="range"] {
  width: 100%;
  height: 24px;
  margin: 0;
  accent-color: var(--accent);
}

.rail-check {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-height: 44px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: var(--bg-panel);
  color: var(--fg);
  font-size: 12px;
}

.rail-check input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.rail-actions {
  display: grid;
  margin-top: auto;
}

.rail-actions button {
  min-height: 44px;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 0;
  background: var(--bg-panel);
  color: var(--fg);
  font-size: 11px;
  font-weight: 600;
}

.rail-actions button:hover,
.rail-actions button:focus-visible {
  border-color: var(--accent);
  color: var(--accent-strong);
  outline: 0;
}

.rail-stage {
  min-width: 0;
  padding: clamp(18px, 3vw, 30px);
  background:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px),
    var(--bg-raised);
  background-size: 24px 24px;
}

.rail-visual {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-sunk));
}

.rail-visual svg {
  display: block;
  width: 100%;
  height: auto;
}

.rail-live-readout {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 16px;
  align-items: center;
  min-height: 40px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  background: var(--bg-sunk);
  color: var(--muted);
  font: 500 10px/1.45 var(--mono);
}

.rail-live-readout span:first-child {
  color: var(--fg-strong);
  font-weight: 700;
}

.rail-live-readout i {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 14px var(--cyan);
  animation: rail-live 1.15s ease-in-out infinite alternate;
}

.rail-live-readout code {
  margin-left: auto;
  color: var(--accent-strong);
  font-size: 10px;
}

.rail-lab[data-motion="paused"] .rail-live-readout i {
  background: var(--faint);
  box-shadow: none;
  animation-play-state: paused;
}

.rail-lab[data-motion="paused"] .rail-live-readout span:first-child::after {
  margin-left: 7px;
  color: var(--faint);
  content: "· 停止中";
}

.rail-metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.rail-metrics > div {
  min-width: 0;
  padding: 12px 13px;
  border: 1px solid var(--line);
  background: var(--bg-sunk);
}

.rail-metrics span {
  display: block;
  color: var(--faint);
  font: 500 9px/1.4 var(--mono);
  letter-spacing: .07em;
}

.rail-metrics strong {
  display: block;
  overflow-wrap: anywhere;
  margin-top: 4px;
  color: var(--fg-strong);
  font: 600 clamp(13px, 2vw, 18px)/1.35 var(--mono);
}

.rail-status {
  min-height: 62px;
  margin: 10px 0 0;
  padding: 11px 14px 11px 17px;
  border: 1px solid var(--line);
  border-left-width: 4px;
  background: var(--bg-panel);
  color: var(--muted);
  font-size: 12px;
}

.rail-status[data-tone="success"] { border-left-color: var(--cyan); }
.rail-status[data-tone="warning"] { border-left-color: var(--accent); }
.rail-status[data-tone="danger"] { border-left-color: var(--danger); }

/* --- ステージ共通部品（鉄道ラボの SVG） -----------------------------
   画面は常に暗い計器盤として描く。テーマ変数は使わず固定色の線画にする。 */
.stage-bg { fill: #0a1219; }
.stage-dot { fill: #9bb5bd; fill-opacity: .12; }
.rail-bed { fill: none; stroke: #1f2e37; stroke-linecap: round; stroke-width: 18; }
.rail-track { fill: none; stroke: #a9bcc3; stroke-linecap: round; stroke-width: 2.5; }
.rail-sleepers { fill: none; stroke: #4c626d; stroke-width: 3; }

.rail-zones rect {
  fill: rgb(87 224 207 / .04);
  stroke: rgb(87 224 207 / .35);
  stroke-dasharray: 6 5;
  stroke-width: 1.2;
  transition: fill .18s ease, stroke .18s ease;
}
.rail-zones rect[data-occupied="true"] { fill: rgb(255 167 54 / .12); stroke: #ffa736; stroke-dasharray: none; }
.rail-zones text { fill: #7f949d; font: 600 10px var(--mono); letter-spacing: .14em; }

.rail-signal-mast { fill: none; stroke: #6f858e; stroke-width: 3; stroke-linecap: round; }
.rail-signal-head { fill: #111c25; stroke: #8fa4ad; stroke-width: 1.5; }
.rail-signal-lamp { fill: #22323c; transition: fill .16s ease; }
.rail-signal[data-aspect="clear"] .rail-signal-lamp { fill: #5cf0b0; filter: drop-shadow(0 0 5px #5cf0b0); }
.rail-signal[data-aspect="stop"] .rail-signal-lamp { fill: #ff5a50; filter: drop-shadow(0 0 5px #ff5a50); }
.rail-signal-arrow { fill: none; stroke: #6f858e; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.5; }

.rail-direction path { fill: none; stroke: #7f949d; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.5; }
.rail-direction text { fill: #7f949d; font: 600 11px var(--mono); }
.rail-braking path { fill: none; stroke: #57e0cf; stroke-linecap: round; stroke-width: 1.5; stroke-dasharray: 6 5; }
.rail-braking text { fill: #57e0cf; font: 600 11px var(--mono); }
.rail-braking[data-enough="false"] path { stroke: #ff6b62; }
.rail-braking[data-enough="false"] text { fill: #ff6b62; }

/* 車両は defs のシンボルを <use> で並べるため、色は原本側のクラスに持たせる。 */
.rail-vehicle-body { fill: #1c2c36; stroke: #a9bcc3; stroke-width: .8; vector-effect: non-scaling-stroke; }
.rail-vehicle-window { fill: #6ad9ff; }
.rail-vehicle-band { fill: #ffa736; }
.rail-vehicle-cargo { fill: #0c151c; stroke: #5b727d; stroke-width: .6; vector-effect: non-scaling-stroke; }
.rail-wheel { fill: #0a1219; stroke: #a9bcc3; stroke-width: .8; vector-effect: non-scaling-stroke; }
.rail-pantograph { fill: none; stroke: #ffa736; stroke-width: 1.2; vector-effect: non-scaling-stroke; }

/* --- 02 交差点 ------------------------------------------------------ */
.junction-block {
  fill: rgb(87 224 207 / .05);
  stroke: rgb(87 224 207 / .4);
  stroke-dasharray: 7 6;
  stroke-width: 1.2;
  transition: fill .18s ease, stroke .18s ease;
}
.junction-block[data-mode="block"] { fill: rgb(255 167 54 / .06); stroke: rgb(255 167 54 / .45); }
.junction-block[data-valid="false"] { fill: rgb(255 107 98 / .1); stroke: #ff6b62; }
.junction-block-label { fill: #7f949d; font: 600 10px var(--mono); letter-spacing: .14em; }

.junction-cell { fill: #111c25; stroke: #6f858e; stroke-width: 1.5; transition: fill .18s ease, stroke .18s ease; }
.junction-cell[data-reserved="true"] { stroke: #f2f7f8; filter: drop-shadow(0 0 5px currentColor); }
.junction-cell-label { fill: #e8f1f4; font: 700 10px var(--mono); pointer-events: none; }

.junction-signal-mast { fill: none; stroke: #6f858e; stroke-width: 2.5; stroke-linecap: round; }
.junction-signal-head { fill: #111c25; stroke: #8fa4ad; stroke-width: 1.5; }
.junction-signal-lamp { fill: #22323c; transition: fill .16s ease; }
.junction-signal[data-aspect="clear"] .junction-signal-lamp { fill: #5cf0b0; filter: drop-shadow(0 0 5px #5cf0b0); }
.junction-signal[data-aspect="stop"] .junction-signal-lamp { fill: #ff5a50; filter: drop-shadow(0 0 5px #ff5a50); }
.junction-signal[data-aspect="error"] .junction-signal-lamp { fill: #ff6b62; filter: drop-shadow(0 0 7px #ff6b62); animation: junction-stop-pulse .7s ease-in-out infinite alternate; }
.junction-signal[data-active="false"] { opacity: .3; }
.junction-signal-label { fill: #8fa4ad; font: 600 9px var(--mono); letter-spacing: .1em; }

.junction-train { transition: opacity .14s ease; will-change: transform; }
.junction-train[data-active="false"] { display: none; }
.junction-train-body { stroke: #0a1219; stroke-width: 1.5; }
.junction-train-nose { fill: #0a1219; }
.junction-train-label { fill: #08131a; font: 800 11px var(--mono); }
.junction-train[data-state="waiting"] { opacity: .55; }
.junction-train[data-state="moving"][data-recycling="true"] { opacity: 0; }

.junction-station { opacity: 0; pointer-events: none; transition: opacity .2s ease; }
.junction-station[data-visible="true"] { opacity: 1; }
.junction-station rect { fill: #1a262e; stroke: #ff6b62; stroke-width: 1.5; }
.junction-station text { fill: #ffd0cd; font: 600 10px var(--mono); letter-spacing: .1em; }
.junction-legend text { fill: #6f858e; font: 600 11px var(--mono); }

/* --- 03 駅 ---------------------------------------------------------- */
.station-building { fill: #111c25; stroke: #a9bcc3; stroke-width: 1.5; }
.station-kicker { fill: #7f949d; font: 600 10px var(--mono); letter-spacing: .14em; }
.station-platform { fill: #111c25; stroke: #5b727d; stroke-width: 1.5; }
.station-platform[data-served="true"] { fill: #12303a; stroke: #57e0cf; }
.station-platform-label { fill: #8fa4ad; font: 700 11px var(--mono); }
.station-direction-arrow { fill: none; stroke: #ffa736; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2.5; }
.station-decision rect { fill: #0a1219; fill-opacity: .9; stroke: #3f545f; stroke-width: 1; }
.station-decision text { fill: #7f949d; font: 600 10px var(--mono); letter-spacing: .14em; }
.station-decision .station-decision-value { fill: #f2f7f8; font: 700 15px var(--mono); letter-spacing: 0; }
.station-facts path { fill: none; stroke: #2f424c; stroke-width: 1; }
.station-facts text { fill: #7f949d; font: 600 11px var(--mono); }

/* --- 04 自動運転の診断 ---------------------------------------------- */
.rail-fieldset {
  display: grid;
  gap: 7px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
}

.rail-fieldset legend {
  padding: 0 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.rail-diagnostics {
  display: grid;
  gap: 7px;
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
  counter-reset: none;
}

.rail-diagnostic {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--line-strong);
  background: var(--bg-raised);
}

.rail-diagnostic b {
  color: var(--muted);
  font: 700 12px var(--mono);
  text-align: center;
}

.rail-diagnostic strong {
  display: block;
  color: var(--fg);
  font-size: 13px;
}

.rail-diagnostic span {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.rail-diagnostic[data-state="ok"] { border-left-color: var(--cyan); }
.rail-diagnostic[data-state="active"] {
  border-left-color: var(--danger);
  background: rgb(255 120 111 / .09);
}
.rail-diagnostic[data-state="active"] b,
.rail-diagnostic[data-state="active"] strong { color: var(--fg-strong); }
.rail-diagnostic[data-state="skipped"] { opacity: .42; }

@keyframes rail-live {
  from { opacity: .45; transform: scale(.85); }
  to { opacity: 1; transform: scale(1.08); }
}

@keyframes junction-stop-pulse {
  from { opacity: .48; }
  to { opacity: 1; }
}

@media (max-width: 980px) {
  .rail-lab__body { grid-template-columns: 1fr; }
  .rail-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .rail-actions { margin-top: 0; }
}

@media (max-width: 760px) {
  /* 幅に合わせて縮めると信号や車両の表示が読めなくなるので、横スクロールで大きさを保つ。 */
  .rail-visual { overflow-x: auto; overflow-y: hidden; }
  .rail-visual svg { min-width: 640px; }
}

@media (max-width: 540px) {
  .rail-controls,
  .rail-metrics { grid-template-columns: 1fr; }
  .rail-controls { padding: 16px; }
  .rail-stage { padding: 12px; }
  .rail-live-readout { align-items: flex-start; }
  .rail-live-readout code { width: 100%; margin-left: 0; }
  .rail-status { min-height: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .rail-live-readout i { animation: none; }
  .junction-station,
  .junction-block,
  .junction-cell,
  .junction-train,
  .rail-zones rect { transition: none; }
  .junction-signal[data-aspect="error"] .junction-signal-lamp { animation: none; }
}
