:root {
  --accent: #0078d4;
  --accent-hover: #106ebe;
  --accent-soft: #e8f4fc;
  --surface: #ffffff;
  --surface-2: #f5f5f5;
  --border: #e0e0e0;
  --text: #1a1a1a;
  --text-2: #616161;
  --danger: #c42b1c;
  --shadow: 0 2px 8px rgba(0,0,0,.08), 0 8px 24px rgba(0,0,0,.1);
  --radius: 12px;
  --bar-h: 52px;
  --bottom-h: 64px;
  --panel-w: 340px;
  --font: "Segoe UI Variable", "Segoe UI", system-ui, Roboto, sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--surface-2);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

.app-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 1100;
  height: var(--bar-h);
  padding: 0 8px 0 4px;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}

.app-bar__brand { flex: 1; min-width: 0; padding-left: 4px; }
.app-bar__title { display: block; font-size: 15px; font-weight: 600; line-height: 1.2; }
.app-bar__subtitle { display: block; font-size: 12px; color: var(--text-2); }

.icon-btn {
  width: 40px; height: 40px;
  border: none; border-radius: 8px;
  background: transparent; color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { background: var(--border); }
.icon-btn--ghost { margin-left: auto; }

.map-stage { flex: 1; margin-top: var(--bar-h); position: relative; }
#map { width: 100%; height: 100%; background: #f2efe9; }
.leaflet-bottom.leaflet-left { bottom: calc(var(--bottom-h) + 12px); }

@media (min-width: 900px) {
  .map-stage { margin-right: var(--panel-w); }
  .leaflet-bottom.leaflet-left { bottom: 24px; }
}

.scrim {
  position: fixed; inset: 0; z-index: 1200;
  background: rgba(0,0,0,.35);
  backdrop-filter: blur(2px);
}

.side-panel {
  position: fixed; z-index: 1300;
  background: var(--surface);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 12px 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-header h2 { font-size: 18px; font-weight: 600; }
.panel-body { flex: 1; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; }

@media (max-width: 899px) {
  .side-panel {
    left: 0; right: 0; bottom: 0;
    max-height: min(85dvh, calc(100dvh - var(--bar-h) - 8px));
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .side-panel.open { transform: translateY(0); }
  .map-stage { margin-bottom: var(--bottom-h); }
}

@media (min-width: 900px) {
  .scrim { display: none !important; }
  .side-panel {
    top: var(--bar-h); right: 0; bottom: 0;
    width: var(--panel-w);
    border-left: 1px solid var(--border);
    transform: none;
  }
  #btn-menu, #btn-close-panel { display: none; }
}

.bottom-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  z-index: 1100;
  height: var(--bottom-h);
  padding-bottom: env(safe-area-inset-bottom, 0);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(0,0,0,.06);
}

.bottom-bar__btn {
  flex: 1; border: none; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 11px; color: var(--text-2);
  cursor: pointer; padding: 6px 0; min-height: 48px;
}
.bottom-bar__btn svg { width: 22px; height: 22px; }
.bottom-bar__btn.active { color: var(--accent); }

@media (min-width: 900px) { .bottom-bar { display: none; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
}
.card__label {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .5px;
  color: var(--text-2); margin-bottom: 8px;
}
.card--actions { display: flex; flex-direction: column; gap: 8px; }
.card--status { background: var(--surface-2); border-style: dashed; }

.field-row { display: flex; gap: 8px; margin-bottom: 8px; min-width: 0; }
.field-row:last-child { margin-bottom: 0; }
.field-row--gauge {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
}
.field-row--swatch { align-items: center; }

.field {
  flex: 1; min-width: 0; min-height: 40px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; font-family: inherit;
  background: var(--surface); color: var(--text);
  max-width: 100%;
}
.field--select {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.field:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.field--grow { flex: 2; }

.fab-mini {
  width: 40px; height: 40px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff;
  font-size: 22px; line-height: 1; cursor: pointer; flex-shrink: 0;
}

.color-swatch {
  width: 40px; height: 40px;
  border-radius: 8px; border: 2px solid var(--border); flex-shrink: 0;
}

.segmented {
  display: flex; background: var(--surface-2);
  border-radius: 10px; padding: 3px; gap: 2px;
}
.segmented button {
  flex: 1; min-height: 36px; border: none; border-radius: 8px;
  background: transparent; font-size: 13px; font-weight: 500;
  color: var(--text-2); cursor: pointer;
}
.segmented button.active {
  background: var(--surface); color: var(--accent);
  box-shadow: 0 1px 4px rgba(0,0,0,.12); font-weight: 600;
}

.segmented--4 button { font-size: 11px; padding: 0 4px; }

.badge {
  display: inline-block;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}

.btn--sm { min-height: 36px; font-size: 13px; margin-top: 8px; width: 100%; }

/* SweetAlert2 theme */
.bd-swal { border-radius: 12px !important; font-family: var(--font) !important; }
.swal2-popup { font-family: var(--font) !important; }

.hint { font-size: 12px; color: var(--text-2); margin-top: 8px; line-height: 1.45; }
.mono { font-family: "Cascadia Code", Consolas, monospace; font-size: 11px; color: var(--text-2); }

.btn {
  display: flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 16px; border-radius: 8px;
  font-size: 14px; font-weight: 500; font-family: inherit;
  cursor: pointer; border: none;
}
.btn--primary { background: var(--accent); color: #fff; }
.btn--tonal { background: var(--accent-soft); color: var(--accent); }
.btn--tonal:disabled { opacity: .45; cursor: not-allowed; }
.btn--outline { background: var(--surface); border: 1px solid var(--border); color: var(--text); }
.btn--danger { color: var(--danger); border-color: #f5c6c2; }
.btn-row { display: flex; gap: 8px; }
.card--tip { background: var(--accent-soft); border-color: #b3d9f2; }
.card--tip .hint { margin-top: 0; }

.btn-row .btn--sm { flex: 1; margin-top: 0; }
.btn-row--track { margin-top: 4px; }
.track-segment-label {
  font-size: 13px; font-weight: 600; color: var(--text);
  margin-bottom: 8px; line-height: 1.35;
  word-break: break-word;
}
.card { overflow: visible; }
.btn--file { text-align: center; }

.station-icon-wrapper { background: transparent !important; border: none !important; }
.station-marker { position: relative; width: 28px; height: 28px; }
.station-svg { display: block; width: 28px; height: 28px; filter: drop-shadow(0 1px 2px rgba(0,0,0,.2)); }

.station-label {
  position: absolute; white-space: nowrap; pointer-events: none; line-height: 1.2;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 1px 1px 0 #fff, -1px -1px 0 #fff;
}
.station-label--minor { font-size: 11px; color: #333; }
.station-label--major { font-size: 12px; color: #111; font-weight: 700; }

.station-marker[data-anchor="right"] .station-label { left: 32px; top: 50%; transform: translateY(-50%); }
.station-marker[data-anchor="left"] .station-label { right: 32px; top: 50%; transform: translateY(-50%); }
.station-marker[data-anchor="top"] .station-label { bottom: 32px; left: 50%; transform: translateX(-50%); }
.station-marker[data-anchor="bottom"] .station-label { top: 32px; left: 50%; transform: translateX(-50%); }

.legend-card {
  position: fixed; z-index: 1050;
  left: 12px; bottom: calc(var(--bottom-h) + 12px);
  max-width: min(360px, calc(100vw - 24px));
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
@media (min-width: 900px) { .legend-card { bottom: 16px; } }
.legend-card.collapsed .legend-card__body { display: none; }

.legend-card__pill {
  display: block; width: 100%; padding: 8px 16px;
  border: none; background: #111; color: #fff;
  font-size: 12px; font-weight: 700; letter-spacing: 1px;
  text-align: center; cursor: pointer;
}
.legend-card__body { padding: 10px 12px 12px; max-height: 50dvh; overflow-y: auto; }
.legend-block__title { font-size: 10px; font-weight: 700; text-transform: uppercase; color: var(--text-2); margin-bottom: 6px; }
.legend-block + .legend-block { margin-top: 10px; }

.matrix-table { width: 100%; border-collapse: collapse; font-size: 9px; }
.matrix-table th { font-weight: 600; padding: 3px 2px; text-align: center; color: var(--text-2); background: var(--accent-soft); }
.matrix-table td { padding: 3px 2px; text-align: center; }
.matrix-label { text-align: left !important; font-size: 8px; white-space: nowrap; }
.matrix-bar { display: block; min-width: 24px; height: 5px; border-radius: 3px; }
.matrix-na { color: #ccc; }

.legend-stations { display: flex; flex-direction: column; gap: 6px; }
.legend-station-row { display: flex; align-items: center; gap: 10px; font-size: 12px; }
.legend-icon { flex-shrink: 0; }

.toast {
  position: fixed;
  bottom: calc(var(--bottom-h) + 16px);
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  z-index: 2000;
  background: #323232; color: #fff;
  padding: 12px 20px; border-radius: 8px; font-size: 14px;
  opacity: 0; transition: transform .3s, opacity .3s;
  pointer-events: none; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (min-width: 900px) { .toast { bottom: 24px; } }

/* SweetAlert toasts – top-right, above all UI */
.bd-swal-toast-wrap {
  z-index: 25000 !important;
  top: calc(var(--bar-h) + 8px) !important;
  right: 8px !important;
  left: auto !important;
  bottom: auto !important;
  padding: 0 !important;
  width: auto !important;
  max-width: calc(100vw - 16px) !important;
}
.bd-swal-toast-wrap .swal2-popup {
  font-family: var(--font) !important;
  font-size: 14px !important;
  box-shadow: 0 4px 16px rgba(0,0,0,.18) !important;
}

/* SweetAlert modals – responsive on all devices */
.bd-swal-modal-wrap {
  z-index: 26000 !important;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
           max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)) !important;
}
.bd-swal-modal-wrap .swal2-popup {
  width: min(100%, 420px) !important;
  max-width: calc(100vw - 24px) !important;
  margin: 0 auto !important;
  font-family: var(--font) !important;
  border-radius: 14px !important;
  padding: 1.25em 1.25em 1em !important;
}
.bd-swal--form .bd-swal-field { display: block; text-align: left; margin: 0 0 12px; }
.bd-swal--form .bd-swal-label { display: block; font-size: 12px; font-weight: 600; color: #444; margin-bottom: 4px; }
.bd-swal--form .swal2-input,
.bd-swal--form .bd-swal-select {
  width: 100%; margin: 0; box-sizing: border-box;
  font-family: var(--font); font-size: 14px;
}
.bd-swal--form .bd-swal-select {
  padding: 10px 12px; border: 1px solid #d1d5db; border-radius: 8px;
  background: #fff; min-height: 44px;
}
.bd-swal-track-route {
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  gap: 6px 8px; margin: 0 0 14px; text-align: center;
}
.bd-swal-track-station {
  font-size: 13px; font-weight: 600; color: #1a1a1a; line-height: 1.35;
  word-break: break-word; flex: 1 1 120px; min-width: 0;
}
.bd-swal-track-arrow {
  font-size: 18px; font-weight: 700; color: #0078d4; line-height: 1;
  flex: 0 0 auto; padding: 0 2px;
}
.bd-swal-track-preview {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 14px; text-align: left;
}
.bd-swal-swatch {
  width: 36px; height: 36px; border-radius: 8px;
  border: 2px solid #e0e0e0; flex-shrink: 0;
}
.bd-swal-preview-label { font-size: 13px; color: #444; }
.bd-swal-track-actions {
  display: flex; flex-direction: column; gap: 8px;
  margin-bottom: 4px;
}
.bd-swal-btn {
  width: 100%; min-height: 44px; padding: 10px 14px;
  border-radius: 8px; font-size: 14px; font-weight: 500;
  font-family: var(--font); cursor: pointer;
}
.bd-swal-btn--outline {
  border: 1px solid #d1d5db; background: #fff; color: #1a1a1a;
}
.bd-swal-btn--outline:disabled { opacity: 0.6; cursor: wait; }
.bd-swal-btn--danger {
  border: 1px solid #fecaca; background: #fef2f2; color: #c42b1c;
}
@media (min-width: 480px) {
  .bd-swal-track-actions { flex-direction: row; }
  .bd-swal-track-actions .bd-swal-btn { flex: 1; }
}

.bd-swal--station .bd-swal-field { display: block; text-align: left; margin: 0 0 12px; }
.bd-swal--station .bd-swal-label { display: block; font-size: 12px; font-weight: 600; color: #444; margin-bottom: 4px; }
.bd-swal--station .swal2-input,
.bd-swal--station .bd-swal-select {
  width: 100%; margin: 0; box-sizing: border-box;
  font-family: var(--font); font-size: 14px;
}
.bd-swal--station .bd-swal-select { padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 6px; background: #fff; }
.bd-swal-coords { font-family: ui-monospace, Consolas, monospace !important; font-size: 13px !important; }
.bd-swal-field--coords { margin-bottom: 0; }
.bd-swal-symbol-preview { margin-top: 8px; display: flex; align-items: center; gap: 8px; }
.bd-swal-symbol-preview .station-svg { width: 28px; height: 28px; }
.bd-swal-station-actions { margin-top: 4px; }
.bd-swal-station-actions .bd-swal-btn { width: 100%; }
.station-svg--local { filter: drop-shadow(0 0 1.5px rgba(0,0,0,.45)); }
.station-icon-wrapper--local .station-svg { width: 22px; height: 22px; }
.bd-swal-hint { font-size: 11px; color: #666; margin-top: 4px; text-align: left; }
.bd-swal-optional { font-weight: 400; color: #888; font-size: 11px; }
.bd-swal-lookup { font-size: 11px; margin-top: 4px; min-height: 16px; text-align: left; }
.bd-swal-lookup--pending { color: #666; }
.bd-swal-lookup--ok { color: #0a7a0a; }
.bd-swal-lookup--fail { color: #b45309; }

.hint--tight { margin-top: 0; margin-bottom: 8px; font-size: 12px; }
.card code { font-size: 11px; background: #f0f0f0; padding: 1px 4px; border-radius: 4px; }
.autosave-note {
  font-size: 11px;
  color: var(--accent);
  text-align: center;
  padding: 6px 0 0;
}

