/**
 * Carbon Sink Tracker - Styles
 * Production-ready CSS with optimizations
 * Version: 1.0.0
 */

/* ==========================================================================
   Base Styles & Reset
   ========================================================================== */

html,
body,
#map {
    height: 100%;
    margin: 0;
    padding: 0;
}

* {
    box-sizing: border-box;
}

body {
    font-family: 'Quicksand', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: #333;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
}

/* ==========================================================================
   Layout - Control Panel
   ========================================================================== */

#controls {
    position: absolute;
    top: 0;
    left: 0;
    width: 300px !important;
    height: 100%;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 20px 15px 20px 15px;
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

/* Custom scrollbar for controls */
#controls::-webkit-scrollbar {
    width: 6px;
}

#controls::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
}

#controls::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

#controls::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

#header-container {
    display: flex;
    align-items: center;
    justify-content: center;
}

#controls h2 {
    margin: 0;
    font-size: 1.4em;
    white-space: nowrap;
    color: #2f5577;
}

#logo-link {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: inherit;
}
#logo-link:hover {
    opacity: 0.9;
}
#logo {
    width: 60px;
    height: auto;
    opacity: 0.9;
}

/* ==========================================================================
   Layout - Map
   ========================================================================== */

#map {
    margin-left: 300px;
    margin-right: 0;
    transition: margin-right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: margin-right;
}

#map.panel-open {
    margin-right: 100%;
}

/* Improve Leaflet performance */
.leaflet-container {
    font-family: inherit;
}

.leaflet-tile {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
}

/* Leaflet Controls - Ensure proper z-index */
.leaflet-control-zoom,
.leaflet-draw,
.leaflet-draw-toolbar,
.leaflet-control-layers {
    z-index: 800 !important;
}

.leaflet-top,
.leaflet-bottom {
    z-index: 800 !important;
}

/* Leaflet Draw Controls - Ensure visibility */
.leaflet-draw-toolbar {
    display: block !important;
}

.leaflet-draw-section {
    display: block !important;
}

.leaflet-draw-toolbar a {
    display: block !important;
    width: 30px !important;
    height: 30px !important;
    background-color: white !important;
    border: 2px solid rgba(0,0,0,0.2) !important;
}

/* ==========================================================================
   Form Elements
   ========================================================================== */

label {
    font-size: 14px;
    font-weight: 500;
    display: flex;
    flex-direction: column;
    margin-bottom: 2px;
    color: #333;
}

input,
select,
button {
    font-family: inherit;
    font-size: inherit;
}

/* ==========================================================================
   Legend
   ========================================================================== */

.info.legend {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.4;
    max-width: 500px;
    max-height: 520px;
    overflow-y: auto;
    overflow-x: hidden;
    border: 1px solid rgba(0, 0, 0, 0.1);
    z-index: 500 !important;
}

/* Minimized legend state */
.info.legend.minimized {
    max-height: 40px;
    overflow: hidden;
}

.info.legend.minimized .legend-groups-container {
    display: none;
}

.legend-header-row {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
}

.legend-header-row h4 {
    margin: 0;
}

.legend-main-arrow {
    font-size: 10px;
    color: #666;
    transition: transform 0.2s ease;
    display: inline-block;
    width: 12px;
    cursor: pointer;
}

.legend-groups-container {
    display: grid;
    grid-template-columns: 1fr;
    gap: 15px;
    margin-top: 10px;
}

/* Legend group dropdown styles */
.legend-group {
    margin-bottom: 0;
}

.legend-group-header {
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 0;
    transition: color 0.2s ease;
}

.legend-group-header.active {
    background: rgba(47, 85, 119, 0.08);
    border-radius: 6px;
    padding: 4px 6px;
}

.legend-category-toggle {
    display: inline-flex;
    align-items: center;
    cursor: inherit;
}

.legend-group-header:hover {
    color: #3f51b5;
}

.legend-arrow {
    font-size: 10px;
    color: #666;
    transition: transform 0.2s ease;
    display: inline-block;
    width: 12px;
    cursor: pointer;
}

.legend-group-content {
    margin-top: 4px;
    transition: all 0.2s ease;
}

.legend-subcategory {
    margin-left: 15px;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
}

.legend-subcategory:hover {
    background: rgba(0, 0, 0, 0.04);
}

.legend-subcategory.active {
    background: rgba(47, 85, 119, 0.12);
}

#yearSliderContainer {
    background: rgba(238, 247, 255, 0.9);
    padding: 8px 12px;
    border-radius: 8px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    gap: 10px;
}

#yearSlider {
    width: 100%;
}

#district-checkboxes {
    display: grid;
    /* Auto-fit prevents forcing a too-wide 2-col layout and avoids horizontal scrolling */
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 7px 12px;
    margin-top: 6px;
    overflow-y: auto;
    overflow-x: hidden;
}

/* ==========================================================================
   Buttons
   ========================================================================== */

button {
    padding: 10px 20px;
    font-size: 16px;
    font-weight: 500;
    min-width: 100px;
    border-radius: 6px;
    background-color: #3f51b5;
    color: white;
    border: none;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Default button hover - only applies to buttons without specific classes */
button:hover:not(:disabled):not(.load-btn):not(.clear-btn):not(.download-btn):not(.play-btn):not(.init-btn):not(.close-btn):not(.chart-icon):not(.graph-toggle):not(.state-graph-toggle):not(.district-graph-toggle):not(.download-graph-btn) {
    background-color: #2c3e9d;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

button:active:not(:disabled):not(.load-btn):not(.clear-btn):not(.download-btn):not(.play-btn):not(.init-btn) {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}

button:focus-visible {
    outline: 2px solid #3f51b5;
    outline-offset: 2px;
}

/* Button-specific styles */
.load-btn {
    width: 49%;
    background-color: #2f5577;
    color: #fff;
    border: 1px solid #2f5577;
}

.load-btn:hover:not(:disabled) {
    background-color: #274764;
    border-color: #274764;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.load-btn:active:not(:disabled) {
    background-color: #1e384f;
    border-color: #1e384f;
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.clear-btn {
    width: 49%;
    background-color: transparent;
    color: #c34444;
    border: 1px solid #c34444;
}

.clear-btn:hover:not(:disabled) {
    background-color: #fff0f0;
    border-color: #a03333;
    color: #a03333;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.clear-btn:active:not(:disabled) {
    background-color: #ffe2e2;
    border-color: #8a2828;
    color: #8a2828;
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.download-btn {
    width: 100%;
    background-color: #fffcf8;
    color: #489493;
    border: 1px solid #489493;
}

.download-btn:hover:not(:disabled) {
    background-color: #f4f9f9;
    border-color: #357372;
    color: #357372;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.download-btn:active:not(:disabled) {
    background-color: #e6f1f1;
    border-color: #285a59;
    color: #285a59;
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}


/* Animation Controls */
.animation-controls {
    margin-top: 20px;
    padding: 15px;
    background-color: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.animation-controls h3 {
    margin: 0 0 15px 0;
    font-size: 1.1em;
    color: #495057;
}

.animation-row {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin-bottom: 15px;
}

.play-btn {
    background-color: #28a745;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    min-width: 100px;
}

.play-btn:hover:not(:disabled) {
    background-color: #218838;
}

.play-btn:disabled {
    background-color: #6c757d;
    cursor: not-allowed;
    opacity: 0.6;
}

.play-btn.playing {
    background-color: #dc3545;
}

.play-btn.playing:hover {
    background-color: #c82333;
}

.year-slider-container {
    margin-top: 10px;
}

.year-slider-container input[type="range"] {
    width: 100%;
}

.init-btn {
    background-color: #17a2b8;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s;
}

.init-btn:hover {
    background-color: #138496;
}

.animation-status {
    font-size: 12px;
    color: #666;
    font-style: italic;
}

/* --- Right panel (charts/graphs slider) --- */
.right-panel {
    position: fixed;
    top: 0;
    right: -100%;
    width: calc(100% - 300px);
    /* max-width: 600px; */
    height: 100%;
    background: #fff;
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.2);
    z-index: 2000;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.right-panel.open {
    right: 0;
}

.panel-header {
    background-color: #2f5577;
    color: white;
    padding: 15px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.panel-header h3 {
    margin: 0;
    font-size: 1.3em;
}

.close-btn {
    background: none;
    border: none;
    color: white;
    font-size: 30px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
    min-width: unset;
}

.close-btn:hover {
    transform: scale(1.2);
    background: none;
}

.panel-content {
    flex: 1;
    overflow: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

/* Graph Toggles (State / District tabs) */
.graph-toggles {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
    border-bottom: 2px solid #e0e0e0;
}

.graph-toggle {
    background: none;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    font-size: 16px;
    color: #666;
    border-bottom: 3px solid transparent;
    transition: all 0.3s;
    min-width: auto;
    border-radius: 0;
}

.graph-toggle:hover {
    color: #2f5577;
    background: rgba(47, 85, 119, 0.05);
    transform: none;
    box-shadow: none;
}

.graph-toggle.active {
    color: #2f5577;
    border-bottom-color: #2f5577;
    font-weight: 600;
}

/* Graph Sections */
.graph-section {
    display: none;
    flex: 1;
    overflow: hidden;
    flex-direction: column;
}

.graph-section.active {
    display: flex;
}

/* Sub-toggles (Forest / Wetland / Total) */
.state-graph-toggles,
.district-graph-toggles {
    display: flex;
    gap: 8px;
    margin: 15px;
}

.state-graph-toggle,
.district-graph-toggle {
    flex: 1;
    background-color: #f0f0f0;
    border: none;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 14px;
    color: #555;
    border-radius: 4px;
    transition: all 0.3s;
    min-width: auto;
}

.state-graph-toggle:hover,
.district-graph-toggle:hover {
    background-color: #e0e0e0;
    transform: none;
    box-shadow: none;
}

.state-graph-toggle.active,
.district-graph-toggle.active {
    background-color: #2f5577;
    color: white;
    font-weight: 600;
}

/* Graphs Container */
.graphs-container {
    position: relative;
    flex: 1;
    overflow: hidden;
    display: flex;
    max-height: calc(100vh - 140px);
    align-items: flex-start;
}

.graph-chart-container {
    display: none;
    flex-direction: column;
    width: 100%;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
}

.graph-chart-container.active {
    display: flex;
}

/* Chart Header with Download Button */
.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.chart-header h4 {
    margin: 0;
    font-size: 1.1em;
    color: #333;
}

.download-graph-btn {
    background: none;
    border: 1px solid #ccc;
    padding: 5px 10px;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
    transition: all 0.2s;
    min-width: auto;
}

.download-graph-btn:hover {
    background-color: #f0f0f0;
    border-color: #999;
    transform: none;
    box-shadow: none;
}

#chartCanvas,
canvas[id*="ChartCanvas"] {
    width: 100% !important;
    height: 100% !important;
    max-height: 100%;
}

.graph-chart-container canvas {
    flex: 1;
    min-height: 0;
    max-height: 100%;
}

/* Chart Icon Button */
.chart-icon {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 0;              /* remove oval */
    background-color: transparent; /* no background */
    color: #2f5577;
    border: none;
    font-size:  24px;
    cursor: pointer;
    box-shadow: none;              /* remove shadow */
    z-index: 900;
    transition: transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: auto;
}

.chart-icon:hover {
    background-color: transparent; /* keep transparent on hover */
    transform: scale(1.05);
    box-shadow: none;
}

/* --- UI Polish: Consistent control sizing and spacing --- */
/* Uniform border radius and heights for controls */
#districtDropdownBtn,
.load-btn,
.clear-btn,
.download-btn {
    height: 44px;
    border-radius: 10px;
}

/* Filter dropdown styling to match district dropdown */
#lulcFilter {
  background: #fff;
  color: #222;
  height: 104%;
  border: 1px solid #bbb;
  border-radius: 4px;
  padding: 7px 30px 7px 14px;
  width: 100%;
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  position: relative;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: none;
}

/* Custom arrow for filter dropdown */
#lulcFilter-wrapper {
  position: relative;
  display: block;
  width: 100%;
  z-index: 5;
}

#lulcFilter-wrapper::after {
  content: "▼";
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  color: #666;
  pointer-events: none;
}

/* Legend Color Box */
.legend-color-box {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 2px;
  margin-right: 6px;
  vertical-align: middle;
}

/* Multiselect Wrapper */
.multiselect-wrapper {
  position: relative;
  z-index: 10;
}

/* District Dropdown Button Styles */
#districtDropdownBtn {
  background: #fff;
  color: #222;
  border: 1px solid #bbb;
  border-radius: 4px;
  padding: 7px 30px 7px 14px;
  width: 100%;
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  position: relative;
  transition: all 0.2s ease;
  /* Prevent the button from creating horizontal scroll when text is long */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#districtDropdownBtn:hover {
  background-color: #f9f9f9;
  border-color: #999;
}
#districtDropdownBtn::after {
  content: "▼";
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  color: #666;
  transition: transform 0.2s ease;
  pointer-events: none;
}

#districtDropdownBtn.dropdown-open::after {
  transform: translateY(-50%) rotate(180deg);
}
#districtDropdownBtn.selected {
  background: #e4f3fd;
  border-color: #2090c0;
  color: #00334a;
}
.multiselect-dropdown {
  display: none;
  position: absolute;
  left: 0;
  z-index: 1100;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  background: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 6px 18px rgba(0,0,0,0.20), 0 1.5px 6px rgba(0,0,0,0.09);
  padding: 10px 8px 10px 10px;
  border-radius: 10px;
  max-height: 300px;
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 15px;
}

/* District checkbox rows: allow wrapping, avoid overflow */
#district-checkboxes > div {
  min-width: 0;
  align-items: flex-start !important; /* override inline alignItems=center */
}

#district-checkboxes > div > label {
  display: block;
  flex: 1;
  min-width: 0;
  white-space: normal;
  overflow-wrap: anywhere;
}

#district-checkboxes input[type="checkbox"] {
  flex: 0 0 auto;
  margin-top: 2px;
}
#districtDropdown .district-checkbox {
  display: block;
  margin-bottom: 5px;
  font-size: 14px;
  cursor: pointer;
}

/* ==========================================================================
   Z-Index Hierarchy (Fixing Layer Stacking)
   ========================================================================== */

/*
  Z-Index Stack (lowest to highest):
  - Map base: 0
  - Legend: 500
  - Leaflet controls: 800
  - Chart icon: 900
  - Controls panel: 1000
  - Multiselect dropdown: 1100
  - Right panel: 2000
*/

.leaflet-pane {
    z-index: 400 !important;
}

.leaflet-map-pane {
    z-index: 0 !important;
}

/* ==========================================================================
   Utility Classes
   ========================================================================== */

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

/* ==========================================================================
   Responsive Design
   ========================================================================== */

/* Tablet and below */
@media (max-width: 768px) {
    #controls {
        width: 100%;
        height: auto;
        max-height: 50vh;
        position: relative;
        z-index: 1000;
    }
    
    #map {
        margin-left: 0;
        height: 50vh;
    }
    
    .leaflet-left {
        left: 10px !important;
    }
    
    .right-panel {
        max-width: 100%;
    }
    
    .legend-groups-container {
        grid-template-columns: 1fr;
    }
    
    #district-checkboxes {
        grid-template-columns: 1fr;
    }
}

/* Mobile */
@media (max-width: 480px) {
    #controls h2 {
        font-size: 1.2em;
    }
    
    #logo {
        width: 50px;
    }
    
    button {
        font-size: 14px;
        padding: 8px 16px;
    }
}

/* Desktop - ensure proper sizing */
@media (min-width: 769px) {
    #controls {
        width: 300px;
    }
    
    #map {
        margin-left: 300px;
    }
}

/* ==========================================================================
   Performance Optimizations
   ========================================================================== */

/* Enable GPU acceleration for animations */
.right-panel {
    will-change: auto;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* Optimize font rendering */
body {
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Reduce repaint/reflow - removed global transform as it causes z-index issues */

/* ==========================================================================
   Accessibility
   ========================================================================== */

/* Focus indicators for keyboard navigation */
a:focus,
button:focus,
input:focus,
select:focus {
    outline: 2px solid #3f51b5;
    outline-offset: 2px;
}

/* Screen reader only class */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    #controls,
    .chart-icon,
    .right-panel,
    button {
        display: none !important;
    }
    
    #map {
        margin-left: 0 !important;
        width: 100% !important;
        height: 100vh !important;
    }
}
