/* ===== CSS Variables ===== */
:root {
  --transition: 0.25s ease;
}

/* --- Dark Theme --- */
body.theme-dark {
  --bg:        #121212;
  --bg-card:   #1e1e1e;
  --bg-input:  #2a2a2a;
  --border:    #333;
  --text:      #e0e0e0;
  --text-muted:#888;
  --primary:   #4f9cf9;
  --primary-h: #3a82e4;
  --danger:    #e05252;
  --danger-h:  #c43c3c;
  --secondary: #3a3a3a;
  --secondary-h:#4a4a4a;
  --terminal-bg:#0d0d0d;
  --terminal-tx:#39ff14;
  --badge-ok:  #39ff14;
  --badge-err: #e05252;
  --topbar-bg: #1a1a1a;
  --scrollbar: #333;
}

/* --- Light Theme --- */
body.theme-light {
  --bg:        #f0f2f5;
  --bg-card:   #ffffff;
  --bg-input:  #f5f5f5;
  --border:    #d0d0d0;
  --text:      #1a1a1a;
  --text-muted:#666;
  --primary:   #1a73e8;
  --primary-h: #1558b0;
  --danger:    #d93025;
  --danger-h:  #b7231a;
  --secondary: #e0e0e0;
  --secondary-h:#c8c8c8;
  --terminal-bg:#1e1e1e;
  --terminal-tx:#c8ffc8;
  --badge-ok:  #1a9e1a;
  --badge-err: #d93025;
  --topbar-bg: #ffffff;
  --scrollbar: #bbb;
}

/* ===== Splash Screen ===== */
#splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #0d0d0d;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  transition: opacity 0.4s ease;
}

#splash.fade-out {
  opacity: 0;
  pointer-events: none;
}

#splashLogo {
  width: clamp(80px, 20vw, 180px);
  height: auto;
  object-fit: contain;
}

#splashName {
  font-size: 2rem;
  font-weight: 700;
  color: #4f9cf9;
  letter-spacing: 0.06em;
}

#splashVersion {
  font-size: 0.9rem;
  color: #888;
  letter-spacing: 0.08em;
}

#splashCredit {
  font-size: 0.82rem;
  color: #666;
}

#splashCredit span { color: #e05252; }

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background var(--transition), color var(--transition);
}

/* ===== Topbar ===== */
.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.55rem 1.25rem;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  transition: background var(--transition), border-color var(--transition);
}

.app-title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--primary);
  white-space: nowrap;
}

.topbar-divider {
  width: 1px;
  height: 1.3em;
  background: var(--border);
  margin: 0 0.15rem;
  flex-shrink: 0;
}

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  flex-wrap: wrap;
}

.meta-item {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.meta-item strong {
  color: var(--text);
  font-weight: 600;
  font-family: 'Cascadia Code', 'Consolas', monospace;
}

.flex-spacer { flex: 1; min-width: 0.5rem; }

/* ===== Hamburger / Dropdown ===== */
.hamburger-wrap {
  position: relative;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-width: 180px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.35);
  z-index: 300;
  overflow: hidden;
  transition: background var(--transition), border-color var(--transition);
}

.dropdown-menu.open { display: block; }

.dropdown-section {
  padding: 0.45rem 0.8rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 0.55rem 0.8rem;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 0.88rem;
  cursor: pointer;
  transition: background var(--transition);
}

.dropdown-item:last-child { border-bottom: none; }
.dropdown-item:hover { background: var(--bg-input); }

.dropdown-has-sub {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.submenu-arrow {
  font-size: 0.65rem;
  transition: transform 0.18s;
  color: var(--text-muted);
}

.dropdown-has-sub.sub-open .submenu-arrow { transform: rotate(90deg); }

.dropdown-submenu {
  display: none;
  background: rgba(0,0,0,0.12);
  border-bottom: 1px solid var(--border);
}

.dropdown-submenu.sub-open { display: block; }

.dropdown-subitem {
  display: block;
  width: 100%;
  padding: 0.45rem 0.8rem 0.45rem 1.8rem;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background var(--transition);
}

.dropdown-subitem:last-child { border-bottom: none; }
.dropdown-subitem:hover { background: var(--bg-input); }

.dropdown-divider {
  height: 1px;
  background: var(--border);
}

/* ===== Settings Modal ===== */
.settings-panel {
  width: min(96vw, 480px);
}

.settings-body {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 0.25rem 0;
}

/* ===== Container ===== */
.container {
  max-width: 900px;
  margin: 2rem auto;
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ===== Card ===== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
  transition: background var(--transition), border-color var(--transition);
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1.25rem;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* ===== Form Grid ===== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

select,
input[type="number"],
input[type="text"] {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 0.65rem;
  font-size: 0.9rem;
  outline: none;
  transition: border-color var(--transition), background var(--transition);
  width: 100%;
}

select:focus,
input[type="number"]:focus,
input[type="text"]:focus {
  border-color: var(--primary);
}

.port-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.port-row select { flex: 1; }

/* ===== Buttons ===== */
.btn {
  padding: 0.5rem 1.1rem;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition);
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary  { background: var(--primary);   color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-h); }

.btn-danger   { background: var(--danger);    color: #fff; }
.btn-danger:hover:not(:disabled)  { background: var(--danger-h);  }

.btn-secondary{ background: var(--secondary); color: var(--text); }
.btn-secondary:hover:not(:disabled){ background: var(--secondary-h); }

.btn-sm  { padding: 0.35rem 0.6rem; font-size: 0.85rem; }

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1.1rem;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  line-height: 1;
}
.btn-icon:hover { background: var(--secondary); }

/* ===== Button Row ===== */
.btn-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* ===== Status Badge ===== */
.status-badge {
  font-size: 0.85rem;
  color: var(--badge-err);
  font-weight: 500;
}
.status-badge.connected { color: var(--badge-ok); }

/* ===== Terminal ===== */
.terminal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.terminal-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.terminal-output {
  background: var(--terminal-bg);
  color: var(--terminal-tx);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem 1rem;
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 0.85rem;
  height: 320px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
  transition: background var(--transition), border-color var(--transition);
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}

.terminal-output .line-tx { color: #9ec8ff; }
.terminal-output .line-rx { color: var(--terminal-tx); }
.terminal-output .line-info { color: var(--text-muted); font-style: italic; }
.terminal-output .line-err { color: var(--badge-err); }

.terminal-input-row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.terminal-input-row input { flex: 1; }

/* ===== Dashboard / Gridstack ===== */
#dashboardWrapper {
  display: flex;
  gap: 0.75rem;
  width: 100%;
  padding: 0.75rem 1rem;
  align-items: flex-start;
  overflow-x: auto;
}

/* Left palette sidebar (edit mode only) */
.dash-palette {
  display: none;
  flex: 0 0 170px;
  position: sticky;
  top: 60px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  flex-direction: column;
  gap: 0.4rem;
  transition: background var(--transition), border-color var(--transition);
}

body.dash-edit .dash-palette { display: flex; }

.palette-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin-top: 0.3rem;
}
.palette-title:first-child { margin-top: 0; }

.palette-item {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.4rem 0.55rem;
  font-size: 0.82rem;
  cursor: grab;
  user-select: none;
  transition: background var(--transition), border-color var(--transition);
}
.palette-item:hover  { border-color: var(--primary); background: var(--secondary); }
.palette-item:active { cursor: grabbing; opacity: 0.6; }

.palette-hint {
  font-size: 0.68rem;
  color: var(--text-muted);
  line-height: 1.4;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

/* Grid takes remaining full width */
#dashGrid {
  flex: 1;
  min-width: 0;
  min-height: 350px;
}

.grid-stack { background: transparent; }

.grid-stack-item-content {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  inset: 0;
  position: absolute;
  overflow: hidden;
  box-shadow: 0 1px 6px rgba(0,0,0,0.35);
  transition: border-color var(--transition), background var(--transition);
}

body.dash-edit .grid-stack-item-content {
  border: 2px dashed var(--primary);
}

/* Drop hint */
#dashGrid.dash-dragover {
  outline: 2px dashed var(--primary);
  outline-offset: 2px;
  border-radius: 8px;
}

/* Panel contents */
.gs-content {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.3rem 0.55rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-input);
  flex-shrink: 0;
  gap: 0.4rem;
  transition: background var(--transition), border-color var(--transition);
}

body.dash-edit .panel-head { cursor: move; }

.panel-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel-head-right {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.panel-cur-val {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--primary);
  font-family: 'Cascadia Code', 'Consolas', monospace;
}

.btn-xs { padding: 0.05rem 0.28rem !important; font-size: 0.68rem !important; }

/* Chart panel */
.panel-canvas-wrap {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
  padding: 0.25rem 0.35rem;
}

/* Value panel */
.panel-val-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  padding: 0.5rem;
}

.panel-big-val {
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
}

.panel-big-val .panel-cur-val {
  font-size: 1em;
  line-height: 1;
}

.panel-val-unit {
  font-size: 0.45em;
  color: var(--text-muted);
}

.panel-raw-val {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-family: 'Cascadia Code', monospace;
}

/* Freitext panel */
.gs-content[data-type="text"] {
  position: relative;
}

.panel-text-body {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}

.panel-text-label {
  font-weight: 700;
  white-space: nowrap;
  line-height: 1.1;
  text-align: center;
  color: var(--primary);
}

.panel-text-actions {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
  display: flex;
  gap: 0.2rem;
  z-index: 5;
}

.cs-text-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 0.25rem 0.4rem;
  font-size: 0.82rem;
  transition: border-color var(--transition);
}

.cs-text-input:focus {
  outline: none;
  border-color: var(--primary);
}

.cs-btn-group {
  display: flex;
  gap: 0.25rem;
}

.cs-toggle-btn {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 0.2rem 0.5rem;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}

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

.cs-select {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 0.25rem 0.4rem;
  font-size: 0.82rem;
  cursor: pointer;
}

/* Gauge panel */
.panel-gauge-body {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.gauge-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* ===== Signal-Gen widget ===== */
.sg-manual-block {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 0.4rem; padding: 0.4rem; min-height: 0;
}
.sg-slider-wrap {
  flex: 1; display: flex; align-items: stretch;
  justify-content: center; min-height: 0; width: 100%;
}
.sg-slider {
  writing-mode: vertical-lr; direction: rtl;
  width: 40px; height: 100%;
  accent-color: var(--primary); cursor: pointer;
}
.sg-val-display {
  font-size: 1.1rem; font-weight: 700;
  color: var(--primary);
  font-family: 'Cascadia Code', 'Consolas', monospace;
}
.sg-auto-block {
  flex: 1; display: flex; flex-direction: column;
  gap: 0.35rem; padding: 0.4rem; min-height: 0;
}
.sg-preview-canvas { flex: 1; display: block; width: 100%; min-height: 0; border-radius: 4px; }
.sg-auto-controls   { display: flex; align-items: center; gap: 0.4rem; flex-shrink: 0; }
.sg-canvas-wrap     { position: relative; flex: 1; min-height: 0; overflow: hidden; border-radius: 4px; cursor: pointer; user-select: none; }
.sg-canvas-wrap canvas { display: block; width: 100%; height: 100%; }
.sg-overlay-info    { position: absolute; top: 4px; right: 5px; display: flex; flex-direction: column; align-items: flex-end; gap: 1px; pointer-events: none; }
.sg-play-icon-overlay { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); font-size: 2.2rem; color: rgba(255,255,255,0.92); pointer-events: none; opacity: 0; transition: opacity 0.55s ease-out; text-shadow: 0 0 10px rgba(0,0,0,0.7); line-height: 1; }
.sg-play-icon-overlay.flash  { opacity: 1; transition: none; }
.sg-play-icon-overlay.paused { opacity: 0.7; transition: none; }
.sg-time-display    { font-size: 0.68rem; font-family: 'Cascadia Code', monospace; color: rgba(255,255,255,0.8); background: rgba(0,0,0,0.45); padding: 0 3px; border-radius: 2px; white-space: nowrap; }

/* Signal-Gen header */
.sg-header {
  display: flex; align-items: center; gap: 0.3rem;
  padding: 0.25rem 0.45rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-input);
  flex-shrink: 0;
  flex-wrap: nowrap; overflow: hidden;
  transition: background var(--transition), border-color var(--transition);
}
.sg-channel-badge { font-size: 0.72rem; font-weight: 700; font-family: 'Cascadia Code', monospace; white-space: nowrap; flex-shrink: 0; }
.sg-mode-btns     { display: flex; gap: 0.15rem; flex-shrink: 0; }
.sg-mode-btn {
  padding: 0.1rem 0.32rem; font-size: 0.71rem;
  border: 1px solid var(--border); border-radius: 4px;
  background: transparent; color: var(--text-muted); cursor: pointer; white-space: nowrap;
  transition: background var(--transition), color var(--transition);
}
.sg-mode-btn.sg-mode-active { background: var(--primary); color: #fff; border-color: var(--primary); }
.sg-loop-label  { font-size: 0.71rem; color: var(--text-muted); white-space: nowrap; flex-shrink: 0; }
.sg-loop-select { font-size: 0.71rem; background: var(--bg-input); border: 1px solid var(--border); border-radius: 4px; color: var(--text); padding: 0.06rem 0.25rem; cursor: pointer; flex-shrink: 1; min-width: 0; max-width: 90px; }
.sg-header-right { margin-left: auto; display: flex; align-items: center; gap: 0.15rem; flex-shrink: 0; }

/* Signal-Gen Editor modal */
.sg-editor-panel    { width: 90vw !important; max-width: 90vw !important; height: 90vh; max-height: 90vh; overflow: hidden; }
.sg-editor-toolbar  { display: flex; align-items: center; gap: 0.5rem; flex-wrap: nowrap; }
.sg-editor-canvas-wrap { flex: 1; min-height: 210px; overflow: hidden; }
.sg-editor-canvas-wrap canvas { display: block; width: 100%; height: 100%; cursor: crosshair; border: 1px solid var(--border); border-radius: 6px; }
.sg-editor-bottom   { display: flex; gap: 0.6rem; flex-shrink: 0; min-height: 0; align-items: flex-start; }
.sg-point-table-wrap { width: 310px; flex-shrink: 0; max-height: 150px; overflow-y: auto; border: 1px solid var(--border); border-radius: 4px; }
.sg-pt-tooltip {
  position: absolute; pointer-events: none; z-index: 10;
  background: rgba(0,0,0,0.78); color: #fa0;
  font-size: 0.75rem; font-family: 'Cascadia Code', monospace;
  padding: 2px 8px; border-radius: 4px; white-space: nowrap;
}
.sg-pt-input {
  width: 72px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 3px; color: var(--text); padding: 0.1rem 0.3rem;
  font-size: 0.8rem; font-family: 'Cascadia Code', monospace;
}
.sg-pt-input:focus { outline: none; border-color: var(--primary); }
.sg-point-table      { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.sg-point-table th   { color: var(--text-muted); font-size: 0.7rem; padding: 0.18rem 0.6rem; text-align: left; border-bottom: 1px solid var(--border); background: var(--bg-input); position: sticky; top: 0; }
.sg-point-table td   { padding: 0.15rem 0.6rem; border-bottom: 1px solid var(--border); cursor: pointer; }
.sg-point-table tr:last-child td { border-bottom: none; }
.sg-point-table tr.sg-pt-selected td { background: var(--bg-input); color: var(--primary); }
.sg-point-table tr:hover td { background: var(--bg-input); }
.sg-library-section  { display: flex; flex-direction: column; gap: 0.4rem; }
.sg-library-row      { display: flex; gap: 0.4rem; align-items: center; }

/* Percent quick-select buttons */
.sg-pct-btns { display: flex; flex-direction: column; justify-content: space-between; padding: 9px 0; margin-left: 6px; flex-shrink: 0; align-self: stretch; }
.sg-pct-btn  { font-size: 0.75rem; padding: 0.18rem 0.5rem; background: var(--bg-input); border: 1px solid var(--border); border-radius: 4px; color: var(--text-muted); cursor: pointer; line-height: 1.3; white-space: nowrap; transition: background var(--transition), color var(--transition); flex: 0 0 auto; min-width: 2.8rem; text-align: center; }
.sg-pct-btn:hover  { background: var(--secondary); color: var(--text); }
.sg-pct-btn:active { background: var(--primary); color: #fff; border-color: var(--primary); }
.sg-val-row          { display: flex; align-items: center; justify-content: center; gap: 0.4rem; flex-shrink: 0; flex-wrap: wrap; }
.sg-real-val-display { font-size: 0.73rem; color: var(--text-muted); font-family: 'Cascadia Code', monospace; white-space: nowrap; }
.sg-cur-val-display  { font-size: 0.7rem; font-family: 'Cascadia Code', monospace; color: #fa0; background: rgba(0,0,0,0.45); padding: 0 3px; border-radius: 2px; white-space: nowrap; }

/* Global Settings */
.settings-section-title { font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); margin-bottom: 0.6rem; }
.settings-row   { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; font-size: 0.82rem; margin-bottom: 0.5rem; }
.settings-label { flex: 0 0 130px; font-weight: 500; color: var(--text); }
.settings-inputs { display: flex; align-items: center; gap: 0.3rem; flex-wrap: wrap; font-size: 0.8rem; color: var(--text-muted); }

/* ===== Terminal Modal ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 500;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay.open { display: flex; }

.modal-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: min(96vw, 820px);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
  transition: background var(--transition), border-color var(--transition);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
}

@media (max-width: 620px) {
  .modal-panel { padding: 0.9rem 0.75rem; gap: 0.5rem; }
  .terminal-output { height: 55vh; }
}

/* ===== Device Info ===== */
.device-info-grid {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.device-info-item {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  flex: 1;
  min-width: 140px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  transition: background var(--transition), border-color var(--transition);
}

.device-info-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.device-info-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
}

.device-info-value.mono {
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 0.9rem;
  word-break: break-all;
}

/* ===== Charts ===== */
.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.chart-item {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  transition: background var(--transition), border-color var(--transition);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.5rem;
}

.chart-label {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.chart-unit {
  font-size: 0.72rem;
  font-weight: 400;
  margin-left: 0.15rem;
}

.chart-value-col {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.1rem;
}

.chart-current {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--primary);
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  line-height: 1.1;
}

.chart-raw {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
}

.chart-canvas-wrap {
  position: relative;
  height: 140px;
}

.chart-settings-btn {
  font-size: 0.72rem;
  padding: 0.1rem 0.3rem;
  align-self: flex-start;
  opacity: 0.4;
  transition: opacity var(--transition);
  flex-shrink: 0;
}
.chart-settings-btn:hover,
.chart-settings-btn.cs-active { opacity: 1; color: var(--primary); }

/* ===== Chart Settings Popup ===== */
.chart-settings-popup {
  position: fixed;
  z-index: 400;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.85rem;
  width: 210px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.35);
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  transition: background var(--transition), border-color var(--transition);
}

.cs-title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--primary);
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border);
}

.cs-row { display: flex; flex-direction: column; gap: 0.25rem; }

.cs-row > label:not(.toggle-label) {
  font-size: 0.73rem;
  color: var(--text-muted);
  font-weight: 500;
}

.cs-range-row { display: flex; align-items: center; gap: 0.5rem; }
.cs-range-row input[type="range"] { flex: 1; accent-color: var(--primary); }

.cs-range-val {
  font-size: 0.78rem;
  min-width: 2.2ch;
  text-align: right;
  font-family: 'Cascadia Code', monospace;
  color: var(--text);
}

.cs-minmax-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }

.cs-minmax { display: flex; flex-direction: column; gap: 0.2rem; }
.cs-minmax label { font-size: 0.72rem; color: var(--text-muted); }
.cs-minmax input { padding: 0.3rem 0.4rem; font-size: 0.85rem; }

.chart-footer {
  display: flex;
  justify-content: flex-end;
  padding-top: 0.5rem;
}

/* ===== Calibration Modal ===== */
.cal-panel {
  width: min(96vw, 920px);
  max-height: 90vh;
}

.cal-status {
  font-size: 0.82rem;
  color: var(--text-muted);
  min-height: 1.2em;
}

/* Split layout */
.cal-layout {
  display: flex;
  gap: 1rem;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.cal-table-side {
  width: 42%;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 0;
}

.cal-table-wrap {
  flex: 1;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 120px;
}

.cal-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.cal-table th {
  background: var(--bg-input);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.45rem 0.65rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 1;
}

.cal-table td {
  padding: 0.4rem 0.65rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-family: 'Cascadia Code', 'Consolas', monospace;
  font-size: 0.83rem;
}

.cal-table tr:last-child td { border-bottom: none; }
.cal-table tr:hover td { background: var(--bg-input); }
.cal-row-selected td { background: rgba(79,156,249,0.15) !important; color: var(--primary); }

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

/* Detail pane */
.cal-detail {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  transition: border-color var(--transition);
}

.cal-detail-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 2rem;
}

.cal-detail-form {
  display: flex;
  flex-direction: column;
  gap: 1.4rem;
}

.cal-field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.cal-field-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.cal-slider-wrap { display: flex; flex-direction: column; gap: 0.15rem; }

#calSlider {
  width: 100%;
  accent-color: var(--primary);
  cursor: pointer;
  height: 6px;
}

.cal-slider-limits {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.cal-num-input {
  padding: 0.45rem 0.65rem;
  font-size: 1rem;
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-weight: 600;
  max-width: 180px;
}

.cal-raw-live {
  font-size: 0.78rem;
  color: var(--primary);
  font-family: 'Cascadia Code', 'Consolas', monospace;
  min-height: 1.2em;
}

.cal-footer {
  display: flex;
  align-items: center;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
}

@media (max-width: 620px) {
  .cal-layout { flex-direction: column; overflow: visible; }
  .cal-table-side { width: 100%; max-height: 180px; }
}

/* ===== Responsive — Mobile ===== */
@media (max-width: 620px) {

  /* Topbar: 2 rows */
  .topbar {
    padding: 0.45rem 0.75rem;
    row-gap: 0.35rem;
  }

  /* Device meta moves to its own full-width row */
  .topbar-meta {
    order: 10;
    width: 100%;
    padding-top: 0.35rem;
    border-top: 1px solid var(--border);
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
  }

  /* Hide the divider before meta — meta already has border-top */
  #infoDivider { display: none !important; }

  .app-title { font-size: 0.9rem; }

  /* Container */
  .container {
    margin: 0.75rem auto;
    padding: 0 0.5rem;
    gap: 0.75rem;
  }

  /* Dashboard: grid scrolls horizontally on mobile */
  #dashboardWrapper { padding: 0.5rem; }
  #dashGrid { min-width: 560px; }

  /* Card */
  .card { padding: 0.9rem 0.75rem; }

  /* Charts: 1 column on phone */
  .charts-grid {
    grid-template-columns: 1fr;
  }

  .chart-canvas-wrap { height: 120px; }

  .chart-current { font-size: 1.15rem; }

  /* Terminal */
  .terminal-output { height: 200px; }

  .terminal-input-row { gap: 0.35rem; }

  .terminal-controls { gap: 0.4rem; }

  /* Buttons smaller */
  .btn-sm { padding: 0.3rem 0.5rem; font-size: 0.8rem; }

  /* Card title smaller */
  .card-title { font-size: 0.85rem; margin-bottom: 0.85rem; }
}

@media (max-width: 380px) {
  .charts-grid { gap: 0.6rem; }
  .chart-canvas-wrap { height: 100px; }
  .topbar { padding: 0.4rem 0.5rem; }
}
