:root {
  color-scheme: light;
  --bg: #f4f6f7;
  --paper: #fff;
  --ink: #14212b;
  --quiet: #66757f;
  --faint: #98a4ac;
  --line: #e3e8eb;
  --field: #f6f8f9;
  --accent: #0a7c66;
  --accent-wash: #e6f4ef;
  --up: #c2410c;
  --down: #0a7c66;
  --radius: 10px;
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
h1, h2, h3, h4, p, dl, dd { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button, select, summary { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.num, input[type="number"] { font-variant-numeric: tabular-nums; }

.shell { width: min(68rem, calc(100% - 2rem)); margin-inline: auto; }

/* Top bar */
.topbar { background: var(--paper); border-bottom: 1px solid var(--line); }
.topbar-inner { display: flex; align-items: center; flex-wrap: wrap; gap: .4rem .75rem; min-height: 3.25rem; padding-block: .5rem; }
h1 { font-size: 1.1rem; font-weight: 700; letter-spacing: -.01em; }
.tag { font-size: .75rem; font-weight: 600; color: var(--accent); background: var(--accent-wash); padding: .12rem .5rem; border-radius: 99px; }
.topbar-note { margin-left: auto; font-size: .8125rem; color: var(--quiet); }
.topbar-note s { color: var(--faint); }
.topbar-note b { color: var(--up); font-weight: 650; }

/* Layout */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 21rem; gap: .875rem; align-items: start; padding-top: .875rem; }
.col-input { display: grid; gap: .875rem; min-width: 0; }
.col-result { position: sticky; top: .875rem; min-width: 0; }
.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; min-width: 0; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; }
h2 { font-size: 1rem; font-weight: 700; }
.ride-summary { font-size: .8125rem; color: var(--quiet); font-variant-numeric: tabular-nums; }
.hint { display: block; color: var(--faint); font-size: .8125rem; margin-top: .2rem; }

/* Inputs */
label { font-size: .75rem; color: var(--quiet); font-weight: 500; }
.field label, .station-field label { display: block; margin-bottom: .2rem; }
input, select {
  width: 100%;
  min-width: 0;
  height: 2.375rem;
  padding: 0 .65rem;
  border: 1px solid transparent;
  border-radius: 7px;
  background: var(--field);
  transition: border-color .12s, background-color .12s;
}
input:hover, select:hover { border-color: var(--line); }
input:focus, select:focus { outline: none; border-color: var(--accent); background-color: #fff; }
input::placeholder { color: var(--faint); font-weight: 400; }
input[aria-invalid="true"] { border-color: #dc2626; background: #fef2f2; }
select {
  appearance: none;
  padding-right: 1.9rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--quiet) 50%), linear-gradient(135deg, var(--quiet) 50%, transparent 50%);
  background-position: calc(100% - .95rem) 52%, calc(100% - .68rem) 52%;
  background-size: .27rem .27rem;
  background-repeat: no-repeat;
}

/* Routes */
.routes { display: grid; margin-top: .6rem; }
.route { margin: 0; padding: .7rem 0; border: 0; border-top: 1px solid var(--line); min-width: 0; }
.route:first-child { border-top: 0; padding-top: .2rem; }
.route-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 2rem minmax(0, 1fr) 8.5rem 2rem;
  grid-template-areas: "origin swap destination rides remove";
  align-items: end;
  gap: .5rem;
}
.route.has-next-month .route-fields { grid-template-areas: "origin swap destination rides remove" ". . . monthly ."; }
.origin-field { grid-area: origin; }
.destination-field { grid-area: destination; }
.ride-field { grid-area: rides; }
.month-count { grid-area: monthly; }
.swap { grid-area: swap; }
.remove-route { grid-area: remove; }
.station-field { position: relative; min-width: 0; }
.station-control { position: relative; }
.station-control input { padding-left: 1.6rem; font-weight: 600; }
.stop { position: absolute; top: 50%; left: .65rem; width: .5rem; height: .5rem; transform: translateY(-50%); border-radius: 50%; pointer-events: none; }
.origin-stop { background: var(--accent); }
.destination-stop { border: 2px solid var(--accent); background: #fff; }
.ride-field input, .month-count input { text-align: center; font-weight: 600; padding: 0 .25rem; }
.count-control {
  display: grid;
  grid-template-columns: 2.25rem minmax(0, 1fr) 2.25rem;
  grid-template-areas: "minus number plus";
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--field);
}
.count-control:focus-within { border-color: var(--accent); }
.count-control input {
  grid-area: number;
  appearance: textfield;
  border: 0;
  background: var(--paper);
  border-radius: 0;
}
.count-control input::-webkit-inner-spin-button, .count-control input::-webkit-outer-spin-button { appearance: none; margin: 0; }
.count-step {
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--accent);
  font-size: 1.125rem;
  line-height: 1;
}
.count-step:hover:not(:disabled) { background: var(--accent-wash); }
.count-step:disabled { color: var(--faint); cursor: default; }
.count-down { grid-area: minus; }
.count-up { grid-area: plus; }
.swap, .remove-route {
  height: 2.375rem;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--faint);
  font-size: 1rem;
  line-height: 1;
}
.swap:hover { color: var(--accent); background: var(--accent-wash); }
.remove-route { font-size: .8rem; }
.remove-route:hover { color: #dc2626; background: #fef2f2; }

.route-prices { display: flex; align-items: baseline; flex-wrap: wrap; gap: .1rem .45rem; margin-top: .45rem; font-size: .8125rem; color: var(--quiet); font-variant-numeric: tabular-nums; }
.fare-old strong { color: var(--quiet); font-weight: 500; }
.fare-arrow { color: var(--faint); }
.fare-new strong { color: var(--ink); font-size: 1rem; font-weight: 700; }
.fare-diff { font-weight: 600; padding: 0 .4rem; border-radius: 4px; background: var(--field); }
.fare-diff.extra { color: var(--up); background: #fff1ea; }
.fare-diff.saving { color: var(--down); background: var(--accent-wash); }
.route-distance { margin-left: auto; color: var(--faint); }
.fare-estimate-note { flex-basis: 100%; margin-top: .25rem; padding: .3rem .5rem; border-radius: 4px; background: #fff1ea; color: #9a3412; }

.add-route { margin-top: .35rem; width: 100%; height: 2.25rem; border: 1px dashed #cfd7dc; border-radius: 7px; background: transparent; color: var(--quiet); font-size: .8125rem; font-weight: 600; }
.add-route:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); background: var(--accent-wash); }
.add-route:disabled { opacity: .5; cursor: default; }

/* Settings */
.settings { padding-block: .75rem; }
.settings-row { display: flex; align-items: center; gap: .75rem; }
.settings-row label { flex: none; font-size: .875rem; color: var(--ink); font-weight: 600; }
.settings-row select { max-width: 16rem; }
summary { width: fit-content; color: var(--accent); font-size: .8125rem; font-weight: 600; margin-top: .55rem; }
.monthly-fields { display: flex; align-items: end; flex-wrap: wrap; gap: .5rem 1rem; margin-top: .5rem; }
.monthly-fields .field { width: 9rem; }
.checkbox-field { height: 2.375rem; display: flex; align-items: center; gap: .4rem; font-size: .8125rem; color: var(--ink); }
.checkbox-field input { width: 1rem; height: 1rem; padding: 0; margin: 0; accent-color: var(--accent); }
.amount-control { position: relative; }
.amount-control span { position: absolute; left: .65rem; top: 50%; transform: translateY(-50%); color: var(--faint); pointer-events: none; }
.amount-control input { padding-left: 1.5rem; }
.amount-control input:disabled { color: var(--faint); }
.error { margin-top: .35rem; color: #dc2626; font-size: .8125rem; }

/* Result */
.panel.result { padding: 0; overflow: hidden; }
.best { padding: 1rem 1rem .9rem; background: var(--ink); color: #fff; }
.best h3 { font-size: .8125rem; font-weight: 600; color: #b9c6cf; }
.best-cost { font-size: 2.5rem; font-weight: 750; line-height: 1.1; letter-spacing: -.03em; font-variant-numeric: tabular-nums; margin-top: .15rem; }
.best-detail { margin-top: .25rem; font-size: .875rem; font-weight: 600; color: #6ee7b7; }

.compare {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: .5rem 1rem;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.compare dt { font-size: .875rem; color: var(--quiet); }
.compare-total dd { margin-top: .15rem; font-size: 1.2rem; font-weight: 700; overflow-wrap: anywhere; }
.compare-total + .compare-total { border-left: 1px solid var(--line); padding-left: 1rem; }
.compare-discount { grid-column: 1 / -1; display: flex; justify-content: space-between; gap: .5rem; }
.compare-discount dd { font-size: .875rem; color: var(--accent); overflow-wrap: anywhere; text-align: right; }

.plans-title { font-size: .75rem; font-weight: 600; color: var(--quiet); padding: .65rem 1rem .25rem; }
.plans { list-style: none; margin: 0; padding: 0 1rem; font-variant-numeric: tabular-nums; }
.plan { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding: .5rem 0; }
.plan + .plan { border-top: 1px solid var(--line); }
.plan.is-best { margin-inline: -.5rem; padding-inline: .5rem; border-radius: 7px; background: var(--accent-wash); border-top-color: transparent; }
.plan.is-best + .plan { border-top-color: transparent; }
.plan h4 { display: flex; align-items: center; gap: .35rem; font-size: .875rem; font-weight: 650; }
.plan-badge { font-size: .6875rem; font-weight: 700; color: #fff; background: var(--accent); padding: 0 .35rem; border-radius: 4px; line-height: 1.5; }
.plan-use { font-size: .75rem; color: var(--faint); }
.plan-num { text-align: right; flex: none; }
.plan-num strong { font-size: .95rem; font-weight: 700; }
.plan.is-best .plan-num strong { color: var(--accent); }
.plan-savings { font-size: .75rem; color: var(--faint); }
.result .hint { padding: .35rem 1rem .7rem; margin: 0; }
.saving { color: var(--down); }
.extra { color: var(--up); }
.cost-empty { padding: 2.5rem 1rem; text-align: center; color: var(--faint); font-size: .875rem; }

/* Share button & modal */
.share-bar { padding: 0 1rem .9rem; }
.share-btn {
  width: 100%;
  height: 2.375rem;
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: #fff;
  font-size: .875rem;
  font-weight: 600;
  transition: background-color .12s, opacity .12s;
}
.share-btn:hover:not(:disabled) { background: #086654; }
.share-btn:disabled { opacity: .45; cursor: default; }

.share-dialog {
  margin: auto;
  width: min(26rem, calc(100% - 1.5rem));
  max-width: none;
  max-height: calc(100dvh - 2rem);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 20px 50px rgb(20 33 43 / 24%);
  overflow: hidden;
}
.share-dialog::backdrop {
  background: rgb(20 33 43 / 52%);
  backdrop-filter: blur(2px);
}
.share-dialog-inner {
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 2rem);
}
.share-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.share-dialog-head h2 { font-size: .95rem; font-weight: 700; }
.share-close {
  width: 1.85rem;
  height: 1.85rem;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--quiet);
  font-size: .9rem;
  line-height: 1;
}
.share-close:hover { background: var(--field); color: var(--ink); }
.share-preview-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: .75rem;
  background: var(--bg);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}
#share-preview {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgb(20 33 43 / 10%);
}
.share-dialog-foot {
  flex: none;
  padding: .75rem 1rem .85rem;
  border-top: 1px solid var(--line);
  background: var(--paper);
}
.share-tip {
  font-size: .75rem;
  color: var(--faint);
  text-align: center;
  margin-bottom: .55rem;
}
.share-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
}
.share-link-row {
  display: flex;
  gap: .5rem;
  margin-top: .55rem;
}
.share-link-row input { flex: 1 1 auto; text-overflow: ellipsis; font-size: .875rem; }
.share-link-row button { flex: none; }
.btn-primary, .btn-secondary {
  flex: 1 1 0;
  height: 2.375rem;
  padding: 0 .75rem;
  border-radius: 7px;
  font-size: .875rem;
  font-weight: 600;
  white-space: nowrap;
}
.btn-primary { border: 0; background: var(--accent); color: #fff; }
.btn-primary:hover { background: #086654; }
.btn-secondary { border: 1px solid var(--line); background: var(--field); color: var(--ink); }
.btn-secondary:hover { background: #ecf0f2; }
.share-status {
  min-height: 1.1rem;
  margin-top: .45rem;
  font-size: .75rem;
  color: var(--accent);
  text-align: center;
}
.share-status:empty { display: none; }

/* Footer */
.footer { display: flex; justify-content: space-between; align-items: flex-start; gap: .5rem 1rem; flex-wrap: wrap; padding-block: 1rem 2rem; color: var(--faint); font-size: .75rem; }
.footer summary { margin-top: 0; color: var(--quiet); font-size: .75rem; }
.rules { flex: 1 1 30rem; }
.rules-body { display: grid; gap: .4rem; max-width: 46rem; margin-top: .4rem; color: var(--quiet); font-size: .8125rem; line-height: 1.6; }

.skip-link { position: absolute; z-index: 6; top: .5rem; left: .5rem; padding: .5rem; background: #fff; transform: translateY(-200%); }
.skip-link:focus { transform: translateY(0); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }

.suggestions {
  position: absolute; z-index: 5; left: 0; right: 0; top: calc(100% + .25rem);
  margin: 0; padding: .25rem; list-style: none; max-height: 16rem; overflow: auto;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgb(20 33 43 / 12%);
}
.suggestions li { display: flex; align-items: center; justify-content: space-between; gap: .1rem .5rem; flex-wrap: wrap; padding: .4rem .5rem; border-radius: 5px; font-size: .875rem; cursor: pointer; }
.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--accent-wash); }
.suggestions li.no-matches { cursor: default; color: var(--faint); }
.suggestions li small { color: var(--faint); font-size: .75rem; }

@media (max-width: 56rem) {
  .layout { grid-template-columns: 1fr; }
  /* Mobile order: trips → result → payment settings */
  .col-input { display: contents; }
  #fare { order: 1; }
  .col-result { position: static; order: 2; }
  .settings { order: 3; }
  .topbar-note { margin-left: 0; width: 100%; }
}
@media (max-width: 34rem) {
  .shell { width: calc(100% - 1.25rem); }
  .panel { padding: .85rem; }
  .route-fields {
    grid-template-columns: minmax(0, 1fr) 5.5rem 2.75rem;
    grid-template-areas: "origin origin swap" "destination destination remove" "rides rides rides";
  }
  .route.has-next-month .route-fields {
    grid-template-areas: "origin origin swap" "destination destination remove" "rides monthly monthly";
  }
  .ride-field { width: min(100%, 9.5rem); }
  .suggestions { max-height: min(13rem, 40dvh); }
  .settings-row select { max-width: none; }
  .best-cost { font-size: 2.125rem; }
}
@media (pointer: coarse) {
  input, select, .swap, .remove-route { height: 2.75rem; font-size: 16px; }
  .count-control { grid-template-columns: 2.65rem minmax(0, 1fr) 2.65rem; }
  .suggestions li { min-height: 2.75rem; }
  .share-actions button, .share-link-row button { height: 2.75rem; }
}
