body, html {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
  overflow-y: auto;
  background: #f5f7fa;
}

/* Header */
#header {
  min-height: 70px;
  background: #ffffff;
  border-bottom: 1px solid #eaeaea;
  display: flex;
  align-items: center;
  padding: 0 30px;
  z-index: 1000;
  position: relative;
  box-shadow: 0 2px 4px rgba(0,0,0,0.03);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

#header img {
  height: 40px;
  width: auto;
  object-fit: contain;
  cursor: pointer;
}

.header-divider {
  width: 1px;
  height: 24px;
  background: #ddd;
}

#header h1 {
  font-size: 20px;
  margin: 0;
  color: #1a1a1a;
  font-weight: 600;
  letter-spacing: -0.5px;
}

#main-container {
  display: flex;
  height: calc(100vh - 70px);
  width: 100%;
  min-height: 0;
}

/* Sidebar */
#sidebar {
  width: 500px;
  background: #fdfdfd;
  border-right: 1px solid #ccc;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tab-nav {
  display: flex;
  background: #f1f1f1;
  border-bottom: 1px solid #ccc;
}

.tab-nav button {
  flex: 1;
  padding: 12px;
  border: none;
  outline: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  color: #666;
  transition: 0.3s;
  background: #f1f1f1;
}

.tab-nav button.active {
  background: #fff;
  color: #1f3bff;
  border-bottom: 2px solid #1f3bff;
}

.tab-content-container {
  flex: 1;
  overflow-y: auto;
  padding: 25px;
  box-sizing: border-box;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* Typography */
p, .ref-item {
  font-size: 14px;
  line-height: 1.6;
  color: #444;
  margin-bottom: 15px;
}

h3 {
  font-size: 1.2em;
  margin-top: 0;
  color: #333;
}

#dynamic-var-title {
  font-size: 1.4em;
  font-weight: bold;
  margin-bottom: 5px;
  color: #1f3bff;
}

#var-definition {
  font-size: 0.85em;
  color: #444;
  line-height: 1.6;
  background: #f0f4ff;
  padding: 12px;
  border-radius: 6px;
  margin-top: 20px;
  border-left: 4px solid #1f3bff;
  box-sizing: border-box;
}

.chart-selector {
  margin-bottom: 15px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
}

.btn-group {
  display: flex;
  background: #eee;
  border-radius: 8px;
  padding: 2px;
  width: 100%;
  justify-content: space-between;
  box-sizing: border-box;
  gap: 2px;
}

.btn-group button {
  flex: 1;
  border: none;
  padding: 6px 4px;
  cursor: pointer;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  background: transparent !important;
  color: #666 !important;
  white-space: nowrap;
}

.btn-group button.active {
  background: #fff !important;
  color: #1f3bff !important;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.info-card {
  background: #f8f9fa;
  padding: 12px 15px;
  border-radius: 8px;
  margin-bottom: 15px;
  border: 1px solid #eee;
  font-size: 13px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 3px;
  gap: 12px;
}

.info-item strong {
  color: #555;
  font-weight: 600;
}

.value-highlight {
  color: #d50000 !important;
  font-weight: 700 !important;
}

#plotImage {
  width: 92%;
  height: auto;
  display: none;
  margin: 15px auto 0;
  border-radius: 4px;
  border: 1px solid #ddd;
}

#map-frame {
  flex: 1;
  position: relative;
  min-width: 0;
}

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

/* Top controls */
#top-controls {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1100;
  background: rgba(203, 213, 224, 0.95);
  padding: 10px 25px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-sizing: border-box;
  backdrop-filter: blur(4px);
  border-bottom: 1px solid rgba(0,0,0,0.2);
}

.controls-row {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}

.controls-row-time {
  gap: 14px;
}

.controls-row-menu {
  gap: 16px;
}

.leaflet-top.leaflet-left {
  top: 65px !important;
}

.menu-group {
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}

.menu-group label {
  font-size: 12px;
  font-weight: 700;
  color: #333;
}

select {
  padding: 4px 8px;
  border-radius: 6px;
  border: 1px solid #d1d1d1;
  font-size: 12px;
  cursor: pointer;
  background: #fff;
}

select:focus {
  outline: none;
  border-color: #1f6feb;
  box-shadow: 0 0 0 2px rgba(31, 111, 235, 0.1);
}

#play-btn {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: #1f3bff;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex-shrink: 0;
}

#year-display {
  font-weight: 800;
  width: 45px;
  text-align: center;
  color: #1f3bff;
  font-size: 16px;
  flex-shrink: 0;
}

#time-slider {
  width: 160px;
}

#opacity-slider {
  width: 90px;
}

/* Legend */
#legend-container {
  position: absolute;
  bottom: 30px;
  left: 20px;
  z-index: 1100;
  background: white;
  padding: 12px;
  border: 1px solid #ccc;
  display: flex;
  gap: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  max-width: calc(100% - 40px);
  box-sizing: border-box;
}

#legend-bar {
  width: 18px;
  height: 220px;
  background: linear-gradient(to top, #1f3bff, #00b7ff, #00c853, #ffeb3b, #ff9800, #d50000);
  border-radius: 2px;
  flex-shrink: 0;
}

#legend-ticks {
  height: 220px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: #444;
}

.references-section {
  margin-top: 30px;
  border-top: 1px solid #eee;
  padding-top: 15px;
}

.references-section h3 {
  font-size: 18px;
  margin-bottom: 15px;
  font-weight: bold;
  color: #333;
}

.ref-item {
  padding-left: 30px;
  text-indent: -30px;
  margin-bottom: 12px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.ref-item a {
  color: #1f3bff;
  text-decoration: none;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.ref-item a:hover {
  text-decoration: underline;
}

@media (max-width: 1350px) {
  #sidebar {
    width: 430px;
  }

  #top-controls {
    gap: 14px;
    padding: 10px 18px;
  }
}

@media (max-width: 1024px) {
  #sidebar {
    width: 380px;
  }

  .controls-row {
    gap: 12px;
  }

  .controls-row-menu {
    gap: 12px;
  }

  .leaflet-top.leaflet-left {
    top: 110px !important;
  }
}

@media (max-width: 768px) {
  body, html {
    overflow-x: hidden;
    overflow-y: auto;
  }

  #header {
    min-height: auto;
    padding: 12px 16px;
  }

  .header-left {
    gap: 12px;
    align-items: center;
  }

  #header img {
    height: 32px;
  }

  .header-divider {
    display: none;
  }

  #header h1 {
    font-size: 17px;
    line-height: 1.3;
  }

  #main-container {
    flex-direction: column;
    height: auto;
    min-height: calc(100vh - 64px);
  }

  #map-frame {
    order: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
  }

  #top-controls {
    position: static;
    order: 1;
    padding: 12px;
    box-shadow: none;
    border-bottom: 1px solid #dbe2ea;
    backdrop-filter: none;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .controls-row {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .controls-row-time {
    gap: 10px;
  }

  .controls-row-menu {
    gap: 10px;
  }

  #play-btn {
    align-self: flex-start;
  }

  #year-display {
    width: auto;
    text-align: left;
  }

  .menu-group {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    white-space: normal;
  }

  .menu-group label {
    font-size: 12px;
  }

  #time-slider,
  #opacity-slider,
  #var-select,
  #model-select,
  #scenario-select {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
  }

  .leaflet-top.leaflet-left {
    top: 12px !important;
  }

  #map {
    order: 2;
    height: 52vh;
    min-height: 360px;
  }

  #legend-container {
    position: static;
    order: 3;
    margin: 12px;
    margin-top: 10px;
    margin-bottom: 0;
    max-width: none;
    width: auto;
    box-sizing: border-box;
  }

  #legend-bar {
    height: 160px;
    width: 14px;
  }

  #legend-ticks {
    height: 160px;
    font-size: 11px;
  }

  #sidebar {
    order: 2;
    width: 100%;
    border-right: none;
    border-top: 1px solid #ccc;
    overflow: visible;
  }

  .tab-content-container {
    overflow: visible;
    padding: 18px 16px 24px 16px;
  }

  .tab-nav button {
    font-size: 13px;
    padding: 11px 8px;
  }

  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  #plotImage {
    width: 100%;
    margin: 15px 0 0 0;
  }

  #var-definition {
    font-size: 0.82em;
    margin-top: 16px;
  }

  .btn-group {
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  .btn-group button {
    flex: 1 1 calc(50% - 4px);
    min-width: 120px;
    white-space: normal;
    line-height: 1.3;
    padding: 8px 6px;
  }

  .references-section {
    margin-top: 24px;
    padding-top: 14px;
  }

  .references-section h3 {
    font-size: 16px;
  }

  .ref-item {
    padding-left: 22px;
    text-indent: -22px;
    font-size: 13px;
    line-height: 1.55;
  }
}
