html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-x: hidden;
  /* Prevent horizontal scroll */
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#controlsPanel {
  position: absolute;
  bottom: calc(10px + env(safe-area-inset-bottom));
  left: 10px;
  background: rgba(255, 255, 255, 0.9);
  padding: 5px;
  border-radius: 16px;
  z-index: 1000;
  font-family: Arial, Helvetica, sans-serif;
  max-width: 300px;
  font-size: 12px;
  line-height: 1.35;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  overflow: hidden;
  transition: all 0.3s ease;
}

#panelBody {
  padding: 12px;
  max-height: 2000px;
  opacity: 1;
  overflow: hidden;
  transition: max-height 0.4s ease, opacity 0.3s ease, padding 0.3s ease;
}

#controlsPanel.collapsed #panelBody {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  margin-top: 0;
  opacity: 0;
}

#controlsPanel.collapsed {
  width: fit-content;
  min-width: 90px;
  max-width: 108px;
  padding: 2px 4px;
  border-radius: 10px;
}

#controlsPanel.collapsed #panelHeader {
  padding: 5px 6px;
  font-size: 12px;
  line-height: 1.1;
  border-top: none;
  background: transparent;
}

#controlsPanel.collapsed #panelHeader span b {
  font-weight: 600;
}

#controlsPanel.collapsed #panelToggle {
  font-size: 12px;
}

#panelHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.1));
  border-top: 1px solid rgba(221, 221, 221, 0.5);
  padding: 10px 12px;
  font-weight: 500;
  color: #333;
}

.sub-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  background-color: #e9e9e9;
  padding: 4px 8px;
  margin-top: 8px;
  border-radius: 4px;
  font-size: 12px;
}

#controlsPanel label {
  font-size: 12px !important;
}

.sub-panel-body {
  padding-left: 10px;
  max-height: 1000px;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}

.sub-panel-body.collapsed {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
}

#bkg_option {
  margin-left: 0px;
}

#panelBody,
#rasterPanel {
  margin-top: 5px;
}

.radio-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 2px;
}

#colorSelect option {
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: black;
}

.radio-row label {
  display: flex;
  align-items: center;
  gap: 5px;
}

#controlsPanel input,
.input-field,
#controlsPanel select,
#controlsPanel button {
  margin: 6px 0;
  width: 100%;
  box-sizing: border-box;
  font-size: 12px;
}

.weight-label {
  background: rgba(255, 255, 255, 0.5);
  font-weight: 600;
  font-size: 12px;
}

.legend {
  background: rgba(255, 255, 255, 0.95);
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.4;
  color: #000;
  max-width: 160px;
}

.legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.1));
  border-top: 1px solid rgba(221, 221, 221, 0.5);
  padding: 10px 12px;
  font-weight: 500;
  font: 1.2em Arial, Helvetica, sans-serif;
  color: #333;
}

.legend .color-box {
  width: 18px;
  height: 18px;
  display: inline-block;
  margin-right: 6px;
  vertical-align: middle;
}

.legend .size-box {
  display: inline-block;
  vertical-align: middle;
  margin-right: 6px;
  border-radius: 50%;
  border: 1px solid #666;
  width: 16px;
  height: 16px;
}

@media (max-width: 1366px) {
  #controlsPanel {
    left: 10px;
    right: auto;
    max-width: calc(100% - 20px);
    width: auto;
  }
}

.form-container {
  display: flex;
  align-items: center;
  gap: 2px;
}

.form-container label {
  margin: 0;
  white-space: nowrap;
}

#histPanel,
#uncertaintyHistPanel,
#scatterPanel {
  position: absolute;
  bottom: 10px;
  right: 200px;
  z-index: 500;
  background: rgba(255, 255, 255, 0.7);
  padding: 8px 10px;
  border-radius: 6px;
  width: auto;
  max-width: 260px;
  font-size: 12px;
  display: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

#scatterPanel {
  background: rgba(255, 255, 255, 0.7) !important;
}

#semivariogramPanel {
  position: absolute;
  bottom: 10px;
  right: 200px;
  z-index: 500;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid #999;
  width: auto;
  max-width: 300px !important;
  font-size: 12px;
  border-radius: 6px;
  padding: 8px 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

#toggleSemivariogram {
  display: inline-block !important;
}

#histCanvas svg {
  display: block;
  margin: 0;
  padding: 0;
}

#histClose:hover {
  color: red;
}

#exportDiv {
  display: flex;
  align-items: center;
  gap: 5px;
}

#bottom-controls-container {
  position: absolute;
  bottom: calc(10px + env(safe-area-inset-bottom));
  left: calc(50% - 70px);
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.9);
  padding: 8px 12px;
  border-radius: 50px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(5px);
  width: fit-content;
  max-width: 95vw;
}

#bottom-controls-container button {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: #555;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  margin: 0;
}

#bottom-controls-container button:hover {
  background-color: #f0f0f0;
  color: #007bff;
  transform: translateY(-2px);
}

#bottom-controls-container button:active {
  transform: translateY(0);
}

.control-separator {
  width: 1px;
  height: 24px;
  background-color: #ddd;
  margin: 0 4px;
}

#dpiSelect {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 4px;
  font-size: 12px;
  background: white;
  color: #555;
  cursor: pointer;
}

.scatter-tooltip {
  position: absolute;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 5px;
  border-radius: 3px;
  font-size: 10px;
  pointer-events: none;
  z-index: 1010;
}

#exportContainer {
  position: relative;
  width: 100vw;
  height: 100dvh;
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

.disabled-panel {
  opacity: 0.4;
  pointer-events: none;
  filter: grayscale(100%);
  transition: opacity 0.3s ease;
}

.legend-hidden {
  opacity: 0.5;
}

.chart-panel {
  position: absolute;
  bottom: calc(10px + env(safe-area-inset-bottom));
  right: 10px;
  z-index: 1001;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid #999;
  border-radius: 5px;
  padding: 8px;
}


.panel-image {
  max-height: 100%;
  width: auto;
  display: block;
}

#controlsPanel img,
#histPanel img,
#uncertaintyHistPanel img,
#scatterPanel img,
#semivariogramPanel img,
.chart-panel img {
  max-width: 100%;
  height: 150px;
  display: block;
}

.modal {
  display: none;
  position: fixed;
  z-index: 10000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(8px);
}

.modal-content {
  margin: 5% auto;
  display: block;
  max-width: 90%;
  max-height: 90%;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.close-modal {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
  cursor: pointer;
}

.legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  border-bottom: 1px solid rgba(204, 204, 204, 0.5);
  padding-bottom: 6px;
  margin-bottom: 6px;
  font-weight: 600;
  color: #333;
}

.legend-body {
  transition: max-height 0.3s ease-out, opacity 0.3s ease-out;
  max-height: 500px;
  opacity: 1;
  overflow: hidden;
}

.legend-body.collapsed {
  max-height: 0;
  opacity: 0;
  margin: 0;
  padding: 0;
}

.legend.legend-collapsed {
  width: fit-content;
  min-width: 90px;
  max-width: 108px;
  padding: 2px 4px;
  border-radius: 10px;
}

.legend.legend-collapsed .legend-header {
  padding: 5px 6px;
  font-size: 12px;
  line-height: 1.1;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 500;
  margin-bottom: 0;
  border-top: none;
  border-bottom: none;
  background: transparent;
}

.legend.legend-collapsed .legend-header span b {
  font-weight: 600;
}

.legend.legend-collapsed .legend-toggle {
  font-size: 12px;
}

.legend-ramp-wrap {
  margin: 4px 0;
  width: 100%;
}

.legend-ramp-bar {
  width: 100%;
  height: 14px;
  border: 1px solid #999;
  border-radius: 2px;
}

.legend-ramp-labels {
  margin-top: 2px;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  font-size: 10px;
  line-height: 1.2;
}

.legend-ramp-labels span {
  flex: 1 1 0;
  min-width: 0;
}

.legend-ramp-labels span:last-child {
  text-align: right;
}

.manual-content {
  width: 90%;
  height: 80%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  position: relative;
}

.manual-container {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.manual-toc {
  width: 250px;
  background-color: #f4f4f4;
  padding: 20px;
  border-right: 1px solid #ddd;
  overflow-y: auto;
}

.manual-toc h3 {
  margin-top: 0;
  font-size: 1.2em;
  border-bottom: 2px solid #ccc;
  padding-bottom: 10px;
}

.manual-toc ul {
  list-style-type: none;
  padding-left: 0;
  counter-reset: item;
}

.manual-toc li {
  margin-bottom: 8px;
  counter-increment: item;
}

/* Skip numbering for Overview in TOC */
.manual-toc>ul>li:first-child {
  counter-increment: none;
}

.manual-toc li::before {
  content: counters(item, ".") ". ";
  font-weight: bold;
  font-size: 0.9em;
  color: #666;
  margin-right: 5px;
}

.manual-toc>ul>li:first-child::before {
  content: none;
}

.manual-toc ul ul {
  padding-left: 20px;
  margin-top: 5px;
}

.manual-toc a {
  text-decoration: none;
  color: #333;
  font-weight: bold;
}

.manual-toc a:hover {
  color: #007bff;
}

.manual-body {
  flex: 1;
  padding: 30px;
  overflow-y: auto;
  background-color: #fff;
  counter-reset: section;
  /* Reset section counter */
}

.manual-body h2 {
  border-bottom: 2px solid #eee;
  padding-bottom: 8px;
  margin-top: 40px;
  counter-reset: subsection;
  /* Reset subsection counter on each h2 */
  counter-increment: section;
  /* Increment section counter */
}

/* Skip numbering for Overview in Body */
.manual-body h2:first-of-type {
  counter-increment: none;
}

.manual-body h2::before {
  content: counter(section) ". ";
}

.manual-body h2:first-of-type::before {
  content: none;
}

.manual-body h3 {
  border-bottom: 1px solid #f0f0f0;
  /* Light divider for subsections */
  padding-bottom: 4px;
  margin-top: 25px;
  counter-increment: subsection;
  /* Increment subsection counter */
}

.manual-body h3::before {
  content: counter(section) "." counter(subsection) ". ";
}

.manual-body h2:first-child {
  margin-top: 0;
}

.manual-img {
  max-width: 100%;
  height: auto;
  border: 1px solid #ddd;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  margin: 15px 0;
  display: block;
}

.close-button {
  position: absolute;
  top: 10px;
  right: 15px;
  font-size: 28px;
  font-weight: bold;
  color: #aaa;
  cursor: pointer;
  z-index: 10;
}

.close-button:hover,
.close-button:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}

/* Sensitivity Analysis Modal Styles */
.sensitivity-content {
  width: 70%;
  max-width: 800px;
  height: auto;
  max-height: 85%;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  position: relative;
}

.sensitivity-container {
  padding: 20px 30px 30px;
  overflow-y: auto;
}

.sensitivity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ddd;
  padding-bottom: 15px;
  margin-bottom: 15px;
  flex-wrap: wrap;
  gap: 15px;
}

.sensitivity-header h3 {
  margin: 0;
  font-size: 1.4em;
  color: #333;
}

.sensitivity-param-selector {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sensitivity-param-selector label {
  font-weight: 500;
  color: #555;
  font-size: 0.95em;
}

.sensitivity-param-selector select {
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 0.95em;
  background: white;
  cursor: pointer;
  min-width: 200px;
}

.sensitivity-param-selector select:hover {
  border-color: #007bff;
}

.sensitivity-fixed-params {
  background: #f8f9fa;
  padding: 10px 15px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 0.9em;
  color: #555;
}

.sensitivity-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px;
  color: #666;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #007bff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

#sensitivityChartContainer {
  min-height: 320px;
  margin-bottom: 15px;
}

#sensitivityChartContainer svg {
  display: block;
  margin: 0 auto;
}

.sensitivity-legend {
  display: flex;
  justify-content: center;
  gap: 30px;
  padding: 10px;
  border-top: 1px solid #eee;
}

.sensitivity-legend .legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95em;
  font-weight: 500;
}

.sensitivity-legend .legend-color {
  width: 16px;
  height: 16px;
  border-radius: 3px;
}

.sensitivity-legend .ccc-color {
  background-color: #4daf4a;
}

.sensitivity-legend .rmse-color {
  background-color: #e41a1c;
}

.sensitivity-legend .mae-color {
  background-color: #377eb8;
}

/* Currently selected bar highlight */
.sensitivity-bar-selected {
  stroke: #ffd700;
  stroke-width: 3px;
}

/* Tooltip for sensitivity chart */
.sensitivity-tooltip {
  position: absolute;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 12px;
  pointer-events: none;
  z-index: 10010;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  max-width: 200px;
}

.sensitivity-tooltip strong {
  display: block;
  margin-bottom: 5px;
  font-size: 13px;
}

/* Responsive Adjustments */
@media (max-width: 1366px) {
  #bottom-controls-container {
    left: 10px;
    right: 10px;
    transform: none;
    width: auto;
    max-width: none;
    padding: 6px 8px;
    gap: 6px;
    bottom: calc(8px + env(safe-area-inset-bottom));
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    border-radius: 14px;
  }

  #bottom-controls-container::-webkit-scrollbar {
    display: none;
  }

  #bottom-controls-container button {
    flex: 0 0 auto;
    width: 30px;
    height: 30px;
    font-size: 14px;
  }

  .control-separator {
    margin: 0 2px;
  }

  #controlsPanel {
    width: min(280px, calc(100vw - 150px));
    max-width: calc(100vw - 140px);
    bottom: calc(54px + env(safe-area-inset-bottom));
    left: 10px;
    right: auto;
  }

  #panelBody {
    max-height: 46vh;
    overflow-y: auto;
  }

  .form-container {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }

  .form-container label {
    white-space: normal;
  }

  /* Keep map controls visible above map overlays */
  .leaflet-top,
  .leaflet-bottom {
    z-index: 1001;
  }

  .leaflet-right {
    right: 5px;
  }

  .leaflet-control-attribution {
    display: none !important;
  }

  .legend {
    position: fixed !important;
    top: auto !important;
    bottom: calc(54px + env(safe-area-inset-bottom)) !important;
    right: 10px !important;
    left: auto !important;
    max-width: 130px;
    z-index: 1002;
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid rgba(0, 0, 0, 0.1);
  }

  .chart-panel,
  #histPanel,
  #uncertaintyHistPanel,
  #scatterPanel,
  #semivariogramPanel,
  #sensitivityPanel {
    right: 10px;
    left: auto;
    top: 56px;
    bottom: auto;
    width: min(300px, calc(100vw - 20px));
    max-width: min(300px, calc(100vw - 20px));
    max-height: 44vh;
    overflow-y: auto;
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.1);
    border-radius: 10px;
    padding: 10px;
    z-index: 1008;
  }

  .manual-content,
  .sensitivity-content {
    width: 95%;
    height: 90%;
    margin: 2% auto;
    position: relative;
  }

  /* Adjust close buttons for mobile - closer to corner and larger */
  .close-modal,
  .close-button {
    right: 10px !important;
    top: 5px !important;
    font-size: 32px !important;
    padding: 10px;
    z-index: 10001;
  }

  .manual-container {
    flex-direction: column;
  }

  .manual-toc {
    width: 100%;
    height: 30%;
    border-right: none;
    border-bottom: 1px solid #ddd;
    padding: 10px;
  }

  .manual-body {
    padding: 15px;
  }

  #dpiSelect {
    display: none;
    /* Hide DPI select on mobile to save space */
  }

  .sensitivity-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .sensitivity-param-selector {
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }

  .sensitivity-param-selector select {
    width: 100%;
  }

  .sensitivity-legend {
    flex-wrap: wrap;
    gap: 15px;
  }
}

@media (max-width: 480px) {
  #bottom-controls-container {
    padding: 4px 6px;
  }

  #bottom-controls-container button {
    width: 28px;
    height: 28px;
    font-size: 13px;
  }

  #panelHeader,
  .legend-header {
    padding: 8px 10px;
    font-size: 0.9em;
  }

  .legend {
    font-size: 10px;
    max-width: 118px;
  }

  #controlsPanel {
    max-width: calc(100vw - 130px);
  }
}

/* Sensitivity Floating Panel */
#sensitivityPanel {
  position: absolute;
  bottom: 10px;
  right: 200px;
  z-index: 500;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid #999;
  width: auto;
  max-width: 290px;
  font-size: 12px;
  border-radius: 6px;
  padding: 8px 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.sensitivity-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid #ddd;
}

.sensitivity-panel-param-selector {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 0;
}

.sensitivity-panel-param-selector label {
  font-weight: 500;
  color: #555;
  font-size: 11px;
  white-space: nowrap;
}

.sensitivity-panel-param-selector select {
  flex: 1;
  padding: 3px 5px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 10px;
  background: white;
  cursor: pointer;
  max-width: 130px;
}

.sensitivity-panel-fixed-inline {
  font-size: 9px;
  color: #666;
  font-style: italic;
  margin-left: 4px;
}

.sensitivity-panel-param-selector select:hover {
  border-color: #007bff;
}

.sensitivity-panel-fixed {
  display: none;
}

#sensitivityPanelChart {
  min-height: 120px;
  margin-top: 0;
  padding-bottom: 0;
  position: relative;
}

#sensitivityPanelChart svg {
  display: block;
  margin: 0 auto;
}

.sensitivity-panel-legend {
  display: flex;
  flex-direction: row;
  justify-content: center;
  gap: 12px;
  padding: 2px 0 4px;
  margin-bottom: 2px;
}

.sensitivity-panel-legend .legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 9px;
  font-weight: 500;
}

.sensitivity-panel-legend .legend-color {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.sensitivity-panel-legend .ccc-color {
  background-color: #4daf4a;
}

.sensitivity-panel-legend .rmse-color {
  background-color: #e41a1c;
}

.sensitivity-panel-legend .mae-color {
  background-color: #377eb8;
}

#sensitivityPanelClose:hover {
  color: red;
}

/* Weight Distance Plot Styles */
#weightPlot {
  width: 100%;
  height: 100%;
}

.axis-label {
  font-family: sans-serif;
  fill: #666;
  font-size: 10px;
}

#weightPlot circle {
  transition: opacity 0.2s;
}

#weightPlot circle:hover {
  opacity: 1;
  stroke: #333;
  stroke-width: 2px;
}

#mobileUiDock {
  display: none;
}

@media (max-width: 1366px) {
  :root {
    --mobile-ui-bottom: calc(10px + env(safe-area-inset-bottom));
    --mobile-dock-left: 10px;
    --mobile-dock-width: 84px;
    --mobile-gap: 8px;
    --mobile-side-left: calc(var(--mobile-dock-left) + var(--mobile-dock-width) + var(--mobile-gap));
    --mobile-right-reserve: 190px;
    --mobile-content-width: clamp(190px, calc(100vw - var(--mobile-side-left) - var(--mobile-right-reserve)), 320px);
    --mobile-content-center: clamp(
      calc(var(--mobile-side-left) + (var(--mobile-content-width) / 2)),
      calc(50vw - 24px),
      calc(100vw - var(--mobile-right-reserve) - (var(--mobile-content-width) / 2))
    );
  }

  #mobileUiDock {
    display: flex;
    position: fixed;
    left: var(--mobile-dock-left);
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 1200;
    flex-direction: column;
    gap: 6px;
  }

  .mobile-dock-btn {
    border: 1px solid #d0d0d0;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    padding: 6px 10px;
    min-width: 84px;
    font-size: 12px;
    text-align: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }

  .mobile-dock-btn[aria-expanded="true"] {
    border-color: #2f6fed;
    background: #e8f0ff;
    color: #1b4fc1;
    font-weight: 600;
  }

  #controlsPanel,
  #bottom-controls-container,
  .legend {
    display: none !important;
  }

  body.mobile-panel-controls #controlsPanel {
    display: block !important;
    position: fixed;
    left: auto;
    right: 10px;
    top: auto;
    width: min(260px, calc(100vw - 20px));
    max-width: min(260px, calc(100vw - 20px));
    bottom: calc(4px + env(safe-area-inset-bottom));
    max-height: 44vh;
    overflow: hidden;
    z-index: 1150;
  }

  body.mobile-panel-controls #controlsPanel #panelBody {
    padding: 8px;
  }

  body.mobile-panel-controls #controlsPanel.collapsed #panelBody {
    padding: 0 !important;
    margin: 0 !important;
    max-height: 0 !important;
    opacity: 0;
    overflow: hidden;
  }

  body.mobile-panel-controls #controlsPanel .sub-panel-header {
    padding: 3px 6px;
  }

  body.mobile-panel-controls #controlsPanel.collapsed {
    width: fit-content !important;
    min-width: 90px !important;
    max-width: 108px !important;
    padding: 2px 4px;
  }

  body.mobile-panel-controls #controlsPanel.collapsed #panelHeader {
    padding: 5px 6px;
    font-size: 12px;
    line-height: 1.1;
  }

  body.mobile-panel-toolbar #bottom-controls-container {
    display: flex !important;
    position: fixed;
    left: var(--mobile-content-center);
    transform: translateX(-50%);
    right: auto;
    width: fit-content;
    max-width: calc(100vw - var(--mobile-side-left) - var(--mobile-right-reserve));
    min-width: 0;
    bottom: calc(4px + env(safe-area-inset-bottom));
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    z-index: 1180;
  }

  body.mobile-panel-toolbar #bottom-controls-container::-webkit-scrollbar {
    display: none;
  }

  body.mobile-panel-toolbar #bottom-controls-container button {
    width: 28px;
    height: 28px;
    font-size: 13px;
  }

  body.mobile-panel-legend .legend {
    display: block !important;
    position: fixed !important;
    left: auto !important;
    right: 4px !important;
    top: auto !important;
    bottom: calc(4px + env(safe-area-inset-bottom)) !important;
    width: min(150px, calc(100vw - 20px));
    max-width: min(150px, calc(100vw - 20px));
    margin: 0 !important;
    z-index: 1150;
  }

  body.mobile-panel-legend {
    --mobile-right-reserve: 210px;
  }

  body.mobile-panel-legend .legend.legend-collapsed {
    width: fit-content !important;
    min-width: 90px !important;
    max-width: 108px !important;
    padding: 2px 4px;
  }

  .chart-panel,
  #histPanel,
  #uncertaintyHistPanel,
  #scatterPanel,
  #semivariogramPanel,
  #sensitivityPanel {
    left: var(--mobile-content-center) !important;
    right: auto !important;
    top: auto !important;
    bottom: calc(4px + env(safe-area-inset-bottom)) !important;
    transform: translateX(-50%);
    width: var(--mobile-content-width) !important;
    max-width: var(--mobile-content-width) !important;
    max-height: 42vh;
    overflow-y: auto;
    z-index: 1140;
  }

  body.mobile-panel-toolbar .chart-panel,
  body.mobile-panel-toolbar #histPanel,
  body.mobile-panel-toolbar #uncertaintyHistPanel,
  body.mobile-panel-toolbar #scatterPanel,
  body.mobile-panel-toolbar #semivariogramPanel,
  body.mobile-panel-toolbar #sensitivityPanel {
    bottom: calc(46px + env(safe-area-inset-bottom)) !important;
    z-index: 1160;
  }
}

@media (max-width: 430px) {
  body.mobile-panel-legend .legend {
    right: 6px !important;
    bottom: calc(8px + env(safe-area-inset-bottom)) !important;
    width: min(132px, calc(100vw - 18px));
    max-width: min(132px, calc(100vw - 18px));
  }

  body.mobile-panel-legend.mobile-panel-toolbar .legend {
    bottom: calc(58px + env(safe-area-inset-bottom)) !important;
  }
}

/* Narrow-phone safeguard: prioritize toolbar/chart space over legend */
@media (max-width: 560px) {
  body.mobile-panel-legend {
    --mobile-right-reserve: 16px;
  }

  .chart-panel,
  #histPanel,
  #uncertaintyHistPanel,
  #scatterPanel,
  #semivariogramPanel,
  #sensitivityPanel {
    left: var(--mobile-side-left) !important;
    right: auto !important;
    transform: none !important;
    width: calc(100vw - var(--mobile-side-left) - 8px) !important;
    max-width: calc(100vw - var(--mobile-side-left) - 8px) !important;
    box-sizing: border-box !important;
  }

  body.mobile-panel-toolbar .chart-panel,
  body.mobile-panel-toolbar #histPanel,
  body.mobile-panel-toolbar #uncertaintyHistPanel,
  body.mobile-panel-toolbar #scatterPanel,
  body.mobile-panel-toolbar #semivariogramPanel,
  body.mobile-panel-toolbar #sensitivityPanel {
    bottom: calc(52px + env(safe-area-inset-bottom)) !important;
  }

  body.mobile-panel-legend .legend {
    display: block !important;
    width: min(132px, calc(100vw - 20px));
    max-width: min(132px, calc(100vw - 20px));
  }

  body.mobile-panel-toolbar #bottom-controls-container {
    left: var(--mobile-side-left);
    transform: none;
    width: calc(100vw - var(--mobile-side-left) - 8px);
    max-width: calc(100vw - var(--mobile-side-left) - 8px);
    box-sizing: border-box;
  }
}
