/* Anatomy Mirror — style.css
   Dark, mobile-first, no build step. All screens and the live stage
   are styled here; JS modules only set positioning for label pills. */

/* ---------- Tokens & base ---------- */
:root {
  --bg: #0b1020;
  --bg-raised: #141b31;
  --bg-card: #1a2340;
  --text: #eef1ff;
  --text-dim: #a8b0cc;
  --accent: #4dd0c4;
  --accent-strong: #2bb3a6;
  --accent-text: #062a27;
  --line: rgba(238, 241, 255, 0.16);
  --label-line: rgba(214, 231, 255, 0.65);
  --danger: #ff8a8a;
  --radius: 14px;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

h1,
h2,
p,
ol,
ul {
  margin: 0;
}

ul,
ol {
  padding: 0;
  list-style: none;
}

button,
input {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Buttons (min 48px tap targets) ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 48px;
  min-width: 48px;
  padding: 0.75rem 1.25rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn:active {
  transform: scale(0.98);
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn-primary:active {
  background: var(--accent-strong);
}

.btn-secondary {
  background: var(--bg-card);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}

.btn-icon {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 50%;
  background: rgba(11, 16, 32, 0.72);
  font-size: 1.25rem;
  line-height: 1;
}

.btn-toggle {
  flex: 1 1 0;
  padding: 0.75rem 0.25rem;
  background: rgba(20, 27, 49, 0.92);
  color: var(--text-dim);
  font-size: 0.95rem;
  white-space: nowrap;
}

.btn-toggle.on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

/* ---------- Screens (general) ---------- */
.screen {
  min-height: 100dvh;
}

.screen-live {
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Screen: Start ---------- */
.start-inner {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  max-width: 34rem;
  margin: 0 auto;
  padding: calc(2.5rem + env(safe-area-inset-top)) 1.25rem
    calc(2rem + env(safe-area-inset-bottom));
}

.app-title {
  font-size: 2.5rem;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.explainer {
  font-size: 1.125rem;
  color: var(--text-dim);
}

.steps {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  counter-reset: step;
}

.steps li {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  padding: 0.875rem 1rem;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.step-num {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}

.start-actions {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.start-actions .btn {
  width: 100%;
  min-height: 56px;
  font-size: 1.0625rem;
}

.footer-note {
  color: var(--text-dim);
  font-size: 0.875rem;
  text-align: center;
}

/* ---------- Screen: Calibrate ---------- */
.calibrate-inner {
  display: flex;
  flex-direction: column;
  gap: 1.125rem;
  max-width: 34rem;
  margin: 0 auto;
  padding: calc(1.75rem + env(safe-area-inset-top)) 1.25rem
    calc(1.75rem + env(safe-area-inset-bottom));
}

.screen-title {
  font-size: 1.75rem;
  line-height: 1.2;
}

.instructions {
  color: var(--text-dim);
  font-size: 1.0625rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.field label {
  font-weight: 600;
}

.field input {
  min-height: 56px;
  padding: 0.75rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
  font-size: 1.125rem;
}

.field input::placeholder {
  color: var(--text-dim);
}

.status-text {
  min-height: 1.45em;
  color: var(--text-dim);
}

.status-text:empty {
  display: none;
}

/* Calibrate progress bar: #cal-progress is the track, its fill is
   #cal-progress-fill / .progress-fill (JS sets the fill width). */
.progress,
#cal-progress {
  height: 10px;
  border-radius: 999px;
  background: var(--bg-card);
  border: 1px solid var(--line);
  overflow: hidden;
}

.progress-fill,
#cal-progress-fill {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 0.25s ease;
}

.measurements-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.measurements-list:empty {
  display: none;
}

.measurements-list li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.calibrate-inner .btn {
  width: 100%;
}

/* ---------- Screen: Live — stage stack ---------- */
#stage {
  position: relative;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #000;
}

#video,
#demo-img,
#gl-canvas,
#labels-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#video {
  z-index: 0;
  object-fit: cover;
}

#demo-img {
  z-index: 1;
  object-fit: cover;
}

#gl-canvas {
  z-index: 2;
  display: block;
}

#labels-layer {
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
}

/* Mirror flip: applied to the video and the GL canvas only.
   #labels-layer must NOT be CSS-flipped — labels.js flips the label
   coordinates itself in JS, so flipping the layer here as well would
   double-flip the labels and put them on the wrong side. */
#stage.mirror #video,
#stage.mirror #gl-canvas {
  transform: scaleX(-1);
}

/* ---------- Labels (created by labels.js) ---------- */
.labels-svg,
#labels-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.labels-svg line,
#labels-svg line,
.labels-svg .bone-leader,
#labels-svg .bone-leader,
line.bone-leader {
  stroke: var(--label-line);
  stroke-width: 1.5;
}

.bone-label {
  position: absolute;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: rgba(11, 16, 32, 0.78);
  border: 1px solid var(--label-line);
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  pointer-events: none;
}

/* ---------- Live: top bar ---------- */
.top-bar {
  position: absolute;
  z-index: 10;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: calc(0.625rem + env(safe-area-inset-top))
    calc(0.75rem + env(safe-area-inset-right)) 0.625rem
    calc(0.75rem + env(safe-area-inset-left));
  pointer-events: none;
}

.top-bar .btn,
.top-bar .live-status {
  pointer-events: auto;
}

.live-status {
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  background: rgba(11, 16, 32, 0.72);
  color: var(--text);
  font-size: 0.9rem;
}

.live-status:empty {
  display: none;
}

/* ---------- Live: control bar ---------- */
.control-bar {
  position: fixed;
  z-index: 10;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 0.5rem;
  padding: 0.625rem calc(0.75rem + env(safe-area-inset-right))
    calc(0.625rem + env(safe-area-inset-bottom))
    calc(0.75rem + env(safe-area-inset-left));
  background: linear-gradient(transparent, rgba(11, 16, 32, 0.88) 30%);
}

/* ---------- Live: proportions slide-over panel ---------- */
.panel,
#panel-proportions {
  position: absolute;
  z-index: 20;
  top: calc(4.5rem + env(safe-area-inset-top));
  right: 0;
  bottom: calc(5.25rem + env(safe-area-inset-bottom));
  width: min(320px, 85vw);
  padding: 1rem;
  overflow-y: auto;
  background: rgba(20, 27, 49, 0.97);
  border: 1px solid var(--line);
  border-right: none;
  border-radius: var(--radius) 0 0 var(--radius);
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.45);
  animation: panel-in 0.22s ease;
}

@keyframes panel-in {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(0);
  }
}

.panel-title {
  font-size: 1.125rem;
  margin-bottom: 0.75rem;
}

.panel-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.panel-list li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.625rem 0.75rem;
  background: var(--bg-card);
  border-radius: 10px;
}

.panel-hint {
  margin-top: 0.875rem;
  color: var(--text-dim);
  font-size: 0.85rem;
}

/* ---------- Live: camera error ---------- */
.live-error {
  position: absolute;
  z-index: 30;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(22rem, calc(100vw - 2.5rem));
  padding: 1.25rem;
  text-align: center;
  background: rgba(20, 27, 49, 0.97);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
}

.live-error-text {
  color: var(--danger);
}

/* ---------- Larger screens ---------- */
@media (min-width: 640px) {
  .app-title {
    font-size: 3rem;
  }

  .start-actions {
    flex-direction: row;
  }

  .start-actions .btn {
    flex: 1 1 0;
    width: auto;
  }

  .bone-label {
    font-size: 0.85rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn:active {
    transform: none;
  }

  .progress-fill,
  #cal-progress-fill {
    transition: none;
  }

  .panel,
  #panel-proportions {
    animation: none;
  }
}

/* ---------- Calibrate camera preview (added at integration) ---------- */
.cal-preview {
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 42dvh;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  margin: 0 0 16px;
}
.cal-preview video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1); /* selfie preview feels natural mirrored */
  display: block;
}

/* ---------- Phase 1: A4 paper measuring + head layers ---------- */
.cal-preview {
  position: relative;
}

/* Paper flow shows the WHOLE frame (letterboxed) so the sheet and
   the user's head/feet are all visible and tappable. */
.cal-preview.show-full video {
  object-fit: contain;
}

#paper-overlay {
  position: absolute;
  inset: 0;
  cursor: crosshair;
  touch-action: manipulation;
}

#paper-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#paper-polygon {
  fill: rgba(45, 212, 191, 0.15);
  stroke: #2dd4bf;
  stroke-width: 2;
}

.paper-dot {
  position: absolute;
  width: 28px;
  height: 28px;
  margin: -14px 0 0 -14px;
  border-radius: 50%;
  background: #2dd4bf;
  border: 2px solid #06281f;
  color: #06281f;
  font-size: 14px;
  font-weight: 700;
  line-height: 24px;
  text-align: center;
  pointer-events: none;
  box-sizing: border-box;
}

.method-switch {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 16px;
}

.paper-steps {
  margin: 0 0 8px;
  padding-left: 20px;
}

.paper-steps li {
  margin: 4px 0;
}

.paper-note {
  opacity: 0.8;
  font-size: 0.92em;
}

.paper-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0;
}

.paper-result {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid #2dd4bf;
  border-radius: 12px;
}

.paper-result-text {
  margin: 0 0 8px;
  font-weight: 600;
}
