:root {
  --bg: #0c1826;
  --panel: rgba(16, 24, 32, 0.9);
  --panel-solid: #121c26;
  --text: #eef0ea;
  --muted: #a9b1a8;
  --line: rgba(255, 255, 255, 0.14);
  --accent: #f2d9a0;
  --topbar-h: 46px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --lens-dock-h: 0px;
  --gap: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

button { font: inherit; color: inherit; }

#map {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}
#map.dragging { cursor: grabbing !important; } /* beats app.setCursor() while dragging */
#map:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; }

/* a visible focus ring for keyboard users on every control */
button:focus-visible, [tabindex]:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.dock-panel:focus { outline: none; }
.dock-panel:focus-visible { outline: 1.5px solid rgba(217, 186, 123, 0.5); outline-offset: -1.5px; }

/* ------------------------------------------------------------------ top bar */
.topbar {
  position: fixed;
  z-index: 5;
  top: 0;
  left: 0;
  right: 0;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) 8px 0 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(rgba(10, 16, 22, 0.94), rgba(14, 22, 30, 0.88));
  border-bottom: 1px solid var(--line);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
}
.topbar .brand {
  display: flex; align-items: center; gap: 8px; flex: none;
  background: none; border: 1px solid transparent; border-radius: 7px;
  padding: 4px 7px; margin-left: -4px; cursor: pointer;
  transition: background 0.14s, border-color 0.14s;
}
.topbar .brand:hover { background: rgba(255, 255, 255, 0.06); border-color: var(--line); }
.topbar .logo { width: 22px; height: 22px; flex: none; display: block; }
.topbar h1 {
  margin: 0;
  font: 600 17px/1 Georgia, "Times New Roman", "Noto Serif", serif;
  letter-spacing: 0.06em;
  color: var(--accent);
}
.topbar .stats {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 2px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  height: 100%;
}
.topbar .stats::-webkit-scrollbar { display: none; }
.topbar .stats.more-r { -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent); mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent); }
.topbar .stats.more-l { -webkit-mask-image: linear-gradient(90deg, transparent, #000 24px); mask-image: linear-gradient(90deg, transparent, #000 24px); }
.topbar .stats.more-l.more-r { -webkit-mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent); mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent); }
.topbar .stat {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 32px;
  padding: 0 8px;
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: help;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.topbar .stat .ico { color: var(--c); flex: none; }
.topbar .stat .val { color: var(--text); font-weight: 600; }
.topbar .stat .est { color: var(--muted); font-size: 12px; margin-left: -2px; }
.topbar .stat:hover, .topbar .stat:focus-visible { border-color: var(--line); background: rgba(255, 255, 255, 0.05); }
.topbar .stat:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.topbar .actions { flex: none; display: flex; align-items: center; gap: 6px; }
.tb-tools, .tb-meta { display: inline-flex; align-items: center; gap: 6px; }
.tb-tools:empty { display: none; }
.tb-modes {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  background: rgba(255, 255, 255, 0.045);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.tb-btn {
  min-width: 34px;
  height: 32px;
  padding: 0 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
}
a.tb-btn { text-decoration: none; color: inherit; }
.tb-btn:hover { border-color: rgba(255, 255, 255, 0.35); }
.tb-btn[hidden] { display: none; }
.tb-btn[aria-pressed="true"] { background: rgba(242, 217, 160, 0.18); border-color: rgba(242, 217, 160, 0.5); }

.lang-menu {
  position: relative;
  display: inline-flex;
  align-items: center;
}
.lang-btn {
  gap: 4px;
  padding: 0 8px;
}
.lang-btn[aria-expanded="true"] {
  background: rgba(242, 217, 160, 0.16);
  border-color: rgba(242, 217, 160, 0.45);
  color: #fff6dc;
}
.lang-caret {
  font-size: 10px;
  line-height: 1;
  opacity: 0.8;
  transition: transform 0.14s ease;
}
.lang-btn[aria-expanded="true"] .lang-caret { transform: rotate(180deg); }
.lang-popover {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 168px;
  max-width: calc(100vw - 16px);
  max-height: calc(100dvh - var(--topbar-h) - var(--safe-top) - 16px);
  overflow-y: auto;
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: rgba(15, 24, 34, 0.97);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 9px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.5);
}
.lang-popover[hidden] { display: none; }
.lang-opt {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 9px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.25;
  text-align: left;
  cursor: pointer;
  color: var(--text);
}
.lang-opt:hover,
.lang-opt:focus-visible {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.14);
  outline: none;
}
.lang-opt.is-active {
  background: rgba(242, 217, 160, 0.16);
  border-color: rgba(242, 217, 160, 0.42);
  color: #fff6dc;
}
.lang-opt-code {
  flex: none;
  width: 24px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.lang-opt.is-active .lang-opt-code { color: var(--accent); }
.lang-opt-name { flex: 1 1 auto; font-weight: 500; }
.lang-opt-check {
  flex: none;
  width: 14px;
  text-align: center;
  font-size: 12px;
  color: var(--accent);
}
.tb-modes .tb-mode-btn {
  height: 28px;
  min-width: 30px;
  padding: 0 9px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  border-radius: 6px;
  font-size: 12.5px;
  letter-spacing: 0.02em;
}
.tb-modes .tb-mode-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.07);
  border-color: transparent;
}
.tb-modes .tb-mode-btn[aria-pressed="true"] {
  background: rgba(242, 217, 160, 0.2);
  border-color: rgba(242, 217, 160, 0.55);
  color: #fff6dc;
}

.stat-tip {
  position: fixed;
  z-index: 8;
  width: max-content;
  max-width: min(300px, calc(100vw - 16px));
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.45;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}
.stat-tip .head { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; }
.stat-tip .big { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; margin: 2px 0 4px; }
.stat-tip p { margin: 3px 0 0; }
.stat-tip .muted { color: var(--muted); }
.stat-tip .warn { color: #f0b46a; }

/* ------------------------------------------------------------------ map controls, status */
.controls {
  position: fixed;
  z-index: 3;
  right: var(--gap);
  bottom: calc(var(--gap) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: right 0.18s ease-out;
}
body.has-dock-right .controls {
  right: calc(var(--gap) * 2 + min(392px, calc(100vw - 2 * var(--gap))));
}
.controls button {
  width: 40px;
  height: 40px;
  font-size: 20px;
  line-height: 1;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.controls button:hover { border-color: rgba(255, 255, 255, 0.35); }
.controls button[hidden] { display: none; }
.controls #reset:hover { border-color: rgba(242, 217, 160, 0.5); color: var(--accent); }

.status {
  position: fixed;
  z-index: 1;
  left: var(--gap);
  bottom: calc(6px + var(--safe-bottom));
  font-size: 11px;
  color: var(--muted);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  pointer-events: none;
}
body.has-lens-dock .status { display: none; }

/* ------------------------------------------------------------------ tooltip */
#tooltip {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 6;
  min-width: 216px;
  max-width: min(280px, calc(100vw - 24px));
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.45;
  background: rgba(16, 25, 35, 0.95);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.42);
  pointer-events: none;
  will-change: transform;
}
#tooltip .title { font-size: 13px; font-weight: 600; }
#tooltip .sub { color: var(--muted); margin-bottom: 4px; }
#tooltip table { border-collapse: collapse; width: 100%; }
#tooltip td { padding: 0; }
#tooltip td.v { text-align: right; padding-left: 12px; font-variant-numeric: tabular-nums; }
#tooltip tr.ext td { font-weight: 600; }
#tooltip .bar { display: inline-block; height: 6px; border-radius: 2px; vertical-align: middle; margin-left: 6px; }
#tooltip .note { margin-top: 4px; color: var(--muted); font-size: 11px; }
#tooltip table.tip-rows { margin-top: 4px; }

/* ------------------------------------------------------------------ docked panels */
.dock {
  position: fixed;
  z-index: 4;
  top: calc(var(--topbar-h) + var(--safe-top) + var(--gap));
  bottom: calc(var(--gap) + var(--safe-bottom));
  width: min(392px, calc(100vw - 2 * var(--gap)));
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.dock:empty { display: none; }
.dock-left { left: var(--gap); bottom: calc(var(--gap) + var(--safe-bottom) + var(--lens-dock-h)); }
.dock-right { right: var(--gap); }
.dock-panel {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: 100%;
  background: rgba(15, 24, 34, 0.95);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.48);
  overflow: hidden;
}
.dock-grip {
  display: none;
}
.dock-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 14px;
  background: rgba(20, 31, 44, 0.85);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.dock-head h2 {
  flex: 1;
  margin: 0;
  font: 600 17px/1.2 Georgia, "Times New Roman", "Noto Serif", serif;
  letter-spacing: 0.01em;
  color: #f4efe4;
}
.dock-close {
  width: 34px; height: 34px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
}
.dock-close:hover { color: var(--text); border-color: var(--line); background: rgba(255, 255, 255, 0.09); }
.dock-body {
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  padding: 12px 14px 16px;
  font-size: 13px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.22) transparent;
}

/* ------------------------------------------------------------------ lens dock slot, toasts */
.lens-dock {
  position: fixed;
  z-index: 3;
  left: calc(var(--gap) + 52px);
  right: calc(var(--gap) + 52px);
  width: fit-content;
  margin-inline: auto;
  bottom: calc(var(--gap) + var(--safe-bottom));
  max-width: calc(100vw - 2 * var(--gap) - 104px);
}
body.has-dock-right .lens-dock {
  left: var(--gap);
  right: calc(var(--gap) * 2 + min(392px, calc(100vw - 2 * var(--gap))) + 52px);
  max-width: calc(100vw - min(392px, calc(100vw - 2 * var(--gap))) - 3 * var(--gap) - 56px);
}
.lens-dock:empty { display: none; }

.toasts {
  position: fixed;
  z-index: 9;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--gap) + var(--safe-bottom) + var(--lens-dock-h) + 8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  padding: 7px 12px;
  font-size: 13px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.in { opacity: 1; transform: none; }

/* ------------------------------------------------------------------ phones */
@media (max-width: 600px) {
  :root { --topbar-h: 44px; --gap: 8px; }
  .topbar { gap: 6px; padding-left: 10px; padding-right: 6px; }
  .topbar h1 { font-size: 15px; }
  .topbar .stat { padding: 0 6px; gap: 4px; font-size: 13px; }
  .topbar .actions { gap: 4px; min-width: 0; }
  .tb-tools, .tb-meta { gap: 4px; }
  /* finger-sized targets: 40 px, up from 32 x 34 and 28 x 31 */
  .tb-btn { padding: 0 7px; max-width: 112px; min-width: 40px; height: 40px; }
  .tb-modes .tb-mode-btn { padding: 0 7px; min-width: 38px; height: 36px; }
  .tb-btn .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tb-btn:has(.ico) .lbl { display: none; }
  .lang-opt { min-height: 38px; padding: 8px 10px; }

  /* side panels become one bottom sheet with peek / half / full snap heights */
  .dock, .dock-left, .dock-right {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 58dvh;
    padding: 0 0 var(--safe-bottom);
    gap: 0;
    transition: max-height 0.2s ease-out;
  }
  .dock[data-sheet="peek"] { max-height: 168px; }
  .dock[data-sheet="full"] { max-height: calc(100dvh - var(--topbar-h) - var(--safe-top) - 8px); }
  .dock-panel { border-radius: 14px 14px 0 0; border-bottom: none; }
  .dock-grip {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 20px;
    padding: 0;
    background: rgba(20, 31, 44, 0.9);
    border: 0;
    cursor: pointer;
    flex: none;
  }
  .dock-grip::before {
    content: "";
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.28);
  }
  .dock-head { padding-top: 2px; }
  .dock-close { width: 40px; height: 40px; }
  .dock-left .dock-panel + .dock-panel, .dock-right .dock-panel + .dock-panel { border-radius: 0; }
  body.has-dock .controls { display: none; }
  body.has-dock .lens-dock { display: none; }
  body.has-dock .status { display: none; }

  .lens-dock, body.has-dock-right .lens-dock { left: var(--gap); right: calc(var(--gap) + 48px); width: auto; margin-inline: 0; transform: none; max-width: none; }

  /* the tooltip docks to the bottom instead of following the finger */
  #tooltip.docked {
    left: var(--gap) !important;
    right: 60px;
    top: auto !important;
    bottom: calc(var(--gap) + var(--safe-bottom) + var(--lens-dock-h));
    max-width: none;
    transform: none !important;
  }
}

/* narrow phones: the flag alone is the brand, which makes room for the 40 px buttons (the name
   stays in the page for screen readers; the button keeps its label) */
@media (max-width: 440px) {
  .topbar { gap: 4px; }
  .topbar h1 { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}

/* ------------------------------------------------------------------ reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
