* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background-color: #f5f5f5;
  height: 100dvh;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button,
.tab,
.file-item,
.menu-item,
.menu-toggle {
  touch-action: manipulation;
}

.container {
  display: flex;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

.sidebar-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid #cbd5e0;
  background: white;
  color: #2d3748;
  border-radius: 8px;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.sidebar-toggle:hover {
  background: #f7fafc;
}

.sidebar-toggle .sidebar-toggle-icon {
  display: inline-block;
  transition: transform 0.15s ease;
}

/* Flip the chevron when the file browser is hidden. */
.sidebar-toggle[aria-expanded="false"] .sidebar-toggle-icon {
  transform: rotate(180deg);
}

.sidebar-backdrop {
  display: none;
}

/* Editor-header dropdown menu (Home + run options). */
.header-menu {
  position: relative;
}

.menu-toggle {
  list-style: none;
  cursor: pointer;
  padding: 0.45rem 0.7rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  background: white;
  color: #4a5568;
  font-size: 1.1rem;
  line-height: 1;
  user-select: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 36px;
}

.menu-toggle::-webkit-details-marker {
  display: none;
}

.menu-toggle::marker {
  display: none;
  content: '';
}

.menu-toggle:hover {
  background: #f7fafc;
  border-color: #cbd5e0;
}

.header-menu[open] > .menu-toggle {
  background: #edf2f7;
  border-color: #cbd5e0;
}

.menu-content {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: white;
  border: 1px solid #cbd5e0;
  border-radius: 8px;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.18);
  z-index: 60;
  min-width: 240px;
  max-width: min(85vw, 320px);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.55rem 0.7rem;
  border-radius: 6px;
  text-decoration: none;
  color: #2d3748;
  font-size: 0.9rem;
  cursor: pointer;
  white-space: nowrap;
}

.menu-item:hover {
  background: #f7fafc;
}

.menu-checkbox input[type="checkbox"] {
  margin: 0;
}

.menu-separator {
  height: 1px;
  background: #e2e8f0;
  margin: 0.35rem 0.2rem;
}

.menu-section-label {
  padding: 0.35rem 0.7rem 0.2rem;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: #94a3b8;
}

.menu-note {
  padding: 0.4rem 0.7rem;
  font-size: 0.78rem;
  color: #94a3b8;
  font-style: italic;
}

/* Buttons styled as menu items (Workspace actions, etc.). */
.menu-action {
  appearance: none;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  font: inherit;
  color: #2d3748;
}

.menu-action:hover,
.menu-action:focus-visible {
  background: #f1f5f9;
  outline: none;
}

.menu-action-danger {
  color: #b91c1c;
}

.menu-action-danger:hover,
.menu-action-danger:focus-visible {
  background: #fee2e2;
}

/* Sidebar */
.sidebar {
  width: 300px;
  background-color: #2d3748;
  color: white;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid #4a5568;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h3 {
  font-size: 1.1rem;
  font-weight: 600;
}

.sidebar-actions {
  display: flex;
  gap: 0.5rem;
}

.sidebar-actions button {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  padding: 0.25rem;
  border-radius: 4px;
  font-size: 1rem;
  transition: background-color 0.2s;
}

.sidebar-actions button:hover {
  background-color: #4a5568;
}

.file-tree {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.file-item {
  padding: 0.5rem;
  cursor: pointer;
  border-radius: 4px;
  margin-bottom: 2px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: background-color 0.2s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.file-item:hover {
  background-color: #4a5568;
}

.file-item.selected {
  background-color: #3182ce;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.22);
}

.file-item.drag-target {
  background-color: #2b6cb0;
  outline: 1px dashed #90cdf4;
}

.file-item.dragging-file {
  opacity: 0.55;
}

.file-item.directory {
  font-weight: 600;
}

.file-icon {
  font-size: 1rem;
}

.loading {
  padding: 1rem;
  text-align: center;
  color: #a0aec0;
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: white;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.editor-header {
  padding: 1rem;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #f7fafc;
}

.file-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.runtime-hint {
  font-size: 0.75rem;
  color: #718096;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 0.2rem 0.45rem;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.workspace-badge-label {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  max-width: 240px;
}

#current-file {
  font-weight: 600;
  color: #2d3748;
}

.save-status {
  font-size: 0.875rem;
  color: #718096;
}

.save-status.saved {
  color: #38a169;
}

.save-status.unsaved {
  color: #e53e3e;
}

.editor-actions {
  display: flex;
  gap: 0.5rem;
}

.editor-actions button {
  padding: 0.5rem 1rem;
  border: 1px solid #e2e8f0;
  background-color: white;
  color: #4a5568;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.2s;
}

.icon-btn {
  font-size: 1rem;
  line-height: 1;
  padding: 0.5rem 0.75rem;
  min-width: 40px;
}

#run-btn.icon-btn {
  color: #2f855a;
}

#stop-btn.icon-btn {
  color: #c53030;
}

.editor-actions button:hover:not(:disabled) {
  background-color: #f7fafc;
  border-color: #cbd5e0;
}

.editor-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.editor-actions button:not(:disabled):active {
  background-color: #edf2f7;
}

.editor-container {
  flex: 1;
  position: relative;
  min-height: 0;
  overflow: hidden;
}


.hidden {
  display: none !important;
}

.completion-dropdown {
  position: absolute;
  z-index: 30;
  display: none;
  min-width: 220px;
  max-width: 420px;
  max-height: 220px;
  overflow-y: auto;
  background: #ffffff;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
}

.completion-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
}

.completion-item:hover,
.completion-item.active {
  background: #ebf8ff;
}

.completion-name {
  color: #1a202c;
}

.completion-type {
  color: #718096;
  font-size: 12px;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid #e2e8f0;
  background: #f8fafc;
  overflow-x: auto;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.6rem;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  background: white;
  color: #2d3748;
  cursor: pointer;
  white-space: nowrap;
}

.tab.active {
  border-color: #3182ce;
  background: #ebf8ff;
}

.tab-title {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-close {
  border: none;
  background: transparent;
  color: #718096;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

.tab-close:hover {
  color: #e53e3e;
}

#editor {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 14px;
  line-height: 1.5;
  padding: 0;
  resize: none;
}

/* CodeMirror 6 styling */
.cm-editor {
  height: 100%;
  font-size: 14px;
  line-height: 1.5;
}

/* Pin line-number gutter hard to the left edge of the editor pane. */
.cm-editor .cm-gutters {
  border-right: 1px solid #e2e8f0;
  background: #f7fafc;
  padding-left: 0;
}

.cm-editor .cm-content {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.cm-focused {
  outline: none;
}

.console-container {
  height: 220px;
  border-top: 1px solid #e2e8f0;
  display: flex;
  flex-direction: column;
  background: #0f172a;
  flex-shrink: 0;
}

.console-container.is-collapsed {
  height: auto;
}

.console-container.is-collapsed .console-output {
  display: none;
}

.led-sim-panel {
  border-top: 1px solid #e2e8f0;
  background: #111827;
  color: #e5e7eb;
  padding: 0.75rem;
}

.led-sim-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}

.led-sim-header h3 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.led-sim-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.led-sim-actions button {
  padding: 0.3rem 0.65rem;
  border: 1px solid #374151;
  background: #1f2937;
  color: #e5e7eb;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.led-sim-actions button:hover:not(:disabled) {
  background: #243244;
}

.led-sim-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.led-meta {
  margin-top: 0.5rem;
  color: #9ca3af;
  font-size: 0.82rem;
}

.led-grid {
  margin-top: 0.6rem;
  display: flex;
  flex-wrap: wrap;
  overflow-x: hidden;
  overflow-y: auto;
  gap: 8px;
  width: 100%;
  max-height: 132px;
  padding: 0.1rem 0 0.2rem;
}

.led-grid.panel-mode {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 24px));
  grid-auto-rows: 24px;
  justify-content: start;
  align-content: start;
  gap: 6px;
  max-height: 470px;
  overflow: auto;
}

.led {
  width: 24px;
  height: 24px;
  min-width: 24px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.45);
}

.pin-panel {
  border-top: 1px solid #e2e8f0;
  background: #0b1220;
  color: #e5e7eb;
  padding: 0.55rem 0.75rem 0.7rem;
}

.pin-panel.hidden {
  display: none;
}

.pin-panel-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.45rem;
}

.pin-panel-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: #f3f4f6;
}

.pin-panel-hint {
  font-size: 0.7rem;
  color: #94a3b8;
}

.pin-rows {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-height: 26vh;
  overflow-y: auto;
}

.pin-row {
  display: grid;
  grid-template-columns: 70px 24px 56px 1fr 90px;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.8rem;
}

.pin-row-label {
  color: #cbd5e1;
  font-variant-numeric: tabular-nums;
}

.pin-led {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #1e293b;
  border: 1px solid #334155;
  box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.6);
  justify-self: center;
}

.pin-led.on {
  background: #fde047;
  border-color: #facc15;
  box-shadow: 0 0 10px rgba(253, 224, 71, 0.7), inset 0 0 4px rgba(0, 0, 0, 0.25);
}

.pin-toggle {
  appearance: none;
  border: 1px solid #334155;
  background: #1e293b;
  color: #e5e7eb;
  border-radius: 6px;
  padding: 0.2rem 0;
  font-family: 'SFMono-Regular', Menlo, monospace;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: center;
  /* iOS Safari swallows taps on small buttons inside scrollable parents
   * unless `touch-action: manipulation` is set (kills the 300ms double-tap
   * zoom delay and the "is this a scroll start?" hesitation). */
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(56, 189, 248, 0.25);
  user-select: none;
}

.pin-toggle.on {
  background: #16a34a;
  border-color: #15803d;
  color: #f0fdf4;
}

.pin-pwm-bar {
  position: relative;
  height: 10px;
  background: #1e293b;
  border-radius: 999px;
  border: 1px solid #334155;
  overflow: hidden;
}

.pin-pwm-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #38bdf8, #818cf8);
  transition: width 80ms linear;
}

.pin-row-detail {
  text-align: right;
  font-size: 0.72rem;
  color: #94a3b8;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 600px) {
  .pin-row {
    grid-template-columns: 60px 22px 50px 1fr 70px;
    gap: 0.4rem;
    font-size: 0.75rem;
  }
  .pin-row-detail {
    font-size: 0.68rem;
  }
}

.adc-panel {
  border-top: 1px solid #e2e8f0;
  background: #0f172a;
  color: #e5e7eb;
  padding: 0.6rem 0.75rem 0.75rem;
}

.adc-panel.hidden {
  display: none;
}

.adc-panel-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.45rem;
}

.adc-panel-title {
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #f3f4f6;
}

.adc-panel-hint {
  font-size: 0.72rem;
  color: #9ca3af;
}

.adc-sliders {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  max-height: 32vh;
  overflow-y: auto;
}

.adc-row {
  display: grid;
  grid-template-columns: 90px 1fr 130px;
  align-items: center;
  gap: 0.6rem;
}

.adc-row-label {
  font-size: 0.8rem;
  color: #cbd5e1;
}

.adc-slider {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  height: 6px;
  background: #1e293b;
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}

.adc-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #38bdf8;
  border: 2px solid #0f172a;
  cursor: grab;
  box-shadow: 0 0 0 1px rgba(56, 189, 248, 0.3);
}

.adc-slider::-webkit-slider-thumb:active {
  cursor: grabbing;
}

.adc-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #38bdf8;
  border: 2px solid #0f172a;
  cursor: grab;
}

.adc-readout {
  font-family: 'SFMono-Regular', Menlo, monospace;
  font-size: 0.78rem;
  color: #94a3b8;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 600px) {
  .adc-row {
    grid-template-columns: 1fr;
    gap: 0.25rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid rgba(148, 163, 184, 0.15);
  }

  .adc-row-label {
    font-size: 0.78rem;
  }

  .adc-readout {
    text-align: left;
  }
}

.serial-panel {
  border-top: 1px solid #e2e8f0;
  background: #0b1220;
  color: #e5e7eb;
  padding: 0.55rem 0.75rem 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.serial-panel.hidden {
  display: none;
}

.serial-header {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}

.serial-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: #f3f4f6;
}

.serial-meta {
  font-size: 0.72rem;
  color: #94a3b8;
  flex: 1 1 auto;
}

.serial-clear {
  appearance: none;
  border: 1px solid #334155;
  background: #1e293b;
  color: #e5e7eb;
  padding: 0.15rem 0.55rem;
  border-radius: 6px;
  font-size: 0.72rem;
  cursor: pointer;
}

.serial-clear:hover {
  background: #243244;
}

.serial-output {
  background: #020617;
  border: 1px solid #1e293b;
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
  font-family: 'SFMono-Regular', Menlo, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.4;
  height: 120px;
  max-height: 28vh;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.serial-rx {
  color: #e2e8f0;
}

.serial-tx {
  color: #34d399;
}

.serial-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.serial-input {
  flex: 1 1 auto;
  min-width: 0;
  background: #0f172a;
  border: 1px solid #334155;
  color: #f8fafc;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  font-family: 'SFMono-Regular', Menlo, Consolas, monospace;
  font-size: 0.85rem;
}

.serial-input:focus {
  outline: none;
  border-color: #38bdf8;
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.25);
}

.serial-newline {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
  color: #cbd5e1;
  user-select: none;
}

.serial-send {
  appearance: none;
  border: 1px solid #1d4ed8;
  background: #2563eb;
  color: #fff;
  font-weight: 600;
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
}

.serial-send:hover {
  background: #1d4ed8;
}

@media (max-width: 600px) {
  .serial-output {
    height: 110px;
    font-size: 0.78rem;
  }

  .serial-form {
    flex-wrap: wrap;
  }

  .serial-input {
    flex: 1 1 100%;
  }

  .serial-newline {
    flex: 0 0 auto;
  }

  .serial-send {
    flex: 0 0 auto;
    margin-left: auto;
  }
}

.console-header {
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  color: #cbd5e1;
  border-bottom: 1px solid #1e293b;
  background: transparent;
  border-left: 0;
  border-right: 0;
  border-top: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
}

.console-header:hover {
  background: rgba(255, 255, 255, 0.04);
}

.console-header .chevron {
  display: inline-block;
  transition: transform 0.15s ease;
  color: #94a3b8;
}

.console-container.is-collapsed .console-header .chevron {
  transform: rotate(-90deg);
}

.console-container.is-collapsed .console-header {
  border-bottom: 0;
}

.console-output {
  flex: 1;
  margin: 0;
  padding: 0.75rem;
  overflow: auto;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
  line-height: 1.45;
  color: #e2e8f0;
  white-space: pre-wrap;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  background-color: white;
  margin: 15% auto;
  padding: 2rem;
  border-radius: 8px;
  width: 400px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.modal-content h3 {
  margin-bottom: 1rem;
  color: #2d3748;
}

.modal-content input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 1rem;
  margin-bottom: 1rem;
}

.modal-content input:focus {
  outline: none;
  border-color: #3182ce;
  box-shadow: 0 0 0 3px rgba(49, 130, 206, 0.1);
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.modal-actions button {
  padding: 0.5rem 1rem;
  border: 1px solid #e2e8f0;
  background-color: white;
  color: #4a5568;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.2s;
}

.modal-actions button:hover {
  background-color: #f7fafc;
  border-color: #cbd5e0;
}

.modal-actions button:first-child {
  background-color: #3182ce;
  color: white;
  border-color: #3182ce;
}

.modal-actions button:first-child:hover {
  background-color: #2c5aa0;
  border-color: #2c5aa0;
}

/* Desktop: hide the file browser when collapsed via the hamburger toggle. */
@media (min-width: 769px) {
  .sidebar.is-collapsed {
    display: none;
  }
}

/* Responsive design */
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }

  /* Sidebar becomes an off-canvas drawer. */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(82vw, 320px);
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 50;
    box-shadow: 2px 0 16px rgba(0, 0, 0, 0.25);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 40;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .sidebar-backdrop.is-open {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-backdrop[hidden] {
    /* Even when the JS hasn't toggled it yet, hide on desktop only. Mobile keeps it
       in the layer for transitions; explicit [hidden] still wins via display:none below. */
    display: none;
  }

  .sidebar-toggle {
    display: inline-flex;
  }

  .sidebar-actions button {
    min-width: 40px;
    min-height: 40px;
    font-size: 1.15rem;
  }

  .main-content {
    width: 100%;
    min-height: 0;
    /* Let the right column scroll as a whole on phones. With ADC + Pins +
     * Serial all open at once, their combined intrinsic heights exceed the
     * viewport; without this the console (last child) gets clipped. */
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .editor-header {
    padding: 0.5rem 0.6rem;
    flex-wrap: nowrap;
    gap: 0.4rem;
    align-items: center;
    padding-top: max(0.5rem, env(safe-area-inset-top));
  }

  .file-info {
    flex: 1 1 auto;
    min-width: 0;
    gap: 0.4rem;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  .save-status {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #workspace-badge {
    font-size: 0.7rem;
    padding: 0.15rem 0.4rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 8rem;
  }

  .editor-actions {
    flex: 0 0 auto;
    gap: 0.35rem;
  }

  .editor-actions .icon-btn {
    flex: 0 0 auto;
    font-size: 1.05rem;
    padding: 0.45rem 0.7rem;
    min-width: 40px;
    min-height: 40px;
  }

  .menu-toggle {
    min-height: 40px;
    flex: 0 0 auto;
    padding: 0.45rem 0.55rem;
  }

  .tabs {
    padding: 0.3rem 0.35rem;
    gap: 0.3rem;
    -webkit-overflow-scrolling: touch;
  }

  .tab {
    padding: 0.45rem 0.55rem;
  }

  .tab-title {
    max-width: 140px;
  }

  .tab-close {
    font-size: 18px;
    padding: 0 0.25rem;
  }

  .editor-container {
    /* Pin the editor at a usable height; the rest of the column scrolls. */
    flex: 0 0 auto;
    height: 50vh;
    min-height: 42vh;
  }

  /* When any of the live simulator panels (Pins / ADC / Serial / LED grid)
   * is visible, give the editor less screen real estate so the panels and
   * console don't get pushed below the fold. The whole column is still
   * scrollable, this just biases space toward the panels. */
  .main-content:has(.pin-panel:not(.hidden), .adc-panel:not(.hidden), .serial-panel:not(.hidden), .led-sim-panel:not(.hidden)) .editor-container {
    height: 32vh;
    min-height: 26vh;
  }

  /* Stop the editor / panels / console from being squashed by flexbox —
   * each keeps its natural (or capped) height and the column scrolls. */
  .led-sim-panel,
  .pin-panel,
  .adc-panel,
  .serial-panel,
  .console-container {
    flex: 0 0 auto;
  }

  .cm-editor {
    font-size: 14px; /* >=16px would prevent iOS zoom but feels too large here; CM is contenteditable so no zoom anyway. */
  }

  .modal-content {
    width: min(92vw, 420px);
    margin: 18vh auto;
    padding: 1.25rem;
  }

  .modal-content input {
    font-size: 16px; /* prevent iOS focus zoom */
  }

  .modal-actions button {
    min-height: 40px;
    flex: 1 1 100px;
  }

  .led-grid {
    max-height: 120px;
  }

  .led-sim-header {
    flex-wrap: wrap;
    align-items: center;
  }

  .led-sim-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .led-sim-actions button {
    flex: 1 1 80px;
    min-height: 36px;
  }

  .console-container {
    height: 160px;
  }

  .file-item {
    padding: 0.65rem 0.5rem;
  }
}

/* ADC / Pins / Serial: touch-friendly on phones + safe-area padding */
@media (max-width: 768px) {
  .adc-panel,
  .pin-panel,
  .serial-panel {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    padding-bottom: max(0.65rem, env(safe-area-inset-bottom));
    -webkit-tap-highlight-color: transparent;
  }

  /* Cap each panel's scroll region tighter on mobile so two or three of them
   * stacked don't push the console far below the fold. The whole column is
   * already scrollable; this just keeps individual panels compact. */
  .pin-rows {
    max-height: 22vh;
  }

  .adc-sliders {
    max-height: 22vh;
  }

  .serial-output {
    max-height: 22vh;
  }

  .adc-slider {
    min-height: 2.75rem;
    padding: 0.75rem 0;
    box-sizing: border-box;
  }

  .adc-slider::-webkit-slider-thumb {
    width: 28px;
    height: 28px;
  }

  .adc-slider::-moz-range-thumb {
    width: 28px;
    height: 28px;
  }

  .pin-toggle {
    min-height: 44px;
    min-width: 44px;
    padding: 0.35rem 0.45rem;
  }

  .pin-led {
    width: 18px;
    height: 18px;
  }

  .serial-send,
  .serial-clear {
    min-height: 44px;
    padding: 0.4rem 0.85rem;
    font-size: 0.9rem;
  }

  .serial-input {
    min-height: 44px;
    font-size: 16px;
  }
}

/* Hide drawer affordances entirely on desktop, even with [hidden] flipped off. */
@media (min-width: 769px) {
  .sidebar-backdrop {
    display: none !important;
  }
}

/* Scrollbar styling */
.file-tree::-webkit-scrollbar {
  width: 6px;
}

.file-tree::-webkit-scrollbar-track {
  background: #2d3748;
}

.file-tree::-webkit-scrollbar-thumb {
  background: #4a5568;
  border-radius: 3px;
}

.file-tree::-webkit-scrollbar-thumb:hover {
  background: #718096;
}

