/* ============================================================================
   CSS VARIABLES
   ============================================================================ */
:root {
    --primary: #2c3e50;
    --secondary: #3498db;
    --accent: #e74c3c;
    --success: #27ae60;
    --border: #dfe4ea;
    --bg-light: #f8f9fa;
    --text: #2c3e50;
    --text-light: #6c757d;
}

/* ============================================================================
   RESET & BASE STYLES
   ============================================================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--text);
    background: white;
    overflow: hidden;
}

/* ============================================================================
   LAYOUT
   ============================================================================ */
#app {
    display: flex;
    height: 100vh;
}

#sidebar {
    width: 320px;
    background: white;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    transition: transform 0.3s ease;
}

#header {
    padding: 20px;
    border-bottom: 2px solid var(--secondary);
}

#header h1 {
    font-size: 24px;
    color: var(--primary);
    margin: 0;
}

#header .subtitle {
    font-size: 12px;
    color: var(--text-light);
    margin-top: 4px;
}

#controls {
    padding: 20px;
    flex: 1;
}

/* ============================================================================
   MOBILE MENU TOGGLE BUTTON
   ============================================================================ */
#menu-toggle {
    display: none; /* Hidden on desktop */
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 2000;
    background: white;
    border: 2px solid var(--primary);
    border-radius: 8px;
    width: 50px;
    height: 50px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
}

#menu-toggle span {
    display: block;
    width: 24px;
    height: 3px;
    background: var(--primary);
    border-radius: 2px;
    transition: all 0.3s ease;
}

#menu-toggle.active span:nth-child(1) {
    transform: rotate(45deg) translate(6px, 6px);
}

#menu-toggle.active span:nth-child(2) {
    opacity: 0;
}

#menu-toggle.active span:nth-child(3) {
    transform: rotate(-45deg) translate(6px, -6px);
}

/* ============================================================================
   FORM CONTROLS
   ============================================================================ */
.control-group {
    margin-bottom: 20px;
}

.control-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

input[type="text"], select {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 14px;
    background: white;
}

input[type="text"]:focus, select:focus {
    outline: none;
    border-color: var(--secondary);
}

.help-text {
    font-size: 11px;
    color: var(--text-light);
    margin-top: 4px;
}

.help-text a {
    color: var(--secondary);
    text-decoration: none;
}

.help-text a:hover {
    text-decoration: underline;
}

.help-text .tooltip {
    cursor: help;
    border-bottom: 1px dotted var(--text-light);
}

.help-text code {
    background: var(--bg-light);
    padding: 1px 3px;
    border-radius: 2px;
    font-family: monospace;
    font-size: 10px;
}

/* ============================================================================
   BUTTONS
   ============================================================================ */
button {
    padding: 10px 16px;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 13px;
    background: white;
    cursor: pointer;
    width: 100%;
    margin-bottom: 8px;
    min-height: 44px; /* Mobile-friendly touch target */
}

button:hover:not(:disabled) {
    background: var(--bg-light);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

button.primary {
    background: var(--secondary);
    color: white;
    border-color: var(--secondary);
}

button.primary:hover:not(:disabled) {
    background: #2980b9;
}

/* ============================================================================
   MODE TOGGLE STYLES
   ============================================================================ */
.mode-toggle {
    background: var(--bg-light);
    padding: 12px;
    border-radius: 4px;
    margin-bottom: 20px;
}

.mode-toggle-label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 8px;
}

.toggle-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px;
}

.toggle-button {
    padding: 12px 8px; /* Increased for better touch targets */
    border: 1px solid var(--border);
    background: white;
    cursor: pointer;
    font-size: 12px;
    text-align: center;
    transition: all 0.2s;
    position: relative;
    min-height: 44px; /* Mobile-friendly touch target */
}

.toggle-button[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.9);
    color: white;
    font-size: 11px;
    line-height: 1.4;
    border-radius: 4px;
    white-space: normal;
    width: 200px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    margin-bottom: 8px;
    z-index: 1000;
    text-align: left;
}

.toggle-button[data-tooltip]:hover::after {
    opacity: 1;
}

.toggle-button:nth-child(1) {
    border-radius: 4px 0 0 0;
}

.toggle-button:nth-child(2) {
    border-radius: 0 4px 0 0;
}

.toggle-button:nth-child(3) {
    border-radius: 0 0 0 4px;
}

.toggle-button:nth-child(4) {
    border-radius: 0 0 4px 0;
}

.toggle-button.active {
    background: var(--secondary);
    color: white;
    border-color: var(--secondary);
}

.toggle-button:not(.active):hover {
    background: var(--bg-light);
}

/* ============================================================================
   CHECKBOX STYLES
   ============================================================================ */
.checkbox-group {
    display: flex;
    align-items: center;
    padding: 12px; /* Increased for better touch targets */
    background: var(--bg-light);
    border-radius: 4px;
    margin-bottom: 15px;
    min-height: 44px; /* Mobile-friendly touch target */
}

.checkbox-group input[type="checkbox"] {
    margin-right: 8px;
    cursor: pointer;
    width: 20px; /* Larger checkbox for mobile */
    height: 20px;
}

.checkbox-group label {
    font-size: 13px;
    cursor: pointer;
    margin: 0;
}

/* ============================================================================
   SLIDER STYLES
   ============================================================================ */
.slider-container {
    margin: 15px 0;
    position: relative;
}

.slider-label {
    font-size: 12px;
    margin-bottom: 5px;
    display: block;
    cursor: help;
}

.slider-track {
    position: relative;
    height: 8px; /* Thicker for mobile */
    background: linear-gradient(to right,
        var(--secondary) 0%,
        var(--success) 50%,
        var(--accent) 100%);
    border-radius: 4px;
    margin: 10px 0;
}

.slider-cursor {
    position: absolute;
    top: -8px;
    width: 24px; /* Larger for mobile touch */
    height: 24px;
    background: white;
    border: 2px solid var(--accent);
    border-radius: 50%;
    cursor: grab;
    transform: translateX(-50%);
    transition: box-shadow 0.2s;
}

.slider-cursor:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.slider-cursor:active {
    cursor: grabbing;
    box-shadow: 0 2px 12px rgba(0,0,0,0.3);
}

.slider-labels {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--text-light);
}

.slider-value {
    text-align: center;
    margin-top: 8px;
    font-size: 12px;
    padding: 2px 6px;
    background: var(--bg-light);
    border-radius: 3px;
    display: inline-block;
}

/* ============================================================================
   MAP CONTAINER
   ============================================================================ */
#map-container {
    flex: 1;
    position: relative;
}

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

#status {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(44, 62, 80, 0.95);
    color: white;
    padding: 12px 20px;
    display: none;
}

#status.active {
    display: block;
}

#progress-bar {
    height: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    margin-top: 8px;
    overflow: hidden;
}

#progress-fill {
    height: 100%;
    background: var(--success);
    width: 0%;
    transition: width 0.3s;
}

/* ============================================================================
   DROPDOWN STYLES
   ============================================================================ */
.dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
    display: none;
    margin-top: 4px;
}

.dropdown.active {
    display: block;
}

.dropdown-item {
    padding: 12px; /* Increased for mobile touch */
    font-size: 13px;
    cursor: pointer;
    border-bottom: 1px solid var(--bg-light);
    min-height: 44px; /* Mobile-friendly touch target */
    display: flex;
    align-items: center;
}

.dropdown-item:hover,
.dropdown-item.selected {
    background: var(--bg-light);
}

.dropdown-item:last-child {
    border-bottom: none;
}

.area-input-container,
.query-input-container {
    position: relative;
}

/* ============================================================================
   MESSAGES
   ============================================================================ */
.message {
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 4px;
    font-size: 12px;
}

.error-message {
    background: #f8d7da;
    border-left: 3px solid var(--accent);
}

.info-message {
    background: #d1ecf1;
    border-left: 3px solid #0c5460;
}

.warning-message {
    background: #fff3cd;
    border-left: 3px solid #ffc107;
}

/* ============================================================================
   STATS & CREDITS
   ============================================================================ */
.stats {
    background: var(--bg-light);
    padding: 10px;
    border-radius: 4px;
    font-size: 12px;
    margin-top: 10px;
}

.stats div {
    margin: 4px 0;
}

.credits {
    font-size: 11px;
    color: var(--text-light);
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
}

.credits a {
    color: var(--secondary);
    text-decoration: none;
}

.credits a:hover {
    text-decoration: underline;
}

/* ============================================================================
   ADVANCED CONTROLS
   ============================================================================ */
.advanced-controls {
    background: var(--bg-light);
    padding: 12px;
    border-radius: 4px;
    margin-top: 15px;
}

/* Voronoi controls */
.voronoi-controls {
    display: none;
    margin-top: 15px;
}

.voronoi-controls.active {
    display: block;
}

.coalesce-info {
    font-size: 11px;
    color: var(--text-light);
    text-align: center;
    margin-top: 5px;
}

/* Controls footer */
.controls-footer {
    text-align: center;
    padding: 20px 10px;
    margin-top: 20px;
    border-top: 1px solid var(--border);
    font-size: 13px;
    color: var(--text-light);
    line-height: 1.6;
}

.controls-footer a {
    color: var(--primary);
    text-decoration: none;
}

.controls-footer a:hover {
    text-decoration: underline;
}

/* Mode-specific controls */
.mode-specific-controls {
    display: none;
}

.mode-specific-controls.active {
    display: block;
}

/* ============================================================================
   MAP ATTRIBUTION
   ============================================================================ */
.maplibregl-ctrl-attrib {
    background: rgba(255, 255, 255, 0.3) !important;
}

/* ============================================================================
   TOOLTIP
   ============================================================================ */
.tooltip {
    position: relative;
    cursor: help;
}

.tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 5px 10px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    font-size: 11px;
    border-radius: 3px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    margin-bottom: 5px;
    z-index: 1000;
}

.tooltip:hover::after {
    opacity: 1;
}

/* ============================================================================
   COLOR PALETTE SELECTOR
   ============================================================================ */
.palette-selector-container {
    position: relative;
}

.palette-selected {
    display: flex;
    height: 36px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    background: white;
}

.palette-selected:hover {
    border-color: var(--secondary);
}

.palette-gradient {
    flex: 1;
    border-radius: 2px;
}

.palette-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border: 1px solid var(--border);
    border-radius: 4px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    margin-top: 4px;
    z-index: 1000;
    display: none;
}

.palette-dropdown.active {
    display: block;
}

.palette-option {
    display: flex;
    height: 36px;
    padding: 4px 8px;
    cursor: pointer;
}

.palette-option:hover {
    background: var(--bg-light);
}

.palette-option .palette-gradient {
    flex: 1;
    border-radius: 2px;
    border: 1px solid var(--border);
}

/* ============================================================================
   MOBILE RESPONSIVE STYLES
   ============================================================================ */

/* Tablets and smaller laptops */
@media (max-width: 768px) {
    #sidebar {
        width: 280px;
    }

    #header h1 {
        font-size: 20px;
    }
}

/* Mobile phones - Portrait and landscape */
@media (max-width: 600px) {
    #app {
        flex-direction: column;
    }

    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 85%;
        max-width: 320px;
        z-index: 1500;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.2);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    #menu-toggle {
        display: flex; /* Show hamburger menu on mobile */
    }

    #map-container {
        width: 100%;
        height: 100vh;
    }

    /* Make toggle buttons single column on very narrow screens */
    .toggle-buttons {
        grid-template-columns: 1fr 1fr;
        gap: 4px;
    }

    .toggle-button {
        font-size: 11px;
        padding: 10px 4px;
    }

    /* Adjust button radius for grid */
    .toggle-button:nth-child(1) {
        border-radius: 4px 0 0 0;
    }

    .toggle-button:nth-child(2) {
        border-radius: 0 4px 0 0;
    }

    .toggle-button:nth-child(3) {
        border-radius: 0 0 0 4px;
    }

    .toggle-button:nth-child(4) {
        border-radius: 0 0 4px 0;
    }

    /* Adjust header for mobile */
    #header {
        padding: 15px;
    }

    #header h1 {
        font-size: 18px;
    }

    #header .subtitle {
        font-size: 11px;
    }

    /* Adjust controls padding */
    #controls {
        padding: 15px;
    }

    /* Make tooltips work on tap for mobile */
    .toggle-button[data-tooltip]:active::after {
        opacity: 1;
    }

    .tooltip:active::after {
        opacity: 1;
    }

    /* Increase font sizes for better readability */
    .control-group label {
        font-size: 14px;
    }

    input[type="text"], select {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 12px;
    }

    button {
        font-size: 14px;
        padding: 12px 16px;
    }

    /* Footer adjustments */
    .controls-footer {
        font-size: 12px;
        padding: 15px 10px;
    }
}

/* Very narrow screens */
@media (max-width: 360px) {
    #sidebar {
        width: 90%;
    }

    .toggle-buttons {
        grid-template-columns: 1fr;
        gap: 2px;
    }

    .toggle-button:nth-child(1) {
        border-radius: 4px 4px 0 0;
    }

    .toggle-button:nth-child(2) {
        border-radius: 0;
    }

    .toggle-button:nth-child(3) {
        border-radius: 0;
    }

    .toggle-button:nth-child(4) {
        border-radius: 0 0 4px 4px;
    }
}

/* Landscape mode on phones */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    #sidebar {
        width: 40%;
        min-width: 280px;
    }

    #controls {
        padding: 10px 15px;
    }

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

    .mode-toggle {
        padding: 8px;
        margin-bottom: 12px;
    }
}

/* Backdrop overlay for mobile menu */
#mobile-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1400;
    opacity: 0;
    transition: opacity 0.3s ease;
}

@media (max-width: 600px) {
    #mobile-backdrop.active {
        display: block;
        opacity: 1;
    }
}
