:root {
    --bg: #f5f7fb;
    --panel: #ffffff;
    --text: #172033;
    --muted: #667085;
    --line: #dce4ef;
    --brand: #0f8b8d;
    --brand-dark: #0a6567;
    --warning: #f5b841;
    --done: #43aa8b;
    --shadow: 0 18px 48px rgba(23, 32, 51, 0.14);
    --app-height: 100dvh;
    --mobile-bottom-space: calc(env(safe-area-inset-bottom, 0px) + 50px);
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
}

body {
    margin: 0;
    min-height: var(--app-height);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
}

.shell {
    position: fixed;
    inset: 0;
    width: 100%;
    height: var(--app-height);
    overflow: hidden;
}

.topbar {
    position: absolute;
    z-index: 530;
    top: 16px;
    left: 16px;
    right: 16px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 18px;
    pointer-events: none;
}

.map-heading {
    max-width: min(420px, calc(100vw - 32px));
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px 16px;
    background: rgba(255, 255, 255, 0.88);
    box-shadow: 0 12px 32px rgba(23, 32, 51, 0.12);
    backdrop-filter: blur(14px);
    pointer-events: auto;
}

h1 {
    margin: 0 0 6px;
    font-size: clamp(24px, 3vw, 36px);
    line-height: 1;
    letter-spacing: 0;
}

.subtitle {
    margin: 0;
    color: var(--muted);
    font-size: 15px;
}

.topbar-logo {
    display: none;
    width: 46px;
    height: auto;
    flex: 0 0 auto;
    align-self: center;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.6);
    box-shadow: 0 0 14px rgba(255, 255, 255, 0.3);
}

.topbar-row {
    display: flex;
    align-items: center;
    gap: 10px;
    pointer-events: auto;
}

.stats {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
    pointer-events: auto;
}

.stat {
    min-width: 118px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    background: rgba(255, 255, 255, 0.82);
    box-shadow: 0 10px 28px rgba(23, 32, 51, 0.08);
}

.stat strong {
    display: block;
    font-size: 20px;
    line-height: 1;
}

.stat span {
    display: block;
    margin-top: 5px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.map-wrap {
    position: absolute;
    inset: 0;
    overflow: hidden;
    width: 100%;
    height: 100%;
    min-height: var(--app-height);
    border: 0;
    border-radius: 0;
    background: var(--panel);
    box-shadow: none;
}

#map {
    width: 100%;
    height: 100%;
    min-height: var(--app-height);
}

/* Respaldo local para Leaflet:
   si el CSS externo no carga, los tiles se ven cortados y desordenados. */
.leaflet-container {
    overflow: hidden;
    background: #d8dee9;
    outline: 0;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
    position: absolute;
    left: 0;
    top: 0;
}

.leaflet-container {
    font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
    user-select: none;
    -webkit-user-drag: none;
}

.leaflet-tile {
    filter: inherit;
    visibility: hidden;
}

.leaflet-tile-loaded {
    visibility: inherit;
}

.leaflet-zoom-animated {
    transform-origin: 0 0;
}

.leaflet-control {
    position: relative;
    z-index: 800;
    pointer-events: auto;
}

.leaflet-top,
.leaflet-bottom {
    position: absolute;
    z-index: 1000;
    pointer-events: none;
}

.leaflet-top {
    top: 0;
}

.leaflet-right {
    right: 0;
}

.leaflet-bottom {
    bottom: 0;
}

.leaflet-left {
    left: 0;
}

.leaflet-control-zoom {
    border: 2px solid rgba(0, 0, 0, 0.14);
    border-radius: 4px;
    background: #ffffff;
}

.leaflet-control-zoom a {
    display: block;
    width: 30px;
    height: 30px;
    color: #172033;
    line-height: 30px;
    text-align: center;
    text-decoration: none;
    font-size: 20px;
    font-weight: 700;
    background: #ffffff;
}

.leaflet-control-zoom a:first-child {
    border-bottom: 1px solid #dce4ef;
}

.leaflet-control-attribution {
    padding: 2px 6px;
    color: #667085;
    background: rgba(255, 255, 255, 0.82);
}

.leaflet-popup {
    position: absolute;
    text-align: center;
    margin-bottom: 20px;
}

.leaflet-popup-content-wrapper {
    border-radius: 8px;
    padding: 1px;
    text-align: left;
    background: #ffffff;
    box-shadow: 0 8px 28px rgba(23, 32, 51, 0.18);
}

.leaflet-popup-content {
    min-width: 190px;
    margin: 14px 16px;
}

.leaflet-popup-tip-container {
    width: 40px;
    height: 20px;
    position: absolute;
    left: 50%;
    margin-left: -20px;
    overflow: hidden;
    pointer-events: none;
}

.leaflet-popup-tip {
    width: 17px;
    height: 17px;
    margin: -10px auto 0;
    transform: rotate(45deg);
    background: #ffffff;
}

.legend {
    position: absolute;
    z-index: 500;
    right: 16px;
    bottom: 16px;
    display: grid;
    gap: 8px;
    width: min(260px, calc(100% - 32px));
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 12px 32px rgba(23, 32, 51, 0.12);
    backdrop-filter: blur(14px);
}

.location-panel {
    position: absolute;
    z-index: 510;
    right: 16px;
    bottom: 118px;
    display: grid;
    gap: 8px;
    width: min(260px, calc(100% - 32px));
    pointer-events: auto;
}

.location-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--text);
    font-size: 14px;
    font-weight: 900;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 12px 32px rgba(23, 32, 51, 0.12);
    backdrop-filter: blur(14px);
}

.location-button:hover {
    border-color: #c8d5e4;
}

.location-icon {
    width: 18px;
    height: 18px;
    fill: var(--brand);
}

.location-status {
    display: none;
    margin: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.35;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 12px 32px rgba(23, 32, 51, 0.1);
    backdrop-filter: blur(14px);
}

.order-card {
    position: absolute;
    z-index: 520;
    left: 16px;
    top: 122px;
    width: min(340px, calc(100% - 32px));
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 16px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 16px 42px rgba(23, 32, 51, 0.16);
    backdrop-filter: blur(14px);
}

.order-card[hidden] {
    display: none;
}

.order-card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.order-card-title {
    margin: 0 0 5px;
    font-size: 18px;
    line-height: 1.2;
}

.order-card-subtitle {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.35;
}

.close-card {
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    width: 30px;
    height: 30px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--muted);
    cursor: pointer;
    background: #ffffff;
    font-size: 18px;
    line-height: 1;
}

.close-card:hover {
    color: var(--text);
    border-color: #c9d5e4;
}

.order-grid {
    display: grid;
    gap: 10px;
    margin: 0;
}

.order-row {
    display: grid;
    gap: 3px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
}

.order-label {
    color: var(--muted);
    font-size: 11px;
    font-weight: 800;
    text-transform: uppercase;
}

.order-value {
    color: var(--text);
    font-size: 14px;
    line-height: 1.35;
}

.delivery-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.action-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 42px;
    border: 0;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 900;
    font-family: inherit;
    text-decoration: none;
    cursor: pointer;
}

.action-button:disabled {
    cursor: not-allowed;
    opacity: 0.66;
}

.delivered-action {
    color: #172033;
    background: var(--warning);
    box-shadow: 0 10px 22px rgba(245, 184, 65, 0.28);
}

.delivered-action:hover {
    background: #f0aa22;
}

.route-action {
    color: #ffffff;
    background: var(--brand);
    box-shadow: 0 10px 22px rgba(15, 139, 141, 0.24);
}

.route-action:hover {
    background: var(--brand-dark);
}

.action-icon {
    width: 17px;
    height: 17px;
    flex: 0 0 auto;
    fill: currentColor;
}

.signature-modal {
    position: fixed;
    z-index: 1200;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(23, 32, 51, 0.38);
    backdrop-filter: blur(10px);
}

.signature-modal[hidden] {
    display: none;
}

.signature-card {
    width: min(520px, 100%);
    max-height: min(760px, calc(var(--app-height) - 36px));
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 18px;
    background: #ffffff;
    box-shadow: var(--shadow);
}

.signature-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 16px;
}

.signature-kicker {
    margin: 0 0 4px;
    color: #7a4e00;
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.signature-title {
    margin: 0;
    color: var(--text);
    font-size: 22px;
    line-height: 1.1;
}

.signature-copy {
    margin: 6px 0 0;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.35;
}

.signature-form {
    display: grid;
    gap: 12px;
}

.field-label {
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
}

.text-field {
    width: 100%;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--text);
    font: inherit;
    background: #ffffff;
}

.text-field:focus {
    border-color: var(--brand);
    outline: 3px solid rgba(15, 139, 141, 0.16);
}

.notes-field {
    min-height: 82px;
    resize: vertical;
}

.signature-pad-wrap {
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #ffffff;
}

.signature-pad-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    min-height: 42px;
    padding: 8px 10px;
    border-bottom: 1px solid var(--line);
    color: var(--muted);
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
    background: #f8fafc;
}

.signature-pad {
    display: block;
    width: 100%;
    height: 190px;
    touch-action: none;
    background:
        linear-gradient(transparent 72%, rgba(102, 112, 133, 0.22) 72%, rgba(102, 112, 133, 0.22) 73%, transparent 73%),
        #ffffff;
}

.text-button {
    border: 0;
    padding: 6px 0;
    color: var(--brand-dark);
    font: inherit;
    font-size: 12px;
    font-weight: 900;
    cursor: pointer;
    background: transparent;
}

.signature-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 2px;
}

.secondary-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--text);
    font: inherit;
    font-size: 14px;
    font-weight: 900;
    cursor: pointer;
    background: #ffffff;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    border-radius: 999px;
    padding: 5px 10px;
    color: #7a4e00;
    font-size: 12px;
    font-weight: 900;
    background: rgba(245, 184, 65, 0.22);
}

.status-badge.pending,
.status-badge.priority {
    color: #7a4e00;
    background: rgba(245, 184, 65, 0.22);
}

.status-badge.done {
    color: #1f6b56;
    background: rgba(67, 170, 139, 0.18);
}

.legend-title {
    margin: 0 0 2px;
    font-size: 13px;
    text-transform: uppercase;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 13px;
}

.legend-dot {
    width: 11px;
    height: 11px;
    border-radius: 999px;
}

.legend-dot.pending {
    background: var(--warning);
}

.legend-dot.done {
    background: var(--done);
}

.delivery-marker {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 3px solid #ffffff;
    border-radius: 999px;
    color: #172033;
    font-size: 13px;
    font-weight: 900;
    background: var(--warning);
    box-shadow: 0 10px 22px rgba(245, 184, 65, 0.34);
}

.delivery-marker.priority {
    background: var(--warning);
    color: #172033;
    box-shadow: 0 10px 22px rgba(245, 184, 65, 0.34);
}

.delivery-marker.done {
    background: var(--done);
    color: #ffffff;
    box-shadow: 0 10px 22px rgba(67, 170, 139, 0.34);
}

/* Entrega más cercana: dorado con anillo pulsante */
.delivery-marker.nearest {
    background: #ffd700;
    box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.5);
    color: #1a1a2e;
    animation: pulse-nearest 1.6s ease-out infinite;
    z-index: 2000 !important;
}

@keyframes pulse-nearest {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.6);
    }
    70% {
        box-shadow: 0 0 0 16px rgba(255, 215, 0, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(255, 215, 0, 0);
    }
}

/* Texto chico con la distancia en metros dentro del popup */
.delivery-distance {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    font-weight: 400;
    color: #ffd700;
    letter-spacing: 0.5px;
}

.driver-marker {
    position: relative;
    display: grid;
    place-items: center;
    width: 46px;
    height: 46px;
    border-radius: 999px;
}

.driver-marker::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: rgba(15, 139, 141, 0.18);
    box-shadow: 0 0 0 12px rgba(15, 139, 141, 0.1);
}

.driver-dot {
    position: relative;
    z-index: 1;
    width: 22px;
    height: 22px;
    border: 4px solid #ffffff;
    border-radius: 999px;
    background: var(--brand);
    box-shadow: 0 10px 24px rgba(15, 139, 141, 0.32);
}

.driver-arrow {
    position: absolute;
    z-index: 2;
    top: -4px;
    left: 50%;
    width: 0;
    height: 0;
    border-left: 7px solid transparent;
    border-right: 7px solid transparent;
    border-bottom: 14px solid #172033;
    transform: translateX(-50%) rotate(var(--driver-heading, 0deg));
    transform-origin: 50% 27px;
    opacity: 0;
    transition: transform 220ms ease, opacity 180ms ease;
}

.driver-marker.has-heading .driver-arrow {
    opacity: 1;
}

.popup-title {
    margin: 0 0 6px;
    font-size: 15px;
}

.popup-copy {
    margin: 0 0 8px;
    color: #667085;
    font-size: 13px;
    line-height: 1.35;
}

.popup-meta {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.popup-pill {
    border-radius: 999px;
    padding: 4px 8px;
    color: #0a6567;
    font-size: 12px;
    font-weight: 800;
    background: rgba(15, 139, 141, 0.12);
}

@media (max-width: 760px) {
    .topbar {
        align-items: stretch;
        flex-direction: column;
        left: 10px;
        right: 10px;
        top: 10px;
    }

    .topbar-logo {
        display: block;
    }

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

    .stats {
        justify-content: flex-end;
    }

    .stat {
        min-width: 88px;
        padding: 8px 10px;
    }

    .stat strong {
        font-size: 18px;
    }

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

    .map-heading {
        display: none;
    }

    .order-card {
        top: auto;
        left: 10px;
        bottom: var(--mobile-bottom-space);
        width: min(340px, calc(100% - 20px));
        max-height: calc(var(--app-height) - 210px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .legend {
        right: 10px;
        bottom: var(--mobile-bottom-space);
        width: min(220px, calc(100% - 20px));
    }

    .location-panel {
        right: 10px;
        bottom: calc(var(--mobile-bottom-space) + 92px);
        width: min(220px, calc(100% - 20px));
    }

    .order-card:not([hidden]) + .legend {
        display: none;
    }

    .order-card:not([hidden]) ~ .location-panel {
        display: none;
    }

    .signature-modal {
        align-items: end;
        padding: 10px;
    }

    .signature-card {
        max-height: calc(var(--app-height) - 20px);
        padding: 16px;
    }

    .signature-pad {
        height: 170px;
    }
}
