@font-face {
    font-family: 'Inter';
    src: url('vendor/fonts/inter-400.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Inter';
    src: url('vendor/fonts/inter-500.woff2') format('woff2');
    font-weight: 500;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Inter';
    src: url('vendor/fonts/inter-600.woff2') format('woff2');
    font-weight: 600;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Inter';
    src: url('vendor/fonts/inter-700.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: 'Inter';
    src: url('vendor/fonts/inter-800.woff2') format('woff2');
    font-weight: 800;
    font-style: normal;
    font-display: swap;
}

/* ==================== Variables ==================== */
:root {
    --bg: oklch(0.12 0 0);
    --bg-panel: oklch(0.18 0 0);
    --border: oklch(1 0 0 / 0.22);
    --border-strong: oklch(1 0 0 / 0.35);
    --text: oklch(0.95 0 0);
    --text-dim: oklch(0.75 0 0);
    --text-muted: oklch(0.65 0 0);
    --text-faint: oklch(0.6 0 0);
    --accent: #00ff41;
    --danger-border: oklch(0.4 0.1 30);
    --danger-border-hover: oklch(0.75 0.16 30);
    --danger-text: #ff0000;
    --mono: ui-monospace, monospace;
    --sans: 'Inter', -apple-system, 'Segoe UI', sans-serif;

    /* Tamaños de texto */
    --fs-xs: 11px;
    --fs-sm: 12px;
    --fs-base: 13px;
    --fs-md: 14px;
    --fs-lg: 15px;
    --fs-xl: 16px;


    --map-gap: 16px;
    --fab-size: 40px;
}

/* ==================== Reset ==================== */

/* 1. Usar un modelo de box-sizing más intuitivo */
*,
*::before,
*::after {
    box-sizing: border-box;
}

/* 2. Quitar el margen por defecto */
*:not(dialog) {
    margin: 0;
}

/* 3. Habilitar animaciones por palabra clave */
@media (prefers-reduced-motion: no-preference) {
    html {
        interpolate-size: allow-keywords;
    }
}

body {
    /* 4. Aumentar el interlineado */
    line-height: 1.5;
    /* 5. Mejorar el renderizado de texto */
    -webkit-font-smoothing: antialiased;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
}

/* Scrollbars oscuros, a juego con el resto de la UI */
::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}
::-webkit-scrollbar-thumb {
    background: oklch(1 0 0 / 0.24);
}
::-webkit-scrollbar-track {
    background: transparent;
}

/* 6. Mejorar los valores por defecto de los medios */
img,
picture,
video,
canvas,
svg {
    display: block;
    max-width: 100%;
}

/* 7. Heredar la fuente en los controles de formulario */
input,
button,
textarea,
select {
    font: inherit;
}

/* 8. Evitar desbordes de texto */
p,
h1,
h2,
h3,
h4,
h5,
h6 {
    overflow-wrap: break-word;
}

/* 9. Mejorar el ajuste de línea */
p {
    text-wrap: pretty;
}
h1,
h2,
h3,
h4,
h5,
h6 {
    text-wrap: balance;
}

/* ==================== Layout / Mapa ==================== */

#container {
    display: flex;
    height: 100vh;
}

#sidebar {
    flex: 0 0 360px;
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-right: 1px solid var(--border);
    background: var(--bg);
}

#map-wrapper {
    position: relative;
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

#map {
    flex: 1;
    /* Encierra los z-index internos de Leaflet (hasta 1000) para que no compitan con los del resto de la app */
    position: relative;
    z-index: 0;
}

.numbered-marker {
    width: 26px;
    height: 26px;
    display: flex;
    cursor: grab;
}


.marker-number {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-panel);
    border: 2px solid var(--text);
    color: var(--text);
    font-family: var(--mono);
    font-weight: 700;
    font-size: var(--fs-sm);
    line-height: 1;
    transition: transform 0.1s ease;
}

.numbered-marker.is-last .marker-number {
    background: var(--text);
    border-color: var(--bg-panel);
    color: var(--bg-panel);
}

.numbered-marker:hover .marker-number {
    transform: scale(1.2);
}

.numbered-marker.dragging {
    cursor: grabbing;
}

.numbered-marker.dragging .marker-number {
    transform: scale(1.2);
}

.leaflet-marker-icon {
    display: flex;
}

/* ==================== Sidebar ==================== */

.sidebar-header {
    flex: none;
    padding: 16px 20px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.app-title {
    font-size: 19px;
    font-weight: 800;
    letter-spacing: 0.06em;
    color: var(--text);
}

.section-label {
    font-size: 12.5px;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.stats-row {
    flex: none;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.stat-card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
}

/* La gráfica ocupa su propia fila a todo lo ancho, debajo de Distancia/Desnivel */
.elevation-mini-card {
    grid-column: 1 / -1;
}

.elevation-mini {
    position: relative;
    height: 74px;
}

.stat-label {
    font-size: var(--fs-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-faint);
}

.stat-value {
    font-size: 17px;
    font-weight: 700;
    font-family: ui-monospace, monospace;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.section {
    flex: none;
    padding: 0 20px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.search-section {
    flex: none;
    padding: 0 20px 14px;
}

.search-box {
    position: relative;
}

.search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 18px;
    height: 18px;
    background-color: var(--text-faint);
    -webkit-mask: url('icons/search.svg') no-repeat center / contain;
    mask: url('icons/search.svg') no-repeat center / contain;
    pointer-events: none;
}

.search-input {
    width: 100%;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 28px 8px 34px;
    font-size: var(--fs-md);
}

.search-clear {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    border: none;
    background: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: var(--fs-md);
    line-height: 1;
    padding: 4px;
}

.search-clear:hover {
    color: var(--text);
}

.search-input:placeholder-shown ~ .search-clear {
    display: none;
}

.search-input::placeholder {
    color: var(--text-faint);
}

.search-input:focus {
    outline: none;
    border-color: var(--border-strong);
}

.search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    z-index: 20;
    max-height: 220px;
    overflow-y: auto;
    background: var(--bg-panel);
    border: 1px solid var(--border-strong);
    box-shadow: 0 8px 24px oklch(0 0 0 / 0.5);
    list-style: none;
    margin: 0;
    padding: 4px;
}

.search-result-item {
    padding: 8px 10px;
    font-size: var(--fs-md);
    color: var(--text);
    cursor: pointer;
}

.search-result-item:hover,
.search-result-item.is-active {
    background: var(--bg-hover, var(--border));
}

.search-result-empty {
    padding: 8px 10px;
    font-size: var(--fs-md);
    color: var(--text-faint);
}

.points-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.points-header {
    flex: none;
    padding: 4px 20px 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.points-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.icon-btn {
    background: transparent;
    border: 1px solid var(--border-strong);
    color: var(--text-dim);
    width: 28px;
    height: 28px;
    font-size: var(--fs-md);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    border-color: var(--text);
    color: var(--text);
}

.icon-btn:disabled {
    opacity: 0.35;
    cursor: default;
    pointer-events: none;
}

.icon-btn-danger {
    border-color: var(--danger-border);
    color: var(--danger-text);
}

.icon-btn-danger:hover {
    border-color: var(--danger-border-hover);
    color: var(--danger-border-hover);
}

/* Iconos SVG */
.icon-svg {
    display: inline-block;
    background-color: currentColor;
}

.icon-svg-x {
    -webkit-mask: url('icons/x.svg') no-repeat center / contain;
    mask: url('icons/x.svg') no-repeat center / contain;
}

.icon-svg-undo {
    -webkit-mask: url('icons/undo.svg') no-repeat center / contain;
    mask: url('icons/undo.svg') no-repeat center / contain;
}

.icon-svg-download {
    -webkit-mask: url('icons/download.svg') no-repeat center / contain;
    mask: url('icons/download.svg') no-repeat center / contain;
}

.icon-svg-drag {
    -webkit-mask: url('icons/drag-handle.svg') no-repeat center / contain;
    mask: url('icons/drag-handle.svg') no-repeat center / contain;
}

.icon-btn .icon-svg {
    width: 14px;
    height: 14px;
}

.icon-btn .icon-svg-undo {
    width: 11px;
    height: 11px;
}

.search-clear .icon-svg {
    width: 12px;
    height: 12px;
}

.delete-btn .icon-svg {
    width: 16px;
    height: 16px;
}

/* Botón de descargar GPX flotando en la esquina superior derecha del mapa, visible en escritorio; en móvil se resuelve aparte con .download-btn-mobile */
.map-corner-btn {
    position: absolute;
    top: var(--map-gap);
    right: var(--map-gap);
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-panel);
    border: 1px solid var(--border-strong);
    color: var(--text-dim);
    cursor: pointer;
    z-index: 50;
}

.map-corner-btn:hover:not(:disabled) {
    border-color: var(--text);
    color: var(--text);
    transform: scale(1.04);
}

.map-corner-btn:disabled {
    cursor: default;
    opacity: 0.75;
}

.map-corner-btn .icon-svg {
    width: 16px;
    height: 16px;
}

.map-corner-btn:hover:not(:disabled) .icon-svg {
    transform: scale(calc(1 / 1.04));
}

.download-btn-mobile {
    display: none;
}

.map-fab {
    display: none;
}

.map-fab .icon-svg {
    width: 18px;
    height: 18px;
}

.map-fab .icon-svg-undo {
    width: 15px;
    height: 15px;
}

.map-fab:disabled {
    opacity: 0.35;
    pointer-events: none;
}

#point-list {
    flex: 1;
    overflow-y: auto;
    scrollbar-gutter: stable;
    list-style: none;
    margin: 0;
    padding: 0 12px 12px;
}

.point-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    margin-bottom: 2px;
    border: 1px dashed transparent;
    cursor: grab;
}

.point-item:hover {
    background: oklch(1 0 0 / 0.05);
}

.point-item.dragging {
    height: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 0;
    border-width: 0;
    opacity: 0;
    overflow: hidden;
    cursor: grabbing;
}

.drag-placeholder {
    border-color: var(--accent);
    background: oklch(1 0 0 / 0.03);
    padding: 0;
}

.drag-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: none;
    color: var(--text-dim);
    opacity: 0.5;
}

.drag-handle .icon-svg {
    width: 14px;
    height: 14px;
}

.point-item:hover .drag-handle {
    opacity: 1;
}

.point-number {
    font-family: ui-monospace, monospace;
    font-size: var(--fs-lg);
    font-weight: 700;
    color: var(--accent);
    width: 26px;
    flex: none;
}

.point-coords {
    font-family: ui-monospace, monospace;
    font-size: 13.5px;
    color: var(--text-dim);
    flex: 1;
}

.delete-btn {
    border: none;
    background: none;
    cursor: pointer;
    color: var(--text-faint);
    font-size: var(--fs-lg);
    line-height: 1;
    padding: 2px 4px;
}

.delete-btn:hover {
    color: var(--text);
}

#graphic {
    width: 100%;
}

/* Quitamos +/- */
.leaflet-control-zoom {
    display: none;
}

/* ==================== Ayuda ==================== */

.help-btn {
    font-family: var(--mono);
    font-weight: 700;
    font-size: var(--fs-md);
    line-height: 1;
}

.help-dialog {
    width: min(440px, calc(100% - 32px));
    max-height: calc(100dvh - 32px);
    padding: 0;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    color: var(--text);
}

.help-dialog::backdrop {
    background: oklch(0 0 0 / 0.6);
}

.help-header {
    position: sticky;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px 12px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}

.help-title {
    font-size: var(--fs-xl);
    font-weight: 700;
    letter-spacing: 0.04em;
}

.help-list {
    padding: 4px 20px 20px;
}

.help-list dt {
    margin-top: 16px;
    font-size: var(--fs-xs);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--accent);
}

.help-list dd {
    margin: 4px 0 0;
    font-size: var(--fs-md);
    color: var(--text-dim);
}

/* ==================== Toasts ==================== */

#toast-container {
    position: absolute;
    top: var(--map-gap);
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: min(480px, calc(100% - 32px));
    pointer-events: none;
}

.toast {
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px 10px 14px;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    color: var(--text);
    font-family: var(--sans);
    font-weight: 500;
    font-size: var(--fs-md);
}

.toast-text {
    flex: 1;
    text-wrap: balance;
    padding-left: 20px;
    text-align: center;
}

.toast-close {
    flex: none;
    background: none;
    border: none;
    color: inherit;
    width: 20px;
    height: 20px;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media (prefers-reduced-motion: no-preference) {
    .toast {
        animation: toast-in 0.2s ease-out;
    }
    @keyframes toast-in {
        from {
            opacity: 0;
            transform: translateY(-8px);
        }
    }
}

/* ==================== Mobile (bottom sheet) ==================== */

.sheet-handle {
    display: none;
}

@media (max-width: 768px) {
    .section-label {
        font-size: 11.5px;
    }

    .stat-label {
        font-size: 10px;
    }

    .stat-value {
        font-size: var(--fs-xl);
    }

    .search-input,
    .search-clear,
    .search-result-item,
    .search-result-empty {
        font-size: var(--fs-base);
    }

    #container {
        position: relative;
        height: 100dvh;
    }

    /* El mapa siempre a pantalla completa, y el bottom sheet se coloca por encima */
    #map-wrapper {
        position: absolute;
        inset: 0;
    }

    #sidebar {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 130px;
        border-right: none;
        border-top: 1px solid var(--border);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -8px 24px oklch(0 0 0 / 0.45);
        overflow: hidden;
        transition: height 0.25s ease;
        z-index: 50;
    }

    .sheet-handle {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: 100%;
        padding: 14px 0 20px;
        background: none;
        border: none;
        cursor: grab;

        touch-action: none;
        user-select: none;
        -webkit-touch-callout: none;
    }

    .sheet-handle:active {
        cursor: grabbing;
    }

    .sheet-handle::before {
        content: '';
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--border-strong);
    }

    .sidebar-header {
        display: none;
    }

    .map-corner-btn {
        display: none;
    }

    .elevation-mini {
        height: 68px;
    }
    #sidebar:not(.is-expanded) .elevation-mini-card {
        display: none;
    }

    #graphic {
        pointer-events: none;
    }

    .icon-btn {
        width: 32px;
        height: 32px;
        font-size: var(--fs-md);
    }

    .point-item {
        padding: 12px 10px;
        gap: 12px;
    }

    .point-number {
        font-size: var(--fs-base);
    }

    .point-coords {
        font-size: var(--fs-md);
    }

    .delete-btn {
        font-size: var(--fs-xl);
        padding: 6px 8px;
    }

    #btn-undo-point {
        display: none;
    }


    .map-fab {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        top: calc(env(safe-area-inset-top, 0px) + var(--map-gap));
        width: var(--fab-size);
        height: var(--fab-size);
        border: 1px solid var(--border-strong);
        background: var(--bg-panel);
        color: var(--text-dim);
        font-size: var(--fs-xl);
        line-height: 1;
        padding: 0;
        cursor: pointer;
        z-index: 50;
    }

    #btn-undo-point-fab {
        right: var(--map-gap);
    }

    #btn-help-fab {
        left: var(--map-gap);
    }

    .download-btn-mobile {
        grid-column: 1 / -1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 10px;
        background: var(--bg-panel);
        border: 1px solid var(--border-strong);
        color: var(--text-dim);
        font: inherit;
        font-size: var(--fs-base);
        font-weight: 500;
        cursor: pointer;
    }

    .download-btn-mobile:active {
        border-color: var(--text);
        color: var(--text);
    }

    .download-btn-mobile:disabled {
        opacity: 0.35;
        pointer-events: none;
    }

    #sidebar:not(.is-expanded) .download-btn-mobile {
        display: none;
    }


    #toast-container {
        top: calc(env(safe-area-inset-top, 0px) + var(--map-gap));
        left: var(--map-gap);
        right: var(--map-gap);
        width: auto;
        transform: none;
    }

    .toast {
        font-size: var(--fs-sm);
    }
}
