/**
 * TrackPress Client Stylesheet
 * Version: 7.0 (Responsive UI)
 */

/* --- Main Layout --- */
.trackpress-container {
    position: relative;
    height: 700px;
    border-radius: 12px;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: #f0f0f1;
}
.trackpress-container.full-height-map {
    position: fixed; top: 0; left: 0; width: 100%; height: 100vh;
    z-index: 99998; border-radius: 0;
}
.trackpress-map-area { height: 100%; width: 100%; }

/* --- Panel (Sidebar on Desktop, Bottom Sheet on Mobile) --- */
.trackpress-panel {
    position: absolute;
    z-index: 1001;
    background: rgba(242, 242, 247, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 0, 0, 0.1);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
}

/* --- Desktop Sidebar Styles --- */
@media (min-width: 769px) {
    .trackpress-panel {
        top: 15px; left: 15px; bottom: 15px;
        width: 320px;
        border-radius: 12px;
        transition: transform 0.4s ease, opacity 0.4s ease;
    }
    .trackpress-container.sidebar-collapsed .trackpress-panel {
        transform: translateX(-110%);
        opacity: 0;
    }
    .trackpress-panel-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 10px 15px 20px;
        border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        flex-shrink: 0;
    }
    .trackpress-panel-handle { display: none; }
    #trackpress-sidebar-toggle { display: flex; }
}

.trackpress-panel-header h2 { margin: 0; font-size: 20px; font-weight: 600; flex-grow: 1; }
.trackpress-panel-content { padding: 10px; overflow-y: auto; flex-grow: 1; }

/* --- Device List Item --- */
.trackpress-device-item {
    padding: 12px; border-radius: 10px; cursor: pointer;
    transition: background-color 0.2s; margin-bottom: 5px;
}
.trackpress-device-item:hover { background-color: rgba(0, 0, 0, 0.05); }
.trackpress-device-item.active { background-color: #007aff; color: white; }
.device-item-content { display: flex; align-items: center; gap: 15px; position: relative; }
.device-icon { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; object-fit: cover; }
.device-details { flex-grow: 1; overflow: hidden; }
.device-title { font-size: 14px; font-weight: 600; color: #1d1d1f; margin-bottom: 2px; }
.device-info { font-size: 13px; color: #6e6e73; line-height: 1.4; }
.device-status {
    position: absolute; top: 0; right: 0; font-size: 11px; font-weight: 500;
    color: #6e6e73; background-color: rgba(0,0,0,0.05);
    padding: 2px 6px; border-radius: 5px; white-space: nowrap;
}
.trackpress-device-item.active .device-title,
.trackpress-device-item.active .device-info,
.trackpress-device-item.active .device-status { color: white; }
.trackpress-device-item.active .device-status { background-color: rgba(255,255,255,0.2); }

/* --- Close Button for Mobile Panel --- */
#trackpress-panel-close-btn {
    display: none; /* Hidden by default */
    position: absolute;
    top: 18px;
    right: 15px;
    width: 30px;
    height: 30px;
    background-color: rgba(0, 0, 0, 0.08);
    background-image: url("data:image/svg+xml,%3Csvg width='14' height='14' viewBox='0 0 14 14' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L13 13' stroke='%236E6E73' stroke-width='2' stroke-linecap='round'/%3E%3Cpath d='M13 1L1 13' stroke='%236E6E73' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E%0A");
    background-repeat: no-repeat;
    background-position: center;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    z-index: 10;
}


/* --- Floating Map Controls --- */
.leaflet-control-container .leaflet-top.leaflet-right,
.leaflet-control-container .leaflet-top.leaflet-left { 
    pointer-events: none; 
}
.leaflet-control-container .leaflet-top.leaflet-right { padding-top: 20px; padding-right: 20px; }
.leaflet-control-container .leaflet-top.leaflet-left { padding-top: 15px; padding-left: 15px; }
.trackpress-control-group {
    background: rgba(255, 255, 255, 0.85); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(0,0,0,0.1); border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    display: flex; pointer-events: auto; margin: 0 25px;
}
.leaflet-top.leaflet-right .trackpress-control-group { transform-origin: top right; transform: scale(1); }
.leaflet-top.leaflet-left .trackpress-control-group { transform-origin: top left; transform: scale(1); }
.trackpress-control-button {
    width: 48px; height: 48px; cursor: pointer; display: flex; align-items: center; justify-content: center;
    transition: background-color 0.2s; border: none; background-color: transparent; padding: 0;
}
.trackpress-control-button:focus { outline: none; }
.trackpress-control-button:hover { background-color: rgba(0,0,0,0.05); }
.trackpress-control-button:first-child:not(:last-child) { border-right: 1px solid rgba(0,0,0,0.1); }
.trackpress-control-button img { width: 24px; height: 24px; }

/* --- Styles for our new independent controls container --- */
.trackpress-custom-controls-wrapper {
    position: absolute;
    z-index: 1002; /* Ensure it's above map layers */

    /* Desktop position (next to the open sidebar) */
    bottom: 20px;
    left: 335px;

    /* Smooth transition for sidebar collapse */
    transition: left 0.4s ease, top 0.4s ease, right 0.4s ease;
}

/* Move to the left when sidebar is collapsed on desktop */
.trackpress-container.sidebar-collapsed .trackpress-custom-controls-wrapper {
    left: 15px;
}

/* --- Layer Switcher Modal (Desktop) --- */
.trackpress-layer-switcher {
    position: absolute;
    bottom: 65px; /* Button height (48px) + offset */
    left: 0;
    width: 150px;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    padding: 5px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}

.trackpress-layer-switcher.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.trackpress-layer-option {
    display: flex;
    align-items: center;
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.trackpress-layer-option:hover {
    background-color: rgba(0,0,0,0.05);
}

.trackpress-layer-option.active {
    background-color: #007aff;
    color: white;
}

.trackpress-layer-option img {
    width: 40px;
    height: 40px;
    border-radius: 6px;
    margin-right: 10px;
    object-fit: cover;
}

.trackpress-layer-option span {
    font-size: 14px;
    font-weight: 500;
}


/* --- Custom Popup Styles (Desktop) --- */
.trackpress-popup .leaflet-popup-content-wrapper {
    background: rgba(242, 242, 247, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 14px;
    box-shadow: 0 5px 25px rgba(0,0,0,0.15);
    padding: 20px 0 15px 0;
}
.trackpress-popup .leaflet-popup-content {
    margin: 0 20px;
    width: 210px !important;
}
.trackpress-popup .leaflet-popup-tip-container { display: none; }
.trackpress-popup-header {
    border-bottom: 1px solid rgba(0,0,0,0.1);
    padding-bottom: 12px;
    margin-bottom: 12px;
}
.trackpress-popup-header h3 { font-size: 18px; font-weight: 600; margin: 0 0 4px 0; color: #1d1d1f; }
.trackpress-popup-info { font-size: 13px; color: #333; line-height: 1.4; }
.trackpress-popup-status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #666; margin-top: 8px; }
.battery-indicator {
    width: 15px; height: 8px; border: 1px solid #8e8e93;
    border-radius: 2px; padding: 1px; position: relative;
}
.battery-indicator::after {
    content: ''; position: absolute; right: -2px; top: 1.5px;
    width: 1px; height: 4px; background-color: #8e8e93;
    border-radius: 0 1px 1px 0;
}
.battery-level { height: 100%; background-color: #34c759; border-radius: 1px; }
.battery-level.low { background-color: #ffcc00; }
.battery-level.critical { background-color: #ff3b30; }
.trackpress-popup-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.trackpress-popup-action-btn {
    background-color: #fff; border-radius: 10px; padding: 10px;
    text-align: left; cursor: pointer; transition: background-color 0.2s;
    border: none; box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    display: flex; flex-direction: column; justify-content: space-between; height: 80px;
}
.trackpress-popup-action-btn:hover { background-color: #f5f5f5; }
.trackpress-popup-action-btn.active { background-color: #007aff; }
.trackpress-popup-action-btn.active span { color: white; }
.trackpress-popup-action-btn.active img { filter: brightness(0) invert(1); }
.trackpress-popup-action-btn img { width: 24px; height: 24px; margin-bottom: 4px; }
.trackpress-popup-action-btn span { font-size: 12px; font-weight: 600; color: #1d1d1f; display: block; }

/* --- Mobile Styles --- */
@media (max-width: 768px) {
    .trackpress-panel {
        left: 0; right: 0; bottom: 0;
        height: 90vh;
        border-radius: 20px 20px 0 0;
        transform: translateY(calc(100% - 80px));
        transition: transform 0.4s ease-out;
    }
    .trackpress-panel.partial {
        transform: translateY(calc(100% - 40vh));
    }
    .trackpress-panel.full {
        transform: translateY(calc(100% - 90vh));
    }
    .trackpress-panel-header {
        padding: 12px;
        text-align: center;
        cursor: grab;
    }
    .trackpress-panel-handle {
        width: 40px;
        height: 5px;
        background-color: #c7c7cc;
        border-radius: 2.5px;
        margin: 0 auto 10px;
    }
    #trackpress-sidebar-toggle { display: none; }
    #trackpress-sidebar-expand-wrapper { display: none; }
    .trackpress-popup { display: none !important; }

    /* Show the close button only when the details view is active */
    .trackpress-panel.is-details-view #trackpress-panel-close-btn {
        display: block;
    }


    /* --- Mobile styles for controls and modal --- */

    /* Hide the original Leaflet zoom buttons */
    .leaflet-top.leaflet-right {
        display: none;
    }

    /* Reposition OUR independent controls container to the top right */
    .trackpress-custom-controls-wrapper {
        bottom: auto;
        left: auto;
        top: 15px;
        right: 15px;
    }

    /* Ensure the buttons stay on top of the modal overlay */
    .trackpress-layer-switcher-wrapper .trackpress-control-group {
        position: relative;
        z-index: 2001;
    }

    /* Style the layer switcher as a full-screen modal */
    .trackpress-layer-switcher {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(8px);
        -webkit-backdrop-filter: blur(8px);
        z-index: 2000;
    
        /* --- NEW GRID LAYOUT --- */
        display: grid;
        grid-template-columns: 1fr 1fr; /* Создаем 2 колонки равной ширины */
        justify-items: center;
        align-content: center;
        /* --- END NEW GRID LAYOUT --- */

        gap: 15px;
        padding: 10px;
        transform: none !important;
        box-sizing: border-box;
    }

    /* Style the options for mobile */
    .trackpress-layer-switcher .trackpress-layer-option {
        display: flex;
        flex-direction: column;
        align-items: center;
    
        /* --- NEW SIZING --- */
        width: 100%;         /* Занимаем всю ширину колонки грида */
        max-width: 180px;    /* Ограничиваем максимальную ширину на планшетах */
        /* --- END NEW SIZING --- */

        background: rgba(255, 255, 255, 0.95);
        border-radius: 14px;
        padding: 10px;
        border: 1px solid rgba(0,0,0,0.05);
    }

    /* Style the image and text inside the options */
    .trackpress-layer-switcher .trackpress-layer-option img {
        width: 100%;
        height: auto;
        aspect-ratio: 16 / 10;
        object-fit: cover;
        border-radius: 8px;
        margin: 0 0 10px 0;
    }
    .trackpress-layer-switcher .trackpress-layer-option span {
        font-size: 14px;
        font-weight: 600;
        color: #333;
    }
}

/* Remove default Leaflet zoom */
.leaflet-control-zoom { display: none !important; }

/* --- Spinner & Notices --- */
.trackpress-spinner-overlay { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: rgba(255, 255, 255, 0.7); z-index: 2000; display: flex; align-items: center; justify-content: center; }
.trackpress-spinner { border: 4px solid #f3f3f3; border-top: 4px solid #3498db; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
.trackpress-notice { margin: 10px; padding: 10px; border-radius: 8px; border-left: 4px solid; }
.notice-error { background: #fff; border-left-color: #d63638; }
.notice-warning { background: #fff; border-left-color: #ffb900; }

/* Prevents the route toggle icon from disappearing on active state */
.trackpress-popup-action-btn[data-action="toggle-track"].active img {
    filter: none;
}