:root {
  --bg: #f5f7fa;
  --card-bg: #ffffff;
  --text: #1a2332;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --primary: #b91c1c;
  --primary-dark: #7f1d1d;
  --accent: #0f766e;
  --pill-bg: #f1f5f9;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1520;
    --card-bg: #1a2333;
    --text: #e6edf3;
    --text-muted: #94a3b8;
    --border: #2d3a4f;
    --primary: #ef4444;
    --primary-dark: #f87171;
    --accent: #2dd4bf;
    --pill-bg: #22304a;
    --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  }
}

* { box-sizing: border-box; }

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

.topbar {
  text-align: center;
  padding: 2rem 1rem 1rem;
}
.topbar h1 {
  margin: 0;
  font-size: 1.6rem;
}
.train-emoji { font-size: 1.4rem; }
.subtitle {
  color: var(--text-muted);
  margin: 0.4rem 0 0;
  font-size: 0.95rem;
}

.container {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 1rem 3rem;
}

.search-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.station-row {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
}
.station-field { flex: 1; min-width: 0; }
.station-field label, .option-field label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.3rem;
}

.autocomplete { position: relative; }

input[type="text"], select {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}
input[type="text"]:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: 260px;
  overflow-y: auto;
  z-index: 20;
  display: none;
}
.suggestions.open { display: block; }
.suggestion-item {
  padding: 0.55rem 0.7rem;
  cursor: pointer;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.suggestion-item:last-child { border-bottom: none; }
.suggestion-item:hover, .suggestion-item.active {
  background: var(--pill-bg);
}
.suggestion-item .s-en {
  color: var(--text-muted);
  font-size: 0.78rem;
  display: block;
}

.swap-btn {
  border: 1px solid var(--border);
  background: var(--pill-bg);
  color: var(--text);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
}
.swap-btn:hover { background: var(--border); }

.options-row {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}
.option-field { flex: 1; min-width: 140px; }

.search-btn {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.4rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}
.search-btn:hover { background: var(--primary-dark); }
.search-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.optimize-toggle {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}
.switch {
  flex-shrink: 0;
  cursor: pointer;
  display: inline-block;
}
.switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.switch-track {
  display: block;
  width: 42px;
  height: 24px;
  background: var(--border);
  border-radius: 999px;
  position: relative;
  transition: background 0.15s ease;
}
.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
  transition: transform 0.15s ease;
}
.switch input:checked + .switch-track {
  background: var(--accent);
}
.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(18px);
}
.switch input:focus-visible + .switch-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.optimize-text { min-width: 0; }
.optimize-title {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.optimize-hint {
  margin: 0.15rem 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

#advancedOptions {
  overflow: hidden;
  max-height: 100px;
  opacity: 1;
  transition: max-height 0.2s ease, opacity 0.2s ease, margin-top 0.2s ease;
}
#advancedOptions.collapsed {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
}

#resultsSection { margin-top: 1.5rem; }

.status-msg {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 1rem;
}
.status-msg.error { color: var(--primary); }

.itinerary {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.1rem;
  margin-bottom: 0.9rem;
  box-shadow: var(--shadow);
}
.itinerary-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 0.7rem;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.itinerary-duration {
  font-size: 1.15rem;
  font-weight: 700;
}
.itinerary-meta {
  font-size: 0.82rem;
  color: var(--text-muted);
}
.transfer-pill {
  display: inline-block;
  background: var(--pill-bg);
  color: var(--text);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  margin-left: 0.4rem;
}

.leg {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0;
  font-size: 0.9rem;
}
.leg + .leg { border-top: 1px dashed var(--border); }
.leg-train-group {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
}
.leg-train {
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}
.type-badge {
  display: inline-block;
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-size: 0.68rem;
  font-weight: 600;
  white-space: nowrap;
  color: #fff;
}
.type-special    { background: #b91c1c; }
.type-express    { background: #ea580c; }
.type-rapid      { background: #2563eb; }
.type-ordinary   { background: #0f766e; }
.type-local      { background: #64748b; }
.type-suburban   { background: #7c3aed; }
.type-tourist    { background: #db2777; }
.type-feeder     { background: #059669; }
.type-feeder_dm  { background: #059669; }
.type-default    { background: #64748b; }
.leg-train-walk { background: var(--text-muted); }
.leg-station { min-width: 0; }
.leg-station .st-name { font-weight: 600; }
.leg-station .st-time { color: var(--text-muted); font-size: 0.8rem; }
.leg-arrow { color: var(--text-muted); }

.wait-row {
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  padding: 0.25rem 0;
  background: var(--pill-bg);
  border-radius: 6px;
  margin: 0.2rem 0;
}

.footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  padding: 1rem 1rem 2rem;
}
.footer a { color: var(--accent); }
.footer-copyright { margin-top: 0.4rem; opacity: 0.7; }

@media (max-width: 520px) {
  .station-row { flex-direction: column; align-items: stretch; }
  .swap-btn { align-self: center; transform: rotate(90deg); }
  .leg { grid-template-columns: auto 1fr; grid-template-rows: auto auto; }
  .leg-arrow { display: none; }
}
