:root {
  color-scheme: dark;
  --bg: #111;
  --panel: #1f1f1f;
  --panel-2: #292929;
  --panel-3: #333;
  --border: #444;
  --text: #f1f1f1;
  --muted: #aaa;
  --accent: #8ab4ff;
  --ok: #5cff8d;
  --warn: #ffcc66;
  --danger: #ff6666;
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  min-height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
select,
input {
  font: inherit;
}

a {
  color: inherit;
}

.setup-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.setup-card {
  width: min(760px, 100%);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--panel);
}

.kicker {
  color: var(--accent);
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.8rem;
}

h1 {
  margin: 8px 0 16px;
  font-size: clamp(2.5rem, 7vw, 5.2rem);
  line-height: 0.95;
}

p {
  color: var(--muted);
  line-height: 1.55;
}

.setup-grid {
  display: grid;
  gap: 16px;
  margin-top: 24px;
}

.field {
  display: grid;
  gap: 6px;
}

label {
  color: var(--muted);
  font-size: 0.9rem;
}

select,
input {
  min-height: 46px;
  width: 100%;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 8px;
}

button,
.button {
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-3);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.primary {
  min-height: 46px;
  padding: 0 18px;
  background: #315f9f;
  border-color: #568ce0;
}

button:hover,
button:focus-visible,
.button:hover,
.button:focus-visible,
select:focus-visible {
  border-color: var(--accent);
  outline: 3px solid rgba(138, 180, 255, 0.25);
  outline-offset: 2px;
}

.app-view {
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(420px, 42vw) minmax(620px, 1fr);
  gap: 8px;
  padding: 8px;
  overflow: hidden;
}

.panel {
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr;
}

.panel-header {
  min-height: 54px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}

.panel-title {
  min-width: 0;
}

.panel-title strong {
  display: block;
  font-size: 1rem;
}

.panel-title span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.panel-actions a,
.panel-actions button {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  font-size: 0.9rem;
}

.map-wrap {
  min-height: 0;
  position: relative;
}

#map {
  height: 100%;
  width: 100%;
  background: #d7d7d7;
}

.map-empty {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

.board-wrap {
  min-height: 0;
  overflow: auto;
}

.edr-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.94rem;
}

.edr-table th,
.edr-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
}

.edr-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #111;
  color: #f2f2f2;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.edr-table tr {
  background: #242424;
}

.edr-table tr:nth-child(even) {
  background: #2b2b2b;
}

.edr-table tr.past {
  opacity: 0.38;
}

.edr-table tr.current {
  background: #375f91;
  opacity: 1;
}

.edr-table tr.current td:first-child {
  box-shadow: inset 5px 0 0 var(--accent);
}

.target-edr th:nth-child(1) { width: 150px; }
.target-edr th:nth-child(2) { width: 72px; }
.target-edr th:nth-child(3) { width: auto; }
.target-edr th:nth-child(4),
.target-edr th:nth-child(5) { width: 150px; }
.target-edr th:nth-child(6) { width: 110px; }
.target-edr th:nth-child(7) { width: 120px; }

.train-main-cell strong {
  display: block;
  font-size: 1.02rem;
}

.train-main-cell span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

.type-badge {
  display: inline-flex;
  min-width: 52px;
  justify-content: center;
  padding: 5px 8px;
  border-radius: 8px;
  background: #49e870;
  color: #06130a;
  font-weight: 900;
  font-size: 0.82rem;
}

.type-badge.freight {
  background: #ffeb3b;
  color: #1c1600;
}

.type-badge.express {
  background: #3f8bd8;
  color: #001226;
}

.type-badge.service {
  background: #ddd;
  color: #111;
}

.local-relation-cell strong {
  display: block;
  font-size: 1rem;
}

.local-relation-cell span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.82rem;
}

.time-block {
  display: grid;
  gap: 2px;
}

.time-line {
  display: grid;
  grid-template-columns: 42px 52px 34px;
  gap: 5px;
  align-items: baseline;
}

.time-label {
  color: #e0e0e0;
  font-style: italic;
}

.time-line strong {
  font-size: 1.08rem;
}

.delay-plus {
  color: #ff8e8e;
  font-weight: 900;
}

.delay-minus {
  color: #9cb6ff;
  font-weight: 900;
}

.delay-zero {
  color: #52d36f;
  font-weight: 900;
}

.stop-block {
  display: grid;
  gap: 3px;
  text-align: center;
}

.stop-block strong {
  font-size: 1rem;
}

.stop-block span {
  color: #eee;
}

.driver-cell {
  font-size: 1.02rem;
  text-align: center;
}

.loading,
.error {
  padding: 22px;
  color: var(--muted);
}

.error {
  color: var(--danger);
}

.leaflet-tooltip {
  font-size: 0.78rem;
  font-weight: 750;
  border: 1px solid rgba(0,0,0,0.25);
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

.train-map-marker {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 7px 3px 4px;
  border: 2px solid #111;
  border-radius: 999px;
  background: #5cff8d;
  color: #041106;
  box-shadow: 0 2px 10px rgba(0,0,0,0.38);
  font-size: 0.76rem;
  font-weight: 900;
  white-space: nowrap;
}

.train-map-marker.slow {
  background: #ffcc66;
  color: #211500;
}

.train-map-marker.stopped {
  background: #ff6666;
  color: #fff;
}

.train-map-arrow {
  font-size: 0.85rem;
}

.train-map-label {
  line-height: 1;
}

.tv {
  overflow: hidden;
}

.tv .app-view {
  font-size: 1.1rem;
  grid-template-columns: minmax(520px, 40vw) minmax(720px, 1fr);
  gap: 10px;
  padding: 10px;
}

.tv .pc-only {
  display: none;
}

.tv .panel-header {
  min-height: 64px;
}

.tv .panel-title strong {
  font-size: 1.25rem;
}

.tv .panel-title span {
  font-size: 0.98rem;
}

.tv .edr-table {
  font-size: 1.04rem;
}

.tv .leaflet-tile {
  filter: grayscale(0.25) contrast(1.18) brightness(0.9);
}

.tv .leaflet-container {
  background: #777;
}

.tv .leaflet-tooltip {
  font-size: 0.92rem;
  font-weight: 900;
  color: #fff;
  background: rgba(0, 0, 0, 0.82);
  border: 2px solid rgba(255, 255, 255, 0.75);
  text-shadow: 0 1px 2px #000;
}

.tv-control-panel {
  position: fixed;
  top: 12px;
  left: 50%;
  z-index: 9999;
  transform: translateX(-50%) translateY(-18px);
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 24px);
  padding: 10px 12px;
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 16px;
  background: rgba(10, 10, 10, 0.86);
  color: var(--text);
  box-shadow: 0 8px 32px rgba(0,0,0,0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.tv-controls-visible .tv-control-panel {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.tv-control-panel span {
  color: var(--muted);
  white-space: nowrap;
}

.tv-control-panel .button,
.tv-control-panel button {
  min-height: 42px;
  border-radius: 12px;
}

@media (max-width: 900px) {
  .app-view {
    height: auto;
    min-height: 100vh;
    grid-template-columns: 1fr;
    overflow: auto;
  }

  .panel {
    min-height: 70vh;
  }

  .target-edr th:nth-child(3) {
    min-width: 220px;
  }
}
