/* =========================================
   BASE
   ========================================= */
body, html { height: 100%; margin: 0; display: flex; flex-direction: column; overflow: hidden; font-family: 'Segoe UI', sans-serif; font-size: 14px !important; }
.navbar { flex-shrink: 0; box-shadow: 0 2px 10px rgba(0,0,0,0.5); z-index: 1000; }

/* =========================================
   PESTAÑAS
   ========================================= */
#tabs-container {
    flex-shrink: 0; background: #1a1d20; padding: 8px 10px; overflow-x: auto;
    white-space: nowrap; border-bottom: 1px solid #333; display: flex; gap: 8px; min-height: 45px; position: relative;
}
.track-tab {
    background: #343a40; border: 1px solid #495057; border-radius: 6px; padding: 5px 15px;
    color: #adb5bd; cursor: pointer; font-size: 0.9rem; display: inline-flex; align-items: center;
    gap: 8px; transition: all 0.2s; user-select: none; -webkit-touch-callout: none;
}
.track-tab:hover { background: #495057; color: #fff; }
.track-tab.active { background: #ee3b65; border-color: #ee3b65; color: white; box-shadow: 0 2px 5px rgba(238,59,101,0.4); }
.color-dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; border: 1px solid rgba(255,255,255,0.3); }
.track-tab.hidden-track { opacity: 0.5; font-style: italic; }

/* =========================================
   LAYOUT PRINCIPAL
   ========================================= */
#main-container { flex-grow: 1; display: flex; flex-direction: row; overflow: hidden; }
#map-container { flex-grow: 1; display: flex; flex-direction: column; position: relative; overflow: hidden; }
#map { flex-grow: 1; width: 100%; cursor: crosshair; min-width: 0; overflow: hidden; }
#map.drag-over { opacity: 0.7; outline: 4px dashed #ee3b65; outline-offset: -10px; transition: all 0.2s ease; }

/* =========================================
   PANEL WAYPOINTS
   ========================================= */
#waypoints-panel {
    width: 320px; background: #212529; border-left: 1px solid #333; display: flex;
    flex-direction: column; flex-shrink: 0; overflow: hidden; transition: width 0.3s;
}
.wpt-header { padding: 10px; background: #1a1d20; border-bottom: 1px solid #444; font-weight: bold; text-align: center; }
#wpt-list { overflow-y: auto; flex-grow: 1; padding: 0; margin: 0; list-style: none; scroll-behavior: smooth; }
.wpt-item { padding: 10px; border-bottom: 1px solid #333; display: flex; align-items: flex-start; gap: 10px; cursor: pointer; transition: background-color 0.3s; }
.wpt-item:hover { background: #2c3034; }
.wpt-emoji-container { font-size: 1.5rem; padding-top: 2px; }
.wpt-inputs { display: flex; flex-direction: column; width: 100%; gap: 4px; }
.wpt-inputs input { background: transparent; border: 1px solid transparent; color: #fff; width: 100%; border-radius: 3px; padding: 2px 5px; }
.wpt-inputs input:focus { border-color: #0d6efd; outline: none; background: #1a1d20; }
.wpt-name { font-size: 0.95rem; font-weight: bold; }
.wpt-desc { font-size: 0.8rem; color: #adb5bd; }

/* =========================================
   BARRA DE ESTADÍSTICAS
   ========================================= */
#stats-bar {
    background-color: #212529; padding: 8px 15px; display: flex; gap: 20px;
    border-top: 1px solid #444; font-size: 0.9rem; color: #ddd; flex-shrink: 0;
    justify-content: center; box-shadow: 0 -2px 10px rgba(0,0,0,0.3); z-index: 500; flex-wrap: wrap;
}
.stat-value { font-weight: bold; color: #fff; transition: all 0.3s ease; display: inline-block; }
.stat-dplus { color: #28a745; }
.stat-dminus { color: #dc3545; }

/* =========================================
   PANEL ELEVACIÓN
   ========================================= */
#elevation-panel { height: 180px; width: 100%; background: #121212; padding: 10px; flex-shrink: 0; border-top: 1px solid #333; transition: all 0.3s ease; }

/* =========================================
   WAYPOINT ICONS & TOOLTIPS
   ========================================= */
.custom-wpt-icon { background: #ffeb3b; border: 2px solid #000; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 16px; box-shadow: 2px 2px 5px rgba(0,0,0,0.6); }
.custom-wpt-icon.inactive { filter: grayscale(100%) opacity(0.6); border-color: #666; }
.wpt-tooltip { background: rgba(0,0,0,0.8); color: white; border: 1px solid #555; font-weight: bold; font-size: 12px; transition: opacity 0.3s ease; }

/* =========================================
   SISTEMA MENÚ CONTEXTUAL NUEVO (ctx-sheet)
   ========================================= */

/* Overlay oscuro */
#ctx-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9998;
    background: transparent;
}
#ctx-overlay.active { display: block; }

/* Base del sheet */
.ctx-sheet {
    position: fixed;
    z-index: 9999;
    background: #1e2227;
    border: 1px solid #3a3f47;
    box-shadow: 0 8px 32px rgba(0,0,0,0.7);
    display: none;
    flex-direction: column;
    overflow-y: auto;
    overscroll-behavior: contain;
}
.ctx-sheet.active { display: flex; }

/* Handle (barrita de arrastre, solo visible en móvil) */
.ctx-sheet-handle {
    width: 36px; height: 4px;
    background: #555; border-radius: 2px;
    margin: 10px auto 6px;
    display: none;
}

/* Títulos de sección */
.ctx-section-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: #6c757d;
    padding: 8px 14px 4px;
}

/* Divisor */
.ctx-divider {
    height: 1px;
    background: #2e3338;
    margin: 4px 12px;
}

/* Safe area (espacio inferior en móvil) */
.ctx-safe-area { height: 8px; }

/* ── Ítems de lista ── */
.ctx-item {
    padding: 10px 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    color: #e9ecef;
    user-select: none;
    transition: background 0.15s;
}
.ctx-item:hover { background: #2c3136; }
.ctx-item.ctx-danger { color: #ff6b6b; }
.ctx-item.ctx-danger:hover { background: rgba(220,53,69,0.15); }
.ctx-item .ctx-ico { font-size: 15px; width: 18px; text-align: center; flex-shrink: 0; }
.ctx-item .ctx-lbl { font-size: 13px; }

/* ── Cuadrícula de acciones rápidas (4 columnas) ── */
.ctx-quick-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    padding: 4px 10px 8px;
}
.ctx-quick-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 4px;
    border-radius: 10px;
    background: #252a30;
    cursor: pointer;
    transition: background 0.15s;
    user-select: none;
}
.ctx-quick-btn:hover { background: #2e8dff22; }
.ctx-quick-btn:active { background: #0d6efd33; }
.ctx-quick-btn .ctx-ico { font-size: 20px; line-height: 1; }
.ctx-quick-btn .ctx-lbl { font-size: 10px; color: #adb5bd; text-align: center; line-height: 1.2; }

/* ── Cuadrícula de herramientas (3 columnas) ── */
.ctx-tool-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    padding: 4px 10px 8px;
}
.ctx-tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 4px;
    border-radius: 10px;
    background: #252a30;
    cursor: pointer;
    transition: background 0.15s;
    user-select: none;
    position: relative;
}
.ctx-tool-btn:hover { background: #2e8dff22; }
.ctx-tool-btn:active { background: #0d6efd33; }
.ctx-tool-btn .ctx-ico { font-size: 18px; line-height: 1; }
.ctx-tool-btn .ctx-lbl { font-size: 10px; color: #adb5bd; text-align: center; line-height: 1.2; }

/* ── Flyout (submenú) ── */
.ctx-flyout {
    display: none;
    flex-direction: column;
    min-width: 220px;
}

/* =========================================
   ESCRITORIO: menú flotante posicionado
   ========================================= */
@media (min-width: 768px) {

    .ctx-quick-btn .ctx-lbl { font-size: 12px; }
    .ctx-tool-btn .ctx-lbl  { font-size: 12px; }
    .ctx-item .ctx-lbl      { font-size: 14px; }
    .ctx-section-title      { font-size: 11px; }
    .ctx-flyout .ctx-item .ctx-lbl { font-size: 13px; }

    .ctx-sheet {
        border-radius: 10px;
        min-width: 240px;
        max-width: 300px;
        max-height: 80vh;
        overflow: visible;
    }

    .ctx-has-sub .ctx-flyout {
        display: none;
        position: fixed;
        background: #1e2227;
        border: 1px solid #3a3f47;
        border-radius: 10px;
        box-shadow: 0 8px 24px rgba(0,0,0,0.6);
        z-index: 10001;
        padding: 6px 0;
        min-width: 240px;
        flex-direction: column;
    }

    /* Ya NO usamos :hover en CSS — lo maneja JS con delay */
    .ctx-has-sub .ctx-flyout.flyout-visible { display: flex; }
}

/* =========================================
   MÓVIL: bottom sheet deslizable
   ========================================= */
@media (max-width: 767px) {
    .ctx-sheet {       
        left: 0;
        right: 0;
        width: 100%;
        border-radius: 16px 16px 0 0;
        border-bottom: none;
        max-height: 85vh;
        transform: translateY(100%);
        transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    }
    .ctx-sheet.active {
        transform: translateY(0);
    }
    .ctx-sheet-handle { display: block; }
    #ctx-overlay.active {
        background: rgba(0,0,0,0.45);
    }

    /* Flyout en móvil: se expande inline como acordeón */
    .ctx-has-sub.open .ctx-flyout {
        display: flex;
        flex-direction: column;
        background: #161a1e;
        border-top: 1px solid #2e3338;
        border-radius: 0 0 8px 8px;
        padding: 4px 0;
        grid-column: 1 / -1;
        width: 100%;
    }
    /* Cuando está abierto, el botón ocupa toda la fila */
    .ctx-has-sub.open {
        grid-column: 1 / -1;
        flex-direction: column;
        border-radius: 10px 10px 0 0;
        background: #2a3040;
    }

    /* Compactar todo en móvil */
    .ctx-section-title {
        font-size: 9px;
        padding: 5px 12px 3px;
    }
    .ctx-quick-grid {
        gap: 3px;
        padding: 3px 8px 6px;
    }
    .ctx-quick-btn {
        padding: 8px 2px;
        gap: 3px;
        border-radius: 8px;
    }
    .ctx-quick-btn .ctx-ico { font-size: 18px; }
    .ctx-quick-btn .ctx-lbl { font-size: 9px; }

    .ctx-tool-grid {
        gap: 3px;
        padding: 3px 8px 6px;
    }
    .ctx-tool-btn {
        padding: 8px 4px;
        gap: 3px;
        border-radius: 8px;
    }
    .ctx-tool-btn .ctx-ico { font-size: 16px; }
    .ctx-tool-btn .ctx-lbl { font-size: 9px; }

    .ctx-item {
        padding: 9px 14px;
        gap: 10px;
    }
    .ctx-item .ctx-ico { font-size: 14px; width: 16px; }
    .ctx-item .ctx-lbl { font-size: 12px; }

    .ctx-divider { margin: 2px 10px; }
    .ctx-safe-area { height: 12px; }
}

/* =========================================
   MARCADORES KM
   ========================================= */
.km-marker-wrapper { position: relative; width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.km-circle { background: white; color: #222; border: 2px solid #000; border-radius: 50%; width: 22px; height: 22px; font-size: 10px; font-weight: bold; display: flex; align-items: center; justify-content: center; z-index: 2; box-shadow: 0 1px 4px rgba(0,0,0,0.6); }
.dir-arrow { position: absolute; width: 16px; height: 16px; z-index: 1; overflow: visible; filter: drop-shadow(1px 1px 2px rgba(0,0,0,0.5)); }

/* =========================================
   MODAL ICONOS
   ========================================= */
.icon-list-modal { max-height: 50vh; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 5px; }
.icon-list-modal::-webkit-scrollbar { width: 8px; }
.icon-list-modal::-webkit-scrollbar-thumb { background: #495057; border-radius: 4px; }
.icon-row-btn { display: flex; align-items: center; gap: 15px; background: #343a40; border: 1px solid #495057; border-radius: 5px; padding: 10px 15px; cursor: pointer; color: #fff; transition: 0.2s; }
.icon-row-btn:hover { background: #0d6efd; border-color: #0d6efd; }
.icon-row-emoji { font-size: 22px; min-width: 30px; text-align: center; }
.icon-row-text { font-size: 0.95rem; font-weight: 500; }

/* =========================================
   MODAL INFO
   ========================================= */
.info-section-title { font-size: 0.9rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px dashed #495057; padding-bottom: 5px; margin-bottom: 10px; color: #0d6efd; }

/* =========================================
   SUBMENÚ CONVERTIDOR GPS (navbar)
   ========================================= */
.dropend-custom { position: relative; }
.dropend-custom .submenu-custom {
    display: none;
    position: absolute;
    top: -5px;
    left: 95%;
    min-width: 260px;
    z-index: 2050;
}
.dropend-custom:hover .submenu-custom { display: block; }

@media (max-width: 991px) {
    .dropend-custom .submenu-custom {
        display: block !important;
        position: static !important;
        float: none !important;
        width: auto !important;
        left: auto !important;
        top: auto !important;
        padding-left: 20px !important;
        background-color: transparent !important;
        border: none !important;
        box-shadow: none !important;
        margin-top: 5px !important;
    }
    .dropend-custom > a > span.text-muted.small.ms-3 { display: none !important; }
    .dropend-custom .submenu-custom li a {
        padding: 8px 15px !important;
        font-size: 0.95em !important;
        color: #adb5bd !important;
    }

    #navbarControles {
        position: absolute;
        top: 100%;
        left: 0;
        width: 100%;
        background-color: var(--bs-body-bg);
        z-index: 3000;
        padding: 10px 15px;
        box-shadow: 0 10px 15px rgba(0,0,0,0.5);
        border-bottom: 1px solid #444;
    }
}

/* =========================================
   OPTIMIZACIÓN MÓVIL
   ========================================= */
@media (max-width: 768px) {
    #tabs-container {
        display: flex;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 5px;
    }
    .track-tab {
        flex-shrink: 0;
        max-width: 150px;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    #stats-bar {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        white-space: nowrap !important;
        padding: 10px 15px !important;
        gap: 7px !important;
        font-size: 0.85rem !important;
        -webkit-overflow-scrolling: touch;
        align-items: center !important;
        min-height: 40px;
    }
    #stats-bar > span {
        width: auto !important;
        margin-bottom: 0 !important;
        flex-shrink: 0 !important;
        display: inline-flex !important;
        align-items: center !important;
        white-space: nowrap !important;
        gap: 5px !important;
    }
    #stats-bar::-webkit-scrollbar { display: none; }
    #main-container { flex-direction: column; }
    #waypoints-panel {
        width: 100% !important;
        height: 30vh;
        border-left: none;
        border-top: 2px solid #444;
    }
}

/* =========================================
   OCULTAR ELEMENTOS PESADOS AL MOVER MAPA
   ========================================= */
body.map-is-moving .km-marker-wrapper,
body.map-is-moving .wpt-tooltip { display: none !important; }

/* =========================================
   GRABACIÓN GPS
   ========================================= */
@keyframes pulse-recording {
    0%   { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.8); }
    70%  { box-shadow: 0 0 0 12px rgba(220, 53, 69, 0); }
    100% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0); }
}
.recording-active {
    background-color: #dc3545 !important;
    color: white !important;
    border-color: #dc3545 !important;
    animation: pulse-recording 1.5s infinite;
}
body.is-recording-map #map {
    border: 3px solid #dc3545 !important;
    border-radius: 4px;
    box-sizing: border-box;
}

/* =========================================
   MARCADOR TRIÁNGULO DIRECCIÓN GPS
   ========================================= */
.current-location-marker-wrapper {
    display: flex; justify-content: center; align-items: center;
    width: 32px; height: 32px;
    transition: transform 0.2s ease-out;
}
.direction-triangle {
    width: 0; height: 0;
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-left: 20px solid #dc3545;
    filter: drop-shadow(0 0 2px rgba(0,0,0,0.5));
    position: relative;
}
.direction-triangle::after {
    content: '';
    position: absolute;
    top: -8px; left: -19px;
    width: 0; height: 0;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-left: 17px solid white;
    z-index: -1;
}

/* =========================================
   CONTROLES LEAFLET PERSONALIZADOS
   ========================================= */
.leaflet-control-center {
    background-color: #fff;
    width: 44px !important; height: 44px !important;
    line-height: 44px; color: #212529; font-size: 1.4rem;
    display: flex !important; align-items: center; justify-content: center;
    cursor: pointer; border-bottom: 1px solid #ccc; transition: background 0.2s;
}
.leaflet-control-center:hover { background-color: #f8f9fa; }
.leaflet-control-center i { line-height: 0; pointer-events: none; }

.leaflet-control-undo {
    background-color: #fff;
    width: 44px !important; height: 44px !important;
    line-height: 44px; color: #0d6efd; font-size: 1.4rem;
    display: flex !important; align-items: center; justify-content: center;
    cursor: pointer; border-bottom: none; border-radius: 4px;
    box-shadow: 0 1px 5px rgba(0,0,0,0.4);
}
.leaflet-control-undo:hover { background-color: #f8f9fa; color: #0056b3; }
.leaflet-control-undo i { line-height: 0; pointer-events: none; }

.leaflet-control-center:active,
.leaflet-control-undo:active {
    transform: scale(0.92);
    background-color: #e9ecef !important;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
    transition: transform 0.1s;
}
.leaflet-control-undo:active i { transform: rotate(-45deg); transition: transform 0.1s; }

.leaflet-control-undo.active-press,
.leaflet-control-center.active-press {
    transform: scale(0.90) !important;
    background-color: #e9ecef !important;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.2) !important;
    transition: transform 0.1s;
}

.leaflet-control-layers label { font-size: 13px !important; }

/* =========================================
   PLANIFICADOR (marcadores inicio/fin)
   ========================================= */
.marker-plan-start, .marker-plan-end { background: none !important; border: none !important; }

/* =========================================
   CUSTOM SCROLLBAR
   ========================================= */
.custom-scrollbar::-webkit-scrollbar { width: 5px; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #495057; border-radius: 3px; }

/* =========================================
   FILA INFERIOR MENÚ CONTEXTUAL
   ========================================= */
.ctx-bottom-actions {
    display: flex;
    gap: 8px;
    padding: 4px 10px 8px;
    justify-content: center;
}

.ctx-bottom-actions .ctx-tool-btn {
    flex: 1;
    max-width: 160px;
    padding: 6px 4px;
    gap: 3px;
}

.ctx-bottom-actions .ctx-tool-btn .ctx-ico { font-size: 14px; }
.ctx-bottom-actions .ctx-tool-btn .ctx-lbl { font-size: 10px; }

.ctx-btn-delete {
    background: #3d1212;
    border: 1px solid #7a2020;
}
.ctx-btn-delete .ctx-ico,
.ctx-btn-delete .ctx-lbl { color: #ff6b6b; }
.ctx-btn-delete:hover  { background: #521a1a; }
.ctx-btn-delete:active { background: #6b2020; }

@media (min-width: 768px) {
    .ctx-bottom-actions .ctx-tool-btn .ctx-ico { font-size: 15px; }
    .ctx-bottom-actions .ctx-tool-btn .ctx-lbl { font-size: 11px; }
}

@media (max-width: 767px) {
    .ctx-bottom-actions {
        gap: 6px;
        padding: 3px 8px 6px;
    }
    .ctx-bottom-actions .ctx-tool-btn {
        padding: 5px 4px;
    }
    .ctx-bottom-actions .ctx-tool-btn .ctx-ico { font-size: 13px; }
    .ctx-bottom-actions .ctx-tool-btn .ctx-lbl { font-size: 9px; }
}

.leaflet-control-brujula a {
    width: 44px !important;
    height: 44px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}