:root {
    --page: #f9f9f7;
    --surface: #fcfcfb;
    --ink: #0b0b0b;
    --ink-2: #52514e;
    --muted: #898781;
    --hairline: #e1e0d9;
    --border: rgba(11, 11, 11, 0.10);
    --accent: #c22d2c;
}
@media (prefers-color-scheme: dark) {
    :root {
        --page: #0d0d0d;
        --surface: #1a1a19;
        --ink: #ffffff;
        --ink-2: #c3c2b7;
        --muted: #898781;
        --hairline: #2c2c2a;
        --border: rgba(255, 255, 255, 0.10);
        --accent: #e34948;
    }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    display: flex;
    flex-direction: column;
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--page);
    color: var(--ink);
}

/* Search controls bar atop the map panel. */
#map-controls {
    padding: 10px 16px 8px;
    border-bottom: 1px solid var(--hairline);
    background: var(--surface);
}
.title-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
    margin-bottom: 8px;
}
#map-controls h1 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 650;
}
.repo-link {
    font-size: 0.8rem;
    color: var(--ink-2);
    white-space: nowrap;
}
.title-links {
    display: flex;
    align-items: baseline;
    gap: 14px;
    white-space: nowrap;
}
/* The "How to read" button, dressed to sit beside the repo link. */
.link-btn {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}
.link-btn:hover {
    color: var(--ink);
}

.info-dialog {
    max-width: 34rem;
    width: calc(100% - 32px);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px 24px 20px;
    background: var(--surface);
    color: var(--ink);
    box-shadow: 0 12px 44px rgba(0, 0, 0, 0.28);
}
.info-dialog::backdrop {
    background: rgba(0, 0, 0, 0.4);
}
.info-dialog form {
    display: flex;
    justify-content: flex-end;
}
.dialog-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    color: var(--muted);
    cursor: pointer;
    padding: 0 4px;
    margin: -4px -10px 0 0;
}
.dialog-close:hover {
    color: var(--ink);
}
.info-dialog h2 {
    margin: 0 0 12px;
    font-size: 1.1rem;
    font-weight: 650;
}
.info-dialog ul {
    margin: 0;
    padding-left: 1.1em;
    display: flex;
    flex-direction: column;
    gap: 9px;
    font-size: 0.9rem;
    line-height: 1.45;
    color: var(--ink-2);
}
.info-dialog li b {
    color: var(--ink);
    font-weight: 620;
}
.info-dialog .fine-print {
    margin: 14px 0 0;
}
/* The Terrain Tiles source list: each provider's required attribution string,
   small and muted since it's legal boilerplate. */
.attr-sources {
    margin: 14px 0 0;
    padding: 12px 0 0 1.1em;
    border-top: 1px solid var(--hairline);
    display: flex;
    flex-direction: column;
    gap: 5px;
    font-size: 0.76rem;
    line-height: 1.4;
    color: var(--muted);
    max-height: 40vh;
    overflow-y: auto;
}
/* Ranking modes as a 2-column table: term | description, one row per mode. */
.info-dialog dl {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 3px 14px;
    margin: 5px 0 2px;
}
.info-dialog dt {
    font-style: italic;
    color: var(--ink);
    white-space: nowrap;
}
.info-dialog dd {
    margin: 0;
}
/* #controls = search (place/radius/go) in the map panel, laid out in a row;
   #rank-controls = ranking inputs in the rank panel, stacked. Both share the
   field styling below. */
#controls {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 16px;
    align-items: center;
}
/* Color toggle rides at the right end of the search row — display option, not a
   search field, so it's pushed away from place/radius/go. */
#color-ctrl { margin-left: auto; display: inline-flex; align-items: center; gap: 8px; }
/* Segmented control: buttons joined edge-to-edge, the active one filled with the
   accent — one click to switch, no dropdown. Pattern borrowed from the comma app,
   retinted to the theme tokens so it works in light and dark. */
.segmented { display: inline-flex; }
.seg-btn {
    font: inherit;
    color: var(--ink-2);
    background: var(--page);
    border: 1px solid var(--hairline);
    padding: 5px 12px;
    margin-left: -1px; /* collapse the shared edge so neighbors share one border */
    cursor: pointer;
}
.seg-btn:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg-btn:last-child { border-radius: 0 6px 6px 0; }
.seg-btn:hover:not(.selected) { color: var(--ink); border-color: var(--muted); }
.seg-btn.selected {
    color: #fff;
    background: var(--accent);
    border-color: var(--accent);
    position: relative; /* lift the selected button so its full border shows over the seam */
    z-index: 1;
}
.seg-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#rank-controls {
    padding: 12px 12px;
    border-bottom: 1px solid var(--hairline);
    display: flex;
    flex-direction: column;
    gap: 9px;
}
.ctrl-row { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; }
#controls, #rank-controls { font-size: 0.85rem; color: var(--ink-2); }
#controls label, #rank-controls label { display: inline-flex; align-items: center; gap: 6px; }
#controls input, #controls select,
#rank-controls input, #rank-controls select {
    font: inherit;
    color: var(--ink);
    background: var(--page);
    border: 1px solid var(--hairline);
    border-radius: 6px;
    padding: 5px 8px;
}
#controls input:focus, #controls select:focus,
#rank-controls input:focus, #rank-controls select:focus { outline: 2px solid var(--accent); outline-offset: 0; }
#radius, #window, #longlen, #listmax { width: 4.5em; }
#go {
    font: inherit;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border: none;
    border-radius: 6px;
    padding: 6px 14px;
    cursor: pointer;
}
#go:disabled { opacity: 0.55; cursor: default; }
#status { min-height: 1.2em; margin-top: 6px; font-size: 0.8rem; color: var(--muted); }
#status.error { color: var(--accent); }
#progress {
    display: none;
    width: 280px;
    height: 3px;
    margin-top: 4px;
    background: var(--hairline);
    border-radius: 2px;
    overflow: hidden;
}
#progress.active { display: block; }
#progress-fill {
    height: 100%;
    width: 0;
    background: var(--accent);
    border-radius: 2px;
    transition: width 0.2s;
}
#progress.indeterminate #progress-fill { width: 30%; animation: progress-slide 1.2s linear infinite; }
@keyframes progress-slide {
    from { margin-left: -30%; }
    to { margin-left: 100%; }
}
.linklike {
    padding: 0;
    font: inherit;
    color: var(--accent);
    background: none;
    border: none;
    text-decoration: underline;
    cursor: pointer;
}

main { flex: 1; display: flex; min-height: 0; }

/* Left panel: search controls bar + the map filling the rest. */
#map-panel { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
#map { flex: 1; min-width: 0; min-height: 0; background: var(--page); }

/* Right panel: ranking controls (fixed) + a scrolling list area. */
#rank-panel {
    width: 380px;
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-left: 1px solid var(--hairline);
    background: var(--surface);
}
/* The list rows sit at this reduced side padding so the rank numbers hug the
   edge; the header/sub/footer add the difference back for a comfortable indent. */
#rank-list-area { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 6px 12px 2px; }
.list-head, #list-sub, .fine-print { padding-left: 10px; padding-right: 6px; }
#list-title { margin: 0; font-size: 0.95rem; font-weight: 650; }
.list-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
#download {
    flex: none;
    font: inherit;
    font-size: 0.75rem;
    color: var(--ink-2);
    background: none;
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 2px 8px;
    cursor: pointer;
    white-space: nowrap;
}
#download:hover { border-color: var(--muted); }
#list-sub { margin: 2px 0 10px; font-size: 0.78rem; color: var(--muted); }

.road-row {
    display: grid;
    grid-template-columns: 1.2rem 1fr auto;
    gap: 6px;
    align-items: center;
    width: 100%;
    padding: 6px 0; /* #rank-list-area already pads horizontally */
    margin: 0 0 2px;
    font: inherit;
    text-align: left;
    color: inherit;
    background: none;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}
.road-row:hover, .road-row:focus-visible { background: var(--page); outline: none; }
.road-rank { font-size: 0.75rem; color: var(--muted); text-align: right; }
.road-main { min-width: 0; }
.road-name {
    display: inline-block;
    max-width: 70%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: bottom;
    font-size: 0.85rem;
    font-weight: 550;
}
.road-sub { margin-left: 6px; font-size: 0.72rem; color: var(--muted); }
.road-track { display: block; margin-top: 3px; height: 10px; }
.road-bar {
    display: block;
    height: 100%;
    border-radius: 0 4px 4px 0;
    box-shadow: inset 0 0 0 1px var(--border);
}
.road-value {
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    color: var(--ink-2);
}

.fine-print { margin-top: 14px; font-size: 0.72rem; line-height: 1.4; color: var(--muted); }
.fine-print a { color: var(--ink-2); }

/* Leaflet legend + popup */
.legend {
    padding: 7px 10px 8px;
    background: var(--surface);
    color: var(--ink-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    font: 11px/1.3 system-ui, sans-serif;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}
.legend-title { margin-bottom: 4px; color: var(--muted); }
.legend-bar { width: 130px; height: 8px; border-radius: 4px; }
.legend-row { display: flex; align-items: center; gap: 6px; margin-bottom: 3px; }
.legend-label { font-size: 10px; color: var(--muted); white-space: nowrap; }
.legend-grind { margin: 6px 0 0; padding-top: 5px; border-top: 1px solid var(--hairline); }
.legend-swatch { width: 16px; height: 8px; border-radius: 2px; flex: none; }
/* Position each tick at its value's spot on the 130px bar: 5% at the left
   edge, 15% centered on the midpoint, 25%+ centered on the right edge. The end
   labels overflow their point into the legend's slack — a wide label like
   "25%+" can't be edge-anchored without shoving its center well off the mark. */
.legend-ticks { position: relative; height: 14px; margin-top: 2px; width: 130px; font-variant-numeric: tabular-nums; }
.legend-ticks span { position: absolute; top: 0; }
.legend-ticks span:nth-child(1) { left: 0; }
.legend-ticks span:nth-child(2) { left: 50%; transform: translateX(-50%); }
.legend-ticks span:nth-child(3) { left: 100%; transform: translateX(-50%); }

.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--ink); }
.popup { font-size: 12px; min-width: 210px; }
.popup-name { font-weight: 650; font-size: 13px; margin-bottom: 6px; }
.popup-row { display: flex; justify-content: space-between; gap: 14px; padding: 1px 0; color: var(--ink-2); }
.popup-row b { font-variant-numeric: tabular-nums; color: var(--ink); font-weight: 550; }
.popup-active { color: var(--ink); font-weight: 600; }
.popup-active b { color: var(--accent); }
/* City-wide rank tag trailing a container value (e.g. "11.8% #14"). */
.popup-rank { color: var(--ink-2); font-weight: 550; margin-left: 4px; }

@media (max-width: 760px) {
    main { flex-direction: column; }
    #map-panel { flex: none; }
    #map { min-height: 46vh; }
    #rank-panel { flex: 1; width: auto; border-left: none; border-top: 1px solid var(--hairline); }
}
