:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --ink: #1b2330;
  --muted: #667085;
  --line: #dfe4ec;
  --line-strong: #c8d0dc;
  --navy: #173a63;
  --navy-ink: #ffffff;
  --accent: #e6a417;
  --accent-soft: #fff4d9;
  --accent-ink: #7a5300;
  --good: #1d7a4f;
  --good-soft: #e3f4ea;
  --warn: #a4511a;
  --warn-soft: #fbeadf;
  --danger: #b42318;
  --tanker: #2457a6;
  --tanker-soft: #e5eefb;
  --focus: #3b82f6;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e141c;
    --surface: #161e29;
    --surface-2: #1b2532;
    --ink: #e6ebf2;
    --muted: #93a0b2;
    --line: #273343;
    --line-strong: #344356;
    --navy: #1e4a7c;
    --accent: #f2b230;
    --accent-soft: #3a2f14;
    --accent-ink: #f6cd72;
    --good: #4fc58a;
    --good-soft: #15301f;
    --warn: #f0a46c;
    --warn-soft: #3a2416;
    --danger: #f47b70;
    --tanker: #7fb0ff;
    --tanker-soft: #172a45;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e141c; --surface: #161e29; --surface-2: #1b2532; --ink: #e6ebf2; --muted: #93a0b2;
  --line: #273343; --line-strong: #344356; --navy: #1e4a7c; --accent: #f2b230; --accent-soft: #3a2f14;
  --accent-ink: #f6cd72; --good: #4fc58a; --good-soft: #15301f; --warn: #f0a46c; --warn-soft: #3a2416;
  --danger: #f47b70; --tanker: #7fb0ff; --tanker-soft: #172a45; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
h2 { font-size: 1.05rem; margin: 0; letter-spacing: -.01em; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 16px; }
main.wrap { padding-bottom: 48px; }

/* ---------- top bar ---------- */
.topbar { background: var(--navy); color: var(--navy-ink); border-bottom: 3px solid var(--accent); padding-top: env(safe-area-inset-top); }
.wrap { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 64px; flex-wrap: wrap; padding-top: 8px; padding-bottom: 8px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 34px; height: 34px; flex: none; }
.brand-mark rect { fill: rgba(255, 255, 255, .12); }
.brand-mark path { fill: var(--accent); }
.brand-name { font-weight: 700; font-size: 1.08rem; letter-spacing: -.01em; }
.brand-sub { font-size: .78rem; opacity: .75; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn.install svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.topbar .btn.install { background: var(--accent); border-color: var(--accent); color: #2b1d00; }
.topbar .btn.install:hover { background: var(--accent); filter: brightness(1.06); }

.install-help { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--ink); padding: 22px; max-width: min(420px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(0, 0, 0, .3); }
.install-help::backdrop { background: rgba(10, 16, 26, .55); }
.install-help h2 { margin-bottom: 10px; }
.install-help ol { margin: 0 0 12px; padding-left: 22px; display: grid; gap: 6px; }
.install-help .note { padding: 8px 10px; border-radius: 8px; background: var(--accent-soft); margin-bottom: 10px; }
.install-help form { margin-top: 14px; text-align: right; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255, 255, 255, .28); background: transparent; }
.topbar .btn.ghost:hover { background: rgba(255, 255, 255, .1); }
.count { display: inline-block; min-width: 1.4em; padding: 0 .4em; margin-left: 4px; border-radius: 99px; background: var(--accent); color: #2b1d00; font-size: .75rem; font-weight: 700; text-align: center; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-top: 16px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px 16px; flex-wrap: wrap; margin-bottom: 14px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- form ---------- */
.grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.span-2 { grid-column: span 2; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: .78rem; font-weight: 600; color: var(--muted); }
.field em { font-style: normal; font-weight: 500; opacity: .8; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
input[type="text"], input[type="number"], select {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 38px;
  font-variant-numeric: tabular-nums;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .switch input:focus-visible + .switch-track { outline: 2px solid var(--focus); outline-offset: 1px; }
input:disabled { background: var(--surface-2); color: var(--muted); border-style: dashed; }
input::placeholder { color: var(--muted); opacity: .7; }
select { width: auto; padding-right: 28px; }

/* switch */
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; position: relative; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track { width: 40px; height: 22px; border-radius: 99px; background: var(--line-strong); position: relative; transition: background .15s; flex: none; }
.switch-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .25); transition: transform .15s; }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch-label { font-weight: 600; font-size: .92rem; }
.switch.small .switch-track { width: 34px; height: 20px; }
.switch.small .switch-track::after { width: 14px; height: 14px; }
.switch.small input:checked + .switch-track::after { transform: translateX(14px); }
.switch.small .switch-label { font-size: .84rem; }

.defaults-note { margin-top: 8px; }
.penalty-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 20px; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.penalty-help { flex: 1 1 260px; }

/* ---------- route editor ---------- */
.route { display: flex; flex-direction: column; }
.stop { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--surface-2); }
.stop.denied { border-color: var(--warn); background: var(--warn-soft); }
.stop.final { padding: 10px 12px; }
.stop-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.stop-fields { margin-top: 12px; }
.pin { width: 26px; height: 26px; border-radius: 50%; background: var(--navy); color: #fff; font-weight: 700; font-size: .8rem; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.icao-wrap { position: relative; flex: none; }
input.icao { width: 110px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; transition: width .12s; }
input.icao:focus { width: 200px; }
.role-wrap { display: flex; flex-direction: column; min-width: 0; flex: 1 1 160px; }
.role { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.apt-name { font-size: .84rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.apt-name:empty { display: none; }
.apt-name.unknown { color: var(--warn); }
.leg-dist { text-transform: none; letter-spacing: 0; font-weight: 600; color: var(--tanker); }

/* airport autofill */
.ac-list {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 20;
  width: min(380px, calc(100vw - 48px));
  margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(16, 24, 40, .18);
  max-height: 320px; overflow-y: auto;
}
.ac-list li { display: flex; gap: 10px; align-items: baseline; padding: 7px 8px; border-radius: 7px; cursor: pointer; }
.ac-list li.active, .ac-list li:hover { background: var(--accent-soft); }
.ac-code { font-weight: 700; letter-spacing: .04em; min-width: 3.4em; font-variant-numeric: tabular-nums; }
.ac-text { display: flex; flex-direction: column; min-width: 0; }
.ac-name { font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ac-place { font-size: .76rem; color: var(--muted); }
.quick { margin-left: auto; }
.stop.denied .switch-label { color: var(--warn); }

.leg { display: flex; gap: 12px; padding: 10px 0 10px 12px; }
.leg-rail { width: 2px; background: repeating-linear-gradient(to bottom, var(--line-strong) 0 6px, transparent 6px 11px); position: relative; margin-left: 0; flex: none; }
.leg-rail svg { position: absolute; top: 50%; left: 50%; width: 20px; height: 20px; transform: translate(-50%, -50%) rotate(180deg); fill: var(--navy); background: var(--surface); border-radius: 50%; padding: 2px; }
.leg-body { flex: 1; min-width: 0; padding-left: 10px; }
.leg-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: .78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px; }
.leg-title .btn.link { text-transform: none; letter-spacing: 0; }
.stop-add { margin-top: 8px; }
.icon-btn { flex: none; width: 30px; height: 30px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid transparent; border-radius: 8px; background: none; color: var(--muted); cursor: pointer; padding: 0; }
.icon-btn svg { width: 16px; height: 16px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; fill: none; }
.icon-btn:hover { color: var(--danger); border-color: var(--line-strong); background: var(--surface); }
.icon-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.stop-head .icon-btn { margin-left: auto; }
.stop-head .quick + .icon-btn { margin-left: 2px; }
.leg-fields { grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 600; font-size: .9rem;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; min-height: 38px;
  border-radius: 8px; border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink); cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #2b1d00; }
.btn.primary:hover { filter: brightness(1.05); background: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.link { border: none; background: none; color: var(--tanker); padding: 0; min-height: 0; }
.btn.link:hover { text-decoration: underline; background: none; }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: .84rem; }
.btn.link.small { padding: 0; min-height: 0; }
.btn.link:disabled { color: var(--muted); text-decoration: none; }
.btn.danger { color: var(--danger); }

/* ---------- results ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 16px; }
.tile { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: var(--surface-2); min-width: 0; }
.tile-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.tile-value { font-size: 1.45rem; font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile-sub { font-size: .78rem; color: var(--muted); }
.tile.accent { background: var(--accent-soft); border-color: var(--accent); }
.tile.accent .tile-value { color: var(--accent-ink); }
.tile.good { background: var(--good-soft); border-color: var(--good); }
.tile.good .tile-value { color: var(--good); }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table.plan { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: .9rem; }
.plan th, .plan td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; white-space: nowrap; }
.plan th { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: var(--surface-2); font-weight: 700; }
.plan .num { text-align: right; }
.plan td.buy { background: var(--accent-soft); }
.plan th.buy { color: var(--accent-ink); }
.plan tr.dest td { background: var(--surface-2); border-bottom: none; }
.plan tr.dest td.buy { background: var(--surface-2); }
.plan s { text-decoration-thickness: 1px; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.tag { font-size: .72rem; font-weight: 600; padding: 1px 7px; border-radius: 99px; white-space: nowrap; }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.tanker { background: var(--tanker-soft); color: var(--tanker); }
.tag.good { background: var(--good-soft); color: var(--good); }
.compare { margin-top: 10px; }
.route-options { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 16px; margin: -4px 0 14px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.route-options p { flex: 1 1 260px; }
.refuel-options { margin-top: 18px; }
.refuel-options h3 { font-size: .95rem; margin: 0 0 2px; }
.refuel-options .table-wrap { margin-top: 8px; }
.plan.options td { white-space: normal; }
.plan.options tr.selected td { background: var(--accent-soft); font-weight: 600; }
.plan.options tr.impossible td { color: var(--muted); }
.tag { background: var(--surface-2); color: var(--muted); }

.empty { padding: 20px; border: 1px dashed var(--line-strong); border-radius: 10px; text-align: center; display: grid; gap: 6px; }
.alert { padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); border: 1px solid var(--warn); color: var(--ink); }
.alert ul { margin: 6px 0 0; padding-left: 20px; }
.alert li { margin: 2px 0; }

/* ---------- saved trips ---------- */
.trips-panel .card { margin-top: 16px; }
.trip-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.trip-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); flex-wrap: wrap; }
.trip-item.current { border-color: var(--accent); }
.trip-meta { display: flex; flex-direction: column; min-width: 0; }
.trip-meta strong { overflow-wrap: anywhere; }
.trip-actions { display: flex; gap: 6px; }
.empty-list { padding: 8px 0; }

.disclaimer { margin-top: 20px; font-size: .78rem; color: var(--muted); text-align: center; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 600; font-size: .9rem; box-shadow: 0 6px 20px rgba(0, 0, 0, .25); max-width: calc(100% - 32px); z-index: 10; }
.toast.error { background: var(--danger); color: #fff; }
.toast { display: flex; align-items: center; gap: 14px; }
.toast[hidden] { display: none; }
.toast-action { font: inherit; font-weight: 700; background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; text-decoration: underline; }

/* ---------- responsive ---------- */
@media (max-width: 820px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .leg-fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 480px) {
  .card { padding: 14px; }
  .span-2 { grid-column: span 2; }
  .quick { margin-left: 0; width: 100%; }
  /* Anchor the autofill list to the stop card so it fits the screen. */
  .stop-head { position: relative; }
  .icao-wrap { position: static; }
  .ac-list { left: 0; right: 0; width: auto; top: 44px; }
  .stop-head { padding-right: 34px; }
  .stop-head .icon-btn { position: absolute; top: 4px; right: 0; }
  .tile-value { font-size: 1.25rem; }
  .brand-sub { display: none; }
  .topbar-actions { gap: 6px; }
  .topbar .btn { padding: 6px 10px; min-height: 34px; font-size: .84rem; }
  .hide-sm { display: none; }
}
