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

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 13px; }

#map { position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 1; }

#controls {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    background: rgba(255,255,255,0.95);
    border-radius: 8px;
    padding: 16px;
    width: 320px;
    top: 5px;
    right: 5px;
    max-height: calc(100vh - 10px);
    overflow-y: auto;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
    font-size: 13px;
}

#controls h3 {
    margin-bottom: 12px;
    font-size: 16px;
    color: #F26522;
    border-bottom: 2px solid #F26522;
    padding-bottom: 6px;
}

.view-btn {
    padding: 3px 10px;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
}

.view-btn:hover { background: #F26522; color: #fff; border-color: #F26522; }

.control-section {
    border-bottom: 1px solid #eee;
    margin-bottom: 4px;
}

.section-header {
    font-size: 12px;
    font-weight: 600;
    color: #00549A;
    padding: 6px 0;
    cursor: pointer;
    user-select: none;
}

.section-header:hover { color: #F26522; }

.section-body { padding-bottom: 4px; }

.section-body.collapsed { display: none; }

.control-group {
    margin-bottom: 8px;
}

.control-group > label {
    display: inline-block;
    font-weight: 600;
    margin-bottom: 4px;
    color: #333;
}

.toggle-btn-group {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

.toggle-btn {
    flex: 1;
    padding: 6px 8px;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
    min-width: 0;
}

.toggle-btn.small { font-size: 10px; padding: 4px 6px; }

.toggle-btn:hover { border-color: #F26522; }

.toggle-btn.active {
    background: #F26522;
    color: #fff;
    border-color: #F26522;
}

select {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 13px;
}

#search-input {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 13px;
}

#search-results {
    max-height: 250px;
    overflow-y: auto;
    margin-top: 4px;
    border: 1px solid #eee;
    border-radius: 4px;
}

#search-results:empty { border: none; }

.search-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 8px;
    background: #f5f5f5;
    border-bottom: 1px solid #eee;
    position: sticky;
    top: 0;
    z-index: 1;
}

.search-actions .search-count {
    font-size: 12px;
    color: #666;
    margin-right: auto;
}

.search-actions a {
    font-size: 12px;
    color: #3498db;
    text-decoration: none;
    white-space: nowrap;
}

.search-actions a:hover { text-decoration: underline; }

.search-item {
    padding: 3px 8px;
    border-radius: 3px;
    font-size: 12px;
}

.search-item:hover { background: #f0f0f0; }

.search-item label {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-weight: normal;
    gap: 4px;
}

.search-item input[type=checkbox] {
    margin: 0;
    flex-shrink: 0;
}

.search-name { flex-shrink: 0; }

.search-subtitle {
    font-size: 12px;
    color: #999;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.search-item .type-badge {
    font-size: 12px;
    padding: 1px 4px;
    border-radius: 3px;
    color: #fff;
    flex-shrink: 0;
}

.type-badge.tinh { background: #3498db; }
.type-badge.huyen { background: #2ecc71; }
.type-badge.xa { background: #e67e22; }
.type-badge.buucuc { background: #F26522; }

#info-panel {
    margin-top: 12px;
    padding: 10px;
    background: #f9f9f9;
    border-radius: 6px;
    border-left: 3px solid #F26522;
}

#info-panel h4 { margin-bottom: 6px; color: #F26522; }

#info-panel table { width: 100%; font-size: 12px; }
#info-panel td { padding: 2px 0; vertical-align: top; }
#info-panel td:first-child { font-weight: 600; color: #666; width: 40%; }

.close-btn {
    margin-top: 8px;
    padding: 4px 12px;
    background: #eee;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

.close-btn:hover { background: #ddd; }

/* ============================================================
   Legends container - bottom left, stacked vertically
   ============================================================ */

#map-legends {
    position: absolute;
    bottom: 20px;
    left: 10px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 280px;
}

#map-legends > div {
    background: rgba(255,255,255,0.95);
    border-radius: 8px;
    padding: 10px 14px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
    font-size: 13px;
}

#legend {
    position: static;
}

#pin-legend-static {
    display: none;
}

.legend-title {
    font-weight: 700;
    margin-bottom: 6px;
    font-size: 12px;
    color: #333;
}

.legend-scale {
    display: flex;
    height: 16px;
    border-radius: 4px;
    overflow: hidden;
    margin: 6px 0;
    border: 1px solid #ddd;
}

.legend-scale div {
    position: relative;
}

.legend-labels {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #666;
    margin-top: 2px;
}

.legend-stats {
    margin-top: 8px;
    font-size: 12px;
    color: #666;
    border-top: 1px solid #eee;
    padding-top: 6px;
}

.legend-stats span { font-weight: 600; color: #333; }

/* Pin legend items */
.pin-legend-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 3px 0;
    font-size: 12px;
}

.pin-legend-icon {
    display: inline-block;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
    vertical-align: middle;
    flex-shrink: 0;
}

.pin-legend-mode {
    margin-top: 6px;
    border-top: 1px solid #eee;
    padding-top: 6px;
    font-size: 12px;
}

.pin-legend-mode span { font-weight: 600; }

.pin-legend-mode a {
    text-decoration: none;
    color: #999;
    margin-left: 6px;
    font-size: 12px;
}

.pin-legend-mode a.active {
    font-weight: 700;
    color: #3498db;
}

.pin-legend-mode a:hover { text-decoration: underline; }

/* ============================================================
   Province filter
   ============================================================ */

.province-filter-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.province-filter-actions a {
    font-size: 12px;
    color: #3498db;
    text-decoration: none;
}

.province-filter-actions a:hover { text-decoration: underline; }

.filter-count {
    font-size: 12px;
    color: #999;
    margin-left: auto;
}

.province-search {
    width: 100%;
    padding: 4px 6px;
    border: 1px solid #ddd;
    border-radius: 3px;
    font-size: 12px;
    margin-bottom: 4px;
}

.province-list {
    max-height: 120px;
    overflow-y: auto;
    border: 1px solid #eee;
    border-radius: 4px;
    padding: 2px;
}

.province-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 4px;
    font-size: 12px;
    font-weight: normal;
    cursor: pointer;
    border-radius: 2px;
}

.province-item:hover { background: #f0f0f0; }

.province-item input[type=checkbox] {
    margin: 0;
    flex-shrink: 0;
}

/* ============================================================
   Pin shapes & labels
   ============================================================ */

.pin-shape {
    border: 2px solid #fff;
    box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

.pin-square { border-radius: 2px; }
.pin-diamond { border-radius: 2px; transform: rotate(45deg); }

.pin-hover-target {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.pin-hover-target:hover .pin-shape {
    transform: scale(1.4);
    box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

.pin-hover-target:hover .pin-diamond {
    transform: rotate(45deg) scale(1.4);
}

.pin-house-wrap svg {
    transition: transform 0.15s;
    filter: drop-shadow(0 1px 3px rgba(0,0,0,0.4));
}

.pin-house-wrap:hover svg {
    transform: scale(1.3);
    filter: drop-shadow(0 2px 6px rgba(0,0,0,0.5));
}

.pin-circle {
    border: 2px solid #fff;
    box-shadow: 0 1px 4px rgba(0,0,0,0.4);
    transition: transform 0.15s;
}

.pin-hover-target:hover .pin-circle {
    transform: scale(1.5);
    box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

.pin-label {
    background: none !important;
    border: none !important;
    box-shadow: none !important;
}

.pin-label span {
    font-size: 12px;
    font-weight: 600;
    color: #333;
    background: rgba(255,255,255,0.85);
    padding: 1px 3px;
    border-radius: 2px;
    white-space: nowrap;
    pointer-events: none;
}

/* ============================================================
   Loading
   ============================================================ */

#loading {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(255,255,255,0.9);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #eee;
    border-top-color: #F26522;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Leaflet popup override */
.leaflet-popup-content { font-size: 14px; line-height: 1.5; min-width: 200px; }
.leaflet-popup-content h4 { color: #F26522; margin-bottom: 6px; font-size: 15px; }
.leaflet-popup-content table td { padding: 2px 8px 2px 0; font-size: 13px; }
.leaflet-popup-content table td:first-child { font-weight: 600; color: #666; }

/* Leaflet layer control */
.leaflet-control-layers {
    border-radius: 8px !important;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15) !important;
}

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

/* ============================================================
   Mobile responsive
   ============================================================ */

/* Controls panel: collapsible on mobile */
#controls-toggle {
    display: none;
    position: absolute;
    top: 5px;
    right: 5px;
    z-index: 1001;
    background: rgba(255,255,255,0.95);
    border: none;
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 14px;
    font-weight: 700;
    color: #F26522;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
    cursor: pointer;
}

.mobile-only { display: none; }

@media (max-width: 768px) {
    .mobile-only { display: block !important; }
    #controls-toggle { display: block; }

    .leaflet-control-layers {
        position: relative;
        top: 0;
    }

    .leaflet-control-layers-toggle {
        width: 40px !important;
        height: 40px !important;
    }

    .leaflet-control-layers-list label {
        font-size: 14px !important;
        padding: 4px 0;
    }

    .leaflet-control-zoom a {
        width: 36px !important;
        height: 36px !important;
        line-height: 36px !important;
        font-size: 20px !important;
    }

    #controls {
        width: calc(100% - 20px);
        max-height: 60vh;
        top: auto;
        bottom: 10px;
        right: 10px;
        left: 10px;
        padding: 12px;
        transform: translateY(0);
        transition: transform 0.3s ease, opacity 0.3s ease;
    }

    #controls.collapsed {
        transform: translateY(calc(100% + 20px));
        opacity: 0;
        pointer-events: none;
    }

    #controls h3 { font-size: 14px; }

    .toggle-btn { padding: 8px 6px; font-size: 12px; }
    .toggle-btn.small { font-size: 12px; padding: 6px 4px; }

    select, #search-input { padding: 8px; font-size: 14px; }

    .province-list { max-height: 80px; }

    /* Hide entire floating legends on mobile */
    #map-legends {
        display: none !important;
    }

    #map-legends > div {
        padding: 8px 10px;
        font-size: 12px;
    }

    .legend-title { font-size: 12px; }

    /* Leaflet popup */
    .leaflet-popup-content { min-width: 160px; font-size: 13px; }
    .leaflet-popup-content h4 { font-size: 14px; }
    .leaflet-popup-content table td { font-size: 12px; }

    /* Pin labels smaller on mobile */
    .pin-label span { font-size: 8px; }

    /* Search results */
    #search-results { max-height: 150px; }

    /* View buttons */
    .view-btn { padding: 5px 8px; }
}

@media (max-width: 400px) {
    #controls {
        max-height: 50vh;
        padding: 10px;
    }

    .toggle-btn-group { flex-wrap: wrap; }
    .toggle-btn { min-width: 45%; }
}

/* ============================================================
   Planning Mode
   ============================================================ */

.planning-marker {
    display: flex;
    align-items: center;
    justify-content: center;
}

.planning-marker-icon {
    width: 20px;
    height: 20px;
    background: #e84393;
    border: 2px solid #fff;
    transform: rotate(45deg);
    box-shadow: 0 0 6px rgba(232, 67, 147, 0.6);
    animation: planning-pulse 2s ease-in-out infinite;
}

.planning-marker-selected .planning-marker-icon {
    box-shadow: 0 0 12px rgba(232, 67, 147, 0.9);
    border-color: #f1c40f;
}

@keyframes planning-pulse {
    0%, 100% { box-shadow: 0 0 6px rgba(232, 67, 147, 0.4); }
    50% { box-shadow: 0 0 14px rgba(232, 67, 147, 0.8); }
}

#planning-controls .toggle-btn.active {
    background: #e84393;
    color: #fff;
}

#planning-draft-list .draft-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 6px;
    margin: 2px 0;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    border: 1px solid transparent;
}

#planning-draft-list .draft-item:hover {
    background: #f0f0f0;
}

#planning-draft-list .draft-item.selected {
    background: #fce4ec;
    border-color: #e84393;
}

#planning-draft-list .draft-item .draft-color {
    width: 10px;
    height: 10px;
    transform: rotate(45deg);
    flex-shrink: 0;
}

#planning-draft-list .draft-item .draft-delete {
    margin-left: auto;
    color: #999;
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
}

#planning-draft-list .draft-item .draft-delete:hover {
    color: #F26522;
}

#planning-draft-info {
    font-size: 11px;
    padding: 6px;
    background: #fafafa;
    border-radius: 4px;
    border: 1px solid #eee;
}

.planning-popup-form input,
.planning-popup-form select,
.planning-popup-form textarea {
    width: 100%;
    padding: 4px 6px;
    margin: 2px 0 6px;
    border: 1px solid #ccc;
    border-radius: 3px;
    font-size: 12px;
    box-sizing: border-box;
}

.planning-popup-form label {
    font-size: 11px;
    font-weight: 600;
    color: #555;
}

.planning-popup-form button {
    padding: 4px 12px;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 12px;
    margin-right: 4px;
}

.planning-popup-form .btn-save {
    background: #e84393;
    color: #fff;
}

.planning-popup-form .btn-cancel {
    background: #eee;
    color: #333;
}

.map-crosshair {
    cursor: crosshair !important;
}

.map-crosshair .leaflet-interactive {
    cursor: crosshair !important;
}
