:root {
  --bg: #0f172a;
  --panel: #ffffff;
  --panel-muted: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #0284c7;
  --accent-soft: #e0f2fe;
  --warn-bg: #fef3c7;
  --warn-border: #f59e0b;
  --warn-text: #92400e;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--panel-muted);
  overflow: hidden;
}

.topbar {
  height: 58px;
  background: #0f172a;
  color: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 16px;
  border-bottom: 1px solid #1e293b;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-badge {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 13px;
  flex-shrink: 0;
}
.brand h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.subtitle {
  margin: 0;
  font-size: 11px;
  color: #94a3b8;
  white-space: nowrap;
}

.nav-tabs {
  display: flex;
  gap: 6px;
  background: #1e293b;
  padding: 4px;
  border-radius: 9px;
  flex-shrink: 0;
}
.tab-btn {
  border: 0;
  background: transparent;
  color: #cbd5e1;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.tab-btn.active {
  background: #0284c7;
  color: #fff;
}

.version-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  background: rgba(245, 158, 11, 0.14);
  color: #fde68a;
  border: 1px solid rgba(245, 158, 11, 0.4);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.version-pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #f59e0b;
}

.view {
  display: none;
  height: calc(100vh - 58px);
}
.view.active {
  display: flex;
}

/* MAP VIEW */
.sidebar {
  width: 340px;
  max-width: 90vw;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 5;
  flex-shrink: 0;
}
.sidebar.collapsed { display: none; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}
.sidebar-scroll {
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.group-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.basemap-grid, .bookmarks {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip, .chip-sm {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 7px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.chip-sm {
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 500;
}
.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #0369a1;
}

.control-card {
  background: var(--panel-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toggle-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  cursor: pointer;
}
.toggle-row input { margin-top: 3px; accent-color: var(--accent); }
.toggle-label { display: flex; flex-direction: column; font-size: 13px; }
.toggle-label small { color: var(--text-muted); font-size: 11px; }

.slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}
.slider-row input[type="range"] { flex: 1; accent-color: var(--accent); }

.select-input {
  width: 100%;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 12px;
  background: #fff;
}

.overlay-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 300px;
  overflow-y: auto;
}
.overlay-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
  border: 1px solid transparent;
  cursor: pointer;
  font-size: 12px;
}
.overlay-item:hover { background: #eef2f6; }
.overlay-item.active {
  background: var(--accent-soft);
  border-color: #bae6fd;
}
.overlay-item .ov-id {
  font-weight: 700;
  color: #0369a1;
  flex-shrink: 0;
}

.map-wrap {
  position: relative;
  flex: 1;
  height: 100%;
}
#map { width: 100%; height: 100%; }

.expand-btn {
  position: absolute;
  top: 70px;
  left: 12px;
  z-index: 6;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.14);
  cursor: pointer;
}

.legend-fab {
  position: absolute;
  bottom: 24px;
  left: 12px;
  z-index: 6;
  background: #0f172a;
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 9px 15px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.25);
  cursor: pointer;
}

.legend-drawer {
  position: absolute;
  bottom: 20px;
  left: 12px;
  width: 310px;
  max-width: calc(100% - 24px);
  max-height: 68vh;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.22);
  display: flex;
  flex-direction: column;
  z-index: 7;
}
.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 700;
}
.legend-body {
  overflow: auto;
  padding: 8px;
}
.legend-body img {
  width: 100%;
  height: auto;
  display: block;
}

/* POPUPS */
.popup-card { font-size: 12px; max-width: 240px; }
.popup-swatch {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 3px;
  margin-right: 6px;
  vertical-align: middle;
  border: 1px solid rgba(0,0,0,0.2);
}

/* GALLERY VIEW */
#galleryView {
  flex-direction: column;
  overflow: hidden;
}
.gallery-toolbar {
  padding: 12px 20px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}
.filter-pills { display: flex; flex-wrap: wrap; gap: 6px; }
.pill {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.pill.active {
  background: #0f172a;
  color: #fff;
  border-color: #0f172a;
}
.search-input {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  min-width: 240px;
}
.gallery-grid {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 16px;
  align-content: start;
}
.sheet-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.sheet-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.1);
}
.sheet-thumb {
  width: 100%;
  aspect-ratio: 1.414 / 1;
  object-fit: cover;
  background: #e2e8f0;
}
.sheet-meta {
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sheet-badges {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
}
.sheet-num {
  font-weight: 700;
  color: #0369a1;
}
.sheet-title {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
}
.sheet-notes {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* MODALS */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.88);
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.hidden { display: none !important; }

.lightbox-bar {
  height: 50px;
  background: #0f172a;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  gap: 10px;
  border-bottom: 1px solid #1e293b;
}
.lightbox-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  font-size: 13px;
}
.lightbox-title-wrap strong {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lb-badge {
  background: #0284c7;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
}
.lightbox-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.zoom-pct { font-size: 12px; min-width: 44px; text-align: center; color: #cbd5e1; }
.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: transparent;
  color: inherit;
  font-size: 16px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.sidebar-header .icon-btn, .dialog-head .icon-btn, .legend-header .icon-btn {
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary {
  border: 1px solid rgba(148, 163, 184, 0.4);
  background: #1e293b;
  color: #fff;
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}

.lb-stage {
  flex: 1;
  overflow: hidden;
  position: relative;
  cursor: grab;
  user-select: none;
  touch-action: none;
}
.lb-stage:active { cursor: grabbing; }
.lb-stage img {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  max-width: none;
}
.lb-footer {
  padding: 7px 14px;
  background: #0f172a;
  color: #94a3b8;
  font-size: 12px;
  border-top: 1px solid #1e293b;
}

.dialog-card {
  margin: auto;
  background: #fff;
  color: var(--text);
  width: 540px;
  max-width: 92vw;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}
.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.dialog-head h3 { margin: 0; font-size: 15px; }
.dialog-body { padding: 16px 18px; font-size: 13px; line-height: 1.55; }
.alert-box {
  background: var(--warn-bg);
  border-left: 4px solid var(--warn-border);
  color: var(--warn-text);
  padding: 10px 12px;
  border-radius: 6px;
  margin: 12px 0;
}
.alert-box p { margin: 6px 0 0; }
.small-note { font-size: 12px; color: var(--text-muted); margin-bottom: 0; }

@media (max-width: 768px) {
  .topbar { height: auto; flex-wrap: wrap; padding: 8px 12px; }
  .subtitle { display: none; }
  .view { height: calc(100vh - 92px); }
  .sidebar { position: absolute; left: 0; top: 92px; bottom: 0; width: 290px; }
}
