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

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --ink: #1e293b;
  --muted: #64748b;
  --accent: #0891b2;
  --accent-soft: #ecfeff;
  --accent-ink: #0e7490;
  --muscle: #e11d48;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 8px 30px rgba(15, 23, 42, 0.08);
  --font: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', system-ui, sans-serif;
  --header-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
}

#app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
  height: 100dvh;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: calc(0.65rem + var(--safe-top)) 1rem 0.65rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.brand-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.brand h1 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.brand p {
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.search-box {
  position: relative;
}

.search-box input {
  width: 200px;
  padding: 0.5rem 0.75rem 0.5rem 2rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--ink);
  font-size: 0.82rem;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-box input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(8, 145, 178, 0.12);
  background: var(--surface);
}

.search-box input::placeholder {
  color: var(--muted);
}

.search-icon {
  position: absolute;
  left: 0.6rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 0.75rem;
  pointer-events: none;
}

.search-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  max-height: 240px;
  overflow-y: auto;
  display: none;
  z-index: 200;
}

.search-dropdown.open {
  display: block;
}

.search-item {
  padding: 0.6rem 0.75rem;
  font-size: 0.82rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
}

.search-item:last-child {
  border-bottom: none;
}

.search-item:hover {
  background: var(--accent-soft);
}

.search-item small {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  margin-top: 2px;
}

.btn {
  padding: 0.5rem 0.85rem;
  min-height: 40px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
  touch-action: manipulation;
}

.btn:hover {
  background: var(--surface2);
  border-color: var(--border-strong);
}

.btn.active {
  background: var(--accent-soft);
  border-color: rgba(8, 145, 178, 0.35);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn-group {
  display: flex;
  gap: 2px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 3px;
  border: 1px solid var(--border);
}

.btn-group .btn {
  border: none;
  background: transparent;
  min-height: 36px;
  padding: 0.4rem 0.65rem;
}

.btn-group .btn.active {
  background: var(--surface);
  box-shadow: var(--shadow);
}

/* Main layout */
.main {
  display: grid;
  grid-template-columns: 1fr 380px;
  overflow: hidden;
  min-height: 0;
}

.canvas-wrap {
  position: relative;
  overflow: hidden;
  min-height: 0;
  background: var(--bg);
}

#canvas3d {
  width: 100%;
  height: 100%;
  touch-action: none;
}

#canvas3d canvas {
  display: block;
}

.canvas-overlay {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  max-width: calc(100% - 1rem);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.canvas-hint {
  position: absolute;
  top: 0.75rem;
  left: 0.75rem;
  font-size: 0.72rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.92);
  padding: 0.35rem 0.65rem;
  border-radius: 20px;
  border: 1px solid var(--border);
  pointer-events: none;
}

.hover-label {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  font-size: 0.82rem;
  color: var(--accent-ink);
  background: rgba(255, 255, 255, 0.95);
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(8, 145, 178, 0.25);
  opacity: 0;
  transform: translateY(-6px);
  transition: all 0.2s;
  pointer-events: none;
  max-width: calc(100% - 1.5rem);
}

.hover-label.visible {
  opacity: 1;
  transform: translateY(0);
}

.symptom-bar {
  position: absolute;
  top: 2.5rem;
  left: 0;
  right: 0;
  display: flex;
  gap: 0.35rem;
  padding: 0 0.75rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  z-index: 10;
}

.symptom-bar::-webkit-scrollbar {
  display: none;
}

.chip {
  flex-shrink: 0;
  padding: 0.35rem 0.65rem;
  border-radius: 20px;
  font-size: 0.72rem;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
  color: var(--muted);
  cursor: pointer;
  transition: all 0.12s;
  touch-action: manipulation;
}

.chip:hover,
.chip.active {
  color: var(--accent-ink);
  border-color: rgba(8, 145, 178, 0.35);
  background: var(--accent-soft);
}

/* Info panel */
.info-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.panel-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 1.5rem;
  text-align: center;
  color: var(--muted);
}

.panel-empty .empty-icon {
  font-size: 2.5rem;
  margin-bottom: 0.75rem;
  opacity: 0.45;
}

.panel-empty h3 {
  font-size: 1rem;
  color: var(--ink);
  margin-bottom: 0.4rem;
}

.panel-empty p {
  font-size: 0.82rem;
  line-height: 1.65;
}

.panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.1rem;
  display: none;
  -webkit-overflow-scrolling: touch;
}

.panel-content.visible {
  display: block;
}

.panel-content::-webkit-scrollbar {
  width: 4px;
}

.panel-content::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
}

.muscle-header {
  margin-bottom: 0.85rem;
}

.muscle-header h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.15rem;
}

.latin {
  font-size: 0.78rem;
  color: var(--muted);
  font-style: italic;
  margin-bottom: 0.5rem;
}

.meta-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.65rem;
}

.tag {
  font-size: 0.68rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  border: 1px solid rgba(8, 145, 178, 0.15);
}

.tag.difficulty {
  background: #fffbeb;
  color: var(--warning);
  border-color: rgba(217, 119, 6, 0.2);
}

.description {
  font-size: 0.82rem;
  line-height: 1.7;
  color: var(--muted);
  margin-bottom: 1rem;
}

.method-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-bottom: 0.85rem;
  background: var(--surface2);
  padding: 4px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.method-tab {
  padding: 0.45rem 0.2rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 0.7rem;
  cursor: pointer;
  transition: all 0.12s;
  text-align: center;
  touch-action: manipulation;
}

.method-tab .tab-icon {
  display: block;
  font-size: 0.95rem;
  margin-bottom: 2px;
}

.method-tab:hover {
  color: var(--ink);
  background: rgba(255, 255, 255, 0.7);
}

.method-tab.active {
  background: var(--surface);
  color: var(--accent-ink);
  font-weight: 600;
  box-shadow: var(--shadow);
}

.action-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem;
}

.action-card h4 {
  font-size: 0.92rem;
  margin-bottom: 0.65rem;
  color: var(--ink);
}

.action-steps {
  list-style: none;
}

.action-steps li {
  font-size: 0.8rem;
  line-height: 1.65;
  color: var(--muted);
  padding: 0.4rem 0 0.4rem 1.85rem;
  position: relative;
  border-bottom: 1px solid var(--border);
}

.action-steps li:last-child {
  border-bottom: none;
}

.action-steps li::before {
  content: counter(step);
  counter-increment: step;
  position: absolute;
  left: 0;
  top: 0.42rem;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 0.65rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.action-steps {
  counter-reset: step;
}

.action-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

.duration {
  font-size: 0.75rem;
  color: var(--success);
}

.difficulty-dots {
  display: flex;
  gap: 4px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--border);
}

.dot.on {
  background: var(--warning);
}

.disclaimer {
  margin-top: 0.85rem;
  padding: 0.6rem 0.7rem;
  border-radius: var(--radius-sm);
  background: #fef2f2;
  border: 1px solid #fecaca;
  font-size: 0.72rem;
  color: #b91c1c;
  line-height: 1.55;
}

.panel-footer {
  padding: 0.65rem 1rem calc(0.65rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  font-size: 0.68rem;
  color: var(--muted);
  text-align: center;
  background: var(--surface2);
}

.loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  background: var(--bg);
  z-index: 50;
  transition: opacity 0.4s;
}

.loader-text {
  font-size: 0.82rem;
  color: var(--muted);
}

.loader.hidden {
  opacity: 0;
  pointer-events: none;
}

.loader-ring {
  width: 40px;
  height: 40px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.anim-bar {
  margin-bottom: 0.85rem;
}

.btn-play {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn-play:hover:not(:disabled) {
  background: var(--accent-ink);
  border-color: var(--accent-ink);
}

.btn-play:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-stop {
  border-color: #fecaca;
  color: var(--danger);
}

.btn-play-full {
  width: 100%;
  padding: 0.7rem 1rem;
  font-size: 0.88rem;
}

.anim-hint {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.35rem;
  line-height: 1.5;
}

.action-steps li.active-step {
  color: var(--ink);
  background: var(--accent-soft);
  border-radius: 6px;
  margin: 0 -0.2rem;
  padding-left: 1.85rem;
  padding-right: 0.35rem;
}

.action-steps li.active-step::before {
  background: var(--accent);
  color: #fff;
}

/* Exercise modal */
.exercise-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(15, 23, 42, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(0.75rem, var(--safe-top)) 0.75rem max(0.75rem, var(--safe-bottom));
}

.exercise-modal.hidden {
  display: none;
}

.exercise-modal-inner {
  width: min(900px, 100%);
  max-height: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
}

.exercise-modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.exercise-modal-tag {
  font-size: 0.68rem;
  color: var(--accent);
  letter-spacing: 0.04em;
  margin-bottom: 0.15rem;
}

.exercise-modal-header h2 {
  font-size: 1.05rem;
  font-weight: 700;
}

.exercise-modal-muscle {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.exercise-close {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}

.exercise-close:hover {
  background: #fef2f2;
  border-color: #fecaca;
  color: var(--danger);
}

.exercise-stage {
  position: relative;
  flex: 1;
  min-height: 360px;
  background: #f0f4f8;
}

#exerciseCanvas {
  width: 100%;
  height: 100%;
  min-height: 360px;
}

#exerciseCanvas canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}

.exercise-legend {
  position: absolute;
  top: 0.65rem;
  right: 0.65rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.68rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.92);
  padding: 0.45rem 0.6rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  max-width: calc(100% - 8rem);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.dot-stretch {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ea580c;
  display: inline-block;
  flex-shrink: 0;
}

.dot-dim {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(212, 100, 111, 0.45);
  display: inline-block;
  flex-shrink: 0;
}

.exercise-phase-badge {
  position: absolute;
  top: 0.65rem;
  left: 0.65rem;
  padding: 0.3rem 0.7rem;
  border-radius: 20px;
  background: var(--accent-soft);
  border: 1px solid rgba(8, 145, 178, 0.25);
  color: var(--accent-ink);
  font-size: 0.75rem;
  font-weight: 600;
}

.exercise-step-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 1rem 1rem 0.85rem;
  background: linear-gradient(transparent, rgba(255, 255, 255, 0.96) 35%);
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
}

.exercise-step-overlay .step-num {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  display: grid;
  place-items: center;
}

.exercise-step-overlay p {
  font-size: 0.88rem;
  line-height: 1.6;
  color: var(--ink);
  padding-top: 0.15rem;
}

.exercise-modal-footer {
  padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.exercise-progress-wrap {
  height: 3px;
  background: var(--border);
  border-radius: 3px;
  margin-bottom: 0.65rem;
  overflow: hidden;
}

.exercise-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.12s linear;
}

.exercise-controls {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.exercise-controls .btn {
  flex: 1;
  min-width: 88px;
}

/* ── Tablet ── */
@media (max-width: 960px) {
  .main {
    grid-template-columns: 1fr;
    grid-template-rows: min(48vh, 420px) 1fr;
  }

  .info-panel {
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .canvas-hint {
    display: none;
  }
}

/* ── Mobile ── */
@media (max-width: 640px) {
  .header {
    flex-wrap: wrap;
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  .brand p {
    display: none;
  }

  .header-actions {
    width: 100%;
    order: 3;
  }

  .search-box {
    flex: 1;
  }

  .search-box input {
    width: 100%;
  }

  .main {
    grid-template-rows: 42vh 1fr;
  }

  .symptom-bar {
    top: auto;
    bottom: 3.25rem;
  }

  .canvas-overlay {
    bottom: 0.65rem;
  }

  .btn-group .btn {
    font-size: 0.72rem;
    padding: 0.35rem 0.5rem;
  }

  .method-tabs {
    grid-template-columns: repeat(2, 1fr);
  }

  .panel-content {
    padding: 0.85rem;
  }

  .exercise-modal {
    padding: 0;
    align-items: stretch;
  }

  .exercise-modal-inner {
    width: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
  }

  .exercise-stage {
    min-height: 0;
    flex: 1;
  }

  #exerciseCanvas {
    min-height: 0;
  }

  .exercise-legend {
    font-size: 0.62rem;
    top: auto;
    bottom: 5.5rem;
    right: 0.5rem;
    left: 0.5rem;
    max-width: none;
  }

  .exercise-step-overlay {
    padding: 0.75rem;
  }

  .exercise-step-overlay p {
    font-size: 0.82rem;
  }

  .exercise-controls {
    flex-direction: column;
  }

  .exercise-controls .btn {
    width: 100%;
  }
}

@media (max-width: 380px) {
  .brand h1 {
    font-size: 0.9rem;
  }

  #xrayBtn {
    padding: 0.5rem 0.6rem;
    font-size: 0.72rem;
  }
}
