* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

#map {
    width: 100%;
    height: 100%;
}

/* Пошукове вікно (внизу справа: кнопка «Пошук» розсувається вліво в поле вводу) */
.search-box {
    position: absolute;
    bottom: 20px;
    right: 20px;
    z-index: 1000;
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    max-width: 400px;
    transition: max-width 0.25s ease;
}

.search-box--collapsed .search-input,
.search-box--collapsed .search-suggestions {
    width: 0;
    min-width: 0;
    padding: 0;
    margin: 0;
    border: none;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}

.search-box--expanded .search-input {
    width: 280px;
    min-width: 120px;
    margin-right: 8px;
}

.search-input {
    padding: 10px 14px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 8px;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    outline: none;
    transition: width 0.25s ease, opacity 0.2s ease, margin 0.25s ease;
}

.search-input:focus {
    border-color: #0066cc;
    box-shadow: 0 2px 8px rgba(0, 102, 204, 0.2);
}

.search-suggestions {
    position: absolute;
    bottom: 100%;
    left: 0;
    right: auto;
    width: 300px;
    margin-bottom: 4px;
    background: white;
    border: 1px solid #ccc;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    max-height: 280px;
    overflow-y: auto;
    display: none;
    transition: opacity 0.2s ease;
}

.search-suggestions.visible {
    display: block;
}

.search-suggestion-item {
    padding: 10px 14px;
    font-size: 14px;
    cursor: pointer;
    border: none;
    width: 100%;
    text-align: left;
    background: none;
    color: #333;
}

.search-suggestion-item:hover,
.search-suggestion-item[aria-selected="true"] {
    background: #f0f0f0;
}

.search-toggle-btn {
    flex-shrink: 0;
    height: 48px;
    padding: 0 18px;
    border-radius: 24px;
    border: none;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    font-size: 14px;
    font-weight: 500;
    color: #333;
    cursor: pointer;
    transition: all 0.2s;
}

.search-toggle-btn:hover {
    background: #f5f5f5;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* Легенда */
.legend {
    position: absolute;
    top: 10px;
    left: 10px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    max-width: 320px;
    max-height: calc(100vh - 20px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.legend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid #e0e0e0;
    background: #f5f5f5;
}

.legend-header h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
    flex: 1;
}

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

.legend-icon-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    border-radius: 4px;
    transition: all 0.2s;
}

.legend-icon-btn:hover {
    background-color: #e0e0e0;
    color: #333;
}

.legend-icon-btn:active {
    background-color: #d0d0d0;
}

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

.legend-share-menu {
    position: relative;
}

.legend-share-menu.active .legend-icon-btn {
    background-color: #e0e0e0;
    color: #333;
}

.share-dropdown {
    position: fixed;
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 2000; /* Вищий за легенду (1000) */
    min-width: 180px;
    overflow: hidden;
}

.share-menu-item {
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: white;
    text-align: left;
    cursor: pointer;
    font-size: 14px;
    color: #333;
    transition: background-color 0.2s;
}

.share-menu-item:hover {
    background-color: #f5f5f5;
}

.share-menu-item:active {
    background-color: #e0e0e0;
}

.legend-toggle {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
}

.legend-toggle:hover {
    color: #000;
}

.legend-content {
    overflow-y: auto;
    padding: 8px;
    flex: 1;
    transition: max-height 0.3s ease-out, opacity 0.3s ease-out, padding 0.3s ease-out;
    max-height: 1000px; /* Велике значення для розгорнутої легенди */
    opacity: 1;
}

.legend.collapsed .legend-content {
    max-height: 0 !important;
    overflow: hidden !important;
    padding: 0 !important;
    margin: 0 !important;
    opacity: 0 !important;
    flex: 0 !important;
    pointer-events: none;
}

.legend-section {
    margin-bottom: 16px;
}

.legend-section:last-child {
    margin-bottom: 0;
}

.legend-section h4 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 8px;
    color: #333;
}

.basemap-switcher {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.basemap-option {
    display: flex;
    align-items: center;
    padding: 8px;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.basemap-option:hover {
    background-color: #f0f0f0;
}

.basemap-option input[type="radio"] {
    margin-right: 8px;
}

.basemap-option.active {
    background-color: #e3f2fd;
    font-weight: 500;
}

.layers-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 400px;
    overflow-y: auto;
}

.layer-item {
    display: flex;
    align-items: flex-start;
    padding: 8px;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.layer-item:hover {
    background-color: #f0f0f0;
}

.layer-item input[type="checkbox"] {
    margin-right: 8px;
    margin-top: 2px;
    flex-shrink: 0;
}

.layer-style-icon {
    margin-right: 8px;
    margin-top: 2px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.layer-style-icon img {
    width: 20px;
    height: 16px;
    object-fit: contain;
}

.layer-item-label {
    flex: 1;
    font-size: 14px;
    cursor: pointer;
    user-select: none;
    transition: color 0.2s;
}

.layer-item-label.layer-inactive {
    color: #999999;
    opacity: 0.6;
}

.layer-sublayers {
    margin-left: 24px;
    margin-top: 4px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.layer-sublayer {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #666;
    padding: 2px 0;
}

.layer-sublayer-icon {
    margin-right: 6px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.layer-sublayer-icon img {
    width: 16px;
    height: 16px;
    object-fit: contain;
}

.layer-sublayer-color {
    width: 16px;
    height: 16px;
    border-radius: 2px;
    margin-right: 6px;
    border: 1px solid #ccc;
    flex-shrink: 0;
}

/* Кнопки */
.btn {
    width: 100%;
    padding: 10px 16px;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 8px;
}

.btn:last-child {
    margin-bottom: 0;
}

.btn-primary {
    background-color: #1976d2;
    color: white;
}

.btn-primary:hover {
    background-color: #1565c0;
}

.btn-secondary {
    background-color: #f5f5f5;
    color: #333;
    border: 1px solid #ddd;
}

.btn-secondary:hover {
    background-color: #e0e0e0;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Інструменти карти (одна позиція вище за кнопку «Пошук») */
.map-tools {
    position: absolute;
    bottom: 80px;
    right: 20px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.map-tool-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    color: #666;
}

.map-tool-btn:hover {
    background: #f5f5f5;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.map-tool-btn.active {
    background: #1976d2;
    color: white;
}

/* Координати */
.map-tool-group {
    position: relative;
}

.coord-menu {
    position: absolute;
    right: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.18);
    padding: 4px 0;
    min-width: 100px;
    display: none;
    z-index: 1001;
}

.coord-menu.visible {
    display: block;
}

.coord-menu-item {
    display: block;
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: none;
    text-align: left;
    font-size: 14px;
    cursor: pointer;
    color: #333;
}

.coord-menu-item:hover {
    background: #f0f4f8;
}

.coord-panel {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: none;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 8px;
    border: 2px solid #1976d2;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    padding: 12px 40px 12px 16px;
    font-size: 14px;
    font-weight: bold;
    color: #1976d2;
    pointer-events: auto;
    user-select: none;
}

.coord-panel.visible {
    display: flex;
}

.coord-display {
    white-space: nowrap;
}

.coord-display--copyable {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    padding: 2px 0;
}

.coord-display--copyable:active {
    opacity: 0.75;
}

.coord-panel.copied {
    border-color: #2e7d32;
    color: #2e7d32;
}

.coord-panel-close {
    position: absolute;
    top: 4px;
    right: 8px;
    border: none;
    background: none;
    font-size: 20px;
    line-height: 20px;
    width: 24px;
    height: 24px;
    color: #666;
    cursor: pointer;
    padding: 0;
}

.coord-panel-close:hover {
    color: #333;
}

.coord-cursor-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #d32f2f;
    border: 2px solid white;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
    pointer-events: none;
}

.coord-crosshair {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 28px;
    height: 28px;
    margin-left: -14px;
    margin-top: -14px;
    pointer-events: none;
    z-index: 5;
    display: none;
}

.coord-crosshair.visible {
    display: block;
}

.coord-crosshair-h,
.coord-crosshair-v {
    position: absolute;
    background: #1976d2;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.85);
}

.coord-crosshair-h {
    left: 0;
    top: 50%;
    width: 100%;
    height: 1px;
    margin-top: -0.5px;
}

.coord-crosshair-v {
    top: 0;
    left: 50%;
    width: 1px;
    height: 100%;
    margin-left: -0.5px;
}

.coord-crosshair.snapped .coord-crosshair-h,
.coord-crosshair.snapped .coord-crosshair-v {
    background: #d32f2f;
}

/* GPS кнопка (для зворотної сумісності) */
.gps-control {
    position: absolute;
    bottom: 20px;
    right: 20px;
    z-index: 1000;
}

.gps-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    color: #666;
}

.gps-btn:hover {
    background: #f5f5f5;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.gps-btn.active {
    background: #1976d2;
    color: white;
}

.gps-btn.tracking {
    background: #4caf50;
    color: white;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.7;
    }
}

/* Лінійка */
.ruler-marker {
    user-select: none;
}

.ruler-label {
    user-select: none;
}

.ruler-total {
    user-select: none;
}

/* Popup */
.popup {
    position: absolute;
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    padding: 0;
    z-index: 2000;
    max-width: 300px;
    max-height: 80vh; /* Верхня межа вікна попапу */
    pointer-events: auto; /* Дозволяємо інтерактивність */
    display: flex;
    flex-direction: column;
    overflow: hidden; /* Скрол лише всередині .popup-body */
    min-height: 0;
}

.popup-content {
    padding: 16px;
    display: flex;
    flex-direction: column;
    flex: 1 1 0; /* база 0 — обов'язково для стиснення під max-height батька */
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
    position: relative;
    z-index: 0;
}

.popup-header {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 12px;
    padding-bottom: 8px;
    padding-right: 32px; /* Місце для кнопки закриття */
    border-bottom: 1px solid #e0e0e0;
    user-select: none; /* Запобігаємо виділенню тексту при перетягуванні */
    flex-shrink: 0;
    position: relative;
}

.popup.dragging {
    cursor: grabbing;
    user-select: none;
}

.popup.dragging .popup-header {
    cursor: grabbing;
}

.popup-body {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex: 1 1 auto;
    min-height: 0;
    /* Явна межа висоти + overflow: інакше в деяких браузерах flex не дає скрол */
    max-height: min(62vh, calc(80vh - 7.5rem));
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    /* Стилі для скролбара */
    scrollbar-width: thin;
    scrollbar-color: #ccc transparent;
}

.popup-body::-webkit-scrollbar {
    width: 6px;
}

.popup-body::-webkit-scrollbar-track {
    background: transparent;
}

.popup-body::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 3px;
}

.popup-body::-webkit-scrollbar-thumb:hover {
    background-color: #999;
}

.popup-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.popup-field-label {
    font-size: 12px;
    color: #666;
    font-weight: 500;
}

.popup-field-value {
    font-size: 14px;
    color: #333;
}

.popup-close {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 1;
    width: 24px;
    height: 24px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 18px;
    color: #666;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    pointer-events: auto;
}

.popup-close:hover {
    background: #f0f0f0;
    color: #000;
}

/* Індикатор завантаження */
.loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 24px;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 3000;
    text-align: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #1976d2;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 12px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading p {
    margin: 0;
    color: #666;
    font-size: 14px;
}

/* Повідомлення */
.error-message,
.success-message {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 3000;
    max-width: 90%;
    text-align: center;
    font-size: 14px;
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

.error-message {
    background: #ffebee;
    color: #c62828;
    border: 1px solid #ef5350;
}

.success-message {
    background: #e8f5e9;
    color: #2e7d32;
    border: 1px solid #66bb6a;
}

/* Адаптивність для мобільних */
@media (max-width: 768px) {
    .legend {
        max-width: calc(100vw - 20px);
        max-height: calc(100vh - 20px);
    }
    
    .legend.collapsed .legend-content {
        display: none;
    }
    
    .popup {
        max-width: calc(100vw - 40px);
    }
    
    .gps-btn {
        width: 44px;
        height: 44px;
    }
}

/* Маркер GPS */
.gps-marker {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #1976d2;
    border: 3px solid white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.gps-accuracy-circle {
    fill: rgba(25, 118, 210, 0.2);
    stroke: rgba(25, 118, 210, 0.5);
    stroke-width: 2;
}



