:root {
  --primary: #1565c0;
  --primary-light: #e3f2fd;
  --danger: #e53935;
  --warning: #ff9800;
  --success: #4caf50;
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-dim: #f0f0f0;
  --text: #212121;
  --text-dim: #757575;
  --border: #e0e0e0;
  --hud-bg: rgba(0,0,0,0.92);
  --hud-text: #ffffff;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --mono: 'Consolas', 'SF Mono', 'Courier New', monospace;
}
[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface-dim: #2a2a2a;
  --text: #e0e0e0;
  --text-dim: #9e9e9e;
  --border: #333;
  --hud-bg: rgba(0,0,0,0.95);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

/* ── App Bar ─────────────────────────────────── */
.app-bar {
  display: flex;
  align-items: center;
  padding: 0 8px;
  height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  flex-shrink: 0;
}
.app-bar .title {
  font-size: 16px;
  font-weight: 700;
  color: var(--danger);
  margin-left: 8px;
  flex: 1;
}
.app-bar .status-chip {
  font-size: 11px;
  padding: 2px 10px;
  border-radius: 12px;
  font-weight: 600;
  margin-right: 8px;
}
.app-bar .status-chip.disconnected { background: #eee; color: #999; }
.app-bar .status-chip.test { background: #e8f5e9; color: #2e7d32; }
.app-bar .status-chip.armed { background: #ffebee; color: #c62828; }
.app-bar .status-chip.play { background: #ffebee; color: #c62828; animation: pulse 1s infinite; }
.app-bar .status-chip.pause { background: #fff3e0; color: #e65100; }
.app-bar .status-chip.sim { background: #e8f5e9; color: #2e7d32; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.5} }

.icon-btn {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: none; cursor: pointer;
  border-radius: 50%; color: var(--text-dim);
  font-size: 20px; transition: background 0.15s;
}
.icon-btn:hover { background: var(--surface-dim); }
.icon-btn.active { color: var(--primary); }

/* ── Tab Bar ─────────────────────────────────── */
.tab-bar {
  display: flex;
  height: 36px;
  background: var(--surface-dim);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
}
.tab-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  padding: 0 8px;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}
.tab-btn .tab-icon { font-size: 18px; }

/* ── Main Content ────────────────────────────── */
.main-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── Filter Bar ──────────────────────────────── */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}
.filter-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.filter-chip.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
}
.filter-chip .count {
  background: var(--primary);
  color: white;
  border-radius: 10px;
  padding: 0 6px;
  font-size: 10px;
  font-weight: 700;
}

/* ── Views ───────────────────────────────────── */
.view-container {
  height: 100%;
  overflow: auto;
  display: none;
}
.view-container.active { display: flex; flex-direction: column; }

/* Table View */
.table-wrap {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.data-table th {
  position: sticky;
  top: 0;
  background: var(--surface-dim);
  font-weight: 600;
  text-align: left;
  padding: 6px 10px;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  z-index: 2;
}
.data-table td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table tr:hover td { background: var(--primary-light); }
.data-table tr.pyro td:first-child { border-left: 3px solid #ff5722; }
.data-table tr.dmx td:first-child { border-left: 3px solid #2196f3; }

/* Card View */
.card-grid {
  display: grid;
  gap: 8px;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}
.card-item {
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.card-item .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.card-item .card-module {
  font-weight: 700;
  font-size: 14px;
}
.card-item .card-time {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text-dim);
}
.card-item .card-effect {
  font-size: 13px;
  color: var(--text-dim);
}
.card-item .card-type {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
}
.card-item .card-type.pyro { background: #fff3e0; color: #e65100; }
.card-item .card-type.dmx { background: #e3f2fd; color: #1565c0; }

/* System / Module Grid View */
.module-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px;
  overflow-y: auto;
  flex: 1;
  justify-content: center;
}
.module-widget {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  cursor: pointer;
  transition: all 0.15s;
  min-width: 120px;
}
.module-widget:hover { border-color: var(--primary); box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.module-widget.selected { border-color: var(--primary); background: var(--primary-light); }
.module-widget .mod-header {
  text-align: center;
  font-weight: 700;
  font-size: 12px;
  padding: 2px 0 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.module-widget .mod-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.mod-channels {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
}
.channel-cell {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 600;
  color: white;
  background: #ccc;
}
.channel-cell.loaded { background: var(--success); }
.channel-cell.empty { background: #424242; color: #888; }
.channel-cell.error { background: var(--danger); }
.channel-cell.dmx-ch { background: #2196f3; }

/* Module Detail (phone fullscreen, tablet bottom panel) */
.module-detail {
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-y: auto;
}
.module-detail .detail-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}
.module-detail .detail-header .mod-title {
  font-weight: 700;
  font-size: 16px;
  flex: 1;
}
.detail-rails {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
}
.detail-rail {
  display: flex;
  align-items: center;
  gap: 4px;
}
.rail-label {
  width: 28px;
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  color: var(--text-dim);
}
.rail-channels {
  display: flex;
  gap: 2px;
  flex: 1;
}
.rail-channels .ch {
  flex: 1;
  aspect-ratio: 1;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  color: white;
  min-width: 20px;
  max-width: 48px;
}

/* Map View */
.map-container {
  flex: 1;
  background: #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 16px;
  position: relative;
}
.map-placeholder {
  text-align: center;
}
.map-placeholder .map-icon { font-size: 64px; opacity: 0.3; }

/* Sequence View */
.seq-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.seq-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.15s;
}
.seq-item:hover { border-color: var(--primary); }
.seq-item.active { border-color: var(--primary); background: var(--primary-light); }
.seq-item .seq-name { font-weight: 600; font-size: 15px; }
.seq-item .seq-info { font-size: 12px; color: var(--text-dim); margin-top: 4px; }

/* Safety Zone View */
.sz-list { flex: 1; overflow-y: auto; padding: 8px; }
.sz-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 8px;
}
.sz-item .sz-name { font-weight: 600; font-size: 15px; }
.sz-item .sz-info { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.sz-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  background: #fff3e0;
  color: #e65100;
}

/* ── RUN View ────────────────────────────────── */
.run-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.run-hud {
  flex: 1;
  background: var(--hud-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px;
  color: var(--hud-text);
  overflow: hidden;
}
.run-hud .hud-line1 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  font-family: var(--mono);
  font-size: 14px;
  opacity: 0.8;
}
.run-hud .hud-time {
  font-family: var(--mono);
  font-size: clamp(32px, 12vw, 80px);
  font-weight: 900;
  letter-spacing: -2px;
  line-height: 1;
}
.run-hud .hud-stats {
  display: flex;
  gap: 16px;
  font-family: var(--mono);
  font-size: 14px;
  margin-top: 4px;
}
.run-hud .hud-events {
  width: 100%;
  margin-top: 8px;
  font-family: var(--mono);
  font-size: 12px;
  max-height: 40%;
  overflow: hidden;
}
.run-hud .hud-event {
  display: flex;
  gap: 8px;
  padding: 1px 8px;
  opacity: 0.7;
}
.run-hud .hud-event.next { opacity: 1; font-weight: 600; font-size: 14px; }
.run-hud .hud-event.fired { opacity: 0.35; }

.run-controls {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 24px;
  gap: 8px;
  background: var(--surface);
}
.run-controls .seq-select-btn {
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--surface);
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.run-controls .seq-select-btn.selected {
  border-color: var(--primary);
  background: var(--primary-light);
}
.run-btn {
  width: 100%;
  padding: 18px;
  border: none;
  border-radius: 12px;
  font-size: 24px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: white;
  transition: all 0.15s;
}
.run-btn:active { transform: scale(0.97); }
.run-btn.start { background: var(--danger); }
.run-btn.pause { background: var(--warning); }
.run-btn.resume { background: var(--danger); }
.run-btn.stop { background: var(--success); font-size: 20px; padding: 14px; }
.sim-label {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--warning);
}

/* ── HUD Overlay (fullscreen) ────────────────── */
.hud-overlay {
  position: absolute;
  inset: 0;
  z-index: 200;
  background: var(--hud-bg);
  display: none;
  flex-direction: column;
  padding: 12px 16px;
  color: var(--hud-text);
}
.hud-overlay.visible { display: flex; }
.hud-overlay .hud-close {
  position: absolute;
  top: 8px;
  right: 8px;
  color: white;
  opacity: 0.5;
  font-size: 24px;
  cursor: pointer;
  z-index: 10;
}
.hud-overlay .hud-menu {
  position: absolute;
  top: 8px;
  left: 8px;
  color: white;
  opacity: 0.5;
  font-size: 24px;
  cursor: pointer;
  z-index: 10;
}

/* ── Empty State ─────────────────────────────── */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-dim);
}
.empty-state .empty-icon { font-size: 64px; opacity: 0.3; }
.empty-state .empty-text { font-size: 16px; }
.empty-state .load-btn {
  padding: 14px 32px;
  border: 2px solid var(--primary);
  background: var(--primary);
  color: white;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.empty-state .load-btn:hover { background: #1255a1; }

/* ── Feature Warning ─────────────────────────── */
.feature-note {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: #fff8e1;
  border-bottom: 1px solid #ffe082;
  font-size: 12px;
  color: #f57f17;
  flex-shrink: 0;
}
[data-theme="dark"] .feature-note {
  background: #3e2723;
  border-color: #5d4037;
  color: #ffb74d;
}

/* ── Responsive: Phone ───────────────────────── */
@media (max-width: 599px) {
  .app-bar { height: 42px; }
  .app-bar .title { font-size: 14px; }
  .tab-btn { font-size: 0; gap: 0; } /* icon only */
  .tab-btn .tab-icon { font-size: 22px; }
  .tab-btn .tab-label { display: none; }
  .card-grid { grid-template-columns: 1fr; }
  .module-widget { min-width: 100px; }
  .run-hud .hud-time { font-size: clamp(28px, 14vw, 60px); }
  .run-controls { padding: 12px 16px; }
  .detail-header .mod-title { font-size: 20px !important; }
  .detail-header .status-icon { font-size: 22px !important; }
  .run-btn { font-size: 28px; padding: 20px; }
  .run-controls .seq-select-btn { font-size: 20px; }
  .sim-label { font-size: 32px; }

  /* Phone: hide appbar+tabs when HUD visible or module detail fullscreen */
  body.hud-active .app-bar,
  body.hud-active .tab-bar,
  body.module-detail-active .app-bar,
  body.module-detail-active .tab-bar { display: none; }
}

/* ── Responsive: Tablet ──────────────────────── */
@media (min-width: 600px) {
  .card-grid { grid-template-columns: repeat(2, 1fr); }
  .module-detail { max-height: 340px; }
  .run-hud .hud-time { font-size: clamp(40px, 8vw, 100px); }
  .run-hud .hud-stats { font-size: 18px; }
}
@media (min-width: 900px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ── Unsupported Feature Badges ──────────────── */
.unsupported-badge {
  font-size: 10px;
  background: #eee;
  color: #999;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 4px;
}
[data-theme="dark"] .unsupported-badge {
  background: #333;
  color: #777;
}
