:root {
  --accent: #14b8a6;
  --accent-strong: #0d9488;
  --text: #0f172a;
  --muted: #64748b;
  --panel: rgba(255, 255, 255, 0.92);
  /* Mobile terrain bar — Apple-like materials */
  --terrain-mobile-fill: rgba(255, 255, 255, 0.78);
  --terrain-mobile-border: rgba(0, 0, 0, 0.09);
  --terrain-mobile-shadow:
    0 8px 32px rgba(0, 0, 0, 0.08),
    0 1px 0 rgba(255, 255, 255, 0.65) inset;
  --terrain-track-h: 6px;
  --terrain-thumb-size: 26px;
  --ad-strip-height: 0px;
}

* {
  box-sizing: border-box;
  margin: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--text);
}

#map {
  position: fixed;
  inset: 0;
  /* Avoid the browser stealing pinch / two-finger gestures on mobile */
  touch-action: none;
}

body.ads-enabled {
  --ad-strip-height: 52px;
}

.ad-banner {
  background: rgba(255, 255, 255, 0.92);
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  bottom: 0;
  left: 0;
  line-height: 0;
  max-height: 52px;
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  pointer-events: auto;
  position: fixed;
  right: 0;
  text-align: center;
  z-index: 12;
}

.ad-banner[hidden] {
  display: none !important;
}

.ad-banner .adsbygoogle {
  display: inline-block;
  max-height: 50px;
  min-height: 50px;
}

/* Search result pin — DOM overlay synced with map.project() */
.search-place-pin {
  background: #ea4335;
  border: 3px solid #fff;
  border-radius: 50% 50% 50% 0;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.4);
  height: 32px;
  left: 0;
  margin-left: -16px;
  margin-top: -32px;
  pointer-events: none;
  position: absolute;
  top: 0;
  transform: rotate(-45deg);
  width: 32px;
  z-index: 20;
}

.search-place-pin::after {
  background: #fff;
  border-radius: 50%;
  content: "";
  height: 10px;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 10px;
}

/* Top trailing: terrain + search — default one row (terrain | search).
   Only narrow landscape viewports use a column (search above terrain); desktop browsers
   are almost always "landscape" by dimensions so we must not key off orientation alone. */
.hud-top {
  align-items: flex-start;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 8px;
  left: auto;
  pointer-events: none;
  position: fixed;
  right: max(4px, env(safe-area-inset-right, 4px));
  top: max(4px, env(safe-area-inset-top, 4px));
  z-index: 12;
}

.hud-top > * {
  pointer-events: auto;
}

.hud-top .search-anchor {
  flex-shrink: 0;
  position: relative;
}

@media (orientation: landscape) and (max-width: 1023px) {
  .hud-top {
    align-items: flex-end;
    flex-direction: column;
    gap: 10px;
  }

  .hud-top .search-anchor {
    order: 0;
  }

  .hud-top .terrain-strip {
    order: 1;
  }
}

/* Wide windows (typical desktop web): always keep terrain + search on one line */
@media (min-width: 1024px) {
  .hud-top {
    align-items: flex-start;
    flex-direction: row;
    gap: 8px;
  }

  .hud-top .search-anchor,
  .hud-top .terrain-strip {
    order: 0;
  }
}

.terrain-strip {
  backdrop-filter: blur(16px);
  background: var(--panel);
  border-radius: 10px;
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.08),
    0 1px 3px rgba(0, 0, 0, 0.05);
  flex: 0 1 auto;
  max-width: min(340px, calc(100vw - 56px));
  padding: 5px 8px 6px;
}

.terrain-compact.terrain-one-row {
  align-items: center;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 8px;
}

.terrain-compact.control {
  gap: 4px;
}

.terrain-title-row {
  align-items: baseline;
  display: flex;
  flex: 0 1 auto;
  font-size: 10px;
  font-weight: 700;
  gap: 6px;
  justify-content: flex-start;
  letter-spacing: 0.02em;
  min-width: 0;
  white-space: nowrap;
}

.terrain-compact .value {
  font-size: 10px;
}

.terrain-short-title {
  display: none;
}

.terrain-range-wrap {
  flex: 1 1 auto;
  max-width: min(152px, 38vw);
  min-width: 72px;
}

.terrain-compact input[type="range"] {
  width: 100%;
}

/* Room for 26px thumb + track (desktop + mobile) */
.terrain-strip .terrain-compact input[type="range"] {
  height: 28px;
}

/* Apple-style range (scoped): rounded track + elevated thumb — applied on touch/mobile below */
.terrain-strip input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.terrain-strip input[type="range"]::-webkit-slider-runnable-track {
  background: rgba(0, 0, 0, 0.08);
  border-radius: 100px;
  height: var(--terrain-track-h);
}

.terrain-strip input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  background: #fff;
  border-radius: 50%;
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(0, 0, 0, 0.06);
  height: var(--terrain-thumb-size);
  margin-top: calc((var(--terrain-track-h) - var(--terrain-thumb-size)) / 2);
  width: var(--terrain-thumb-size);
}

.terrain-strip input[type="range"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.terrain-strip input[type="range"]::-moz-range-track {
  background: rgba(0, 0, 0, 0.08);
  border-radius: 100px;
  height: var(--terrain-track-h);
}

.terrain-strip input[type="range"]::-moz-range-thumb {
  background: #fff;
  border: none;
  border-radius: 50%;
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(0, 0, 0, 0.06);
  height: var(--terrain-thumb-size);
  width: var(--terrain-thumb-size);
}

/* Search popover — anchored to search button in top HUD */
.hud-top .search-popover {
  bottom: auto;
  left: auto;
  margin-top: 6px;
  right: 0;
  top: 100%;
}

/* Bottom-trailing: settings + compact ℹ — hug corner; safe-area still respected on notched devices */
.map-bottom-corner {
  align-items: flex-end;
  bottom: max(2px, calc(env(safe-area-inset-bottom, 0px) + var(--ad-strip-height)));
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
  position: fixed;
  right: max(2px, env(safe-area-inset-right, 0px));
  z-index: 13;
}

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

@media (orientation: landscape) {
  .map-bottom-corner {
    flex-direction: row;
    gap: 8px;
  }
}

.hud-bottom-right {
  display: flex;
  flex-shrink: 0;
  pointer-events: none;
}

.hud-bottom-right > * {
  pointer-events: auto;
}

/* Reparented out of #map — flex handles layout (override MapLibre absolute corner) */
.map-bottom-corner .maplibregl-ctrl-bottom-right {
  align-items: flex-end;
  display: flex;
  flex-direction: column;
  gap: 0;
  margin: 0 !important;
  position: static !important;
  right: auto !important;
  bottom: auto !important;
  float: none !important;
}

/* MapLibre default gives compact attrib ~10px margin — pull flush inside cluster */
.map-bottom-corner .maplibregl-ctrl-attrib.maplibregl-compact {
  margin: 0 !important;
}

.icon-fab {
  align-items: center;
  background: var(--panel);
  border: 1px solid rgba(226, 232, 240, 0.95);
  border-radius: 50%;
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.1),
    0 1px 3px rgba(0, 0, 0, 0.06);
  color: var(--accent-strong);
  cursor: pointer;
  display: inline-flex;
  height: 36px;
  justify-content: center;
  padding: 0;
  width: 36px;
}

.icon-fab:active {
  opacity: 0.92;
}

.fab-svg {
  height: 20px;
  width: 20px;
}

.search-popover {
  backdrop-filter: blur(16px);
  background: var(--panel);
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  left: 0;
  margin-top: 6px;
  min-width: min(288px, calc(100vw - 40px));
  padding: 12px;
  position: absolute;
  top: 100%;
  z-index: 13;
}

.search-popover[hidden] {
  display: none !important;
}

.search-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 0;
}

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

#search-input {
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  font: inherit;
  font-size: 13px;
  padding: 10px 12px;
  width: 100%;
}

#search-btn {
  padding: 10px 12px;
}

.search-feedback {
  color: var(--muted);
  font-size: 12px;
}

.search-results {
  display: flex;
  flex-direction: column;
  gap: 6px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.search-results button {
  background: rgba(241, 245, 249, 0.75);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  font-size: 12px;
  line-height: 1.35;
  padding: 8px 10px;
  text-align: left;
  width: 100%;
}

.panel-shell {
  position: relative;
  z-index: 11;
}

.panel-toolbar {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}

.panel-toolbar-title {
  font-size: 15px;
  font-weight: 800;
}

.panel-icon-btn {
  align-items: center;
  background: rgba(241, 245, 249, 0.9);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  height: 36px;
  justify-content: center;
  line-height: 1;
  padding: 0;
  width: 36px;
}

/* Uses .icon-fab; keep unobtrusive next to search-style controls */
.panel-open-btn {
  flex-shrink: 0;
}

.panel-backdrop {
  background: rgba(15, 23, 42, 0.42);
  display: none;
  inset: 0;
  pointer-events: auto;
  position: fixed;
  z-index: 10;
}

.panel-shell.panel-shell--open .panel-backdrop {
  display: block;
}

.panel {
  backdrop-filter: blur(16px);
  background: var(--panel);
  border-radius: 16px;
  bottom: max(56px, calc(env(safe-area-inset-bottom, 8px) + 52px + var(--ad-strip-height)));
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.12),
    0 1px 3px rgba(0, 0, 0, 0.06);
  display: none;
  flex-direction: column;
  gap: 14px;
  left: max(8px, env(safe-area-inset-left, 8px));
  max-height: min(72vh, calc(100vh - 120px));
  min-height: 0;
  overflow-y: auto;
  padding: 14px 18px 18px;
  position: fixed;
  width: min(320px, calc(100vw - 20px));
  z-index: 11;
}

.panel-shell.panel-shell--open .panel {
  display: flex;
}

.brand h1 {
  font-size: 26px;
  font-weight: 800;
  line-height: 1.1;
}

.brand p {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
  margin-top: 6px;
}

.control {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lang-control {
  margin-bottom: 0;
}

.lang-control label {
  display: block;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
}

.language-select {
  accent-color: var(--accent);
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 9px 12px;
  width: 100%;
}

.control label {
  align-items: baseline;
  display: flex;
  font-size: 13px;
  font-weight: 700;
  justify-content: space-between;
}

.value {
  color: var(--accent-strong);
  font-variant-numeric: tabular-nums;
  font-weight: 800;
}

input[type="range"] {
  accent-color: var(--accent);
  width: 100%;
}

.legal-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.legal-link {
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
}

.btn {
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 10px 14px;
  transition: opacity 0.15s;
}

.btn:disabled {
  cursor: default;
  opacity: 0.4;
}

.btn-outline {
  background: #fff;
  border: 2px solid var(--accent);
  color: var(--accent-strong);
}

@media (max-width: 520px) {
  .hud-top {
    flex-wrap: nowrap;
  }
}

/* Phones / tablets — portrait: terrain strip uses horizontal space (edge to edge minus safe area & search) */
@media (max-width: 900px) and (orientation: portrait) {
  .hud-top {
    align-items: center;
    left: max(12px, env(safe-area-inset-left, 12px));
    max-width: none;
    right: max(12px, env(safe-area-inset-right, 12px));
    top: max(10px, env(safe-area-inset-top, 10px));
    width: auto;
  }

  /* App portrait: thinner bar = less padding + slimmer track (not less horizontal width) */
  .terrain-strip {
    --terrain-track-h: 3px;
    --terrain-thumb-size: 16px;

    backdrop-filter: saturate(180%) blur(22px);
    background: var(--terrain-mobile-fill);
    border: 0.5px solid var(--terrain-mobile-border);
    border-radius: 12px;
    box-shadow: var(--terrain-mobile-shadow);
    flex: 1 1 auto;
    max-width: none;
    min-width: 0;
    padding: 4px 11px 5px;
  }

  .terrain-compact.terrain-one-row {
    gap: 6px;
  }

  .terrain-compact input[type="range"] {
    height: 20px;
  }

  .terrain-range-wrap {
    flex: 1 1 auto;
    max-width: none;
    min-width: 0;
  }

  .terrain-title-row {
    font-size: 9px;
    font-weight: 600;
    letter-spacing: -0.02em;
  }

  .terrain-compact .value {
    font-size: 9px;
    font-weight: 700;
  }
}

/* Phone landscape (short height): search above full-height vertical terrain bar on the left */
@media (orientation: landscape) and (max-height: 640px) {
  .hud-top {
    align-items: center;
    /* Lift column off the physical bottom edge for breathing room (safe-area already in calc) */
    height: calc(
      100vh -
        env(safe-area-inset-top, 0px) -
        env(safe-area-inset-bottom, 0px) -
        14px
    );
    left: 0;
    max-width: none;
    padding-left: env(safe-area-inset-left, 0px);
    padding-top: env(safe-area-inset-top, 0px);
    right: auto;
    top: env(safe-area-inset-top, 0px);
  }

  .hud-top .search-anchor {
    flex: 0 0 auto;
  }

  .hud-top .search-popover {
    left: 0;
    right: auto;
  }

  /* Left column thickness (short side) ≈ 20% slimmer; strip length along map unchanged */
  .terrain-strip {
    align-self: stretch;
    backdrop-filter: saturate(180%) blur(22px);
    background: var(--terrain-mobile-fill);
    border: 0.5px solid var(--terrain-mobile-border);
    /* Full rounded corners (same family as portrait / tablet landscape) */
    border-radius: 14px;
    box-shadow: var(--terrain-mobile-shadow);
    flex: 1 1 auto;
    max-height: none;
    min-height: 0;
    max-width: none;
    overflow: hidden;
    padding: 10px 4px 12px;
    width: 38px;
  }

  .terrain-compact.terrain-one-row {
    align-items: center;
    flex-direction: column;
    gap: 6px;
    height: 100%;
    justify-content: center;
    min-height: 0;
  }

  .terrain-title-row {
    flex-direction: column;
    gap: 2px;
    justify-content: center;
    text-align: center;
  }

  .terrain-label-full {
    display: none;
  }

  .terrain-compact .value {
    font-size: 11px;
  }

  .terrain-short-title {
    color: var(--muted);
    display: block;
    flex-shrink: 0;
    font-size: 9px;
    font-weight: 800;
    letter-spacing: 0.12em;
    line-height: 1.2;
    margin-top: 2px;
    text-orientation: mixed;
    white-space: nowrap;
    writing-mode: vertical-rl;
  }

  /*
   * Rotated range: `width` = track length (vertical on screen). Must not use 100cqh here — many WebViews
   * report container height 0 and the track collapses. Use dvh minus reserved chrome (search + titles + pad +
   * hud top/bottom offset); `.terrain-strip { overflow: hidden }` clips rare overflow past the card.
   */
  .terrain-range-wrap {
    align-items: center;
    display: flex;
    /* `1 1 0` fills remaining column height more reliably than `1 1 auto` in Android WebView */
    flex: 1 1 0;
    justify-content: center;
    margin-top: 6px;
    max-height: 100%;
    max-width: none;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
    width: 100%;
  }

  /* Track length = wrap clientHeight; app.js sets width (pre-rotate) in px for reliable WebView layout */
  .terrain-strip .terrain-range-wrap input[type="range"] {
    height: 32px;
    min-width: 0;
    transform: rotate(-90deg);
    transform-origin: center center;
  }

  .search-popover {
    max-height: min(320px, 75vh);
    overflow-y: auto;
  }

  .touch-only-hint {
    display: none;
  }

  .panel {
    left: max(
      10px,
      calc(env(safe-area-inset-left, 0px) + 38px + 8px)
    );
    max-height: min(78vh, calc(100vh - 100px));
    width: min(300px, calc(100vw - 58px));
  }
}

/* Tablet / large landscape: search above top horizontal strip, top-trailing (no left vertical bar) */
@media (orientation: landscape) and (min-height: 641px) {
  .hud-top {
    align-items: flex-end;
    gap: 10px;
    height: auto;
    left: auto;
    max-width: calc(100vw - 24px);
    padding-bottom: 12px;
    padding-left: 0;
    padding-top: max(10px, env(safe-area-inset-top, 10px));
    right: max(12px, env(safe-area-inset-right, 12px));
    top: max(10px, env(safe-area-inset-top, 10px));
  }

  .terrain-strip {
    align-self: auto;
    border-radius: 14px;
    box-shadow:
      0 6px 24px rgba(0, 0, 0, 0.1),
      0 1px 3px rgba(0, 0, 0, 0.06);
    flex: 0 1 auto;
    max-height: none;
    max-width: min(420px, 52vw);
    min-width: min(300px, 44vw);
    /* Thinner bar: less vertical padding (row is horizontal; “thickness” = height) */
    padding: 5px 14px 6px;
    width: auto;
  }

  .terrain-compact.terrain-one-row {
    gap: 12px;
    height: auto;
    justify-content: flex-start;
    min-height: 0;
  }

  .terrain-title-row {
    flex-direction: row;
    font-size: 11px;
    gap: 8px;
    justify-content: flex-start;
    text-align: left;
  }

  .terrain-label-full {
    display: inline;
  }

  .terrain-compact .value {
    font-size: 11px;
  }

  .terrain-short-title {
    display: none;
  }

  .terrain-range-wrap {
    flex: 1 1 auto;
    margin-top: 0;
    max-width: min(220px, 28vw);
    min-height: 0;
    min-width: 120px;
    overflow: visible;
  }

  .terrain-range-wrap input[type="range"] {
    height: 5px;
    transform: none;
    width: 100%;
  }

  .search-popover {
    max-height: min(360px, 70vh);
    overflow-y: auto;
  }

  .panel {
    bottom: max(56px, calc(env(safe-area-inset-bottom, 8px) + 52px + var(--ad-strip-height)));
    left: max(10px, env(safe-area-inset-left, 10px));
    max-height: min(70vh, calc(100vh - 120px));
    width: min(340px, calc(100vw - 24px));
  }
}
