:root {
    --bg:          #1a1a2e;
    --panel:       #16213e;
    --panel-deep:  #0f3460;
    --line:        #1a2a4a;
    --text:        #e0e0e0;
    --text-soft:   #b0c4de;
    --text-muted:  #8a9bb0;
    --accent:      #e94560;
    --accent-dark: #c73652;
    --start:       #4caf50;
    --poi:         #4fc3f7;
    --action:      #9c27b0;
    --action-soft: #ce93d8;
    --warning:     #ffc107;
    --warning-bg:  #2a2a1a;
    --sidebar-w:   340px;
}

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

/* Every element hidden via the attribute stays hidden, whatever display a rule sets. */
[hidden] {
    display: none !important;
}

body {
    font-family: 'Segoe UI', Arial, sans-serif;
    display: flex;
    height: 100vh;
    height: 100dvh;
    background: var(--bg);
    color: var(--text);
}

button, input, select {
    font: inherit;
}

:focus-visible {
    outline: 2px solid var(--poi);
    outline-offset: 2px;
}

/* ---- Layout ---------------------------------------------------------------- */

#sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--panel);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

#map {
    flex: 1;
    height: 100%;
}

#header {
    background: var(--panel-deep);
    padding: 16px 20px;
    border-bottom: 2px solid var(--accent);
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
}

#header-title {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    letter-spacing: 0.5px;
}

#header-sub {
    font-size: 12px;
    color: var(--text-muted);
}

.section {
    padding: 14px 20px;
    border-bottom: 1px solid var(--line);
}

.section-label {
    display: block;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 10px;
}

/* Collapsible sections: the summary is the label, with a chevron that turns. */
details.section > summary {
    cursor: pointer;
    list-style: none;
    user-select: none;
}

details.section > summary::-webkit-details-marker {
    display: none;
}

details.section > summary::after {
    content: '▸';
    float: right;
    color: var(--text-muted);
    transition: transform 0.15s;
}

details.section[open] > summary::after {
    transform: rotate(90deg);
}

details.section:not([open]) > summary {
    margin-bottom: 0;
}

.info-value {
    font-size: 14px;
    color: var(--text-soft);
    margin: 10px 0;
}

.info-value.muted {
    color: var(--text-muted);
}

.info-sub {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
}

/* ---- Start & POIs ---------------------------------------------------------- */

#action-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

/* min-width:0 lets a button shrink below its label instead of overflowing; the
   flex-basis stacks the pair rather than squashing it on a narrow sidebar. */
#action-buttons button {
    flex: 1 1 120px;
    min-width: 0;
    padding: 9px 6px;
    cursor: pointer;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
    transition: background 0.2s, color 0.2s;
}

#btn-set-source {
    background: #1a3a2a;
    color: var(--start);
}

#btn-set-source:hover, #btn-set-source.active {
    background: var(--start);
    color: #fff;
}

#btn-add-poi {
    background: #16304a;
    color: var(--poi);
}

#btn-add-poi:hover, #btn-add-poi.active {
    background: var(--poi);
    color: var(--panel);
}

#poi-search-input {
    width: 100%;
    padding: 9px 10px;
    background: var(--panel-deep);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 14px;
}

#poi-search-input:focus {
    outline: none;
    border-color: var(--poi);
}

#poi-search-input::placeholder {
    color: var(--text-muted);
}

#poi-search-results {
    list-style: none;
}

#poi-search-results:not(:empty) {
    margin-top: 6px;
}

.poi-result {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 7px 10px;
    border-radius: 6px;
    cursor: pointer;
}

.poi-result.active,
.poi-result:hover {
    background: #16304a;
}

.poi-result-name {
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.poi-result-meta,
.poi-result-empty {
    font-size: 12px;
    color: var(--text-muted);
}

.poi-result-empty {
    padding: 7px 10px;
}

#poi-section {
    margin-top: 10px;
}

.poi-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid var(--line);
}

.poi-row:last-child {
    border-bottom: none;
}

.poi-name {
    font-size: 14px;
    color: var(--text-soft);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.poi-index {
    color: var(--poi);
    font-weight: 700;
    margin-right: 8px;
}

.poi-remove {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    padding: 2px 6px;
    transition: color 0.2s;
}

.poi-remove:hover {
    color: var(--accent);
}

/* ---- Route settings and preferences --------------------------------------- */

.round-route-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
}

.round-route-row:last-child {
    margin-bottom: 0;
}

.round-label,
.slider-label {
    font-size: 14px;
    color: var(--text-soft);
}

.round-input-wrap {
    display: flex;
    align-items: center;
    gap: 6px;
}

#round-distance,
#round-variants,
#round-method {
    padding: 7px 10px;
    background: var(--panel-deep);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 14px;
}

#round-distance {
    width: 72px;
    text-align: center;
}

#round-distance:focus,
#round-variants:focus,
#round-method:focus {
    outline: none;
    border-color: var(--action);
}

.round-unit {
    font-size: 14px;
    color: var(--text-muted);
}

.slider-row {
    margin-bottom: 14px;
}

.slider-row:last-child {
    margin-bottom: 0;
}

.slider-row-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;
}

.slider-value {
    font-size: 13px;
    font-weight: 600;
    color: var(--action-soft);
    min-width: 3ch;
    text-align: right;
}

.slider-input-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
}

.slider-end {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
    width: 52px;
}

.slider-end:last-child {
    text-align: right;
}

.slider-input-wrap input[type="range"] {
    flex: 1;
    min-width: 0;
    -webkit-appearance: none;
    appearance: none;
    height: 4px;
    background: var(--panel-deep);
    border-radius: 2px;
}

.slider-input-wrap input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background: var(--action);
    border: 2px solid var(--line);
    border-radius: 50%;
    cursor: pointer;
}

.slider-input-wrap input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: var(--action);
    border: 2px solid var(--line);
    border-radius: 50%;
    cursor: pointer;
}

.slider-input-wrap input[type="range"]:hover::-webkit-slider-thumb { background: var(--action-soft); }
.slider-input-wrap input[type="range"]:hover::-moz-range-thumb     { background: var(--action-soft); }

/* ---- Result ---------------------------------------------------------------- */

#result-section {
    background: #132a52;
}

/* A loop outside the tolerance: what it missed by, and the length it did reach. */
#route-warning {
    background: var(--warning-bg);
    border: 1px solid #5a4a10;
    border-left: 3px solid var(--warning);
    border-radius: 6px;
    padding: 10px 12px;
    margin-bottom: 12px;
    font-size: 13px;
    color: var(--text-soft);
}

#route-warning-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--warning);
    margin-bottom: 4px;
}

#route-warning-hint {
    margin-top: 4px;
    color: var(--text-muted);
}

#btn-recalculate {
    margin-top: 10px;
    width: 100%;
    padding: 8px;
    border: 1px solid var(--warning);
    border-radius: 6px;
    background: transparent;
    color: var(--warning);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

#btn-recalculate:hover {
    background: var(--warning);
    color: var(--bg);
}

.badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    vertical-align: middle;
}

.badge-warning {
    background: var(--warning-bg);
    border: 1px solid var(--warning);
    color: var(--warning);
}

#route-options {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.route-option {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px 10px;
    background: var(--panel-deep);
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--text-soft);
    text-align: left;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
}

.route-option:hover {
    background: #1a4a80;
}

.route-option.selected {
    border-color: var(--action);
    background: #2a1a3a;
    color: #fff;
}

.route-swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    flex-shrink: 0;
}

.route-option-title {
    font-weight: 700;
    min-width: 1.5ch;
}

.route-option-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.route-option-main {
    font-size: 14px;
}

.route-option-shares {
    font-size: 12px;
    color: var(--text-muted);
}

.route-stat {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid var(--line);
}

.route-stat:last-child {
    border-bottom: none;
}

.stat-label {
    font-size: 14px;
    color: var(--text-muted);
}

.stat-label[title] {
    text-decoration: underline dotted;
    text-underline-offset: 3px;
    cursor: help;
}

.stat-value {
    font-size: 15px;
    font-weight: 600;
    color: var(--poi);
}

/* ---- Actions: status and the two main buttons, below the settings --------- */

#sidebar-actions {
    padding: 14px 20px;
    border-bottom: 1px solid var(--line);
}

.status {
    padding: 10px 12px;
    border-radius: 6px;
    font-size: 14px;
    text-align: center;
    font-weight: 600;
    margin-bottom: 10px;
}

.status-idle    { background: var(--line); color: var(--text-muted); }
.status-waiting { background: #1a3a2a; color: var(--start); }
.status-done    { background: #1a3a2a; color: var(--start); }
.status-warning { background: #2a2a1a; color: var(--warning); }
.status-error   { background: #3a1a1a; color: var(--accent); }
.status-loading {
    background: #2a2a1a;
    color: var(--warning);
    animation: pulse 1s infinite alternate;
}

@keyframes pulse {
    from { opacity: 0.7; }
    to   { opacity: 1.0; }
}

#action-row {
    display: flex;
    gap: 8px;
}

#btn-round-route,
#btn-clear {
    padding: 11px;
    cursor: pointer;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    transition: background 0.2s, color 0.2s;
}

#btn-round-route {
    flex: 1;
    background: var(--action);
    color: #fff;
}

#btn-round-route:hover:not(:disabled) {
    background: #7b1fa2;
}

#btn-round-route:disabled {
    background: #2a1a3a;
    color: var(--action-soft);
    opacity: 0.6;
    cursor: not-allowed;
}

#btn-clear {
    background: var(--line);
    color: var(--text-soft);
    padding-inline: 16px;
}

#btn-clear:hover {
    background: var(--accent);
    color: #fff;
}

/* ---- Modal ------------------------------------------------------------------ */

#modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 9999;
    justify-content: center;
    align-items: center;
}

#modal-overlay.visible {
    display: flex;
}

#modal {
    background: var(--panel);
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 28px;
    max-width: 420px;
    width: calc(100% - 32px);
    text-align: center;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

#modal-icon {
    font-size: 36px;
    margin-bottom: 10px;
}

#modal-title {
    font-size: 19px;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 14px;
}

#modal-body {
    font-size: 14px;
    color: var(--text-soft);
    line-height: 1.6;
    margin-bottom: 16px;
    overflow-wrap: anywhere;
}

#modal-details {
    background: var(--panel-deep);
    border-radius: 6px;
    padding: 12px;
    text-align: left;
    margin-bottom: 20px;
    line-height: 1.7;
}

#modal-details:empty {
    display: none;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.detail-label {
    color: var(--text-muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
}

.detail-val {
    color: var(--poi);
    font-size: 12px;
    font-weight: 600;
    font-family: monospace;
}

.detail-bullet {
    font-size: 12px;
    color: var(--text-muted);
    padding-left: 14px;
    position: relative;
}

.detail-bullet::before {
    content: '•';
    position: absolute;
    left: 2px;
    color: var(--accent);
}

#modal-close {
    padding: 10px 28px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

#modal-close:hover {
    background: var(--accent-dark);
}

/* ---- Small screens: map on top, controls below ----------------------------- */

@media (max-width: 760px) {
    body {
        flex-direction: column;
    }

    #map {
        order: -1;
        flex: none;
        height: 45vh;
        height: 45dvh;
    }

    #sidebar {
        width: 100%;
        min-width: 0;
        flex: 1;
    }

    #header {
        padding: 10px 16px;
    }

    #header-title {
        font-size: 17px;
    }

    .section,
    #sidebar-actions {
        padding: 12px 16px;
    }
}
