:root {
  --color-bg: #F7F8FB;
  --color-surface: #FFFFFF;
  --color-surface-alt: #F6F8FC;
  --color-border: #E4E8F1;
  --color-ink: #1E2432;
  --color-ink-soft: #647086;
  --color-ink-faint: #98A2B3;
  --color-accent: #5145F5;
  --color-accent-soft: #EFEDFF;
  --color-accent-dark: #3E34D6;
  --color-success: #22B86A;
  --color-danger: #FF4D57;
  --font-display: 'Manrope', ui-sans-serif, sans-serif;
  --font-body: 'Inter', ui-sans-serif, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-md: 0 10px 26px rgba(24, 32, 56, 0.10);
  --shadow-lg: 0 18px 48px rgba(24, 32, 56, 0.16);
  --radius-lg: 12px;
  --radius-md: 10px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  background: var(--color-bg);
  color: var(--color-ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}
body { overflow: auto; }
button, input, textarea { font: inherit; }
button { border: 0; background: transparent; cursor: pointer; }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 66px 1fr;
  background: var(--color-bg);
}

.app-rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 18px 12px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
}
.rail-group { display: flex; flex-direction: column; gap: 18px; margin-top: 28px; }
.rail-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: #667085;
  border-radius: 8px;
  transition: 0.16s ease;
}
.rail-btn span, .rail-btn svg { width: 21px; height: 21px; }
.rail-btn:hover, .rail-btn.is-active { color: var(--color-accent); background: var(--color-accent-soft); }
.rail-logo { background: var(--color-accent-soft); }
.rail-logo img { width: 25px; height: 25px; }

.app-main { min-width: 0; display: flex; flex-direction: column; min-height: 100vh; }
.topbar {
  height: 74px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 28px;
  background: rgba(255,255,255,.92);
  border-bottom: 1px solid var(--color-border);
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 236px; }
.brand-mark {
  width: 40px;
  height: 40px;
  padding: 8px;
  border-radius: 10px;
  background: var(--color-accent-soft);
}
.brand-title {
  display: block;
  font-family: var(--font-display);
  font-size: 18px;
  line-height: 1.15;
  font-weight: 800;
}
.brand-subtitle { display: block; color: var(--color-ink-soft); font-size: 13px; font-weight: 600; margin-top: 2px; }
.top-search { width: min(455px, 34vw); }
.topbar-spacer { flex: 1; }

.workspace {
  flex: 1;
  min-height: 640px;
  height: calc(100vh - 74px);
  display: grid;
  grid-template-columns: 328px minmax(420px, 1fr) 372px;
  gap: 0;
  padding: 22px 24px 24px 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  margin-right: 6px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  box-shadow: 10px 0 28px rgba(31, 41, 55, 0.04);
}
.sidebar-head { padding: 18px 16px 14px; }
.sidebar-title-row { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.sidebar-title-row h2 { margin: 0; font-family: var(--font-display); font-size: 20px; font-weight: 800; }
.count-pill {
  min-width: 24px;
  height: 24px;
  padding: 0 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: #EEF1F5;
  color: #7B8495;
  font-size: 12px;
  font-weight: 800;
}
.sidebar-search-row { display: flex; align-items: center; gap: 8px; }
.filter-btn {
  width: 40px;
  height: 38px;
  display: grid;
  place-items: center;
  color: var(--color-ink-soft);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: white;
}
.filter-btn span, .filter-btn svg { width: 18px; height: 18px; }
.sidebar-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 6px 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field-input {
  width: 100%;
  min-height: 38px;
  color: var(--color-ink);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color .16s ease, box-shadow .16s ease;
}
.field-input::placeholder { color: #9AA4B5; }
.field-input:focus { border-color: #A69FFF; box-shadow: 0 0 0 4px rgba(81,69,245,.10); }
.field-label {
  display: block;
  margin-bottom: 9px;
  color: #697386;
  font-size: 13px;
  font-weight: 700;
}

.btn {
  min-height: 41px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 7px;
  border: 1px solid transparent;
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
  transition: transform .14s ease, background .14s ease, border-color .14s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary { color: white; background: var(--color-accent); box-shadow: 0 8px 20px rgba(81,69,245,.24); }
.btn-primary:hover { background: var(--color-accent-dark); }
.btn-secondary { color: var(--color-ink); background: white; border-color: var(--color-border); }
.btn-secondary:hover { background: #F8FAFC; border-color: #D6DBE6; }
.btn-danger-ghost { color: var(--color-danger); background: #fff; border-color: #FFB8BE; }
.btn-danger-ghost:hover { background: #FFF3F4; }
.icon-btn, .top-icon {
  width: 38px;
  height: 38px;
  display: inline-grid;
  place-items: center;
  color: #667085;
  border-radius: 8px;
}
.icon-btn:hover, .top-icon:hover { background: #F3F5F9; color: var(--color-ink); }
.icon-btn span, .icon-btn svg { width: 20px; height: 20px; }

.territory-card {
  position: relative;
  width: 100%;
  min-height: 82px;
  display: grid;
  grid-template-columns: 8px 70px minmax(0, 1fr) auto 18px;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 4px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: white;
  text-align: left;
  color: var(--color-ink);
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.03);
  transition: border-color .16s ease, box-shadow .16s ease, transform .16s ease;
}
.territory-card:hover { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.territory-card.is-active { border-color: var(--color-accent); box-shadow: 0 0 0 1px var(--color-accent); }
.card-dot { width: 7px; height: 7px; border-radius: 50%; background: #E8ECF3; justify-self: center; }
.territory-card.is-active .card-dot { background: var(--color-accent); }
.territory-thumb {
  width: 70px;
  height: 62px;
  border-radius: 7px;
  overflow: hidden;
  background: #F2F4F7;
}
.territory-info { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.territory-number { font-family: var(--font-display); font-size: 13px; font-weight: 800; }
.territory-name { font-size: 12px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.territory-date { color: var(--color-ink-soft); font-size: 11px; font-weight: 600; }
.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
}
.status-pill.mini { align-self: end; margin-bottom: 9px; }
.card-menu { color: #8791A3; }
.card-menu svg { width: 18px; height: 18px; }

.pager {
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 12px 0 18px;
}
.pager-btn, .pager-current {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px solid var(--color-border);
  background: white;
  color: #667085;
  font-weight: 800;
}
.pager-current { background: var(--color-accent-soft); color: var(--color-accent); border-color: transparent; }
.pager svg { width: 18px; height: 18px; }

.map-stage { min-width: 0; padding: 0 12px 0 0; }
.map-card {
  position: relative;
  height: 100%;
  min-height: 520px;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: #EEF2F6;
  box-shadow: var(--shadow-sm);
}
#map { position: absolute; inset: 0; }
.leaflet-container { font-family: var(--font-body); background: #EEF2F6; }
.leaflet-tile-pane { opacity: .58; filter: saturate(.62) contrast(.95) brightness(1.05); }
.leaflet-overlay-pane path { filter: drop-shadow(0 6px 10px rgba(81,69,245,.16)); }
.leaflet-control-attribution {
  display: block !important;
  right: 12px !important;
  bottom: 4px !important;
  background: rgba(255,255,255,.75) !important;
  font-size: 11px !important;
}
.leaflet-draw.leaflet-control, .leaflet-control-zoom { display: none !important; }

.map-toolbar, .zoom-control {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 7px;
  background: rgba(255,255,255,.95);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
}
.map-tool-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: #566176;
  border-radius: 7px;
  transition: .15s ease;
}
.map-tool-btn:hover, .map-tool-btn.tool-active { background: var(--color-accent-soft); color: var(--color-accent); }
.map-tool-btn.danger { color: var(--color-danger); }
.map-tool-btn svg, .zoom-control svg { width: 20px; height: 20px; }
.zoom-control { gap: 0; padding: 0; overflow: hidden; }
.zoom-control button {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  color: var(--color-ink);
}
.zoom-control button:first-child { border-bottom: 1px solid var(--color-border); }
.layers-float, .locate-float {
  position: absolute;
  z-index: 400;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(255,255,255,.95);
  border: 1px solid var(--color-border);
  color: #1F2937;
  box-shadow: var(--shadow-md);
}
.layers-float { top: 18px; right: 18px; }
.locate-float { left: 20px; bottom: 20px; }
.layers-float svg, .locate-float svg { width: 22px; height: 22px; }

.right-panel-shell {
  min-width: 0;
  display: flex;
  align-items: stretch;
}
.panel-inner { width: 100%; min-height: 0; }
.panel-card {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: white;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
}
.panel-top {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  padding: 22px 20px 12px;
}
.panel-kicker { margin: 0 0 10px; color: #8B95A8; font-size: 13px; font-weight: 800; }
.panel-top h3 { margin: 0; font-family: var(--font-display); font-size: 21px; font-weight: 800; }
.panel-date { margin: 8px 0 0; color: #737D91; font-size: 13px; font-weight: 600; }
.panel-actions-top {
  position: absolute;
  top: 16px;
  right: 14px;
  display: flex;
  gap: 8px;
}
.panel-top > .status-pill { align-self: end; margin-right: 0; margin-top: 60px; }
.panel-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 20px 18px;
}
.form-group { margin-bottom: 20px; }
.section-row { display: flex; align-items: center; justify-content: space-between; }
textarea.field-input { min-height: 100px; line-height: 1.5; }
.char-count {
  margin: -25px 10px 0 0;
  color: #8A94A6;
  font-size: 11px;
  font-weight: 700;
  text-align: right;
  pointer-events: none;
}
.panel-section-title {
  margin: 0 0 28px;
  color: #344054;
  font-size: 14px;
  font-weight: 800;
}
.card-preview {
  height: 160px;
  display: grid;
  grid-template-columns: 1fr 164px;
  overflow: hidden;
  background: white;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: var(--shadow-sm);
}
.card-preview-copy {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
  padding: 18px 16px;
}
.preview-number {
  margin: 0;
  color: var(--color-accent);
  font-family: var(--font-display);
  font-size: 42px;
  line-height: 1;
  font-weight: 800;
}
.preview-name { margin: 0; font-size: 18px; font-weight: 800; }
.preview-date {
  margin: 4px 0 0;
  display: flex;
  align-items: center;
  gap: 6px;
  color: #667085;
  font-size: 12px;
  font-weight: 700;
}
.preview-date span, .preview-date svg { width: 15px; height: 15px; }
.card-preview-map { height: 100%; background: #F2F5F1; }
.panel-footer {
  flex-shrink: 0;
  padding: 18px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.empty-panel {
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 32px;
  text-align: center;
}

.address-results {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 260px;
  overflow-y: auto;
  z-index: 50;
  background: white;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
}

.export-map-canvas {
  position: fixed;
  left: 0;
  top: 0;
  width: 960px;
  height: 720px;
  overflow: hidden;
  background: #EEF2F6;
  z-index: -1;
  pointer-events: none;
}

.export-map-canvas .leaflet-tile-pane {
  opacity: .78;
  filter: saturate(.72) contrast(.96) brightness(1.04);
}

.export-map-canvas .leaflet-control-attribution {
  display: block !important;
  right: 8px !important;
  bottom: 4px !important;
  background: rgba(255, 255, 255, .72) !important;
  font-size: 10px !important;
}

.scroll-thin::-webkit-scrollbar, #right-panel::-webkit-scrollbar { width: 6px; }
.scroll-thin::-webkit-scrollbar-thumb, #right-panel::-webkit-scrollbar-thumb { background: #D7DDE8; border-radius: 999px; }
.scroll-thin::-webkit-scrollbar-track, #right-panel::-webkit-scrollbar-track { background: transparent; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 12px);
  opacity: 0;
  z-index: 60;
  background: var(--color-ink);
  color: white;
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 800;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  transition: transform .2s ease, opacity .2s ease;
}
#toast.toast-in { transform: translate(-50%, 0); opacity: 1; }
#toast.toast-error { background: var(--color-danger); }

@media (max-width: 1180px) {
  .workspace { grid-template-columns: 308px minmax(360px, 1fr); padding-right: 16px; }
  .right-panel-shell {
    position: fixed;
    top: 86px;
    right: 16px;
    bottom: 16px;
    width: 360px;
    z-index: 45;
  }
  #right-panel-wrapper { transform: translateX(110%); opacity: 0; pointer-events: none; transition: .22s ease; }
  #right-panel-wrapper.panel-open { transform: translateX(0); opacity: 1; pointer-events: auto; }
}

@media (min-width: 1181px) {
  #right-panel-wrapper { transform: none; opacity: 1; pointer-events: auto; }
}

@media (max-width: 767px) {
  body { overflow: auto; }
  .app-shell { grid-template-columns: 1fr; }
  .app-rail { display: none; }
  .topbar { height: auto; min-height: 68px; padding: 12px; gap: 10px; flex-wrap: wrap; }
  .brand { min-width: auto; flex: 1; }
  .brand-subtitle { display: none; }
  .top-search { order: 3; width: 100%; }
  .top-icon { display: none; }
  .workspace { display: block; padding: 12px; }
  .sidebar {
    position: fixed;
    top: 68px;
    left: 0;
    bottom: 0;
    width: min(88vw, 330px);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .22s ease;
  }
  .sidebar.sidebar-open { transform: translateX(0); }
  .map-stage { padding: 0; height: calc(100vh - 92px); }
  .right-panel-shell {
    position: fixed;
    inset: 82px 12px 12px;
    width: auto;
  }
  .card-preview { grid-template-columns: 1fr; height: auto; }
  .card-preview-map { height: 132px; }
}
