:root {
  --touch-target-min: 44px;
  --touch-target-comfortable: 48px;
  --touch-spacing: 8px;
  --touch-padding: 12px;
}

/* General */
.toolctx-shape,
.toolctx-shape * {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.toolctx-shape input {
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  user-select: text;
}

@supports (overscroll-behavior: contain) {
  .toolctx-shape {
    overscroll-behavior: contain;
  }
}

@supports (-webkit-touch-callout: none) {
  .toolctx-shape input[type="number"],
  .toolctx-shape input[type="range"] {
    font-size: 16px !important;
  }
}

.toolctx-shape button {
  -webkit-appearance: none;
  appearance: none;
}

.toolctx-shape button,
.toolctx-shape input {
  -webkit-tap-highlight-color: #fff0;
}

@supports (padding: max(0px)) {
  .toolctx-shape {
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
  }
}

/* Base Layout */
.toolctx-shape {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  min-height: 36px;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.4;
  animation: toolctx-fade-in 0.2s ease-out;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.toolctx-shape.touch-enabled {
  gap: 10px;
  padding: 6px 10px;
  min-height: var(--touch-target-min);
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .toolctx-shape {
    flex-wrap: wrap;
    gap: 6px;
  }

  .toolctx-shape.touch-enabled {
    gap: var(--touch-spacing);
  }
}

@media (max-width: 480px) {
  .toolctx-shape {
    font-size: 13px;
  }

  .toolctx-shape .hint {
    display: none;
  }
}

/* Labels */
.toolctx-shape .label {
  min-width: 54px;
  text-align: right;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary, #333);
  white-space: nowrap;
}

.toolctx-shape.touch-enabled .label {
  font-size: 15px;
  min-width: 60px;
}

/* Numeric inputs */
.toolctx-shape .num {
  width: 60px;
  height: 28px;
  padding: 4px 8px;
  border: 1px solid var(--border-color, #ccc);
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
  text-align: center;
  background-color: var(--input-bg, #fff);
  color: var(--text-primary, #333);
  transition: all 0.2s ease;
  touch-action: manipulation;
  -moz-appearance: textfield;
  appearance: textfield;
  -webkit-appearance: none;
}

.toolctx-shape.touch-enabled .num {
  width: 70px;
  height: var(--touch-target-min);
  font-size: 16px;
  padding: 8px 12px;
  border-width: 2px;
}

.toolctx-shape #shapeSidesInput,
.toolctx-shape #polygonSidesInput {
  width: 72px;
}

.toolctx-shape.touch-enabled #shapeSidesInput,
.toolctx-shape.touch-enabled #polygonSidesInput {
  width: 80px;
}

.toolctx-shape .num::-webkit-inner-spin-button,
.toolctx-shape .num::-webkit-outer-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}

.toolctx-shape .num:hover:not(:disabled) {
  border-color: var(--hover-border, #999);
}

.toolctx-shape .num:focus {
  outline: none;
  border-color: var(--focus-color, #0066cc);
  box-shadow: 0 0 0 3px var(--focus-ring, rgb(0 102 204 / .25));
}

.toolctx-shape.touch-enabled .num:focus {
  box-shadow: 0 0 0 4px var(--focus-ring, rgb(0 102 204 / .25));
  transform: scale(1.05);
}

.toolctx-shape .num:invalid {
  border-color: var(--error-color, #d32f2f);
  background-color: var(--error-bg, #ffebee);
}

.toolctx-shape .num:disabled {
  opacity: .5;
  cursor: not-allowed;
  background-color: var(--disabled-bg, #f5f5f5);
}

/* Slider */
.toolctx-shape .slider {
  width: 120px;
  height: 24px;
  cursor: pointer;
  accent-color: var(--accent-color, #0066cc);
  transition: opacity 0.2s ease;
  touch-action: pan-x;
  -webkit-appearance: none;
  appearance: none;
}

.toolctx-shape.touch-enabled .slider {
  width: 140px;
  height: 32px;
}

.toolctx-shape .slider::-webkit-slider-track {
  height: 4px;
  background: var(--slider-track, #ddd);
  border-radius: 2px;
}

.toolctx-shape.touch-enabled .slider::-webkit-slider-track {
  height: 6px;
}

.toolctx-shape .slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-color, #0066cc);
  border: 2px solid var(--slider-thumb-border, white);
  box-shadow: 0 2px 4px rgb(0 0 0 / .2);
  transition: transform 0.1s ease;
  cursor: pointer;
}

.toolctx-shape.touch-enabled .slider::-webkit-slider-thumb {
  width: 24px;
  height: 24px;
  border-width: 3px;
}

.toolctx-shape .slider::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}

.toolctx-shape .slider::-webkit-slider-thumb:active {
  transform: scale(1.2);
}

.toolctx-shape .slider::-moz-range-track {
  height: 4px;
  background: var(--slider-track, #ddd);
  border-radius: 2px;
}

.toolctx-shape.touch-enabled .slider::-moz-range-track {
  height: 6px;
}

.toolctx-shape .slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-color, #0066cc);
  border: 2px solid var(--slider-thumb-border, white);
  box-shadow: 0 2px 4px rgb(0 0 0 / .2);
  cursor: pointer;
}

.toolctx-shape.touch-enabled .slider::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border-width: 3px;
}

.toolctx-shape .slider:focus {
  outline: none;
}

.toolctx-shape .slider:focus::-webkit-slider-thumb {
  box-shadow: 0 0 0 3px var(--focus-ring, rgb(0 102 204 / .25));
}

.toolctx-shape.touch-enabled .slider:focus::-webkit-slider-thumb {
  box-shadow: 0 0 0 4px var(--focus-ring, rgb(0 102 204 / .25));
}

.toolctx-shape .slider:focus::-moz-range-thumb {
  box-shadow: 0 0 0 3px var(--focus-ring, rgb(0 102 204 / .25));
}

.toolctx-shape.touch-enabled .slider:focus::-moz-range-thumb {
  box-shadow: 0 0 0 4px var(--focus-ring, rgb(0 102 204 / .25));
}

.toolctx-shape .slider:disabled {
  opacity: .5;
  cursor: not-allowed;
}

/* Hints */
.toolctx-shape .hint {
  font-size: 12px;
  color: var(--text-muted, #666);
  margin-left: 8px;
  font-style: italic;
  opacity: .8;
  white-space: nowrap;
  transition: opacity 0.2s ease;
}

/* Shape type toggle */
.shape-type-toggle {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  background: #f0f2f5;
  border-radius: 8px;
  border: 1px solid #d9dfea;
}

.touch-enabled .shape-type-toggle {
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
  border-width: 2px;
}

/* Shape type buttons */
.shape-type-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: #fff0;
  cursor: pointer;
  transition: all 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
  -webkit-appearance: none;
  appearance: none;
}

.touch-enabled .shape-type-btn {
  min-width: var(--touch-target-min);
  min-height: var(--touch-target-min);
  width: var(--touch-target-min);
  height: var(--touch-target-min);
  border-radius: 8px;
}

.shape-type-btn:hover {
  background: rgb(255 255 255 / .6);
}

.shape-type-btn:active {
  transform: scale(.95);
}

.shape-type-btn.active {
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / .1);
}

.shape-type-btn.touch-active {
  background: rgb(0 102 204 / .1);
  transform: scale(.95);
}

.shape-type-btn svg {
  width: 18px;
  height: 18px;
  color: #6b7280;
  pointer-events: none;
}

.touch-enabled .shape-type-btn svg {
  width: 22px;
  height: 22px;
}

.shape-type-btn:hover svg {
  color: var(--ink, #333);
}

.shape-type-btn.active svg {
  color: var(--accent, #0066cc);
}

/* Shape submenu */
.shape-submenu {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(4px) scale(.95);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px;
  background: #fff;
  border: 1px solid #e6e8ee;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgb(0 0 0 / .12), 0 2px 8px rgb(0 0 0 / .08);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 100;
  margin-top: 4px;
  touch-action: manipulation;
}

.shape-submenu.touch-enabled {
  gap: 6px;
  padding: 6px;
  border-radius: 14px;
  border-width: 2px;
}

.shape-submenu.show {
  opacity: 1;
  transform: translateX(-50%) translateY(4px) scale(1);
  pointer-events: all;
}

/* Submenu buttons */
.shape-submenu-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid #fff0;
  background: #fff;
  cursor: pointer;
  transition: all 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
  -webkit-appearance: none;
  appearance: none;
}

.touch-enabled .shape-submenu-btn {
  min-width: 52px;
  min-height: 52px;
  width: 52px;
  height: 52px;
  border-radius: 10px;
  border-width: 2px;
}

.shape-submenu-btn:hover {
  background: #f0f2f5;
  border-color: #e6e8ee;
}

.shape-submenu-btn:active {
  transform: scale(.95);
}

.shape-submenu-btn.active {
  background: #f0f9ff;
  border-color: #0ea5e9;
}

.shape-submenu-btn.touch-active {
  background: #e0f2fe;
  transform: scale(.95);
}

.shape-submenu-btn svg {
  width: 20px;
  height: 20px;
  color: #5b657a;
  pointer-events: none;
}

.touch-enabled .shape-submenu-btn svg {
  width: 26px;
  height: 26px;
}

.shape-submenu-btn:hover svg {
  color: var(--ink, #333);
}

.shape-submenu-btn.active svg {
  color: #0ea5e9;
}

/* Input groups */
.toolctx-shape > div,
.toolctx-shape .input-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.toolctx-shape.touch-enabled > div,
.toolctx-shape.touch-enabled .input-group {
  gap: 6px;
}

/* Mode cycle button */
.mode-cycle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border-color, #ccc);
  border-radius: 6px;
  background: var(--input-bg, #fff);
  cursor: pointer;
  font-size: 16px;
  transition: all 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: #fff0;
  -webkit-appearance: none;
  appearance: none;
}

.touch-enabled .mode-cycle-btn {
  min-width: var(--touch-target-min);
  min-height: var(--touch-target-min);
  width: var(--touch-target-min);
  height: var(--touch-target-min);
  font-size: 18px;
}

.mode-cycle-btn:hover {
  background: var(--hover-bg, #f5f5f5);
  border-color: var(--hover-border, #999);
}

.mode-cycle-btn:active {
  transform: scale(.95);
}

.mode-cycle-btn:focus {
  outline: none;
  border-color: var(--focus-color, #0066cc);
  box-shadow: 0 0 0 3px var(--focus-ring, rgb(0 102 204 / .25));
}

/* Touch adjustments for stage */
#stage.touch-enabled {
  touch-action: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: contain;
}

.touch-enabled .shape-element,
#stage.touch-enabled rect,
#stage.touch-enabled circle,
#stage.touch-enabled ellipse,
#stage.touch-enabled polygon {
  stroke-width: inherit;
  touch-action: none;
}

/* Animations */
@keyframes toolctx-fade-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes touch-ripple {
  0% { transform: scale(.95); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* Accessibility adjustments */
@media (prefers-contrast: high) {
  .toolctx-shape .num { border-width: 2px; }
  .toolctx-shape .slider::-webkit-slider-thumb,
  .toolctx-shape .slider::-moz-range-thumb { border-width: 3px; }
  .toolctx-shape .slider:focus,
  .toolctx-shape .num:focus { outline-width: 3px; }
  .shape-type-toggle,
  .shape-submenu { border-width: 2px; }
}

@media (prefers-reduced-motion: reduce) {
  .toolctx-shape *,
  .shape-submenu *,
  .shape-type-btn,
  .shape-submenu-btn {
    transition: none !important;
    animation: none !important;
  }
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  .toolctx-shape {
    --text-primary: #e0e0e0;
    --text-muted: #999;
    --border-color: #555;
    --input-bg: #2a2a2a;
    --hover-bg: #3a3a3a;
    --hover-border: #777;
    --focus-color: #4da6ff;
    --focus-ring: rgba(77, 166, 255, 0.3);
    --error-color: #ff5252;
    --error-bg: #3d1919;
    --disabled-bg: #1a1a1a;
    --slider-track: #555;
    --slider-thumb-border: #1a1a1a;
    --accent-color: #4da6ff;
    --ink: #e0e0e0;
    --accent: #4da6ff;
  }

  .shape-type-toggle {
    background: #1a1f2e;
    border-color: #2a3441;
  }

  .shape-type-btn:hover {
    background: rgb    (255 255 255 / .1);
  }

  .shape-type-btn.active {
    background: #2a3441;
  }

  .shape-submenu {
    background: #1a1f2e;
    border-color: #2a3441;
  }

  .shape-submenu-btn {
    background: #1a1f2e;
  }

  .shape-submenu-btn:hover {
    background: #2a3441;
    border-color: #3a4451;
  }

  .shape-submenu-btn.active {
    background: #1a3a5a;
    border-color: #0ea5e9;
  }

  .mode-cycle-btn {
    background: #2a2a2a;
    border-color: #555;
  }

  .mode-cycle-btn:hover {
    background: #3a3a3a;
    border-color: #777;
  }
}

/* Medium devices - touch */
@media (min-width: 768px) and (max-width: 1024px) and (pointer: coarse) {
  .toolctx-shape {
    gap: 10px;
    padding: 8px 12px;
  }

  .toolctx-shape .num {
    height: 40px;
    font-size: 15px;
  }

  .shape-type-btn {
    min-width: 40px;
    min-height: 40px;
    width: 40px;
    height: 40px;
  }

  .shape-submenu-btn {
    min-width: 48px;
    min-height: 48px;
    width: 48px;
    height: 48px;
  }

  .mode-cycle-btn {
    min-width: 40px;
    min-height: 40px;
    width: 40px;
    height: 40px;
  }
}

/* Small touch devices */
@media (max-width: 767px) and (pointer: coarse) {
  .toolctx-shape {
    gap: var(--touch-spacing);
    padding: var(--touch-spacing);
  }

  .toolctx-shape .num {
    height: var(--touch-target-comfortable);
    font-size: 16px;
    width: 75px;
  }

  .toolctx-shape .slider {
    width: 100px;
  }

  .shape-type-btn {
    min-width: var(--touch-target-comfortable);
    min-height: var(--touch-target-comfortable);
    width: var(--touch-target-comfortable);
    height: var(--touch-target-comfortable);
  }

  .shape-submenu-btn {
    min-width: 56px;
    min-height: 56px;
    width: 56px;
    height: 56px;
  }

  .mode-cycle-btn {
    min-width: var(--touch-target-comfortable);
    min-height: var(--touch-target-comfortable);
    width: var(--touch-target-comfortable);
    height: var(--touch-target-comfortable);
  }
}

/* Landscape / compact view */
@media (max-height: 500px) and (orientation: landscape) {
  .toolctx-shape {
    gap: 4px;
    padding: 4px 8px;
    min-height: 36px;
  }

  .toolctx-shape .num {
    height: 36px;
  }

  .shape-type-btn,
  .shape-submenu-btn,
  .mode-cycle-btn {
    min-width: 36px;
    min-height: 36px;
    width: 36px;
    height: 36px;
  }
}

/* Print */
@media print {
  .toolctx-shape {
    display: none !important;
  }

  .shape-submenu {
    display: none !important;
  }
}