:root {
  --bg: #0f172a; --surface: #1e293b; --surface2: #273548;
  --border: #334155; --text: #e2e8f0; --muted: #64748b; --dim: #475569;
  --accent: #38bdf8; --green: #34d399; --green-bg: rgba(52,211,153,0.15);
  --red: #f87171; --yellow: #fbbf24;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); }

/* Custom scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--dim); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
* { scrollbar-width: thin; scrollbar-color: var(--dim) transparent; }

/* --- Header --- */
.header { display: flex; align-items: center; gap: 1rem; padding: 1rem 1.5rem; border-bottom: 1px solid var(--border); }
.header h1 { font-size: 1.3rem; color: var(--accent); white-space: nowrap; }
.header .tabs { display: flex; gap: 0.25rem; margin-left: 1rem; }
.header .tabs button {
  padding: 0.4rem 1rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); border-radius: 0.375rem; cursor: pointer; font-size: 0.85rem;
}
.header .tabs button.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.header-components-btn {
  margin-left: auto; padding: 0.3rem 0.7rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--accent); border-radius: 0.375rem; cursor: pointer; font-size: 1.1rem; font-weight: bold; line-height: 1;
}
.header-components-btn:hover { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.pill { display: inline-block; padding: 0.2rem 0.6rem; border-radius: 9999px; font-size: 0.75rem; margin-left: 0.5rem; }
.pill.ok { background: #065f46; color: #6ee7b7; }
.pill.warn { background: #78350f; color: #fcd34d; }
.pill.err { background: #7f1d1d; color: #fca5a5; }

/* --- Dashboard --- */
#dashboard { padding: 1rem; height: calc(100vh - 57px); overflow-y: auto; }

/* Desktop tab bar */
.dash-tab-bar {
  display: flex; gap: 0.3rem; margin-bottom: 0.75rem;
}
.dash-tab {
  padding: 0.3rem 0.8rem; border: 1px solid var(--border);
  background: var(--surface); color: var(--dim); border-radius: 0.35rem;
  font-size: 0.8rem; cursor: pointer;
}
.dash-tab:hover { color: var(--text); border-color: var(--dim); }
.dash-tab.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.dash-tab-add { color: var(--accent); border-style: dashed; background: none; }
#dash-empty { text-align: center; color: var(--muted); padding: 3rem; }

/* Tile grid container */
#tile-grid {
  position: relative; min-height: calc(100vh - 120px);
  background-image: radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 20px 20px;
}

/* Tile — absolutely positioned */
.tile {
  position: absolute; padding: 0.75rem 1rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 0.5rem;
  display: flex; flex-direction: column; overflow: hidden;
  cursor: grab; user-select: none;
}
.tile:hover { border-color: var(--dim); }
.tile:active { cursor: grabbing; }

/* Resize handle */
.tile-resize-handle {
  position: absolute; bottom: 0; right: 0; width: 14px; height: 14px;
  cursor: se-resize; opacity: 0; transition: opacity 0.2s;
  background: linear-gradient(135deg, transparent 50%, var(--dim) 50%);
  border-radius: 0 0 0.5rem 0;
}
.tile:hover .tile-resize-handle { opacity: 0.6; }
.tile.flash .value { color: var(--green); transition: color 0.3s; }
.tile .label { font-size: 0.7rem; color: var(--fg, var(--text)); margin-bottom: 0.25rem; word-break: break-all; }
.tile .value { font-size: 1.8rem; font-weight: 700; color: var(--text); flex-shrink: 0; }
.tile .value.no-data { color: var(--red); }
.incomplete-tag {
  display: inline-block; font-size: 0.55rem; color: var(--red); border: 1px solid var(--red);
  border-radius: 0.2rem; padding: 0 0.3rem; margin-left: 0.3rem; vertical-align: middle;
}
.tile .unit { font-size: 0.85rem; color: var(--dim); margin-left: 0.2rem; }
/* Stale indicator: no data update in 5s */
.tile.stale { border-color: var(--red); }
.tile.stale .value, .tile.stale .label { color: var(--red); }
.composite-item.stale .composite-item-value, .composite-item.stale .composite-item-label { color: var(--red); }
.composite-item.stale { border: 1px solid var(--red); }
.tile .remove {
  position: absolute; top: 0.4rem; right: 0.5rem; background: none; border: none;
  color: var(--accent); cursor: pointer; font-size: 0.9rem; line-height: 1; z-index: 1;
  opacity: 0; transition: opacity 0.15s;
}
.tile:hover .remove { opacity: 1; }
.tile .remove:hover { color: var(--red); }
/* Tile options gear */
.tile .tile-options-btn {
  position: absolute; top: 0.4rem; right: 3rem; background: none; border: none;
  color: var(--accent); cursor: pointer; font-size: 0.65rem; line-height: 1; z-index: 1;
  opacity: 0; transition: opacity 0.15s; letter-spacing: 1px;
}
.tile:hover .tile-options-btn { opacity: 1; }
.tile .tile-options-btn:hover { color: var(--text); }
/* Tile options popover */
.tile-options-popover {
  position: absolute; top: 1.6rem; right: 0.5rem; z-index: 20;
  background: var(--surface2); border: 1px solid var(--border); border-radius: 0.375rem;
  padding: 0.5rem 0.65rem; min-width: 140px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3); font-size: 0.72rem;
}
.tile-options-popover label {
  display: flex; align-items: center; gap: 0.4rem; cursor: pointer;
  color: var(--text); padding: 0.2rem 0; user-select: none;
}
.tile-options-popover label input { accent-color: var(--accent); }
/* Segmented toggle pill */
.seg-toggle {
  display: inline-flex; border: 1px solid var(--border); border-radius: 0.375rem;
  overflow: hidden; font-size: 0.8rem; font-weight: 600;
}
.seg-option {
  padding: 0.25rem 0.6rem; cursor: pointer; border: none; background: transparent;
  color: var(--dim); transition: all 0.15s; white-space: nowrap;
}
.seg-option:not(:last-child) { border-right: 1px solid var(--border); }
.seg-option:hover { background: var(--surface2); }
.seg-option.active { background: var(--accent); color: var(--bg); }
.seg-option.active.on { background: var(--green); }
.seg-option.active.off { background: var(--red); color: var(--bg); }
.seg-toggle-val { margin-left: 0.5rem; font-size: 1.1rem; font-weight: 600; }

/* Command pending indicator — pulses until server responds */
.cmd-pending { animation: cmd-pulse 0.6s ease-in-out infinite alternate; }
@keyframes cmd-pulse {
  from { opacity: 1; }
  to { opacity: 0.35; }
}

/* Numeric writable control */
.numeric-control {
  display: inline-flex; align-items: center; gap: 0.15rem;
}
.numeric-btn {
  width: 24px; height: 24px; padding: 0; border: 1px solid var(--border); border-radius: 0.25rem;
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 0.75rem;
  display: flex; align-items: center; justify-content: center; line-height: 1;
}
.numeric-btn:hover { background: var(--surface2); }
.numeric-btn:active { background: var(--accent); color: var(--bg); }
.numeric-input {
  width: 3.5rem; text-align: center; padding: 0.15rem 0.25rem; border: 1px solid var(--border);
  border-radius: 0.25rem; background: var(--bg); color: var(--text); font-size: 0.95rem;
  font-weight: 600; appearance: textfield; -moz-appearance: textfield;
}
.numeric-input::-webkit-outer-spin-button,
.numeric-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.numeric-input:focus { outline: none; border-color: var(--accent); }

/* Drop zones (shown when dragging tile over another) */
.drop-zone {
  position: absolute; z-index: 90; pointer-events: none;
  border: 2px dashed transparent; border-radius: 0.375rem;
  transition: background 0.1s, border-color 0.1s;
  background: rgba(100,100,255,0.05);
}
.drop-zone.active {
  background: rgba(100,100,255,0.18);
  border-color: var(--accent);
}

/* Composite tiles */
.tile.composite { padding: 0.5rem 0.6rem; }
.composite-item {
  text-align: center; padding: 0.25rem;
  border-radius: 0.25rem; background: rgba(255,255,255,0.03);
  cursor: grab;
}
.composite-item:active { cursor: grabbing; }
.composite-item { position: relative; }
.composite-item-label {
  font-size: 0.6rem; color: var(--text); margin-bottom: 0.15rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.composite-item.sub-drop-target { outline: 1px solid var(--accent); border-radius: 0.25rem; }
.sub-drop-indicator { position: absolute; background: var(--accent); border-radius: 2px; z-index: 10; pointer-events: none; }
.sub-drop-left { left: -3px; top: 0; width: 3px; height: 100%; }
.sub-drop-right { right: -3px; top: 0; width: 3px; height: 100%; }
.sub-drop-top { top: -3px; left: 0; width: 100%; height: 3px; }
.sub-drop-bottom { bottom: -3px; left: 0; width: 100%; height: 3px; }
.composite-item-value { font-size: 1rem; font-weight: 600; }

/* Merge ghost preview (shown in drop zone when dragging) */
.tile.drag-hidden { opacity: 0 !important; pointer-events: none; }
.merge-ghost {
  position: absolute; z-index: 95; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(100,100,255,0.15); border: 2px solid var(--accent);
  border-radius: 0.375rem; padding: 0.3rem;
  transition: left 0.08s, top 0.08s, width 0.08s, height 0.08s;
}
.merge-ghost .composite-item-label { font-size: 0.6rem; color: var(--fg); margin-bottom: 0.1rem; }
.merge-ghost .composite-item-value { font-size: 0.85rem; font-weight: 600; color: var(--fg); }

/* Full tile ghost when dragging sub-tile out of composite */
.drag-ghost-tile {
  position: fixed !important; z-index: 200 !important; pointer-events: none !important;
  opacity: 0.85; box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

/* Inline drop zones (inside composite during sub-tile rearrange) */
.drop-zone-inline {
  position: absolute; z-index: 10; pointer-events: none;
  border: 2px dashed transparent; border-radius: 0.25rem;
  transition: background 0.1s, border-color 0.1s;
}
.drop-zone-inline[data-zone="left"] { left: 0; top: 0; width: 33%; height: 100%; }
.drop-zone-inline[data-zone="right"] { right: 0; top: 0; width: 33%; height: 100%; }
.drop-zone-inline[data-zone="top"] { left: 0; top: 0; width: 100%; height: 33%; }
.drop-zone-inline[data-zone="bottom"] { left: 0; bottom: 0; width: 100%; height: 33%; }
.drop-zone-inline.active {
  background: rgba(100,100,255,0.15);
  border-color: var(--accent);
}

/* Generic tile detail panel */
.tile-detail-toggle {
  position: absolute; top: 0.4rem; right: 1.8rem;
  font-size: 0.7rem; color: var(--accent); cursor: pointer;
  user-select: none; line-height: 1;
  opacity: 0; transition: opacity 0.15s;
}
.tile:hover .tile-detail-toggle { opacity: 1; }
.tile-detail-toggle:hover { text-decoration: underline; }
.tile-detail {
  margin-top: 0.3rem; border-top: 1px solid var(--border);
  padding-top: 0.3rem; display: flex; flex-direction: column; gap: 0.15rem;
  overflow-y: auto; font-size: 0.7rem;
}
.detail-row { display: flex; gap: 0.5rem; }
.detail-label { color: var(--muted); min-width: 50px; }

/* --- Topic tree (shared) --- */
.search-input {
  flex: 1; padding: 0.5rem 0.75rem; background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.375rem; color: var(--text); font-size: 0.85rem; outline: none;
}
.search-input:focus { border-color: var(--accent); }
.search-input::placeholder { color: var(--dim); }
.topic-count { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }

.tree-container { flex: 1; overflow-y: auto; padding: 0.5rem; }
.tree-node { user-select: none; }
.tree-row {
  display: flex; align-items: center; gap: 0.4rem; padding: 0.2rem 0.4rem;
  border-radius: 0.25rem; cursor: pointer; font-size: 0.82rem;
  white-space: nowrap;
}
.tree-row:hover { background: var(--surface2); }
.tree-row .arrow { width: 1rem; text-align: center; color: var(--dim); font-size: 0.7rem; flex-shrink: 0; }
.tree-row .arrow.empty { visibility: hidden; }
.tree-row .node-label { color: var(--muted); }
.tree-row .node-value { color: var(--text); font-family: monospace; font-size: 0.78rem; margin-left: auto; padding-left: 1rem; }
.tree-row .flash-dot {
  width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0;
  transition: background 0.3s;
}
.tree-row .flash-dot.active { background: var(--green); }
.tree-row.selected { border-left: 3px solid var(--green); background: rgba(74,222,128,0.08); }
.tree-row.selected .node-label { color: var(--green); }
.tree-children { margin-left: 1rem; }
.tree-children.collapsed { display: none; }
.tree-row.search-hidden { display: none; }

/* --- Settings --- */
.settings-section {
  margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border);
}
.settings-section .section-title {
  font-size: 0.75rem; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.05em; margin-bottom: 0.5rem;
}
.setting-group { margin-bottom: 0.6rem; }
.setting-group label {
  display: block; font-size: 0.75rem; color: var(--dim); margin-bottom: 0.2rem;
}
.setting-group select {
  width: 100%; padding: 0.4rem 0.5rem; background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.375rem; color: var(--text); font-size: 0.8rem; outline: none;
}
.setting-group select:focus { border-color: var(--accent); }
.soc-preview {
  background: var(--surface2); border-radius: 0.375rem; padding: 0.5rem;
  font-size: 0.8rem; text-align: center; margin-top: 0.5rem;
}
.soc-preview .soc-val { font-size: 1.4rem; font-weight: 700; color: var(--green); }
.soc-preview .soc-label { font-size: 0.65rem; color: var(--dim); }

/* --- Devices tab --- */
#devices { display: none; height: calc(100vh - 57px); }
.devices-layout { display: flex; height: 100%; }
.devices-sidebar {
  width: 240px; border-right: 1px solid var(--border); padding: 1rem;
  display: flex; flex-direction: column; gap: 0.5rem; flex-shrink: 0; overflow-y: auto;
}
.sidebar-label {
  font-size: 0.75rem; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.05em; margin-bottom: 0.25rem;
}
.device-tree-edit {
  font-size: 0.7rem; padding: 0.15rem 0.5rem; background: var(--surface);
  border: 1px solid var(--border); color: var(--muted); border-radius: 0.25rem; cursor: pointer;
}
.device-tree-edit:hover { border-color: var(--accent); color: var(--text); }
.server-btn {
  padding: 0.45rem 0.75rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); border-radius: 0.375rem; cursor: pointer; text-align: left; font-size: 0.8rem;
  display: block; width: 100%; margin-bottom: 0.25rem;
}
.server-btn:hover { border-color: var(--accent); color: var(--text); }
.server-btn.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.driver-type-btn {
  padding: 0.6rem 0.75rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--muted); border-radius: 0.375rem; cursor: pointer; text-align: left; font-size: 0.85rem;
  display: flex; align-items: center; gap: 0.5rem; width: 100%;
}
.driver-type-btn:hover { border-color: var(--accent); color: var(--text); }
.driver-type-btn.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.driver-type-btn .scan-badge {
  font-size: 0.6rem; padding: 0.1rem 0.35rem; border-radius: 9999px;
  background: rgba(56,189,248,0.15); color: var(--accent); margin-left: auto;
}
.driver-type-btn.active .scan-badge { background: rgba(0,0,0,0.2); color: var(--bg); }

.devices-main { flex: 1; display: flex; flex-direction: column; overflow-y: auto; padding: 1rem 1.5rem; }
.devices-header { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.devices-header h2 { font-size: 1rem; color: var(--text); }
.btn {
  padding: 0.45rem 1rem; border-radius: 0.375rem; cursor: pointer; font-size: 0.85rem;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
}
.btn:hover { color: var(--text); border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { opacity: 0.9; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: rgba(248,113,113,0.1); border-color: var(--red); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.scan-status { font-size: 0.8rem; color: var(--muted); }
.scan-status .spinner { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--dim); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; margin-right: 0.4rem; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Existing devices list */
.device-table { width: 100%; margin-bottom: 1.5rem; }
.device-table th {
  text-align: left; font-size: 0.7rem; color: var(--dim); text-transform: uppercase;
  letter-spacing: 0.05em; padding: 0.4rem 0.75rem; border-bottom: 1px solid var(--border);
}
.device-table td {
  padding: 0.5rem 0.75rem; font-size: 0.85rem; border-bottom: 1px solid var(--border);
}
.device-table tr:hover td { background: var(--surface2); }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 0.4rem; }
.status-dot.on { background: var(--green); }
.status-dot.off { background: var(--red); }
.status-dot.connecting { background: var(--yellow); animation: pulse-dot 1s ease-in-out infinite; }
.status-dot.disabled { background: var(--dim); }

/* Toggle switch */
.toggle-switch { position: relative; display: inline-block; width: 32px; height: 18px; flex-shrink: 0; cursor: pointer; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0; background: var(--border); border-radius: 9px; transition: background 0.2s;
}
.toggle-slider::before {
  content: ""; position: absolute; width: 14px; height: 14px; left: 2px; bottom: 2px;
  background: var(--text); border-radius: 50%; transition: transform 0.2s;
}
.toggle-switch input:checked + .toggle-slider { background: var(--green); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(14px); }

/* Disabled device */
.device-tree-section.device-disabled { opacity: 0.5; }
.device-tree-section.device-disabled .device-tree-header { background: var(--bg); }
@keyframes pulse-dot { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

/* Scan results */
.scan-results { flex: 1; overflow-y: auto; }
.scan-card {
  display: flex; align-items: flex-start; gap: 0.75rem; padding: 0.75rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 0.5rem;
  margin-bottom: 0.5rem; transition: border-color 0.2s;
}
.scan-card:hover { border-color: var(--dim); }
.scan-card.selected { border-color: var(--accent); background: rgba(56,189,248,0.05); }
.scan-card .check {
  width: 20px; height: 20px; border: 2px solid var(--border); border-radius: 0.25rem;
  flex-shrink: 0; cursor: pointer; display: flex; align-items: center; justify-content: center;
  margin-top: 0.15rem; transition: all 0.15s;
}
.scan-card.selected .check { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.scan-card .device-info { flex: 1; min-width: 0; }
.scan-card .device-name { font-weight: 600; font-size: 0.9rem; }
.scan-card .device-addr { font-size: 0.75rem; color: var(--dim); font-family: monospace; }
.scan-card .config-fields { margin-top: 0.5rem; display: flex; flex-direction: column; gap: 0.4rem; }
.scan-card .config-field label { display: block; font-size: 0.7rem; color: var(--muted); margin-bottom: 0.15rem; }
.scan-card .config-field input {
  width: 100%; padding: 0.35rem 0.5rem; background: var(--bg); border: 1px solid var(--border);
  border-radius: 0.25rem; color: var(--text); font-size: 0.8rem; font-family: monospace; outline: none;
}
.scan-card .config-field input:focus,
.scan-card .config-field select:focus { border-color: var(--accent); }
.scan-card .config-field select {
  width: 100%; padding: 0.25rem 0.4rem; background: var(--bg); border: 1px solid var(--border);
  border-radius: 0.25rem; color: var(--text); font-size: 0.8rem; outline: none;
}
.scan-card .config-field input::placeholder { color: var(--dim); }
.scan-empty { text-align: center; color: var(--dim); padding: 2rem; font-size: 0.9rem; }

/* Manual add section */
.manual-form { display: flex; flex-direction: column; gap: 0.6rem; max-width: 400px; }
.manual-form label { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 0.15rem; }
.manual-form input[type="text"],
.manual-form input[type="number"],
.manual-form select {
  width: 100%; padding: 0.4rem 0.6rem; background: var(--bg); border: 1px solid var(--border);
  border-radius: 0.375rem; color: var(--text); font-size: 0.85rem; outline: none;
}
.manual-form input:focus,
.manual-form select:focus { border-color: var(--accent); }
.manual-form input::placeholder { color: var(--dim); }
.manual-form .field-help { font-size: 0.65rem; color: var(--dim); margin-top: 0.1rem; }
.manual-form .checkbox-row { display: flex; align-items: center; gap: 0.5rem; }
.manual-form .checkbox-row input[type="checkbox"] { accent-color: var(--accent); }
.manual-form .checkbox-row span { font-size: 0.85rem; color: var(--text); }
.form-error { color: var(--red); font-size: 0.8rem; }

/* Device topic tree sections (Devices tab) */
.devices-global-search { margin-bottom: 0.75rem; }
.devices-global-search .search-input { width: 100%; }
.device-sections { display: flex; flex-direction: column; gap: 0.15rem; margin-bottom: 1.5rem; }
.device-tree-section {
  border: 1px solid var(--border); border-radius: 0.5rem; overflow: hidden;
}
.device-tree-header {
  display: flex; align-items: center; gap: 0.5rem; padding: 0.5rem 0.75rem;
  cursor: pointer; font-size: 0.85rem; font-weight: 500;
  color: var(--text); user-select: none; background: var(--surface);
}
.device-tree-header:hover { background: var(--surface2); }
.device-tree-header .arrow { width: 1rem; text-align: center; color: var(--dim); font-size: 0.7rem; flex-shrink: 0; }
.device-tree-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-tree-meta { color: var(--dim); font-size: 0.7rem; white-space: nowrap; }
.device-tree-rm { padding: 0.15rem 0.4rem !important; font-size: 0.65rem !important; flex-shrink: 0; }
.device-tree-body { padding: 0.25rem 0.5rem 0.5rem 1.5rem; border-top: 1px solid var(--border); }

/* Compact topic tree variant */
.topic-tree-compact .tree-row { font-size: 0.78rem; padding: 0.15rem 0.3rem; }
.topic-tree-compact .tree-row .node-value { font-size: 0.72rem; }
.topic-tree-toolbar { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.4rem; }
.topic-tree-count { font-size: 0.7rem; color: var(--dim); white-space: nowrap; }


/* Mode switcher (group aggregator) */
.tile-mode-switcher {
  display: flex; gap: 2px; margin: 0.2rem 0 0.3rem;
}
.tile-mode-switcher .mode-btn {
  background: none; border: 1px solid var(--border); color: var(--dim);
  padding: 0.05rem 0.35rem; font-size: 0.55rem; border-radius: 0.2rem;
  cursor: pointer; line-height: 1.2;
}
.tile-mode-switcher .mode-btn:hover { color: var(--fg); border-color: var(--dim); }
.tile-mode-switcher .mode-btn.active {
  background: var(--accent); color: var(--bg); border-color: var(--accent);
}

/* Aggregator mode */
.agg-info { color: var(--dim); font-size: 0.6rem; }
.agg-members {
  display: flex; flex-direction: column; gap: 0.15rem;
}
.agg-member {
  display: flex; align-items: center; gap: 0.4rem; font-size: 0.7rem;
  padding: 0.1rem 0;
}
.agg-member.dim { opacity: 0.5; }
.agg-member-name { color: var(--muted); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agg-member-val { white-space: nowrap; }
.agg-member-val.no-data { color: var(--red); }
.agg-member.flash .agg-member-val { color: var(--green); transition: color 0.3s; }
.composite-item.flash .composite-item-value { color: var(--green); transition: color 0.3s; }
.combined-source.flash .combined-source-val { color: var(--green); transition: color 0.3s; }

/* Tile rename input */
.tile-rename-input {
  background: var(--surface2); border: 1px solid var(--accent); color: var(--text);
  font-size: 0.8rem; padding: 0.15rem 0.3rem; border-radius: 0.25rem;
  width: 100%; outline: none;
}

/* Combine popup */
.combine-popup {
  display: none; position: fixed; z-index: 1000;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0.5rem; padding: 0.75rem; min-width: 180px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.combine-title { font-size: 0.85rem; font-weight: 600; margin-bottom: 0.25rem; }
.combine-subtitle { font-size: 0.7rem; color: var(--muted); margin-bottom: 0.5rem; }
.combine-ops { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-bottom: 0.5rem; }
.combine-ops button {
  flex: 1; min-width: 70px; padding: 0.35rem 0.5rem; border: 1px solid var(--border);
  background: var(--surface2); color: var(--text); border-radius: 0.3rem;
  cursor: pointer; font-size: 0.75rem;
}
.combine-ops button:hover { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.combine-cancel {
  width: 100%; padding: 0.3rem; border: none; background: none;
  color: var(--muted); cursor: pointer; font-size: 0.7rem;
}
.combine-cancel:hover { color: var(--text); }

/* ==========================================================================
   Mobile Dashboard
   ========================================================================== */

.mobile-grid {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem;
}

/* Override absolute positioning — tiles flow in flex container */
.mobile-grid .tile {
  position: static;
  width: 100% !important;
  height: auto !important;
  left: auto !important;
  top: auto !important;
  overflow: visible;
  cursor: default;
  user-select: auto;
}
.mobile-grid .tile:active { cursor: default; }

/* Hide desktop-only elements */
.mobile-grid .tile-resize-handle { display: none; }
.mobile-grid .tile .remove { display: none; }

/* Tighter padding on mobile */
.mobile-grid .tile { padding: 0.5rem 0.75rem; }
.mobile-grid .tile .value { font-size: 1.4rem; }
.mobile-grid .tile .label { font-size: 0.65rem; }
.mobile-grid .tile .unit { font-size: 0.75rem; }

/* Composite group: flex-wrap, items fill rows naturally */
.mobile-grid .composite-grid {
  flex-wrap: wrap !important;
  flex-direction: row !important;
}
.mobile-grid .composite-row {
  display: contents; /* dissolve row wrappers — items flow as siblings */
}
/* Group sub-tiles: grow to fill row, wrap when needed, never shrink below content */
.mobile-grid .composite-grid > .composite-item,
.mobile-grid .composite-grid .composite-item {
  flex: 1 1 auto !important;
  min-width: min-content !important;
}
/* Allow labels to wrap on mobile instead of truncating */
.mobile-grid .composite-item-label {
  white-space: normal; overflow: visible; text-overflow: clip;
}

/* Aggregator expanded members: keep vertical list on mobile */
.mobile-grid .agg-members {
  flex-direction: column !important;
}
.mobile-grid .agg-member {
  max-width: none !important;
  min-width: 0 !important;
  flex: 1 1 auto !important;
}

/* Mobile tab bar */
.mobile-tab-bar {
  display: flex; gap: 0.25rem; padding: 0.4rem 0.5rem;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.mobile-tab {
  flex: 0 0 auto; padding: 0.3rem 0.75rem; border: 1px solid var(--border);
  background: var(--surface); color: var(--dim); border-radius: 0.35rem;
  font-size: 0.75rem; cursor: pointer; white-space: nowrap;
}
.mobile-tab.active { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.mobile-tab-add { color: var(--accent); border-style: dashed; background: none; }

/* Mobile drag-to-reorder */
.mobile-dragging { opacity: 0.85; box-shadow: 0 8px 24px rgba(0,0,0,0.4); transform: scale(1.03); }
.mobile-drag-placeholder {
  border: 2px dashed var(--accent); border-radius: 0.5rem;
  background: rgba(99,102,241,0.08); transition: height 0.15s ease;
}

/* Mobile body: no overflow, fill viewport */
body.mobile #dashboard { padding: 0; height: calc(100vh - 48px); overflow-y: auto; -webkit-overflow-scrolling: touch; }
body.mobile header.header { padding: 0.4rem 0.75rem; }

