:root {
  color-scheme: dark;
  --background: #101827;
  --surface: #192538;
  --surface-light: #22334b;
  --border: #31445e;
  --text: #eef4fb;
  --muted: #9aacc2;
  --blue: #39758c;
  --blue-light: #5bb1d0;
  --green: #2e9d70;
  --red: #e74c3c;
  --yellow: #e5ad45;
  --track: #aab7c7;
  --track-added: #5bb1d0;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--background);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 2px solid var(--blue-light);
  outline-offset: 3px;
}

[hidden] {
  display: none !important;
}

.app {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
  padding: 14px 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  background: var(--surface-light);
  border-radius: 11px;
  font-size: 22px;
}

.brand-title {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.brand-subtitle {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.phase-badge,
.live-badge {
  padding: 7px 11px;
  background: #293f57;
  border: 1px solid #405a76;
  border-radius: 20px;
  color: #bdeaff;
  font-size: 11px;
  font-weight: 800;
}

.live-badge {
  background: #173e34;
  border-color: #286a54;
  color: #71e2b4;
}

.main-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  align-items: start;
  gap: 20px;
}

.board-card,
.control-card {
  min-width: 0;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.eyebrow {
  margin-bottom: 6px;
  color: var(--blue-light);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.15em;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 7px;
  font-size: clamp(20px, 3vw, 27px);
}

h2 {
  margin-bottom: 0;
  font-size: 18px;
}

h3 {
  font-size: 13px;
}

.section-heading p,
.panel-description {
  margin-bottom: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
}

.board-wrapper {
  overflow: hidden;
  width: 100%;
  background: #142235;
  border: 1px solid #31445e;
  border-radius: 12px;
}

#trackBoard {
  display: block;
  width: 100%;
  height: auto;
  touch-action: manipulation;
}

.board-grid {
  fill: #344760;
  opacity: 0.55;
}

.grid-dot {
  fill: #70849d;
  stroke: #142235;
  stroke-width: 2;
  cursor: pointer;
  transition: fill 0.12s, r 0.12s;
}

.grid-dot.connected {
  fill: #aebed0;
}

.grid-dot.selected {
  fill: var(--yellow);
  stroke: #fff1c7;
  stroke-width: 4;
}

.grid-dot:hover {
  fill: var(--yellow);
}

.track-line {
  fill: none;
  stroke: var(--track);
  stroke-width: 8;
  stroke-linecap: round;
  cursor: pointer;
}

.track-line.added-track {
  stroke: var(--track-added);
}

.track-line.signal-target {
  cursor: crosshair;
}

.station-box {
  fill: #223852;
  stroke: #85a8c6;
  stroke-width: 2;
}

.station-label {
  fill: #e6f0fb;
  font-size: 13px;
  font-weight: 800;
  text-anchor: middle;
}

.switch-marker {
  cursor: pointer;
}

.switch-marker circle {
  fill: #e5ad45;
  stroke: #fff0c3;
  stroke-width: 2;
}

.switch-marker text {
  fill: #182235;
  font-size: 12px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.signal-marker {
  cursor: pointer;
}

.signal-marker circle {
  stroke: #eaf0f7;
  stroke-width: 2;
}

.signal-marker.red circle {
  fill: var(--red);
}

.signal-marker.green circle {
  fill: var(--green);
}

.signal-marker text {
  fill: white;
  font-size: 9px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.train-marker circle {
  stroke: #ffffff;
  stroke-width: 2;
}

.train-marker.train-a circle {
  fill: #397fc2;
}

.train-marker.train-b circle {
  fill: #cf7a35;
}

.train-marker text {
  fill: white;
  font-size: 12px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 13px;
  padding: 13px 3px 4px;
  color: var(--muted);
  font-size: 10px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-line {
  display: inline-block;
  width: 20px;
  height: 4px;
  background: var(--track);
  border-radius: 4px;
}

.legend-added {
  background: var(--track-added);
}

.legend-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
}

.legend-red {
  background: var(--red);
}

.legend-green {
  background: var(--green);
}

.message {
  margin-top: 12px;
  padding: 11px 12px;
  background: #182f43;
  border: 1px solid #2d536d;
  border-radius: 9px;
  color: #c3eaff;
  font-size: 12px;
  line-height: 1.6;
}

.message.error {
  background: #3b242b;
  border-color: #75404a;
  color: #ffbdc4;
}

.message.success {
  background: #19372f;
  border-color: #326c58;
  color: #9be9c8;
}

.control-column {
  display: grid;
  gap: 16px;
}

.control-card {
  padding: 17px;
}

.card-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}

.tool-list {
  display: grid;
  gap: 8px;
  margin: 15px 0;
}

.tool-button {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 11px;
  background: #1b2b40;
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.tool-button.active {
  background: #1c4053;
  border-color: var(--blue-light);
}

.tool-icon {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  background: #2b4058;
  border-radius: 8px;
  font-size: 19px;
}

.tool-button strong,
.tool-button small {
  display: block;
}

.tool-button strong {
  margin-bottom: 3px;
  font-size: 12px;
}

.tool-button small {
  color: var(--muted);
  font-size: 10px;
}

.selected-info {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 15px 0;
  padding: 10px;
  background: #142235;
  border-radius: 8px;
  color: var(--muted);
  font-size: 11px;
}

.selected-info strong {
  color: var(--text);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  margin-bottom: 15px;
}

.stat-box {
  padding: 10px 5px;
  background: #142235;
  border: 1px solid #293d55;
  border-radius: 8px;
  text-align: center;
}

.stat-box span,
.stat-box strong {
  display: block;
}

.stat-box span {
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 10px;
}

.stat-box strong {
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.stage-rules {
  margin: 14px 0;
  padding: 12px;
  background: #142235;
  border-radius: 9px;
}

.stage-rules h3 {
  margin-bottom: 9px;
}

.stage-rules ul {
  margin: 0;
  padding-left: 18px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.9;
}

.button {
  min-height: 42px;
  padding: 10px 14px;
  border: 1px solid transparent;
  border-radius: 9px;
  color: white;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  transition: filter 0.15s, transform 0.15s;
}

.button:hover {
  filter: brightness(1.12);
}

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

.button-primary {
  background: var(--blue);
  border-color: #5b91a8;
}

.button-secondary {
  background: #30435b;
  border-color: #455d78;
}

.button-muted {
  background: #29384c;
  border-color: #40516a;
  color: #d2dce8;
}

.button-small {
  min-height: 34px;
  padding: 7px 11px;
  font-size: 11px;
}

.button-full {
  display: block;
  width: 100%;
  margin-top: 9px;
}

.subheading {
  margin: 18px 0 8px;
  color: #c9d8e8;
}

.operation-controls {
  display: grid;
  gap: 7px;
}

.operation-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 10px;
  background: #142235;
  border: 1px solid #2e425a;
  border-radius: 8px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.operation-control span {
  font-size: 11px;
  font-weight: 700;
}

.control-state {
  padding: 5px 8px;
  background: #30435b;
  border-radius: 20px;
  color: #d8e5f2;
  font-size: 10px;
  font-weight: 800;
}

.control-state.red {
  background: #612e36;
  color: #ffc0c7;
}

.control-state.green {
  background: #205743;
  color: #a8f0d0;
}

.operation-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 17px;
}

.operation-buttons .button {
  padding: 10px 7px;
}

.train-status {
  display: grid;
  gap: 7px;
  margin-top: 17px;
}

.train-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  background: #142235;
  border-radius: 8px;
  font-size: 11px;
}

.train-name {
  font-weight: 800;
}

.train-detail {
  color: var(--muted);
  font-size: 10px;
}

.operation-stats {
  margin-top: 12px;
  color: var(--muted);
  font-size: 10px;
  text-align: right;
}

.result-card h2 {
  margin-bottom: 15px;
}

.result-title {
  margin-bottom: 13px;
  color: #a8f0d0;
  font-size: 22px;
  font-weight: 900;
}

.result-description {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
}

.result-metrics {
  display: grid;
  gap: 8px;
  margin: 16px 0;
}

.result-metric {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background: #142235;
  border-radius: 8px;
  font-size: 11px;
}

.result-metric strong {
  color: #9be9c8;
}

.footer {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 20px 3px 5px;
  color: #71849c;
  font-size: 10px;
  letter-spacing: 0.04em;
}

@media (max-width: 850px) {
  .app {
    padding: 10px;
  }

  .main-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .control-column {
    grid-template-columns: minmax(0, 1fr);
  }

  .board-card,
  .control-card {
    padding: 13px;
  }
}

@media (max-width: 440px) {
  .topbar {
    padding: 11px;
  }

  .brand-title {
    font-size: 15px;
  }

  .brand-subtitle {
    font-size: 10px;
  }

  .phase-badge {
    padding: 6px 8px;
    font-size: 9px;
  }

  .topbar-actions {
    gap: 6px;
  }

  .legend {
    gap: 9px;
  }

  .legend span {
    font-size: 9px;
  }

  .operation-buttons {
    grid-template-columns: 1fr;
  }
}