/* ======================================================
   Med3D Viewer - Professional Medical Imaging Browser
   ====================================================== */

/* ----- Design Tokens ----- */
:root {
  --color-bg: #f0f2f5;
  --color-surface: #ffffff;
  --color-surface-hover: #f7f8fa;
  --color-surface-active: #eef2f9;
  --color-border: #e4e7ed;
  --color-border-light: #ebeef3;
  --color-text: #1d2129;
  --color-text-secondary: #6b7280;
  --color-text-tertiary: #9ca3af;
  --color-accent: #1a6ff5;
  --color-accent-hover: #3d8af7;
  --color-accent-light: rgba(26, 111, 245, 0.08);
  --color-accent-glow: rgba(26, 111, 245, 0.18);
  --color-danger: #ef4444;
  --color-success: #22c55e;
  --color-warning: #f59e0b;
  --color-lung: #f97316;
  --color-dark-bg: #0a1628;
  --color-dark-surface: #0f1d33;
  --radius-sm: 4px;
  --radius: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.10);
  --shadow-xl: 0 12px 48px rgba(0,0,0,0.14);
  --transition-fast: 0.15s ease;
  --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Helvetica Neue", sans-serif;
}

/* ----- Reset ----- */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ----- 3D Viewport ----- */
#viewer-container {
  position: fixed; inset: 0; z-index: 1;
}
#viewer-container canvas { display: block; }

/* =============================================
   Top Header Bar
   ============================================= */
#header-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 30;
  height: 44px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  background: linear-gradient(180deg, rgba(255,255,255,0.95) 0%, rgba(255,255,255,0.88) 100%);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--color-border-light);
  display: none;
}
#header-bar .header-brand {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600; color: var(--color-text);
}
#header-bar .header-brand .brand-icon {
  width: 26px; height: 26px; border-radius: 6px;
  background: var(--color-accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
}
#header-bar .header-info {
  display: flex; align-items: center; gap: 16px; font-size: 12px;
}
#header-bar .header-info-item {
  display: flex; align-items: center; gap: 4px; color: var(--color-text-secondary);
}
#header-bar .header-info-item strong { color: var(--color-text); font-weight: 600; }

/* =============================================
   Top Toolbar
   ============================================= */
#toolbar {
  position: fixed; top: 56px; left: 50%; transform: translateX(-50%);
  z-index: 20; display: none; gap: 0;
  background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: var(--radius-xl); padding: 4px;
  box-shadow: var(--shadow-lg);
  flex-wrap: wrap; justify-content: center;
}
.tool-group {
  display: flex; gap: 0; padding: 0 4px;
  border-right: 1px solid var(--color-border-light);
}
.tool-group:first-child { padding-left: 2px; }
.tool-group:last-child { border-right: none; padding-right: 2px; }
.tool-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; width: 50px; height: 46px;
  background: transparent; border: none; border-radius: var(--radius);
  color: var(--color-text-secondary); cursor: pointer;
  font-size: 10px; transition: all var(--transition-fast);
  position: relative; font-family: var(--font);
}
.tool-btn:hover { background: var(--color-surface-hover); color: var(--color-text); }
.tool-btn:active { transform: scale(0.95); }
.tool-btn.active {
  background: var(--color-accent); color: #fff;
  box-shadow: 0 2px 8px var(--color-accent-glow);
}
.tool-btn svg { width: 18px; height: 18px; }
.tool-btn .tool-label { font-size: 10px; line-height: 1; font-weight: 500; }

/* =============================================
   Left Panel: Structure Tree (always visible)
   ============================================= */
#left-panel {
  position: fixed; left: 0; top: 0; bottom: 0; z-index: 15;
  width: 90px; background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform var(--transition);
  box-shadow: 2px 0 20px rgba(0,0,0,0.04);
}
#left-panel.open { transform: translateX(0); }

#left-panel .panel-header {
  display: flex; align-items: center; justify-content: center;
  padding: 8px 4px; border-bottom: 1px solid var(--color-border-light);
  font-size: 14px; font-weight: 600; color: var(--color-text);
  background: linear-gradient(180deg, var(--color-surface-hover) 0%, var(--color-surface) 100%);
  flex-shrink: 0;
}
#left-panel .panel-header span {
  display: none; /* Hide "模型列表" text in narrow mode */
}
#left-panel .panel-close--mobile {
  display: none; /* Close via backdrop in narrow mode */
}
.panel-header-icon {
  width: 18px; height: 18px; color: var(--color-accent); flex-shrink: 0;
}

#structure-tree {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 6px;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}
#structure-tree::-webkit-scrollbar { width: 5px; }
#structure-tree::-webkit-scrollbar-track { background: transparent; }
#structure-tree::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

/* ----- Tree Nodes ----- */
.tree-node { user-select: none; }
.tree-row {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px; border-radius: var(--radius);
  cursor: pointer; transition: background var(--transition-fast);
  margin-bottom: 1px;
}
.tree-row:hover { background: var(--color-accent-light); }

.tree-arrow {
  width: 16px; height: 16px; display: flex; align-items: center; justify-content: center;
  font-size: 8px; color: var(--color-text-tertiary); flex-shrink: 0;
  transition: transform var(--transition-fast);
}
.tree-arrow.expanded { transform: rotate(90deg); }
.tree-children { padding-left: 0; }

/* Color dot indicator */
.tree-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
  box-shadow: 0 0 4px rgba(0,0,0,0.2);
  transition: transform var(--transition-fast);
}
.tree-node .item-name {
  font-size: 13px; color: var(--color-text); flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Action buttons (eye + focus) */
.item-actions {
  display: flex; gap: 2px; opacity: 0; transition: opacity var(--transition-fast);
}
.tree-row:hover .item-actions { opacity: 1; }

.item-action-btn {
  width: 28px; height: 28px; border-radius: var(--radius-sm); border: none;
  background: transparent; color: var(--color-text-tertiary);
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; transition: all var(--transition-fast);
  padding: 0;
}
.item-action-btn:hover { background: var(--color-accent-light); color: var(--color-accent); }
.item-action-btn.hidden { opacity: 0.3; }
.item-action-btn svg { width: 16px; height: 16px; }

/* Eye icon specific */
.eye-btn .eye-icon { display: block; }
.eye-btn.hidden .eye-on { display: none; }
.eye-btn.hidden .eye-off { display: block; }

/* Focus button specific */
.focus-btn:hover { color: var(--color-lung); }

.tree-empty {
	  padding: 24px; text-align: center; color: var(--color-text-tertiary); font-size: 13px;
}

/* =============================================
   Organ Buttons Grid (replaces structure tree)
   ============================================= */
#organ-buttons {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 5px; display: flex;
  flex-direction: column; gap: 5px;
  align-items: center;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}
#organ-buttons::-webkit-scrollbar { width: 5px; }
#organ-buttons::-webkit-scrollbar-track { background: transparent; }
#organ-buttons::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

.organ-btn {
  position: relative;
  width: 80px; height: 80px;
  min-width: 80px; min-height: 80px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-family: var(--font);
  transition: all var(--transition);
  overflow: hidden;
  background: var(--color-surface);
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}

/* State 0: Opaque — solid organ color */
.organ-btn.state-0 {
  background: var(--organ-color);
  border-color: transparent;
  box-shadow: 0 4px 16px color-mix(in srgb, var(--organ-color) 30%, transparent);
  color: #fff;
  text-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

/* State 1: Semi-transparent — left half color, right half white gradient */
.organ-btn.state-1 {
  background: linear-gradient(to right, var(--organ-color) 48%, #ffffff 48%);
  border-color: var(--color-border);
  color: var(--color-text);
  text-shadow: none;
}
.organ-btn.state-1::after {
  content: '';
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 52%;
  background: linear-gradient(to right, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0.95) 30%);
  pointer-events: none;
}

/* State 2: Fully transparent — colorless */
.organ-btn.state-2 {
  background: #f0f1f3;
  border-color: var(--color-border-light);
  color: var(--color-text-tertiary);
  text-shadow: none;
  box-shadow: none;
}

/* Hover & active */
.organ-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.12);
}
.organ-btn.state-2:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.organ-btn:active {
  transform: scale(0.96);
  transition: transform 0.1s ease;
}

/* Organ button label */
.organ-btn .organ-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
  line-height: 1.2;
  z-index: 1;
  transition: all var(--transition);
  text-align: center;
  word-break: keep-all;
}
.organ-btn.state-2 .organ-label {
  color: var(--color-text-tertiary);
}

/* Toggle button for expanding descendant panel (replaces old state badge) */
.organ-toggle-btn {
  position: absolute;
  bottom: 3px; right: 3px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: rgba(255,255,255,0.35);
  border: none;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  line-height: 1;
  padding: 0;
  transition: all var(--transition-fast);
  font-family: var(--font);
}
.organ-toggle-btn:hover {
  background: rgba(255,255,255,0.55);
  transform: scale(1.1);
}
.organ-btn.state-1 .organ-toggle-btn {
  color: var(--color-text);
  background: rgba(0,0,0,0.08);
}
.organ-btn.state-2 .organ-toggle-btn {
  color: var(--color-text-tertiary);
  background: var(--color-border);
}

/* Subtle inner glow ring for state-0 */
.organ-btn.state-0::before {
  content: '';
  position: absolute;
  inset: 3px;
  border-radius: calc(var(--radius) - 3px);
  border: 1px solid rgba(255,255,255,0.15);
  pointer-events: none;
  z-index: 0;
}

/* Empty state */
.organ-buttons-empty {
  width: 100%;
  padding: 16px 6px;
  text-align: center;
  color: var(--color-text-tertiary);
  font-size: 11px;
}

/* =============================================
   Second Column Panel: Descendant Buttons
   ============================================= */
#descendant-panel {
  position: fixed; left: 90px; top: 0; bottom: 0;
  z-index: 14; width: 90px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform var(--transition);
  box-shadow: 2px 0 20px rgba(0,0,0,0.04);
}
#descendant-panel.open { transform: translateX(0); }

#descendant-panel .descendant-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 6px; border-bottom: 1px solid var(--color-border-light);
  font-size: 10px; font-weight: 600; color: var(--color-text);
  background: linear-gradient(180deg, var(--color-surface-hover) 0%, var(--color-surface) 100%);
  flex-shrink: 0; min-height: 36px;
}
#descendant-panel-title {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 1; text-align: center;
}

#descendant-buttons {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: 5px; display: flex;
  flex-direction: column; gap: 5px;
  align-items: center;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}
#descendant-buttons::-webkit-scrollbar { width: 5px; }
#descendant-buttons::-webkit-scrollbar-track { background: transparent; }
#descendant-buttons::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }

/* Descendant buttons slightly smaller */
#descendant-buttons .organ-btn {
  width: 72px; height: 72px;
  min-width: 72px; min-height: 72px;
}
#descendant-buttons .organ-btn .organ-label { font-size: 10px; }

/* Lobe / Segment styling */
.tree-node.lobe > .tree-row { font-weight: 600; }
.tree-node.lobe > .tree-row .item-name { font-size: 14px; color: var(--color-lung); }
.tree-node.segment > .tree-row .item-name { font-size: 13px; }
.tree-node.subsegment > .tree-row .item-name { font-size: 12px; color: var(--color-text-secondary); }
.tree-node.other > .tree-row .item-name { font-size: 13px; font-weight: 500; }

/* =============================================
   Right Panel: Measurements
   ============================================= */
#right-panel {
  position: fixed; right: 0; top: 0; bottom: 0; z-index: 15;
  width: 290px; background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition);
  box-shadow: -2px 0 24px rgba(0,0,0,0.06);
}
#right-panel.open { transform: translateX(0); }
#right-panel .panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--color-border-light);
  font-size: 14px; font-weight: 600; color: var(--color-text);
  background: linear-gradient(180deg, var(--color-surface-hover) 0%, var(--color-surface) 100%);
  flex-shrink: 0;
}
.panel-close {
  background: none; border: none; color: var(--color-text-tertiary); cursor: pointer;
  font-size: 20px; width: 28px; height: 28px; display: flex; align-items: center;
  justify-content: center; border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}
.panel-close:hover { color: var(--color-text); background: var(--color-border); }

/* Mobile close button (hidden on desktop) */
.panel-close--mobile {
  display: none;
  margin-left: auto;
}
.panel-close--mobile svg {
  width: 18px; height: 18px;
}

#measurement-list {
  flex: 1; overflow-y: auto; padding: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}
#measurement-list::-webkit-scrollbar { width: 5px; }
#measurement-list::-webkit-scrollbar-track { background: transparent; }
#measurement-list::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 10px; }
.measurement-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-radius: var(--radius); margin-bottom: 6px;
  background: var(--color-surface-hover); border: 1px solid var(--color-border-light);
  font-size: 13px; transition: all var(--transition-fast);
}
.measurement-item:hover { border-color: var(--color-accent); }
.measurement-item .meas-label { color: var(--color-text-tertiary); font-size: 11px; }
.measurement-item .meas-value { color: var(--color-success); font-weight: 600; font-size: 15px; }
.meas-delete {
  background: none; border: none; color: var(--color-text-tertiary); cursor: pointer;
  font-size: 16px; width: 24px; height: 24px; display: flex; align-items: center;
  justify-content: center; border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}
.meas-delete:hover { color: var(--color-danger); background: rgba(239,68,68,0.08); }

/* =============================================
   View Preset Dropdown
   ============================================= */
#view-preset-menu {
  position: fixed; z-index: 30;
  background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: var(--radius-lg); padding: 4px;
  box-shadow: var(--shadow-lg);
  min-width: 110px;
  animation: panelSlideIn 0.15s ease;
}
@keyframes panelSlideIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}
.view-preset-btn {
  display: block; width: 100%; padding: 8px 16px; border: none;
  background: transparent; color: var(--color-text-secondary);
  font-size: 13px; cursor: pointer; text-align: left;
  border-radius: var(--radius); transition: all var(--transition-fast);
  font-family: var(--font);
}
.view-preset-btn:hover { background: var(--color-accent-light); color: var(--color-accent); }
.view-preset-btn .preset-key {
  display: inline-block; width: 22px; height: 20px; line-height: 20px;
  text-align: center; border-radius: 3px; font-size: 10px; font-weight: 700;
  background: var(--color-surface-hover); color: var(--color-text-secondary);
  margin-right: 8px; border: 1px solid var(--color-border-light);
}

/* =============================================
   Model Selector
   ============================================= */
#model-selector {
  position: fixed; top: 110px; left: 12px; z-index: 25;
  width: 230px; background: var(--color-surface);
  border: 1px solid var(--color-border); border-radius: var(--radius-lg);
  overflow: hidden; box-shadow: var(--shadow-lg);
  display: none;
  animation: panelSlideIn 0.15s ease;
}
#model-selector.open { display: block; }
.model-selector-item {
  padding: 10px 14px; cursor: pointer; font-size: 13px;
  transition: background var(--transition-fast); color: var(--color-text-secondary);
  border-bottom: 1px solid var(--color-border-light);
}
.model-selector-item:last-child { border-bottom: none; }
.model-selector-item:hover { background: var(--color-surface-hover); }
.model-selector-item.active { background: var(--color-accent-light); color: var(--color-accent); }
.model-selector-name { display: block; font-size: 13px; color: var(--color-text); }
.model-selector-meta { display: block; font-size: 11px; color: var(--color-text-tertiary); margin-top: 2px; }

/* =============================================
   Loading Overlay
   ============================================= */
#loading-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--color-dark-bg);
}
.loading-logo {
  width: 56px; height: 56px; border-radius: 14px;
  background: var(--color-accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 700; margin-bottom: 20px;
  box-shadow: 0 4px 20px var(--color-accent-glow);
}
.loading-spinner {
  width: 40px; height: 40px; border: 3px solid rgba(255,255,255,0.08);
  border-top-color: var(--color-accent); border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text {
  margin-top: 16px; color: rgba(255,255,255,0.45); font-size: 13px;
  letter-spacing: 0.5px;
}
.loading-progress-bar {
  width: 220px; height: 3px; background: rgba(255,255,255,0.06);
  border-radius: 2px; margin-top: 14px; overflow: hidden;
}
.loading-progress-fill {
  height: 100%; background: linear-gradient(90deg, var(--color-accent), #5b9cf5);
  border-radius: 2px; transition: width 0.3s ease; width: 0%;
}
.loading-percent {
  margin-top: 8px; color: rgba(255,255,255,0.5); font-size: 12px;
  font-weight: 600; font-variant-numeric: tabular-nums;
}

/* =============================================
   Error Overlay
   ============================================= */
#error-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  background: var(--color-dark-bg);
}
.error-icon {
  width: 64px; height: 64px; border-radius: 50%;
  background: rgba(239,68,68,0.1); color: var(--color-danger);
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; margin-bottom: 20px;
}
.error-title {
  font-size: 18px; color: rgba(255,255,255,0.85); margin-bottom: 8px;
  font-weight: 600;
}
.error-message {
  font-size: 13px; color: rgba(255,255,255,0.4); margin-bottom: 28px;
  text-align: center; max-width: 380px; line-height: 1.6;
}
.error-retry-btn {
  padding: 10px 36px; border: none; border-radius: var(--radius);
  background: var(--color-accent); color: #fff; font-size: 14px;
  cursor: pointer; font-weight: 500; transition: all var(--transition-fast);
  font-family: var(--font);
}
.error-retry-btn:hover { background: var(--color-accent-hover); transform: translateY(-1px); }

/* =============================================
   Admin Panel
   ============================================= */
#admin-panel {
  position: fixed; inset: 0; z-index: 90;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  background: var(--color-dark-bg);
}
.admin-panel-card {
  width: 90%; max-width: 500px; max-height: 80vh;
  background: var(--color-dark-surface); border: 1px solid rgba(255,255,255,0.06);
  border-radius: var(--radius-xl); overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-xl);
}
.admin-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid rgba(255,255,255,0.06);
  font-size: 15px; font-weight: 600; color: rgba(255,255,255,0.85);
}
.admin-panel-body { flex: 1; overflow-y: auto; padding: 12px; }
.admin-search-row { display: flex; gap: 8px; margin-bottom: 12px; }
.admin-refresh-btn {
  padding: 6px 16px; border: 1px solid rgba(255,255,255,0.08); border-radius: var(--radius);
  background: transparent; color: rgba(255,255,255,0.5); cursor: pointer;
  font-size: 13px; transition: all var(--transition-fast); font-family: var(--font);
}
.admin-refresh-btn:hover { border-color: var(--color-accent); color: var(--color-accent); }
.admin-model-list { display: flex; flex-direction: column; gap: 6px; }
.admin-model-item {
  display: flex; align-items: center; gap: 12px; padding: 12px;
  border-radius: var(--radius); cursor: pointer;
  transition: background var(--transition-fast); border: 1px solid transparent;
}
.admin-model-item:hover { background: rgba(255,255,255,0.03); border-color: rgba(255,255,255,0.06); }
.admin-model-left { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.admin-qr-thumb {
  width: 40px; height: 40px; border-radius: var(--radius-sm); object-fit: contain;
  cursor: zoom-in; transition: transform 0.2s;
}
.admin-qr-thumb.expanded {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 200px; height: 200px; z-index: 100; border-radius: var(--radius-lg);
  box-shadow: 0 8px 40px rgba(0,0,0,0.6);
}
.admin-qr-placeholder {
  width: 40px; height: 40px; border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.04); display: flex; align-items: center;
  justify-content: center; font-size: 10px; color: rgba(255,255,255,0.3);
}
.admin-model-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.admin-model-name {
  font-size: 14px; color: rgba(255,255,255,0.8); font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.admin-model-meta { font-size: 11px; color: rgba(255,255,255,0.4); }
.admin-model-btn {
  padding: 6px 16px; border: none; border-radius: var(--radius);
  background: var(--color-accent); color: #fff; cursor: pointer;
  font-size: 13px; white-space: nowrap; transition: all var(--transition-fast);
  font-family: var(--font);
}
.admin-model-btn:hover { background: var(--color-accent-hover); }
.admin-loading, .admin-empty, .admin-error {
  text-align: center; padding: 40px 20px; color: rgba(255,255,255,0.4); font-size: 14px;
}

/* =============================================
   Panel Backdrop (mobile only)
   ============================================= */
#panel-backdrop {
  position: fixed; inset: 0; z-index: 14;
  background: rgba(0, 0, 0, 0.35);
  opacity: 0; visibility: hidden;
  transition: opacity var(--transition), visibility var(--transition);
  -webkit-tap-highlight-color: transparent;
}
#panel-backdrop.visible { opacity: 1; visibility: visible; }

/* =============================================
   Measurement Labels (3D overlay)
   ============================================= */
#label-container {
  position: fixed; inset: 0; z-index: 10; pointer-events: none;
}
.measurement-label {
  position: absolute; background: rgba(0,0,0,0.82); color: var(--color-success);
  padding: 3px 10px; border-radius: 10px; font-size: 11px; font-weight: 600;
  white-space: nowrap; pointer-events: none;
  border: 1px solid rgba(34,197,94,0.3);
  backdrop-filter: blur(4px);
}

/* =============================================
   Responsive - Mobile (max-width: 768px)
   ============================================= */
@media (max-width: 768px) {
  /* ----- Header Bar: compact ----- */
  #header-bar {
    height: 36px; padding: 0 10px;
  }
  #header-bar .header-brand { font-size: 11px; gap: 6px; }
  #header-bar .header-brand .brand-icon { width: 22px; height: 22px; font-size: 11px; }
  #header-bar .header-info { gap: 8px; font-size: 10px; }

  /* ----- Toolbar: move below header, compact ----- */
  #toolbar {
    top: 36px; bottom: auto; left: 0; right: 0; transform: none;
    border-radius: 0; padding: 2px 4px;
    border-left: none; border-right: none; border-bottom: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    z-index: 25;
  }
  .tool-group { padding: 0 2px; }
  .tool-group:first-child { padding-left: 0; }
  .tool-group:last-child { padding-right: 0; }
  .tool-btn {
    width: 40px; height: 38px; gap: 1px;
  }
  .tool-btn svg { width: 16px; height: 16px; }
  .tool-btn .tool-label { font-size: 8px; }

  /* ----- Left Panel: bottom drawer with horizontal buttons ----- */
  #left-panel {
    top: auto; bottom: 114px; left: 0; right: 0;
    width: 100%; height: auto;
    max-height: 100px;
    transform: translateY(100%);
    border-right: none;
    border-top: 1px solid var(--color-border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.10);
    flex-direction: column;
    z-index: 26;
  }
  #left-panel:not(.open) { transform: translateY(100%); }
  #left-panel.open { transform: translateY(0); }

  /* Hide panel header on mobile */
  #left-panel .panel-header { display: none; }

  /* Organ buttons: horizontal scroll row */
  #organ-buttons {
    flex-direction: row;
    overflow-x: auto; overflow-y: hidden;
    padding: 8px 8px; gap: 8px;
    align-items: center;
    flex: 0 0 auto;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }
  #organ-buttons::-webkit-scrollbar { height: 3px; }
  #organ-buttons::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }
  .organ-btn {
    width: 56px; height: 56px;
    min-width: 56px; min-height: 56px;
    border-radius: var(--radius);
    scroll-snap-align: start;
    flex-shrink: 0;
  }
  .organ-btn .organ-label { font-size: 10px; }
  .organ-btn .organ-toggle-btn { font-size: 8px; width: 14px; height: 14px; bottom: 2px; right: 2px; }
  .organ-buttons-empty {
    padding: 12px 8px; font-size: 10px;
    white-space: nowrap;
  }

  /* Structure tree: hidden on mobile (use organ buttons instead) */
  #structure-tree { display: none !important; }

  /* ----- Descendant Panel: slides from left-panel, 5px above buttons ----- */
  #descendant-panel {
    top: auto; bottom: 128px; left: 50%;
    width: fit-content; max-width: calc(100vw - 16px);
    height: auto;
    max-height: 80px;
    transform: translateY(14px) translateX(-50%);
    opacity: 0; visibility: hidden;
    border-right: none;
    border-top: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: 0 -4px 24px rgba(0,0,0,0.10);
    z-index: 27;
    transition: transform 0.3s ease, opacity 0.25s ease, visibility 0.25s;
  }
  #descendant-panel.open {
    transform: translateY(0) translateX(-50%);
    opacity: 1; visibility: visible;
  }

  /* Hide descendant panel header on mobile */
  #descendant-panel .descendant-panel-header { display: none; }
  #descendant-buttons {
    flex-direction: row;
    overflow-x: auto; overflow-y: hidden;
    padding: 6px 8px; gap: 6px;
    align-items: center;
    flex: 0 0 auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    scroll-behavior: smooth;
  }
  #descendant-buttons::-webkit-scrollbar { height: 3px; }
  #descendant-buttons::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }
  #descendant-buttons .organ-btn {
    width: 48px; height: 48px;
    min-width: 48px; min-height: 48px;
    flex-shrink: 0;
  }
  #descendant-buttons .organ-label { font-size: 9px; }

  /* Right Panel: full width overlay */
  #right-panel {
    width: 100%; z-index: 26;
    box-shadow: -4px 0 32px rgba(0,0,0,0.15);
  }

  /* View Preset Menu: below toolbar */
  #view-preset-menu {
    top: 80px; bottom: auto;
    left: 50%; transform: translateX(-50%);
    border-radius: var(--radius-lg);
  }

  /* Model Selector: below toolbar, left aligned */
  #model-selector {
    width: calc(100vw - 24px); left: 12px; top: 80px; bottom: auto;
    max-height: 50vh; overflow-y: auto;
  }
}

/* =============================================
   Responsive - Small phone (max-width: 400px)
   ============================================= */
@media (max-width: 400px) {
  #toolbar { padding: 1px 2px; }
  .tool-btn { width: 36px; height: 34px; }
  .tool-btn svg { width: 14px; height: 14px; }
  .tool-btn .tool-label { font-size: 7px; }
  #header-bar { height: 32px; padding: 0 8px; }
  #header-bar .header-brand { font-size: 10px; }
  /* Organ buttons: compact on small phones */
  #organ-buttons { padding: 4px 6px; gap: 6px; }
  .organ-btn { width: 48px; height: 48px; min-width: 48px; min-height: 48px; }
  .organ-btn .organ-label { font-size: 9px; }
  .organ-btn .organ-toggle-btn { font-size: 7px; width: 12px; height: 12px; }
  /* Descendant panel: extra compact on small phones */
  #descendant-buttons { padding: 4px 6px; gap: 4px; }
  #descendant-buttons .organ-btn {
    width: 42px; height: 42px;
    min-width: 42px; min-height: 42px;
  }
  #descendant-buttons .organ-label { font-size: 8px; }
}