:root {
  color-scheme: light;

  --bg: #f4f0e7;
  --bg-tint: #ece6d7;
  --panel: #fffdf8;
  --panel-muted: #f7f2e7;
  --ink: #1c1d1a;
  --ink-soft: #3a3b36;
  --muted: #7a7568;
  --border: #e6ddc8;
  --border-strong: #d7cbaf;
  --accent: #2f6b4f;
  --accent-ink: #1e4a36;
  --accent-soft: rgba(47, 107, 79, 0.10);
  --accent-softer: rgba(47, 107, 79, 0.06);
  --warn: #a8421f;
  --route: #dd6b20;
  --route-soft: rgba(221, 107, 32, 0.14);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;

  --shadow-sm: 0 1px 2px rgba(28, 29, 26, 0.05);
  --shadow: 0 6px 24px -12px rgba(28, 29, 26, 0.18), 0 2px 6px -3px rgba(28, 29, 26, 0.08);
  --shadow-lg: 0 24px 60px -30px rgba(28, 29, 26, 0.35), 0 8px 20px -12px rgba(28, 29, 26, 0.12);

  --font-sans: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-serif: "Instrument Serif", "Iowan Old Style", Georgia, serif;
}

[data-theme="dark"] {
  color-scheme: dark;

  --bg: #12140f;
  --bg-tint: #1a1d17;
  --panel: #1c1f19;
  --panel-muted: #1f231c;
  --ink: #f0ebe0;
  --ink-soft: #d3ccbd;
  --muted: #97907f;
  --border: #2a2d25;
  --border-strong: #3a3d33;
  --accent: #8ec6a6;
  --accent-ink: #c2e4cf;
  --accent-soft: rgba(142, 198, 166, 0.14);
  --accent-softer: rgba(142, 198, 166, 0.08);
  --warn: #e89575;
  --route: #f7a85c;
  --route-soft: rgba(247, 168, 92, 0.18);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow: 0 6px 24px -12px rgba(0, 0, 0, 0.6), 0 2px 6px -3px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 24px 60px -30px rgba(0, 0, 0, 0.8), 0 8px 20px -12px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at -10% -10%, var(--accent-softer), transparent 60%),
    radial-gradient(900px 500px at 110% 10%, rgba(221, 107, 32, 0.05), transparent 60%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }

a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

/* ------- Top bar ------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
}
.brand:hover { text-decoration: none; }

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent);
  color: #f8f5ec;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-sm);
}

.brand-text { display: grid; line-height: 1.1; }

.brand-title {
  font-family: var(--font-serif);
  font-size: 22px;
  letter-spacing: -0.01em;
  font-weight: 400;
}

.brand-tag {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

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

/* ------- Buttons ------- */
button, .as-label {
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  color: var(--ink);
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease, transform 120ms ease;
}

.soft-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}
.soft-button:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: var(--panel-muted);
}
.soft-button:active:not(:disabled) { transform: translateY(1px); }
.soft-button:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

.soft-button.block { width: 100%; justify-content: center; }

.soft-button.subtle {
  background: transparent;
  box-shadow: none;
  border-color: transparent;
  color: var(--muted);
}
.soft-button.subtle:hover:not(:disabled) {
  color: var(--ink);
  background: var(--accent-softer);
  border-color: var(--border);
}

.soft-button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
[data-theme="dark"] .soft-button.primary { color: #12140f; }
.soft-button.primary:hover:not(:disabled) { filter: brightness(0.95); }

.ghost-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}
.ghost-button:hover { border-color: var(--border-strong); background: var(--panel-muted); }

.ghost-button .btn-icon {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.link-button {
  background: none;
  border: 0;
  padding: 0;
  margin-left: 6px;
  color: var(--accent-ink);
  font-weight: 500;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.link-button:hover { text-decoration: none; }

/* theme toggle icon swap */
.icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: inline-block; }

/* ------- Layout ------- */
.layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 20px;
  padding: 20px 24px 24px;
  max-width: 1600px;
  margin: 0 auto;
  align-items: start;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 16px;
    gap: 16px;
  }
  .workspace { order: 0; }
  .sidebar { order: 1; }
  .intro { display: none; }
}

/* ------- Sidebar ------- */
.sidebar {
  display: grid;
  gap: 14px;
  position: sticky;
  top: 78px;
  max-height: calc(100vh - 96px);
  overflow-y: auto;
  padding-right: 4px;
}

@media (max-width: 980px) {
  .sidebar { position: static; max-height: none; overflow: visible; }
}

.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; }

.intro h1 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 34px;
  line-height: 1.1;
  letter-spacing: -0.02em;
  margin-bottom: 6px;
}
.intro p { color: var(--muted); margin: 0; font-size: 14px; max-width: 36ch; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 14px;
  box-shadow: var(--shadow-sm);
  display: grid;
  gap: 10px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-head h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--muted);
  font-weight: 600;
}

.panel-hint {
  font-size: 12px;
  color: var(--muted);
}

.panel-head-toggle {
  all: unset;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--muted);
  font-weight: 600;
}
.panel-head-toggle .chev { transition: transform 150ms ease; }
.panel.debug[data-open="true"] .chev { transform: rotate(180deg); }

.panel.debug .debug-body { display: none; }
.panel.debug[data-open="true"] .debug-body { display: grid; gap: 10px; }

.debug ul {
  margin: 0;
  padding-left: 16px;
  color: var(--muted);
  font-size: 12px;
  display: grid;
  gap: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  max-height: 240px;
  overflow-y: auto;
}

.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }

.help-text {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

/* ------- Form controls ------- */
.field {
  display: grid;
  gap: 6px;
}

.field-label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}

.field-hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.3;
}

select, input[type="text"], input[type="password"] {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--panel-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
select:focus, input[type="text"]:focus, input[type="password"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  background: var(--panel);
}
select:disabled, input:disabled { opacity: 0.55; cursor: not-allowed; }

select {
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237a7568' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'><path d='M6 9l6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* Switch */
.switch {
  display: grid;
  grid-template-columns: 40px 1fr;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 4px 0;
}
.switch.compact { padding: 0; }
.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.switch-track {
  position: relative;
  width: 36px;
  height: 20px;
  background: var(--bg-tint);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background 150ms ease, border-color 150ms ease;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 2px;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  transition: left 150ms ease, background 150ms ease;
}
.switch input:checked + .switch-track {
  background: var(--accent);
  border-color: var(--accent);
}
.switch input:checked + .switch-track::after { left: 18px; background: #fff; }
.switch input:focus-visible + .switch-track {
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.switch-text { display: grid; gap: 2px; }

/* Routing options container */
.routing-options {
  display: grid;
  gap: 10px;
  padding-top: 2px;
}

.routing-options[aria-disabled="true"] {
  opacity: 0.5;
  pointer-events: none;
}

/* API key */
.api-key { display: grid; gap: 6px; }
.api-key-row { display: flex; gap: 6px; flex-wrap: wrap; }
.api-key-row input { flex: 1 1 160px; min-width: 0; }
.api-key.has-key input { display: none; }
.api-key.has-key #saveKeyBtn { display: none; }
.api-key:not(.has-key) #editKeyBtn { display: none; }

/* Save / share rows */
.save-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.save-row input, .save-row select { flex: 1; min-width: 0; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ------- Workspace (map + extras) ------- */
.workspace { display: grid; gap: 14px; min-width: 0; }

.map-wrap {
  position: relative;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow);
  isolation: isolate;
}

.map {
  width: 100%;
  height: clamp(420px, calc(100vh - 180px), 820px);
}

@media (max-width: 980px) {
  .map { height: 60vh; min-height: 360px; }
}

/* Stats floating over map */
.stat-overlay {
  position: absolute;
  right: 14px;
  top: 14px;
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  max-width: calc(100% - 68px);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: saturate(140%) blur(10px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
  z-index: 450;
  font-variant-numeric: tabular-nums;
}

.stat {
  display: grid;
  gap: 2px;
  padding: 0 8px;
  min-width: 82px;
}
.stat + .stat { border-left: 1px solid var(--border); }

@media (max-width: 520px) {
  .stat-overlay { padding: 8px 10px; gap: 4px 6px; }
  .stat { padding: 0 6px; min-width: 76px; }
  .stat-value { font-size: 14px; }
}

.stat-label {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}
.stat-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
}

/* Hint when no points yet */
.map-hint {
  position: absolute;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--ink-soft);
  box-shadow: var(--shadow);
  z-index: 450;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
}
.map-hint.hidden { opacity: 0; transform: translate(-50%, 6px); }

/* Status line */
.status-line {
  min-height: 18px;
  font-size: 13px;
  color: var(--muted);
  padding: 0 4px;
}
.status-line.error { color: var(--warn); }

/* Chart panel */
.chart-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow-sm);
  display: grid;
  gap: 10px;
}
.chart-wrap {
  position: relative;
  width: 100%;
  height: 220px;
}
.chart-panel canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* Directions */
.directions {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow-sm);
}
.directions ol {
  margin: 10px 0 0;
  padding-left: 18px;
  color: var(--ink-soft);
  font-size: 14px;
  display: grid;
  gap: 6px;
  max-height: 260px;
  overflow-y: auto;
}

.hidden { display: none !important; }

/* Footer */
.footer {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
  padding: 4px 6px 8px;
}
.footer a { color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.footer a:hover { color: var(--ink); }
.footer p { margin: 0; }
.footer p + p { margin-top: 6px; }
.footer-meta { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; }
.footer-meta a { display: inline-flex; align-items: center; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 22, 17, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}
.modal.hidden { display: none; }

.modal-content {
  position: relative;
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius-lg);
  padding: 24px 26px 22px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}

.modal-content h3 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 26px;
  letter-spacing: -0.01em;
  margin-bottom: 10px;
}
.modal-content p { color: var(--ink-soft); margin: 0 0 10px; }
.modal-content ol {
  padding-left: 18px;
  margin: 8px 0 12px;
  color: var(--ink-soft);
  display: grid;
  gap: 4px;
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
}
.modal-close:hover { background: var(--accent-softer); color: var(--ink); }

.modal-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  color: var(--accent-ink);
  font-weight: 600;
}

/* ------- Leaflet tweaks ------- */
.leaflet-container {
  font-family: inherit;
  background: var(--panel-muted);
}
.leaflet-control-zoom a {
  background: var(--panel) !important;
  color: var(--ink) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--panel-muted) !important; }
.leaflet-control-attribution {
  background: color-mix(in srgb, var(--panel) 85%, transparent) !important;
  color: var(--muted) !important;
  font-size: 11px !important;
  backdrop-filter: blur(4px);
}
.leaflet-control-attribution a { color: var(--ink-soft) !important; }

/* Dark mode map tile tone-down */
[data-theme="dark"] .leaflet-tile-pane {
  filter: brightness(0.78) contrast(1.02) saturate(0.85);
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
