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

:root {
    --bg: #14182022;
    --panel: rgba(255, 255, 255, 0.05);
    --panel-border: rgba(255, 255, 255, 0.09);
    --accent: #f7931e;
    --text: #e8eaed;
    --muted: #8a909a;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: var(--text);
    min-height: 100vh;
    padding: 20px 24px 40px;
}

.back-link {
    color: var(--muted);
    text-decoration: none;
    font-size: 0.9rem;
}
.back-link:hover { color: var(--accent); }

.header-bar {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 20px;
    flex-wrap: wrap;
    margin-bottom: 18px;
}
.header-bar h1 {
    font-size: 1.9rem;
    margin: 6px 0 4px;
}
.header-icon { margin-right: 6px; }
.subtitle {
    color: var(--muted);
    font-size: 0.95rem;
    max-width: 640px;
}

.world-select-wrap {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.world-select-wrap label {
    font-size: 0.8rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
select, input[type="search"] {
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--panel-border);
    color: var(--text);
    border-radius: 8px;
    padding: 9px 12px;
    font-size: 0.95rem;
    outline: none;
    width: 100%;
}
select:focus, input[type="search"]:focus { border-color: var(--accent); }
/* Dark, high-contrast popup list (native option rendering). */
select option {
    background: #1b1f2a;
    color: #eef1f6;
}

.status {
    min-height: 0;
    color: var(--accent);
    font-size: 0.9rem;
    margin-bottom: 8px;
}
.status.error { color: #ff6b6b; }

.layout {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 20px;
    align-items: start;
}

/* Sidebar */
.sidebar {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    padding: 16px;
    position: sticky;
    top: 16px;
    max-height: calc(100vh - 40px);
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.sidebar-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding-top: 12px;
    border-top: 1px solid var(--panel-border);
}
.sidebar-head h2 { font-size: 1.1rem; }
.route-count { color: var(--muted); font-size: 0.8rem; }
.route-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;
    margin: 0 -6px;
    padding: 0 6px;
}
.route-list li {
    padding: 9px 11px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    line-height: 1.3;
    border: 1px solid transparent;
    transition: background 0.15s, border-color 0.15s;
}
.route-list li:hover { background: rgba(255, 255, 255, 0.06); }
.route-list li.active {
    background: rgba(247, 147, 30, 0.14);
    border-color: rgba(247, 147, 30, 0.5);
}
#route-list li, #road-list li {
    position: relative;
    padding-right: 33px;
}
.route-list li[aria-busy="true"]::after {
    content: '';
    position: absolute;
    top: 11px;
    right: 11px;
    width: 14px;
    height: 14px;
    border: 2px solid var(--panel-border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: list-loading-spin 0.8s linear infinite;
}
@keyframes list-loading-spin {
    to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
    .route-list li[aria-busy="true"]::after { animation: none; }
}
.route-list li .r-meta {
    display: block;
    color: var(--muted);
    font-size: 0.78rem;
    margin-top: 2px;
}
.road-route-picker {
    width: min(420px, calc(100vw - 32px));
    max-height: 80vh;
    margin: auto;
    padding: 16px;
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    background: #202328;
    color: var(--text);
}
.road-route-picker[open] { display: flex; flex-direction: column; gap: 12px; }
.road-route-picker::backdrop { background: rgba(0, 0, 0, 0.55); }
.road-picker-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.road-picker-head h2 { font-size: 1.1rem; }
.road-picker-head button { width: 32px; height: 32px; padding: 0; font-size: 1.5rem; line-height: 1; }
.road-picker-status { color: #ff6b6b; font-size: 0.85rem; }
.road-route-picker input {
    width: 100%;
    flex-shrink: 0;
    border: 1px solid var(--panel-border);
    border-radius: 6px;
    padding: 8px 10px;
    background: rgba(0, 0, 0, 0.25);
    color: inherit;
    font: inherit;
}
.road-picker-status:empty { display: none; }
.road-route-picker .route-list { margin: 0; padding: 0; min-height: 0; }
.road-route-picker li { padding-right: 33px; position: relative; }
.road-route-picker li button {
    display: block;
    width: 100%;
    border: 0;
    padding: 0;
    background: none;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
}
.road-route-picker li button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.route-list li .r-badge {
    display: inline-block;
    font-size: 0.68rem;
    color: var(--accent);
    border: 1px solid rgba(247, 147, 30, 0.4);
    border-radius: 4px;
    padding: 0 4px;
    margin-left: 6px;
    vertical-align: middle;
}

/* Sport (cycling/running) filter + per-route tag */
.sport-filter {
    display: flex;
    gap: 4px;
}
.sport-btn {
    flex: 1;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--panel-border);
    color: var(--muted);
    border-radius: 8px;
    padding: 6px 0;
    font-size: 0.82rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.sport-btn:hover { border-color: var(--accent); color: var(--text); }
.sport-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.sport-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #0d1220;
    font-weight: 600;
}
.route-list li .sport-tag {
    display: inline-block;
    font-size: 0.68rem;
    border-radius: 4px;
    padding: 0 4px;
    margin-left: 6px;
    vertical-align: middle;
    border: 1px solid var(--panel-border);
    color: var(--muted);
}
.route-list li .sport-tag.sport-cycling {
    color: #4fc3f7;
    border-color: rgba(79, 195, 247, 0.4);
}
.route-list li .sport-tag.sport-running {
    color: #81c784;
    border-color: rgba(129, 199, 132, 0.4);
}

/* Main */
.main { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

.map-panel, .route-detail {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    padding: 14px 16px;
}

.map-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
    margin-bottom: 10px;
}
.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 14px;
    font-size: 0.82rem;
}
.legend .item { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.legend .swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.4);
}
.map-actions { display: flex; align-items: center; gap: 14px; }
.btn-sm {
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid var(--panel-border);
    color: var(--text);
    border-radius: 8px;
    padding: 7px 12px;
    font-size: 0.85rem;
    cursor: pointer;
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }
.toggle { font-size: 0.85rem; color: var(--muted); display: flex; align-items: center; gap: 6px; cursor: pointer; }

.canvas-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 10;
    background: #0d1220;
    border-radius: 10px;
    overflow: hidden;
}
#map-canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: grab;
}
#map-canvas:active { cursor: grabbing; }
#map-canvas[hidden], #satellite-map[hidden] { display: none; }
#satellite-map { position: absolute; inset: 0; }
.map-scale {
    position: absolute;
    bottom: 16px;
    left: 16px;
    padding: 6px 8px;
    border-radius: 4px;
    background: rgba(16, 24, 32, 0.85);
    color: #fff;
    font-size: 12px;
    line-height: 1.4;
    text-align: center;
    pointer-events: none;
    z-index: 2;
}
.map-scale[hidden] { display: none; }
.map-scale.satellite-scale { bottom: 32px; }
.map-scale-bar {
    height: 7px;
    border: solid #fff;
    border-width: 0 2px 2px;
    box-sizing: border-box;
}
.map-hint {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 0.95rem;
    pointer-events: none;
    text-align: center;
    padding: 20px;
}

/* Route detail */
.route-detail-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 12px;
}
.route-detail-head h2 { font-size: 1.25rem; }
.route-detail-actions { display: flex; gap: 8px; align-items: center; }
.rd-stats { display: flex; gap: 18px; flex-wrap: wrap; }
.rd-stats .stat { text-align: right; }
.rd-stats .stat .v { font-size: 1.05rem; font-weight: 600; }
.rd-stats .stat .k {
    display: block;
    font-size: 0.72rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.rd-breakdown {
    margin-bottom: 14px;
}
.rd-breakdown .bar {
    display: flex;
    height: 16px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid rgba(0, 0, 0, 0.4);
}
.rd-breakdown .bar span { height: 100%; }
.rd-breakdown .bkey {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    margin-top: 8px;
    font-size: 0.82rem;
    color: var(--muted);
}
.rd-breakdown .bkey .item { display: flex; align-items: center; gap: 6px; }
.rd-breakdown .bkey .swatch {
    width: 12px; height: 12px; border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.4);
}

.elevation-chart { width: 100%; height: 300px; }

/* Dev road inspector */
.route-list li .swatch {
    display: inline-block;
    width: 10px; height: 10px; border-radius: 3px;
    border: 1px solid rgba(0, 0, 0, 0.4);
    margin: 0 4px 0 2px;
    vertical-align: middle;
}
.route-list li .road-id {
    color: var(--accent);
    font-variant-numeric: tabular-nums;
    font-size: 0.82rem;
}
.route-list li .muted { color: var(--muted); }
.dev-road-list li { display: block; }

.view-filter {
    width: 100%;
    margin: 8px 0 4px;
    text-align: center;
}
.view-filter.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #0d1220;
    font-weight: 600;
}
.view-filter.active:hover { color: #0d1220; }

.dev-road-detail { min-height: 0; }
.dev-browser-tabs { display: flex; gap: 2px; margin: 12px 0 4px; border-bottom: 1px solid var(--panel-border); }
.dev-browser-tabs button {
    flex: 1; padding: 9px 4px; border: 0; border-bottom: 2px solid transparent;
    background: transparent; color: var(--muted); font: inherit; font-size: 0.85rem; cursor: pointer;
}
.dev-browser-tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.dev-browser-list[hidden], #arch-detail[hidden], #spawn-detail[hidden], #arch-tooltip[hidden] { display: none; }
.arch-row {
    display: block; width: 100%; padding: 0; border: 0; background: transparent;
    color: inherit; font: inherit; text-align: left; cursor: pointer; overflow-wrap: anywhere;
}
.arch-row .r-meta { white-space: normal; }
.arch-unlocated { color: #e8ba6e; }
.arch-fields { display: grid; grid-template-columns: minmax(120px, 1fr) minmax(0, 3fr); gap: 8px 16px; font-size: 0.85rem; }
.arch-fields dt { color: var(--muted); }
.arch-fields dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.arch-location-status { color: #76ddba; font-size: 0.85rem; margin-bottom: 12px; }
.arch-location-status.incomplete { color: #e8ba6e; }
.arch-map-key { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.arch-start-key, .arch-finish-key { width: 10px; height: 10px; border-radius: 50%; display: inline-block; border: 1px solid #141b22; }
.arch-start-key { background: #64e3bb; }
.arch-finish-key { background: #fff; }
.arch-tooltip {
    position: absolute; z-index: 3; pointer-events: none; padding: 8px 10px;
    max-width: min(300px, calc(100% - 16px)); border: 1px solid var(--panel-border);
    border-radius: 4px; background: #151c24; color: var(--text); font-size: 0.8rem;
    white-space: pre-line; overflow-wrap: anywhere;
}
.map-toolbar:has(#toggle-arches) { flex-wrap: wrap; }
.map-actions:has(#toggle-arches) { flex-wrap: wrap; gap: 8px 14px; }
.canvas-wrap:has(#arch-tooltip) { touch-action: none; }
.xml-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
    margin: 4px 0 6px;
    font-size: 0.85rem;
    color: var(--muted);
}
.xml-head span:first-child {
    font-weight: 600;
    color: var(--text);
    font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}
.xml-hint { font-size: 0.76rem; }
.road-xml {
    margin: 0;
    max-height: 420px;
    overflow: auto;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    padding: 10px 12px;
    font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
    font-size: 0.76rem;
    line-height: 1.5;
    white-space: pre;
    tab-size: 2;
}
.road-xml .xml-line { display: block; border-radius: 3px; }
.road-xml .xml-line.clickable { cursor: pointer; }
.road-xml .xml-line.node-line.clickable:hover { background: rgba(46, 196, 255, 0.16); }
.road-xml .xml-line.marker-line.clickable:hover { background: rgba(247, 147, 30, 0.16); }
.road-xml .xml-line.selected {
    background: rgba(46, 196, 255, 0.24);
    outline: 1px solid rgba(46, 196, 255, 0.5);
}
.road-xml .xml-line.marker-line.selected {
    background: rgba(247, 147, 30, 0.24);
    outline-color: rgba(247, 147, 30, 0.6);
}

/* Responsive: stack columns on narrow viewports (must follow base rules). */
@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; }
    .sidebar { position: static; max-height: 340px; }
    .sidebar:has(.dev-browser-tabs) { max-height: 460px; }
}
