:root {
  --circuit-grid-dot: var(--border, #1e293b);
}

body.theme-dark {
  --circuit-grid-dot: var(--border, #1e293b);
}

body:not(.theme-dark) {
  --circuit-grid-dot: var(--border, #e2e8f0);
}

.circuit-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: var(--bg-main, #0b0f19);
  color: var(--text-primary, #f8fafc);
  overflow: hidden;
  position: relative;
  font-family: var(--font-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif);
  box-sizing: border-box;
}

/* 1. TOP SIMULATION TOOLBAR — Positioned inline next to tabs in the top header (No separate solid navbar) */
.circuit-toolbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 32px;
  background: transparent;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 0 244px; /* Leaves room on the left for [Blocks | C++ | Circuit] tabs */
  gap: 8px;
  flex-shrink: 0;
  z-index: 100;
  box-sizing: border-box;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}

.toolbar-divider {
  width: 1px;
  height: 18px;
  background: var(--border, rgba(255, 255, 255, 0.12));
  margin: 0 3px;
}

/* Prominent Start/Stop Simulation Button (Rounded Square Style) */
.sim-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 28px;
  padding: 0 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 700;
  border: none;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.btn-start {
  background: #10b981;
  color: #ffffff;
}

.btn-start:hover {
  background: #059669;
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(16, 185, 129, 0.35);
}

.btn-stop {
  background: #ef4444;
  color: #ffffff;
  animation: pulse-stop 1.5s infinite;
}

.btn-stop:hover {
  background: #dc2626;
}

@keyframes pulse-stop {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.6); }
  70% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

.status-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 12px;
  letter-spacing: 0.5px;
}

.status-stopped {
  background: rgba(148, 163, 184, 0.15);
  color: #94a3b8;
}

.status-running {
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.4);
}

.toolbar-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 26px;
  padding: 0 8px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 5px;
  color: var(--text-primary, #cbd5e1);
  cursor: pointer;
  font-size: 11.5px;
  font-weight: 600;
  transition: all 0.15s ease;
}

.toolbar-icon-btn:hover {
  background: var(--bg-hover, rgba(255, 255, 255, 0.08));
  border-color: var(--border, rgba(255, 255, 255, 0.12));
}

.btn-label {
  font-size: 11px;
  font-weight: 600;
}

/* Wire Color Dropdown */
.wire-color-picker-dropdown {
  position: relative;
}

.color-swatch-indicator {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.4);
}

.color-dropdown-menu {
  position: absolute;
  top: 30px;
  left: 0;
  background: var(--bg-sidebar, #1e293b);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.15));
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
  padding: 6px;
  display: none;
  flex-direction: column;
  gap: 2px;
  z-index: 50;
  min-width: 140px;
}

.color-dropdown-menu.show {
  display: flex;
}

.color-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-primary, #e2e8f0);
}

.color-option:hover {
  background: var(--bg-hover, rgba(255, 255, 255, 0.1));
}

.color-option .swatch {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

/* 2. MAIN BODY & icoding-STYLE SIDEBAR */
.circuit-main {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  margin-top: 0;
}

.circuit-left-column {
  position: absolute;
  top: 32px;
  left: 0;
  height: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
  pointer-events: none;
}

.circuit-icoding-sidebar {
  pointer-events: auto;
  width: 186px;
  background: var(--bg-sidebar, #0f172a);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.08));
  border-left: none;
  border-radius: 0 12px 12px 0;
  height: fit-content !important;
  max-height: calc(100% - 44px);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 15;
  padding: 6px 0 6px 0;
  box-sizing: border-box;
  box-shadow: 2px 4px 16px rgba(0, 0, 0, 0.25);
  transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

/* Sidebar Drag-to-Delete Dropzone Highlight (Desktop & Mobile - Scoped strictly to sidebar element itself) */
.circuit-icoding-sidebar.delete-dropzone-active {
  background: #dc2626 !important;
  border-color: #ef4444 !important;
  box-shadow: 0 0 28px rgba(239, 68, 68, 0.85) inset, 0 4px 20px rgba(220, 38, 38, 0.7) !important;
  transition: all 0.15s ease-in-out;
  position: relative;
  overflow: hidden;
}

body:not(.theme-dark) .circuit-icoding-sidebar.delete-dropzone-active {
  background: #dc2626 !important;
  border-color: #ef4444 !important;
}

/* Sidebar Trash Dropzone Overlay with Basket Icon & Label */
.sidebar-trash-dropzone {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background: #dc2626 !important;
  border-radius: inherit;
  z-index: 999;
  pointer-events: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  padding: 12px 6px;
  animation: trash-dropzone-appear 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}

.delete-dropzone-active > .sidebar-trash-dropzone,
.delete-dropzone-active.sidebar-trash-dropzone,
.delete-dropzone-active .sidebar-trash-dropzone {
  display: flex !important;
}

.trash-basket-icon-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  animation: trash-basket-bounce 0.6s ease infinite alternate;
}

.trash-basket-svg {
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.45));
  stroke: #ffffff !important;
  transition: transform 0.15s ease;
}

.trash-basket-label {
  font-size: 11px;
  font-weight: 800;
  color: #ffffff;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  line-height: 1.2;
}

@keyframes trash-dropzone-appear {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes trash-basket-bounce {
  from {
    transform: translateY(0) scale(1);
  }
  to {
    transform: translateY(-4px) scale(1.08);
  }
}

.circuit-mobile-tools-card {
  display: none; /* Desktop uses toolbar */
}

.sidebar-search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-main, #090e17);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.12));
  border-radius: 6px;
  padding: 6px 10px;
  margin: 4px 8px 10px 8px;
  transition: border-color 0.15s ease;
}

.sidebar-search-box:focus-within {
  border-color: var(--accent, #3b82f6);
}

.sidebar-search-box svg {
  color: var(--text-muted, #64748b);
  flex-shrink: 0;
}

.sidebar-search-box input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary, #ffffff);
  font-size: 13px;
  width: 100%;
}

.sidebar-search-box input::placeholder {
  color: var(--text-muted, #64748b);
}

.sidebar-categories-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.category-row {
  display: flex;
  align-items: center;
  margin: 3px 8px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.18s ease;
  user-select: none;
  box-sizing: border-box;
}

.category-row:hover {
  background-color: var(--bg-hover, rgba(255, 255, 255, 0.08));
}

.category-row.selected {
  background-color: color-mix(in srgb, var(--accent, #3b82f6) 16%, transparent);
}

.toolbox-category-icon-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 7px;
  margin-right: 10px;
  flex-shrink: 0;
  pointer-events: none !important;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.toolbox-category-icon-badge svg {
  width: 16px;
  height: 16px;
}

.mobile-search-row {
  display: none;
}

.category-label {
  font-family: var(--font-ui, sans-serif);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary, #cbd5e1);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.category-row.selected .category-label {
  color: var(--text-bright, #ffffff);
  font-weight: 700;
}

/* 3. SLIDING FLYOUT PALETTE PANEL (Cuts cleanly with rounded corners when categories finish) */
.circuit-flyout-panel {
  position: absolute;
  top: 32px;
  left: 186px;
  width: 220px;
  height: fit-content !important;
  max-height: calc(100% - 44px) !important;
  background: var(--bg-sidebar, #1e293b);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.12));
  border-left: none;
  border-radius: 0 12px 12px 0;
  box-shadow: 8px 0 24px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  z-index: 25;
  overflow: hidden;
  animation: slideRight 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideRight {
  from { opacity: 0; transform: translateX(-15px); }
  to { opacity: 1; transform: translateX(0); }
}

.flyout-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border, rgba(255, 255, 255, 0.08));
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 42px;
  box-sizing: border-box;
}

.flyout-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-bright, #ffffff);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex: 1;
  min-width: 0;
}

.flyout-search-container {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-main, #090e17);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.15));
  border-radius: 6px;
  padding: 4px 8px;
  width: 100%;
  box-sizing: border-box;
}

.flyout-search-container input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary, #ffffff);
  font-size: 12px;
  width: 100%;
}

.flyout-close-btn {
  background: transparent;
  border: none;
  color: var(--text-muted, #94a3b8);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
  flex-shrink: 0;
}

.flyout-close-btn:hover {
  background: var(--bg-hover, rgba(255, 255, 255, 0.1));
  color: #ffffff;
}

.flyout-content {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.flyout-items-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* Component Boxes as Rounded Squares */
.component-card {
  background: var(--bg-card, rgba(255, 255, 255, 0.04));
  border: 1px solid var(--border, rgba(255, 255, 255, 0.08));
  border-radius: 12px;
  aspect-ratio: 1 / 1;
  padding: 8px 4px 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
  text-align: center;
  overflow: hidden;
  position: relative;
}

.component-card:hover {
  background: var(--bg-hover, rgba(59, 130, 246, 0.14));
  border-color: #3b82f6;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.card-icon-wrapper {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  padding: 2px;
}

.card-icon-wrapper svg {
  max-width: 86%;
  max-height: 42px;
  display: block;
}

.card-name {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-primary, #cbd5e1);
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  padding: 0 2px;
  margin-top: 2px;
}

/* 4. CANVAS AREA & INTERACTIVE OVERLAYS */
#circuit-tab,
.circuit-container,
.circuit-main,
.circuit-canvas-wrapper {
  touch-action: none !important;
  overscroll-behavior: none !important;
  -webkit-overflow-scrolling: auto !important;
  -webkit-touch-callout: none !important;
  -webkit-user-select: none !important;
  user-select: none !important;
}

.circuit-canvas-wrapper {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-main, #0f172a);
  overflow: hidden;
  touch-action: none !important;
  -webkit-user-select: none;
  user-select: none;
}

.circuit-canvas-wrapper svg {
  touch-action: none !important;
}

.circuit-component {
  cursor: grab;
  touch-action: none !important;
}

.circuit-component.selected {
  cursor: grabbing;
}

.pin-hitbox {
  touch-action: none !important;
}

.wire-waypoint-handle {
  touch-action: none !important;
}

.circuit-wire {
  touch-action: none !important;
}

.terminal-pin:hover circle {
  filter: drop-shadow(0 0 4px #3b82f6);
}

/* Inspector Popover */
.inspector-popover {
  position: absolute;
  background: var(--bg-sidebar, #1e293b);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.15));
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 170px;
}

.popover-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted, #94a3b8);
  margin-bottom: 2px;
}

.popover-label {
  font-size: 11px;
  color: #cbd5e1;
}

.popover-close-btn {
  background: none;
  border: none;
  color: #94a3b8;
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  transition: color 0.15s ease;
}

.popover-close-btn:hover {
  color: #ffffff;
}

.popover-duplicate-btn {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 4px;
  color: #cbd5e1;
  font-size: 11px;
  cursor: pointer;
  padding: 2px 6px;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  transition: all 0.15s ease;
}

.popover-duplicate-btn:hover {
  background: rgba(255, 255, 255, 0.16);
  color: #ffffff;
}

.popover-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.popover-row input, .popover-row select {
  background: var(--bg-main, #0f172a);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.2));
  color: #ffffff;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 12px;
  outline: none;
}

.popover-row input:focus, .popover-row select:focus {
  border-color: #38bdf8;
}

.popover-range {
  accent-color: #38bdf8;
}

.popover-color-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.led-color-opt {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
}

.led-color-opt.active {
  border-color: #ffffff;
  box-shadow: 0 0 6px #ffffff;
}

/* HC-05 Bluetooth Popover Terminal */
.bt-terminal-log {
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 4px;
  padding: 6px;
  font-family: monospace;
  font-size: 11px;
  height: 80px;
  max-height: 80px;
  overflow-y: auto;
  color: #e2e8f0;
  margin-bottom: 6px;
  width: 185px;
}

.bt-msg-phone {
  color: #38bdf8;
  font-weight: bold;
}

.bt-msg-device {
  color: #4ade80;
  font-weight: bold;
}

.bt-msg-text {
  color: #e2e8f0;
}

.bt-quick-btn {
  padding: 2px 8px;
  font-size: 11px;
  font-weight: bold;
  background: #1e293b;
  color: #cbd5e1;
  border: 1px solid #475569;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.bt-quick-btn:hover {
  background: #334155;
  color: #ffffff;
}

.bt-quick-btn[data-cmd="1"] {
  color: #38bdf8;
}

.bt-cmd-input {
  flex: 1;
  padding: 4px 6px;
  font-size: 11px;
  background: #1e293b;
  border: 1px solid #475569;
  border-radius: 4px;
  color: #f8fafc;
  outline: none;
  width: 120px;
}

.bt-cmd-input:focus {
  border-color: #38bdf8;
}

.bt-cmd-input::placeholder {
  color: #64748b;
}

.bt-send-btn {
  padding: 4px 8px;
  font-size: 11px;
  font-weight: bold;
  background: #0284c7;
  color: #ffffff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.bt-send-btn:hover {
  background: #0369a1;
}

/* Short Circuit Toast */
.circuit-alert-toast {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #dc2626;
  color: #ffffff;
  font-size: 12px;
  font-weight: 600;
  padding: 8px 18px;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(220, 38, 38, 0.4);
  z-index: 45;
  animation: slide-down 0.25s ease;
}

@keyframes slide-down {
  from { transform: translate(-50%, -20px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

/* ─── LIGHT MODE COMPATIBILITY ─── */
body:not(.theme-dark) .circuit-container {
  background: var(--bg-main, #f8fafc);
  color: #1e293b;
}
body:not(.theme-dark) .circuit-toolbar {
  background: transparent;
}
body:not(.theme-dark) .toolbar-divider {
  background: #e2e8f0;
}
body:not(.theme-dark) .toolbar-icon-btn {
  color: #475569;
}
body:not(.theme-dark) .toolbar-icon-btn:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #0f172a;
}
body:not(.theme-dark) .circuit-icoding-sidebar {
  background: #ffffff;
  border-color: #e2e8f0;
}
body:not(.theme-dark) .sidebar-search-box {
  background: #f8fafc;
  border-color: #e2e8f0;
}
body:not(.theme-dark) .sidebar-search-box input {
  color: #1e293b;
}
body:not(.theme-dark) .category-row {
  color: #475569;
}
body:not(.theme-dark) .category-row:hover {
  background: #f1f5f9;
  color: #0f172a;
}
body:not(.theme-dark) .category-row.selected {
  background: rgba(16, 185, 129, 0.12);
  color: #0f172a;
}
body:not(.theme-dark) .category-row .category-label {
  color: #64748b !important;
}
body:not(.theme-dark) .category-row.selected .category-label {
  color: #0f172a !important;
  font-weight: 700 !important;
}
body:not(.theme-dark) .circuit-flyout-panel {
  background: #ffffff;
  border-color: #e2e8f0;
  box-shadow: 4px 12px 30px rgba(0, 0, 0, 0.12);
}
body:not(.theme-dark) .flyout-header {
  border-bottom-color: #e2e8f0;
  color: #0f172a;
}
body:not(.theme-dark) .flyout-title {
  color: #0f172a;
}
body:not(.theme-dark) .flyout-close-btn {
  color: #64748b;
}
body:not(.theme-dark) .flyout-close-btn:hover {
  background: #f1f5f9;
  color: #0f172a;
}
body:not(.theme-dark) .flyout-search-container {
  background: #f1f5f9;
  border-color: #e2e8f0;
}
body:not(.theme-dark) .flyout-search-container input {
  color: #0f172a;
}
body:not(.theme-dark) .component-card {
  background: #f8fafc;
  border-color: #e2e8f0;
}
body:not(.theme-dark) .component-card:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
body:not(.theme-dark) .card-name {
  color: #334155;
}
body:not(.theme-dark) .circuit-canvas-wrapper {
  background: #f1f5f9;
}
body:not(.theme-dark) .color-dropdown-menu {
  background: #ffffff;
  border-color: #e2e8f0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
body:not(.theme-dark) .color-option,
body:not(.theme-dark) .mobile-color-option {
  color: #1e293b;
}
body:not(.theme-dark) .color-option:hover,
body:not(.theme-dark) .mobile-color-option:hover {
  background: #f1f5f9;
}
body:not(.theme-dark) .inspector-popover {
  background: #ffffff;
  border-color: #e2e8f0;
  color: #1e293b;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
body:not(.theme-dark) .popover-title,
body:not(.theme-dark) .inspector-title {
  color: #0f172a;
  border-bottom-color: #e2e8f0;
}
body:not(.theme-dark) .popover-label {
  color: #334155;
}
body:not(.theme-dark) .popover-close-btn {
  color: #64748b;
}
body:not(.theme-dark) .popover-close-btn:hover {
  color: #0f172a;
}
body:not(.theme-dark) .popover-duplicate-btn {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #334155;
}
body:not(.theme-dark) .popover-duplicate-btn:hover {
  background: #e2e8f0;
  color: #0f172a;
}
body:not(.theme-dark) .popover-row input,
body:not(.theme-dark) .popover-row select,
body:not(.theme-dark) .inspector-field input,
body:not(.theme-dark) .inspector-field select {
  background: #ffffff !important;
  border-color: #cbd5e1 !important;
  color: #0f172a !important;
}
body:not(.theme-dark) .popover-row input:focus,
body:not(.theme-dark) .popover-row select:focus,
body:not(.theme-dark) .inspector-field input:focus,
body:not(.theme-dark) .inspector-field select:focus {
  border-color: #0284c7 !important;
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2) !important;
}
body:not(.theme-dark) .popover-row select option,
body:not(.theme-dark) .inspector-field select option {
  background: #ffffff !important;
  color: #0f172a !important;
}
body:not(.theme-dark) .led-color-opt.active {
  border-color: #0f172a !important;
  box-shadow: 0 0 6px rgba(15, 23, 42, 0.35) !important;
}
body:not(.theme-dark) .popover-range {
  accent-color: #0284c7;
}
body:not(.theme-dark) .bt-terminal-log {
  background: #f8fafc;
  border-color: #cbd5e1;
  color: #0f172a;
}
body:not(.theme-dark) .bt-msg-phone {
  color: #0284c7;
}
body:not(.theme-dark) .bt-msg-device {
  color: #16a34a;
}
body:not(.theme-dark) .bt-msg-text {
  color: #1e293b;
}
body:not(.theme-dark) .bt-quick-btn {
  background: #ffffff;
  border-color: #cbd5e1;
  color: #334155;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
body:not(.theme-dark) .bt-quick-btn:hover {
  background: #f1f5f9;
  border-color: #94a3b8;
  color: #0f172a;
}
body:not(.theme-dark) .bt-quick-btn[data-cmd="1"] {
  background: #f0f9ff;
  border-color: #7dd3fc;
  color: #0284c7;
}
body:not(.theme-dark) .bt-quick-btn[data-cmd="1"]:hover {
  background: #e0f2fe;
}
body:not(.theme-dark) .bt-cmd-input {
  background: #ffffff;
  border-color: #cbd5e1;
  color: #0f172a;
}
body:not(.theme-dark) .bt-cmd-input:focus {
  border-color: #0284c7;
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2);
}
body:not(.theme-dark) .bt-cmd-input::placeholder {
  color: #94a3b8;
}
body:not(.theme-dark) .bt-send-btn {
  background: #0284c7;
  color: #ffffff;
}
body:not(.theme-dark) .circuit-mobile-tools-card {
  background: #ffffff !important;
  border-color: #e2e8f0 !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
}
body:not(.theme-dark) .status-stopped {
  background: rgba(100, 116, 139, 0.12);
  color: #64748b;
}

/* ─── CIRCUIT CONTEXT MENU (LONG-PRESS DUPLICATE) ─── */
.circuit-context-menu {
  position: fixed;
  z-index: 10000;
  background: var(--bg-sidebar, #1e293b);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.15));
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
  min-width: 110px;
}
body:not(.theme-dark) .circuit-context-menu {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.context-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-primary, #f8fafc);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: all 0.15s ease;
}
body:not(.theme-dark) .context-menu-item {
  color: #1e293b;
}
.context-menu-item:hover {
  background: var(--accent, #10b981);
  color: #ffffff !important;
}
.context-menu-item svg {
  stroke: currentColor;
}

/* ==========================================
   MOBILE & TOUCH RESPONSIVE STYLES
   ========================================== */
@media (max-width: 768px) {
  /* Fluid Right-Aligned Inline Toolbar next to top tabs */
  .circuit-toolbar {
    position: absolute;
    top: 0;
    right: 8px;
    left: auto;
    max-width: calc(100% - 150px);
    height: 32px;
    padding: 0;
    background: transparent;
    pointer-events: none;
    border: none;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
    z-index: 102;
    box-sizing: border-box;
  }

  .desktop-only-toolbar {
    display: none !important;
  }

  .toolbar-left {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  #sim-toggle-btn {
    width: 30px !important;
    min-width: 30px !important;
    height: 28px !important;
    padding: 0 !important;
    border-radius: 8px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex-shrink: 0 !important;
  }
  #sim-toggle-btn svg {
    margin: 0 !important;
    flex-shrink: 0 !important;
  }
  #sim-toggle-btn #sim-btn-text,
  #sim-toggle-btn #sim-toggle-text {
    display: none !important; /* Always icon-only on mobile so it never overlaps tabs! */
  }
  #sim-status-badge {
    display: none !important;
  }

  /* Shortened Mobile Tools Row (Delete, Color, Rotate, Mirror) placed at top with minimal spacing */
  .circuit-mobile-tools-card {
    display: flex !important;
    flex-direction: row !important;
    gap: 10px !important; /* Clean minimal spacing between tool icons! */
    align-items: center !important;
    justify-content: center !important;
    pointer-events: auto !important;
    width: auto !important;
    min-width: unset !important;
    height: 28px !important;
    background: var(--bg-sidebar, #0f172a) !important;
    border: 1px solid var(--border, rgba(255, 255, 255, 0.12)) !important;
    border-radius: 8px !important;
    padding: 0 10px !important;
    box-sizing: border-box !important;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25) !important;
    flex-shrink: 0 !important;
  }

  .circuit-mobile-tools-card .toolbar-icon-btn {
    width: 22px !important;
    max-width: 22px !important;
    height: 22px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 4px !important;
    flex-shrink: 0 !important;
  }

  .circuit-mobile-tools-card .wire-color-picker-dropdown {
    width: 22px !important;
    display: flex !important;
    justify-content: center !important;
    flex-shrink: 0 !important;
  }

  .circuit-mobile-tools-card .color-dropdown-menu {
    left: auto !important;
    right: 0 !important;
    top: 100% !important;
    margin-top: 4px !important;
  }

  /* Main Workspace */
  .circuit-main {
    align-items: flex-start;
    margin-top: 0;
    height: 100%;
  }

  /* Category Sidebar: Attached flush to the left screen edge, NOT extending beyond categories */
  .circuit-left-column {
    position: absolute;
    top: 36px;
    left: 0;
    margin: 0 !important;
    padding: 0 !important;
    height: auto !important;
    max-height: calc(100% - 44px) !important;
    z-index: 20;
    pointer-events: none;
  }

  .circuit-icoding-sidebar {
    width: 76px !important;
    height: fit-content !important;
    max-height: calc(100% - 44px) !important;
    border-radius: 0 12px 12px 0 !important;
    border: 1px solid var(--border, rgba(255, 255, 255, 0.1)) !important;
    border-left: none !important;
    box-shadow: 2px 4px 16px rgba(0, 0, 0, 0.3) !important;
    padding: 6px 0 6px 0 !important;
    margin: 0 !important;
    pointer-events: auto;
  }

  .circuit-icoding-sidebar.delete-dropzone-active {
    background: #dc2626 !important;
    border-color: #ef4444 !important;
    pointer-events: auto !important;
  }
  .circuit-left-column:has(.delete-dropzone-active) {
    pointer-events: auto !important;
  }
  .circuit-icoding-sidebar .sidebar-trash-dropzone {
    padding: 8px 2px;
  }
  .circuit-icoding-sidebar .trash-basket-svg {
    width: 28px !important;
    height: 28px !important;
  }
  .circuit-icoding-sidebar .trash-basket-label {
    font-size: 9.5px !important;
    max-width: 68px;
    word-break: break-word;
  }

  .sidebar-search-box {
    display: none !important;
  }

  .mobile-search-row {
    display: flex !important;
  }

  .category-row {
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    margin: 3px 2px !important;
    padding: 6px 2px !important;
    border-radius: 8px !important;
    text-align: center !important;
  }

  .category-row .toolbox-category-icon-badge {
    margin-right: 0 !important;
    margin-bottom: 2px !important;
  }

  .category-row .category-label {
    display: block !important;
    font-size: 8.5px !important;
    margin-top: 4px !important;
    line-height: 1.2 !important;
    color: #cbbde2 !important;
    font-weight: 600 !important;
    text-align: center !important;
    width: 100% !important;
    white-space: normal !important;
    overflow: visible !important;
  }

  .category-row.selected .category-label {
    color: #ffffff !important;
    font-weight: 700 !important;
  }

  /* Compact flyout panel on mobile: starts flush right next to sidebar */
  .circuit-flyout-panel {
    left: 76px !important;
    width: 172px !important;
    max-width: 172px !important;
    top: 36px !important;
    margin-top: 0 !important;
    height: fit-content !important;
    max-height: calc(100% - 44px) !important;
    border-radius: 0 12px 12px 0 !important;
    border: 1px solid var(--border, rgba(255, 255, 255, 0.12)) !important;
    border-left: none !important;
    z-index: 50;
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.45);
  }

  .flyout-content {
    padding: 6px !important;
  }

  .flyout-items-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 6px !important;
  }

  /* Component boxes as rounded squares on mobile */
  .component-card {
    aspect-ratio: 1 / 1 !important;
    border-radius: 10px !important;
    padding: 6px 3px 4px 3px !important;
    min-height: unset !important;
  }

  .card-icon-wrapper svg {
    max-height: 34px !important;
  }

  .card-name {
    font-size: 8.5px !important;
  }

  /* Dynamic Mobile Sidebar Size Settings (Small / Medium) */
  body.sidebar-small .circuit-icoding-sidebar {
    width: 66px !important;
    padding: 4px 0 4px 0 !important;
  }
  body.sidebar-small .circuit-flyout-panel {
    left: 66px !important;
    width: 156px !important;
    max-width: 156px !important;
  }
  body.sidebar-small .category-row {
    margin: 2px 1px !important;
    padding: 4px 1px !important;
  }
  body.sidebar-small .category-label {
    font-size: 7.5px !important;
    margin-top: 2px !important;
  }
  body.sidebar-small .toolbox-category-icon-badge {
    width: 22px !important;
    height: 22px !important;
    min-width: 22px !important;
    border-radius: 5px !important;
  }
  body.sidebar-small .toolbox-category-icon-badge svg {
    width: 13px !important;
    height: 13px !important;
  }
}

@media (max-width: 600px) {
  .circuit-toolbar {
    right: 8px;
    gap: 8px;
    max-width: calc(100% - 145px);
  }
}

@media (max-width: 480px) {
  .circuit-toolbar {
    right: 6px;
    gap: 6px;
    max-width: calc(100% - 135px);
  }
  .circuit-mobile-tools-card {
    padding: 0 8px !important;
    gap: 8px !important; /* Spacious minimal design preserved! */
  }
  .circuit-mobile-tools-card .toolbar-icon-btn {
    width: 21px !important;
    max-width: 21px !important;
  }
  .circuit-mobile-tools-card .wire-color-picker-dropdown {
    width: 21px !important;
  }
}

@media (max-width: 380px) {
  .circuit-toolbar {
    right: 4px;
    gap: 5px;
    max-width: calc(100% - 125px);
  }
  #sim-toggle-btn {
    width: 26px !important;
    min-width: 26px !important;
    height: 26px !important;
    padding: 0 !important;
  }
  .circuit-mobile-tools-card {
    height: 26px !important;
    padding: 0 6px !important;
    gap: 6px !important;
  }
  .circuit-mobile-tools-card .toolbar-icon-btn {
    width: 19px !important;
    max-width: 19px !important;
  }
  .circuit-mobile-tools-card .wire-color-picker-dropdown {
    width: 19px !important;
  }
}

/* ─── DYNAMIC DESKTOP SIDEBAR SIZE VARIANTS ─── */
@media (min-width: 769px) {
  .circuit-mobile-tools-card {
    display: none !important;
  }
  body.sidebar-small .circuit-icoding-sidebar {
    width: 130px !important;
  }
  body.sidebar-small .circuit-flyout-panel {
    left: 130px !important;
  }
  body.sidebar-small .category-row {
    padding: 5px 6px !important;
    margin: 2px 4px !important;
  }
  body.sidebar-small .category-label {
    font-size: 11px !important;
  }
  body.sidebar-small .toolbox-category-icon-badge {
    width: 24px !important;
    height: 24px !important;
    min-width: 24px !important;
    margin-right: 6px !important;
  }
  body.sidebar-small .toolbox-category-icon-badge svg {
    width: 14px !important;
    height: 14px !important;
  }
}

/* ─── IR SENSOR OBSTACLE BARRIER ANIMATIONS ─── */
@keyframes ir-barrier-fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes ir-barrier-fade-out {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.ir-obstacle-barrier {
  will-change: transform, opacity;
}

@keyframes ir-beam-flow {
  from {
    stroke-dashoffset: 8;
  }
  to {
    stroke-dashoffset: 0;
  }
}

/* ─── TT GEAR MOTOR ROTATION KEYFRAMES (Dynamic D-shaft profile & rotating shades) ─── */
.gear-motor-shaft {
  transform-box: fill-box !important;
  transform-origin: center !important;
}

@keyframes shaft-scale-fwd {
  0% { transform: scaleY(1.0); }
  25% { transform: scaleY(0.72); }
  50% { transform: scaleY(0.46); }
  75% { transform: scaleY(0.72); }
  100% { transform: scaleY(1.0); }
}

@keyframes shaft-scale-rev {
  0% { transform: scaleY(1.0); }
  25% { transform: scaleY(0.72); }
  50% { transform: scaleY(0.46); }
  75% { transform: scaleY(0.72); }
  100% { transform: scaleY(1.0); }
}

@keyframes shaft-top-highlight-fwd {
  0% { opacity: 0.85; transform: translateY(0); }
  25% { opacity: 1.0; transform: translateY(0.5px); }
  50% { opacity: 0.25; transform: translateY(0); }
  75% { opacity: 0.05; transform: translateY(-0.5px); }
  100% { opacity: 0.85; transform: translateY(0); }
}

@keyframes shaft-top-highlight-rev {
  0% { opacity: 0.85; transform: translateY(0); }
  25% { opacity: 0.05; transform: translateY(-0.5px); }
  50% { opacity: 0.25; transform: translateY(0); }
  75% { opacity: 1.0; transform: translateY(0.5px); }
  100% { opacity: 0.85; transform: translateY(0); }
}

@keyframes shaft-bottom-shade-fwd {
  0% { opacity: 0.35; }
  25% { opacity: 0.95; }
  50% { opacity: 0.90; }
  75% { opacity: 0.15; }
  100% { opacity: 0.35; }
}

@keyframes shaft-bottom-shade-rev {
  0% { opacity: 0.35; }
  25% { opacity: 0.15; }
  50% { opacity: 0.90; }
  75% { opacity: 0.95; }
  100% { opacity: 0.35; }
}

@keyframes shaft-facet-roll-fwd {
  0% { opacity: 0.1; transform: translateY(0); }
  25% { opacity: 0.65; transform: translateY(1.5px); }
  50% { opacity: 0.9; transform: translateY(0); }
  75% { opacity: 0.65; transform: translateY(-1.5px); }
  100% { opacity: 0.1; transform: translateY(0); }
}

@keyframes shaft-facet-roll-rev {
  0% { opacity: 0.1; transform: translateY(0); }
  25% { opacity: 0.65; transform: translateY(-1.5px); }
  50% { opacity: 0.9; transform: translateY(0); }
  75% { opacity: 0.65; transform: translateY(1.5px); }
  100% { opacity: 0.1; transform: translateY(0); }
}

/* ─── FLOATING WORKSPACE CONTROLS (Center View & Zoom Controls, matching Image 4) ─── */
.circuit-floating-controls {
  position: absolute;
  top: 72px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 99;
}

.circuit-floating-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-sidebar, #ffffff);
  border: 1px solid var(--border, #cbd5e1);
  color: var(--text-muted, #64748b);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  outline: none;
  padding: 0;
}

.circuit-floating-btn:hover {
  background: var(--bg-hover, #f1f5f9);
  color: var(--accent, #7c3aed);
  border-color: var(--accent, #7c3aed);
  transform: scale(1.08);
}

.circuit-floating-btn:active {
  transform: scale(0.95);
}

body.theme-dark .circuit-floating-btn {
  background: #1e293b;
  border-color: #334155;
  color: #94a3b8;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

body.theme-dark .circuit-floating-btn:hover {
  background: #334155;
  color: #38bdf8;
  border-color: #38bdf8;
}
