/* ============================================================
   SECTION: Global Styles & CSS Variables
   ============================================================ */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap');

:root {
  /* --- Theme tokens (overwritten by THEMES in app.js) ------ */
  --bg-main: #1a1a2e;
  --bg-sidebar: #16213e;
  --bg-toolbar: #0f3460;
  --accent: #0f3460;
  --accent-light: #e94560;
  --text-primary: #e0e0e0;
  --text-secondary: #b8b8cc;
  --border: #2a2a4e;
  --wire-high: #00e676;
  --wire-low: #555577;
  --wire-unconnected: #e53935;
  --wire-selected: #ffeb3b;
  --gate-fill: #1e2d50;
  --gate-stroke: #5c7aea;
  --gate-selected: #e94560;
  --pin-color: #7c9af0;
  --pin-hover: #ffeb3b;
  --shadow: 0 4px 24px rgba(0,0,0,0.5);

  /* --- Design tokens (constants, never themed) ------------- */
  --radius-xs: 4px;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --shadow-sm: 0 2px 6px rgba(0,0,0,0.3);
  --shadow-md: 0 6px 20px rgba(0,0,0,0.45);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.6);
  --dur-fast: 140ms;
  --dur-base: 200ms;
  --dur-slow: 320ms;
  --ease: cubic-bezier(.22,.8,.22,1);
  --font-sans: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Consolas', 'Cascadia Code', monospace;
  --focus-ring: 2px solid var(--accent-light);
  --focus-offset: 2px;
}

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

body {
  font-family: var(--font-sans);
  background: var(--bg-main);
  color: var(--text-primary);
  overflow: hidden;
  user-select: none;
  height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "cv11","ss01";
}

/* Global focus-visible baseline for all interactive elements */
button:focus-visible,
select:focus-visible,
input:focus-visible,
[role="button"]:focus-visible,
.gate-item:focus-visible,
.help-nav-item:focus-visible {
  outline: var(--focus-ring);
  outline-offset: var(--focus-offset);
  border-radius: var(--radius-sm);
}

/* ============================================================
   SECTION: Toolbar
   ============================================================ */
#toolbar {
  min-height: 48px;
  height: auto;
  background: var(--bg-toolbar);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: var(--space-1) var(--space-3);
  gap: var(--space-1);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
  box-shadow: var(--shadow-md);
}

#hamburger-btn { display: none; }
#toolbar-btns { display: contents; }

#toolbar .toolbar-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--accent-light);
  margin-right: var(--space-2);
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.toolbar-sep {
  width: 1px;
  height: 26px;
  background: var(--border);
  margin: 0 var(--space-1);
  opacity: 0.6;
}

#theme-selector {
  padding: 5px 7px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease);
}
#theme-selector:hover { border-color: var(--accent-light); }
#theme-selector option {
  background: var(--bg-sidebar);
  color: var(--text-primary);
}

.toolbar-btn {
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease);
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}
.toolbar-btn:hover {
  background: rgba(255,255,255,0.14);
  border-color: var(--accent-light);
  color: #fff;
}
.toolbar-btn:active { background: rgba(255,255,255,0.2); }
.toolbar-btn svg {
  width: 14px; height: 14px;
  flex-shrink: 0;
}

#zoom-level {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  min-width: 46px;
  text-align: center;
  letter-spacing: 0.02em;
}

/* ============================================================
   SECTION: Main Layout
   ============================================================ */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ============================================================
   SECTION: Sidebar (consolidated)
   ============================================================ */
#sidebar {
  width: 170px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  flex-shrink: 0;
  padding: var(--space-2) 0 var(--space-4);
  z-index: 5;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#sidebar::-webkit-scrollbar { width: 5px; }
#sidebar::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
#sidebar::-webkit-scrollbar-thumb:hover { background: var(--gate-stroke); }

#sidebar-search {
  display: block;
  width: calc(100% - 16px);
  margin: var(--space-2) var(--space-2) var(--space-1);
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 11px;
  padding: 6px 9px;
  outline: none;
  transition: border-color var(--dur-fast) var(--ease),
              background var(--dur-fast) var(--ease);
}
#sidebar-search::placeholder { color: var(--text-secondary); opacity: 0.7; }
#sidebar-search:focus {
  border-color: var(--gate-stroke);
  background: rgba(255,255,255,0.09);
}

.sidebar-section { margin-bottom: var(--space-1); }

.sidebar-section-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 1.2px;
  color: var(--text-secondary);
  padding: 9px 10px 5px;
  text-transform: uppercase;
  border-left: 2px solid var(--gate-stroke);
  margin: 0 6px;
  border-radius: 0 var(--radius-xs) var(--radius-xs) 0;
  background: rgba(92,122,234,0.07);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
  transition: color var(--dur-fast) var(--ease),
              background var(--dur-fast) var(--ease);
}
.sidebar-section-label:hover {
  color: var(--text-primary);
  background: rgba(92,122,234,0.16);
}
.sidebar-section-label .sec-arrow {
  font-size: 10px;
  transition: transform var(--dur-base) var(--ease);
  display: inline-block;
}
.sidebar-section.collapsed .sec-arrow {
  transform: rotate(-90deg);
}
.sidebar-section-body {
  overflow: hidden;
  max-height: 2000px;
  transition: max-height var(--dur-slow) var(--ease),
              opacity var(--dur-base) var(--ease);
  opacity: 1;
}
.sidebar-section.collapsed .sidebar-section-body {
  max-height: 0;
  opacity: 0;
}

.gate-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 7px;
  margin: 2px 5px;
  border-radius: var(--radius-sm);
  cursor: grab;
  border: 1px solid transparent;
  gap: var(--space-1);
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease),
              box-shadow var(--dur-base) var(--ease),
              transform var(--dur-fast) var(--ease);
}
.gate-item:hover {
  background: rgba(255,255,255,0.07);
  border-color: var(--gate-stroke);
  box-shadow: var(--shadow-sm);
  transform: translateY(-1px);
}
.gate-item:active { cursor: grabbing; transform: translateY(0); }
.gate-item canvas {
  width: 100px;
  height: 52px;
  display: block;
}
.gate-item-label {
  font-size: 9px;
  font-weight: 700;
  color: var(--text-secondary);
  letter-spacing: 0.5px;
}
.gate-item-badge {
  font-family: var(--font-mono);
  font-size: 8px;
  color: var(--text-secondary);
  opacity: 0.7;
  font-weight: 500;
  margin-top: 1px;
}

/* ============================================================
   SECTION: Canvas Container
   ============================================================ */
#canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--bg-main);
}
#main-canvas {
  position: absolute;
  top: 0; left: 0;
  cursor: default;
  display: block;
}
#hint-overlay {
  position: absolute;
  bottom: 14px;
  right: 18px;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-secondary);
  pointer-events: none;
  text-align: right;
  line-height: 1.75;
  opacity: 0.8;
  background: rgba(10,15,35,0.35);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* ============================================================
   SECTION: Modal / Truth Table
   ============================================================ */
#modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 100;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade-in var(--dur-base) var(--ease);
}
#modal-overlay.active { display: flex; }

#modal-box {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  min-width: 340px;
  max-width: 90vw;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: scale-in var(--dur-base) var(--ease);
}

#modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#modal-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}
#modal-close {
  background: rgba(255,255,255,0.04);
  border: 1px solid transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-sm);
  transition: color var(--dur-fast) var(--ease),
              background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
}
#modal-close:hover {
  color: var(--accent-light);
  background: rgba(255,255,255,0.09);
  border-color: var(--border);
}

#modal-body {
  overflow: auto;
  padding: var(--space-4) 18px;
  flex: 1;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
#modal-body::-webkit-scrollbar { width: 6px; }
#modal-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.truth-table {
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 12px;
  width: 100%;
}
.truth-table th, .truth-table td {
  border: 1px solid var(--border);
  padding: 6px 12px;
  text-align: center;
}
.truth-table th {
  background: var(--accent);
  color: var(--text-primary);
  font-weight: 600;
  position: sticky;
  top: 0;
  letter-spacing: 0.04em;
}
.truth-table td { color: var(--text-secondary); }
.truth-table tr:nth-child(even) td { background: rgba(255,255,255,0.02); }
.truth-table tr:hover td { background: rgba(92,122,234,0.07); }
.truth-table .val-1 { color: var(--wire-high); font-weight: 700; }
.truth-table .val-0 { color: var(--wire-low); }

.modal-msg {
  color: var(--text-secondary);
  font-size: 13px;
  text-align: center;
  padding: 22px 0;
}

/* ============================================================
   SECTION: Notification Toast
   ============================================================ */
#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(35,35,74,0.92);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 22px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-slow) var(--ease),
              transform var(--dur-slow) var(--ease);
  z-index: 200;
  white-space: nowrap;
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ============================================================
   SECTION: Language Selector
   ============================================================ */
#lang-select {
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 5px 9px;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease),
              background var(--dur-fast) var(--ease);
}
#lang-select:hover {
  border-color: var(--accent-light);
  background: rgba(255,255,255,0.12);
}
#lang-select option { background: var(--bg-sidebar); color: var(--text-primary); }

/* ============================================================
   SECTION: Step-by-Step Panel
   ============================================================ */
#step-panel {
  display: none;
  height: 46px;
  background: #0a2244;
  border-bottom: 1px solid var(--border);
  align-items: center;
  padding: 0 var(--space-3);
  gap: var(--space-2);
  flex-shrink: 0;
  box-shadow: inset 0 -2px 10px rgba(0,0,0,0.35);
}
#step-panel.active { display: flex; }

.step-btn {
  background: rgba(255,255,255,0.08);
  border: 1px solid #2a4a7a;
  color: var(--text-primary);
  padding: 5px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
  white-space: nowrap;
}
.step-btn:hover { background: rgba(255,255,255,0.16); border-color: #5c9af0; }
.step-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.step-btn.primary {
  background: linear-gradient(180deg, #2a5cb0, #1a4a8a);
  border-color: #5c9af0;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.12);
}
.step-btn.primary:hover {
  background: linear-gradient(180deg, #3a6cc0, #2a5aaa);
}

#step-info {
  font-family: var(--font-mono);
  font-size: 11px;
  color: #a0c4f0;
  font-weight: 600;
  min-width: 92px;
  letter-spacing: 0.02em;
}
#step-gate-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: #6080a0;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#step-progress {
  height: 5px;
  background: #1a3060;
  border-radius: 3px;
  overflow: hidden;
  width: 88px;
  flex-shrink: 0;
}
#step-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #3a7af0, #00e676);
  border-radius: 3px;
  transition: width var(--dur-base) var(--ease);
  width: 0%;
  box-shadow: 0 0 8px rgba(0,230,118,0.4);
}

/* ============================================================
   SECTION: Minimap
   ============================================================ */
#minimap-container {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 190px;
  background: rgba(10,15,35,0.86);
  border: 1px solid #2a3a6a;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  z-index: 20;
  user-select: none;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
#minimap-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  background: rgba(15,52,96,0.7);
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 600;
  color: #7090c0;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  cursor: pointer;
}
#minimap-canvas {
  display: block;
  width: 190px;
  height: 116px;
}
#minimap-toggle-btn {
  background: none;
  border: none;
  color: #5c7aea;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  padding: 0 4px;
  transition: color var(--dur-fast) var(--ease);
}
#minimap-toggle-btn:hover { color: var(--accent-light); }

/* ============================================================
   SECTION: Config Popups (MUX/DEMUX size, CLOCK freq)
   ============================================================ */
.cfg-popup {
  display: none;
  position: fixed;
  z-index: 600;
  background: var(--bg-sidebar);
  border: 1px solid #2a4a8a;
  border-radius: var(--radius-md);
  padding: 14px 18px;
  box-shadow: var(--shadow-lg);
  min-width: 190px;
  animation: scale-in var(--dur-base) var(--ease);
}
.cfg-popup.active { display: block; }
.cfg-popup-title {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  color: #7c9af0;
  letter-spacing: 1px;
  margin-bottom: 11px;
  text-transform: uppercase;
}
.cfg-popup-btns { display: flex; gap: var(--space-1); flex-wrap: wrap; }
.cfg-popup-btn {
  background: rgba(255,255,255,0.08);
  border: 1px solid #2a4a8a;
  color: #c0d0ff;
  padding: 6px 11px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
}
.cfg-popup-btn:hover { background: rgba(92,122,234,0.3); border-color: #5c7aea; }
.cfg-popup-btn.active {
  background: #1a4a8a;
  border-color: #5c9af0;
  color: #fff;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.12);
}
.cfg-popup-close {
  position: absolute;
  top: 8px; right: 10px;
  background: none; border: none;
  color: #5c7aea; cursor: pointer;
  font-size: 14px; line-height: 1;
  transition: color var(--dur-fast) var(--ease);
}
.cfg-popup-close:hover { color: var(--accent-light); }
.cfg-popup label {
  font-size: 11px;
  color: #8090b0;
  display: block;
  margin-bottom: var(--space-1);
  margin-top: var(--space-2);
}
.cfg-popup input[type="range"] {
  width: 100%;
  margin-top: 3px;
  accent-color: #5c7aea;
}
.cfg-popup-val {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 700;
  color: var(--wire-high);
  text-align: center;
  margin-top: var(--space-1);
}

/* ============================================================
   SECTION: Inline text editor for LABEL gate
   ============================================================ */
#label-editor {
  display: none;
  position: fixed;
  z-index: 500;
  background: var(--gate-fill);
  border: 1px solid var(--gate-stroke);
  border-radius: var(--radius-xs);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 13px;
  padding: 4px 8px;
  outline: none;
  min-width: 80px;
  box-shadow: var(--shadow-md);
}

/* ============================================================
   SECTION: Help Modal
   ============================================================ */
#help-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.78);
  z-index: 3000;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade-in var(--dur-base) var(--ease);
}
#help-overlay.open { display: flex; }
#help-box {
  background: #0d1525;
  border: 1px solid #2a3a6a;
  border-radius: var(--radius-lg);
  width: min(900px, 95vw);
  height: min(640px, 90vh);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: scale-in var(--dur-base) var(--ease);
}
#help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 20px;
  background: #0a0f20;
  border-bottom: 1px solid #1a2a5a;
  flex-shrink: 0;
}
#help-header span {
  font-size: 15px;
  font-weight: 600;
  color: #7c9af0;
  letter-spacing: -0.01em;
}
#help-close {
  background: rgba(233,69,96,0.08);
  border: 1px solid rgba(233,69,96,0.25);
  color: #e94560;
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  transition: background var(--dur-fast) var(--ease);
}
#help-close:hover { background: rgba(233,69,96,0.2); }

#help-search {
  padding: 10px 20px;
  background: #080f1e;
  border-bottom: 1px solid #1a2050;
  flex-shrink: 0;
}
#help-search input {
  width: 100%;
  padding: 6px 11px;
  background: #0e1a30;
  border: 1px solid #2a3a6a;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 12px;
  transition: border-color var(--dur-fast) var(--ease);
}
#help-search input:focus { border-color: var(--gate-stroke); outline: none; }

#help-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}
#help-nav {
  width: 200px;
  flex-shrink: 0;
  overflow-y: auto;
  background: #090e1e;
  border-right: 1px solid #1a2050;
  padding: var(--space-2) 0;
  scrollbar-width: thin;
  scrollbar-color: #1a2050 transparent;
}
.help-nav-section {
  padding: 8px 16px 4px;
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 600;
  color: #4a5a8a;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-top: var(--space-1);
}
.help-nav-item {
  padding: 6px 20px;
  font-size: 12px;
  color: #8090b0;
  cursor: pointer;
  border-left: 2px solid transparent;
  transition: background var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.help-nav-item:hover { background: rgba(92,122,234,0.1); color: #c0d0ff; }
.help-nav-item.active {
  background: rgba(92,122,234,0.16);
  color: #a5b8e8;
  border-left-color: #5c7aea;
  font-weight: 600;
}
.help-nav-item.hidden { display: none; }

#help-content {
  flex: 1;
  overflow-y: auto;
  padding: 22px 26px;
  line-height: 1.65;
  scrollbar-width: thin;
  scrollbar-color: #1a2a5a transparent;
}
#help-content::-webkit-scrollbar { width: 8px; }
#help-content::-webkit-scrollbar-thumb { background: #1a2a5a; border-radius: 4px; }

#help-content h2 {
  font-size: 19px;
  font-weight: 700;
  color: #a5b8e8;
  margin: 0 0 8px;
  border-bottom: 1px solid #1a2a5a;
  padding-bottom: 8px;
  letter-spacing: -0.015em;
}
#help-content h3 {
  font-size: 13px;
  font-weight: 600;
  color: #c0d0ff;
  margin: 18px 0 7px;
}
#help-content p  { font-size: 12.5px; color: #a8b6d0; margin: 0 0 9px; }
#help-content ul { font-size: 12.5px; color: #a8b6d0; margin: 0 0 9px; padding-left: 20px; }
#help-content li { margin-bottom: 4px; }
#help-content code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: #0a1428;
  border: 1px solid #1a2a4a;
  border-radius: var(--radius-xs);
  padding: 1px 6px;
  color: #80c0ff;
}
#help-content pre {
  background: #060e18;
  border: 1px solid #1a2a4a;
  border-radius: var(--radius-md);
  padding: 13px 15px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: #80e0a0;
  overflow-x: auto;
  margin: var(--space-2) 0 var(--space-3);
  line-height: 1.7;
}
#help-content .pin-table {
  border-collapse: collapse;
  font-size: 11px;
  margin: var(--space-2) 0 var(--space-3);
  width: 100%;
}
#help-content .pin-table th {
  background: #0e1a30;
  color: #a5b8e8;
  padding: 6px 11px;
  text-align: left;
  border: 1px solid #1a2a5a;
  font-weight: 600;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
#help-content .pin-table td {
  padding: 5px 11px;
  border: 1px solid #1a2a5a;
  color: #a8b6d0;
  vertical-align: top;
}
#help-content .truth-table {
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 11px;
  margin: var(--space-2) 0 var(--space-3);
}
#help-content .truth-table th {
  background: #0e1a30;
  color: #a5b8e8;
  padding: 5px 11px;
  border: 1px solid #1a2a5a;
}
#help-content .truth-table td {
  padding: 4px 11px;
  border: 1px solid #1a2a5a;
  color: #a8b6d0;
  text-align: center;
}
#help-content .truth-table tr:nth-child(even) td { background: rgba(255,255,255,0.025); }

.help-tag {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 9px;
  padding: 2px 7px;
  border-radius: var(--radius-xs);
  margin-left: 6px;
  font-weight: 600;
  vertical-align: middle;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.help-tag-in  { background:#0e2a0e; color:#40e060; border:1px solid #1a4a1a; }
.help-tag-out { background:#0e0e2a; color:#6080f0; border:1px solid #1a1a4a; }
.help-tag-clk { background:#2a1a0e; color:#e0a040; border:1px solid #4a2a0e; }

/* Syntax highlighting (MCUScript) */
.kw  { color: #ff9090; }
.cm  { color: #80a080; font-style: italic; }
.val { color: #80ffb0; }
.var { color: #80c0ff; }
.op  { color: #ffcc60; }

/* ============================================================
   SECTION: Animations
   ============================================================ */
@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes scale-in {
  from { opacity: 0; transform: scale(.96); }
  to   { opacity: 1; transform: scale(1); }
}

/* ============================================================
   SECTION: Responsive / Mobile
   ============================================================ */

/* Tablet i mòbil: ≤768px */
@media (max-width: 768px) {
  #hamburger-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    font-size: 18px;
    background: rgba(255,255,255,0.08);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    cursor: pointer;
    flex-shrink: 0;
  }

  #toolbar-btns {
    display: none;
    position: fixed;
    top: 48px;
    left: 0;
    right: 0;
    background: var(--bg-toolbar);
    border-bottom: 1px solid var(--border);
    flex-direction: column;
    padding: var(--space-2) var(--space-3);
    gap: var(--space-1);
    z-index: 50;
    box-shadow: var(--shadow-lg);
  }
  #toolbar-btns.open { display: flex; }

  .toolbar-btn {
    width: 100%;
    justify-content: flex-start;
    padding: 10px 14px;
    font-size: 13px;
    min-height: 44px;
  }

  .toolbar-sep { display: none; }

  #sidebar {
    position: fixed;
    left: -180px;
    top: 48px;
    bottom: 0;
    z-index: 40;
    transition: left var(--dur-slow) var(--ease);
    box-shadow: 4px 0 24px rgba(0,0,0,0.55);
    width: 170px !important;
  }
  #sidebar.open { left: 0; }

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 39;
    top: 48px;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }
  #sidebar-overlay.active { display: block; }

  #canvas-container { touch-action: none; }

  .gate-item {
    padding: 9px 7px;
    min-height: 44px;
  }
  .gate-item-label { font-size: 11px !important; }
  .gate-item-badge { font-size: 9px !important; }

  #minimap-container { display: none; }
  #hint-overlay { display: none; }

  #zoom-level { font-size: 13px; min-width: 48px; }

  #modal-box { max-width: 98vw; max-height: 92vh; }
  #help-box { width: 98vw; height: 95vh; }
  #help-nav { display: none; }

  /* Touch targets: mínim 44px */
  .toolbar-btn,
  .step-btn,
  .cfg-popup-btn,
  #modal-close,
  #help-close { min-height: 44px; }

  #mobile-sidebar-btn { display: flex !important; }
}

/* Mòbil petit: ≤480px */
@media (max-width: 480px) {
  .toolbar-title { font-size: 13px !important; }

  #step-panel {
    flex-wrap: wrap;
    height: auto;
    padding: 7px var(--space-2);
    gap: var(--space-1);
  }
  #step-progress { display: none; }

  #modal-box {
    border-radius: 0;
    max-width: 100vw;
    max-height: 100vh;
    border-left: none;
    border-right: none;
  }
}

/* Botó flotant per obrir la sidebar (visible només en mòbil) */
#mobile-sidebar-btn {
  display: none;
  position: fixed;
  bottom: 22px;
  left: 18px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--bg-toolbar);
  border: 2px solid var(--gate-stroke);
  color: var(--text-primary);
  font-size: 22px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 45;
  box-shadow: var(--shadow-lg);
  transition: background var(--dur-fast) var(--ease),
              transform var(--dur-fast) var(--ease);
}
#mobile-sidebar-btn:hover {
  background: var(--accent);
  transform: scale(1.06);
}
#mobile-sidebar-btn:active { transform: scale(0.96); }

/* Touch targets globals */
button, [role="button"] {
  -webkit-tap-highlight-color: transparent;
}

/* ============================================================
   SECTION: Reduced Motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
