:root {
    --ink: #1A2420;
    --ink-soft: #4B564E;
    --paper: #EFF1EA;
    --surface: #FFFFFF;
    --moss: #2F5233;
    --moss-deep: #1F3A24;
    --moss-light: #6C8F6E;
    --ochre: #B8722E;
    --line: #D3D6C9;
    --line-soft: #E4E6DC;
    --danger: #9C4A32;
    --shadow: 0 1px 2px rgba(26,36,32,0.06), 0 4px 14px rgba(26,36,32,0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    width: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', -apple-system, sans-serif;
    background: var(--paper);
    color: var(--ink);
    position: fixed;
    inset: 0;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
}

header {
    position: relative;
    z-index: 4000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 16px;
    padding-top: max(10px, env(safe-area-inset-top));
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    flex-wrap: nowrap;
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: baseline;
    gap: 8px;
    flex-shrink: 0;
}

.brand h1 {
    font-family: 'Big Shoulders Display', sans-serif;
    font-weight: 700;
    font-size: 24px;
    letter-spacing: 0.01em;
    margin: 0;
    color: var(--moss-deep);
    line-height: 1;
}

.search-row {
    display: flex;
    gap: 8px;
    flex: 1;
    min-width: 0;
    max-width: 560px;
}

.search-wrap {
    position: relative;
    flex: 1;
    min-width: 0;
}

.search-wrap input {
    width: 100%;
    padding: 10px 12px;
    padding-left: 24px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--paper);
    font-size: 16px;
    font-family: inherit;
    color: var(--ink);
    outline: none;
    transition: border-color .15s ease, background .15s ease;
    -webkit-appearance: none;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

.search-wrap input:focus {
    border-color: var(--moss-light);
    background: var(--surface);
}

.search-wrap .dot-marker {
    position: absolute;
    left: 10px;
    top: 50%;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    transform: translateY(-50%);
    pointer-events: none;
}

.search-wrap.from .dot-marker { background: var(--moss-deep); }
.search-wrap.to .dot-marker { background: var(--ochre); }

.search-results {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    max-height: 260px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: none;
    z-index: 10;
}

.search-results.open { display: block; }

.search-result {
    padding: 11px 12px;
    font-size: 13.5px;
    cursor: pointer;
    border-bottom: 1px solid var(--line-soft);
}

.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--paper); }

.back-btn {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink-soft);
    text-decoration: none;
    white-space: nowrap;
    flex-shrink: 0;
}
.back-btn:hover { color: var(--ink); }

@media (max-width: 768px) {
    header { flex-wrap: wrap; }
    .brand { order: 1; }
    .back-btn { order: 2; }
    .search-row {
        order: 3;
        flex-basis: 100%;
        max-width: none;
        margin-top: 4px;
    }
}

#main {
    display: grid;
    grid-template-columns: 320px 1fr;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

@media (max-width: 760px) {
    #main {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(0, 1fr) minmax(220px, 42vh);
    }
    #mapArea {
        order: 1;
        min-height: 150px;
    }
    #sidebar {
        order: 2;
        max-height: 100%;
        border-top: 1px solid var(--line);
        border-right: none;
    }
}

#sidebar {
    background: var(--surface);
    border-right: 1px solid var(--line);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
}

.sidebar-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 14px 14px 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.sidebar-dock {
    flex-shrink: 0;
    padding: 10px 14px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: var(--surface);
}

.field-label {
    font-size: 11px;
    font-weight: 700;
    color: var(--ink-soft);
    letter-spacing: 0.03em;
    margin-bottom: 6px;
}

.pace-row { display: flex; align-items: center; gap: 10px; }
.pace-row input {
    width: 74px;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--paper);
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
    color: var(--ink);
    text-align: center;
    outline: none;
}
.pace-row input:focus { border-color: var(--moss-light); background: var(--surface); }
.pace-row span.pace-unit { font-size: 12.5px; color: var(--ink-soft); }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; min-width: 0; }
.stat-box { background: var(--paper); border-radius: 10px; padding: 10px 12px; min-width: 0; overflow: hidden; }
.stat-box .num {
    font-family: 'Big Shoulders Display', sans-serif;
    font-size: clamp(18px, 5vw, 24px);
    font-weight: 700;
    line-height: 1;
    color: var(--moss-deep);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.stat-box.climb .num { color: var(--ochre); }
.stat-box .unit { font-size: 11px; font-weight: 600; color: var(--ink-soft); margin-left: 3px; }
.stat-box .lbl { font-size: 10.5px; color: var(--ink-soft); margin-top: 3px; }

.action-row { display: flex; gap: 8px; }
button.btn {
    font-family: inherit;
    font-size: 13.5px;
    font-weight: 600;
    border-radius: 8px;
    padding: 10px 12px;
    min-height: 40px;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--ink);
    cursor: pointer;
    flex: 1;
    transition: background .12s ease, border-color .12s ease;
    touch-action: manipulation;
}
button.btn:hover { background: var(--paper); }
button.btn:active { background: var(--line-soft); }
button.btn:disabled { opacity: 0.4; cursor: not-allowed; }
button.btn.primary { background: var(--moss-deep); border-color: var(--moss-deep); color: #fff; }
button.btn.primary:hover { background: var(--moss); border-color: var(--moss); }
button.btn.primary:disabled { background: var(--moss-deep); border-color: var(--moss-deep); }

.hint {
    font-size: 12px;
    line-height: 1.4;
    color: var(--ink-soft);
    background: var(--paper);
    border-radius: 10px;
    padding: 10px 12px;
}
.hint b { color: var(--ink); }

.route-status {
    font-size: 12px;
    color: var(--moss-light);
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 14px;
}
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--moss-light); flex-shrink: 0; }
.route-status.error { color: var(--danger); }
.route-status.error .dot { background: var(--danger); }

#mapArea { position: relative; min-height: 150px; flex: 1; min-width: 0; }
#map { height: 100%; width: 100%; }

.leaflet-div-icon.wp-marker {
    background: var(--moss);
    border: 2px solid #fff;
    border-radius: 50%;
    box-shadow: 0 1px 4px rgba(0,0,0,0.35);
}
.leaflet-div-icon.wp-marker.wp-start { background: var(--moss-deep); }
.leaflet-div-icon.wp-marker.wp-end { background: var(--ochre); }
.leaflet-div-icon.runner-marker {
    background: var(--ochre);
    border: 3px solid #fff;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
    position: relative;
}
.runner-pulse {
    position: absolute;
    top: 50%; left: 50%;
    width: 16px; height: 16px;
    margin: -8px 0 0 -8px;
    border-radius: 50%;
    background: var(--ochre);
    opacity: 0.55;
    animation: runnerPulse 1.4s ease-out infinite;
}
@keyframes runnerPulse {
    0%   { transform: scale(1);   opacity: 0.55; }
    100% { transform: scale(2.6); opacity: 0; }
}

#elevPanel {
    position: absolute;
    left: 10px; right: 10px;
    bottom: 10px;
    background: rgba(255,255,255,0.96);
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 8px 12px 4px;
    height: 95px;
    display: none;
    z-index: 1000;
}
#elevPanel.visible { display: block; }
#elevPanel .elev-head {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    font-weight: 700;
    color: var(--ink-soft);
    letter-spacing: 0.02em;
    margin-bottom: 2px;
}
#elevCanvas { width: 100%; height: 50px; }

#playPanel {
    position: absolute;
    top: 10px; left: 10px; right: 10px;
    background: rgba(255,255,255,0.97);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 10px 12px;
    display: none;
    z-index: 3000;
}
#playPanel.visible { display: block; }

.play-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.play-title { font-size: 11px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0.03em; }
.play-close {
    border: none; background: none; color: var(--ink-soft); font-size: 18px; line-height: 1;
    cursor: pointer; padding: 2px 4px;
}
.play-close:hover { color: var(--ink); }
.play-stats { display: flex; gap: 14px; margin-bottom: 8px; flex-wrap: wrap; }
.play-stat .num {
    font-family: 'Big Shoulders Display', sans-serif; font-size: 18px; font-weight: 700;
    color: var(--moss-deep); line-height: 1;
}
.play-stat .lbl { font-size: 10px; color: var(--ink-soft); margin-top: 2px; }
.play-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.play-btn-round {
    width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--moss-deep); color: #fff;
    cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 14px;
    touch-action: manipulation;
}
.play-btn-round:hover { background: var(--moss); }
#playScrub { flex: 1; accent-color: var(--moss-deep); height: 24px; touch-action: pan-x; min-width: 80px; }

.speed-group {
    display: flex;
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
}
.speed-btn {
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    border: none;
    background: var(--surface);
    color: var(--ink-soft);
    padding: 6px 8px;
    min-height: 36px;
    cursor: pointer;
    touch-action: manipulation;
}
.speed-btn + .speed-btn { border-left: 1px solid var(--line); }
.speed-btn:hover { background: var(--paper); }
.speed-btn.active { background: var(--moss-deep); color: #fff; }

@media (max-width: 760px) {
    #playPanel {
        top: 8px;
        left: 8px;
        right: 8px;
        padding: 8px 10px;
        border-radius: 10px;
    }
    .play-stats { gap: 10px; margin-bottom: 6px; }
    .play-stat .num { font-size: 15px; }
    .play-stat .lbl { font-size: 9px; }
    .play-controls { gap: 6px; }
    .play-btn-round { width: 32px; height: 32px; font-size: 13px; }
    .speed-btn { padding: 4px 7px; min-height: 32px; font-size: 11px; }

    #elevPanel {
        left: 8px;
        right: 8px;
        bottom: 8px;
        height: 64px;
        padding: 4px 10px 2px;
        border-radius: 8px;
    }
    #elevPanel .elev-head { font-size: 10px; margin-bottom: 0; }
    #elevCanvas { height: 36px; }
}

.modal-backdrop {
    position: fixed; inset: 0; background: rgba(26,36,32,0.45);
    display: none; align-items: center; justify-content: center;
    z-index: 6000; padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
    background: var(--surface);
    border-radius: 14px;
    box-shadow: var(--shadow);
    padding: 20px;
    max-width: 420px;
    width: 100%;
}
.modal h2 {
    font-family: 'Big Shoulders Display', sans-serif;
    font-size: 22px;
    margin: 0 0 4px;
    color: var(--moss-deep);
}
.modal p.sub { margin: 0 0 14px; font-size: 12.5px; color: var(--ink-soft); }
.share-link-row { display: flex; gap: 8px; margin-bottom: 14px; }
.share-link-row input {
    flex: 1; min-width: 0; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line);
    background: var(--paper); font-family: inherit; font-size: 13px; color: var(--ink); outline: none;
}

.field-hint {
    font-size: 11px;
    line-height: 1.35;
    color: var(--ink-soft);
    margin: 6px 0 0;
}
#targetKmInput {
    width: 74px;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--paper);
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
    color: var(--ink);
    text-align: center;
    outline: none;
}
#targetKmInput:focus {
    border-color: var(--moss-light);
    background: var(--surface);
}
.modal-close-row { display: flex; justify-content: flex-end; }
.copy-toast {
    position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
    background: var(--moss-deep); color: #fff; font-size: 13px; font-weight: 600;
    padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow);
    opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease;
    z-index: 7000;
}
.copy-toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

::selection { background: var(--moss-light); color: #fff; }
