/* ==========================================================================
   1. VARIABLES & THEMES
   ========================================================================== */
:root {
  --bg-color: #ffffff;
  --text-color: #0f172a;
  --panel-bg: #ffffff;
  --panel-border: #e2e8f0;
  --btn-bg: #ffffff;
  --btn-border: #cbd5e1;
  --btn-hover-bg: #f8fafc;
  --btn-hover-border: #94a3b8;
  --primary-color: #2563eb;
  --primary-hover: #1d4ed8;
  --danger-color: #ef4444;
  --success-color: #22c55e;
  --muted-text: #64748b;
  --subtle-bg: #f1f5f9;
  --shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
  --grid-dot-color: #cbd5e1;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --transition-fast: 0.15s ease;
}

body.dark-theme {
  --bg-color: #0f172a;
  --text-color: #f8fafc;
  --panel-bg: #1e293b;
  --panel-border: #334155;
  --btn-bg: #334155;
  --btn-border: #475569;
  --btn-hover-bg: #475569;
  --btn-hover-border: #64748b;
  --primary-color: #38bdf8;
  --primary-hover: #0284c7;
  --muted-text: #94a3b8;
  --subtle-bg: #0f172a;
  --shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.3);
  --grid-dot-color: #334155;
}

/* ==========================================================================
   2. BASE & GLOBAL
   ========================================================================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* iPad / touch: the browser itself must never zoom or scroll the page.
   Pinch on the workspace zooms the app instead (app.js bindCanvasInteractions). */
html, body {
  touch-action: none;
  overscroll-behavior: none;
}

/* Safari reopened the page zoomed in: allow pinching the page back out (app.js resetPageZoom). */
html.page-zoomed,
html.page-zoomed body,
html.page-zoomed #canvas-container {
  touch-action: manipulation;
}

/* Areas that scroll by themselves keep finger scrolling (no pinch zoom). */
.sidebar,
.help-tabs-bar,
.help-tabs,
.help-content-area,
#help-modal #help-tab-content {
  touch-action: pan-x pan-y;
}

/* Text fields still allow typing and selecting text. */
input[type="text"],
input[type="search"],
input[type="number"],
textarea {
  -webkit-user-select: text;
  user-select: text;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  width: 100vw;
  height: 100vh;
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color 0.2s ease, color 0.2s ease;
}

*:focus-visible {
  outline: 2px solid var(--primary-color);
  outline-offset: 2px;
}

/* Scrollbar Customization */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--btn-border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--muted-text);
}

/* ==========================================================================
   3. UI CONTROLS (Sidebar & Toolbar)
   ========================================================================== */
/* ===== Sidebar ===== */
.sidebar {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 100;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 190px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.sidebar h3 {
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 2px;
  color: var(--muted-text);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sidebar-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sidebar-group-title {
  font-size: 10px;
  font-weight: 700;
  color: var(--muted-text);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 6px 8px;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.sidebar-group-title::after {
  content: attr(data-count);
  font-weight: 600;
  opacity: 0.7;
}

.sidebar-group-title:hover,
.sidebar-group-title:focus-visible {
  color: var(--text-color);
  background: var(--btn-bg);
  outline: none;
}

.sidebar-group:not(.collapsed) > .sidebar-group-title {
  color: var(--primary-color);
  border-color: var(--primary-color);
}

.sidebar-group.collapsed > .add-module-btn {
  display: none;
}

/* Search box: while searching, matches show even in closed categories */
.sidebar-search {
  width: 100%;
  box-sizing: border-box;
  background: var(--btn-bg);
  color: var(--text-color);
  border: 1px solid var(--btn-border);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}

.sidebar-search:focus {
  border-color: var(--primary-color);
}

.sidebar-search::placeholder {
  color: var(--muted-text);
}

.sidebar.searching .sidebar-group.collapsed > .add-module-btn {
  display: block;
}

.sidebar.searching .sidebar-group > .add-module-btn.search-hidden,
.sidebar.searching .sidebar-group.search-empty {
  display: none;
}

.sidebar-no-results {
  font-size: 12px;
  color: var(--muted-text);
  margin: 0;
  padding-left: 2px;
}

.sidebar-no-results[hidden] {
  display: none;
}

.add-module-btn {
  background: var(--btn-bg);
  color: var(--text-color);
  border: 1px solid var(--btn-border);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  text-align: left;
}

.add-module-btn:hover {
  background: var(--btn-hover-bg);
  border-color: var(--btn-hover-border);
  transform: translateY(-1px);
}

.add-module-btn:active {
  transform: translateY(0);
}

/* ===== Toolbar ===== */
.toolbar {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 100;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  padding: 8px 14px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  gap: 12px;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.tool-btn {
  background: var(--btn-bg);
  border: 1px solid var(--btn-border);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  font-weight: 600;
  color: var(--text-color);
  transition: all var(--transition-fast);
}

.tool-btn:hover {
  background: var(--btn-hover-bg);
  border-color: var(--btn-hover-border);
}

.tool-btn:disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}

#undo-btn, #redo-btn {
  font-size: 16px;
  line-height: 1;
  padding: 4px 9px;
}

.danger-btn {
  background: var(--btn-bg);
  color: var(--danger-color);
  border-color: var(--btn-border);
}

.danger-btn:hover {
  background: rgba(239, 68, 68, 0.1);
  border-color: var(--danger-color);
}

/* ===== Draggable Panels (Sidebar & Toolbar) ===== */
.panel-drag-handle {
  cursor: move;
  user-select: none;
  touch-action: none;
}

.toolbar .panel-drag-handle {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted-text);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 6px 4px;
  border-right: 1px solid var(--panel-border);
  padding-right: 10px;
}

.panel-dragging {
  box-shadow: var(--shadow-md, 0 10px 25px rgba(0, 0, 0, 0.15));
  transition: none;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
}

/* ==========================================================================
   4. CANVAS & WORKSPACE
   ========================================================================== */
#canvas-container {
  width: 100%;
  height: 100%;
  position: relative;
  touch-action: none;
  background-color: var(--bg-color);
  background-image: radial-gradient(var(--grid-dot-color) 1.5px, transparent 1.5px);
  background-size: 20px 20px;
}

#connections-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 30;
}

/* Cables behind modules: toggled by the "Cables" toolbar button */
body.cables-behind #connections-canvas {
  z-index: 0;
}

#workspace-viewport {
  z-index: 1;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
}

#selection-box {
  position: absolute;
  border: 1px dashed var(--muted-text);
  background: rgba(100, 116, 139, 0.08);
  pointer-events: none;
  display: none;
  z-index: 50;
}

/* ==========================================================================
   5. MODULE CARDS & UI COMPONENTS
   ========================================================================== */
.module-card {
  position: absolute;
  min-width: 220px;
  width: 220px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.06);
  z-index: 20;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  resize: none;
  min-height: 180px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

/* Resize grip (app.js makeDraggable): works with mouse, finger and Apple Pencil */
.module-resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 22px;
  height: 22px;
  cursor: nwse-resize;
  touch-action: none;
  z-index: 5;
  background: linear-gradient(135deg, transparent 0 55%, var(--btn-border) 55% 62%, transparent 62% 72%, var(--btn-border) 72% 79%, transparent 79%);
  border-bottom-right-radius: var(--radius-lg);
}
.module-resize-handle:hover {
  background: linear-gradient(135deg, transparent 0 55%, var(--muted-text) 55% 62%, transparent 62% 72%, var(--muted-text) 72% 79%, transparent 79%);
}
body.presentation-mode .module-resize-handle {
  display: none;
}

.module-card.selected {
  box-shadow: 0 0 0 2px var(--muted-text);
}

.node-header {
  padding: 10px 14px;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--panel-border);
  cursor: move;
  background: var(--panel-bg);
  color: var(--text-color);
}

.delete-module-btn {
  background: var(--btn-bg);
  border: 1px solid var(--btn-border);
  border-radius: 4px;
  width: 22px;
  height: 22px;
  line-height: 18px;
  text-align: center;
  cursor: pointer;
  font-weight: bold;
  color: var(--muted-text);
  transition: all var(--transition-fast);
}

.delete-module-btn:hover {
  background: var(--btn-hover-bg);
  color: var(--danger-color);
  border-color: var(--danger-color);
}

.node-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  flex: 1;
}

.action-btn {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--btn-border);
  border-radius: var(--radius-sm);
  background-color: var(--btn-bg);
  color: var(--text-color);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  text-align: center;
}

.action-btn:hover {
  background-color: var(--btn-hover-bg);
  border-color: var(--btn-hover-border);
}

/* Ports & Connections Container */
.ports-container,
.granular-ports-row,
.ports-row,
.ports-matrix {
  margin-top: auto;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 10px 14px;
  background: var(--panel-bg);
  border-top: 1px solid var(--panel-border);
}

.port-group {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 700;
}

.port {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  cursor: crosshair;
  transition: transform 0.15s ease;
}

.port:hover {
  transform: scale(1.25);
}

.port-in {
  background: #64748b;
}

.port-out {
  background: #334155;
}

.dark-theme .port-out {
  background: #94a3b8;
}

/* Connected ports take the color of their cable (set as --cable-color by app.js) */
.port.connected,
.dark-theme .port.connected {
  background: var(--cable-color, #10b981);
  border-color: var(--cable-color, #10b981) !important;
  box-shadow: 0 0 0 2px var(--panel-bg), 0 0 0 4px var(--cable-color, #10b981);
}

/* ==========================================================================
   6. SPECIFIC MODULES
   ========================================================================== */
.module-card:has(.granular-body) {
  width: 500px;
}

.granular-body {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 16px;
  padding: 14px;
  background: var(--panel-bg);
}

.granular-left-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.upload-btn {
  background: var(--btn-bg);
  color: var(--text-color);
  border: 1px solid var(--btn-border);
  padding: 8px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.upload-btn:hover {
  background: var(--btn-hover-bg);
}

.cv-routing-section {
  background: var(--subtle-bg);
  border: 1px solid var(--panel-border);
  padding: 8px;
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cv-routing-section label {
  font-size: 11px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.control-select {
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--btn-border);
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 11px;
}

.granular-right-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 14px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.control-group label {
  font-size: 11px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.control-group input[type="range"] {
  width: 100%;
  accent-color: var(--muted-text);
}

/* Audio Input VU Meter */
.vu-meter-container {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.vu-meter-bg {
  flex: 1;
  height: 8px;
  background-color: var(--subtle-bg);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--panel-border);
}

.vu-meter-bar {
  height: 100%;
  width: 0%;
  background-color: var(--success-color);
  transition: width 0.05s linear, background-color 0.1s ease;
  border-radius: 4px;
}

.mic-toggle-btn.active {
  background-color: var(--danger-color) !important;
  color: #ffffff !important;
  border-color: var(--danger-color) !important;
}

/* ==========================================================================
   7. HELP & PRESETS MODAL (CLEAN & FIXED LAYOUT)
   ========================================================================== */
#help-btn {
  font-weight: bold;
  font-size: 16px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.help-modal-overlay {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.7);
  backdrop-filter: blur(6px);
  z-index: 10000;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.help-modal-overlay.hidden {
  display: none !important;
}

.help-modal-container {
  width: 100%;
  max-width: 880px;
  height: 82vh;
  max-height: 720px;
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Header */
.help-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--subtle-bg);
}

.help-title-area h2 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.help-close-btn {
  background: transparent;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--muted-text);
  line-height: 1;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all var(--transition-fast);
}

.help-close-btn:hover {
  color: var(--text-color);
  background: var(--btn-hover-bg);
}

/* Search Bar */
.modal-search-bar {
  padding: 12px 24px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
}

.modal-search-bar input {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--btn-border);
  background: var(--subtle-bg);
  font-size: 0.92rem;
  color: var(--text-color);
  outline: none;
  box-sizing: border-box;
}

.modal-search-bar input:focus {
  border-color: var(--primary-color);
}

/* Horizontal Nav Tabs Bar */
#help-nav-tabs,
.help-tabs-bar,
.help-tabs {
  display: flex;
  flex-direction: row;
  width: 100%;
  background: var(--subtle-bg);
  border-bottom: 1px solid var(--panel-border);
  border-right: none;
  padding: 8px 24px 0 24px;
  gap: 6px;
  overflow-x: auto;
  box-sizing: border-box;
}

.help-tab-btn {
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted-text);
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 6px 6px 0 0;
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--transition-fast);
  width: auto;
  text-align: center;
}

.help-tab-btn:hover {
  color: var(--text-color);
  background: rgba(226, 232, 240, 0.5);
}

.help-tab-btn.active {
  color: var(--primary-color);
  background: var(--panel-bg);
  border-bottom-color: var(--primary-color);
}

/* Main Content Area */
#help-tab-content,
.help-content-area {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  line-height: 1.6;
  color: var(--text-color);
  background: var(--panel-bg);
}

.help-content-area h2,
.help-content-area h3 {
  margin-top: 0;
  margin-bottom: 10px;
  color: var(--text-color);
}

.help-content-area p {
  margin-bottom: 16px;
  color: var(--muted-text);
}

/* Grids & Cards inside Modal */
.help-grid,
.modules-help-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.help-card,
.help-module-box {
  background: var(--subtle-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 16px;
}

.help-card h3, .help-card h4,
.help-module-box h3 {
  margin-top: 0;
  margin-bottom: 8px;
  font-size: 1rem;
  color: var(--text-color);
}

.help-card ul, .help-card ol {
  padding-left: 20px;
  padding-right: 20px;
}

.load-preset-btn {
  margin-top: 12px;
  width: 100%;
  padding: 8px 14px;
  background: var(--primary-color);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.load-preset-btn:hover {
  background: var(--primary-hover);
}

[dir="rtl"] .help-content-area {
  text-align: right;
}

/* ==========================================================================
   8. PRESENTATION / PLAY MODE
   ========================================================================== */
body.presentation-mode .port,
body.presentation-mode .delete-module-btn,
body.presentation-mode .close-btn,
body.presentation-mode .sidebar,
body.presentation-mode #selection-box {
  display: none !important;
  pointer-events: none;
}

body.presentation-mode .node-header {
  cursor: default;
}

#presentation-mode-btn.active {
  background-color: var(--success-color);
  color: #ffffff;
  border-color: var(--success-color);
}

#cable-layer-btn.active {
  background-color: var(--primary-color);
  color: #ffffff;
  border-color: var(--primary-color);
}
/* App notification (e.g. "Patch loaded"): small card at the bottom center, matches the panels */
.app-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translate(-50%, 8px);
  max-width: min(520px, calc(100vw - 32px));
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--panel-border);
  border-left: 3px solid var(--primary-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  direction: ltr;
  z-index: 99999;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}
.app-toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Jack hint card (portGuide.js): shown when hovering a jack, follows the Tooltips ON/OFF button */
.port-hint {
  position: fixed;
  display: none;
  width: 250px;
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.45;
  direction: ltr;
  text-align: left;
  z-index: 100000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.port-hint.visible { opacity: 1; }
.port-hint-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
}
.port-hint-head strong { font-size: 13px; }
.port-hint-dir {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted-text);
}
.port-hint-where {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--panel-border);
}
.port-hint-where span {
  display: block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--primary-color);
}

/* While dragging a cable: matching jacks light up, recommended ones pulse, the rest fade */
body.cable-dragging .port.port-incompatible {
  opacity: 0.25;
}
body.cable-dragging .port.port-compatible {
  box-shadow: 0 0 0 2px var(--panel-bg), 0 0 0 4px var(--muted-text);
}
body.cable-dragging .port.port-suggested {
  box-shadow: 0 0 0 2px var(--panel-bg), 0 0 0 5px var(--primary-color), 0 0 12px 5px var(--primary-color);
  animation: port-suggest-pulse 1s ease-in-out infinite;
}
@keyframes port-suggest-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.35); }
}

/* Always-visible jack labels (portGuide.js): a small tag under every jack saying IN or OUT,
   the signal kind, and where it usually connects. Drawn in #port-chip-layer above the canvas
   (below the Sidebar/Toolbar). Hidden when Tooltips is OFF, in Play Mode and while Help is open. */
#port-chip-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 90;
  overflow: hidden;
}
.port-chip {
  position: fixed;
  max-width: 92px;
  padding: 2px 5px 3px;
  border-radius: 4px;
  font-size: 9px;
  font-weight: 500;
  line-height: 1.2;
  text-align: center;
  pointer-events: none;
  box-shadow: var(--shadow-sm);
  direction: ltr;
}
.port-chip b {
  display: block;
  font-size: 8.5px;
  font-weight: 800;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.port-chip-where { display: block; }
.port-chip.compact .port-chip-where,
.port-chip.tiny .port-chip-where,
.port-chip.tiny .port-chip-sig { display: none; }
.port-chip.tiny { padding: 1px 4px; }
.port-chip-in {
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--muted-text);
}
.port-chip-out {
  background: var(--primary-color);
  color: #ffffff;
  border: 1px solid var(--primary-color);
}
.dark-theme .port-chip-out { color: #0f172a; }
/* LFO wave preview (theme-aware) */
.lfo-preview {
  display: block;
  width: 100%;
  height: 36px;
  background: var(--subtle-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
}

/* LFO to slider modulation (modulation.js): strip under a modulated slider showing how far it moves */
.knob-mod-band {
  position: absolute;
  height: 3px;
  border-radius: 2px;
  opacity: 0.85;
  pointer-events: none;
}

/* While a cable is dragged from LFO OUT, sliders it can be dropped on are outlined */
body.cable-dragging .module-card input[type="range"].knob-droppable {
  outline: 2px dashed var(--primary-color);
  outline-offset: 3px;
  border-radius: 4px;
}

/* ==========================================================================
   Modules built on the shared template (ModuleBase.js)
   ========================================================================== */
.module-base-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.module-label {
  font-size: 11px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.module-base-ports {
  margin-top: auto;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding-top: 8px;
  border-top: 1px solid var(--panel-border);
}

.module-base-ports .port-group {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 700;
}

.module-toggle.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #fff;
}

/* Ribbon */
.ribbon-strip {
  width: 100%;
  height: 100px;
  border-radius: var(--radius-md, 8px);
  border: 1px solid var(--panel-border);
  touch-action: none;
  cursor: pointer;
  display: block;
}

.ribbon-readout {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 700;
}

.module-toggle-row {
  display: flex;
  gap: 8px;
}

/* Drum Machine (DrumMachineModule.js) */
.dm-top {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--panel-border);
}

.dm-top .dm-play {
  width: 90px;
  flex: none;
}

.dm-play.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #fff;
}

.dm-tempo { flex: 2; }
.dm-top > .dm-knob:not(.dm-tempo) { flex: 1; }

.dm-bpm-box {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.dm-bpm {
  width: 52px;
  padding: 2px 4px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  background: var(--panel-bg);
  color: var(--text-color);
}

.dm-bars {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: none;
}

.dm-bar-btns {
  display: flex;
  gap: 6px;
}

.dm-small-btn {
  width: auto;
  padding: 4px 10px;
  font-size: 11px;
}

.dm-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--panel-border);
}

.dm-row-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px; /* room for the always-visible jack labels under TRIG / OUT */
}

.dm-row-name {
  font-size: 13px;
  font-weight: 700;
  width: 60px;
}

.dm-sound {
  width: 130px;
}

.dm-row-jacks {
  margin-left: auto;
  display: flex;
  gap: 18px;
}

.dm-row-jacks .port-group {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 700;
}

.dm-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  max-width: 712px;
}

.dm-bar {
  display: flex;
  gap: 3px;
}

.dm-step {
  width: 18px;
  height: 26px;
  padding: 0;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  background: var(--btn-bg, transparent);
  cursor: pointer;
  touch-action: manipulation;
}

.dm-step.dm-beat:not(:first-child) {
  margin-left: 4px;
}

.dm-step.dm-beat {
  border-color: var(--muted-text);
}

.dm-step.dm-on {
  background: color-mix(in srgb, var(--primary-color) 50%, transparent);
  border-color: var(--primary-color);
}

.dm-step.dm-accent {
  background: var(--primary-color);
  border-color: var(--primary-color);
  box-shadow: inset 0 0 0 3px color-mix(in srgb, #fff 45%, var(--primary-color));
}

.dm-step.dm-now {
  outline: 2px solid var(--text-color);
  outline-offset: 1px;
}

.dm-knobs,
.dm-custom {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.dm-custom[hidden] {
  display: none;
}

.dm-custom {
  padding: 8px;
  border: 1px dashed var(--panel-border);
  border-radius: var(--radius-sm);
}

.dm-knob {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 120px;
  min-width: 110px;
}

.dm-knobs[hidden] {
  display: none;
}

.dm-small-btn.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #fff;
}

/* Stereo Delay (StereoDelayModule.js) */
.sd-display {
  width: 100%;
  height: 120px;
  display: block;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  background: var(--subtle-bg, transparent);
  touch-action: none;
  cursor: grab;
}

.sd-hint {
  font-size: 10px;
  color: var(--muted-text);
  margin-top: -4px;
}

.sd-grid {
  display: grid;
  grid-template-columns: 64px 1fr 1fr 1fr;
  gap: 10px;
  align-items: end;
}

.sd-grid[hidden] {
  display: none;
}

.sd-head span {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted-text);
  text-align: center;
}

.sd-name {
  font-size: 11px;
  font-weight: 600;
  padding-bottom: 4px;
}

.sd-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.sd-cell input[type="range"] {
  width: 100%;
}

.sd-cell input[type="range"]:disabled {
  opacity: 0.4;
}

.sd-readout {
  font-size: 10px;
  font-weight: 600;
  text-align: center;
}

.sd-cell .control-select {
  font-size: 11px;
  padding: 2px 4px;
}

.sd-toggle {
  width: 100%;
}

.sd-bottom {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sd-bottom .sd-toggle {
  width: auto;
}

.sd-clock {
  margin-left: auto;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted-text);
}
/* Metronome (MetronomeModule.js) */
.metro-top {
  display: flex;
  gap: 8px;
}

.metro-top .action-btn { flex: 1; }

.metro-play.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #fff;
}

.metro-lights {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  padding: 8px 0;
}

.metro-light {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--panel-border);
  background: var(--panel-bg);
  transition: background 0.05s;
}

.metro-light.metro-first { border-color: var(--primary-color); }
.metro-light.metro-now { background: var(--text-color); }
.metro-light.metro-first.metro-now { background: var(--primary-color); }

/* Wavetable Osc (WavetableModule.js): the stacked-waves display */
.wt-display {
  width: 100%;
  height: 120px;
  display: block;
  margin-bottom: 10px;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  background: var(--subtle-bg, transparent);
}

/* "New version" notice (pwa.js): stays until Refresh or Later, bottom right */
.update-notice {
  position: fixed;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  right: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
  background: var(--panel-bg);
  color: var(--text-color);
  border: 1px solid var(--panel-border);
  border-left: 3px solid var(--primary-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 8px 10px 8px 16px;
  font-size: 13px;
  font-weight: 500;
  direction: ltr;
  z-index: 100000;
}
.update-notice .update-notice-refresh {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #ffffff;
}
.wt-display.wt-drawing {
  cursor: crosshair;
  touch-action: none;
  margin-bottom: 4px;
}
.wt-hint {
  font-size: 11px;
  color: var(--muted-text);
  margin-bottom: 10px;
}
.wt-hint[hidden] { display: none; }

/* Poly Synth (PolySynthModule.js): one numbered cell per voice, lit while it holds a note */
.poly-voices {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin-bottom: 4px;
}
.poly-voice {
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 0;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  color: var(--muted-text);
  transition: background 0.05s, color 0.05s;
}
.poly-voice.on {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #ffffff;
}
.poly-voice.off { opacity: 0.3; }
