.viz-root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --wash: #f0efec;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --critical: #d03b3b;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--text-primary); background: var(--page); }
#app { display: flex; height: 100vh; }
#side { width: 390px; min-width: 340px; background: var(--surface-1); border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow-y: auto; }
#map { flex: 1; position: relative; }
header, section, footer { padding: 12px 16px; border-bottom: 1px solid var(--grid); }
section.grow { min-height: 260px; display: flex; flex-direction: column; }
h1 { font-size: 20px; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-secondary); margin: 0 0 8px; font-weight: 600; }
.sub, .hint { font-size: 12px; color: var(--text-secondary); margin: 4px 0 0; line-height: 1.4; }
.muted { color: var(--text-muted); text-transform: none; letter-spacing: 0; font-weight: 400; }
.d { color: var(--text-muted); font-size: 11px; line-height: 1.3; }

.btn { display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--axis); background: var(--surface-1); color: var(--text-primary);
  border-radius: 6px; padding: 6px 10px; font: inherit; font-size: 13px; cursor: pointer; }
.btn:hover { background: var(--wash); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #256abf; }
.btn.danger { color: var(--critical); border-color: var(--critical); margin-right: auto; }
.row-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.row-buttons.end { justify-content: flex-end; }

.search { position: relative; }
.search input, .field input, .field select, .field textarea, select {
  width: 100%; font: inherit; font-size: 14px; padding: 7px 8px; border: 1px solid var(--axis); border-radius: 6px; background: var(--surface-1); color: var(--text-primary); }
.results { position: absolute; z-index: 1200; left: 0; right: 0; top: 100%; margin: 2px 0 0; padding: 0; list-style: none; background: var(--surface-1);
  border: 1px solid var(--axis); border-radius: 6px; box-shadow: 0 6px 18px rgba(0,0,0,.12); max-height: 260px; overflow-y: auto; }
.results li { padding: 8px 10px; font-size: 13px; cursor: pointer; border-bottom: 1px solid var(--grid); }
.results li:last-child { border-bottom: 0; }
.results li:hover, .results li.active { background: var(--wash); }
.results li .d { display: block; }
.field { display: block; margin-top: 8px; }
.field > span { display: block; font-size: 12px; color: var(--text-secondary); margin-bottom: 3px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 8px; }

.seg { display: flex; border: 1px solid var(--axis); border-radius: 6px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: transparent; padding: 6px 8px; font: inherit; font-size: 13px; cursor: pointer; color: var(--text-secondary); }
.seg button.on { background: var(--accent); color: #fff; }

.row { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; margin-top: 8px; }
.row output { font-variant-numeric: tabular-nums; color: var(--text-secondary); white-space: nowrap; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; margin: 4px 0; }
.check.strong { margin-top: 12px; font-weight: 600; }
.overlays { margin-top: 10px; }
#weights .hint { margin: 0 0 4px; }
#weights .row { margin-top: 10px; }

#legend { margin-top: 8px; font-size: 12px; color: var(--text-secondary); }
#legend .bar { display: flex; gap: 2px; margin: 4px 0 2px; }
#legend .bar span { flex: 1; height: 10px; border-radius: 2px; }
#legend .ticks { position: relative; height: 15px; font-variant-numeric: tabular-nums; }
#legend .ticks span { position: absolute; transform: translateX(-50%); }
#legend .dir { display: flex; justify-content: space-between; color: var(--text-muted); }

.tablewrap { overflow-y: auto; max-height: 420px; border: 1px solid var(--grid); border-radius: 6px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
th { position: sticky; top: 0; background: var(--surface-1); text-align: left; font-weight: 600; color: var(--text-secondary); padding: 6px; border-bottom: 1px solid var(--axis); cursor: help; }
td { padding: 5px 6px; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
td:first-child { max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--wash); }
.up { color: #b3261e; } .down { color: #006300; }

.im-list { list-style: none; padding: 0; margin: 10px 0 0; }
.im-list li { border: 1px solid var(--grid); border-radius: 8px; padding: 8px 10px; margin-bottom: 6px; font-size: 13px; cursor: pointer; }
.im-list li:hover { background: var(--wash); }
.im-list li.out { opacity: .45; }
.im-list .top { display: flex; justify-content: space-between; gap: 8px; }
.im-list .top b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.im-list .price { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.im-list .empty { color: var(--text-muted); cursor: default; border-style: dashed; }
#im-filters { margin-top: 8px; font-size: 13px; }
#im-filters summary { cursor: pointer; color: var(--text-secondary); }

footer { font-size: 12px; color: var(--text-secondary); border-bottom: 0; }
footer ul { padding-left: 18px; margin: 6px 0; }
footer li { margin: 3px 0; line-height: 1.4; }

.loading { position: absolute; z-index: 1500; left: 50%; top: 14px; transform: translateX(-50%); background: var(--text-primary); color: #fff;
  font-size: 13px; padding: 6px 12px; border-radius: 999px; box-shadow: 0 2px 10px rgba(0,0,0,.2); white-space: nowrap; }
.loading[hidden] { display: none; }
.pick-banner { position: absolute; z-index: 1500; left: 50%; top: 14px; transform: translateX(-50%); background: var(--accent); color: #fff;
  font-size: 14px; padding: 8px 8px 8px 14px; border-radius: 999px; box-shadow: 0 2px 10px rgba(0,0,0,.25); display: flex; gap: 10px; align-items: center; white-space: nowrap; }
.pick-banner[hidden] { display: none; }
.pick-banner button { border: 0; background: rgba(255,255,255,.2); color: #fff; border-radius: 999px; padding: 4px 10px; font: inherit; font-size: 13px; cursor: pointer; }
.picking .leaflet-container { cursor: crosshair; }

/* map */
.leaflet-container { font-family: inherit; background: var(--page); }
.cells-img { image-rendering: pixelated; image-rendering: crisp-edges; }
.bairro-label { background: transparent; border: 0; box-shadow: none; font-size: 11px; font-weight: 600; color: #2b2b29; text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 3px #fff; white-space: nowrap; }
.bairro-label::before, .st-label::before { display: none; }
.st-label { background: rgba(255,255,255,.85); border: 0; box-shadow: none; font-size: 10.5px; padding: 0 3px; color: #2b2b29; }
.dest-icon { background: #0b0b0b; color: #fff; border-radius: 6px; font-size: 12px; font-weight: 700; padding: 3px 7px; white-space: nowrap; box-shadow: 0 0 0 2px #fff, 0 2px 8px rgba(0,0,0,.3); transform: translate(-50%, -50%); display: inline-block; }
.im-pin { background: #fff; color: #0b0b0b; border: 2px solid #0b0b0b; border-radius: 999px; font-size: 11.5px; font-weight: 700; padding: 1px 7px;
  white-space: nowrap; box-shadow: 0 2px 6px rgba(0,0,0,.25); transform: translate(-50%, -50%); display: inline-block; font-variant-numeric: tabular-nums; }
.im-pin.out { opacity: .35; border-color: var(--text-muted); }

/* details (popup on desktop, sheet on phones) */
.pop h3 { margin: 0 0 2px; font-size: 15px; }
.pop .d { margin-bottom: 6px; }
.pop table { font-size: 12px; }
.pop td { padding: 3px 4px; border-bottom: 1px solid var(--grid); vertical-align: top; }
.pop td:first-child { color: var(--text-secondary); max-width: none; white-space: normal; width: 45%; }
.pop .route { font-size: 12px; background: var(--wash); border-radius: 4px; padding: 6px; margin: 6px 0; line-height: 1.45; }
.pop .route .leg { display: inline-block; border-radius: 3px; padding: 0 4px; margin: 1px 0; color: #fff; font-weight: 600; }
.pop .route .leg.bus { background: #52514e; }
.pop .scores { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin: 6px 0; text-align: center; }
.pop .scores div { background: var(--wash); border-radius: 4px; padding: 3px 0; font-size: 11px; color: var(--text-secondary); }
.pop .scores b { display: block; font-size: 14px; color: var(--text-primary); }
.pop .cost { font-size: 13px; margin: 6px 0; }
.pop .cost b { font-size: 16px; }
.pop .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }

/* details panel: always centred on the page, scrolls inside when long */
#sheet { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 3000;
  width: min(440px, calc(100vw - 24px)); max-height: 85vh; max-height: 85dvh; overflow-y: auto;
  background: var(--surface-1); border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,.3); padding: 8px 16px 18px;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
#sheet[hidden] { display: none; }
#sheet-close { position: sticky; top: 0; display: block; margin: 0 0 6px auto; border: 1px solid var(--axis); background: var(--surface-1);
  border-radius: 999px; padding: 8px 14px; font: inherit; font-size: 14px; color: var(--text-primary); cursor: pointer; z-index: 1; }
#sheet .pop table { width: 100%; }

dialog { border: 0; border-radius: 12px; padding: 16px; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto;
  background: var(--surface-1); color: var(--text-primary); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin: 0 0 4px; }
.form-error { color: var(--critical); font-size: 13px; }

/* phones: map on top, controls below, page scrolls */
@media (max-width: 768px) {
  html, body { height: auto; }
  #app { flex-direction: column-reverse; height: auto; }
  #map { flex: none; height: 62vh; height: 62dvh; }
  #side { width: 100%; min-width: 0; border-right: 0; overflow: visible; }
  section.grow { min-height: 0; }
  .tablewrap { max-height: 70vh; }
  .seg button { padding: 10px 8px; font-size: 14px; }
  .btn { padding: 9px 12px; font-size: 14px; }
  .check { font-size: 14px; margin: 8px 0; }
  .check input { width: 18px; height: 18px; }
  input[type=range] { height: 28px; }
  td { padding: 8px 6px; }
  td:first-child { max-width: 46vw; }
  .pop table { font-size: 11.5px; }
  .pick-banner { font-size: 13px; }
}

/* header link, warnings, source lines */
.title-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.head-link, footer a { font-size: 12px; color: var(--accent); text-decoration: none; white-space: nowrap; }
.head-link:hover, footer a:hover { text-decoration: underline; }
.warn { display: flex; gap: 8px; align-items: flex-start; background: #fff4dc; border: 1px solid #f3d38a; border-radius: 6px;
  padding: 7px 9px; margin: 6px 0; font-size: 12px; line-height: 1.4; color: #3d3320; }
.warn > span { font-size: 14px; line-height: 1.2; }
.warn a, .src a { color: var(--accent); }
.warn-mark { color: #b06d00; font-weight: 700; cursor: help; }
.src { margin-top: 8px; font-size: 10.5px; color: var(--text-muted); line-height: 1.4; }

/* search feedback */
.results li.status { cursor: default; color: var(--text-secondary); display: flex; align-items: center; gap: 8px; }
.results li.status:hover { background: none; }
.results li.src { cursor: default; font-size: 10.5px; color: var(--text-muted); padding: 4px 10px; }
.results li.src:hover { background: none; }
.spinner { width: 14px; height: 14px; flex: none; border-radius: 50%; border: 2px solid var(--grid); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
