:root {
  font: 14px/1.45 "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: light;
  --text: #16261f;
  --muted: #586c62;
  --page: #f2f1ea;
  --panel: #fbfaf5;
  --surface: #ffffff;
  --line: #dadfd6;
  --line-strong: #c2cbc0;
  --accent: #1c5d46;
  --accent-soft: #e4f1eb;
  --accent-ink: #ffffff;
  --hover: #edf2ec;
  --focus: #237a5b;
  --shadow-sm: 0 1px 2px rgba(18, 38, 30, 0.06);
  --shadow-md: 0 6px 20px rgba(18, 38, 30, 0.08);
  --shadow-lg: 0 20px 55px rgba(14, 32, 25, 0.24);
  --radius-sm: 7px;
  --radius-md: 10px;
  --radius-lg: 14px;
}
:root[data-theme=dark] {
  color-scheme: dark;
  --text: #ebf1ec;
  --muted: #98aea1;
  --page: #111916;
  --panel: #17221d;
  --surface: #1e2c26;
  --line: #2d3e36;
  --line-strong: #3d5248;
  --accent: #7fd4b1;
  --accent-soft: #203b31;
  --accent-ink: #0d2119;
  --hover: #253830;
  --focus: #96e0c1;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.28);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.34);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  min-width: 320px;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button, a, input, select { touch-action: manipulation; }
button {
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease, transform 100ms ease, box-shadow 140ms ease;
}
button:active:not(:disabled) { transform: translateY(0.5px); }
button:disabled { opacity: .42; cursor: default; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
[hidden] { display: none !important; }
a { color: inherit; }
h1, h2, p { margin: 0; }

/* Slim custom scrollbars */
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* Topbar */
.topbar {
  height: 58px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow-sm);
  position: relative;
  z-index: 10;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-size: 19px;
  font-weight: 760;
  letter-spacing: -0.03em;
}
.brand img { width: 30px; height: 30px; }
.brand-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}
.top-actions, .history, .dialog-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

/* Buttons */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  white-space: nowrap;
}
.button:hover:not(:disabled) {
  background: var(--hover);
  border-color: var(--line-strong);
}
.primary, .primary:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 2px 8px color-mix(in srgb, var(--accent) 28%, transparent);
}
.primary:hover:not(:disabled) { filter: brightness(0.93); }
.icon-button { width: 34px; padding: 0; font-size: 17px; }

/* Segmented control */
.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--page);
  gap: 2px;
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 4px 10px;
  border-radius: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed=true] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* Workspace layout */
.workspace {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr) 356px;
  min-height: 600px;
  height: calc(100dvh - 58px);
}
.navigator, .inspector {
  min-width: 0;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.navigator { border-right: 1px solid var(--line); }
.inspector { border-left: 1px solid var(--line); }
.nav-top, .nav-geography, .nav-territories, .inspector-header { padding: 14px 16px; }
.nav-top, .nav-geography, .inspector-header { border-bottom: 1px solid var(--line); }

.field-label {
  color: var(--muted);
  font-size: 10.5px;
  letter-spacing: .09em;
  text-transform: uppercase;
  font-weight: 700;
  display: block;
  margin-bottom: 6px;
}
.map-picker { display: grid; grid-template-columns: minmax(0, 1fr) 34px; gap: 6px; }
select, input:not([type=checkbox], [type=radio], [type=color], [type=file]) {
  min-width: 0;
  max-width: 100%;
  width: 100%;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  min-height: 34px;
  font-size: 13px;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
select:hover, input:not([type=checkbox], [type=radio], [type=color], [type=file]):hover {
  border-color: var(--line-strong);
}
input[type=color] {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px;
  width: 40px;
  height: 32px;
}
input[type=checkbox] { accent-color: var(--accent); }
.map-actions { display: flex; justify-content: space-between; margin-top: 7px; }
.map-actions button, .link-button {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 550;
  padding: 2px 1px;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.map-actions button:hover:not(:disabled), .link-button:hover:not(:disabled) { color: var(--accent); }
.check { display: flex; align-items: center; gap: 7px; margin: 10px 0; font-size: 13px; }
.check.compact { font-size: 12px; margin: 8px 0 0; color: var(--muted); }
.mode-select-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Territories navigator */
.nav-territories {
  flex: 1;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  padding-bottom: 6px;
  overflow: hidden;
}
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 7px;
}
.section-title h2 { font-size: 13.5px; font-weight: 700; }
.section-title span { color: var(--muted); font-size: 11.5px; font-weight: 600; }
.territory-filter-row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.territory-filter-row input { flex: 1; }
.mini-pill {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 650;
  white-space: nowrap;
}
.mini-pill:hover { border-color: var(--line-strong); color: var(--text); }
.mini-pill[aria-pressed=true] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.territory-list {
  overflow: auto;
  flex: 1;
  margin: 8px -6px 0;
  padding: 2px 6px;
}
.territory-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: inherit;
  text-align: left;
  min-height: 31px;
  padding: 4px 7px;
  font-size: 13px;
}
.territory-row:hover { background: var(--hover); }
.territory-row[aria-current=true] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 680;
}
.territory-row.region { padding-left: 20px; font-size: 12px; min-height: 28px; }
.territory-row small {
  margin-left: auto;
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 600;
  max-width: 72px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.swatch {
  width: 10px;
  height: 10px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.14);
}
.catalog-button { margin: 8px 16px 14px; }

/* Map canvas area */
.canvas {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(260px, 1fr) auto;
  padding: 14px 18px 10px;
  background: var(--page);
}
.canvas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding-bottom: 12px;
  flex-wrap: wrap;
}
.canvas-heading { min-width: 0; }
.canvas-heading .field-label { margin-bottom: 1px; }
.canvas h1 {
  font-size: clamp(19px, 2.4vw, 25px);
  font-weight: 750;
  letter-spacing: -0.03em;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.map-stage {
  position: relative;
  min-height: 0;
  display: grid;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow-md);
}
.map-canvas { position: absolute; inset: 0; display: grid; place-items: stretch; }
.map-canvas svg { width: 100%; height: 100%; display: block; }
.map-canvas svg [data-territory] { cursor: pointer; }
.map-canvas svg [data-territory] .territory-shape {
  transition: filter 120ms ease, opacity 120ms ease;
}
.map-canvas svg [data-territory]:hover .territory-shape {
  filter: brightness(0.95) saturate(1.08);
}
.map-canvas svg [data-territory][data-active=true] .territory-shape {
  filter: brightness(1.04) saturate(1.12);
}

/* Floating hover tooltip pill on map */
.map-hover-pill {
  position: absolute;
  top: 12px;
  left: 12px;
  pointer-events: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: min(75%, 360px);
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-md);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  z-index: 5;
  transition: opacity 120ms ease;
}
.map-hover-pill .hover-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.16);
}
.map-hover-pill .hover-party {
  color: var(--muted);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Floating zoom controls */
.zoom {
  display: grid;
  position: absolute;
  bottom: 14px;
  left: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow-md);
  z-index: 5;
}
.zoom button {
  height: 33px;
  width: 35px;
  background: none;
  border: 0;
  font-size: 17px;
  font-weight: 600;
}
.zoom button + button { border-top: 1px solid var(--line); }
.zoom button:hover { background: var(--hover); }

/* Canvas footer with progress bar */
.canvas-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 9px;
  color: var(--muted);
  font-size: 12px;
  flex-wrap: wrap;
}
.footer-progress {
  display: inline-flex;
  align-items: center;
  gap: 9px;
}
.progress-track {
  width: 84px;
  height: 6px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width 220ms ease;
}
.kbd-hints {
  color: var(--muted);
  font-size: 11px;
  opacity: 0.85;
}
.kbd-hints kbd {
  display: inline-block;
  padding: 1px 5px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface);
  font-family: inherit;
  font-size: 10.5px;
  font-weight: 600;
}

/* Inspector */
.inspector-header h2 {
  font-size: 20px;
  font-weight: 740;
  letter-spacing: -0.025em;
  line-height: 1.2;
  overflow-wrap: anywhere;
}
#party-panel { min-height: 0; flex: 1; display: flex; flex-direction: column; overflow: hidden; padding: 14px 16px; }
.empty-note { color: var(--muted); padding: 4px; line-height: 1.55; font-size: 13px; }
.territory-style { margin: 8px 16px 14px; }

/* Dialogs */
dialog {
  max-width: min(95vw, 560px);
  width: 100%;
  max-height: 88dvh;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 0;
  color: var(--text);
  background: var(--panel);
  box-shadow: var(--shadow-lg);
}
dialog::backdrop {
  background: rgba(14, 28, 22, 0.55);
  backdrop-filter: blur(3px);
}
.dialog-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.dialog-header h2 { font-size: 18px; font-weight: 720; letter-spacing: -0.02em; }
.dialog-body { padding: 18px; }
.dialog-body label:not(.check) { display: grid; gap: 4px; font-size: 12.5px; font-weight: 550; }
.settings-grid, .export-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.settings-grid + .settings-grid { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--line); }
.settings-grid label.check { margin: 2px 0; }
.dialog-actions { justify-content: flex-end; margin-top: 15px; }
.export-other {
  margin-top: 16px;
  padding-top: 13px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 7px;
  flex-wrap: wrap;
}
#export-status:not(:empty) { margin-top: 10px; }

/* Responsive */
@media (max-width: 1180px) {
  .workspace { grid-template-columns: 210px minmax(0, 1fr) 310px; }
  .nav-top, .nav-geography, .nav-territories, .inspector-header { padding: 12px; }
  .canvas { padding-inline: 12px; }
  .kbd-hints { display: none; }
}
@media (max-width: 860px) {
  .workspace { grid-template-columns: minmax(0, 1fr) 295px; height: auto; min-height: calc(100dvh - 58px); }
  .navigator { grid-column: 1 / -1; border-right: 0; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 1fr 1fr; overflow: visible; }
  .nav-territories { min-height: 0; max-height: 190px; }
  .canvas { min-height: 540px; }
  .catalog-button { align-self: start; }
  .inspector { max-height: 620px; }
}
@media (max-width: 620px) {
  .topbar { padding: 0 10px; }
  .brand-badge { display: none; }
  .top-actions { gap: 5px; }
  .top-actions .button { padding-inline: 8px; font-size: 12px; }
  .brand { font-size: 16px; gap: 6px; }
  .brand img { width: 26px; height: 26px; }
  .workspace { display: flex; flex-direction: column; }
  .canvas { min-height: 460px; }
  .inspector { border-left: 0; border-top: 1px solid var(--line); min-height: 340px; max-height: none; }
  .inspector .pm-party-list { max-height: min(60vh, 37rem); }
  .navigator { order: 2; grid-template-columns: 1fr; }
  .settings-grid, .export-form { grid-template-columns: 1fr; }
}
