:root {
  color-scheme: light;
  --ink: #172026;
  --muted: #63717a;
  --soft: #eef3f1;
  --paper: #fbfcfb;
  --panel: #ffffff;
  --line: #d8e1df;
  --field: #f8faf9;
  --button-soft: #f3f7f6;
  --button-active: #ffffff;
  --button-hover-line: #a9bbb6;
  --label: #2a383e;
  --accent: #14745f;
  --accent-strong: #0d5949;
  --accent-hover: #0d5949;
  --sky: #dcecf9;
  --sky-ink: #24485f;
  --warning: #f3bc55;
  --danger: #b84b4b;
  --marker: #253840;
  --map-bg: #dfe9e5;
  --overlay: rgba(255, 255, 255, 0.94);
  --overlay-border: rgba(216, 225, 223, 0.95);
  --overlay-shadow: 0 8px 22px rgba(23, 32, 38, 0.14);
  --pin-border: #ffffff;
  --toast-bg: #172026;
  --shadow: 0 18px 48px rgba(18, 35, 42, 0.16);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #edf4f2;
  --muted: #9fb0ad;
  --soft: #0f1719;
  --paper: #111b1d;
  --panel: #152123;
  --line: #314245;
  --field: #10191b;
  --button-soft: #1d2b2e;
  --button-active: #233537;
  --button-hover-line: #4d6869;
  --label: #d8e6e2;
  --accent: #2fc59d;
  --accent-strong: #64e0bf;
  --accent-hover: #229b7e;
  --sky: #213f4d;
  --sky-ink: #cfeefa;
  --warning: #f0c15d;
  --danger: #ff8983;
  --marker: #32484f;
  --map-bg: #11191b;
  --overlay: rgba(21, 33, 35, 0.94);
  --overlay-border: rgba(64, 84, 88, 0.96);
  --overlay-shadow: 0 12px 30px rgba(0, 0, 0, 0.34);
  --pin-border: #0f1719;
  --toast-bg: #edf4f2;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

.leaflet-container {
  position: relative;
  overflow: hidden;
  background: var(--map-bg);
  outline-offset: 1px;
  font-family: inherit;
  touch-action: pan-x pan-y;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
  position: absolute;
  top: 0;
  left: 0;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
  user-select: none;
  -webkit-user-drag: none;
}

.leaflet-container img.leaflet-tile,
.leaflet-container .leaflet-marker-icon,
.leaflet-container .leaflet-marker-shadow {
  max-width: none !important;
  max-height: none !important;
}

.leaflet-tile {
  visibility: hidden;
}

.leaflet-tile-loaded {
  visibility: inherit;
}

.leaflet-map-pane {
  z-index: auto;
}

.leaflet-tile-pane {
  z-index: 200;
}

.leaflet-overlay-pane {
  z-index: 400;
}

.leaflet-shadow-pane {
  z-index: 500;
}

.leaflet-marker-pane {
  z-index: 600;
}

.leaflet-tooltip-pane {
  z-index: 650;
}

.leaflet-popup-pane {
  z-index: 700;
}

.leaflet-top,
.leaflet-bottom {
  position: absolute;
  z-index: 1000;
  pointer-events: none;
}

.leaflet-top {
  top: 0;
}

.leaflet-right {
  right: 0;
}

.leaflet-bottom {
  bottom: 0;
}

.leaflet-left {
  left: 0;
}

.leaflet-control {
  position: relative;
  z-index: 800;
  float: left;
  clear: both;
  pointer-events: auto;
}

.leaflet-right .leaflet-control {
  float: right;
}

.leaflet-control-zoom {
  border: 1px solid var(--overlay-border);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--overlay-shadow);
}

.leaflet-control-zoom a {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  background: var(--overlay);
  color: var(--ink);
  text-decoration: none;
  font-weight: 800;
  line-height: 1;
}

.leaflet-control-zoom a + a {
  border-top: 1px solid var(--overlay-border);
}

.leaflet-control-attribution {
  padding: 2px 6px;
  background: var(--overlay);
  color: var(--muted);
}

.leaflet-control-attribution a {
  color: var(--accent-strong);
}

.leaflet-popup {
  position: absolute;
  margin-bottom: 20px;
  text-align: center;
}

.leaflet-popup-content-wrapper {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
  text-align: left;
}

.leaflet-popup-content {
  min-width: 160px;
  margin: 12px 14px;
  line-height: 1.35;
  font-size: 0.9rem;
}

.leaflet-popup-tip-container {
  position: absolute;
  left: 50%;
  width: 24px;
  height: 12px;
  margin-left: -12px;
  overflow: hidden;
  pointer-events: none;
}

.leaflet-popup-tip {
  width: 16px;
  height: 16px;
  margin: -8px auto 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  transform: rotate(45deg);
}

.leaflet-popup-close-button {
  position: absolute;
  top: 7px;
  right: 8px;
  color: var(--muted);
  text-decoration: none;
  font-size: 18px;
  line-height: 1;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--soft);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  overflow-x: hidden;
}

button,
input,
label,
a {
  font: inherit;
}

button,
a,
.file-button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(340px, 410px) minmax(0, 1fr);
  height: 100vh;
  min-height: 100vh;
  overflow: hidden;
}

html, body {
  height: 100%;
  overflow: hidden;
}

.planner-panel {
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
  min-height: 100vh;
  padding: 22px;
  overflow: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.app-header {
  display: flex;
  align-items: center;
  gap: 14px;
}

.app-header > div:nth-child(2) {
  min-width: 0;
}

.theme-toggle {
  margin-left: auto;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--accent);
  color: white;
}

.brand-mark svg {
  width: 25px;
  height: 25px;
}

h1 {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.15;
  letter-spacing: 0;
}

p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.control-section {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.control-section.compact {
  gap: 8px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

label,
.section-title > label {
  color: var(--label);
  font-weight: 700;
  font-size: 0.92rem;
}

.input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

input[type="text"] {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--field);
  color: var(--ink);
  outline: none;
}

input[type="text"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(20, 116, 95, 0.15);
}

.primary-button,
.secondary-button,
.icon-button,
.mode-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.primary-button {
  min-width: 82px;
  padding: 0 14px;
  background: var(--accent);
  color: white;
  font-weight: 700;
}

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

.secondary-button {
  min-height: 40px;
  padding: 0 12px;
  background: var(--button-soft);
  border-color: var(--line);
  color: var(--ink);
  font-weight: 700;
}

.secondary-button:hover,
.icon-button:hover,
.mode-button:hover {
  border-color: var(--button-hover-line);
}

.secondary-button.small {
  min-height: 34px;
  padding: 0 10px;
  font-size: 0.88rem;
}

.icon-button {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  background: var(--button-soft);
  border-color: var(--line);
  color: var(--ink);
}

.icon-button.light,
.secondary-button.light {
  background: var(--overlay);
  border-color: var(--overlay-border);
  box-shadow: var(--overlay-shadow);
}

.icon-button svg,
.primary-button svg,
.secondary-button svg,
.mode-button svg {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
}

.file-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.file-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  min-width: 0;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--button-soft);
}

.mode-button {
  min-width: 0;
  padding: 0 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.mode-button.active {
  background: var(--button-active);
  color: var(--accent-strong);
  border-color: var(--line);
  box-shadow: 0 2px 8px rgba(18, 35, 42, 0.08);
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--sky);
  color: var(--sky-ink);
  font-weight: 800;
  font-size: 0.82rem;
}

.inline-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.result-line {
  min-height: 20px;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.35;
}

.result-line.error {
  color: var(--danger);
}

.stop-section {
  min-height: 0;
}

.stop-list {
  display: grid;
  gap: 8px;
  min-height: 90px;
  max-height: calc(100vh - 575px);
  margin: 0;
  padding: 0;
  overflow: auto;
  overflow-x: hidden;
  list-style: none;
}

.stop-card {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-width: 0;
  max-width: 100%;
  min-height: 62px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}

.stop-number {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--marker);
  color: #ffffff;
  font-weight: 800;
  font-size: 0.88rem;
}

.stop-main {
  min-width: 0;
}

.stop-main strong,
.stop-main span {
  display: block;
  overflow-wrap: anywhere;
}

.stop-main strong {
  color: var(--ink);
  font-size: 0.94rem;
  line-height: 1.25;
}

.stop-main span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.3;
}

.stop-tools {
  display: inline-grid;
  grid-auto-flow: column;
  gap: 4px;
  max-width: 100%;
}

.stop-tools .icon-button {
  width: 32px;
  height: 32px;
  min-height: 32px;
  flex-basis: 32px;
}

.stop-tools .icon-button svg {
  width: 15px;
  height: 15px;
}

.empty-state {
  display: grid;
  place-items: center;
  gap: 8px;
  min-height: 110px;
  border: 1px dashed var(--button-hover-line);
  border-radius: 8px;
  color: var(--muted);
  font-weight: 700;
}

.empty-state.hidden {
  display: none;
}

.empty-state svg {
  width: 26px;
  height: 26px;
}

.map-pane {
  position: relative;
  min-width: 0;
  height: 100vh;
  min-height: 0;
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
  min-height: 0;
  background: var(--map-bg);
}

.map-topbar {
  position: absolute;
  z-index: 500;
  top: 18px;
  left: 18px;
  right: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.map-topbar > * {
  pointer-events: auto;
}

.route-summary {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  min-height: 44px;
  max-width: min(620px, 100%);
  padding: 8px 12px;
  border: 1px solid var(--overlay-border);
  border-radius: 8px;
  background: var(--overlay);
  box-shadow: var(--overlay-shadow);
}

.route-summary strong {
  font-size: 1.02rem;
}

.route-summary span {
  color: var(--muted);
  font-size: 0.92rem;
}

.map-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.leaflet-control-attribution {
  font-size: 0.68rem;
}

:root[data-theme="dark"] .leaflet-tile-pane {
  filter: brightness(0.62) invert(1) contrast(0.92) hue-rotate(170deg) saturate(0.72);
}

.pin-marker {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 2px solid var(--pin-border);
  border-radius: 50%;
  background: var(--marker);
  color: #ffffff;
  box-shadow: 0 6px 16px rgba(23, 32, 38, 0.28);
  font-weight: 800;
  font-size: 0.88rem;
}

.pin-marker.origin {
  background: var(--accent);
}

.toast {
  position: fixed;
  z-index: 1000;
  right: 18px;
  bottom: 18px;
  max-width: min(360px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--toast-bg);
  color: var(--soft);
  box-shadow: var(--shadow);
  font-weight: 700;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: 100vh;
    overflow: hidden;
  }

  .planner-panel {
    max-height: 45vh;
    min-height: auto;
    overflow: auto;
    box-shadow: none;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .stop-list {
    max-height: 160px;
  }

  .map-pane {
    height: auto;
    min-height: 0;
    flex: 1;
  }

  #map {
    height: 100%;
    min-height: 0;
  }
}

@media (max-width: 560px) {
  .planner-panel {
    padding: 16px;
  }

  .section-title {
    flex-wrap: wrap;
  }

  .stop-section .section-title {
    align-items: flex-start;
  }

  .stop-section .inline-actions {
    width: 100%;
    justify-content: space-between;
  }

  .app-header {
    align-items: flex-start;
  }

  .input-row,
  .file-actions {
    grid-template-columns: 1fr;
  }

  .primary-button,
  .secondary-button {
    width: 100%;
  }

  .stop-section .secondary-button.small {
    width: auto;
  }

  .mode-button {
    gap: 5px;
    padding: 0 5px;
  }

  .mode-button span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .map-topbar {
    top: 10px;
    left: 10px;
    right: 10px;
    align-items: stretch;
    flex-direction: column;
  }

  .map-actions {
    justify-content: flex-end;
  }

  .route-summary {
    width: 100%;
  }

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

  .stop-tools {
    grid-column: 1 / -1;
    justify-content: end;
  }
}
