/* --------------------------------------------------
   0. GLOBAL VARIABLES & THEMES
-------------------------------------------------- */

:root {
  /* Typography */
  --font-main: 'Montserrat', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;

  /* Light theme colors (default) */
  --color-bg: #ffffff;
  --color-bg-alt: #ffffff;
  --color-panel: #CEE5F6;
  --color-header: #A9D2F0;
  --color-primary: #0597E0;
  --color-primary-soft: #03AEE2;
  --color-primary-muted: #84BFE8;
  --color-accent: #FCEE21;

  --color-text: #222222;
  --color-text-muted: #444444;
  --color-border: #A9D2F0;
  --color-border-soft: #dddddd;

  --shadow-soft: 0 2px 6px rgba(0, 0, 0, 0.08);
}

/* Dark theme overrides */
html[data-theme="dark"] {
  --color-bg: #111827;
  --color-bg-alt: #020617;
  --color-panel: #0b1220;
  --color-header: #0f172a;
  --color-primary: #03AEE2;
  --color-primary-soft: #84BFE8;
  --color-primary-muted: #0597E0;
  --color-accent: #FCEE21;

  --color-text: #e5e7eb;
  --color-text-muted: #cbd5f5;
  --color-border: #1f2937;
  --color-border-soft: #111827;

  --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.5);
}

/* --------------------------------------------------
   1. RESET & BASE
-------------------------------------------------- */

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font-main);
  background: var(--color-bg);
  color: var(--color-text);
  overflow: auto;
}

/* Typography scale */
h1, h2, h3, h4, p, span, label, button {
  font-family: var(--font-main);
}

h1 {
  font-weight: 700;
  font-size: 1.4rem;
}

h2 {
  font-weight: 600;
  font-size: 1.1rem;
}

h3 {
  font-weight: 600;
  font-size: 0.9rem;
}

h4 {
  font-weight: 600;
  font-size: 0.75rem;
}

p {
  font-weight: 300;
  font-size: 0.65rem;
}

span {
  font-weight: 400;
}

label {
  font-weight: 500;
  font-size: 0.6rem;
}

button {
  font-weight: 600;
  font-size: 0.7rem;
}

/* Info labels / values */
.info-label {
  font-weight: 600;
  font-size: 0.85rem;
}

.info-value {
  font-weight: 400;
  font-size: 0.85rem;
}

/* Map status & popup */
#map-status,
.popup-table {
  font-weight: 500;
  font-size: 0.8rem;
}

/* --------------------------------------------------
   2. LAYOUT
-------------------------------------------------- */

.grid-container {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 4px 340px;
  grid-template-rows: 100vh;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.grid-1 {
  height: 100%;
}

.grid-2 {
  height: 100%;
}

/* Map full area of its grid cell */
#map {
  width: 100%;
  height: 100%;
}

/* Resizable divider */
#divider {
  cursor: ew-resize;
  background-color: var(--color-border-soft);
  width: 4px;
  transition: background-color 0.2s;
}

#divider:hover {
  background-color: var(--color-border);
}

/* Sidebar column */
.column-menu {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  background-color: var(--color-bg-alt);
  color: var(--color-text);
  border-left: 3px solid var(--color-primary-muted);
}

/* --------------------------------------------------
   3. HEADER
-------------------------------------------------- */

#header-box {
  display: flex;
  align-items: center;
  padding: 10px;
  border-bottom: 2px solid var(--color-border-soft);
  background: var(--color-bg);
}

#logo {
  height: 80px;
  margin-right: 10px;
}

#header-box h2 {
  font-size: 1.4rem;
  font-weight: 600;
  margin: 0;
}

/* --------------------------------------------------
   4. LAYERS & LEGEND SECTIONS
-------------------------------------------------- */

#layers-section,
#legend-section {
  margin-top: 10px;
  padding: 5px 8px;
}

#legend-section {
  border-bottom: 1px solid var(--color-border-soft);
}

/* Shared header style */
#layers-header,
#legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--color-header);
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
}

#layers-header h3,
#legend-header h3 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--color-text);
}

/* Header toggle buttons ( + / - ) */
#layers-toggle,
#legend-toggle {
  background: var(--color-primary);
  color: #ffffff;
  border: none;
  width: 26px;
  height: 26px;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
}

#layers-toggle:hover,
#legend-toggle:hover {
  background: var(--color-primary-soft);
  transform: translateY(-1px);
}

/* Small spinner icon next to layer names */
.layer-spinner {
  width: 14px;
  height: 14px;
  vertical-align: middle;
  margin-left: 6px;
  display: none;
}

.layer-spinner.active {
  display: inline-block;
}

/* --------------------------------------------------
   5. LEAFLET LAYERS PANEL & MAP LABELS
-------------------------------------------------- */

.polygon-label {
  background: transparent;
  border: none;
  box-shadow: none;
  color: #000;
  font-size: 0.75rem;
  font-weight: 700;
  text-shadow: 1px 1px 2px #ffffff;
  padding: 0;
}

#leaflet-layers-panel {
  max-height: 250px;
  overflow-y: auto;
  padding-right: 6px;
}

/* Scrollbar */
#leaflet-layers-panel::-webkit-scrollbar {
  width: 6px;
}
#leaflet-layers-panel::-webkit-scrollbar-thumb {
  background: var(--color-primary-muted);
  border-radius: 4px;
}
#leaflet-layers-panel::-webkit-scrollbar-thumb:hover {
  background: var(--color-primary);
}

/* Remove default Leaflet panel chrome when inside our panel */
#leaflet-layers-panel .leaflet-control-layers {
  background: none;
  box-shadow: none;
  border: none;
}

#leaflet-layers-panel label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

/* Global Leaflet control styling */
.leaflet-control-layers {
  background-color: rgba(5, 151, 224, 0.95);
  color: #ffffff;
  font-family: var(--font-main);
  font-size: 0.7rem;
  padding: 8px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

.leaflet-control-layers input {
  accent-color: var(--color-accent);
}

.leaflet-control-layers label {
  display: block;
  margin: 4px 0;
  color: #f9fafb;
}

.leaflet-control-layers-toggle {
  background-color: #111827;
  border-radius: 4px;
}

/* Push default Leaflet controls slightly down */
.leaflet-top.leaflet-left {
  top: 5px !important;
}

/* --------------------------------------------------
   6. TOOLS AREA (IDENTIFY, CLEAN)
-------------------------------------------------- */

#tools-box {
  padding: 10px;
  border-bottom: 1px solid var(--color-border-soft);
}

#identify-btn,
#clean-btn {
  width: 100%;
  padding: 10px;
  border: none;
  color: #ffffff;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.2s, box-shadow 0.15s, transform 0.05s;
}

/* Main action */
#identify-btn {
  font-size: 0.95rem;
  background: var(--color-primary);
}

#identify-btn:hover {
  background: var(--color-primary-soft);
  box-shadow: var(--shadow-soft);
  transform: translateY(-1px);
}

/* Secondary action */
#clean-btn {
  font-size: 0.85rem;
  margin-top: 10px;
  background: var(--color-primary-muted);
}

#clean-btn:hover {
  background: var(--color-primary);
  box-shadow: var(--shadow-soft);
  transform: translateY(-1px);
}

/* --------------------------------------------------
   7. SEARCH BOX
-------------------------------------------------- */

#search-container {
  width: 100%;
  margin-top: 10px;
  background: var(--color-bg);
  border: 1px solid var(--color-border-soft);
  border-radius: 6px;
  padding: 6px;
  box-shadow: var(--shadow-soft);
}

#search-input {
  width: 100%;
  padding: 8px;
  border: none;
  outline: none;
  font-size: 0.85rem;
  background: transparent;
  color: var(--color-text);
}

#results-list {
  list-style: none;
  padding: 0;
  margin: 5px 0 0 0;
  max-height: 150px;
  overflow-y: auto;
  border-top: 1px solid var(--color-border-soft);
}

#results-list li {
  padding: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

#results-list li:hover {
  background: var(--color-panel);
}

/* --------------------------------------------------
   8. INFO PANEL
-------------------------------------------------- */

#info-box {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  width: 100%;
  box-sizing: border-box;
}

#info-box h3 {
  margin: 0 0 5px 0;
  font-size: 1rem;
  color: var(--color-text);
}

#info-content {
  width: 100%;
}

/* info key/value */
.info-label {
  color: var(--color-text-muted);
  white-space: nowrap;
}

.info-value {
  color: var(--color-text);
  word-break: break-word;
  border-bottom: 1px solid #f3f4f6;
  padding: 2px 0;
}

/* Collapsible info sections */
.info-section {
  background: var(--color-bg);
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--color-border-soft);
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 10px;
  box-shadow: var(--shadow-soft);
}

.info-section h4 {
  margin: 1px 0;
  font-size: 0.8rem;
  color: var(--color-text);
  font-weight: 700;
  border-bottom: 1px solid var(--color-border-soft);
  padding-bottom: 2px;
}

/* Collapsed state */
.info-section.collapsed .info-content-section {
  display: none;
}

.info-title {
  cursor: pointer;
}

/* Icon arrow for collapse/expand */
.info-title::after {
  content: "▼";
  color: var(--color-accent);
  float: right;
  transition: transform 0.2s;
}

.info-section.collapsed .info-title::after {
  transform: rotate(-90deg);
}

.info-content-section {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 10px;
  font-size: 0.8rem;
  margin-top: 4px;
}

/* Generic hidden class */
.hidden {
  display: none;
}

/* Identify loading */
#identify-loading {
  align-items: center;
  gap: 6px;
  padding: 4px 2px;
  color: var(--color-primary-soft);
  font-size: 0.85rem;
}

/* --------------------------------------------------
   9. MAP STATUS BAR
-------------------------------------------------- */

#map-status {
  padding: 6px 10px;
  border-top: 1px solid var(--color-border-soft);
  background: var(--color-bg-alt);
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

/* Popup table */
.popup-table {
  border-collapse: collapse;
}

.popup-table th {
  text-align: left;
  padding-right: 6px;
  font-weight: bold;
  color: var(--color-text);
}

.popup-table td {
  padding-left: 4px;
  color: var(--color-text-muted);
  white-space: nowrap;
}

/* --------------------------------------------------
   10. RESPONSIVE BEHAVIOR
-------------------------------------------------- */

@media (max-width: 1100px) {
  .grid-container {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  /* Sidebar above map */
  .grid-2 {
    grid-column: 1 / 2;
    grid-row: 1 / 2;

    /* 👉 Make the sidebar scrollable */
    max-height: 55vh;
    overflow-y: auto;

    /* Smooth scroll & avoid cutoff */
    -webkit-overflow-scrolling: touch;
  }

  .grid-1 {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
  }

  #divider {
    display: none;
  }

  .column-menu {
    height: auto !important;
    min-height: auto;
  }

  #info-box {
    flex: none !important;  /* Don't try to calculate flexible space */
    height: auto !important; /* Be as tall as the content needs */
    overflow: visible !important; /* No internal scrollbar */
  }
}




@media (max-width: 640px) {
  #header-box {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  #logo {
    height: 60px;
  }

  #tools-box {
    padding: 8px;
  }

  #identify-btn,
  #clean-btn {
    padding: 8px;
  }
}
