.sel-box {
  fill: none;
  stroke: #3b82f6;
  stroke-width: 2;
  stroke-dasharray: 5, 5;
  pointer-events: all;
  cursor: move;
  animation: sel-dash 0.5s linear infinite;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.sel-box.locked {
  stroke: #ef4444;
  stroke-dasharray: 3, 3;
  cursor: not-allowed;
  animation: sel-dash-locked 0.5s linear infinite;
}

@keyframes sel-dash {
  to { stroke-dashoffset: -10; }
}

@keyframes sel-dash-locked {
  to { stroke-dashoffset: -6; }
}

.sel-handle-hit {
  fill: #fff0;
  stroke: none;
  pointer-events: all;
  cursor: nwse-resize;
  touch-action: none;
  -webkit-tap-highlight-color: #fff0;
}

.sel-handle-hit.nw,
.sel-handle-hit.se {
  cursor: nwse-resize;
}

.sel-handle-hit.ne,
.sel-handle-hit.sw {
  cursor: nesw-resize;
}

.sel-handle-hit.n,
.sel-handle-hit.s {
  cursor: ns-resize;
}

.sel-handle-hit.e,
.sel-handle-hit.w {
  cursor: ew-resize;
}

.sel-rotate-arm {
  stroke: #3b82f6;
  stroke-width: 2;
  stroke-dasharray: 3, 3;
  pointer-events: none;
}

.sel-rotate {
  fill: #3b82f6;
  stroke: #3b82f6;
  stroke-width: 2;
  cursor: grab;
  pointer-events: all;
  touch-action: none;
  -webkit-tap-highlight-color: #fff0;
}

.sel-rotate:active {
  cursor: grabbing;
  fill: #dbeafe;
}

.sel-marquee {
  fill: rgb(59 130 246 / .1);
  stroke: #3b82f6;
  stroke-width: 1;
  stroke-dasharray: 4, 4;
  pointer-events: none;
  animation: sel-marquee-dash 0.5s linear infinite;
}

@keyframes sel-marquee-dash {
  to { stroke-dashoffset: -8; }
}

/* CRITICAL FIX: Prevent icon wrapping on smaller screens */
.toolctx-select {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgb(0 0 0 / .1);
  flex-wrap: nowrap; /* Changed from 'wrap' to prevent wrapping */
  overflow-x: auto; /* Enable horizontal scrolling if needed */
  overflow-y: hidden;
}

.toolctx-select .sep {
  width: 1px;
  height: 24px;
  background: #e5e7eb;
  margin: 0 4px;
  flex-shrink: 0; /* Prevent separators from shrinking */
}

.toolctx-select .iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  min-width: 36px; /* Ensure minimum width */
  padding: 0;
  border: none;
  background: #fff0;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  color: #374151;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
  flex-shrink: 0; /* Prevent buttons from shrinking */
}

@media (hover: none) and (pointer: coarse) {
  .toolctx-select .iconbtn {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
  }
}

.toolctx-select .iconbtn:hover:not(:disabled) {
  background: #f3f4f6;
  color: #1f2937;
}

.toolctx-select .iconbtn:active:not(:disabled) {
  background: #e5e7eb;
  transform: scale(.95);
}

.toolctx-select .iconbtn:disabled {
  opacity: .4;
  cursor: not-allowed;
}

.toolctx-select .iconbtn.active {
  background: #dbeafe;
  color: #3b82f6;
}

.toolctx-select .iconbtn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

/* UPDATED: stroke width control now flexes to fill spare space
   and has no grey background behind it */
.stroke-width-ctrl {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  background: transparent;      /* no grey pill behind the slider */
  border-radius: 6px;
  border: none;
  flex: 1 1 240px;              /* allow it to grow on wide screens */
  max-width: 420px;
  box-sizing: border-box;
}

.stroke-width-ctrl label {
  font-size: 13px;
  font-weight: 500;
  color: rgb(0, 0, 0);
  white-space: nowrap;
}

/* UPDATED: make slider fill the available width + transparent background */
.stroke-width-ctrl input[type="range"] {
  flex: 1 1 auto;
  width: 100%;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;    /* remove grey background block */
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  touch-action: manipulation;
  box-sizing: border-box;
}

/* Custom track so only a thin line is visible, not a chunky grey bar */
.stroke-width-ctrl input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: #d1d5db;
}

.stroke-width-ctrl input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: #d1d5db;
}

.stroke-width-ctrl input[type="range"]::-ms-track {
  height: 4px;
  border-radius: 2px;
  background: #d1d5db;
  border-color: transparent;
  color: transparent;
}

/* Thumb unchanged, still clearly visible */
.stroke-width-ctrl input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: #3b82f6;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.15s ease;
  margin-top: -6px; /* center thumb over 4px track */
}

.stroke-width-ctrl input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: #3b82f6;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.15s ease;
}

.stroke-width-ctrl input[type="range"]::-ms-thumb {
  width: 16px;
  height: 16px;
  background: #3b82f6;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.15s ease;
}

.stroke-width-ctrl input[type="range"]::-webkit-slider-thumb:hover {
  background: #2563eb;
  transform: scale(1.1);
}

.stroke-width-ctrl input[type="range"]::-moz-range-thumb:hover {
  background: #2563eb;
  transform: scale(1.1);
}

@media (hover: none) and (pointer: coarse) {
  .stroke-width-ctrl input[type="range"]::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
    margin-top: -10px;
  }
  .stroke-width-ctrl input[type="range"]::-moz-range-thumb {
    width: 24px;
    height: 24px;
  }
}

.stroke-width-ctrl input[type="number"] {
  width: 60px;
  padding: 4px 8px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 13px;
  text-align: center;
  outline: none;
  transition: border-color 0.15s ease;
  touch-action: manipulation;
  box-sizing: border-box;
}

.stroke-width-ctrl input[type="number"]:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgb(59 130 246 / .1);
}

.stroke-width-ctrl span {
  font-size: 13px;
  color: #6b7280;
}

.toolctx-select .hint {
  font-size: 12px;
  color: #9ca3af;
  font-style: italic;
  margin-left: auto;
  white-space: nowrap;
  flex-shrink: 0;
}

/* RESPONSIVE: Tablet-specific adjustments (iPads, etc.) */
@media (min-width: 641px) and (max-width: 1024px) {
  .toolctx-select {
    gap: 6px;
    padding: 6px 10px;
  }

  .toolctx-select .iconbtn {
    width: 32px;
    height: 32px;
    min-width: 32px;
  }

  .toolctx-select .iconbtn svg {
    width: 18px;
    height: 18px;
  }

  .stroke-width-ctrl {
    flex: 1 1 180px;
    max-width: 360px;
  }

  .stroke-width-ctrl input[type="range"] {
    /* still fills available space */
  }

  .stroke-width-ctrl input[type="number"] {
    width: 50px;
  }
}

@media (max-width: 768px) {
  .toolctx-select .hint {
    display: none;
  }
}

/* Touch action menu */
.touch-action-menu {
  position: fixed;
  bottom: 80px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgb(0 0 0 / .15);
  z-index: 1000;
  touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
  .touch-action-menu {
    display: none;
  }
}

.touch-action-menu button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: none;
  background: #f3f4f6;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #374151;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
}

.touch-action-menu button:active {
  background: #e5e7eb;
  transform: scale(.95);
}

.touch-action-menu button svg {
  width: 28px;
  height: 28px;
}

/* Uniform scale toggle */
.uniform-scale-toggle {
  position: fixed;
  bottom: 80px;
  left: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 2px solid #d1d5db;
  background: #fff;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #6b7280;
  z-index: 1000;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
}

@media (hover: hover) and (pointer: fine) {
  .uniform-scale-toggle {
    display: none;
  }
}

.uniform-scale-toggle.active {
  background: #3b82f6;
  border-color: #3b82f6;
  color: #fff;
}

.uniform-scale-toggle:active {
  transform: scale(.95);
}

.uniform-scale-toggle svg {
  width: 28px;
  height: 28px;
}

/* Tooltip */
#selectionTooltip {
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

#selectionTooltip rect {
  filter: drop-shadow(0 2px 4px rgb(0 0 0 / .2));
}

#selectionTooltip text {
  pointer-events: none;
}

/* Focus styles */
.toolctx-select .iconbtn:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

.stroke-width-ctrl input:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* Accessibility preferences */
@media (prefers-contrast: high) {
  .sel-box { stroke-width: 3; }
  .sel-handle { stroke-width: 3; }
  .sel-rotate { stroke-width: 3; }
}

@media (prefers-reduced-motion: reduce) {
  .sel-box,
  .sel-box.locked,
  .sel-marquee { animation: none; }

  .toolctx-select .iconbtn,
  .touch-action-menu button,
  .uniform-scale-toggle { transition: none; }
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  .toolctx-select {
    background: #1f2937;
    box-shadow: 0 2px 8px rgb(0 0 0 / .3);
  }

  .toolctx-select .sep { background: #374151; }

  .toolctx-select .iconbtn { color: #d1d5db; }

  .toolctx-select .iconbtn:hover:not(:disabled) {
    background: #374151;
    color: #f9fafb;
  }

  .toolctx-select .iconbtn:active:not(:disabled) { background: #4b5563; }

  .stroke-width-ctrl {
    background: transparent;
    border-color: transparent;
  }

  .stroke-width-ctrl label,
  .stroke-width-ctrl span { color: #9ca3af; }

  .stroke-width-ctrl input[type="number"] {
    background: #1f2937;
    border-color: #4b5563;
    color: #f9fafb;
  }

  .touch-action-menu { background: #1f2937; }

  .touch-action-menu button {
    background: #374151;
    color: #d1d5db;
  }

  .touch-action-menu button:active { background: #4b5563; }

  .uniform-scale-toggle {
    background: #1f2937;
    border-color: #4b5563;
    color: #9ca3af;
  }

  .uniform-scale-toggle.active {
    background: #3b82f6;
    border-color: #3b82f6;
    color: #fff;
  }
}

/* Print */
@media print {
  .sel-box,
  .sel-handle,
  .sel-handle-hit,
  .sel-rotate-arm,
  .sel-rotate,
  .sel-marquee,
  .toolctx-select,
  .touch-action-menu,
  .uniform-scale-toggle,
  #selectionTooltip {
    display: none !important;
  }
}

/* Stage / overlay */
#overlayLayer {
  pointer-events: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#overlayLayer[pointer-events="all"] {
  pointer-events: all;
}

#stage {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body.dragging {
  -webkit-user-select: none;
  user-select: none;
  cursor: move;
}

/* States */
.sel-box.loading {
  opacity: .5;
  pointer-events: none;
}

.sel-box.error {
  stroke: #ef4444;
  stroke-width: 3;
  animation: sel-error-pulse 1s ease-in-out infinite;
}

@keyframes sel-error-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .5; }
}

.sel-box.success {
  stroke: #10b981;
  animation: sel-success-flash 0.5s ease-out;
}

@keyframes sel-success-flash {
  0%   { stroke-width: 2; }
  50%  { stroke-width: 4; filter: drop-shadow(0 0 8px rgb(16 185 129 / .5)); }
  100% { stroke-width: 2; }
}

/* Responsive sizes */
@media (max-width: 640px) {
  .toolctx-select {
    gap: 4px;
    padding: 6px 8px;
  }

  .toolctx-select .iconbtn {
    width: 32px;
    height: 32px;
    min-width: 32px;
  }

  .toolctx-select .iconbtn svg {
    width: 18px;
    height: 18px;
  }

  .stroke-width-ctrl { 
    flex: 1 1 140px;
    max-width: 260px;
    flex-wrap: wrap; 
  }

  .stroke-width-ctrl input[type="range"] { 
    /* still full-width within its smaller container */
  }

  .stroke-width-ctrl input[type="number"] { width: 45px; }

  .touch-action-menu {
    bottom: 70px;
    right: 16px;
    gap: 10px;
    padding: 10px;
  }

  .touch-action-menu button {
    width: 52px;
    height: 52px;
  }

  .uniform-scale-toggle {
    bottom: 70px;
    left: 16px;
    width: 52px;
    height: 52px;
  }
}

@media (min-width: 1920px) {
  .toolctx-select {
    padding: 10px 16px;
    gap: 10px;
  }

  .toolctx-select .iconbtn {
    width: 40px;
    height: 40px;
    min-width: 40px;
  }

  .toolctx-select .iconbtn svg {
    width: 22px;
    height: 22px;
  }

  .stroke-width-ctrl {
    flex: 1 1 260px;
    max-width: 520px;
  }
}