:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --surface: #ffffff;
  --surface-strong: #f0f4f3;
  --ink: #1d2524;
  --muted: #6a7471;
  --line: #dbe4e1;
  --accent: #087f5b;
  --accent-dark: #046346;
  --blue: #2563eb;
  --amber: #b7791f;
  --red: #b42318;
  --shadow: 0 18px 45px rgba(26, 39, 37, 0.09);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: wait;
  opacity: 0.62;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 76px;
  padding: 16px 28px;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.app-header h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
  font-weight: 760;
}

.app-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.header-actions,
.controls-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.app-shell {
  width: min(1440px, calc(100% - 36px));
  margin: 20px auto 40px;
}

.controls-panel,
.map-panel,
.routes-panel,
.stops-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.controls-panel {
  display: grid;
  grid-template-columns: minmax(320px, 1.1fr) minmax(460px, 1.5fr) auto;
  gap: 18px;
  align-items: end;
  padding: 18px;
}

.source-block label,
.settings-grid label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 720;
}

textarea {
  width: 100%;
  min-height: 148px;
  max-height: 280px;
  resize: vertical;
  padding: 12px;
  color: var(--ink);
  background: #fbfcfc;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
}

textarea:focus,
input:focus,
select:focus {
  border-color: rgba(8, 127, 91, 0.55);
  box-shadow: 0 0 0 3px rgba(8, 127, 91, 0.12);
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(120px, 1fr));
  gap: 12px;
}

input {
  width: 100%;
  height: 42px;
  padding: 0 11px;
  color: var(--ink);
  background: #fbfcfc;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
}

.settings-grid select {
  width: 100%;
  height: 42px;
  padding: 0 11px;
  color: var(--ink);
  background: #fbfcfc;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
}

.primary-button,
.ghost-button,
.plain-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 15px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 760;
  white-space: nowrap;
}

.primary-button {
  color: #fff;
  background: var(--accent);
}

.primary-button:hover {
  background: var(--accent-dark);
}

.ghost-button {
  color: var(--accent-dark);
  background: #e7f3ef;
}

.plain-button {
  color: var(--ink);
  background: var(--surface-strong);
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
  gap: 10px;
  margin: 14px 0;
}

.summary-strip div {
  min-width: 0;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.summary-strip span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 720;
}

.summary-strip strong {
  display: block;
  margin-top: 3px;
  font-size: 22px;
  line-height: 1.15;
}

.warnings {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
}

.warning-item {
  padding: 11px 13px;
  color: #623b04;
  background: #fff7e6;
  border: 1px solid #f5d48b;
  border-radius: var(--radius);
  font-size: 13px;
}

.warning-item.critical {
  color: var(--red);
  background: #fff1f0;
  border-color: #ffccc7;
}

.planning-grid {
  display: grid;
  grid-template-columns: minmax(480px, 1.1fr) minmax(420px, 0.9fr);
  gap: 14px;
  align-items: start;
}

.map-panel,
.routes-panel,
.stops-section {
  min-width: 0;
  overflow: hidden;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.route-panel-title {
  align-items: flex-start;
  flex-wrap: wrap;
}

.panel-title-main {
  display: grid;
  gap: 4px;
}

.panel-title h2 {
  margin: 0;
  font-size: 16px;
  line-height: 1.2;
}

.panel-title span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.route-filters {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.filter-chip {
  min-height: 30px;
  padding: 0 10px;
  color: var(--muted);
  background: #eef3f1;
  border: 1px solid transparent;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 780;
}

.filter-chip:not([data-filter-rider="all"]) {
  color: var(--route-color);
  background: color-mix(in srgb, var(--route-color) 9%, #ffffff);
}

.filter-chip.active {
  color: #ffffff;
  background: var(--ink);
}

.filter-chip.active:not([data-filter-rider="all"]) {
  background: var(--route-color);
}

.route-map {
  min-height: 610px;
  background: #dfe8e5;
}

.route-map svg.fallback-route-svg {
  display: block;
  width: 100%;
  height: 610px;
}

.leaflet-container {
  width: 100%;
  min-height: 610px;
  height: 610px;
  font: inherit;
  background: #dfe8e5;
}

.leaflet-control-attribution {
  font-size: 11px;
}

.map-stop-marker,
.map-depot-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  box-shadow: 0 8px 20px rgba(20, 30, 28, 0.24);
  font-size: 11px;
  font-weight: 900;
}

.map-stop-marker {
  width: var(--marker-width, 32px);
  height: var(--marker-height, 32px);
  color: var(--marker-color);
  background: #fff;
  border: 3px solid var(--marker-color);
  line-height: 1;
  white-space: nowrap;
}

.map-stop-marker.is-muted {
  opacity: 0.28;
  filter: grayscale(0.45);
}

.map-depot-marker {
  width: 38px;
  height: 38px;
  color: #fff;
  background: #17201f;
  border: 3px solid #fff;
}

.leaflet-popup-content {
  min-width: 190px;
  margin: 10px 12px;
  color: var(--ink);
  font-size: 13px;
}

.map-popup-title {
  display: block;
  margin-bottom: 4px;
  font-weight: 800;
}

.map-popup-meta {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.map-popup-address {
  display: block;
  margin-top: 4px;
  line-height: 1.35;
}

.map-popup-assignment {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  padding-top: 9px;
  border-top: 1px solid var(--line);
}

.map-popup-assignment > span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 780;
}

.map-popup-assignment .assign-control select {
  width: 100%;
}

.routes-panel {
  max-height: 690px;
}

#routes {
  display: grid;
  gap: 0;
  max-height: 634px;
  overflow: auto;
}

.route-card {
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.route-card:last-child {
  border-bottom: 0;
}

.route-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.rider-name {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0;
  font-size: 16px;
}

.route-dot {
  width: 12px;
  height: 12px;
  flex: 0 0 12px;
  border-radius: 999px;
  background: var(--route-color);
}

.route-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 10px;
}

.chip {
  padding: 5px 8px;
  color: #263231;
  background: #eef3f1;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 720;
}

.chip.warn {
  color: #7c4700;
  background: #fff1cc;
}

.nav-link {
  color: var(--blue);
  font-size: 13px;
  font-weight: 760;
  text-decoration: none;
  white-space: nowrap;
}

.nav-link:hover {
  text-decoration: underline;
}

.stop-list {
  display: grid;
  gap: 8px;
  margin: 14px 0 0;
  padding: 0;
  list-style: none;
}

.stop-row {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) auto minmax(112px, auto);
  gap: 10px;
  align-items: start;
  padding: 10px 0;
  border-top: 1px solid #edf2f0;
}

.stop-index {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  color: #fff;
  background: var(--route-color);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
}

.stop-main strong {
  display: block;
  font-size: 13px;
  line-height: 1.35;
}

.stop-main span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.eta {
  color: var(--accent-dark);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}

.stop-actions {
  display: flex;
  justify-content: flex-end;
}

.assign-control {
  display: block;
}

.assign-control select {
  width: 112px;
  height: 32px;
  padding: 0 8px;
  color: var(--ink);
  background: #fbfcfc;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12px;
  font-weight: 720;
}

.assign-control select:focus {
  border-color: rgba(8, 127, 91, 0.55);
  box-shadow: 0 0 0 3px rgba(8, 127, 91, 0.12);
  outline: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.stops-section {
  margin-top: 14px;
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 11px 12px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid #edf2f0;
  font-size: 13px;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  color: var(--muted);
  background: #fbfcfc;
  font-size: 12px;
}

td.route-color-cell {
  font-weight: 800;
}

.empty-state {
  padding: 24px;
  color: var(--muted);
}

@media (max-width: 1100px) {
  .controls-panel,
  .planning-grid {
    grid-template-columns: 1fr;
  }

  .controls-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 760px) {
  .app-header {
    position: static;
    flex-direction: column;
    align-items: stretch;
    padding: 16px;
  }

  .header-actions {
    width: 100%;
  }

  .header-actions button,
  .controls-actions button {
    flex: 1;
  }

  .app-shell {
    width: min(100% - 20px, 720px);
    margin-top: 10px;
  }

  .settings-grid,
  .summary-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .route-map,
  .route-map svg.fallback-route-svg,
  .leaflet-container {
    min-height: 420px;
    height: 420px;
  }

  .stop-row {
    grid-template-columns: 34px minmax(0, 1fr);
  }

  .eta,
  .stop-actions {
    grid-column: 2;
  }

  .stop-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 480px) {
  .settings-grid,
  .summary-strip {
    grid-template-columns: 1fr;
  }
}
