/* 可视化模块样式（倾斜/旋转仪表 + 新拟态进针） + Demo 页面布局 */

:root {
  --viz-bg: #1e2329;
  --viz-surface: #e8ecef;
  --viz-text: #2c333a;
  --viz-muted: #6b7580;
  --viz-track: #3a424c;
  --needle-light: #ffffff;
  --needle-dark: #b8c0c8;
  --font-ui: "Segoe UI", system-ui, -apple-system, sans-serif;
  --demo-bg: #f0f2f5;
  --demo-panel: #ffffff;
  --demo-border: #d5dbe3;
  --demo-accent: #3d7a96;
  --demo-accent-soft: #e4eef3;
  --demo-danger: #a85a4a;
}

/* —— Demo 页面布局 —— */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--demo-bg);
  color: var(--viz-text);
  font-family: var(--font-ui);
}

.demo-app {
  max-width: 1500px;
  margin: 0 auto;
  padding: 12px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100vh;
}

.demo-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 10px 14px;
  background: var(--demo-panel);
  border: 1px solid var(--demo-border);
  border-radius: 10px;
}

.demo-status {
  display: flex;
  align-items: baseline;
  gap: 12px;
  min-width: 0;
}

#statusText {
  font-size: 14px;
  font-weight: 600;
  color: var(--viz-text);
}

.demo-fps {
  font-size: 12px;
  color: var(--viz-muted);
  font-variant-numeric: tabular-nums;
}

.demo-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.demo-actions button {
  appearance: none;
  border: 1px solid var(--demo-border);
  background: #f7f9fb;
  color: var(--viz-text);
  font: inherit;
  font-size: 13px;
  line-height: 1.2;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.demo-actions button:hover {
  background: #eef2f6;
  border-color: #c5ced8;
}

.demo-actions button:active {
  background: #e4e9ef;
}

.demo-actions button.is-active {
  background: var(--demo-accent-soft);
  border-color: #9cbcce;
  color: var(--demo-accent);
  font-weight: 600;
}

.demo-actions button.is-connected {
  border-color: #c9a39a;
  color: var(--demo-danger);
  background: #faf3f1;
}

.demo-workspace {
  display: grid;
  grid-template-columns: minmax(0, 6fr) minmax(0, 4fr);
  gap: 12px;
  align-items: stretch;
  min-height: 0;
}

.demo-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(180px, 240px);
  gap: 12px;
  align-items: stretch;
  overflow: visible;
  min-width: 0;
}

.demo-panel {
  background: var(--demo-panel);
  border: 1px solid var(--demo-border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.demo-panel-needle {
  overflow: visible;
  align-items: stretch;
}

.demo-panel-needle #needleRoot {
  width: 100%;
  overflow: visible;
}

.demo-panel-needle .needle-stage {
  margin: 0;
  width: 100%;
  max-width: none;
}

.demo-panel .viz-ring {
  width: 100%;
  max-width: 220px;
  aspect-ratio: 1;
  height: auto;
}

.demo-metrics {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 0;
}

.metric-card {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px 12px;
  padding: 12px 14px;
  background: var(--demo-panel);
  border: 1px solid var(--demo-border);
  border-radius: 10px;
  flex: 1 1 0;
  align-content: start;
}

.metric {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.metric-label {
  font-size: 11px;
  color: var(--viz-muted);
}

.metric-value {
  font-size: 30px;
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  color: var(--viz-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  margin-top: 10px;
  min-height: 42px;
}

.metric-edit {
  display: flex;
  gap: 4px;
  min-width: 0;
  height: 42px;
  align-items: center;
  margin-top: 10px;
}

.metric-edit input[type="number"] {
  width: 100px;
  max-width: 100%;
  margin: 0;
  padding: 6px 12px;
  border: 1px solid var(--demo-border);
  border-radius: 6px;
  background: #f7f9fb;
  color: var(--viz-text);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}

.metric-edit input[type="number"]:focus {
  outline: none;
  border-color: var(--demo-accent);
  background: #fff;
  box-shadow: 0 0 0 2px var(--demo-accent-soft);
}

.metric-edit input[type="number"]:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.metric-unit {
  font-size: 13px;
  font-weight: 600;
  color: var(--viz-muted);
  flex-shrink: 0;
}

.demo-debug {
  width: 100%;
  padding: 10px 12px 12px;
  background: var(--demo-panel);
  border: 1px solid var(--demo-border);
  border-radius: 10px;
}

.demo-debug[hidden],
.debug-advanced[hidden] {
  display: none !important;
}

.battery-bar {
  position: relative;
  display: block;
  width: 100%;
  height: 18px;
  margin-top: 2px;
  border-radius: 5px;
  background: #e4e9ef;
  overflow: hidden;
}

.battery-bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0%;
  background: var(--demo-accent);
  border-radius: 5px;
  transition: width 0.2s ease;
}

.battery-bar-text {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--viz-text);
  line-height: 1;
}

.demo-debug-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.demo-debug-title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--viz-text);
}

.demo-debug-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.demo-debug-refresh {
  appearance: none;
  border: 1px solid var(--demo-border);
  background: #f7f9fb;
  color: var(--viz-text);
  font: inherit;
  font-size: 12px;
  line-height: 1.2;
  padding: 5px 10px;
  border-radius: 7px;
  cursor: pointer;
}

.demo-debug-refresh:hover {
  background: #eef2f6;
  border-color: #c5ced8;
}

.demo-debug-refresh:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.demo-debug-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px 10px;
  margin: 0;
}

.debug-item {
  min-width: 0;
  padding: 6px 8px;
  background: #f7f9fb;
  border-radius: 7px;
}

.debug-item-wide {
  grid-column: 1 / -1;
}

.debug-item dt {
  margin: 0;
  font-size: 11px;
  color: var(--viz-muted);
}

.debug-item dd {
  margin: 2px 0 0;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--viz-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.debug-item-edit {
  display: flex;
  align-items: baseline;
  gap: 4px;
  overflow: visible;
}

.debug-item-edit input[type="number"] {
  width: 4.5em;
  max-width: 100%;
  margin: 0;
  padding: 2px 6px;
  border: 1px solid var(--demo-border);
  border-radius: 6px;
  background: #fff;
  color: var(--viz-text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}

.debug-item-edit input[type="number"]:focus {
  outline: none;
  border-color: var(--demo-accent);
  box-shadow: 0 0 0 2px var(--demo-accent-soft);
}

.debug-item-edit span {
  font-size: 12px;
  font-weight: 600;
  color: var(--viz-muted);
  flex-shrink: 0;
}

.debug-item dt label {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.debug-item dd:has(.battery-bar) {
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
}

.debug-item-wide dd {
  white-space: normal;
  word-break: break-word;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 500;
}

.demo-log {
  width: 100%;
  margin: 0;
  padding: 10px 12px;
  min-height: 96px;
  max-height: 160px;
  overflow: auto;
  background: #1e2329;
  color: #c5ccd4;
  border-radius: 10px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.demo-footer {
  font-size: 11px;
  color: var(--viz-muted);
  text-align: center;
  padding: 4px 0 8px;
}

@media (max-width: 840px) {
  .demo-workspace {
    grid-template-columns: 1fr;
  }

  .demo-main {
    grid-template-columns: 1fr 1fr;
  }

  .demo-panel-needle {
    grid-column: 1 / -1;
  }

  .metric-card {
    flex: 0 0 auto;
  }
}

@media (max-width: 520px) {
  .demo-app {
    padding: 8px 10px 20px;
  }

  .demo-main {
    grid-template-columns: 1fr;
  }

  .demo-panel-needle {
    grid-column: auto;
  }

  .metric-card {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .demo-actions button {
    flex: 1 1 auto;
  }
}

/* —— Canvas 仪表容器 —— */
.viz-ring {
  position: relative;
  width: 220px;
  height: 220px;
  max-width: 100%;
  background: #ffffff;
  border-radius: 12px;
  overflow: hidden;
}

.viz-ring canvas,
canvas.rotation-ring {
  display: block;
  width: 100%;
  height: 100%;
}

.viz-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-start;
  font-family: var(--font-ui);
}

/* —— 进针舞台 —— */
.needle-stage {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  max-width: none;
  /* 上方需容纳约 50mm 针长（深度区 25mm 的 2 倍） */
  height: 420px;
  overflow: visible;
  background: var(--viz-surface);
  border-radius: 16px;
  font-family: var(--font-ui);
  color: var(--viz-text);
}

.needle-viewport {
  position: absolute;
  left: 48px;
  right: 16px;
  top: 16px;
  bottom: 48px;
  overflow: visible;
}

/* 0mm 线：上方约 2/3 留给 50mm 针身，下方 1/3 为 0–25mm 深度区 */
.needle-skin {
  position: absolute;
  left: 4%;
  right: 4%;
  top: 66.666%;
  height: 2px;
  background: linear-gradient(90deg, transparent, #9aa3ad 10%, #9aa3ad 90%, transparent);
  border-radius: 1px;
  pointer-events: none;
  z-index: 1;
}

/*
 * 针体：轴宽 4px，顶部柄 10px；针尖在元素底边。
 * 高度由 JS 按 50mm 刻度比例设置。
 */
.needle-body {
  position: absolute;
  left: 50%;
  width: 4px;
  margin-left: -2px;
  height: 0;
  transform-origin: 50% 100%;
  border-radius: 2px 2px 1px 1px;
  background: linear-gradient(180deg, #e8ecef 0%, #cfd6de 70%, #9aa3ad 100%);
  box-shadow:
    3px 3px 6px var(--needle-dark),
    -2px -2px 5px var(--needle-light),
    inset 0.5px 0.5px 1px rgba(255, 255, 255, 0.7),
    inset -0.5px -0.5px 1px rgba(140, 150, 160, 0.35);
  z-index: 2;
  will-change: top, height, transform;
  pointer-events: none;
}

/* 针顶手柄 10px */
.needle-body::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -2px;
  width: 10px;
  height: 10px;
  margin-left: -5px;
  border-radius: 3px;
  background: linear-gradient(180deg, #f4f6f8, #d0d7df);
  box-shadow:
    2px 2px 4px var(--needle-dark),
    -1px -1px 3px var(--needle-light);
}

/* 针尖略收 */
.needle-body::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 4px;
  height: 10px;
  margin-left: -2px;
  border-radius: 0 0 2px 2px;
  background: linear-gradient(180deg, #9aa3ad, #6e7680);
}

/* 刻度侧栏：top/height 由 JS 对齐到 0mm→25mm 深度区 */
.needle-scale {
  position: absolute;
  left: 0;
  width: 44px;
  z-index: 3;
  pointer-events: none;
}

.needle-scale-tick {
  position: absolute;
  left: 0;
  right: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
  transform: translateY(-50%);
}

.needle-scale-mark {
  display: block;
  width: 10px;
  height: 1px;
  background: var(--viz-muted);
  margin-left: auto;
}

.needle-scale-text {
  font-size: 11px;
  line-height: 1;
  color: var(--viz-muted);
  min-width: 1.4em;
  text-align: right;
}

.needle-scale-unit {
  position: absolute;
  left: 8px;
  bottom: -22px;
  font-size: 10px;
  color: var(--viz-muted);
}

.needle-depth-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 12px;
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--viz-text);
  z-index: 3;
}

@media (max-width: 720px) {
  .demo-main {
    grid-template-columns: 1fr;
  }

  .viz-ring {
    width: 180px;
    height: 180px;
  }

  .needle-stage {
    height: 360px;
  }
}
