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

:root {
  --primary-color: #764ba2;
  --secondary-color: #764ba2;
  --bg-start: #667eea;
  --bg-end: #764ba2;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --ink: #2d3748;
  --muted: #4a5568;
}

body.theme-esp32 {
  --primary-color: #764ba2;
  --secondary-color: #764ba2;
  --bg-start: #667eea;
  --bg-end: #764ba2;
}

body.theme-microbit {
  --primary-color: #3454d1;
  --secondary-color: #3454d1;
  --bg-start: #3454d1;
  --bg-end: #3454d1;
}

body.theme-orange {
  --primary-color: #fb8720;
  --secondary-color: #fb8720;
  --bg-start: #ff9966;
  --bg-end: #ff5e62;
}

body {
  font-family: 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
  line-height: 1.6;
  color: #333;
  background: linear-gradient(135deg, var(--bg-start) 0%, var(--bg-end) 100%);
  min-height: 100vh;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 260px 20px 40px 20px;
}

@media (max-width: 900px) {
  .container {
    padding-top: 200px;
  }
}

@media (max-width: 480px) {
  .container {
    padding-top: 130px;
  }
}

.header-content h1 {
  color: var(--primary-color);
}

.back-btn {
  color: var(--primary-color);
}

.header-top-controls {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 6px;
}

.right-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-toggle-btn {
  position: absolute;
  top: 5px;
  right: 8px;
  z-index: 10;
  background: #eee;
  border-radius: 50%;
  border: none;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.header-toggle-btn::before {
  content: '';
  display: block;
  width: 0;
  height: 0;
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-bottom: 12px solid var(--primary-color);
  transition: transform 0.3s;
}

.header-toggle-btn.closed::before {
  transform: rotate(180deg);
}

.header-title-desc {
  text-align: center;
  margin-top: 0;
}

.header-settings-bar {
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  width: 100%;
  margin: 0 auto;
  padding: 0 0 16px 0;
  background: none;
  border-radius: 16px;
  position: relative;
  gap: 16px;
  max-width: 100%;
}

.header-settings-bar.closed {
  max-height: 0;
  overflow: hidden;
  padding: 0;
  border: none;
}

.header-settings-left,
.header-settings-right {
  width: 32%;
  min-width: 220px;
  height: 185px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  flex-shrink: 0;
}

.header-settings-col {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

.settings-title {
  font-size: 1.1em;
  font-weight: 600;
  text-align: center;
  color: var(--muted);
  margin-bottom: 12px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.device-select-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 6px;
}

.device-select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  min-width: 125px;
  font-size: 1em;
}

.toggle-container {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 6px;
  gap: 12px;
}

.toggle-switch {
  position: relative;
  width: 60px;
  height: 30px;
  background: var(--primary-color);
  transition: background 0.3s;
  border-radius: 15px;
  cursor: pointer;
}

.toggle-switch.active {
  background: linear-gradient(135deg, var(--bg-start) 0%, var(--bg-end) 100%);
}

.toggle-switch.locked {
  opacity: 0.7;
}

.toggle-slider {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.toggle-switch.active .toggle-slider {
  transform: translateX(30px);
}

.toggle-label {
  font-weight: 500;
  color: var(--muted);
  font-size: 0.9em;
}

.nav-overlay.open #overlaySettingsMount {
  width: 70%;
  max-width: 900px;
  padding: 0 16px;
  margin: 88px auto 0 auto;
}

.nav-overlay.open .header-settings-bar {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 16px;
  max-width: 680px;
  margin: 0 auto 24px auto;
  padding-bottom: 24px;
}

.nav-overlay.open .header-settings-bar .header-settings-col {
  width: 100%;
  min-width: 0;
  height: auto;
}

.nav-overlay.open .header-settings-bar .header-settings-left .btn,
.nav-overlay.open .header-settings-bar .header-settings-right .btn {
  width: 70%;
  margin-left: auto;
  margin-right: auto;
}

.main-section {
  background: var(--card-bg);
  border-radius: 16px;
  --section-pad-x: 30px;
  --section-pad-y: 40px;
  padding: var(--section-pad-y) var(--section-pad-x);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  margin-bottom: 20px;
  position: relative;
}

.main-top {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
  gap: 20px;
  margin-bottom: 24px;
}

.data-card h3 {
  font-size: 1.1em;
}

.data-actions {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  gap: 16px;
  margin-top: 6px;
}

.data-block h4 {
  margin-bottom: 6px;
  font-size: 1em;
  color: var(--ink);
}

.data-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.graphs-shell {
  position: relative;
}

.graphs-stage {
  position: relative;
  overflow: hidden;
  border-radius: 18px;
  padding: 4px;
  margin: -4px;
}

.graphs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.graphs-header .btn {
  margin-left: auto;
}

.group-panel-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 20;
}

.group-panel-drawer {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 320px;
  max-width: 80%;
  background: #f8fafc;
  transform: translateX(-110%);
  transition: transform 0.25s ease, opacity 0.2s ease;
  z-index: 30;
  overflow-y: auto;
  padding: 18px 16px 20px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  overscroll-behavior: contain;
}

.group-panel-card {
  background: #fff;
}

.group-panel-controls {
  display: inline-flex;
  gap: 8px;
  flex-wrap: wrap;
}

.main-section.group-panel-open .group-panel-overlay {
  opacity: 1;
  pointer-events: auto;
}

.main-section.group-panel-open .group-panel-drawer {
  transform: translateX(0);
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.main-section.group-panel-open .graphs-list {
  filter: grayscale(0.1);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.45);
  z-index: 2000;
  padding: 16px;
}

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

.modal-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px 26px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.18);
}

.modal-card.modal-wide {
  max-width: 520px;
}

.modal-card h3 {
  margin-bottom: 10px;
  font-size: 1.2em;
}

.modal-card p {
  color: #4a5568;
  margin-bottom: 18px;
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 15px;
}

.modal-list {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-check-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.modal-scroll {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  background: #f1f5f9;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.95em;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.08);
}

.modal-check input {
  width: 16px;
  height: 16px;
  accent-color: var(--primary-color);
}

.modal-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid #e2e8f0;
  background: #fff;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.modal-item:hover {
  border-color: #cbd5e0;
  box-shadow: 0 6px 16px rgba(15, 23, 42, 0.08);
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(0, 280px) minmax(0, 1fr);
  gap: 20px;
  min-width: 0;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.panel-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  max-width: 100%;
  min-width: 0;
  width: 100%;
}

.panel-card h2 {
  color: var(--ink);
  font-size: 1.4em;
  margin-bottom: 10px;
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.graphs-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.graphs-header h2 {
  color: var(--ink);
  font-size: 1.7em;
  margin-bottom: 6px;
}

.graphs-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 700;
  color: #6b7280;
  font-size: 0.78em;
  margin-bottom: 4px;
}

.sub {
  color: var(--muted);
  font-size: 0.98em;
}

.status-badges {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  align-items: stretch;
}

.group-actions {
  display: none;
  margin-top: 10px;
}

.is-editing .group-actions {
  display: flex;
  justify-content: flex-end;
}

.status-chip {
  background: #f7fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.status-chip .label {
  font-size: 0.85em;
  color: #718096;
}

.status-value {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.9em;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.status-connected,
.status-ready {
  background: #c6f6d5;
  color: #22543d;
}

.status-disconnected {
  background: #fed7d7;
  color: #742a2a;
}

.status-connecting,
.status-waiting {
  background: #fef5e7;
  color: #744210;
}

.group-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

.group-item {
  border: 1px dashed #cbd5f5;
  border-radius: 12px;
  padding: 12px;
  background: #f8faff;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}

.group-series {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.group-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 8px;
  gap: 8px;
}

.group-meta {
  font-size: 0.85em;
  color: #718096;
}

.group-name-input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.98em;
  font-weight: 700;
  color: var(--ink);
  background: #fff;
}

.group-name-input[readonly],
.series-label-input[readonly] {
  background: #f7fafc;
  cursor: default;
}

.series-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 6px 0;
  width: 100%;
  min-width: 0;
  position: relative;
}

.series-color {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.series-label-input {
  flex: 1 1 0;
  min-width: 0;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.95em;
  background: #fff;
}

.series-add-btn {
  display: none;
}

.is-editing .series-add-btn {
  display: inline-flex;
}

.group-delete,
.series-delete {
  display: none;
  border: none;
  background: #edf2f7;
  color: #4a5568;
  font-weight: 700;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.is-editing .series-item {
  cursor: grab;
}

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

body.is-touch-dragging {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.touch-drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  transform: translate(-50%, -50%);
  padding: 8px 12px;
  border-radius: 12px;
  background: #111827;
  color: #fff;
  font-size: 0.9em;
  font-weight: 600;
  letter-spacing: 0.01em;
  max-width: 220px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none;
  z-index: 6000;
  opacity: 0.92;
}

.series-item.drop-before::before,
.series-item.drop-after::after {
  content: '';
  position: absolute;
  left: 6px;
  right: 6px;
  height: 2px;
  background: var(--primary-color);
  border-radius: 2px;
}

.series-item.drop-before::before {
  top: -6px;
}

.series-item.drop-after::after {
  bottom: -6px;
}

.group-delete:disabled,
.series-delete:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.is-editing .group-delete,
.is-editing .series-delete {
  display: inline-flex;
}

.helper {
  margin-top: 12px;
  color: #718096;
  font-size: 0.9em;
}

.helper.small {
  font-size: 0.85em;
}

.graph-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  min-width: 0;
  position: relative;
}

.graph-card.dragging {
  opacity: 0.6;
}

.graph-card.drop-before::before,
.graph-card.drop-after::after {
  content: '';
  position: absolute;
  left: 10px;
  right: 10px;
  height: 2px;
  background: var(--primary-color);
  border-radius: 2px;
}

.graph-card.drop-before::before {
  top: -6px;
}

.graph-card.drop-after::after {
  bottom: -6px;
}

.graph-card.folded .graph-body {
  display: none;
}

.graph-card-header:active {
  cursor: grabbing;
}

.graph-card-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 12px;
  cursor: grab;
}

.graph-card-header h3 {
  font-size: 1.1em;
  color: var(--ink);
}

.graph-meta {
  color: #718096;
  font-size: 0.9em;
  margin-top: 4px;
}

.graph-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85em;
  color: #4a5568;
}

.graph-body {
  position: relative;
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #f8fafc;
  height: 260px;
  min-height: 70px;
  resize: none;
  overflow: hidden;
}

.graph-axis {
  position: relative;
  width: var(--graph-axis-width, 52px);
  flex: 0 0 var(--graph-axis-width, 52px);
  background: #f8fafc;
  pointer-events: none;
  order: 0;
}

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

.graph-resize-handle {
  height: 14px;
  margin-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: ns-resize;
  color: #94a3b8;
  font-weight: 700;
  user-select: none;
  touch-action: none;
}

.graph-card.folded .graph-resize-handle::after {
  content: '=';
  font-size: 14px;
}

.graph-resize-handle:hover {
  color: var(--primary-color);
}

.is-resizing {
  cursor: ns-resize;
  user-select: none;
}

.graph-scroll {
  flex: 1 1 auto;
  order: 1;
  width: auto;
  min-width: 0;
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  position: relative;
  z-index: 1;
}

.graph-track {
  height: 1px;
  width: 100%;
  pointer-events: none;
}

.graph-scroll .graph-canvas {
  position: sticky;
  left: 0;
  top: 0;
  display: block;
  height: 100%;
  z-index: 2;
}

.graph-tooltip {
  position: absolute;
  left: 0;
  top: 0;
  min-width: 140px;
  max-width: 220px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #111827;
  color: #fff;
  font-size: 0.85em;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 5;
}

.graph-tooltip.visible {
  opacity: 0.95;
  transform: translateY(0);
}

.graph-tooltip-time {
  font-weight: 700;
  margin-bottom: 6px;
}

.graph-tooltip-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.graph-tooltip-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.graph-tooltip-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.graph-tooltip-label {
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.graph-tooltip-value {
  font-weight: 700;
}

.graph-folded {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 0.92em;
  color: #94a3b8;
  z-index: 3;
}

.graph-card.folded .graph-scroll {
  display: none;
}

.graph-card.folded .graph-folded {
  display: flex;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 8px;
  background: #cbd5e0;
}

.status-indicator.status-connected {
  background: #38a169;
}

.status-indicator.status-connecting {
  background: #f6ad55;
}

.status-indicator.status-disconnected {
  background: #e53e3e;
}

.btn {
  background: var(--primary-color);
  color: #fff;
  border: none;
  padding: 11px 20px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 1em;
  font-weight: 700;
  transition: 0.2s;
  box-shadow: 0 4px 14px rgba(118, 75, 162, 0.22);
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 24px rgba(118, 75, 162, 0.28);
}

.btn-small {
  padding: 8px 14px;
  font-size: 0.92em;
  margin: 0;
}

.btn-secondary {
  background: #eef3f6;
  color: var(--ink);
  box-shadow: none;
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: #e3ecf2;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

@media (max-width: 900px) {
  .dashboard-grid {
    grid-template-columns: 1fr;
  }
  .header-settings-left,
  .header-settings-right {
    width: 100%;
  }
  .main-top {
    grid-template-columns: 1fr;
  }
  .group-panel-drawer {
    width: 80%;
    max-width: 360px;
  }
  .group-panel-header {
    flex-direction: column;
    align-items: flex-start;
  }
  .group-panel-controls {
    width: 100%;
    justify-content: flex-start;
    margin-top: 6px;
  }
  .graph-resize-handle {
    height: 32px;
    margin-top: 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: #eef3f6;
  }
  .graph-resize-handle::after {
    content: '=';
    font-size: 14px;
  }
}

@media (max-width: 515px) {
  .data-actions {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .main-section {
    --section-pad-x: 20px;
    --section-pad-y: 28px;
  }
  .group-panel-drawer {
    width: 86%;
    max-width: 320px;
  }
  .btn {
    font-size: 0.9em;
    padding: 9px 14px;
  }
}
