:root {
  color-scheme: light dark;
  font-family: "Microsoft YaHei", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body,
.app {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: #eef1f6;
  color: #172033;
}

.app[data-theme="dark"],
.app[data-theme="auto"] {
  --bg: #111827;
  --panel: rgba(17, 24, 39, 0.88);
  --panel-solid: #151e2e;
  --text: #f8fafc;
  --muted: #9aa8bd;
  --line: rgba(148, 163, 184, 0.26);
  --accent: #18a0fb;
  --accent-strong: #0577d4;
  --danger: #ef4444;
  --canvas: #0b1120;
}

.app[data-theme="light"] {
  --bg: #eef1f6;
  --panel: rgba(255, 255, 255, 0.9);
  --panel-solid: #ffffff;
  --text: #172033;
  --muted: #637083;
  --line: rgba(99, 112, 131, 0.24);
  --accent: #0969da;
  --accent-strong: #0553b1;
  --danger: #d92d20;
  --canvas: #f7f9fc;
}

@media (prefers-color-scheme: light) {
  .app[data-theme="auto"] {
    --bg: #eef1f6;
    --panel: rgba(255, 255, 255, 0.9);
    --panel-solid: #ffffff;
    --text: #172033;
    --muted: #637083;
    --line: rgba(99, 112, 131, 0.24);
    --accent: #0969da;
    --accent-strong: #0553b1;
    --danger: #d92d20;
    --canvas: #f7f9fc;
  }
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.login-view,
.viewer-view {
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

.login-view {
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-solid);
  box-shadow: 0 20px 80px rgba(15, 23, 42, 0.18);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 13px;
}

.login-panel h1 {
  margin: 0 0 24px;
  font-size: 28px;
  letter-spacing: 0;
}

.field {
  display: grid;
  gap: 8px;
  color: var(--muted);
}

.field input {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
}

.primary-button,
.secondary-button,
.text-button,
.icon-button,
.tool-button {
  border-radius: 6px;
}

.primary-button {
  min-height: 38px;
  padding: 0 14px;
  color: #fff;
  background: var(--accent);
}

.primary-button:hover {
  background: var(--accent-strong);
}

.secondary-button {
  min-height: 36px;
  padding: 0 12px;
  color: var(--text);
  border: 1px solid var(--line);
  background: transparent;
}

.text-button {
  min-height: 34px;
  padding: 0 10px;
  color: var(--muted);
  background: transparent;
}

.icon-button,
.tool-button {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  color: var(--text);
  border: 1px solid var(--line);
  background: var(--panel);
}

#loginSubmit {
  width: 100%;
  margin-top: 18px;
}

.form-error {
  min-height: 20px;
  margin: 12px 0 0;
  color: var(--danger);
}

.viewer-view {
  position: relative;
}

.topbar {
  position: absolute;
  z-index: 20;
  top: 0;
  left: 0;
  right: 0;
  height: 58px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: max(10px, env(safe-area-inset-top)) 16px 8px;
  background: linear-gradient(to bottom, var(--bg), transparent);
  pointer-events: none;
}

.topbar > * {
  pointer-events: auto;
}

.topbar h1 {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 17px;
  font-weight: 650;
  text-align: center;
  letter-spacing: 0;
}

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.stage {
  position: absolute;
  inset: 0;
}

.viewer-canvas {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--canvas);
  touch-action: none;
}

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

.viewer-message,
.load-overlay {
  position: absolute;
  z-index: 8;
  left: 50%;
  top: 50%;
  width: min(320px, calc(100vw - 40px));
  transform: translate(-50%, -50%);
  display: grid;
  gap: 10px;
  justify-items: center;
  padding: 18px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  text-align: center;
}

.viewer-message span,
.load-overlay span {
  color: var(--muted);
  font-size: 13px;
}

.meter {
  width: 100%;
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.25);
}

.meter i {
  display: block;
  width: 0%;
  height: 100%;
  background: var(--accent);
  transition: width 0.18s ease;
}

.tool-strip {
  position: absolute;
  z-index: 18;
  right: 18px;
  top: 50%;
  transform: translateY(-50%);
  display: grid;
  gap: 8px;
}

.tool-button.active {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent);
}

.floating-panel,
.manager-drawer {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  backdrop-filter: blur(18px);
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.22);
}

.floating-panel {
  position: absolute;
  z-index: 19;
  right: 72px;
  bottom: 22px;
  width: 280px;
  padding: 14px;
  border-radius: 8px;
}

.panel-title,
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.switch-row,
.range-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  color: var(--muted);
}

.range-row input {
  min-width: 0;
  flex: 1;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-top: 14px;
}

.segment {
  min-height: 34px;
  color: var(--text);
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 6px;
}

.segment.active {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent);
}

#sectionInvertBtn {
  width: 100%;
  margin-top: 14px;
}

.weld-info {
  left: 18px;
  right: auto;
  bottom: 22px;
}

.weld-info p {
  margin: 12px 0 0;
  color: var(--muted);
  line-height: 1.6;
  word-break: break-word;
}

.manager-drawer {
  position: absolute;
  z-index: 30;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(380px, 92vw);
  display: grid;
  grid-template-rows: auto auto auto 1fr;
  gap: 14px;
  padding: calc(18px + env(safe-area-inset-top)) 16px 18px;
  transform: translateX(-105%);
  transition: transform 0.2s ease;
}

.manager-drawer.open {
  transform: translateX(0);
}

.upload-card {
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
}

.upload-card.dragging {
  border-color: var(--accent);
  background: rgba(24, 160, 251, 0.08);
}

.upload-card strong {
  color: var(--text);
}

.drawer-actions {
  display: grid;
  grid-template-columns: 90px 1fr;
  gap: 10px;
}

.model-list {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding-right: 2px;
}

.model-card {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.04);
}

.model-card.active {
  border-color: var(--accent);
}

.model-card h3 {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  letter-spacing: 0;
}

.model-meta {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
}

.model-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.danger-button {
  min-height: 34px;
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
  background: transparent;
  border-radius: 6px;
}

.toast {
  position: absolute;
  z-index: 40;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  max-width: min(480px, calc(100vw - 40px));
  padding: 10px 14px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-solid);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.22);
}

.axis-helper {
  position: absolute;
  z-index: 7;
  right: 18px;
  bottom: 18px;
  width: 72px;
  height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  pointer-events: none;
}

.axis-helper span {
  position: absolute;
  font-size: 12px;
  font-weight: 700;
}

.axis-x {
  right: 11px;
  top: 30px;
  color: #ef4444;
}

.axis-y {
  left: 31px;
  top: 7px;
  color: #22c55e;
}

.axis-z {
  left: 13px;
  bottom: 11px;
  color: #3b82f6;
}

@media (max-width: 760px) {
  .topbar {
    height: 62px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .text-button {
    display: none;
  }

  .tool-strip {
    left: 50%;
    right: auto;
    top: auto;
    bottom: calc(14px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    display: flex;
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
  }

  .floating-panel {
    left: 10px;
    right: 10px;
    bottom: calc(78px + env(safe-area-inset-bottom));
    width: auto;
  }

  .axis-helper {
    right: 10px;
    bottom: calc(88px + env(safe-area-inset-bottom));
  }

  .manager-drawer {
    width: 100vw;
  }

  .model-actions {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
