/* The Merge Train panel (web/merge-train.mjs). Colours and sizes are the
   control room's own tokens from styles.css; nothing here names a colour. */

/* Its own height, always whole: the scene, its line, the bar, two buttons and
   the last three runs. A rail block left to shrink is squeezed to its border
   by the panels around it, which each hold a minimum. */
#rail-train { flex: 0 0 auto; }
#train-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3) var(--sp-3);
  overflow: visible;
}

/* ---- the scene: sky, train, track ---------------------------------------- */

.train-scene {
  position: relative;
  height: 92px;
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  background: linear-gradient(to bottom, var(--surface-inset) 0%, var(--surface-inset) 62%, var(--accent-wash) 100%);
  overflow: hidden;
}

.train-rail {
  position: absolute;
  inset: 0 6px 13px 8px;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 2px;
}

/* Two rails and the sleepers between them. While a run is under way the
   sleepers run backwards under the train, which is what makes it move. */
.train-track {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 5px;
  height: 8px;
  border-top: 2px solid var(--ink-faint);
  background-image: repeating-linear-gradient(to right, var(--edge) 0 3px, transparent 3px 14px);
  background-size: 14px 100%;
  opacity: 0.9;
}

.train-cars {
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-start;
  align-items: flex-end;
  gap: 4px;
  flex: 1 1 auto;
  min-width: 0;
  margin: 0 3px 0 0;
  padding: 0;
  list-style: none;
}

/* A wagon: a box on a flat bed, a coupling to the one ahead, two wheels. */
.train-car {
  position: relative;
  flex: 1 1 0;
  max-width: 44px;
  min-width: 0;
  height: 23px;
  margin-bottom: 8px;
  border: 1px solid var(--accent-line-hot);
  border-bottom-width: 3px;
  border-radius: 4px 4px 2px 2px;
  background: var(--accent-tint);
  color: var(--ink);
  transition: background-color 0.3s ease, border-color 0.3s ease, opacity 0.3s ease;
}
.train-car-label {
  display: block;
  font-size: var(--fs-xs);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 19px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
}
.train-car::before {
  /* The coupling to whatever is ahead (to the right). */
  content: '';
  position: absolute;
  right: -6px;
  bottom: 0;
  width: 6px;
  height: 2px;
  background: var(--ink-faint);
}
.train-car::after {
  /* Both wheels, as one strip with two discs. */
  content: '';
  position: absolute;
  left: 3px;
  right: 3px;
  bottom: -10px;
  height: 7px;
  background:
    radial-gradient(circle at 3.5px 3.5px, var(--ink-dim) 0 3.2px, transparent 3.5px),
    radial-gradient(circle at calc(100% - 3.5px) 3.5px, var(--ink-dim) 0 3.2px, transparent 3.5px);
}
.train-car.is-empty { border-style: dashed; border-color: var(--edge); background: transparent; opacity: 0.55; }
.train-car.is-empty::before,
.train-car.is-empty::after { opacity: 0.35; }
.train-car.is-riding { background: var(--teal); border-color: var(--teal-hot); color: var(--on-accent); }

.train-engine { flex: 0 0 auto; width: 92px; height: 69px; margin-bottom: -2px; overflow: visible; }
.train-paint { fill: var(--teal); }
.train-metal { fill: var(--ink-dim); }
.train-band { fill: var(--on-accent); opacity: 0.28; }
.train-window { fill: var(--surface-inset); stroke: var(--on-accent); stroke-opacity: 0.35; stroke-width: 1; }
.train-lamp { fill: var(--ink-faint); }
.train-tyre { fill: var(--surface-inset); stroke: var(--ink-dim); stroke-width: 2.2; }
.train-spoke { stroke: var(--ink-dim); stroke-width: 1.2; }
.train-hub { fill: var(--ink-dim); }
.train-rod { fill: var(--ink); opacity: 0.85; }
.train-wheel { transform-box: fill-box; transform-origin: center; }
.train-rod { transform-box: fill-box; transform-origin: center; }
.train-puff { fill: var(--ink-dim); opacity: 0; transform-box: fill-box; transform-origin: center; }

/* Nobody aboard: the engine is cold. */
[data-train='empty'] .train-paint,
[data-train='off'] .train-paint { fill: var(--ink-faint); }
[data-train='empty'] .train-engine { opacity: 0.7; }
/* Switched off for this project: the whole scene stands dim. */
[data-train='off'] .train-scene { opacity: 0.45; }
[data-train='off'] .train-meter { display: none; }
.train-power { flex: 0 0 auto; }
[data-train='off'] .train-power { order: -1; flex: 1 1 auto; justify-content: center; height: var(--ctl-h-lg); color: var(--teal-hot); border: 1px solid var(--accent-line-hot); background: var(--accent-wash); font-size: var(--fs-md); font-weight: 600; }

/* Somebody aboard: steam up, a lazy puff now and then. */
[data-train='waiting'] .train-puff { animation: train-puff-idle 3.6s ease-out infinite; }
[data-train='waiting'] .train-puff-1 { animation-delay: 1.2s; }
[data-train='waiting'] .train-puff-2 { animation-delay: 2.4s; }
[data-train='waiting'] .train-puff-3 { animation: none; }
[data-train='waiting'] .train-lamp,
[data-train='running'] .train-lamp { fill: var(--amber); }

/* Merging: wheels turn, the rod works, the track runs, the smoke streams back. */
[data-train='running'] .train-puff { animation: train-puff-run 1.3s linear infinite; fill: var(--ink); }
[data-train='running'] .train-puff-1 { animation-delay: 0.32s; }
[data-train='running'] .train-puff-2 { animation-delay: 0.65s; }
[data-train='running'] .train-puff-3 { animation-delay: 0.97s; }
[data-train='running'] .train-wheel { animation: train-spin 0.9s linear infinite; }
[data-train='running'] .train-rod { animation: train-rod 0.9s linear infinite; }
[data-train='running'] .train-track { animation: train-track 0.35s linear infinite; }
[data-train='running'] .train-body,
[data-train='running'] .train-car { animation: train-bob 0.45s ease-in-out infinite; }
[data-train='running'] .train-car:nth-child(even) { animation-delay: 0.22s; }
[data-train='running'] .train-lamp { filter: drop-shadow(0 0 3px var(--amber)); }

@keyframes train-puff-idle {
  0% { opacity: 0; transform: translate(0, 0) scale(0.45); }
  20% { opacity: 0.5; }
  100% { opacity: 0; transform: translate(-5px, -22px) scale(1.7); }
}
@keyframes train-puff-run {
  0% { opacity: 0; transform: translate(0, 0) scale(0.5); }
  12% { opacity: 0.85; }
  100% { opacity: 0; transform: translate(-58px, -16px) scale(3.2); }
}
@keyframes train-spin { to { transform: rotate(360deg); } }
@keyframes train-rod {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(2.2px, 2.2px); }
  50% { transform: translate(0, 4.4px); }
  75% { transform: translate(-2.2px, 2.2px); }
}
@keyframes train-track { to { background-position: -14px 0; } }
@keyframes train-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-0.8px); }
}

/* ---- what it is doing ---------------------------------------------------- */

.train-line { margin: 0; min-height: 1.45em; color: var(--ink-dim); font-size: var(--fs-md); line-height: 1.45; }
[data-train='running'] .train-line { color: var(--ink); }

/* The wait, as a bar that fills until the train leaves by itself. */
.train-meter { height: 4px; border-radius: 2px; background: var(--edge); overflow: hidden; }
.train-meter-fill { display: block; height: 100%; width: 0; border-radius: 2px; background: var(--teal); transition: width 2s linear; }
[data-train='empty'] .train-meter,
[data-train='running'] .train-meter { display: none; }

/* The four steps of a run, shown only while one is under way. */
.train-steps { display: none; gap: var(--sp-1); margin: 0; padding: 0; list-style: none; }
[data-train='running'] .train-steps { display: flex; }
.train-step {
  flex: 1 1 0;
  padding: 2px 0;
  border: 1px solid var(--edge);
  border-radius: var(--radius-sm);
  color: var(--ink-faint);
  font-size: var(--fs-xs);
  text-align: center;
}
.train-step.is-done { color: var(--ink-dim); border-color: var(--accent-line); background: var(--accent-wash); }
.train-step.is-now { color: var(--on-accent); border-color: var(--teal-hot); background: var(--teal); font-weight: 600; }

.train-controls { display: flex; gap: var(--sp-2); align-items: center; }
.train-go { flex: 1 1 auto; justify-content: center; }
.train-go:disabled { opacity: 0.45; cursor: default; }
.train-sound { flex: 0 0 auto; }
.train-sound svg { width: 16px; height: 16px; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }
.train-sound[aria-pressed='true'] { color: var(--teal-hot); border-color: var(--accent-line-hot); }

.train-history { display: flex; flex-direction: column; gap: 3px; margin: 0; padding: 0; list-style: none; color: var(--ink-faint); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.train-run { position: relative; padding-left: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.train-run::before { content: ''; position: absolute; left: 1px; top: 0.5em; width: 6px; height: 6px; border-radius: 50%; background: var(--green); }
.train-run.has-returns { color: var(--amber); }
.train-run.has-returns::before { background: var(--amber); }

@media (prefers-reduced-motion: reduce) {
  .train-puff, .train-wheel, .train-rod, .train-track, .train-body, .train-car { animation: none !important; }
  [data-train='waiting'] .train-puff-0, [data-train='running'] .train-puff-0 { opacity: 0.6; transform: translate(-6px, -10px) scale(1.6); }
  .train-meter-fill { transition: none; }
}
