:root {
  --mv-bg: #f8fafc;
  --mv-surface: #ffffff;
  --mv-surface-soft: #f8fafc;
  --mv-surface-hover: #f1f5f9;
  --mv-text: #0f172a;
  --mv-text-soft: #334155;
  --mv-muted: #64748b;
  --mv-faint: #94a3b8;
  --mv-border: #e2e8f0;
  --mv-border-strong: #cbd5e1;
  --mv-accent: #2563eb;
  --mv-accent-soft: #dbeafe;
  --mv-accent-dark: #1d4ed8;
  --mv-water: #0ea5e9;
  --mv-success: #16a34a;
  --mv-warning: #d97706;
  --mv-danger: #dc2626;
  --mv-radius-sm: 7px;
  --mv-radius-control: 9px;
  --mv-radius-panel: 13px;
  --mv-shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .045);
  --mv-shadow-float: 0 8px 24px rgba(15, 23, 42, .14);
  --mv-header-h: 58px;
  --mv-sidebar-w: 326px;
  --mv-profile-h: 39vh;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; }
body {
  overflow: hidden;
  font-family: 'DM Sans', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--mv-text);
  background: var(--mv-bg);
}
button, input, select { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }

.app-header {
  height: var(--mv-header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 0 18px;
  background: rgba(255, 255, 255, .97);
  border-bottom: 1px solid var(--mv-border);
  box-shadow: 0 1px 8px rgba(15, 23, 42, .05);
  position: relative;
  z-index: 900;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}
.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: linear-gradient(145deg, #38bdf8, #2563eb 68%, #1d4ed8);
  color: #fff;
  box-shadow: 0 5px 16px rgba(37, 99, 235, .22);
}
.brand-mark svg { width: 18px; height: 18px; }
.brand-copy { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.brand-copy strong { font-size: 16px; letter-spacing: -.025em; }
.brand-copy span { color: var(--mv-muted); font-size: 13px; white-space: nowrap; }
.version-badge {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  color: var(--mv-muted);
  background: var(--mv-surface-soft);
  border: 1px solid var(--mv-border);
  padding: 3px 6px;
  border-radius: 999px;
}
.header-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn, .text-btn, .seg-btn, .action-btn {
  border: 1px solid var(--mv-border);
  background: #fff;
  border-radius: var(--mv-radius-control);
  min-height: 36px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease, transform .15s ease, box-shadow .15s ease;
}
.icon-btn:hover, .text-btn:hover, .seg-btn:hover, .action-btn:hover:not(:disabled) {
  background: var(--mv-surface-soft);
  border-color: var(--mv-border-strong);
}
.icon-btn:focus-visible, .text-btn:focus-visible, .seg-btn:focus-visible, .action-btn:focus-visible, input:focus-visible {
  outline: 3px solid rgba(37, 99, 235, .13);
  outline-offset: 1px;
  border-color: rgba(37, 99, 235, .45);
}
.icon-btn:disabled, .text-btn:disabled, .seg-btn:disabled, .action-btn:disabled { opacity: .4; cursor: not-allowed; }
.text-btn { padding: 0 12px; font-size: 12px; font-weight: 650; color: var(--mv-text-soft); }
.text-btn.primary { background: var(--mv-accent); color: #fff; border-color: var(--mv-accent); }
.text-btn.primary:hover { background: var(--mv-accent-dark); border-color: var(--mv-accent-dark); }

.workspace {
  height: calc(100vh - var(--mv-header-h));
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--mv-sidebar-w);
  grid-template-rows: minmax(0, 1fr) var(--mv-profile-h);
  gap: 12px;
  padding: 12px;
  min-height: 0;
}

.map-shell {
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--mv-border);
  border-radius: var(--mv-radius-panel);
  background: #e2e8f0;
  box-shadow: var(--mv-shadow);
}
#map { width: 100%; height: 100%; }

.map-tool-rail {
  position: absolute;
  z-index: 600;
  top: 14px;
  left: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.map-tool-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid rgba(15, 23, 42, .11);
  background: rgba(255, 255, 255, .95);
  color: #475569;
  box-shadow: var(--mv-shadow-float);
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: transform .15s ease, color .15s ease, background .15s ease, border-color .15s ease;
  position: relative;
}
.map-tool-btn:hover:not(:disabled) { transform: translateY(-1px); color: #0f172a; border-color: rgba(37, 99, 235, .25); }
.map-tool-btn.active { color: #fff; background: var(--mv-accent); border-color: var(--mv-accent); }
.map-tool-btn:disabled { opacity: .38; cursor: not-allowed; }
.map-tool-btn svg { width: 18px; height: 18px; }
.map-tool-btn::after {
  content: attr(data-tooltip);
  position: absolute;
  left: calc(100% + 9px);
  top: 50%;
  transform: translateY(-50%) translateX(-3px);
  pointer-events: none;
  opacity: 0;
  white-space: nowrap;
  background: rgba(15, 23, 42, .94);
  color: #fff;
  font-size: 11px;
  font-weight: 650;
  padding: 6px 8px;
  border-radius: 6px;
  transition: opacity .12s ease, transform .12s ease;
}
.map-tool-btn:hover::after { opacity: 1; transform: translateY(-50%) translateX(0); }

.profile-sidebar {
  grid-column: 2;
  grid-row: 1 / span 2;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background: var(--mv-surface);
  border: 1px solid var(--mv-border);
  border-radius: var(--mv-radius-panel);
  box-shadow: var(--mv-shadow);
}
.sidebar-section { padding: 15px; border-bottom: 1px solid var(--mv-border); }
.sidebar-section:last-child { border-bottom: 0; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.section-title-row h2, .section-title-row h3 { margin: 0; font-size: 13px; letter-spacing: -.015em; }
.section-caption { margin: -5px 0 12px; color: var(--mv-muted); font-size: 11px; line-height: 1.45; }

.profile-list { display: grid; gap: 7px; }
.profile-card {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 10px;
  min-height: 54px;
  padding: 8px 9px;
  display: grid;
  grid-template-columns: 5px minmax(0, 1fr) 14px;
  gap: 10px;
  align-items: center;
  text-align: left;
  cursor: pointer;
}
.profile-card:hover { background: var(--mv-surface-soft); }
.profile-card.selected { background: #eff6ff; border-color: #bfdbfe; }
.profile-color { width: 5px; align-self: stretch; min-height: 34px; border-radius: 999px; background: var(--profile-color); }
.profile-card-main { display: grid; gap: 3px; min-width: 0; }
.profile-card-name { font-size: 12px; font-weight: 700; color: var(--mv-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.profile-card-meta { font-size: 10.5px; color: var(--mv-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.profile-status-dot { width: 9px; height: 9px; border-radius: 999px; background: var(--mv-border-strong); }
.profile-status-dot.working { background: var(--mv-accent); box-shadow: 0 0 0 4px rgba(37, 99, 235, .08); animation: pulse 1.2s ease-in-out infinite; }
.profile-status-dot.ready { background: var(--mv-success); }
.profile-status-dot.warning { background: var(--mv-warning); }
.profile-status-dot.error { background: var(--mv-danger); }
@keyframes pulse { 50% { opacity: .42; } }

.empty-mini {
  border: 1px dashed var(--mv-border-strong);
  border-radius: 10px;
  padding: 13px;
  color: var(--mv-muted);
  font-size: 11px;
  line-height: 1.5;
  text-align: center;
}

.segment-group { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.segment-group.three { grid-template-columns: repeat(3, 1fr); }
.seg-btn {
  min-width: 0;
  padding: 0 7px;
  min-height: 34px;
  font-size: 11px;
  font-weight: 700;
  color: var(--mv-muted);
  background: var(--mv-surface-soft);
}
.seg-btn.active { color: var(--mv-accent-dark); border-color: #93c5fd; background: #eff6ff; box-shadow: inset 0 0 0 1px rgba(37, 99, 235, .04); }

.settings-block + .settings-block { margin-top: 15px; }
.settings-label { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 7px; font-size: 11px; font-weight: 700; color: var(--mv-text-soft); }
.settings-hint { font-weight: 500; color: var(--mv-faint); }

.water-row { display: grid; grid-template-columns: 1fr 92px; gap: 7px; align-items: center; }
.water-input {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--mv-border);
  border-radius: var(--mv-radius-sm);
  background: var(--mv-surface-soft);
  padding: 0 8px;
  font-size: 11px;
  color: var(--mv-text-soft);
}
.water-input:disabled { opacity: .45; }

.sidebar-actions { display: grid; gap: 7px; }
.action-btn {
  width: 100%;
  min-height: 38px;
  padding: 0 10px;
  font-size: 11px;
  font-weight: 700;
  color: var(--mv-text-soft);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.action-btn strong { color: var(--mv-text); }
.action-btn.primary { background: var(--mv-accent); color: #fff; border-color: var(--mv-accent); justify-content: center; }
.action-btn.primary:hover { background: var(--mv-accent-dark); border-color: var(--mv-accent-dark); }

.profile-dock {
  grid-column: 1;
  grid-row: 2;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 46px minmax(0, 1fr);
  background: var(--mv-surface);
  border: 1px solid var(--mv-border);
  border-radius: var(--mv-radius-panel);
  box-shadow: var(--mv-shadow);
  overflow: hidden;
}
.profile-dock-head {
  min-width: 0;
  border-bottom: 1px solid var(--mv-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 12px 0 16px;
}
.profile-head-copy { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.profile-head-copy h2 { margin: 0; font-size: 13px; white-space: nowrap; }
.profile-head-copy span { color: var(--mv-muted); font-size: 10.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.profile-dock-actions { display: flex; align-items: center; gap: 7px; position: relative; }
.icon-btn { width: 34px; height: 34px; min-height: 34px; display: grid; place-items: center; color: var(--mv-muted); padding: 0; }
.icon-btn.active { color: var(--mv-accent-dark); border-color: #93c5fd; background: #eff6ff; }
.icon-btn svg { width: 16px; height: 16px; }
.profile-canvas { min-height: 0; overflow: auto; padding: 10px; background: #f8fafc; }
.profile-canvas > .profile-svg { width: 100%; min-width: 760px; height: 100%; display: block; }
.comparison-profile { margin-bottom: 10px; border-radius: 12px; overflow: hidden; border: 1px solid var(--mv-border); background: #fff; }
.comparison-profile .profile-svg { width: 100%; min-width: 760px; display: block; }
.empty-state { height: 100%; min-height: 150px; display: grid; place-content: center; text-align: center; gap: 6px; color: var(--mv-muted); font-size: 12px; }
.empty-state strong { color: var(--mv-text-soft); font-size: 13px; }

.export-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  width: 210px;
  padding: 7px;
  border: 1px solid var(--mv-border);
  border-radius: 11px;
  background: rgba(255, 255, 255, .98);
  box-shadow: var(--mv-shadow-float);
  z-index: 1000;
}
.export-menu button {
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 7px;
  min-height: 34px;
  padding: 0 9px;
  text-align: left;
  font-size: 11px;
  cursor: pointer;
  color: var(--mv-text-soft);
}
.export-menu button:hover:not(:disabled) { background: var(--mv-surface-soft); }
.export-menu button:disabled { opacity: .4; cursor: not-allowed; }
.export-menu hr { border: 0; border-top: 1px solid var(--mv-border); margin: 5px 0; }

.toast {
  position: fixed;
  z-index: 2000;
  left: 50%;
  bottom: 18px;
  transform: translate(-50%, 12px);
  max-width: min(560px, calc(100vw - 32px));
  background: rgba(15, 23, 42, .94);
  color: #fff;
  padding: 9px 12px;
  border-radius: 9px;
  font-size: 11px;
  line-height: 1.4;
  opacity: 0;
  pointer-events: none;
  transition: opacity .16s ease, transform .16s ease;
  box-shadow: var(--mv-shadow-float);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-tone="warning"] { background: rgba(146, 64, 14, .95); }
.toast[data-tone="error"] { background: rgba(153, 27, 27, .96); }

.leaflet-control-zoom, .leaflet-control-layers {
  border: 1px solid rgba(15, 23, 42, .11) !important;
  border-radius: 10px !important;
  box-shadow: var(--mv-shadow-float) !important;
  overflow: hidden;
}
.leaflet-control-zoom a { width: 38px !important; height: 38px !important; line-height: 38px !important; color: #475569 !important; }
.leaflet-control-layers { background: rgba(255, 255, 255, .96) !important; }
.profile-line-label {
  border: 0 !important;
  background: rgba(255, 255, 255, .92) !important;
  color: #334155 !important;
  font-family: 'DM Sans', system-ui, sans-serif !important;
  font-size: 10px !important;
  font-weight: 800 !important;
  border-radius: 999px !important;
  padding: 3px 6px !important;
  box-shadow: 0 2px 8px rgba(15, 23, 42, .13) !important;
}
.profile-line-label::before { display: none !important; }

@media (max-width: 980px) {
  :root { --mv-sidebar-w: 286px; --mv-profile-h: 42vh; }
  .brand-copy span { display: none; }
}

@media (max-width: 760px) {
  body { overflow: auto; }
  .workspace {
    height: auto;
    min-height: calc(100vh - var(--mv-header-h));
    grid-template-columns: 1fr;
    grid-template-rows: 52vh auto 42vh;
    overflow: visible;
  }
  .map-shell { grid-column: 1; grid-row: 1; }
  .profile-sidebar { grid-column: 1; grid-row: 2; overflow: visible; }
  .profile-dock { grid-column: 1; grid-row: 3; }
  .header-actions .text-btn:not(.primary) { display: none; }
}
